eeh-ioda.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  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/debugfs.h>
  15. #include <linux/delay.h>
  16. #include <linux/io.h>
  17. #include <linux/irq.h>
  18. #include <linux/kernel.h>
  19. #include <linux/msi.h>
  20. #include <linux/notifier.h>
  21. #include <linux/pci.h>
  22. #include <linux/string.h>
  23. #include <asm/eeh.h>
  24. #include <asm/eeh_event.h>
  25. #include <asm/io.h>
  26. #include <asm/iommu.h>
  27. #include <asm/msi_bitmap.h>
  28. #include <asm/opal.h>
  29. #include <asm/pci-bridge.h>
  30. #include <asm/ppc-pci.h>
  31. #include <asm/tce.h>
  32. #include "powernv.h"
  33. #include "pci.h"
  34. static int ioda_eeh_nb_init = 0;
  35. static int ioda_eeh_event(struct notifier_block *nb,
  36. unsigned long events, void *change)
  37. {
  38. uint64_t changed_evts = (uint64_t)change;
  39. /*
  40. * We simply send special EEH event if EEH has
  41. * been enabled, or clear pending events in
  42. * case that we enable EEH soon
  43. */
  44. if (!(changed_evts & OPAL_EVENT_PCI_ERROR) ||
  45. !(events & OPAL_EVENT_PCI_ERROR))
  46. return 0;
  47. if (eeh_enabled())
  48. eeh_send_failure_event(NULL);
  49. else
  50. opal_notifier_update_evt(OPAL_EVENT_PCI_ERROR, 0x0ul);
  51. return 0;
  52. }
  53. static struct notifier_block ioda_eeh_nb = {
  54. .notifier_call = ioda_eeh_event,
  55. .next = NULL,
  56. .priority = 0
  57. };
  58. #ifdef CONFIG_DEBUG_FS
  59. static ssize_t ioda_eeh_ei_write(struct file *filp,
  60. const char __user *user_buf,
  61. size_t count, loff_t *ppos)
  62. {
  63. struct pci_controller *hose = filp->private_data;
  64. struct pnv_phb *phb = hose->private_data;
  65. struct eeh_dev *edev;
  66. struct eeh_pe *pe;
  67. int pe_no, type, func;
  68. unsigned long addr, mask;
  69. char buf[50];
  70. int ret;
  71. if (!phb->eeh_ops || !phb->eeh_ops->err_inject)
  72. return -ENXIO;
  73. ret = simple_write_to_buffer(buf, sizeof(buf), ppos, user_buf, count);
  74. if (!ret)
  75. return -EFAULT;
  76. /* Retrieve parameters */
  77. ret = sscanf(buf, "%x:%x:%x:%lx:%lx",
  78. &pe_no, &type, &func, &addr, &mask);
  79. if (ret != 5)
  80. return -EINVAL;
  81. /* Retrieve PE */
  82. edev = kzalloc(sizeof(*edev), GFP_KERNEL);
  83. if (!edev)
  84. return -ENOMEM;
  85. edev->phb = hose;
  86. edev->pe_config_addr = pe_no;
  87. pe = eeh_pe_get(edev);
  88. kfree(edev);
  89. if (!pe)
  90. return -ENODEV;
  91. /* Do error injection */
  92. ret = phb->eeh_ops->err_inject(pe, type, func, addr, mask);
  93. return ret < 0 ? ret : count;
  94. }
  95. static const struct file_operations ioda_eeh_ei_fops = {
  96. .open = simple_open,
  97. .llseek = no_llseek,
  98. .write = ioda_eeh_ei_write,
  99. };
  100. static int ioda_eeh_dbgfs_set(void *data, int offset, u64 val)
  101. {
  102. struct pci_controller *hose = data;
  103. struct pnv_phb *phb = hose->private_data;
  104. out_be64(phb->regs + offset, val);
  105. return 0;
  106. }
  107. static int ioda_eeh_dbgfs_get(void *data, int offset, u64 *val)
  108. {
  109. struct pci_controller *hose = data;
  110. struct pnv_phb *phb = hose->private_data;
  111. *val = in_be64(phb->regs + offset);
  112. return 0;
  113. }
  114. static int ioda_eeh_outb_dbgfs_set(void *data, u64 val)
  115. {
  116. return ioda_eeh_dbgfs_set(data, 0xD10, val);
  117. }
  118. static int ioda_eeh_outb_dbgfs_get(void *data, u64 *val)
  119. {
  120. return ioda_eeh_dbgfs_get(data, 0xD10, val);
  121. }
  122. static int ioda_eeh_inbA_dbgfs_set(void *data, u64 val)
  123. {
  124. return ioda_eeh_dbgfs_set(data, 0xD90, val);
  125. }
  126. static int ioda_eeh_inbA_dbgfs_get(void *data, u64 *val)
  127. {
  128. return ioda_eeh_dbgfs_get(data, 0xD90, val);
  129. }
  130. static int ioda_eeh_inbB_dbgfs_set(void *data, u64 val)
  131. {
  132. return ioda_eeh_dbgfs_set(data, 0xE10, val);
  133. }
  134. static int ioda_eeh_inbB_dbgfs_get(void *data, u64 *val)
  135. {
  136. return ioda_eeh_dbgfs_get(data, 0xE10, val);
  137. }
  138. DEFINE_SIMPLE_ATTRIBUTE(ioda_eeh_outb_dbgfs_ops, ioda_eeh_outb_dbgfs_get,
  139. ioda_eeh_outb_dbgfs_set, "0x%llx\n");
  140. DEFINE_SIMPLE_ATTRIBUTE(ioda_eeh_inbA_dbgfs_ops, ioda_eeh_inbA_dbgfs_get,
  141. ioda_eeh_inbA_dbgfs_set, "0x%llx\n");
  142. DEFINE_SIMPLE_ATTRIBUTE(ioda_eeh_inbB_dbgfs_ops, ioda_eeh_inbB_dbgfs_get,
  143. ioda_eeh_inbB_dbgfs_set, "0x%llx\n");
  144. #endif /* CONFIG_DEBUG_FS */
  145. /**
  146. * ioda_eeh_post_init - Chip dependent post initialization
  147. * @hose: PCI controller
  148. *
  149. * The function will be called after eeh PEs and devices
  150. * have been built. That means the EEH is ready to supply
  151. * service with I/O cache.
  152. */
  153. static int ioda_eeh_post_init(struct pci_controller *hose)
  154. {
  155. struct pnv_phb *phb = hose->private_data;
  156. int ret;
  157. /* Register OPAL event notifier */
  158. if (!ioda_eeh_nb_init) {
  159. ret = opal_notifier_register(&ioda_eeh_nb);
  160. if (ret) {
  161. pr_err("%s: Can't register OPAL event notifier (%d)\n",
  162. __func__, ret);
  163. return ret;
  164. }
  165. ioda_eeh_nb_init = 1;
  166. }
  167. #ifdef CONFIG_DEBUG_FS
  168. if (!phb->has_dbgfs && phb->dbgfs) {
  169. phb->has_dbgfs = 1;
  170. debugfs_create_file("err_injct", 0200,
  171. phb->dbgfs, hose,
  172. &ioda_eeh_ei_fops);
  173. debugfs_create_file("err_injct_outbound", 0600,
  174. phb->dbgfs, hose,
  175. &ioda_eeh_outb_dbgfs_ops);
  176. debugfs_create_file("err_injct_inboundA", 0600,
  177. phb->dbgfs, hose,
  178. &ioda_eeh_inbA_dbgfs_ops);
  179. debugfs_create_file("err_injct_inboundB", 0600,
  180. phb->dbgfs, hose,
  181. &ioda_eeh_inbB_dbgfs_ops);
  182. }
  183. #endif
  184. /* If EEH is enabled, we're going to rely on that.
  185. * Otherwise, we restore to conventional mechanism
  186. * to clear frozen PE during PCI config access.
  187. */
  188. if (eeh_enabled())
  189. phb->flags |= PNV_PHB_FLAG_EEH;
  190. else
  191. phb->flags &= ~PNV_PHB_FLAG_EEH;
  192. return 0;
  193. }
  194. /**
  195. * ioda_eeh_set_option - Set EEH operation or I/O setting
  196. * @pe: EEH PE
  197. * @option: options
  198. *
  199. * Enable or disable EEH option for the indicated PE. The
  200. * function also can be used to enable I/O or DMA for the
  201. * PE.
  202. */
  203. static int ioda_eeh_set_option(struct eeh_pe *pe, int option)
  204. {
  205. struct pci_controller *hose = pe->phb;
  206. struct pnv_phb *phb = hose->private_data;
  207. bool freeze_pe = false;
  208. int enable, ret = 0;
  209. s64 rc;
  210. /* Check on PE number */
  211. if (pe->addr < 0 || pe->addr >= phb->ioda.total_pe) {
  212. pr_err("%s: PE address %x out of range [0, %x] "
  213. "on PHB#%x\n",
  214. __func__, pe->addr, phb->ioda.total_pe,
  215. hose->global_number);
  216. return -EINVAL;
  217. }
  218. switch (option) {
  219. case EEH_OPT_DISABLE:
  220. return -EPERM;
  221. case EEH_OPT_ENABLE:
  222. return 0;
  223. case EEH_OPT_THAW_MMIO:
  224. enable = OPAL_EEH_ACTION_CLEAR_FREEZE_MMIO;
  225. break;
  226. case EEH_OPT_THAW_DMA:
  227. enable = OPAL_EEH_ACTION_CLEAR_FREEZE_DMA;
  228. break;
  229. case EEH_OPT_FREEZE_PE:
  230. freeze_pe = true;
  231. enable = OPAL_EEH_ACTION_SET_FREEZE_ALL;
  232. break;
  233. default:
  234. pr_warn("%s: Invalid option %d\n",
  235. __func__, option);
  236. return -EINVAL;
  237. }
  238. /* If PHB supports compound PE, to handle it */
  239. if (freeze_pe) {
  240. if (phb->freeze_pe) {
  241. phb->freeze_pe(phb, pe->addr);
  242. } else {
  243. rc = opal_pci_eeh_freeze_set(phb->opal_id,
  244. pe->addr,
  245. enable);
  246. if (rc != OPAL_SUCCESS) {
  247. pr_warn("%s: Failure %lld freezing "
  248. "PHB#%x-PE#%x\n",
  249. __func__, rc,
  250. phb->hose->global_number, pe->addr);
  251. ret = -EIO;
  252. }
  253. }
  254. } else {
  255. if (phb->unfreeze_pe) {
  256. ret = phb->unfreeze_pe(phb, pe->addr, enable);
  257. } else {
  258. rc = opal_pci_eeh_freeze_clear(phb->opal_id,
  259. pe->addr,
  260. enable);
  261. if (rc != OPAL_SUCCESS) {
  262. pr_warn("%s: Failure %lld enable %d "
  263. "for PHB#%x-PE#%x\n",
  264. __func__, rc, option,
  265. phb->hose->global_number, pe->addr);
  266. ret = -EIO;
  267. }
  268. }
  269. }
  270. return ret;
  271. }
  272. static void ioda_eeh_phb_diag(struct eeh_pe *pe)
  273. {
  274. struct pnv_phb *phb = pe->phb->private_data;
  275. long rc;
  276. rc = opal_pci_get_phb_diag_data2(phb->opal_id, pe->data,
  277. PNV_PCI_DIAG_BUF_SIZE);
  278. if (rc != OPAL_SUCCESS)
  279. pr_warn("%s: Failed to get diag-data for PHB#%x (%ld)\n",
  280. __func__, pe->phb->global_number, rc);
  281. }
  282. static int ioda_eeh_get_phb_state(struct eeh_pe *pe)
  283. {
  284. struct pnv_phb *phb = pe->phb->private_data;
  285. u8 fstate;
  286. __be16 pcierr;
  287. s64 rc;
  288. int result = 0;
  289. rc = opal_pci_eeh_freeze_status(phb->opal_id,
  290. pe->addr,
  291. &fstate,
  292. &pcierr,
  293. NULL);
  294. if (rc != OPAL_SUCCESS) {
  295. pr_warn("%s: Failure %lld getting PHB#%x state\n",
  296. __func__, rc, phb->hose->global_number);
  297. return EEH_STATE_NOT_SUPPORT;
  298. }
  299. /*
  300. * Check PHB state. If the PHB is frozen for the
  301. * first time, to dump the PHB diag-data.
  302. */
  303. if (be16_to_cpu(pcierr) != OPAL_EEH_PHB_ERROR) {
  304. result = (EEH_STATE_MMIO_ACTIVE |
  305. EEH_STATE_DMA_ACTIVE |
  306. EEH_STATE_MMIO_ENABLED |
  307. EEH_STATE_DMA_ENABLED);
  308. } else if (!(pe->state & EEH_PE_ISOLATED)) {
  309. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  310. ioda_eeh_phb_diag(pe);
  311. }
  312. return result;
  313. }
  314. static int ioda_eeh_get_pe_state(struct eeh_pe *pe)
  315. {
  316. struct pnv_phb *phb = pe->phb->private_data;
  317. u8 fstate;
  318. __be16 pcierr;
  319. s64 rc;
  320. int result;
  321. /*
  322. * We don't clobber hardware frozen state until PE
  323. * reset is completed. In order to keep EEH core
  324. * moving forward, we have to return operational
  325. * state during PE reset.
  326. */
  327. if (pe->state & EEH_PE_CFG_BLOCKED) {
  328. result = (EEH_STATE_MMIO_ACTIVE |
  329. EEH_STATE_DMA_ACTIVE |
  330. EEH_STATE_MMIO_ENABLED |
  331. EEH_STATE_DMA_ENABLED);
  332. return result;
  333. }
  334. /*
  335. * Fetch PE state from hardware. If the PHB
  336. * supports compound PE, let it handle that.
  337. */
  338. if (phb->get_pe_state) {
  339. fstate = phb->get_pe_state(phb, pe->addr);
  340. } else {
  341. rc = opal_pci_eeh_freeze_status(phb->opal_id,
  342. pe->addr,
  343. &fstate,
  344. &pcierr,
  345. NULL);
  346. if (rc != OPAL_SUCCESS) {
  347. pr_warn("%s: Failure %lld getting PHB#%x-PE%x state\n",
  348. __func__, rc, phb->hose->global_number, pe->addr);
  349. return EEH_STATE_NOT_SUPPORT;
  350. }
  351. }
  352. /* Figure out state */
  353. switch (fstate) {
  354. case OPAL_EEH_STOPPED_NOT_FROZEN:
  355. result = (EEH_STATE_MMIO_ACTIVE |
  356. EEH_STATE_DMA_ACTIVE |
  357. EEH_STATE_MMIO_ENABLED |
  358. EEH_STATE_DMA_ENABLED);
  359. break;
  360. case OPAL_EEH_STOPPED_MMIO_FREEZE:
  361. result = (EEH_STATE_DMA_ACTIVE |
  362. EEH_STATE_DMA_ENABLED);
  363. break;
  364. case OPAL_EEH_STOPPED_DMA_FREEZE:
  365. result = (EEH_STATE_MMIO_ACTIVE |
  366. EEH_STATE_MMIO_ENABLED);
  367. break;
  368. case OPAL_EEH_STOPPED_MMIO_DMA_FREEZE:
  369. result = 0;
  370. break;
  371. case OPAL_EEH_STOPPED_RESET:
  372. result = EEH_STATE_RESET_ACTIVE;
  373. break;
  374. case OPAL_EEH_STOPPED_TEMP_UNAVAIL:
  375. result = EEH_STATE_UNAVAILABLE;
  376. break;
  377. case OPAL_EEH_STOPPED_PERM_UNAVAIL:
  378. result = EEH_STATE_NOT_SUPPORT;
  379. break;
  380. default:
  381. result = EEH_STATE_NOT_SUPPORT;
  382. pr_warn("%s: Invalid PHB#%x-PE#%x state %x\n",
  383. __func__, phb->hose->global_number,
  384. pe->addr, fstate);
  385. }
  386. /*
  387. * If PHB supports compound PE, to freeze all
  388. * slave PEs for consistency.
  389. *
  390. * If the PE is switching to frozen state for the
  391. * first time, to dump the PHB diag-data.
  392. */
  393. if (!(result & EEH_STATE_NOT_SUPPORT) &&
  394. !(result & EEH_STATE_UNAVAILABLE) &&
  395. !(result & EEH_STATE_MMIO_ACTIVE) &&
  396. !(result & EEH_STATE_DMA_ACTIVE) &&
  397. !(pe->state & EEH_PE_ISOLATED)) {
  398. if (phb->freeze_pe)
  399. phb->freeze_pe(phb, pe->addr);
  400. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  401. ioda_eeh_phb_diag(pe);
  402. }
  403. return result;
  404. }
  405. /**
  406. * ioda_eeh_get_state - Retrieve the state of PE
  407. * @pe: EEH PE
  408. *
  409. * The PE's state should be retrieved from the PEEV, PEST
  410. * IODA tables. Since the OPAL has exported the function
  411. * to do it, it'd better to use that.
  412. */
  413. static int ioda_eeh_get_state(struct eeh_pe *pe)
  414. {
  415. struct pnv_phb *phb = pe->phb->private_data;
  416. /* Sanity check on PE number. PHB PE should have 0 */
  417. if (pe->addr < 0 ||
  418. pe->addr >= phb->ioda.total_pe) {
  419. pr_warn("%s: PHB#%x-PE#%x out of range [0, %x]\n",
  420. __func__, phb->hose->global_number,
  421. pe->addr, phb->ioda.total_pe);
  422. return EEH_STATE_NOT_SUPPORT;
  423. }
  424. if (pe->type & EEH_PE_PHB)
  425. return ioda_eeh_get_phb_state(pe);
  426. return ioda_eeh_get_pe_state(pe);
  427. }
  428. static s64 ioda_eeh_phb_poll(struct pnv_phb *phb)
  429. {
  430. s64 rc = OPAL_HARDWARE;
  431. while (1) {
  432. rc = opal_pci_poll(phb->opal_id);
  433. if (rc <= 0)
  434. break;
  435. if (system_state < SYSTEM_RUNNING)
  436. udelay(1000 * rc);
  437. else
  438. msleep(rc);
  439. }
  440. return rc;
  441. }
  442. int ioda_eeh_phb_reset(struct pci_controller *hose, int option)
  443. {
  444. struct pnv_phb *phb = hose->private_data;
  445. s64 rc = OPAL_HARDWARE;
  446. pr_debug("%s: Reset PHB#%x, option=%d\n",
  447. __func__, hose->global_number, option);
  448. /* Issue PHB complete reset request */
  449. if (option == EEH_RESET_FUNDAMENTAL ||
  450. option == EEH_RESET_HOT)
  451. rc = opal_pci_reset(phb->opal_id,
  452. OPAL_RESET_PHB_COMPLETE,
  453. OPAL_ASSERT_RESET);
  454. else if (option == EEH_RESET_DEACTIVATE)
  455. rc = opal_pci_reset(phb->opal_id,
  456. OPAL_RESET_PHB_COMPLETE,
  457. OPAL_DEASSERT_RESET);
  458. if (rc < 0)
  459. goto out;
  460. /*
  461. * Poll state of the PHB until the request is done
  462. * successfully. The PHB reset is usually PHB complete
  463. * reset followed by hot reset on root bus. So we also
  464. * need the PCI bus settlement delay.
  465. */
  466. rc = ioda_eeh_phb_poll(phb);
  467. if (option == EEH_RESET_DEACTIVATE) {
  468. if (system_state < SYSTEM_RUNNING)
  469. udelay(1000 * EEH_PE_RST_SETTLE_TIME);
  470. else
  471. msleep(EEH_PE_RST_SETTLE_TIME);
  472. }
  473. out:
  474. if (rc != OPAL_SUCCESS)
  475. return -EIO;
  476. return 0;
  477. }
  478. static int ioda_eeh_root_reset(struct pci_controller *hose, int option)
  479. {
  480. struct pnv_phb *phb = hose->private_data;
  481. s64 rc = OPAL_SUCCESS;
  482. pr_debug("%s: Reset PHB#%x, option=%d\n",
  483. __func__, hose->global_number, option);
  484. /*
  485. * During the reset deassert time, we needn't care
  486. * the reset scope because the firmware does nothing
  487. * for fundamental or hot reset during deassert phase.
  488. */
  489. if (option == EEH_RESET_FUNDAMENTAL)
  490. rc = opal_pci_reset(phb->opal_id,
  491. OPAL_RESET_PCI_FUNDAMENTAL,
  492. OPAL_ASSERT_RESET);
  493. else if (option == EEH_RESET_HOT)
  494. rc = opal_pci_reset(phb->opal_id,
  495. OPAL_RESET_PCI_HOT,
  496. OPAL_ASSERT_RESET);
  497. else if (option == EEH_RESET_DEACTIVATE)
  498. rc = opal_pci_reset(phb->opal_id,
  499. OPAL_RESET_PCI_HOT,
  500. OPAL_DEASSERT_RESET);
  501. if (rc < 0)
  502. goto out;
  503. /* Poll state of the PHB until the request is done */
  504. rc = ioda_eeh_phb_poll(phb);
  505. if (option == EEH_RESET_DEACTIVATE)
  506. msleep(EEH_PE_RST_SETTLE_TIME);
  507. out:
  508. if (rc != OPAL_SUCCESS)
  509. return -EIO;
  510. return 0;
  511. }
  512. static int ioda_eeh_bridge_reset(struct pci_dev *dev, int option)
  513. {
  514. struct device_node *dn = pci_device_to_OF_node(dev);
  515. struct eeh_dev *edev = of_node_to_eeh_dev(dn);
  516. int aer = edev ? edev->aer_cap : 0;
  517. u32 ctrl;
  518. pr_debug("%s: Reset PCI bus %04x:%02x with option %d\n",
  519. __func__, pci_domain_nr(dev->bus),
  520. dev->bus->number, option);
  521. switch (option) {
  522. case EEH_RESET_FUNDAMENTAL:
  523. case EEH_RESET_HOT:
  524. /* Don't report linkDown event */
  525. if (aer) {
  526. eeh_ops->read_config(dn, aer + PCI_ERR_UNCOR_MASK,
  527. 4, &ctrl);
  528. ctrl |= PCI_ERR_UNC_SURPDN;
  529. eeh_ops->write_config(dn, aer + PCI_ERR_UNCOR_MASK,
  530. 4, ctrl);
  531. }
  532. eeh_ops->read_config(dn, PCI_BRIDGE_CONTROL, 2, &ctrl);
  533. ctrl |= PCI_BRIDGE_CTL_BUS_RESET;
  534. eeh_ops->write_config(dn, PCI_BRIDGE_CONTROL, 2, ctrl);
  535. msleep(EEH_PE_RST_HOLD_TIME);
  536. break;
  537. case EEH_RESET_DEACTIVATE:
  538. eeh_ops->read_config(dn, PCI_BRIDGE_CONTROL, 2, &ctrl);
  539. ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
  540. eeh_ops->write_config(dn, PCI_BRIDGE_CONTROL, 2, ctrl);
  541. msleep(EEH_PE_RST_SETTLE_TIME);
  542. /* Continue reporting linkDown event */
  543. if (aer) {
  544. eeh_ops->read_config(dn, aer + PCI_ERR_UNCOR_MASK,
  545. 4, &ctrl);
  546. ctrl &= ~PCI_ERR_UNC_SURPDN;
  547. eeh_ops->write_config(dn, aer + PCI_ERR_UNCOR_MASK,
  548. 4, ctrl);
  549. }
  550. break;
  551. }
  552. return 0;
  553. }
  554. void pnv_pci_reset_secondary_bus(struct pci_dev *dev)
  555. {
  556. struct pci_controller *hose;
  557. if (pci_is_root_bus(dev->bus)) {
  558. hose = pci_bus_to_host(dev->bus);
  559. ioda_eeh_root_reset(hose, EEH_RESET_HOT);
  560. ioda_eeh_root_reset(hose, EEH_RESET_DEACTIVATE);
  561. } else {
  562. ioda_eeh_bridge_reset(dev, EEH_RESET_HOT);
  563. ioda_eeh_bridge_reset(dev, EEH_RESET_DEACTIVATE);
  564. }
  565. }
  566. /**
  567. * ioda_eeh_reset - Reset the indicated PE
  568. * @pe: EEH PE
  569. * @option: reset option
  570. *
  571. * Do reset on the indicated PE. For PCI bus sensitive PE,
  572. * we need to reset the parent p2p bridge. The PHB has to
  573. * be reinitialized if the p2p bridge is root bridge. For
  574. * PCI device sensitive PE, we will try to reset the device
  575. * through FLR. For now, we don't have OPAL APIs to do HARD
  576. * reset yet, so all reset would be SOFT (HOT) reset.
  577. */
  578. static int ioda_eeh_reset(struct eeh_pe *pe, int option)
  579. {
  580. struct pci_controller *hose = pe->phb;
  581. struct pci_bus *bus;
  582. int ret;
  583. /*
  584. * For PHB reset, we always have complete reset. For those PEs whose
  585. * primary bus derived from root complex (root bus) or root port
  586. * (usually bus#1), we apply hot or fundamental reset on the root port.
  587. * For other PEs, we always have hot reset on the PE primary bus.
  588. *
  589. * Here, we have different design to pHyp, which always clear the
  590. * frozen state during PE reset. However, the good idea here from
  591. * benh is to keep frozen state before we get PE reset done completely
  592. * (until BAR restore). With the frozen state, HW drops illegal IO
  593. * or MMIO access, which can incur recrusive frozen PE during PE
  594. * reset. The side effect is that EEH core has to clear the frozen
  595. * state explicitly after BAR restore.
  596. */
  597. if (pe->type & EEH_PE_PHB) {
  598. ret = ioda_eeh_phb_reset(hose, option);
  599. } else {
  600. struct pnv_phb *phb;
  601. s64 rc;
  602. /*
  603. * The frozen PE might be caused by PAPR error injection
  604. * registers, which are expected to be cleared after hitting
  605. * frozen PE as stated in the hardware spec. Unfortunately,
  606. * that's not true on P7IOC. So we have to clear it manually
  607. * to avoid recursive EEH errors during recovery.
  608. */
  609. phb = hose->private_data;
  610. if (phb->model == PNV_PHB_MODEL_P7IOC &&
  611. (option == EEH_RESET_HOT ||
  612. option == EEH_RESET_FUNDAMENTAL)) {
  613. rc = opal_pci_reset(phb->opal_id,
  614. OPAL_RESET_PHB_ERROR,
  615. OPAL_ASSERT_RESET);
  616. if (rc != OPAL_SUCCESS) {
  617. pr_warn("%s: Failure %lld clearing "
  618. "error injection registers\n",
  619. __func__, rc);
  620. return -EIO;
  621. }
  622. }
  623. bus = eeh_pe_bus_get(pe);
  624. if (pci_is_root_bus(bus) ||
  625. pci_is_root_bus(bus->parent))
  626. ret = ioda_eeh_root_reset(hose, option);
  627. else
  628. ret = ioda_eeh_bridge_reset(bus->self, option);
  629. }
  630. return ret;
  631. }
  632. /**
  633. * ioda_eeh_get_log - Retrieve error log
  634. * @pe: frozen PE
  635. * @severity: permanent or temporary error
  636. * @drv_log: device driver log
  637. * @len: length of device driver log
  638. *
  639. * Retrieve error log, which contains log from device driver
  640. * and firmware.
  641. */
  642. static int ioda_eeh_get_log(struct eeh_pe *pe, int severity,
  643. char *drv_log, unsigned long len)
  644. {
  645. pnv_pci_dump_phb_diag_data(pe->phb, pe->data);
  646. return 0;
  647. }
  648. /**
  649. * ioda_eeh_configure_bridge - Configure the PCI bridges for the indicated PE
  650. * @pe: EEH PE
  651. *
  652. * For particular PE, it might have included PCI bridges. In order
  653. * to make the PE work properly, those PCI bridges should be configured
  654. * correctly. However, we need do nothing on P7IOC since the reset
  655. * function will do everything that should be covered by the function.
  656. */
  657. static int ioda_eeh_configure_bridge(struct eeh_pe *pe)
  658. {
  659. return 0;
  660. }
  661. static int ioda_eeh_err_inject(struct eeh_pe *pe, int type, int func,
  662. unsigned long addr, unsigned long mask)
  663. {
  664. struct pci_controller *hose = pe->phb;
  665. struct pnv_phb *phb = hose->private_data;
  666. s64 ret;
  667. /* Sanity check on error type */
  668. if (type != OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR &&
  669. type != OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR64) {
  670. pr_warn("%s: Invalid error type %d\n",
  671. __func__, type);
  672. return -ERANGE;
  673. }
  674. if (func < OPAL_ERR_INJECT_FUNC_IOA_LD_MEM_ADDR ||
  675. func > OPAL_ERR_INJECT_FUNC_IOA_DMA_WR_TARGET) {
  676. pr_warn("%s: Invalid error function %d\n",
  677. __func__, func);
  678. return -ERANGE;
  679. }
  680. /* Firmware supports error injection ? */
  681. if (!opal_check_token(OPAL_PCI_ERR_INJECT)) {
  682. pr_warn("%s: Firmware doesn't support error injection\n",
  683. __func__);
  684. return -ENXIO;
  685. }
  686. /* Do error injection */
  687. ret = opal_pci_err_inject(phb->opal_id, pe->addr,
  688. type, func, addr, mask);
  689. if (ret != OPAL_SUCCESS) {
  690. pr_warn("%s: Failure %lld injecting error "
  691. "%d-%d to PHB#%x-PE#%x\n",
  692. __func__, ret, type, func,
  693. hose->global_number, pe->addr);
  694. return -EIO;
  695. }
  696. return 0;
  697. }
  698. static void ioda_eeh_hub_diag_common(struct OpalIoP7IOCErrorData *data)
  699. {
  700. /* GEM */
  701. if (data->gemXfir || data->gemRfir ||
  702. data->gemRirqfir || data->gemMask || data->gemRwof)
  703. pr_info(" GEM: %016llx %016llx %016llx %016llx %016llx\n",
  704. be64_to_cpu(data->gemXfir),
  705. be64_to_cpu(data->gemRfir),
  706. be64_to_cpu(data->gemRirqfir),
  707. be64_to_cpu(data->gemMask),
  708. be64_to_cpu(data->gemRwof));
  709. /* LEM */
  710. if (data->lemFir || data->lemErrMask ||
  711. data->lemAction0 || data->lemAction1 || data->lemWof)
  712. pr_info(" LEM: %016llx %016llx %016llx %016llx %016llx\n",
  713. be64_to_cpu(data->lemFir),
  714. be64_to_cpu(data->lemErrMask),
  715. be64_to_cpu(data->lemAction0),
  716. be64_to_cpu(data->lemAction1),
  717. be64_to_cpu(data->lemWof));
  718. }
  719. static void ioda_eeh_hub_diag(struct pci_controller *hose)
  720. {
  721. struct pnv_phb *phb = hose->private_data;
  722. struct OpalIoP7IOCErrorData *data = &phb->diag.hub_diag;
  723. long rc;
  724. rc = opal_pci_get_hub_diag_data(phb->hub_id, data, sizeof(*data));
  725. if (rc != OPAL_SUCCESS) {
  726. pr_warn("%s: Failed to get HUB#%llx diag-data (%ld)\n",
  727. __func__, phb->hub_id, rc);
  728. return;
  729. }
  730. switch (data->type) {
  731. case OPAL_P7IOC_DIAG_TYPE_RGC:
  732. pr_info("P7IOC diag-data for RGC\n\n");
  733. ioda_eeh_hub_diag_common(data);
  734. if (data->rgc.rgcStatus || data->rgc.rgcLdcp)
  735. pr_info(" RGC: %016llx %016llx\n",
  736. be64_to_cpu(data->rgc.rgcStatus),
  737. be64_to_cpu(data->rgc.rgcLdcp));
  738. break;
  739. case OPAL_P7IOC_DIAG_TYPE_BI:
  740. pr_info("P7IOC diag-data for BI %s\n\n",
  741. data->bi.biDownbound ? "Downbound" : "Upbound");
  742. ioda_eeh_hub_diag_common(data);
  743. if (data->bi.biLdcp0 || data->bi.biLdcp1 ||
  744. data->bi.biLdcp2 || data->bi.biFenceStatus)
  745. pr_info(" BI: %016llx %016llx %016llx %016llx\n",
  746. be64_to_cpu(data->bi.biLdcp0),
  747. be64_to_cpu(data->bi.biLdcp1),
  748. be64_to_cpu(data->bi.biLdcp2),
  749. be64_to_cpu(data->bi.biFenceStatus));
  750. break;
  751. case OPAL_P7IOC_DIAG_TYPE_CI:
  752. pr_info("P7IOC diag-data for CI Port %d\n\n",
  753. data->ci.ciPort);
  754. ioda_eeh_hub_diag_common(data);
  755. if (data->ci.ciPortStatus || data->ci.ciPortLdcp)
  756. pr_info(" CI: %016llx %016llx\n",
  757. be64_to_cpu(data->ci.ciPortStatus),
  758. be64_to_cpu(data->ci.ciPortLdcp));
  759. break;
  760. case OPAL_P7IOC_DIAG_TYPE_MISC:
  761. pr_info("P7IOC diag-data for MISC\n\n");
  762. ioda_eeh_hub_diag_common(data);
  763. break;
  764. case OPAL_P7IOC_DIAG_TYPE_I2C:
  765. pr_info("P7IOC diag-data for I2C\n\n");
  766. ioda_eeh_hub_diag_common(data);
  767. break;
  768. default:
  769. pr_warn("%s: Invalid type of HUB#%llx diag-data (%d)\n",
  770. __func__, phb->hub_id, data->type);
  771. }
  772. }
  773. static int ioda_eeh_get_pe(struct pci_controller *hose,
  774. u16 pe_no, struct eeh_pe **pe)
  775. {
  776. struct pnv_phb *phb = hose->private_data;
  777. struct pnv_ioda_pe *pnv_pe;
  778. struct eeh_pe *dev_pe;
  779. struct eeh_dev edev;
  780. /*
  781. * If PHB supports compound PE, to fetch
  782. * the master PE because slave PE is invisible
  783. * to EEH core.
  784. */
  785. pnv_pe = &phb->ioda.pe_array[pe_no];
  786. if (pnv_pe->flags & PNV_IODA_PE_SLAVE) {
  787. pnv_pe = pnv_pe->master;
  788. WARN_ON(!pnv_pe ||
  789. !(pnv_pe->flags & PNV_IODA_PE_MASTER));
  790. pe_no = pnv_pe->pe_number;
  791. }
  792. /* Find the PE according to PE# */
  793. memset(&edev, 0, sizeof(struct eeh_dev));
  794. edev.phb = hose;
  795. edev.pe_config_addr = pe_no;
  796. dev_pe = eeh_pe_get(&edev);
  797. if (!dev_pe)
  798. return -EEXIST;
  799. /* Freeze the (compound) PE */
  800. *pe = dev_pe;
  801. if (!(dev_pe->state & EEH_PE_ISOLATED))
  802. phb->freeze_pe(phb, pe_no);
  803. /*
  804. * At this point, we're sure the (compound) PE should
  805. * have been frozen. However, we still need poke until
  806. * hitting the frozen PE on top level.
  807. */
  808. dev_pe = dev_pe->parent;
  809. while (dev_pe && !(dev_pe->type & EEH_PE_PHB)) {
  810. int ret;
  811. int active_flags = (EEH_STATE_MMIO_ACTIVE |
  812. EEH_STATE_DMA_ACTIVE);
  813. ret = eeh_ops->get_state(dev_pe, NULL);
  814. if (ret <= 0 || (ret & active_flags) == active_flags) {
  815. dev_pe = dev_pe->parent;
  816. continue;
  817. }
  818. /* Frozen parent PE */
  819. *pe = dev_pe;
  820. if (!(dev_pe->state & EEH_PE_ISOLATED))
  821. phb->freeze_pe(phb, dev_pe->addr);
  822. /* Next one */
  823. dev_pe = dev_pe->parent;
  824. }
  825. return 0;
  826. }
  827. /**
  828. * ioda_eeh_next_error - Retrieve next error for EEH core to handle
  829. * @pe: The affected PE
  830. *
  831. * The function is expected to be called by EEH core while it gets
  832. * special EEH event (without binding PE). The function calls to
  833. * OPAL APIs for next error to handle. The informational error is
  834. * handled internally by platform. However, the dead IOC, dead PHB,
  835. * fenced PHB and frozen PE should be handled by EEH core eventually.
  836. */
  837. static int ioda_eeh_next_error(struct eeh_pe **pe)
  838. {
  839. struct pci_controller *hose;
  840. struct pnv_phb *phb;
  841. struct eeh_pe *phb_pe, *parent_pe;
  842. __be64 frozen_pe_no;
  843. __be16 err_type, severity;
  844. int active_flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
  845. long rc;
  846. int state, ret = EEH_NEXT_ERR_NONE;
  847. /*
  848. * While running here, it's safe to purge the event queue.
  849. * And we should keep the cached OPAL notifier event sychronized
  850. * between the kernel and firmware.
  851. */
  852. eeh_remove_event(NULL, false);
  853. opal_notifier_update_evt(OPAL_EVENT_PCI_ERROR, 0x0ul);
  854. list_for_each_entry(hose, &hose_list, list_node) {
  855. /*
  856. * If the subordinate PCI buses of the PHB has been
  857. * removed or is exactly under error recovery, we
  858. * needn't take care of it any more.
  859. */
  860. phb = hose->private_data;
  861. phb_pe = eeh_phb_pe_get(hose);
  862. if (!phb_pe || (phb_pe->state & EEH_PE_ISOLATED))
  863. continue;
  864. rc = opal_pci_next_error(phb->opal_id,
  865. &frozen_pe_no, &err_type, &severity);
  866. /* If OPAL API returns error, we needn't proceed */
  867. if (rc != OPAL_SUCCESS) {
  868. pr_devel("%s: Invalid return value on "
  869. "PHB#%x (0x%lx) from opal_pci_next_error",
  870. __func__, hose->global_number, rc);
  871. continue;
  872. }
  873. /* If the PHB doesn't have error, stop processing */
  874. if (be16_to_cpu(err_type) == OPAL_EEH_NO_ERROR ||
  875. be16_to_cpu(severity) == OPAL_EEH_SEV_NO_ERROR) {
  876. pr_devel("%s: No error found on PHB#%x\n",
  877. __func__, hose->global_number);
  878. continue;
  879. }
  880. /*
  881. * Processing the error. We're expecting the error with
  882. * highest priority reported upon multiple errors on the
  883. * specific PHB.
  884. */
  885. pr_devel("%s: Error (%d, %d, %llu) on PHB#%x\n",
  886. __func__, be16_to_cpu(err_type), be16_to_cpu(severity),
  887. be64_to_cpu(frozen_pe_no), hose->global_number);
  888. switch (be16_to_cpu(err_type)) {
  889. case OPAL_EEH_IOC_ERROR:
  890. if (be16_to_cpu(severity) == OPAL_EEH_SEV_IOC_DEAD) {
  891. pr_err("EEH: dead IOC detected\n");
  892. ret = EEH_NEXT_ERR_DEAD_IOC;
  893. } else if (be16_to_cpu(severity) == OPAL_EEH_SEV_INF) {
  894. pr_info("EEH: IOC informative error "
  895. "detected\n");
  896. ioda_eeh_hub_diag(hose);
  897. ret = EEH_NEXT_ERR_NONE;
  898. }
  899. break;
  900. case OPAL_EEH_PHB_ERROR:
  901. if (be16_to_cpu(severity) == OPAL_EEH_SEV_PHB_DEAD) {
  902. *pe = phb_pe;
  903. pr_err("EEH: dead PHB#%x detected, "
  904. "location: %s\n",
  905. hose->global_number,
  906. eeh_pe_loc_get(phb_pe));
  907. ret = EEH_NEXT_ERR_DEAD_PHB;
  908. } else if (be16_to_cpu(severity) ==
  909. OPAL_EEH_SEV_PHB_FENCED) {
  910. *pe = phb_pe;
  911. pr_err("EEH: Fenced PHB#%x detected, "
  912. "location: %s\n",
  913. hose->global_number,
  914. eeh_pe_loc_get(phb_pe));
  915. ret = EEH_NEXT_ERR_FENCED_PHB;
  916. } else if (be16_to_cpu(severity) == OPAL_EEH_SEV_INF) {
  917. pr_info("EEH: PHB#%x informative error "
  918. "detected, location: %s\n",
  919. hose->global_number,
  920. eeh_pe_loc_get(phb_pe));
  921. ioda_eeh_phb_diag(phb_pe);
  922. pnv_pci_dump_phb_diag_data(hose, phb_pe->data);
  923. ret = EEH_NEXT_ERR_NONE;
  924. }
  925. break;
  926. case OPAL_EEH_PE_ERROR:
  927. /*
  928. * If we can't find the corresponding PE, we
  929. * just try to unfreeze.
  930. */
  931. if (ioda_eeh_get_pe(hose,
  932. be64_to_cpu(frozen_pe_no), pe)) {
  933. /* Try best to clear it */
  934. pr_info("EEH: Clear non-existing PHB#%x-PE#%llx\n",
  935. hose->global_number, frozen_pe_no);
  936. pr_info("EEH: PHB location: %s\n",
  937. eeh_pe_loc_get(phb_pe));
  938. opal_pci_eeh_freeze_clear(phb->opal_id, frozen_pe_no,
  939. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  940. ret = EEH_NEXT_ERR_NONE;
  941. } else if ((*pe)->state & EEH_PE_ISOLATED ||
  942. eeh_pe_passed(*pe)) {
  943. ret = EEH_NEXT_ERR_NONE;
  944. } else {
  945. pr_err("EEH: Frozen PE#%x on PHB#%x detected\n",
  946. (*pe)->addr, (*pe)->phb->global_number);
  947. pr_err("EEH: PE location: %s, PHB location: %s\n",
  948. eeh_pe_loc_get(*pe), eeh_pe_loc_get(phb_pe));
  949. ret = EEH_NEXT_ERR_FROZEN_PE;
  950. }
  951. break;
  952. default:
  953. pr_warn("%s: Unexpected error type %d\n",
  954. __func__, be16_to_cpu(err_type));
  955. }
  956. /*
  957. * EEH core will try recover from fenced PHB or
  958. * frozen PE. In the time for frozen PE, EEH core
  959. * enable IO path for that before collecting logs,
  960. * but it ruins the site. So we have to dump the
  961. * log in advance here.
  962. */
  963. if ((ret == EEH_NEXT_ERR_FROZEN_PE ||
  964. ret == EEH_NEXT_ERR_FENCED_PHB) &&
  965. !((*pe)->state & EEH_PE_ISOLATED)) {
  966. eeh_pe_state_mark(*pe, EEH_PE_ISOLATED);
  967. ioda_eeh_phb_diag(*pe);
  968. }
  969. /*
  970. * We probably have the frozen parent PE out there and
  971. * we need have to handle frozen parent PE firstly.
  972. */
  973. if (ret == EEH_NEXT_ERR_FROZEN_PE) {
  974. parent_pe = (*pe)->parent;
  975. while (parent_pe) {
  976. /* Hit the ceiling ? */
  977. if (parent_pe->type & EEH_PE_PHB)
  978. break;
  979. /* Frozen parent PE ? */
  980. state = ioda_eeh_get_state(parent_pe);
  981. if (state > 0 &&
  982. (state & active_flags) != active_flags)
  983. *pe = parent_pe;
  984. /* Next parent level */
  985. parent_pe = parent_pe->parent;
  986. }
  987. /* We possibly migrate to another PE */
  988. eeh_pe_state_mark(*pe, EEH_PE_ISOLATED);
  989. }
  990. /*
  991. * If we have no errors on the specific PHB or only
  992. * informative error there, we continue poking it.
  993. * Otherwise, we need actions to be taken by upper
  994. * layer.
  995. */
  996. if (ret > EEH_NEXT_ERR_INF)
  997. break;
  998. }
  999. return ret;
  1000. }
  1001. struct pnv_eeh_ops ioda_eeh_ops = {
  1002. .post_init = ioda_eeh_post_init,
  1003. .set_option = ioda_eeh_set_option,
  1004. .get_state = ioda_eeh_get_state,
  1005. .reset = ioda_eeh_reset,
  1006. .get_log = ioda_eeh_get_log,
  1007. .configure_bridge = ioda_eeh_configure_bridge,
  1008. .err_inject = ioda_eeh_err_inject,
  1009. .next_error = ioda_eeh_next_error
  1010. };