eeh_pseries.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /*
  2. * The file intends to implement the platform dependent EEH operations on pseries.
  3. * Actually, the pseries platform is built based on RTAS heavily. That means the
  4. * pseries platform dependent EEH operations will be built on RTAS calls. The functions
  5. * are devired from arch/powerpc/platforms/pseries/eeh.c and necessary cleanup has
  6. * been done.
  7. *
  8. * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2011.
  9. * Copyright IBM Corporation 2001, 2005, 2006
  10. * Copyright Dave Engebretsen & Todd Inglett 2001
  11. * Copyright Linas Vepstas 2005, 2006
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. #include <linux/atomic.h>
  28. #include <linux/delay.h>
  29. #include <linux/export.h>
  30. #include <linux/init.h>
  31. #include <linux/list.h>
  32. #include <linux/of.h>
  33. #include <linux/pci.h>
  34. #include <linux/proc_fs.h>
  35. #include <linux/rbtree.h>
  36. #include <linux/sched.h>
  37. #include <linux/seq_file.h>
  38. #include <linux/spinlock.h>
  39. #include <asm/eeh.h>
  40. #include <asm/eeh_event.h>
  41. #include <asm/io.h>
  42. #include <asm/machdep.h>
  43. #include <asm/ppc-pci.h>
  44. #include <asm/rtas.h>
  45. /* RTAS tokens */
  46. static int ibm_set_eeh_option;
  47. static int ibm_set_slot_reset;
  48. static int ibm_read_slot_reset_state;
  49. static int ibm_read_slot_reset_state2;
  50. static int ibm_slot_error_detail;
  51. static int ibm_get_config_addr_info;
  52. static int ibm_get_config_addr_info2;
  53. static int ibm_configure_bridge;
  54. static int ibm_configure_pe;
  55. /*
  56. * Buffer for reporting slot-error-detail rtas calls. Its here
  57. * in BSS, and not dynamically alloced, so that it ends up in
  58. * RMO where RTAS can access it.
  59. */
  60. static unsigned char slot_errbuf[RTAS_ERROR_LOG_MAX];
  61. static DEFINE_SPINLOCK(slot_errbuf_lock);
  62. static int eeh_error_buf_size;
  63. /**
  64. * pseries_eeh_init - EEH platform dependent initialization
  65. *
  66. * EEH platform dependent initialization on pseries.
  67. */
  68. static int pseries_eeh_init(void)
  69. {
  70. /* figure out EEH RTAS function call tokens */
  71. ibm_set_eeh_option = rtas_token("ibm,set-eeh-option");
  72. ibm_set_slot_reset = rtas_token("ibm,set-slot-reset");
  73. ibm_read_slot_reset_state2 = rtas_token("ibm,read-slot-reset-state2");
  74. ibm_read_slot_reset_state = rtas_token("ibm,read-slot-reset-state");
  75. ibm_slot_error_detail = rtas_token("ibm,slot-error-detail");
  76. ibm_get_config_addr_info2 = rtas_token("ibm,get-config-addr-info2");
  77. ibm_get_config_addr_info = rtas_token("ibm,get-config-addr-info");
  78. ibm_configure_pe = rtas_token("ibm,configure-pe");
  79. ibm_configure_bridge = rtas_token ("ibm,configure-bridge");
  80. /* necessary sanity check */
  81. if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE) {
  82. pr_warning("%s: RTAS service <ibm,set-eeh-option> invalid\n",
  83. __func__);
  84. return -EINVAL;
  85. } else if (ibm_set_slot_reset == RTAS_UNKNOWN_SERVICE) {
  86. pr_warning("%s: RTAS service <ibm, set-slot-reset> invalid\n",
  87. __func__);
  88. return -EINVAL;
  89. } else if (ibm_read_slot_reset_state2 == RTAS_UNKNOWN_SERVICE &&
  90. ibm_read_slot_reset_state == RTAS_UNKNOWN_SERVICE) {
  91. pr_warning("%s: RTAS service <ibm,read-slot-reset-state2> and "
  92. "<ibm,read-slot-reset-state> invalid\n",
  93. __func__);
  94. return -EINVAL;
  95. } else if (ibm_slot_error_detail == RTAS_UNKNOWN_SERVICE) {
  96. pr_warning("%s: RTAS service <ibm,slot-error-detail> invalid\n",
  97. __func__);
  98. return -EINVAL;
  99. } else if (ibm_get_config_addr_info2 == RTAS_UNKNOWN_SERVICE &&
  100. ibm_get_config_addr_info == RTAS_UNKNOWN_SERVICE) {
  101. pr_warning("%s: RTAS service <ibm,get-config-addr-info2> and "
  102. "<ibm,get-config-addr-info> invalid\n",
  103. __func__);
  104. return -EINVAL;
  105. } else if (ibm_configure_pe == RTAS_UNKNOWN_SERVICE &&
  106. ibm_configure_bridge == RTAS_UNKNOWN_SERVICE) {
  107. pr_warning("%s: RTAS service <ibm,configure-pe> and "
  108. "<ibm,configure-bridge> invalid\n",
  109. __func__);
  110. return -EINVAL;
  111. }
  112. /* Initialize error log lock and size */
  113. spin_lock_init(&slot_errbuf_lock);
  114. eeh_error_buf_size = rtas_token("rtas-error-log-max");
  115. if (eeh_error_buf_size == RTAS_UNKNOWN_SERVICE) {
  116. pr_warning("%s: unknown EEH error log size\n",
  117. __func__);
  118. eeh_error_buf_size = 1024;
  119. } else if (eeh_error_buf_size > RTAS_ERROR_LOG_MAX) {
  120. pr_warning("%s: EEH error log size %d exceeds the maximal %d\n",
  121. __func__, eeh_error_buf_size, RTAS_ERROR_LOG_MAX);
  122. eeh_error_buf_size = RTAS_ERROR_LOG_MAX;
  123. }
  124. return 0;
  125. }
  126. /**
  127. * pseries_eeh_set_option - Initialize EEH or MMIO/DMA reenable
  128. * @dn: device node
  129. * @option: operation to be issued
  130. *
  131. * The function is used to control the EEH functionality globally.
  132. * Currently, following options are support according to PAPR:
  133. * Enable EEH, Disable EEH, Enable MMIO and Enable DMA
  134. */
  135. static int pseries_eeh_set_option(struct device_node *dn, int option)
  136. {
  137. int ret = 0;
  138. struct eeh_dev *edev;
  139. const u32 *reg;
  140. int config_addr;
  141. edev = of_node_to_eeh_dev(dn);
  142. /*
  143. * When we're enabling or disabling EEH functioality on
  144. * the particular PE, the PE config address is possibly
  145. * unavailable. Therefore, we have to figure it out from
  146. * the FDT node.
  147. */
  148. switch (option) {
  149. case EEH_OPT_DISABLE:
  150. case EEH_OPT_ENABLE:
  151. reg = of_get_property(dn, "reg", NULL);
  152. config_addr = reg[0];
  153. break;
  154. case EEH_OPT_THAW_MMIO:
  155. case EEH_OPT_THAW_DMA:
  156. config_addr = edev->config_addr;
  157. if (edev->pe_config_addr)
  158. config_addr = edev->pe_config_addr;
  159. break;
  160. default:
  161. pr_err("%s: Invalid option %d\n",
  162. __func__, option);
  163. return -EINVAL;
  164. }
  165. ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
  166. config_addr, BUID_HI(edev->phb->buid),
  167. BUID_LO(edev->phb->buid), option);
  168. return ret;
  169. }
  170. /**
  171. * pseries_eeh_get_pe_addr - Retrieve PE address
  172. * @dn: device node
  173. *
  174. * Retrieve the assocated PE address. Actually, there're 2 RTAS
  175. * function calls dedicated for the purpose. We need implement
  176. * it through the new function and then the old one. Besides,
  177. * you should make sure the config address is figured out from
  178. * FDT node before calling the function.
  179. *
  180. * It's notable that zero'ed return value means invalid PE config
  181. * address.
  182. */
  183. static int pseries_eeh_get_pe_addr(struct device_node *dn)
  184. {
  185. struct eeh_dev *edev;
  186. int ret = 0;
  187. int rets[3];
  188. edev = of_node_to_eeh_dev(dn);
  189. if (ibm_get_config_addr_info2 != RTAS_UNKNOWN_SERVICE) {
  190. /*
  191. * First of all, we need to make sure there has one PE
  192. * associated with the device. Otherwise, PE address is
  193. * meaningless.
  194. */
  195. ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
  196. edev->config_addr, BUID_HI(edev->phb->buid),
  197. BUID_LO(edev->phb->buid), 1);
  198. if (ret || (rets[0] == 0))
  199. return 0;
  200. /* Retrieve the associated PE config address */
  201. ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
  202. edev->config_addr, BUID_HI(edev->phb->buid),
  203. BUID_LO(edev->phb->buid), 0);
  204. if (ret) {
  205. pr_warning("%s: Failed to get PE address for %s\n",
  206. __func__, dn->full_name);
  207. return 0;
  208. }
  209. return rets[0];
  210. }
  211. if (ibm_get_config_addr_info != RTAS_UNKNOWN_SERVICE) {
  212. ret = rtas_call(ibm_get_config_addr_info, 4, 2, rets,
  213. edev->config_addr, BUID_HI(edev->phb->buid),
  214. BUID_LO(edev->phb->buid), 0);
  215. if (ret) {
  216. pr_warning("%s: Failed to get PE address for %s\n",
  217. __func__, dn->full_name);
  218. return 0;
  219. }
  220. return rets[0];
  221. }
  222. return ret;
  223. }
  224. /**
  225. * pseries_eeh_get_state - Retrieve PE state
  226. * @dn: PE associated device node
  227. * @state: return value
  228. *
  229. * Retrieve the state of the specified PE. On RTAS compliant
  230. * pseries platform, there already has one dedicated RTAS function
  231. * for the purpose. It's notable that the associated PE config address
  232. * might be ready when calling the function. Therefore, endeavour to
  233. * use the PE config address if possible. Further more, there're 2
  234. * RTAS calls for the purpose, we need to try the new one and back
  235. * to the old one if the new one couldn't work properly.
  236. */
  237. static int pseries_eeh_get_state(struct device_node *dn, int *state)
  238. {
  239. struct eeh_dev *edev;
  240. int config_addr;
  241. int ret;
  242. int rets[4];
  243. int result;
  244. /* Figure out PE config address if possible */
  245. edev = of_node_to_eeh_dev(dn);
  246. config_addr = edev->config_addr;
  247. if (edev->pe_config_addr)
  248. config_addr = edev->pe_config_addr;
  249. if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
  250. ret = rtas_call(ibm_read_slot_reset_state2, 3, 4, rets,
  251. config_addr, BUID_HI(edev->phb->buid),
  252. BUID_LO(edev->phb->buid));
  253. } else if (ibm_read_slot_reset_state != RTAS_UNKNOWN_SERVICE) {
  254. /* Fake PE unavailable info */
  255. rets[2] = 0;
  256. ret = rtas_call(ibm_read_slot_reset_state, 3, 3, rets,
  257. config_addr, BUID_HI(edev->phb->buid),
  258. BUID_LO(edev->phb->buid));
  259. } else {
  260. return EEH_STATE_NOT_SUPPORT;
  261. }
  262. if (ret)
  263. return ret;
  264. /* Parse the result out */
  265. result = 0;
  266. if (rets[1]) {
  267. switch(rets[0]) {
  268. case 0:
  269. result &= ~EEH_STATE_RESET_ACTIVE;
  270. result |= EEH_STATE_MMIO_ACTIVE;
  271. result |= EEH_STATE_DMA_ACTIVE;
  272. break;
  273. case 1:
  274. result |= EEH_STATE_RESET_ACTIVE;
  275. result |= EEH_STATE_MMIO_ACTIVE;
  276. result |= EEH_STATE_DMA_ACTIVE;
  277. break;
  278. case 2:
  279. result &= ~EEH_STATE_RESET_ACTIVE;
  280. result &= ~EEH_STATE_MMIO_ACTIVE;
  281. result &= ~EEH_STATE_DMA_ACTIVE;
  282. break;
  283. case 4:
  284. result &= ~EEH_STATE_RESET_ACTIVE;
  285. result &= ~EEH_STATE_MMIO_ACTIVE;
  286. result &= ~EEH_STATE_DMA_ACTIVE;
  287. result |= EEH_STATE_MMIO_ENABLED;
  288. break;
  289. case 5:
  290. if (rets[2]) {
  291. if (state) *state = rets[2];
  292. result = EEH_STATE_UNAVAILABLE;
  293. } else {
  294. result = EEH_STATE_NOT_SUPPORT;
  295. }
  296. default:
  297. result = EEH_STATE_NOT_SUPPORT;
  298. }
  299. } else {
  300. result = EEH_STATE_NOT_SUPPORT;
  301. }
  302. return result;
  303. }
  304. /**
  305. * pseries_eeh_reset - Reset the specified PE
  306. * @dn: PE associated device node
  307. * @option: reset option
  308. *
  309. * Reset the specified PE
  310. */
  311. static int pseries_eeh_reset(struct device_node *dn, int option)
  312. {
  313. struct eeh_dev *edev;
  314. int config_addr;
  315. int ret;
  316. /* Figure out PE address */
  317. edev = of_node_to_eeh_dev(dn);
  318. config_addr = edev->config_addr;
  319. if (edev->pe_config_addr)
  320. config_addr = edev->pe_config_addr;
  321. /* Reset PE through RTAS call */
  322. ret = rtas_call(ibm_set_slot_reset, 4, 1, NULL,
  323. config_addr, BUID_HI(edev->phb->buid),
  324. BUID_LO(edev->phb->buid), option);
  325. /* If fundamental-reset not supported, try hot-reset */
  326. if (option == EEH_RESET_FUNDAMENTAL &&
  327. ret == -8) {
  328. ret = rtas_call(ibm_set_slot_reset, 4, 1, NULL,
  329. config_addr, BUID_HI(edev->phb->buid),
  330. BUID_LO(edev->phb->buid), EEH_RESET_HOT);
  331. }
  332. return ret;
  333. }
  334. /**
  335. * pseries_eeh_wait_state - Wait for PE state
  336. * @dn: PE associated device node
  337. * @max_wait: maximal period in microsecond
  338. *
  339. * Wait for the state of associated PE. It might take some time
  340. * to retrieve the PE's state.
  341. */
  342. static int pseries_eeh_wait_state(struct device_node *dn, int max_wait)
  343. {
  344. int ret;
  345. int mwait;
  346. /*
  347. * According to PAPR, the state of PE might be temporarily
  348. * unavailable. Under the circumstance, we have to wait
  349. * for indicated time determined by firmware. The maximal
  350. * wait time is 5 minutes, which is acquired from the original
  351. * EEH implementation. Also, the original implementation
  352. * also defined the minimal wait time as 1 second.
  353. */
  354. #define EEH_STATE_MIN_WAIT_TIME (1000)
  355. #define EEH_STATE_MAX_WAIT_TIME (300 * 1000)
  356. while (1) {
  357. ret = pseries_eeh_get_state(dn, &mwait);
  358. /*
  359. * If the PE's state is temporarily unavailable,
  360. * we have to wait for the specified time. Otherwise,
  361. * the PE's state will be returned immediately.
  362. */
  363. if (ret != EEH_STATE_UNAVAILABLE)
  364. return ret;
  365. if (max_wait <= 0) {
  366. pr_warning("%s: Timeout when getting PE's state (%d)\n",
  367. __func__, max_wait);
  368. return EEH_STATE_NOT_SUPPORT;
  369. }
  370. if (mwait <= 0) {
  371. pr_warning("%s: Firmware returned bad wait value %d\n",
  372. __func__, mwait);
  373. mwait = EEH_STATE_MIN_WAIT_TIME;
  374. } else if (mwait > EEH_STATE_MAX_WAIT_TIME) {
  375. pr_warning("%s: Firmware returned too long wait value %d\n",
  376. __func__, mwait);
  377. mwait = EEH_STATE_MAX_WAIT_TIME;
  378. }
  379. max_wait -= mwait;
  380. msleep(mwait);
  381. }
  382. return EEH_STATE_NOT_SUPPORT;
  383. }
  384. /**
  385. * pseries_eeh_get_log - Retrieve error log
  386. * @dn: device node
  387. * @severity: temporary or permanent error log
  388. * @drv_log: driver log to be combined with retrieved error log
  389. * @len: length of driver log
  390. *
  391. * Retrieve the temporary or permanent error from the PE.
  392. * Actually, the error will be retrieved through the dedicated
  393. * RTAS call.
  394. */
  395. static int pseries_eeh_get_log(struct device_node *dn, int severity, char *drv_log, unsigned long len)
  396. {
  397. struct eeh_dev *edev;
  398. int config_addr;
  399. unsigned long flags;
  400. int ret;
  401. edev = of_node_to_eeh_dev(dn);
  402. spin_lock_irqsave(&slot_errbuf_lock, flags);
  403. memset(slot_errbuf, 0, eeh_error_buf_size);
  404. /* Figure out the PE address */
  405. config_addr = edev->config_addr;
  406. if (edev->pe_config_addr)
  407. config_addr = edev->pe_config_addr;
  408. ret = rtas_call(ibm_slot_error_detail, 8, 1, NULL, config_addr,
  409. BUID_HI(edev->phb->buid), BUID_LO(edev->phb->buid),
  410. virt_to_phys(drv_log), len,
  411. virt_to_phys(slot_errbuf), eeh_error_buf_size,
  412. severity);
  413. if (!ret)
  414. log_error(slot_errbuf, ERR_TYPE_RTAS_LOG, 0);
  415. spin_unlock_irqrestore(&slot_errbuf_lock, flags);
  416. return ret;
  417. }
  418. /**
  419. * pseries_eeh_configure_bridge - Configure PCI bridges in the indicated PE
  420. * @dn: PE associated device node
  421. *
  422. * The function will be called to reconfigure the bridges included
  423. * in the specified PE so that the mulfunctional PE would be recovered
  424. * again.
  425. */
  426. static int pseries_eeh_configure_bridge(struct device_node *dn)
  427. {
  428. struct eeh_dev *edev;
  429. int config_addr;
  430. int ret;
  431. /* Figure out the PE address */
  432. edev = of_node_to_eeh_dev(dn);
  433. config_addr = edev->config_addr;
  434. if (edev->pe_config_addr)
  435. config_addr = edev->pe_config_addr;
  436. /* Use new configure-pe function, if supported */
  437. if (ibm_configure_pe != RTAS_UNKNOWN_SERVICE) {
  438. ret = rtas_call(ibm_configure_pe, 3, 1, NULL,
  439. config_addr, BUID_HI(edev->phb->buid),
  440. BUID_LO(edev->phb->buid));
  441. } else if (ibm_configure_bridge != RTAS_UNKNOWN_SERVICE) {
  442. ret = rtas_call(ibm_configure_bridge, 3, 1, NULL,
  443. config_addr, BUID_HI(edev->phb->buid),
  444. BUID_LO(edev->phb->buid));
  445. } else {
  446. return -EFAULT;
  447. }
  448. if (ret)
  449. pr_warning("%s: Unable to configure bridge %d for %s\n",
  450. __func__, ret, dn->full_name);
  451. return ret;
  452. }
  453. /**
  454. * pseries_eeh_read_config - Read PCI config space
  455. * @dn: device node
  456. * @where: PCI address
  457. * @size: size to read
  458. * @val: return value
  459. *
  460. * Read config space from the speicifed device
  461. */
  462. static int pseries_eeh_read_config(struct device_node *dn, int where, int size, u32 *val)
  463. {
  464. struct pci_dn *pdn;
  465. pdn = PCI_DN(dn);
  466. return rtas_read_config(pdn, where, size, val);
  467. }
  468. /**
  469. * pseries_eeh_write_config - Write PCI config space
  470. * @dn: device node
  471. * @where: PCI address
  472. * @size: size to write
  473. * @val: value to be written
  474. *
  475. * Write config space to the specified device
  476. */
  477. static int pseries_eeh_write_config(struct device_node *dn, int where, int size, u32 val)
  478. {
  479. struct pci_dn *pdn;
  480. pdn = PCI_DN(dn);
  481. return rtas_write_config(pdn, where, size, val);
  482. }
  483. static struct eeh_ops pseries_eeh_ops = {
  484. .name = "pseries",
  485. .init = pseries_eeh_init,
  486. .set_option = pseries_eeh_set_option,
  487. .get_pe_addr = pseries_eeh_get_pe_addr,
  488. .get_state = pseries_eeh_get_state,
  489. .reset = pseries_eeh_reset,
  490. .wait_state = pseries_eeh_wait_state,
  491. .get_log = pseries_eeh_get_log,
  492. .configure_bridge = pseries_eeh_configure_bridge,
  493. .read_config = pseries_eeh_read_config,
  494. .write_config = pseries_eeh_write_config
  495. };
  496. /**
  497. * eeh_pseries_init - Register platform dependent EEH operations
  498. *
  499. * EEH initialization on pseries platform. This function should be
  500. * called before any EEH related functions.
  501. */
  502. int __init eeh_pseries_init(void)
  503. {
  504. return eeh_ops_register(&pseries_eeh_ops);
  505. }