eeh.c 45 KB

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