eeh.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright IBM Corporation 2001, 2005, 2006
  4. * Copyright Dave Engebretsen & Todd Inglett 2001
  5. * Copyright Linas Vepstas 2005, 2006
  6. * Copyright 2001-2012 IBM Corporation.
  7. *
  8. * Please address comments and feedback to Linas Vepstas <linas@austin.ibm.com>
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/sched.h>
  12. #include <linux/init.h>
  13. #include <linux/list.h>
  14. #include <linux/pci.h>
  15. #include <linux/iommu.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/rbtree.h>
  18. #include <linux/reboot.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/export.h>
  22. #include <linux/of.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/atomic.h>
  25. #include <asm/eeh.h>
  26. #include <asm/eeh_event.h>
  27. #include <asm/io.h>
  28. #include <asm/iommu.h>
  29. #include <asm/machdep.h>
  30. #include <asm/ppc-pci.h>
  31. #include <asm/rtas.h>
  32. #include <asm/pte-walk.h>
  33. /** Overview:
  34. * EEH, or "Enhanced Error Handling" is a PCI bridge technology for
  35. * dealing with PCI bus errors that can't be dealt with within the
  36. * usual PCI framework, except by check-stopping the CPU. Systems
  37. * that are designed for high-availability/reliability cannot afford
  38. * to crash due to a "mere" PCI error, thus the need for EEH.
  39. * An EEH-capable bridge operates by converting a detected error
  40. * into a "slot freeze", taking the PCI adapter off-line, making
  41. * the slot behave, from the OS'es point of view, as if the slot
  42. * were "empty": all reads return 0xff's and all writes are silently
  43. * ignored. EEH slot isolation events can be triggered by parity
  44. * errors on the address or data busses (e.g. during posted writes),
  45. * which in turn might be caused by low voltage on the bus, dust,
  46. * vibration, humidity, radioactivity or plain-old failed hardware.
  47. *
  48. * Note, however, that one of the leading causes of EEH slot
  49. * freeze events are buggy device drivers, buggy device microcode,
  50. * or buggy device hardware. This is because any attempt by the
  51. * device to bus-master data to a memory address that is not
  52. * assigned to the device will trigger a slot freeze. (The idea
  53. * is to prevent devices-gone-wild from corrupting system memory).
  54. * Buggy hardware/drivers will have a miserable time co-existing
  55. * with EEH.
  56. *
  57. * Ideally, a PCI device driver, when suspecting that an isolation
  58. * event has occurred (e.g. by reading 0xff's), will then ask EEH
  59. * whether this is the case, and then take appropriate steps to
  60. * reset the PCI slot, the PCI device, and then resume operations.
  61. * However, until that day, the checking is done here, with the
  62. * eeh_check_failure() routine embedded in the MMIO macros. If
  63. * the slot is found to be isolated, an "EEH Event" is synthesized
  64. * and sent out for processing.
  65. */
  66. /* If a device driver keeps reading an MMIO register in an interrupt
  67. * handler after a slot isolation event, it might be broken.
  68. * This sets the threshold for how many read attempts we allow
  69. * before printing an error message.
  70. */
  71. #define EEH_MAX_FAILS 2100000
  72. /* Time to wait for a PCI slot to report status, in milliseconds */
  73. #define PCI_BUS_RESET_WAIT_MSEC (5*60*1000)
  74. /*
  75. * EEH probe mode support, which is part of the flags,
  76. * is to support multiple platforms for EEH. Some platforms
  77. * like pSeries do PCI emunation based on device tree.
  78. * However, other platforms like powernv probe PCI devices
  79. * from hardware. The flag is used to distinguish that.
  80. * In addition, struct eeh_ops::probe would be invoked for
  81. * particular OF node or PCI device so that the corresponding
  82. * PE would be created there.
  83. */
  84. int eeh_subsystem_flags;
  85. EXPORT_SYMBOL(eeh_subsystem_flags);
  86. /*
  87. * EEH allowed maximal frozen times. If one particular PE's
  88. * frozen count in last hour exceeds this limit, the PE will
  89. * be forced to be offline permanently.
  90. */
  91. u32 eeh_max_freezes = 5;
  92. /*
  93. * Controls whether a recovery event should be scheduled when an
  94. * isolated device is discovered. This is only really useful for
  95. * debugging problems with the EEH core.
  96. */
  97. bool eeh_debugfs_no_recover;
  98. /* Platform dependent EEH operations */
  99. struct eeh_ops *eeh_ops = NULL;
  100. /* Lock to avoid races due to multiple reports of an error */
  101. DEFINE_RAW_SPINLOCK(confirm_error_lock);
  102. EXPORT_SYMBOL_GPL(confirm_error_lock);
  103. /* Lock to protect passed flags */
  104. static DEFINE_MUTEX(eeh_dev_mutex);
  105. /* Buffer for reporting pci register dumps. Its here in BSS, and
  106. * not dynamically alloced, so that it ends up in RMO where RTAS
  107. * can access it.
  108. */
  109. #define EEH_PCI_REGS_LOG_LEN 8192
  110. static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];
  111. /*
  112. * The struct is used to maintain the EEH global statistic
  113. * information. Besides, the EEH global statistics will be
  114. * exported to user space through procfs
  115. */
  116. struct eeh_stats {
  117. u64 no_device; /* PCI device not found */
  118. u64 no_dn; /* OF node not found */
  119. u64 no_cfg_addr; /* Config address not found */
  120. u64 ignored_check; /* EEH check skipped */
  121. u64 total_mmio_ffs; /* Total EEH checks */
  122. u64 false_positives; /* Unnecessary EEH checks */
  123. u64 slot_resets; /* PE reset */
  124. };
  125. static struct eeh_stats eeh_stats;
  126. static int __init eeh_setup(char *str)
  127. {
  128. if (!strcmp(str, "off"))
  129. eeh_add_flag(EEH_FORCE_DISABLED);
  130. else if (!strcmp(str, "early_log"))
  131. eeh_add_flag(EEH_EARLY_DUMP_LOG);
  132. return 1;
  133. }
  134. __setup("eeh=", eeh_setup);
  135. void eeh_show_enabled(void)
  136. {
  137. if (eeh_has_flag(EEH_FORCE_DISABLED))
  138. pr_info("EEH: Recovery disabled by kernel parameter.\n");
  139. else if (eeh_has_flag(EEH_ENABLED))
  140. pr_info("EEH: Capable adapter found: recovery enabled.\n");
  141. else
  142. pr_info("EEH: No capable adapters found: recovery disabled.\n");
  143. }
  144. /*
  145. * This routine captures assorted PCI configuration space data
  146. * for the indicated PCI device, and puts them into a buffer
  147. * for RTAS error logging.
  148. */
  149. static size_t eeh_dump_dev_log(struct eeh_dev *edev, char *buf, size_t len)
  150. {
  151. u32 cfg;
  152. int cap, i;
  153. int n = 0, l = 0;
  154. char buffer[128];
  155. n += scnprintf(buf+n, len-n, "%04x:%02x:%02x.%01x\n",
  156. edev->pe->phb->global_number, edev->bdfn >> 8,
  157. PCI_SLOT(edev->bdfn), PCI_FUNC(edev->bdfn));
  158. pr_warn("EEH: of node=%04x:%02x:%02x.%01x\n",
  159. edev->pe->phb->global_number, edev->bdfn >> 8,
  160. PCI_SLOT(edev->bdfn), PCI_FUNC(edev->bdfn));
  161. eeh_ops->read_config(edev, PCI_VENDOR_ID, 4, &cfg);
  162. n += scnprintf(buf+n, len-n, "dev/vend:%08x\n", cfg);
  163. pr_warn("EEH: PCI device/vendor: %08x\n", cfg);
  164. eeh_ops->read_config(edev, PCI_COMMAND, 4, &cfg);
  165. n += scnprintf(buf+n, len-n, "cmd/stat:%x\n", cfg);
  166. pr_warn("EEH: PCI cmd/status register: %08x\n", cfg);
  167. /* Gather bridge-specific registers */
  168. if (edev->mode & EEH_DEV_BRIDGE) {
  169. eeh_ops->read_config(edev, PCI_SEC_STATUS, 2, &cfg);
  170. n += scnprintf(buf+n, len-n, "sec stat:%x\n", cfg);
  171. pr_warn("EEH: Bridge secondary status: %04x\n", cfg);
  172. eeh_ops->read_config(edev, PCI_BRIDGE_CONTROL, 2, &cfg);
  173. n += scnprintf(buf+n, len-n, "brdg ctl:%x\n", cfg);
  174. pr_warn("EEH: Bridge control: %04x\n", cfg);
  175. }
  176. /* Dump out the PCI-X command and status regs */
  177. cap = edev->pcix_cap;
  178. if (cap) {
  179. eeh_ops->read_config(edev, cap, 4, &cfg);
  180. n += scnprintf(buf+n, len-n, "pcix-cmd:%x\n", cfg);
  181. pr_warn("EEH: PCI-X cmd: %08x\n", cfg);
  182. eeh_ops->read_config(edev, cap+4, 4, &cfg);
  183. n += scnprintf(buf+n, len-n, "pcix-stat:%x\n", cfg);
  184. pr_warn("EEH: PCI-X status: %08x\n", cfg);
  185. }
  186. /* If PCI-E capable, dump PCI-E cap 10 */
  187. cap = edev->pcie_cap;
  188. if (cap) {
  189. n += scnprintf(buf+n, len-n, "pci-e cap10:\n");
  190. pr_warn("EEH: PCI-E capabilities and status follow:\n");
  191. for (i=0; i<=8; i++) {
  192. eeh_ops->read_config(edev, cap+4*i, 4, &cfg);
  193. n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
  194. if ((i % 4) == 0) {
  195. if (i != 0)
  196. pr_warn("%s\n", buffer);
  197. l = scnprintf(buffer, sizeof(buffer),
  198. "EEH: PCI-E %02x: %08x ",
  199. 4*i, cfg);
  200. } else {
  201. l += scnprintf(buffer+l, sizeof(buffer)-l,
  202. "%08x ", cfg);
  203. }
  204. }
  205. pr_warn("%s\n", buffer);
  206. }
  207. /* If AER capable, dump it */
  208. cap = edev->aer_cap;
  209. if (cap) {
  210. n += scnprintf(buf+n, len-n, "pci-e AER:\n");
  211. pr_warn("EEH: PCI-E AER capability register set follows:\n");
  212. for (i=0; i<=13; i++) {
  213. eeh_ops->read_config(edev, cap+4*i, 4, &cfg);
  214. n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
  215. if ((i % 4) == 0) {
  216. if (i != 0)
  217. pr_warn("%s\n", buffer);
  218. l = scnprintf(buffer, sizeof(buffer),
  219. "EEH: PCI-E AER %02x: %08x ",
  220. 4*i, cfg);
  221. } else {
  222. l += scnprintf(buffer+l, sizeof(buffer)-l,
  223. "%08x ", cfg);
  224. }
  225. }
  226. pr_warn("%s\n", buffer);
  227. }
  228. return n;
  229. }
  230. static void *eeh_dump_pe_log(struct eeh_pe *pe, void *flag)
  231. {
  232. struct eeh_dev *edev, *tmp;
  233. size_t *plen = flag;
  234. eeh_pe_for_each_dev(pe, edev, tmp)
  235. *plen += eeh_dump_dev_log(edev, pci_regs_buf + *plen,
  236. EEH_PCI_REGS_LOG_LEN - *plen);
  237. return NULL;
  238. }
  239. /**
  240. * eeh_slot_error_detail - Generate combined log including driver log and error log
  241. * @pe: EEH PE
  242. * @severity: temporary or permanent error log
  243. *
  244. * This routine should be called to generate the combined log, which
  245. * is comprised of driver log and error log. The driver log is figured
  246. * out from the config space of the corresponding PCI device, while
  247. * the error log is fetched through platform dependent function call.
  248. */
  249. void eeh_slot_error_detail(struct eeh_pe *pe, int severity)
  250. {
  251. size_t loglen = 0;
  252. /*
  253. * When the PHB is fenced or dead, it's pointless to collect
  254. * the data from PCI config space because it should return
  255. * 0xFF's. For ER, we still retrieve the data from the PCI
  256. * config space.
  257. *
  258. * For pHyp, we have to enable IO for log retrieval. Otherwise,
  259. * 0xFF's is always returned from PCI config space.
  260. *
  261. * When the @severity is EEH_LOG_PERM, the PE is going to be
  262. * removed. Prior to that, the drivers for devices included in
  263. * the PE will be closed. The drivers rely on working IO path
  264. * to bring the devices to quiet state. Otherwise, PCI traffic
  265. * from those devices after they are removed is like to cause
  266. * another unexpected EEH error.
  267. */
  268. if (!(pe->type & EEH_PE_PHB)) {
  269. if (eeh_has_flag(EEH_ENABLE_IO_FOR_LOG) ||
  270. severity == EEH_LOG_PERM)
  271. eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
  272. /*
  273. * The config space of some PCI devices can't be accessed
  274. * when their PEs are in frozen state. Otherwise, fenced
  275. * PHB might be seen. Those PEs are identified with flag
  276. * EEH_PE_CFG_RESTRICTED, indicating EEH_PE_CFG_BLOCKED
  277. * is set automatically when the PE is put to EEH_PE_ISOLATED.
  278. *
  279. * Restoring BARs possibly triggers PCI config access in
  280. * (OPAL) firmware and then causes fenced PHB. If the
  281. * PCI config is blocked with flag EEH_PE_CFG_BLOCKED, it's
  282. * pointless to restore BARs and dump config space.
  283. */
  284. eeh_ops->configure_bridge(pe);
  285. if (!(pe->state & EEH_PE_CFG_BLOCKED)) {
  286. eeh_pe_restore_bars(pe);
  287. pci_regs_buf[0] = 0;
  288. eeh_pe_traverse(pe, eeh_dump_pe_log, &loglen);
  289. }
  290. }
  291. eeh_ops->get_log(pe, severity, pci_regs_buf, loglen);
  292. }
  293. /**
  294. * eeh_token_to_phys - Convert EEH address token to phys address
  295. * @token: I/O token, should be address in the form 0xA....
  296. *
  297. * This routine should be called to convert virtual I/O address
  298. * to physical one.
  299. */
  300. static inline unsigned long eeh_token_to_phys(unsigned long token)
  301. {
  302. return ppc_find_vmap_phys(token);
  303. }
  304. /*
  305. * On PowerNV platform, we might already have fenced PHB there.
  306. * For that case, it's meaningless to recover frozen PE. Intead,
  307. * We have to handle fenced PHB firstly.
  308. */
  309. static int eeh_phb_check_failure(struct eeh_pe *pe)
  310. {
  311. struct eeh_pe *phb_pe;
  312. unsigned long flags;
  313. int ret;
  314. if (!eeh_has_flag(EEH_PROBE_MODE_DEV))
  315. return -EPERM;
  316. /* Find the PHB PE */
  317. phb_pe = eeh_phb_pe_get(pe->phb);
  318. if (!phb_pe) {
  319. pr_warn("%s Can't find PE for PHB#%x\n",
  320. __func__, pe->phb->global_number);
  321. return -EEXIST;
  322. }
  323. /* If the PHB has been in problematic state */
  324. eeh_serialize_lock(&flags);
  325. if (phb_pe->state & EEH_PE_ISOLATED) {
  326. ret = 0;
  327. goto out;
  328. }
  329. /* Check PHB state */
  330. ret = eeh_ops->get_state(phb_pe, NULL);
  331. if ((ret < 0) ||
  332. (ret == EEH_STATE_NOT_SUPPORT) || eeh_state_active(ret)) {
  333. ret = 0;
  334. goto out;
  335. }
  336. /* Isolate the PHB and send event */
  337. eeh_pe_mark_isolated(phb_pe);
  338. eeh_serialize_unlock(flags);
  339. pr_debug("EEH: PHB#%x failure detected, location: %s\n",
  340. phb_pe->phb->global_number, eeh_pe_loc_get(phb_pe));
  341. eeh_send_failure_event(phb_pe);
  342. return 1;
  343. out:
  344. eeh_serialize_unlock(flags);
  345. return ret;
  346. }
  347. static inline const char *eeh_driver_name(struct pci_dev *pdev)
  348. {
  349. if (pdev)
  350. return dev_driver_string(&pdev->dev);
  351. return "<null>";
  352. }
  353. /**
  354. * eeh_dev_check_failure - Check if all 1's data is due to EEH slot freeze
  355. * @edev: eeh device
  356. *
  357. * Check for an EEH failure for the given device node. Call this
  358. * routine if the result of a read was all 0xff's and you want to
  359. * find out if this is due to an EEH slot freeze. This routine
  360. * will query firmware for the EEH status.
  361. *
  362. * Returns 0 if there has not been an EEH error; otherwise returns
  363. * a non-zero value and queues up a slot isolation event notification.
  364. *
  365. * It is safe to call this routine in an interrupt context.
  366. */
  367. int eeh_dev_check_failure(struct eeh_dev *edev)
  368. {
  369. int ret;
  370. unsigned long flags;
  371. struct device_node *dn;
  372. struct pci_dev *dev;
  373. struct eeh_pe *pe, *parent_pe;
  374. int rc = 0;
  375. const char *location = NULL;
  376. eeh_stats.total_mmio_ffs++;
  377. if (!eeh_enabled())
  378. return 0;
  379. if (!edev) {
  380. eeh_stats.no_dn++;
  381. return 0;
  382. }
  383. dev = eeh_dev_to_pci_dev(edev);
  384. pe = eeh_dev_to_pe(edev);
  385. /* Access to IO BARs might get this far and still not want checking. */
  386. if (!pe) {
  387. eeh_stats.ignored_check++;
  388. eeh_edev_dbg(edev, "Ignored check\n");
  389. return 0;
  390. }
  391. /*
  392. * On PowerNV platform, we might already have fenced PHB
  393. * there and we need take care of that firstly.
  394. */
  395. ret = eeh_phb_check_failure(pe);
  396. if (ret > 0)
  397. return ret;
  398. /*
  399. * If the PE isn't owned by us, we shouldn't check the
  400. * state. Instead, let the owner handle it if the PE has
  401. * been frozen.
  402. */
  403. if (eeh_pe_passed(pe))
  404. return 0;
  405. /* If we already have a pending isolation event for this
  406. * slot, we know it's bad already, we don't need to check.
  407. * Do this checking under a lock; as multiple PCI devices
  408. * in one slot might report errors simultaneously, and we
  409. * only want one error recovery routine running.
  410. */
  411. eeh_serialize_lock(&flags);
  412. rc = 1;
  413. if (pe->state & EEH_PE_ISOLATED) {
  414. pe->check_count++;
  415. if (pe->check_count == EEH_MAX_FAILS) {
  416. dn = pci_device_to_OF_node(dev);
  417. if (dn)
  418. location = of_get_property(dn, "ibm,loc-code",
  419. NULL);
  420. eeh_edev_err(edev, "%d reads ignored for recovering device at location=%s driver=%s\n",
  421. pe->check_count,
  422. location ? location : "unknown",
  423. eeh_driver_name(dev));
  424. eeh_edev_err(edev, "Might be infinite loop in %s driver\n",
  425. eeh_driver_name(dev));
  426. dump_stack();
  427. }
  428. goto dn_unlock;
  429. }
  430. /*
  431. * Now test for an EEH failure. This is VERY expensive.
  432. * Note that the eeh_config_addr may be a parent device
  433. * in the case of a device behind a bridge, or it may be
  434. * function zero of a multi-function device.
  435. * In any case they must share a common PHB.
  436. */
  437. ret = eeh_ops->get_state(pe, NULL);
  438. /* Note that config-io to empty slots may fail;
  439. * they are empty when they don't have children.
  440. * We will punt with the following conditions: Failure to get
  441. * PE's state, EEH not support and Permanently unavailable
  442. * state, PE is in good state.
  443. *
  444. * On the pSeries, after reaching the threshold, get_state might
  445. * return EEH_STATE_NOT_SUPPORT. However, it's possible that the
  446. * device state remains uncleared if the device is not marked
  447. * pci_channel_io_perm_failure. Therefore, consider logging the
  448. * event to let device removal happen.
  449. *
  450. */
  451. if ((ret < 0) ||
  452. (ret == EEH_STATE_NOT_SUPPORT &&
  453. dev->error_state == pci_channel_io_perm_failure) ||
  454. eeh_state_active(ret)) {
  455. eeh_stats.false_positives++;
  456. pe->false_positives++;
  457. rc = 0;
  458. goto dn_unlock;
  459. }
  460. /*
  461. * It should be corner case that the parent PE has been
  462. * put into frozen state as well. We should take care
  463. * that at first.
  464. */
  465. parent_pe = pe->parent;
  466. while (parent_pe) {
  467. /* Hit the ceiling ? */
  468. if (parent_pe->type & EEH_PE_PHB)
  469. break;
  470. /* Frozen parent PE ? */
  471. ret = eeh_ops->get_state(parent_pe, NULL);
  472. if (ret > 0 && !eeh_state_active(ret)) {
  473. pe = parent_pe;
  474. pr_err("EEH: Failure of PHB#%x-PE#%x will be handled at parent PHB#%x-PE#%x.\n",
  475. pe->phb->global_number, pe->addr,
  476. pe->phb->global_number, parent_pe->addr);
  477. }
  478. /* Next parent level */
  479. parent_pe = parent_pe->parent;
  480. }
  481. eeh_stats.slot_resets++;
  482. /* Avoid repeated reports of this failure, including problems
  483. * with other functions on this device, and functions under
  484. * bridges.
  485. */
  486. eeh_pe_mark_isolated(pe);
  487. eeh_serialize_unlock(flags);
  488. /* Most EEH events are due to device driver bugs. Having
  489. * a stack trace will help the device-driver authors figure
  490. * out what happened. So print that out.
  491. */
  492. pr_debug("EEH: %s: Frozen PHB#%x-PE#%x detected\n",
  493. __func__, pe->phb->global_number, pe->addr);
  494. eeh_send_failure_event(pe);
  495. return 1;
  496. dn_unlock:
  497. eeh_serialize_unlock(flags);
  498. return rc;
  499. }
  500. EXPORT_SYMBOL_GPL(eeh_dev_check_failure);
  501. /**
  502. * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
  503. * @token: I/O address
  504. *
  505. * Check for an EEH failure at the given I/O address. Call this
  506. * routine if the result of a read was all 0xff's and you want to
  507. * find out if this is due to an EEH slot freeze event. This routine
  508. * will query firmware for the EEH status.
  509. *
  510. * Note this routine is safe to call in an interrupt context.
  511. */
  512. int eeh_check_failure(const volatile void __iomem *token)
  513. {
  514. unsigned long addr;
  515. struct eeh_dev *edev;
  516. /* Finding the phys addr + pci device; this is pretty quick. */
  517. addr = eeh_token_to_phys((unsigned long __force) token);
  518. edev = eeh_addr_cache_get_dev(addr);
  519. if (!edev) {
  520. eeh_stats.no_device++;
  521. return 0;
  522. }
  523. return eeh_dev_check_failure(edev);
  524. }
  525. EXPORT_SYMBOL(eeh_check_failure);
  526. /**
  527. * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
  528. * @pe: EEH PE
  529. * @function: EEH option
  530. *
  531. * This routine should be called to reenable frozen MMIO or DMA
  532. * so that it would work correctly again. It's useful while doing
  533. * recovery or log collection on the indicated device.
  534. */
  535. int eeh_pci_enable(struct eeh_pe *pe, int function)
  536. {
  537. int active_flag, rc;
  538. /*
  539. * pHyp doesn't allow to enable IO or DMA on unfrozen PE.
  540. * Also, it's pointless to enable them on unfrozen PE. So
  541. * we have to check before enabling IO or DMA.
  542. */
  543. switch (function) {
  544. case EEH_OPT_THAW_MMIO:
  545. active_flag = EEH_STATE_MMIO_ACTIVE | EEH_STATE_MMIO_ENABLED;
  546. break;
  547. case EEH_OPT_THAW_DMA:
  548. active_flag = EEH_STATE_DMA_ACTIVE;
  549. break;
  550. case EEH_OPT_DISABLE:
  551. case EEH_OPT_ENABLE:
  552. case EEH_OPT_FREEZE_PE:
  553. active_flag = 0;
  554. break;
  555. default:
  556. pr_warn("%s: Invalid function %d\n",
  557. __func__, function);
  558. return -EINVAL;
  559. }
  560. /*
  561. * Check if IO or DMA has been enabled before
  562. * enabling them.
  563. */
  564. if (active_flag) {
  565. rc = eeh_ops->get_state(pe, NULL);
  566. if (rc < 0)
  567. return rc;
  568. /* Needn't enable it at all */
  569. if (rc == EEH_STATE_NOT_SUPPORT)
  570. return 0;
  571. /* It's already enabled */
  572. if (rc & active_flag)
  573. return 0;
  574. }
  575. /* Issue the request */
  576. rc = eeh_ops->set_option(pe, function);
  577. if (rc)
  578. pr_warn("%s: Unexpected state change %d on "
  579. "PHB#%x-PE#%x, err=%d\n",
  580. __func__, function, pe->phb->global_number,
  581. pe->addr, rc);
  582. /* Check if the request is finished successfully */
  583. if (active_flag) {
  584. rc = eeh_wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
  585. if (rc < 0)
  586. return rc;
  587. if (rc & active_flag)
  588. return 0;
  589. return -EIO;
  590. }
  591. return rc;
  592. }
  593. static void eeh_disable_and_save_dev_state(struct eeh_dev *edev,
  594. void *userdata)
  595. {
  596. struct pci_dev *pdev = eeh_dev_to_pci_dev(edev);
  597. struct pci_dev *dev = userdata;
  598. /*
  599. * The caller should have disabled and saved the
  600. * state for the specified device
  601. */
  602. if (!pdev || pdev == dev)
  603. return;
  604. /* Ensure we have D0 power state */
  605. pci_set_power_state(pdev, PCI_D0);
  606. /* Save device state */
  607. pci_save_state(pdev);
  608. /*
  609. * Disable device to avoid any DMA traffic and
  610. * interrupt from the device
  611. */
  612. pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
  613. }
  614. static void eeh_restore_dev_state(struct eeh_dev *edev, void *userdata)
  615. {
  616. struct pci_dev *pdev = eeh_dev_to_pci_dev(edev);
  617. struct pci_dev *dev = userdata;
  618. if (!pdev)
  619. return;
  620. /* Apply customization from firmware */
  621. if (eeh_ops->restore_config)
  622. eeh_ops->restore_config(edev);
  623. /* The caller should restore state for the specified device */
  624. if (pdev != dev)
  625. pci_restore_state(pdev);
  626. }
  627. /**
  628. * pcibios_set_pcie_reset_state - Set PCI-E reset state
  629. * @dev: pci device struct
  630. * @state: reset state to enter
  631. *
  632. * Return value:
  633. * 0 if success
  634. */
  635. int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  636. {
  637. struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
  638. struct eeh_pe *pe = eeh_dev_to_pe(edev);
  639. if (!pe) {
  640. pr_err("%s: No PE found on PCI device %s\n",
  641. __func__, pci_name(dev));
  642. return -EINVAL;
  643. }
  644. switch (state) {
  645. case pcie_deassert_reset:
  646. eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
  647. eeh_unfreeze_pe(pe);
  648. if (!(pe->type & EEH_PE_VF))
  649. eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED, true);
  650. eeh_pe_dev_traverse(pe, eeh_restore_dev_state, dev);
  651. eeh_pe_state_clear(pe, EEH_PE_ISOLATED, true);
  652. break;
  653. case pcie_hot_reset:
  654. eeh_pe_mark_isolated(pe);
  655. eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED, true);
  656. eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
  657. eeh_pe_dev_traverse(pe, eeh_disable_and_save_dev_state, dev);
  658. if (!(pe->type & EEH_PE_VF))
  659. eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
  660. eeh_ops->reset(pe, EEH_RESET_HOT);
  661. break;
  662. case pcie_warm_reset:
  663. eeh_pe_mark_isolated(pe);
  664. eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED, true);
  665. eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
  666. eeh_pe_dev_traverse(pe, eeh_disable_and_save_dev_state, dev);
  667. if (!(pe->type & EEH_PE_VF))
  668. eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
  669. eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
  670. break;
  671. default:
  672. eeh_pe_state_clear(pe, EEH_PE_ISOLATED | EEH_PE_CFG_BLOCKED, true);
  673. return -EINVAL;
  674. }
  675. return 0;
  676. }
  677. /**
  678. * eeh_set_dev_freset - Check the required reset for the indicated device
  679. * @edev: EEH device
  680. * @flag: return value
  681. *
  682. * Each device might have its preferred reset type: fundamental or
  683. * hot reset. The routine is used to collected the information for
  684. * the indicated device and its children so that the bunch of the
  685. * devices could be reset properly.
  686. */
  687. static void eeh_set_dev_freset(struct eeh_dev *edev, void *flag)
  688. {
  689. struct pci_dev *dev;
  690. unsigned int *freset = (unsigned int *)flag;
  691. dev = eeh_dev_to_pci_dev(edev);
  692. if (dev)
  693. *freset |= dev->needs_freset;
  694. }
  695. static void eeh_pe_refreeze_passed(struct eeh_pe *root)
  696. {
  697. struct eeh_pe *pe;
  698. int state;
  699. eeh_for_each_pe(root, pe) {
  700. if (eeh_pe_passed(pe)) {
  701. state = eeh_ops->get_state(pe, NULL);
  702. if (state &
  703. (EEH_STATE_MMIO_ACTIVE | EEH_STATE_MMIO_ENABLED)) {
  704. pr_info("EEH: Passed-through PE PHB#%x-PE#%x was thawed by reset, re-freezing for safety.\n",
  705. pe->phb->global_number, pe->addr);
  706. eeh_pe_set_option(pe, EEH_OPT_FREEZE_PE);
  707. }
  708. }
  709. }
  710. }
  711. /**
  712. * eeh_pe_reset_full - Complete a full reset process on the indicated PE
  713. * @pe: EEH PE
  714. * @include_passed: include passed-through devices?
  715. *
  716. * This function executes a full reset procedure on a PE, including setting
  717. * the appropriate flags, performing a fundamental or hot reset, and then
  718. * deactivating the reset status. It is designed to be used within the EEH
  719. * subsystem, as opposed to eeh_pe_reset which is exported to drivers and
  720. * only performs a single operation at a time.
  721. *
  722. * This function will attempt to reset a PE three times before failing.
  723. */
  724. int eeh_pe_reset_full(struct eeh_pe *pe, bool include_passed)
  725. {
  726. int reset_state = (EEH_PE_RESET | EEH_PE_CFG_BLOCKED);
  727. int type = EEH_RESET_HOT;
  728. unsigned int freset = 0;
  729. int i, state = 0, ret;
  730. /*
  731. * Determine the type of reset to perform - hot or fundamental.
  732. * Hot reset is the default operation, unless any device under the
  733. * PE requires a fundamental reset.
  734. */
  735. eeh_pe_dev_traverse(pe, eeh_set_dev_freset, &freset);
  736. if (freset)
  737. type = EEH_RESET_FUNDAMENTAL;
  738. /* Mark the PE as in reset state and block config space accesses */
  739. eeh_pe_state_mark(pe, reset_state);
  740. /* Make three attempts at resetting the bus */
  741. for (i = 0; i < 3; i++) {
  742. ret = eeh_pe_reset(pe, type, include_passed);
  743. if (!ret)
  744. ret = eeh_pe_reset(pe, EEH_RESET_DEACTIVATE,
  745. include_passed);
  746. if (ret) {
  747. ret = -EIO;
  748. pr_warn("EEH: Failure %d resetting PHB#%x-PE#%x (attempt %d)\n\n",
  749. state, pe->phb->global_number, pe->addr, i + 1);
  750. continue;
  751. }
  752. if (i)
  753. pr_warn("EEH: PHB#%x-PE#%x: Successful reset (attempt %d)\n",
  754. pe->phb->global_number, pe->addr, i + 1);
  755. /* Wait until the PE is in a functioning state */
  756. state = eeh_wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
  757. if (state < 0) {
  758. pr_warn("EEH: Unrecoverable slot failure on PHB#%x-PE#%x",
  759. pe->phb->global_number, pe->addr);
  760. ret = -ENOTRECOVERABLE;
  761. break;
  762. }
  763. if (eeh_state_active(state))
  764. break;
  765. else
  766. pr_warn("EEH: PHB#%x-PE#%x: Slot inactive after reset: 0x%x (attempt %d)\n",
  767. pe->phb->global_number, pe->addr, state, i + 1);
  768. }
  769. /* Resetting the PE may have unfrozen child PEs. If those PEs have been
  770. * (potentially) passed through to a guest, re-freeze them:
  771. */
  772. if (!include_passed)
  773. eeh_pe_refreeze_passed(pe);
  774. eeh_pe_state_clear(pe, reset_state, true);
  775. return ret;
  776. }
  777. /**
  778. * eeh_save_bars - Save device bars
  779. * @edev: PCI device associated EEH device
  780. *
  781. * Save the values of the device bars. Unlike the restore
  782. * routine, this routine is *not* recursive. This is because
  783. * PCI devices are added individually; but, for the restore,
  784. * an entire slot is reset at a time.
  785. */
  786. void eeh_save_bars(struct eeh_dev *edev)
  787. {
  788. int i;
  789. if (!edev)
  790. return;
  791. for (i = 0; i < 16; i++)
  792. eeh_ops->read_config(edev, 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. static int eeh_reboot_notifier(struct notifier_block *nb,
  803. unsigned long action, void *unused)
  804. {
  805. eeh_clear_flag(EEH_ENABLED);
  806. return NOTIFY_DONE;
  807. }
  808. static struct notifier_block eeh_reboot_nb = {
  809. .notifier_call = eeh_reboot_notifier,
  810. };
  811. static int eeh_device_notifier(struct notifier_block *nb,
  812. unsigned long action, void *data)
  813. {
  814. struct device *dev = data;
  815. switch (action) {
  816. /*
  817. * Note: It's not possible to perform EEH device addition (i.e.
  818. * {pseries,pnv}_pcibios_bus_add_device()) here because it depends on
  819. * the device's resources, which have not yet been set up.
  820. */
  821. case BUS_NOTIFY_DEL_DEVICE:
  822. eeh_remove_device(to_pci_dev(dev));
  823. break;
  824. default:
  825. break;
  826. }
  827. return NOTIFY_DONE;
  828. }
  829. static struct notifier_block eeh_device_nb = {
  830. .notifier_call = eeh_device_notifier,
  831. };
  832. /**
  833. * eeh_init - System wide EEH initialization
  834. * @ops: struct to trace EEH operation callback functions
  835. *
  836. * It's the platform's job to call this from an arch_initcall().
  837. */
  838. int eeh_init(struct eeh_ops *ops)
  839. {
  840. struct pci_controller *hose, *tmp;
  841. int ret = 0;
  842. /* the platform should only initialise EEH once */
  843. if (WARN_ON(eeh_ops))
  844. return -EEXIST;
  845. if (WARN_ON(!ops))
  846. return -ENOENT;
  847. eeh_ops = ops;
  848. /* Register reboot notifier */
  849. ret = register_reboot_notifier(&eeh_reboot_nb);
  850. if (ret) {
  851. pr_warn("%s: Failed to register reboot notifier (%d)\n",
  852. __func__, ret);
  853. return ret;
  854. }
  855. ret = bus_register_notifier(&pci_bus_type, &eeh_device_nb);
  856. if (ret) {
  857. pr_warn("%s: Failed to register bus notifier (%d)\n",
  858. __func__, ret);
  859. return ret;
  860. }
  861. /* Initialize PHB PEs */
  862. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  863. eeh_phb_pe_create(hose);
  864. eeh_addr_cache_init();
  865. /* Initialize EEH event */
  866. return eeh_event_init();
  867. }
  868. /**
  869. * eeh_probe_device() - Perform EEH initialization for the indicated pci device
  870. * @dev: pci device for which to set up EEH
  871. *
  872. * This routine must be used to complete EEH initialization for PCI
  873. * devices that were added after system boot (e.g. hotplug, dlpar).
  874. */
  875. void eeh_probe_device(struct pci_dev *dev)
  876. {
  877. struct eeh_dev *edev;
  878. pr_debug("EEH: Adding device %s\n", pci_name(dev));
  879. /*
  880. * pci_dev_to_eeh_dev() can only work if eeh_probe_dev() was
  881. * already called for this device.
  882. */
  883. if (WARN_ON_ONCE(pci_dev_to_eeh_dev(dev))) {
  884. pci_dbg(dev, "Already bound to an eeh_dev!\n");
  885. return;
  886. }
  887. edev = eeh_ops->probe(dev);
  888. if (!edev) {
  889. pr_debug("EEH: Adding device failed\n");
  890. return;
  891. }
  892. /*
  893. * FIXME: We rely on pcibios_release_device() to remove the
  894. * existing EEH state. The release function is only called if
  895. * the pci_dev's refcount drops to zero so if something is
  896. * keeping a ref to a device (e.g. a filesystem) we need to
  897. * remove the old EEH state.
  898. *
  899. * FIXME: HEY MA, LOOK AT ME, NO LOCKING!
  900. */
  901. if (edev->pdev && edev->pdev != dev) {
  902. eeh_pe_tree_remove(edev);
  903. eeh_addr_cache_rmv_dev(edev->pdev);
  904. eeh_sysfs_remove_device(edev->pdev);
  905. /*
  906. * We definitely should have the PCI device removed
  907. * though it wasn't correctly. So we needn't call
  908. * into error handler afterwards.
  909. */
  910. edev->mode |= EEH_DEV_NO_HANDLER;
  911. }
  912. /* bind the pdev and the edev together */
  913. edev->pdev = dev;
  914. dev->dev.archdata.edev = edev;
  915. eeh_addr_cache_insert_dev(dev);
  916. eeh_sysfs_add_device(dev);
  917. }
  918. /**
  919. * eeh_remove_device - Undo EEH setup for the indicated pci device
  920. * @dev: pci device to be removed
  921. *
  922. * This routine should be called when a device is removed from
  923. * a running system (e.g. by hotplug or dlpar). It unregisters
  924. * the PCI device from the EEH subsystem. I/O errors affecting
  925. * this device will no longer be detected after this call; thus,
  926. * i/o errors affecting this slot may leave this device unusable.
  927. */
  928. void eeh_remove_device(struct pci_dev *dev)
  929. {
  930. struct eeh_dev *edev;
  931. if (!dev || !eeh_enabled())
  932. return;
  933. edev = pci_dev_to_eeh_dev(dev);
  934. /* Unregister the device with the EEH/PCI address search system */
  935. dev_dbg(&dev->dev, "EEH: Removing device\n");
  936. if (!edev || !edev->pdev || !edev->pe) {
  937. dev_dbg(&dev->dev, "EEH: Device not referenced!\n");
  938. return;
  939. }
  940. /*
  941. * During the hotplug for EEH error recovery, we need the EEH
  942. * device attached to the parent PE in order for BAR restore
  943. * a bit later. So we keep it for BAR restore and remove it
  944. * from the parent PE during the BAR resotre.
  945. */
  946. edev->pdev = NULL;
  947. /*
  948. * eeh_sysfs_remove_device() uses pci_dev_to_eeh_dev() so we need to
  949. * remove the sysfs files before clearing dev.archdata.edev
  950. */
  951. if (edev->mode & EEH_DEV_SYSFS)
  952. eeh_sysfs_remove_device(dev);
  953. /*
  954. * We're removing from the PCI subsystem, that means
  955. * the PCI device driver can't support EEH or not
  956. * well. So we rely on hotplug completely to do recovery
  957. * for the specific PCI device.
  958. */
  959. edev->mode |= EEH_DEV_NO_HANDLER;
  960. eeh_addr_cache_rmv_dev(dev);
  961. /*
  962. * The flag "in_error" is used to trace EEH devices for VFs
  963. * in error state or not. It's set in eeh_report_error(). If
  964. * it's not set, eeh_report_{reset,resume}() won't be called
  965. * for the VF EEH device.
  966. */
  967. edev->in_error = false;
  968. dev->dev.archdata.edev = NULL;
  969. if (!(edev->pe->state & EEH_PE_KEEP))
  970. eeh_pe_tree_remove(edev);
  971. else
  972. edev->mode |= EEH_DEV_DISCONNECTED;
  973. }
  974. int eeh_unfreeze_pe(struct eeh_pe *pe)
  975. {
  976. int ret;
  977. ret = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
  978. if (ret) {
  979. pr_warn("%s: Failure %d enabling IO on PHB#%x-PE#%x\n",
  980. __func__, ret, pe->phb->global_number, pe->addr);
  981. return ret;
  982. }
  983. ret = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
  984. if (ret) {
  985. pr_warn("%s: Failure %d enabling DMA on PHB#%x-PE#%x\n",
  986. __func__, ret, pe->phb->global_number, pe->addr);
  987. return ret;
  988. }
  989. return ret;
  990. }
  991. EXPORT_SYMBOL_GPL(eeh_unfreeze_pe);
  992. static struct pci_device_id eeh_reset_ids[] = {
  993. { PCI_DEVICE(0x19a2, 0x0710) }, /* Emulex, BE */
  994. { PCI_DEVICE(0x10df, 0xe220) }, /* Emulex, Lancer */
  995. { PCI_DEVICE(0x14e4, 0x1657) }, /* Broadcom BCM5719 */
  996. { 0 }
  997. };
  998. static int eeh_pe_change_owner(struct eeh_pe *pe)
  999. {
  1000. struct eeh_dev *edev, *tmp;
  1001. struct pci_dev *pdev;
  1002. struct pci_device_id *id;
  1003. int ret;
  1004. /* Check PE state */
  1005. ret = eeh_ops->get_state(pe, NULL);
  1006. if (ret < 0 || ret == EEH_STATE_NOT_SUPPORT)
  1007. return 0;
  1008. /* Unfrozen PE, nothing to do */
  1009. if (eeh_state_active(ret))
  1010. return 0;
  1011. /* Frozen PE, check if it needs PE level reset */
  1012. eeh_pe_for_each_dev(pe, edev, tmp) {
  1013. pdev = eeh_dev_to_pci_dev(edev);
  1014. if (!pdev)
  1015. continue;
  1016. for (id = &eeh_reset_ids[0]; id->vendor != 0; id++) {
  1017. if (id->vendor != PCI_ANY_ID &&
  1018. id->vendor != pdev->vendor)
  1019. continue;
  1020. if (id->device != PCI_ANY_ID &&
  1021. id->device != pdev->device)
  1022. continue;
  1023. if (id->subvendor != PCI_ANY_ID &&
  1024. id->subvendor != pdev->subsystem_vendor)
  1025. continue;
  1026. if (id->subdevice != PCI_ANY_ID &&
  1027. id->subdevice != pdev->subsystem_device)
  1028. continue;
  1029. return eeh_pe_reset_and_recover(pe);
  1030. }
  1031. }
  1032. ret = eeh_unfreeze_pe(pe);
  1033. if (!ret)
  1034. eeh_pe_state_clear(pe, EEH_PE_ISOLATED, true);
  1035. return ret;
  1036. }
  1037. /**
  1038. * eeh_dev_open - Increase count of pass through devices for PE
  1039. * @pdev: PCI device
  1040. *
  1041. * Increase count of passed through devices for the indicated
  1042. * PE. In the result, the EEH errors detected on the PE won't be
  1043. * reported. The PE owner will be responsible for detection
  1044. * and recovery.
  1045. */
  1046. int eeh_dev_open(struct pci_dev *pdev)
  1047. {
  1048. struct eeh_dev *edev;
  1049. int ret = -ENODEV;
  1050. mutex_lock(&eeh_dev_mutex);
  1051. /* No PCI device ? */
  1052. if (!pdev)
  1053. goto out;
  1054. /* No EEH device or PE ? */
  1055. edev = pci_dev_to_eeh_dev(pdev);
  1056. if (!edev || !edev->pe)
  1057. goto out;
  1058. /*
  1059. * The PE might have been put into frozen state, but we
  1060. * didn't detect that yet. The passed through PCI devices
  1061. * in frozen PE won't work properly. Clear the frozen state
  1062. * in advance.
  1063. */
  1064. ret = eeh_pe_change_owner(edev->pe);
  1065. if (ret)
  1066. goto out;
  1067. /* Increase PE's pass through count */
  1068. atomic_inc(&edev->pe->pass_dev_cnt);
  1069. mutex_unlock(&eeh_dev_mutex);
  1070. return 0;
  1071. out:
  1072. mutex_unlock(&eeh_dev_mutex);
  1073. return ret;
  1074. }
  1075. EXPORT_SYMBOL_GPL(eeh_dev_open);
  1076. /**
  1077. * eeh_dev_release - Decrease count of pass through devices for PE
  1078. * @pdev: PCI device
  1079. *
  1080. * Decrease count of pass through devices for the indicated PE. If
  1081. * there is no passed through device in PE, the EEH errors detected
  1082. * on the PE will be reported and handled as usual.
  1083. */
  1084. void eeh_dev_release(struct pci_dev *pdev)
  1085. {
  1086. struct eeh_dev *edev;
  1087. mutex_lock(&eeh_dev_mutex);
  1088. /* No PCI device ? */
  1089. if (!pdev)
  1090. goto out;
  1091. /* No EEH device ? */
  1092. edev = pci_dev_to_eeh_dev(pdev);
  1093. if (!edev || !edev->pe || !eeh_pe_passed(edev->pe))
  1094. goto out;
  1095. /* Decrease PE's pass through count */
  1096. WARN_ON(atomic_dec_if_positive(&edev->pe->pass_dev_cnt) < 0);
  1097. eeh_pe_change_owner(edev->pe);
  1098. out:
  1099. mutex_unlock(&eeh_dev_mutex);
  1100. }
  1101. EXPORT_SYMBOL(eeh_dev_release);
  1102. #ifdef CONFIG_IOMMU_API
  1103. /**
  1104. * eeh_iommu_group_to_pe - Convert IOMMU group to EEH PE
  1105. * @group: IOMMU group
  1106. *
  1107. * The routine is called to convert IOMMU group to EEH PE.
  1108. */
  1109. struct eeh_pe *eeh_iommu_group_to_pe(struct iommu_group *group)
  1110. {
  1111. struct pci_dev *pdev = NULL;
  1112. struct eeh_dev *edev;
  1113. int ret;
  1114. /* No IOMMU group ? */
  1115. if (!group)
  1116. return NULL;
  1117. ret = iommu_group_for_each_dev(group, &pdev, dev_has_iommu_table);
  1118. if (!ret || !pdev)
  1119. return NULL;
  1120. /* No EEH device or PE ? */
  1121. edev = pci_dev_to_eeh_dev(pdev);
  1122. if (!edev || !edev->pe)
  1123. return NULL;
  1124. return edev->pe;
  1125. }
  1126. EXPORT_SYMBOL_GPL(eeh_iommu_group_to_pe);
  1127. #endif /* CONFIG_IOMMU_API */
  1128. /**
  1129. * eeh_pe_set_option - Set options for the indicated PE
  1130. * @pe: EEH PE
  1131. * @option: requested option
  1132. *
  1133. * The routine is called to enable or disable EEH functionality
  1134. * on the indicated PE, to enable IO or DMA for the frozen PE.
  1135. */
  1136. int eeh_pe_set_option(struct eeh_pe *pe, int option)
  1137. {
  1138. int ret = 0;
  1139. /* Invalid PE ? */
  1140. if (!pe)
  1141. return -ENODEV;
  1142. /*
  1143. * EEH functionality could possibly be disabled, just
  1144. * return error for the case. And the EEH functionality
  1145. * isn't expected to be disabled on one specific PE.
  1146. */
  1147. switch (option) {
  1148. case EEH_OPT_ENABLE:
  1149. if (eeh_enabled()) {
  1150. ret = eeh_pe_change_owner(pe);
  1151. break;
  1152. }
  1153. ret = -EIO;
  1154. break;
  1155. case EEH_OPT_DISABLE:
  1156. break;
  1157. case EEH_OPT_THAW_MMIO:
  1158. case EEH_OPT_THAW_DMA:
  1159. case EEH_OPT_FREEZE_PE:
  1160. if (!eeh_ops || !eeh_ops->set_option) {
  1161. ret = -ENOENT;
  1162. break;
  1163. }
  1164. ret = eeh_pci_enable(pe, option);
  1165. break;
  1166. default:
  1167. pr_debug("%s: Option %d out of range (%d, %d)\n",
  1168. __func__, option, EEH_OPT_DISABLE, EEH_OPT_THAW_DMA);
  1169. ret = -EINVAL;
  1170. }
  1171. return ret;
  1172. }
  1173. EXPORT_SYMBOL_GPL(eeh_pe_set_option);
  1174. /**
  1175. * eeh_pe_get_state - Retrieve PE's state
  1176. * @pe: EEH PE
  1177. *
  1178. * Retrieve the PE's state, which includes 3 aspects: enabled
  1179. * DMA, enabled IO and asserted reset.
  1180. */
  1181. int eeh_pe_get_state(struct eeh_pe *pe)
  1182. {
  1183. int result, ret = 0;
  1184. bool rst_active, dma_en, mmio_en;
  1185. /* Existing PE ? */
  1186. if (!pe)
  1187. return -ENODEV;
  1188. if (!eeh_ops || !eeh_ops->get_state)
  1189. return -ENOENT;
  1190. /*
  1191. * If the parent PE is owned by the host kernel and is undergoing
  1192. * error recovery, we should return the PE state as temporarily
  1193. * unavailable so that the error recovery on the guest is suspended
  1194. * until the recovery completes on the host.
  1195. */
  1196. if (pe->parent &&
  1197. !(pe->state & EEH_PE_REMOVED) &&
  1198. (pe->parent->state & (EEH_PE_ISOLATED | EEH_PE_RECOVERING)))
  1199. return EEH_PE_STATE_UNAVAIL;
  1200. result = eeh_ops->get_state(pe, NULL);
  1201. rst_active = !!(result & EEH_STATE_RESET_ACTIVE);
  1202. dma_en = !!(result & EEH_STATE_DMA_ENABLED);
  1203. mmio_en = !!(result & EEH_STATE_MMIO_ENABLED);
  1204. if (rst_active)
  1205. ret = EEH_PE_STATE_RESET;
  1206. else if (dma_en && mmio_en)
  1207. ret = EEH_PE_STATE_NORMAL;
  1208. else if (!dma_en && !mmio_en)
  1209. ret = EEH_PE_STATE_STOPPED_IO_DMA;
  1210. else if (!dma_en && mmio_en)
  1211. ret = EEH_PE_STATE_STOPPED_DMA;
  1212. else
  1213. ret = EEH_PE_STATE_UNAVAIL;
  1214. return ret;
  1215. }
  1216. EXPORT_SYMBOL_GPL(eeh_pe_get_state);
  1217. static int eeh_pe_reenable_devices(struct eeh_pe *pe, bool include_passed)
  1218. {
  1219. struct eeh_dev *edev, *tmp;
  1220. struct pci_dev *pdev;
  1221. int ret = 0;
  1222. eeh_pe_restore_bars(pe);
  1223. /*
  1224. * Reenable PCI devices as the devices passed
  1225. * through are always enabled before the reset.
  1226. */
  1227. eeh_pe_for_each_dev(pe, edev, tmp) {
  1228. pdev = eeh_dev_to_pci_dev(edev);
  1229. if (!pdev)
  1230. continue;
  1231. ret = pci_reenable_device(pdev);
  1232. if (ret) {
  1233. pr_warn("%s: Failure %d reenabling %s\n",
  1234. __func__, ret, pci_name(pdev));
  1235. return ret;
  1236. }
  1237. }
  1238. /* The PE is still in frozen state */
  1239. if (include_passed || !eeh_pe_passed(pe)) {
  1240. ret = eeh_unfreeze_pe(pe);
  1241. } else
  1242. pr_info("EEH: Note: Leaving passthrough PHB#%x-PE#%x frozen.\n",
  1243. pe->phb->global_number, pe->addr);
  1244. if (!ret)
  1245. eeh_pe_state_clear(pe, EEH_PE_ISOLATED, include_passed);
  1246. return ret;
  1247. }
  1248. /**
  1249. * eeh_pe_reset - Issue PE reset according to specified type
  1250. * @pe: EEH PE
  1251. * @option: reset type
  1252. * @include_passed: include passed-through devices?
  1253. *
  1254. * The routine is called to reset the specified PE with the
  1255. * indicated type, either fundamental reset or hot reset.
  1256. * PE reset is the most important part for error recovery.
  1257. */
  1258. int eeh_pe_reset(struct eeh_pe *pe, int option, bool include_passed)
  1259. {
  1260. int ret = 0;
  1261. /* Invalid PE ? */
  1262. if (!pe)
  1263. return -ENODEV;
  1264. if (!eeh_ops || !eeh_ops->set_option || !eeh_ops->reset)
  1265. return -ENOENT;
  1266. switch (option) {
  1267. case EEH_RESET_DEACTIVATE:
  1268. ret = eeh_ops->reset(pe, option);
  1269. eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED, include_passed);
  1270. if (ret)
  1271. break;
  1272. ret = eeh_pe_reenable_devices(pe, include_passed);
  1273. break;
  1274. case EEH_RESET_HOT:
  1275. case EEH_RESET_FUNDAMENTAL:
  1276. /*
  1277. * Proactively freeze the PE to drop all MMIO access
  1278. * during reset, which should be banned as it's always
  1279. * cause recursive EEH error.
  1280. */
  1281. eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
  1282. eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
  1283. ret = eeh_ops->reset(pe, option);
  1284. break;
  1285. default:
  1286. pr_debug("%s: Unsupported option %d\n",
  1287. __func__, option);
  1288. ret = -EINVAL;
  1289. }
  1290. return ret;
  1291. }
  1292. EXPORT_SYMBOL_GPL(eeh_pe_reset);
  1293. /**
  1294. * eeh_pe_configure - Configure PCI bridges after PE reset
  1295. * @pe: EEH PE
  1296. *
  1297. * The routine is called to restore the PCI config space for
  1298. * those PCI devices, especially PCI bridges affected by PE
  1299. * reset issued previously.
  1300. */
  1301. int eeh_pe_configure(struct eeh_pe *pe)
  1302. {
  1303. int ret = 0;
  1304. /* Invalid PE ? */
  1305. if (!pe)
  1306. return -ENODEV;
  1307. else
  1308. ret = eeh_ops->configure_bridge(pe);
  1309. return ret;
  1310. }
  1311. EXPORT_SYMBOL_GPL(eeh_pe_configure);
  1312. /**
  1313. * eeh_pe_inject_err - Injecting the specified PCI error to the indicated PE
  1314. * @pe: the indicated PE
  1315. * @type: error type
  1316. * @func: error function
  1317. * @addr: address
  1318. * @mask: address mask
  1319. *
  1320. * The routine is called to inject the specified PCI error, which
  1321. * is determined by @type and @func, to the indicated PE for
  1322. * testing purpose.
  1323. */
  1324. int eeh_pe_inject_err(struct eeh_pe *pe, int type, int func,
  1325. unsigned long addr, unsigned long mask)
  1326. {
  1327. /* Invalid PE ? */
  1328. if (!pe)
  1329. return -ENODEV;
  1330. /* Unsupported operation ? */
  1331. if (!eeh_ops || !eeh_ops->err_inject)
  1332. return -ENOENT;
  1333. /* Check on PCI error function */
  1334. if (func < EEH_ERR_FUNC_MIN || func > EEH_ERR_FUNC_MAX)
  1335. return -EINVAL;
  1336. return eeh_ops->err_inject(pe, type, func, addr, mask);
  1337. }
  1338. EXPORT_SYMBOL_GPL(eeh_pe_inject_err);
  1339. #ifdef CONFIG_PROC_FS
  1340. static int proc_eeh_show(struct seq_file *m, void *v)
  1341. {
  1342. if (!eeh_enabled()) {
  1343. seq_printf(m, "EEH Subsystem is globally disabled\n");
  1344. seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
  1345. } else {
  1346. seq_printf(m, "EEH Subsystem is enabled\n");
  1347. seq_printf(m,
  1348. "no device=%llu\n"
  1349. "no device node=%llu\n"
  1350. "no config address=%llu\n"
  1351. "check not wanted=%llu\n"
  1352. "eeh_total_mmio_ffs=%llu\n"
  1353. "eeh_false_positives=%llu\n"
  1354. "eeh_slot_resets=%llu\n",
  1355. eeh_stats.no_device,
  1356. eeh_stats.no_dn,
  1357. eeh_stats.no_cfg_addr,
  1358. eeh_stats.ignored_check,
  1359. eeh_stats.total_mmio_ffs,
  1360. eeh_stats.false_positives,
  1361. eeh_stats.slot_resets);
  1362. }
  1363. return 0;
  1364. }
  1365. #endif /* CONFIG_PROC_FS */
  1366. static int eeh_break_device(struct pci_dev *pdev)
  1367. {
  1368. struct resource *bar = NULL;
  1369. void __iomem *mapped;
  1370. u16 old, bit;
  1371. int i, pos;
  1372. /* Do we have an MMIO BAR to disable? */
  1373. for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
  1374. struct resource *r = &pdev->resource[i];
  1375. if (!r->flags || !r->start)
  1376. continue;
  1377. if (r->flags & IORESOURCE_IO)
  1378. continue;
  1379. if (r->flags & IORESOURCE_UNSET)
  1380. continue;
  1381. bar = r;
  1382. break;
  1383. }
  1384. if (!bar) {
  1385. pci_err(pdev, "Unable to find Memory BAR to cause EEH with\n");
  1386. return -ENXIO;
  1387. }
  1388. pci_err(pdev, "Going to break: %pR\n", bar);
  1389. if (pdev->is_virtfn) {
  1390. #ifndef CONFIG_PCI_IOV
  1391. return -ENXIO;
  1392. #else
  1393. /*
  1394. * VFs don't have a per-function COMMAND register, so the best
  1395. * we can do is clear the Memory Space Enable bit in the PF's
  1396. * SRIOV control reg.
  1397. *
  1398. * Unfortunately, this requires that we have a PF (i.e doesn't
  1399. * work for a passed-through VF) and it has the potential side
  1400. * effect of also causing an EEH on every other VF under the
  1401. * PF. Oh well.
  1402. */
  1403. pdev = pdev->physfn;
  1404. if (!pdev)
  1405. return -ENXIO; /* passed through VFs have no PF */
  1406. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
  1407. pos += PCI_SRIOV_CTRL;
  1408. bit = PCI_SRIOV_CTRL_MSE;
  1409. #endif /* !CONFIG_PCI_IOV */
  1410. } else {
  1411. bit = PCI_COMMAND_MEMORY;
  1412. pos = PCI_COMMAND;
  1413. }
  1414. /*
  1415. * Process here is:
  1416. *
  1417. * 1. Disable Memory space.
  1418. *
  1419. * 2. Perform an MMIO to the device. This should result in an error
  1420. * (CA / UR) being raised by the device which results in an EEH
  1421. * PE freeze. Using the in_8() accessor skips the eeh detection hook
  1422. * so the freeze hook so the EEH Detection machinery won't be
  1423. * triggered here. This is to match the usual behaviour of EEH
  1424. * where the HW will asynchronously freeze a PE and it's up to
  1425. * the kernel to notice and deal with it.
  1426. *
  1427. * 3. Turn Memory space back on. This is more important for VFs
  1428. * since recovery will probably fail if we don't. For normal
  1429. * the COMMAND register is reset as a part of re-initialising
  1430. * the device.
  1431. *
  1432. * Breaking stuff is the point so who cares if it's racy ;)
  1433. */
  1434. pci_read_config_word(pdev, pos, &old);
  1435. mapped = ioremap(bar->start, PAGE_SIZE);
  1436. if (!mapped) {
  1437. pci_err(pdev, "Unable to map MMIO BAR %pR\n", bar);
  1438. return -ENXIO;
  1439. }
  1440. pci_write_config_word(pdev, pos, old & ~bit);
  1441. in_8(mapped);
  1442. pci_write_config_word(pdev, pos, old);
  1443. iounmap(mapped);
  1444. return 0;
  1445. }
  1446. int eeh_pe_inject_mmio_error(struct pci_dev *pdev)
  1447. {
  1448. return eeh_break_device(pdev);
  1449. }
  1450. #ifdef CONFIG_DEBUG_FS
  1451. static struct pci_dev *eeh_debug_lookup_pdev(struct file *filp,
  1452. const char __user *user_buf,
  1453. size_t count, loff_t *ppos)
  1454. {
  1455. uint32_t domain, bus, dev, fn;
  1456. struct pci_dev *pdev;
  1457. char buf[20];
  1458. int ret;
  1459. memset(buf, 0, sizeof(buf));
  1460. ret = simple_write_to_buffer(buf, sizeof(buf)-1, ppos, user_buf, count);
  1461. if (!ret)
  1462. return ERR_PTR(-EFAULT);
  1463. ret = sscanf(buf, "%x:%x:%x.%x", &domain, &bus, &dev, &fn);
  1464. if (ret != 4) {
  1465. pr_err("%s: expected 4 args, got %d\n", __func__, ret);
  1466. return ERR_PTR(-EINVAL);
  1467. }
  1468. pdev = pci_get_domain_bus_and_slot(domain, bus, (dev << 3) | fn);
  1469. if (!pdev)
  1470. return ERR_PTR(-ENODEV);
  1471. return pdev;
  1472. }
  1473. static int eeh_enable_dbgfs_set(void *data, u64 val)
  1474. {
  1475. if (val)
  1476. eeh_clear_flag(EEH_FORCE_DISABLED);
  1477. else
  1478. eeh_add_flag(EEH_FORCE_DISABLED);
  1479. return 0;
  1480. }
  1481. static int eeh_enable_dbgfs_get(void *data, u64 *val)
  1482. {
  1483. if (eeh_enabled())
  1484. *val = 0x1ul;
  1485. else
  1486. *val = 0x0ul;
  1487. return 0;
  1488. }
  1489. DEFINE_DEBUGFS_ATTRIBUTE(eeh_enable_dbgfs_ops, eeh_enable_dbgfs_get,
  1490. eeh_enable_dbgfs_set, "0x%llx\n");
  1491. static ssize_t eeh_force_recover_write(struct file *filp,
  1492. const char __user *user_buf,
  1493. size_t count, loff_t *ppos)
  1494. {
  1495. struct pci_controller *hose;
  1496. uint32_t phbid, pe_no;
  1497. struct eeh_pe *pe;
  1498. char buf[20];
  1499. int ret;
  1500. ret = simple_write_to_buffer(buf, sizeof(buf), ppos, user_buf, count);
  1501. if (!ret)
  1502. return -EFAULT;
  1503. /*
  1504. * When PE is NULL the event is a "special" event. Rather than
  1505. * recovering a specific PE it forces the EEH core to scan for failed
  1506. * PHBs and recovers each. This needs to be done before any device
  1507. * recoveries can occur.
  1508. */
  1509. if (!strncmp(buf, "hwcheck", 7)) {
  1510. __eeh_send_failure_event(NULL);
  1511. return count;
  1512. }
  1513. ret = sscanf(buf, "%x:%x", &phbid, &pe_no);
  1514. if (ret != 2)
  1515. return -EINVAL;
  1516. hose = pci_find_controller_for_domain(phbid);
  1517. if (!hose)
  1518. return -ENODEV;
  1519. /* Retrieve PE */
  1520. pe = eeh_pe_get(hose, pe_no);
  1521. if (!pe)
  1522. return -ENODEV;
  1523. /*
  1524. * We don't do any state checking here since the detection
  1525. * process is async to the recovery process. The recovery
  1526. * thread *should* not break even if we schedule a recovery
  1527. * from an odd state (e.g. PE removed, or recovery of a
  1528. * non-isolated PE)
  1529. */
  1530. __eeh_send_failure_event(pe);
  1531. return ret < 0 ? ret : count;
  1532. }
  1533. static const struct file_operations eeh_force_recover_fops = {
  1534. .open = simple_open,
  1535. .write = eeh_force_recover_write,
  1536. };
  1537. static ssize_t eeh_debugfs_dev_usage(struct file *filp,
  1538. char __user *user_buf,
  1539. size_t count, loff_t *ppos)
  1540. {
  1541. static const char usage[] = "input format: <domain>:<bus>:<dev>.<fn>\n";
  1542. return simple_read_from_buffer(user_buf, count, ppos,
  1543. usage, sizeof(usage) - 1);
  1544. }
  1545. static ssize_t eeh_dev_check_write(struct file *filp,
  1546. const char __user *user_buf,
  1547. size_t count, loff_t *ppos)
  1548. {
  1549. struct pci_dev *pdev;
  1550. struct eeh_dev *edev;
  1551. int ret;
  1552. pdev = eeh_debug_lookup_pdev(filp, user_buf, count, ppos);
  1553. if (IS_ERR(pdev))
  1554. return PTR_ERR(pdev);
  1555. edev = pci_dev_to_eeh_dev(pdev);
  1556. if (!edev) {
  1557. pci_err(pdev, "No eeh_dev for this device!\n");
  1558. pci_dev_put(pdev);
  1559. return -ENODEV;
  1560. }
  1561. ret = eeh_dev_check_failure(edev);
  1562. pci_info(pdev, "eeh_dev_check_failure(%s) = %d\n",
  1563. pci_name(pdev), ret);
  1564. pci_dev_put(pdev);
  1565. return count;
  1566. }
  1567. static const struct file_operations eeh_dev_check_fops = {
  1568. .open = simple_open,
  1569. .write = eeh_dev_check_write,
  1570. .read = eeh_debugfs_dev_usage,
  1571. };
  1572. static ssize_t eeh_dev_break_write(struct file *filp,
  1573. const char __user *user_buf,
  1574. size_t count, loff_t *ppos)
  1575. {
  1576. struct pci_dev *pdev;
  1577. int ret;
  1578. pdev = eeh_debug_lookup_pdev(filp, user_buf, count, ppos);
  1579. if (IS_ERR(pdev))
  1580. return PTR_ERR(pdev);
  1581. ret = eeh_break_device(pdev);
  1582. pci_dev_put(pdev);
  1583. if (ret < 0)
  1584. return ret;
  1585. return count;
  1586. }
  1587. static const struct file_operations eeh_dev_break_fops = {
  1588. .open = simple_open,
  1589. .write = eeh_dev_break_write,
  1590. .read = eeh_debugfs_dev_usage,
  1591. };
  1592. static ssize_t eeh_dev_can_recover(struct file *filp,
  1593. const char __user *user_buf,
  1594. size_t count, loff_t *ppos)
  1595. {
  1596. struct pci_driver *drv;
  1597. struct pci_dev *pdev;
  1598. size_t ret;
  1599. pdev = eeh_debug_lookup_pdev(filp, user_buf, count, ppos);
  1600. if (IS_ERR(pdev))
  1601. return PTR_ERR(pdev);
  1602. /*
  1603. * In order for error recovery to work the driver needs to implement
  1604. * .error_detected(), so it can quiesce IO to the device, and
  1605. * .slot_reset() so it can re-initialise the device after a reset.
  1606. *
  1607. * Ideally they'd implement .resume() too, but some drivers which
  1608. * we need to support (notably IPR) don't so I guess we can tolerate
  1609. * that.
  1610. *
  1611. * .mmio_enabled() is mostly there as a work-around for devices which
  1612. * take forever to re-init after a hot reset. Implementing that is
  1613. * strictly optional.
  1614. */
  1615. drv = pci_dev_driver(pdev);
  1616. if (drv &&
  1617. drv->err_handler &&
  1618. drv->err_handler->error_detected &&
  1619. drv->err_handler->slot_reset) {
  1620. ret = count;
  1621. } else {
  1622. ret = -EOPNOTSUPP;
  1623. }
  1624. pci_dev_put(pdev);
  1625. return ret;
  1626. }
  1627. static const struct file_operations eeh_dev_can_recover_fops = {
  1628. .open = simple_open,
  1629. .write = eeh_dev_can_recover,
  1630. .read = eeh_debugfs_dev_usage,
  1631. };
  1632. #endif
  1633. static int __init eeh_init_proc(void)
  1634. {
  1635. if (machine_is(pseries) || machine_is(powernv)) {
  1636. proc_create_single("powerpc/eeh", 0, NULL, proc_eeh_show);
  1637. #ifdef CONFIG_DEBUG_FS
  1638. debugfs_create_file_unsafe("eeh_enable", 0600,
  1639. arch_debugfs_dir, NULL,
  1640. &eeh_enable_dbgfs_ops);
  1641. debugfs_create_u32("eeh_max_freezes", 0600,
  1642. arch_debugfs_dir, &eeh_max_freezes);
  1643. debugfs_create_bool("eeh_disable_recovery", 0600,
  1644. arch_debugfs_dir,
  1645. &eeh_debugfs_no_recover);
  1646. debugfs_create_file_unsafe("eeh_dev_check", 0600,
  1647. arch_debugfs_dir, NULL,
  1648. &eeh_dev_check_fops);
  1649. debugfs_create_file_unsafe("eeh_dev_break", 0600,
  1650. arch_debugfs_dir, NULL,
  1651. &eeh_dev_break_fops);
  1652. debugfs_create_file_unsafe("eeh_force_recover", 0600,
  1653. arch_debugfs_dir, NULL,
  1654. &eeh_force_recover_fops);
  1655. debugfs_create_file_unsafe("eeh_dev_can_recover", 0600,
  1656. arch_debugfs_dir, NULL,
  1657. &eeh_dev_can_recover_fops);
  1658. eeh_cache_debugfs_init();
  1659. #endif
  1660. }
  1661. return 0;
  1662. }
  1663. __initcall(eeh_init_proc);