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 mutex_unlock(&sbefifo_ffdc_mutex); 198 } 199 200 int sbefifo_parse_status(struct device *dev, u16 cmd, __be32 *response, 201 size_t resp_len, size_t *data_len) 202 { 203 u32 dh, s0, s1; 204 size_t ffdc_sz; 205 206 if (resp_len < 3) { 207 pr_debug("sbefifo: cmd %04x, response too small: %zd\n", 208 cmd, resp_len); 209 return -ENXIO; 210 } 211 dh = be32_to_cpu(response[resp_len - 1]); 212 if (dh > resp_len || dh < 3) { 213 dev_err(dev, "SBE cmd %02x:%02x status offset out of range: %d/%zd\n", 214 cmd >> 8, cmd & 0xff, dh, resp_len); 215 return -ENXIO; 216 } 217 s0 = be32_to_cpu(response[resp_len - dh]); 218 s1 = be32_to_cpu(response[resp_len - dh + 1]); 219 if (((s0 >> 16) != 0xC0DE) || ((s0 & 0xffff) != cmd)) { 220 dev_err(dev, "SBE cmd %02x:%02x, status signature invalid: 0x%08x 0x%08x\n", 221 cmd >> 8, cmd & 0xff, s0, s1); 222 return -ENXIO; 223 } 224 if (s1 != 0) { 225 ffdc_sz = dh - 3; 226 dev_warn(dev, "SBE error cmd %02x:%02x status=%04x:%04x\n", 227 cmd >> 8, cmd & 0xff, s1 >> 16, s1 & 0xffff); 228 if (ffdc_sz) 229 sbefifo_dump_ffdc(dev, &response[resp_len - dh + 2], 230 ffdc_sz, false); 231 } 232 if (data_len) 233 *data_len = resp_len - dh; 234 235 /* 236 * Primary status don't have the top bit set, so can't be confused with 237 * Linux negative error codes, so return the status word whole. 238 */ 239 return s1; 240 } 241 EXPORT_SYMBOL_GPL(sbefifo_parse_status); 242 243 static int sbefifo_regr(struct sbefifo *sbefifo, int reg, u32 *word) 244 { 245 __be32 raw_word; 246 int rc; 247 248 rc = fsi_device_read(sbefifo->fsi_dev, reg, &raw_word, 249 sizeof(raw_word)); 250 if (rc) 251 return rc; 252 253 *word = be32_to_cpu(raw_word); 254 255 return 0; 256 } 257 258 static int sbefifo_regw(struct sbefifo *sbefifo, int reg, u32 word) 259 { 260 __be32 raw_word = cpu_to_be32(word); 261 262 return fsi_device_write(sbefifo->fsi_dev, reg, &raw_word, 263 sizeof(raw_word)); 264 } 265 266 static int sbefifo_check_sbe_state(struct sbefifo *sbefifo) 267 { 268 __be32 raw_word; 269 u32 sbm; 270 int rc; 271 272 rc = fsi_slave_read(sbefifo->fsi_dev->slave, CFAM_GP_MBOX_SBM_ADDR, 273 &raw_word, sizeof(raw_word)); 274 if (rc) 275 return rc; 276 sbm = be32_to_cpu(raw_word); 277 278 /* SBE booted at all ? */ 279 if (!(sbm & CFAM_SBM_SBE_BOOTED)) 280 return -ESHUTDOWN; 281 282 /* Check its state */ 283 switch ((sbm & CFAM_SBM_SBE_STATE_MASK) >> CFAM_SBM_SBE_STATE_SHIFT) { 284 case SBE_STATE_UNKNOWN: 285 return -ESHUTDOWN; 286 case SBE_STATE_IPLING: 287 case SBE_STATE_ISTEP: 288 case SBE_STATE_MPIPL: 289 case SBE_STATE_DMT: 290 return -EBUSY; 291 case SBE_STATE_RUNTIME: 292 case SBE_STATE_DUMP: /* Not sure about that one */ 293 break; 294 case SBE_STATE_FAILURE: 295 case SBE_STATE_QUIESCE: 296 return -ESHUTDOWN; 297 } 298 299 /* Is there async FFDC available ? Remember it */ 300 if (sbm & CFAM_SBM_SBE_ASYNC_FFDC) 301 sbefifo->async_ffdc = true; 302 303 return 0; 304 } 305 306 /* Don't flip endianness of data to/from FIFO, just pass through. */ 307 static int sbefifo_down_read(struct sbefifo *sbefifo, __be32 *word) 308 { 309 return fsi_device_read(sbefifo->fsi_dev, SBEFIFO_DOWN, word, 310 sizeof(*word)); 311 } 312 313 static int sbefifo_up_write(struct sbefifo *sbefifo, __be32 word) 314 { 315 return fsi_device_write(sbefifo->fsi_dev, SBEFIFO_UP, &word, 316 sizeof(word)); 317 } 318 319 static int sbefifo_request_reset(struct sbefifo *sbefifo) 320 { 321 struct device *dev = &sbefifo->fsi_dev->dev; 322 u32 status, timeout; 323 int rc; 324 325 dev_dbg(dev, "Requesting FIFO reset\n"); 326 327 /* Mark broken first, will be cleared if reset succeeds */ 328 sbefifo->broken = true; 329 330 /* Send reset request */ 331 rc = sbefifo_regw(sbefifo, SBEFIFO_UP | SBEFIFO_REQ_RESET, 1); 332 if (rc) { 333 dev_err(dev, "Sending reset request failed, rc=%d\n", rc); 334 return rc; 335 } 336 337 /* Wait for it to complete */ 338 for (timeout = 0; timeout < SBEFIFO_RESET_TIMEOUT; timeout++) { 339 rc = sbefifo_regr(sbefifo, SBEFIFO_UP | SBEFIFO_STS, &status); 340 if (rc) { 341 dev_err(dev, "Failed to read UP fifo status during reset" 342 " , rc=%d\n", rc); 343 return rc; 344 } 345 346 if (!(status & SBEFIFO_STS_RESET_REQ)) { 347 dev_dbg(dev, "FIFO reset done\n"); 348 sbefifo->broken = false; 349 return 0; 350 } 351 352 msleep(1); 353 } 354 dev_err(dev, "FIFO reset timed out\n"); 355 356 return -ETIMEDOUT; 357 } 358 359 static int sbefifo_cleanup_hw(struct sbefifo *sbefifo) 360 { 361 struct device *dev = &sbefifo->fsi_dev->dev; 362 u32 up_status, down_status; 363 bool need_reset = false; 364 int rc; 365 366 rc = sbefifo_check_sbe_state(sbefifo); 367 if (rc) { 368 dev_dbg(dev, "SBE state=%d\n", rc); 369 return rc; 370 } 371 372 /* If broken, we don't need to look at status, go straight to reset */ 373 if (sbefifo->broken) 374 goto do_reset; 375 376 rc = sbefifo_regr(sbefifo, SBEFIFO_UP | SBEFIFO_STS, &up_status); 377 if (rc) { 378 dev_err(dev, "Cleanup: Reading UP status failed, rc=%d\n", rc); 379 380 /* Will try reset again on next attempt at using it */ 381 sbefifo->broken = true; 382 return rc; 383 } 384 385 rc = sbefifo_regr(sbefifo, SBEFIFO_DOWN | SBEFIFO_STS, &down_status); 386 if (rc) { 387 dev_err(dev, "Cleanup: Reading DOWN status failed, rc=%d\n", rc); 388 389 /* Will try reset again on next attempt at using it */ 390 sbefifo->broken = true; 391 return rc; 392 } 393 394 /* The FIFO already contains a reset request from the SBE ? */ 395 if (down_status & SBEFIFO_STS_RESET_REQ) { 396 dev_info(dev, "Cleanup: FIFO reset request set, resetting\n"); 397 rc = sbefifo_regw(sbefifo, SBEFIFO_UP, SBEFIFO_PERFORM_RESET); 398 if (rc) { 399 sbefifo->broken = true; 400 dev_err(dev, "Cleanup: Reset reg write failed, rc=%d\n", rc); 401 return rc; 402 } 403 sbefifo->broken = false; 404 return 0; 405 } 406 407 /* Parity error on either FIFO ? */ 408 if ((up_status | down_status) & SBEFIFO_STS_PARITY_ERR) 409 need_reset = true; 410 411 /* Either FIFO not empty ? */ 412 if (!((up_status & down_status) & SBEFIFO_STS_EMPTY)) 413 need_reset = true; 414 415 if (!need_reset) 416 return 0; 417 418 dev_info(dev, "Cleanup: FIFO not clean (up=0x%08x down=0x%08x)\n", 419 up_status, down_status); 420 421 do_reset: 422 423 /* Mark broken, will be cleared if/when reset succeeds */ 424 return sbefifo_request_reset(sbefifo); 425 } 426 427 static int sbefifo_wait(struct sbefifo *sbefifo, bool up, 428 u32 *status, unsigned long timeout) 429 { 430 struct device *dev = &sbefifo->fsi_dev->dev; 431 unsigned long end_time; 432 bool ready = false; 433 u32 addr, sts = 0; 434 int rc; 435 436 dev_vdbg(dev, "Wait on %s fifo...\n", up ? "up" : "down"); 437 438 addr = (up ? SBEFIFO_UP : SBEFIFO_DOWN) | SBEFIFO_STS; 439 440 end_time = jiffies + timeout; 441 while (!time_after(jiffies, end_time)) { 442 cond_resched(); 443 rc = sbefifo_regr(sbefifo, addr, &sts); 444 if (rc < 0) { 445 dev_err(dev, "FSI error %d reading status register\n", rc); 446 return rc; 447 } 448 if (!up && sbefifo_parity_err(sts)) { 449 dev_err(dev, "Parity error in DOWN FIFO\n"); 450 return -ENXIO; 451 } 452 ready = !(up ? sbefifo_full(sts) : sbefifo_empty(sts)); 453 if (ready) 454 break; 455 } 456 if (!ready) { 457 dev_err(dev, "%s FIFO Timeout ! status=%08x\n", up ? "UP" : "DOWN", sts); 458 return -ETIMEDOUT; 459 } 460 dev_vdbg(dev, "End of wait status: %08x\n", sts); 461 462 *status = sts; 463 464 return 0; 465 } 466 467 static int sbefifo_send_command(struct sbefifo *sbefifo, 468 const __be32 *command, size_t cmd_len) 469 { 470 struct device *dev = &sbefifo->fsi_dev->dev; 471 size_t len, chunk, vacant = 0, remaining = cmd_len; 472 unsigned long timeout; 473 u32 status; 474 int rc; 475 476 dev_vdbg(dev, "sending command (%zd words, cmd=%04x)\n", 477 cmd_len, be32_to_cpu(command[1])); 478 479 /* As long as there's something to send */ 480 timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_START_CMD); 481 while (remaining) { 482 /* Wait for room in the FIFO */ 483 rc = sbefifo_wait(sbefifo, true, &status, timeout); 484 if (rc < 0) 485 return rc; 486 timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_IN_CMD); 487 488 vacant = sbefifo_vacant(status); 489 len = chunk = min(vacant, remaining); 490 491 dev_vdbg(dev, " status=%08x vacant=%zd chunk=%zd\n", 492 status, vacant, chunk); 493 494 /* Write as much as we can */ 495 while (len--) { 496 rc = sbefifo_up_write(sbefifo, *(command++)); 497 if (rc) { 498 dev_err(dev, "FSI error %d writing UP FIFO\n", rc); 499 return rc; 500 } 501 } 502 remaining -= chunk; 503 vacant -= chunk; 504 } 505 506 /* If there's no room left, wait for some to write EOT */ 507 if (!vacant) { 508 rc = sbefifo_wait(sbefifo, true, &status, timeout); 509 if (rc) 510 return rc; 511 } 512 513 /* Send an EOT */ 514 rc = sbefifo_regw(sbefifo, SBEFIFO_UP | SBEFIFO_EOT_RAISE, 0); 515 if (rc) 516 dev_err(dev, "FSI error %d writing EOT\n", rc); 517 return rc; 518 } 519 520 static int sbefifo_read_response(struct sbefifo *sbefifo, struct iov_iter *response) 521 { 522 struct device *dev = &sbefifo->fsi_dev->dev; 523 u32 status, eot_set; 524 unsigned long timeout; 525 bool overflow = false; 526 __be32 data; 527 size_t len; 528 int rc; 529 530 dev_vdbg(dev, "reading response, buflen = %zd\n", iov_iter_count(response)); 531 532 timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_START_RSP); 533 for (;;) { 534 /* Grab FIFO status (this will handle parity errors) */ 535 rc = sbefifo_wait(sbefifo, false, &status, timeout); 536 if (rc < 0) 537 return rc; 538 timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_IN_RSP); 539 540 /* Decode status */ 541 len = sbefifo_populated(status); 542 eot_set = sbefifo_eot_set(status); 543 544 dev_vdbg(dev, " chunk size %zd eot_set=0x%x\n", len, eot_set); 545 546 /* Go through the chunk */ 547 while(len--) { 548 /* Read the data */ 549 rc = sbefifo_down_read(sbefifo, &data); 550 if (rc < 0) 551 return rc; 552 553 /* Was it an EOT ? */ 554 if (eot_set & 0x80) { 555 /* 556 * There should be nothing else in the FIFO, 557 * if there is, mark broken, this will force 558 * a reset on next use, but don't fail the 559 * command. 560 */ 561 if (len) { 562 dev_warn(dev, "FIFO read hit" 563 " EOT with still %zd data\n", 564 len); 565 sbefifo->broken = true; 566 } 567 568 /* We are done */ 569 rc = sbefifo_regw(sbefifo, 570 SBEFIFO_DOWN | SBEFIFO_EOT_ACK, 0); 571 572 /* 573 * If that write fail, still complete the request but mark 574 * the fifo as broken for subsequent reset (not much else 575 * we can do here). 576 */ 577 if (rc) { 578 dev_err(dev, "FSI error %d ack'ing EOT\n", rc); 579 sbefifo->broken = true; 580 } 581 582 /* Tell whether we overflowed */ 583 return overflow ? -EOVERFLOW : 0; 584 } 585 586 /* Store it if there is room */ 587 if (iov_iter_count(response) >= sizeof(__be32)) { 588 if (copy_to_iter(&data, sizeof(__be32), response) < sizeof(__be32)) 589 return -EFAULT; 590 } else { 591 dev_vdbg(dev, "Response overflowed !\n"); 592 593 overflow = true; 594 } 595 596 /* Next EOT bit */ 597 eot_set <<= 1; 598 } 599 } 600 /* Shouldn't happen */ 601 return -EIO; 602 } 603 604 static int sbefifo_do_command(struct sbefifo *sbefifo, 605 const __be32 *command, size_t cmd_len, 606 struct iov_iter *response) 607 { 608 /* Try sending the command */ 609 int rc = sbefifo_send_command(sbefifo, command, cmd_len); 610 if (rc) 611 return rc; 612 613 /* Now, get the response */ 614 return sbefifo_read_response(sbefifo, response); 615 } 616 617 static void sbefifo_collect_async_ffdc(struct sbefifo *sbefifo) 618 { 619 struct device *dev = &sbefifo->fsi_dev->dev; 620 struct iov_iter ffdc_iter; 621 struct kvec ffdc_iov; 622 __be32 *ffdc; 623 size_t ffdc_sz; 624 __be32 cmd[2]; 625 int rc; 626 627 sbefifo->async_ffdc = false; 628 ffdc = vmalloc(SBEFIFO_MAX_FFDC_SIZE); 629 if (!ffdc) { 630 dev_err(dev, "Failed to allocate SBE FFDC buffer\n"); 631 return; 632 } 633 ffdc_iov.iov_base = ffdc; 634 ffdc_iov.iov_len = SBEFIFO_MAX_FFDC_SIZE; 635 iov_iter_kvec(&ffdc_iter, WRITE | ITER_KVEC, &ffdc_iov, 1, SBEFIFO_MAX_FFDC_SIZE); 636 cmd[0] = cpu_to_be32(2); 637 cmd[1] = cpu_to_be32(SBEFIFO_CMD_GET_SBE_FFDC); 638 rc = sbefifo_do_command(sbefifo, cmd, 2, &ffdc_iter); 639 if (rc != 0) { 640 dev_err(dev, "Error %d retrieving SBE FFDC\n", rc); 641 goto bail; 642 } 643 ffdc_sz = SBEFIFO_MAX_FFDC_SIZE - iov_iter_count(&ffdc_iter); 644 ffdc_sz /= sizeof(__be32); 645 rc = sbefifo_parse_status(dev, SBEFIFO_CMD_GET_SBE_FFDC, ffdc, 646 ffdc_sz, &ffdc_sz); 647 if (rc != 0) { 648 dev_err(dev, "Error %d decoding SBE FFDC\n", rc); 649 goto bail; 650 } 651 if (ffdc_sz > 0) 652 sbefifo_dump_ffdc(dev, ffdc, ffdc_sz, true); 653 bail: 654 vfree(ffdc); 655 656 } 657 658 static int __sbefifo_submit(struct sbefifo *sbefifo, 659 const __be32 *command, size_t cmd_len, 660 struct iov_iter *response) 661 { 662 struct device *dev = &sbefifo->fsi_dev->dev; 663 int rc; 664 665 if (cmd_len < 2 || be32_to_cpu(command[0]) != cmd_len) { 666 dev_vdbg(dev, "Invalid command len %zd (header: %d)\n", 667 cmd_len, be32_to_cpu(command[0])); 668 return -EINVAL; 669 } 670 671 /* First ensure the HW is in a clean state */ 672 rc = sbefifo_cleanup_hw(sbefifo); 673 if (rc) 674 return rc; 675 676 /* Look for async FFDC first if any */ 677 if (sbefifo->async_ffdc) 678 sbefifo_collect_async_ffdc(sbefifo); 679 680 rc = sbefifo_do_command(sbefifo, command, cmd_len, response); 681 if (rc != 0 && rc != -EOVERFLOW) 682 goto fail; 683 return rc; 684 fail: 685 /* 686 * On failure, attempt a reset. Ignore the result, it will mark 687 * the fifo broken if the reset fails 688 */ 689 sbefifo_request_reset(sbefifo); 690 691 /* Return original error */ 692 return rc; 693 } 694 695 /** 696 * sbefifo_submit() - Submit and SBE fifo command and receive response 697 * @dev: The sbefifo device 698 * @command: The raw command data 699 * @cmd_len: The command size (in 32-bit words) 700 * @response: The output response buffer 701 * @resp_len: In: Response buffer size, Out: Response size 702 * 703 * This will perform the entire operation. If the reponse buffer 704 * overflows, returns -EOVERFLOW 705 */ 706 int sbefifo_submit(struct device *dev, const __be32 *command, size_t cmd_len, 707 __be32 *response, size_t *resp_len) 708 { 709 struct sbefifo *sbefifo = dev_get_drvdata(dev); 710 struct iov_iter resp_iter; 711 struct kvec resp_iov; 712 size_t rbytes; 713 int rc; 714 715 if (!dev || !sbefifo) 716 return -ENODEV; 717 if (WARN_ON_ONCE(sbefifo->magic != SBEFIFO_MAGIC)) 718 return -ENODEV; 719 if (!resp_len || !command || !response || cmd_len > SBEFIFO_MAX_CMD_LEN) 720 return -EINVAL; 721 722 /* Prepare iov iterator */ 723 rbytes = (*resp_len) * sizeof(__be32); 724 resp_iov.iov_base = response; 725 resp_iov.iov_len = rbytes; 726 iov_iter_kvec(&resp_iter, WRITE | ITER_KVEC, &resp_iov, 1, rbytes); 727 728 /* Perform the command */ 729 mutex_lock(&sbefifo->lock); 730 rc = __sbefifo_submit(sbefifo, command, cmd_len, &resp_iter); 731 mutex_unlock(&sbefifo->lock); 732 733 /* Extract the response length */ 734 rbytes -= iov_iter_count(&resp_iter); 735 *resp_len = rbytes / sizeof(__be32); 736 737 return rc; 738 } 739 EXPORT_SYMBOL_GPL(sbefifo_submit); 740 741 /* 742 * Char device interface 743 */ 744 static int sbefifo_user_open(struct inode *inode, struct file *file) 745 { 746 struct sbefifo *sbefifo = container_of(file->private_data, 747 struct sbefifo, mdev); 748 struct sbefifo_user *user; 749 750 user = kzalloc(sizeof(struct sbefifo_user), GFP_KERNEL); 751 if (!user) 752 return -ENOMEM; 753 754 file->private_data = user; 755 user->sbefifo = sbefifo; 756 user->pending_cmd = (void *)__get_free_page(GFP_KERNEL); 757 if (!user->pending_cmd) { 758 kfree(user); 759 return -ENOMEM; 760 } 761 mutex_init(&user->file_lock); 762 763 return 0; 764 } 765 766 static ssize_t sbefifo_user_read(struct file *file, char __user *buf, 767 size_t len, loff_t *offset) 768 { 769 struct sbefifo_user *user = file->private_data; 770 struct sbefifo *sbefifo; 771 struct iov_iter resp_iter; 772 struct iovec resp_iov; 773 size_t cmd_len; 774 int rc; 775 776 if (!user) 777 return -EINVAL; 778 sbefifo = user->sbefifo; 779 if (len & 3) 780 return -EINVAL; 781 782 mutex_lock(&user->file_lock); 783 784 /* Cronus relies on -EAGAIN after a short read */ 785 if (user->pending_len == 0) { 786 rc = -EAGAIN; 787 goto bail; 788 } 789 if (user->pending_len < 8) { 790 rc = -EINVAL; 791 goto bail; 792 } 793 cmd_len = user->pending_len >> 2; 794 795 /* Prepare iov iterator */ 796 resp_iov.iov_base = buf; 797 resp_iov.iov_len = len; 798 iov_iter_init(&resp_iter, WRITE, &resp_iov, 1, len); 799 800 /* Perform the command */ 801 mutex_lock(&sbefifo->lock); 802 rc = __sbefifo_submit(sbefifo, user->pending_cmd, cmd_len, &resp_iter); 803 mutex_unlock(&sbefifo->lock); 804 if (rc < 0) 805 goto bail; 806 807 /* Extract the response length */ 808 rc = len - iov_iter_count(&resp_iter); 809 bail: 810 user->pending_len = 0; 811 mutex_unlock(&user->file_lock); 812 return rc; 813 } 814 815 static ssize_t sbefifo_user_write(struct file *file, const char __user *buf, 816 size_t len, loff_t *offset) 817 { 818 struct sbefifo_user *user = file->private_data; 819 struct sbefifo *sbefifo; 820 int rc = len; 821 822 if (!user) 823 return -EINVAL; 824 sbefifo = user->sbefifo; 825 if (len > SBEFIFO_MAX_CMD_LEN) 826 return -EINVAL; 827 if (len & 3) 828 return -EINVAL; 829 830 mutex_lock(&user->file_lock); 831 832 /* Copy the command into the staging buffer */ 833 if (copy_from_user(user->pending_cmd, buf, len)) { 834 rc = -EFAULT; 835 goto bail; 836 } 837 838 /* Check for the magic reset command */ 839 if (len == 4 && be32_to_cpu(*(__be32 *)user->pending_cmd) == 840 SBEFIFO_RESET_MAGIC) { 841 842 /* Clear out any pending command */ 843 user->pending_len = 0; 844 845 /* Trigger reset request */ 846 mutex_lock(&sbefifo->lock); 847 rc = sbefifo_request_reset(user->sbefifo); 848 mutex_unlock(&sbefifo->lock); 849 if (rc == 0) 850 rc = 4; 851 goto bail; 852 } 853 854 /* Update the staging buffer size */ 855 user->pending_len = len; 856 bail: 857 mutex_unlock(&user->file_lock); 858 859 /* And that's it, we'll issue the command on a read */ 860 return rc; 861 } 862 863 static int sbefifo_user_release(struct inode *inode, struct file *file) 864 { 865 struct sbefifo_user *user = file->private_data; 866 867 if (!user) 868 return -EINVAL; 869 870 free_page((unsigned long)user->pending_cmd); 871 kfree(user); 872 873 return 0; 874 } 875 876 static const struct file_operations sbefifo_fops = { 877 .owner = THIS_MODULE, 878 .open = sbefifo_user_open, 879 .read = sbefifo_user_read, 880 .write = sbefifo_user_write, 881 .release = sbefifo_user_release, 882 }; 883 884 /* 885 * Probe/remove 886 */ 887 888 static int sbefifo_probe(struct device *dev) 889 { 890 struct fsi_device *fsi_dev = to_fsi_dev(dev); 891 struct sbefifo *sbefifo; 892 struct device_node *np; 893 struct platform_device *child; 894 char child_name[32]; 895 int rc, child_idx = 0; 896 897 dev_dbg(dev, "Found sbefifo device\n"); 898 899 sbefifo = devm_kzalloc(dev, sizeof(*sbefifo), GFP_KERNEL); 900 if (!sbefifo) 901 return -ENOMEM; 902 sbefifo->magic = SBEFIFO_MAGIC; 903 sbefifo->fsi_dev = fsi_dev; 904 mutex_init(&sbefifo->lock); 905 906 /* 907 * Try cleaning up the FIFO. If this fails, we still register the 908 * driver and will try cleaning things up again on the next access. 909 */ 910 rc = sbefifo_cleanup_hw(sbefifo); 911 if (rc && rc != -ESHUTDOWN) 912 dev_err(dev, "Initial HW cleanup failed, will retry later\n"); 913 914 sbefifo->idx = ida_simple_get(&sbefifo_ida, 1, INT_MAX, GFP_KERNEL); 915 snprintf(sbefifo->name, sizeof(sbefifo->name), "sbefifo%d", 916 sbefifo->idx); 917 918 dev_set_drvdata(dev, sbefifo); 919 920 /* Create misc chardev for userspace access */ 921 sbefifo->mdev.minor = MISC_DYNAMIC_MINOR; 922 sbefifo->mdev.fops = &sbefifo_fops; 923 sbefifo->mdev.name = sbefifo->name; 924 sbefifo->mdev.parent = dev; 925 rc = misc_register(&sbefifo->mdev); 926 if (rc) { 927 dev_err(dev, "Failed to register miscdevice: %d\n", rc); 928 ida_simple_remove(&sbefifo_ida, sbefifo->idx); 929 return rc; 930 } 931 932 /* Create platform devs for dts child nodes (occ, etc) */ 933 for_each_available_child_of_node(dev->of_node, np) { 934 snprintf(child_name, sizeof(child_name), "%s-dev%d", 935 sbefifo->name, child_idx++); 936 child = of_platform_device_create(np, child_name, dev); 937 if (!child) 938 dev_warn(dev, "failed to create child %s dev\n", 939 child_name); 940 } 941 942 return 0; 943 } 944 945 static int sbefifo_unregister_child(struct device *dev, void *data) 946 { 947 struct platform_device *child = to_platform_device(dev); 948 949 of_device_unregister(child); 950 if (dev->of_node) 951 of_node_clear_flag(dev->of_node, OF_POPULATED); 952 953 return 0; 954 } 955 956 static int sbefifo_remove(struct device *dev) 957 { 958 struct sbefifo *sbefifo = dev_get_drvdata(dev); 959 960 dev_dbg(dev, "Removing sbefifo device...\n"); 961 962 misc_deregister(&sbefifo->mdev); 963 device_for_each_child(dev, NULL, sbefifo_unregister_child); 964 965 ida_simple_remove(&sbefifo_ida, sbefifo->idx); 966 967 return 0; 968 } 969 970 static struct fsi_device_id sbefifo_ids[] = { 971 { 972 .engine_type = FSI_ENGID_SBE, 973 .version = FSI_VERSION_ANY, 974 }, 975 { 0 } 976 }; 977 978 static struct fsi_driver sbefifo_drv = { 979 .id_table = sbefifo_ids, 980 .drv = { 981 .name = DEVICE_NAME, 982 .bus = &fsi_bus_type, 983 .probe = sbefifo_probe, 984 .remove = sbefifo_remove, 985 } 986 }; 987 988 static int sbefifo_init(void) 989 { 990 return fsi_driver_register(&sbefifo_drv); 991 } 992 993 static void sbefifo_exit(void) 994 { 995 fsi_driver_unregister(&sbefifo_drv); 996 997 ida_destroy(&sbefifo_ida); 998 } 999 1000 module_init(sbefifo_init); 1001 module_exit(sbefifo_exit); 1002 MODULE_LICENSE("GPL"); 1003 MODULE_AUTHOR("Brad Bishop <bradleyb@fuzziesquirrel.com>"); 1004 MODULE_AUTHOR("Eddie James <eajames@linux.vnet.ibm.com>"); 1005 MODULE_AUTHOR("Andrew Jeffery <andrew@aj.id.au>"); 1006 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>"); 1007 MODULE_DESCRIPTION("Linux device interface to the POWER Self Boot Engine"); 1008