xref: /openbmc/linux/arch/s390/pci/pci_bus.c (revision 11a163f2)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright IBM Corp. 2020
4  *
5  * Author(s):
6  *   Pierre Morel <pmorel@linux.ibm.com>
7  *
8  */
9 
10 #define KMSG_COMPONENT "zpci"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/err.h>
16 #include <linux/export.h>
17 #include <linux/delay.h>
18 #include <linux/seq_file.h>
19 #include <linux/jump_label.h>
20 #include <linux/pci.h>
21 #include <linux/printk.h>
22 
23 #include <asm/pci_clp.h>
24 #include <asm/pci_dma.h>
25 
26 #include "pci_bus.h"
27 #include "pci_iov.h"
28 
29 static LIST_HEAD(zbus_list);
30 static DEFINE_SPINLOCK(zbus_list_lock);
31 static int zpci_nb_devices;
32 
33 /* zpci_bus_scan
34  * @zbus: the zbus holding the zdevices
35  * @ops: the pci operations
36  *
37  * The domain number must be set before pci_scan_root_bus is called.
38  * This function can be called once the domain is known, hence
39  * when the function_0 is dicovered.
40  */
41 static int zpci_bus_scan(struct zpci_bus *zbus, int domain, struct pci_ops *ops)
42 {
43 	struct pci_bus *bus;
44 	int rc;
45 
46 	rc = zpci_alloc_domain(domain);
47 	if (rc < 0)
48 		return rc;
49 	zbus->domain_nr = rc;
50 
51 	bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, ops, zbus, &zbus->resources);
52 	if (!bus) {
53 		zpci_free_domain(zbus->domain_nr);
54 		return -EFAULT;
55 	}
56 
57 	zbus->bus = bus;
58 	pci_bus_add_devices(bus);
59 	return 0;
60 }
61 
62 static void zpci_bus_release(struct kref *kref)
63 {
64 	struct zpci_bus *zbus = container_of(kref, struct zpci_bus, kref);
65 
66 	if (zbus->bus) {
67 		pci_lock_rescan_remove();
68 		pci_stop_root_bus(zbus->bus);
69 
70 		zpci_free_domain(zbus->domain_nr);
71 		pci_free_resource_list(&zbus->resources);
72 
73 		pci_remove_root_bus(zbus->bus);
74 		pci_unlock_rescan_remove();
75 	}
76 
77 	spin_lock(&zbus_list_lock);
78 	list_del(&zbus->bus_next);
79 	spin_unlock(&zbus_list_lock);
80 	kfree(zbus);
81 }
82 
83 static void zpci_bus_put(struct zpci_bus *zbus)
84 {
85 	kref_put(&zbus->kref, zpci_bus_release);
86 }
87 
88 static struct zpci_bus *zpci_bus_get(int pchid)
89 {
90 	struct zpci_bus *zbus;
91 
92 	spin_lock(&zbus_list_lock);
93 	list_for_each_entry(zbus, &zbus_list, bus_next) {
94 		if (pchid == zbus->pchid) {
95 			kref_get(&zbus->kref);
96 			goto out_unlock;
97 		}
98 	}
99 	zbus = NULL;
100 out_unlock:
101 	spin_unlock(&zbus_list_lock);
102 	return zbus;
103 }
104 
105 static struct zpci_bus *zpci_bus_alloc(int pchid)
106 {
107 	struct zpci_bus *zbus;
108 
109 	zbus = kzalloc(sizeof(*zbus), GFP_KERNEL);
110 	if (!zbus)
111 		return NULL;
112 
113 	zbus->pchid = pchid;
114 	INIT_LIST_HEAD(&zbus->bus_next);
115 	spin_lock(&zbus_list_lock);
116 	list_add_tail(&zbus->bus_next, &zbus_list);
117 	spin_unlock(&zbus_list_lock);
118 
119 	kref_init(&zbus->kref);
120 	INIT_LIST_HEAD(&zbus->resources);
121 
122 	zbus->bus_resource.start = 0;
123 	zbus->bus_resource.end = ZPCI_BUS_NR;
124 	zbus->bus_resource.flags = IORESOURCE_BUS;
125 	pci_add_resource(&zbus->resources, &zbus->bus_resource);
126 
127 	return zbus;
128 }
129 
130 void pcibios_bus_add_device(struct pci_dev *pdev)
131 {
132 	struct zpci_dev *zdev = to_zpci(pdev);
133 
134 	/*
135 	 * With pdev->no_vf_scan the common PCI probing code does not
136 	 * perform PF/VF linking.
137 	 */
138 	if (zdev->vfn) {
139 		zpci_iov_setup_virtfn(zdev->zbus, pdev, zdev->vfn);
140 		pdev->no_command_memory = 1;
141 	}
142 }
143 
144 static int zpci_bus_add_device(struct zpci_bus *zbus, struct zpci_dev *zdev)
145 {
146 	struct pci_bus *bus;
147 	struct resource_entry *window, *n;
148 	struct resource *res;
149 	struct pci_dev *pdev;
150 	int rc;
151 
152 	bus = zbus->bus;
153 	if (!bus)
154 		return -EINVAL;
155 
156 	pdev = pci_get_slot(bus, zdev->devfn);
157 	if (pdev) {
158 		/* Device is already known. */
159 		pci_dev_put(pdev);
160 		return 0;
161 	}
162 
163 	rc = zpci_init_slot(zdev);
164 	if (rc)
165 		return rc;
166 	zdev->has_hp_slot = 1;
167 
168 	resource_list_for_each_entry_safe(window, n, &zbus->resources) {
169 		res = window->res;
170 		pci_bus_add_resource(bus, res, 0);
171 	}
172 
173 	pdev = pci_scan_single_device(bus, zdev->devfn);
174 	if (pdev)
175 		pci_bus_add_device(pdev);
176 
177 	return 0;
178 }
179 
180 static void zpci_bus_add_devices(struct zpci_bus *zbus)
181 {
182 	int i;
183 
184 	for (i = 1; i < ZPCI_FUNCTIONS_PER_BUS; i++)
185 		if (zbus->function[i])
186 			zpci_bus_add_device(zbus, zbus->function[i]);
187 
188 	pci_lock_rescan_remove();
189 	pci_bus_add_devices(zbus->bus);
190 	pci_unlock_rescan_remove();
191 }
192 
193 int zpci_bus_device_register(struct zpci_dev *zdev, struct pci_ops *ops)
194 {
195 	struct zpci_bus *zbus = NULL;
196 	int rc = -EBADF;
197 
198 	if (zpci_nb_devices == ZPCI_NR_DEVICES) {
199 		pr_warn("Adding PCI function %08x failed because the configured limit of %d is reached\n",
200 			zdev->fid, ZPCI_NR_DEVICES);
201 		return -ENOSPC;
202 	}
203 	zpci_nb_devices++;
204 
205 	if (zdev->devfn >= ZPCI_FUNCTIONS_PER_BUS)
206 		return -EINVAL;
207 
208 	if (!s390_pci_no_rid && zdev->rid_available)
209 		zbus = zpci_bus_get(zdev->pchid);
210 
211 	if (!zbus) {
212 		zbus = zpci_bus_alloc(zdev->pchid);
213 		if (!zbus)
214 			return -ENOMEM;
215 	}
216 
217 	zdev->zbus = zbus;
218 	if (zbus->function[zdev->devfn]) {
219 		pr_err("devfn %04x is already assigned\n", zdev->devfn);
220 		goto error; /* rc already set */
221 	}
222 	zbus->function[zdev->devfn] = zdev;
223 
224 	zpci_setup_bus_resources(zdev, &zbus->resources);
225 
226 	if (zbus->bus) {
227 		if (!zbus->multifunction) {
228 			WARN_ONCE(1, "zbus is not multifunction\n");
229 			goto error_bus;
230 		}
231 		if (!zdev->rid_available) {
232 			WARN_ONCE(1, "rid_available not set for multifunction\n");
233 			goto error_bus;
234 		}
235 		rc = zpci_bus_add_device(zbus, zdev);
236 		if (rc)
237 			goto error_bus;
238 	} else if (zdev->devfn == 0) {
239 		if (zbus->multifunction && !zdev->rid_available) {
240 			WARN_ONCE(1, "rid_available not set on function 0 for multifunction\n");
241 			goto error_bus;
242 		}
243 		rc = zpci_bus_scan(zbus, (u16)zdev->uid, ops);
244 		if (rc)
245 			goto error_bus;
246 		zpci_bus_add_devices(zbus);
247 		rc = zpci_init_slot(zdev);
248 		if (rc)
249 			goto error_bus;
250 		zdev->has_hp_slot = 1;
251 		zbus->multifunction = zdev->rid_available;
252 		zbus->max_bus_speed = zdev->max_bus_speed;
253 	} else {
254 		zbus->multifunction = 1;
255 	}
256 
257 	return 0;
258 
259 error_bus:
260 	zpci_nb_devices--;
261 	zbus->function[zdev->devfn] = NULL;
262 error:
263 	pr_err("Adding PCI function %08x failed\n", zdev->fid);
264 	zpci_bus_put(zbus);
265 	return rc;
266 }
267 
268 void zpci_bus_device_unregister(struct zpci_dev *zdev)
269 {
270 	struct zpci_bus *zbus = zdev->zbus;
271 
272 	zpci_nb_devices--;
273 	zbus->function[zdev->devfn] = NULL;
274 	zpci_bus_put(zbus);
275 }
276