eeh.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  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/debugfs.h>
  25. #include <linux/sched.h>
  26. #include <linux/init.h>
  27. #include <linux/list.h>
  28. #include <linux/pci.h>
  29. #include <linux/iommu.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/rbtree.h>
  32. #include <linux/reboot.h>
  33. #include <linux/seq_file.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/export.h>
  36. #include <linux/of.h>
  37. #include <linux/atomic.h>
  38. #include <asm/debug.h>
  39. #include <asm/eeh.h>
  40. #include <asm/eeh_event.h>
  41. #include <asm/io.h>
  42. #include <asm/iommu.h>
  43. #include <asm/machdep.h>
  44. #include <asm/ppc-pci.h>
  45. #include <asm/rtas.h>
  46. /** Overview:
  47. * EEH, or "Extended Error Handling" is a PCI bridge technology for
  48. * dealing with PCI bus errors that can't be dealt with within the
  49. * usual PCI framework, except by check-stopping the CPU. Systems
  50. * that are designed for high-availability/reliability cannot afford
  51. * to crash due to a "mere" PCI error, thus the need for EEH.
  52. * An EEH-capable bridge operates by converting a detected error
  53. * into a "slot freeze", taking the PCI adapter off-line, making
  54. * the slot behave, from the OS'es point of view, as if the slot
  55. * were "empty": all reads return 0xff's and all writes are silently
  56. * ignored. EEH slot isolation events can be triggered by parity
  57. * errors on the address or data busses (e.g. during posted writes),
  58. * which in turn might be caused by low voltage on the bus, dust,
  59. * vibration, humidity, radioactivity or plain-old failed hardware.
  60. *
  61. * Note, however, that one of the leading causes of EEH slot
  62. * freeze events are buggy device drivers, buggy device microcode,
  63. * or buggy device hardware. This is because any attempt by the
  64. * device to bus-master data to a memory address that is not
  65. * assigned to the device will trigger a slot freeze. (The idea
  66. * is to prevent devices-gone-wild from corrupting system memory).
  67. * Buggy hardware/drivers will have a miserable time co-existing
  68. * with EEH.
  69. *
  70. * Ideally, a PCI device driver, when suspecting that an isolation
  71. * event has occurred (e.g. by reading 0xff's), will then ask EEH
  72. * whether this is the case, and then take appropriate steps to
  73. * reset the PCI slot, the PCI device, and then resume operations.
  74. * However, until that day, the checking is done here, with the
  75. * eeh_check_failure() routine embedded in the MMIO macros. If
  76. * the slot is found to be isolated, an "EEH Event" is synthesized
  77. * and sent out for processing.
  78. */
  79. /* If a device driver keeps reading an MMIO register in an interrupt
  80. * handler after a slot isolation event, it might be broken.
  81. * This sets the threshold for how many read attempts we allow
  82. * before printing an error message.
  83. */
  84. #define EEH_MAX_FAILS 2100000
  85. /* Time to wait for a PCI slot to report status, in milliseconds */
  86. #define PCI_BUS_RESET_WAIT_MSEC (5*60*1000)
  87. /*
  88. * EEH probe mode support, which is part of the flags,
  89. * is to support multiple platforms for EEH. Some platforms
  90. * like pSeries do PCI emunation based on device tree.
  91. * However, other platforms like powernv probe PCI devices
  92. * from hardware. The flag is used to distinguish that.
  93. * In addition, struct eeh_ops::probe would be invoked for
  94. * particular OF node or PCI device so that the corresponding
  95. * PE would be created there.
  96. */
  97. int eeh_subsystem_flags;
  98. EXPORT_SYMBOL(eeh_subsystem_flags);
  99. /* Platform dependent EEH operations */
  100. struct eeh_ops *eeh_ops = NULL;
  101. /* Lock to avoid races due to multiple reports of an error */
  102. DEFINE_RAW_SPINLOCK(confirm_error_lock);
  103. /* Lock to protect passed flags */
  104. static DEFINE_MUTEX(eeh_dev_mutex);
  105. /* Buffer for reporting pci register dumps. Its here in BSS, and
  106. * not dynamically alloced, so that it ends up in RMO where RTAS
  107. * can access it.
  108. */
  109. #define EEH_PCI_REGS_LOG_LEN 4096
  110. static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];
  111. /*
  112. * The struct is used to maintain the EEH global statistic
  113. * information. Besides, the EEH global statistics will be
  114. * exported to user space through procfs
  115. */
  116. struct eeh_stats {
  117. u64 no_device; /* PCI device not found */
  118. u64 no_dn; /* OF node not found */
  119. u64 no_cfg_addr; /* Config address not found */
  120. u64 ignored_check; /* EEH check skipped */
  121. u64 total_mmio_ffs; /* Total EEH checks */
  122. u64 false_positives; /* Unnecessary EEH checks */
  123. u64 slot_resets; /* PE reset */
  124. };
  125. static struct eeh_stats eeh_stats;
  126. #define IS_BRIDGE(class_code) (((class_code)<<16) == PCI_BASE_CLASS_BRIDGE)
  127. static int __init eeh_setup(char *str)
  128. {
  129. if (!strcmp(str, "off"))
  130. eeh_add_flag(EEH_FORCE_DISABLED);
  131. return 1;
  132. }
  133. __setup("eeh=", eeh_setup);
  134. /**
  135. * eeh_gather_pci_data - Copy assorted PCI config space registers to buff
  136. * @edev: device to report data for
  137. * @buf: point to buffer in which to log
  138. * @len: amount of room in buffer
  139. *
  140. * This routine captures assorted PCI configuration space data,
  141. * and puts them into a buffer for RTAS error logging.
  142. */
  143. static size_t eeh_gather_pci_data(struct eeh_dev *edev, char *buf, size_t len)
  144. {
  145. struct device_node *dn = eeh_dev_to_of_node(edev);
  146. u32 cfg;
  147. int cap, i;
  148. int n = 0, l = 0;
  149. char buffer[128];
  150. n += scnprintf(buf+n, len-n, "%s\n", dn->full_name);
  151. pr_warn("EEH: of node=%s\n", dn->full_name);
  152. eeh_ops->read_config(dn, PCI_VENDOR_ID, 4, &cfg);
  153. n += scnprintf(buf+n, len-n, "dev/vend:%08x\n", cfg);
  154. pr_warn("EEH: PCI device/vendor: %08x\n", cfg);
  155. eeh_ops->read_config(dn, PCI_COMMAND, 4, &cfg);
  156. n += scnprintf(buf+n, len-n, "cmd/stat:%x\n", cfg);
  157. pr_warn("EEH: PCI cmd/status register: %08x\n", cfg);
  158. /* Gather bridge-specific registers */
  159. if (edev->mode & EEH_DEV_BRIDGE) {
  160. eeh_ops->read_config(dn, PCI_SEC_STATUS, 2, &cfg);
  161. n += scnprintf(buf+n, len-n, "sec stat:%x\n", cfg);
  162. pr_warn("EEH: Bridge secondary status: %04x\n", cfg);
  163. eeh_ops->read_config(dn, PCI_BRIDGE_CONTROL, 2, &cfg);
  164. n += scnprintf(buf+n, len-n, "brdg ctl:%x\n", cfg);
  165. pr_warn("EEH: Bridge control: %04x\n", cfg);
  166. }
  167. /* Dump out the PCI-X command and status regs */
  168. cap = edev->pcix_cap;
  169. if (cap) {
  170. eeh_ops->read_config(dn, cap, 4, &cfg);
  171. n += scnprintf(buf+n, len-n, "pcix-cmd:%x\n", cfg);
  172. pr_warn("EEH: PCI-X cmd: %08x\n", cfg);
  173. eeh_ops->read_config(dn, cap+4, 4, &cfg);
  174. n += scnprintf(buf+n, len-n, "pcix-stat:%x\n", cfg);
  175. pr_warn("EEH: PCI-X status: %08x\n", cfg);
  176. }
  177. /* If PCI-E capable, dump PCI-E cap 10 */
  178. cap = edev->pcie_cap;
  179. if (cap) {
  180. n += scnprintf(buf+n, len-n, "pci-e cap10:\n");
  181. pr_warn("EEH: PCI-E capabilities and status follow:\n");
  182. for (i=0; i<=8; 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. if ((i % 4) == 0) {
  186. if (i != 0)
  187. pr_warn("%s\n", buffer);
  188. l = scnprintf(buffer, sizeof(buffer),
  189. "EEH: PCI-E %02x: %08x ",
  190. 4*i, cfg);
  191. } else {
  192. l += scnprintf(buffer+l, sizeof(buffer)-l,
  193. "%08x ", cfg);
  194. }
  195. }
  196. pr_warn("%s\n", buffer);
  197. }
  198. /* If AER capable, dump it */
  199. cap = edev->aer_cap;
  200. if (cap) {
  201. n += scnprintf(buf+n, len-n, "pci-e AER:\n");
  202. pr_warn("EEH: PCI-E AER capability register set follows:\n");
  203. for (i=0; i<=13; i++) {
  204. eeh_ops->read_config(dn, cap+4*i, 4, &cfg);
  205. n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
  206. if ((i % 4) == 0) {
  207. if (i != 0)
  208. pr_warn("%s\n", buffer);
  209. l = scnprintf(buffer, sizeof(buffer),
  210. "EEH: PCI-E AER %02x: %08x ",
  211. 4*i, cfg);
  212. } else {
  213. l += scnprintf(buffer+l, sizeof(buffer)-l,
  214. "%08x ", cfg);
  215. }
  216. }
  217. pr_warn("%s\n", buffer);
  218. }
  219. return n;
  220. }
  221. /**
  222. * eeh_slot_error_detail - Generate combined log including driver log and error log
  223. * @pe: EEH PE
  224. * @severity: temporary or permanent error log
  225. *
  226. * This routine should be called to generate the combined log, which
  227. * is comprised of driver log and error log. The driver log is figured
  228. * out from the config space of the corresponding PCI device, while
  229. * the error log is fetched through platform dependent function call.
  230. */
  231. void eeh_slot_error_detail(struct eeh_pe *pe, int severity)
  232. {
  233. size_t loglen = 0;
  234. struct eeh_dev *edev, *tmp;
  235. /*
  236. * When the PHB is fenced or dead, it's pointless to collect
  237. * the data from PCI config space because it should return
  238. * 0xFF's. For ER, we still retrieve the data from the PCI
  239. * config space.
  240. *
  241. * For pHyp, we have to enable IO for log retrieval. Otherwise,
  242. * 0xFF's is always returned from PCI config space.
  243. */
  244. if (!(pe->type & EEH_PE_PHB)) {
  245. if (eeh_has_flag(EEH_ENABLE_IO_FOR_LOG))
  246. eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
  247. eeh_ops->configure_bridge(pe);
  248. eeh_pe_restore_bars(pe);
  249. pci_regs_buf[0] = 0;
  250. eeh_pe_for_each_dev(pe, edev, tmp) {
  251. loglen += eeh_gather_pci_data(edev, pci_regs_buf + loglen,
  252. EEH_PCI_REGS_LOG_LEN - loglen);
  253. }
  254. }
  255. eeh_ops->get_log(pe, severity, pci_regs_buf, loglen);
  256. }
  257. /**
  258. * eeh_token_to_phys - Convert EEH address token to phys address
  259. * @token: I/O token, should be address in the form 0xA....
  260. *
  261. * This routine should be called to convert virtual I/O address
  262. * to physical one.
  263. */
  264. static inline unsigned long eeh_token_to_phys(unsigned long token)
  265. {
  266. pte_t *ptep;
  267. unsigned long pa;
  268. int hugepage_shift;
  269. /*
  270. * We won't find hugepages here, iomem
  271. */
  272. ptep = find_linux_pte_or_hugepte(init_mm.pgd, token, &hugepage_shift);
  273. if (!ptep)
  274. return token;
  275. WARN_ON(hugepage_shift);
  276. pa = pte_pfn(*ptep) << PAGE_SHIFT;
  277. return pa | (token & (PAGE_SIZE-1));
  278. }
  279. /*
  280. * On PowerNV platform, we might already have fenced PHB there.
  281. * For that case, it's meaningless to recover frozen PE. Intead,
  282. * We have to handle fenced PHB firstly.
  283. */
  284. static int eeh_phb_check_failure(struct eeh_pe *pe)
  285. {
  286. struct eeh_pe *phb_pe;
  287. unsigned long flags;
  288. int ret;
  289. if (!eeh_has_flag(EEH_PROBE_MODE_DEV))
  290. return -EPERM;
  291. /* Find the PHB PE */
  292. phb_pe = eeh_phb_pe_get(pe->phb);
  293. if (!phb_pe) {
  294. pr_warn("%s Can't find PE for PHB#%d\n",
  295. __func__, pe->phb->global_number);
  296. return -EEXIST;
  297. }
  298. /* If the PHB has been in problematic state */
  299. eeh_serialize_lock(&flags);
  300. if (phb_pe->state & EEH_PE_ISOLATED) {
  301. ret = 0;
  302. goto out;
  303. }
  304. /* Check PHB state */
  305. ret = eeh_ops->get_state(phb_pe, NULL);
  306. if ((ret < 0) ||
  307. (ret == EEH_STATE_NOT_SUPPORT) ||
  308. (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
  309. (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
  310. ret = 0;
  311. goto out;
  312. }
  313. /* Isolate the PHB and send event */
  314. eeh_pe_state_mark(phb_pe, EEH_PE_ISOLATED);
  315. eeh_serialize_unlock(flags);
  316. pr_err("EEH: PHB#%x failure detected, location: %s\n",
  317. phb_pe->phb->global_number, eeh_pe_loc_get(phb_pe));
  318. dump_stack();
  319. eeh_send_failure_event(phb_pe);
  320. return 1;
  321. out:
  322. eeh_serialize_unlock(flags);
  323. return ret;
  324. }
  325. /**
  326. * eeh_dev_check_failure - Check if all 1's data is due to EEH slot freeze
  327. * @edev: eeh device
  328. *
  329. * Check for an EEH failure for the given device node. Call this
  330. * routine if the result of a read was all 0xff's and you want to
  331. * find out if this is due to an EEH slot freeze. This routine
  332. * will query firmware for the EEH status.
  333. *
  334. * Returns 0 if there has not been an EEH error; otherwise returns
  335. * a non-zero value and queues up a slot isolation event notification.
  336. *
  337. * It is safe to call this routine in an interrupt context.
  338. */
  339. int eeh_dev_check_failure(struct eeh_dev *edev)
  340. {
  341. int ret;
  342. int active_flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
  343. unsigned long flags;
  344. struct device_node *dn;
  345. struct pci_dev *dev;
  346. struct eeh_pe *pe, *parent_pe, *phb_pe;
  347. int rc = 0;
  348. const char *location;
  349. eeh_stats.total_mmio_ffs++;
  350. if (!eeh_enabled())
  351. return 0;
  352. if (!edev) {
  353. eeh_stats.no_dn++;
  354. return 0;
  355. }
  356. dn = eeh_dev_to_of_node(edev);
  357. dev = eeh_dev_to_pci_dev(edev);
  358. pe = edev->pe;
  359. /* Access to IO BARs might get this far and still not want checking. */
  360. if (!pe) {
  361. eeh_stats.ignored_check++;
  362. pr_debug("EEH: Ignored check for %s %s\n",
  363. eeh_pci_name(dev), dn->full_name);
  364. return 0;
  365. }
  366. if (!pe->addr && !pe->config_addr) {
  367. eeh_stats.no_cfg_addr++;
  368. return 0;
  369. }
  370. /*
  371. * On PowerNV platform, we might already have fenced PHB
  372. * there and we need take care of that firstly.
  373. */
  374. ret = eeh_phb_check_failure(pe);
  375. if (ret > 0)
  376. return ret;
  377. /*
  378. * If the PE isn't owned by us, we shouldn't check the
  379. * state. Instead, let the owner handle it if the PE has
  380. * been frozen.
  381. */
  382. if (eeh_pe_passed(pe))
  383. return 0;
  384. /* If we already have a pending isolation event for this
  385. * slot, we know it's bad already, we don't need to check.
  386. * Do this checking under a lock; as multiple PCI devices
  387. * in one slot might report errors simultaneously, and we
  388. * only want one error recovery routine running.
  389. */
  390. eeh_serialize_lock(&flags);
  391. rc = 1;
  392. if (pe->state & EEH_PE_ISOLATED) {
  393. pe->check_count++;
  394. if (pe->check_count % EEH_MAX_FAILS == 0) {
  395. location = of_get_property(dn, "ibm,loc-code", NULL);
  396. printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
  397. "location=%s driver=%s pci addr=%s\n",
  398. pe->check_count, location,
  399. eeh_driver_name(dev), eeh_pci_name(dev));
  400. printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
  401. eeh_driver_name(dev));
  402. dump_stack();
  403. }
  404. goto dn_unlock;
  405. }
  406. /*
  407. * Now test for an EEH failure. This is VERY expensive.
  408. * Note that the eeh_config_addr may be a parent device
  409. * in the case of a device behind a bridge, or it may be
  410. * function zero of a multi-function device.
  411. * In any case they must share a common PHB.
  412. */
  413. ret = eeh_ops->get_state(pe, NULL);
  414. /* Note that config-io to empty slots may fail;
  415. * they are empty when they don't have children.
  416. * We will punt with the following conditions: Failure to get
  417. * PE's state, EEH not support and Permanently unavailable
  418. * state, PE is in good state.
  419. */
  420. if ((ret < 0) ||
  421. (ret == EEH_STATE_NOT_SUPPORT) ||
  422. ((ret & active_flags) == active_flags)) {
  423. eeh_stats.false_positives++;
  424. pe->false_positives++;
  425. rc = 0;
  426. goto dn_unlock;
  427. }
  428. /*
  429. * It should be corner case that the parent PE has been
  430. * put into frozen state as well. We should take care
  431. * that at first.
  432. */
  433. parent_pe = pe->parent;
  434. while (parent_pe) {
  435. /* Hit the ceiling ? */
  436. if (parent_pe->type & EEH_PE_PHB)
  437. break;
  438. /* Frozen parent PE ? */
  439. ret = eeh_ops->get_state(parent_pe, NULL);
  440. if (ret > 0 &&
  441. (ret & active_flags) != active_flags)
  442. pe = parent_pe;
  443. /* Next parent level */
  444. parent_pe = parent_pe->parent;
  445. }
  446. eeh_stats.slot_resets++;
  447. /* Avoid repeated reports of this failure, including problems
  448. * with other functions on this device, and functions under
  449. * bridges.
  450. */
  451. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  452. eeh_serialize_unlock(flags);
  453. /* Most EEH events are due to device driver bugs. Having
  454. * a stack trace will help the device-driver authors figure
  455. * out what happened. So print that out.
  456. */
  457. phb_pe = eeh_phb_pe_get(pe->phb);
  458. pr_err("EEH: Frozen PHB#%x-PE#%x detected\n",
  459. pe->phb->global_number, pe->addr);
  460. pr_err("EEH: PE location: %s, PHB location: %s\n",
  461. eeh_pe_loc_get(pe), eeh_pe_loc_get(phb_pe));
  462. dump_stack();
  463. eeh_send_failure_event(pe);
  464. return 1;
  465. dn_unlock:
  466. eeh_serialize_unlock(flags);
  467. return rc;
  468. }
  469. EXPORT_SYMBOL_GPL(eeh_dev_check_failure);
  470. /**
  471. * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
  472. * @token: I/O token, should be address in the form 0xA....
  473. * @val: value, should be all 1's (XXX why do we need this arg??)
  474. *
  475. * Check for an EEH failure at the given token address. Call this
  476. * routine if the result of a read was all 0xff's and you want to
  477. * find out if this is due to an EEH slot freeze event. This routine
  478. * will query firmware for the EEH status.
  479. *
  480. * Note this routine is safe to call in an interrupt context.
  481. */
  482. unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
  483. {
  484. unsigned long addr;
  485. struct eeh_dev *edev;
  486. /* Finding the phys addr + pci device; this is pretty quick. */
  487. addr = eeh_token_to_phys((unsigned long __force) token);
  488. edev = eeh_addr_cache_get_dev(addr);
  489. if (!edev) {
  490. eeh_stats.no_device++;
  491. return val;
  492. }
  493. eeh_dev_check_failure(edev);
  494. return val;
  495. }
  496. EXPORT_SYMBOL(eeh_check_failure);
  497. /**
  498. * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
  499. * @pe: EEH PE
  500. *
  501. * This routine should be called to reenable frozen MMIO or DMA
  502. * so that it would work correctly again. It's useful while doing
  503. * recovery or log collection on the indicated device.
  504. */
  505. int eeh_pci_enable(struct eeh_pe *pe, int function)
  506. {
  507. int rc, flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
  508. /*
  509. * pHyp doesn't allow to enable IO or DMA on unfrozen PE.
  510. * Also, it's pointless to enable them on unfrozen PE. So
  511. * we have the check here.
  512. */
  513. if (function == EEH_OPT_THAW_MMIO ||
  514. function == EEH_OPT_THAW_DMA) {
  515. rc = eeh_ops->get_state(pe, NULL);
  516. if (rc < 0)
  517. return rc;
  518. /* Needn't to enable or already enabled */
  519. if ((rc == EEH_STATE_NOT_SUPPORT) ||
  520. ((rc & flags) == flags))
  521. return 0;
  522. }
  523. rc = eeh_ops->set_option(pe, function);
  524. if (rc)
  525. pr_warn("%s: Unexpected state change %d on "
  526. "PHB#%d-PE#%x, err=%d\n",
  527. __func__, function, pe->phb->global_number,
  528. pe->addr, rc);
  529. rc = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
  530. if (rc <= 0)
  531. return rc;
  532. if ((function == EEH_OPT_THAW_MMIO) &&
  533. (rc & EEH_STATE_MMIO_ENABLED))
  534. return 0;
  535. if ((function == EEH_OPT_THAW_DMA) &&
  536. (rc & EEH_STATE_DMA_ENABLED))
  537. return 0;
  538. return rc;
  539. }
  540. /**
  541. * pcibios_set_pcie_slot_reset - Set PCI-E reset state
  542. * @dev: pci device struct
  543. * @state: reset state to enter
  544. *
  545. * Return value:
  546. * 0 if success
  547. */
  548. int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  549. {
  550. struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
  551. struct eeh_pe *pe = edev->pe;
  552. if (!pe) {
  553. pr_err("%s: No PE found on PCI device %s\n",
  554. __func__, pci_name(dev));
  555. return -EINVAL;
  556. }
  557. switch (state) {
  558. case pcie_deassert_reset:
  559. eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
  560. break;
  561. case pcie_hot_reset:
  562. eeh_ops->reset(pe, EEH_RESET_HOT);
  563. break;
  564. case pcie_warm_reset:
  565. eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
  566. break;
  567. default:
  568. return -EINVAL;
  569. };
  570. return 0;
  571. }
  572. /**
  573. * eeh_set_pe_freset - Check the required reset for the indicated device
  574. * @data: EEH device
  575. * @flag: return value
  576. *
  577. * Each device might have its preferred reset type: fundamental or
  578. * hot reset. The routine is used to collected the information for
  579. * the indicated device and its children so that the bunch of the
  580. * devices could be reset properly.
  581. */
  582. static void *eeh_set_dev_freset(void *data, void *flag)
  583. {
  584. struct pci_dev *dev;
  585. unsigned int *freset = (unsigned int *)flag;
  586. struct eeh_dev *edev = (struct eeh_dev *)data;
  587. dev = eeh_dev_to_pci_dev(edev);
  588. if (dev)
  589. *freset |= dev->needs_freset;
  590. return NULL;
  591. }
  592. /**
  593. * eeh_reset_pe_once - Assert the pci #RST line for 1/4 second
  594. * @pe: EEH PE
  595. *
  596. * Assert the PCI #RST line for 1/4 second.
  597. */
  598. static void eeh_reset_pe_once(struct eeh_pe *pe)
  599. {
  600. unsigned int freset = 0;
  601. /* Determine type of EEH reset required for
  602. * Partitionable Endpoint, a hot-reset (1)
  603. * or a fundamental reset (3).
  604. * A fundamental reset required by any device under
  605. * Partitionable Endpoint trumps hot-reset.
  606. */
  607. eeh_pe_dev_traverse(pe, eeh_set_dev_freset, &freset);
  608. if (freset)
  609. eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
  610. else
  611. eeh_ops->reset(pe, EEH_RESET_HOT);
  612. eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
  613. }
  614. /**
  615. * eeh_reset_pe - Reset the indicated PE
  616. * @pe: EEH PE
  617. *
  618. * This routine should be called to reset indicated device, including
  619. * PE. A PE might include multiple PCI devices and sometimes PCI bridges
  620. * might be involved as well.
  621. */
  622. int eeh_reset_pe(struct eeh_pe *pe)
  623. {
  624. int flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
  625. int i, rc;
  626. /* Take three shots at resetting the bus */
  627. for (i=0; i<3; i++) {
  628. eeh_reset_pe_once(pe);
  629. /*
  630. * EEH_PE_ISOLATED is expected to be removed after
  631. * BAR restore.
  632. */
  633. rc = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
  634. if ((rc & flags) == flags)
  635. return 0;
  636. if (rc < 0) {
  637. pr_err("%s: Unrecoverable slot failure on PHB#%d-PE#%x",
  638. __func__, pe->phb->global_number, pe->addr);
  639. return -1;
  640. }
  641. pr_err("EEH: bus reset %d failed on PHB#%d-PE#%x, rc=%d\n",
  642. i+1, pe->phb->global_number, pe->addr, rc);
  643. }
  644. return -1;
  645. }
  646. /**
  647. * eeh_save_bars - Save device bars
  648. * @edev: PCI device associated EEH device
  649. *
  650. * Save the values of the device bars. Unlike the restore
  651. * routine, this routine is *not* recursive. This is because
  652. * PCI devices are added individually; but, for the restore,
  653. * an entire slot is reset at a time.
  654. */
  655. void eeh_save_bars(struct eeh_dev *edev)
  656. {
  657. int i;
  658. struct device_node *dn;
  659. if (!edev)
  660. return;
  661. dn = eeh_dev_to_of_node(edev);
  662. for (i = 0; i < 16; i++)
  663. eeh_ops->read_config(dn, i * 4, 4, &edev->config_space[i]);
  664. /*
  665. * For PCI bridges including root port, we need enable bus
  666. * master explicitly. Otherwise, it can't fetch IODA table
  667. * entries correctly. So we cache the bit in advance so that
  668. * we can restore it after reset, either PHB range or PE range.
  669. */
  670. if (edev->mode & EEH_DEV_BRIDGE)
  671. edev->config_space[1] |= PCI_COMMAND_MASTER;
  672. }
  673. /**
  674. * eeh_ops_register - Register platform dependent EEH operations
  675. * @ops: platform dependent EEH operations
  676. *
  677. * Register the platform dependent EEH operation callback
  678. * functions. The platform should call this function before
  679. * any other EEH operations.
  680. */
  681. int __init eeh_ops_register(struct eeh_ops *ops)
  682. {
  683. if (!ops->name) {
  684. pr_warn("%s: Invalid EEH ops name for %p\n",
  685. __func__, ops);
  686. return -EINVAL;
  687. }
  688. if (eeh_ops && eeh_ops != ops) {
  689. pr_warn("%s: EEH ops of platform %s already existing (%s)\n",
  690. __func__, eeh_ops->name, ops->name);
  691. return -EEXIST;
  692. }
  693. eeh_ops = ops;
  694. return 0;
  695. }
  696. /**
  697. * eeh_ops_unregister - Unreigster platform dependent EEH operations
  698. * @name: name of EEH platform operations
  699. *
  700. * Unregister the platform dependent EEH operation callback
  701. * functions.
  702. */
  703. int __exit eeh_ops_unregister(const char *name)
  704. {
  705. if (!name || !strlen(name)) {
  706. pr_warn("%s: Invalid EEH ops name\n",
  707. __func__);
  708. return -EINVAL;
  709. }
  710. if (eeh_ops && !strcmp(eeh_ops->name, name)) {
  711. eeh_ops = NULL;
  712. return 0;
  713. }
  714. return -EEXIST;
  715. }
  716. static int eeh_reboot_notifier(struct notifier_block *nb,
  717. unsigned long action, void *unused)
  718. {
  719. eeh_clear_flag(EEH_ENABLED);
  720. return NOTIFY_DONE;
  721. }
  722. static struct notifier_block eeh_reboot_nb = {
  723. .notifier_call = eeh_reboot_notifier,
  724. };
  725. /**
  726. * eeh_init - EEH initialization
  727. *
  728. * Initialize EEH by trying to enable it for all of the adapters in the system.
  729. * As a side effect we can determine here if eeh is supported at all.
  730. * Note that we leave EEH on so failed config cycles won't cause a machine
  731. * check. If a user turns off EEH for a particular adapter they are really
  732. * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
  733. * grant access to a slot if EEH isn't enabled, and so we always enable
  734. * EEH for all slots/all devices.
  735. *
  736. * The eeh-force-off option disables EEH checking globally, for all slots.
  737. * Even if force-off is set, the EEH hardware is still enabled, so that
  738. * newer systems can boot.
  739. */
  740. int eeh_init(void)
  741. {
  742. struct pci_controller *hose, *tmp;
  743. struct device_node *phb;
  744. static int cnt = 0;
  745. int ret = 0;
  746. /*
  747. * We have to delay the initialization on PowerNV after
  748. * the PCI hierarchy tree has been built because the PEs
  749. * are figured out based on PCI devices instead of device
  750. * tree nodes
  751. */
  752. if (machine_is(powernv) && cnt++ <= 0)
  753. return ret;
  754. /* Register reboot notifier */
  755. ret = register_reboot_notifier(&eeh_reboot_nb);
  756. if (ret) {
  757. pr_warn("%s: Failed to register notifier (%d)\n",
  758. __func__, ret);
  759. return ret;
  760. }
  761. /* call platform initialization function */
  762. if (!eeh_ops) {
  763. pr_warn("%s: Platform EEH operation not found\n",
  764. __func__);
  765. return -EEXIST;
  766. } else if ((ret = eeh_ops->init())) {
  767. pr_warn("%s: Failed to call platform init function (%d)\n",
  768. __func__, ret);
  769. return ret;
  770. }
  771. /* Initialize EEH event */
  772. ret = eeh_event_init();
  773. if (ret)
  774. return ret;
  775. /* Enable EEH for all adapters */
  776. if (eeh_has_flag(EEH_PROBE_MODE_DEVTREE)) {
  777. list_for_each_entry_safe(hose, tmp,
  778. &hose_list, list_node) {
  779. phb = hose->dn;
  780. traverse_pci_devices(phb, eeh_ops->of_probe, NULL);
  781. }
  782. } else if (eeh_has_flag(EEH_PROBE_MODE_DEV)) {
  783. list_for_each_entry_safe(hose, tmp,
  784. &hose_list, list_node)
  785. pci_walk_bus(hose->bus, eeh_ops->dev_probe, NULL);
  786. } else {
  787. pr_warn("%s: Invalid probe mode %x",
  788. __func__, eeh_subsystem_flags);
  789. return -EINVAL;
  790. }
  791. /*
  792. * Call platform post-initialization. Actually, It's good chance
  793. * to inform platform that EEH is ready to supply service if the
  794. * I/O cache stuff has been built up.
  795. */
  796. if (eeh_ops->post_init) {
  797. ret = eeh_ops->post_init();
  798. if (ret)
  799. return ret;
  800. }
  801. if (eeh_enabled())
  802. pr_info("EEH: PCI Enhanced I/O Error Handling Enabled\n");
  803. else
  804. pr_warn("EEH: No capable adapters found\n");
  805. return ret;
  806. }
  807. core_initcall_sync(eeh_init);
  808. /**
  809. * eeh_add_device_early - Enable EEH for the indicated device_node
  810. * @dn: device node for which to set up EEH
  811. *
  812. * This routine must be used to perform EEH initialization for PCI
  813. * devices that were added after system boot (e.g. hotplug, dlpar).
  814. * This routine must be called before any i/o is performed to the
  815. * adapter (inluding any config-space i/o).
  816. * Whether this actually enables EEH or not for this device depends
  817. * on the CEC architecture, type of the device, on earlier boot
  818. * command-line arguments & etc.
  819. */
  820. void eeh_add_device_early(struct device_node *dn)
  821. {
  822. struct pci_controller *phb;
  823. /*
  824. * If we're doing EEH probe based on PCI device, we
  825. * would delay the probe until late stage because
  826. * the PCI device isn't available this moment.
  827. */
  828. if (!eeh_has_flag(EEH_PROBE_MODE_DEVTREE))
  829. return;
  830. if (!of_node_to_eeh_dev(dn))
  831. return;
  832. phb = of_node_to_eeh_dev(dn)->phb;
  833. /* USB Bus children of PCI devices will not have BUID's */
  834. if (NULL == phb || 0 == phb->buid)
  835. return;
  836. eeh_ops->of_probe(dn, NULL);
  837. }
  838. /**
  839. * eeh_add_device_tree_early - Enable EEH for the indicated device
  840. * @dn: device node
  841. *
  842. * This routine must be used to perform EEH initialization for the
  843. * indicated PCI device that was added after system boot (e.g.
  844. * hotplug, dlpar).
  845. */
  846. void eeh_add_device_tree_early(struct device_node *dn)
  847. {
  848. struct device_node *sib;
  849. for_each_child_of_node(dn, sib)
  850. eeh_add_device_tree_early(sib);
  851. eeh_add_device_early(dn);
  852. }
  853. EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
  854. /**
  855. * eeh_add_device_late - Perform EEH initialization for the indicated pci device
  856. * @dev: pci device for which to set up EEH
  857. *
  858. * This routine must be used to complete EEH initialization for PCI
  859. * devices that were added after system boot (e.g. hotplug, dlpar).
  860. */
  861. void eeh_add_device_late(struct pci_dev *dev)
  862. {
  863. struct device_node *dn;
  864. struct eeh_dev *edev;
  865. if (!dev || !eeh_enabled())
  866. return;
  867. pr_debug("EEH: Adding device %s\n", pci_name(dev));
  868. dn = pci_device_to_OF_node(dev);
  869. edev = of_node_to_eeh_dev(dn);
  870. if (edev->pdev == dev) {
  871. pr_debug("EEH: Already referenced !\n");
  872. return;
  873. }
  874. /*
  875. * The EEH cache might not be removed correctly because of
  876. * unbalanced kref to the device during unplug time, which
  877. * relies on pcibios_release_device(). So we have to remove
  878. * that here explicitly.
  879. */
  880. if (edev->pdev) {
  881. eeh_rmv_from_parent_pe(edev);
  882. eeh_addr_cache_rmv_dev(edev->pdev);
  883. eeh_sysfs_remove_device(edev->pdev);
  884. edev->mode &= ~EEH_DEV_SYSFS;
  885. /*
  886. * We definitely should have the PCI device removed
  887. * though it wasn't correctly. So we needn't call
  888. * into error handler afterwards.
  889. */
  890. edev->mode |= EEH_DEV_NO_HANDLER;
  891. edev->pdev = NULL;
  892. dev->dev.archdata.edev = NULL;
  893. }
  894. edev->pdev = dev;
  895. dev->dev.archdata.edev = edev;
  896. /*
  897. * We have to do the EEH probe here because the PCI device
  898. * hasn't been created yet in the early stage.
  899. */
  900. if (eeh_has_flag(EEH_PROBE_MODE_DEV))
  901. eeh_ops->dev_probe(dev, NULL);
  902. eeh_addr_cache_insert_dev(dev);
  903. }
  904. /**
  905. * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
  906. * @bus: PCI bus
  907. *
  908. * This routine must be used to perform EEH initialization for PCI
  909. * devices which are attached to the indicated PCI bus. The PCI bus
  910. * is added after system boot through hotplug or dlpar.
  911. */
  912. void eeh_add_device_tree_late(struct pci_bus *bus)
  913. {
  914. struct pci_dev *dev;
  915. list_for_each_entry(dev, &bus->devices, bus_list) {
  916. eeh_add_device_late(dev);
  917. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  918. struct pci_bus *subbus = dev->subordinate;
  919. if (subbus)
  920. eeh_add_device_tree_late(subbus);
  921. }
  922. }
  923. }
  924. EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
  925. /**
  926. * eeh_add_sysfs_files - Add EEH sysfs files for the indicated PCI bus
  927. * @bus: PCI bus
  928. *
  929. * This routine must be used to add EEH sysfs files for PCI
  930. * devices which are attached to the indicated PCI bus. The PCI bus
  931. * is added after system boot through hotplug or dlpar.
  932. */
  933. void eeh_add_sysfs_files(struct pci_bus *bus)
  934. {
  935. struct pci_dev *dev;
  936. list_for_each_entry(dev, &bus->devices, bus_list) {
  937. eeh_sysfs_add_device(dev);
  938. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  939. struct pci_bus *subbus = dev->subordinate;
  940. if (subbus)
  941. eeh_add_sysfs_files(subbus);
  942. }
  943. }
  944. }
  945. EXPORT_SYMBOL_GPL(eeh_add_sysfs_files);
  946. /**
  947. * eeh_remove_device - Undo EEH setup for the indicated pci device
  948. * @dev: pci device to be removed
  949. *
  950. * This routine should be called when a device is removed from
  951. * a running system (e.g. by hotplug or dlpar). It unregisters
  952. * the PCI device from the EEH subsystem. I/O errors affecting
  953. * this device will no longer be detected after this call; thus,
  954. * i/o errors affecting this slot may leave this device unusable.
  955. */
  956. void eeh_remove_device(struct pci_dev *dev)
  957. {
  958. struct eeh_dev *edev;
  959. if (!dev || !eeh_enabled())
  960. return;
  961. edev = pci_dev_to_eeh_dev(dev);
  962. /* Unregister the device with the EEH/PCI address search system */
  963. pr_debug("EEH: Removing device %s\n", pci_name(dev));
  964. if (!edev || !edev->pdev || !edev->pe) {
  965. pr_debug("EEH: Not referenced !\n");
  966. return;
  967. }
  968. /*
  969. * During the hotplug for EEH error recovery, we need the EEH
  970. * device attached to the parent PE in order for BAR restore
  971. * a bit later. So we keep it for BAR restore and remove it
  972. * from the parent PE during the BAR resotre.
  973. */
  974. edev->pdev = NULL;
  975. dev->dev.archdata.edev = NULL;
  976. if (!(edev->pe->state & EEH_PE_KEEP))
  977. eeh_rmv_from_parent_pe(edev);
  978. else
  979. edev->mode |= EEH_DEV_DISCONNECTED;
  980. /*
  981. * We're removing from the PCI subsystem, that means
  982. * the PCI device driver can't support EEH or not
  983. * well. So we rely on hotplug completely to do recovery
  984. * for the specific PCI device.
  985. */
  986. edev->mode |= EEH_DEV_NO_HANDLER;
  987. eeh_addr_cache_rmv_dev(dev);
  988. eeh_sysfs_remove_device(dev);
  989. edev->mode &= ~EEH_DEV_SYSFS;
  990. }
  991. /**
  992. * eeh_dev_open - Increase count of pass through devices for PE
  993. * @pdev: PCI device
  994. *
  995. * Increase count of passed through devices for the indicated
  996. * PE. In the result, the EEH errors detected on the PE won't be
  997. * reported. The PE owner will be responsible for detection
  998. * and recovery.
  999. */
  1000. int eeh_dev_open(struct pci_dev *pdev)
  1001. {
  1002. struct eeh_dev *edev;
  1003. mutex_lock(&eeh_dev_mutex);
  1004. /* No PCI device ? */
  1005. if (!pdev)
  1006. goto out;
  1007. /* No EEH device or PE ? */
  1008. edev = pci_dev_to_eeh_dev(pdev);
  1009. if (!edev || !edev->pe)
  1010. goto out;
  1011. /* Increase PE's pass through count */
  1012. atomic_inc(&edev->pe->pass_dev_cnt);
  1013. mutex_unlock(&eeh_dev_mutex);
  1014. return 0;
  1015. out:
  1016. mutex_unlock(&eeh_dev_mutex);
  1017. return -ENODEV;
  1018. }
  1019. EXPORT_SYMBOL_GPL(eeh_dev_open);
  1020. /**
  1021. * eeh_dev_release - Decrease count of pass through devices for PE
  1022. * @pdev: PCI device
  1023. *
  1024. * Decrease count of pass through devices for the indicated PE. If
  1025. * there is no passed through device in PE, the EEH errors detected
  1026. * on the PE will be reported and handled as usual.
  1027. */
  1028. void eeh_dev_release(struct pci_dev *pdev)
  1029. {
  1030. struct eeh_dev *edev;
  1031. mutex_lock(&eeh_dev_mutex);
  1032. /* No PCI device ? */
  1033. if (!pdev)
  1034. goto out;
  1035. /* No EEH device ? */
  1036. edev = pci_dev_to_eeh_dev(pdev);
  1037. if (!edev || !edev->pe || !eeh_pe_passed(edev->pe))
  1038. goto out;
  1039. /* Decrease PE's pass through count */
  1040. atomic_dec(&edev->pe->pass_dev_cnt);
  1041. WARN_ON(atomic_read(&edev->pe->pass_dev_cnt) < 0);
  1042. out:
  1043. mutex_unlock(&eeh_dev_mutex);
  1044. }
  1045. EXPORT_SYMBOL(eeh_dev_release);
  1046. #ifdef CONFIG_IOMMU_API
  1047. static int dev_has_iommu_table(struct device *dev, void *data)
  1048. {
  1049. struct pci_dev *pdev = to_pci_dev(dev);
  1050. struct pci_dev **ppdev = data;
  1051. struct iommu_table *tbl;
  1052. if (!dev)
  1053. return 0;
  1054. tbl = get_iommu_table_base(dev);
  1055. if (tbl && tbl->it_group) {
  1056. *ppdev = pdev;
  1057. return 1;
  1058. }
  1059. return 0;
  1060. }
  1061. /**
  1062. * eeh_iommu_group_to_pe - Convert IOMMU group to EEH PE
  1063. * @group: IOMMU group
  1064. *
  1065. * The routine is called to convert IOMMU group to EEH PE.
  1066. */
  1067. struct eeh_pe *eeh_iommu_group_to_pe(struct iommu_group *group)
  1068. {
  1069. struct pci_dev *pdev = NULL;
  1070. struct eeh_dev *edev;
  1071. int ret;
  1072. /* No IOMMU group ? */
  1073. if (!group)
  1074. return NULL;
  1075. ret = iommu_group_for_each_dev(group, &pdev, dev_has_iommu_table);
  1076. if (!ret || !pdev)
  1077. return NULL;
  1078. /* No EEH device or PE ? */
  1079. edev = pci_dev_to_eeh_dev(pdev);
  1080. if (!edev || !edev->pe)
  1081. return NULL;
  1082. return edev->pe;
  1083. }
  1084. EXPORT_SYMBOL_GPL(eeh_iommu_group_to_pe);
  1085. #endif /* CONFIG_IOMMU_API */
  1086. /**
  1087. * eeh_pe_set_option - Set options for the indicated PE
  1088. * @pe: EEH PE
  1089. * @option: requested option
  1090. *
  1091. * The routine is called to enable or disable EEH functionality
  1092. * on the indicated PE, to enable IO or DMA for the frozen PE.
  1093. */
  1094. int eeh_pe_set_option(struct eeh_pe *pe, int option)
  1095. {
  1096. int ret = 0;
  1097. /* Invalid PE ? */
  1098. if (!pe)
  1099. return -ENODEV;
  1100. /*
  1101. * EEH functionality could possibly be disabled, just
  1102. * return error for the case. And the EEH functinality
  1103. * isn't expected to be disabled on one specific PE.
  1104. */
  1105. switch (option) {
  1106. case EEH_OPT_ENABLE:
  1107. if (eeh_enabled())
  1108. break;
  1109. ret = -EIO;
  1110. break;
  1111. case EEH_OPT_DISABLE:
  1112. break;
  1113. case EEH_OPT_THAW_MMIO:
  1114. case EEH_OPT_THAW_DMA:
  1115. if (!eeh_ops || !eeh_ops->set_option) {
  1116. ret = -ENOENT;
  1117. break;
  1118. }
  1119. ret = eeh_ops->set_option(pe, option);
  1120. break;
  1121. default:
  1122. pr_debug("%s: Option %d out of range (%d, %d)\n",
  1123. __func__, option, EEH_OPT_DISABLE, EEH_OPT_THAW_DMA);
  1124. ret = -EINVAL;
  1125. }
  1126. return ret;
  1127. }
  1128. EXPORT_SYMBOL_GPL(eeh_pe_set_option);
  1129. /**
  1130. * eeh_pe_get_state - Retrieve PE's state
  1131. * @pe: EEH PE
  1132. *
  1133. * Retrieve the PE's state, which includes 3 aspects: enabled
  1134. * DMA, enabled IO and asserted reset.
  1135. */
  1136. int eeh_pe_get_state(struct eeh_pe *pe)
  1137. {
  1138. int result, ret = 0;
  1139. bool rst_active, dma_en, mmio_en;
  1140. /* Existing PE ? */
  1141. if (!pe)
  1142. return -ENODEV;
  1143. if (!eeh_ops || !eeh_ops->get_state)
  1144. return -ENOENT;
  1145. result = eeh_ops->get_state(pe, NULL);
  1146. rst_active = !!(result & EEH_STATE_RESET_ACTIVE);
  1147. dma_en = !!(result & EEH_STATE_DMA_ENABLED);
  1148. mmio_en = !!(result & EEH_STATE_MMIO_ENABLED);
  1149. if (rst_active)
  1150. ret = EEH_PE_STATE_RESET;
  1151. else if (dma_en && mmio_en)
  1152. ret = EEH_PE_STATE_NORMAL;
  1153. else if (!dma_en && !mmio_en)
  1154. ret = EEH_PE_STATE_STOPPED_IO_DMA;
  1155. else if (!dma_en && mmio_en)
  1156. ret = EEH_PE_STATE_STOPPED_DMA;
  1157. else
  1158. ret = EEH_PE_STATE_UNAVAIL;
  1159. return ret;
  1160. }
  1161. EXPORT_SYMBOL_GPL(eeh_pe_get_state);
  1162. /**
  1163. * eeh_pe_reset - Issue PE reset according to specified type
  1164. * @pe: EEH PE
  1165. * @option: reset type
  1166. *
  1167. * The routine is called to reset the specified PE with the
  1168. * indicated type, either fundamental reset or hot reset.
  1169. * PE reset is the most important part for error recovery.
  1170. */
  1171. int eeh_pe_reset(struct eeh_pe *pe, int option)
  1172. {
  1173. int ret = 0;
  1174. /* Invalid PE ? */
  1175. if (!pe)
  1176. return -ENODEV;
  1177. if (!eeh_ops || !eeh_ops->set_option || !eeh_ops->reset)
  1178. return -ENOENT;
  1179. switch (option) {
  1180. case EEH_RESET_DEACTIVATE:
  1181. ret = eeh_ops->reset(pe, option);
  1182. if (ret)
  1183. break;
  1184. /*
  1185. * The PE is still in frozen state and we need to clear
  1186. * that. It's good to clear frozen state after deassert
  1187. * to avoid messy IO access during reset, which might
  1188. * cause recursive frozen PE.
  1189. */
  1190. ret = eeh_ops->set_option(pe, EEH_OPT_THAW_MMIO);
  1191. if (!ret)
  1192. ret = eeh_ops->set_option(pe, EEH_OPT_THAW_DMA);
  1193. if (!ret)
  1194. eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
  1195. break;
  1196. case EEH_RESET_HOT:
  1197. case EEH_RESET_FUNDAMENTAL:
  1198. ret = eeh_ops->reset(pe, option);
  1199. break;
  1200. default:
  1201. pr_debug("%s: Unsupported option %d\n",
  1202. __func__, option);
  1203. ret = -EINVAL;
  1204. }
  1205. return ret;
  1206. }
  1207. EXPORT_SYMBOL_GPL(eeh_pe_reset);
  1208. /**
  1209. * eeh_pe_configure - Configure PCI bridges after PE reset
  1210. * @pe: EEH PE
  1211. *
  1212. * The routine is called to restore the PCI config space for
  1213. * those PCI devices, especially PCI bridges affected by PE
  1214. * reset issued previously.
  1215. */
  1216. int eeh_pe_configure(struct eeh_pe *pe)
  1217. {
  1218. int ret = 0;
  1219. /* Invalid PE ? */
  1220. if (!pe)
  1221. return -ENODEV;
  1222. /* Restore config space for the affected devices */
  1223. eeh_pe_restore_bars(pe);
  1224. return ret;
  1225. }
  1226. EXPORT_SYMBOL_GPL(eeh_pe_configure);
  1227. static int proc_eeh_show(struct seq_file *m, void *v)
  1228. {
  1229. if (!eeh_enabled()) {
  1230. seq_printf(m, "EEH Subsystem is globally disabled\n");
  1231. seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
  1232. } else {
  1233. seq_printf(m, "EEH Subsystem is enabled\n");
  1234. seq_printf(m,
  1235. "no device=%llu\n"
  1236. "no device node=%llu\n"
  1237. "no config address=%llu\n"
  1238. "check not wanted=%llu\n"
  1239. "eeh_total_mmio_ffs=%llu\n"
  1240. "eeh_false_positives=%llu\n"
  1241. "eeh_slot_resets=%llu\n",
  1242. eeh_stats.no_device,
  1243. eeh_stats.no_dn,
  1244. eeh_stats.no_cfg_addr,
  1245. eeh_stats.ignored_check,
  1246. eeh_stats.total_mmio_ffs,
  1247. eeh_stats.false_positives,
  1248. eeh_stats.slot_resets);
  1249. }
  1250. return 0;
  1251. }
  1252. static int proc_eeh_open(struct inode *inode, struct file *file)
  1253. {
  1254. return single_open(file, proc_eeh_show, NULL);
  1255. }
  1256. static const struct file_operations proc_eeh_operations = {
  1257. .open = proc_eeh_open,
  1258. .read = seq_read,
  1259. .llseek = seq_lseek,
  1260. .release = single_release,
  1261. };
  1262. #ifdef CONFIG_DEBUG_FS
  1263. static int eeh_enable_dbgfs_set(void *data, u64 val)
  1264. {
  1265. if (val)
  1266. eeh_clear_flag(EEH_FORCE_DISABLED);
  1267. else
  1268. eeh_add_flag(EEH_FORCE_DISABLED);
  1269. /* Notify the backend */
  1270. if (eeh_ops->post_init)
  1271. eeh_ops->post_init();
  1272. return 0;
  1273. }
  1274. static int eeh_enable_dbgfs_get(void *data, u64 *val)
  1275. {
  1276. if (eeh_enabled())
  1277. *val = 0x1ul;
  1278. else
  1279. *val = 0x0ul;
  1280. return 0;
  1281. }
  1282. DEFINE_SIMPLE_ATTRIBUTE(eeh_enable_dbgfs_ops, eeh_enable_dbgfs_get,
  1283. eeh_enable_dbgfs_set, "0x%llx\n");
  1284. #endif
  1285. static int __init eeh_init_proc(void)
  1286. {
  1287. if (machine_is(pseries) || machine_is(powernv)) {
  1288. proc_create("powerpc/eeh", 0, NULL, &proc_eeh_operations);
  1289. #ifdef CONFIG_DEBUG_FS
  1290. debugfs_create_file("eeh_enable", 0600,
  1291. powerpc_debugfs_root, NULL,
  1292. &eeh_enable_dbgfs_ops);
  1293. #endif
  1294. }
  1295. return 0;
  1296. }
  1297. __initcall(eeh_init_proc);