pnv_php.c 23 KB

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