eeh.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. /*
  2. * Copyright IBM Corporation 2001, 2005, 2006
  3. * Copyright Dave Engebretsen & Todd Inglett 2001
  4. * Copyright Linas Vepstas 2005, 2006
  5. * Copyright 2001-2012 IBM Corporation.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * Please address comments and feedback to Linas Vepstas <linas@austin.ibm.com>
  22. */
  23. #include <linux/delay.h>
  24. #include <linux/sched.h>
  25. #include <linux/init.h>
  26. #include <linux/list.h>
  27. #include <linux/pci.h>
  28. #include <linux/proc_fs.h>
  29. #include <linux/rbtree.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/export.h>
  33. #include <linux/of.h>
  34. #include <linux/atomic.h>
  35. #include <asm/eeh.h>
  36. #include <asm/eeh_event.h>
  37. #include <asm/io.h>
  38. #include <asm/machdep.h>
  39. #include <asm/ppc-pci.h>
  40. #include <asm/rtas.h>
  41. /** Overview:
  42. * EEH, or "Extended Error Handling" is a PCI bridge technology for
  43. * dealing with PCI bus errors that can't be dealt with within the
  44. * usual PCI framework, except by check-stopping the CPU. Systems
  45. * that are designed for high-availability/reliability cannot afford
  46. * to crash due to a "mere" PCI error, thus the need for EEH.
  47. * An EEH-capable bridge operates by converting a detected error
  48. * into a "slot freeze", taking the PCI adapter off-line, making
  49. * the slot behave, from the OS'es point of view, as if the slot
  50. * were "empty": all reads return 0xff's and all writes are silently
  51. * ignored. EEH slot isolation events can be triggered by parity
  52. * errors on the address or data busses (e.g. during posted writes),
  53. * which in turn might be caused by low voltage on the bus, dust,
  54. * vibration, humidity, radioactivity or plain-old failed hardware.
  55. *
  56. * Note, however, that one of the leading causes of EEH slot
  57. * freeze events are buggy device drivers, buggy device microcode,
  58. * or buggy device hardware. This is because any attempt by the
  59. * device to bus-master data to a memory address that is not
  60. * assigned to the device will trigger a slot freeze. (The idea
  61. * is to prevent devices-gone-wild from corrupting system memory).
  62. * Buggy hardware/drivers will have a miserable time co-existing
  63. * with EEH.
  64. *
  65. * Ideally, a PCI device driver, when suspecting that an isolation
  66. * event has occurred (e.g. by reading 0xff's), will then ask EEH
  67. * whether this is the case, and then take appropriate steps to
  68. * reset the PCI slot, the PCI device, and then resume operations.
  69. * However, until that day, the checking is done here, with the
  70. * eeh_check_failure() routine embedded in the MMIO macros. If
  71. * the slot is found to be isolated, an "EEH Event" is synthesized
  72. * and sent out for processing.
  73. */
  74. /* If a device driver keeps reading an MMIO register in an interrupt
  75. * handler after a slot isolation event, it might be broken.
  76. * This sets the threshold for how many read attempts we allow
  77. * before printing an error message.
  78. */
  79. #define EEH_MAX_FAILS 2100000
  80. /* Time to wait for a PCI slot to report status, in milliseconds */
  81. #define PCI_BUS_RESET_WAIT_MSEC (60*1000)
  82. /* Platform dependent EEH operations */
  83. struct eeh_ops *eeh_ops = NULL;
  84. int eeh_subsystem_enabled;
  85. EXPORT_SYMBOL(eeh_subsystem_enabled);
  86. /*
  87. * EEH probe mode support. The intention is to support multiple
  88. * platforms for EEH. Some platforms like pSeries do PCI emunation
  89. * based on device tree. However, other platforms like powernv probe
  90. * PCI devices from hardware. The flag is used to distinguish that.
  91. * In addition, struct eeh_ops::probe would be invoked for particular
  92. * OF node or PCI device so that the corresponding PE would be created
  93. * there.
  94. */
  95. int eeh_probe_mode;
  96. /* Global EEH mutex */
  97. DEFINE_MUTEX(eeh_mutex);
  98. /* Lock to avoid races due to multiple reports of an error */
  99. static DEFINE_RAW_SPINLOCK(confirm_error_lock);
  100. /* Buffer for reporting pci register dumps. Its here in BSS, and
  101. * not dynamically alloced, so that it ends up in RMO where RTAS
  102. * can access it.
  103. */
  104. #define EEH_PCI_REGS_LOG_LEN 4096
  105. static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];
  106. /*
  107. * The struct is used to maintain the EEH global statistic
  108. * information. Besides, the EEH global statistics will be
  109. * exported to user space through procfs
  110. */
  111. struct eeh_stats {
  112. u64 no_device; /* PCI device not found */
  113. u64 no_dn; /* OF node not found */
  114. u64 no_cfg_addr; /* Config address not found */
  115. u64 ignored_check; /* EEH check skipped */
  116. u64 total_mmio_ffs; /* Total EEH checks */
  117. u64 false_positives; /* Unnecessary EEH checks */
  118. u64 slot_resets; /* PE reset */
  119. };
  120. static struct eeh_stats eeh_stats;
  121. #define IS_BRIDGE(class_code) (((class_code)<<16) == PCI_BASE_CLASS_BRIDGE)
  122. /**
  123. * eeh_gather_pci_data - Copy assorted PCI config space registers to buff
  124. * @edev: device to report data for
  125. * @buf: point to buffer in which to log
  126. * @len: amount of room in buffer
  127. *
  128. * This routine captures assorted PCI configuration space data,
  129. * and puts them into a buffer for RTAS error logging.
  130. */
  131. static size_t eeh_gather_pci_data(struct eeh_dev *edev, char * buf, size_t len)
  132. {
  133. struct device_node *dn = eeh_dev_to_of_node(edev);
  134. struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
  135. u32 cfg;
  136. int cap, i;
  137. int n = 0;
  138. n += scnprintf(buf+n, len-n, "%s\n", dn->full_name);
  139. printk(KERN_WARNING "EEH: of node=%s\n", dn->full_name);
  140. eeh_ops->read_config(dn, PCI_VENDOR_ID, 4, &cfg);
  141. n += scnprintf(buf+n, len-n, "dev/vend:%08x\n", cfg);
  142. printk(KERN_WARNING "EEH: PCI device/vendor: %08x\n", cfg);
  143. eeh_ops->read_config(dn, PCI_COMMAND, 4, &cfg);
  144. n += scnprintf(buf+n, len-n, "cmd/stat:%x\n", cfg);
  145. printk(KERN_WARNING "EEH: PCI cmd/status register: %08x\n", cfg);
  146. if (!dev) {
  147. printk(KERN_WARNING "EEH: no PCI device for this of node\n");
  148. return n;
  149. }
  150. /* Gather bridge-specific registers */
  151. if (dev->class >> 16 == PCI_BASE_CLASS_BRIDGE) {
  152. eeh_ops->read_config(dn, PCI_SEC_STATUS, 2, &cfg);
  153. n += scnprintf(buf+n, len-n, "sec stat:%x\n", cfg);
  154. printk(KERN_WARNING "EEH: Bridge secondary status: %04x\n", cfg);
  155. eeh_ops->read_config(dn, PCI_BRIDGE_CONTROL, 2, &cfg);
  156. n += scnprintf(buf+n, len-n, "brdg ctl:%x\n", cfg);
  157. printk(KERN_WARNING "EEH: Bridge control: %04x\n", cfg);
  158. }
  159. /* Dump out the PCI-X command and status regs */
  160. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  161. if (cap) {
  162. eeh_ops->read_config(dn, cap, 4, &cfg);
  163. n += scnprintf(buf+n, len-n, "pcix-cmd:%x\n", cfg);
  164. printk(KERN_WARNING "EEH: PCI-X cmd: %08x\n", cfg);
  165. eeh_ops->read_config(dn, cap+4, 4, &cfg);
  166. n += scnprintf(buf+n, len-n, "pcix-stat:%x\n", cfg);
  167. printk(KERN_WARNING "EEH: PCI-X status: %08x\n", cfg);
  168. }
  169. /* If PCI-E capable, dump PCI-E cap 10, and the AER */
  170. cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
  171. if (cap) {
  172. n += scnprintf(buf+n, len-n, "pci-e cap10:\n");
  173. printk(KERN_WARNING
  174. "EEH: PCI-E capabilities and status follow:\n");
  175. for (i=0; i<=8; i++) {
  176. eeh_ops->read_config(dn, cap+4*i, 4, &cfg);
  177. n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
  178. printk(KERN_WARNING "EEH: PCI-E %02x: %08x\n", i, cfg);
  179. }
  180. cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  181. if (cap) {
  182. n += scnprintf(buf+n, len-n, "pci-e AER:\n");
  183. printk(KERN_WARNING
  184. "EEH: PCI-E AER capability register set follows:\n");
  185. for (i=0; i<14; i++) {
  186. eeh_ops->read_config(dn, cap+4*i, 4, &cfg);
  187. n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
  188. printk(KERN_WARNING "EEH: PCI-E AER %02x: %08x\n", i, cfg);
  189. }
  190. }
  191. }
  192. return n;
  193. }
  194. /**
  195. * eeh_slot_error_detail - Generate combined log including driver log and error log
  196. * @pe: EEH PE
  197. * @severity: temporary or permanent error log
  198. *
  199. * This routine should be called to generate the combined log, which
  200. * is comprised of driver log and error log. The driver log is figured
  201. * out from the config space of the corresponding PCI device, while
  202. * the error log is fetched through platform dependent function call.
  203. */
  204. void eeh_slot_error_detail(struct eeh_pe *pe, int severity)
  205. {
  206. size_t loglen = 0;
  207. struct eeh_dev *edev;
  208. eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
  209. eeh_ops->configure_bridge(pe);
  210. eeh_pe_restore_bars(pe);
  211. pci_regs_buf[0] = 0;
  212. eeh_pe_for_each_dev(pe, edev) {
  213. loglen += eeh_gather_pci_data(edev, pci_regs_buf,
  214. EEH_PCI_REGS_LOG_LEN);
  215. }
  216. eeh_ops->get_log(pe, severity, pci_regs_buf, loglen);
  217. }
  218. /**
  219. * eeh_token_to_phys - Convert EEH address token to phys address
  220. * @token: I/O token, should be address in the form 0xA....
  221. *
  222. * This routine should be called to convert virtual I/O address
  223. * to physical one.
  224. */
  225. static inline unsigned long eeh_token_to_phys(unsigned long token)
  226. {
  227. pte_t *ptep;
  228. unsigned long pa;
  229. ptep = find_linux_pte(init_mm.pgd, token);
  230. if (!ptep)
  231. return token;
  232. pa = pte_pfn(*ptep) << PAGE_SHIFT;
  233. return pa | (token & (PAGE_SIZE-1));
  234. }
  235. /**
  236. * eeh_dev_check_failure - Check if all 1's data is due to EEH slot freeze
  237. * @edev: eeh device
  238. *
  239. * Check for an EEH failure for the given device node. Call this
  240. * routine if the result of a read was all 0xff's and you want to
  241. * find out if this is due to an EEH slot freeze. This routine
  242. * will query firmware for the EEH status.
  243. *
  244. * Returns 0 if there has not been an EEH error; otherwise returns
  245. * a non-zero value and queues up a slot isolation event notification.
  246. *
  247. * It is safe to call this routine in an interrupt context.
  248. */
  249. int eeh_dev_check_failure(struct eeh_dev *edev)
  250. {
  251. int ret;
  252. unsigned long flags;
  253. struct device_node *dn;
  254. struct pci_dev *dev;
  255. struct eeh_pe *pe;
  256. int rc = 0;
  257. const char *location;
  258. eeh_stats.total_mmio_ffs++;
  259. if (!eeh_subsystem_enabled)
  260. return 0;
  261. if (!edev) {
  262. eeh_stats.no_dn++;
  263. return 0;
  264. }
  265. dn = eeh_dev_to_of_node(edev);
  266. dev = eeh_dev_to_pci_dev(edev);
  267. pe = edev->pe;
  268. /* Access to IO BARs might get this far and still not want checking. */
  269. if (!pe) {
  270. eeh_stats.ignored_check++;
  271. pr_debug("EEH: Ignored check for %s %s\n",
  272. eeh_pci_name(dev), dn->full_name);
  273. return 0;
  274. }
  275. if (!pe->addr && !pe->config_addr) {
  276. eeh_stats.no_cfg_addr++;
  277. return 0;
  278. }
  279. /* If we already have a pending isolation event for this
  280. * slot, we know it's bad already, we don't need to check.
  281. * Do this checking under a lock; as multiple PCI devices
  282. * in one slot might report errors simultaneously, and we
  283. * only want one error recovery routine running.
  284. */
  285. raw_spin_lock_irqsave(&confirm_error_lock, flags);
  286. rc = 1;
  287. if (pe->state & EEH_PE_ISOLATED) {
  288. pe->check_count++;
  289. if (pe->check_count % EEH_MAX_FAILS == 0) {
  290. location = of_get_property(dn, "ibm,loc-code", NULL);
  291. printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
  292. "location=%s driver=%s pci addr=%s\n",
  293. pe->check_count, location,
  294. eeh_driver_name(dev), eeh_pci_name(dev));
  295. printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
  296. eeh_driver_name(dev));
  297. dump_stack();
  298. }
  299. goto dn_unlock;
  300. }
  301. /*
  302. * Now test for an EEH failure. This is VERY expensive.
  303. * Note that the eeh_config_addr may be a parent device
  304. * in the case of a device behind a bridge, or it may be
  305. * function zero of a multi-function device.
  306. * In any case they must share a common PHB.
  307. */
  308. ret = eeh_ops->get_state(pe, NULL);
  309. /* Note that config-io to empty slots may fail;
  310. * they are empty when they don't have children.
  311. * We will punt with the following conditions: Failure to get
  312. * PE's state, EEH not support and Permanently unavailable
  313. * state, PE is in good state.
  314. */
  315. if ((ret < 0) ||
  316. (ret == EEH_STATE_NOT_SUPPORT) ||
  317. (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
  318. (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
  319. eeh_stats.false_positives++;
  320. pe->false_positives++;
  321. rc = 0;
  322. goto dn_unlock;
  323. }
  324. eeh_stats.slot_resets++;
  325. /* Avoid repeated reports of this failure, including problems
  326. * with other functions on this device, and functions under
  327. * bridges.
  328. */
  329. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  330. raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
  331. eeh_send_failure_event(pe);
  332. /* Most EEH events are due to device driver bugs. Having
  333. * a stack trace will help the device-driver authors figure
  334. * out what happened. So print that out.
  335. */
  336. WARN(1, "EEH: failure detected\n");
  337. return 1;
  338. dn_unlock:
  339. raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
  340. return rc;
  341. }
  342. EXPORT_SYMBOL_GPL(eeh_dev_check_failure);
  343. /**
  344. * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
  345. * @token: I/O token, should be address in the form 0xA....
  346. * @val: value, should be all 1's (XXX why do we need this arg??)
  347. *
  348. * Check for an EEH failure at the given token address. Call this
  349. * routine if the result of a read was all 0xff's and you want to
  350. * find out if this is due to an EEH slot freeze event. This routine
  351. * will query firmware for the EEH status.
  352. *
  353. * Note this routine is safe to call in an interrupt context.
  354. */
  355. unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
  356. {
  357. unsigned long addr;
  358. struct eeh_dev *edev;
  359. /* Finding the phys addr + pci device; this is pretty quick. */
  360. addr = eeh_token_to_phys((unsigned long __force) token);
  361. edev = eeh_addr_cache_get_dev(addr);
  362. if (!edev) {
  363. eeh_stats.no_device++;
  364. return val;
  365. }
  366. eeh_dev_check_failure(edev);
  367. pci_dev_put(eeh_dev_to_pci_dev(edev));
  368. return val;
  369. }
  370. EXPORT_SYMBOL(eeh_check_failure);
  371. /**
  372. * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
  373. * @pe: EEH PE
  374. *
  375. * This routine should be called to reenable frozen MMIO or DMA
  376. * so that it would work correctly again. It's useful while doing
  377. * recovery or log collection on the indicated device.
  378. */
  379. int eeh_pci_enable(struct eeh_pe *pe, int function)
  380. {
  381. int rc;
  382. rc = eeh_ops->set_option(pe, function);
  383. if (rc)
  384. pr_warning("%s: Unexpected state change %d on PHB#%d-PE#%x, err=%d\n",
  385. __func__, function, pe->phb->global_number, pe->addr, rc);
  386. rc = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
  387. if (rc > 0 && (rc & EEH_STATE_MMIO_ENABLED) &&
  388. (function == EEH_OPT_THAW_MMIO))
  389. return 0;
  390. return rc;
  391. }
  392. /**
  393. * pcibios_set_pcie_slot_reset - Set PCI-E reset state
  394. * @dev: pci device struct
  395. * @state: reset state to enter
  396. *
  397. * Return value:
  398. * 0 if success
  399. */
  400. int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  401. {
  402. struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
  403. struct eeh_pe *pe = edev->pe;
  404. if (!pe) {
  405. pr_err("%s: No PE found on PCI device %s\n",
  406. __func__, pci_name(dev));
  407. return -EINVAL;
  408. }
  409. switch (state) {
  410. case pcie_deassert_reset:
  411. eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
  412. break;
  413. case pcie_hot_reset:
  414. eeh_ops->reset(pe, EEH_RESET_HOT);
  415. break;
  416. case pcie_warm_reset:
  417. eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
  418. break;
  419. default:
  420. return -EINVAL;
  421. };
  422. return 0;
  423. }
  424. /**
  425. * eeh_set_pe_freset - Check the required reset for the indicated device
  426. * @data: EEH device
  427. * @flag: return value
  428. *
  429. * Each device might have its preferred reset type: fundamental or
  430. * hot reset. The routine is used to collected the information for
  431. * the indicated device and its children so that the bunch of the
  432. * devices could be reset properly.
  433. */
  434. static void *eeh_set_dev_freset(void *data, void *flag)
  435. {
  436. struct pci_dev *dev;
  437. unsigned int *freset = (unsigned int *)flag;
  438. struct eeh_dev *edev = (struct eeh_dev *)data;
  439. dev = eeh_dev_to_pci_dev(edev);
  440. if (dev)
  441. *freset |= dev->needs_freset;
  442. return NULL;
  443. }
  444. /**
  445. * eeh_reset_pe_once - Assert the pci #RST line for 1/4 second
  446. * @pe: EEH PE
  447. *
  448. * Assert the PCI #RST line for 1/4 second.
  449. */
  450. static void eeh_reset_pe_once(struct eeh_pe *pe)
  451. {
  452. unsigned int freset = 0;
  453. /* Determine type of EEH reset required for
  454. * Partitionable Endpoint, a hot-reset (1)
  455. * or a fundamental reset (3).
  456. * A fundamental reset required by any device under
  457. * Partitionable Endpoint trumps hot-reset.
  458. */
  459. eeh_pe_dev_traverse(pe, eeh_set_dev_freset, &freset);
  460. if (freset)
  461. eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
  462. else
  463. eeh_ops->reset(pe, EEH_RESET_HOT);
  464. /* The PCI bus requires that the reset be held high for at least
  465. * a 100 milliseconds. We wait a bit longer 'just in case'.
  466. */
  467. #define PCI_BUS_RST_HOLD_TIME_MSEC 250
  468. msleep(PCI_BUS_RST_HOLD_TIME_MSEC);
  469. /* We might get hit with another EEH freeze as soon as the
  470. * pci slot reset line is dropped. Make sure we don't miss
  471. * these, and clear the flag now.
  472. */
  473. eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
  474. eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
  475. /* After a PCI slot has been reset, the PCI Express spec requires
  476. * a 1.5 second idle time for the bus to stabilize, before starting
  477. * up traffic.
  478. */
  479. #define PCI_BUS_SETTLE_TIME_MSEC 1800
  480. msleep(PCI_BUS_SETTLE_TIME_MSEC);
  481. }
  482. /**
  483. * eeh_reset_pe - Reset the indicated PE
  484. * @pe: EEH PE
  485. *
  486. * This routine should be called to reset indicated device, including
  487. * PE. A PE might include multiple PCI devices and sometimes PCI bridges
  488. * might be involved as well.
  489. */
  490. int eeh_reset_pe(struct eeh_pe *pe)
  491. {
  492. int i, rc;
  493. /* Take three shots at resetting the bus */
  494. for (i=0; i<3; i++) {
  495. eeh_reset_pe_once(pe);
  496. rc = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
  497. if (rc == (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE))
  498. return 0;
  499. if (rc < 0) {
  500. pr_err("%s: Unrecoverable slot failure on PHB#%d-PE#%x",
  501. __func__, pe->phb->global_number, pe->addr);
  502. return -1;
  503. }
  504. pr_err("EEH: bus reset %d failed on PHB#%d-PE#%x, rc=%d\n",
  505. i+1, pe->phb->global_number, pe->addr, rc);
  506. }
  507. return -1;
  508. }
  509. /**
  510. * eeh_save_bars - Save device bars
  511. * @edev: PCI device associated EEH device
  512. *
  513. * Save the values of the device bars. Unlike the restore
  514. * routine, this routine is *not* recursive. This is because
  515. * PCI devices are added individually; but, for the restore,
  516. * an entire slot is reset at a time.
  517. */
  518. void eeh_save_bars(struct eeh_dev *edev)
  519. {
  520. int i;
  521. struct device_node *dn;
  522. if (!edev)
  523. return;
  524. dn = eeh_dev_to_of_node(edev);
  525. for (i = 0; i < 16; i++)
  526. eeh_ops->read_config(dn, i * 4, 4, &edev->config_space[i]);
  527. }
  528. /**
  529. * eeh_ops_register - Register platform dependent EEH operations
  530. * @ops: platform dependent EEH operations
  531. *
  532. * Register the platform dependent EEH operation callback
  533. * functions. The platform should call this function before
  534. * any other EEH operations.
  535. */
  536. int __init eeh_ops_register(struct eeh_ops *ops)
  537. {
  538. if (!ops->name) {
  539. pr_warning("%s: Invalid EEH ops name for %p\n",
  540. __func__, ops);
  541. return -EINVAL;
  542. }
  543. if (eeh_ops && eeh_ops != ops) {
  544. pr_warning("%s: EEH ops of platform %s already existing (%s)\n",
  545. __func__, eeh_ops->name, ops->name);
  546. return -EEXIST;
  547. }
  548. eeh_ops = ops;
  549. return 0;
  550. }
  551. /**
  552. * eeh_ops_unregister - Unreigster platform dependent EEH operations
  553. * @name: name of EEH platform operations
  554. *
  555. * Unregister the platform dependent EEH operation callback
  556. * functions.
  557. */
  558. int __exit eeh_ops_unregister(const char *name)
  559. {
  560. if (!name || !strlen(name)) {
  561. pr_warning("%s: Invalid EEH ops name\n",
  562. __func__);
  563. return -EINVAL;
  564. }
  565. if (eeh_ops && !strcmp(eeh_ops->name, name)) {
  566. eeh_ops = NULL;
  567. return 0;
  568. }
  569. return -EEXIST;
  570. }
  571. /**
  572. * eeh_init - EEH initialization
  573. *
  574. * Initialize EEH by trying to enable it for all of the adapters in the system.
  575. * As a side effect we can determine here if eeh is supported at all.
  576. * Note that we leave EEH on so failed config cycles won't cause a machine
  577. * check. If a user turns off EEH for a particular adapter they are really
  578. * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
  579. * grant access to a slot if EEH isn't enabled, and so we always enable
  580. * EEH for all slots/all devices.
  581. *
  582. * The eeh-force-off option disables EEH checking globally, for all slots.
  583. * Even if force-off is set, the EEH hardware is still enabled, so that
  584. * newer systems can boot.
  585. */
  586. static int __init eeh_init(void)
  587. {
  588. struct pci_controller *hose, *tmp;
  589. struct device_node *phb;
  590. int ret;
  591. /* call platform initialization function */
  592. if (!eeh_ops) {
  593. pr_warning("%s: Platform EEH operation not found\n",
  594. __func__);
  595. return -EEXIST;
  596. } else if ((ret = eeh_ops->init())) {
  597. pr_warning("%s: Failed to call platform init function (%d)\n",
  598. __func__, ret);
  599. return ret;
  600. }
  601. raw_spin_lock_init(&confirm_error_lock);
  602. /* Enable EEH for all adapters */
  603. if (eeh_probe_mode_devtree()) {
  604. list_for_each_entry_safe(hose, tmp,
  605. &hose_list, list_node) {
  606. phb = hose->dn;
  607. traverse_pci_devices(phb, eeh_ops->of_probe, NULL);
  608. }
  609. }
  610. if (eeh_subsystem_enabled)
  611. pr_info("EEH: PCI Enhanced I/O Error Handling Enabled\n");
  612. else
  613. pr_warning("EEH: No capable adapters found\n");
  614. return ret;
  615. }
  616. core_initcall_sync(eeh_init);
  617. /**
  618. * eeh_add_device_early - Enable EEH for the indicated device_node
  619. * @dn: device node for which to set up EEH
  620. *
  621. * This routine must be used to perform EEH initialization for PCI
  622. * devices that were added after system boot (e.g. hotplug, dlpar).
  623. * This routine must be called before any i/o is performed to the
  624. * adapter (inluding any config-space i/o).
  625. * Whether this actually enables EEH or not for this device depends
  626. * on the CEC architecture, type of the device, on earlier boot
  627. * command-line arguments & etc.
  628. */
  629. static void eeh_add_device_early(struct device_node *dn)
  630. {
  631. struct pci_controller *phb;
  632. if (!of_node_to_eeh_dev(dn))
  633. return;
  634. phb = of_node_to_eeh_dev(dn)->phb;
  635. /* USB Bus children of PCI devices will not have BUID's */
  636. if (NULL == phb || 0 == phb->buid)
  637. return;
  638. /* FIXME: hotplug support on POWERNV */
  639. eeh_ops->of_probe(dn, NULL);
  640. }
  641. /**
  642. * eeh_add_device_tree_early - Enable EEH for the indicated device
  643. * @dn: device node
  644. *
  645. * This routine must be used to perform EEH initialization for the
  646. * indicated PCI device that was added after system boot (e.g.
  647. * hotplug, dlpar).
  648. */
  649. void eeh_add_device_tree_early(struct device_node *dn)
  650. {
  651. struct device_node *sib;
  652. for_each_child_of_node(dn, sib)
  653. eeh_add_device_tree_early(sib);
  654. eeh_add_device_early(dn);
  655. }
  656. EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
  657. /**
  658. * eeh_add_device_late - Perform EEH initialization for the indicated pci device
  659. * @dev: pci device for which to set up EEH
  660. *
  661. * This routine must be used to complete EEH initialization for PCI
  662. * devices that were added after system boot (e.g. hotplug, dlpar).
  663. */
  664. static void eeh_add_device_late(struct pci_dev *dev)
  665. {
  666. struct device_node *dn;
  667. struct eeh_dev *edev;
  668. if (!dev || !eeh_subsystem_enabled)
  669. return;
  670. pr_debug("EEH: Adding device %s\n", pci_name(dev));
  671. dn = pci_device_to_OF_node(dev);
  672. edev = of_node_to_eeh_dev(dn);
  673. if (edev->pdev == dev) {
  674. pr_debug("EEH: Already referenced !\n");
  675. return;
  676. }
  677. WARN_ON(edev->pdev);
  678. pci_dev_get(dev);
  679. edev->pdev = dev;
  680. dev->dev.archdata.edev = edev;
  681. eeh_addr_cache_insert_dev(dev);
  682. }
  683. /**
  684. * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
  685. * @bus: PCI bus
  686. *
  687. * This routine must be used to perform EEH initialization for PCI
  688. * devices which are attached to the indicated PCI bus. The PCI bus
  689. * is added after system boot through hotplug or dlpar.
  690. */
  691. void eeh_add_device_tree_late(struct pci_bus *bus)
  692. {
  693. struct pci_dev *dev;
  694. list_for_each_entry(dev, &bus->devices, bus_list) {
  695. eeh_add_device_late(dev);
  696. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  697. struct pci_bus *subbus = dev->subordinate;
  698. if (subbus)
  699. eeh_add_device_tree_late(subbus);
  700. }
  701. }
  702. }
  703. EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
  704. /**
  705. * eeh_add_sysfs_files - Add EEH sysfs files for the indicated PCI bus
  706. * @bus: PCI bus
  707. *
  708. * This routine must be used to add EEH sysfs files for PCI
  709. * devices which are attached to the indicated PCI bus. The PCI bus
  710. * is added after system boot through hotplug or dlpar.
  711. */
  712. void eeh_add_sysfs_files(struct pci_bus *bus)
  713. {
  714. struct pci_dev *dev;
  715. list_for_each_entry(dev, &bus->devices, bus_list) {
  716. eeh_sysfs_add_device(dev);
  717. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  718. struct pci_bus *subbus = dev->subordinate;
  719. if (subbus)
  720. eeh_add_sysfs_files(subbus);
  721. }
  722. }
  723. }
  724. EXPORT_SYMBOL_GPL(eeh_add_sysfs_files);
  725. /**
  726. * eeh_remove_device - Undo EEH setup for the indicated pci device
  727. * @dev: pci device to be removed
  728. * @purge_pe: remove the PE or not
  729. *
  730. * This routine should be called when a device is removed from
  731. * a running system (e.g. by hotplug or dlpar). It unregisters
  732. * the PCI device from the EEH subsystem. I/O errors affecting
  733. * this device will no longer be detected after this call; thus,
  734. * i/o errors affecting this slot may leave this device unusable.
  735. */
  736. static void eeh_remove_device(struct pci_dev *dev, int purge_pe)
  737. {
  738. struct eeh_dev *edev;
  739. if (!dev || !eeh_subsystem_enabled)
  740. return;
  741. edev = pci_dev_to_eeh_dev(dev);
  742. /* Unregister the device with the EEH/PCI address search system */
  743. pr_debug("EEH: Removing device %s\n", pci_name(dev));
  744. if (!edev || !edev->pdev) {
  745. pr_debug("EEH: Not referenced !\n");
  746. return;
  747. }
  748. edev->pdev = NULL;
  749. dev->dev.archdata.edev = NULL;
  750. pci_dev_put(dev);
  751. eeh_rmv_from_parent_pe(edev, purge_pe);
  752. eeh_addr_cache_rmv_dev(dev);
  753. eeh_sysfs_remove_device(dev);
  754. }
  755. /**
  756. * eeh_remove_bus_device - Undo EEH setup for the indicated PCI device
  757. * @dev: PCI device
  758. * @purge_pe: remove the corresponding PE or not
  759. *
  760. * This routine must be called when a device is removed from the
  761. * running system through hotplug or dlpar. The corresponding
  762. * PCI address cache will be removed.
  763. */
  764. void eeh_remove_bus_device(struct pci_dev *dev, int purge_pe)
  765. {
  766. struct pci_bus *bus = dev->subordinate;
  767. struct pci_dev *child, *tmp;
  768. eeh_remove_device(dev, purge_pe);
  769. if (bus && dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  770. list_for_each_entry_safe(child, tmp, &bus->devices, bus_list)
  771. eeh_remove_bus_device(child, purge_pe);
  772. }
  773. }
  774. EXPORT_SYMBOL_GPL(eeh_remove_bus_device);
  775. static int proc_eeh_show(struct seq_file *m, void *v)
  776. {
  777. if (0 == eeh_subsystem_enabled) {
  778. seq_printf(m, "EEH Subsystem is globally disabled\n");
  779. seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
  780. } else {
  781. seq_printf(m, "EEH Subsystem is enabled\n");
  782. seq_printf(m,
  783. "no device=%llu\n"
  784. "no device node=%llu\n"
  785. "no config address=%llu\n"
  786. "check not wanted=%llu\n"
  787. "eeh_total_mmio_ffs=%llu\n"
  788. "eeh_false_positives=%llu\n"
  789. "eeh_slot_resets=%llu\n",
  790. eeh_stats.no_device,
  791. eeh_stats.no_dn,
  792. eeh_stats.no_cfg_addr,
  793. eeh_stats.ignored_check,
  794. eeh_stats.total_mmio_ffs,
  795. eeh_stats.false_positives,
  796. eeh_stats.slot_resets);
  797. }
  798. return 0;
  799. }
  800. static int proc_eeh_open(struct inode *inode, struct file *file)
  801. {
  802. return single_open(file, proc_eeh_show, NULL);
  803. }
  804. static const struct file_operations proc_eeh_operations = {
  805. .open = proc_eeh_open,
  806. .read = seq_read,
  807. .llseek = seq_lseek,
  808. .release = single_release,
  809. };
  810. static int __init eeh_init_proc(void)
  811. {
  812. if (machine_is(pseries))
  813. proc_create("powerpc/eeh", 0, NULL, &proc_eeh_operations);
  814. return 0;
  815. }
  816. __initcall(eeh_init_proc);