pnv_php.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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 = kzalloc(fdt_totalsize(fdt1), GFP_KERNEL);
  228. if (!fdt) {
  229. ret = -ENOMEM;
  230. goto free_fdt1;
  231. }
  232. /* Unflatten device tree blob */
  233. memcpy(fdt, fdt1, fdt_totalsize(fdt1));
  234. dt = of_fdt_unflatten_tree(fdt, php_slot->dn, NULL);
  235. if (!dt) {
  236. ret = -EINVAL;
  237. pci_warn(php_slot->pdev, "Cannot unflatten FDT\n");
  238. goto free_fdt;
  239. }
  240. /* Initialize and apply the changeset */
  241. of_changeset_init(&php_slot->ocs);
  242. pnv_php_reverse_nodes(php_slot->dn);
  243. ret = pnv_php_populate_changeset(&php_slot->ocs, php_slot->dn);
  244. if (ret) {
  245. pnv_php_reverse_nodes(php_slot->dn);
  246. pci_warn(php_slot->pdev, "Error %d populating changeset\n",
  247. ret);
  248. goto free_dt;
  249. }
  250. php_slot->dn->child = NULL;
  251. ret = of_changeset_apply(&php_slot->ocs);
  252. if (ret) {
  253. pci_warn(php_slot->pdev, "Error %d applying changeset\n", ret);
  254. goto destroy_changeset;
  255. }
  256. /* Add device node firmware data */
  257. pnv_php_add_pdns(php_slot);
  258. php_slot->fdt = fdt;
  259. php_slot->dt = dt;
  260. kfree(fdt1);
  261. goto out;
  262. destroy_changeset:
  263. of_changeset_destroy(&php_slot->ocs);
  264. free_dt:
  265. kfree(dt);
  266. php_slot->dn->child = NULL;
  267. free_fdt:
  268. kfree(fdt);
  269. free_fdt1:
  270. kfree(fdt1);
  271. out:
  272. return ret;
  273. }
  274. int pnv_php_set_slot_power_state(struct hotplug_slot *slot,
  275. uint8_t state)
  276. {
  277. struct pnv_php_slot *php_slot = slot->private;
  278. struct opal_msg msg;
  279. int ret;
  280. ret = pnv_pci_set_power_state(php_slot->id, state, &msg);
  281. if (ret > 0) {
  282. if (be64_to_cpu(msg.params[1]) != php_slot->dn->phandle ||
  283. be64_to_cpu(msg.params[2]) != state ||
  284. be64_to_cpu(msg.params[3]) != OPAL_SUCCESS) {
  285. pci_warn(php_slot->pdev, "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. } else if (ret < 0) {
  292. pci_warn(php_slot->pdev, "Error %d powering %s\n",
  293. ret, (state == OPAL_PCI_SLOT_POWER_ON) ? "on" : "off");
  294. return ret;
  295. }
  296. if (state == OPAL_PCI_SLOT_POWER_OFF || state == OPAL_PCI_SLOT_OFFLINE)
  297. pnv_php_rmv_devtree(php_slot);
  298. else
  299. ret = pnv_php_add_devtree(php_slot);
  300. return ret;
  301. }
  302. EXPORT_SYMBOL_GPL(pnv_php_set_slot_power_state);
  303. static int pnv_php_get_power_state(struct hotplug_slot *slot, u8 *state)
  304. {
  305. struct pnv_php_slot *php_slot = slot->private;
  306. uint8_t power_state = OPAL_PCI_SLOT_POWER_ON;
  307. int ret;
  308. /*
  309. * Retrieve power status from firmware. If we fail
  310. * getting that, the power status fails back to
  311. * be on.
  312. */
  313. ret = pnv_pci_get_power_state(php_slot->id, &power_state);
  314. if (ret) {
  315. pci_warn(php_slot->pdev, "Error %d getting power status\n",
  316. ret);
  317. } else {
  318. *state = power_state;
  319. slot->info->power_status = power_state;
  320. }
  321. return 0;
  322. }
  323. static int pnv_php_get_adapter_state(struct hotplug_slot *slot, u8 *state)
  324. {
  325. struct pnv_php_slot *php_slot = slot->private;
  326. uint8_t presence = OPAL_PCI_SLOT_EMPTY;
  327. int ret;
  328. /*
  329. * Retrieve presence status from firmware. If we can't
  330. * get that, it will fail back to be empty.
  331. */
  332. ret = pnv_pci_get_presence_state(php_slot->id, &presence);
  333. if (ret >= 0) {
  334. *state = presence;
  335. slot->info->adapter_status = presence;
  336. ret = 0;
  337. } else {
  338. pci_warn(php_slot->pdev, "Error %d getting presence\n", ret);
  339. }
  340. return ret;
  341. }
  342. static int pnv_php_set_attention_state(struct hotplug_slot *slot, u8 state)
  343. {
  344. /* FIXME: Make it real once firmware supports it */
  345. slot->info->attention_status = state;
  346. return 0;
  347. }
  348. static int pnv_php_enable(struct pnv_php_slot *php_slot, bool rescan)
  349. {
  350. struct hotplug_slot *slot = &php_slot->slot;
  351. uint8_t presence = OPAL_PCI_SLOT_EMPTY;
  352. uint8_t power_status = OPAL_PCI_SLOT_POWER_ON;
  353. int ret;
  354. /* Check if the slot has been configured */
  355. if (php_slot->state != PNV_PHP_STATE_REGISTERED)
  356. return 0;
  357. /* Retrieve slot presence status */
  358. ret = pnv_php_get_adapter_state(slot, &presence);
  359. if (ret)
  360. return ret;
  361. /*
  362. * Proceed if there have nothing behind the slot. However,
  363. * we should leave the slot in registered state at the
  364. * beginning. Otherwise, the PCI devices inserted afterwards
  365. * won't be probed and populated.
  366. */
  367. if (presence == OPAL_PCI_SLOT_EMPTY) {
  368. if (!php_slot->power_state_check) {
  369. php_slot->power_state_check = true;
  370. return 0;
  371. }
  372. goto scan;
  373. }
  374. /*
  375. * If the power supply to the slot is off, we can't detect
  376. * adapter presence state. That means we have to turn the
  377. * slot on before going to probe slot's presence state.
  378. *
  379. * On the first time, we don't change the power status to
  380. * boost system boot with assumption that the firmware
  381. * supplies consistent slot power status: empty slot always
  382. * has its power off and non-empty slot has its power on.
  383. */
  384. if (!php_slot->power_state_check) {
  385. php_slot->power_state_check = true;
  386. ret = pnv_php_get_power_state(slot, &power_status);
  387. if (ret)
  388. return ret;
  389. if (power_status != OPAL_PCI_SLOT_POWER_ON)
  390. return 0;
  391. }
  392. /* Check the power status. Scan the slot if it is already on */
  393. ret = pnv_php_get_power_state(slot, &power_status);
  394. if (ret)
  395. return ret;
  396. if (power_status == OPAL_PCI_SLOT_POWER_ON)
  397. goto scan;
  398. /* Power is off, turn it on and then scan the slot */
  399. ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_ON);
  400. if (ret)
  401. return ret;
  402. scan:
  403. if (presence == OPAL_PCI_SLOT_PRESENT) {
  404. if (rescan) {
  405. pci_lock_rescan_remove();
  406. pci_hp_add_devices(php_slot->bus);
  407. pci_unlock_rescan_remove();
  408. }
  409. /* Rescan for child hotpluggable slots */
  410. php_slot->state = PNV_PHP_STATE_POPULATED;
  411. if (rescan)
  412. pnv_php_register(php_slot->dn);
  413. } else {
  414. php_slot->state = PNV_PHP_STATE_POPULATED;
  415. }
  416. return 0;
  417. }
  418. static int pnv_php_enable_slot(struct hotplug_slot *slot)
  419. {
  420. struct pnv_php_slot *php_slot = container_of(slot,
  421. struct pnv_php_slot, slot);
  422. return pnv_php_enable(php_slot, true);
  423. }
  424. static int pnv_php_disable_slot(struct hotplug_slot *slot)
  425. {
  426. struct pnv_php_slot *php_slot = slot->private;
  427. int ret;
  428. if (php_slot->state != PNV_PHP_STATE_POPULATED)
  429. return 0;
  430. /* Remove all devices behind the slot */
  431. pci_lock_rescan_remove();
  432. pci_hp_remove_devices(php_slot->bus);
  433. pci_unlock_rescan_remove();
  434. /* Detach the child hotpluggable slots */
  435. pnv_php_unregister(php_slot->dn);
  436. /* Notify firmware and remove device nodes */
  437. ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_OFF);
  438. php_slot->state = PNV_PHP_STATE_REGISTERED;
  439. return ret;
  440. }
  441. static struct hotplug_slot_ops php_slot_ops = {
  442. .get_power_status = pnv_php_get_power_state,
  443. .get_adapter_status = pnv_php_get_adapter_state,
  444. .set_attention_status = pnv_php_set_attention_state,
  445. .enable_slot = pnv_php_enable_slot,
  446. .disable_slot = pnv_php_disable_slot,
  447. };
  448. static void pnv_php_release(struct pnv_php_slot *php_slot)
  449. {
  450. unsigned long flags;
  451. /* Remove from global or child list */
  452. spin_lock_irqsave(&pnv_php_lock, flags);
  453. list_del(&php_slot->link);
  454. spin_unlock_irqrestore(&pnv_php_lock, flags);
  455. /* Detach from parent */
  456. pnv_php_put_slot(php_slot);
  457. pnv_php_put_slot(php_slot->parent);
  458. }
  459. static struct pnv_php_slot *pnv_php_alloc_slot(struct device_node *dn)
  460. {
  461. struct pnv_php_slot *php_slot;
  462. struct pci_bus *bus;
  463. const char *label;
  464. uint64_t id;
  465. int ret;
  466. ret = of_property_read_string(dn, "ibm,slot-label", &label);
  467. if (ret)
  468. return NULL;
  469. if (pnv_pci_get_slot_id(dn, &id))
  470. return NULL;
  471. bus = pci_find_bus_by_node(dn);
  472. if (!bus)
  473. return NULL;
  474. php_slot = kzalloc(sizeof(*php_slot), GFP_KERNEL);
  475. if (!php_slot)
  476. return NULL;
  477. php_slot->name = kstrdup(label, GFP_KERNEL);
  478. if (!php_slot->name) {
  479. kfree(php_slot);
  480. return NULL;
  481. }
  482. if (dn->child && PCI_DN(dn->child))
  483. php_slot->slot_no = PCI_SLOT(PCI_DN(dn->child)->devfn);
  484. else
  485. php_slot->slot_no = -1; /* Placeholder slot */
  486. kref_init(&php_slot->kref);
  487. php_slot->state = PNV_PHP_STATE_INITIALIZED;
  488. php_slot->dn = dn;
  489. php_slot->pdev = bus->self;
  490. php_slot->bus = bus;
  491. php_slot->id = id;
  492. php_slot->power_state_check = false;
  493. php_slot->slot.ops = &php_slot_ops;
  494. php_slot->slot.info = &php_slot->slot_info;
  495. php_slot->slot.private = php_slot;
  496. INIT_LIST_HEAD(&php_slot->children);
  497. INIT_LIST_HEAD(&php_slot->link);
  498. return php_slot;
  499. }
  500. static int pnv_php_register_slot(struct pnv_php_slot *php_slot)
  501. {
  502. struct pnv_php_slot *parent;
  503. struct device_node *dn = php_slot->dn;
  504. unsigned long flags;
  505. int ret;
  506. /* Check if the slot is registered or not */
  507. parent = pnv_php_find_slot(php_slot->dn);
  508. if (parent) {
  509. pnv_php_put_slot(parent);
  510. return -EEXIST;
  511. }
  512. /* Register PCI slot */
  513. ret = pci_hp_register(&php_slot->slot, php_slot->bus,
  514. php_slot->slot_no, php_slot->name);
  515. if (ret) {
  516. pci_warn(php_slot->pdev, "Error %d registering slot\n", ret);
  517. return ret;
  518. }
  519. /* Attach to the parent's child list or global list */
  520. while ((dn = of_get_parent(dn))) {
  521. if (!PCI_DN(dn)) {
  522. of_node_put(dn);
  523. break;
  524. }
  525. parent = pnv_php_find_slot(dn);
  526. if (parent) {
  527. of_node_put(dn);
  528. break;
  529. }
  530. of_node_put(dn);
  531. }
  532. spin_lock_irqsave(&pnv_php_lock, flags);
  533. php_slot->parent = parent;
  534. if (parent)
  535. list_add_tail(&php_slot->link, &parent->children);
  536. else
  537. list_add_tail(&php_slot->link, &pnv_php_slot_list);
  538. spin_unlock_irqrestore(&pnv_php_lock, flags);
  539. php_slot->state = PNV_PHP_STATE_REGISTERED;
  540. return 0;
  541. }
  542. static int pnv_php_enable_msix(struct pnv_php_slot *php_slot)
  543. {
  544. struct pci_dev *pdev = php_slot->pdev;
  545. struct msix_entry entry;
  546. int nr_entries, ret;
  547. u16 pcie_flag;
  548. /* Get total number of MSIx entries */
  549. nr_entries = pci_msix_vec_count(pdev);
  550. if (nr_entries < 0)
  551. return nr_entries;
  552. /* Check hotplug MSIx entry is in range */
  553. pcie_capability_read_word(pdev, PCI_EXP_FLAGS, &pcie_flag);
  554. entry.entry = (pcie_flag & PCI_EXP_FLAGS_IRQ) >> 9;
  555. if (entry.entry >= nr_entries)
  556. return -ERANGE;
  557. /* Enable MSIx */
  558. ret = pci_enable_msix_exact(pdev, &entry, 1);
  559. if (ret) {
  560. pci_warn(pdev, "Error %d enabling MSIx\n", ret);
  561. return ret;
  562. }
  563. return entry.vector;
  564. }
  565. static void pnv_php_event_handler(struct work_struct *work)
  566. {
  567. struct pnv_php_event *event =
  568. container_of(work, struct pnv_php_event, work);
  569. struct pnv_php_slot *php_slot = event->php_slot;
  570. if (event->added)
  571. pnv_php_enable_slot(&php_slot->slot);
  572. else
  573. pnv_php_disable_slot(&php_slot->slot);
  574. kfree(event);
  575. }
  576. static irqreturn_t pnv_php_interrupt(int irq, void *data)
  577. {
  578. struct pnv_php_slot *php_slot = data;
  579. struct pci_dev *pchild, *pdev = php_slot->pdev;
  580. struct eeh_dev *edev;
  581. struct eeh_pe *pe;
  582. struct pnv_php_event *event;
  583. u16 sts, lsts;
  584. u8 presence;
  585. bool added;
  586. unsigned long flags;
  587. int ret;
  588. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &sts);
  589. sts &= (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC);
  590. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, sts);
  591. if (sts & PCI_EXP_SLTSTA_DLLSC) {
  592. pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lsts);
  593. added = !!(lsts & PCI_EXP_LNKSTA_DLLLA);
  594. } else if (!(php_slot->flags & PNV_PHP_FLAG_BROKEN_PDC) &&
  595. (sts & PCI_EXP_SLTSTA_PDC)) {
  596. ret = pnv_pci_get_presence_state(php_slot->id, &presence);
  597. if (ret) {
  598. pci_warn(pdev, "PCI slot [%s] error %d getting presence (0x%04x), to retry the operation.\n",
  599. php_slot->name, ret, sts);
  600. return IRQ_HANDLED;
  601. }
  602. added = !!(presence == OPAL_PCI_SLOT_PRESENT);
  603. } else {
  604. return IRQ_NONE;
  605. }
  606. /* Freeze the removed PE to avoid unexpected error reporting */
  607. if (!added) {
  608. pchild = list_first_entry_or_null(&php_slot->bus->devices,
  609. struct pci_dev, bus_list);
  610. edev = pchild ? pci_dev_to_eeh_dev(pchild) : NULL;
  611. pe = edev ? edev->pe : NULL;
  612. if (pe) {
  613. eeh_serialize_lock(&flags);
  614. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  615. eeh_serialize_unlock(flags);
  616. eeh_pe_set_option(pe, EEH_OPT_FREEZE_PE);
  617. }
  618. }
  619. /*
  620. * The PE is left in frozen state if the event is missed. It's
  621. * fine as the PCI devices (PE) aren't functional any more.
  622. */
  623. event = kzalloc(sizeof(*event), GFP_ATOMIC);
  624. if (!event) {
  625. pci_warn(pdev, "PCI slot [%s] missed hotplug event 0x%04x\n",
  626. php_slot->name, sts);
  627. return IRQ_HANDLED;
  628. }
  629. pci_info(pdev, "PCI slot [%s] %s (IRQ: %d)\n",
  630. php_slot->name, added ? "added" : "removed", irq);
  631. INIT_WORK(&event->work, pnv_php_event_handler);
  632. event->added = added;
  633. event->php_slot = php_slot;
  634. queue_work(php_slot->wq, &event->work);
  635. return IRQ_HANDLED;
  636. }
  637. static void pnv_php_init_irq(struct pnv_php_slot *php_slot, int irq)
  638. {
  639. struct pci_dev *pdev = php_slot->pdev;
  640. u32 broken_pdc = 0;
  641. u16 sts, ctrl;
  642. int ret;
  643. /* Allocate workqueue */
  644. php_slot->wq = alloc_workqueue("pciehp-%s", 0, 0, php_slot->name);
  645. if (!php_slot->wq) {
  646. pci_warn(pdev, "Cannot alloc workqueue\n");
  647. pnv_php_disable_irq(php_slot, true);
  648. return;
  649. }
  650. /* Check PDC (Presence Detection Change) is broken or not */
  651. ret = of_property_read_u32(php_slot->dn, "ibm,slot-broken-pdc",
  652. &broken_pdc);
  653. if (!ret && broken_pdc)
  654. php_slot->flags |= PNV_PHP_FLAG_BROKEN_PDC;
  655. /* Clear pending interrupts */
  656. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &sts);
  657. if (php_slot->flags & PNV_PHP_FLAG_BROKEN_PDC)
  658. sts |= PCI_EXP_SLTSTA_DLLSC;
  659. else
  660. sts |= (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC);
  661. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, sts);
  662. /* Request the interrupt */
  663. ret = request_irq(irq, pnv_php_interrupt, IRQF_SHARED,
  664. php_slot->name, php_slot);
  665. if (ret) {
  666. pnv_php_disable_irq(php_slot, true);
  667. pci_warn(pdev, "Error %d enabling IRQ %d\n", ret, irq);
  668. return;
  669. }
  670. /* Enable the interrupts */
  671. pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &ctrl);
  672. if (php_slot->flags & PNV_PHP_FLAG_BROKEN_PDC) {
  673. ctrl &= ~PCI_EXP_SLTCTL_PDCE;
  674. ctrl |= (PCI_EXP_SLTCTL_HPIE |
  675. PCI_EXP_SLTCTL_DLLSCE);
  676. } else {
  677. ctrl |= (PCI_EXP_SLTCTL_HPIE |
  678. PCI_EXP_SLTCTL_PDCE |
  679. PCI_EXP_SLTCTL_DLLSCE);
  680. }
  681. pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, ctrl);
  682. /* The interrupt is initialized successfully when @irq is valid */
  683. php_slot->irq = irq;
  684. }
  685. static void pnv_php_enable_irq(struct pnv_php_slot *php_slot)
  686. {
  687. struct pci_dev *pdev = php_slot->pdev;
  688. int irq, ret;
  689. /*
  690. * The MSI/MSIx interrupt might have been occupied by other
  691. * drivers. Don't populate the surprise hotplug capability
  692. * in that case.
  693. */
  694. if (pci_dev_msi_enabled(pdev))
  695. return;
  696. ret = pci_enable_device(pdev);
  697. if (ret) {
  698. pci_warn(pdev, "Error %d enabling device\n", ret);
  699. return;
  700. }
  701. pci_set_master(pdev);
  702. /* Enable MSIx interrupt */
  703. irq = pnv_php_enable_msix(php_slot);
  704. if (irq > 0) {
  705. pnv_php_init_irq(php_slot, irq);
  706. return;
  707. }
  708. /*
  709. * Use MSI if MSIx doesn't work. Fail back to legacy INTx
  710. * if MSI doesn't work either
  711. */
  712. ret = pci_enable_msi(pdev);
  713. if (!ret || pdev->irq) {
  714. irq = pdev->irq;
  715. pnv_php_init_irq(php_slot, irq);
  716. }
  717. }
  718. static int pnv_php_register_one(struct device_node *dn)
  719. {
  720. struct pnv_php_slot *php_slot;
  721. u32 prop32;
  722. int ret;
  723. /* Check if it's hotpluggable slot */
  724. ret = of_property_read_u32(dn, "ibm,slot-pluggable", &prop32);
  725. if (ret || !prop32)
  726. return -ENXIO;
  727. ret = of_property_read_u32(dn, "ibm,reset-by-firmware", &prop32);
  728. if (ret || !prop32)
  729. return -ENXIO;
  730. php_slot = pnv_php_alloc_slot(dn);
  731. if (!php_slot)
  732. return -ENODEV;
  733. ret = pnv_php_register_slot(php_slot);
  734. if (ret)
  735. goto free_slot;
  736. ret = pnv_php_enable(php_slot, false);
  737. if (ret)
  738. goto unregister_slot;
  739. /* Enable interrupt if the slot supports surprise hotplug */
  740. ret = of_property_read_u32(dn, "ibm,slot-surprise-pluggable", &prop32);
  741. if (!ret && prop32)
  742. pnv_php_enable_irq(php_slot);
  743. return 0;
  744. unregister_slot:
  745. pnv_php_unregister_one(php_slot->dn);
  746. free_slot:
  747. pnv_php_put_slot(php_slot);
  748. return ret;
  749. }
  750. static void pnv_php_register(struct device_node *dn)
  751. {
  752. struct device_node *child;
  753. /*
  754. * The parent slots should be registered before their
  755. * child slots.
  756. */
  757. for_each_child_of_node(dn, child) {
  758. pnv_php_register_one(child);
  759. pnv_php_register(child);
  760. }
  761. }
  762. static void pnv_php_unregister_one(struct device_node *dn)
  763. {
  764. struct pnv_php_slot *php_slot;
  765. php_slot = pnv_php_find_slot(dn);
  766. if (!php_slot)
  767. return;
  768. php_slot->state = PNV_PHP_STATE_OFFLINE;
  769. pci_hp_deregister(&php_slot->slot);
  770. pnv_php_release(php_slot);
  771. pnv_php_put_slot(php_slot);
  772. }
  773. static void pnv_php_unregister(struct device_node *dn)
  774. {
  775. struct device_node *child;
  776. /* The child slots should go before their parent slots */
  777. for_each_child_of_node(dn, child) {
  778. pnv_php_unregister(child);
  779. pnv_php_unregister_one(child);
  780. }
  781. }
  782. static int __init pnv_php_init(void)
  783. {
  784. struct device_node *dn;
  785. pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  786. for_each_compatible_node(dn, NULL, "ibm,ioda2-phb")
  787. pnv_php_register(dn);
  788. return 0;
  789. }
  790. static void __exit pnv_php_exit(void)
  791. {
  792. struct device_node *dn;
  793. for_each_compatible_node(dn, NULL, "ibm,ioda2-phb")
  794. pnv_php_unregister(dn);
  795. }
  796. module_init(pnv_php_init);
  797. module_exit(pnv_php_exit);
  798. MODULE_VERSION(DRIVER_VERSION);
  799. MODULE_LICENSE("GPL v2");
  800. MODULE_AUTHOR(DRIVER_AUTHOR);
  801. MODULE_DESCRIPTION(DRIVER_DESC);