xref: /openbmc/linux/arch/s390/pci/pci_bus.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
105bc1be6SPierre Morel // SPDX-License-Identifier: GPL-2.0
205bc1be6SPierre Morel /*
305bc1be6SPierre Morel  * Copyright IBM Corp. 2020
405bc1be6SPierre Morel  *
505bc1be6SPierre Morel  * Author(s):
605bc1be6SPierre Morel  *   Pierre Morel <pmorel@linux.ibm.com>
705bc1be6SPierre Morel  *
805bc1be6SPierre Morel  */
905bc1be6SPierre Morel 
1005bc1be6SPierre Morel #define KMSG_COMPONENT "zpci"
1105bc1be6SPierre Morel #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
1205bc1be6SPierre Morel 
1305bc1be6SPierre Morel #include <linux/kernel.h>
1405bc1be6SPierre Morel #include <linux/slab.h>
1505bc1be6SPierre Morel #include <linux/err.h>
1605bc1be6SPierre Morel #include <linux/export.h>
1705bc1be6SPierre Morel #include <linux/delay.h>
1805bc1be6SPierre Morel #include <linux/seq_file.h>
1905bc1be6SPierre Morel #include <linux/jump_label.h>
2005bc1be6SPierre Morel #include <linux/pci.h>
2105bc1be6SPierre Morel #include <linux/printk.h>
2205bc1be6SPierre Morel 
2305bc1be6SPierre Morel #include <asm/pci_clp.h>
2405bc1be6SPierre Morel #include <asm/pci_dma.h>
2505bc1be6SPierre Morel 
2605bc1be6SPierre Morel #include "pci_bus.h"
27abb95b75SNiklas Schnelle #include "pci_iov.h"
2805bc1be6SPierre Morel 
2905bc1be6SPierre Morel static LIST_HEAD(zbus_list);
3003502761SNiklas Schnelle static DEFINE_MUTEX(zbus_list_lock);
3105bc1be6SPierre Morel static int zpci_nb_devices;
3205bc1be6SPierre Morel 
33a50297cfSNiklas Schnelle /* zpci_bus_prepare_device - Prepare a zPCI function for scanning
34a50297cfSNiklas Schnelle  * @zdev: the zPCI function to be prepared
35a50297cfSNiklas Schnelle  *
36a50297cfSNiklas Schnelle  * The PCI resources for the function are set up and added to its zbus and the
37a50297cfSNiklas Schnelle  * function is enabled. The function must be added to a zbus which must have
38a50297cfSNiklas Schnelle  * a PCI bus created. If an error occurs the zPCI function is not enabled.
39a50297cfSNiklas Schnelle  *
40a50297cfSNiklas Schnelle  * Return: 0 on success, an error code otherwise
41a50297cfSNiklas Schnelle  */
zpci_bus_prepare_device(struct zpci_dev * zdev)42a50297cfSNiklas Schnelle static int zpci_bus_prepare_device(struct zpci_dev *zdev)
43a50297cfSNiklas Schnelle {
44ab909509SNiklas Schnelle 	int rc, i;
45a50297cfSNiklas Schnelle 
46a50297cfSNiklas Schnelle 	if (!zdev_enabled(zdev)) {
47a50297cfSNiklas Schnelle 		rc = zpci_enable_device(zdev);
48a50297cfSNiklas Schnelle 		if (rc)
49a50297cfSNiklas Schnelle 			return rc;
501f3f7681SNiklas Schnelle 		rc = zpci_dma_init_device(zdev);
511f3f7681SNiklas Schnelle 		if (rc) {
521f3f7681SNiklas Schnelle 			zpci_disable_device(zdev);
531f3f7681SNiklas Schnelle 			return rc;
541f3f7681SNiklas Schnelle 		}
55a50297cfSNiklas Schnelle 	}
56a50297cfSNiklas Schnelle 
57a50297cfSNiklas Schnelle 	if (!zdev->has_resources) {
58ab909509SNiklas Schnelle 		zpci_setup_bus_resources(zdev);
59ab909509SNiklas Schnelle 		for (i = 0; i < PCI_STD_NUM_BARS; i++) {
60ab909509SNiklas Schnelle 			if (zdev->bars[i].res)
61ab909509SNiklas Schnelle 				pci_bus_add_resource(zdev->zbus->bus, zdev->bars[i].res, 0);
62a50297cfSNiklas Schnelle 		}
63a50297cfSNiklas Schnelle 	}
64a50297cfSNiklas Schnelle 
65a50297cfSNiklas Schnelle 	return 0;
66a50297cfSNiklas Schnelle }
67a50297cfSNiklas Schnelle 
68faf29a4dSNiklas Schnelle /* zpci_bus_scan_device - Scan a single device adding it to the PCI core
69faf29a4dSNiklas Schnelle  * @zdev: the zdev to be scanned
70faf29a4dSNiklas Schnelle  *
71faf29a4dSNiklas Schnelle  * Scans the PCI function making it available to the common PCI code.
72faf29a4dSNiklas Schnelle  *
73faf29a4dSNiklas Schnelle  * Return: 0 on success, an error value otherwise
74faf29a4dSNiklas Schnelle  */
zpci_bus_scan_device(struct zpci_dev * zdev)75faf29a4dSNiklas Schnelle int zpci_bus_scan_device(struct zpci_dev *zdev)
76faf29a4dSNiklas Schnelle {
77faf29a4dSNiklas Schnelle 	struct pci_dev *pdev;
78a50297cfSNiklas Schnelle 	int rc;
79a50297cfSNiklas Schnelle 
80a50297cfSNiklas Schnelle 	rc = zpci_bus_prepare_device(zdev);
81a50297cfSNiklas Schnelle 	if (rc)
82a50297cfSNiklas Schnelle 		return rc;
83faf29a4dSNiklas Schnelle 
84faf29a4dSNiklas Schnelle 	pdev = pci_scan_single_device(zdev->zbus->bus, zdev->devfn);
85faf29a4dSNiklas Schnelle 	if (!pdev)
86faf29a4dSNiklas Schnelle 		return -ENODEV;
87faf29a4dSNiklas Schnelle 
88faf29a4dSNiklas Schnelle 	pci_lock_rescan_remove();
89ae83707fSNiklas Schnelle 	pci_bus_add_device(pdev);
90faf29a4dSNiklas Schnelle 	pci_unlock_rescan_remove();
91faf29a4dSNiklas Schnelle 
92faf29a4dSNiklas Schnelle 	return 0;
93faf29a4dSNiklas Schnelle }
94faf29a4dSNiklas Schnelle 
9595b3a8b4SNiklas Schnelle /* zpci_bus_remove_device - Removes the given zdev from the PCI core
9695b3a8b4SNiklas Schnelle  * @zdev: the zdev to be removed from the PCI core
9795b3a8b4SNiklas Schnelle  * @set_error: if true the device's error state is set to permanent failure
9895b3a8b4SNiklas Schnelle  *
9995b3a8b4SNiklas Schnelle  * Sets a zPCI device to a configured but offline state; the zPCI
10095b3a8b4SNiklas Schnelle  * device is still accessible through its hotplug slot and the zPCI
10195b3a8b4SNiklas Schnelle  * API but is removed from the common code PCI bus, making it
10295b3a8b4SNiklas Schnelle  * no longer available to drivers.
10395b3a8b4SNiklas Schnelle  */
zpci_bus_remove_device(struct zpci_dev * zdev,bool set_error)10495b3a8b4SNiklas Schnelle void zpci_bus_remove_device(struct zpci_dev *zdev, bool set_error)
10595b3a8b4SNiklas Schnelle {
10695b3a8b4SNiklas Schnelle 	struct zpci_bus *zbus = zdev->zbus;
10795b3a8b4SNiklas Schnelle 	struct pci_dev *pdev;
10895b3a8b4SNiklas Schnelle 
10995b3a8b4SNiklas Schnelle 	if (!zdev->zbus->bus)
11095b3a8b4SNiklas Schnelle 		return;
11195b3a8b4SNiklas Schnelle 
11295b3a8b4SNiklas Schnelle 	pdev = pci_get_slot(zbus->bus, zdev->devfn);
11395b3a8b4SNiklas Schnelle 	if (pdev) {
11495b3a8b4SNiklas Schnelle 		if (set_error)
11595b3a8b4SNiklas Schnelle 			pdev->error_state = pci_channel_io_perm_failure;
11695b3a8b4SNiklas Schnelle 		if (pdev->is_virtfn) {
11795b3a8b4SNiklas Schnelle 			zpci_iov_remove_virtfn(pdev, zdev->vfn);
11895b3a8b4SNiklas Schnelle 			/* balance pci_get_slot */
11995b3a8b4SNiklas Schnelle 			pci_dev_put(pdev);
12095b3a8b4SNiklas Schnelle 			return;
12195b3a8b4SNiklas Schnelle 		}
12295b3a8b4SNiklas Schnelle 		pci_stop_and_remove_bus_device_locked(pdev);
12395b3a8b4SNiklas Schnelle 		/* balance pci_get_slot */
12495b3a8b4SNiklas Schnelle 		pci_dev_put(pdev);
12595b3a8b4SNiklas Schnelle 	}
12695b3a8b4SNiklas Schnelle }
12795b3a8b4SNiklas Schnelle 
128a50297cfSNiklas Schnelle /* zpci_bus_scan_bus - Scan all configured zPCI functions on the bus
129a50297cfSNiklas Schnelle  * @zbus: the zbus to be scanned
130a50297cfSNiklas Schnelle  *
131a50297cfSNiklas Schnelle  * Enables and scans all PCI functions on the bus making them available to the
132*45e5f0c0SNiklas Schnelle  * common PCI code. If a PCI function fails to be initialized an error will be
133*45e5f0c0SNiklas Schnelle  * returned but attempts will still be made for all other functions on the bus.
134a50297cfSNiklas Schnelle  *
135a50297cfSNiklas Schnelle  * Return: 0 on success, an error value otherwise
136a50297cfSNiklas Schnelle  */
zpci_bus_scan_bus(struct zpci_bus * zbus)137a50297cfSNiklas Schnelle int zpci_bus_scan_bus(struct zpci_bus *zbus)
138a50297cfSNiklas Schnelle {
139a50297cfSNiklas Schnelle 	struct zpci_dev *zdev;
140a50297cfSNiklas Schnelle 	int devfn, rc, ret = 0;
141a50297cfSNiklas Schnelle 
142a50297cfSNiklas Schnelle 	for (devfn = 0; devfn < ZPCI_FUNCTIONS_PER_BUS; devfn++) {
143a50297cfSNiklas Schnelle 		zdev = zbus->function[devfn];
144a50297cfSNiklas Schnelle 		if (zdev && zdev->state == ZPCI_FN_STATE_CONFIGURED) {
145a50297cfSNiklas Schnelle 			rc = zpci_bus_prepare_device(zdev);
146a50297cfSNiklas Schnelle 			if (rc)
147a50297cfSNiklas Schnelle 				ret = -EIO;
148a50297cfSNiklas Schnelle 		}
149a50297cfSNiklas Schnelle 	}
150a50297cfSNiklas Schnelle 
151a50297cfSNiklas Schnelle 	pci_lock_rescan_remove();
152a50297cfSNiklas Schnelle 	pci_scan_child_bus(zbus->bus);
153a50297cfSNiklas Schnelle 	pci_bus_add_devices(zbus->bus);
154a50297cfSNiklas Schnelle 	pci_unlock_rescan_remove();
155a50297cfSNiklas Schnelle 
156a50297cfSNiklas Schnelle 	return ret;
157a50297cfSNiklas Schnelle }
158a50297cfSNiklas Schnelle 
15914c87ba8SNiklas Schnelle /* zpci_bus_scan_busses - Scan all registered busses
16014c87ba8SNiklas Schnelle  *
16114c87ba8SNiklas Schnelle  * Scan all available zbusses
16214c87ba8SNiklas Schnelle  *
16314c87ba8SNiklas Schnelle  */
zpci_bus_scan_busses(void)16414c87ba8SNiklas Schnelle void zpci_bus_scan_busses(void)
16514c87ba8SNiklas Schnelle {
16614c87ba8SNiklas Schnelle 	struct zpci_bus *zbus = NULL;
16714c87ba8SNiklas Schnelle 
16814c87ba8SNiklas Schnelle 	mutex_lock(&zbus_list_lock);
16914c87ba8SNiklas Schnelle 	list_for_each_entry(zbus, &zbus_list, bus_next) {
17014c87ba8SNiklas Schnelle 		zpci_bus_scan_bus(zbus);
17114c87ba8SNiklas Schnelle 		cond_resched();
17214c87ba8SNiklas Schnelle 	}
17314c87ba8SNiklas Schnelle 	mutex_unlock(&zbus_list_lock);
17414c87ba8SNiklas Schnelle }
17514c87ba8SNiklas Schnelle 
176a50297cfSNiklas Schnelle /* zpci_bus_create_pci_bus - Create the PCI bus associated with this zbus
17705bc1be6SPierre Morel  * @zbus: the zbus holding the zdevices
178960ac362SNiklas Schnelle  * @fr: PCI root function that will determine the bus's domain, and bus speeed
17905bc1be6SPierre Morel  * @ops: the pci operations
18005bc1be6SPierre Morel  *
181960ac362SNiklas Schnelle  * The PCI function @fr determines the domain (its UID), multifunction property
182960ac362SNiklas Schnelle  * and maximum bus speed of the entire bus.
1837dc697d6SNiklas Schnelle  *
1847dc697d6SNiklas Schnelle  * Return: 0 on success, an error code otherwise
18505bc1be6SPierre Morel  */
zpci_bus_create_pci_bus(struct zpci_bus * zbus,struct zpci_dev * fr,struct pci_ops * ops)186960ac362SNiklas Schnelle static int zpci_bus_create_pci_bus(struct zpci_bus *zbus, struct zpci_dev *fr, struct pci_ops *ops)
18705bc1be6SPierre Morel {
18805bc1be6SPierre Morel 	struct pci_bus *bus;
1897dc697d6SNiklas Schnelle 	int domain;
19005bc1be6SPierre Morel 
191960ac362SNiklas Schnelle 	domain = zpci_alloc_domain((u16)fr->uid);
1927dc697d6SNiklas Schnelle 	if (domain < 0)
1937dc697d6SNiklas Schnelle 		return domain;
19405bc1be6SPierre Morel 
1957dc697d6SNiklas Schnelle 	zbus->domain_nr = domain;
196960ac362SNiklas Schnelle 	zbus->multifunction = fr->rid_available;
197960ac362SNiklas Schnelle 	zbus->max_bus_speed = fr->max_bus_speed;
1987dc697d6SNiklas Schnelle 
1997dc697d6SNiklas Schnelle 	/*
2007dc697d6SNiklas Schnelle 	 * Note that the zbus->resources are taken over and zbus->resources
2017dc697d6SNiklas Schnelle 	 * is empty after a successful call
2027dc697d6SNiklas Schnelle 	 */
203a50297cfSNiklas Schnelle 	bus = pci_create_root_bus(NULL, ZPCI_BUS_NR, ops, zbus, &zbus->resources);
20405bc1be6SPierre Morel 	if (!bus) {
20505bc1be6SPierre Morel 		zpci_free_domain(zbus->domain_nr);
20605bc1be6SPierre Morel 		return -EFAULT;
20705bc1be6SPierre Morel 	}
20805bc1be6SPierre Morel 
20905bc1be6SPierre Morel 	zbus->bus = bus;
210a50297cfSNiklas Schnelle 
21105bc1be6SPierre Morel 	return 0;
21205bc1be6SPierre Morel }
21305bc1be6SPierre Morel 
zpci_bus_release(struct kref * kref)21405bc1be6SPierre Morel static void zpci_bus_release(struct kref *kref)
21505bc1be6SPierre Morel {
21605bc1be6SPierre Morel 	struct zpci_bus *zbus = container_of(kref, struct zpci_bus, kref);
21705bc1be6SPierre Morel 
21844510d6fSPierre Morel 	if (zbus->bus) {
21905bc1be6SPierre Morel 		pci_lock_rescan_remove();
22005bc1be6SPierre Morel 		pci_stop_root_bus(zbus->bus);
22105bc1be6SPierre Morel 
22205bc1be6SPierre Morel 		zpci_free_domain(zbus->domain_nr);
22305bc1be6SPierre Morel 		pci_free_resource_list(&zbus->resources);
22405bc1be6SPierre Morel 
22505bc1be6SPierre Morel 		pci_remove_root_bus(zbus->bus);
22605bc1be6SPierre Morel 		pci_unlock_rescan_remove();
22744510d6fSPierre Morel 	}
22805bc1be6SPierre Morel 
22903502761SNiklas Schnelle 	mutex_lock(&zbus_list_lock);
23005bc1be6SPierre Morel 	list_del(&zbus->bus_next);
23103502761SNiklas Schnelle 	mutex_unlock(&zbus_list_lock);
23205bc1be6SPierre Morel 	kfree(zbus);
23305bc1be6SPierre Morel }
23405bc1be6SPierre Morel 
zpci_bus_put(struct zpci_bus * zbus)23505bc1be6SPierre Morel static void zpci_bus_put(struct zpci_bus *zbus)
23605bc1be6SPierre Morel {
23705bc1be6SPierre Morel 	kref_put(&zbus->kref, zpci_bus_release);
23805bc1be6SPierre Morel }
23905bc1be6SPierre Morel 
zpci_bus_get(int pchid)24044510d6fSPierre Morel static struct zpci_bus *zpci_bus_get(int pchid)
24144510d6fSPierre Morel {
24244510d6fSPierre Morel 	struct zpci_bus *zbus;
24344510d6fSPierre Morel 
24403502761SNiklas Schnelle 	mutex_lock(&zbus_list_lock);
24544510d6fSPierre Morel 	list_for_each_entry(zbus, &zbus_list, bus_next) {
24644510d6fSPierre Morel 		if (pchid == zbus->pchid) {
24744510d6fSPierre Morel 			kref_get(&zbus->kref);
24844510d6fSPierre Morel 			goto out_unlock;
24944510d6fSPierre Morel 		}
25044510d6fSPierre Morel 	}
25144510d6fSPierre Morel 	zbus = NULL;
25244510d6fSPierre Morel out_unlock:
25303502761SNiklas Schnelle 	mutex_unlock(&zbus_list_lock);
25444510d6fSPierre Morel 	return zbus;
25544510d6fSPierre Morel }
25644510d6fSPierre Morel 
zpci_bus_alloc(int pchid)25705bc1be6SPierre Morel static struct zpci_bus *zpci_bus_alloc(int pchid)
25805bc1be6SPierre Morel {
25905bc1be6SPierre Morel 	struct zpci_bus *zbus;
26005bc1be6SPierre Morel 
26105bc1be6SPierre Morel 	zbus = kzalloc(sizeof(*zbus), GFP_KERNEL);
26205bc1be6SPierre Morel 	if (!zbus)
26305bc1be6SPierre Morel 		return NULL;
26405bc1be6SPierre Morel 
26505bc1be6SPierre Morel 	zbus->pchid = pchid;
26605bc1be6SPierre Morel 	INIT_LIST_HEAD(&zbus->bus_next);
26703502761SNiklas Schnelle 	mutex_lock(&zbus_list_lock);
26805bc1be6SPierre Morel 	list_add_tail(&zbus->bus_next, &zbus_list);
26903502761SNiklas Schnelle 	mutex_unlock(&zbus_list_lock);
27005bc1be6SPierre Morel 
27105bc1be6SPierre Morel 	kref_init(&zbus->kref);
27205bc1be6SPierre Morel 	INIT_LIST_HEAD(&zbus->resources);
27305bc1be6SPierre Morel 
27465e450a9SPierre Morel 	zbus->bus_resource.start = 0;
27565e450a9SPierre Morel 	zbus->bus_resource.end = ZPCI_BUS_NR;
27665e450a9SPierre Morel 	zbus->bus_resource.flags = IORESOURCE_BUS;
27765e450a9SPierre Morel 	pci_add_resource(&zbus->resources, &zbus->bus_resource);
27865e450a9SPierre Morel 
27905bc1be6SPierre Morel 	return zbus;
28005bc1be6SPierre Morel }
28105bc1be6SPierre Morel 
pcibios_bus_add_device(struct pci_dev * pdev)282b97bf44fSNiklas Schnelle void pcibios_bus_add_device(struct pci_dev *pdev)
283b97bf44fSNiklas Schnelle {
284b97bf44fSNiklas Schnelle 	struct zpci_dev *zdev = to_zpci(pdev);
285b97bf44fSNiklas Schnelle 
286b97bf44fSNiklas Schnelle 	/*
287b97bf44fSNiklas Schnelle 	 * With pdev->no_vf_scan the common PCI probing code does not
288b97bf44fSNiklas Schnelle 	 * perform PF/VF linking.
289b97bf44fSNiklas Schnelle 	 */
29008b6e22bSMatthew Rosato 	if (zdev->vfn) {
291abb95b75SNiklas Schnelle 		zpci_iov_setup_virtfn(zdev->zbus, pdev, zdev->vfn);
29208b6e22bSMatthew Rosato 		pdev->no_command_memory = 1;
29308b6e22bSMatthew Rosato 	}
294b97bf44fSNiklas Schnelle }
295b97bf44fSNiklas Schnelle 
zpci_bus_add_device(struct zpci_bus * zbus,struct zpci_dev * zdev)296a50297cfSNiklas Schnelle static int zpci_bus_add_device(struct zpci_bus *zbus, struct zpci_dev *zdev)
29744510d6fSPierre Morel {
298a50297cfSNiklas Schnelle 	int rc = -EINVAL;
29944510d6fSPierre Morel 
300a50297cfSNiklas Schnelle 	if (zbus->function[zdev->devfn]) {
301a50297cfSNiklas Schnelle 		pr_err("devfn %04x is already assigned\n", zdev->devfn);
302a50297cfSNiklas Schnelle 		return rc;
303a50297cfSNiklas Schnelle 	}
304960ac362SNiklas Schnelle 
30581a07617SNiklas Schnelle 	zdev->zbus = zbus;
306a50297cfSNiklas Schnelle 	zbus->function[zdev->devfn] = zdev;
307a50297cfSNiklas Schnelle 	zpci_nb_devices++;
308a50297cfSNiklas Schnelle 
309a50297cfSNiklas Schnelle 	if (zbus->multifunction && !zdev->rid_available) {
310a50297cfSNiklas Schnelle 		WARN_ONCE(1, "rid_available not set for multifunction\n");
311a50297cfSNiklas Schnelle 		goto error;
312a50297cfSNiklas Schnelle 	}
313960ac362SNiklas Schnelle 	rc = zpci_init_slot(zdev);
314960ac362SNiklas Schnelle 	if (rc)
315960ac362SNiklas Schnelle 		goto error;
316960ac362SNiklas Schnelle 	zdev->has_hp_slot = 1;
317a50297cfSNiklas Schnelle 
318a50297cfSNiklas Schnelle 	return 0;
319a50297cfSNiklas Schnelle 
320a50297cfSNiklas Schnelle error:
321a50297cfSNiklas Schnelle 	zbus->function[zdev->devfn] = NULL;
32281a07617SNiklas Schnelle 	zdev->zbus = NULL;
323a50297cfSNiklas Schnelle 	zpci_nb_devices--;
324a50297cfSNiklas Schnelle 	return rc;
32544510d6fSPierre Morel }
32644510d6fSPierre Morel 
zpci_bus_device_register(struct zpci_dev * zdev,struct pci_ops * ops)32705bc1be6SPierre Morel int zpci_bus_device_register(struct zpci_dev *zdev, struct pci_ops *ops)
32805bc1be6SPierre Morel {
32944510d6fSPierre Morel 	struct zpci_bus *zbus = NULL;
33044510d6fSPierre Morel 	int rc = -EBADF;
33105bc1be6SPierre Morel 
33205bc1be6SPierre Morel 	if (zpci_nb_devices == ZPCI_NR_DEVICES) {
33305bc1be6SPierre Morel 		pr_warn("Adding PCI function %08x failed because the configured limit of %d is reached\n",
33405bc1be6SPierre Morel 			zdev->fid, ZPCI_NR_DEVICES);
33505bc1be6SPierre Morel 		return -ENOSPC;
33605bc1be6SPierre Morel 	}
33705bc1be6SPierre Morel 
33844510d6fSPierre Morel 	if (zdev->devfn >= ZPCI_FUNCTIONS_PER_BUS)
33905bc1be6SPierre Morel 		return -EINVAL;
34005bc1be6SPierre Morel 
34144510d6fSPierre Morel 	if (!s390_pci_no_rid && zdev->rid_available)
34244510d6fSPierre Morel 		zbus = zpci_bus_get(zdev->pchid);
34344510d6fSPierre Morel 
34444510d6fSPierre Morel 	if (!zbus) {
34505bc1be6SPierre Morel 		zbus = zpci_bus_alloc(zdev->pchid);
34605bc1be6SPierre Morel 		if (!zbus)
34705bc1be6SPierre Morel 			return -ENOMEM;
34844510d6fSPierre Morel 	}
34905bc1be6SPierre Morel 
350960ac362SNiklas Schnelle 	if (!zbus->bus) {
351960ac362SNiklas Schnelle 		/* The UID of the first PCI function registered with a zpci_bus
352960ac362SNiklas Schnelle 		 * is used as the domain number for that bus. Currently there
353960ac362SNiklas Schnelle 		 * is exactly one zpci_bus per domain.
354960ac362SNiklas Schnelle 		 */
355a50297cfSNiklas Schnelle 		rc = zpci_bus_create_pci_bus(zbus, zdev, ops);
356a50297cfSNiklas Schnelle 		if (rc)
357a50297cfSNiklas Schnelle 			goto error;
35844510d6fSPierre Morel 	}
35905bc1be6SPierre Morel 
36044510d6fSPierre Morel 	rc = zpci_bus_add_device(zbus, zdev);
36144510d6fSPierre Morel 	if (rc)
362a50297cfSNiklas Schnelle 		goto error;
363a50297cfSNiklas Schnelle 
36405bc1be6SPierre Morel 	return 0;
36505bc1be6SPierre Morel 
36644510d6fSPierre Morel error:
36705bc1be6SPierre Morel 	pr_err("Adding PCI function %08x failed\n", zdev->fid);
36805bc1be6SPierre Morel 	zpci_bus_put(zbus);
36905bc1be6SPierre Morel 	return rc;
37005bc1be6SPierre Morel }
37105bc1be6SPierre Morel 
zpci_bus_device_unregister(struct zpci_dev * zdev)37205bc1be6SPierre Morel void zpci_bus_device_unregister(struct zpci_dev *zdev)
37305bc1be6SPierre Morel {
37405bc1be6SPierre Morel 	struct zpci_bus *zbus = zdev->zbus;
37505bc1be6SPierre Morel 
37605bc1be6SPierre Morel 	zpci_nb_devices--;
37744510d6fSPierre Morel 	zbus->function[zdev->devfn] = NULL;
37805bc1be6SPierre Morel 	zpci_bus_put(zbus);
37905bc1be6SPierre Morel }
380