pnv_php.c 23 KB

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