eeh.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. /* Global EEH mutex */
  87. DEFINE_MUTEX(eeh_mutex);
  88. /* Lock to avoid races due to multiple reports of an error */
  89. static DEFINE_RAW_SPINLOCK(confirm_error_lock);
  90. /* Buffer for reporting pci register dumps. Its here in BSS, and
  91. * not dynamically alloced, so that it ends up in RMO where RTAS
  92. * can access it.
  93. */
  94. #define EEH_PCI_REGS_LOG_LEN 4096
  95. static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];
  96. /*
  97. * The struct is used to maintain the EEH global statistic
  98. * information. Besides, the EEH global statistics will be
  99. * exported to user space through procfs
  100. */
  101. struct eeh_stats {
  102. u64 no_device; /* PCI device not found */
  103. u64 no_dn; /* OF node not found */
  104. u64 no_cfg_addr; /* Config address not found */
  105. u64 ignored_check; /* EEH check skipped */
  106. u64 total_mmio_ffs; /* Total EEH checks */
  107. u64 false_positives; /* Unnecessary EEH checks */
  108. u64 slot_resets; /* PE reset */
  109. };
  110. static struct eeh_stats eeh_stats;
  111. #define IS_BRIDGE(class_code) (((class_code)<<16) == PCI_BASE_CLASS_BRIDGE)
  112. /**
  113. * eeh_gather_pci_data - Copy assorted PCI config space registers to buff
  114. * @edev: device to report data for
  115. * @buf: point to buffer in which to log
  116. * @len: amount of room in buffer
  117. *
  118. * This routine captures assorted PCI configuration space data,
  119. * and puts them into a buffer for RTAS error logging.
  120. */
  121. static size_t eeh_gather_pci_data(struct eeh_dev *edev, char * buf, size_t len)
  122. {
  123. struct device_node *dn = eeh_dev_to_of_node(edev);
  124. struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
  125. u32 cfg;
  126. int cap, i;
  127. int n = 0;
  128. n += scnprintf(buf+n, len-n, "%s\n", dn->full_name);
  129. printk(KERN_WARNING "EEH: of node=%s\n", dn->full_name);
  130. eeh_ops->read_config(dn, PCI_VENDOR_ID, 4, &cfg);
  131. n += scnprintf(buf+n, len-n, "dev/vend:%08x\n", cfg);
  132. printk(KERN_WARNING "EEH: PCI device/vendor: %08x\n", cfg);
  133. eeh_ops->read_config(dn, PCI_COMMAND, 4, &cfg);
  134. n += scnprintf(buf+n, len-n, "cmd/stat:%x\n", cfg);
  135. printk(KERN_WARNING "EEH: PCI cmd/status register: %08x\n", cfg);
  136. if (!dev) {
  137. printk(KERN_WARNING "EEH: no PCI device for this of node\n");
  138. return n;
  139. }
  140. /* Gather bridge-specific registers */
  141. if (dev->class >> 16 == PCI_BASE_CLASS_BRIDGE) {
  142. eeh_ops->read_config(dn, PCI_SEC_STATUS, 2, &cfg);
  143. n += scnprintf(buf+n, len-n, "sec stat:%x\n", cfg);
  144. printk(KERN_WARNING "EEH: Bridge secondary status: %04x\n", cfg);
  145. eeh_ops->read_config(dn, PCI_BRIDGE_CONTROL, 2, &cfg);
  146. n += scnprintf(buf+n, len-n, "brdg ctl:%x\n", cfg);
  147. printk(KERN_WARNING "EEH: Bridge control: %04x\n", cfg);
  148. }
  149. /* Dump out the PCI-X command and status regs */
  150. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  151. if (cap) {
  152. eeh_ops->read_config(dn, cap, 4, &cfg);
  153. n += scnprintf(buf+n, len-n, "pcix-cmd:%x\n", cfg);
  154. printk(KERN_WARNING "EEH: PCI-X cmd: %08x\n", cfg);
  155. eeh_ops->read_config(dn, cap+4, 4, &cfg);
  156. n += scnprintf(buf+n, len-n, "pcix-stat:%x\n", cfg);
  157. printk(KERN_WARNING "EEH: PCI-X status: %08x\n", cfg);
  158. }
  159. /* If PCI-E capable, dump PCI-E cap 10, and the AER */
  160. cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
  161. if (cap) {
  162. n += scnprintf(buf+n, len-n, "pci-e cap10:\n");
  163. printk(KERN_WARNING
  164. "EEH: PCI-E capabilities and status follow:\n");
  165. for (i=0; i<=8; i++) {
  166. eeh_ops->read_config(dn, cap+4*i, 4, &cfg);
  167. n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
  168. printk(KERN_WARNING "EEH: PCI-E %02x: %08x\n", i, cfg);
  169. }
  170. cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  171. if (cap) {
  172. n += scnprintf(buf+n, len-n, "pci-e AER:\n");
  173. printk(KERN_WARNING
  174. "EEH: PCI-E AER capability register set follows:\n");
  175. for (i=0; i<14; 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 AER %02x: %08x\n", i, cfg);
  179. }
  180. }
  181. }
  182. return n;
  183. }
  184. /**
  185. * eeh_slot_error_detail - Generate combined log including driver log and error log
  186. * @pe: EEH PE
  187. * @severity: temporary or permanent error log
  188. *
  189. * This routine should be called to generate the combined log, which
  190. * is comprised of driver log and error log. The driver log is figured
  191. * out from the config space of the corresponding PCI device, while
  192. * the error log is fetched through platform dependent function call.
  193. */
  194. void eeh_slot_error_detail(struct eeh_pe *pe, int severity)
  195. {
  196. size_t loglen = 0;
  197. struct eeh_dev *edev;
  198. eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
  199. eeh_ops->configure_bridge(pe);
  200. eeh_pe_restore_bars(pe);
  201. pci_regs_buf[0] = 0;
  202. eeh_pe_for_each_dev(pe, edev) {
  203. loglen += eeh_gather_pci_data(edev, pci_regs_buf,
  204. EEH_PCI_REGS_LOG_LEN);
  205. }
  206. eeh_ops->get_log(pe, severity, pci_regs_buf, loglen);
  207. }
  208. /**
  209. * eeh_token_to_phys - Convert EEH address token to phys address
  210. * @token: I/O token, should be address in the form 0xA....
  211. *
  212. * This routine should be called to convert virtual I/O address
  213. * to physical one.
  214. */
  215. static inline unsigned long eeh_token_to_phys(unsigned long token)
  216. {
  217. pte_t *ptep;
  218. unsigned long pa;
  219. ptep = find_linux_pte(init_mm.pgd, token);
  220. if (!ptep)
  221. return token;
  222. pa = pte_pfn(*ptep) << PAGE_SHIFT;
  223. return pa | (token & (PAGE_SIZE-1));
  224. }
  225. /**
  226. * eeh_dn_check_failure - Check if all 1's data is due to EEH slot freeze
  227. * @dn: device node
  228. * @dev: pci device, if known
  229. *
  230. * Check for an EEH failure for the given device node. Call this
  231. * routine if the result of a read was all 0xff's and you want to
  232. * find out if this is due to an EEH slot freeze. This routine
  233. * will query firmware for the EEH status.
  234. *
  235. * Returns 0 if there has not been an EEH error; otherwise returns
  236. * a non-zero value and queues up a slot isolation event notification.
  237. *
  238. * It is safe to call this routine in an interrupt context.
  239. */
  240. int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)
  241. {
  242. int ret;
  243. unsigned long flags;
  244. struct eeh_pe *pe;
  245. struct eeh_dev *edev;
  246. int rc = 0;
  247. const char *location;
  248. eeh_stats.total_mmio_ffs++;
  249. if (!eeh_subsystem_enabled)
  250. return 0;
  251. if (dn) {
  252. edev = of_node_to_eeh_dev(dn);
  253. } else if (dev) {
  254. edev = pci_dev_to_eeh_dev(dev);
  255. dn = pci_device_to_OF_node(dev);
  256. } else {
  257. eeh_stats.no_dn++;
  258. return 0;
  259. }
  260. pe = edev->pe;
  261. /* Access to IO BARs might get this far and still not want checking. */
  262. if (!pe) {
  263. eeh_stats.ignored_check++;
  264. pr_debug("EEH: Ignored check for %s %s\n",
  265. eeh_pci_name(dev), dn->full_name);
  266. return 0;
  267. }
  268. if (!pe->addr && !pe->config_addr) {
  269. eeh_stats.no_cfg_addr++;
  270. return 0;
  271. }
  272. /* If we already have a pending isolation event for this
  273. * slot, we know it's bad already, we don't need to check.
  274. * Do this checking under a lock; as multiple PCI devices
  275. * in one slot might report errors simultaneously, and we
  276. * only want one error recovery routine running.
  277. */
  278. raw_spin_lock_irqsave(&confirm_error_lock, flags);
  279. rc = 1;
  280. if (pe->state & EEH_PE_ISOLATED) {
  281. pe->check_count++;
  282. if (pe->check_count % EEH_MAX_FAILS == 0) {
  283. location = of_get_property(dn, "ibm,loc-code", NULL);
  284. printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
  285. "location=%s driver=%s pci addr=%s\n",
  286. pe->check_count, location,
  287. eeh_driver_name(dev), eeh_pci_name(dev));
  288. printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
  289. eeh_driver_name(dev));
  290. dump_stack();
  291. }
  292. goto dn_unlock;
  293. }
  294. /*
  295. * Now test for an EEH failure. This is VERY expensive.
  296. * Note that the eeh_config_addr may be a parent device
  297. * in the case of a device behind a bridge, or it may be
  298. * function zero of a multi-function device.
  299. * In any case they must share a common PHB.
  300. */
  301. ret = eeh_ops->get_state(pe, NULL);
  302. /* Note that config-io to empty slots may fail;
  303. * they are empty when they don't have children.
  304. * We will punt with the following conditions: Failure to get
  305. * PE's state, EEH not support and Permanently unavailable
  306. * state, PE is in good state.
  307. */
  308. if ((ret < 0) ||
  309. (ret == EEH_STATE_NOT_SUPPORT) ||
  310. (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
  311. (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
  312. eeh_stats.false_positives++;
  313. pe->false_positives++;
  314. rc = 0;
  315. goto dn_unlock;
  316. }
  317. eeh_stats.slot_resets++;
  318. /* Avoid repeated reports of this failure, including problems
  319. * with other functions on this device, and functions under
  320. * bridges.
  321. */
  322. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  323. raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
  324. eeh_send_failure_event(pe);
  325. /* Most EEH events are due to device driver bugs. Having
  326. * a stack trace will help the device-driver authors figure
  327. * out what happened. So print that out.
  328. */
  329. WARN(1, "EEH: failure detected\n");
  330. return 1;
  331. dn_unlock:
  332. raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
  333. return rc;
  334. }
  335. EXPORT_SYMBOL_GPL(eeh_dn_check_failure);
  336. /**
  337. * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
  338. * @token: I/O token, should be address in the form 0xA....
  339. * @val: value, should be all 1's (XXX why do we need this arg??)
  340. *
  341. * Check for an EEH failure at the given token address. Call this
  342. * routine if the result of a read was all 0xff's and you want to
  343. * find out if this is due to an EEH slot freeze event. This routine
  344. * will query firmware for the EEH status.
  345. *
  346. * Note this routine is safe to call in an interrupt context.
  347. */
  348. unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
  349. {
  350. unsigned long addr;
  351. struct pci_dev *dev;
  352. struct device_node *dn;
  353. /* Finding the phys addr + pci device; this is pretty quick. */
  354. addr = eeh_token_to_phys((unsigned long __force) token);
  355. dev = pci_addr_cache_get_device(addr);
  356. if (!dev) {
  357. eeh_stats.no_device++;
  358. return val;
  359. }
  360. dn = pci_device_to_OF_node(dev);
  361. eeh_dn_check_failure(dn, dev);
  362. pci_dev_put(dev);
  363. return val;
  364. }
  365. EXPORT_SYMBOL(eeh_check_failure);
  366. /**
  367. * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
  368. * @pe: EEH PE
  369. *
  370. * This routine should be called to reenable frozen MMIO or DMA
  371. * so that it would work correctly again. It's useful while doing
  372. * recovery or log collection on the indicated device.
  373. */
  374. int eeh_pci_enable(struct eeh_pe *pe, int function)
  375. {
  376. int rc;
  377. rc = eeh_ops->set_option(pe, function);
  378. if (rc)
  379. pr_warning("%s: Unexpected state change %d on PHB#%d-PE#%x, err=%d\n",
  380. __func__, function, pe->phb->global_number, pe->addr, rc);
  381. rc = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
  382. if (rc > 0 && (rc & EEH_STATE_MMIO_ENABLED) &&
  383. (function == EEH_OPT_THAW_MMIO))
  384. return 0;
  385. return rc;
  386. }
  387. /**
  388. * pcibios_set_pcie_slot_reset - Set PCI-E reset state
  389. * @dev: pci device struct
  390. * @state: reset state to enter
  391. *
  392. * Return value:
  393. * 0 if success
  394. */
  395. int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  396. {
  397. struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
  398. struct eeh_pe *pe = edev->pe;
  399. if (!pe) {
  400. pr_err("%s: No PE found on PCI device %s\n",
  401. __func__, pci_name(dev));
  402. return -EINVAL;
  403. }
  404. switch (state) {
  405. case pcie_deassert_reset:
  406. eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
  407. break;
  408. case pcie_hot_reset:
  409. eeh_ops->reset(pe, EEH_RESET_HOT);
  410. break;
  411. case pcie_warm_reset:
  412. eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
  413. break;
  414. default:
  415. return -EINVAL;
  416. };
  417. return 0;
  418. }
  419. /**
  420. * eeh_set_pe_freset - Check the required reset for the indicated device
  421. * @data: EEH device
  422. * @flag: return value
  423. *
  424. * Each device might have its preferred reset type: fundamental or
  425. * hot reset. The routine is used to collected the information for
  426. * the indicated device and its children so that the bunch of the
  427. * devices could be reset properly.
  428. */
  429. static void *eeh_set_dev_freset(void *data, void *flag)
  430. {
  431. struct pci_dev *dev;
  432. unsigned int *freset = (unsigned int *)flag;
  433. struct eeh_dev *edev = (struct eeh_dev *)data;
  434. dev = eeh_dev_to_pci_dev(edev);
  435. if (dev)
  436. *freset |= dev->needs_freset;
  437. return NULL;
  438. }
  439. /**
  440. * eeh_reset_pe_once - Assert the pci #RST line for 1/4 second
  441. * @pe: EEH PE
  442. *
  443. * Assert the PCI #RST line for 1/4 second.
  444. */
  445. static void eeh_reset_pe_once(struct eeh_pe *pe)
  446. {
  447. unsigned int freset = 0;
  448. /* Determine type of EEH reset required for
  449. * Partitionable Endpoint, a hot-reset (1)
  450. * or a fundamental reset (3).
  451. * A fundamental reset required by any device under
  452. * Partitionable Endpoint trumps hot-reset.
  453. */
  454. eeh_pe_dev_traverse(pe, eeh_set_dev_freset, &freset);
  455. if (freset)
  456. eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
  457. else
  458. eeh_ops->reset(pe, EEH_RESET_HOT);
  459. /* The PCI bus requires that the reset be held high for at least
  460. * a 100 milliseconds. We wait a bit longer 'just in case'.
  461. */
  462. #define PCI_BUS_RST_HOLD_TIME_MSEC 250
  463. msleep(PCI_BUS_RST_HOLD_TIME_MSEC);
  464. /* We might get hit with another EEH freeze as soon as the
  465. * pci slot reset line is dropped. Make sure we don't miss
  466. * these, and clear the flag now.
  467. */
  468. eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
  469. eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
  470. /* After a PCI slot has been reset, the PCI Express spec requires
  471. * a 1.5 second idle time for the bus to stabilize, before starting
  472. * up traffic.
  473. */
  474. #define PCI_BUS_SETTLE_TIME_MSEC 1800
  475. msleep(PCI_BUS_SETTLE_TIME_MSEC);
  476. }
  477. /**
  478. * eeh_reset_pe - Reset the indicated PE
  479. * @pe: EEH PE
  480. *
  481. * This routine should be called to reset indicated device, including
  482. * PE. A PE might include multiple PCI devices and sometimes PCI bridges
  483. * might be involved as well.
  484. */
  485. int eeh_reset_pe(struct eeh_pe *pe)
  486. {
  487. int i, rc;
  488. /* Take three shots at resetting the bus */
  489. for (i=0; i<3; i++) {
  490. eeh_reset_pe_once(pe);
  491. rc = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
  492. if (rc == (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE))
  493. return 0;
  494. if (rc < 0) {
  495. pr_err("%s: Unrecoverable slot failure on PHB#%d-PE#%x",
  496. __func__, pe->phb->global_number, pe->addr);
  497. return -1;
  498. }
  499. pr_err("EEH: bus reset %d failed on PHB#%d-PE#%x, rc=%d\n",
  500. i+1, pe->phb->global_number, pe->addr, rc);
  501. }
  502. return -1;
  503. }
  504. /**
  505. * eeh_save_bars - Save device bars
  506. * @edev: PCI device associated EEH device
  507. *
  508. * Save the values of the device bars. Unlike the restore
  509. * routine, this routine is *not* recursive. This is because
  510. * PCI devices are added individually; but, for the restore,
  511. * an entire slot is reset at a time.
  512. */
  513. static void eeh_save_bars(struct eeh_dev *edev)
  514. {
  515. int i;
  516. struct device_node *dn;
  517. if (!edev)
  518. return;
  519. dn = eeh_dev_to_of_node(edev);
  520. for (i = 0; i < 16; i++)
  521. eeh_ops->read_config(dn, i * 4, 4, &edev->config_space[i]);
  522. }
  523. /**
  524. * eeh_early_enable - Early enable EEH on the indicated device
  525. * @dn: device node
  526. * @data: BUID
  527. *
  528. * Enable EEH functionality on the specified PCI device. The function
  529. * is expected to be called before real PCI probing is done. However,
  530. * the PHBs have been initialized at this point.
  531. */
  532. static void *eeh_early_enable(struct device_node *dn, void *data)
  533. {
  534. int ret;
  535. const u32 *class_code = of_get_property(dn, "class-code", NULL);
  536. const u32 *vendor_id = of_get_property(dn, "vendor-id", NULL);
  537. const u32 *device_id = of_get_property(dn, "device-id", NULL);
  538. const u32 *regs;
  539. int enable;
  540. struct eeh_dev *edev = of_node_to_eeh_dev(dn);
  541. struct eeh_pe pe;
  542. edev->class_code = 0;
  543. edev->mode = 0;
  544. if (!of_device_is_available(dn))
  545. return NULL;
  546. /* Ignore bad nodes. */
  547. if (!class_code || !vendor_id || !device_id)
  548. return NULL;
  549. /* There is nothing to check on PCI to ISA bridges */
  550. if (dn->type && !strcmp(dn->type, "isa"))
  551. return NULL;
  552. edev->class_code = *class_code;
  553. /* Ok... see if this device supports EEH. Some do, some don't,
  554. * and the only way to find out is to check each and every one.
  555. */
  556. regs = of_get_property(dn, "reg", NULL);
  557. if (regs) {
  558. /* Initialize the fake PE */
  559. memset(&pe, 0, sizeof(struct eeh_pe));
  560. pe.phb = edev->phb;
  561. pe.config_addr = regs[0];
  562. /* First register entry is addr (00BBSS00) */
  563. /* Try to enable eeh */
  564. ret = eeh_ops->set_option(&pe, EEH_OPT_ENABLE);
  565. enable = 0;
  566. if (ret == 0) {
  567. edev->config_addr = regs[0];
  568. /* If the newer, better, ibm,get-config-addr-info is supported,
  569. * then use that instead.
  570. */
  571. edev->pe_config_addr = eeh_ops->get_pe_addr(&pe);
  572. pe.addr = edev->pe_config_addr;
  573. /* Some older systems (Power4) allow the
  574. * ibm,set-eeh-option call to succeed even on nodes
  575. * where EEH is not supported. Verify support
  576. * explicitly.
  577. */
  578. ret = eeh_ops->get_state(&pe, NULL);
  579. if (ret > 0 && ret != EEH_STATE_NOT_SUPPORT)
  580. enable = 1;
  581. }
  582. if (enable) {
  583. eeh_subsystem_enabled = 1;
  584. eeh_add_to_parent_pe(edev);
  585. pr_debug("EEH: %s: eeh enabled, config=%x pe_config=%x\n",
  586. dn->full_name, edev->config_addr,
  587. edev->pe_config_addr);
  588. } else {
  589. /* This device doesn't support EEH, but it may have an
  590. * EEH parent, in which case we mark it as supported.
  591. */
  592. if (dn->parent && of_node_to_eeh_dev(dn->parent) &&
  593. of_node_to_eeh_dev(dn->parent)->pe) {
  594. /* Parent supports EEH. */
  595. edev->config_addr = of_node_to_eeh_dev(dn->parent)->config_addr;
  596. edev->pe_config_addr = of_node_to_eeh_dev(dn->parent)->pe_config_addr;
  597. eeh_add_to_parent_pe(edev);
  598. return NULL;
  599. }
  600. }
  601. } else {
  602. printk(KERN_WARNING "EEH: %s: unable to get reg property.\n",
  603. dn->full_name);
  604. }
  605. eeh_save_bars(edev);
  606. return NULL;
  607. }
  608. /**
  609. * eeh_ops_register - Register platform dependent EEH operations
  610. * @ops: platform dependent EEH operations
  611. *
  612. * Register the platform dependent EEH operation callback
  613. * functions. The platform should call this function before
  614. * any other EEH operations.
  615. */
  616. int __init eeh_ops_register(struct eeh_ops *ops)
  617. {
  618. if (!ops->name) {
  619. pr_warning("%s: Invalid EEH ops name for %p\n",
  620. __func__, ops);
  621. return -EINVAL;
  622. }
  623. if (eeh_ops && eeh_ops != ops) {
  624. pr_warning("%s: EEH ops of platform %s already existing (%s)\n",
  625. __func__, eeh_ops->name, ops->name);
  626. return -EEXIST;
  627. }
  628. eeh_ops = ops;
  629. return 0;
  630. }
  631. /**
  632. * eeh_ops_unregister - Unreigster platform dependent EEH operations
  633. * @name: name of EEH platform operations
  634. *
  635. * Unregister the platform dependent EEH operation callback
  636. * functions.
  637. */
  638. int __exit eeh_ops_unregister(const char *name)
  639. {
  640. if (!name || !strlen(name)) {
  641. pr_warning("%s: Invalid EEH ops name\n",
  642. __func__);
  643. return -EINVAL;
  644. }
  645. if (eeh_ops && !strcmp(eeh_ops->name, name)) {
  646. eeh_ops = NULL;
  647. return 0;
  648. }
  649. return -EEXIST;
  650. }
  651. /**
  652. * eeh_init - EEH initialization
  653. *
  654. * Initialize EEH by trying to enable it for all of the adapters in the system.
  655. * As a side effect we can determine here if eeh is supported at all.
  656. * Note that we leave EEH on so failed config cycles won't cause a machine
  657. * check. If a user turns off EEH for a particular adapter they are really
  658. * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
  659. * grant access to a slot if EEH isn't enabled, and so we always enable
  660. * EEH for all slots/all devices.
  661. *
  662. * The eeh-force-off option disables EEH checking globally, for all slots.
  663. * Even if force-off is set, the EEH hardware is still enabled, so that
  664. * newer systems can boot.
  665. */
  666. static int __init eeh_init(void)
  667. {
  668. struct pci_controller *hose, *tmp;
  669. struct device_node *phb;
  670. int ret;
  671. /* call platform initialization function */
  672. if (!eeh_ops) {
  673. pr_warning("%s: Platform EEH operation not found\n",
  674. __func__);
  675. return -EEXIST;
  676. } else if ((ret = eeh_ops->init())) {
  677. pr_warning("%s: Failed to call platform init function (%d)\n",
  678. __func__, ret);
  679. return ret;
  680. }
  681. raw_spin_lock_init(&confirm_error_lock);
  682. /* Enable EEH for all adapters */
  683. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  684. phb = hose->dn;
  685. traverse_pci_devices(phb, eeh_early_enable, NULL);
  686. }
  687. if (eeh_subsystem_enabled)
  688. printk(KERN_INFO "EEH: PCI Enhanced I/O Error Handling Enabled\n");
  689. else
  690. printk(KERN_WARNING "EEH: No capable adapters found\n");
  691. return ret;
  692. }
  693. core_initcall_sync(eeh_init);
  694. /**
  695. * eeh_add_device_early - Enable EEH for the indicated device_node
  696. * @dn: device node for which to set up EEH
  697. *
  698. * This routine must be used to perform EEH initialization for PCI
  699. * devices that were added after system boot (e.g. hotplug, dlpar).
  700. * This routine must be called before any i/o is performed to the
  701. * adapter (inluding any config-space i/o).
  702. * Whether this actually enables EEH or not for this device depends
  703. * on the CEC architecture, type of the device, on earlier boot
  704. * command-line arguments & etc.
  705. */
  706. static void eeh_add_device_early(struct device_node *dn)
  707. {
  708. struct pci_controller *phb;
  709. if (!dn || !of_node_to_eeh_dev(dn))
  710. return;
  711. phb = of_node_to_eeh_dev(dn)->phb;
  712. /* USB Bus children of PCI devices will not have BUID's */
  713. if (NULL == phb || 0 == phb->buid)
  714. return;
  715. eeh_early_enable(dn, NULL);
  716. }
  717. /**
  718. * eeh_add_device_tree_early - Enable EEH for the indicated device
  719. * @dn: device node
  720. *
  721. * This routine must be used to perform EEH initialization for the
  722. * indicated PCI device that was added after system boot (e.g.
  723. * hotplug, dlpar).
  724. */
  725. void eeh_add_device_tree_early(struct device_node *dn)
  726. {
  727. struct device_node *sib;
  728. for_each_child_of_node(dn, sib)
  729. eeh_add_device_tree_early(sib);
  730. eeh_add_device_early(dn);
  731. }
  732. EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
  733. /**
  734. * eeh_add_device_late - Perform EEH initialization for the indicated pci device
  735. * @dev: pci device for which to set up EEH
  736. *
  737. * This routine must be used to complete EEH initialization for PCI
  738. * devices that were added after system boot (e.g. hotplug, dlpar).
  739. */
  740. static void eeh_add_device_late(struct pci_dev *dev)
  741. {
  742. struct device_node *dn;
  743. struct eeh_dev *edev;
  744. if (!dev || !eeh_subsystem_enabled)
  745. return;
  746. pr_debug("EEH: Adding device %s\n", pci_name(dev));
  747. dn = pci_device_to_OF_node(dev);
  748. edev = of_node_to_eeh_dev(dn);
  749. if (edev->pdev == dev) {
  750. pr_debug("EEH: Already referenced !\n");
  751. return;
  752. }
  753. WARN_ON(edev->pdev);
  754. pci_dev_get(dev);
  755. edev->pdev = dev;
  756. dev->dev.archdata.edev = edev;
  757. pci_addr_cache_insert_device(dev);
  758. eeh_sysfs_add_device(dev);
  759. }
  760. /**
  761. * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
  762. * @bus: PCI bus
  763. *
  764. * This routine must be used to perform EEH initialization for PCI
  765. * devices which are attached to the indicated PCI bus. The PCI bus
  766. * is added after system boot through hotplug or dlpar.
  767. */
  768. void eeh_add_device_tree_late(struct pci_bus *bus)
  769. {
  770. struct pci_dev *dev;
  771. list_for_each_entry(dev, &bus->devices, bus_list) {
  772. eeh_add_device_late(dev);
  773. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  774. struct pci_bus *subbus = dev->subordinate;
  775. if (subbus)
  776. eeh_add_device_tree_late(subbus);
  777. }
  778. }
  779. }
  780. EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
  781. /**
  782. * eeh_remove_device - Undo EEH setup for the indicated pci device
  783. * @dev: pci device to be removed
  784. *
  785. * This routine should be called when a device is removed from
  786. * a running system (e.g. by hotplug or dlpar). It unregisters
  787. * the PCI device from the EEH subsystem. I/O errors affecting
  788. * this device will no longer be detected after this call; thus,
  789. * i/o errors affecting this slot may leave this device unusable.
  790. */
  791. static void eeh_remove_device(struct pci_dev *dev)
  792. {
  793. struct eeh_dev *edev;
  794. if (!dev || !eeh_subsystem_enabled)
  795. return;
  796. edev = pci_dev_to_eeh_dev(dev);
  797. /* Unregister the device with the EEH/PCI address search system */
  798. pr_debug("EEH: Removing device %s\n", pci_name(dev));
  799. if (!edev || !edev->pdev) {
  800. pr_debug("EEH: Not referenced !\n");
  801. return;
  802. }
  803. edev->pdev = NULL;
  804. dev->dev.archdata.edev = NULL;
  805. pci_dev_put(dev);
  806. eeh_rmv_from_parent_pe(edev);
  807. pci_addr_cache_remove_device(dev);
  808. eeh_sysfs_remove_device(dev);
  809. }
  810. /**
  811. * eeh_remove_bus_device - Undo EEH setup for the indicated PCI device
  812. * @dev: PCI device
  813. *
  814. * This routine must be called when a device is removed from the
  815. * running system through hotplug or dlpar. The corresponding
  816. * PCI address cache will be removed.
  817. */
  818. void eeh_remove_bus_device(struct pci_dev *dev)
  819. {
  820. struct pci_bus *bus = dev->subordinate;
  821. struct pci_dev *child, *tmp;
  822. eeh_remove_device(dev);
  823. if (bus && dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  824. list_for_each_entry_safe(child, tmp, &bus->devices, bus_list)
  825. eeh_remove_bus_device(child);
  826. }
  827. }
  828. EXPORT_SYMBOL_GPL(eeh_remove_bus_device);
  829. static int proc_eeh_show(struct seq_file *m, void *v)
  830. {
  831. if (0 == eeh_subsystem_enabled) {
  832. seq_printf(m, "EEH Subsystem is globally disabled\n");
  833. seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
  834. } else {
  835. seq_printf(m, "EEH Subsystem is enabled\n");
  836. seq_printf(m,
  837. "no device=%llu\n"
  838. "no device node=%llu\n"
  839. "no config address=%llu\n"
  840. "check not wanted=%llu\n"
  841. "eeh_total_mmio_ffs=%llu\n"
  842. "eeh_false_positives=%llu\n"
  843. "eeh_slot_resets=%llu\n",
  844. eeh_stats.no_device,
  845. eeh_stats.no_dn,
  846. eeh_stats.no_cfg_addr,
  847. eeh_stats.ignored_check,
  848. eeh_stats.total_mmio_ffs,
  849. eeh_stats.false_positives,
  850. eeh_stats.slot_resets);
  851. }
  852. return 0;
  853. }
  854. static int proc_eeh_open(struct inode *inode, struct file *file)
  855. {
  856. return single_open(file, proc_eeh_show, NULL);
  857. }
  858. static const struct file_operations proc_eeh_operations = {
  859. .open = proc_eeh_open,
  860. .read = seq_read,
  861. .llseek = seq_lseek,
  862. .release = single_release,
  863. };
  864. static int __init eeh_init_proc(void)
  865. {
  866. if (machine_is(pseries))
  867. proc_create("powerpc/eeh", 0, NULL, &proc_eeh_operations);
  868. return 0;
  869. }
  870. __initcall(eeh_init_proc);