pnv_php.c 23 KB

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