eeh-ioda.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * The file intends to implement the functions needed by EEH, which is
  3. * built on IODA compliant chip. Actually, lots of functions related
  4. * to EEH would be built based on the OPAL APIs.
  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/bootmem.h>
  14. #include <linux/delay.h>
  15. #include <linux/init.h>
  16. #include <linux/io.h>
  17. #include <linux/irq.h>
  18. #include <linux/kernel.h>
  19. #include <linux/msi.h>
  20. #include <linux/pci.h>
  21. #include <linux/string.h>
  22. #include <asm/eeh.h>
  23. #include <asm/eeh_event.h>
  24. #include <asm/io.h>
  25. #include <asm/iommu.h>
  26. #include <asm/msi_bitmap.h>
  27. #include <asm/opal.h>
  28. #include <asm/pci-bridge.h>
  29. #include <asm/ppc-pci.h>
  30. #include <asm/tce.h>
  31. #include "powernv.h"
  32. #include "pci.h"
  33. /**
  34. * ioda_eeh_post_init - Chip dependent post initialization
  35. * @hose: PCI controller
  36. *
  37. * The function will be called after eeh PEs and devices
  38. * have been built. That means the EEH is ready to supply
  39. * service with I/O cache.
  40. */
  41. static int ioda_eeh_post_init(struct pci_controller *hose)
  42. {
  43. struct pnv_phb *phb = hose->private_data;
  44. /* FIXME: Enable it for PHB3 later */
  45. if (phb->type == PNV_PHB_IODA1)
  46. phb->eeh_enabled = 1;
  47. return 0;
  48. }
  49. /**
  50. * ioda_eeh_set_option - Set EEH operation or I/O setting
  51. * @pe: EEH PE
  52. * @option: options
  53. *
  54. * Enable or disable EEH option for the indicated PE. The
  55. * function also can be used to enable I/O or DMA for the
  56. * PE.
  57. */
  58. static int ioda_eeh_set_option(struct eeh_pe *pe, int option)
  59. {
  60. s64 ret;
  61. u32 pe_no;
  62. struct pci_controller *hose = pe->phb;
  63. struct pnv_phb *phb = hose->private_data;
  64. /* Check on PE number */
  65. if (pe->addr < 0 || pe->addr >= phb->ioda.total_pe) {
  66. pr_err("%s: PE address %x out of range [0, %x] "
  67. "on PHB#%x\n",
  68. __func__, pe->addr, phb->ioda.total_pe,
  69. hose->global_number);
  70. return -EINVAL;
  71. }
  72. pe_no = pe->addr;
  73. switch (option) {
  74. case EEH_OPT_DISABLE:
  75. ret = -EEXIST;
  76. break;
  77. case EEH_OPT_ENABLE:
  78. ret = 0;
  79. break;
  80. case EEH_OPT_THAW_MMIO:
  81. ret = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no,
  82. OPAL_EEH_ACTION_CLEAR_FREEZE_MMIO);
  83. if (ret) {
  84. pr_warning("%s: Failed to enable MMIO for "
  85. "PHB#%x-PE#%x, err=%lld\n",
  86. __func__, hose->global_number, pe_no, ret);
  87. return -EIO;
  88. }
  89. break;
  90. case EEH_OPT_THAW_DMA:
  91. ret = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no,
  92. OPAL_EEH_ACTION_CLEAR_FREEZE_DMA);
  93. if (ret) {
  94. pr_warning("%s: Failed to enable DMA for "
  95. "PHB#%x-PE#%x, err=%lld\n",
  96. __func__, hose->global_number, pe_no, ret);
  97. return -EIO;
  98. }
  99. break;
  100. default:
  101. pr_warning("%s: Invalid option %d\n", __func__, option);
  102. return -EINVAL;
  103. }
  104. return ret;
  105. }
  106. /**
  107. * ioda_eeh_get_state - Retrieve the state of PE
  108. * @pe: EEH PE
  109. *
  110. * The PE's state should be retrieved from the PEEV, PEST
  111. * IODA tables. Since the OPAL has exported the function
  112. * to do it, it'd better to use that.
  113. */
  114. static int ioda_eeh_get_state(struct eeh_pe *pe)
  115. {
  116. s64 ret = 0;
  117. u8 fstate;
  118. u16 pcierr;
  119. u32 pe_no;
  120. int result;
  121. struct pci_controller *hose = pe->phb;
  122. struct pnv_phb *phb = hose->private_data;
  123. /*
  124. * Sanity check on PE address. The PHB PE address should
  125. * be zero.
  126. */
  127. if (pe->addr < 0 || pe->addr >= phb->ioda.total_pe) {
  128. pr_err("%s: PE address %x out of range [0, %x] "
  129. "on PHB#%x\n",
  130. __func__, pe->addr, phb->ioda.total_pe,
  131. hose->global_number);
  132. return EEH_STATE_NOT_SUPPORT;
  133. }
  134. /* Retrieve PE status through OPAL */
  135. pe_no = pe->addr;
  136. ret = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
  137. &fstate, &pcierr, NULL);
  138. if (ret) {
  139. pr_err("%s: Failed to get EEH status on "
  140. "PHB#%x-PE#%x\n, err=%lld\n",
  141. __func__, hose->global_number, pe_no, ret);
  142. return EEH_STATE_NOT_SUPPORT;
  143. }
  144. /* Check PHB status */
  145. if (pe->type & EEH_PE_PHB) {
  146. result = 0;
  147. result &= ~EEH_STATE_RESET_ACTIVE;
  148. if (pcierr != OPAL_EEH_PHB_ERROR) {
  149. result |= EEH_STATE_MMIO_ACTIVE;
  150. result |= EEH_STATE_DMA_ACTIVE;
  151. result |= EEH_STATE_MMIO_ENABLED;
  152. result |= EEH_STATE_DMA_ENABLED;
  153. }
  154. return result;
  155. }
  156. /* Parse result out */
  157. result = 0;
  158. switch (fstate) {
  159. case OPAL_EEH_STOPPED_NOT_FROZEN:
  160. result &= ~EEH_STATE_RESET_ACTIVE;
  161. result |= EEH_STATE_MMIO_ACTIVE;
  162. result |= EEH_STATE_DMA_ACTIVE;
  163. result |= EEH_STATE_MMIO_ENABLED;
  164. result |= EEH_STATE_DMA_ENABLED;
  165. break;
  166. case OPAL_EEH_STOPPED_MMIO_FREEZE:
  167. result &= ~EEH_STATE_RESET_ACTIVE;
  168. result |= EEH_STATE_DMA_ACTIVE;
  169. result |= EEH_STATE_DMA_ENABLED;
  170. break;
  171. case OPAL_EEH_STOPPED_DMA_FREEZE:
  172. result &= ~EEH_STATE_RESET_ACTIVE;
  173. result |= EEH_STATE_MMIO_ACTIVE;
  174. result |= EEH_STATE_MMIO_ENABLED;
  175. break;
  176. case OPAL_EEH_STOPPED_MMIO_DMA_FREEZE:
  177. result &= ~EEH_STATE_RESET_ACTIVE;
  178. break;
  179. case OPAL_EEH_STOPPED_RESET:
  180. result |= EEH_STATE_RESET_ACTIVE;
  181. break;
  182. case OPAL_EEH_STOPPED_TEMP_UNAVAIL:
  183. result |= EEH_STATE_UNAVAILABLE;
  184. break;
  185. case OPAL_EEH_STOPPED_PERM_UNAVAIL:
  186. result |= EEH_STATE_NOT_SUPPORT;
  187. break;
  188. default:
  189. pr_warning("%s: Unexpected EEH status 0x%x "
  190. "on PHB#%x-PE#%x\n",
  191. __func__, fstate, hose->global_number, pe_no);
  192. }
  193. return result;
  194. }
  195. static int ioda_eeh_pe_clear(struct eeh_pe *pe)
  196. {
  197. struct pci_controller *hose;
  198. struct pnv_phb *phb;
  199. u32 pe_no;
  200. u8 fstate;
  201. u16 pcierr;
  202. s64 ret;
  203. pe_no = pe->addr;
  204. hose = pe->phb;
  205. phb = pe->phb->private_data;
  206. /* Clear the EEH error on the PE */
  207. ret = opal_pci_eeh_freeze_clear(phb->opal_id,
  208. pe_no, OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  209. if (ret) {
  210. pr_err("%s: Failed to clear EEH error for "
  211. "PHB#%x-PE#%x, err=%lld\n",
  212. __func__, hose->global_number, pe_no, ret);
  213. return -EIO;
  214. }
  215. /*
  216. * Read the PE state back and verify that the frozen
  217. * state has been removed.
  218. */
  219. ret = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
  220. &fstate, &pcierr, NULL);
  221. if (ret) {
  222. pr_err("%s: Failed to get EEH status on "
  223. "PHB#%x-PE#%x\n, err=%lld\n",
  224. __func__, hose->global_number, pe_no, ret);
  225. return -EIO;
  226. }
  227. if (fstate != OPAL_EEH_STOPPED_NOT_FROZEN) {
  228. pr_err("%s: Frozen state not cleared on "
  229. "PHB#%x-PE#%x, sts=%x\n",
  230. __func__, hose->global_number, pe_no, fstate);
  231. return -EIO;
  232. }
  233. return 0;
  234. }
  235. static s64 ioda_eeh_phb_poll(struct pnv_phb *phb)
  236. {
  237. s64 rc = OPAL_HARDWARE;
  238. while (1) {
  239. rc = opal_pci_poll(phb->opal_id);
  240. if (rc <= 0)
  241. break;
  242. msleep(rc);
  243. }
  244. return rc;
  245. }
  246. static int ioda_eeh_phb_reset(struct pci_controller *hose, int option)
  247. {
  248. struct pnv_phb *phb = hose->private_data;
  249. s64 rc = OPAL_HARDWARE;
  250. pr_debug("%s: Reset PHB#%x, option=%d\n",
  251. __func__, hose->global_number, option);
  252. /* Issue PHB complete reset request */
  253. if (option == EEH_RESET_FUNDAMENTAL ||
  254. option == EEH_RESET_HOT)
  255. rc = opal_pci_reset(phb->opal_id,
  256. OPAL_PHB_COMPLETE,
  257. OPAL_ASSERT_RESET);
  258. else if (option == EEH_RESET_DEACTIVATE)
  259. rc = opal_pci_reset(phb->opal_id,
  260. OPAL_PHB_COMPLETE,
  261. OPAL_DEASSERT_RESET);
  262. if (rc < 0)
  263. goto out;
  264. /*
  265. * Poll state of the PHB until the request is done
  266. * successfully.
  267. */
  268. rc = ioda_eeh_phb_poll(phb);
  269. out:
  270. if (rc != OPAL_SUCCESS)
  271. return -EIO;
  272. return 0;
  273. }
  274. static int ioda_eeh_root_reset(struct pci_controller *hose, int option)
  275. {
  276. struct pnv_phb *phb = hose->private_data;
  277. s64 rc = OPAL_SUCCESS;
  278. pr_debug("%s: Reset PHB#%x, option=%d\n",
  279. __func__, hose->global_number, option);
  280. /*
  281. * During the reset deassert time, we needn't care
  282. * the reset scope because the firmware does nothing
  283. * for fundamental or hot reset during deassert phase.
  284. */
  285. if (option == EEH_RESET_FUNDAMENTAL)
  286. rc = opal_pci_reset(phb->opal_id,
  287. OPAL_PCI_FUNDAMENTAL_RESET,
  288. OPAL_ASSERT_RESET);
  289. else if (option == EEH_RESET_HOT)
  290. rc = opal_pci_reset(phb->opal_id,
  291. OPAL_PCI_HOT_RESET,
  292. OPAL_ASSERT_RESET);
  293. else if (option == EEH_RESET_DEACTIVATE)
  294. rc = opal_pci_reset(phb->opal_id,
  295. OPAL_PCI_HOT_RESET,
  296. OPAL_DEASSERT_RESET);
  297. if (rc < 0)
  298. goto out;
  299. /* Poll state of the PHB until the request is done */
  300. rc = ioda_eeh_phb_poll(phb);
  301. out:
  302. if (rc != OPAL_SUCCESS)
  303. return -EIO;
  304. return 0;
  305. }
  306. static int ioda_eeh_bridge_reset(struct pci_controller *hose,
  307. struct pci_dev *dev, int option)
  308. {
  309. u16 ctrl;
  310. pr_debug("%s: Reset device %04x:%02x:%02x.%01x with option %d\n",
  311. __func__, hose->global_number, dev->bus->number,
  312. PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn), option);
  313. switch (option) {
  314. case EEH_RESET_FUNDAMENTAL:
  315. case EEH_RESET_HOT:
  316. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &ctrl);
  317. ctrl |= PCI_BRIDGE_CTL_BUS_RESET;
  318. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl);
  319. break;
  320. case EEH_RESET_DEACTIVATE:
  321. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &ctrl);
  322. ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
  323. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl);
  324. break;
  325. }
  326. return 0;
  327. }
  328. /**
  329. * ioda_eeh_reset - Reset the indicated PE
  330. * @pe: EEH PE
  331. * @option: reset option
  332. *
  333. * Do reset on the indicated PE. For PCI bus sensitive PE,
  334. * we need to reset the parent p2p bridge. The PHB has to
  335. * be reinitialized if the p2p bridge is root bridge. For
  336. * PCI device sensitive PE, we will try to reset the device
  337. * through FLR. For now, we don't have OPAL APIs to do HARD
  338. * reset yet, so all reset would be SOFT (HOT) reset.
  339. */
  340. static int ioda_eeh_reset(struct eeh_pe *pe, int option)
  341. {
  342. struct pci_controller *hose = pe->phb;
  343. struct eeh_dev *edev;
  344. struct pci_dev *dev;
  345. int ret;
  346. /*
  347. * Anyway, we have to clear the problematic state for the
  348. * corresponding PE. However, we needn't do it if the PE
  349. * is PHB associated. That means the PHB is having fatal
  350. * errors and it needs reset. Further more, the AIB interface
  351. * isn't reliable any more.
  352. */
  353. if (!(pe->type & EEH_PE_PHB) &&
  354. (option == EEH_RESET_HOT ||
  355. option == EEH_RESET_FUNDAMENTAL)) {
  356. ret = ioda_eeh_pe_clear(pe);
  357. if (ret)
  358. return -EIO;
  359. }
  360. /*
  361. * The rules applied to reset, either fundamental or hot reset:
  362. *
  363. * We always reset the direct upstream bridge of the PE. If the
  364. * direct upstream bridge isn't root bridge, we always take hot
  365. * reset no matter what option (fundamental or hot) is. Otherwise,
  366. * we should do the reset according to the required option.
  367. */
  368. if (pe->type & EEH_PE_PHB) {
  369. ret = ioda_eeh_phb_reset(hose, option);
  370. } else {
  371. if (pe->type & EEH_PE_DEVICE) {
  372. /*
  373. * If it's device PE, we didn't refer to the parent
  374. * PCI bus yet. So we have to figure it out indirectly.
  375. */
  376. edev = list_first_entry(&pe->edevs,
  377. struct eeh_dev, list);
  378. dev = eeh_dev_to_pci_dev(edev);
  379. dev = dev->bus->self;
  380. } else {
  381. /*
  382. * If it's bus PE, the parent PCI bus is already there
  383. * and just pick it up.
  384. */
  385. dev = pe->bus->self;
  386. }
  387. /*
  388. * Do reset based on the fact that the direct upstream bridge
  389. * is root bridge (port) or not.
  390. */
  391. if (dev->bus->number == 0)
  392. ret = ioda_eeh_root_reset(hose, option);
  393. else
  394. ret = ioda_eeh_bridge_reset(hose, dev, option);
  395. }
  396. return ret;
  397. }
  398. /**
  399. * ioda_eeh_get_log - Retrieve error log
  400. * @pe: EEH PE
  401. * @severity: Severity level of the log
  402. * @drv_log: buffer to store the log
  403. * @len: space of the log buffer
  404. *
  405. * The function is used to retrieve error log from P7IOC.
  406. */
  407. static int ioda_eeh_get_log(struct eeh_pe *pe, int severity,
  408. char *drv_log, unsigned long len)
  409. {
  410. s64 ret;
  411. unsigned long flags;
  412. struct pci_controller *hose = pe->phb;
  413. struct pnv_phb *phb = hose->private_data;
  414. spin_lock_irqsave(&phb->lock, flags);
  415. ret = opal_pci_get_phb_diag_data2(phb->opal_id,
  416. phb->diag.blob, PNV_PCI_DIAG_BUF_SIZE);
  417. if (ret) {
  418. spin_unlock_irqrestore(&phb->lock, flags);
  419. pr_warning("%s: Failed to get log for PHB#%x-PE#%x\n",
  420. __func__, hose->global_number, pe->addr);
  421. return -EIO;
  422. }
  423. /*
  424. * FIXME: We probably need log the error in somewhere.
  425. * Lets make it up in future.
  426. */
  427. /* pr_info("%s", phb->diag.blob); */
  428. spin_unlock_irqrestore(&phb->lock, flags);
  429. return 0;
  430. }
  431. /**
  432. * ioda_eeh_configure_bridge - Configure the PCI bridges for the indicated PE
  433. * @pe: EEH PE
  434. *
  435. * For particular PE, it might have included PCI bridges. In order
  436. * to make the PE work properly, those PCI bridges should be configured
  437. * correctly. However, we need do nothing on P7IOC since the reset
  438. * function will do everything that should be covered by the function.
  439. */
  440. static int ioda_eeh_configure_bridge(struct eeh_pe *pe)
  441. {
  442. return 0;
  443. }
  444. struct pnv_eeh_ops ioda_eeh_ops = {
  445. .post_init = ioda_eeh_post_init,
  446. .set_option = ioda_eeh_set_option,
  447. .get_state = ioda_eeh_get_state,
  448. .reset = ioda_eeh_reset,
  449. .get_log = ioda_eeh_get_log,
  450. .configure_bridge = ioda_eeh_configure_bridge,
  451. .next_error = NULL
  452. };