pnv_php.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * PCI Hotplug Driver for PowerPC PowerNV platform.
  4. *
  5. * Copyright Gavin Shan, IBM Corporation 2016.
  6. * Copyright (C) 2025 Raptor Engineering, LLC
  7. * Copyright (C) 2025 Raptor Computing Systems, LLC
  8. */
  9. #include <linux/bitfield.h>
  10. #include <linux/libfdt.h>
  11. #include <linux/module.h>
  12. #include <linux/pci.h>
  13. #include <linux/delay.h>
  14. #include <linux/pci_hotplug.h>
  15. #include <linux/of_fdt.h>
  16. #include <asm/opal.h>
  17. #include <asm/pnv-pci.h>
  18. #include <asm/ppc-pci.h>
  19. #define DRIVER_VERSION "0.1"
  20. #define DRIVER_AUTHOR "Gavin Shan, IBM Corporation"
  21. #define DRIVER_DESC "PowerPC PowerNV PCI Hotplug Driver"
  22. #define SLOT_WARN(sl, x...) \
  23. ((sl)->pdev ? pci_warn((sl)->pdev, x) : dev_warn(&(sl)->bus->dev, x))
  24. struct pnv_php_event {
  25. bool added;
  26. struct pnv_php_slot *php_slot;
  27. struct work_struct work;
  28. };
  29. static LIST_HEAD(pnv_php_slot_list);
  30. static DEFINE_SPINLOCK(pnv_php_lock);
  31. static void pnv_php_register(struct device_node *dn);
  32. static void pnv_php_unregister_one(struct device_node *dn);
  33. static void pnv_php_unregister(struct device_node *dn);
  34. static void pnv_php_enable_irq(struct pnv_php_slot *php_slot);
  35. static void pnv_php_disable_irq(struct pnv_php_slot *php_slot,
  36. bool disable_device, bool disable_msi)
  37. {
  38. struct pci_dev *pdev = php_slot->pdev;
  39. u16 ctrl;
  40. if (php_slot->irq > 0) {
  41. pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &ctrl);
  42. ctrl &= ~(PCI_EXP_SLTCTL_HPIE |
  43. PCI_EXP_SLTCTL_PDCE |
  44. PCI_EXP_SLTCTL_DLLSCE);
  45. pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, ctrl);
  46. free_irq(php_slot->irq, php_slot);
  47. php_slot->irq = 0;
  48. }
  49. if (disable_device || disable_msi) {
  50. if (pdev->msix_enabled)
  51. pci_disable_msix(pdev);
  52. else if (pdev->msi_enabled)
  53. pci_disable_msi(pdev);
  54. }
  55. if (disable_device)
  56. pci_disable_device(pdev);
  57. }
  58. static void pnv_php_free_slot(struct kref *kref)
  59. {
  60. struct pnv_php_slot *php_slot = container_of(kref,
  61. struct pnv_php_slot, kref);
  62. WARN_ON(!list_empty(&php_slot->children));
  63. pnv_php_disable_irq(php_slot, false, false);
  64. destroy_workqueue(php_slot->wq);
  65. kfree(php_slot->name);
  66. kfree(php_slot);
  67. }
  68. static inline void pnv_php_put_slot(struct pnv_php_slot *php_slot)
  69. {
  70. if (!php_slot)
  71. return;
  72. kref_put(&php_slot->kref, pnv_php_free_slot);
  73. }
  74. static struct pnv_php_slot *pnv_php_match(struct device_node *dn,
  75. struct pnv_php_slot *php_slot)
  76. {
  77. struct pnv_php_slot *target, *tmp;
  78. if (php_slot->dn == dn) {
  79. kref_get(&php_slot->kref);
  80. return php_slot;
  81. }
  82. list_for_each_entry(tmp, &php_slot->children, link) {
  83. target = pnv_php_match(dn, tmp);
  84. if (target)
  85. return target;
  86. }
  87. return NULL;
  88. }
  89. struct pnv_php_slot *pnv_php_find_slot(struct device_node *dn)
  90. {
  91. struct pnv_php_slot *php_slot, *tmp;
  92. unsigned long flags;
  93. spin_lock_irqsave(&pnv_php_lock, flags);
  94. list_for_each_entry(tmp, &pnv_php_slot_list, link) {
  95. php_slot = pnv_php_match(dn, tmp);
  96. if (php_slot) {
  97. spin_unlock_irqrestore(&pnv_php_lock, flags);
  98. return php_slot;
  99. }
  100. }
  101. spin_unlock_irqrestore(&pnv_php_lock, flags);
  102. return NULL;
  103. }
  104. EXPORT_SYMBOL_GPL(pnv_php_find_slot);
  105. /*
  106. * Remove pdn for all children of the indicated device node.
  107. * The function should remove pdn in a depth-first manner.
  108. */
  109. static void pnv_php_rmv_pdns(struct device_node *dn)
  110. {
  111. struct device_node *child;
  112. for_each_child_of_node(dn, child) {
  113. pnv_php_rmv_pdns(child);
  114. pci_remove_device_node_info(child);
  115. }
  116. }
  117. /*
  118. * Detach all child nodes of the indicated device nodes. The
  119. * function should handle device nodes in depth-first manner.
  120. *
  121. * We should not invoke of_node_release() as the memory for
  122. * individual device node is part of large memory block. The
  123. * large block is allocated from memblock (system bootup) or
  124. * kmalloc() when unflattening the device tree by OF changeset.
  125. * We can not free the large block allocated from memblock. For
  126. * later case, it should be released at once.
  127. */
  128. static void pnv_php_detach_device_nodes(struct device_node *parent)
  129. {
  130. struct device_node *dn;
  131. for_each_child_of_node(parent, dn) {
  132. pnv_php_detach_device_nodes(dn);
  133. of_node_put(dn);
  134. of_detach_node(dn);
  135. }
  136. }
  137. static void pnv_php_rmv_devtree(struct pnv_php_slot *php_slot)
  138. {
  139. pnv_php_rmv_pdns(php_slot->dn);
  140. /*
  141. * Decrease the refcount if the device nodes were created
  142. * through OF changeset before detaching them.
  143. */
  144. if (php_slot->fdt)
  145. of_changeset_destroy(&php_slot->ocs);
  146. pnv_php_detach_device_nodes(php_slot->dn);
  147. if (php_slot->fdt) {
  148. kfree(php_slot->dt);
  149. kfree(php_slot->fdt);
  150. php_slot->dt = NULL;
  151. php_slot->dn->child = NULL;
  152. php_slot->fdt = NULL;
  153. }
  154. }
  155. /*
  156. * As the nodes in OF changeset are applied in reverse order, we
  157. * need revert the nodes in advance so that we have correct node
  158. * order after the changeset is applied.
  159. */
  160. static void pnv_php_reverse_nodes(struct device_node *parent)
  161. {
  162. struct device_node *child, *next;
  163. /* In-depth first */
  164. for_each_child_of_node(parent, child)
  165. pnv_php_reverse_nodes(child);
  166. /* Reverse the nodes in the child list */
  167. child = parent->child;
  168. parent->child = NULL;
  169. while (child) {
  170. next = child->sibling;
  171. child->sibling = parent->child;
  172. parent->child = child;
  173. child = next;
  174. }
  175. }
  176. static int pnv_php_populate_changeset(struct of_changeset *ocs,
  177. struct device_node *dn)
  178. {
  179. struct device_node *child;
  180. int ret = 0;
  181. for_each_child_of_node(dn, child) {
  182. ret = of_changeset_attach_node(ocs, child);
  183. if (ret) {
  184. of_node_put(child);
  185. break;
  186. }
  187. ret = pnv_php_populate_changeset(ocs, child);
  188. if (ret) {
  189. of_node_put(child);
  190. break;
  191. }
  192. }
  193. return ret;
  194. }
  195. static void *pnv_php_add_one_pdn(struct device_node *dn, void *data)
  196. {
  197. struct pci_controller *hose = (struct pci_controller *)data;
  198. struct pci_dn *pdn;
  199. pdn = pci_add_device_node_info(hose, dn);
  200. if (!pdn)
  201. return ERR_PTR(-ENOMEM);
  202. return NULL;
  203. }
  204. static void pnv_php_add_pdns(struct pnv_php_slot *slot)
  205. {
  206. struct pci_controller *hose = pci_bus_to_host(slot->bus);
  207. pci_traverse_device_nodes(slot->dn, pnv_php_add_one_pdn, hose);
  208. }
  209. static int pnv_php_add_devtree(struct pnv_php_slot *php_slot)
  210. {
  211. void *fdt, *fdt1, *dt;
  212. int ret;
  213. /* We don't know the FDT blob size. We try to get it through
  214. * maximal memory chunk and then copy it to another chunk that
  215. * fits the real size.
  216. */
  217. fdt1 = kzalloc(0x10000, GFP_KERNEL);
  218. if (!fdt1) {
  219. ret = -ENOMEM;
  220. goto out;
  221. }
  222. ret = pnv_pci_get_device_tree(php_slot->dn->phandle, fdt1, 0x10000);
  223. if (ret) {
  224. SLOT_WARN(php_slot, "Error %d getting FDT blob\n", ret);
  225. goto free_fdt1;
  226. }
  227. fdt = kmemdup(fdt1, fdt_totalsize(fdt1), GFP_KERNEL);
  228. if (!fdt) {
  229. ret = -ENOMEM;
  230. goto free_fdt1;
  231. }
  232. /* Unflatten device tree blob */
  233. dt = of_fdt_unflatten_tree(fdt, php_slot->dn, NULL);
  234. if (!dt) {
  235. ret = -EINVAL;
  236. SLOT_WARN(php_slot, "Cannot unflatten FDT\n");
  237. goto free_fdt;
  238. }
  239. /* Initialize and apply the changeset */
  240. of_changeset_init(&php_slot->ocs);
  241. pnv_php_reverse_nodes(php_slot->dn);
  242. ret = pnv_php_populate_changeset(&php_slot->ocs, php_slot->dn);
  243. if (ret) {
  244. pnv_php_reverse_nodes(php_slot->dn);
  245. SLOT_WARN(php_slot, "Error %d populating changeset\n",
  246. ret);
  247. goto free_dt;
  248. }
  249. php_slot->dn->child = NULL;
  250. ret = of_changeset_apply(&php_slot->ocs);
  251. if (ret) {
  252. SLOT_WARN(php_slot, "Error %d applying changeset\n", ret);
  253. goto destroy_changeset;
  254. }
  255. /* Add device node firmware data */
  256. pnv_php_add_pdns(php_slot);
  257. php_slot->fdt = fdt;
  258. php_slot->dt = dt;
  259. kfree(fdt1);
  260. goto out;
  261. destroy_changeset:
  262. of_changeset_destroy(&php_slot->ocs);
  263. free_dt:
  264. kfree(dt);
  265. php_slot->dn->child = NULL;
  266. free_fdt:
  267. kfree(fdt);
  268. free_fdt1:
  269. kfree(fdt1);
  270. out:
  271. return ret;
  272. }
  273. static inline struct pnv_php_slot *to_pnv_php_slot(struct hotplug_slot *slot)
  274. {
  275. return container_of(slot, struct pnv_php_slot, slot);
  276. }
  277. int pnv_php_set_slot_power_state(struct hotplug_slot *slot,
  278. uint8_t state)
  279. {
  280. struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
  281. struct opal_msg msg;
  282. int ret;
  283. ret = pnv_pci_set_power_state(php_slot->id, state, &msg);
  284. if (ret > 0) {
  285. if (be64_to_cpu(msg.params[1]) != php_slot->dn->phandle ||
  286. be64_to_cpu(msg.params[2]) != state) {
  287. SLOT_WARN(php_slot, "Wrong msg (%lld, %lld, %lld)\n",
  288. be64_to_cpu(msg.params[1]),
  289. be64_to_cpu(msg.params[2]),
  290. be64_to_cpu(msg.params[3]));
  291. return -ENOMSG;
  292. }
  293. if (be64_to_cpu(msg.params[3]) != OPAL_SUCCESS) {
  294. ret = -ENODEV;
  295. goto error;
  296. }
  297. } else if (ret < 0) {
  298. goto error;
  299. }
  300. if (state == OPAL_PCI_SLOT_POWER_OFF || state == OPAL_PCI_SLOT_OFFLINE)
  301. pnv_php_rmv_devtree(php_slot);
  302. else
  303. ret = pnv_php_add_devtree(php_slot);
  304. return ret;
  305. error:
  306. SLOT_WARN(php_slot, "Error %d powering %s\n",
  307. ret, (state == OPAL_PCI_SLOT_POWER_ON) ? "on" : "off");
  308. return ret;
  309. }
  310. EXPORT_SYMBOL_GPL(pnv_php_set_slot_power_state);
  311. static int pnv_php_get_power_state(struct hotplug_slot *slot, u8 *state)
  312. {
  313. struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
  314. uint8_t power_state = OPAL_PCI_SLOT_POWER_ON;
  315. int ret;
  316. /*
  317. * Retrieve power status from firmware. If we fail
  318. * getting that, the power status fails back to
  319. * be on.
  320. */
  321. ret = pnv_pci_get_power_state(php_slot->id, &power_state);
  322. if (ret) {
  323. SLOT_WARN(php_slot, "Error %d getting power status\n",
  324. ret);
  325. } else {
  326. *state = power_state;
  327. }
  328. return 0;
  329. }
  330. static int pcie_check_link_active(struct pci_dev *pdev)
  331. {
  332. u16 lnk_status;
  333. int ret;
  334. ret = pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
  335. if (ret == PCIBIOS_DEVICE_NOT_FOUND || PCI_POSSIBLE_ERROR(lnk_status))
  336. return -ENODEV;
  337. ret = !!(lnk_status & PCI_EXP_LNKSTA_DLLLA);
  338. return ret;
  339. }
  340. static int pnv_php_get_adapter_state(struct hotplug_slot *slot, u8 *state)
  341. {
  342. struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
  343. uint8_t presence = OPAL_PCI_SLOT_EMPTY;
  344. int ret;
  345. /*
  346. * Retrieve presence status from firmware. If we can't
  347. * get that, it will fail back to be empty.
  348. */
  349. ret = pnv_pci_get_presence_state(php_slot->id, &presence);
  350. if (ret >= 0) {
  351. if (pci_pcie_type(php_slot->pdev) == PCI_EXP_TYPE_DOWNSTREAM &&
  352. presence == OPAL_PCI_SLOT_EMPTY) {
  353. /*
  354. * Similar to pciehp_hpc, check whether the Link Active
  355. * bit is set to account for broken downstream bridges
  356. * that don't properly assert Presence Detect State, as
  357. * was observed on the Microsemi Switchtec PM8533 PFX
  358. * [11f8:8533].
  359. */
  360. if (pcie_check_link_active(php_slot->pdev) > 0)
  361. presence = OPAL_PCI_SLOT_PRESENT;
  362. }
  363. *state = presence;
  364. ret = 0;
  365. } else {
  366. SLOT_WARN(php_slot, "Error %d getting presence\n", ret);
  367. }
  368. return ret;
  369. }
  370. static int pnv_php_get_attention_state(struct hotplug_slot *slot, u8 *state)
  371. {
  372. struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
  373. *state = php_slot->attention_state;
  374. return 0;
  375. }
  376. static int pnv_php_set_attention_state(struct hotplug_slot *slot, u8 state)
  377. {
  378. struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
  379. struct pci_dev *bridge = php_slot->pdev;
  380. u16 new, mask;
  381. php_slot->attention_state = state;
  382. if (!bridge)
  383. return 0;
  384. mask = PCI_EXP_SLTCTL_AIC;
  385. if (state)
  386. new = PCI_EXP_SLTCTL_ATTN_IND_ON;
  387. else
  388. new = PCI_EXP_SLTCTL_ATTN_IND_OFF;
  389. pcie_capability_clear_and_set_word(bridge, PCI_EXP_SLTCTL, mask, new);
  390. return 0;
  391. }
  392. static int pnv_php_activate_slot(struct pnv_php_slot *php_slot,
  393. struct hotplug_slot *slot)
  394. {
  395. int ret, i;
  396. /*
  397. * Issue initial slot activation command to firmware
  398. *
  399. * Firmware will power slot on, attempt to train the link, and
  400. * discover any downstream devices. If this process fails, firmware
  401. * will return an error code and an invalid device tree. Failure
  402. * can be caused for multiple reasons, including a faulty
  403. * downstream device, poor connection to the downstream device, or
  404. * a previously latched PHB fence. On failure, issue fundamental
  405. * reset up to three times before aborting.
  406. */
  407. ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_ON);
  408. if (ret) {
  409. SLOT_WARN(
  410. php_slot,
  411. "PCI slot activation failed with error code %d, possible frozen PHB",
  412. ret);
  413. SLOT_WARN(
  414. php_slot,
  415. "Attempting complete PHB reset before retrying slot activation\n");
  416. for (i = 0; i < 3; i++) {
  417. /*
  418. * Slot activation failed, PHB may be fenced from a
  419. * prior device failure.
  420. *
  421. * Use the OPAL fundamental reset call to both try a
  422. * device reset and clear any potentially active PHB
  423. * fence / freeze.
  424. */
  425. SLOT_WARN(php_slot, "Try %d...\n", i + 1);
  426. pci_set_pcie_reset_state(php_slot->pdev,
  427. pcie_warm_reset);
  428. msleep(250);
  429. pci_set_pcie_reset_state(php_slot->pdev,
  430. pcie_deassert_reset);
  431. ret = pnv_php_set_slot_power_state(
  432. slot, OPAL_PCI_SLOT_POWER_ON);
  433. if (!ret)
  434. break;
  435. }
  436. if (i >= 3)
  437. SLOT_WARN(php_slot,
  438. "Failed to bring slot online, aborting!\n");
  439. }
  440. return ret;
  441. }
  442. static int pnv_php_enable(struct pnv_php_slot *php_slot, bool rescan)
  443. {
  444. struct hotplug_slot *slot = &php_slot->slot;
  445. uint8_t presence = OPAL_PCI_SLOT_EMPTY;
  446. uint8_t power_status = OPAL_PCI_SLOT_POWER_ON;
  447. int ret;
  448. /* Check if the slot has been configured */
  449. if (php_slot->state != PNV_PHP_STATE_REGISTERED)
  450. return 0;
  451. /* Retrieve slot presence status */
  452. ret = pnv_php_get_adapter_state(slot, &presence);
  453. if (ret)
  454. return ret;
  455. /*
  456. * Proceed if there have nothing behind the slot. However,
  457. * we should leave the slot in registered state at the
  458. * beginning. Otherwise, the PCI devices inserted afterwards
  459. * won't be probed and populated.
  460. */
  461. if (presence == OPAL_PCI_SLOT_EMPTY) {
  462. if (!php_slot->power_state_check) {
  463. php_slot->power_state_check = true;
  464. return 0;
  465. }
  466. goto scan;
  467. }
  468. /*
  469. * If the power supply to the slot is off, we can't detect
  470. * adapter presence state. That means we have to turn the
  471. * slot on before going to probe slot's presence state.
  472. *
  473. * On the first time, we don't change the power status to
  474. * boost system boot with assumption that the firmware
  475. * supplies consistent slot power status: empty slot always
  476. * has its power off and non-empty slot has its power on.
  477. */
  478. if (!php_slot->power_state_check) {
  479. php_slot->power_state_check = true;
  480. ret = pnv_php_get_power_state(slot, &power_status);
  481. if (ret)
  482. return ret;
  483. if (power_status != OPAL_PCI_SLOT_POWER_ON)
  484. return 0;
  485. }
  486. /* Check the power status. Scan the slot if it is already on */
  487. ret = pnv_php_get_power_state(slot, &power_status);
  488. if (ret)
  489. return ret;
  490. if (power_status == OPAL_PCI_SLOT_POWER_ON)
  491. goto scan;
  492. /* Power is off, turn it on and then scan the slot */
  493. ret = pnv_php_activate_slot(php_slot, slot);
  494. if (ret)
  495. return ret;
  496. scan:
  497. if (presence == OPAL_PCI_SLOT_PRESENT) {
  498. if (rescan) {
  499. pci_lock_rescan_remove();
  500. pci_hp_add_devices(php_slot->bus);
  501. pci_unlock_rescan_remove();
  502. }
  503. /* Rescan for child hotpluggable slots */
  504. php_slot->state = PNV_PHP_STATE_POPULATED;
  505. if (rescan)
  506. pnv_php_register(php_slot->dn);
  507. } else {
  508. php_slot->state = PNV_PHP_STATE_POPULATED;
  509. }
  510. return 0;
  511. }
  512. static int pnv_php_reset_slot(struct hotplug_slot *slot, bool probe)
  513. {
  514. struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
  515. struct pci_dev *bridge = php_slot->pdev;
  516. uint16_t sts;
  517. /*
  518. * The CAPI folks want pnv_php to drive OpenCAPI slots
  519. * which don't have a bridge. Only claim to support
  520. * reset_slot() if we have a bridge device (for now...)
  521. */
  522. if (probe)
  523. return !bridge;
  524. /* mask our interrupt while resetting the bridge */
  525. if (php_slot->irq > 0)
  526. disable_irq(php_slot->irq);
  527. pci_bridge_secondary_bus_reset(bridge);
  528. /* clear any state changes that happened due to the reset */
  529. pcie_capability_read_word(php_slot->pdev, PCI_EXP_SLTSTA, &sts);
  530. sts &= (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC);
  531. pcie_capability_write_word(php_slot->pdev, PCI_EXP_SLTSTA, sts);
  532. if (php_slot->irq > 0)
  533. enable_irq(php_slot->irq);
  534. return 0;
  535. }
  536. static int pnv_php_enable_slot(struct hotplug_slot *slot)
  537. {
  538. struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
  539. u32 prop32;
  540. int ret;
  541. ret = pnv_php_enable(php_slot, true);
  542. if (ret)
  543. return ret;
  544. /* (Re-)enable interrupt if the slot supports surprise hotplug */
  545. ret = of_property_read_u32(php_slot->dn, "ibm,slot-surprise-pluggable",
  546. &prop32);
  547. if (!ret && prop32)
  548. pnv_php_enable_irq(php_slot);
  549. return 0;
  550. }
  551. /*
  552. * Disable any hotplug interrupts for all slots on the provided bus, as well as
  553. * all downstream slots in preparation for a hot unplug.
  554. */
  555. static int pnv_php_disable_all_irqs(struct pci_bus *bus)
  556. {
  557. struct pci_bus *child_bus;
  558. struct pci_slot *slot;
  559. /* First go down child buses */
  560. list_for_each_entry(child_bus, &bus->children, node)
  561. pnv_php_disable_all_irqs(child_bus);
  562. /* Disable IRQs for all pnv_php slots on this bus */
  563. list_for_each_entry(slot, &bus->slots, list) {
  564. struct pnv_php_slot *php_slot = to_pnv_php_slot(slot->hotplug);
  565. pnv_php_disable_irq(php_slot, false, true);
  566. }
  567. return 0;
  568. }
  569. /*
  570. * Disable any hotplug interrupts for all downstream slots on the provided
  571. * bus in preparation for a hot unplug.
  572. */
  573. static int pnv_php_disable_all_downstream_irqs(struct pci_bus *bus)
  574. {
  575. struct pci_bus *child_bus;
  576. /* Go down child buses, recursively deactivating their IRQs */
  577. list_for_each_entry(child_bus, &bus->children, node)
  578. pnv_php_disable_all_irqs(child_bus);
  579. return 0;
  580. }
  581. static int pnv_php_disable_slot(struct hotplug_slot *slot)
  582. {
  583. struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
  584. int ret;
  585. /*
  586. * Allow to disable a slot already in the registered state to
  587. * cover cases where the slot couldn't be enabled and never
  588. * reached the populated state
  589. */
  590. if (php_slot->state != PNV_PHP_STATE_POPULATED &&
  591. php_slot->state != PNV_PHP_STATE_REGISTERED)
  592. return 0;
  593. /*
  594. * Free all IRQ resources from all child slots before remove.
  595. * Note that we do not disable the root slot IRQ here as that
  596. * would also deactivate the slot hot (re)plug interrupt!
  597. */
  598. pnv_php_disable_all_downstream_irqs(php_slot->bus);
  599. /* Remove all devices behind the slot */
  600. pci_lock_rescan_remove();
  601. pci_hp_remove_devices(php_slot->bus);
  602. pci_unlock_rescan_remove();
  603. /* Detach the child hotpluggable slots */
  604. pnv_php_unregister(php_slot->dn);
  605. /* Notify firmware and remove device nodes */
  606. ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_OFF);
  607. php_slot->state = PNV_PHP_STATE_REGISTERED;
  608. return ret;
  609. }
  610. static const struct hotplug_slot_ops php_slot_ops = {
  611. .get_power_status = pnv_php_get_power_state,
  612. .get_adapter_status = pnv_php_get_adapter_state,
  613. .get_attention_status = pnv_php_get_attention_state,
  614. .set_attention_status = pnv_php_set_attention_state,
  615. .enable_slot = pnv_php_enable_slot,
  616. .disable_slot = pnv_php_disable_slot,
  617. .reset_slot = pnv_php_reset_slot,
  618. };
  619. static void pnv_php_release(struct pnv_php_slot *php_slot)
  620. {
  621. unsigned long flags;
  622. /* Remove from global or child list */
  623. spin_lock_irqsave(&pnv_php_lock, flags);
  624. list_del(&php_slot->link);
  625. spin_unlock_irqrestore(&pnv_php_lock, flags);
  626. /* Detach from parent */
  627. pnv_php_put_slot(php_slot);
  628. pnv_php_put_slot(php_slot->parent);
  629. }
  630. static struct pnv_php_slot *pnv_php_alloc_slot(struct device_node *dn)
  631. {
  632. struct pnv_php_slot *php_slot;
  633. struct pci_bus *bus;
  634. const char *label;
  635. uint64_t id;
  636. int ret;
  637. ret = of_property_read_string(dn, "ibm,slot-label", &label);
  638. if (ret)
  639. return NULL;
  640. if (pnv_pci_get_slot_id(dn, &id))
  641. return NULL;
  642. bus = pci_find_bus_by_node(dn);
  643. if (!bus)
  644. return NULL;
  645. php_slot = kzalloc(sizeof(*php_slot), GFP_KERNEL);
  646. if (!php_slot)
  647. return NULL;
  648. php_slot->name = kstrdup(label, GFP_KERNEL);
  649. if (!php_slot->name) {
  650. kfree(php_slot);
  651. return NULL;
  652. }
  653. /* Allocate workqueue for this slot's interrupt handling */
  654. php_slot->wq = alloc_workqueue("pciehp-%s", 0, 0, php_slot->name);
  655. if (!php_slot->wq) {
  656. SLOT_WARN(php_slot, "Cannot alloc workqueue\n");
  657. kfree(php_slot->name);
  658. kfree(php_slot);
  659. return NULL;
  660. }
  661. if (dn->child && PCI_DN(dn->child))
  662. php_slot->slot_no = PCI_SLOT(PCI_DN(dn->child)->devfn);
  663. else
  664. php_slot->slot_no = -1; /* Placeholder slot */
  665. kref_init(&php_slot->kref);
  666. php_slot->state = PNV_PHP_STATE_INITIALIZED;
  667. php_slot->dn = dn;
  668. php_slot->pdev = bus->self;
  669. php_slot->bus = bus;
  670. php_slot->id = id;
  671. php_slot->power_state_check = false;
  672. php_slot->slot.ops = &php_slot_ops;
  673. INIT_LIST_HEAD(&php_slot->children);
  674. INIT_LIST_HEAD(&php_slot->link);
  675. return php_slot;
  676. }
  677. static int pnv_php_register_slot(struct pnv_php_slot *php_slot)
  678. {
  679. struct pnv_php_slot *parent;
  680. struct device_node *dn = php_slot->dn;
  681. unsigned long flags;
  682. int ret;
  683. /* Check if the slot is registered or not */
  684. parent = pnv_php_find_slot(php_slot->dn);
  685. if (parent) {
  686. pnv_php_put_slot(parent);
  687. return -EEXIST;
  688. }
  689. /* Register PCI slot */
  690. ret = pci_hp_register(&php_slot->slot, php_slot->bus,
  691. php_slot->slot_no, php_slot->name);
  692. if (ret) {
  693. SLOT_WARN(php_slot, "Error %d registering slot\n", ret);
  694. return ret;
  695. }
  696. /* Attach to the parent's child list or global list */
  697. while ((dn = of_get_parent(dn))) {
  698. if (!PCI_DN(dn)) {
  699. of_node_put(dn);
  700. break;
  701. }
  702. parent = pnv_php_find_slot(dn);
  703. if (parent) {
  704. of_node_put(dn);
  705. break;
  706. }
  707. of_node_put(dn);
  708. }
  709. spin_lock_irqsave(&pnv_php_lock, flags);
  710. php_slot->parent = parent;
  711. if (parent)
  712. list_add_tail(&php_slot->link, &parent->children);
  713. else
  714. list_add_tail(&php_slot->link, &pnv_php_slot_list);
  715. spin_unlock_irqrestore(&pnv_php_lock, flags);
  716. php_slot->state = PNV_PHP_STATE_REGISTERED;
  717. return 0;
  718. }
  719. static int pnv_php_enable_msix(struct pnv_php_slot *php_slot)
  720. {
  721. struct pci_dev *pdev = php_slot->pdev;
  722. struct msix_entry entry;
  723. int nr_entries, ret;
  724. u16 pcie_flag;
  725. /* Get total number of MSIx entries */
  726. nr_entries = pci_msix_vec_count(pdev);
  727. if (nr_entries < 0)
  728. return nr_entries;
  729. /* Check hotplug MSIx entry is in range */
  730. pcie_capability_read_word(pdev, PCI_EXP_FLAGS, &pcie_flag);
  731. entry.entry = FIELD_GET(PCI_EXP_FLAGS_IRQ, pcie_flag);
  732. if (entry.entry >= nr_entries)
  733. return -ERANGE;
  734. /* Enable MSIx */
  735. ret = pci_enable_msix_exact(pdev, &entry, 1);
  736. if (ret) {
  737. SLOT_WARN(php_slot, "Error %d enabling MSIx\n", ret);
  738. return ret;
  739. }
  740. return entry.vector;
  741. }
  742. static void
  743. pnv_php_detect_clear_suprise_removal_freeze(struct pnv_php_slot *php_slot)
  744. {
  745. struct pci_dev *pdev = php_slot->pdev;
  746. struct eeh_dev *edev;
  747. struct eeh_pe *pe;
  748. int i, rc;
  749. /*
  750. * When a device is surprise removed from a downstream bridge slot,
  751. * the upstream bridge port can still end up frozen due to related EEH
  752. * events, which will in turn block the MSI interrupts for slot hotplug
  753. * detection.
  754. *
  755. * Detect and thaw any frozen upstream PE after slot deactivation.
  756. */
  757. edev = pci_dev_to_eeh_dev(pdev);
  758. pe = edev ? edev->pe : NULL;
  759. rc = eeh_pe_get_state(pe);
  760. if ((rc == -ENODEV) || (rc == -ENOENT)) {
  761. SLOT_WARN(
  762. php_slot,
  763. "Upstream bridge PE state unknown, hotplug detect may fail\n");
  764. } else {
  765. if (pe->state & EEH_PE_ISOLATED) {
  766. SLOT_WARN(
  767. php_slot,
  768. "Upstream bridge PE %02x frozen, thawing...\n",
  769. pe->addr);
  770. for (i = 0; i < 3; i++)
  771. if (!eeh_unfreeze_pe(pe))
  772. break;
  773. if (i >= 3)
  774. SLOT_WARN(
  775. php_slot,
  776. "Unable to thaw PE %02x, hotplug detect will fail!\n",
  777. pe->addr);
  778. else
  779. SLOT_WARN(php_slot,
  780. "PE %02x thawed successfully\n",
  781. pe->addr);
  782. }
  783. }
  784. }
  785. static void pnv_php_event_handler(struct work_struct *work)
  786. {
  787. struct pnv_php_event *event =
  788. container_of(work, struct pnv_php_event, work);
  789. struct pnv_php_slot *php_slot = event->php_slot;
  790. if (event->added) {
  791. pnv_php_enable_slot(&php_slot->slot);
  792. } else {
  793. pnv_php_disable_slot(&php_slot->slot);
  794. pnv_php_detect_clear_suprise_removal_freeze(php_slot);
  795. }
  796. kfree(event);
  797. }
  798. static irqreturn_t pnv_php_interrupt(int irq, void *data)
  799. {
  800. struct pnv_php_slot *php_slot = data;
  801. struct pci_dev *pchild, *pdev = php_slot->pdev;
  802. struct eeh_dev *edev;
  803. struct eeh_pe *pe;
  804. struct pnv_php_event *event;
  805. u16 sts, lsts;
  806. u8 presence;
  807. bool added;
  808. unsigned long flags;
  809. int ret;
  810. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &sts);
  811. sts &= (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC);
  812. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, sts);
  813. pci_dbg(pdev, "PCI slot [%s]: HP int! DLAct: %d, PresDet: %d\n",
  814. php_slot->name,
  815. !!(sts & PCI_EXP_SLTSTA_DLLSC),
  816. !!(sts & PCI_EXP_SLTSTA_PDC));
  817. if (sts & PCI_EXP_SLTSTA_DLLSC) {
  818. pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lsts);
  819. added = !!(lsts & PCI_EXP_LNKSTA_DLLLA);
  820. } else if (!(php_slot->flags & PNV_PHP_FLAG_BROKEN_PDC) &&
  821. (sts & PCI_EXP_SLTSTA_PDC)) {
  822. ret = pnv_pci_get_presence_state(php_slot->id, &presence);
  823. if (ret) {
  824. SLOT_WARN(php_slot,
  825. "PCI slot [%s] error %d getting presence (0x%04x), to retry the operation.\n",
  826. php_slot->name, ret, sts);
  827. return IRQ_HANDLED;
  828. }
  829. added = !!(presence == OPAL_PCI_SLOT_PRESENT);
  830. } else {
  831. pci_dbg(pdev, "PCI slot [%s]: Spurious IRQ?\n", php_slot->name);
  832. return IRQ_NONE;
  833. }
  834. /* Freeze the removed PE to avoid unexpected error reporting */
  835. if (!added) {
  836. pchild = list_first_entry_or_null(&php_slot->bus->devices,
  837. struct pci_dev, bus_list);
  838. edev = pchild ? pci_dev_to_eeh_dev(pchild) : NULL;
  839. pe = edev ? edev->pe : NULL;
  840. if (pe) {
  841. eeh_serialize_lock(&flags);
  842. eeh_pe_mark_isolated(pe);
  843. eeh_serialize_unlock(flags);
  844. eeh_pe_set_option(pe, EEH_OPT_FREEZE_PE);
  845. }
  846. }
  847. /*
  848. * The PE is left in frozen state if the event is missed. It's
  849. * fine as the PCI devices (PE) aren't functional any more.
  850. */
  851. event = kzalloc(sizeof(*event), GFP_ATOMIC);
  852. if (!event) {
  853. SLOT_WARN(php_slot,
  854. "PCI slot [%s] missed hotplug event 0x%04x\n",
  855. php_slot->name, sts);
  856. return IRQ_HANDLED;
  857. }
  858. pci_info(pdev, "PCI slot [%s] %s (IRQ: %d)\n",
  859. php_slot->name, added ? "added" : "removed", irq);
  860. INIT_WORK(&event->work, pnv_php_event_handler);
  861. event->added = added;
  862. event->php_slot = php_slot;
  863. queue_work(php_slot->wq, &event->work);
  864. return IRQ_HANDLED;
  865. }
  866. static void pnv_php_init_irq(struct pnv_php_slot *php_slot, int irq)
  867. {
  868. struct pci_dev *pdev = php_slot->pdev;
  869. u32 broken_pdc = 0;
  870. u16 sts, ctrl;
  871. int ret;
  872. /* Check PDC (Presence Detection Change) is broken or not */
  873. ret = of_property_read_u32(php_slot->dn, "ibm,slot-broken-pdc",
  874. &broken_pdc);
  875. if (!ret && broken_pdc)
  876. php_slot->flags |= PNV_PHP_FLAG_BROKEN_PDC;
  877. /* Clear pending interrupts */
  878. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &sts);
  879. if (php_slot->flags & PNV_PHP_FLAG_BROKEN_PDC)
  880. sts |= PCI_EXP_SLTSTA_DLLSC;
  881. else
  882. sts |= (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC);
  883. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, sts);
  884. /* Request the interrupt */
  885. ret = request_irq(irq, pnv_php_interrupt, IRQF_SHARED,
  886. php_slot->name, php_slot);
  887. if (ret) {
  888. pnv_php_disable_irq(php_slot, true, true);
  889. SLOT_WARN(php_slot, "Error %d enabling IRQ %d\n", ret, irq);
  890. return;
  891. }
  892. /* Enable the interrupts */
  893. pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &ctrl);
  894. if (php_slot->flags & PNV_PHP_FLAG_BROKEN_PDC) {
  895. ctrl &= ~PCI_EXP_SLTCTL_PDCE;
  896. ctrl |= (PCI_EXP_SLTCTL_HPIE |
  897. PCI_EXP_SLTCTL_DLLSCE);
  898. } else {
  899. ctrl |= (PCI_EXP_SLTCTL_HPIE |
  900. PCI_EXP_SLTCTL_PDCE |
  901. PCI_EXP_SLTCTL_DLLSCE);
  902. }
  903. pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, ctrl);
  904. /* The interrupt is initialized successfully when @irq is valid */
  905. php_slot->irq = irq;
  906. }
  907. static void pnv_php_enable_irq(struct pnv_php_slot *php_slot)
  908. {
  909. struct pci_dev *pdev = php_slot->pdev;
  910. int irq, ret;
  911. /*
  912. * The MSI/MSIx interrupt might have been occupied by other
  913. * drivers. Don't populate the surprise hotplug capability
  914. * in that case.
  915. */
  916. if (pci_dev_msi_enabled(pdev))
  917. return;
  918. ret = pci_enable_device(pdev);
  919. if (ret) {
  920. SLOT_WARN(php_slot, "Error %d enabling device\n", ret);
  921. return;
  922. }
  923. pci_set_master(pdev);
  924. /* Enable MSIx interrupt */
  925. irq = pnv_php_enable_msix(php_slot);
  926. if (irq > 0) {
  927. pnv_php_init_irq(php_slot, irq);
  928. return;
  929. }
  930. /*
  931. * Use MSI if MSIx doesn't work. Fail back to legacy INTx
  932. * if MSI doesn't work either
  933. */
  934. ret = pci_enable_msi(pdev);
  935. if (!ret || pdev->irq) {
  936. irq = pdev->irq;
  937. pnv_php_init_irq(php_slot, irq);
  938. }
  939. }
  940. static int pnv_php_register_one(struct device_node *dn)
  941. {
  942. struct pnv_php_slot *php_slot;
  943. u32 prop32;
  944. int ret;
  945. /* Check if it's hotpluggable slot */
  946. ret = of_property_read_u32(dn, "ibm,slot-pluggable", &prop32);
  947. if (ret || !prop32)
  948. return -ENXIO;
  949. ret = of_property_read_u32(dn, "ibm,reset-by-firmware", &prop32);
  950. if (ret || !prop32)
  951. return -ENXIO;
  952. php_slot = pnv_php_alloc_slot(dn);
  953. if (!php_slot)
  954. return -ENODEV;
  955. ret = pnv_php_register_slot(php_slot);
  956. if (ret)
  957. goto free_slot;
  958. ret = pnv_php_enable(php_slot, false);
  959. if (ret)
  960. goto unregister_slot;
  961. /* Enable interrupt if the slot supports surprise hotplug */
  962. ret = of_property_read_u32(dn, "ibm,slot-surprise-pluggable", &prop32);
  963. if (!ret && prop32)
  964. pnv_php_enable_irq(php_slot);
  965. return 0;
  966. unregister_slot:
  967. pnv_php_unregister_one(php_slot->dn);
  968. free_slot:
  969. pnv_php_put_slot(php_slot);
  970. return ret;
  971. }
  972. static void pnv_php_register(struct device_node *dn)
  973. {
  974. struct device_node *child;
  975. /*
  976. * The parent slots should be registered before their
  977. * child slots.
  978. */
  979. for_each_child_of_node(dn, child) {
  980. pnv_php_register_one(child);
  981. pnv_php_register(child);
  982. }
  983. }
  984. static void pnv_php_unregister_one(struct device_node *dn)
  985. {
  986. struct pnv_php_slot *php_slot;
  987. php_slot = pnv_php_find_slot(dn);
  988. if (!php_slot)
  989. return;
  990. php_slot->state = PNV_PHP_STATE_OFFLINE;
  991. pci_hp_deregister(&php_slot->slot);
  992. pnv_php_release(php_slot);
  993. pnv_php_put_slot(php_slot);
  994. }
  995. static void pnv_php_unregister(struct device_node *dn)
  996. {
  997. struct device_node *child;
  998. /* The child slots should go before their parent slots */
  999. for_each_child_of_node(dn, child) {
  1000. pnv_php_unregister(child);
  1001. pnv_php_unregister_one(child);
  1002. }
  1003. }
  1004. static int __init pnv_php_init(void)
  1005. {
  1006. struct device_node *dn;
  1007. pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  1008. for_each_compatible_node(dn, NULL, "ibm,ioda2-phb")
  1009. pnv_php_register(dn);
  1010. for_each_compatible_node(dn, NULL, "ibm,ioda3-phb")
  1011. pnv_php_register(dn);
  1012. for_each_compatible_node(dn, NULL, "ibm,ioda2-npu2-opencapi-phb")
  1013. pnv_php_register_one(dn); /* slot directly under the PHB */
  1014. return 0;
  1015. }
  1016. static void __exit pnv_php_exit(void)
  1017. {
  1018. struct device_node *dn;
  1019. for_each_compatible_node(dn, NULL, "ibm,ioda2-phb")
  1020. pnv_php_unregister(dn);
  1021. for_each_compatible_node(dn, NULL, "ibm,ioda3-phb")
  1022. pnv_php_unregister(dn);
  1023. for_each_compatible_node(dn, NULL, "ibm,ioda2-npu2-opencapi-phb")
  1024. pnv_php_unregister_one(dn); /* slot directly under the PHB */
  1025. }
  1026. module_init(pnv_php_init);
  1027. module_exit(pnv_php_exit);
  1028. MODULE_VERSION(DRIVER_VERSION);
  1029. MODULE_LICENSE("GPL v2");
  1030. MODULE_AUTHOR(DRIVER_AUTHOR);
  1031. MODULE_DESCRIPTION(DRIVER_DESC);