eeh.c 30 KB

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