eeh_pe.c 24 KB

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