eeh_pe.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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/export.h>
  25. #include <linux/gfp.h>
  26. #include <linux/init.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 __devinit 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. eeh_lock();
  72. list_add_tail(&pe->child, &eeh_phb_pe);
  73. eeh_unlock();
  74. pr_debug("EEH: Add PE for PHB#%d\n", phb->global_number);
  75. return 0;
  76. }
  77. /**
  78. * eeh_phb_pe_get - Retrieve PHB PE based on the given PHB
  79. * @phb: PCI controller
  80. *
  81. * The overall PEs form hierarchy tree. The first layer of the
  82. * hierarchy tree is composed of PHB PEs. The function is used
  83. * to retrieve the corresponding PHB PE according to the given PHB.
  84. */
  85. static struct eeh_pe *eeh_phb_pe_get(struct pci_controller *phb)
  86. {
  87. struct eeh_pe *pe;
  88. list_for_each_entry(pe, &eeh_phb_pe, child) {
  89. /*
  90. * Actually, we needn't check the type since
  91. * the PE for PHB has been determined when that
  92. * was created.
  93. */
  94. if ((pe->type & EEH_PE_PHB) &&
  95. pe->phb == phb) {
  96. eeh_unlock();
  97. return pe;
  98. }
  99. }
  100. return NULL;
  101. }
  102. /**
  103. * eeh_pe_next - Retrieve the next PE in the tree
  104. * @pe: current PE
  105. * @root: root PE
  106. *
  107. * The function is used to retrieve the next PE in the
  108. * hierarchy PE tree.
  109. */
  110. static struct eeh_pe *eeh_pe_next(struct eeh_pe *pe,
  111. struct eeh_pe *root)
  112. {
  113. struct list_head *next = pe->child_list.next;
  114. if (next == &pe->child_list) {
  115. while (1) {
  116. if (pe == root)
  117. return NULL;
  118. next = pe->child.next;
  119. if (next != &pe->parent->child_list)
  120. break;
  121. pe = pe->parent;
  122. }
  123. }
  124. return list_entry(next, struct eeh_pe, child);
  125. }
  126. /**
  127. * eeh_pe_traverse - Traverse PEs in the specified PHB
  128. * @root: root PE
  129. * @fn: callback
  130. * @flag: extra parameter to callback
  131. *
  132. * The function is used to traverse the specified PE and its
  133. * child PEs. The traversing is to be terminated once the
  134. * callback returns something other than NULL, or no more PEs
  135. * to be traversed.
  136. */
  137. static void *eeh_pe_traverse(struct eeh_pe *root,
  138. eeh_traverse_func fn, void *flag)
  139. {
  140. struct eeh_pe *pe;
  141. void *ret;
  142. for (pe = root; pe; pe = eeh_pe_next(pe, root)) {
  143. ret = fn(pe, flag);
  144. if (ret) return ret;
  145. }
  146. return NULL;
  147. }
  148. /**
  149. * eeh_pe_dev_traverse - Traverse the devices from the PE
  150. * @root: EEH PE
  151. * @fn: function callback
  152. * @flag: extra parameter to callback
  153. *
  154. * The function is used to traverse the devices of the specified
  155. * PE and its child PEs.
  156. */
  157. void *eeh_pe_dev_traverse(struct eeh_pe *root,
  158. eeh_traverse_func fn, void *flag)
  159. {
  160. struct eeh_pe *pe;
  161. struct eeh_dev *edev;
  162. void *ret;
  163. if (!root) {
  164. pr_warning("%s: Invalid PE %p\n", __func__, root);
  165. return NULL;
  166. }
  167. eeh_lock();
  168. /* Traverse root PE */
  169. for (pe = root; pe; pe = eeh_pe_next(pe, root)) {
  170. eeh_pe_for_each_dev(pe, edev) {
  171. ret = fn(edev, flag);
  172. if (ret) {
  173. eeh_unlock();
  174. return ret;
  175. }
  176. }
  177. }
  178. eeh_unlock();
  179. return NULL;
  180. }
  181. /**
  182. * __eeh_pe_get - Check the PE address
  183. * @data: EEH PE
  184. * @flag: EEH device
  185. *
  186. * For one particular PE, it can be identified by PE address
  187. * or tranditional BDF address. BDF address is composed of
  188. * Bus/Device/Function number. The extra data referred by flag
  189. * indicates which type of address should be used.
  190. */
  191. static void *__eeh_pe_get(void *data, void *flag)
  192. {
  193. struct eeh_pe *pe = (struct eeh_pe *)data;
  194. struct eeh_dev *edev = (struct eeh_dev *)flag;
  195. /* Unexpected PHB PE */
  196. if (pe->type & EEH_PE_PHB)
  197. return NULL;
  198. /* We prefer PE address */
  199. if (edev->pe_config_addr &&
  200. (edev->pe_config_addr == pe->addr))
  201. return pe;
  202. /* Try BDF address */
  203. if (edev->pe_config_addr &&
  204. (edev->config_addr == pe->config_addr))
  205. return pe;
  206. return NULL;
  207. }
  208. /**
  209. * eeh_pe_get - Search PE based on the given address
  210. * @edev: EEH device
  211. *
  212. * Search the corresponding PE based on the specified address which
  213. * is included in the eeh device. The function is used to check if
  214. * the associated PE has been created against the PE address. It's
  215. * notable that the PE address has 2 format: traditional PE address
  216. * which is composed of PCI bus/device/function number, or unified
  217. * PE address.
  218. */
  219. static struct eeh_pe *eeh_pe_get(struct eeh_dev *edev)
  220. {
  221. struct eeh_pe *root = eeh_phb_pe_get(edev->phb);
  222. struct eeh_pe *pe;
  223. pe = eeh_pe_traverse(root, __eeh_pe_get, edev);
  224. return pe;
  225. }
  226. /**
  227. * eeh_pe_get_parent - Retrieve the parent PE
  228. * @edev: EEH device
  229. *
  230. * The whole PEs existing in the system are organized as hierarchy
  231. * tree. The function is used to retrieve the parent PE according
  232. * to the parent EEH device.
  233. */
  234. static struct eeh_pe *eeh_pe_get_parent(struct eeh_dev *edev)
  235. {
  236. struct device_node *dn;
  237. struct eeh_dev *parent;
  238. /*
  239. * It might have the case for the indirect parent
  240. * EEH device already having associated PE, but
  241. * the direct parent EEH device doesn't have yet.
  242. */
  243. dn = edev->dn->parent;
  244. while (dn) {
  245. /* We're poking out of PCI territory */
  246. if (!PCI_DN(dn)) return NULL;
  247. parent = of_node_to_eeh_dev(dn);
  248. /* We're poking out of PCI territory */
  249. if (!parent) return NULL;
  250. if (parent->pe)
  251. return parent->pe;
  252. dn = dn->parent;
  253. }
  254. return NULL;
  255. }
  256. /**
  257. * eeh_add_to_parent_pe - Add EEH device to parent PE
  258. * @edev: EEH device
  259. *
  260. * Add EEH device to the parent PE. If the parent PE already
  261. * exists, the PE type will be changed to EEH_PE_BUS. Otherwise,
  262. * we have to create new PE to hold the EEH device and the new
  263. * PE will be linked to its parent PE as well.
  264. */
  265. int eeh_add_to_parent_pe(struct eeh_dev *edev)
  266. {
  267. struct eeh_pe *pe, *parent;
  268. eeh_lock();
  269. /*
  270. * Search the PE has been existing or not according
  271. * to the PE address. If that has been existing, the
  272. * PE should be composed of PCI bus and its subordinate
  273. * components.
  274. */
  275. pe = eeh_pe_get(edev);
  276. if (pe && !(pe->type & EEH_PE_INVALID)) {
  277. if (!edev->pe_config_addr) {
  278. eeh_unlock();
  279. pr_err("%s: PE with addr 0x%x already exists\n",
  280. __func__, edev->config_addr);
  281. return -EEXIST;
  282. }
  283. /* Mark the PE as type of PCI bus */
  284. pe->type = EEH_PE_BUS;
  285. edev->pe = pe;
  286. /* Put the edev to PE */
  287. list_add_tail(&edev->list, &pe->edevs);
  288. eeh_unlock();
  289. pr_debug("EEH: Add %s to Bus PE#%x\n",
  290. edev->dn->full_name, pe->addr);
  291. return 0;
  292. } else if (pe && (pe->type & EEH_PE_INVALID)) {
  293. list_add_tail(&edev->list, &pe->edevs);
  294. edev->pe = pe;
  295. /*
  296. * We're running to here because of PCI hotplug caused by
  297. * EEH recovery. We need clear EEH_PE_INVALID until the top.
  298. */
  299. parent = pe;
  300. while (parent) {
  301. if (!(parent->type & EEH_PE_INVALID))
  302. break;
  303. parent->type &= ~EEH_PE_INVALID;
  304. parent = parent->parent;
  305. }
  306. eeh_unlock();
  307. pr_debug("EEH: Add %s to Device PE#%x, Parent PE#%x\n",
  308. edev->dn->full_name, pe->addr, pe->parent->addr);
  309. return 0;
  310. }
  311. /* Create a new EEH PE */
  312. pe = eeh_pe_alloc(edev->phb, EEH_PE_DEVICE);
  313. if (!pe) {
  314. eeh_unlock();
  315. pr_err("%s: out of memory!\n", __func__);
  316. return -ENOMEM;
  317. }
  318. pe->addr = edev->pe_config_addr;
  319. pe->config_addr = edev->config_addr;
  320. /*
  321. * Put the new EEH PE into hierarchy tree. If the parent
  322. * can't be found, the newly created PE will be attached
  323. * to PHB directly. Otherwise, we have to associate the
  324. * PE with its parent.
  325. */
  326. parent = eeh_pe_get_parent(edev);
  327. if (!parent) {
  328. parent = eeh_phb_pe_get(edev->phb);
  329. if (!parent) {
  330. eeh_unlock();
  331. pr_err("%s: No PHB PE is found (PHB Domain=%d)\n",
  332. __func__, edev->phb->global_number);
  333. edev->pe = NULL;
  334. kfree(pe);
  335. return -EEXIST;
  336. }
  337. }
  338. pe->parent = parent;
  339. /*
  340. * Put the newly created PE into the child list and
  341. * link the EEH device accordingly.
  342. */
  343. list_add_tail(&pe->child, &parent->child_list);
  344. list_add_tail(&edev->list, &pe->edevs);
  345. edev->pe = pe;
  346. eeh_unlock();
  347. pr_debug("EEH: Add %s to Device PE#%x, Parent PE#%x\n",
  348. edev->dn->full_name, pe->addr, pe->parent->addr);
  349. return 0;
  350. }
  351. /**
  352. * eeh_rmv_from_parent_pe - Remove one EEH device from the associated PE
  353. * @edev: EEH device
  354. * @purge_pe: remove PE or not
  355. *
  356. * The PE hierarchy tree might be changed when doing PCI hotplug.
  357. * Also, the PCI devices or buses could be removed from the system
  358. * during EEH recovery. So we have to call the function remove the
  359. * corresponding PE accordingly if necessary.
  360. */
  361. int eeh_rmv_from_parent_pe(struct eeh_dev *edev, int purge_pe)
  362. {
  363. struct eeh_pe *pe, *parent, *child;
  364. int cnt;
  365. if (!edev->pe) {
  366. pr_warning("%s: No PE found for EEH device %s\n",
  367. __func__, edev->dn->full_name);
  368. return -EEXIST;
  369. }
  370. eeh_lock();
  371. /* Remove the EEH device */
  372. pe = edev->pe;
  373. edev->pe = NULL;
  374. list_del(&edev->list);
  375. /*
  376. * Check if the parent PE includes any EEH devices.
  377. * If not, we should delete that. Also, we should
  378. * delete the parent PE if it doesn't have associated
  379. * child PEs and EEH devices.
  380. */
  381. while (1) {
  382. parent = pe->parent;
  383. if (pe->type & EEH_PE_PHB)
  384. break;
  385. if (purge_pe) {
  386. if (list_empty(&pe->edevs) &&
  387. list_empty(&pe->child_list)) {
  388. list_del(&pe->child);
  389. kfree(pe);
  390. } else {
  391. break;
  392. }
  393. } else {
  394. if (list_empty(&pe->edevs)) {
  395. cnt = 0;
  396. list_for_each_entry(child, &pe->child_list, child) {
  397. if (!(pe->type & EEH_PE_INVALID)) {
  398. cnt++;
  399. break;
  400. }
  401. }
  402. if (!cnt)
  403. pe->type |= EEH_PE_INVALID;
  404. else
  405. break;
  406. }
  407. }
  408. pe = parent;
  409. }
  410. eeh_unlock();
  411. return 0;
  412. }
  413. /**
  414. * __eeh_pe_state_mark - Mark the state for the PE
  415. * @data: EEH PE
  416. * @flag: state
  417. *
  418. * The function is used to mark the indicated state for the given
  419. * PE. Also, the associated PCI devices will be put into IO frozen
  420. * state as well.
  421. */
  422. static void *__eeh_pe_state_mark(void *data, void *flag)
  423. {
  424. struct eeh_pe *pe = (struct eeh_pe *)data;
  425. int state = *((int *)flag);
  426. struct eeh_dev *tmp;
  427. struct pci_dev *pdev;
  428. /*
  429. * Mark the PE with the indicated state. Also,
  430. * the associated PCI device will be put into
  431. * I/O frozen state to avoid I/O accesses from
  432. * the PCI device driver.
  433. */
  434. pe->state |= state;
  435. eeh_pe_for_each_dev(pe, tmp) {
  436. pdev = eeh_dev_to_pci_dev(tmp);
  437. if (pdev)
  438. pdev->error_state = pci_channel_io_frozen;
  439. }
  440. return NULL;
  441. }
  442. /**
  443. * eeh_pe_state_mark - Mark specified state for PE and its associated device
  444. * @pe: EEH PE
  445. *
  446. * EEH error affects the current PE and its child PEs. The function
  447. * is used to mark appropriate state for the affected PEs and the
  448. * associated devices.
  449. */
  450. void eeh_pe_state_mark(struct eeh_pe *pe, int state)
  451. {
  452. eeh_lock();
  453. eeh_pe_traverse(pe, __eeh_pe_state_mark, &state);
  454. eeh_unlock();
  455. }
  456. /**
  457. * __eeh_pe_state_clear - Clear state for the PE
  458. * @data: EEH PE
  459. * @flag: state
  460. *
  461. * The function is used to clear the indicated state from the
  462. * given PE. Besides, we also clear the check count of the PE
  463. * as well.
  464. */
  465. static void *__eeh_pe_state_clear(void *data, void *flag)
  466. {
  467. struct eeh_pe *pe = (struct eeh_pe *)data;
  468. int state = *((int *)flag);
  469. pe->state &= ~state;
  470. pe->check_count = 0;
  471. return NULL;
  472. }
  473. /**
  474. * eeh_pe_state_clear - Clear state for the PE and its children
  475. * @pe: PE
  476. * @state: state to be cleared
  477. *
  478. * When the PE and its children has been recovered from error,
  479. * we need clear the error state for that. The function is used
  480. * for the purpose.
  481. */
  482. void eeh_pe_state_clear(struct eeh_pe *pe, int state)
  483. {
  484. eeh_lock();
  485. eeh_pe_traverse(pe, __eeh_pe_state_clear, &state);
  486. eeh_unlock();
  487. }
  488. /**
  489. * eeh_restore_one_device_bars - Restore the Base Address Registers for one device
  490. * @data: EEH device
  491. * @flag: Unused
  492. *
  493. * Loads the PCI configuration space base address registers,
  494. * the expansion ROM base address, the latency timer, and etc.
  495. * from the saved values in the device node.
  496. */
  497. static void *eeh_restore_one_device_bars(void *data, void *flag)
  498. {
  499. int i;
  500. u32 cmd;
  501. struct eeh_dev *edev = (struct eeh_dev *)data;
  502. struct device_node *dn = eeh_dev_to_of_node(edev);
  503. for (i = 4; i < 10; i++)
  504. eeh_ops->write_config(dn, i*4, 4, edev->config_space[i]);
  505. /* 12 == Expansion ROM Address */
  506. eeh_ops->write_config(dn, 12*4, 4, edev->config_space[12]);
  507. #define BYTE_SWAP(OFF) (8*((OFF)/4)+3-(OFF))
  508. #define SAVED_BYTE(OFF) (((u8 *)(edev->config_space))[BYTE_SWAP(OFF)])
  509. eeh_ops->write_config(dn, PCI_CACHE_LINE_SIZE, 1,
  510. SAVED_BYTE(PCI_CACHE_LINE_SIZE));
  511. eeh_ops->write_config(dn, PCI_LATENCY_TIMER, 1,
  512. SAVED_BYTE(PCI_LATENCY_TIMER));
  513. /* max latency, min grant, interrupt pin and line */
  514. eeh_ops->write_config(dn, 15*4, 4, edev->config_space[15]);
  515. /*
  516. * Restore PERR & SERR bits, some devices require it,
  517. * don't touch the other command bits
  518. */
  519. eeh_ops->read_config(dn, PCI_COMMAND, 4, &cmd);
  520. if (edev->config_space[1] & PCI_COMMAND_PARITY)
  521. cmd |= PCI_COMMAND_PARITY;
  522. else
  523. cmd &= ~PCI_COMMAND_PARITY;
  524. if (edev->config_space[1] & PCI_COMMAND_SERR)
  525. cmd |= PCI_COMMAND_SERR;
  526. else
  527. cmd &= ~PCI_COMMAND_SERR;
  528. eeh_ops->write_config(dn, PCI_COMMAND, 4, cmd);
  529. return NULL;
  530. }
  531. /**
  532. * eeh_pe_restore_bars - Restore the PCI config space info
  533. * @pe: EEH PE
  534. *
  535. * This routine performs a recursive walk to the children
  536. * of this device as well.
  537. */
  538. void eeh_pe_restore_bars(struct eeh_pe *pe)
  539. {
  540. /*
  541. * We needn't take the EEH lock since eeh_pe_dev_traverse()
  542. * will take that.
  543. */
  544. eeh_pe_dev_traverse(pe, eeh_restore_one_device_bars, NULL);
  545. }
  546. /**
  547. * eeh_pe_bus_get - Retrieve PCI bus according to the given PE
  548. * @pe: EEH PE
  549. *
  550. * Retrieve the PCI bus according to the given PE. Basically,
  551. * there're 3 types of PEs: PHB/Bus/Device. For PHB PE, the
  552. * primary PCI bus will be retrieved. The parent bus will be
  553. * returned for BUS PE. However, we don't have associated PCI
  554. * bus for DEVICE PE.
  555. */
  556. struct pci_bus *eeh_pe_bus_get(struct eeh_pe *pe)
  557. {
  558. struct pci_bus *bus = NULL;
  559. struct eeh_dev *edev;
  560. struct pci_dev *pdev;
  561. eeh_lock();
  562. if (pe->type & EEH_PE_PHB) {
  563. bus = pe->phb->bus;
  564. } else if (pe->type & EEH_PE_BUS) {
  565. edev = list_first_entry(&pe->edevs, struct eeh_dev, list);
  566. pdev = eeh_dev_to_pci_dev(edev);
  567. if (pdev)
  568. bus = pdev->bus;
  569. }
  570. eeh_unlock();
  571. return bus;
  572. }