pnv_php.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. /*
  2. * PCI Hotplug Driver for PowerPC PowerNV platform.
  3. *
  4. * Copyright Gavin Shan, IBM Corporation 2016.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/libfdt.h>
  12. #include <linux/module.h>
  13. #include <linux/pci.h>
  14. #include <linux/pci_hotplug.h>
  15. #include <asm/opal.h>
  16. #include <asm/pnv-pci.h>
  17. #include <asm/ppc-pci.h>
  18. #define DRIVER_VERSION "0.1"
  19. #define DRIVER_AUTHOR "Gavin Shan, IBM Corporation"
  20. #define DRIVER_DESC "PowerPC PowerNV PCI Hotplug Driver"
  21. struct pnv_php_event {
  22. bool added;
  23. struct pnv_php_slot *php_slot;
  24. struct work_struct work;
  25. };
  26. static LIST_HEAD(pnv_php_slot_list);
  27. static DEFINE_SPINLOCK(pnv_php_lock);
  28. static void pnv_php_register(struct device_node *dn);
  29. static void pnv_php_unregister_one(struct device_node *dn);
  30. static void pnv_php_unregister(struct device_node *dn);
  31. static void pnv_php_disable_irq(struct pnv_php_slot *php_slot)
  32. {
  33. struct pci_dev *pdev = php_slot->pdev;
  34. u16 ctrl;
  35. if (php_slot->irq > 0) {
  36. pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &ctrl);
  37. ctrl &= ~(PCI_EXP_SLTCTL_HPIE |
  38. PCI_EXP_SLTCTL_PDCE |
  39. PCI_EXP_SLTCTL_DLLSCE);
  40. pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, ctrl);
  41. free_irq(php_slot->irq, php_slot);
  42. php_slot->irq = 0;
  43. }
  44. if (php_slot->wq) {
  45. destroy_workqueue(php_slot->wq);
  46. php_slot->wq = NULL;
  47. }
  48. if (pdev->msix_enabled)
  49. pci_disable_msix(pdev);
  50. else if (pdev->msi_enabled)
  51. pci_disable_msi(pdev);
  52. }
  53. static void pnv_php_free_slot(struct kref *kref)
  54. {
  55. struct pnv_php_slot *php_slot = container_of(kref,
  56. struct pnv_php_slot, kref);
  57. WARN_ON(!list_empty(&php_slot->children));
  58. pnv_php_disable_irq(php_slot);
  59. kfree(php_slot->name);
  60. kfree(php_slot);
  61. }
  62. static inline void pnv_php_put_slot(struct pnv_php_slot *php_slot)
  63. {
  64. if (WARN_ON(!php_slot))
  65. return;
  66. kref_put(&php_slot->kref, pnv_php_free_slot);
  67. }
  68. static struct pnv_php_slot *pnv_php_match(struct device_node *dn,
  69. struct pnv_php_slot *php_slot)
  70. {
  71. struct pnv_php_slot *target, *tmp;
  72. if (php_slot->dn == dn) {
  73. kref_get(&php_slot->kref);
  74. return php_slot;
  75. }
  76. list_for_each_entry(tmp, &php_slot->children, link) {
  77. target = pnv_php_match(dn, tmp);
  78. if (target)
  79. return target;
  80. }
  81. return NULL;
  82. }
  83. struct pnv_php_slot *pnv_php_find_slot(struct device_node *dn)
  84. {
  85. struct pnv_php_slot *php_slot, *tmp;
  86. unsigned long flags;
  87. spin_lock_irqsave(&pnv_php_lock, flags);
  88. list_for_each_entry(tmp, &pnv_php_slot_list, link) {
  89. php_slot = pnv_php_match(dn, tmp);
  90. if (php_slot) {
  91. spin_unlock_irqrestore(&pnv_php_lock, flags);
  92. return php_slot;
  93. }
  94. }
  95. spin_unlock_irqrestore(&pnv_php_lock, flags);
  96. return NULL;
  97. }
  98. EXPORT_SYMBOL_GPL(pnv_php_find_slot);
  99. /*
  100. * Remove pdn for all children of the indicated device node.
  101. * The function should remove pdn in a depth-first manner.
  102. */
  103. static void pnv_php_rmv_pdns(struct device_node *dn)
  104. {
  105. struct device_node *child;
  106. for_each_child_of_node(dn, child) {
  107. pnv_php_rmv_pdns(child);
  108. pci_remove_device_node_info(child);
  109. }
  110. }
  111. /*
  112. * Detach all child nodes of the indicated device nodes. The
  113. * function should handle device nodes in depth-first manner.
  114. *
  115. * We should not invoke of_node_release() as the memory for
  116. * individual device node is part of large memory block. The
  117. * large block is allocated from memblock (system bootup) or
  118. * kmalloc() when unflattening the device tree by OF changeset.
  119. * We can not free the large block allocated from memblock. For
  120. * later case, it should be released at once.
  121. */
  122. static void pnv_php_detach_device_nodes(struct device_node *parent)
  123. {
  124. struct device_node *dn;
  125. int refcount;
  126. for_each_child_of_node(parent, dn) {
  127. pnv_php_detach_device_nodes(dn);
  128. of_node_put(dn);
  129. refcount = kref_read(&dn->kobj.kref);
  130. if (refcount != 1)
  131. pr_warn("Invalid refcount %d on <%s>\n",
  132. refcount, of_node_full_name(dn));
  133. of_detach_node(dn);
  134. }
  135. }
  136. static void pnv_php_rmv_devtree(struct pnv_php_slot *php_slot)
  137. {
  138. pnv_php_rmv_pdns(php_slot->dn);
  139. /*
  140. * Decrease the refcount if the device nodes were created
  141. * through OF changeset before detaching them.
  142. */
  143. if (php_slot->fdt)
  144. of_changeset_destroy(&php_slot->ocs);
  145. pnv_php_detach_device_nodes(php_slot->dn);
  146. if (php_slot->fdt) {
  147. kfree(php_slot->dt);
  148. kfree(php_slot->fdt);
  149. php_slot->dt = NULL;
  150. php_slot->dn->child = NULL;
  151. php_slot->fdt = NULL;
  152. }
  153. }
  154. /*
  155. * As the nodes in OF changeset are applied in reverse order, we
  156. * need revert the nodes in advance so that we have correct node
  157. * order after the changeset is applied.
  158. */
  159. static void pnv_php_reverse_nodes(struct device_node *parent)
  160. {
  161. struct device_node *child, *next;
  162. /* In-depth first */
  163. for_each_child_of_node(parent, child)
  164. pnv_php_reverse_nodes(child);
  165. /* Reverse the nodes in the child list */
  166. child = parent->child;
  167. parent->child = NULL;
  168. while (child) {
  169. next = child->sibling;
  170. child->sibling = parent->child;
  171. parent->child = child;
  172. child = next;
  173. }
  174. }
  175. static int pnv_php_populate_changeset(struct of_changeset *ocs,
  176. struct device_node *dn)
  177. {
  178. struct device_node *child;
  179. int ret = 0;
  180. for_each_child_of_node(dn, child) {
  181. ret = of_changeset_attach_node(ocs, child);
  182. if (ret)
  183. break;
  184. ret = pnv_php_populate_changeset(ocs, child);
  185. if (ret)
  186. break;
  187. }
  188. return ret;
  189. }
  190. static void *pnv_php_add_one_pdn(struct device_node *dn, void *data)
  191. {
  192. struct pci_controller *hose = (struct pci_controller *)data;
  193. struct pci_dn *pdn;
  194. pdn = pci_add_device_node_info(hose, dn);
  195. if (!pdn)
  196. return ERR_PTR(-ENOMEM);
  197. return NULL;
  198. }
  199. static void pnv_php_add_pdns(struct pnv_php_slot *slot)
  200. {
  201. struct pci_controller *hose = pci_bus_to_host(slot->bus);
  202. pci_traverse_device_nodes(slot->dn, pnv_php_add_one_pdn, hose);
  203. }
  204. static int pnv_php_add_devtree(struct pnv_php_slot *php_slot)
  205. {
  206. void *fdt, *fdt1, *dt;
  207. int ret;
  208. /* We don't know the FDT blob size. We try to get it through
  209. * maximal memory chunk and then copy it to another chunk that
  210. * fits the real size.
  211. */
  212. fdt1 = kzalloc(0x10000, GFP_KERNEL);
  213. if (!fdt1) {
  214. ret = -ENOMEM;
  215. dev_warn(&php_slot->pdev->dev, "Cannot alloc FDT blob\n");
  216. goto out;
  217. }
  218. ret = pnv_pci_get_device_tree(php_slot->dn->phandle, fdt1, 0x10000);
  219. if (ret) {
  220. dev_warn(&php_slot->pdev->dev, "Error %d getting FDT blob\n",
  221. ret);
  222. goto free_fdt1;
  223. }
  224. fdt = kzalloc(fdt_totalsize(fdt1), GFP_KERNEL);
  225. if (!fdt) {
  226. ret = -ENOMEM;
  227. dev_warn(&php_slot->pdev->dev, "Cannot %d bytes memory\n",
  228. fdt_totalsize(fdt1));
  229. goto free_fdt1;
  230. }
  231. /* Unflatten device tree blob */
  232. memcpy(fdt, fdt1, fdt_totalsize(fdt1));
  233. dt = of_fdt_unflatten_tree(fdt, php_slot->dn, NULL);
  234. if (!dt) {
  235. ret = -EINVAL;
  236. dev_warn(&php_slot->pdev->dev, "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. dev_warn(&php_slot->pdev->dev, "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. dev_warn(&php_slot->pdev->dev, "Error %d applying changeset\n",
  253. 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. dev_warn(&php_slot->pdev->dev, "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. dev_warn(&php_slot->pdev->dev, "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. dev_warn(&php_slot->pdev->dev, "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. dev_warn(&php_slot->pdev->dev, "Error %d getting presence\n",
  339. ret);
  340. }
  341. return ret;
  342. }
  343. static int pnv_php_set_attention_state(struct hotplug_slot *slot, u8 state)
  344. {
  345. /* FIXME: Make it real once firmware supports it */
  346. slot->info->attention_status = state;
  347. return 0;
  348. }
  349. static int pnv_php_enable(struct pnv_php_slot *php_slot, bool rescan)
  350. {
  351. struct hotplug_slot *slot = &php_slot->slot;
  352. uint8_t presence = OPAL_PCI_SLOT_EMPTY;
  353. uint8_t power_status = OPAL_PCI_SLOT_POWER_ON;
  354. int ret;
  355. /* Check if the slot has been configured */
  356. if (php_slot->state != PNV_PHP_STATE_REGISTERED)
  357. return 0;
  358. /* Retrieve slot presence status */
  359. ret = pnv_php_get_adapter_state(slot, &presence);
  360. if (ret)
  361. return ret;
  362. /* Proceed if there have nothing behind the slot */
  363. if (presence == OPAL_PCI_SLOT_EMPTY)
  364. goto scan;
  365. /*
  366. * If the power supply to the slot is off, we can't detect
  367. * adapter presence state. That means we have to turn the
  368. * slot on before going to probe slot's presence state.
  369. *
  370. * On the first time, we don't change the power status to
  371. * boost system boot with assumption that the firmware
  372. * supplies consistent slot power status: empty slot always
  373. * has its power off and non-empty slot has its power on.
  374. */
  375. if (!php_slot->power_state_check) {
  376. php_slot->power_state_check = true;
  377. ret = pnv_php_get_power_state(slot, &power_status);
  378. if (ret)
  379. return ret;
  380. if (power_status != OPAL_PCI_SLOT_POWER_ON)
  381. return 0;
  382. }
  383. /* Check the power status. Scan the slot if it is already on */
  384. ret = pnv_php_get_power_state(slot, &power_status);
  385. if (ret)
  386. return ret;
  387. if (power_status == OPAL_PCI_SLOT_POWER_ON)
  388. goto scan;
  389. /* Power is off, turn it on and then scan the slot */
  390. ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_ON);
  391. if (ret)
  392. return ret;
  393. scan:
  394. if (presence == OPAL_PCI_SLOT_PRESENT) {
  395. if (rescan) {
  396. pci_lock_rescan_remove();
  397. pci_hp_add_devices(php_slot->bus);
  398. pci_unlock_rescan_remove();
  399. }
  400. /* Rescan for child hotpluggable slots */
  401. php_slot->state = PNV_PHP_STATE_POPULATED;
  402. if (rescan)
  403. pnv_php_register(php_slot->dn);
  404. } else {
  405. php_slot->state = PNV_PHP_STATE_POPULATED;
  406. }
  407. return 0;
  408. }
  409. static int pnv_php_enable_slot(struct hotplug_slot *slot)
  410. {
  411. struct pnv_php_slot *php_slot = container_of(slot,
  412. struct pnv_php_slot, slot);
  413. return pnv_php_enable(php_slot, true);
  414. }
  415. static int pnv_php_disable_slot(struct hotplug_slot *slot)
  416. {
  417. struct pnv_php_slot *php_slot = slot->private;
  418. int ret;
  419. if (php_slot->state != PNV_PHP_STATE_POPULATED)
  420. return 0;
  421. /* Remove all devices behind the slot */
  422. pci_lock_rescan_remove();
  423. pci_hp_remove_devices(php_slot->bus);
  424. pci_unlock_rescan_remove();
  425. /* Detach the child hotpluggable slots */
  426. pnv_php_unregister(php_slot->dn);
  427. /* Notify firmware and remove device nodes */
  428. ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_OFF);
  429. php_slot->state = PNV_PHP_STATE_REGISTERED;
  430. return ret;
  431. }
  432. static struct hotplug_slot_ops php_slot_ops = {
  433. .get_power_status = pnv_php_get_power_state,
  434. .get_adapter_status = pnv_php_get_adapter_state,
  435. .set_attention_status = pnv_php_set_attention_state,
  436. .enable_slot = pnv_php_enable_slot,
  437. .disable_slot = pnv_php_disable_slot,
  438. };
  439. static void pnv_php_release(struct hotplug_slot *slot)
  440. {
  441. struct pnv_php_slot *php_slot = slot->private;
  442. unsigned long flags;
  443. /* Remove from global or child list */
  444. spin_lock_irqsave(&pnv_php_lock, flags);
  445. list_del(&php_slot->link);
  446. spin_unlock_irqrestore(&pnv_php_lock, flags);
  447. /* Detach from parent */
  448. pnv_php_put_slot(php_slot);
  449. pnv_php_put_slot(php_slot->parent);
  450. }
  451. static struct pnv_php_slot *pnv_php_alloc_slot(struct device_node *dn)
  452. {
  453. struct pnv_php_slot *php_slot;
  454. struct pci_bus *bus;
  455. const char *label;
  456. uint64_t id;
  457. int ret;
  458. ret = of_property_read_string(dn, "ibm,slot-label", &label);
  459. if (ret)
  460. return NULL;
  461. if (pnv_pci_get_slot_id(dn, &id))
  462. return NULL;
  463. bus = pci_find_bus_by_node(dn);
  464. if (!bus)
  465. return NULL;
  466. php_slot = kzalloc(sizeof(*php_slot), GFP_KERNEL);
  467. if (!php_slot)
  468. return NULL;
  469. php_slot->name = kstrdup(label, GFP_KERNEL);
  470. if (!php_slot->name) {
  471. kfree(php_slot);
  472. return NULL;
  473. }
  474. if (dn->child && PCI_DN(dn->child))
  475. php_slot->slot_no = PCI_SLOT(PCI_DN(dn->child)->devfn);
  476. else
  477. php_slot->slot_no = -1; /* Placeholder slot */
  478. kref_init(&php_slot->kref);
  479. php_slot->state = PNV_PHP_STATE_INITIALIZED;
  480. php_slot->dn = dn;
  481. php_slot->pdev = bus->self;
  482. php_slot->bus = bus;
  483. php_slot->id = id;
  484. php_slot->power_state_check = false;
  485. php_slot->slot.ops = &php_slot_ops;
  486. php_slot->slot.info = &php_slot->slot_info;
  487. php_slot->slot.release = pnv_php_release;
  488. php_slot->slot.private = php_slot;
  489. INIT_LIST_HEAD(&php_slot->children);
  490. INIT_LIST_HEAD(&php_slot->link);
  491. return php_slot;
  492. }
  493. static int pnv_php_register_slot(struct pnv_php_slot *php_slot)
  494. {
  495. struct pnv_php_slot *parent;
  496. struct device_node *dn = php_slot->dn;
  497. unsigned long flags;
  498. int ret;
  499. /* Check if the slot is registered or not */
  500. parent = pnv_php_find_slot(php_slot->dn);
  501. if (parent) {
  502. pnv_php_put_slot(parent);
  503. return -EEXIST;
  504. }
  505. /* Register PCI slot */
  506. ret = pci_hp_register(&php_slot->slot, php_slot->bus,
  507. php_slot->slot_no, php_slot->name);
  508. if (ret) {
  509. dev_warn(&php_slot->pdev->dev, "Error %d registering slot\n",
  510. ret);
  511. return ret;
  512. }
  513. /* Attach to the parent's child list or global list */
  514. while ((dn = of_get_parent(dn))) {
  515. if (!PCI_DN(dn)) {
  516. of_node_put(dn);
  517. break;
  518. }
  519. parent = pnv_php_find_slot(dn);
  520. if (parent) {
  521. of_node_put(dn);
  522. break;
  523. }
  524. of_node_put(dn);
  525. }
  526. spin_lock_irqsave(&pnv_php_lock, flags);
  527. php_slot->parent = parent;
  528. if (parent)
  529. list_add_tail(&php_slot->link, &parent->children);
  530. else
  531. list_add_tail(&php_slot->link, &pnv_php_slot_list);
  532. spin_unlock_irqrestore(&pnv_php_lock, flags);
  533. php_slot->state = PNV_PHP_STATE_REGISTERED;
  534. return 0;
  535. }
  536. static int pnv_php_enable_msix(struct pnv_php_slot *php_slot)
  537. {
  538. struct pci_dev *pdev = php_slot->pdev;
  539. struct msix_entry entry;
  540. int nr_entries, ret;
  541. u16 pcie_flag;
  542. /* Get total number of MSIx entries */
  543. nr_entries = pci_msix_vec_count(pdev);
  544. if (nr_entries < 0)
  545. return nr_entries;
  546. /* Check hotplug MSIx entry is in range */
  547. pcie_capability_read_word(pdev, PCI_EXP_FLAGS, &pcie_flag);
  548. entry.entry = (pcie_flag & PCI_EXP_FLAGS_IRQ) >> 9;
  549. if (entry.entry >= nr_entries)
  550. return -ERANGE;
  551. /* Enable MSIx */
  552. ret = pci_enable_msix_exact(pdev, &entry, 1);
  553. if (ret) {
  554. dev_warn(&pdev->dev, "Error %d enabling MSIx\n", ret);
  555. return ret;
  556. }
  557. return entry.vector;
  558. }
  559. static void pnv_php_event_handler(struct work_struct *work)
  560. {
  561. struct pnv_php_event *event =
  562. container_of(work, struct pnv_php_event, work);
  563. struct pnv_php_slot *php_slot = event->php_slot;
  564. if (event->added)
  565. pnv_php_enable_slot(&php_slot->slot);
  566. else
  567. pnv_php_disable_slot(&php_slot->slot);
  568. kfree(event);
  569. }
  570. static irqreturn_t pnv_php_interrupt(int irq, void *data)
  571. {
  572. struct pnv_php_slot *php_slot = data;
  573. struct pci_dev *pchild, *pdev = php_slot->pdev;
  574. struct eeh_dev *edev;
  575. struct eeh_pe *pe;
  576. struct pnv_php_event *event;
  577. u16 sts, lsts;
  578. u8 presence;
  579. bool added;
  580. unsigned long flags;
  581. int ret;
  582. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &sts);
  583. sts &= (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC);
  584. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, sts);
  585. if (sts & PCI_EXP_SLTSTA_DLLSC) {
  586. pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lsts);
  587. added = !!(lsts & PCI_EXP_LNKSTA_DLLLA);
  588. } else if (sts & PCI_EXP_SLTSTA_PDC) {
  589. ret = pnv_pci_get_presence_state(php_slot->id, &presence);
  590. if (!ret)
  591. return IRQ_HANDLED;
  592. added = !!(presence == OPAL_PCI_SLOT_PRESENT);
  593. } else {
  594. return IRQ_NONE;
  595. }
  596. /* Freeze the removed PE to avoid unexpected error reporting */
  597. if (!added) {
  598. pchild = list_first_entry_or_null(&php_slot->bus->devices,
  599. struct pci_dev, bus_list);
  600. edev = pchild ? pci_dev_to_eeh_dev(pchild) : NULL;
  601. pe = edev ? edev->pe : NULL;
  602. if (pe) {
  603. eeh_serialize_lock(&flags);
  604. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  605. eeh_serialize_unlock(flags);
  606. eeh_pe_set_option(pe, EEH_OPT_FREEZE_PE);
  607. }
  608. }
  609. /*
  610. * The PE is left in frozen state if the event is missed. It's
  611. * fine as the PCI devices (PE) aren't functional any more.
  612. */
  613. event = kzalloc(sizeof(*event), GFP_ATOMIC);
  614. if (!event) {
  615. dev_warn(&pdev->dev, "PCI slot [%s] missed hotplug event 0x%04x\n",
  616. php_slot->name, sts);
  617. return IRQ_HANDLED;
  618. }
  619. dev_info(&pdev->dev, "PCI slot [%s] %s (IRQ: %d)\n",
  620. php_slot->name, added ? "added" : "removed", irq);
  621. INIT_WORK(&event->work, pnv_php_event_handler);
  622. event->added = added;
  623. event->php_slot = php_slot;
  624. queue_work(php_slot->wq, &event->work);
  625. return IRQ_HANDLED;
  626. }
  627. static void pnv_php_init_irq(struct pnv_php_slot *php_slot, int irq)
  628. {
  629. struct pci_dev *pdev = php_slot->pdev;
  630. u16 sts, ctrl;
  631. int ret;
  632. /* Allocate workqueue */
  633. php_slot->wq = alloc_workqueue("pciehp-%s", 0, 0, php_slot->name);
  634. if (!php_slot->wq) {
  635. dev_warn(&pdev->dev, "Cannot alloc workqueue\n");
  636. pnv_php_disable_irq(php_slot);
  637. return;
  638. }
  639. /* Clear pending interrupts */
  640. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &sts);
  641. sts |= (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC);
  642. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, sts);
  643. /* Request the interrupt */
  644. ret = request_irq(irq, pnv_php_interrupt, IRQF_SHARED,
  645. php_slot->name, php_slot);
  646. if (ret) {
  647. pnv_php_disable_irq(php_slot);
  648. dev_warn(&pdev->dev, "Error %d enabling IRQ %d\n", ret, irq);
  649. return;
  650. }
  651. /* Enable the interrupts */
  652. pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &ctrl);
  653. ctrl |= (PCI_EXP_SLTCTL_HPIE |
  654. PCI_EXP_SLTCTL_PDCE |
  655. PCI_EXP_SLTCTL_DLLSCE);
  656. pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, ctrl);
  657. /* The interrupt is initialized successfully when @irq is valid */
  658. php_slot->irq = irq;
  659. }
  660. static void pnv_php_enable_irq(struct pnv_php_slot *php_slot)
  661. {
  662. struct pci_dev *pdev = php_slot->pdev;
  663. int irq, ret;
  664. ret = pci_enable_device(pdev);
  665. if (ret) {
  666. dev_warn(&pdev->dev, "Error %d enabling device\n", ret);
  667. return;
  668. }
  669. pci_set_master(pdev);
  670. /* Enable MSIx interrupt */
  671. irq = pnv_php_enable_msix(php_slot);
  672. if (irq > 0) {
  673. pnv_php_init_irq(php_slot, irq);
  674. return;
  675. }
  676. /*
  677. * Use MSI if MSIx doesn't work. Fail back to legacy INTx
  678. * if MSI doesn't work either
  679. */
  680. ret = pci_enable_msi(pdev);
  681. if (!ret || pdev->irq) {
  682. irq = pdev->irq;
  683. pnv_php_init_irq(php_slot, irq);
  684. }
  685. }
  686. static int pnv_php_register_one(struct device_node *dn)
  687. {
  688. struct pnv_php_slot *php_slot;
  689. u32 prop32;
  690. int ret;
  691. /* Check if it's hotpluggable slot */
  692. ret = of_property_read_u32(dn, "ibm,slot-pluggable", &prop32);
  693. if (ret || !prop32)
  694. return -ENXIO;
  695. ret = of_property_read_u32(dn, "ibm,reset-by-firmware", &prop32);
  696. if (ret || !prop32)
  697. return -ENXIO;
  698. php_slot = pnv_php_alloc_slot(dn);
  699. if (!php_slot)
  700. return -ENODEV;
  701. ret = pnv_php_register_slot(php_slot);
  702. if (ret)
  703. goto free_slot;
  704. ret = pnv_php_enable(php_slot, false);
  705. if (ret)
  706. goto unregister_slot;
  707. /* Enable interrupt if the slot supports surprise hotplug */
  708. ret = of_property_read_u32(dn, "ibm,slot-surprise-pluggable", &prop32);
  709. if (!ret && prop32)
  710. pnv_php_enable_irq(php_slot);
  711. return 0;
  712. unregister_slot:
  713. pnv_php_unregister_one(php_slot->dn);
  714. free_slot:
  715. pnv_php_put_slot(php_slot);
  716. return ret;
  717. }
  718. static void pnv_php_register(struct device_node *dn)
  719. {
  720. struct device_node *child;
  721. /*
  722. * The parent slots should be registered before their
  723. * child slots.
  724. */
  725. for_each_child_of_node(dn, child) {
  726. pnv_php_register_one(child);
  727. pnv_php_register(child);
  728. }
  729. }
  730. static void pnv_php_unregister_one(struct device_node *dn)
  731. {
  732. struct pnv_php_slot *php_slot;
  733. php_slot = pnv_php_find_slot(dn);
  734. if (!php_slot)
  735. return;
  736. php_slot->state = PNV_PHP_STATE_OFFLINE;
  737. pnv_php_put_slot(php_slot);
  738. pci_hp_deregister(&php_slot->slot);
  739. }
  740. static void pnv_php_unregister(struct device_node *dn)
  741. {
  742. struct device_node *child;
  743. /* The child slots should go before their parent slots */
  744. for_each_child_of_node(dn, child) {
  745. pnv_php_unregister(child);
  746. pnv_php_unregister_one(child);
  747. }
  748. }
  749. static int __init pnv_php_init(void)
  750. {
  751. struct device_node *dn;
  752. pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  753. for_each_compatible_node(dn, NULL, "ibm,ioda2-phb")
  754. pnv_php_register(dn);
  755. return 0;
  756. }
  757. static void __exit pnv_php_exit(void)
  758. {
  759. struct device_node *dn;
  760. for_each_compatible_node(dn, NULL, "ibm,ioda2-phb")
  761. pnv_php_unregister(dn);
  762. }
  763. module_init(pnv_php_init);
  764. module_exit(pnv_php_exit);
  765. MODULE_VERSION(DRIVER_VERSION);
  766. MODULE_LICENSE("GPL v2");
  767. MODULE_AUTHOR(DRIVER_AUTHOR);
  768. MODULE_DESCRIPTION(DRIVER_DESC);