pnv_php.c 25 KB

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