eeh_pe.c 22 KB

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