Skip to content

Commit 44510d6

Browse files
Pierre MorelVasily Gorbik
authored andcommitted
s390/pci: Handling multifunctions
We allow multiple functions on a single bus. We suppress the ZPCI_DEVFN definition and replace its occurences with zpci->devfn. We verify the number of device during the registration. There can never be more domains in use than existing devices, so we do not need to verify the count of domain after having verified the count of devices. Signed-off-by: Pierre Morel <pmorel@linux.ibm.com> Reviewed-by: Niklas Schnelle <schnelle@linux.ibm.com> Signed-off-by: Vasily Gorbik <gor@linux.ibm.com>
1 parent 65e450a commit 44510d6

6 files changed

Lines changed: 159 additions & 51 deletions

File tree

‎arch/s390/include/asm/pci.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ int pci_domain_nr(struct pci_bus *);
2222
int pci_proc_domain(struct pci_bus *);
2323

2424
#define ZPCI_BUS_NR 0 /* default bus number */
25-
#define ZPCI_DEVFN 0 /* default device number */
2625

2726
#define ZPCI_NR_DMA_SPACES 1
2827
#define ZPCI_NR_DEVICES CONFIG_PCI_NR_FUNCTIONS
@@ -110,13 +109,15 @@ struct zpci_bus {
110109
struct resource bus_resource;
111110
int pchid;
112111
int domain_nr;
112+
bool multifunction;
113113
enum pci_bus_speed max_bus_speed;
114114
};
115115

116116
/* Private data per function */
117117
struct zpci_dev {
118118
struct zpci_bus *zbus;
119119
struct list_head entry; /* list of all zpci_devices, needed for hotplug, etc. */
120+
struct list_head bus_next;
120121
struct kref kref;
121122
struct hotplug_slot hotplug_slot;
122123

@@ -129,7 +130,8 @@ struct zpci_dev {
129130
u8 pft; /* pci function type */
130131
u8 port;
131132
u8 rid_available : 1;
132-
u8 reserved : 7;
133+
u8 has_hp_slot : 1;
134+
u8 reserved : 6;
133135
unsigned int devfn; /* DEVFN part of the RID*/
134136

135137
struct mutex lock;
@@ -253,7 +255,7 @@ static inline struct zpci_dev *to_zpci(struct pci_dev *pdev)
253255
{
254256
struct zpci_bus *zbus = pdev->sysdata;
255257

256-
return zbus->function[ZPCI_DEVFN];
258+
return zbus->function[pdev->devfn];
257259
}
258260

259261
static inline struct zpci_dev *to_zpci_dev(struct device *dev)

‎arch/s390/pci/pci.c‎

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -371,29 +371,17 @@ EXPORT_SYMBOL(pci_iounmap);
371371
static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
372372
int size, u32 *val)
373373
{
374-
struct zpci_dev *zdev = get_zdev_by_bus(bus);
375-
int ret;
374+
struct zpci_dev *zdev = get_zdev_by_bus(bus, devfn);
376375

377-
if (!zdev || devfn != ZPCI_DEVFN)
378-
ret = -ENODEV;
379-
else
380-
ret = zpci_cfg_load(zdev, where, val, size);
381-
382-
return ret;
376+
return (zdev) ? zpci_cfg_load(zdev, where, val, size) : -ENODEV;
383377
}
384378

385379
static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
386380
int size, u32 val)
387381
{
388-
struct zpci_dev *zdev = get_zdev_by_bus(bus);
389-
int ret;
382+
struct zpci_dev *zdev = get_zdev_by_bus(bus, devfn);
390383

391-
if (!zdev || devfn != ZPCI_DEVFN)
392-
ret = -ENODEV;
393-
else
394-
ret = zpci_cfg_store(zdev, where, val, size);
395-
396-
return ret;
384+
return (zdev) ? zpci_cfg_store(zdev, where, val, size) : -ENODEV;
397385
}
398386

399387
static struct pci_ops pci_root_ops = {
@@ -708,12 +696,12 @@ int zpci_create_device(struct zpci_dev *zdev)
708696
if (rc)
709697
goto out_disable;
710698

711-
zpci_init_slot(zdev);
712699
return 0;
713700

714701
out_disable:
715702
if (zdev->state == ZPCI_FN_STATE_ONLINE)
716703
zpci_disable_device(zdev);
704+
717705
out_destroy_iommu:
718706
zpci_destroy_iommu(zdev);
719707
out:
@@ -727,18 +715,25 @@ void zpci_release_device(struct kref *kref)
727715
{
728716
struct zpci_dev *zdev = container_of(kref, struct zpci_dev, kref);
729717

718+
if (zdev->zbus->bus) {
719+
struct pci_dev *pdev;
720+
721+
pdev = pci_get_slot(zdev->zbus->bus, zdev->devfn);
722+
if (pdev)
723+
pci_stop_and_remove_bus_device_locked(pdev);
724+
}
725+
730726
switch (zdev->state) {
731727
case ZPCI_FN_STATE_ONLINE:
732728
case ZPCI_FN_STATE_CONFIGURED:
733729
zpci_disable_device(zdev);
734730
fallthrough;
735731
case ZPCI_FN_STATE_STANDBY:
736-
if (zdev->zbus) {
732+
if (zdev->has_hp_slot)
737733
zpci_exit_slot(zdev);
738-
zpci_cleanup_bus_resources(zdev);
739-
zpci_bus_device_unregister(zdev);
740-
zpci_destroy_iommu(zdev);
741-
}
734+
zpci_cleanup_bus_resources(zdev);
735+
zpci_bus_device_unregister(zdev);
736+
zpci_destroy_iommu(zdev);
742737
fallthrough;
743738
default:
744739
break;

‎arch/s390/pci/pci_bus.c‎

Lines changed: 129 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@ static void zpci_bus_release(struct kref *kref)
6262
{
6363
struct zpci_bus *zbus = container_of(kref, struct zpci_bus, kref);
6464

65-
pci_lock_rescan_remove();
66-
pci_stop_root_bus(zbus->bus);
65+
if (zbus->bus) {
66+
pci_lock_rescan_remove();
67+
pci_stop_root_bus(zbus->bus);
6768

68-
zpci_free_domain(zbus->domain_nr);
69-
pci_free_resource_list(&zbus->resources);
69+
zpci_free_domain(zbus->domain_nr);
70+
pci_free_resource_list(&zbus->resources);
7071

71-
pci_remove_root_bus(zbus->bus);
72-
pci_unlock_rescan_remove();
72+
pci_remove_root_bus(zbus->bus);
73+
pci_unlock_rescan_remove();
74+
}
7375

7476
spin_lock(&zbus_list_lock);
7577
list_del(&zbus->bus_next);
@@ -82,6 +84,23 @@ static void zpci_bus_put(struct zpci_bus *zbus)
8284
kref_put(&zbus->kref, zpci_bus_release);
8385
}
8486

87+
static struct zpci_bus *zpci_bus_get(int pchid)
88+
{
89+
struct zpci_bus *zbus;
90+
91+
spin_lock(&zbus_list_lock);
92+
list_for_each_entry(zbus, &zbus_list, bus_next) {
93+
if (pchid == zbus->pchid) {
94+
kref_get(&zbus->kref);
95+
goto out_unlock;
96+
}
97+
}
98+
zbus = NULL;
99+
out_unlock:
100+
spin_unlock(&zbus_list_lock);
101+
return zbus;
102+
}
103+
85104
static struct zpci_bus *zpci_bus_alloc(int pchid)
86105
{
87106
struct zpci_bus *zbus;
@@ -107,37 +126,128 @@ static struct zpci_bus *zpci_bus_alloc(int pchid)
107126
return zbus;
108127
}
109128

110-
int zpci_bus_device_register(struct zpci_dev *zdev, struct pci_ops *ops)
129+
static int zpci_bus_add_device(struct zpci_bus *zbus, struct zpci_dev *zdev)
111130
{
112-
struct zpci_bus *zbus;
131+
struct pci_bus *bus;
132+
struct resource_entry *window, *n;
133+
struct resource *res;
134+
struct pci_dev *pdev;
113135
int rc;
114136

137+
bus = zbus->bus;
138+
if (!bus)
139+
return -EINVAL;
140+
141+
pdev = pci_get_slot(bus, zdev->devfn);
142+
if (pdev) {
143+
/* Device is already known. */
144+
pci_dev_put(pdev);
145+
return 0;
146+
}
147+
148+
rc = zpci_init_slot(zdev);
149+
if (rc)
150+
return rc;
151+
zdev->has_hp_slot = 1;
152+
153+
resource_list_for_each_entry_safe(window, n, &zbus->resources) {
154+
res = window->res;
155+
pci_bus_add_resource(bus, res, 0);
156+
}
157+
158+
pdev = pci_scan_single_device(bus, zdev->devfn);
159+
if (pdev) {
160+
pdev->multifunction = 1;
161+
pci_bus_add_device(pdev);
162+
}
163+
164+
return 0;
165+
}
166+
167+
static void zpci_bus_add_devices(struct zpci_bus *zbus)
168+
{
169+
int i;
170+
171+
for (i = 1; i < ZPCI_FUNCTIONS_PER_BUS; i++)
172+
if (zbus->function[i])
173+
zpci_bus_add_device(zbus, zbus->function[i]);
174+
175+
pci_lock_rescan_remove();
176+
pci_bus_add_devices(zbus->bus);
177+
pci_unlock_rescan_remove();
178+
}
179+
180+
int zpci_bus_device_register(struct zpci_dev *zdev, struct pci_ops *ops)
181+
{
182+
struct zpci_bus *zbus = NULL;
183+
int rc = -EBADF;
184+
115185
if (zpci_nb_devices == ZPCI_NR_DEVICES) {
116186
pr_warn("Adding PCI function %08x failed because the configured limit of %d is reached\n",
117187
zdev->fid, ZPCI_NR_DEVICES);
118188
return -ENOSPC;
119189
}
120190
zpci_nb_devices++;
121191

122-
if (zdev->devfn != ZPCI_DEVFN)
192+
if (zdev->devfn >= ZPCI_FUNCTIONS_PER_BUS)
123193
return -EINVAL;
124194

125-
zbus = zpci_bus_alloc(zdev->pchid);
126-
if (!zbus)
127-
return -ENOMEM;
195+
if (!s390_pci_no_rid && zdev->rid_available)
196+
zbus = zpci_bus_get(zdev->pchid);
197+
198+
if (!zbus) {
199+
zbus = zpci_bus_alloc(zdev->pchid);
200+
if (!zbus)
201+
return -ENOMEM;
202+
}
128203

129204
zdev->zbus = zbus;
130-
zbus->function[ZPCI_DEVFN] = zdev;
205+
if (zbus->function[zdev->devfn]) {
206+
pr_err("devfn %04x is already assigned\n", zdev->devfn);
207+
goto error; /* rc already set */
208+
}
209+
zbus->function[zdev->devfn] = zdev;
131210

132211
zpci_setup_bus_resources(zdev, &zbus->resources);
133-
zbus->max_bus_speed = zdev->max_bus_speed;
134212

135-
rc = zpci_bus_scan(zbus, (u16)zdev->uid, ops);
136-
if (!rc)
137-
return 0;
213+
if (zbus->bus) {
214+
if (!zbus->multifunction) {
215+
WARN_ONCE(1, "zbus is not multifunction\n");
216+
goto error_bus;
217+
}
218+
if (!zdev->rid_available) {
219+
WARN_ONCE(1, "rid_available not set for multifunction\n");
220+
goto error_bus;
221+
}
222+
rc = zpci_bus_add_device(zbus, zdev);
223+
if (rc)
224+
goto error_bus;
225+
} else if (zdev->devfn == 0) {
226+
if (zbus->multifunction && !zdev->rid_available) {
227+
WARN_ONCE(1, "rid_available not set on function 0 for multifunction\n");
228+
goto error_bus;
229+
}
230+
rc = zpci_bus_scan(zbus, (u16)zdev->uid, ops);
231+
if (rc)
232+
goto error_bus;
233+
zpci_bus_add_devices(zbus);
234+
rc = zpci_init_slot(zdev);
235+
if (rc)
236+
goto error_bus;
237+
zdev->has_hp_slot = 1;
238+
zbus->multifunction = zdev->rid_available;
239+
zbus->max_bus_speed = zdev->max_bus_speed;
240+
} else {
241+
zbus->multifunction = 1;
242+
}
138243

244+
return 0;
245+
246+
error_bus:
247+
zpci_nb_devices--;
248+
zbus->function[zdev->devfn] = NULL;
249+
error:
139250
pr_err("Adding PCI function %08x failed\n", zdev->fid);
140-
zdev->zbus = NULL;
141251
zpci_bus_put(zbus);
142252
return rc;
143253
}
@@ -147,6 +257,6 @@ void zpci_bus_device_unregister(struct zpci_dev *zdev)
147257
struct zpci_bus *zbus = zdev->zbus;
148258

149259
zpci_nb_devices--;
150-
zbus->function[ZPCI_DEVFN] = NULL;
260+
zbus->function[zdev->devfn] = NULL;
151261
zpci_bus_put(zbus);
152262
}

‎arch/s390/pci/pci_bus.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ void zpci_free_domain(int domain);
2222
int zpci_setup_bus_resources(struct zpci_dev *zdev,
2323
struct list_head *resources);
2424

25-
static inline struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus)
25+
static inline struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus,
26+
unsigned int devfn)
2627
{
2728
struct zpci_bus *zbus = bus->sysdata;
2829

29-
return zbus->function[ZPCI_DEVFN];
30+
return (devfn >= ZPCI_FUNCTIONS_PER_BUS) ? NULL : zbus->function[devfn];
3031
}

‎arch/s390/pci/pci_event.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static void __zpci_event_error(struct zpci_ccdf_err *ccdf)
5555
zpci_err_hex(ccdf, sizeof(*ccdf));
5656

5757
if (zdev)
58-
pdev = pci_get_slot(zdev->zbus->bus, ZPCI_DEVFN);
58+
pdev = pci_get_slot(zdev->zbus->bus, zdev->devfn);
5959

6060
pr_err("%s: Event 0x%x reports an error for PCI function 0x%x\n",
6161
pdev ? pci_name(pdev) : "n/a", ccdf->pec, ccdf->fid);
@@ -81,7 +81,7 @@ static void __zpci_event_availability(struct zpci_ccdf_avail *ccdf)
8181
int ret;
8282

8383
if (zdev && zdev->zbus && zdev->zbus->bus)
84-
pdev = pci_get_slot(zdev->zbus->bus, ZPCI_DEVFN);
84+
pdev = pci_get_slot(zdev->zbus->bus, zdev->devfn);
8585

8686
zpci_err("avail CCDF:\n");
8787
zpci_err_hex(ccdf, sizeof(*ccdf));

‎drivers/pci/hotplug/s390_pci_hpc.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static int enable_slot(struct hotplug_slot *hotplug_slot)
6666
if (rc)
6767
goto out_deconfigure;
6868

69-
pci_scan_slot(zbus->bus, ZPCI_DEVFN);
69+
pci_scan_slot(zbus->bus, zdev->devfn);
7070
pci_lock_rescan_remove();
7171
pci_bus_add_devices(zbus->bus);
7272
pci_unlock_rescan_remove();
@@ -89,7 +89,7 @@ static int disable_slot(struct hotplug_slot *hotplug_slot)
8989
if (!zpci_fn_configured(zdev->state))
9090
return -EIO;
9191

92-
pdev = pci_get_slot(zbus->bus, ZPCI_DEVFN);
92+
pdev = pci_get_slot(zbus->bus, zdev->devfn);
9393
if (pdev) {
9494
pci_stop_and_remove_bus_device_locked(pdev);
9595
pci_dev_put(pdev);
@@ -141,7 +141,7 @@ int zpci_init_slot(struct zpci_dev *zdev)
141141

142142
snprintf(name, SLOT_NAME_SIZE, "%08x", zdev->fid);
143143
return pci_hp_register(&zdev->hotplug_slot, zbus->bus,
144-
ZPCI_DEVFN, name);
144+
zdev->devfn, name);
145145
}
146146

147147
void zpci_exit_slot(struct zpci_dev *zdev)

0 commit comments

Comments
 (0)