eeh.c 46 KB

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