pci_bus.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2020
  4. *
  5. * Author(s):
  6. * Pierre Morel <[email protected]>
  7. *
  8. */
  9. #define KMSG_COMPONENT "zpci"
  10. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/err.h>
  14. #include <linux/export.h>
  15. #include <linux/delay.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/jump_label.h>
  18. #include <linux/pci.h>
  19. #include <linux/printk.h>
  20. #include <asm/pci_clp.h>
  21. #include <asm/pci_dma.h>
  22. #include "pci_bus.h"
  23. #include "pci_iov.h"
  24. static LIST_HEAD(zbus_list);
  25. static DEFINE_MUTEX(zbus_list_lock);
  26. static int zpci_nb_devices;
  27. /* zpci_bus_prepare_device - Prepare a zPCI function for scanning
  28. * @zdev: the zPCI function to be prepared
  29. *
  30. * The PCI resources for the function are set up and added to its zbus and the
  31. * function is enabled. The function must be added to a zbus which must have
  32. * a PCI bus created. If an error occurs the zPCI function is not enabled.
  33. *
  34. * Return: 0 on success, an error code otherwise
  35. */
  36. static int zpci_bus_prepare_device(struct zpci_dev *zdev)
  37. {
  38. int rc, i;
  39. if (!zdev_enabled(zdev)) {
  40. rc = zpci_enable_device(zdev);
  41. if (rc)
  42. return rc;
  43. rc = zpci_dma_init_device(zdev);
  44. if (rc) {
  45. zpci_disable_device(zdev);
  46. return rc;
  47. }
  48. }
  49. if (!zdev->has_resources) {
  50. zpci_setup_bus_resources(zdev);
  51. for (i = 0; i < PCI_STD_NUM_BARS; i++) {
  52. if (zdev->bars[i].res)
  53. pci_bus_add_resource(zdev->zbus->bus, zdev->bars[i].res, 0);
  54. }
  55. }
  56. return 0;
  57. }
  58. /* zpci_bus_scan_device - Scan a single device adding it to the PCI core
  59. * @zdev: the zdev to be scanned
  60. *
  61. * Scans the PCI function making it available to the common PCI code.
  62. *
  63. * Return: 0 on success, an error value otherwise
  64. */
  65. int zpci_bus_scan_device(struct zpci_dev *zdev)
  66. {
  67. struct pci_dev *pdev;
  68. int rc;
  69. rc = zpci_bus_prepare_device(zdev);
  70. if (rc)
  71. return rc;
  72. pdev = pci_scan_single_device(zdev->zbus->bus, zdev->devfn);
  73. if (!pdev)
  74. return -ENODEV;
  75. pci_bus_add_device(pdev);
  76. pci_lock_rescan_remove();
  77. pci_bus_add_devices(zdev->zbus->bus);
  78. pci_unlock_rescan_remove();
  79. return 0;
  80. }
  81. /* zpci_bus_remove_device - Removes the given zdev from the PCI core
  82. * @zdev: the zdev to be removed from the PCI core
  83. * @set_error: if true the device's error state is set to permanent failure
  84. *
  85. * Sets a zPCI device to a configured but offline state; the zPCI
  86. * device is still accessible through its hotplug slot and the zPCI
  87. * API but is removed from the common code PCI bus, making it
  88. * no longer available to drivers.
  89. */
  90. void zpci_bus_remove_device(struct zpci_dev *zdev, bool set_error)
  91. {
  92. struct zpci_bus *zbus = zdev->zbus;
  93. struct pci_dev *pdev;
  94. if (!zdev->zbus->bus)
  95. return;
  96. pdev = pci_get_slot(zbus->bus, zdev->devfn);
  97. if (pdev) {
  98. if (set_error)
  99. pdev->error_state = pci_channel_io_perm_failure;
  100. if (pdev->is_virtfn) {
  101. zpci_iov_remove_virtfn(pdev, zdev->vfn);
  102. /* balance pci_get_slot */
  103. pci_dev_put(pdev);
  104. return;
  105. }
  106. pci_stop_and_remove_bus_device_locked(pdev);
  107. /* balance pci_get_slot */
  108. pci_dev_put(pdev);
  109. }
  110. }
  111. /* zpci_bus_scan_bus - Scan all configured zPCI functions on the bus
  112. * @zbus: the zbus to be scanned
  113. *
  114. * Enables and scans all PCI functions on the bus making them available to the
  115. * common PCI code. If there is no function 0 on the zbus nothing is scanned. If
  116. * a function does not have a slot yet because it was added to the zbus before
  117. * function 0 the slot is created. If a PCI function fails to be initialized
  118. * an error will be returned but attempts will still be made for all other
  119. * functions on the bus.
  120. *
  121. * Return: 0 on success, an error value otherwise
  122. */
  123. int zpci_bus_scan_bus(struct zpci_bus *zbus)
  124. {
  125. struct zpci_dev *zdev;
  126. int devfn, rc, ret = 0;
  127. for (devfn = 0; devfn < ZPCI_FUNCTIONS_PER_BUS; devfn++) {
  128. zdev = zbus->function[devfn];
  129. if (zdev && zdev->state == ZPCI_FN_STATE_CONFIGURED) {
  130. rc = zpci_bus_prepare_device(zdev);
  131. if (rc)
  132. ret = -EIO;
  133. }
  134. }
  135. pci_lock_rescan_remove();
  136. pci_scan_child_bus(zbus->bus);
  137. pci_bus_add_devices(zbus->bus);
  138. pci_unlock_rescan_remove();
  139. return ret;
  140. }
  141. /* zpci_bus_scan_busses - Scan all registered busses
  142. *
  143. * Scan all available zbusses
  144. *
  145. */
  146. void zpci_bus_scan_busses(void)
  147. {
  148. struct zpci_bus *zbus = NULL;
  149. mutex_lock(&zbus_list_lock);
  150. list_for_each_entry(zbus, &zbus_list, bus_next) {
  151. zpci_bus_scan_bus(zbus);
  152. cond_resched();
  153. }
  154. mutex_unlock(&zbus_list_lock);
  155. }
  156. /* zpci_bus_create_pci_bus - Create the PCI bus associated with this zbus
  157. * @zbus: the zbus holding the zdevices
  158. * @fr: PCI root function that will determine the bus's domain, and bus speeed
  159. * @ops: the pci operations
  160. *
  161. * The PCI function @fr determines the domain (its UID), multifunction property
  162. * and maximum bus speed of the entire bus.
  163. *
  164. * Return: 0 on success, an error code otherwise
  165. */
  166. static int zpci_bus_create_pci_bus(struct zpci_bus *zbus, struct zpci_dev *fr, struct pci_ops *ops)
  167. {
  168. struct pci_bus *bus;
  169. int domain;
  170. domain = zpci_alloc_domain((u16)fr->uid);
  171. if (domain < 0)
  172. return domain;
  173. zbus->domain_nr = domain;
  174. zbus->multifunction = fr->rid_available;
  175. zbus->max_bus_speed = fr->max_bus_speed;
  176. /*
  177. * Note that the zbus->resources are taken over and zbus->resources
  178. * is empty after a successful call
  179. */
  180. bus = pci_create_root_bus(NULL, ZPCI_BUS_NR, ops, zbus, &zbus->resources);
  181. if (!bus) {
  182. zpci_free_domain(zbus->domain_nr);
  183. return -EFAULT;
  184. }
  185. zbus->bus = bus;
  186. pci_bus_add_devices(bus);
  187. return 0;
  188. }
  189. static void zpci_bus_release(struct kref *kref)
  190. {
  191. struct zpci_bus *zbus = container_of(kref, struct zpci_bus, kref);
  192. if (zbus->bus) {
  193. pci_lock_rescan_remove();
  194. pci_stop_root_bus(zbus->bus);
  195. zpci_free_domain(zbus->domain_nr);
  196. pci_free_resource_list(&zbus->resources);
  197. pci_remove_root_bus(zbus->bus);
  198. pci_unlock_rescan_remove();
  199. }
  200. mutex_lock(&zbus_list_lock);
  201. list_del(&zbus->bus_next);
  202. mutex_unlock(&zbus_list_lock);
  203. kfree(zbus);
  204. }
  205. static void zpci_bus_put(struct zpci_bus *zbus)
  206. {
  207. kref_put(&zbus->kref, zpci_bus_release);
  208. }
  209. static struct zpci_bus *zpci_bus_get(int pchid)
  210. {
  211. struct zpci_bus *zbus;
  212. mutex_lock(&zbus_list_lock);
  213. list_for_each_entry(zbus, &zbus_list, bus_next) {
  214. if (pchid == zbus->pchid) {
  215. kref_get(&zbus->kref);
  216. goto out_unlock;
  217. }
  218. }
  219. zbus = NULL;
  220. out_unlock:
  221. mutex_unlock(&zbus_list_lock);
  222. return zbus;
  223. }
  224. static struct zpci_bus *zpci_bus_alloc(int pchid)
  225. {
  226. struct zpci_bus *zbus;
  227. zbus = kzalloc(sizeof(*zbus), GFP_KERNEL);
  228. if (!zbus)
  229. return NULL;
  230. zbus->pchid = pchid;
  231. INIT_LIST_HEAD(&zbus->bus_next);
  232. mutex_lock(&zbus_list_lock);
  233. list_add_tail(&zbus->bus_next, &zbus_list);
  234. mutex_unlock(&zbus_list_lock);
  235. kref_init(&zbus->kref);
  236. INIT_LIST_HEAD(&zbus->resources);
  237. zbus->bus_resource.start = 0;
  238. zbus->bus_resource.end = ZPCI_BUS_NR;
  239. zbus->bus_resource.flags = IORESOURCE_BUS;
  240. pci_add_resource(&zbus->resources, &zbus->bus_resource);
  241. return zbus;
  242. }
  243. void pcibios_bus_add_device(struct pci_dev *pdev)
  244. {
  245. struct zpci_dev *zdev = to_zpci(pdev);
  246. /*
  247. * With pdev->no_vf_scan the common PCI probing code does not
  248. * perform PF/VF linking.
  249. */
  250. if (zdev->vfn) {
  251. zpci_iov_setup_virtfn(zdev->zbus, pdev, zdev->vfn);
  252. pdev->no_command_memory = 1;
  253. }
  254. }
  255. static int zpci_bus_add_device(struct zpci_bus *zbus, struct zpci_dev *zdev)
  256. {
  257. int rc = -EINVAL;
  258. if (zbus->function[zdev->devfn]) {
  259. pr_err("devfn %04x is already assigned\n", zdev->devfn);
  260. return rc;
  261. }
  262. zdev->zbus = zbus;
  263. zbus->function[zdev->devfn] = zdev;
  264. zpci_nb_devices++;
  265. if (zbus->multifunction && !zdev->rid_available) {
  266. WARN_ONCE(1, "rid_available not set for multifunction\n");
  267. goto error;
  268. }
  269. rc = zpci_init_slot(zdev);
  270. if (rc)
  271. goto error;
  272. zdev->has_hp_slot = 1;
  273. return 0;
  274. error:
  275. zbus->function[zdev->devfn] = NULL;
  276. zdev->zbus = NULL;
  277. zpci_nb_devices--;
  278. return rc;
  279. }
  280. int zpci_bus_device_register(struct zpci_dev *zdev, struct pci_ops *ops)
  281. {
  282. struct zpci_bus *zbus = NULL;
  283. int rc = -EBADF;
  284. if (zpci_nb_devices == ZPCI_NR_DEVICES) {
  285. pr_warn("Adding PCI function %08x failed because the configured limit of %d is reached\n",
  286. zdev->fid, ZPCI_NR_DEVICES);
  287. return -ENOSPC;
  288. }
  289. if (zdev->devfn >= ZPCI_FUNCTIONS_PER_BUS)
  290. return -EINVAL;
  291. if (!s390_pci_no_rid && zdev->rid_available)
  292. zbus = zpci_bus_get(zdev->pchid);
  293. if (!zbus) {
  294. zbus = zpci_bus_alloc(zdev->pchid);
  295. if (!zbus)
  296. return -ENOMEM;
  297. }
  298. if (!zbus->bus) {
  299. /* The UID of the first PCI function registered with a zpci_bus
  300. * is used as the domain number for that bus. Currently there
  301. * is exactly one zpci_bus per domain.
  302. */
  303. rc = zpci_bus_create_pci_bus(zbus, zdev, ops);
  304. if (rc)
  305. goto error;
  306. }
  307. rc = zpci_bus_add_device(zbus, zdev);
  308. if (rc)
  309. goto error;
  310. return 0;
  311. error:
  312. pr_err("Adding PCI function %08x failed\n", zdev->fid);
  313. zpci_bus_put(zbus);
  314. return rc;
  315. }
  316. void zpci_bus_device_unregister(struct zpci_dev *zdev)
  317. {
  318. struct zpci_bus *zbus = zdev->zbus;
  319. zpci_nb_devices--;
  320. zbus->function[zdev->devfn] = NULL;
  321. zpci_bus_put(zbus);
  322. }