eeh_pe.c 24 KB

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