eeh.c 42 KB

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