eeh_pe.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*
  2. * The file intends to implement PE based on the information from
  3. * platforms. Basically, there have 3 types of PEs: PHB/Bus/Device.
  4. * All the PEs should be organized as hierarchy tree. The first level
  5. * of the tree will be associated to existing PHBs since the particular
  6. * PE is only meaningful in one PHB domain.
  7. *
  8. * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2012.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/delay.h>
  25. #include <linux/export.h>
  26. #include <linux/gfp.h>
  27. #include <linux/init.h>
  28. #include <linux/kernel.h>
  29. #include <linux/pci.h>
  30. #include <linux/string.h>
  31. #include <asm/pci-bridge.h>
  32. #include <asm/ppc-pci.h>
  33. static LIST_HEAD(eeh_phb_pe);
  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. /* Allocate PHB PE */
  45. pe = kzalloc(sizeof(struct eeh_pe), GFP_KERNEL);
  46. if (!pe) return NULL;
  47. /* Initialize PHB PE */
  48. pe->type = type;
  49. pe->phb = phb;
  50. INIT_LIST_HEAD(&pe->child_list);
  51. INIT_LIST_HEAD(&pe->child);
  52. INIT_LIST_HEAD(&pe->edevs);
  53. return pe;
  54. }
  55. /**
  56. * eeh_phb_pe_create - Create PHB PE
  57. * @phb: PCI controller
  58. *
  59. * The function should be called while the PHB is detected during
  60. * system boot or PCI hotplug in order to create PHB PE.
  61. */
  62. int eeh_phb_pe_create(struct pci_controller *phb)
  63. {
  64. struct eeh_pe *pe;
  65. /* Allocate PHB PE */
  66. pe = eeh_pe_alloc(phb, EEH_PE_PHB);
  67. if (!pe) {
  68. pr_err("%s: out of memory!\n", __func__);
  69. return -ENOMEM;
  70. }
  71. /* Put it into the list */
  72. list_add_tail(&pe->child, &eeh_phb_pe);
  73. pr_debug("EEH: Add PE for PHB#%d\n", phb->global_number);
  74. return 0;
  75. }
  76. /**
  77. * eeh_phb_pe_get - Retrieve PHB PE based on the given PHB
  78. * @phb: PCI controller
  79. *
  80. * The overall PEs form hierarchy tree. The first layer of the
  81. * hierarchy tree is composed of PHB PEs. The function is used
  82. * to retrieve the corresponding PHB PE according to the given PHB.
  83. */
  84. struct eeh_pe *eeh_phb_pe_get(struct pci_controller *phb)
  85. {
  86. struct eeh_pe *pe;
  87. list_for_each_entry(pe, &eeh_phb_pe, child) {
  88. /*
  89. * Actually, we needn't check the type since
  90. * the PE for PHB has been determined when that
  91. * was created.
  92. */
  93. if ((pe->type & EEH_PE_PHB) && pe->phb == phb)
  94. return pe;
  95. }
  96. return NULL;
  97. }
  98. /**
  99. * eeh_pe_next - Retrieve the next PE in the tree
  100. * @pe: current PE
  101. * @root: root PE
  102. *
  103. * The function is used to retrieve the next PE in the
  104. * hierarchy PE tree.
  105. */
  106. static struct eeh_pe *eeh_pe_next(struct eeh_pe *pe,
  107. struct eeh_pe *root)
  108. {
  109. struct list_head *next = pe->child_list.next;
  110. if (next == &pe->child_list) {
  111. while (1) {
  112. if (pe == root)
  113. return NULL;
  114. next = pe->child.next;
  115. if (next != &pe->parent->child_list)
  116. break;
  117. pe = pe->parent;
  118. }
  119. }
  120. return list_entry(next, struct eeh_pe, child);
  121. }
  122. /**
  123. * eeh_pe_traverse - Traverse PEs in the specified PHB
  124. * @root: root PE
  125. * @fn: callback
  126. * @flag: extra parameter to callback
  127. *
  128. * The function is used to traverse the specified PE and its
  129. * child PEs. The traversing is to be terminated once the
  130. * callback returns something other than NULL, or no more PEs
  131. * to be traversed.
  132. */
  133. void *eeh_pe_traverse(struct eeh_pe *root,
  134. eeh_traverse_func fn, void *flag)
  135. {
  136. struct eeh_pe *pe;
  137. void *ret;
  138. for (pe = root; pe; pe = eeh_pe_next(pe, root)) {
  139. ret = fn(pe, flag);
  140. if (ret) return ret;
  141. }
  142. return NULL;
  143. }
  144. /**
  145. * eeh_pe_dev_traverse - Traverse the devices from the PE
  146. * @root: EEH PE
  147. * @fn: function callback
  148. * @flag: extra parameter to callback
  149. *
  150. * The function is used to traverse the devices of the specified
  151. * PE and its child PEs.
  152. */
  153. void *eeh_pe_dev_traverse(struct eeh_pe *root,
  154. eeh_traverse_func fn, void *flag)
  155. {
  156. struct eeh_pe *pe;
  157. struct eeh_dev *edev, *tmp;
  158. void *ret;
  159. if (!root) {
  160. pr_warning("%s: Invalid PE %p\n", __func__, root);
  161. return NULL;
  162. }
  163. /* Traverse root PE */
  164. for (pe = root; pe; pe = eeh_pe_next(pe, root)) {
  165. eeh_pe_for_each_dev(pe, edev, tmp) {
  166. ret = fn(edev, flag);
  167. if (ret)
  168. return ret;
  169. }
  170. }
  171. return NULL;
  172. }
  173. /**
  174. * __eeh_pe_get - Check the PE address
  175. * @data: EEH PE
  176. * @flag: EEH device
  177. *
  178. * For one particular PE, it can be identified by PE address
  179. * or tranditional BDF address. BDF address is composed of
  180. * Bus/Device/Function number. The extra data referred by flag
  181. * indicates which type of address should be used.
  182. */
  183. static void *__eeh_pe_get(void *data, void *flag)
  184. {
  185. struct eeh_pe *pe = (struct eeh_pe *)data;
  186. struct eeh_dev *edev = (struct eeh_dev *)flag;
  187. /* Unexpected PHB PE */
  188. if (pe->type & EEH_PE_PHB)
  189. return NULL;
  190. /* We prefer PE address */
  191. if (edev->pe_config_addr &&
  192. (edev->pe_config_addr == pe->addr))
  193. return pe;
  194. /* Try BDF address */
  195. if (edev->config_addr &&
  196. (edev->config_addr == pe->config_addr))
  197. return pe;
  198. return NULL;
  199. }
  200. /**
  201. * eeh_pe_get - Search PE based on the given address
  202. * @edev: EEH device
  203. *
  204. * Search the corresponding PE based on the specified address which
  205. * is included in the eeh device. The function is used to check if
  206. * the associated PE has been created against the PE address. It's
  207. * notable that the PE address has 2 format: traditional PE address
  208. * which is composed of PCI bus/device/function number, or unified
  209. * PE address.
  210. */
  211. struct eeh_pe *eeh_pe_get(struct eeh_dev *edev)
  212. {
  213. struct eeh_pe *root = eeh_phb_pe_get(edev->phb);
  214. struct eeh_pe *pe;
  215. pe = eeh_pe_traverse(root, __eeh_pe_get, edev);
  216. return pe;
  217. }
  218. /**
  219. * eeh_pe_get_parent - Retrieve the parent PE
  220. * @edev: EEH device
  221. *
  222. * The whole PEs existing in the system are organized as hierarchy
  223. * tree. The function is used to retrieve the parent PE according
  224. * to the parent EEH device.
  225. */
  226. static struct eeh_pe *eeh_pe_get_parent(struct eeh_dev *edev)
  227. {
  228. struct device_node *dn;
  229. struct eeh_dev *parent;
  230. /*
  231. * It might have the case for the indirect parent
  232. * EEH device already having associated PE, but
  233. * the direct parent EEH device doesn't have yet.
  234. */
  235. dn = edev->dn->parent;
  236. while (dn) {
  237. /* We're poking out of PCI territory */
  238. if (!PCI_DN(dn)) return NULL;
  239. parent = of_node_to_eeh_dev(dn);
  240. /* We're poking out of PCI territory */
  241. if (!parent) return NULL;
  242. if (parent->pe)
  243. return parent->pe;
  244. dn = dn->parent;
  245. }
  246. return NULL;
  247. }
  248. /**
  249. * eeh_add_to_parent_pe - Add EEH device to parent PE
  250. * @edev: EEH device
  251. *
  252. * Add EEH device to the parent PE. If the parent PE already
  253. * exists, the PE type will be changed to EEH_PE_BUS. Otherwise,
  254. * we have to create new PE to hold the EEH device and the new
  255. * PE will be linked to its parent PE as well.
  256. */
  257. int eeh_add_to_parent_pe(struct eeh_dev *edev)
  258. {
  259. struct eeh_pe *pe, *parent;
  260. /*
  261. * Search the PE has been existing or not according
  262. * to the PE address. If that has been existing, the
  263. * PE should be composed of PCI bus and its subordinate
  264. * components.
  265. */
  266. pe = eeh_pe_get(edev);
  267. if (pe && !(pe->type & EEH_PE_INVALID)) {
  268. if (!edev->pe_config_addr) {
  269. pr_err("%s: PE with addr 0x%x already exists\n",
  270. __func__, edev->config_addr);
  271. return -EEXIST;
  272. }
  273. /* Mark the PE as type of PCI bus */
  274. pe->type = EEH_PE_BUS;
  275. edev->pe = pe;
  276. /* Put the edev to PE */
  277. list_add_tail(&edev->list, &pe->edevs);
  278. pr_debug("EEH: Add %s to Bus PE#%x\n",
  279. edev->dn->full_name, pe->addr);
  280. return 0;
  281. } else if (pe && (pe->type & EEH_PE_INVALID)) {
  282. list_add_tail(&edev->list, &pe->edevs);
  283. edev->pe = pe;
  284. /*
  285. * We're running to here because of PCI hotplug caused by
  286. * EEH recovery. We need clear EEH_PE_INVALID until the top.
  287. */
  288. parent = pe;
  289. while (parent) {
  290. if (!(parent->type & EEH_PE_INVALID))
  291. break;
  292. parent->type &= ~(EEH_PE_INVALID | EEH_PE_KEEP);
  293. parent = parent->parent;
  294. }
  295. pr_debug("EEH: Add %s to Device PE#%x, Parent PE#%x\n",
  296. edev->dn->full_name, pe->addr, pe->parent->addr);
  297. return 0;
  298. }
  299. /* Create a new EEH PE */
  300. pe = eeh_pe_alloc(edev->phb, EEH_PE_DEVICE);
  301. if (!pe) {
  302. pr_err("%s: out of memory!\n", __func__);
  303. return -ENOMEM;
  304. }
  305. pe->addr = edev->pe_config_addr;
  306. pe->config_addr = edev->config_addr;
  307. /*
  308. * While doing PE reset, we probably hot-reset the
  309. * upstream bridge. However, the PCI devices including
  310. * the associated EEH devices might be removed when EEH
  311. * core is doing recovery. So that won't safe to retrieve
  312. * the bridge through downstream EEH device. We have to
  313. * trace the parent PCI bus, then the upstream bridge.
  314. */
  315. if (eeh_probe_mode_dev())
  316. pe->bus = eeh_dev_to_pci_dev(edev)->bus;
  317. /*
  318. * Put the new EEH PE into hierarchy tree. If the parent
  319. * can't be found, the newly created PE will be attached
  320. * to PHB directly. Otherwise, we have to associate the
  321. * PE with its parent.
  322. */
  323. parent = eeh_pe_get_parent(edev);
  324. if (!parent) {
  325. parent = eeh_phb_pe_get(edev->phb);
  326. if (!parent) {
  327. pr_err("%s: No PHB PE is found (PHB Domain=%d)\n",
  328. __func__, edev->phb->global_number);
  329. edev->pe = NULL;
  330. kfree(pe);
  331. return -EEXIST;
  332. }
  333. }
  334. pe->parent = parent;
  335. /*
  336. * Put the newly created PE into the child list and
  337. * link the EEH device accordingly.
  338. */
  339. list_add_tail(&pe->child, &parent->child_list);
  340. list_add_tail(&edev->list, &pe->edevs);
  341. edev->pe = pe;
  342. pr_debug("EEH: Add %s to Device PE#%x, Parent PE#%x\n",
  343. edev->dn->full_name, pe->addr, pe->parent->addr);
  344. return 0;
  345. }
  346. /**
  347. * eeh_rmv_from_parent_pe - Remove one EEH device from the associated PE
  348. * @edev: EEH device
  349. *
  350. * The PE hierarchy tree might be changed when doing PCI hotplug.
  351. * Also, the PCI devices or buses could be removed from the system
  352. * during EEH recovery. So we have to call the function remove the
  353. * corresponding PE accordingly if necessary.
  354. */
  355. int eeh_rmv_from_parent_pe(struct eeh_dev *edev)
  356. {
  357. struct eeh_pe *pe, *parent, *child;
  358. int cnt;
  359. if (!edev->pe) {
  360. pr_debug("%s: No PE found for EEH device %s\n",
  361. __func__, edev->dn->full_name);
  362. return -EEXIST;
  363. }
  364. /* Remove the EEH device */
  365. pe = edev->pe;
  366. edev->pe = NULL;
  367. list_del(&edev->list);
  368. /*
  369. * Check if the parent PE includes any EEH devices.
  370. * If not, we should delete that. Also, we should
  371. * delete the parent PE if it doesn't have associated
  372. * child PEs and EEH devices.
  373. */
  374. while (1) {
  375. parent = pe->parent;
  376. if (pe->type & EEH_PE_PHB)
  377. break;
  378. if (!(pe->state & EEH_PE_KEEP)) {
  379. if (list_empty(&pe->edevs) &&
  380. list_empty(&pe->child_list)) {
  381. list_del(&pe->child);
  382. kfree(pe);
  383. } else {
  384. break;
  385. }
  386. } else {
  387. if (list_empty(&pe->edevs)) {
  388. cnt = 0;
  389. list_for_each_entry(child, &pe->child_list, child) {
  390. if (!(child->type & EEH_PE_INVALID)) {
  391. cnt++;
  392. break;
  393. }
  394. }
  395. if (!cnt)
  396. pe->type |= EEH_PE_INVALID;
  397. else
  398. break;
  399. }
  400. }
  401. pe = parent;
  402. }
  403. return 0;
  404. }
  405. /**
  406. * eeh_pe_update_time_stamp - Update PE's frozen time stamp
  407. * @pe: EEH PE
  408. *
  409. * We have time stamp for each PE to trace its time of getting
  410. * frozen in last hour. The function should be called to update
  411. * the time stamp on first error of the specific PE. On the other
  412. * handle, we needn't account for errors happened in last hour.
  413. */
  414. void eeh_pe_update_time_stamp(struct eeh_pe *pe)
  415. {
  416. struct timeval tstamp;
  417. if (!pe) return;
  418. if (pe->freeze_count <= 0) {
  419. pe->freeze_count = 0;
  420. do_gettimeofday(&pe->tstamp);
  421. } else {
  422. do_gettimeofday(&tstamp);
  423. if (tstamp.tv_sec - pe->tstamp.tv_sec > 3600) {
  424. pe->tstamp = tstamp;
  425. pe->freeze_count = 0;
  426. }
  427. }
  428. }
  429. /**
  430. * __eeh_pe_state_mark - Mark the state for the PE
  431. * @data: EEH PE
  432. * @flag: state
  433. *
  434. * The function is used to mark the indicated state for the given
  435. * PE. Also, the associated PCI devices will be put into IO frozen
  436. * state as well.
  437. */
  438. static void *__eeh_pe_state_mark(void *data, void *flag)
  439. {
  440. struct eeh_pe *pe = (struct eeh_pe *)data;
  441. int state = *((int *)flag);
  442. struct eeh_dev *edev, *tmp;
  443. struct pci_dev *pdev;
  444. /*
  445. * Mark the PE with the indicated state. Also,
  446. * the associated PCI device will be put into
  447. * I/O frozen state to avoid I/O accesses from
  448. * the PCI device driver.
  449. */
  450. pe->state |= state;
  451. eeh_pe_for_each_dev(pe, edev, tmp) {
  452. pdev = eeh_dev_to_pci_dev(edev);
  453. if (pdev)
  454. pdev->error_state = pci_channel_io_frozen;
  455. }
  456. return NULL;
  457. }
  458. /**
  459. * eeh_pe_state_mark - Mark specified state for PE and its associated device
  460. * @pe: EEH PE
  461. *
  462. * EEH error affects the current PE and its child PEs. The function
  463. * is used to mark appropriate state for the affected PEs and the
  464. * associated devices.
  465. */
  466. void eeh_pe_state_mark(struct eeh_pe *pe, int state)
  467. {
  468. eeh_pe_traverse(pe, __eeh_pe_state_mark, &state);
  469. }
  470. /**
  471. * __eeh_pe_state_clear - Clear state for the PE
  472. * @data: EEH PE
  473. * @flag: state
  474. *
  475. * The function is used to clear the indicated state from the
  476. * given PE. Besides, we also clear the check count of the PE
  477. * as well.
  478. */
  479. static void *__eeh_pe_state_clear(void *data, void *flag)
  480. {
  481. struct eeh_pe *pe = (struct eeh_pe *)data;
  482. int state = *((int *)flag);
  483. pe->state &= ~state;
  484. pe->check_count = 0;
  485. return NULL;
  486. }
  487. /**
  488. * eeh_pe_state_clear - Clear state for the PE and its children
  489. * @pe: PE
  490. * @state: state to be cleared
  491. *
  492. * When the PE and its children has been recovered from error,
  493. * we need clear the error state for that. The function is used
  494. * for the purpose.
  495. */
  496. void eeh_pe_state_clear(struct eeh_pe *pe, int state)
  497. {
  498. eeh_pe_traverse(pe, __eeh_pe_state_clear, &state);
  499. }
  500. /*
  501. * Some PCI bridges (e.g. PLX bridges) have primary/secondary
  502. * buses assigned explicitly by firmware, and we probably have
  503. * lost that after reset. So we have to delay the check until
  504. * the PCI-CFG registers have been restored for the parent
  505. * bridge.
  506. *
  507. * Don't use normal PCI-CFG accessors, which probably has been
  508. * blocked on normal path during the stage. So we need utilize
  509. * eeh operations, which is always permitted.
  510. */
  511. static void eeh_bridge_check_link(struct eeh_dev *edev,
  512. struct device_node *dn)
  513. {
  514. int cap;
  515. uint32_t val;
  516. int timeout = 0;
  517. /*
  518. * We only check root port and downstream ports of
  519. * PCIe switches
  520. */
  521. if (!(edev->mode & (EEH_DEV_ROOT_PORT | EEH_DEV_DS_PORT)))
  522. return;
  523. pr_debug("%s: Check PCIe link for %04x:%02x:%02x.%01x ...\n",
  524. __func__, edev->phb->global_number,
  525. edev->config_addr >> 8,
  526. PCI_SLOT(edev->config_addr & 0xFF),
  527. PCI_FUNC(edev->config_addr & 0xFF));
  528. /* Check slot status */
  529. cap = edev->pcie_cap;
  530. eeh_ops->read_config(dn, cap + PCI_EXP_SLTSTA, 2, &val);
  531. if (!(val & PCI_EXP_SLTSTA_PDS)) {
  532. pr_debug(" No card in the slot (0x%04x) !\n", val);
  533. return;
  534. }
  535. /* Check power status if we have the capability */
  536. eeh_ops->read_config(dn, cap + PCI_EXP_SLTCAP, 2, &val);
  537. if (val & PCI_EXP_SLTCAP_PCP) {
  538. eeh_ops->read_config(dn, cap + PCI_EXP_SLTCTL, 2, &val);
  539. if (val & PCI_EXP_SLTCTL_PCC) {
  540. pr_debug(" In power-off state, power it on ...\n");
  541. val &= ~(PCI_EXP_SLTCTL_PCC | PCI_EXP_SLTCTL_PIC);
  542. val |= (0x0100 & PCI_EXP_SLTCTL_PIC);
  543. eeh_ops->write_config(dn, cap + PCI_EXP_SLTCTL, 2, val);
  544. msleep(2 * 1000);
  545. }
  546. }
  547. /* Enable link */
  548. eeh_ops->read_config(dn, cap + PCI_EXP_LNKCTL, 2, &val);
  549. val &= ~PCI_EXP_LNKCTL_LD;
  550. eeh_ops->write_config(dn, cap + PCI_EXP_LNKCTL, 2, val);
  551. /* Check link */
  552. eeh_ops->read_config(dn, cap + PCI_EXP_LNKCAP, 4, &val);
  553. if (!(val & PCI_EXP_LNKCAP_DLLLARC)) {
  554. pr_debug(" No link reporting capability (0x%08x) \n", val);
  555. msleep(1000);
  556. return;
  557. }
  558. /* Wait the link is up until timeout (5s) */
  559. timeout = 0;
  560. while (timeout < 5000) {
  561. msleep(20);
  562. timeout += 20;
  563. eeh_ops->read_config(dn, cap + PCI_EXP_LNKSTA, 2, &val);
  564. if (val & PCI_EXP_LNKSTA_DLLLA)
  565. break;
  566. }
  567. if (val & PCI_EXP_LNKSTA_DLLLA)
  568. pr_debug(" Link up (%s)\n",
  569. (val & PCI_EXP_LNKSTA_CLS_2_5GB) ? "2.5GB" : "5GB");
  570. else
  571. pr_debug(" Link not ready (0x%04x)\n", val);
  572. }
  573. #define BYTE_SWAP(OFF) (8*((OFF)/4)+3-(OFF))
  574. #define SAVED_BYTE(OFF) (((u8 *)(edev->config_space))[BYTE_SWAP(OFF)])
  575. static void eeh_restore_bridge_bars(struct eeh_dev *edev,
  576. struct device_node *dn)
  577. {
  578. int i;
  579. /*
  580. * Device BARs: 0x10 - 0x18
  581. * Bus numbers and windows: 0x18 - 0x30
  582. */
  583. for (i = 4; i < 13; i++)
  584. eeh_ops->write_config(dn, i*4, 4, edev->config_space[i]);
  585. /* Rom: 0x38 */
  586. eeh_ops->write_config(dn, 14*4, 4, edev->config_space[14]);
  587. /* Cache line & Latency timer: 0xC 0xD */
  588. eeh_ops->write_config(dn, PCI_CACHE_LINE_SIZE, 1,
  589. SAVED_BYTE(PCI_CACHE_LINE_SIZE));
  590. eeh_ops->write_config(dn, PCI_LATENCY_TIMER, 1,
  591. SAVED_BYTE(PCI_LATENCY_TIMER));
  592. /* Max latency, min grant, interrupt ping and line: 0x3C */
  593. eeh_ops->write_config(dn, 15*4, 4, edev->config_space[15]);
  594. /* PCI Command: 0x4 */
  595. eeh_ops->write_config(dn, PCI_COMMAND, 4, edev->config_space[1]);
  596. /* Check the PCIe link is ready */
  597. eeh_bridge_check_link(edev, dn);
  598. }
  599. static void eeh_restore_device_bars(struct eeh_dev *edev,
  600. struct device_node *dn)
  601. {
  602. int i;
  603. u32 cmd;
  604. for (i = 4; i < 10; i++)
  605. eeh_ops->write_config(dn, i*4, 4, edev->config_space[i]);
  606. /* 12 == Expansion ROM Address */
  607. eeh_ops->write_config(dn, 12*4, 4, edev->config_space[12]);
  608. eeh_ops->write_config(dn, PCI_CACHE_LINE_SIZE, 1,
  609. SAVED_BYTE(PCI_CACHE_LINE_SIZE));
  610. eeh_ops->write_config(dn, PCI_LATENCY_TIMER, 1,
  611. SAVED_BYTE(PCI_LATENCY_TIMER));
  612. /* max latency, min grant, interrupt pin and line */
  613. eeh_ops->write_config(dn, 15*4, 4, edev->config_space[15]);
  614. /*
  615. * Restore PERR & SERR bits, some devices require it,
  616. * don't touch the other command bits
  617. */
  618. eeh_ops->read_config(dn, PCI_COMMAND, 4, &cmd);
  619. if (edev->config_space[1] & PCI_COMMAND_PARITY)
  620. cmd |= PCI_COMMAND_PARITY;
  621. else
  622. cmd &= ~PCI_COMMAND_PARITY;
  623. if (edev->config_space[1] & PCI_COMMAND_SERR)
  624. cmd |= PCI_COMMAND_SERR;
  625. else
  626. cmd &= ~PCI_COMMAND_SERR;
  627. eeh_ops->write_config(dn, PCI_COMMAND, 4, cmd);
  628. }
  629. /**
  630. * eeh_restore_one_device_bars - Restore the Base Address Registers for one device
  631. * @data: EEH device
  632. * @flag: Unused
  633. *
  634. * Loads the PCI configuration space base address registers,
  635. * the expansion ROM base address, the latency timer, and etc.
  636. * from the saved values in the device node.
  637. */
  638. static void *eeh_restore_one_device_bars(void *data, void *flag)
  639. {
  640. struct eeh_dev *edev = (struct eeh_dev *)data;
  641. struct device_node *dn = eeh_dev_to_of_node(edev);
  642. /* Do special restore for bridges */
  643. if (edev->mode & EEH_DEV_BRIDGE)
  644. eeh_restore_bridge_bars(edev, dn);
  645. else
  646. eeh_restore_device_bars(edev, dn);
  647. return NULL;
  648. }
  649. /**
  650. * eeh_pe_restore_bars - Restore the PCI config space info
  651. * @pe: EEH PE
  652. *
  653. * This routine performs a recursive walk to the children
  654. * of this device as well.
  655. */
  656. void eeh_pe_restore_bars(struct eeh_pe *pe)
  657. {
  658. /*
  659. * We needn't take the EEH lock since eeh_pe_dev_traverse()
  660. * will take that.
  661. */
  662. eeh_pe_dev_traverse(pe, eeh_restore_one_device_bars, NULL);
  663. }
  664. /**
  665. * eeh_pe_bus_get - Retrieve PCI bus according to the given PE
  666. * @pe: EEH PE
  667. *
  668. * Retrieve the PCI bus according to the given PE. Basically,
  669. * there're 3 types of PEs: PHB/Bus/Device. For PHB PE, the
  670. * primary PCI bus will be retrieved. The parent bus will be
  671. * returned for BUS PE. However, we don't have associated PCI
  672. * bus for DEVICE PE.
  673. */
  674. struct pci_bus *eeh_pe_bus_get(struct eeh_pe *pe)
  675. {
  676. struct pci_bus *bus = NULL;
  677. struct eeh_dev *edev;
  678. struct pci_dev *pdev;
  679. if (pe->type & EEH_PE_PHB) {
  680. bus = pe->phb->bus;
  681. } else if (pe->type & EEH_PE_BUS ||
  682. pe->type & EEH_PE_DEVICE) {
  683. if (pe->bus) {
  684. bus = pe->bus;
  685. goto out;
  686. }
  687. edev = list_first_entry(&pe->edevs, struct eeh_dev, list);
  688. pdev = eeh_dev_to_pci_dev(edev);
  689. if (pdev)
  690. bus = pdev->bus;
  691. }
  692. out:
  693. return bus;
  694. }