eeh_pe.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * The file intends to implement PE based on the information from
  4. * platforms. Basically, there have 3 types of PEs: PHB/Bus/Device.
  5. * All the PEs should be organized as hierarchy tree. The first level
  6. * of the tree will be associated to existing PHBs since the particular
  7. * PE is only meaningful in one PHB domain.
  8. *
  9. * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2012.
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/export.h>
  13. #include <linux/gfp.h>
  14. #include <linux/kernel.h>
  15. #include <linux/of.h>
  16. #include <linux/pci.h>
  17. #include <linux/string.h>
  18. #include <asm/pci-bridge.h>
  19. #include <asm/ppc-pci.h>
  20. static int eeh_pe_aux_size = 0;
  21. static LIST_HEAD(eeh_phb_pe);
  22. /**
  23. * eeh_set_pe_aux_size - Set PE auxillary data size
  24. * @size: PE auxillary data size
  25. *
  26. * Set PE auxillary data size
  27. */
  28. void eeh_set_pe_aux_size(int size)
  29. {
  30. if (size < 0)
  31. return;
  32. eeh_pe_aux_size = size;
  33. }
  34. /**
  35. * eeh_pe_alloc - Allocate PE
  36. * @phb: PCI controller
  37. * @type: PE type
  38. *
  39. * Allocate PE instance dynamically.
  40. */
  41. static struct eeh_pe *eeh_pe_alloc(struct pci_controller *phb, int type)
  42. {
  43. struct eeh_pe *pe;
  44. size_t alloc_size;
  45. alloc_size = sizeof(struct eeh_pe);
  46. if (eeh_pe_aux_size) {
  47. alloc_size = ALIGN(alloc_size, cache_line_size());
  48. alloc_size += eeh_pe_aux_size;
  49. }
  50. /* Allocate PHB PE */
  51. pe = kzalloc(alloc_size, GFP_KERNEL);
  52. if (!pe) return NULL;
  53. /* Initialize PHB PE */
  54. pe->type = type;
  55. pe->phb = phb;
  56. INIT_LIST_HEAD(&pe->child_list);
  57. INIT_LIST_HEAD(&pe->edevs);
  58. pe->data = (void *)pe + ALIGN(sizeof(struct eeh_pe),
  59. cache_line_size());
  60. return pe;
  61. }
  62. /**
  63. * eeh_phb_pe_create - Create PHB PE
  64. * @phb: PCI controller
  65. *
  66. * The function should be called while the PHB is detected during
  67. * system boot or PCI hotplug in order to create PHB PE.
  68. */
  69. int eeh_phb_pe_create(struct pci_controller *phb)
  70. {
  71. struct eeh_pe *pe;
  72. /* Allocate PHB PE */
  73. pe = eeh_pe_alloc(phb, EEH_PE_PHB);
  74. if (!pe) {
  75. pr_err("%s: out of memory!\n", __func__);
  76. return -ENOMEM;
  77. }
  78. /* Put it into the list */
  79. list_add_tail(&pe->child, &eeh_phb_pe);
  80. pr_debug("EEH: Add PE for PHB#%x\n", phb->global_number);
  81. return 0;
  82. }
  83. /**
  84. * eeh_wait_state - Wait for PE state
  85. * @pe: EEH PE
  86. * @max_wait: maximal period in millisecond
  87. *
  88. * Wait for the state of associated PE. It might take some time
  89. * to retrieve the PE's state.
  90. */
  91. int eeh_wait_state(struct eeh_pe *pe, int max_wait)
  92. {
  93. int ret;
  94. int mwait;
  95. /*
  96. * According to PAPR, the state of PE might be temporarily
  97. * unavailable. Under the circumstance, we have to wait
  98. * for indicated time determined by firmware. The maximal
  99. * wait time is 5 minutes, which is acquired from the original
  100. * EEH implementation. Also, the original implementation
  101. * also defined the minimal wait time as 1 second.
  102. */
  103. #define EEH_STATE_MIN_WAIT_TIME (1000)
  104. #define EEH_STATE_MAX_WAIT_TIME (300 * 1000)
  105. while (1) {
  106. ret = eeh_ops->get_state(pe, &mwait);
  107. if (ret != EEH_STATE_UNAVAILABLE)
  108. return ret;
  109. if (max_wait <= 0) {
  110. pr_warn("%s: Timeout when getting PE's state (%d)\n",
  111. __func__, max_wait);
  112. return EEH_STATE_NOT_SUPPORT;
  113. }
  114. if (mwait < EEH_STATE_MIN_WAIT_TIME) {
  115. pr_warn("%s: Firmware returned bad wait value %d\n",
  116. __func__, mwait);
  117. mwait = EEH_STATE_MIN_WAIT_TIME;
  118. } else if (mwait > EEH_STATE_MAX_WAIT_TIME) {
  119. pr_warn("%s: Firmware returned too long wait value %d\n",
  120. __func__, mwait);
  121. mwait = EEH_STATE_MAX_WAIT_TIME;
  122. }
  123. msleep(min(mwait, max_wait));
  124. max_wait -= mwait;
  125. }
  126. }
  127. /**
  128. * eeh_phb_pe_get - Retrieve PHB PE based on the given PHB
  129. * @phb: PCI controller
  130. *
  131. * The overall PEs form hierarchy tree. The first layer of the
  132. * hierarchy tree is composed of PHB PEs. The function is used
  133. * to retrieve the corresponding PHB PE according to the given PHB.
  134. */
  135. struct eeh_pe *eeh_phb_pe_get(struct pci_controller *phb)
  136. {
  137. struct eeh_pe *pe;
  138. list_for_each_entry(pe, &eeh_phb_pe, child) {
  139. /*
  140. * Actually, we needn't check the type since
  141. * the PE for PHB has been determined when that
  142. * was created.
  143. */
  144. if ((pe->type & EEH_PE_PHB) && pe->phb == phb)
  145. return pe;
  146. }
  147. return NULL;
  148. }
  149. /**
  150. * eeh_pe_next - Retrieve the next PE in the tree
  151. * @pe: current PE
  152. * @root: root PE
  153. *
  154. * The function is used to retrieve the next PE in the
  155. * hierarchy PE tree.
  156. */
  157. struct eeh_pe *eeh_pe_next(struct eeh_pe *pe, struct eeh_pe *root)
  158. {
  159. struct list_head *next = pe->child_list.next;
  160. if (next == &pe->child_list) {
  161. while (1) {
  162. if (pe == root)
  163. return NULL;
  164. next = pe->child.next;
  165. if (next != &pe->parent->child_list)
  166. break;
  167. pe = pe->parent;
  168. }
  169. }
  170. return list_entry(next, struct eeh_pe, child);
  171. }
  172. /**
  173. * eeh_pe_traverse - Traverse PEs in the specified PHB
  174. * @root: root PE
  175. * @fn: callback
  176. * @flag: extra parameter to callback
  177. *
  178. * The function is used to traverse the specified PE and its
  179. * child PEs. The traversing is to be terminated once the
  180. * callback returns something other than NULL, or no more PEs
  181. * to be traversed.
  182. */
  183. void *eeh_pe_traverse(struct eeh_pe *root,
  184. eeh_pe_traverse_func fn, void *flag)
  185. {
  186. struct eeh_pe *pe;
  187. void *ret;
  188. eeh_for_each_pe(root, pe) {
  189. ret = fn(pe, flag);
  190. if (ret) return ret;
  191. }
  192. return NULL;
  193. }
  194. /**
  195. * eeh_pe_dev_traverse - Traverse the devices from the PE
  196. * @root: EEH PE
  197. * @fn: function callback
  198. * @flag: extra parameter to callback
  199. *
  200. * The function is used to traverse the devices of the specified
  201. * PE and its child PEs.
  202. */
  203. void eeh_pe_dev_traverse(struct eeh_pe *root,
  204. eeh_edev_traverse_func fn, void *flag)
  205. {
  206. struct eeh_pe *pe;
  207. struct eeh_dev *edev, *tmp;
  208. if (!root) {
  209. pr_warn("%s: Invalid PE %p\n",
  210. __func__, root);
  211. return;
  212. }
  213. /* Traverse root PE */
  214. eeh_for_each_pe(root, pe)
  215. eeh_pe_for_each_dev(pe, edev, tmp)
  216. fn(edev, flag);
  217. }
  218. /**
  219. * __eeh_pe_get - Check the PE address
  220. *
  221. * For one particular PE, it can be identified by PE address
  222. * or tranditional BDF address. BDF address is composed of
  223. * Bus/Device/Function number. The extra data referred by flag
  224. * indicates which type of address should be used.
  225. */
  226. static void *__eeh_pe_get(struct eeh_pe *pe, void *flag)
  227. {
  228. int *target_pe = flag;
  229. /* PHB PEs are special and should be ignored */
  230. if (pe->type & EEH_PE_PHB)
  231. return NULL;
  232. if (*target_pe == pe->addr)
  233. return pe;
  234. return NULL;
  235. }
  236. /**
  237. * eeh_pe_get - Search PE based on the given address
  238. * @phb: PCI controller
  239. * @pe_no: PE number
  240. *
  241. * Search the corresponding PE based on the specified address which
  242. * is included in the eeh device. The function is used to check if
  243. * the associated PE has been created against the PE address. It's
  244. * notable that the PE address has 2 format: traditional PE address
  245. * which is composed of PCI bus/device/function number, or unified
  246. * PE address.
  247. */
  248. struct eeh_pe *eeh_pe_get(struct pci_controller *phb, int pe_no)
  249. {
  250. struct eeh_pe *root = eeh_phb_pe_get(phb);
  251. return eeh_pe_traverse(root, __eeh_pe_get, &pe_no);
  252. }
  253. /**
  254. * eeh_pe_tree_insert - Add EEH device to parent PE
  255. * @edev: EEH device
  256. * @new_pe_parent: PE to create additional PEs under
  257. *
  258. * Add EEH device to the PE in edev->pe_config_addr. If a PE already
  259. * exists with that address then @edev is added to that PE. Otherwise
  260. * a new PE is created and inserted into the PE tree as a child of
  261. * @new_pe_parent.
  262. *
  263. * If @new_pe_parent is NULL then the new PE will be inserted under
  264. * directly under the PHB.
  265. */
  266. int eeh_pe_tree_insert(struct eeh_dev *edev, struct eeh_pe *new_pe_parent)
  267. {
  268. struct pci_controller *hose = edev->controller;
  269. struct eeh_pe *pe, *parent;
  270. /*
  271. * Search the PE has been existing or not according
  272. * to the PE address. If that has been existing, the
  273. * PE should be composed of PCI bus and its subordinate
  274. * components.
  275. */
  276. pe = eeh_pe_get(hose, edev->pe_config_addr);
  277. if (pe) {
  278. if (pe->type & EEH_PE_INVALID) {
  279. list_add_tail(&edev->entry, &pe->edevs);
  280. edev->pe = pe;
  281. /*
  282. * We're running to here because of PCI hotplug caused by
  283. * EEH recovery. We need clear EEH_PE_INVALID until the top.
  284. */
  285. parent = pe;
  286. while (parent) {
  287. if (!(parent->type & EEH_PE_INVALID))
  288. break;
  289. parent->type &= ~EEH_PE_INVALID;
  290. parent = parent->parent;
  291. }
  292. eeh_edev_dbg(edev, "Added to existing PE (parent: PE#%x)\n",
  293. pe->parent->addr);
  294. } else {
  295. /* Mark the PE as type of PCI bus */
  296. pe->type = EEH_PE_BUS;
  297. edev->pe = pe;
  298. /* Put the edev to PE */
  299. list_add_tail(&edev->entry, &pe->edevs);
  300. eeh_edev_dbg(edev, "Added to bus PE\n");
  301. }
  302. return 0;
  303. }
  304. /* Create a new EEH PE */
  305. if (edev->physfn)
  306. pe = eeh_pe_alloc(hose, EEH_PE_VF);
  307. else
  308. pe = eeh_pe_alloc(hose, EEH_PE_DEVICE);
  309. if (!pe) {
  310. pr_err("%s: out of memory!\n", __func__);
  311. return -ENOMEM;
  312. }
  313. pe->addr = edev->pe_config_addr;
  314. /*
  315. * Put the new EEH PE into hierarchy tree. If the parent
  316. * can't be found, the newly created PE will be attached
  317. * to PHB directly. Otherwise, we have to associate the
  318. * PE with its parent.
  319. */
  320. if (!new_pe_parent) {
  321. new_pe_parent = eeh_phb_pe_get(hose);
  322. if (!new_pe_parent) {
  323. pr_err("%s: No PHB PE is found (PHB Domain=%d)\n",
  324. __func__, hose->global_number);
  325. edev->pe = NULL;
  326. kfree(pe);
  327. return -EEXIST;
  328. }
  329. }
  330. /* link new PE into the tree */
  331. pe->parent = new_pe_parent;
  332. list_add_tail(&pe->child, &new_pe_parent->child_list);
  333. /*
  334. * Put the newly created PE into the child list and
  335. * link the EEH device accordingly.
  336. */
  337. list_add_tail(&edev->entry, &pe->edevs);
  338. edev->pe = pe;
  339. eeh_edev_dbg(edev, "Added to new (parent: PE#%x)\n",
  340. new_pe_parent->addr);
  341. return 0;
  342. }
  343. /**
  344. * eeh_pe_tree_remove - Remove one EEH device from the associated PE
  345. * @edev: EEH device
  346. *
  347. * The PE hierarchy tree might be changed when doing PCI hotplug.
  348. * Also, the PCI devices or buses could be removed from the system
  349. * during EEH recovery. So we have to call the function remove the
  350. * corresponding PE accordingly if necessary.
  351. */
  352. int eeh_pe_tree_remove(struct eeh_dev *edev)
  353. {
  354. struct eeh_pe *pe, *parent, *child;
  355. bool keep, recover;
  356. int cnt;
  357. pe = eeh_dev_to_pe(edev);
  358. if (!pe) {
  359. eeh_edev_dbg(edev, "No PE found for device.\n");
  360. return -EEXIST;
  361. }
  362. /* Remove the EEH device */
  363. edev->pe = NULL;
  364. list_del(&edev->entry);
  365. /*
  366. * Check if the parent PE includes any EEH devices.
  367. * If not, we should delete that. Also, we should
  368. * delete the parent PE if it doesn't have associated
  369. * child PEs and EEH devices.
  370. */
  371. while (1) {
  372. parent = pe->parent;
  373. /* PHB PEs should never be removed */
  374. if (pe->type & EEH_PE_PHB)
  375. break;
  376. /*
  377. * XXX: KEEP is set while resetting a PE. I don't think it's
  378. * ever set without RECOVERING also being set. I could
  379. * be wrong though so catch that with a WARN.
  380. */
  381. keep = !!(pe->state & EEH_PE_KEEP);
  382. recover = !!(pe->state & EEH_PE_RECOVERING);
  383. WARN_ON(keep && !recover);
  384. if (!keep && !recover) {
  385. if (list_empty(&pe->edevs) &&
  386. list_empty(&pe->child_list)) {
  387. list_del(&pe->child);
  388. kfree(pe);
  389. } else {
  390. break;
  391. }
  392. } else {
  393. /*
  394. * Mark the PE as invalid. At the end of the recovery
  395. * process any invalid PEs will be garbage collected.
  396. *
  397. * We need to delay the free()ing of them since we can
  398. * remove edev's while traversing the PE tree which
  399. * might trigger the removal of a PE and we can't
  400. * deal with that (yet).
  401. */
  402. if (list_empty(&pe->edevs)) {
  403. cnt = 0;
  404. list_for_each_entry(child, &pe->child_list, child) {
  405. if (!(child->type & EEH_PE_INVALID)) {
  406. cnt++;
  407. break;
  408. }
  409. }
  410. if (!cnt)
  411. pe->type |= EEH_PE_INVALID;
  412. else
  413. break;
  414. }
  415. }
  416. pe = parent;
  417. }
  418. return 0;
  419. }
  420. /**
  421. * eeh_pe_update_time_stamp - Update PE's frozen time stamp
  422. * @pe: EEH PE
  423. *
  424. * We have time stamp for each PE to trace its time of getting
  425. * frozen in last hour. The function should be called to update
  426. * the time stamp on first error of the specific PE. On the other
  427. * handle, we needn't account for errors happened in last hour.
  428. */
  429. void eeh_pe_update_time_stamp(struct eeh_pe *pe)
  430. {
  431. time64_t tstamp;
  432. if (!pe) return;
  433. if (pe->freeze_count <= 0) {
  434. pe->freeze_count = 0;
  435. pe->tstamp = ktime_get_seconds();
  436. } else {
  437. tstamp = ktime_get_seconds();
  438. if (tstamp - pe->tstamp > 3600) {
  439. pe->tstamp = tstamp;
  440. pe->freeze_count = 0;
  441. }
  442. }
  443. }
  444. /**
  445. * eeh_pe_state_mark - Mark specified state for PE and its associated device
  446. * @pe: EEH PE
  447. *
  448. * EEH error affects the current PE and its child PEs. The function
  449. * is used to mark appropriate state for the affected PEs and the
  450. * associated devices.
  451. */
  452. void eeh_pe_state_mark(struct eeh_pe *root, int state)
  453. {
  454. struct eeh_pe *pe;
  455. eeh_for_each_pe(root, pe)
  456. if (!(pe->state & EEH_PE_REMOVED))
  457. pe->state |= state;
  458. }
  459. EXPORT_SYMBOL_GPL(eeh_pe_state_mark);
  460. /**
  461. * eeh_pe_mark_isolated
  462. * @pe: EEH PE
  463. *
  464. * Record that a PE has been isolated by marking the PE and it's children as
  465. * EEH_PE_ISOLATED (and EEH_PE_CFG_BLOCKED, if required) and their PCI devices
  466. * as pci_channel_io_frozen.
  467. */
  468. void eeh_pe_mark_isolated(struct eeh_pe *root)
  469. {
  470. struct eeh_pe *pe;
  471. struct eeh_dev *edev;
  472. struct pci_dev *pdev;
  473. eeh_pe_state_mark(root, EEH_PE_ISOLATED);
  474. eeh_for_each_pe(root, pe) {
  475. list_for_each_entry(edev, &pe->edevs, entry) {
  476. pdev = eeh_dev_to_pci_dev(edev);
  477. if (pdev)
  478. pdev->error_state = pci_channel_io_frozen;
  479. }
  480. /* Block PCI config access if required */
  481. if (pe->state & EEH_PE_CFG_RESTRICTED)
  482. pe->state |= EEH_PE_CFG_BLOCKED;
  483. }
  484. }
  485. EXPORT_SYMBOL_GPL(eeh_pe_mark_isolated);
  486. static void __eeh_pe_dev_mode_mark(struct eeh_dev *edev, void *flag)
  487. {
  488. int mode = *((int *)flag);
  489. edev->mode |= mode;
  490. }
  491. /**
  492. * eeh_pe_dev_state_mark - Mark state for all device under the PE
  493. * @pe: EEH PE
  494. *
  495. * Mark specific state for all child devices of the PE.
  496. */
  497. void eeh_pe_dev_mode_mark(struct eeh_pe *pe, int mode)
  498. {
  499. eeh_pe_dev_traverse(pe, __eeh_pe_dev_mode_mark, &mode);
  500. }
  501. /**
  502. * eeh_pe_state_clear - Clear state for the PE
  503. * @data: EEH PE
  504. * @state: state
  505. * @include_passed: include passed-through devices?
  506. *
  507. * The function is used to clear the indicated state from the
  508. * given PE. Besides, we also clear the check count of the PE
  509. * as well.
  510. */
  511. void eeh_pe_state_clear(struct eeh_pe *root, int state, bool include_passed)
  512. {
  513. struct eeh_pe *pe;
  514. struct eeh_dev *edev, *tmp;
  515. struct pci_dev *pdev;
  516. eeh_for_each_pe(root, pe) {
  517. /* Keep the state of permanently removed PE intact */
  518. if (pe->state & EEH_PE_REMOVED)
  519. continue;
  520. if (!include_passed && eeh_pe_passed(pe))
  521. continue;
  522. pe->state &= ~state;
  523. /*
  524. * Special treatment on clearing isolated state. Clear
  525. * check count since last isolation and put all affected
  526. * devices to normal state.
  527. */
  528. if (!(state & EEH_PE_ISOLATED))
  529. continue;
  530. pe->check_count = 0;
  531. eeh_pe_for_each_dev(pe, edev, tmp) {
  532. pdev = eeh_dev_to_pci_dev(edev);
  533. if (!pdev)
  534. continue;
  535. pdev->error_state = pci_channel_io_normal;
  536. }
  537. /* Unblock PCI config access if required */
  538. if (pe->state & EEH_PE_CFG_RESTRICTED)
  539. pe->state &= ~EEH_PE_CFG_BLOCKED;
  540. }
  541. }
  542. /*
  543. * Some PCI bridges (e.g. PLX bridges) have primary/secondary
  544. * buses assigned explicitly by firmware, and we probably have
  545. * lost that after reset. So we have to delay the check until
  546. * the PCI-CFG registers have been restored for the parent
  547. * bridge.
  548. *
  549. * Don't use normal PCI-CFG accessors, which probably has been
  550. * blocked on normal path during the stage. So we need utilize
  551. * eeh operations, which is always permitted.
  552. */
  553. static void eeh_bridge_check_link(struct eeh_dev *edev)
  554. {
  555. int cap;
  556. uint32_t val;
  557. int timeout = 0;
  558. /*
  559. * We only check root port and downstream ports of
  560. * PCIe switches
  561. */
  562. if (!(edev->mode & (EEH_DEV_ROOT_PORT | EEH_DEV_DS_PORT)))
  563. return;
  564. eeh_edev_dbg(edev, "Checking PCIe link...\n");
  565. /* Check slot status */
  566. cap = edev->pcie_cap;
  567. eeh_ops->read_config(edev, cap + PCI_EXP_SLTSTA, 2, &val);
  568. if (!(val & PCI_EXP_SLTSTA_PDS)) {
  569. eeh_edev_dbg(edev, "No card in the slot (0x%04x) !\n", val);
  570. return;
  571. }
  572. /* Check power status if we have the capability */
  573. eeh_ops->read_config(edev, cap + PCI_EXP_SLTCAP, 2, &val);
  574. if (val & PCI_EXP_SLTCAP_PCP) {
  575. eeh_ops->read_config(edev, cap + PCI_EXP_SLTCTL, 2, &val);
  576. if (val & PCI_EXP_SLTCTL_PCC) {
  577. eeh_edev_dbg(edev, "In power-off state, power it on ...\n");
  578. val &= ~(PCI_EXP_SLTCTL_PCC | PCI_EXP_SLTCTL_PIC);
  579. val |= (0x0100 & PCI_EXP_SLTCTL_PIC);
  580. eeh_ops->write_config(edev, cap + PCI_EXP_SLTCTL, 2, val);
  581. msleep(2 * 1000);
  582. }
  583. }
  584. /* Enable link */
  585. eeh_ops->read_config(edev, cap + PCI_EXP_LNKCTL, 2, &val);
  586. val &= ~PCI_EXP_LNKCTL_LD;
  587. eeh_ops->write_config(edev, cap + PCI_EXP_LNKCTL, 2, val);
  588. /* Check link */
  589. eeh_ops->read_config(edev, cap + PCI_EXP_LNKCAP, 4, &val);
  590. if (!(val & PCI_EXP_LNKCAP_DLLLARC)) {
  591. eeh_edev_dbg(edev, "No link reporting capability (0x%08x) \n", val);
  592. msleep(1000);
  593. return;
  594. }
  595. /* Wait the link is up until timeout (5s) */
  596. timeout = 0;
  597. while (timeout < 5000) {
  598. msleep(20);
  599. timeout += 20;
  600. eeh_ops->read_config(edev, cap + PCI_EXP_LNKSTA, 2, &val);
  601. if (val & PCI_EXP_LNKSTA_DLLLA)
  602. break;
  603. }
  604. if (val & PCI_EXP_LNKSTA_DLLLA)
  605. eeh_edev_dbg(edev, "Link up (%s)\n",
  606. (val & PCI_EXP_LNKSTA_CLS_2_5GB) ? "2.5GB" : "5GB");
  607. else
  608. eeh_edev_dbg(edev, "Link not ready (0x%04x)\n", val);
  609. }
  610. #define BYTE_SWAP(OFF) (8*((OFF)/4)+3-(OFF))
  611. #define SAVED_BYTE(OFF) (((u8 *)(edev->config_space))[BYTE_SWAP(OFF)])
  612. static void eeh_restore_bridge_bars(struct eeh_dev *edev)
  613. {
  614. int i;
  615. /*
  616. * Device BARs: 0x10 - 0x18
  617. * Bus numbers and windows: 0x18 - 0x30
  618. */
  619. for (i = 4; i < 13; i++)
  620. eeh_ops->write_config(edev, i*4, 4, edev->config_space[i]);
  621. /* Rom: 0x38 */
  622. eeh_ops->write_config(edev, 14*4, 4, edev->config_space[14]);
  623. /* Cache line & Latency timer: 0xC 0xD */
  624. eeh_ops->write_config(edev, PCI_CACHE_LINE_SIZE, 1,
  625. SAVED_BYTE(PCI_CACHE_LINE_SIZE));
  626. eeh_ops->write_config(edev, PCI_LATENCY_TIMER, 1,
  627. SAVED_BYTE(PCI_LATENCY_TIMER));
  628. /* Max latency, min grant, interrupt ping and line: 0x3C */
  629. eeh_ops->write_config(edev, 15*4, 4, edev->config_space[15]);
  630. /* PCI Command: 0x4 */
  631. eeh_ops->write_config(edev, PCI_COMMAND, 4, edev->config_space[1] |
  632. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  633. /* Check the PCIe link is ready */
  634. eeh_bridge_check_link(edev);
  635. }
  636. static void eeh_restore_device_bars(struct eeh_dev *edev)
  637. {
  638. int i;
  639. u32 cmd;
  640. for (i = 4; i < 10; i++)
  641. eeh_ops->write_config(edev, i*4, 4, edev->config_space[i]);
  642. /* 12 == Expansion ROM Address */
  643. eeh_ops->write_config(edev, 12*4, 4, edev->config_space[12]);
  644. eeh_ops->write_config(edev, PCI_CACHE_LINE_SIZE, 1,
  645. SAVED_BYTE(PCI_CACHE_LINE_SIZE));
  646. eeh_ops->write_config(edev, PCI_LATENCY_TIMER, 1,
  647. SAVED_BYTE(PCI_LATENCY_TIMER));
  648. /* max latency, min grant, interrupt pin and line */
  649. eeh_ops->write_config(edev, 15*4, 4, edev->config_space[15]);
  650. /*
  651. * Restore PERR & SERR bits, some devices require it,
  652. * don't touch the other command bits
  653. */
  654. eeh_ops->read_config(edev, PCI_COMMAND, 4, &cmd);
  655. if (edev->config_space[1] & PCI_COMMAND_PARITY)
  656. cmd |= PCI_COMMAND_PARITY;
  657. else
  658. cmd &= ~PCI_COMMAND_PARITY;
  659. if (edev->config_space[1] & PCI_COMMAND_SERR)
  660. cmd |= PCI_COMMAND_SERR;
  661. else
  662. cmd &= ~PCI_COMMAND_SERR;
  663. eeh_ops->write_config(edev, PCI_COMMAND, 4, cmd);
  664. }
  665. /**
  666. * eeh_restore_one_device_bars - Restore the Base Address Registers for one device
  667. * @data: EEH device
  668. * @flag: Unused
  669. *
  670. * Loads the PCI configuration space base address registers,
  671. * the expansion ROM base address, the latency timer, and etc.
  672. * from the saved values in the device node.
  673. */
  674. static void eeh_restore_one_device_bars(struct eeh_dev *edev, void *flag)
  675. {
  676. /* Do special restore for bridges */
  677. if (edev->mode & EEH_DEV_BRIDGE)
  678. eeh_restore_bridge_bars(edev);
  679. else
  680. eeh_restore_device_bars(edev);
  681. if (eeh_ops->restore_config)
  682. eeh_ops->restore_config(edev);
  683. }
  684. /**
  685. * eeh_pe_restore_bars - Restore the PCI config space info
  686. * @pe: EEH PE
  687. *
  688. * This routine performs a recursive walk to the children
  689. * of this device as well.
  690. */
  691. void eeh_pe_restore_bars(struct eeh_pe *pe)
  692. {
  693. /*
  694. * We needn't take the EEH lock since eeh_pe_dev_traverse()
  695. * will take that.
  696. */
  697. eeh_pe_dev_traverse(pe, eeh_restore_one_device_bars, NULL);
  698. }
  699. /**
  700. * eeh_pe_loc_get - Retrieve location code binding to the given PE
  701. * @pe: EEH PE
  702. *
  703. * Retrieve the location code of the given PE. If the primary PE bus
  704. * is root bus, we will grab location code from PHB device tree node
  705. * or root port. Otherwise, the upstream bridge's device tree node
  706. * of the primary PE bus will be checked for the location code.
  707. */
  708. const char *eeh_pe_loc_get(struct eeh_pe *pe)
  709. {
  710. struct pci_bus *bus = eeh_pe_bus_get(pe);
  711. struct device_node *dn;
  712. const char *loc = NULL;
  713. while (bus) {
  714. dn = pci_bus_to_OF_node(bus);
  715. if (!dn) {
  716. bus = bus->parent;
  717. continue;
  718. }
  719. if (pci_is_root_bus(bus))
  720. loc = of_get_property(dn, "ibm,io-base-loc-code", NULL);
  721. else
  722. loc = of_get_property(dn, "ibm,slot-location-code",
  723. NULL);
  724. if (loc)
  725. return loc;
  726. bus = bus->parent;
  727. }
  728. return "N/A";
  729. }
  730. /**
  731. * eeh_pe_bus_get - Retrieve PCI bus according to the given PE
  732. * @pe: EEH PE
  733. *
  734. * Retrieve the PCI bus according to the given PE. Basically,
  735. * there're 3 types of PEs: PHB/Bus/Device. For PHB PE, the
  736. * primary PCI bus will be retrieved. The parent bus will be
  737. * returned for BUS PE. However, we don't have associated PCI
  738. * bus for DEVICE PE.
  739. */
  740. struct pci_bus *eeh_pe_bus_get(struct eeh_pe *pe)
  741. {
  742. struct eeh_dev *edev;
  743. struct pci_dev *pdev;
  744. if (pe->type & EEH_PE_PHB)
  745. return pe->phb->bus;
  746. /* The primary bus might be cached during probe time */
  747. if (pe->state & EEH_PE_PRI_BUS)
  748. return pe->bus;
  749. /* Retrieve the parent PCI bus of first (top) PCI device */
  750. edev = list_first_entry_or_null(&pe->edevs, struct eeh_dev, entry);
  751. pdev = eeh_dev_to_pci_dev(edev);
  752. if (pdev)
  753. return pdev->bus;
  754. return NULL;
  755. }