pnv_php.c 23 KB

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