eeh.c 47 KB

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