eeh.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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. /* Gather status on devices under the bridge */
  183. if (dev->class >> 16 == PCI_BASE_CLASS_BRIDGE) {
  184. struct device_node *child;
  185. for_each_child_of_node(dn, child) {
  186. if (of_node_to_eeh_dev(child))
  187. n += eeh_gather_pci_data(of_node_to_eeh_dev(child), buf+n, len-n);
  188. }
  189. }
  190. return n;
  191. }
  192. /**
  193. * eeh_slot_error_detail - Generate combined log including driver log and error log
  194. * @edev: device to report error log for
  195. * @severity: temporary or permanent error log
  196. *
  197. * This routine should be called to generate the combined log, which
  198. * is comprised of driver log and error log. The driver log is figured
  199. * out from the config space of the corresponding PCI device, while
  200. * the error log is fetched through platform dependent function call.
  201. */
  202. void eeh_slot_error_detail(struct eeh_dev *edev, int severity)
  203. {
  204. size_t loglen = 0;
  205. pci_regs_buf[0] = 0;
  206. eeh_pci_enable(edev, EEH_OPT_THAW_MMIO);
  207. eeh_ops->configure_bridge(eeh_dev_to_of_node(edev));
  208. eeh_restore_bars(edev);
  209. loglen = eeh_gather_pci_data(edev, pci_regs_buf, EEH_PCI_REGS_LOG_LEN);
  210. eeh_ops->get_log(eeh_dev_to_of_node(edev), severity, pci_regs_buf, loglen);
  211. }
  212. /**
  213. * eeh_token_to_phys - Convert EEH address token to phys address
  214. * @token: I/O token, should be address in the form 0xA....
  215. *
  216. * This routine should be called to convert virtual I/O address
  217. * to physical one.
  218. */
  219. static inline unsigned long eeh_token_to_phys(unsigned long token)
  220. {
  221. pte_t *ptep;
  222. unsigned long pa;
  223. ptep = find_linux_pte(init_mm.pgd, token);
  224. if (!ptep)
  225. return token;
  226. pa = pte_pfn(*ptep) << PAGE_SHIFT;
  227. return pa | (token & (PAGE_SIZE-1));
  228. }
  229. /**
  230. * eeh_dn_check_failure - Check if all 1's data is due to EEH slot freeze
  231. * @dn: device node
  232. * @dev: pci device, if known
  233. *
  234. * Check for an EEH failure for the given device node. Call this
  235. * routine if the result of a read was all 0xff's and you want to
  236. * find out if this is due to an EEH slot freeze. This routine
  237. * will query firmware for the EEH status.
  238. *
  239. * Returns 0 if there has not been an EEH error; otherwise returns
  240. * a non-zero value and queues up a slot isolation event notification.
  241. *
  242. * It is safe to call this routine in an interrupt context.
  243. */
  244. int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)
  245. {
  246. int ret;
  247. unsigned long flags;
  248. struct eeh_pe *pe;
  249. struct eeh_dev *edev;
  250. int rc = 0;
  251. const char *location;
  252. eeh_stats.total_mmio_ffs++;
  253. if (!eeh_subsystem_enabled)
  254. return 0;
  255. if (dn) {
  256. edev = of_node_to_eeh_dev(dn);
  257. } else if (dev) {
  258. edev = pci_dev_to_eeh_dev(dev);
  259. dn = pci_device_to_OF_node(dev);
  260. } else {
  261. eeh_stats.no_dn++;
  262. return 0;
  263. }
  264. pe = edev->pe;
  265. /* Access to IO BARs might get this far and still not want checking. */
  266. if (!pe) {
  267. eeh_stats.ignored_check++;
  268. pr_debug("EEH: Ignored check for %s %s\n",
  269. eeh_pci_name(dev), dn->full_name);
  270. return 0;
  271. }
  272. if (!pe->addr && !pe->config_addr) {
  273. eeh_stats.no_cfg_addr++;
  274. return 0;
  275. }
  276. /* If we already have a pending isolation event for this
  277. * slot, we know it's bad already, we don't need to check.
  278. * Do this checking under a lock; as multiple PCI devices
  279. * in one slot might report errors simultaneously, and we
  280. * only want one error recovery routine running.
  281. */
  282. raw_spin_lock_irqsave(&confirm_error_lock, flags);
  283. rc = 1;
  284. if (pe->state & EEH_PE_ISOLATED) {
  285. pe->check_count++;
  286. if (pe->check_count % EEH_MAX_FAILS == 0) {
  287. location = of_get_property(dn, "ibm,loc-code", NULL);
  288. printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
  289. "location=%s driver=%s pci addr=%s\n",
  290. pe->check_count, location,
  291. eeh_driver_name(dev), eeh_pci_name(dev));
  292. printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
  293. eeh_driver_name(dev));
  294. dump_stack();
  295. }
  296. goto dn_unlock;
  297. }
  298. /*
  299. * Now test for an EEH failure. This is VERY expensive.
  300. * Note that the eeh_config_addr may be a parent device
  301. * in the case of a device behind a bridge, or it may be
  302. * function zero of a multi-function device.
  303. * In any case they must share a common PHB.
  304. */
  305. ret = eeh_ops->get_state(pe, NULL);
  306. /* Note that config-io to empty slots may fail;
  307. * they are empty when they don't have children.
  308. * We will punt with the following conditions: Failure to get
  309. * PE's state, EEH not support and Permanently unavailable
  310. * state, PE is in good state.
  311. */
  312. if ((ret < 0) ||
  313. (ret == EEH_STATE_NOT_SUPPORT) ||
  314. (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
  315. (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
  316. eeh_stats.false_positives++;
  317. pe->false_positives++;
  318. rc = 0;
  319. goto dn_unlock;
  320. }
  321. eeh_stats.slot_resets++;
  322. /* Avoid repeated reports of this failure, including problems
  323. * with other functions on this device, and functions under
  324. * bridges.
  325. */
  326. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  327. raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
  328. eeh_send_failure_event(pe);
  329. /* Most EEH events are due to device driver bugs. Having
  330. * a stack trace will help the device-driver authors figure
  331. * out what happened. So print that out.
  332. */
  333. WARN(1, "EEH: failure detected\n");
  334. return 1;
  335. dn_unlock:
  336. raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
  337. return rc;
  338. }
  339. EXPORT_SYMBOL_GPL(eeh_dn_check_failure);
  340. /**
  341. * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
  342. * @token: I/O token, should be address in the form 0xA....
  343. * @val: value, should be all 1's (XXX why do we need this arg??)
  344. *
  345. * Check for an EEH failure at the given token address. Call this
  346. * routine if the result of a read was all 0xff's and you want to
  347. * find out if this is due to an EEH slot freeze event. This routine
  348. * will query firmware for the EEH status.
  349. *
  350. * Note this routine is safe to call in an interrupt context.
  351. */
  352. unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
  353. {
  354. unsigned long addr;
  355. struct pci_dev *dev;
  356. struct device_node *dn;
  357. /* Finding the phys addr + pci device; this is pretty quick. */
  358. addr = eeh_token_to_phys((unsigned long __force) token);
  359. dev = pci_addr_cache_get_device(addr);
  360. if (!dev) {
  361. eeh_stats.no_device++;
  362. return val;
  363. }
  364. dn = pci_device_to_OF_node(dev);
  365. eeh_dn_check_failure(dn, dev);
  366. pci_dev_put(dev);
  367. return val;
  368. }
  369. EXPORT_SYMBOL(eeh_check_failure);
  370. /**
  371. * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
  372. * @edev: pci device node
  373. *
  374. * This routine should be called to reenable frozen MMIO or DMA
  375. * so that it would work correctly again. It's useful while doing
  376. * recovery or log collection on the indicated device.
  377. */
  378. int eeh_pci_enable(struct eeh_dev *edev, int function)
  379. {
  380. int rc;
  381. struct device_node *dn = eeh_dev_to_of_node(edev);
  382. rc = eeh_ops->set_option(dn, function);
  383. if (rc)
  384. printk(KERN_WARNING "EEH: Unexpected state change %d, err=%d dn=%s\n",
  385. function, rc, dn->full_name);
  386. rc = eeh_ops->wait_state(dn, 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 device_node *dn = pci_device_to_OF_node(dev);
  403. switch (state) {
  404. case pcie_deassert_reset:
  405. eeh_ops->reset(dn, EEH_RESET_DEACTIVATE);
  406. break;
  407. case pcie_hot_reset:
  408. eeh_ops->reset(dn, EEH_RESET_HOT);
  409. break;
  410. case pcie_warm_reset:
  411. eeh_ops->reset(dn, EEH_RESET_FUNDAMENTAL);
  412. break;
  413. default:
  414. return -EINVAL;
  415. };
  416. return 0;
  417. }
  418. /**
  419. * __eeh_set_pe_freset - Check the required reset for child devices
  420. * @parent: parent device
  421. * @freset: return value
  422. *
  423. * Each device might have its preferred reset type: fundamental or
  424. * hot reset. The routine is used to collect the information from
  425. * the child devices so that they could be reset accordingly.
  426. */
  427. void __eeh_set_pe_freset(struct device_node *parent, unsigned int *freset)
  428. {
  429. struct device_node *dn;
  430. for_each_child_of_node(parent, dn) {
  431. if (of_node_to_eeh_dev(dn)) {
  432. struct pci_dev *dev = of_node_to_eeh_dev(dn)->pdev;
  433. if (dev && dev->driver)
  434. *freset |= dev->needs_freset;
  435. __eeh_set_pe_freset(dn, freset);
  436. }
  437. }
  438. }
  439. /**
  440. * eeh_set_pe_freset - Check the required reset for the indicated device and its children
  441. * @dn: parent device
  442. * @freset: return value
  443. *
  444. * Each device might have its preferred reset type: fundamental or
  445. * hot reset. The routine is used to collected the information for
  446. * the indicated device and its children so that the bunch of the
  447. * devices could be reset properly.
  448. */
  449. void eeh_set_pe_freset(struct device_node *dn, unsigned int *freset)
  450. {
  451. struct pci_dev *dev;
  452. dn = eeh_find_device_pe(dn);
  453. /* Back up one, since config addrs might be shared */
  454. if (!pcibios_find_pci_bus(dn) && of_node_to_eeh_dev(dn->parent))
  455. dn = dn->parent;
  456. dev = of_node_to_eeh_dev(dn)->pdev;
  457. if (dev)
  458. *freset |= dev->needs_freset;
  459. __eeh_set_pe_freset(dn, freset);
  460. }
  461. /**
  462. * eeh_reset_pe_once - Assert the pci #RST line for 1/4 second
  463. * @edev: pci device node to be reset.
  464. *
  465. * Assert the PCI #RST line for 1/4 second.
  466. */
  467. static void eeh_reset_pe_once(struct eeh_dev *edev)
  468. {
  469. unsigned int freset = 0;
  470. struct device_node *dn = eeh_dev_to_of_node(edev);
  471. /* Determine type of EEH reset required for
  472. * Partitionable Endpoint, a hot-reset (1)
  473. * or a fundamental reset (3).
  474. * A fundamental reset required by any device under
  475. * Partitionable Endpoint trumps hot-reset.
  476. */
  477. eeh_set_pe_freset(dn, &freset);
  478. if (freset)
  479. eeh_ops->reset(dn, EEH_RESET_FUNDAMENTAL);
  480. else
  481. eeh_ops->reset(dn, EEH_RESET_HOT);
  482. /* The PCI bus requires that the reset be held high for at least
  483. * a 100 milliseconds. We wait a bit longer 'just in case'.
  484. */
  485. #define PCI_BUS_RST_HOLD_TIME_MSEC 250
  486. msleep(PCI_BUS_RST_HOLD_TIME_MSEC);
  487. /* We might get hit with another EEH freeze as soon as the
  488. * pci slot reset line is dropped. Make sure we don't miss
  489. * these, and clear the flag now.
  490. */
  491. eeh_clear_slot(dn, EEH_MODE_ISOLATED);
  492. eeh_ops->reset(dn, EEH_RESET_DEACTIVATE);
  493. /* After a PCI slot has been reset, the PCI Express spec requires
  494. * a 1.5 second idle time for the bus to stabilize, before starting
  495. * up traffic.
  496. */
  497. #define PCI_BUS_SETTLE_TIME_MSEC 1800
  498. msleep(PCI_BUS_SETTLE_TIME_MSEC);
  499. }
  500. /**
  501. * eeh_reset_pe - Reset the indicated PE
  502. * @edev: PCI device associated EEH device
  503. *
  504. * This routine should be called to reset indicated device, including
  505. * PE. A PE might include multiple PCI devices and sometimes PCI bridges
  506. * might be involved as well.
  507. */
  508. int eeh_reset_pe(struct eeh_dev *edev)
  509. {
  510. int i, rc;
  511. struct device_node *dn = eeh_dev_to_of_node(edev);
  512. /* Take three shots at resetting the bus */
  513. for (i=0; i<3; i++) {
  514. eeh_reset_pe_once(edev);
  515. rc = eeh_ops->wait_state(dn, PCI_BUS_RESET_WAIT_MSEC);
  516. if (rc == (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE))
  517. return 0;
  518. if (rc < 0) {
  519. printk(KERN_ERR "EEH: unrecoverable slot failure %s\n",
  520. dn->full_name);
  521. return -1;
  522. }
  523. printk(KERN_ERR "EEH: bus reset %d failed on slot %s, rc=%d\n",
  524. i+1, dn->full_name, rc);
  525. }
  526. return -1;
  527. }
  528. /** Save and restore of PCI BARs
  529. *
  530. * Although firmware will set up BARs during boot, it doesn't
  531. * set up device BAR's after a device reset, although it will,
  532. * if requested, set up bridge configuration. Thus, we need to
  533. * configure the PCI devices ourselves.
  534. */
  535. /**
  536. * eeh_restore_one_device_bars - Restore the Base Address Registers for one device
  537. * @edev: PCI device associated EEH device
  538. *
  539. * Loads the PCI configuration space base address registers,
  540. * the expansion ROM base address, the latency timer, and etc.
  541. * from the saved values in the device node.
  542. */
  543. static inline void eeh_restore_one_device_bars(struct eeh_dev *edev)
  544. {
  545. int i;
  546. u32 cmd;
  547. struct device_node *dn = eeh_dev_to_of_node(edev);
  548. if (!edev->phb)
  549. return;
  550. for (i=4; i<10; i++) {
  551. eeh_ops->write_config(dn, i*4, 4, edev->config_space[i]);
  552. }
  553. /* 12 == Expansion ROM Address */
  554. eeh_ops->write_config(dn, 12*4, 4, edev->config_space[12]);
  555. #define BYTE_SWAP(OFF) (8*((OFF)/4)+3-(OFF))
  556. #define SAVED_BYTE(OFF) (((u8 *)(edev->config_space))[BYTE_SWAP(OFF)])
  557. eeh_ops->write_config(dn, PCI_CACHE_LINE_SIZE, 1,
  558. SAVED_BYTE(PCI_CACHE_LINE_SIZE));
  559. eeh_ops->write_config(dn, PCI_LATENCY_TIMER, 1,
  560. SAVED_BYTE(PCI_LATENCY_TIMER));
  561. /* max latency, min grant, interrupt pin and line */
  562. eeh_ops->write_config(dn, 15*4, 4, edev->config_space[15]);
  563. /* Restore PERR & SERR bits, some devices require it,
  564. * don't touch the other command bits
  565. */
  566. eeh_ops->read_config(dn, PCI_COMMAND, 4, &cmd);
  567. if (edev->config_space[1] & PCI_COMMAND_PARITY)
  568. cmd |= PCI_COMMAND_PARITY;
  569. else
  570. cmd &= ~PCI_COMMAND_PARITY;
  571. if (edev->config_space[1] & PCI_COMMAND_SERR)
  572. cmd |= PCI_COMMAND_SERR;
  573. else
  574. cmd &= ~PCI_COMMAND_SERR;
  575. eeh_ops->write_config(dn, PCI_COMMAND, 4, cmd);
  576. }
  577. /**
  578. * eeh_restore_bars - Restore the PCI config space info
  579. * @edev: EEH device
  580. *
  581. * This routine performs a recursive walk to the children
  582. * of this device as well.
  583. */
  584. void eeh_restore_bars(struct eeh_dev *edev)
  585. {
  586. struct device_node *dn;
  587. if (!edev)
  588. return;
  589. if ((edev->mode & EEH_MODE_SUPPORTED) && !IS_BRIDGE(edev->class_code))
  590. eeh_restore_one_device_bars(edev);
  591. for_each_child_of_node(eeh_dev_to_of_node(edev), dn)
  592. eeh_restore_bars(of_node_to_eeh_dev(dn));
  593. }
  594. /**
  595. * eeh_save_bars - Save device bars
  596. * @edev: PCI device associated EEH device
  597. *
  598. * Save the values of the device bars. Unlike the restore
  599. * routine, this routine is *not* recursive. This is because
  600. * PCI devices are added individually; but, for the restore,
  601. * an entire slot is reset at a time.
  602. */
  603. static void eeh_save_bars(struct eeh_dev *edev)
  604. {
  605. int i;
  606. struct device_node *dn;
  607. if (!edev)
  608. return;
  609. dn = eeh_dev_to_of_node(edev);
  610. for (i = 0; i < 16; i++)
  611. eeh_ops->read_config(dn, i * 4, 4, &edev->config_space[i]);
  612. }
  613. /**
  614. * eeh_early_enable - Early enable EEH on the indicated device
  615. * @dn: device node
  616. * @data: BUID
  617. *
  618. * Enable EEH functionality on the specified PCI device. The function
  619. * is expected to be called before real PCI probing is done. However,
  620. * the PHBs have been initialized at this point.
  621. */
  622. static void *eeh_early_enable(struct device_node *dn, void *data)
  623. {
  624. int ret;
  625. const u32 *class_code = of_get_property(dn, "class-code", NULL);
  626. const u32 *vendor_id = of_get_property(dn, "vendor-id", NULL);
  627. const u32 *device_id = of_get_property(dn, "device-id", NULL);
  628. const u32 *regs;
  629. int enable;
  630. struct eeh_dev *edev = of_node_to_eeh_dev(dn);
  631. edev->class_code = 0;
  632. edev->mode = 0;
  633. edev->check_count = 0;
  634. edev->freeze_count = 0;
  635. edev->false_positives = 0;
  636. if (!of_device_is_available(dn))
  637. return NULL;
  638. /* Ignore bad nodes. */
  639. if (!class_code || !vendor_id || !device_id)
  640. return NULL;
  641. /* There is nothing to check on PCI to ISA bridges */
  642. if (dn->type && !strcmp(dn->type, "isa")) {
  643. edev->mode |= EEH_MODE_NOCHECK;
  644. return NULL;
  645. }
  646. edev->class_code = *class_code;
  647. /* Ok... see if this device supports EEH. Some do, some don't,
  648. * and the only way to find out is to check each and every one.
  649. */
  650. regs = of_get_property(dn, "reg", NULL);
  651. if (regs) {
  652. /* First register entry is addr (00BBSS00) */
  653. /* Try to enable eeh */
  654. ret = eeh_ops->set_option(dn, EEH_OPT_ENABLE);
  655. enable = 0;
  656. if (ret == 0) {
  657. edev->config_addr = regs[0];
  658. /* If the newer, better, ibm,get-config-addr-info is supported,
  659. * then use that instead.
  660. */
  661. edev->pe_config_addr = eeh_ops->get_pe_addr(dn);
  662. /* Some older systems (Power4) allow the
  663. * ibm,set-eeh-option call to succeed even on nodes
  664. * where EEH is not supported. Verify support
  665. * explicitly.
  666. */
  667. ret = eeh_ops->get_state(dn, NULL);
  668. if (ret > 0 && ret != EEH_STATE_NOT_SUPPORT)
  669. enable = 1;
  670. }
  671. if (enable) {
  672. eeh_subsystem_enabled = 1;
  673. edev->mode |= EEH_MODE_SUPPORTED;
  674. eeh_add_to_parent_pe(edev);
  675. pr_debug("EEH: %s: eeh enabled, config=%x pe_config=%x\n",
  676. dn->full_name, edev->config_addr,
  677. edev->pe_config_addr);
  678. } else {
  679. /* This device doesn't support EEH, but it may have an
  680. * EEH parent, in which case we mark it as supported.
  681. */
  682. if (dn->parent && of_node_to_eeh_dev(dn->parent) &&
  683. (of_node_to_eeh_dev(dn->parent)->mode & EEH_MODE_SUPPORTED)) {
  684. /* Parent supports EEH. */
  685. edev->mode |= EEH_MODE_SUPPORTED;
  686. edev->config_addr = of_node_to_eeh_dev(dn->parent)->config_addr;
  687. edev->pe_config_addr = of_node_to_eeh_dev(dn->parent)->pe_config_addr;
  688. eeh_add_to_parent_pe(edev);
  689. return NULL;
  690. }
  691. }
  692. } else {
  693. printk(KERN_WARNING "EEH: %s: unable to get reg property.\n",
  694. dn->full_name);
  695. }
  696. eeh_save_bars(edev);
  697. return NULL;
  698. }
  699. /**
  700. * eeh_ops_register - Register platform dependent EEH operations
  701. * @ops: platform dependent EEH operations
  702. *
  703. * Register the platform dependent EEH operation callback
  704. * functions. The platform should call this function before
  705. * any other EEH operations.
  706. */
  707. int __init eeh_ops_register(struct eeh_ops *ops)
  708. {
  709. if (!ops->name) {
  710. pr_warning("%s: Invalid EEH ops name for %p\n",
  711. __func__, ops);
  712. return -EINVAL;
  713. }
  714. if (eeh_ops && eeh_ops != ops) {
  715. pr_warning("%s: EEH ops of platform %s already existing (%s)\n",
  716. __func__, eeh_ops->name, ops->name);
  717. return -EEXIST;
  718. }
  719. eeh_ops = ops;
  720. return 0;
  721. }
  722. /**
  723. * eeh_ops_unregister - Unreigster platform dependent EEH operations
  724. * @name: name of EEH platform operations
  725. *
  726. * Unregister the platform dependent EEH operation callback
  727. * functions.
  728. */
  729. int __exit eeh_ops_unregister(const char *name)
  730. {
  731. if (!name || !strlen(name)) {
  732. pr_warning("%s: Invalid EEH ops name\n",
  733. __func__);
  734. return -EINVAL;
  735. }
  736. if (eeh_ops && !strcmp(eeh_ops->name, name)) {
  737. eeh_ops = NULL;
  738. return 0;
  739. }
  740. return -EEXIST;
  741. }
  742. /**
  743. * eeh_init - EEH initialization
  744. *
  745. * Initialize EEH by trying to enable it for all of the adapters in the system.
  746. * As a side effect we can determine here if eeh is supported at all.
  747. * Note that we leave EEH on so failed config cycles won't cause a machine
  748. * check. If a user turns off EEH for a particular adapter they are really
  749. * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
  750. * grant access to a slot if EEH isn't enabled, and so we always enable
  751. * EEH for all slots/all devices.
  752. *
  753. * The eeh-force-off option disables EEH checking globally, for all slots.
  754. * Even if force-off is set, the EEH hardware is still enabled, so that
  755. * newer systems can boot.
  756. */
  757. static int __init eeh_init(void)
  758. {
  759. struct pci_controller *hose, *tmp;
  760. struct device_node *phb;
  761. int ret;
  762. /* call platform initialization function */
  763. if (!eeh_ops) {
  764. pr_warning("%s: Platform EEH operation not found\n",
  765. __func__);
  766. return -EEXIST;
  767. } else if ((ret = eeh_ops->init())) {
  768. pr_warning("%s: Failed to call platform init function (%d)\n",
  769. __func__, ret);
  770. return ret;
  771. }
  772. raw_spin_lock_init(&confirm_error_lock);
  773. /* Enable EEH for all adapters */
  774. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  775. phb = hose->dn;
  776. traverse_pci_devices(phb, eeh_early_enable, NULL);
  777. }
  778. if (eeh_subsystem_enabled)
  779. printk(KERN_INFO "EEH: PCI Enhanced I/O Error Handling Enabled\n");
  780. else
  781. printk(KERN_WARNING "EEH: No capable adapters found\n");
  782. return ret;
  783. }
  784. core_initcall_sync(eeh_init);
  785. /**
  786. * eeh_add_device_early - Enable EEH for the indicated device_node
  787. * @dn: device node for which to set up EEH
  788. *
  789. * This routine must be used to perform EEH initialization for PCI
  790. * devices that were added after system boot (e.g. hotplug, dlpar).
  791. * This routine must be called before any i/o is performed to the
  792. * adapter (inluding any config-space i/o).
  793. * Whether this actually enables EEH or not for this device depends
  794. * on the CEC architecture, type of the device, on earlier boot
  795. * command-line arguments & etc.
  796. */
  797. static void eeh_add_device_early(struct device_node *dn)
  798. {
  799. struct pci_controller *phb;
  800. if (!dn || !of_node_to_eeh_dev(dn))
  801. return;
  802. phb = of_node_to_eeh_dev(dn)->phb;
  803. /* USB Bus children of PCI devices will not have BUID's */
  804. if (NULL == phb || 0 == phb->buid)
  805. return;
  806. eeh_early_enable(dn, NULL);
  807. }
  808. /**
  809. * eeh_add_device_tree_early - Enable EEH for the indicated device
  810. * @dn: device node
  811. *
  812. * This routine must be used to perform EEH initialization for the
  813. * indicated PCI device that was added after system boot (e.g.
  814. * hotplug, dlpar).
  815. */
  816. void eeh_add_device_tree_early(struct device_node *dn)
  817. {
  818. struct device_node *sib;
  819. for_each_child_of_node(dn, sib)
  820. eeh_add_device_tree_early(sib);
  821. eeh_add_device_early(dn);
  822. }
  823. EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
  824. /**
  825. * eeh_add_device_late - Perform EEH initialization for the indicated pci device
  826. * @dev: pci device for which to set up EEH
  827. *
  828. * This routine must be used to complete EEH initialization for PCI
  829. * devices that were added after system boot (e.g. hotplug, dlpar).
  830. */
  831. static void eeh_add_device_late(struct pci_dev *dev)
  832. {
  833. struct device_node *dn;
  834. struct eeh_dev *edev;
  835. if (!dev || !eeh_subsystem_enabled)
  836. return;
  837. pr_debug("EEH: Adding device %s\n", pci_name(dev));
  838. dn = pci_device_to_OF_node(dev);
  839. edev = of_node_to_eeh_dev(dn);
  840. if (edev->pdev == dev) {
  841. pr_debug("EEH: Already referenced !\n");
  842. return;
  843. }
  844. WARN_ON(edev->pdev);
  845. pci_dev_get(dev);
  846. edev->pdev = dev;
  847. dev->dev.archdata.edev = edev;
  848. pci_addr_cache_insert_device(dev);
  849. eeh_sysfs_add_device(dev);
  850. }
  851. /**
  852. * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
  853. * @bus: PCI bus
  854. *
  855. * This routine must be used to perform EEH initialization for PCI
  856. * devices which are attached to the indicated PCI bus. The PCI bus
  857. * is added after system boot through hotplug or dlpar.
  858. */
  859. void eeh_add_device_tree_late(struct pci_bus *bus)
  860. {
  861. struct pci_dev *dev;
  862. list_for_each_entry(dev, &bus->devices, bus_list) {
  863. eeh_add_device_late(dev);
  864. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  865. struct pci_bus *subbus = dev->subordinate;
  866. if (subbus)
  867. eeh_add_device_tree_late(subbus);
  868. }
  869. }
  870. }
  871. EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
  872. /**
  873. * eeh_remove_device - Undo EEH setup for the indicated pci device
  874. * @dev: pci device to be removed
  875. *
  876. * This routine should be called when a device is removed from
  877. * a running system (e.g. by hotplug or dlpar). It unregisters
  878. * the PCI device from the EEH subsystem. I/O errors affecting
  879. * this device will no longer be detected after this call; thus,
  880. * i/o errors affecting this slot may leave this device unusable.
  881. */
  882. static void eeh_remove_device(struct pci_dev *dev)
  883. {
  884. struct eeh_dev *edev;
  885. if (!dev || !eeh_subsystem_enabled)
  886. return;
  887. edev = pci_dev_to_eeh_dev(dev);
  888. /* Unregister the device with the EEH/PCI address search system */
  889. pr_debug("EEH: Removing device %s\n", pci_name(dev));
  890. if (!edev || !edev->pdev) {
  891. pr_debug("EEH: Not referenced !\n");
  892. return;
  893. }
  894. edev->pdev = NULL;
  895. dev->dev.archdata.edev = NULL;
  896. pci_dev_put(dev);
  897. eeh_rmv_from_parent_pe(edev);
  898. pci_addr_cache_remove_device(dev);
  899. eeh_sysfs_remove_device(dev);
  900. }
  901. /**
  902. * eeh_remove_bus_device - Undo EEH setup for the indicated PCI device
  903. * @dev: PCI device
  904. *
  905. * This routine must be called when a device is removed from the
  906. * running system through hotplug or dlpar. The corresponding
  907. * PCI address cache will be removed.
  908. */
  909. void eeh_remove_bus_device(struct pci_dev *dev)
  910. {
  911. struct pci_bus *bus = dev->subordinate;
  912. struct pci_dev *child, *tmp;
  913. eeh_remove_device(dev);
  914. if (bus && dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  915. list_for_each_entry_safe(child, tmp, &bus->devices, bus_list)
  916. eeh_remove_bus_device(child);
  917. }
  918. }
  919. EXPORT_SYMBOL_GPL(eeh_remove_bus_device);
  920. static int proc_eeh_show(struct seq_file *m, void *v)
  921. {
  922. if (0 == eeh_subsystem_enabled) {
  923. seq_printf(m, "EEH Subsystem is globally disabled\n");
  924. seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
  925. } else {
  926. seq_printf(m, "EEH Subsystem is enabled\n");
  927. seq_printf(m,
  928. "no device=%llu\n"
  929. "no device node=%llu\n"
  930. "no config address=%llu\n"
  931. "check not wanted=%llu\n"
  932. "eeh_total_mmio_ffs=%llu\n"
  933. "eeh_false_positives=%llu\n"
  934. "eeh_slot_resets=%llu\n",
  935. eeh_stats.no_device,
  936. eeh_stats.no_dn,
  937. eeh_stats.no_cfg_addr,
  938. eeh_stats.ignored_check,
  939. eeh_stats.total_mmio_ffs,
  940. eeh_stats.false_positives,
  941. eeh_stats.slot_resets);
  942. }
  943. return 0;
  944. }
  945. static int proc_eeh_open(struct inode *inode, struct file *file)
  946. {
  947. return single_open(file, proc_eeh_show, NULL);
  948. }
  949. static const struct file_operations proc_eeh_operations = {
  950. .open = proc_eeh_open,
  951. .read = seq_read,
  952. .llseek = seq_lseek,
  953. .release = single_release,
  954. };
  955. static int __init eeh_init_proc(void)
  956. {
  957. if (machine_is(pseries))
  958. proc_create("powerpc/eeh", 0, NULL, &proc_eeh_operations);
  959. return 0;
  960. }
  961. __initcall(eeh_init_proc);