pnv_php.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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 <asm/opal.h>
  12. #include <asm/pnv-pci.h>
  13. #include <asm/ppc-pci.h>
  14. #define DRIVER_VERSION "0.1"
  15. #define DRIVER_AUTHOR "Gavin Shan, IBM Corporation"
  16. #define DRIVER_DESC "PowerPC PowerNV PCI Hotplug Driver"
  17. struct pnv_php_event {
  18. bool added;
  19. struct pnv_php_slot *php_slot;
  20. struct work_struct work;
  21. };
  22. static LIST_HEAD(pnv_php_slot_list);
  23. static DEFINE_SPINLOCK(pnv_php_lock);
  24. static void pnv_php_register(struct device_node *dn);
  25. static void pnv_php_unregister_one(struct device_node *dn);
  26. static void pnv_php_unregister(struct device_node *dn);
  27. static void pnv_php_disable_irq(struct pnv_php_slot *php_slot,
  28. bool disable_device)
  29. {
  30. struct pci_dev *pdev = php_slot->pdev;
  31. int irq = php_slot->irq;
  32. u16 ctrl;
  33. if (php_slot->irq > 0) {
  34. pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &ctrl);
  35. ctrl &= ~(PCI_EXP_SLTCTL_HPIE |
  36. PCI_EXP_SLTCTL_PDCE |
  37. PCI_EXP_SLTCTL_DLLSCE);
  38. pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, ctrl);
  39. free_irq(php_slot->irq, php_slot);
  40. php_slot->irq = 0;
  41. }
  42. if (php_slot->wq) {
  43. destroy_workqueue(php_slot->wq);
  44. php_slot->wq = NULL;
  45. }
  46. if (disable_device || irq > 0) {
  47. if (pdev->msix_enabled)
  48. pci_disable_msix(pdev);
  49. else if (pdev->msi_enabled)
  50. pci_disable_msi(pdev);
  51. pci_disable_device(pdev);
  52. }
  53. }
  54. static void pnv_php_free_slot(struct kref *kref)
  55. {
  56. struct pnv_php_slot *php_slot = container_of(kref,
  57. struct pnv_php_slot, kref);
  58. WARN_ON(!list_empty(&php_slot->children));
  59. pnv_php_disable_irq(php_slot, false);
  60. kfree(php_slot->name);
  61. kfree(php_slot);
  62. }
  63. static inline void pnv_php_put_slot(struct pnv_php_slot *php_slot)
  64. {
  65. if (!php_slot)
  66. return;
  67. kref_put(&php_slot->kref, pnv_php_free_slot);
  68. }
  69. static struct pnv_php_slot *pnv_php_match(struct device_node *dn,
  70. struct pnv_php_slot *php_slot)
  71. {
  72. struct pnv_php_slot *target, *tmp;
  73. if (php_slot->dn == dn) {
  74. kref_get(&php_slot->kref);
  75. return php_slot;
  76. }
  77. list_for_each_entry(tmp, &php_slot->children, link) {
  78. target = pnv_php_match(dn, tmp);
  79. if (target)
  80. return target;
  81. }
  82. return NULL;
  83. }
  84. struct pnv_php_slot *pnv_php_find_slot(struct device_node *dn)
  85. {
  86. struct pnv_php_slot *php_slot, *tmp;
  87. unsigned long flags;
  88. spin_lock_irqsave(&pnv_php_lock, flags);
  89. list_for_each_entry(tmp, &pnv_php_slot_list, link) {
  90. php_slot = pnv_php_match(dn, tmp);
  91. if (php_slot) {
  92. spin_unlock_irqrestore(&pnv_php_lock, flags);
  93. return php_slot;
  94. }
  95. }
  96. spin_unlock_irqrestore(&pnv_php_lock, flags);
  97. return NULL;
  98. }
  99. EXPORT_SYMBOL_GPL(pnv_php_find_slot);
  100. /*
  101. * Remove pdn for all children of the indicated device node.
  102. * The function should remove pdn in a depth-first manner.
  103. */
  104. static void pnv_php_rmv_pdns(struct device_node *dn)
  105. {
  106. struct device_node *child;
  107. for_each_child_of_node(dn, child) {
  108. pnv_php_rmv_pdns(child);
  109. pci_remove_device_node_info(child);
  110. }
  111. }
  112. /*
  113. * Detach all child nodes of the indicated device nodes. The
  114. * function should handle device nodes in depth-first manner.
  115. *
  116. * We should not invoke of_node_release() as the memory for
  117. * individual device node is part of large memory block. The
  118. * large block is allocated from memblock (system bootup) or
  119. * kmalloc() when unflattening the device tree by OF changeset.
  120. * We can not free the large block allocated from memblock. For
  121. * later case, it should be released at once.
  122. */
  123. static void pnv_php_detach_device_nodes(struct device_node *parent)
  124. {
  125. struct device_node *dn;
  126. int refcount;
  127. for_each_child_of_node(parent, dn) {
  128. pnv_php_detach_device_nodes(dn);
  129. of_node_put(dn);
  130. refcount = kref_read(&dn->kobj.kref);
  131. if (refcount != 1)
  132. pr_warn("Invalid refcount %d on <%pOF>\n",
  133. refcount, 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. pci_warn(php_slot->pdev, "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. pci_warn(php_slot->pdev, "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. pci_warn(php_slot->pdev, "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. pci_warn(php_slot->pdev, "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. be64_to_cpu(msg.params[3]) != OPAL_SUCCESS) {
  288. pci_warn(php_slot->pdev, "Wrong msg (%lld, %lld, %lld)\n",
  289. be64_to_cpu(msg.params[1]),
  290. be64_to_cpu(msg.params[2]),
  291. be64_to_cpu(msg.params[3]));
  292. return -ENOMSG;
  293. }
  294. } else if (ret < 0) {
  295. pci_warn(php_slot->pdev, "Error %d powering %s\n",
  296. ret, (state == OPAL_PCI_SLOT_POWER_ON) ? "on" : "off");
  297. return ret;
  298. }
  299. if (state == OPAL_PCI_SLOT_POWER_OFF || state == OPAL_PCI_SLOT_OFFLINE)
  300. pnv_php_rmv_devtree(php_slot);
  301. else
  302. ret = pnv_php_add_devtree(php_slot);
  303. return ret;
  304. }
  305. EXPORT_SYMBOL_GPL(pnv_php_set_slot_power_state);
  306. static int pnv_php_get_power_state(struct hotplug_slot *slot, u8 *state)
  307. {
  308. struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
  309. uint8_t power_state = OPAL_PCI_SLOT_POWER_ON;
  310. int ret;
  311. /*
  312. * Retrieve power status from firmware. If we fail
  313. * getting that, the power status fails back to
  314. * be on.
  315. */
  316. ret = pnv_pci_get_power_state(php_slot->id, &power_state);
  317. if (ret) {
  318. pci_warn(php_slot->pdev, "Error %d getting power status\n",
  319. ret);
  320. } else {
  321. *state = power_state;
  322. }
  323. return 0;
  324. }
  325. static int pnv_php_get_adapter_state(struct hotplug_slot *slot, u8 *state)
  326. {
  327. struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
  328. uint8_t presence = OPAL_PCI_SLOT_EMPTY;
  329. int ret;
  330. /*
  331. * Retrieve presence status from firmware. If we can't
  332. * get that, it will fail back to be empty.
  333. */
  334. ret = pnv_pci_get_presence_state(php_slot->id, &presence);
  335. if (ret >= 0) {
  336. *state = presence;
  337. ret = 0;
  338. } else {
  339. pci_warn(php_slot->pdev, "Error %d getting presence\n", ret);
  340. }
  341. return ret;
  342. }
  343. static int pnv_php_get_attention_state(struct hotplug_slot *slot, u8 *state)
  344. {
  345. struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
  346. *state = php_slot->attention_state;
  347. return 0;
  348. }
  349. static int pnv_php_set_attention_state(struct hotplug_slot *slot, u8 state)
  350. {
  351. struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
  352. /* FIXME: Make it real once firmware supports it */
  353. php_slot->attention_state = state;
  354. return 0;
  355. }
  356. static int pnv_php_enable(struct pnv_php_slot *php_slot, bool rescan)
  357. {
  358. struct hotplug_slot *slot = &php_slot->slot;
  359. uint8_t presence = OPAL_PCI_SLOT_EMPTY;
  360. uint8_t power_status = OPAL_PCI_SLOT_POWER_ON;
  361. int ret;
  362. /* Check if the slot has been configured */
  363. if (php_slot->state != PNV_PHP_STATE_REGISTERED)
  364. return 0;
  365. /* Retrieve slot presence status */
  366. ret = pnv_php_get_adapter_state(slot, &presence);
  367. if (ret)
  368. return ret;
  369. /*
  370. * Proceed if there have nothing behind the slot. However,
  371. * we should leave the slot in registered state at the
  372. * beginning. Otherwise, the PCI devices inserted afterwards
  373. * won't be probed and populated.
  374. */
  375. if (presence == OPAL_PCI_SLOT_EMPTY) {
  376. if (!php_slot->power_state_check) {
  377. php_slot->power_state_check = true;
  378. return 0;
  379. }
  380. goto scan;
  381. }
  382. /*
  383. * If the power supply to the slot is off, we can't detect
  384. * adapter presence state. That means we have to turn the
  385. * slot on before going to probe slot's presence state.
  386. *
  387. * On the first time, we don't change the power status to
  388. * boost system boot with assumption that the firmware
  389. * supplies consistent slot power status: empty slot always
  390. * has its power off and non-empty slot has its power on.
  391. */
  392. if (!php_slot->power_state_check) {
  393. php_slot->power_state_check = true;
  394. ret = pnv_php_get_power_state(slot, &power_status);
  395. if (ret)
  396. return ret;
  397. if (power_status != OPAL_PCI_SLOT_POWER_ON)
  398. return 0;
  399. }
  400. /* Check the power status. Scan the slot if it is already on */
  401. ret = pnv_php_get_power_state(slot, &power_status);
  402. if (ret)
  403. return ret;
  404. if (power_status == OPAL_PCI_SLOT_POWER_ON)
  405. goto scan;
  406. /* Power is off, turn it on and then scan the slot */
  407. ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_ON);
  408. if (ret)
  409. return ret;
  410. scan:
  411. if (presence == OPAL_PCI_SLOT_PRESENT) {
  412. if (rescan) {
  413. pci_lock_rescan_remove();
  414. pci_hp_add_devices(php_slot->bus);
  415. pci_unlock_rescan_remove();
  416. }
  417. /* Rescan for child hotpluggable slots */
  418. php_slot->state = PNV_PHP_STATE_POPULATED;
  419. if (rescan)
  420. pnv_php_register(php_slot->dn);
  421. } else {
  422. php_slot->state = PNV_PHP_STATE_POPULATED;
  423. }
  424. return 0;
  425. }
  426. static int pnv_php_enable_slot(struct hotplug_slot *slot)
  427. {
  428. struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
  429. return pnv_php_enable(php_slot, true);
  430. }
  431. static int pnv_php_disable_slot(struct hotplug_slot *slot)
  432. {
  433. struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
  434. int ret;
  435. if (php_slot->state != PNV_PHP_STATE_POPULATED)
  436. return 0;
  437. /* Remove all devices behind the slot */
  438. pci_lock_rescan_remove();
  439. pci_hp_remove_devices(php_slot->bus);
  440. pci_unlock_rescan_remove();
  441. /* Detach the child hotpluggable slots */
  442. pnv_php_unregister(php_slot->dn);
  443. /* Notify firmware and remove device nodes */
  444. ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_OFF);
  445. php_slot->state = PNV_PHP_STATE_REGISTERED;
  446. return ret;
  447. }
  448. static const struct hotplug_slot_ops php_slot_ops = {
  449. .get_power_status = pnv_php_get_power_state,
  450. .get_adapter_status = pnv_php_get_adapter_state,
  451. .get_attention_status = pnv_php_get_attention_state,
  452. .set_attention_status = pnv_php_set_attention_state,
  453. .enable_slot = pnv_php_enable_slot,
  454. .disable_slot = pnv_php_disable_slot,
  455. };
  456. static void pnv_php_release(struct pnv_php_slot *php_slot)
  457. {
  458. unsigned long flags;
  459. /* Remove from global or child list */
  460. spin_lock_irqsave(&pnv_php_lock, flags);
  461. list_del(&php_slot->link);
  462. spin_unlock_irqrestore(&pnv_php_lock, flags);
  463. /* Detach from parent */
  464. pnv_php_put_slot(php_slot);
  465. pnv_php_put_slot(php_slot->parent);
  466. }
  467. static struct pnv_php_slot *pnv_php_alloc_slot(struct device_node *dn)
  468. {
  469. struct pnv_php_slot *php_slot;
  470. struct pci_bus *bus;
  471. const char *label;
  472. uint64_t id;
  473. int ret;
  474. ret = of_property_read_string(dn, "ibm,slot-label", &label);
  475. if (ret)
  476. return NULL;
  477. if (pnv_pci_get_slot_id(dn, &id))
  478. return NULL;
  479. bus = pci_find_bus_by_node(dn);
  480. if (!bus)
  481. return NULL;
  482. php_slot = kzalloc(sizeof(*php_slot), GFP_KERNEL);
  483. if (!php_slot)
  484. return NULL;
  485. php_slot->name = kstrdup(label, GFP_KERNEL);
  486. if (!php_slot->name) {
  487. kfree(php_slot);
  488. return NULL;
  489. }
  490. if (dn->child && PCI_DN(dn->child))
  491. php_slot->slot_no = PCI_SLOT(PCI_DN(dn->child)->devfn);
  492. else
  493. php_slot->slot_no = -1; /* Placeholder slot */
  494. kref_init(&php_slot->kref);
  495. php_slot->state = PNV_PHP_STATE_INITIALIZED;
  496. php_slot->dn = dn;
  497. php_slot->pdev = bus->self;
  498. php_slot->bus = bus;
  499. php_slot->id = id;
  500. php_slot->power_state_check = false;
  501. php_slot->slot.ops = &php_slot_ops;
  502. INIT_LIST_HEAD(&php_slot->children);
  503. INIT_LIST_HEAD(&php_slot->link);
  504. return php_slot;
  505. }
  506. static int pnv_php_register_slot(struct pnv_php_slot *php_slot)
  507. {
  508. struct pnv_php_slot *parent;
  509. struct device_node *dn = php_slot->dn;
  510. unsigned long flags;
  511. int ret;
  512. /* Check if the slot is registered or not */
  513. parent = pnv_php_find_slot(php_slot->dn);
  514. if (parent) {
  515. pnv_php_put_slot(parent);
  516. return -EEXIST;
  517. }
  518. /* Register PCI slot */
  519. ret = pci_hp_register(&php_slot->slot, php_slot->bus,
  520. php_slot->slot_no, php_slot->name);
  521. if (ret) {
  522. pci_warn(php_slot->pdev, "Error %d registering slot\n", ret);
  523. return ret;
  524. }
  525. /* Attach to the parent's child list or global list */
  526. while ((dn = of_get_parent(dn))) {
  527. if (!PCI_DN(dn)) {
  528. of_node_put(dn);
  529. break;
  530. }
  531. parent = pnv_php_find_slot(dn);
  532. if (parent) {
  533. of_node_put(dn);
  534. break;
  535. }
  536. of_node_put(dn);
  537. }
  538. spin_lock_irqsave(&pnv_php_lock, flags);
  539. php_slot->parent = parent;
  540. if (parent)
  541. list_add_tail(&php_slot->link, &parent->children);
  542. else
  543. list_add_tail(&php_slot->link, &pnv_php_slot_list);
  544. spin_unlock_irqrestore(&pnv_php_lock, flags);
  545. php_slot->state = PNV_PHP_STATE_REGISTERED;
  546. return 0;
  547. }
  548. static int pnv_php_enable_msix(struct pnv_php_slot *php_slot)
  549. {
  550. struct pci_dev *pdev = php_slot->pdev;
  551. struct msix_entry entry;
  552. int nr_entries, ret;
  553. u16 pcie_flag;
  554. /* Get total number of MSIx entries */
  555. nr_entries = pci_msix_vec_count(pdev);
  556. if (nr_entries < 0)
  557. return nr_entries;
  558. /* Check hotplug MSIx entry is in range */
  559. pcie_capability_read_word(pdev, PCI_EXP_FLAGS, &pcie_flag);
  560. entry.entry = (pcie_flag & PCI_EXP_FLAGS_IRQ) >> 9;
  561. if (entry.entry >= nr_entries)
  562. return -ERANGE;
  563. /* Enable MSIx */
  564. ret = pci_enable_msix_exact(pdev, &entry, 1);
  565. if (ret) {
  566. pci_warn(pdev, "Error %d enabling MSIx\n", ret);
  567. return ret;
  568. }
  569. return entry.vector;
  570. }
  571. static void pnv_php_event_handler(struct work_struct *work)
  572. {
  573. struct pnv_php_event *event =
  574. container_of(work, struct pnv_php_event, work);
  575. struct pnv_php_slot *php_slot = event->php_slot;
  576. if (event->added)
  577. pnv_php_enable_slot(&php_slot->slot);
  578. else
  579. pnv_php_disable_slot(&php_slot->slot);
  580. kfree(event);
  581. }
  582. static irqreturn_t pnv_php_interrupt(int irq, void *data)
  583. {
  584. struct pnv_php_slot *php_slot = data;
  585. struct pci_dev *pchild, *pdev = php_slot->pdev;
  586. struct eeh_dev *edev;
  587. struct eeh_pe *pe;
  588. struct pnv_php_event *event;
  589. u16 sts, lsts;
  590. u8 presence;
  591. bool added;
  592. unsigned long flags;
  593. int ret;
  594. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &sts);
  595. sts &= (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC);
  596. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, sts);
  597. if (sts & PCI_EXP_SLTSTA_DLLSC) {
  598. pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lsts);
  599. added = !!(lsts & PCI_EXP_LNKSTA_DLLLA);
  600. } else if (!(php_slot->flags & PNV_PHP_FLAG_BROKEN_PDC) &&
  601. (sts & PCI_EXP_SLTSTA_PDC)) {
  602. ret = pnv_pci_get_presence_state(php_slot->id, &presence);
  603. if (ret) {
  604. pci_warn(pdev, "PCI slot [%s] error %d getting presence (0x%04x), to retry the operation.\n",
  605. php_slot->name, ret, sts);
  606. return IRQ_HANDLED;
  607. }
  608. added = !!(presence == OPAL_PCI_SLOT_PRESENT);
  609. } else {
  610. return IRQ_NONE;
  611. }
  612. /* Freeze the removed PE to avoid unexpected error reporting */
  613. if (!added) {
  614. pchild = list_first_entry_or_null(&php_slot->bus->devices,
  615. struct pci_dev, bus_list);
  616. edev = pchild ? pci_dev_to_eeh_dev(pchild) : NULL;
  617. pe = edev ? edev->pe : NULL;
  618. if (pe) {
  619. eeh_serialize_lock(&flags);
  620. eeh_pe_mark_isolated(pe);
  621. eeh_serialize_unlock(flags);
  622. eeh_pe_set_option(pe, EEH_OPT_FREEZE_PE);
  623. }
  624. }
  625. /*
  626. * The PE is left in frozen state if the event is missed. It's
  627. * fine as the PCI devices (PE) aren't functional any more.
  628. */
  629. event = kzalloc(sizeof(*event), GFP_ATOMIC);
  630. if (!event) {
  631. pci_warn(pdev, "PCI slot [%s] missed hotplug event 0x%04x\n",
  632. php_slot->name, sts);
  633. return IRQ_HANDLED;
  634. }
  635. pci_info(pdev, "PCI slot [%s] %s (IRQ: %d)\n",
  636. php_slot->name, added ? "added" : "removed", irq);
  637. INIT_WORK(&event->work, pnv_php_event_handler);
  638. event->added = added;
  639. event->php_slot = php_slot;
  640. queue_work(php_slot->wq, &event->work);
  641. return IRQ_HANDLED;
  642. }
  643. static void pnv_php_init_irq(struct pnv_php_slot *php_slot, int irq)
  644. {
  645. struct pci_dev *pdev = php_slot->pdev;
  646. u32 broken_pdc = 0;
  647. u16 sts, ctrl;
  648. int ret;
  649. /* Allocate workqueue */
  650. php_slot->wq = alloc_workqueue("pciehp-%s", 0, 0, php_slot->name);
  651. if (!php_slot->wq) {
  652. pci_warn(pdev, "Cannot alloc workqueue\n");
  653. pnv_php_disable_irq(php_slot, true);
  654. return;
  655. }
  656. /* Check PDC (Presence Detection Change) is broken or not */
  657. ret = of_property_read_u32(php_slot->dn, "ibm,slot-broken-pdc",
  658. &broken_pdc);
  659. if (!ret && broken_pdc)
  660. php_slot->flags |= PNV_PHP_FLAG_BROKEN_PDC;
  661. /* Clear pending interrupts */
  662. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &sts);
  663. if (php_slot->flags & PNV_PHP_FLAG_BROKEN_PDC)
  664. sts |= PCI_EXP_SLTSTA_DLLSC;
  665. else
  666. sts |= (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC);
  667. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, sts);
  668. /* Request the interrupt */
  669. ret = request_irq(irq, pnv_php_interrupt, IRQF_SHARED,
  670. php_slot->name, php_slot);
  671. if (ret) {
  672. pnv_php_disable_irq(php_slot, true);
  673. pci_warn(pdev, "Error %d enabling IRQ %d\n", ret, irq);
  674. return;
  675. }
  676. /* Enable the interrupts */
  677. pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &ctrl);
  678. if (php_slot->flags & PNV_PHP_FLAG_BROKEN_PDC) {
  679. ctrl &= ~PCI_EXP_SLTCTL_PDCE;
  680. ctrl |= (PCI_EXP_SLTCTL_HPIE |
  681. PCI_EXP_SLTCTL_DLLSCE);
  682. } else {
  683. ctrl |= (PCI_EXP_SLTCTL_HPIE |
  684. PCI_EXP_SLTCTL_PDCE |
  685. PCI_EXP_SLTCTL_DLLSCE);
  686. }
  687. pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, ctrl);
  688. /* The interrupt is initialized successfully when @irq is valid */
  689. php_slot->irq = irq;
  690. }
  691. static void pnv_php_enable_irq(struct pnv_php_slot *php_slot)
  692. {
  693. struct pci_dev *pdev = php_slot->pdev;
  694. int irq, ret;
  695. /*
  696. * The MSI/MSIx interrupt might have been occupied by other
  697. * drivers. Don't populate the surprise hotplug capability
  698. * in that case.
  699. */
  700. if (pci_dev_msi_enabled(pdev))
  701. return;
  702. ret = pci_enable_device(pdev);
  703. if (ret) {
  704. pci_warn(pdev, "Error %d enabling device\n", ret);
  705. return;
  706. }
  707. pci_set_master(pdev);
  708. /* Enable MSIx interrupt */
  709. irq = pnv_php_enable_msix(php_slot);
  710. if (irq > 0) {
  711. pnv_php_init_irq(php_slot, irq);
  712. return;
  713. }
  714. /*
  715. * Use MSI if MSIx doesn't work. Fail back to legacy INTx
  716. * if MSI doesn't work either
  717. */
  718. ret = pci_enable_msi(pdev);
  719. if (!ret || pdev->irq) {
  720. irq = pdev->irq;
  721. pnv_php_init_irq(php_slot, irq);
  722. }
  723. }
  724. static int pnv_php_register_one(struct device_node *dn)
  725. {
  726. struct pnv_php_slot *php_slot;
  727. u32 prop32;
  728. int ret;
  729. /* Check if it's hotpluggable slot */
  730. ret = of_property_read_u32(dn, "ibm,slot-pluggable", &prop32);
  731. if (ret || !prop32)
  732. return -ENXIO;
  733. ret = of_property_read_u32(dn, "ibm,reset-by-firmware", &prop32);
  734. if (ret || !prop32)
  735. return -ENXIO;
  736. php_slot = pnv_php_alloc_slot(dn);
  737. if (!php_slot)
  738. return -ENODEV;
  739. ret = pnv_php_register_slot(php_slot);
  740. if (ret)
  741. goto free_slot;
  742. ret = pnv_php_enable(php_slot, false);
  743. if (ret)
  744. goto unregister_slot;
  745. /* Enable interrupt if the slot supports surprise hotplug */
  746. ret = of_property_read_u32(dn, "ibm,slot-surprise-pluggable", &prop32);
  747. if (!ret && prop32)
  748. pnv_php_enable_irq(php_slot);
  749. return 0;
  750. unregister_slot:
  751. pnv_php_unregister_one(php_slot->dn);
  752. free_slot:
  753. pnv_php_put_slot(php_slot);
  754. return ret;
  755. }
  756. static void pnv_php_register(struct device_node *dn)
  757. {
  758. struct device_node *child;
  759. /*
  760. * The parent slots should be registered before their
  761. * child slots.
  762. */
  763. for_each_child_of_node(dn, child) {
  764. pnv_php_register_one(child);
  765. pnv_php_register(child);
  766. }
  767. }
  768. static void pnv_php_unregister_one(struct device_node *dn)
  769. {
  770. struct pnv_php_slot *php_slot;
  771. php_slot = pnv_php_find_slot(dn);
  772. if (!php_slot)
  773. return;
  774. php_slot->state = PNV_PHP_STATE_OFFLINE;
  775. pci_hp_deregister(&php_slot->slot);
  776. pnv_php_release(php_slot);
  777. pnv_php_put_slot(php_slot);
  778. }
  779. static void pnv_php_unregister(struct device_node *dn)
  780. {
  781. struct device_node *child;
  782. /* The child slots should go before their parent slots */
  783. for_each_child_of_node(dn, child) {
  784. pnv_php_unregister(child);
  785. pnv_php_unregister_one(child);
  786. }
  787. }
  788. static int __init pnv_php_init(void)
  789. {
  790. struct device_node *dn;
  791. pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  792. for_each_compatible_node(dn, NULL, "ibm,ioda2-phb")
  793. pnv_php_register(dn);
  794. return 0;
  795. }
  796. static void __exit pnv_php_exit(void)
  797. {
  798. struct device_node *dn;
  799. for_each_compatible_node(dn, NULL, "ibm,ioda2-phb")
  800. pnv_php_unregister(dn);
  801. }
  802. module_init(pnv_php_init);
  803. module_exit(pnv_php_exit);
  804. MODULE_VERSION(DRIVER_VERSION);
  805. MODULE_LICENSE("GPL v2");
  806. MODULE_AUTHOR(DRIVER_AUTHOR);
  807. MODULE_DESCRIPTION(DRIVER_DESC);