eeh_pe.c 24 KB

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