eeh.c 30 KB

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