1
2
3
4
5
6
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
29static LIST_HEAD(zbus_list);
30static DEFINE_MUTEX(zbus_list_lock);
31static int zpci_nb_devices;
32
33
34
35
36
37
38
39
40
41
42static int zpci_bus_prepare_device(struct zpci_dev *zdev)
43{
44 struct resource_entry *window, *n;
45 struct resource *res;
46 int rc;
47
48 if (!zdev_enabled(zdev)) {
49 rc = zpci_enable_device(zdev);
50 if (rc)
51 return rc;
52 rc = zpci_dma_init_device(zdev);
53 if (rc) {
54 zpci_disable_device(zdev);
55 return rc;
56 }
57 }
58
59 if (!zdev->has_resources) {
60 zpci_setup_bus_resources(zdev, &zdev->zbus->resources);
61 resource_list_for_each_entry_safe(window, n, &zdev->zbus->resources) {
62 res = window->res;
63 pci_bus_add_resource(zdev->zbus->bus, res, 0);
64 }
65 }
66
67 return 0;
68}
69
70
71
72
73
74
75
76
77int zpci_bus_scan_device(struct zpci_dev *zdev)
78{
79 struct pci_dev *pdev;
80 int rc;
81
82 rc = zpci_bus_prepare_device(zdev);
83 if (rc)
84 return rc;
85
86 pdev = pci_scan_single_device(zdev->zbus->bus, zdev->devfn);
87 if (!pdev)
88 return -ENODEV;
89
90 pci_bus_add_device(pdev);
91 pci_lock_rescan_remove();
92 pci_bus_add_devices(zdev->zbus->bus);
93 pci_unlock_rescan_remove();
94
95 return 0;
96}
97
98
99
100
101
102
103
104
105
106
107void zpci_bus_remove_device(struct zpci_dev *zdev, bool set_error)
108{
109 struct zpci_bus *zbus = zdev->zbus;
110 struct pci_dev *pdev;
111
112 if (!zdev->zbus->bus)
113 return;
114
115 pdev = pci_get_slot(zbus->bus, zdev->devfn);
116 if (pdev) {
117 if (set_error)
118 pdev->error_state = pci_channel_io_perm_failure;
119 if (pdev->is_virtfn) {
120 zpci_iov_remove_virtfn(pdev, zdev->vfn);
121
122 pci_dev_put(pdev);
123 return;
124 }
125 pci_stop_and_remove_bus_device_locked(pdev);
126
127 pci_dev_put(pdev);
128 }
129}
130
131
132
133
134
135
136
137
138
139
140
141
142
143int zpci_bus_scan_bus(struct zpci_bus *zbus)
144{
145 struct zpci_dev *zdev;
146 int devfn, rc, ret = 0;
147
148 if (!zbus->function[0])
149 return 0;
150
151 for (devfn = 0; devfn < ZPCI_FUNCTIONS_PER_BUS; devfn++) {
152 zdev = zbus->function[devfn];
153 if (zdev && zdev->state == ZPCI_FN_STATE_CONFIGURED) {
154 rc = zpci_bus_prepare_device(zdev);
155 if (rc)
156 ret = -EIO;
157 }
158 }
159
160 pci_lock_rescan_remove();
161 pci_scan_child_bus(zbus->bus);
162 pci_bus_add_devices(zbus->bus);
163 pci_unlock_rescan_remove();
164
165 return ret;
166}
167
168
169
170
171
172
173void zpci_bus_scan_busses(void)
174{
175 struct zpci_bus *zbus = NULL;
176
177 mutex_lock(&zbus_list_lock);
178 list_for_each_entry(zbus, &zbus_list, bus_next) {
179 zpci_bus_scan_bus(zbus);
180 cond_resched();
181 }
182 mutex_unlock(&zbus_list_lock);
183}
184
185
186
187
188
189
190
191
192
193
194
195static int zpci_bus_create_pci_bus(struct zpci_bus *zbus, struct zpci_dev *f0, struct pci_ops *ops)
196{
197 struct pci_bus *bus;
198 int domain;
199
200 domain = zpci_alloc_domain((u16)f0->uid);
201 if (domain < 0)
202 return domain;
203
204 zbus->domain_nr = domain;
205 zbus->multifunction = f0->rid_available;
206 zbus->max_bus_speed = f0->max_bus_speed;
207
208
209
210
211
212 bus = pci_create_root_bus(NULL, ZPCI_BUS_NR, ops, zbus, &zbus->resources);
213 if (!bus) {
214 zpci_free_domain(zbus->domain_nr);
215 return -EFAULT;
216 }
217
218 zbus->bus = bus;
219 pci_bus_add_devices(bus);
220
221 return 0;
222}
223
224static void zpci_bus_release(struct kref *kref)
225{
226 struct zpci_bus *zbus = container_of(kref, struct zpci_bus, kref);
227
228 if (zbus->bus) {
229 pci_lock_rescan_remove();
230 pci_stop_root_bus(zbus->bus);
231
232 zpci_free_domain(zbus->domain_nr);
233 pci_free_resource_list(&zbus->resources);
234
235 pci_remove_root_bus(zbus->bus);
236 pci_unlock_rescan_remove();
237 }
238
239 mutex_lock(&zbus_list_lock);
240 list_del(&zbus->bus_next);
241 mutex_unlock(&zbus_list_lock);
242 kfree(zbus);
243}
244
245static void zpci_bus_put(struct zpci_bus *zbus)
246{
247 kref_put(&zbus->kref, zpci_bus_release);
248}
249
250static struct zpci_bus *zpci_bus_get(int pchid)
251{
252 struct zpci_bus *zbus;
253
254 mutex_lock(&zbus_list_lock);
255 list_for_each_entry(zbus, &zbus_list, bus_next) {
256 if (pchid == zbus->pchid) {
257 kref_get(&zbus->kref);
258 goto out_unlock;
259 }
260 }
261 zbus = NULL;
262out_unlock:
263 mutex_unlock(&zbus_list_lock);
264 return zbus;
265}
266
267static struct zpci_bus *zpci_bus_alloc(int pchid)
268{
269 struct zpci_bus *zbus;
270
271 zbus = kzalloc(sizeof(*zbus), GFP_KERNEL);
272 if (!zbus)
273 return NULL;
274
275 zbus->pchid = pchid;
276 INIT_LIST_HEAD(&zbus->bus_next);
277 mutex_lock(&zbus_list_lock);
278 list_add_tail(&zbus->bus_next, &zbus_list);
279 mutex_unlock(&zbus_list_lock);
280
281 kref_init(&zbus->kref);
282 INIT_LIST_HEAD(&zbus->resources);
283
284 zbus->bus_resource.start = 0;
285 zbus->bus_resource.end = ZPCI_BUS_NR;
286 zbus->bus_resource.flags = IORESOURCE_BUS;
287 pci_add_resource(&zbus->resources, &zbus->bus_resource);
288
289 return zbus;
290}
291
292void pcibios_bus_add_device(struct pci_dev *pdev)
293{
294 struct zpci_dev *zdev = to_zpci(pdev);
295
296
297
298
299
300 if (zdev->vfn) {
301 zpci_iov_setup_virtfn(zdev->zbus, pdev, zdev->vfn);
302 pdev->no_command_memory = 1;
303 }
304}
305
306
307
308
309
310
311
312
313
314
315
316static int zpci_bus_create_hotplug_slots(struct zpci_dev *zdev)
317{
318 struct zpci_bus *zbus = zdev->zbus;
319 int devfn, rc = 0;
320
321 rc = zpci_init_slot(zdev);
322 if (rc)
323 return rc;
324 zdev->has_hp_slot = 1;
325
326 if (zdev->devfn == 0 && zbus->multifunction) {
327
328
329
330
331
332 for (devfn = 1; devfn < ZPCI_FUNCTIONS_PER_BUS; devfn++) {
333 zdev = zbus->function[devfn];
334 if (zdev && !zdev->has_hp_slot) {
335 rc = zpci_init_slot(zdev);
336 if (rc)
337 return rc;
338 zdev->has_hp_slot = 1;
339 }
340 }
341
342 }
343
344 return rc;
345}
346
347static int zpci_bus_add_device(struct zpci_bus *zbus, struct zpci_dev *zdev)
348{
349 int rc = -EINVAL;
350
351 if (zbus->function[zdev->devfn]) {
352 pr_err("devfn %04x is already assigned\n", zdev->devfn);
353 return rc;
354 }
355 zdev->zbus = zbus;
356 zbus->function[zdev->devfn] = zdev;
357 zpci_nb_devices++;
358
359 if (zbus->bus) {
360 if (zbus->multifunction && !zdev->rid_available) {
361 WARN_ONCE(1, "rid_available not set for multifunction\n");
362 goto error;
363 }
364
365 zpci_bus_create_hotplug_slots(zdev);
366 } else {
367
368 zbus->multifunction = 1;
369 }
370
371 return 0;
372
373error:
374 zbus->function[zdev->devfn] = NULL;
375 zdev->zbus = NULL;
376 zpci_nb_devices--;
377 return rc;
378}
379
380int zpci_bus_device_register(struct zpci_dev *zdev, struct pci_ops *ops)
381{
382 struct zpci_bus *zbus = NULL;
383 int rc = -EBADF;
384
385 if (zpci_nb_devices == ZPCI_NR_DEVICES) {
386 pr_warn("Adding PCI function %08x failed because the configured limit of %d is reached\n",
387 zdev->fid, ZPCI_NR_DEVICES);
388 return -ENOSPC;
389 }
390
391 if (zdev->devfn >= ZPCI_FUNCTIONS_PER_BUS)
392 return -EINVAL;
393
394 if (!s390_pci_no_rid && zdev->rid_available)
395 zbus = zpci_bus_get(zdev->pchid);
396
397 if (!zbus) {
398 zbus = zpci_bus_alloc(zdev->pchid);
399 if (!zbus)
400 return -ENOMEM;
401 }
402
403 if (zdev->devfn == 0) {
404 rc = zpci_bus_create_pci_bus(zbus, zdev, ops);
405 if (rc)
406 goto error;
407 }
408
409 rc = zpci_bus_add_device(zbus, zdev);
410 if (rc)
411 goto error;
412
413 return 0;
414
415error:
416 pr_err("Adding PCI function %08x failed\n", zdev->fid);
417 zpci_bus_put(zbus);
418 return rc;
419}
420
421void zpci_bus_device_unregister(struct zpci_dev *zdev)
422{
423 struct zpci_bus *zbus = zdev->zbus;
424
425 zpci_nb_devices--;
426 zbus->function[zdev->devfn] = NULL;
427 zpci_bus_put(zbus);
428}
429