eeh-powernv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * The file intends to implement the platform dependent EEH operations on
  3. * powernv platform. Actually, the powernv was created in order to fully
  4. * hypervisor support.
  5. *
  6. * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2013.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/atomic.h>
  14. #include <linux/delay.h>
  15. #include <linux/export.h>
  16. #include <linux/init.h>
  17. #include <linux/list.h>
  18. #include <linux/msi.h>
  19. #include <linux/of.h>
  20. #include <linux/pci.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/rbtree.h>
  23. #include <linux/sched.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/spinlock.h>
  26. #include <asm/eeh.h>
  27. #include <asm/eeh_event.h>
  28. #include <asm/firmware.h>
  29. #include <asm/io.h>
  30. #include <asm/iommu.h>
  31. #include <asm/machdep.h>
  32. #include <asm/msi_bitmap.h>
  33. #include <asm/opal.h>
  34. #include <asm/ppc-pci.h>
  35. #include "powernv.h"
  36. #include "pci.h"
  37. /**
  38. * powernv_eeh_init - EEH platform dependent initialization
  39. *
  40. * EEH platform dependent initialization on powernv
  41. */
  42. static int powernv_eeh_init(void)
  43. {
  44. struct pci_controller *hose;
  45. struct pnv_phb *phb;
  46. /* We require OPALv3 */
  47. if (!firmware_has_feature(FW_FEATURE_OPALv3)) {
  48. pr_warn("%s: OPALv3 is required !\n",
  49. __func__);
  50. return -EINVAL;
  51. }
  52. /* Set probe mode */
  53. eeh_add_flag(EEH_PROBE_MODE_DEV);
  54. /*
  55. * P7IOC blocks PCI config access to frozen PE, but PHB3
  56. * doesn't do that. So we have to selectively enable I/O
  57. * prior to collecting error log.
  58. */
  59. list_for_each_entry(hose, &hose_list, list_node) {
  60. phb = hose->private_data;
  61. if (phb->model == PNV_PHB_MODEL_P7IOC)
  62. eeh_add_flag(EEH_ENABLE_IO_FOR_LOG);
  63. break;
  64. }
  65. return 0;
  66. }
  67. /**
  68. * powernv_eeh_post_init - EEH platform dependent post initialization
  69. *
  70. * EEH platform dependent post initialization on powernv. When
  71. * the function is called, the EEH PEs and devices should have
  72. * been built. If the I/O cache staff has been built, EEH is
  73. * ready to supply service.
  74. */
  75. static int powernv_eeh_post_init(void)
  76. {
  77. struct pci_controller *hose;
  78. struct pnv_phb *phb;
  79. int ret = 0;
  80. list_for_each_entry(hose, &hose_list, list_node) {
  81. phb = hose->private_data;
  82. if (phb->eeh_ops && phb->eeh_ops->post_init) {
  83. ret = phb->eeh_ops->post_init(hose);
  84. if (ret)
  85. break;
  86. }
  87. }
  88. return ret;
  89. }
  90. /**
  91. * powernv_eeh_dev_probe - Do probe on PCI device
  92. * @dev: PCI device
  93. * @flag: unused
  94. *
  95. * When EEH module is installed during system boot, all PCI devices
  96. * are checked one by one to see if it supports EEH. The function
  97. * is introduced for the purpose. By default, EEH has been enabled
  98. * on all PCI devices. That's to say, we only need do necessary
  99. * initialization on the corresponding eeh device and create PE
  100. * accordingly.
  101. *
  102. * It's notable that's unsafe to retrieve the EEH device through
  103. * the corresponding PCI device. During the PCI device hotplug, which
  104. * was possiblly triggered by EEH core, the binding between EEH device
  105. * and the PCI device isn't built yet.
  106. */
  107. static int powernv_eeh_dev_probe(struct pci_dev *dev, void *flag)
  108. {
  109. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  110. struct pnv_phb *phb = hose->private_data;
  111. struct device_node *dn = pci_device_to_OF_node(dev);
  112. struct eeh_dev *edev = of_node_to_eeh_dev(dn);
  113. int ret;
  114. /*
  115. * When probing the root bridge, which doesn't have any
  116. * subordinate PCI devices. We don't have OF node for
  117. * the root bridge. So it's not reasonable to continue
  118. * the probing.
  119. */
  120. if (!dn || !edev || edev->pe)
  121. return 0;
  122. /* Skip for PCI-ISA bridge */
  123. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
  124. return 0;
  125. /* Initialize eeh device */
  126. edev->class_code = dev->class;
  127. edev->mode &= 0xFFFFFF00;
  128. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
  129. edev->mode |= EEH_DEV_BRIDGE;
  130. edev->pcix_cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  131. if (pci_is_pcie(dev)) {
  132. edev->pcie_cap = pci_pcie_cap(dev);
  133. if (pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT)
  134. edev->mode |= EEH_DEV_ROOT_PORT;
  135. else if (pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM)
  136. edev->mode |= EEH_DEV_DS_PORT;
  137. edev->aer_cap = pci_find_ext_capability(dev,
  138. PCI_EXT_CAP_ID_ERR);
  139. }
  140. edev->config_addr = ((dev->bus->number << 8) | dev->devfn);
  141. edev->pe_config_addr = phb->bdfn_to_pe(phb, dev->bus, dev->devfn & 0xff);
  142. /* Create PE */
  143. ret = eeh_add_to_parent_pe(edev);
  144. if (ret) {
  145. pr_warn("%s: Can't add PCI dev %s to parent PE (%d)\n",
  146. __func__, pci_name(dev), ret);
  147. return ret;
  148. }
  149. /*
  150. * If the PE contains any one of following adapters, the
  151. * PCI config space can't be accessed when dumping EEH log.
  152. * Otherwise, we will run into fenced PHB caused by shortage
  153. * of outbound credits in the adapter. The PCI config access
  154. * should be blocked until PE reset. MMIO access is dropped
  155. * by hardware certainly. In order to drop PCI config requests,
  156. * one more flag (EEH_PE_CFG_RESTRICTED) is introduced, which
  157. * will be checked in the backend for PE state retrival. If
  158. * the PE becomes frozen for the first time and the flag has
  159. * been set for the PE, we will set EEH_PE_CFG_BLOCKED for
  160. * that PE to block its config space.
  161. *
  162. * Broadcom Austin 4-ports NICs (14e4:1657)
  163. * Broadcom Shiner 2-ports 10G NICs (14e4:168e)
  164. */
  165. if ((dev->vendor == PCI_VENDOR_ID_BROADCOM && dev->device == 0x1657) ||
  166. (dev->vendor == PCI_VENDOR_ID_BROADCOM && dev->device == 0x168e))
  167. edev->pe->state |= EEH_PE_CFG_RESTRICTED;
  168. /*
  169. * Cache the PE primary bus, which can't be fetched when
  170. * full hotplug is in progress. In that case, all child
  171. * PCI devices of the PE are expected to be removed prior
  172. * to PE reset.
  173. */
  174. if (!edev->pe->bus)
  175. edev->pe->bus = dev->bus;
  176. /*
  177. * Enable EEH explicitly so that we will do EEH check
  178. * while accessing I/O stuff
  179. */
  180. eeh_add_flag(EEH_ENABLED);
  181. /* Save memory bars */
  182. eeh_save_bars(edev);
  183. return 0;
  184. }
  185. /**
  186. * powernv_eeh_set_option - Initialize EEH or MMIO/DMA reenable
  187. * @pe: EEH PE
  188. * @option: operation to be issued
  189. *
  190. * The function is used to control the EEH functionality globally.
  191. * Currently, following options are support according to PAPR:
  192. * Enable EEH, Disable EEH, Enable MMIO and Enable DMA
  193. */
  194. static int powernv_eeh_set_option(struct eeh_pe *pe, int option)
  195. {
  196. struct pci_controller *hose = pe->phb;
  197. struct pnv_phb *phb = hose->private_data;
  198. int ret = -EEXIST;
  199. /*
  200. * What we need do is pass it down for hardware
  201. * implementation to handle it.
  202. */
  203. if (phb->eeh_ops && phb->eeh_ops->set_option)
  204. ret = phb->eeh_ops->set_option(pe, option);
  205. return ret;
  206. }
  207. /**
  208. * powernv_eeh_get_pe_addr - Retrieve PE address
  209. * @pe: EEH PE
  210. *
  211. * Retrieve the PE address according to the given tranditional
  212. * PCI BDF (Bus/Device/Function) address.
  213. */
  214. static int powernv_eeh_get_pe_addr(struct eeh_pe *pe)
  215. {
  216. return pe->addr;
  217. }
  218. /**
  219. * powernv_eeh_get_state - Retrieve PE state
  220. * @pe: EEH PE
  221. * @delay: delay while PE state is temporarily unavailable
  222. *
  223. * Retrieve the state of the specified PE. For IODA-compitable
  224. * platform, it should be retrieved from IODA table. Therefore,
  225. * we prefer passing down to hardware implementation to handle
  226. * it.
  227. */
  228. static int powernv_eeh_get_state(struct eeh_pe *pe, int *delay)
  229. {
  230. struct pci_controller *hose = pe->phb;
  231. struct pnv_phb *phb = hose->private_data;
  232. int ret = EEH_STATE_NOT_SUPPORT;
  233. if (phb->eeh_ops && phb->eeh_ops->get_state) {
  234. ret = phb->eeh_ops->get_state(pe);
  235. /*
  236. * If the PE state is temporarily unavailable,
  237. * to inform the EEH core delay for default
  238. * period (1 second)
  239. */
  240. if (delay) {
  241. *delay = 0;
  242. if (ret & EEH_STATE_UNAVAILABLE)
  243. *delay = 1000;
  244. }
  245. }
  246. return ret;
  247. }
  248. /**
  249. * powernv_eeh_reset - Reset the specified PE
  250. * @pe: EEH PE
  251. * @option: reset option
  252. *
  253. * Reset the specified PE
  254. */
  255. static int powernv_eeh_reset(struct eeh_pe *pe, int option)
  256. {
  257. struct pci_controller *hose = pe->phb;
  258. struct pnv_phb *phb = hose->private_data;
  259. int ret = -EEXIST;
  260. if (phb->eeh_ops && phb->eeh_ops->reset)
  261. ret = phb->eeh_ops->reset(pe, option);
  262. return ret;
  263. }
  264. /**
  265. * powernv_eeh_wait_state - Wait for PE state
  266. * @pe: EEH PE
  267. * @max_wait: maximal period in microsecond
  268. *
  269. * Wait for the state of associated PE. It might take some time
  270. * to retrieve the PE's state.
  271. */
  272. static int powernv_eeh_wait_state(struct eeh_pe *pe, int max_wait)
  273. {
  274. int ret;
  275. int mwait;
  276. while (1) {
  277. ret = powernv_eeh_get_state(pe, &mwait);
  278. /*
  279. * If the PE's state is temporarily unavailable,
  280. * we have to wait for the specified time. Otherwise,
  281. * the PE's state will be returned immediately.
  282. */
  283. if (ret != EEH_STATE_UNAVAILABLE)
  284. return ret;
  285. max_wait -= mwait;
  286. if (max_wait <= 0) {
  287. pr_warn("%s: Timeout getting PE#%x's state (%d)\n",
  288. __func__, pe->addr, max_wait);
  289. return EEH_STATE_NOT_SUPPORT;
  290. }
  291. msleep(mwait);
  292. }
  293. return EEH_STATE_NOT_SUPPORT;
  294. }
  295. /**
  296. * powernv_eeh_get_log - Retrieve error log
  297. * @pe: EEH PE
  298. * @severity: temporary or permanent error log
  299. * @drv_log: driver log to be combined with retrieved error log
  300. * @len: length of driver log
  301. *
  302. * Retrieve the temporary or permanent error from the PE.
  303. */
  304. static int powernv_eeh_get_log(struct eeh_pe *pe, int severity,
  305. char *drv_log, unsigned long len)
  306. {
  307. struct pci_controller *hose = pe->phb;
  308. struct pnv_phb *phb = hose->private_data;
  309. int ret = -EEXIST;
  310. if (phb->eeh_ops && phb->eeh_ops->get_log)
  311. ret = phb->eeh_ops->get_log(pe, severity, drv_log, len);
  312. return ret;
  313. }
  314. /**
  315. * powernv_eeh_configure_bridge - Configure PCI bridges in the indicated PE
  316. * @pe: EEH PE
  317. *
  318. * The function will be called to reconfigure the bridges included
  319. * in the specified PE so that the mulfunctional PE would be recovered
  320. * again.
  321. */
  322. static int powernv_eeh_configure_bridge(struct eeh_pe *pe)
  323. {
  324. struct pci_controller *hose = pe->phb;
  325. struct pnv_phb *phb = hose->private_data;
  326. int ret = 0;
  327. if (phb->eeh_ops && phb->eeh_ops->configure_bridge)
  328. ret = phb->eeh_ops->configure_bridge(pe);
  329. return ret;
  330. }
  331. /**
  332. * powernv_pe_err_inject - Inject specified error to the indicated PE
  333. * @pe: the indicated PE
  334. * @type: error type
  335. * @func: specific error type
  336. * @addr: address
  337. * @mask: address mask
  338. *
  339. * The routine is called to inject specified error, which is
  340. * determined by @type and @func, to the indicated PE for
  341. * testing purpose.
  342. */
  343. static int powernv_eeh_err_inject(struct eeh_pe *pe, int type, int func,
  344. unsigned long addr, unsigned long mask)
  345. {
  346. struct pci_controller *hose = pe->phb;
  347. struct pnv_phb *phb = hose->private_data;
  348. int ret = -EEXIST;
  349. if (phb->eeh_ops && phb->eeh_ops->err_inject)
  350. ret = phb->eeh_ops->err_inject(pe, type, func, addr, mask);
  351. return ret;
  352. }
  353. static inline bool powernv_eeh_cfg_blocked(struct device_node *dn)
  354. {
  355. struct eeh_dev *edev = of_node_to_eeh_dev(dn);
  356. if (!edev || !edev->pe)
  357. return false;
  358. if (edev->pe->state & EEH_PE_CFG_BLOCKED)
  359. return true;
  360. return false;
  361. }
  362. static int powernv_eeh_read_config(struct device_node *dn,
  363. int where, int size, u32 *val)
  364. {
  365. if (powernv_eeh_cfg_blocked(dn)) {
  366. *val = 0xFFFFFFFF;
  367. return PCIBIOS_SET_FAILED;
  368. }
  369. return pnv_pci_cfg_read(dn, where, size, val);
  370. }
  371. static int powernv_eeh_write_config(struct device_node *dn,
  372. int where, int size, u32 val)
  373. {
  374. if (powernv_eeh_cfg_blocked(dn))
  375. return PCIBIOS_SET_FAILED;
  376. return pnv_pci_cfg_write(dn, where, size, val);
  377. }
  378. /**
  379. * powernv_eeh_next_error - Retrieve next EEH error to handle
  380. * @pe: Affected PE
  381. *
  382. * Using OPAL API, to retrieve next EEH error for EEH core to handle
  383. */
  384. static int powernv_eeh_next_error(struct eeh_pe **pe)
  385. {
  386. struct pci_controller *hose;
  387. struct pnv_phb *phb = NULL;
  388. list_for_each_entry(hose, &hose_list, list_node) {
  389. phb = hose->private_data;
  390. break;
  391. }
  392. if (phb && phb->eeh_ops->next_error)
  393. return phb->eeh_ops->next_error(pe);
  394. return -EEXIST;
  395. }
  396. static int powernv_eeh_restore_config(struct device_node *dn)
  397. {
  398. struct eeh_dev *edev = of_node_to_eeh_dev(dn);
  399. struct pnv_phb *phb;
  400. s64 ret;
  401. if (!edev)
  402. return -EEXIST;
  403. phb = edev->phb->private_data;
  404. ret = opal_pci_reinit(phb->opal_id,
  405. OPAL_REINIT_PCI_DEV, edev->config_addr);
  406. if (ret) {
  407. pr_warn("%s: Can't reinit PCI dev 0x%x (%lld)\n",
  408. __func__, edev->config_addr, ret);
  409. return -EIO;
  410. }
  411. return 0;
  412. }
  413. static struct eeh_ops powernv_eeh_ops = {
  414. .name = "powernv",
  415. .init = powernv_eeh_init,
  416. .post_init = powernv_eeh_post_init,
  417. .of_probe = NULL,
  418. .dev_probe = powernv_eeh_dev_probe,
  419. .set_option = powernv_eeh_set_option,
  420. .get_pe_addr = powernv_eeh_get_pe_addr,
  421. .get_state = powernv_eeh_get_state,
  422. .reset = powernv_eeh_reset,
  423. .wait_state = powernv_eeh_wait_state,
  424. .get_log = powernv_eeh_get_log,
  425. .configure_bridge = powernv_eeh_configure_bridge,
  426. .err_inject = powernv_eeh_err_inject,
  427. .read_config = powernv_eeh_read_config,
  428. .write_config = powernv_eeh_write_config,
  429. .next_error = powernv_eeh_next_error,
  430. .restore_config = powernv_eeh_restore_config
  431. };
  432. /**
  433. * eeh_powernv_init - Register platform dependent EEH operations
  434. *
  435. * EEH initialization on powernv platform. This function should be
  436. * called before any EEH related functions.
  437. */
  438. static int __init eeh_powernv_init(void)
  439. {
  440. int ret = -EINVAL;
  441. eeh_set_pe_aux_size(PNV_PCI_DIAG_BUF_SIZE);
  442. ret = eeh_ops_register(&powernv_eeh_ops);
  443. if (!ret)
  444. pr_info("EEH: PowerNV platform initialized\n");
  445. else
  446. pr_info("EEH: Failed to initialize PowerNV platform (%d)\n", ret);
  447. return ret;
  448. }
  449. machine_early_initcall(powernv, eeh_powernv_init);