Blame view

drivers/pci/iov.c 16.3 KB
d1b054da8   Yu Zhao   PCI: initialize a...
1
2
3
4
5
6
7
  /*
   * drivers/pci/iov.c
   *
   * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
   *
   * PCI Express I/O Virtualization (IOV) support.
   *   Single Root IOV 1.0
302b4215d   Yu Zhao   PCI: support the ...
8
   *   Address Translation Service 1.0
d1b054da8   Yu Zhao   PCI: initialize a...
9
10
11
   */
  
  #include <linux/pci.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
12
  #include <linux/slab.h>
d1b054da8   Yu Zhao   PCI: initialize a...
13
  #include <linux/mutex.h>
363c75db1   Paul Gortmaker   pci: Fix files ne...
14
  #include <linux/export.h>
d1b054da8   Yu Zhao   PCI: initialize a...
15
16
  #include <linux/string.h>
  #include <linux/delay.h>
5cdede240   Joerg Roedel   PCI: Move ATS dec...
17
  #include <linux/pci-ats.h>
d1b054da8   Yu Zhao   PCI: initialize a...
18
  #include "pci.h"
dd7cc44d0   Yu Zhao   PCI: add SR-IOV A...
19
  #define VIRTFN_ID_LEN	16
d1b054da8   Yu Zhao   PCI: initialize a...
20

a28724b0f   Yu Zhao   PCI: reserve bus ...
21
22
23
24
25
26
27
28
29
30
31
  static inline u8 virtfn_bus(struct pci_dev *dev, int id)
  {
  	return dev->bus->number + ((dev->devfn + dev->sriov->offset +
  				    dev->sriov->stride * id) >> 8);
  }
  
  static inline u8 virtfn_devfn(struct pci_dev *dev, int id)
  {
  	return (dev->devfn + dev->sriov->offset +
  		dev->sriov->stride * id) & 0xff;
  }
dd7cc44d0   Yu Zhao   PCI: add SR-IOV A...
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
  {
  	int rc;
  	struct pci_bus *child;
  
  	if (bus->number == busnr)
  		return bus;
  
  	child = pci_find_bus(pci_domain_nr(bus), busnr);
  	if (child)
  		return child;
  
  	child = pci_add_new_bus(bus, NULL, busnr);
  	if (!child)
  		return NULL;
  
  	child->subordinate = busnr;
  	child->dev.parent = bus->bridge;
  	rc = pci_bus_add_child(child);
  	if (rc) {
  		pci_remove_bus(child);
  		return NULL;
  	}
  
  	return child;
  }
  
  static void virtfn_remove_bus(struct pci_bus *bus, int busnr)
  {
  	struct pci_bus *child;
  
  	if (bus->number == busnr)
  		return;
  
  	child = pci_find_bus(pci_domain_nr(bus), busnr);
  	BUG_ON(!child);
  
  	if (list_empty(&child->devices))
  		pci_remove_bus(child);
  }
  
  static int virtfn_add(struct pci_dev *dev, int id, int reset)
  {
  	int i;
  	int rc;
  	u64 size;
  	char buf[VIRTFN_ID_LEN];
  	struct pci_dev *virtfn;
  	struct resource *res;
  	struct pci_sriov *iov = dev->sriov;
  
  	virtfn = alloc_pci_dev();
  	if (!virtfn)
  		return -ENOMEM;
  
  	mutex_lock(&iov->dev->sriov->lock);
  	virtfn->bus = virtfn_add_bus(dev->bus, virtfn_bus(dev, id));
  	if (!virtfn->bus) {
  		kfree(virtfn);
  		mutex_unlock(&iov->dev->sriov->lock);
  		return -ENOMEM;
  	}
  	virtfn->devfn = virtfn_devfn(dev, id);
  	virtfn->vendor = dev->vendor;
  	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_DID, &virtfn->device);
  	pci_setup_device(virtfn);
  	virtfn->dev.parent = dev->dev.parent;
  
  	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  		res = dev->resource + PCI_IOV_RESOURCES + i;
  		if (!res->parent)
  			continue;
  		virtfn->resource[i].name = pci_name(virtfn);
  		virtfn->resource[i].flags = res->flags;
  		size = resource_size(res);
  		do_div(size, iov->total);
  		virtfn->resource[i].start = res->start + size * id;
  		virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
  		rc = request_resource(res, &virtfn->resource[i]);
  		BUG_ON(rc);
  	}
  
  	if (reset)
8c1c699fe   Yu Zhao   PCI: cleanup Func...
115
  		__pci_reset_function(virtfn);
dd7cc44d0   Yu Zhao   PCI: add SR-IOV A...
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  
  	pci_device_add(virtfn, virtfn->bus);
  	mutex_unlock(&iov->dev->sriov->lock);
  
  	virtfn->physfn = pci_dev_get(dev);
  	virtfn->is_virtfn = 1;
  
  	rc = pci_bus_add_device(virtfn);
  	if (rc)
  		goto failed1;
  	sprintf(buf, "virtfn%u", id);
  	rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
  	if (rc)
  		goto failed1;
  	rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
  	if (rc)
  		goto failed2;
  
  	kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
  
  	return 0;
  
  failed2:
  	sysfs_remove_link(&dev->dev.kobj, buf);
  failed1:
  	pci_dev_put(dev);
  	mutex_lock(&iov->dev->sriov->lock);
  	pci_remove_bus_device(virtfn);
  	virtfn_remove_bus(dev->bus, virtfn_bus(dev, id));
  	mutex_unlock(&iov->dev->sriov->lock);
  
  	return rc;
  }
  
  static void virtfn_remove(struct pci_dev *dev, int id, int reset)
  {
  	char buf[VIRTFN_ID_LEN];
  	struct pci_bus *bus;
  	struct pci_dev *virtfn;
  	struct pci_sriov *iov = dev->sriov;
  
  	bus = pci_find_bus(pci_domain_nr(dev->bus), virtfn_bus(dev, id));
  	if (!bus)
  		return;
  
  	virtfn = pci_get_slot(bus, virtfn_devfn(dev, id));
  	if (!virtfn)
  		return;
  
  	pci_dev_put(virtfn);
  
  	if (reset) {
  		device_release_driver(&virtfn->dev);
8c1c699fe   Yu Zhao   PCI: cleanup Func...
169
  		__pci_reset_function(virtfn);
dd7cc44d0   Yu Zhao   PCI: add SR-IOV A...
170
171
172
173
174
175
176
177
178
179
180
181
182
  	}
  
  	sprintf(buf, "virtfn%u", id);
  	sysfs_remove_link(&dev->dev.kobj, buf);
  	sysfs_remove_link(&virtfn->dev.kobj, "physfn");
  
  	mutex_lock(&iov->dev->sriov->lock);
  	pci_remove_bus_device(virtfn);
  	virtfn_remove_bus(dev->bus, virtfn_bus(dev, id));
  	mutex_unlock(&iov->dev->sriov->lock);
  
  	pci_dev_put(dev);
  }
74bb1bcc7   Yu Zhao   PCI: handle SR-IO...
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
  static int sriov_migration(struct pci_dev *dev)
  {
  	u16 status;
  	struct pci_sriov *iov = dev->sriov;
  
  	if (!iov->nr_virtfn)
  		return 0;
  
  	if (!(iov->cap & PCI_SRIOV_CAP_VFM))
  		return 0;
  
  	pci_read_config_word(dev, iov->pos + PCI_SRIOV_STATUS, &status);
  	if (!(status & PCI_SRIOV_STATUS_VFM))
  		return 0;
  
  	schedule_work(&iov->mtask);
  
  	return 1;
  }
  
  static void sriov_migration_task(struct work_struct *work)
  {
  	int i;
  	u8 state;
  	u16 status;
  	struct pci_sriov *iov = container_of(work, struct pci_sriov, mtask);
  
  	for (i = iov->initial; i < iov->nr_virtfn; i++) {
  		state = readb(iov->mstate + i);
  		if (state == PCI_SRIOV_VFM_MI) {
  			writeb(PCI_SRIOV_VFM_AV, iov->mstate + i);
  			state = readb(iov->mstate + i);
  			if (state == PCI_SRIOV_VFM_AV)
  				virtfn_add(iov->self, i, 1);
  		} else if (state == PCI_SRIOV_VFM_MO) {
  			virtfn_remove(iov->self, i, 1);
  			writeb(PCI_SRIOV_VFM_UA, iov->mstate + i);
  			state = readb(iov->mstate + i);
  			if (state == PCI_SRIOV_VFM_AV)
  				virtfn_add(iov->self, i, 0);
  		}
  	}
  
  	pci_read_config_word(iov->self, iov->pos + PCI_SRIOV_STATUS, &status);
  	status &= ~PCI_SRIOV_STATUS_VFM;
  	pci_write_config_word(iov->self, iov->pos + PCI_SRIOV_STATUS, status);
  }
  
  static int sriov_enable_migration(struct pci_dev *dev, int nr_virtfn)
  {
  	int bir;
  	u32 table;
  	resource_size_t pa;
  	struct pci_sriov *iov = dev->sriov;
  
  	if (nr_virtfn <= iov->initial)
  		return 0;
  
  	pci_read_config_dword(dev, iov->pos + PCI_SRIOV_VFM, &table);
  	bir = PCI_SRIOV_VFM_BIR(table);
  	if (bir > PCI_STD_RESOURCE_END)
  		return -EIO;
  
  	table = PCI_SRIOV_VFM_OFFSET(table);
  	if (table + nr_virtfn > pci_resource_len(dev, bir))
  		return -EIO;
  
  	pa = pci_resource_start(dev, bir) + table;
  	iov->mstate = ioremap(pa, nr_virtfn);
  	if (!iov->mstate)
  		return -ENOMEM;
  
  	INIT_WORK(&iov->mtask, sriov_migration_task);
  
  	iov->ctrl |= PCI_SRIOV_CTRL_VFM | PCI_SRIOV_CTRL_INTR;
  	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  
  	return 0;
  }
  
  static void sriov_disable_migration(struct pci_dev *dev)
  {
  	struct pci_sriov *iov = dev->sriov;
  
  	iov->ctrl &= ~(PCI_SRIOV_CTRL_VFM | PCI_SRIOV_CTRL_INTR);
  	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  
  	cancel_work_sync(&iov->mtask);
  	iounmap(iov->mstate);
  }
dd7cc44d0   Yu Zhao   PCI: add SR-IOV A...
273
274
275
276
277
278
279
280
281
  static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
  {
  	int rc;
  	int i, j;
  	int nres;
  	u16 offset, stride, initial;
  	struct resource *res;
  	struct pci_dev *pdev;
  	struct pci_sriov *iov = dev->sriov;
bbef98ab0   Ram Pai   PCI: defer enable...
282
  	int bars = 0;
dd7cc44d0   Yu Zhao   PCI: add SR-IOV A...
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
  
  	if (!nr_virtfn)
  		return 0;
  
  	if (iov->nr_virtfn)
  		return -EINVAL;
  
  	pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
  	if (initial > iov->total ||
  	    (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total)))
  		return -EIO;
  
  	if (nr_virtfn < 0 || nr_virtfn > iov->total ||
  	    (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
  		return -EINVAL;
  
  	pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
  	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &offset);
  	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &stride);
  	if (!offset || (nr_virtfn > 1 && !stride))
  		return -EIO;
  
  	nres = 0;
  	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
bbef98ab0   Ram Pai   PCI: defer enable...
307
  		bars |= (1 << (i + PCI_IOV_RESOURCES));
dd7cc44d0   Yu Zhao   PCI: add SR-IOV A...
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
  		res = dev->resource + PCI_IOV_RESOURCES + i;
  		if (res->parent)
  			nres++;
  	}
  	if (nres != iov->nres) {
  		dev_err(&dev->dev, "not enough MMIO resources for SR-IOV
  ");
  		return -ENOMEM;
  	}
  
  	iov->offset = offset;
  	iov->stride = stride;
  
  	if (virtfn_bus(dev, nr_virtfn - 1) > dev->bus->subordinate) {
  		dev_err(&dev->dev, "SR-IOV: bus number out of range
  ");
  		return -ENOMEM;
  	}
bbef98ab0   Ram Pai   PCI: defer enable...
326
327
328
329
330
  	if (pci_enable_resources(dev, bars)) {
  		dev_err(&dev->dev, "SR-IOV: IOV BARS not allocated
  ");
  		return -ENOMEM;
  	}
dd7cc44d0   Yu Zhao   PCI: add SR-IOV A...
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
  	if (iov->link != dev->devfn) {
  		pdev = pci_get_slot(dev->bus, iov->link);
  		if (!pdev)
  			return -ENODEV;
  
  		pci_dev_put(pdev);
  
  		if (!pdev->is_physfn)
  			return -ENODEV;
  
  		rc = sysfs_create_link(&dev->dev.kobj,
  					&pdev->dev.kobj, "dep_link");
  		if (rc)
  			return rc;
  	}
afd24ece5   Ram Pai   PCI: delay config...
346
  	pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
dd7cc44d0   Yu Zhao   PCI: add SR-IOV A...
347
  	iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
fb51ccbf2   Jan Kiszka   PCI: Rework confi...
348
  	pci_cfg_access_lock(dev);
dd7cc44d0   Yu Zhao   PCI: add SR-IOV A...
349
350
  	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  	msleep(100);
fb51ccbf2   Jan Kiszka   PCI: Rework confi...
351
  	pci_cfg_access_unlock(dev);
dd7cc44d0   Yu Zhao   PCI: add SR-IOV A...
352
353
354
355
356
357
358
359
360
361
  
  	iov->initial = initial;
  	if (nr_virtfn < initial)
  		initial = nr_virtfn;
  
  	for (i = 0; i < initial; i++) {
  		rc = virtfn_add(dev, i, 0);
  		if (rc)
  			goto failed;
  	}
74bb1bcc7   Yu Zhao   PCI: handle SR-IO...
362
363
364
365
366
  	if (iov->cap & PCI_SRIOV_CAP_VFM) {
  		rc = sriov_enable_migration(dev, nr_virtfn);
  		if (rc)
  			goto failed;
  	}
dd7cc44d0   Yu Zhao   PCI: add SR-IOV A...
367
368
369
370
371
372
373
374
375
376
  	kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
  	iov->nr_virtfn = nr_virtfn;
  
  	return 0;
  
  failed:
  	for (j = 0; j < i; j++)
  		virtfn_remove(dev, j, 0);
  
  	iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
fb51ccbf2   Jan Kiszka   PCI: Rework confi...
377
  	pci_cfg_access_lock(dev);
dd7cc44d0   Yu Zhao   PCI: add SR-IOV A...
378
379
  	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  	ssleep(1);
fb51ccbf2   Jan Kiszka   PCI: Rework confi...
380
  	pci_cfg_access_unlock(dev);
dd7cc44d0   Yu Zhao   PCI: add SR-IOV A...
381
382
383
384
385
386
387
388
389
390
391
392
393
394
  
  	if (iov->link != dev->devfn)
  		sysfs_remove_link(&dev->dev.kobj, "dep_link");
  
  	return rc;
  }
  
  static void sriov_disable(struct pci_dev *dev)
  {
  	int i;
  	struct pci_sriov *iov = dev->sriov;
  
  	if (!iov->nr_virtfn)
  		return;
74bb1bcc7   Yu Zhao   PCI: handle SR-IO...
395
396
  	if (iov->cap & PCI_SRIOV_CAP_VFM)
  		sriov_disable_migration(dev);
dd7cc44d0   Yu Zhao   PCI: add SR-IOV A...
397
398
399
400
  	for (i = 0; i < iov->nr_virtfn; i++)
  		virtfn_remove(dev, i, 0);
  
  	iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
fb51ccbf2   Jan Kiszka   PCI: Rework confi...
401
  	pci_cfg_access_lock(dev);
dd7cc44d0   Yu Zhao   PCI: add SR-IOV A...
402
403
  	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  	ssleep(1);
fb51ccbf2   Jan Kiszka   PCI: Rework confi...
404
  	pci_cfg_access_unlock(dev);
dd7cc44d0   Yu Zhao   PCI: add SR-IOV A...
405
406
407
408
409
410
  
  	if (iov->link != dev->devfn)
  		sysfs_remove_link(&dev->dev.kobj, "dep_link");
  
  	iov->nr_virtfn = 0;
  }
d1b054da8   Yu Zhao   PCI: initialize a...
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
  static int sriov_init(struct pci_dev *dev, int pos)
  {
  	int i;
  	int rc;
  	int nres;
  	u32 pgsz;
  	u16 ctrl, total, offset, stride;
  	struct pci_sriov *iov;
  	struct resource *res;
  	struct pci_dev *pdev;
  
  	if (dev->pcie_type != PCI_EXP_TYPE_RC_END &&
  	    dev->pcie_type != PCI_EXP_TYPE_ENDPOINT)
  		return -ENODEV;
  
  	pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
  	if (ctrl & PCI_SRIOV_CTRL_VFE) {
  		pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
  		ssleep(1);
  	}
  
  	pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
  	if (!total)
  		return 0;
  
  	ctrl = 0;
  	list_for_each_entry(pdev, &dev->bus->devices, bus_list)
  		if (pdev->is_physfn)
  			goto found;
  
  	pdev = NULL;
  	if (pci_ari_enabled(dev->bus))
  		ctrl |= PCI_SRIOV_CTRL_ARI;
  
  found:
  	pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
d1b054da8   Yu Zhao   PCI: initialize a...
447
448
449
450
451
452
453
454
455
456
457
458
  	pci_read_config_word(dev, pos + PCI_SRIOV_VF_OFFSET, &offset);
  	pci_read_config_word(dev, pos + PCI_SRIOV_VF_STRIDE, &stride);
  	if (!offset || (total > 1 && !stride))
  		return -EIO;
  
  	pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
  	i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
  	pgsz &= ~((1 << i) - 1);
  	if (!pgsz)
  		return -EIO;
  
  	pgsz &= ~(pgsz - 1);
d1b054da8   Yu Zhao   PCI: initialize a...
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
  
  	nres = 0;
  	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  		res = dev->resource + PCI_IOV_RESOURCES + i;
  		i += __pci_read_base(dev, pci_bar_unknown, res,
  				     pos + PCI_SRIOV_BAR + i * 4);
  		if (!res->flags)
  			continue;
  		if (resource_size(res) & (PAGE_SIZE - 1)) {
  			rc = -EIO;
  			goto failed;
  		}
  		res->end = res->start + resource_size(res) * total - 1;
  		nres++;
  	}
  
  	iov = kzalloc(sizeof(*iov), GFP_KERNEL);
  	if (!iov) {
  		rc = -ENOMEM;
  		goto failed;
  	}
  
  	iov->pos = pos;
  	iov->nres = nres;
  	iov->ctrl = ctrl;
  	iov->total = total;
  	iov->offset = offset;
  	iov->stride = stride;
  	iov->pgsz = pgsz;
  	iov->self = dev;
  	pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
  	pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
4d135dbee   Yu Zhao   PCI: fix SR-IOV f...
491
492
  	if (dev->pcie_type == PCI_EXP_TYPE_RC_END)
  		iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
d1b054da8   Yu Zhao   PCI: initialize a...
493
494
495
  
  	if (pdev)
  		iov->dev = pci_dev_get(pdev);
e277d2fc7   Yu Zhao   PCI: handle Virtu...
496
  	else
d1b054da8   Yu Zhao   PCI: initialize a...
497
  		iov->dev = dev;
e277d2fc7   Yu Zhao   PCI: handle Virtu...
498
499
  
  	mutex_init(&iov->lock);
d1b054da8   Yu Zhao   PCI: initialize a...
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
  
  	dev->sriov = iov;
  	dev->is_physfn = 1;
  
  	return 0;
  
  failed:
  	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  		res = dev->resource + PCI_IOV_RESOURCES + i;
  		res->flags = 0;
  	}
  
  	return rc;
  }
  
  static void sriov_release(struct pci_dev *dev)
  {
dd7cc44d0   Yu Zhao   PCI: add SR-IOV A...
517
  	BUG_ON(dev->sriov->nr_virtfn);
e277d2fc7   Yu Zhao   PCI: handle Virtu...
518
  	if (dev != dev->sriov->dev)
d1b054da8   Yu Zhao   PCI: initialize a...
519
  		pci_dev_put(dev->sriov->dev);
e277d2fc7   Yu Zhao   PCI: handle Virtu...
520
  	mutex_destroy(&dev->sriov->lock);
d1b054da8   Yu Zhao   PCI: initialize a...
521
522
523
  	kfree(dev->sriov);
  	dev->sriov = NULL;
  }
8c5cdb6ad   Yu Zhao   PCI: restore save...
524
525
526
527
528
529
530
531
532
533
534
535
536
537
  static void sriov_restore_state(struct pci_dev *dev)
  {
  	int i;
  	u16 ctrl;
  	struct pci_sriov *iov = dev->sriov;
  
  	pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
  	if (ctrl & PCI_SRIOV_CTRL_VFE)
  		return;
  
  	for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
  		pci_update_resource(dev, i);
  
  	pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
dd7cc44d0   Yu Zhao   PCI: add SR-IOV A...
538
  	pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, iov->nr_virtfn);
8c5cdb6ad   Yu Zhao   PCI: restore save...
539
540
541
542
  	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  	if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
  		msleep(100);
  }
d1b054da8   Yu Zhao   PCI: initialize a...
543
544
545
546
547
548
549
550
551
  /**
   * pci_iov_init - initialize the IOV capability
   * @dev: the PCI device
   *
   * Returns 0 on success, or negative on failure.
   */
  int pci_iov_init(struct pci_dev *dev)
  {
  	int pos;
5f4d91a12   Kenji Kaneshige   PCI: use pci_is_p...
552
  	if (!pci_is_pcie(dev))
d1b054da8   Yu Zhao   PCI: initialize a...
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
  		return -ENODEV;
  
  	pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
  	if (pos)
  		return sriov_init(dev, pos);
  
  	return -ENODEV;
  }
  
  /**
   * pci_iov_release - release resources used by the IOV capability
   * @dev: the PCI device
   */
  void pci_iov_release(struct pci_dev *dev)
  {
  	if (dev->is_physfn)
  		sriov_release(dev);
  }
  
  /**
   * pci_iov_resource_bar - get position of the SR-IOV BAR
   * @dev: the PCI device
   * @resno: the resource number
   * @type: the BAR type to be filled in
   *
   * Returns position of the BAR encapsulated in the SR-IOV capability.
   */
  int pci_iov_resource_bar(struct pci_dev *dev, int resno,
  			 enum pci_bar_type *type)
  {
  	if (resno < PCI_IOV_RESOURCES || resno > PCI_IOV_RESOURCE_END)
  		return 0;
  
  	BUG_ON(!dev->is_physfn);
  
  	*type = pci_bar_unknown;
  
  	return dev->sriov->pos + PCI_SRIOV_BAR +
  		4 * (resno - PCI_IOV_RESOURCES);
  }
8c5cdb6ad   Yu Zhao   PCI: restore save...
593
594
  
  /**
6faf17f6f   Chris Wright   PCI SR-IOV: corre...
595
596
597
598
599
600
601
602
603
   * pci_sriov_resource_alignment - get resource alignment for VF BAR
   * @dev: the PCI device
   * @resno: the resource number
   *
   * Returns the alignment of the VF BAR found in the SR-IOV capability.
   * This is not the same as the resource size which is defined as
   * the VF BAR size multiplied by the number of VFs.  The alignment
   * is just the VF BAR size.
   */
0e52247a2   Cam Macdonell   PCI: fix pci_reso...
604
  resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
6faf17f6f   Chris Wright   PCI SR-IOV: corre...
605
606
607
608
609
610
611
612
613
614
615
616
617
  {
  	struct resource tmp;
  	enum pci_bar_type type;
  	int reg = pci_iov_resource_bar(dev, resno, &type);
  	
  	if (!reg)
  		return 0;
  
  	 __pci_read_base(dev, type, &tmp, reg);
  	return resource_alignment(&tmp);
  }
  
  /**
8c5cdb6ad   Yu Zhao   PCI: restore save...
618
619
620
621
622
623
624
625
   * pci_restore_iov_state - restore the state of the IOV capability
   * @dev: the PCI device
   */
  void pci_restore_iov_state(struct pci_dev *dev)
  {
  	if (dev->is_physfn)
  		sriov_restore_state(dev);
  }
a28724b0f   Yu Zhao   PCI: reserve bus ...
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
  
  /**
   * pci_iov_bus_range - find bus range used by Virtual Function
   * @bus: the PCI bus
   *
   * Returns max number of buses (exclude current one) used by Virtual
   * Functions.
   */
  int pci_iov_bus_range(struct pci_bus *bus)
  {
  	int max = 0;
  	u8 busnr;
  	struct pci_dev *dev;
  
  	list_for_each_entry(dev, &bus->devices, bus_list) {
  		if (!dev->is_physfn)
  			continue;
  		busnr = virtfn_bus(dev, dev->sriov->total - 1);
  		if (busnr > max)
  			max = busnr;
  	}
  
  	return max ? max - bus->number : 0;
  }
dd7cc44d0   Yu Zhao   PCI: add SR-IOV A...
650
651
652
653
  
  /**
   * pci_enable_sriov - enable the SR-IOV capability
   * @dev: the PCI device
52a8873ba   Randy Dunlap   PCI-IOV: fix miss...
654
   * @nr_virtfn: number of virtual functions to enable
dd7cc44d0   Yu Zhao   PCI: add SR-IOV A...
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
   *
   * Returns 0 on success, or negative on failure.
   */
  int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
  {
  	might_sleep();
  
  	if (!dev->is_physfn)
  		return -ENODEV;
  
  	return sriov_enable(dev, nr_virtfn);
  }
  EXPORT_SYMBOL_GPL(pci_enable_sriov);
  
  /**
   * pci_disable_sriov - disable the SR-IOV capability
   * @dev: the PCI device
   */
  void pci_disable_sriov(struct pci_dev *dev)
  {
  	might_sleep();
  
  	if (!dev->is_physfn)
  		return;
  
  	sriov_disable(dev);
  }
  EXPORT_SYMBOL_GPL(pci_disable_sriov);
74bb1bcc7   Yu Zhao   PCI: handle SR-IO...
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
  
  /**
   * pci_sriov_migration - notify SR-IOV core of Virtual Function Migration
   * @dev: the PCI device
   *
   * Returns IRQ_HANDLED if the IRQ is handled, or IRQ_NONE if not.
   *
   * Physical Function driver is responsible to register IRQ handler using
   * VF Migration Interrupt Message Number, and call this function when the
   * interrupt is generated by the hardware.
   */
  irqreturn_t pci_sriov_migration(struct pci_dev *dev)
  {
  	if (!dev->is_physfn)
  		return IRQ_NONE;
  
  	return sriov_migration(dev) ? IRQ_HANDLED : IRQ_NONE;
  }
  EXPORT_SYMBOL_GPL(pci_sriov_migration);
302b4215d   Yu Zhao   PCI: support the ...
702

fb8a0d9d1   Williams, Mitch A   pci: Add SR-IOV c...
703
704
705
706
707
708
709
710
711
712
713
714
715
716
  /**
   * pci_num_vf - return number of VFs associated with a PF device_release_driver
   * @dev: the PCI device
   *
   * Returns number of VFs, or 0 if SR-IOV is not enabled.
   */
  int pci_num_vf(struct pci_dev *dev)
  {
  	if (!dev || !dev->is_physfn)
  		return 0;
  	else
  		return dev->sriov->nr_virtfn;
  }
  EXPORT_SYMBOL_GPL(pci_num_vf);