eeh.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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. 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. * On PowerNV platform, we might already have fenced PHB there.
  237. * For that case, it's meaningless to recover frozen PE. Intead,
  238. * We have to handle fenced PHB firstly.
  239. */
  240. static int eeh_phb_check_failure(struct eeh_pe *pe)
  241. {
  242. struct eeh_pe *phb_pe;
  243. unsigned long flags;
  244. int ret;
  245. if (!eeh_probe_mode_dev())
  246. return -EPERM;
  247. /* Find the PHB PE */
  248. phb_pe = eeh_phb_pe_get(pe->phb);
  249. if (!phb_pe) {
  250. pr_warning("%s Can't find PE for PHB#%d\n",
  251. __func__, pe->phb->global_number);
  252. return -EEXIST;
  253. }
  254. /* If the PHB has been in problematic state */
  255. eeh_serialize_lock(&flags);
  256. if (phb_pe->state & (EEH_PE_ISOLATED | EEH_PE_PHB_DEAD)) {
  257. ret = 0;
  258. goto out;
  259. }
  260. /* Check PHB state */
  261. ret = eeh_ops->get_state(phb_pe, NULL);
  262. if ((ret < 0) ||
  263. (ret == EEH_STATE_NOT_SUPPORT) ||
  264. (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
  265. (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
  266. ret = 0;
  267. goto out;
  268. }
  269. /* Isolate the PHB and send event */
  270. eeh_pe_state_mark(phb_pe, EEH_PE_ISOLATED);
  271. eeh_serialize_unlock(flags);
  272. eeh_send_failure_event(phb_pe);
  273. WARN(1, "EEH: PHB failure detected\n");
  274. return 1;
  275. out:
  276. eeh_serialize_unlock(flags);
  277. return ret;
  278. }
  279. /**
  280. * eeh_dev_check_failure - Check if all 1's data is due to EEH slot freeze
  281. * @edev: eeh device
  282. *
  283. * Check for an EEH failure for the given device node. Call this
  284. * routine if the result of a read was all 0xff's and you want to
  285. * find out if this is due to an EEH slot freeze. This routine
  286. * will query firmware for the EEH status.
  287. *
  288. * Returns 0 if there has not been an EEH error; otherwise returns
  289. * a non-zero value and queues up a slot isolation event notification.
  290. *
  291. * It is safe to call this routine in an interrupt context.
  292. */
  293. int eeh_dev_check_failure(struct eeh_dev *edev)
  294. {
  295. int ret;
  296. unsigned long flags;
  297. struct device_node *dn;
  298. struct pci_dev *dev;
  299. struct eeh_pe *pe;
  300. int rc = 0;
  301. const char *location;
  302. eeh_stats.total_mmio_ffs++;
  303. if (!eeh_subsystem_enabled)
  304. return 0;
  305. if (!edev) {
  306. eeh_stats.no_dn++;
  307. return 0;
  308. }
  309. dn = eeh_dev_to_of_node(edev);
  310. dev = eeh_dev_to_pci_dev(edev);
  311. pe = edev->pe;
  312. /* Access to IO BARs might get this far and still not want checking. */
  313. if (!pe) {
  314. eeh_stats.ignored_check++;
  315. pr_debug("EEH: Ignored check for %s %s\n",
  316. eeh_pci_name(dev), dn->full_name);
  317. return 0;
  318. }
  319. if (!pe->addr && !pe->config_addr) {
  320. eeh_stats.no_cfg_addr++;
  321. return 0;
  322. }
  323. /*
  324. * On PowerNV platform, we might already have fenced PHB
  325. * there and we need take care of that firstly.
  326. */
  327. ret = eeh_phb_check_failure(pe);
  328. if (ret > 0)
  329. return ret;
  330. /* If we already have a pending isolation event for this
  331. * slot, we know it's bad already, we don't need to check.
  332. * Do this checking under a lock; as multiple PCI devices
  333. * in one slot might report errors simultaneously, and we
  334. * only want one error recovery routine running.
  335. */
  336. eeh_serialize_lock(&flags);
  337. rc = 1;
  338. if (pe->state & EEH_PE_ISOLATED) {
  339. pe->check_count++;
  340. if (pe->check_count % EEH_MAX_FAILS == 0) {
  341. location = of_get_property(dn, "ibm,loc-code", NULL);
  342. printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
  343. "location=%s driver=%s pci addr=%s\n",
  344. pe->check_count, location,
  345. eeh_driver_name(dev), eeh_pci_name(dev));
  346. printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
  347. eeh_driver_name(dev));
  348. dump_stack();
  349. }
  350. goto dn_unlock;
  351. }
  352. /*
  353. * Now test for an EEH failure. This is VERY expensive.
  354. * Note that the eeh_config_addr may be a parent device
  355. * in the case of a device behind a bridge, or it may be
  356. * function zero of a multi-function device.
  357. * In any case they must share a common PHB.
  358. */
  359. ret = eeh_ops->get_state(pe, NULL);
  360. /* Note that config-io to empty slots may fail;
  361. * they are empty when they don't have children.
  362. * We will punt with the following conditions: Failure to get
  363. * PE's state, EEH not support and Permanently unavailable
  364. * state, PE is in good state.
  365. */
  366. if ((ret < 0) ||
  367. (ret == EEH_STATE_NOT_SUPPORT) ||
  368. (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
  369. (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
  370. eeh_stats.false_positives++;
  371. pe->false_positives++;
  372. rc = 0;
  373. goto dn_unlock;
  374. }
  375. eeh_stats.slot_resets++;
  376. /* Avoid repeated reports of this failure, including problems
  377. * with other functions on this device, and functions under
  378. * bridges.
  379. */
  380. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  381. eeh_serialize_unlock(flags);
  382. eeh_send_failure_event(pe);
  383. /* Most EEH events are due to device driver bugs. Having
  384. * a stack trace will help the device-driver authors figure
  385. * out what happened. So print that out.
  386. */
  387. WARN(1, "EEH: failure detected\n");
  388. return 1;
  389. dn_unlock:
  390. eeh_serialize_unlock(flags);
  391. return rc;
  392. }
  393. EXPORT_SYMBOL_GPL(eeh_dev_check_failure);
  394. /**
  395. * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
  396. * @token: I/O token, should be address in the form 0xA....
  397. * @val: value, should be all 1's (XXX why do we need this arg??)
  398. *
  399. * Check for an EEH failure at the given token address. Call this
  400. * routine if the result of a read was all 0xff's and you want to
  401. * find out if this is due to an EEH slot freeze event. This routine
  402. * will query firmware for the EEH status.
  403. *
  404. * Note this routine is safe to call in an interrupt context.
  405. */
  406. unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
  407. {
  408. unsigned long addr;
  409. struct eeh_dev *edev;
  410. /* Finding the phys addr + pci device; this is pretty quick. */
  411. addr = eeh_token_to_phys((unsigned long __force) token);
  412. edev = eeh_addr_cache_get_dev(addr);
  413. if (!edev) {
  414. eeh_stats.no_device++;
  415. return val;
  416. }
  417. eeh_dev_check_failure(edev);
  418. pci_dev_put(eeh_dev_to_pci_dev(edev));
  419. return val;
  420. }
  421. EXPORT_SYMBOL(eeh_check_failure);
  422. /**
  423. * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
  424. * @pe: EEH PE
  425. *
  426. * This routine should be called to reenable frozen MMIO or DMA
  427. * so that it would work correctly again. It's useful while doing
  428. * recovery or log collection on the indicated device.
  429. */
  430. int eeh_pci_enable(struct eeh_pe *pe, int function)
  431. {
  432. int rc;
  433. rc = eeh_ops->set_option(pe, function);
  434. if (rc)
  435. pr_warning("%s: Unexpected state change %d on PHB#%d-PE#%x, err=%d\n",
  436. __func__, function, pe->phb->global_number, pe->addr, rc);
  437. rc = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
  438. if (rc > 0 && (rc & EEH_STATE_MMIO_ENABLED) &&
  439. (function == EEH_OPT_THAW_MMIO))
  440. return 0;
  441. return rc;
  442. }
  443. /**
  444. * pcibios_set_pcie_slot_reset - Set PCI-E reset state
  445. * @dev: pci device struct
  446. * @state: reset state to enter
  447. *
  448. * Return value:
  449. * 0 if success
  450. */
  451. int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  452. {
  453. struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
  454. struct eeh_pe *pe = edev->pe;
  455. if (!pe) {
  456. pr_err("%s: No PE found on PCI device %s\n",
  457. __func__, pci_name(dev));
  458. return -EINVAL;
  459. }
  460. switch (state) {
  461. case pcie_deassert_reset:
  462. eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
  463. break;
  464. case pcie_hot_reset:
  465. eeh_ops->reset(pe, EEH_RESET_HOT);
  466. break;
  467. case pcie_warm_reset:
  468. eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
  469. break;
  470. default:
  471. return -EINVAL;
  472. };
  473. return 0;
  474. }
  475. /**
  476. * eeh_set_pe_freset - Check the required reset for the indicated device
  477. * @data: EEH device
  478. * @flag: return value
  479. *
  480. * Each device might have its preferred reset type: fundamental or
  481. * hot reset. The routine is used to collected the information for
  482. * the indicated device and its children so that the bunch of the
  483. * devices could be reset properly.
  484. */
  485. static void *eeh_set_dev_freset(void *data, void *flag)
  486. {
  487. struct pci_dev *dev;
  488. unsigned int *freset = (unsigned int *)flag;
  489. struct eeh_dev *edev = (struct eeh_dev *)data;
  490. dev = eeh_dev_to_pci_dev(edev);
  491. if (dev)
  492. *freset |= dev->needs_freset;
  493. return NULL;
  494. }
  495. /**
  496. * eeh_reset_pe_once - Assert the pci #RST line for 1/4 second
  497. * @pe: EEH PE
  498. *
  499. * Assert the PCI #RST line for 1/4 second.
  500. */
  501. static void eeh_reset_pe_once(struct eeh_pe *pe)
  502. {
  503. unsigned int freset = 0;
  504. /* Determine type of EEH reset required for
  505. * Partitionable Endpoint, a hot-reset (1)
  506. * or a fundamental reset (3).
  507. * A fundamental reset required by any device under
  508. * Partitionable Endpoint trumps hot-reset.
  509. */
  510. eeh_pe_dev_traverse(pe, eeh_set_dev_freset, &freset);
  511. if (freset)
  512. eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
  513. else
  514. eeh_ops->reset(pe, EEH_RESET_HOT);
  515. /* The PCI bus requires that the reset be held high for at least
  516. * a 100 milliseconds. We wait a bit longer 'just in case'.
  517. */
  518. #define PCI_BUS_RST_HOLD_TIME_MSEC 250
  519. msleep(PCI_BUS_RST_HOLD_TIME_MSEC);
  520. /* We might get hit with another EEH freeze as soon as the
  521. * pci slot reset line is dropped. Make sure we don't miss
  522. * these, and clear the flag now.
  523. */
  524. eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
  525. eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
  526. /* After a PCI slot has been reset, the PCI Express spec requires
  527. * a 1.5 second idle time for the bus to stabilize, before starting
  528. * up traffic.
  529. */
  530. #define PCI_BUS_SETTLE_TIME_MSEC 1800
  531. msleep(PCI_BUS_SETTLE_TIME_MSEC);
  532. }
  533. /**
  534. * eeh_reset_pe - Reset the indicated PE
  535. * @pe: EEH PE
  536. *
  537. * This routine should be called to reset indicated device, including
  538. * PE. A PE might include multiple PCI devices and sometimes PCI bridges
  539. * might be involved as well.
  540. */
  541. int eeh_reset_pe(struct eeh_pe *pe)
  542. {
  543. int flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
  544. int i, rc;
  545. /* Take three shots at resetting the bus */
  546. for (i=0; i<3; i++) {
  547. eeh_reset_pe_once(pe);
  548. rc = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
  549. if ((rc & flags) == flags)
  550. return 0;
  551. if (rc < 0) {
  552. pr_err("%s: Unrecoverable slot failure on PHB#%d-PE#%x",
  553. __func__, pe->phb->global_number, pe->addr);
  554. return -1;
  555. }
  556. pr_err("EEH: bus reset %d failed on PHB#%d-PE#%x, rc=%d\n",
  557. i+1, pe->phb->global_number, pe->addr, rc);
  558. }
  559. return -1;
  560. }
  561. /**
  562. * eeh_save_bars - Save device bars
  563. * @edev: PCI device associated EEH device
  564. *
  565. * Save the values of the device bars. Unlike the restore
  566. * routine, this routine is *not* recursive. This is because
  567. * PCI devices are added individually; but, for the restore,
  568. * an entire slot is reset at a time.
  569. */
  570. void eeh_save_bars(struct eeh_dev *edev)
  571. {
  572. int i;
  573. struct device_node *dn;
  574. if (!edev)
  575. return;
  576. dn = eeh_dev_to_of_node(edev);
  577. for (i = 0; i < 16; i++)
  578. eeh_ops->read_config(dn, i * 4, 4, &edev->config_space[i]);
  579. }
  580. /**
  581. * eeh_ops_register - Register platform dependent EEH operations
  582. * @ops: platform dependent EEH operations
  583. *
  584. * Register the platform dependent EEH operation callback
  585. * functions. The platform should call this function before
  586. * any other EEH operations.
  587. */
  588. int __init eeh_ops_register(struct eeh_ops *ops)
  589. {
  590. if (!ops->name) {
  591. pr_warning("%s: Invalid EEH ops name for %p\n",
  592. __func__, ops);
  593. return -EINVAL;
  594. }
  595. if (eeh_ops && eeh_ops != ops) {
  596. pr_warning("%s: EEH ops of platform %s already existing (%s)\n",
  597. __func__, eeh_ops->name, ops->name);
  598. return -EEXIST;
  599. }
  600. eeh_ops = ops;
  601. return 0;
  602. }
  603. /**
  604. * eeh_ops_unregister - Unreigster platform dependent EEH operations
  605. * @name: name of EEH platform operations
  606. *
  607. * Unregister the platform dependent EEH operation callback
  608. * functions.
  609. */
  610. int __exit eeh_ops_unregister(const char *name)
  611. {
  612. if (!name || !strlen(name)) {
  613. pr_warning("%s: Invalid EEH ops name\n",
  614. __func__);
  615. return -EINVAL;
  616. }
  617. if (eeh_ops && !strcmp(eeh_ops->name, name)) {
  618. eeh_ops = NULL;
  619. return 0;
  620. }
  621. return -EEXIST;
  622. }
  623. /**
  624. * eeh_init - EEH initialization
  625. *
  626. * Initialize EEH by trying to enable it for all of the adapters in the system.
  627. * As a side effect we can determine here if eeh is supported at all.
  628. * Note that we leave EEH on so failed config cycles won't cause a machine
  629. * check. If a user turns off EEH for a particular adapter they are really
  630. * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
  631. * grant access to a slot if EEH isn't enabled, and so we always enable
  632. * EEH for all slots/all devices.
  633. *
  634. * The eeh-force-off option disables EEH checking globally, for all slots.
  635. * Even if force-off is set, the EEH hardware is still enabled, so that
  636. * newer systems can boot.
  637. */
  638. int __init eeh_init(void)
  639. {
  640. struct pci_controller *hose, *tmp;
  641. struct device_node *phb;
  642. static int cnt = 0;
  643. int ret = 0;
  644. /*
  645. * We have to delay the initialization on PowerNV after
  646. * the PCI hierarchy tree has been built because the PEs
  647. * are figured out based on PCI devices instead of device
  648. * tree nodes
  649. */
  650. if (machine_is(powernv) && cnt++ <= 0)
  651. return ret;
  652. /* call platform initialization function */
  653. if (!eeh_ops) {
  654. pr_warning("%s: Platform EEH operation not found\n",
  655. __func__);
  656. return -EEXIST;
  657. } else if ((ret = eeh_ops->init())) {
  658. pr_warning("%s: Failed to call platform init function (%d)\n",
  659. __func__, ret);
  660. return ret;
  661. }
  662. /* Initialize EEH event */
  663. ret = eeh_event_init();
  664. if (ret)
  665. return ret;
  666. /* Enable EEH for all adapters */
  667. if (eeh_probe_mode_devtree()) {
  668. list_for_each_entry_safe(hose, tmp,
  669. &hose_list, list_node) {
  670. phb = hose->dn;
  671. traverse_pci_devices(phb, eeh_ops->of_probe, NULL);
  672. }
  673. } else if (eeh_probe_mode_dev()) {
  674. list_for_each_entry_safe(hose, tmp,
  675. &hose_list, list_node)
  676. pci_walk_bus(hose->bus, eeh_ops->dev_probe, NULL);
  677. } else {
  678. pr_warning("%s: Invalid probe mode %d\n",
  679. __func__, eeh_probe_mode);
  680. return -EINVAL;
  681. }
  682. /*
  683. * Call platform post-initialization. Actually, It's good chance
  684. * to inform platform that EEH is ready to supply service if the
  685. * I/O cache stuff has been built up.
  686. */
  687. if (eeh_ops->post_init) {
  688. ret = eeh_ops->post_init();
  689. if (ret)
  690. return ret;
  691. }
  692. if (eeh_subsystem_enabled)
  693. pr_info("EEH: PCI Enhanced I/O Error Handling Enabled\n");
  694. else
  695. pr_warning("EEH: No capable adapters found\n");
  696. return ret;
  697. }
  698. core_initcall_sync(eeh_init);
  699. /**
  700. * eeh_add_device_early - Enable EEH for the indicated device_node
  701. * @dn: device node for which to set up EEH
  702. *
  703. * This routine must be used to perform EEH initialization for PCI
  704. * devices that were added after system boot (e.g. hotplug, dlpar).
  705. * This routine must be called before any i/o is performed to the
  706. * adapter (inluding any config-space i/o).
  707. * Whether this actually enables EEH or not for this device depends
  708. * on the CEC architecture, type of the device, on earlier boot
  709. * command-line arguments & etc.
  710. */
  711. static void eeh_add_device_early(struct device_node *dn)
  712. {
  713. struct pci_controller *phb;
  714. /*
  715. * If we're doing EEH probe based on PCI device, we
  716. * would delay the probe until late stage because
  717. * the PCI device isn't available this moment.
  718. */
  719. if (!eeh_probe_mode_devtree())
  720. return;
  721. if (!of_node_to_eeh_dev(dn))
  722. return;
  723. phb = of_node_to_eeh_dev(dn)->phb;
  724. /* USB Bus children of PCI devices will not have BUID's */
  725. if (NULL == phb || 0 == phb->buid)
  726. return;
  727. eeh_ops->of_probe(dn, NULL);
  728. }
  729. /**
  730. * eeh_add_device_tree_early - Enable EEH for the indicated device
  731. * @dn: device node
  732. *
  733. * This routine must be used to perform EEH initialization for the
  734. * indicated PCI device that was added after system boot (e.g.
  735. * hotplug, dlpar).
  736. */
  737. void eeh_add_device_tree_early(struct device_node *dn)
  738. {
  739. struct device_node *sib;
  740. for_each_child_of_node(dn, sib)
  741. eeh_add_device_tree_early(sib);
  742. eeh_add_device_early(dn);
  743. }
  744. EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
  745. /**
  746. * eeh_add_device_late - Perform EEH initialization for the indicated pci device
  747. * @dev: pci device for which to set up EEH
  748. *
  749. * This routine must be used to complete EEH initialization for PCI
  750. * devices that were added after system boot (e.g. hotplug, dlpar).
  751. */
  752. static void eeh_add_device_late(struct pci_dev *dev)
  753. {
  754. struct device_node *dn;
  755. struct eeh_dev *edev;
  756. if (!dev || !eeh_subsystem_enabled)
  757. return;
  758. pr_debug("EEH: Adding device %s\n", pci_name(dev));
  759. dn = pci_device_to_OF_node(dev);
  760. edev = of_node_to_eeh_dev(dn);
  761. if (edev->pdev == dev) {
  762. pr_debug("EEH: Already referenced !\n");
  763. return;
  764. }
  765. WARN_ON(edev->pdev);
  766. pci_dev_get(dev);
  767. edev->pdev = dev;
  768. dev->dev.archdata.edev = edev;
  769. /*
  770. * We have to do the EEH probe here because the PCI device
  771. * hasn't been created yet in the early stage.
  772. */
  773. if (eeh_probe_mode_dev())
  774. eeh_ops->dev_probe(dev, NULL);
  775. eeh_addr_cache_insert_dev(dev);
  776. }
  777. /**
  778. * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
  779. * @bus: PCI bus
  780. *
  781. * This routine must be used to perform EEH initialization for PCI
  782. * devices which are attached to the indicated PCI bus. The PCI bus
  783. * is added after system boot through hotplug or dlpar.
  784. */
  785. void eeh_add_device_tree_late(struct pci_bus *bus)
  786. {
  787. struct pci_dev *dev;
  788. list_for_each_entry(dev, &bus->devices, bus_list) {
  789. eeh_add_device_late(dev);
  790. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  791. struct pci_bus *subbus = dev->subordinate;
  792. if (subbus)
  793. eeh_add_device_tree_late(subbus);
  794. }
  795. }
  796. }
  797. EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
  798. /**
  799. * eeh_add_sysfs_files - Add EEH sysfs files for the indicated PCI bus
  800. * @bus: PCI bus
  801. *
  802. * This routine must be used to add EEH sysfs files for PCI
  803. * devices which are attached to the indicated PCI bus. The PCI bus
  804. * is added after system boot through hotplug or dlpar.
  805. */
  806. void eeh_add_sysfs_files(struct pci_bus *bus)
  807. {
  808. struct pci_dev *dev;
  809. list_for_each_entry(dev, &bus->devices, bus_list) {
  810. eeh_sysfs_add_device(dev);
  811. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  812. struct pci_bus *subbus = dev->subordinate;
  813. if (subbus)
  814. eeh_add_sysfs_files(subbus);
  815. }
  816. }
  817. }
  818. EXPORT_SYMBOL_GPL(eeh_add_sysfs_files);
  819. /**
  820. * eeh_remove_device - Undo EEH setup for the indicated pci device
  821. * @dev: pci device to be removed
  822. * @purge_pe: remove the PE or not
  823. *
  824. * This routine should be called when a device is removed from
  825. * a running system (e.g. by hotplug or dlpar). It unregisters
  826. * the PCI device from the EEH subsystem. I/O errors affecting
  827. * this device will no longer be detected after this call; thus,
  828. * i/o errors affecting this slot may leave this device unusable.
  829. */
  830. static void eeh_remove_device(struct pci_dev *dev, int purge_pe)
  831. {
  832. struct eeh_dev *edev;
  833. if (!dev || !eeh_subsystem_enabled)
  834. return;
  835. edev = pci_dev_to_eeh_dev(dev);
  836. /* Unregister the device with the EEH/PCI address search system */
  837. pr_debug("EEH: Removing device %s\n", pci_name(dev));
  838. if (!edev || !edev->pdev) {
  839. pr_debug("EEH: Not referenced !\n");
  840. return;
  841. }
  842. edev->pdev = NULL;
  843. dev->dev.archdata.edev = NULL;
  844. pci_dev_put(dev);
  845. eeh_rmv_from_parent_pe(edev, purge_pe);
  846. eeh_addr_cache_rmv_dev(dev);
  847. eeh_sysfs_remove_device(dev);
  848. }
  849. /**
  850. * eeh_remove_bus_device - Undo EEH setup for the indicated PCI device
  851. * @dev: PCI device
  852. * @purge_pe: remove the corresponding PE or not
  853. *
  854. * This routine must be called when a device is removed from the
  855. * running system through hotplug or dlpar. The corresponding
  856. * PCI address cache will be removed.
  857. */
  858. void eeh_remove_bus_device(struct pci_dev *dev, int purge_pe)
  859. {
  860. struct pci_bus *bus = dev->subordinate;
  861. struct pci_dev *child, *tmp;
  862. eeh_remove_device(dev, purge_pe);
  863. if (bus && dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  864. list_for_each_entry_safe(child, tmp, &bus->devices, bus_list)
  865. eeh_remove_bus_device(child, purge_pe);
  866. }
  867. }
  868. EXPORT_SYMBOL_GPL(eeh_remove_bus_device);
  869. static int proc_eeh_show(struct seq_file *m, void *v)
  870. {
  871. if (0 == eeh_subsystem_enabled) {
  872. seq_printf(m, "EEH Subsystem is globally disabled\n");
  873. seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
  874. } else {
  875. seq_printf(m, "EEH Subsystem is enabled\n");
  876. seq_printf(m,
  877. "no device=%llu\n"
  878. "no device node=%llu\n"
  879. "no config address=%llu\n"
  880. "check not wanted=%llu\n"
  881. "eeh_total_mmio_ffs=%llu\n"
  882. "eeh_false_positives=%llu\n"
  883. "eeh_slot_resets=%llu\n",
  884. eeh_stats.no_device,
  885. eeh_stats.no_dn,
  886. eeh_stats.no_cfg_addr,
  887. eeh_stats.ignored_check,
  888. eeh_stats.total_mmio_ffs,
  889. eeh_stats.false_positives,
  890. eeh_stats.slot_resets);
  891. }
  892. return 0;
  893. }
  894. static int proc_eeh_open(struct inode *inode, struct file *file)
  895. {
  896. return single_open(file, proc_eeh_show, NULL);
  897. }
  898. static const struct file_operations proc_eeh_operations = {
  899. .open = proc_eeh_open,
  900. .read = seq_read,
  901. .llseek = seq_lseek,
  902. .release = single_release,
  903. };
  904. static int __init eeh_init_proc(void)
  905. {
  906. if (machine_is(pseries))
  907. proc_create("powerpc/eeh", 0, NULL, &proc_eeh_operations);
  908. return 0;
  909. }
  910. __initcall(eeh_init_proc);