eeh_pe.c 14 KB

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