eeh_pe.c 22 KB

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