eeh.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779
  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. /*
  100. * EEH allowed maximal frozen times. If one particular PE's
  101. * frozen count in last hour exceeds this limit, the PE will
  102. * be forced to be offline permanently.
  103. */
  104. int eeh_max_freezes = 5;
  105. /* Platform dependent EEH operations */
  106. struct eeh_ops *eeh_ops = NULL;
  107. /* Lock to avoid races due to multiple reports of an error */
  108. DEFINE_RAW_SPINLOCK(confirm_error_lock);
  109. /* Lock to protect passed flags */
  110. static DEFINE_MUTEX(eeh_dev_mutex);
  111. /* Buffer for reporting pci register dumps. Its here in BSS, and
  112. * not dynamically alloced, so that it ends up in RMO where RTAS
  113. * can access it.
  114. */
  115. #define EEH_PCI_REGS_LOG_LEN 8192
  116. static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];
  117. /*
  118. * The struct is used to maintain the EEH global statistic
  119. * information. Besides, the EEH global statistics will be
  120. * exported to user space through procfs
  121. */
  122. struct eeh_stats {
  123. u64 no_device; /* PCI device not found */
  124. u64 no_dn; /* OF node not found */
  125. u64 no_cfg_addr; /* Config address not found */
  126. u64 ignored_check; /* EEH check skipped */
  127. u64 total_mmio_ffs; /* Total EEH checks */
  128. u64 false_positives; /* Unnecessary EEH checks */
  129. u64 slot_resets; /* PE reset */
  130. };
  131. static struct eeh_stats eeh_stats;
  132. static int __init eeh_setup(char *str)
  133. {
  134. if (!strcmp(str, "off"))
  135. eeh_add_flag(EEH_FORCE_DISABLED);
  136. else if (!strcmp(str, "early_log"))
  137. eeh_add_flag(EEH_EARLY_DUMP_LOG);
  138. return 1;
  139. }
  140. __setup("eeh=", eeh_setup);
  141. /*
  142. * This routine captures assorted PCI configuration space data
  143. * for the indicated PCI device, and puts them into a buffer
  144. * for RTAS error logging.
  145. */
  146. static size_t eeh_dump_dev_log(struct eeh_dev *edev, char *buf, size_t len)
  147. {
  148. struct pci_dn *pdn = eeh_dev_to_pdn(edev);
  149. u32 cfg;
  150. int cap, i;
  151. int n = 0, l = 0;
  152. char buffer[128];
  153. n += scnprintf(buf+n, len-n, "%04x:%02x:%02x:%01x\n",
  154. edev->phb->global_number, pdn->busno,
  155. PCI_SLOT(pdn->devfn), PCI_FUNC(pdn->devfn));
  156. pr_warn("EEH: of node=%04x:%02x:%02x:%01x\n",
  157. edev->phb->global_number, pdn->busno,
  158. PCI_SLOT(pdn->devfn), PCI_FUNC(pdn->devfn));
  159. eeh_ops->read_config(pdn, PCI_VENDOR_ID, 4, &cfg);
  160. n += scnprintf(buf+n, len-n, "dev/vend:%08x\n", cfg);
  161. pr_warn("EEH: PCI device/vendor: %08x\n", cfg);
  162. eeh_ops->read_config(pdn, PCI_COMMAND, 4, &cfg);
  163. n += scnprintf(buf+n, len-n, "cmd/stat:%x\n", cfg);
  164. pr_warn("EEH: PCI cmd/status register: %08x\n", cfg);
  165. /* Gather bridge-specific registers */
  166. if (edev->mode & EEH_DEV_BRIDGE) {
  167. eeh_ops->read_config(pdn, PCI_SEC_STATUS, 2, &cfg);
  168. n += scnprintf(buf+n, len-n, "sec stat:%x\n", cfg);
  169. pr_warn("EEH: Bridge secondary status: %04x\n", cfg);
  170. eeh_ops->read_config(pdn, PCI_BRIDGE_CONTROL, 2, &cfg);
  171. n += scnprintf(buf+n, len-n, "brdg ctl:%x\n", cfg);
  172. pr_warn("EEH: Bridge control: %04x\n", cfg);
  173. }
  174. /* Dump out the PCI-X command and status regs */
  175. cap = edev->pcix_cap;
  176. if (cap) {
  177. eeh_ops->read_config(pdn, cap, 4, &cfg);
  178. n += scnprintf(buf+n, len-n, "pcix-cmd:%x\n", cfg);
  179. pr_warn("EEH: PCI-X cmd: %08x\n", cfg);
  180. eeh_ops->read_config(pdn, cap+4, 4, &cfg);
  181. n += scnprintf(buf+n, len-n, "pcix-stat:%x\n", cfg);
  182. pr_warn("EEH: PCI-X status: %08x\n", cfg);
  183. }
  184. /* If PCI-E capable, dump PCI-E cap 10 */
  185. cap = edev->pcie_cap;
  186. if (cap) {
  187. n += scnprintf(buf+n, len-n, "pci-e cap10:\n");
  188. pr_warn("EEH: PCI-E capabilities and status follow:\n");
  189. for (i=0; i<=8; i++) {
  190. eeh_ops->read_config(pdn, cap+4*i, 4, &cfg);
  191. n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
  192. if ((i % 4) == 0) {
  193. if (i != 0)
  194. pr_warn("%s\n", buffer);
  195. l = scnprintf(buffer, sizeof(buffer),
  196. "EEH: PCI-E %02x: %08x ",
  197. 4*i, cfg);
  198. } else {
  199. l += scnprintf(buffer+l, sizeof(buffer)-l,
  200. "%08x ", cfg);
  201. }
  202. }
  203. pr_warn("%s\n", buffer);
  204. }
  205. /* If AER capable, dump it */
  206. cap = edev->aer_cap;
  207. if (cap) {
  208. n += scnprintf(buf+n, len-n, "pci-e AER:\n");
  209. pr_warn("EEH: PCI-E AER capability register set follows:\n");
  210. for (i=0; i<=13; i++) {
  211. eeh_ops->read_config(pdn, cap+4*i, 4, &cfg);
  212. n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
  213. if ((i % 4) == 0) {
  214. if (i != 0)
  215. pr_warn("%s\n", buffer);
  216. l = scnprintf(buffer, sizeof(buffer),
  217. "EEH: PCI-E AER %02x: %08x ",
  218. 4*i, cfg);
  219. } else {
  220. l += scnprintf(buffer+l, sizeof(buffer)-l,
  221. "%08x ", cfg);
  222. }
  223. }
  224. pr_warn("%s\n", buffer);
  225. }
  226. return n;
  227. }
  228. static void *eeh_dump_pe_log(void *data, void *flag)
  229. {
  230. struct eeh_pe *pe = data;
  231. struct eeh_dev *edev, *tmp;
  232. size_t *plen = flag;
  233. /* If the PE's config space is blocked, 0xFF's will be
  234. * returned. It's pointless to collect the log in this
  235. * case.
  236. */
  237. if (pe->state & EEH_PE_CFG_BLOCKED)
  238. return NULL;
  239. eeh_pe_for_each_dev(pe, edev, tmp)
  240. *plen += eeh_dump_dev_log(edev, pci_regs_buf + *plen,
  241. EEH_PCI_REGS_LOG_LEN - *plen);
  242. return NULL;
  243. }
  244. /**
  245. * eeh_slot_error_detail - Generate combined log including driver log and error log
  246. * @pe: EEH PE
  247. * @severity: temporary or permanent error log
  248. *
  249. * This routine should be called to generate the combined log, which
  250. * is comprised of driver log and error log. The driver log is figured
  251. * out from the config space of the corresponding PCI device, while
  252. * the error log is fetched through platform dependent function call.
  253. */
  254. void eeh_slot_error_detail(struct eeh_pe *pe, int severity)
  255. {
  256. size_t loglen = 0;
  257. /*
  258. * When the PHB is fenced or dead, it's pointless to collect
  259. * the data from PCI config space because it should return
  260. * 0xFF's. For ER, we still retrieve the data from the PCI
  261. * config space.
  262. *
  263. * For pHyp, we have to enable IO for log retrieval. Otherwise,
  264. * 0xFF's is always returned from PCI config space.
  265. */
  266. if (!(pe->type & EEH_PE_PHB)) {
  267. if (eeh_has_flag(EEH_ENABLE_IO_FOR_LOG))
  268. eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
  269. eeh_ops->configure_bridge(pe);
  270. eeh_pe_restore_bars(pe);
  271. pci_regs_buf[0] = 0;
  272. eeh_pe_traverse(pe, eeh_dump_pe_log, &loglen);
  273. }
  274. eeh_ops->get_log(pe, severity, pci_regs_buf, loglen);
  275. }
  276. /**
  277. * eeh_token_to_phys - Convert EEH address token to phys address
  278. * @token: I/O token, should be address in the form 0xA....
  279. *
  280. * This routine should be called to convert virtual I/O address
  281. * to physical one.
  282. */
  283. static inline unsigned long eeh_token_to_phys(unsigned long token)
  284. {
  285. pte_t *ptep;
  286. unsigned long pa;
  287. int hugepage_shift;
  288. /*
  289. * We won't find hugepages here(this is iomem). Hence we are not
  290. * worried about _PAGE_SPLITTING/collapse. Also we will not hit
  291. * page table free, because of init_mm.
  292. */
  293. ptep = __find_linux_pte_or_hugepte(init_mm.pgd, token, &hugepage_shift);
  294. if (!ptep)
  295. return token;
  296. WARN_ON(hugepage_shift);
  297. pa = pte_pfn(*ptep) << PAGE_SHIFT;
  298. return pa | (token & (PAGE_SIZE-1));
  299. }
  300. /*
  301. * On PowerNV platform, we might already have fenced PHB there.
  302. * For that case, it's meaningless to recover frozen PE. Intead,
  303. * We have to handle fenced PHB firstly.
  304. */
  305. static int eeh_phb_check_failure(struct eeh_pe *pe)
  306. {
  307. struct eeh_pe *phb_pe;
  308. unsigned long flags;
  309. int ret;
  310. if (!eeh_has_flag(EEH_PROBE_MODE_DEV))
  311. return -EPERM;
  312. /* Find the PHB PE */
  313. phb_pe = eeh_phb_pe_get(pe->phb);
  314. if (!phb_pe) {
  315. pr_warn("%s Can't find PE for PHB#%d\n",
  316. __func__, pe->phb->global_number);
  317. return -EEXIST;
  318. }
  319. /* If the PHB has been in problematic state */
  320. eeh_serialize_lock(&flags);
  321. if (phb_pe->state & EEH_PE_ISOLATED) {
  322. ret = 0;
  323. goto out;
  324. }
  325. /* Check PHB state */
  326. ret = eeh_ops->get_state(phb_pe, NULL);
  327. if ((ret < 0) ||
  328. (ret == EEH_STATE_NOT_SUPPORT) ||
  329. (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
  330. (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
  331. ret = 0;
  332. goto out;
  333. }
  334. /* Isolate the PHB and send event */
  335. eeh_pe_state_mark(phb_pe, EEH_PE_ISOLATED);
  336. eeh_serialize_unlock(flags);
  337. pr_err("EEH: PHB#%x failure detected, location: %s\n",
  338. phb_pe->phb->global_number, eeh_pe_loc_get(phb_pe));
  339. dump_stack();
  340. eeh_send_failure_event(phb_pe);
  341. return 1;
  342. out:
  343. eeh_serialize_unlock(flags);
  344. return ret;
  345. }
  346. /**
  347. * eeh_dev_check_failure - Check if all 1's data is due to EEH slot freeze
  348. * @edev: eeh device
  349. *
  350. * Check for an EEH failure for the given device node. Call this
  351. * routine if the result of a read was all 0xff's and you want to
  352. * find out if this is due to an EEH slot freeze. This routine
  353. * will query firmware for the EEH status.
  354. *
  355. * Returns 0 if there has not been an EEH error; otherwise returns
  356. * a non-zero value and queues up a slot isolation event notification.
  357. *
  358. * It is safe to call this routine in an interrupt context.
  359. */
  360. int eeh_dev_check_failure(struct eeh_dev *edev)
  361. {
  362. int ret;
  363. int active_flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
  364. unsigned long flags;
  365. struct pci_dn *pdn;
  366. struct pci_dev *dev;
  367. struct eeh_pe *pe, *parent_pe, *phb_pe;
  368. int rc = 0;
  369. const char *location = NULL;
  370. eeh_stats.total_mmio_ffs++;
  371. if (!eeh_enabled())
  372. return 0;
  373. if (!edev) {
  374. eeh_stats.no_dn++;
  375. return 0;
  376. }
  377. dev = eeh_dev_to_pci_dev(edev);
  378. pe = eeh_dev_to_pe(edev);
  379. /* Access to IO BARs might get this far and still not want checking. */
  380. if (!pe) {
  381. eeh_stats.ignored_check++;
  382. pr_debug("EEH: Ignored check for %s\n",
  383. eeh_pci_name(dev));
  384. return 0;
  385. }
  386. if (!pe->addr && !pe->config_addr) {
  387. eeh_stats.no_cfg_addr++;
  388. return 0;
  389. }
  390. /*
  391. * On PowerNV platform, we might already have fenced PHB
  392. * there and we need take care of that firstly.
  393. */
  394. ret = eeh_phb_check_failure(pe);
  395. if (ret > 0)
  396. return ret;
  397. /*
  398. * If the PE isn't owned by us, we shouldn't check the
  399. * state. Instead, let the owner handle it if the PE has
  400. * been frozen.
  401. */
  402. if (eeh_pe_passed(pe))
  403. return 0;
  404. /* If we already have a pending isolation event for this
  405. * slot, we know it's bad already, we don't need to check.
  406. * Do this checking under a lock; as multiple PCI devices
  407. * in one slot might report errors simultaneously, and we
  408. * only want one error recovery routine running.
  409. */
  410. eeh_serialize_lock(&flags);
  411. rc = 1;
  412. if (pe->state & EEH_PE_ISOLATED) {
  413. pe->check_count++;
  414. if (pe->check_count % EEH_MAX_FAILS == 0) {
  415. pdn = eeh_dev_to_pdn(edev);
  416. if (pdn->node)
  417. location = of_get_property(pdn->node, "ibm,loc-code", NULL);
  418. printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
  419. "location=%s driver=%s pci addr=%s\n",
  420. pe->check_count,
  421. location ? location : "unknown",
  422. eeh_driver_name(dev), eeh_pci_name(dev));
  423. printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
  424. eeh_driver_name(dev));
  425. dump_stack();
  426. }
  427. goto dn_unlock;
  428. }
  429. /*
  430. * Now test for an EEH failure. This is VERY expensive.
  431. * Note that the eeh_config_addr may be a parent device
  432. * in the case of a device behind a bridge, or it may be
  433. * function zero of a multi-function device.
  434. * In any case they must share a common PHB.
  435. */
  436. ret = eeh_ops->get_state(pe, NULL);
  437. /* Note that config-io to empty slots may fail;
  438. * they are empty when they don't have children.
  439. * We will punt with the following conditions: Failure to get
  440. * PE's state, EEH not support and Permanently unavailable
  441. * state, PE is in good state.
  442. */
  443. if ((ret < 0) ||
  444. (ret == EEH_STATE_NOT_SUPPORT) ||
  445. ((ret & active_flags) == active_flags)) {
  446. eeh_stats.false_positives++;
  447. pe->false_positives++;
  448. rc = 0;
  449. goto dn_unlock;
  450. }
  451. /*
  452. * It should be corner case that the parent PE has been
  453. * put into frozen state as well. We should take care
  454. * that at first.
  455. */
  456. parent_pe = pe->parent;
  457. while (parent_pe) {
  458. /* Hit the ceiling ? */
  459. if (parent_pe->type & EEH_PE_PHB)
  460. break;
  461. /* Frozen parent PE ? */
  462. ret = eeh_ops->get_state(parent_pe, NULL);
  463. if (ret > 0 &&
  464. (ret & active_flags) != active_flags)
  465. pe = parent_pe;
  466. /* Next parent level */
  467. parent_pe = parent_pe->parent;
  468. }
  469. eeh_stats.slot_resets++;
  470. /* Avoid repeated reports of this failure, including problems
  471. * with other functions on this device, and functions under
  472. * bridges.
  473. */
  474. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  475. eeh_serialize_unlock(flags);
  476. /* Most EEH events are due to device driver bugs. Having
  477. * a stack trace will help the device-driver authors figure
  478. * out what happened. So print that out.
  479. */
  480. phb_pe = eeh_phb_pe_get(pe->phb);
  481. pr_err("EEH: Frozen PHB#%x-PE#%x detected\n",
  482. pe->phb->global_number, pe->addr);
  483. pr_err("EEH: PE location: %s, PHB location: %s\n",
  484. eeh_pe_loc_get(pe), eeh_pe_loc_get(phb_pe));
  485. dump_stack();
  486. eeh_send_failure_event(pe);
  487. return 1;
  488. dn_unlock:
  489. eeh_serialize_unlock(flags);
  490. return rc;
  491. }
  492. EXPORT_SYMBOL_GPL(eeh_dev_check_failure);
  493. /**
  494. * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
  495. * @token: I/O address
  496. *
  497. * Check for an EEH failure at the given I/O address. Call this
  498. * routine if the result of a read was all 0xff's and you want to
  499. * find out if this is due to an EEH slot freeze event. This routine
  500. * will query firmware for the EEH status.
  501. *
  502. * Note this routine is safe to call in an interrupt context.
  503. */
  504. int eeh_check_failure(const volatile void __iomem *token)
  505. {
  506. unsigned long addr;
  507. struct eeh_dev *edev;
  508. /* Finding the phys addr + pci device; this is pretty quick. */
  509. addr = eeh_token_to_phys((unsigned long __force) token);
  510. edev = eeh_addr_cache_get_dev(addr);
  511. if (!edev) {
  512. eeh_stats.no_device++;
  513. return 0;
  514. }
  515. return eeh_dev_check_failure(edev);
  516. }
  517. EXPORT_SYMBOL(eeh_check_failure);
  518. /**
  519. * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
  520. * @pe: EEH PE
  521. *
  522. * This routine should be called to reenable frozen MMIO or DMA
  523. * so that it would work correctly again. It's useful while doing
  524. * recovery or log collection on the indicated device.
  525. */
  526. int eeh_pci_enable(struct eeh_pe *pe, int function)
  527. {
  528. int active_flag, rc;
  529. /*
  530. * pHyp doesn't allow to enable IO or DMA on unfrozen PE.
  531. * Also, it's pointless to enable them on unfrozen PE. So
  532. * we have to check before enabling IO or DMA.
  533. */
  534. switch (function) {
  535. case EEH_OPT_THAW_MMIO:
  536. active_flag = EEH_STATE_MMIO_ACTIVE;
  537. break;
  538. case EEH_OPT_THAW_DMA:
  539. active_flag = EEH_STATE_DMA_ACTIVE;
  540. break;
  541. case EEH_OPT_DISABLE:
  542. case EEH_OPT_ENABLE:
  543. case EEH_OPT_FREEZE_PE:
  544. active_flag = 0;
  545. break;
  546. default:
  547. pr_warn("%s: Invalid function %d\n",
  548. __func__, function);
  549. return -EINVAL;
  550. }
  551. /*
  552. * Check if IO or DMA has been enabled before
  553. * enabling them.
  554. */
  555. if (active_flag) {
  556. rc = eeh_ops->get_state(pe, NULL);
  557. if (rc < 0)
  558. return rc;
  559. /* Needn't enable it at all */
  560. if (rc == EEH_STATE_NOT_SUPPORT)
  561. return 0;
  562. /* It's already enabled */
  563. if (rc & active_flag)
  564. return 0;
  565. }
  566. /* Issue the request */
  567. rc = eeh_ops->set_option(pe, function);
  568. if (rc)
  569. pr_warn("%s: Unexpected state change %d on "
  570. "PHB#%d-PE#%x, err=%d\n",
  571. __func__, function, pe->phb->global_number,
  572. pe->addr, rc);
  573. /* Check if the request is finished successfully */
  574. if (active_flag) {
  575. rc = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
  576. if (rc <= 0)
  577. return rc;
  578. if (rc & active_flag)
  579. return 0;
  580. return -EIO;
  581. }
  582. return rc;
  583. }
  584. static void *eeh_disable_and_save_dev_state(void *data, void *userdata)
  585. {
  586. struct eeh_dev *edev = data;
  587. struct pci_dev *pdev = eeh_dev_to_pci_dev(edev);
  588. struct pci_dev *dev = userdata;
  589. /*
  590. * The caller should have disabled and saved the
  591. * state for the specified device
  592. */
  593. if (!pdev || pdev == dev)
  594. return NULL;
  595. /* Ensure we have D0 power state */
  596. pci_set_power_state(pdev, PCI_D0);
  597. /* Save device state */
  598. pci_save_state(pdev);
  599. /*
  600. * Disable device to avoid any DMA traffic and
  601. * interrupt from the device
  602. */
  603. pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
  604. return NULL;
  605. }
  606. static void *eeh_restore_dev_state(void *data, void *userdata)
  607. {
  608. struct eeh_dev *edev = data;
  609. struct pci_dn *pdn = eeh_dev_to_pdn(edev);
  610. struct pci_dev *pdev = eeh_dev_to_pci_dev(edev);
  611. struct pci_dev *dev = userdata;
  612. if (!pdev)
  613. return NULL;
  614. /* Apply customization from firmware */
  615. if (pdn && eeh_ops->restore_config)
  616. eeh_ops->restore_config(pdn);
  617. /* The caller should restore state for the specified device */
  618. if (pdev != dev)
  619. pci_restore_state(pdev);
  620. return NULL;
  621. }
  622. /**
  623. * pcibios_set_pcie_slot_reset - Set PCI-E reset state
  624. * @dev: pci device struct
  625. * @state: reset state to enter
  626. *
  627. * Return value:
  628. * 0 if success
  629. */
  630. int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  631. {
  632. struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
  633. struct eeh_pe *pe = eeh_dev_to_pe(edev);
  634. if (!pe) {
  635. pr_err("%s: No PE found on PCI device %s\n",
  636. __func__, pci_name(dev));
  637. return -EINVAL;
  638. }
  639. switch (state) {
  640. case pcie_deassert_reset:
  641. eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
  642. eeh_unfreeze_pe(pe, false);
  643. eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED);
  644. eeh_pe_dev_traverse(pe, eeh_restore_dev_state, dev);
  645. eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
  646. break;
  647. case pcie_hot_reset:
  648. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  649. eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
  650. eeh_pe_dev_traverse(pe, eeh_disable_and_save_dev_state, dev);
  651. eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
  652. eeh_ops->reset(pe, EEH_RESET_HOT);
  653. break;
  654. case pcie_warm_reset:
  655. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  656. eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
  657. eeh_pe_dev_traverse(pe, eeh_disable_and_save_dev_state, dev);
  658. eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
  659. eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
  660. break;
  661. default:
  662. eeh_pe_state_clear(pe, EEH_PE_ISOLATED | EEH_PE_CFG_BLOCKED);
  663. return -EINVAL;
  664. };
  665. return 0;
  666. }
  667. /**
  668. * eeh_set_pe_freset - Check the required reset for the indicated device
  669. * @data: EEH device
  670. * @flag: return value
  671. *
  672. * Each device might have its preferred reset type: fundamental or
  673. * hot reset. The routine is used to collected the information for
  674. * the indicated device and its children so that the bunch of the
  675. * devices could be reset properly.
  676. */
  677. static void *eeh_set_dev_freset(void *data, void *flag)
  678. {
  679. struct pci_dev *dev;
  680. unsigned int *freset = (unsigned int *)flag;
  681. struct eeh_dev *edev = (struct eeh_dev *)data;
  682. dev = eeh_dev_to_pci_dev(edev);
  683. if (dev)
  684. *freset |= dev->needs_freset;
  685. return NULL;
  686. }
  687. /**
  688. * eeh_reset_pe_once - Assert the pci #RST line for 1/4 second
  689. * @pe: EEH PE
  690. *
  691. * Assert the PCI #RST line for 1/4 second.
  692. */
  693. static void eeh_reset_pe_once(struct eeh_pe *pe)
  694. {
  695. unsigned int freset = 0;
  696. /* Determine type of EEH reset required for
  697. * Partitionable Endpoint, a hot-reset (1)
  698. * or a fundamental reset (3).
  699. * A fundamental reset required by any device under
  700. * Partitionable Endpoint trumps hot-reset.
  701. */
  702. eeh_pe_dev_traverse(pe, eeh_set_dev_freset, &freset);
  703. if (freset)
  704. eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
  705. else
  706. eeh_ops->reset(pe, EEH_RESET_HOT);
  707. eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
  708. }
  709. /**
  710. * eeh_reset_pe - Reset the indicated PE
  711. * @pe: EEH PE
  712. *
  713. * This routine should be called to reset indicated device, including
  714. * PE. A PE might include multiple PCI devices and sometimes PCI bridges
  715. * might be involved as well.
  716. */
  717. int eeh_reset_pe(struct eeh_pe *pe)
  718. {
  719. int flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
  720. int i, state, ret;
  721. /* Mark as reset and block config space */
  722. eeh_pe_state_mark(pe, EEH_PE_RESET | EEH_PE_CFG_BLOCKED);
  723. /* Take three shots at resetting the bus */
  724. for (i = 0; i < 3; i++) {
  725. eeh_reset_pe_once(pe);
  726. /*
  727. * EEH_PE_ISOLATED is expected to be removed after
  728. * BAR restore.
  729. */
  730. state = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
  731. if ((state & flags) == flags) {
  732. ret = 0;
  733. goto out;
  734. }
  735. if (state < 0) {
  736. pr_warn("%s: Unrecoverable slot failure on PHB#%d-PE#%x",
  737. __func__, pe->phb->global_number, pe->addr);
  738. ret = -ENOTRECOVERABLE;
  739. goto out;
  740. }
  741. /* We might run out of credits */
  742. ret = -EIO;
  743. pr_warn("%s: Failure %d resetting PHB#%x-PE#%x\n (%d)\n",
  744. __func__, state, pe->phb->global_number, pe->addr, (i + 1));
  745. }
  746. out:
  747. eeh_pe_state_clear(pe, EEH_PE_RESET | EEH_PE_CFG_BLOCKED);
  748. return ret;
  749. }
  750. /**
  751. * eeh_save_bars - Save device bars
  752. * @edev: PCI device associated EEH device
  753. *
  754. * Save the values of the device bars. Unlike the restore
  755. * routine, this routine is *not* recursive. This is because
  756. * PCI devices are added individually; but, for the restore,
  757. * an entire slot is reset at a time.
  758. */
  759. void eeh_save_bars(struct eeh_dev *edev)
  760. {
  761. struct pci_dn *pdn;
  762. int i;
  763. pdn = eeh_dev_to_pdn(edev);
  764. if (!pdn)
  765. return;
  766. for (i = 0; i < 16; i++)
  767. eeh_ops->read_config(pdn, i * 4, 4, &edev->config_space[i]);
  768. /*
  769. * For PCI bridges including root port, we need enable bus
  770. * master explicitly. Otherwise, it can't fetch IODA table
  771. * entries correctly. So we cache the bit in advance so that
  772. * we can restore it after reset, either PHB range or PE range.
  773. */
  774. if (edev->mode & EEH_DEV_BRIDGE)
  775. edev->config_space[1] |= PCI_COMMAND_MASTER;
  776. }
  777. /**
  778. * eeh_ops_register - Register platform dependent EEH operations
  779. * @ops: platform dependent EEH operations
  780. *
  781. * Register the platform dependent EEH operation callback
  782. * functions. The platform should call this function before
  783. * any other EEH operations.
  784. */
  785. int __init eeh_ops_register(struct eeh_ops *ops)
  786. {
  787. if (!ops->name) {
  788. pr_warn("%s: Invalid EEH ops name for %p\n",
  789. __func__, ops);
  790. return -EINVAL;
  791. }
  792. if (eeh_ops && eeh_ops != ops) {
  793. pr_warn("%s: EEH ops of platform %s already existing (%s)\n",
  794. __func__, eeh_ops->name, ops->name);
  795. return -EEXIST;
  796. }
  797. eeh_ops = ops;
  798. return 0;
  799. }
  800. /**
  801. * eeh_ops_unregister - Unreigster platform dependent EEH operations
  802. * @name: name of EEH platform operations
  803. *
  804. * Unregister the platform dependent EEH operation callback
  805. * functions.
  806. */
  807. int __exit eeh_ops_unregister(const char *name)
  808. {
  809. if (!name || !strlen(name)) {
  810. pr_warn("%s: Invalid EEH ops name\n",
  811. __func__);
  812. return -EINVAL;
  813. }
  814. if (eeh_ops && !strcmp(eeh_ops->name, name)) {
  815. eeh_ops = NULL;
  816. return 0;
  817. }
  818. return -EEXIST;
  819. }
  820. static int eeh_reboot_notifier(struct notifier_block *nb,
  821. unsigned long action, void *unused)
  822. {
  823. eeh_clear_flag(EEH_ENABLED);
  824. return NOTIFY_DONE;
  825. }
  826. static struct notifier_block eeh_reboot_nb = {
  827. .notifier_call = eeh_reboot_notifier,
  828. };
  829. /**
  830. * eeh_init - EEH initialization
  831. *
  832. * Initialize EEH by trying to enable it for all of the adapters in the system.
  833. * As a side effect we can determine here if eeh is supported at all.
  834. * Note that we leave EEH on so failed config cycles won't cause a machine
  835. * check. If a user turns off EEH for a particular adapter they are really
  836. * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
  837. * grant access to a slot if EEH isn't enabled, and so we always enable
  838. * EEH for all slots/all devices.
  839. *
  840. * The eeh-force-off option disables EEH checking globally, for all slots.
  841. * Even if force-off is set, the EEH hardware is still enabled, so that
  842. * newer systems can boot.
  843. */
  844. int eeh_init(void)
  845. {
  846. struct pci_controller *hose, *tmp;
  847. struct pci_dn *pdn;
  848. static int cnt = 0;
  849. int ret = 0;
  850. /*
  851. * We have to delay the initialization on PowerNV after
  852. * the PCI hierarchy tree has been built because the PEs
  853. * are figured out based on PCI devices instead of device
  854. * tree nodes
  855. */
  856. if (machine_is(powernv) && cnt++ <= 0)
  857. return ret;
  858. /* Register reboot notifier */
  859. ret = register_reboot_notifier(&eeh_reboot_nb);
  860. if (ret) {
  861. pr_warn("%s: Failed to register notifier (%d)\n",
  862. __func__, ret);
  863. return ret;
  864. }
  865. /* call platform initialization function */
  866. if (!eeh_ops) {
  867. pr_warn("%s: Platform EEH operation not found\n",
  868. __func__);
  869. return -EEXIST;
  870. } else if ((ret = eeh_ops->init()))
  871. return ret;
  872. /* Initialize EEH event */
  873. ret = eeh_event_init();
  874. if (ret)
  875. return ret;
  876. /* Enable EEH for all adapters */
  877. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  878. pdn = hose->pci_data;
  879. traverse_pci_dn(pdn, eeh_ops->probe, NULL);
  880. }
  881. /*
  882. * Call platform post-initialization. Actually, It's good chance
  883. * to inform platform that EEH is ready to supply service if the
  884. * I/O cache stuff has been built up.
  885. */
  886. if (eeh_ops->post_init) {
  887. ret = eeh_ops->post_init();
  888. if (ret)
  889. return ret;
  890. }
  891. if (eeh_enabled())
  892. pr_info("EEH: PCI Enhanced I/O Error Handling Enabled\n");
  893. else
  894. pr_warn("EEH: No capable adapters found\n");
  895. return ret;
  896. }
  897. core_initcall_sync(eeh_init);
  898. /**
  899. * eeh_add_device_early - Enable EEH for the indicated device node
  900. * @pdn: PCI device node for which to set up EEH
  901. *
  902. * This routine must be used to perform EEH initialization for PCI
  903. * devices that were added after system boot (e.g. hotplug, dlpar).
  904. * This routine must be called before any i/o is performed to the
  905. * adapter (inluding any config-space i/o).
  906. * Whether this actually enables EEH or not for this device depends
  907. * on the CEC architecture, type of the device, on earlier boot
  908. * command-line arguments & etc.
  909. */
  910. void eeh_add_device_early(struct pci_dn *pdn)
  911. {
  912. struct pci_controller *phb;
  913. struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
  914. if (!edev || !eeh_enabled())
  915. return;
  916. if (!eeh_has_flag(EEH_PROBE_MODE_DEVTREE))
  917. return;
  918. /* USB Bus children of PCI devices will not have BUID's */
  919. phb = edev->phb;
  920. if (NULL == phb ||
  921. (eeh_has_flag(EEH_PROBE_MODE_DEVTREE) && 0 == phb->buid))
  922. return;
  923. eeh_ops->probe(pdn, NULL);
  924. }
  925. /**
  926. * eeh_add_device_tree_early - Enable EEH for the indicated device
  927. * @pdn: PCI device node
  928. *
  929. * This routine must be used to perform EEH initialization for the
  930. * indicated PCI device that was added after system boot (e.g.
  931. * hotplug, dlpar).
  932. */
  933. void eeh_add_device_tree_early(struct pci_dn *pdn)
  934. {
  935. struct pci_dn *n;
  936. if (!pdn)
  937. return;
  938. list_for_each_entry(n, &pdn->child_list, list)
  939. eeh_add_device_tree_early(n);
  940. eeh_add_device_early(pdn);
  941. }
  942. EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
  943. /**
  944. * eeh_add_device_late - Perform EEH initialization for the indicated pci device
  945. * @dev: pci device for which to set up EEH
  946. *
  947. * This routine must be used to complete EEH initialization for PCI
  948. * devices that were added after system boot (e.g. hotplug, dlpar).
  949. */
  950. void eeh_add_device_late(struct pci_dev *dev)
  951. {
  952. struct pci_dn *pdn;
  953. struct eeh_dev *edev;
  954. if (!dev || !eeh_enabled())
  955. return;
  956. pr_debug("EEH: Adding device %s\n", pci_name(dev));
  957. pdn = pci_get_pdn_by_devfn(dev->bus, dev->devfn);
  958. edev = pdn_to_eeh_dev(pdn);
  959. if (edev->pdev == dev) {
  960. pr_debug("EEH: Already referenced !\n");
  961. return;
  962. }
  963. if (eeh_has_flag(EEH_PROBE_MODE_DEV))
  964. eeh_ops->probe(pdn, NULL);
  965. /*
  966. * The EEH cache might not be removed correctly because of
  967. * unbalanced kref to the device during unplug time, which
  968. * relies on pcibios_release_device(). So we have to remove
  969. * that here explicitly.
  970. */
  971. if (edev->pdev) {
  972. eeh_rmv_from_parent_pe(edev);
  973. eeh_addr_cache_rmv_dev(edev->pdev);
  974. eeh_sysfs_remove_device(edev->pdev);
  975. edev->mode &= ~EEH_DEV_SYSFS;
  976. /*
  977. * We definitely should have the PCI device removed
  978. * though it wasn't correctly. So we needn't call
  979. * into error handler afterwards.
  980. */
  981. edev->mode |= EEH_DEV_NO_HANDLER;
  982. edev->pdev = NULL;
  983. dev->dev.archdata.edev = NULL;
  984. }
  985. edev->pdev = dev;
  986. dev->dev.archdata.edev = edev;
  987. eeh_addr_cache_insert_dev(dev);
  988. }
  989. /**
  990. * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
  991. * @bus: PCI bus
  992. *
  993. * This routine must be used to perform EEH initialization for PCI
  994. * devices which are attached to the indicated PCI bus. The PCI bus
  995. * is added after system boot through hotplug or dlpar.
  996. */
  997. void eeh_add_device_tree_late(struct pci_bus *bus)
  998. {
  999. struct pci_dev *dev;
  1000. list_for_each_entry(dev, &bus->devices, bus_list) {
  1001. eeh_add_device_late(dev);
  1002. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  1003. struct pci_bus *subbus = dev->subordinate;
  1004. if (subbus)
  1005. eeh_add_device_tree_late(subbus);
  1006. }
  1007. }
  1008. }
  1009. EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
  1010. /**
  1011. * eeh_add_sysfs_files - Add EEH sysfs files for the indicated PCI bus
  1012. * @bus: PCI bus
  1013. *
  1014. * This routine must be used to add EEH sysfs files for PCI
  1015. * devices which are attached to the indicated PCI bus. The PCI bus
  1016. * is added after system boot through hotplug or dlpar.
  1017. */
  1018. void eeh_add_sysfs_files(struct pci_bus *bus)
  1019. {
  1020. struct pci_dev *dev;
  1021. list_for_each_entry(dev, &bus->devices, bus_list) {
  1022. eeh_sysfs_add_device(dev);
  1023. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  1024. struct pci_bus *subbus = dev->subordinate;
  1025. if (subbus)
  1026. eeh_add_sysfs_files(subbus);
  1027. }
  1028. }
  1029. }
  1030. EXPORT_SYMBOL_GPL(eeh_add_sysfs_files);
  1031. /**
  1032. * eeh_remove_device - Undo EEH setup for the indicated pci device
  1033. * @dev: pci device to be removed
  1034. *
  1035. * This routine should be called when a device is removed from
  1036. * a running system (e.g. by hotplug or dlpar). It unregisters
  1037. * the PCI device from the EEH subsystem. I/O errors affecting
  1038. * this device will no longer be detected after this call; thus,
  1039. * i/o errors affecting this slot may leave this device unusable.
  1040. */
  1041. void eeh_remove_device(struct pci_dev *dev)
  1042. {
  1043. struct eeh_dev *edev;
  1044. if (!dev || !eeh_enabled())
  1045. return;
  1046. edev = pci_dev_to_eeh_dev(dev);
  1047. /* Unregister the device with the EEH/PCI address search system */
  1048. pr_debug("EEH: Removing device %s\n", pci_name(dev));
  1049. if (!edev || !edev->pdev || !edev->pe) {
  1050. pr_debug("EEH: Not referenced !\n");
  1051. return;
  1052. }
  1053. /*
  1054. * During the hotplug for EEH error recovery, we need the EEH
  1055. * device attached to the parent PE in order for BAR restore
  1056. * a bit later. So we keep it for BAR restore and remove it
  1057. * from the parent PE during the BAR resotre.
  1058. */
  1059. edev->pdev = NULL;
  1060. dev->dev.archdata.edev = NULL;
  1061. if (!(edev->pe->state & EEH_PE_KEEP))
  1062. eeh_rmv_from_parent_pe(edev);
  1063. else
  1064. edev->mode |= EEH_DEV_DISCONNECTED;
  1065. /*
  1066. * We're removing from the PCI subsystem, that means
  1067. * the PCI device driver can't support EEH or not
  1068. * well. So we rely on hotplug completely to do recovery
  1069. * for the specific PCI device.
  1070. */
  1071. edev->mode |= EEH_DEV_NO_HANDLER;
  1072. eeh_addr_cache_rmv_dev(dev);
  1073. eeh_sysfs_remove_device(dev);
  1074. edev->mode &= ~EEH_DEV_SYSFS;
  1075. }
  1076. int eeh_unfreeze_pe(struct eeh_pe *pe, bool sw_state)
  1077. {
  1078. int ret;
  1079. ret = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
  1080. if (ret) {
  1081. pr_warn("%s: Failure %d enabling IO on PHB#%x-PE#%x\n",
  1082. __func__, ret, pe->phb->global_number, pe->addr);
  1083. return ret;
  1084. }
  1085. ret = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
  1086. if (ret) {
  1087. pr_warn("%s: Failure %d enabling DMA on PHB#%x-PE#%x\n",
  1088. __func__, ret, pe->phb->global_number, pe->addr);
  1089. return ret;
  1090. }
  1091. /* Clear software isolated state */
  1092. if (sw_state && (pe->state & EEH_PE_ISOLATED))
  1093. eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
  1094. return ret;
  1095. }
  1096. static struct pci_device_id eeh_reset_ids[] = {
  1097. { PCI_DEVICE(0x19a2, 0x0710) }, /* Emulex, BE */
  1098. { PCI_DEVICE(0x10df, 0xe220) }, /* Emulex, Lancer */
  1099. { PCI_DEVICE(0x14e4, 0x1657) }, /* Broadcom BCM5719 */
  1100. { 0 }
  1101. };
  1102. static int eeh_pe_change_owner(struct eeh_pe *pe)
  1103. {
  1104. struct eeh_dev *edev, *tmp;
  1105. struct pci_dev *pdev;
  1106. struct pci_device_id *id;
  1107. int flags, ret;
  1108. /* Check PE state */
  1109. flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
  1110. ret = eeh_ops->get_state(pe, NULL);
  1111. if (ret < 0 || ret == EEH_STATE_NOT_SUPPORT)
  1112. return 0;
  1113. /* Unfrozen PE, nothing to do */
  1114. if ((ret & flags) == flags)
  1115. return 0;
  1116. /* Frozen PE, check if it needs PE level reset */
  1117. eeh_pe_for_each_dev(pe, edev, tmp) {
  1118. pdev = eeh_dev_to_pci_dev(edev);
  1119. if (!pdev)
  1120. continue;
  1121. for (id = &eeh_reset_ids[0]; id->vendor != 0; id++) {
  1122. if (id->vendor != PCI_ANY_ID &&
  1123. id->vendor != pdev->vendor)
  1124. continue;
  1125. if (id->device != PCI_ANY_ID &&
  1126. id->device != pdev->device)
  1127. continue;
  1128. if (id->subvendor != PCI_ANY_ID &&
  1129. id->subvendor != pdev->subsystem_vendor)
  1130. continue;
  1131. if (id->subdevice != PCI_ANY_ID &&
  1132. id->subdevice != pdev->subsystem_device)
  1133. continue;
  1134. goto reset;
  1135. }
  1136. }
  1137. return eeh_unfreeze_pe(pe, true);
  1138. reset:
  1139. return eeh_pe_reset_and_recover(pe);
  1140. }
  1141. /**
  1142. * eeh_dev_open - Increase count of pass through devices for PE
  1143. * @pdev: PCI device
  1144. *
  1145. * Increase count of passed through devices for the indicated
  1146. * PE. In the result, the EEH errors detected on the PE won't be
  1147. * reported. The PE owner will be responsible for detection
  1148. * and recovery.
  1149. */
  1150. int eeh_dev_open(struct pci_dev *pdev)
  1151. {
  1152. struct eeh_dev *edev;
  1153. int ret = -ENODEV;
  1154. mutex_lock(&eeh_dev_mutex);
  1155. /* No PCI device ? */
  1156. if (!pdev)
  1157. goto out;
  1158. /* No EEH device or PE ? */
  1159. edev = pci_dev_to_eeh_dev(pdev);
  1160. if (!edev || !edev->pe)
  1161. goto out;
  1162. /*
  1163. * The PE might have been put into frozen state, but we
  1164. * didn't detect that yet. The passed through PCI devices
  1165. * in frozen PE won't work properly. Clear the frozen state
  1166. * in advance.
  1167. */
  1168. ret = eeh_pe_change_owner(edev->pe);
  1169. if (ret)
  1170. goto out;
  1171. /* Increase PE's pass through count */
  1172. atomic_inc(&edev->pe->pass_dev_cnt);
  1173. mutex_unlock(&eeh_dev_mutex);
  1174. return 0;
  1175. out:
  1176. mutex_unlock(&eeh_dev_mutex);
  1177. return ret;
  1178. }
  1179. EXPORT_SYMBOL_GPL(eeh_dev_open);
  1180. /**
  1181. * eeh_dev_release - Decrease count of pass through devices for PE
  1182. * @pdev: PCI device
  1183. *
  1184. * Decrease count of pass through devices for the indicated PE. If
  1185. * there is no passed through device in PE, the EEH errors detected
  1186. * on the PE will be reported and handled as usual.
  1187. */
  1188. void eeh_dev_release(struct pci_dev *pdev)
  1189. {
  1190. struct eeh_dev *edev;
  1191. mutex_lock(&eeh_dev_mutex);
  1192. /* No PCI device ? */
  1193. if (!pdev)
  1194. goto out;
  1195. /* No EEH device ? */
  1196. edev = pci_dev_to_eeh_dev(pdev);
  1197. if (!edev || !edev->pe || !eeh_pe_passed(edev->pe))
  1198. goto out;
  1199. /* Decrease PE's pass through count */
  1200. atomic_dec(&edev->pe->pass_dev_cnt);
  1201. WARN_ON(atomic_read(&edev->pe->pass_dev_cnt) < 0);
  1202. eeh_pe_change_owner(edev->pe);
  1203. out:
  1204. mutex_unlock(&eeh_dev_mutex);
  1205. }
  1206. EXPORT_SYMBOL(eeh_dev_release);
  1207. #ifdef CONFIG_IOMMU_API
  1208. static int dev_has_iommu_table(struct device *dev, void *data)
  1209. {
  1210. struct pci_dev *pdev = to_pci_dev(dev);
  1211. struct pci_dev **ppdev = data;
  1212. if (!dev)
  1213. return 0;
  1214. if (dev->iommu_group) {
  1215. *ppdev = pdev;
  1216. return 1;
  1217. }
  1218. return 0;
  1219. }
  1220. /**
  1221. * eeh_iommu_group_to_pe - Convert IOMMU group to EEH PE
  1222. * @group: IOMMU group
  1223. *
  1224. * The routine is called to convert IOMMU group to EEH PE.
  1225. */
  1226. struct eeh_pe *eeh_iommu_group_to_pe(struct iommu_group *group)
  1227. {
  1228. struct pci_dev *pdev = NULL;
  1229. struct eeh_dev *edev;
  1230. int ret;
  1231. /* No IOMMU group ? */
  1232. if (!group)
  1233. return NULL;
  1234. ret = iommu_group_for_each_dev(group, &pdev, dev_has_iommu_table);
  1235. if (!ret || !pdev)
  1236. return NULL;
  1237. /* No EEH device or PE ? */
  1238. edev = pci_dev_to_eeh_dev(pdev);
  1239. if (!edev || !edev->pe)
  1240. return NULL;
  1241. return edev->pe;
  1242. }
  1243. EXPORT_SYMBOL_GPL(eeh_iommu_group_to_pe);
  1244. #endif /* CONFIG_IOMMU_API */
  1245. /**
  1246. * eeh_pe_set_option - Set options for the indicated PE
  1247. * @pe: EEH PE
  1248. * @option: requested option
  1249. *
  1250. * The routine is called to enable or disable EEH functionality
  1251. * on the indicated PE, to enable IO or DMA for the frozen PE.
  1252. */
  1253. int eeh_pe_set_option(struct eeh_pe *pe, int option)
  1254. {
  1255. int ret = 0;
  1256. /* Invalid PE ? */
  1257. if (!pe)
  1258. return -ENODEV;
  1259. /*
  1260. * EEH functionality could possibly be disabled, just
  1261. * return error for the case. And the EEH functinality
  1262. * isn't expected to be disabled on one specific PE.
  1263. */
  1264. switch (option) {
  1265. case EEH_OPT_ENABLE:
  1266. if (eeh_enabled()) {
  1267. ret = eeh_pe_change_owner(pe);
  1268. break;
  1269. }
  1270. ret = -EIO;
  1271. break;
  1272. case EEH_OPT_DISABLE:
  1273. break;
  1274. case EEH_OPT_THAW_MMIO:
  1275. case EEH_OPT_THAW_DMA:
  1276. if (!eeh_ops || !eeh_ops->set_option) {
  1277. ret = -ENOENT;
  1278. break;
  1279. }
  1280. ret = eeh_pci_enable(pe, option);
  1281. break;
  1282. default:
  1283. pr_debug("%s: Option %d out of range (%d, %d)\n",
  1284. __func__, option, EEH_OPT_DISABLE, EEH_OPT_THAW_DMA);
  1285. ret = -EINVAL;
  1286. }
  1287. return ret;
  1288. }
  1289. EXPORT_SYMBOL_GPL(eeh_pe_set_option);
  1290. /**
  1291. * eeh_pe_get_state - Retrieve PE's state
  1292. * @pe: EEH PE
  1293. *
  1294. * Retrieve the PE's state, which includes 3 aspects: enabled
  1295. * DMA, enabled IO and asserted reset.
  1296. */
  1297. int eeh_pe_get_state(struct eeh_pe *pe)
  1298. {
  1299. int result, ret = 0;
  1300. bool rst_active, dma_en, mmio_en;
  1301. /* Existing PE ? */
  1302. if (!pe)
  1303. return -ENODEV;
  1304. if (!eeh_ops || !eeh_ops->get_state)
  1305. return -ENOENT;
  1306. result = eeh_ops->get_state(pe, NULL);
  1307. rst_active = !!(result & EEH_STATE_RESET_ACTIVE);
  1308. dma_en = !!(result & EEH_STATE_DMA_ENABLED);
  1309. mmio_en = !!(result & EEH_STATE_MMIO_ENABLED);
  1310. if (rst_active)
  1311. ret = EEH_PE_STATE_RESET;
  1312. else if (dma_en && mmio_en)
  1313. ret = EEH_PE_STATE_NORMAL;
  1314. else if (!dma_en && !mmio_en)
  1315. ret = EEH_PE_STATE_STOPPED_IO_DMA;
  1316. else if (!dma_en && mmio_en)
  1317. ret = EEH_PE_STATE_STOPPED_DMA;
  1318. else
  1319. ret = EEH_PE_STATE_UNAVAIL;
  1320. return ret;
  1321. }
  1322. EXPORT_SYMBOL_GPL(eeh_pe_get_state);
  1323. static int eeh_pe_reenable_devices(struct eeh_pe *pe)
  1324. {
  1325. struct eeh_dev *edev, *tmp;
  1326. struct pci_dev *pdev;
  1327. int ret = 0;
  1328. /* Restore config space */
  1329. eeh_pe_restore_bars(pe);
  1330. /*
  1331. * Reenable PCI devices as the devices passed
  1332. * through are always enabled before the reset.
  1333. */
  1334. eeh_pe_for_each_dev(pe, edev, tmp) {
  1335. pdev = eeh_dev_to_pci_dev(edev);
  1336. if (!pdev)
  1337. continue;
  1338. ret = pci_reenable_device(pdev);
  1339. if (ret) {
  1340. pr_warn("%s: Failure %d reenabling %s\n",
  1341. __func__, ret, pci_name(pdev));
  1342. return ret;
  1343. }
  1344. }
  1345. /* The PE is still in frozen state */
  1346. return eeh_unfreeze_pe(pe, true);
  1347. }
  1348. /**
  1349. * eeh_pe_reset - Issue PE reset according to specified type
  1350. * @pe: EEH PE
  1351. * @option: reset type
  1352. *
  1353. * The routine is called to reset the specified PE with the
  1354. * indicated type, either fundamental reset or hot reset.
  1355. * PE reset is the most important part for error recovery.
  1356. */
  1357. int eeh_pe_reset(struct eeh_pe *pe, int option)
  1358. {
  1359. int ret = 0;
  1360. /* Invalid PE ? */
  1361. if (!pe)
  1362. return -ENODEV;
  1363. if (!eeh_ops || !eeh_ops->set_option || !eeh_ops->reset)
  1364. return -ENOENT;
  1365. switch (option) {
  1366. case EEH_RESET_DEACTIVATE:
  1367. ret = eeh_ops->reset(pe, option);
  1368. eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED);
  1369. if (ret)
  1370. break;
  1371. ret = eeh_pe_reenable_devices(pe);
  1372. break;
  1373. case EEH_RESET_HOT:
  1374. case EEH_RESET_FUNDAMENTAL:
  1375. /*
  1376. * Proactively freeze the PE to drop all MMIO access
  1377. * during reset, which should be banned as it's always
  1378. * cause recursive EEH error.
  1379. */
  1380. eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
  1381. eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
  1382. ret = eeh_ops->reset(pe, option);
  1383. break;
  1384. default:
  1385. pr_debug("%s: Unsupported option %d\n",
  1386. __func__, option);
  1387. ret = -EINVAL;
  1388. }
  1389. return ret;
  1390. }
  1391. EXPORT_SYMBOL_GPL(eeh_pe_reset);
  1392. /**
  1393. * eeh_pe_configure - Configure PCI bridges after PE reset
  1394. * @pe: EEH PE
  1395. *
  1396. * The routine is called to restore the PCI config space for
  1397. * those PCI devices, especially PCI bridges affected by PE
  1398. * reset issued previously.
  1399. */
  1400. int eeh_pe_configure(struct eeh_pe *pe)
  1401. {
  1402. int ret = 0;
  1403. /* Invalid PE ? */
  1404. if (!pe)
  1405. return -ENODEV;
  1406. return ret;
  1407. }
  1408. EXPORT_SYMBOL_GPL(eeh_pe_configure);
  1409. /**
  1410. * eeh_pe_inject_err - Injecting the specified PCI error to the indicated PE
  1411. * @pe: the indicated PE
  1412. * @type: error type
  1413. * @function: error function
  1414. * @addr: address
  1415. * @mask: address mask
  1416. *
  1417. * The routine is called to inject the specified PCI error, which
  1418. * is determined by @type and @function, to the indicated PE for
  1419. * testing purpose.
  1420. */
  1421. int eeh_pe_inject_err(struct eeh_pe *pe, int type, int func,
  1422. unsigned long addr, unsigned long mask)
  1423. {
  1424. /* Invalid PE ? */
  1425. if (!pe)
  1426. return -ENODEV;
  1427. /* Unsupported operation ? */
  1428. if (!eeh_ops || !eeh_ops->err_inject)
  1429. return -ENOENT;
  1430. /* Check on PCI error type */
  1431. if (type != EEH_ERR_TYPE_32 && type != EEH_ERR_TYPE_64)
  1432. return -EINVAL;
  1433. /* Check on PCI error function */
  1434. if (func < EEH_ERR_FUNC_MIN || func > EEH_ERR_FUNC_MAX)
  1435. return -EINVAL;
  1436. return eeh_ops->err_inject(pe, type, func, addr, mask);
  1437. }
  1438. EXPORT_SYMBOL_GPL(eeh_pe_inject_err);
  1439. static int proc_eeh_show(struct seq_file *m, void *v)
  1440. {
  1441. if (!eeh_enabled()) {
  1442. seq_printf(m, "EEH Subsystem is globally disabled\n");
  1443. seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
  1444. } else {
  1445. seq_printf(m, "EEH Subsystem is enabled\n");
  1446. seq_printf(m,
  1447. "no device=%llu\n"
  1448. "no device node=%llu\n"
  1449. "no config address=%llu\n"
  1450. "check not wanted=%llu\n"
  1451. "eeh_total_mmio_ffs=%llu\n"
  1452. "eeh_false_positives=%llu\n"
  1453. "eeh_slot_resets=%llu\n",
  1454. eeh_stats.no_device,
  1455. eeh_stats.no_dn,
  1456. eeh_stats.no_cfg_addr,
  1457. eeh_stats.ignored_check,
  1458. eeh_stats.total_mmio_ffs,
  1459. eeh_stats.false_positives,
  1460. eeh_stats.slot_resets);
  1461. }
  1462. return 0;
  1463. }
  1464. static int proc_eeh_open(struct inode *inode, struct file *file)
  1465. {
  1466. return single_open(file, proc_eeh_show, NULL);
  1467. }
  1468. static const struct file_operations proc_eeh_operations = {
  1469. .open = proc_eeh_open,
  1470. .read = seq_read,
  1471. .llseek = seq_lseek,
  1472. .release = single_release,
  1473. };
  1474. #ifdef CONFIG_DEBUG_FS
  1475. static int eeh_enable_dbgfs_set(void *data, u64 val)
  1476. {
  1477. if (val)
  1478. eeh_clear_flag(EEH_FORCE_DISABLED);
  1479. else
  1480. eeh_add_flag(EEH_FORCE_DISABLED);
  1481. /* Notify the backend */
  1482. if (eeh_ops->post_init)
  1483. eeh_ops->post_init();
  1484. return 0;
  1485. }
  1486. static int eeh_enable_dbgfs_get(void *data, u64 *val)
  1487. {
  1488. if (eeh_enabled())
  1489. *val = 0x1ul;
  1490. else
  1491. *val = 0x0ul;
  1492. return 0;
  1493. }
  1494. static int eeh_freeze_dbgfs_set(void *data, u64 val)
  1495. {
  1496. eeh_max_freezes = val;
  1497. return 0;
  1498. }
  1499. static int eeh_freeze_dbgfs_get(void *data, u64 *val)
  1500. {
  1501. *val = eeh_max_freezes;
  1502. return 0;
  1503. }
  1504. DEFINE_SIMPLE_ATTRIBUTE(eeh_enable_dbgfs_ops, eeh_enable_dbgfs_get,
  1505. eeh_enable_dbgfs_set, "0x%llx\n");
  1506. DEFINE_SIMPLE_ATTRIBUTE(eeh_freeze_dbgfs_ops, eeh_freeze_dbgfs_get,
  1507. eeh_freeze_dbgfs_set, "0x%llx\n");
  1508. #endif
  1509. static int __init eeh_init_proc(void)
  1510. {
  1511. if (machine_is(pseries) || machine_is(powernv)) {
  1512. proc_create("powerpc/eeh", 0, NULL, &proc_eeh_operations);
  1513. #ifdef CONFIG_DEBUG_FS
  1514. debugfs_create_file("eeh_enable", 0600,
  1515. powerpc_debugfs_root, NULL,
  1516. &eeh_enable_dbgfs_ops);
  1517. debugfs_create_file("eeh_max_freezes", 0600,
  1518. powerpc_debugfs_root, NULL,
  1519. &eeh_freeze_dbgfs_ops);
  1520. #endif
  1521. }
  1522. return 0;
  1523. }
  1524. __initcall(eeh_init_proc);