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, &hugepage_shift);
  308. if (!ptep)
  309. return token;
  310. WARN_ON(hugepage_shift);
  311. pa = pte_pfn(*ptep) << PAGE_SHIFT;
  312. return pa | (token & (PAGE_SIZE-1));
  313. }
  314. /*
  315. * On PowerNV platform, we might already have fenced PHB there.
  316. * For that case, it's meaningless to recover frozen PE. Intead,
  317. * We have to handle fenced PHB firstly.
  318. */
  319. static int eeh_phb_check_failure(struct eeh_pe *pe)
  320. {
  321. struct eeh_pe *phb_pe;
  322. unsigned long flags;
  323. int ret;
  324. if (!eeh_has_flag(EEH_PROBE_MODE_DEV))
  325. return -EPERM;
  326. /* Find the PHB PE */
  327. phb_pe = eeh_phb_pe_get(pe->phb);
  328. if (!phb_pe) {
  329. pr_warn("%s Can't find PE for PHB#%d\n",
  330. __func__, pe->phb->global_number);
  331. return -EEXIST;
  332. }
  333. /* If the PHB has been in problematic state */
  334. eeh_serialize_lock(&flags);
  335. if (phb_pe->state & EEH_PE_ISOLATED) {
  336. ret = 0;
  337. goto out;
  338. }
  339. /* Check PHB state */
  340. ret = eeh_ops->get_state(phb_pe, NULL);
  341. if ((ret < 0) ||
  342. (ret == EEH_STATE_NOT_SUPPORT) ||
  343. (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
  344. (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
  345. ret = 0;
  346. goto out;
  347. }
  348. /* Isolate the PHB and send event */
  349. eeh_pe_state_mark(phb_pe, EEH_PE_ISOLATED);
  350. eeh_serialize_unlock(flags);
  351. pr_err("EEH: PHB#%x failure detected, location: %s\n",
  352. phb_pe->phb->global_number, eeh_pe_loc_get(phb_pe));
  353. dump_stack();
  354. eeh_send_failure_event(phb_pe);
  355. return 1;
  356. out:
  357. eeh_serialize_unlock(flags);
  358. return ret;
  359. }
  360. /**
  361. * eeh_dev_check_failure - Check if all 1's data is due to EEH slot freeze
  362. * @edev: eeh device
  363. *
  364. * Check for an EEH failure for the given device node. Call this
  365. * routine if the result of a read was all 0xff's and you want to
  366. * find out if this is due to an EEH slot freeze. This routine
  367. * will query firmware for the EEH status.
  368. *
  369. * Returns 0 if there has not been an EEH error; otherwise returns
  370. * a non-zero value and queues up a slot isolation event notification.
  371. *
  372. * It is safe to call this routine in an interrupt context.
  373. */
  374. int eeh_dev_check_failure(struct eeh_dev *edev)
  375. {
  376. int ret;
  377. int active_flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
  378. unsigned long flags;
  379. struct pci_dn *pdn;
  380. struct pci_dev *dev;
  381. struct eeh_pe *pe, *parent_pe, *phb_pe;
  382. int rc = 0;
  383. const char *location = NULL;
  384. eeh_stats.total_mmio_ffs++;
  385. if (!eeh_enabled())
  386. return 0;
  387. if (!edev) {
  388. eeh_stats.no_dn++;
  389. return 0;
  390. }
  391. dev = eeh_dev_to_pci_dev(edev);
  392. pe = eeh_dev_to_pe(edev);
  393. /* Access to IO BARs might get this far and still not want checking. */
  394. if (!pe) {
  395. eeh_stats.ignored_check++;
  396. pr_debug("EEH: Ignored check for %s\n",
  397. eeh_pci_name(dev));
  398. return 0;
  399. }
  400. if (!pe->addr && !pe->config_addr) {
  401. eeh_stats.no_cfg_addr++;
  402. return 0;
  403. }
  404. /*
  405. * On PowerNV platform, we might already have fenced PHB
  406. * there and we need take care of that firstly.
  407. */
  408. ret = eeh_phb_check_failure(pe);
  409. if (ret > 0)
  410. return ret;
  411. /*
  412. * If the PE isn't owned by us, we shouldn't check the
  413. * state. Instead, let the owner handle it if the PE has
  414. * been frozen.
  415. */
  416. if (eeh_pe_passed(pe))
  417. return 0;
  418. /* If we already have a pending isolation event for this
  419. * slot, we know it's bad already, we don't need to check.
  420. * Do this checking under a lock; as multiple PCI devices
  421. * in one slot might report errors simultaneously, and we
  422. * only want one error recovery routine running.
  423. */
  424. eeh_serialize_lock(&flags);
  425. rc = 1;
  426. if (pe->state & EEH_PE_ISOLATED) {
  427. pe->check_count++;
  428. if (pe->check_count % EEH_MAX_FAILS == 0) {
  429. pdn = eeh_dev_to_pdn(edev);
  430. if (pdn->node)
  431. location = of_get_property(pdn->node, "ibm,loc-code", NULL);
  432. printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
  433. "location=%s driver=%s pci addr=%s\n",
  434. pe->check_count,
  435. location ? location : "unknown",
  436. eeh_driver_name(dev), eeh_pci_name(dev));
  437. printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
  438. eeh_driver_name(dev));
  439. dump_stack();
  440. }
  441. goto dn_unlock;
  442. }
  443. /*
  444. * Now test for an EEH failure. This is VERY expensive.
  445. * Note that the eeh_config_addr may be a parent device
  446. * in the case of a device behind a bridge, or it may be
  447. * function zero of a multi-function device.
  448. * In any case they must share a common PHB.
  449. */
  450. ret = eeh_ops->get_state(pe, NULL);
  451. /* Note that config-io to empty slots may fail;
  452. * they are empty when they don't have children.
  453. * We will punt with the following conditions: Failure to get
  454. * PE's state, EEH not support and Permanently unavailable
  455. * state, PE is in good state.
  456. */
  457. if ((ret < 0) ||
  458. (ret == EEH_STATE_NOT_SUPPORT) ||
  459. ((ret & active_flags) == active_flags)) {
  460. eeh_stats.false_positives++;
  461. pe->false_positives++;
  462. rc = 0;
  463. goto dn_unlock;
  464. }
  465. /*
  466. * It should be corner case that the parent PE has been
  467. * put into frozen state as well. We should take care
  468. * that at first.
  469. */
  470. parent_pe = pe->parent;
  471. while (parent_pe) {
  472. /* Hit the ceiling ? */
  473. if (parent_pe->type & EEH_PE_PHB)
  474. break;
  475. /* Frozen parent PE ? */
  476. ret = eeh_ops->get_state(parent_pe, NULL);
  477. if (ret > 0 &&
  478. (ret & active_flags) != active_flags)
  479. pe = parent_pe;
  480. /* Next parent level */
  481. parent_pe = parent_pe->parent;
  482. }
  483. eeh_stats.slot_resets++;
  484. /* Avoid repeated reports of this failure, including problems
  485. * with other functions on this device, and functions under
  486. * bridges.
  487. */
  488. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  489. eeh_serialize_unlock(flags);
  490. /* Most EEH events are due to device driver bugs. Having
  491. * a stack trace will help the device-driver authors figure
  492. * out what happened. So print that out.
  493. */
  494. phb_pe = eeh_phb_pe_get(pe->phb);
  495. pr_err("EEH: Frozen PHB#%x-PE#%x detected\n",
  496. pe->phb->global_number, pe->addr);
  497. pr_err("EEH: PE location: %s, PHB location: %s\n",
  498. eeh_pe_loc_get(pe), eeh_pe_loc_get(phb_pe));
  499. dump_stack();
  500. eeh_send_failure_event(pe);
  501. return 1;
  502. dn_unlock:
  503. eeh_serialize_unlock(flags);
  504. return rc;
  505. }
  506. EXPORT_SYMBOL_GPL(eeh_dev_check_failure);
  507. /**
  508. * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
  509. * @token: I/O address
  510. *
  511. * Check for an EEH failure at the given I/O address. Call this
  512. * routine if the result of a read was all 0xff's and you want to
  513. * find out if this is due to an EEH slot freeze event. This routine
  514. * will query firmware for the EEH status.
  515. *
  516. * Note this routine is safe to call in an interrupt context.
  517. */
  518. int eeh_check_failure(const volatile void __iomem *token)
  519. {
  520. unsigned long addr;
  521. struct eeh_dev *edev;
  522. /* Finding the phys addr + pci device; this is pretty quick. */
  523. addr = eeh_token_to_phys((unsigned long __force) token);
  524. edev = eeh_addr_cache_get_dev(addr);
  525. if (!edev) {
  526. eeh_stats.no_device++;
  527. return 0;
  528. }
  529. return eeh_dev_check_failure(edev);
  530. }
  531. EXPORT_SYMBOL(eeh_check_failure);
  532. /**
  533. * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
  534. * @pe: EEH PE
  535. *
  536. * This routine should be called to reenable frozen MMIO or DMA
  537. * so that it would work correctly again. It's useful while doing
  538. * recovery or log collection on the indicated device.
  539. */
  540. int eeh_pci_enable(struct eeh_pe *pe, int function)
  541. {
  542. int active_flag, rc;
  543. /*
  544. * pHyp doesn't allow to enable IO or DMA on unfrozen PE.
  545. * Also, it's pointless to enable them on unfrozen PE. So
  546. * we have to check before enabling IO or DMA.
  547. */
  548. switch (function) {
  549. case EEH_OPT_THAW_MMIO:
  550. active_flag = EEH_STATE_MMIO_ACTIVE;
  551. break;
  552. case EEH_OPT_THAW_DMA:
  553. active_flag = EEH_STATE_DMA_ACTIVE;
  554. break;
  555. case EEH_OPT_DISABLE:
  556. case EEH_OPT_ENABLE:
  557. case EEH_OPT_FREEZE_PE:
  558. active_flag = 0;
  559. break;
  560. default:
  561. pr_warn("%s: Invalid function %d\n",
  562. __func__, function);
  563. return -EINVAL;
  564. }
  565. /*
  566. * Check if IO or DMA has been enabled before
  567. * enabling them.
  568. */
  569. if (active_flag) {
  570. rc = eeh_ops->get_state(pe, NULL);
  571. if (rc < 0)
  572. return rc;
  573. /* Needn't enable it at all */
  574. if (rc == EEH_STATE_NOT_SUPPORT)
  575. return 0;
  576. /* It's already enabled */
  577. if (rc & active_flag)
  578. return 0;
  579. }
  580. /* Issue the request */
  581. rc = eeh_ops->set_option(pe, function);
  582. if (rc)
  583. pr_warn("%s: Unexpected state change %d on "
  584. "PHB#%d-PE#%x, err=%d\n",
  585. __func__, function, pe->phb->global_number,
  586. pe->addr, rc);
  587. /* Check if the request is finished successfully */
  588. if (active_flag) {
  589. rc = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
  590. if (rc <= 0)
  591. return rc;
  592. if (rc & active_flag)
  593. return 0;
  594. return -EIO;
  595. }
  596. return rc;
  597. }
  598. static void *eeh_disable_and_save_dev_state(void *data, void *userdata)
  599. {
  600. struct eeh_dev *edev = data;
  601. struct pci_dev *pdev = eeh_dev_to_pci_dev(edev);
  602. struct pci_dev *dev = userdata;
  603. /*
  604. * The caller should have disabled and saved the
  605. * state for the specified device
  606. */
  607. if (!pdev || pdev == dev)
  608. return NULL;
  609. /* Ensure we have D0 power state */
  610. pci_set_power_state(pdev, PCI_D0);
  611. /* Save device state */
  612. pci_save_state(pdev);
  613. /*
  614. * Disable device to avoid any DMA traffic and
  615. * interrupt from the device
  616. */
  617. pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
  618. return NULL;
  619. }
  620. static void *eeh_restore_dev_state(void *data, void *userdata)
  621. {
  622. struct eeh_dev *edev = data;
  623. struct pci_dn *pdn = eeh_dev_to_pdn(edev);
  624. struct pci_dev *pdev = eeh_dev_to_pci_dev(edev);
  625. struct pci_dev *dev = userdata;
  626. if (!pdev)
  627. return NULL;
  628. /* Apply customization from firmware */
  629. if (pdn && eeh_ops->restore_config)
  630. eeh_ops->restore_config(pdn);
  631. /* The caller should restore state for the specified device */
  632. if (pdev != dev)
  633. pci_restore_state(pdev);
  634. return NULL;
  635. }
  636. /**
  637. * pcibios_set_pcie_slot_reset - Set PCI-E reset state
  638. * @dev: pci device struct
  639. * @state: reset state to enter
  640. *
  641. * Return value:
  642. * 0 if success
  643. */
  644. int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  645. {
  646. struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
  647. struct eeh_pe *pe = eeh_dev_to_pe(edev);
  648. if (!pe) {
  649. pr_err("%s: No PE found on PCI device %s\n",
  650. __func__, pci_name(dev));
  651. return -EINVAL;
  652. }
  653. switch (state) {
  654. case pcie_deassert_reset:
  655. eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
  656. eeh_unfreeze_pe(pe, false);
  657. eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED);
  658. eeh_pe_dev_traverse(pe, eeh_restore_dev_state, dev);
  659. eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
  660. break;
  661. case pcie_hot_reset:
  662. eeh_pe_state_mark_with_cfg(pe, EEH_PE_ISOLATED);
  663. eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
  664. eeh_pe_dev_traverse(pe, eeh_disable_and_save_dev_state, dev);
  665. eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
  666. eeh_ops->reset(pe, EEH_RESET_HOT);
  667. break;
  668. case pcie_warm_reset:
  669. eeh_pe_state_mark_with_cfg(pe, EEH_PE_ISOLATED);
  670. eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
  671. eeh_pe_dev_traverse(pe, eeh_disable_and_save_dev_state, dev);
  672. eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
  673. eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
  674. break;
  675. default:
  676. eeh_pe_state_clear(pe, EEH_PE_ISOLATED | EEH_PE_CFG_BLOCKED);
  677. return -EINVAL;
  678. };
  679. return 0;
  680. }
  681. /**
  682. * eeh_set_pe_freset - Check the required reset for the indicated device
  683. * @data: EEH device
  684. * @flag: return value
  685. *
  686. * Each device might have its preferred reset type: fundamental or
  687. * hot reset. The routine is used to collected the information for
  688. * the indicated device and its children so that the bunch of the
  689. * devices could be reset properly.
  690. */
  691. static void *eeh_set_dev_freset(void *data, void *flag)
  692. {
  693. struct pci_dev *dev;
  694. unsigned int *freset = (unsigned int *)flag;
  695. struct eeh_dev *edev = (struct eeh_dev *)data;
  696. dev = eeh_dev_to_pci_dev(edev);
  697. if (dev)
  698. *freset |= dev->needs_freset;
  699. return NULL;
  700. }
  701. /**
  702. * eeh_reset_pe_once - Assert the pci #RST line for 1/4 second
  703. * @pe: EEH PE
  704. *
  705. * Assert the PCI #RST line for 1/4 second.
  706. */
  707. static void eeh_reset_pe_once(struct eeh_pe *pe)
  708. {
  709. unsigned int freset = 0;
  710. /* Determine type of EEH reset required for
  711. * Partitionable Endpoint, a hot-reset (1)
  712. * or a fundamental reset (3).
  713. * A fundamental reset required by any device under
  714. * Partitionable Endpoint trumps hot-reset.
  715. */
  716. eeh_pe_dev_traverse(pe, eeh_set_dev_freset, &freset);
  717. if (freset)
  718. eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
  719. else
  720. eeh_ops->reset(pe, EEH_RESET_HOT);
  721. eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
  722. }
  723. /**
  724. * eeh_reset_pe - Reset the indicated PE
  725. * @pe: EEH PE
  726. *
  727. * This routine should be called to reset indicated device, including
  728. * PE. A PE might include multiple PCI devices and sometimes PCI bridges
  729. * might be involved as well.
  730. */
  731. int eeh_reset_pe(struct eeh_pe *pe)
  732. {
  733. int flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
  734. int i, state, ret;
  735. /* Mark as reset and block config space */
  736. eeh_pe_state_mark(pe, EEH_PE_RESET | EEH_PE_CFG_BLOCKED);
  737. /* Take three shots at resetting the bus */
  738. for (i = 0; i < 3; i++) {
  739. eeh_reset_pe_once(pe);
  740. /*
  741. * EEH_PE_ISOLATED is expected to be removed after
  742. * BAR restore.
  743. */
  744. state = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
  745. if ((state & flags) == flags) {
  746. ret = 0;
  747. goto out;
  748. }
  749. if (state < 0) {
  750. pr_warn("%s: Unrecoverable slot failure on PHB#%d-PE#%x",
  751. __func__, pe->phb->global_number, pe->addr);
  752. ret = -ENOTRECOVERABLE;
  753. goto out;
  754. }
  755. /* We might run out of credits */
  756. ret = -EIO;
  757. pr_warn("%s: Failure %d resetting PHB#%x-PE#%x\n (%d)\n",
  758. __func__, state, pe->phb->global_number, pe->addr, (i + 1));
  759. }
  760. out:
  761. eeh_pe_state_clear(pe, EEH_PE_RESET | EEH_PE_CFG_BLOCKED);
  762. return ret;
  763. }
  764. /**
  765. * eeh_save_bars - Save device bars
  766. * @edev: PCI device associated EEH device
  767. *
  768. * Save the values of the device bars. Unlike the restore
  769. * routine, this routine is *not* recursive. This is because
  770. * PCI devices are added individually; but, for the restore,
  771. * an entire slot is reset at a time.
  772. */
  773. void eeh_save_bars(struct eeh_dev *edev)
  774. {
  775. struct pci_dn *pdn;
  776. int i;
  777. pdn = eeh_dev_to_pdn(edev);
  778. if (!pdn)
  779. return;
  780. for (i = 0; i < 16; i++)
  781. eeh_ops->read_config(pdn, i * 4, 4, &edev->config_space[i]);
  782. /*
  783. * For PCI bridges including root port, we need enable bus
  784. * master explicitly. Otherwise, it can't fetch IODA table
  785. * entries correctly. So we cache the bit in advance so that
  786. * we can restore it after reset, either PHB range or PE range.
  787. */
  788. if (edev->mode & EEH_DEV_BRIDGE)
  789. edev->config_space[1] |= PCI_COMMAND_MASTER;
  790. }
  791. /**
  792. * eeh_ops_register - Register platform dependent EEH operations
  793. * @ops: platform dependent EEH operations
  794. *
  795. * Register the platform dependent EEH operation callback
  796. * functions. The platform should call this function before
  797. * any other EEH operations.
  798. */
  799. int __init eeh_ops_register(struct eeh_ops *ops)
  800. {
  801. if (!ops->name) {
  802. pr_warn("%s: Invalid EEH ops name for %p\n",
  803. __func__, ops);
  804. return -EINVAL;
  805. }
  806. if (eeh_ops && eeh_ops != ops) {
  807. pr_warn("%s: EEH ops of platform %s already existing (%s)\n",
  808. __func__, eeh_ops->name, ops->name);
  809. return -EEXIST;
  810. }
  811. eeh_ops = ops;
  812. return 0;
  813. }
  814. /**
  815. * eeh_ops_unregister - Unreigster platform dependent EEH operations
  816. * @name: name of EEH platform operations
  817. *
  818. * Unregister the platform dependent EEH operation callback
  819. * functions.
  820. */
  821. int __exit eeh_ops_unregister(const char *name)
  822. {
  823. if (!name || !strlen(name)) {
  824. pr_warn("%s: Invalid EEH ops name\n",
  825. __func__);
  826. return -EINVAL;
  827. }
  828. if (eeh_ops && !strcmp(eeh_ops->name, name)) {
  829. eeh_ops = NULL;
  830. return 0;
  831. }
  832. return -EEXIST;
  833. }
  834. static int eeh_reboot_notifier(struct notifier_block *nb,
  835. unsigned long action, void *unused)
  836. {
  837. eeh_clear_flag(EEH_ENABLED);
  838. return NOTIFY_DONE;
  839. }
  840. static struct notifier_block eeh_reboot_nb = {
  841. .notifier_call = eeh_reboot_notifier,
  842. };
  843. /**
  844. * eeh_init - EEH initialization
  845. *
  846. * Initialize EEH by trying to enable it for all of the adapters in the system.
  847. * As a side effect we can determine here if eeh is supported at all.
  848. * Note that we leave EEH on so failed config cycles won't cause a machine
  849. * check. If a user turns off EEH for a particular adapter they are really
  850. * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
  851. * grant access to a slot if EEH isn't enabled, and so we always enable
  852. * EEH for all slots/all devices.
  853. *
  854. * The eeh-force-off option disables EEH checking globally, for all slots.
  855. * Even if force-off is set, the EEH hardware is still enabled, so that
  856. * newer systems can boot.
  857. */
  858. int eeh_init(void)
  859. {
  860. struct pci_controller *hose, *tmp;
  861. struct pci_dn *pdn;
  862. static int cnt = 0;
  863. int ret = 0;
  864. /*
  865. * We have to delay the initialization on PowerNV after
  866. * the PCI hierarchy tree has been built because the PEs
  867. * are figured out based on PCI devices instead of device
  868. * tree nodes
  869. */
  870. if (machine_is(powernv) && cnt++ <= 0)
  871. return ret;
  872. /* Register reboot notifier */
  873. ret = register_reboot_notifier(&eeh_reboot_nb);
  874. if (ret) {
  875. pr_warn("%s: Failed to register notifier (%d)\n",
  876. __func__, ret);
  877. return ret;
  878. }
  879. /* call platform initialization function */
  880. if (!eeh_ops) {
  881. pr_warn("%s: Platform EEH operation not found\n",
  882. __func__);
  883. return -EEXIST;
  884. } else if ((ret = eeh_ops->init()))
  885. return ret;
  886. /* Initialize EEH event */
  887. ret = eeh_event_init();
  888. if (ret)
  889. return ret;
  890. /* Enable EEH for all adapters */
  891. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  892. pdn = hose->pci_data;
  893. traverse_pci_dn(pdn, eeh_ops->probe, NULL);
  894. }
  895. /*
  896. * Call platform post-initialization. Actually, It's good chance
  897. * to inform platform that EEH is ready to supply service if the
  898. * I/O cache stuff has been built up.
  899. */
  900. if (eeh_ops->post_init) {
  901. ret = eeh_ops->post_init();
  902. if (ret)
  903. return ret;
  904. }
  905. if (eeh_enabled())
  906. pr_info("EEH: PCI Enhanced I/O Error Handling Enabled\n");
  907. else
  908. pr_warn("EEH: No capable adapters found\n");
  909. return ret;
  910. }
  911. core_initcall_sync(eeh_init);
  912. /**
  913. * eeh_add_device_early - Enable EEH for the indicated device node
  914. * @pdn: PCI device node for which to set up EEH
  915. *
  916. * This routine must be used to perform EEH initialization for PCI
  917. * devices that were added after system boot (e.g. hotplug, dlpar).
  918. * This routine must be called before any i/o is performed to the
  919. * adapter (inluding any config-space i/o).
  920. * Whether this actually enables EEH or not for this device depends
  921. * on the CEC architecture, type of the device, on earlier boot
  922. * command-line arguments & etc.
  923. */
  924. void eeh_add_device_early(struct pci_dn *pdn)
  925. {
  926. struct pci_controller *phb;
  927. struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
  928. if (!edev || !eeh_enabled())
  929. return;
  930. if (!eeh_has_flag(EEH_PROBE_MODE_DEVTREE))
  931. return;
  932. /* USB Bus children of PCI devices will not have BUID's */
  933. phb = edev->phb;
  934. if (NULL == phb ||
  935. (eeh_has_flag(EEH_PROBE_MODE_DEVTREE) && 0 == phb->buid))
  936. return;
  937. eeh_ops->probe(pdn, NULL);
  938. }
  939. /**
  940. * eeh_add_device_tree_early - Enable EEH for the indicated device
  941. * @pdn: PCI device node
  942. *
  943. * This routine must be used to perform EEH initialization for the
  944. * indicated PCI device that was added after system boot (e.g.
  945. * hotplug, dlpar).
  946. */
  947. void eeh_add_device_tree_early(struct pci_dn *pdn)
  948. {
  949. struct pci_dn *n;
  950. if (!pdn)
  951. return;
  952. list_for_each_entry(n, &pdn->child_list, list)
  953. eeh_add_device_tree_early(n);
  954. eeh_add_device_early(pdn);
  955. }
  956. EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
  957. /**
  958. * eeh_add_device_late - Perform EEH initialization for the indicated pci device
  959. * @dev: pci device for which to set up EEH
  960. *
  961. * This routine must be used to complete EEH initialization for PCI
  962. * devices that were added after system boot (e.g. hotplug, dlpar).
  963. */
  964. void eeh_add_device_late(struct pci_dev *dev)
  965. {
  966. struct pci_dn *pdn;
  967. struct eeh_dev *edev;
  968. if (!dev || !eeh_enabled())
  969. return;
  970. pr_debug("EEH: Adding device %s\n", pci_name(dev));
  971. pdn = pci_get_pdn_by_devfn(dev->bus, dev->devfn);
  972. edev = pdn_to_eeh_dev(pdn);
  973. if (edev->pdev == dev) {
  974. pr_debug("EEH: Already referenced !\n");
  975. return;
  976. }
  977. /*
  978. * The EEH cache might not be removed correctly because of
  979. * unbalanced kref to the device during unplug time, which
  980. * relies on pcibios_release_device(). So we have to remove
  981. * that here explicitly.
  982. */
  983. if (edev->pdev) {
  984. eeh_rmv_from_parent_pe(edev);
  985. eeh_addr_cache_rmv_dev(edev->pdev);
  986. eeh_sysfs_remove_device(edev->pdev);
  987. edev->mode &= ~EEH_DEV_SYSFS;
  988. /*
  989. * We definitely should have the PCI device removed
  990. * though it wasn't correctly. So we needn't call
  991. * into error handler afterwards.
  992. */
  993. edev->mode |= EEH_DEV_NO_HANDLER;
  994. edev->pdev = NULL;
  995. dev->dev.archdata.edev = NULL;
  996. }
  997. if (eeh_has_flag(EEH_PROBE_MODE_DEV))
  998. eeh_ops->probe(pdn, NULL);
  999. edev->pdev = dev;
  1000. dev->dev.archdata.edev = edev;
  1001. eeh_addr_cache_insert_dev(dev);
  1002. }
  1003. /**
  1004. * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
  1005. * @bus: PCI bus
  1006. *
  1007. * This routine must be used to perform EEH initialization for PCI
  1008. * devices which are attached to the indicated PCI bus. The PCI bus
  1009. * is added after system boot through hotplug or dlpar.
  1010. */
  1011. void eeh_add_device_tree_late(struct pci_bus *bus)
  1012. {
  1013. struct pci_dev *dev;
  1014. list_for_each_entry(dev, &bus->devices, bus_list) {
  1015. eeh_add_device_late(dev);
  1016. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  1017. struct pci_bus *subbus = dev->subordinate;
  1018. if (subbus)
  1019. eeh_add_device_tree_late(subbus);
  1020. }
  1021. }
  1022. }
  1023. EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
  1024. /**
  1025. * eeh_add_sysfs_files - Add EEH sysfs files for the indicated PCI bus
  1026. * @bus: PCI bus
  1027. *
  1028. * This routine must be used to add EEH sysfs files for PCI
  1029. * devices which are attached to the indicated PCI bus. The PCI bus
  1030. * is added after system boot through hotplug or dlpar.
  1031. */
  1032. void eeh_add_sysfs_files(struct pci_bus *bus)
  1033. {
  1034. struct pci_dev *dev;
  1035. list_for_each_entry(dev, &bus->devices, bus_list) {
  1036. eeh_sysfs_add_device(dev);
  1037. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  1038. struct pci_bus *subbus = dev->subordinate;
  1039. if (subbus)
  1040. eeh_add_sysfs_files(subbus);
  1041. }
  1042. }
  1043. }
  1044. EXPORT_SYMBOL_GPL(eeh_add_sysfs_files);
  1045. /**
  1046. * eeh_remove_device - Undo EEH setup for the indicated pci device
  1047. * @dev: pci device to be removed
  1048. *
  1049. * This routine should be called when a device is removed from
  1050. * a running system (e.g. by hotplug or dlpar). It unregisters
  1051. * the PCI device from the EEH subsystem. I/O errors affecting
  1052. * this device will no longer be detected after this call; thus,
  1053. * i/o errors affecting this slot may leave this device unusable.
  1054. */
  1055. void eeh_remove_device(struct pci_dev *dev)
  1056. {
  1057. struct eeh_dev *edev;
  1058. if (!dev || !eeh_enabled())
  1059. return;
  1060. edev = pci_dev_to_eeh_dev(dev);
  1061. /* Unregister the device with the EEH/PCI address search system */
  1062. pr_debug("EEH: Removing device %s\n", pci_name(dev));
  1063. if (!edev || !edev->pdev || !edev->pe) {
  1064. pr_debug("EEH: Not referenced !\n");
  1065. return;
  1066. }
  1067. /*
  1068. * During the hotplug for EEH error recovery, we need the EEH
  1069. * device attached to the parent PE in order for BAR restore
  1070. * a bit later. So we keep it for BAR restore and remove it
  1071. * from the parent PE during the BAR resotre.
  1072. */
  1073. edev->pdev = NULL;
  1074. dev->dev.archdata.edev = NULL;
  1075. if (!(edev->pe->state & EEH_PE_KEEP))
  1076. eeh_rmv_from_parent_pe(edev);
  1077. else
  1078. edev->mode |= EEH_DEV_DISCONNECTED;
  1079. /*
  1080. * We're removing from the PCI subsystem, that means
  1081. * the PCI device driver can't support EEH or not
  1082. * well. So we rely on hotplug completely to do recovery
  1083. * for the specific PCI device.
  1084. */
  1085. edev->mode |= EEH_DEV_NO_HANDLER;
  1086. eeh_addr_cache_rmv_dev(dev);
  1087. eeh_sysfs_remove_device(dev);
  1088. edev->mode &= ~EEH_DEV_SYSFS;
  1089. }
  1090. int eeh_unfreeze_pe(struct eeh_pe *pe, bool sw_state)
  1091. {
  1092. int ret;
  1093. ret = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
  1094. if (ret) {
  1095. pr_warn("%s: Failure %d enabling IO on PHB#%x-PE#%x\n",
  1096. __func__, ret, pe->phb->global_number, pe->addr);
  1097. return ret;
  1098. }
  1099. ret = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
  1100. if (ret) {
  1101. pr_warn("%s: Failure %d enabling DMA on PHB#%x-PE#%x\n",
  1102. __func__, ret, pe->phb->global_number, pe->addr);
  1103. return ret;
  1104. }
  1105. /* Clear software isolated state */
  1106. if (sw_state && (pe->state & EEH_PE_ISOLATED))
  1107. eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
  1108. return ret;
  1109. }
  1110. static struct pci_device_id eeh_reset_ids[] = {
  1111. { PCI_DEVICE(0x19a2, 0x0710) }, /* Emulex, BE */
  1112. { PCI_DEVICE(0x10df, 0xe220) }, /* Emulex, Lancer */
  1113. { PCI_DEVICE(0x14e4, 0x1657) }, /* Broadcom BCM5719 */
  1114. { 0 }
  1115. };
  1116. static int eeh_pe_change_owner(struct eeh_pe *pe)
  1117. {
  1118. struct eeh_dev *edev, *tmp;
  1119. struct pci_dev *pdev;
  1120. struct pci_device_id *id;
  1121. int flags, ret;
  1122. /* Check PE state */
  1123. flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
  1124. ret = eeh_ops->get_state(pe, NULL);
  1125. if (ret < 0 || ret == EEH_STATE_NOT_SUPPORT)
  1126. return 0;
  1127. /* Unfrozen PE, nothing to do */
  1128. if ((ret & flags) == flags)
  1129. return 0;
  1130. /* Frozen PE, check if it needs PE level reset */
  1131. eeh_pe_for_each_dev(pe, edev, tmp) {
  1132. pdev = eeh_dev_to_pci_dev(edev);
  1133. if (!pdev)
  1134. continue;
  1135. for (id = &eeh_reset_ids[0]; id->vendor != 0; id++) {
  1136. if (id->vendor != PCI_ANY_ID &&
  1137. id->vendor != pdev->vendor)
  1138. continue;
  1139. if (id->device != PCI_ANY_ID &&
  1140. id->device != pdev->device)
  1141. continue;
  1142. if (id->subvendor != PCI_ANY_ID &&
  1143. id->subvendor != pdev->subsystem_vendor)
  1144. continue;
  1145. if (id->subdevice != PCI_ANY_ID &&
  1146. id->subdevice != pdev->subsystem_device)
  1147. continue;
  1148. goto reset;
  1149. }
  1150. }
  1151. return eeh_unfreeze_pe(pe, true);
  1152. reset:
  1153. return eeh_pe_reset_and_recover(pe);
  1154. }
  1155. /**
  1156. * eeh_dev_open - Increase count of pass through devices for PE
  1157. * @pdev: PCI device
  1158. *
  1159. * Increase count of passed through devices for the indicated
  1160. * PE. In the result, the EEH errors detected on the PE won't be
  1161. * reported. The PE owner will be responsible for detection
  1162. * and recovery.
  1163. */
  1164. int eeh_dev_open(struct pci_dev *pdev)
  1165. {
  1166. struct eeh_dev *edev;
  1167. int ret = -ENODEV;
  1168. mutex_lock(&eeh_dev_mutex);
  1169. /* No PCI device ? */
  1170. if (!pdev)
  1171. goto out;
  1172. /* No EEH device or PE ? */
  1173. edev = pci_dev_to_eeh_dev(pdev);
  1174. if (!edev || !edev->pe)
  1175. goto out;
  1176. /*
  1177. * The PE might have been put into frozen state, but we
  1178. * didn't detect that yet. The passed through PCI devices
  1179. * in frozen PE won't work properly. Clear the frozen state
  1180. * in advance.
  1181. */
  1182. ret = eeh_pe_change_owner(edev->pe);
  1183. if (ret)
  1184. goto out;
  1185. /* Increase PE's pass through count */
  1186. atomic_inc(&edev->pe->pass_dev_cnt);
  1187. mutex_unlock(&eeh_dev_mutex);
  1188. return 0;
  1189. out:
  1190. mutex_unlock(&eeh_dev_mutex);
  1191. return ret;
  1192. }
  1193. EXPORT_SYMBOL_GPL(eeh_dev_open);
  1194. /**
  1195. * eeh_dev_release - Decrease count of pass through devices for PE
  1196. * @pdev: PCI device
  1197. *
  1198. * Decrease count of pass through devices for the indicated PE. If
  1199. * there is no passed through device in PE, the EEH errors detected
  1200. * on the PE will be reported and handled as usual.
  1201. */
  1202. void eeh_dev_release(struct pci_dev *pdev)
  1203. {
  1204. struct eeh_dev *edev;
  1205. mutex_lock(&eeh_dev_mutex);
  1206. /* No PCI device ? */
  1207. if (!pdev)
  1208. goto out;
  1209. /* No EEH device ? */
  1210. edev = pci_dev_to_eeh_dev(pdev);
  1211. if (!edev || !edev->pe || !eeh_pe_passed(edev->pe))
  1212. goto out;
  1213. /* Decrease PE's pass through count */
  1214. atomic_dec(&edev->pe->pass_dev_cnt);
  1215. WARN_ON(atomic_read(&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);