eeh_pe.c 24 KB

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