blob: 9e480fdebe1f059419e8864c473e3c6ee2f02cb4 [file] [log] [blame]
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001/*
2 * PCI Stub Driver - Grabs devices in backend to be exported later
3 *
4 * Ryan Wilson <hap9@epoch.ncsc.mil>
5 * Chris Bookholt <hap10@epoch.ncsc.mil>
6 */
Joe Perches283c0972013-06-28 03:21:41 -07007
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040010#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/rwsem.h>
13#include <linux/list.h>
14#include <linux/spinlock.h>
15#include <linux/kref.h>
16#include <linux/pci.h>
17#include <linux/wait.h>
18#include <linux/sched.h>
Konrad Rzeszutek Wilk8bfd4e02011-07-19 20:09:43 -040019#include <linux/atomic.h>
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040020#include <xen/events.h>
21#include <asm/xen/pci.h>
22#include <asm/xen/hypervisor.h>
Jan Beulich909b3fd2013-03-12 15:06:23 +000023#include <xen/interface/physdev.h>
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040024#include "pciback.h"
25#include "conf_space.h"
26#include "conf_space_quirks.h"
27
Juergen Grossb0578782016-09-22 10:45:41 +020028#define PCISTUB_DRIVER_NAME "pciback"
29
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040030static char *pci_devs_to_hide;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040031wait_queue_head_t xen_pcibk_aer_wait_queue;
32/*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops,
33* We want to avoid in middle of AER ops, xen_pcibk devices is being removed
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040034*/
35static DECLARE_RWSEM(pcistub_sem);
36module_param_named(hide, pci_devs_to_hide, charp, 0444);
37
38struct pcistub_device_id {
39 struct list_head slot_list;
40 int domain;
41 unsigned char bus;
42 unsigned int devfn;
43};
44static LIST_HEAD(pcistub_device_ids);
45static DEFINE_SPINLOCK(device_ids_lock);
46
47struct pcistub_device {
48 struct kref kref;
49 struct list_head dev_list;
50 spinlock_t lock;
51
52 struct pci_dev *dev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040053 struct xen_pcibk_device *pdev;/* non-NULL if struct pci_dev is in use */
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040054};
55
56/* Access to pcistub_devices & seized_devices lists and the initialize_devices
57 * flag must be locked with pcistub_devices_lock
58 */
59static DEFINE_SPINLOCK(pcistub_devices_lock);
60static LIST_HEAD(pcistub_devices);
61
62/* wait for device_initcall before initializing our devices
63 * (see pcistub_init_devices_late)
64 */
65static int initialize_devices;
66static LIST_HEAD(seized_devices);
67
68static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
69{
70 struct pcistub_device *psdev;
71
72 dev_dbg(&dev->dev, "pcistub_device_alloc\n");
73
74 psdev = kzalloc(sizeof(*psdev), GFP_ATOMIC);
75 if (!psdev)
76 return NULL;
77
78 psdev->dev = pci_dev_get(dev);
79 if (!psdev->dev) {
80 kfree(psdev);
81 return NULL;
82 }
83
84 kref_init(&psdev->kref);
85 spin_lock_init(&psdev->lock);
86
87 return psdev;
88}
89
90/* Don't call this directly as it's called by pcistub_device_put */
91static void pcistub_device_release(struct kref *kref)
92{
93 struct pcistub_device *psdev;
Jan Beulich909b3fd2013-03-12 15:06:23 +000094 struct pci_dev *dev;
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -050095 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040096
97 psdev = container_of(kref, struct pcistub_device, kref);
Jan Beulich909b3fd2013-03-12 15:06:23 +000098 dev = psdev->dev;
99 dev_data = pci_get_drvdata(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400100
Jan Beulich909b3fd2013-03-12 15:06:23 +0000101 dev_dbg(&dev->dev, "pcistub_device_release\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400102
Jan Beulich909b3fd2013-03-12 15:06:23 +0000103 xen_unregister_device_domain_owner(dev);
Konrad Rzeszutek Wilk6221a9b2009-12-09 17:43:15 -0500104
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500105 /* Call the reset function which does not take lock as this
106 * is called from "unbind" which takes a device_lock mutex.
107 */
Jan Beulich909b3fd2013-03-12 15:06:23 +0000108 __pci_reset_function_locked(dev);
109 if (pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state))
Konrad Rzeszutek Wilkb1df4a52014-12-03 16:40:32 -0500110 dev_info(&dev->dev, "Could not reload PCI state\n");
Jan Beulich909b3fd2013-03-12 15:06:23 +0000111 else
112 pci_restore_state(dev);
113
Jan Beulichd69c0e32013-05-29 13:31:15 +0100114 if (dev->msix_cap) {
Jan Beulich909b3fd2013-03-12 15:06:23 +0000115 struct physdev_pci_device ppdev = {
116 .seg = pci_domain_nr(dev->bus),
117 .bus = dev->bus->number,
118 .devfn = dev->devfn
119 };
120 int err = HYPERVISOR_physdev_op(PHYSDEVOP_release_msix,
121 &ppdev);
122
Konrad Rzeszutek Wilk74beaf62015-02-27 16:11:06 -0500123 if (err && err != -ENOSYS)
Jan Beulich909b3fd2013-03-12 15:06:23 +0000124 dev_warn(&dev->dev, "MSI-X release failed (%d)\n",
125 err);
126 }
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500127
128 /* Disable the device */
Jan Beulich909b3fd2013-03-12 15:06:23 +0000129 xen_pcibk_reset_device(dev);
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500130
131 kfree(dev_data);
Jan Beulich909b3fd2013-03-12 15:06:23 +0000132 pci_set_drvdata(dev, NULL);
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500133
134 /* Clean-up the device */
Jan Beulich909b3fd2013-03-12 15:06:23 +0000135 xen_pcibk_config_free_dyn_fields(dev);
136 xen_pcibk_config_free_dev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400137
Ethan Zhaobe507fd2014-09-09 10:21:27 +0800138 pci_clear_dev_assigned(dev);
Jan Beulich909b3fd2013-03-12 15:06:23 +0000139 pci_dev_put(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400140
141 kfree(psdev);
142}
143
144static inline void pcistub_device_get(struct pcistub_device *psdev)
145{
146 kref_get(&psdev->kref);
147}
148
149static inline void pcistub_device_put(struct pcistub_device *psdev)
150{
151 kref_put(&psdev->kref, pcistub_device_release);
152}
153
Juergen Gross1af916b2016-09-22 10:45:39 +0200154static struct pcistub_device *pcistub_device_find_locked(int domain, int bus,
155 int slot, int func)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400156{
Juergen Gross1af916b2016-09-22 10:45:39 +0200157 struct pcistub_device *psdev;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400158
159 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
160 if (psdev->dev != NULL
161 && domain == pci_domain_nr(psdev->dev->bus)
162 && bus == psdev->dev->bus->number
Jan Beulichb3e40b72012-11-02 14:37:13 +0000163 && slot == PCI_SLOT(psdev->dev->devfn)
164 && func == PCI_FUNC(psdev->dev->devfn)) {
Juergen Gross1af916b2016-09-22 10:45:39 +0200165 return psdev;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400166 }
167 }
168
Juergen Gross1af916b2016-09-22 10:45:39 +0200169 return NULL;
170}
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400171
Juergen Gross1af916b2016-09-22 10:45:39 +0200172static struct pcistub_device *pcistub_device_find(int domain, int bus,
173 int slot, int func)
174{
175 struct pcistub_device *psdev;
176 unsigned long flags;
177
178 spin_lock_irqsave(&pcistub_devices_lock, flags);
179
180 psdev = pcistub_device_find_locked(domain, bus, slot, func);
181 if (psdev)
182 pcistub_device_get(psdev);
183
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400184 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
185 return psdev;
186}
187
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400188static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400189 struct pcistub_device *psdev)
190{
191 struct pci_dev *pci_dev = NULL;
192 unsigned long flags;
193
194 pcistub_device_get(psdev);
195
196 spin_lock_irqsave(&psdev->lock, flags);
197 if (!psdev->pdev) {
198 psdev->pdev = pdev;
199 pci_dev = psdev->dev;
200 }
201 spin_unlock_irqrestore(&psdev->lock, flags);
202
203 if (!pci_dev)
204 pcistub_device_put(psdev);
205
206 return pci_dev;
207}
208
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400209struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400210 int domain, int bus,
211 int slot, int func)
212{
213 struct pcistub_device *psdev;
214 struct pci_dev *found_dev = NULL;
215 unsigned long flags;
216
217 spin_lock_irqsave(&pcistub_devices_lock, flags);
218
Juergen Gross1af916b2016-09-22 10:45:39 +0200219 psdev = pcistub_device_find_locked(domain, bus, slot, func);
220 if (psdev)
221 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400222
223 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
224 return found_dev;
225}
226
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400227struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400228 struct pci_dev *dev)
229{
230 struct pcistub_device *psdev;
231 struct pci_dev *found_dev = NULL;
232 unsigned long flags;
233
234 spin_lock_irqsave(&pcistub_devices_lock, flags);
235
236 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
237 if (psdev->dev == dev) {
238 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
239 break;
240 }
241 }
242
243 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
244 return found_dev;
245}
246
Konrad Rzeszutek Wilk0a9fd012014-04-22 10:48:17 -0400247/*
248 * Called when:
249 * - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device
250 * - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove
251 * - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove
252 * - 'echo BDF > unbind' with a guest still using it. See pcistub_remove
253 *
254 * As such we have to be careful.
Konrad Rzeszutek Wilke8801a72014-12-03 16:40:26 -0500255 *
256 * To make this easier, the caller has to hold the device lock.
Konrad Rzeszutek Wilk0a9fd012014-04-22 10:48:17 -0400257 */
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400258void pcistub_put_pci_dev(struct pci_dev *dev)
259{
260 struct pcistub_device *psdev, *found_psdev = NULL;
261 unsigned long flags;
Konrad Rzeszutek Wilkb1df4a52014-12-03 16:40:32 -0500262 struct xen_pcibk_dev_data *dev_data;
263 int ret;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400264
265 spin_lock_irqsave(&pcistub_devices_lock, flags);
266
267 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
268 if (psdev->dev == dev) {
269 found_psdev = psdev;
270 break;
271 }
272 }
273
274 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
Konrad Rzeszutek Wilk4645bf32011-09-29 13:12:43 -0400275 if (WARN_ON(!found_psdev))
276 return;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400277
278 /*hold this lock for avoiding breaking link between
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400279 * pcistub and xen_pcibk when AER is in processing
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400280 */
281 down_write(&pcistub_sem);
282 /* Cleanup our device
283 * (so it's ready for the next domain)
284 */
Konrad Rzeszutek Wilkac801022014-12-03 16:40:27 -0500285 device_lock_assert(&dev->dev);
Konrad Rzeszutek Wilke8801a72014-12-03 16:40:26 -0500286 __pci_reset_function_locked(dev);
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500287
Konrad Rzeszutek Wilkb1df4a52014-12-03 16:40:32 -0500288 dev_data = pci_get_drvdata(dev);
289 ret = pci_load_saved_state(dev, dev_data->pci_saved_state);
290 if (!ret) {
291 /*
292 * The usual sequence is pci_save_state & pci_restore_state
293 * but the guest might have messed the configuration space up.
294 * Use the initial version (when device was bound to us).
295 */
296 pci_restore_state(dev);
297 } else
298 dev_info(&dev->dev, "Could not reload PCI state\n");
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500299 /* This disables the device. */
Konrad Rzeszutek Wilk88990352013-12-03 21:34:03 -0500300 xen_pcibk_reset_device(dev);
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500301
302 /* And cleanup up our emulated fields. */
Konrad Rzeszutek Wilk88990352013-12-03 21:34:03 -0500303 xen_pcibk_config_reset_dev(dev);
Konrad Rzeszutek Wilkfcb8ce92013-12-03 21:37:24 -0500304 xen_pcibk_config_free_dyn_fields(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400305
Konrad Rzeszutek Wilk88990352013-12-03 21:34:03 -0500306 xen_unregister_device_domain_owner(dev);
Konrad Rzeszutek Wilk31673552012-01-04 15:11:02 -0500307
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400308 spin_lock_irqsave(&found_psdev->lock, flags);
309 found_psdev->pdev = NULL;
310 spin_unlock_irqrestore(&found_psdev->lock, flags);
311
312 pcistub_device_put(found_psdev);
313 up_write(&pcistub_sem);
314}
315
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800316static int pcistub_match_one(struct pci_dev *dev,
317 struct pcistub_device_id *pdev_id)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400318{
319 /* Match the specified device by domain, bus, slot, func and also if
320 * any of the device's parent bridges match.
321 */
322 for (; dev != NULL; dev = dev->bus->self) {
323 if (pci_domain_nr(dev->bus) == pdev_id->domain
324 && dev->bus->number == pdev_id->bus
325 && dev->devfn == pdev_id->devfn)
326 return 1;
327
328 /* Sometimes topmost bridge links to itself. */
329 if (dev == dev->bus->self)
330 break;
331 }
332
333 return 0;
334}
335
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800336static int pcistub_match(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400337{
338 struct pcistub_device_id *pdev_id;
339 unsigned long flags;
340 int found = 0;
341
342 spin_lock_irqsave(&device_ids_lock, flags);
343 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
344 if (pcistub_match_one(dev, pdev_id)) {
345 found = 1;
346 break;
347 }
348 }
349 spin_unlock_irqrestore(&device_ids_lock, flags);
350
351 return found;
352}
353
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800354static int pcistub_init_device(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400355{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400356 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400357 int err = 0;
358
359 dev_dbg(&dev->dev, "initializing...\n");
360
361 /* The PCI backend is not intended to be a module (or to work with
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400362 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400363 * would need to be called somewhere to free the memory allocated
364 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
365 */
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400366 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
367 + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400368 if (!dev_data) {
369 err = -ENOMEM;
370 goto out;
371 }
372 pci_set_drvdata(dev, dev_data);
373
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400374 /*
375 * Setup name for fake IRQ handler. It will only be enabled
376 * once the device is turned on by the guest.
377 */
378 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
379
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400380 dev_dbg(&dev->dev, "initializing config\n");
381
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400382 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
383 err = xen_pcibk_config_init_dev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400384 if (err)
385 goto out;
386
387 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
388 * must do this here because pcibios_enable_device may specify
389 * the pci device's true irq (and possibly its other resources)
390 * if they differ from what's in the configuration space.
391 * This makes the assumption that the device's resources won't
392 * change after this point (otherwise this code may break!)
393 */
394 dev_dbg(&dev->dev, "enabling device\n");
395 err = pci_enable_device(dev);
396 if (err)
397 goto config_release;
398
Jan Beulichd69c0e32013-05-29 13:31:15 +0100399 if (dev->msix_cap) {
Jan Beulich909b3fd2013-03-12 15:06:23 +0000400 struct physdev_pci_device ppdev = {
401 .seg = pci_domain_nr(dev->bus),
402 .bus = dev->bus->number,
403 .devfn = dev->devfn
404 };
405
406 err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev);
Konrad Rzeszutek Wilk74beaf62015-02-27 16:11:06 -0500407 if (err && err != -ENOSYS)
Jan Beulich909b3fd2013-03-12 15:06:23 +0000408 dev_err(&dev->dev, "MSI-X preparation failed (%d)\n",
409 err);
410 }
411
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500412 /* We need the device active to save the state. */
413 dev_dbg(&dev->dev, "save state of device\n");
414 pci_save_state(dev);
415 dev_data->pci_saved_state = pci_store_saved_state(dev);
416 if (!dev_data->pci_saved_state)
417 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
Konrad Rzeszutek Wilk80ba77d2012-09-05 16:35:20 -0400418 else {
Masanari Iida744627e92012-11-05 23:30:40 +0900419 dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
Konrad Rzeszutek Wilk80ba77d2012-09-05 16:35:20 -0400420 __pci_reset_function_locked(dev);
Konrad Rzeszutek Wilkc341ca42012-09-25 16:48:24 -0400421 pci_restore_state(dev);
Konrad Rzeszutek Wilk80ba77d2012-09-05 16:35:20 -0400422 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400423 /* Now disable the device (this also ensures some private device
424 * data is setup before we export)
425 */
426 dev_dbg(&dev->dev, "reset device\n");
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400427 xen_pcibk_reset_device(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400428
Ethan Zhaobe507fd2014-09-09 10:21:27 +0800429 pci_set_dev_assigned(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400430 return 0;
431
432config_release:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400433 xen_pcibk_config_free_dev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400434
435out:
436 pci_set_drvdata(dev, NULL);
437 kfree(dev_data);
438 return err;
439}
440
441/*
442 * Because some initialization still happens on
443 * devices during fs_initcall, we need to defer
444 * full initialization of our devices until
445 * device_initcall.
446 */
447static int __init pcistub_init_devices_late(void)
448{
449 struct pcistub_device *psdev;
450 unsigned long flags;
451 int err = 0;
452
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400453 spin_lock_irqsave(&pcistub_devices_lock, flags);
454
455 while (!list_empty(&seized_devices)) {
456 psdev = container_of(seized_devices.next,
457 struct pcistub_device, dev_list);
458 list_del(&psdev->dev_list);
459
460 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
461
462 err = pcistub_init_device(psdev->dev);
463 if (err) {
464 dev_err(&psdev->dev->dev,
465 "error %d initializing device\n", err);
466 kfree(psdev);
467 psdev = NULL;
468 }
469
470 spin_lock_irqsave(&pcistub_devices_lock, flags);
471
472 if (psdev)
473 list_add_tail(&psdev->dev_list, &pcistub_devices);
474 }
475
476 initialize_devices = 1;
477
478 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
479
480 return 0;
481}
482
Juergen Gross9f8bee92016-09-22 10:45:40 +0200483static void pcistub_device_id_add_list(struct pcistub_device_id *new,
484 int domain, int bus, unsigned int devfn)
485{
486 struct pcistub_device_id *pci_dev_id;
487 unsigned long flags;
488 int found = 0;
489
490 spin_lock_irqsave(&device_ids_lock, flags);
491
492 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
493 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus &&
494 pci_dev_id->devfn == devfn) {
495 found = 1;
496 break;
497 }
498 }
499
500 if (!found) {
501 new->domain = domain;
502 new->bus = bus;
503 new->devfn = devfn;
504 list_add_tail(&new->slot_list, &pcistub_device_ids);
505 }
506
507 spin_unlock_irqrestore(&device_ids_lock, flags);
508
509 if (found)
510 kfree(new);
511}
512
Juergen Grossb0578782016-09-22 10:45:41 +0200513static int pcistub_seize(struct pci_dev *dev,
514 struct pcistub_device_id *pci_dev_id)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400515{
516 struct pcistub_device *psdev;
517 unsigned long flags;
518 int err = 0;
519
520 psdev = pcistub_device_alloc(dev);
Juergen Grossb0578782016-09-22 10:45:41 +0200521 if (!psdev) {
522 kfree(pci_dev_id);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400523 return -ENOMEM;
Juergen Grossb0578782016-09-22 10:45:41 +0200524 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400525
526 spin_lock_irqsave(&pcistub_devices_lock, flags);
527
528 if (initialize_devices) {
529 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
530
531 /* don't want irqs disabled when calling pcistub_init_device */
532 err = pcistub_init_device(psdev->dev);
533
534 spin_lock_irqsave(&pcistub_devices_lock, flags);
535
536 if (!err)
537 list_add(&psdev->dev_list, &pcistub_devices);
538 } else {
539 dev_dbg(&dev->dev, "deferring initialization\n");
540 list_add(&psdev->dev_list, &seized_devices);
541 }
542
543 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
544
Juergen Grossb0578782016-09-22 10:45:41 +0200545 if (err) {
546 kfree(pci_dev_id);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400547 pcistub_device_put(psdev);
Juergen Grossb0578782016-09-22 10:45:41 +0200548 } else if (pci_dev_id)
549 pcistub_device_id_add_list(pci_dev_id, pci_domain_nr(dev->bus),
550 dev->bus->number, dev->devfn);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400551
552 return err;
553}
554
Konrad Rzeszutek Wilk24d8bf12014-04-21 15:43:08 -0400555/* Called when 'bind'. This means we must _NOT_ call pci_reset_function or
556 * other functions that take the sysfs lock. */
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800557static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400558{
Juergen Grossb0578782016-09-22 10:45:41 +0200559 int err = 0, match;
560 struct pcistub_device_id *pci_dev_id = NULL;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400561
562 dev_dbg(&dev->dev, "probing...\n");
563
Juergen Grossb0578782016-09-22 10:45:41 +0200564 match = pcistub_match(dev);
565
566 if ((dev->driver_override &&
567 !strcmp(dev->driver_override, PCISTUB_DRIVER_NAME)) ||
568 match) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400569
570 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
571 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
572 dev_err(&dev->dev, "can't export pci devices that "
573 "don't have a normal (0) or bridge (1) "
574 "header type!\n");
575 err = -ENODEV;
576 goto out;
577 }
578
Juergen Grossb0578782016-09-22 10:45:41 +0200579 if (!match) {
580 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_ATOMIC);
581 if (!pci_dev_id) {
582 err = -ENOMEM;
583 goto out;
584 }
585 }
586
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400587 dev_info(&dev->dev, "seizing device\n");
Juergen Grossb0578782016-09-22 10:45:41 +0200588 err = pcistub_seize(dev, pci_dev_id);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400589 } else
590 /* Didn't find the device */
591 err = -ENODEV;
592
593out:
594 return err;
595}
596
Konrad Rzeszutek Wilk24d8bf12014-04-21 15:43:08 -0400597/* Called when 'unbind'. This means we must _NOT_ call pci_reset_function or
598 * other functions that take the sysfs lock. */
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400599static void pcistub_remove(struct pci_dev *dev)
600{
601 struct pcistub_device *psdev, *found_psdev = NULL;
602 unsigned long flags;
603
604 dev_dbg(&dev->dev, "removing\n");
605
606 spin_lock_irqsave(&pcistub_devices_lock, flags);
607
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400608 xen_pcibk_config_quirk_release(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400609
610 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
611 if (psdev->dev == dev) {
612 found_psdev = psdev;
613 break;
614 }
615 }
616
617 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
618
619 if (found_psdev) {
Konrad Rzeszutek Wilkb5d51212014-12-03 16:40:28 -0500620 dev_dbg(&dev->dev, "found device to remove %s\n",
621 found_psdev->pdev ? "- in-use" : "");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400622
623 if (found_psdev->pdev) {
Konrad Rzeszutek Wilkb5d51212014-12-03 16:40:28 -0500624 int domid = xen_find_device_domain_owner(dev);
625
626 pr_warn("****** removing device %s while still in-use by domain %d! ******\n",
627 pci_name(found_psdev->dev), domid);
Joe Perches283c0972013-06-28 03:21:41 -0700628 pr_warn("****** driver domain may still access this device's i/o resources!\n");
629 pr_warn("****** shutdown driver domain before binding device\n");
630 pr_warn("****** to other drivers or domains\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400631
Konrad Rzeszutek Wilk8be9df62013-12-03 21:47:37 -0500632 /* N.B. This ends up calling pcistub_put_pci_dev which ends up
633 * doing the FLR. */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400634 xen_pcibk_release_pci_dev(found_psdev->pdev,
Konrad Rzeszutek Wilke8801a72014-12-03 16:40:26 -0500635 found_psdev->dev,
636 false /* caller holds the lock. */);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400637 }
638
639 spin_lock_irqsave(&pcistub_devices_lock, flags);
640 list_del(&found_psdev->dev_list);
641 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
642
643 /* the final put for releasing from the list */
644 pcistub_device_put(found_psdev);
645 }
646}
647
Benoit Taine9baa3c32014-08-08 15:56:03 +0200648static const struct pci_device_id pcistub_ids[] = {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400649 {
650 .vendor = PCI_ANY_ID,
651 .device = PCI_ANY_ID,
652 .subvendor = PCI_ANY_ID,
653 .subdevice = PCI_ANY_ID,
654 },
655 {0,},
656};
657
658#define PCI_NODENAME_MAX 40
659static void kill_domain_by_device(struct pcistub_device *psdev)
660{
661 struct xenbus_transaction xbt;
662 int err;
663 char nodename[PCI_NODENAME_MAX];
664
Konrad Rzeszutek Wilk72bf8092011-09-29 13:43:28 -0400665 BUG_ON(!psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400666 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
667 psdev->pdev->xdev->otherend_id);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400668
669again:
670 err = xenbus_transaction_start(&xbt);
671 if (err) {
672 dev_err(&psdev->dev->dev,
673 "error %d when start xenbus transaction\n", err);
674 return;
675 }
676 /*PV AER handlers will set this flag*/
677 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
678 err = xenbus_transaction_end(xbt, 0);
679 if (err) {
680 if (err == -EAGAIN)
681 goto again;
682 dev_err(&psdev->dev->dev,
683 "error %d when end xenbus transaction\n", err);
684 return;
685 }
686}
687
688/* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400689 * backend need to have cooperation. In xen_pcibk, those steps will do similar
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400690 * jobs: send service request and waiting for front_end response.
691*/
692static pci_ers_result_t common_process(struct pcistub_device *psdev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400693 pci_channel_state_t state, int aer_cmd,
694 pci_ers_result_t result)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400695{
696 pci_ers_result_t res = result;
697 struct xen_pcie_aer_op *aer_op;
Konrad Rzeszutek Wilkc1a04332014-12-03 16:40:30 -0500698 struct xen_pcibk_device *pdev = psdev->pdev;
699 struct xen_pci_sharedinfo *sh_info = pdev->sh_info;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400700 int ret;
701
702 /*with PV AER drivers*/
Konrad Rzeszutek Wilkc1a04332014-12-03 16:40:30 -0500703 aer_op = &(sh_info->aer_op);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400704 aer_op->cmd = aer_cmd ;
705 /*useful for error_detected callback*/
706 aer_op->err = state;
707 /*pcifront_end BDF*/
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400708 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400709 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
710 if (!ret) {
711 dev_err(&psdev->dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400712 DRV_NAME ": failed to get pcifront device\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400713 return PCI_ERS_RESULT_NONE;
714 }
715 wmb();
716
717 dev_dbg(&psdev->dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400718 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400719 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400720 /*local flag to mark there's aer request, xen_pcibk callback will use
721 * this flag to judge whether we need to check pci-front give aer
722 * service ack signal
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400723 */
Konrad Rzeszutek Wilkc1a04332014-12-03 16:40:30 -0500724 set_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400725
726 /*It is possible that a pcifront conf_read_write ops request invokes
727 * the callback which cause the spurious execution of wake_up.
728 * Yet it is harmless and better than a spinlock here
729 */
730 set_bit(_XEN_PCIB_active,
Konrad Rzeszutek Wilkc1a04332014-12-03 16:40:30 -0500731 (unsigned long *)&sh_info->flags);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400732 wmb();
Konrad Rzeszutek Wilkc1a04332014-12-03 16:40:30 -0500733 notify_remote_via_irq(pdev->evtchn_irq);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400734
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400735 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
736 !(test_bit(_XEN_PCIB_active, (unsigned long *)
Konrad Rzeszutek Wilkc1a04332014-12-03 16:40:30 -0500737 &sh_info->flags)), 300*HZ);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400738
739 if (!ret) {
740 if (test_bit(_XEN_PCIB_active,
Konrad Rzeszutek Wilkc1a04332014-12-03 16:40:30 -0500741 (unsigned long *)&sh_info->flags)) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400742 dev_err(&psdev->dev->dev,
743 "pcifront aer process not responding!\n");
744 clear_bit(_XEN_PCIB_active,
Konrad Rzeszutek Wilkc1a04332014-12-03 16:40:30 -0500745 (unsigned long *)&sh_info->flags);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400746 aer_op->err = PCI_ERS_RESULT_NONE;
747 return res;
748 }
749 }
Konrad Rzeszutek Wilkc1a04332014-12-03 16:40:30 -0500750 clear_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400751
752 if (test_bit(_XEN_PCIF_active,
Konrad Rzeszutek Wilkc1a04332014-12-03 16:40:30 -0500753 (unsigned long *)&sh_info->flags)) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400754 dev_dbg(&psdev->dev->dev,
Jan Beulich402c5e12011-09-21 16:22:11 -0400755 "schedule pci_conf service in " DRV_NAME "\n");
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400756 xen_pcibk_test_and_schedule_op(psdev->pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400757 }
758
759 res = (pci_ers_result_t)aer_op->err;
760 return res;
761}
762
763/*
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400764* xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400765* of the device driver could provide this service, and then wait for pcifront
766* ack.
767* @dev: pointer to PCI devices
768* return value is used by aer_core do_recovery policy
769*/
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400770static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400771{
772 struct pcistub_device *psdev;
773 pci_ers_result_t result;
774
775 result = PCI_ERS_RESULT_RECOVERED;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400776 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400777 dev->bus->number, dev->devfn);
778
779 down_write(&pcistub_sem);
780 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
781 dev->bus->number,
782 PCI_SLOT(dev->devfn),
783 PCI_FUNC(dev->devfn));
784
785 if (!psdev || !psdev->pdev) {
786 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400787 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400788 goto end;
789 }
790
791 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400792 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400793 " by HVM, kill it\n");
794 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100795 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400796 }
797
798 if (!test_bit(_XEN_PCIB_AERHANDLER,
799 (unsigned long *)&psdev->pdev->sh_info->flags)) {
800 dev_err(&dev->dev,
801 "guest with no AER driver should have been killed\n");
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100802 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400803 }
804 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
805
806 if (result == PCI_ERS_RESULT_NONE ||
807 result == PCI_ERS_RESULT_DISCONNECT) {
808 dev_dbg(&dev->dev,
809 "No AER slot_reset service or disconnected!\n");
810 kill_domain_by_device(psdev);
811 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400812end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100813 if (psdev)
814 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400815 up_write(&pcistub_sem);
816 return result;
817
818}
819
820
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400821/*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400822* in case of the device driver could provide this service, and then wait
823* for pcifront ack
824* @dev: pointer to PCI devices
825* return value is used by aer_core do_recovery policy
826*/
827
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400828static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400829{
830 struct pcistub_device *psdev;
831 pci_ers_result_t result;
832
833 result = PCI_ERS_RESULT_RECOVERED;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400834 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400835 dev->bus->number, dev->devfn);
836
837 down_write(&pcistub_sem);
838 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
839 dev->bus->number,
840 PCI_SLOT(dev->devfn),
841 PCI_FUNC(dev->devfn));
842
843 if (!psdev || !psdev->pdev) {
844 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400845 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400846 goto end;
847 }
848
849 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400850 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400851 " by HVM, kill it\n");
852 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100853 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400854 }
855
856 if (!test_bit(_XEN_PCIB_AERHANDLER,
857 (unsigned long *)&psdev->pdev->sh_info->flags)) {
858 dev_err(&dev->dev,
859 "guest with no AER driver should have been killed\n");
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100860 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400861 }
862 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
863
864 if (result == PCI_ERS_RESULT_NONE ||
865 result == PCI_ERS_RESULT_DISCONNECT) {
866 dev_dbg(&dev->dev,
867 "No AER mmio_enabled service or disconnected!\n");
868 kill_domain_by_device(psdev);
869 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400870end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100871 if (psdev)
872 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400873 up_write(&pcistub_sem);
874 return result;
875}
876
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400877/*xen_pcibk_error_detected: it will send the error_detected request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400878* in case of the device driver could provide this service, and then wait
879* for pcifront ack.
880* @dev: pointer to PCI devices
881* @error: the current PCI connection state
882* return value is used by aer_core do_recovery policy
883*/
884
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400885static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400886 pci_channel_state_t error)
887{
888 struct pcistub_device *psdev;
889 pci_ers_result_t result;
890
891 result = PCI_ERS_RESULT_CAN_RECOVER;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400892 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400893 dev->bus->number, dev->devfn);
894
895 down_write(&pcistub_sem);
896 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
897 dev->bus->number,
898 PCI_SLOT(dev->devfn),
899 PCI_FUNC(dev->devfn));
900
901 if (!psdev || !psdev->pdev) {
902 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400903 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400904 goto end;
905 }
906
907 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400908 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400909 " by HVM, kill it\n");
910 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100911 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400912 }
913
914 /*Guest owns the device yet no aer handler regiested, kill guest*/
915 if (!test_bit(_XEN_PCIB_AERHANDLER,
916 (unsigned long *)&psdev->pdev->sh_info->flags)) {
917 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
918 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100919 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400920 }
921 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
922
923 if (result == PCI_ERS_RESULT_NONE ||
924 result == PCI_ERS_RESULT_DISCONNECT) {
925 dev_dbg(&dev->dev,
926 "No AER error_detected service or disconnected!\n");
927 kill_domain_by_device(psdev);
928 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400929end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100930 if (psdev)
931 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400932 up_write(&pcistub_sem);
933 return result;
934}
935
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400936/*xen_pcibk_error_resume: it will send the error_resume request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400937* in case of the device driver could provide this service, and then wait
938* for pcifront ack.
939* @dev: pointer to PCI devices
940*/
941
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400942static void xen_pcibk_error_resume(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400943{
944 struct pcistub_device *psdev;
945
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400946 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400947 dev->bus->number, dev->devfn);
948
949 down_write(&pcistub_sem);
950 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
951 dev->bus->number,
952 PCI_SLOT(dev->devfn),
953 PCI_FUNC(dev->devfn));
954
955 if (!psdev || !psdev->pdev) {
956 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400957 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400958 goto end;
959 }
960
961 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400962 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400963 " by HVM, kill it\n");
964 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100965 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400966 }
967
968 if (!test_bit(_XEN_PCIB_AERHANDLER,
969 (unsigned long *)&psdev->pdev->sh_info->flags)) {
970 dev_err(&dev->dev,
971 "guest with no AER driver should have been killed\n");
972 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100973 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400974 }
975 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
976 PCI_ERS_RESULT_RECOVERED);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400977end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100978 if (psdev)
979 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400980 up_write(&pcistub_sem);
981 return;
982}
983
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400984/*add xen_pcibk AER handling*/
Stephen Hemminger1d352032012-09-07 09:33:17 -0700985static const struct pci_error_handlers xen_pcibk_error_handler = {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400986 .error_detected = xen_pcibk_error_detected,
987 .mmio_enabled = xen_pcibk_mmio_enabled,
988 .slot_reset = xen_pcibk_slot_reset,
989 .resume = xen_pcibk_error_resume,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400990};
991
992/*
993 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
994 * for a normal device. I don't want it to be loaded automatically.
995 */
996
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400997static struct pci_driver xen_pcibk_pci_driver = {
998 /* The name should be xen_pciback, but until the tools are updated
999 * we will keep it as pciback. */
Juergen Grossb0578782016-09-22 10:45:41 +02001000 .name = PCISTUB_DRIVER_NAME,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001001 .id_table = pcistub_ids,
1002 .probe = pcistub_probe,
1003 .remove = pcistub_remove,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001004 .err_handler = &xen_pcibk_error_handler,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001005};
1006
1007static inline int str_to_slot(const char *buf, int *domain, int *bus,
1008 int *slot, int *func)
1009{
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001010 int parsed = 0;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001011
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001012 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
1013 &parsed)) {
Jan Beulichc3cb4702012-09-18 12:29:03 +01001014 case 3:
1015 *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001016 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001017 break;
1018 case 2:
1019 *slot = *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001020 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001021 break;
1022 }
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001023 if (parsed && !buf[parsed])
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001024 return 0;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001025
1026 /* try again without domain */
1027 *domain = 0;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001028 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
Jan Beulichc3cb4702012-09-18 12:29:03 +01001029 case 2:
1030 *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001031 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001032 break;
1033 case 1:
1034 *slot = *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001035 sscanf(buf, " %x:*.* %n", bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001036 break;
1037 }
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001038 if (parsed && !buf[parsed])
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001039 return 0;
1040
1041 return -EINVAL;
1042}
1043
1044static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
1045 *slot, int *func, int *reg, int *size, int *mask)
1046{
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001047 int parsed = 0;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001048
Jan Beulichb3e40b72012-11-02 14:37:13 +00001049 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001050 reg, size, mask, &parsed);
1051 if (parsed && !buf[parsed])
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001052 return 0;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001053
1054 /* try again without domain */
1055 *domain = 0;
Jan Beulichb3e40b72012-11-02 14:37:13 +00001056 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001057 mask, &parsed);
1058 if (parsed && !buf[parsed])
1059 return 0;
1060
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001061 return -EINVAL;
1062}
1063
1064static int pcistub_device_id_add(int domain, int bus, int slot, int func)
1065{
1066 struct pcistub_device_id *pci_dev_id;
Jan Beulichb3e40b72012-11-02 14:37:13 +00001067 int rc = 0, devfn = PCI_DEVFN(slot, func);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001068
1069 if (slot < 0) {
1070 for (slot = 0; !rc && slot < 32; ++slot)
1071 rc = pcistub_device_id_add(domain, bus, slot, func);
1072 return rc;
1073 }
1074
1075 if (func < 0) {
1076 for (func = 0; !rc && func < 8; ++func)
1077 rc = pcistub_device_id_add(domain, bus, slot, func);
1078 return rc;
1079 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001080
Jan Beulichb3e40b72012-11-02 14:37:13 +00001081 if ((
1082#if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1083 || !defined(CONFIG_PCI_DOMAINS)
1084 !pci_domains_supported ? domain :
1085#endif
1086 domain < 0 || domain > 0xffff)
1087 || bus < 0 || bus > 0xff
1088 || PCI_SLOT(devfn) != slot
1089 || PCI_FUNC(devfn) != func)
1090 return -EINVAL;
1091
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001092 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
1093 if (!pci_dev_id)
1094 return -ENOMEM;
1095
Joe Perches283c0972013-06-28 03:21:41 -07001096 pr_debug("wants to seize %04x:%02x:%02x.%d\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001097 domain, bus, slot, func);
1098
Juergen Gross9f8bee92016-09-22 10:45:40 +02001099 pcistub_device_id_add_list(pci_dev_id, domain, bus, devfn);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001100
1101 return 0;
1102}
1103
1104static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1105{
1106 struct pcistub_device_id *pci_dev_id, *t;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001107 int err = -ENOENT;
1108 unsigned long flags;
1109
1110 spin_lock_irqsave(&device_ids_lock, flags);
1111 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1112 slot_list) {
Jan Beulichc3cb4702012-09-18 12:29:03 +01001113 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1114 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1115 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001116 /* Don't break; here because it's possible the same
1117 * slot could be in the list more than once
1118 */
1119 list_del(&pci_dev_id->slot_list);
1120 kfree(pci_dev_id);
1121
1122 err = 0;
1123
Joe Perches283c0972013-06-28 03:21:41 -07001124 pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1125 domain, bus, slot, func);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001126 }
1127 }
1128 spin_unlock_irqrestore(&device_ids_lock, flags);
1129
1130 return err;
1131}
1132
Jan Beulichb3e40b72012-11-02 14:37:13 +00001133static int pcistub_reg_add(int domain, int bus, int slot, int func,
1134 unsigned int reg, unsigned int size,
1135 unsigned int mask)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001136{
1137 int err = 0;
1138 struct pcistub_device *psdev;
1139 struct pci_dev *dev;
1140 struct config_field *field;
1141
Jan Beulichb3e40b72012-11-02 14:37:13 +00001142 if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1143 return -EINVAL;
1144
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001145 psdev = pcistub_device_find(domain, bus, slot, func);
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001146 if (!psdev) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001147 err = -ENODEV;
1148 goto out;
1149 }
1150 dev = psdev->dev;
1151
1152 field = kzalloc(sizeof(*field), GFP_ATOMIC);
1153 if (!field) {
1154 err = -ENOMEM;
1155 goto out;
1156 }
1157
1158 field->offset = reg;
1159 field->size = size;
1160 field->mask = mask;
1161 field->init = NULL;
1162 field->reset = NULL;
1163 field->release = NULL;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001164 field->clean = xen_pcibk_config_field_free;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001165
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001166 err = xen_pcibk_config_quirks_add_field(dev, field);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001167 if (err)
1168 kfree(field);
1169out:
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001170 if (psdev)
1171 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001172 return err;
1173}
1174
Greg Kroah-Hartman0538dcb2017-07-19 16:43:44 +02001175static ssize_t new_slot_store(struct device_driver *drv, const char *buf,
1176 size_t count)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001177{
1178 int domain, bus, slot, func;
1179 int err;
1180
1181 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1182 if (err)
1183 goto out;
1184
1185 err = pcistub_device_id_add(domain, bus, slot, func);
1186
1187out:
1188 if (!err)
1189 err = count;
1190 return err;
1191}
Greg Kroah-Hartman0538dcb2017-07-19 16:43:44 +02001192static DRIVER_ATTR_WO(new_slot);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001193
Greg Kroah-Hartman0538dcb2017-07-19 16:43:44 +02001194static ssize_t remove_slot_store(struct device_driver *drv, const char *buf,
1195 size_t count)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001196{
1197 int domain, bus, slot, func;
1198 int err;
1199
1200 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1201 if (err)
1202 goto out;
1203
1204 err = pcistub_device_id_remove(domain, bus, slot, func);
1205
1206out:
1207 if (!err)
1208 err = count;
1209 return err;
1210}
Greg Kroah-Hartman0538dcb2017-07-19 16:43:44 +02001211static DRIVER_ATTR_WO(remove_slot);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001212
Greg Kroah-Hartman0538dcb2017-07-19 16:43:44 +02001213static ssize_t slots_show(struct device_driver *drv, char *buf)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001214{
1215 struct pcistub_device_id *pci_dev_id;
1216 size_t count = 0;
1217 unsigned long flags;
1218
1219 spin_lock_irqsave(&device_ids_lock, flags);
1220 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1221 if (count >= PAGE_SIZE)
1222 break;
1223
1224 count += scnprintf(buf + count, PAGE_SIZE - count,
Konrad Rzeszutek Wilke4de8662012-01-25 16:00:00 -05001225 "%04x:%02x:%02x.%d\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001226 pci_dev_id->domain, pci_dev_id->bus,
1227 PCI_SLOT(pci_dev_id->devfn),
1228 PCI_FUNC(pci_dev_id->devfn));
1229 }
1230 spin_unlock_irqrestore(&device_ids_lock, flags);
1231
1232 return count;
1233}
Greg Kroah-Hartman0538dcb2017-07-19 16:43:44 +02001234static DRIVER_ATTR_RO(slots);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001235
Greg Kroah-Hartman0538dcb2017-07-19 16:43:44 +02001236static ssize_t irq_handlers_show(struct device_driver *drv, char *buf)
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001237{
1238 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001239 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001240 size_t count = 0;
1241 unsigned long flags;
1242
1243 spin_lock_irqsave(&pcistub_devices_lock, flags);
1244 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1245 if (count >= PAGE_SIZE)
1246 break;
1247 if (!psdev->dev)
1248 continue;
1249 dev_data = pci_get_drvdata(psdev->dev);
1250 if (!dev_data)
1251 continue;
1252 count +=
1253 scnprintf(buf + count, PAGE_SIZE - count,
1254 "%s:%s:%sing:%ld\n",
1255 pci_name(psdev->dev),
1256 dev_data->isr_on ? "on" : "off",
1257 dev_data->ack_intr ? "ack" : "not ack",
1258 dev_data->handled);
1259 }
1260 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1261 return count;
1262}
Greg Kroah-Hartman0538dcb2017-07-19 16:43:44 +02001263static DRIVER_ATTR_RO(irq_handlers);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001264
Greg Kroah-Hartman0538dcb2017-07-19 16:43:44 +02001265static ssize_t irq_handler_state_store(struct device_driver *drv,
1266 const char *buf, size_t count)
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001267{
1268 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001269 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001270 int domain, bus, slot, func;
Wei Yongjun405010d2013-05-31 19:59:20 +08001271 int err;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001272
1273 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1274 if (err)
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001275 return err;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001276
1277 psdev = pcistub_device_find(domain, bus, slot, func);
Wei Yongjun405010d2013-05-31 19:59:20 +08001278 if (!psdev) {
1279 err = -ENOENT;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001280 goto out;
Wei Yongjun405010d2013-05-31 19:59:20 +08001281 }
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001282
1283 dev_data = pci_get_drvdata(psdev->dev);
Wei Yongjun405010d2013-05-31 19:59:20 +08001284 if (!dev_data) {
1285 err = -ENOENT;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001286 goto out;
Wei Yongjun405010d2013-05-31 19:59:20 +08001287 }
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001288
1289 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1290 dev_data->irq_name, dev_data->isr_on,
1291 !dev_data->isr_on);
1292
1293 dev_data->isr_on = !(dev_data->isr_on);
1294 if (dev_data->isr_on)
1295 dev_data->ack_intr = 1;
1296out:
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001297 if (psdev)
1298 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001299 if (!err)
1300 err = count;
1301 return err;
1302}
Greg Kroah-Hartman0538dcb2017-07-19 16:43:44 +02001303static DRIVER_ATTR_WO(irq_handler_state);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001304
Greg Kroah-Hartman0538dcb2017-07-19 16:43:44 +02001305static ssize_t quirks_store(struct device_driver *drv, const char *buf,
1306 size_t count)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001307{
1308 int domain, bus, slot, func, reg, size, mask;
1309 int err;
1310
1311 err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
1312 &mask);
1313 if (err)
1314 goto out;
1315
1316 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1317
1318out:
1319 if (!err)
1320 err = count;
1321 return err;
1322}
1323
Greg Kroah-Hartman0538dcb2017-07-19 16:43:44 +02001324static ssize_t quirks_show(struct device_driver *drv, char *buf)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001325{
1326 int count = 0;
1327 unsigned long flags;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001328 struct xen_pcibk_config_quirk *quirk;
1329 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001330 const struct config_field *field;
1331 const struct config_field_entry *cfg_entry;
1332
1333 spin_lock_irqsave(&device_ids_lock, flags);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001334 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001335 if (count >= PAGE_SIZE)
1336 goto out;
1337
1338 count += scnprintf(buf + count, PAGE_SIZE - count,
1339 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1340 quirk->pdev->bus->number,
1341 PCI_SLOT(quirk->pdev->devfn),
1342 PCI_FUNC(quirk->pdev->devfn),
1343 quirk->devid.vendor, quirk->devid.device,
1344 quirk->devid.subvendor,
1345 quirk->devid.subdevice);
1346
1347 dev_data = pci_get_drvdata(quirk->pdev);
1348
1349 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1350 field = cfg_entry->field;
1351 if (count >= PAGE_SIZE)
1352 goto out;
1353
1354 count += scnprintf(buf + count, PAGE_SIZE - count,
1355 "\t\t%08x:%01x:%08x\n",
1356 cfg_entry->base_offset +
1357 field->offset, field->size,
1358 field->mask);
1359 }
1360 }
1361
1362out:
1363 spin_unlock_irqrestore(&device_ids_lock, flags);
1364
1365 return count;
1366}
Greg Kroah-Hartman0538dcb2017-07-19 16:43:44 +02001367static DRIVER_ATTR_RW(quirks);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001368
Greg Kroah-Hartman0538dcb2017-07-19 16:43:44 +02001369static ssize_t permissive_store(struct device_driver *drv, const char *buf,
1370 size_t count)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001371{
1372 int domain, bus, slot, func;
1373 int err;
1374 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001375 struct xen_pcibk_dev_data *dev_data;
Jan Beulichb3e40b72012-11-02 14:37:13 +00001376
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001377 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1378 if (err)
1379 goto out;
Jan Beulichb3e40b72012-11-02 14:37:13 +00001380
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001381 psdev = pcistub_device_find(domain, bus, slot, func);
1382 if (!psdev) {
1383 err = -ENODEV;
1384 goto out;
1385 }
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001386
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001387 dev_data = pci_get_drvdata(psdev->dev);
1388 /* the driver data for a device should never be null at this point */
1389 if (!dev_data) {
1390 err = -ENXIO;
1391 goto release;
1392 }
1393 if (!dev_data->permissive) {
1394 dev_data->permissive = 1;
1395 /* Let user know that what they're doing could be unsafe */
1396 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1397 "configuration space accesses!\n");
1398 dev_warn(&psdev->dev->dev,
1399 "permissive mode is potentially unsafe!\n");
1400 }
1401release:
1402 pcistub_device_put(psdev);
1403out:
1404 if (!err)
1405 err = count;
1406 return err;
1407}
1408
1409static ssize_t permissive_show(struct device_driver *drv, char *buf)
1410{
1411 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001412 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001413 size_t count = 0;
1414 unsigned long flags;
1415 spin_lock_irqsave(&pcistub_devices_lock, flags);
1416 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1417 if (count >= PAGE_SIZE)
1418 break;
1419 if (!psdev->dev)
1420 continue;
1421 dev_data = pci_get_drvdata(psdev->dev);
1422 if (!dev_data || !dev_data->permissive)
1423 continue;
1424 count +=
1425 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1426 pci_name(psdev->dev));
1427 }
1428 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1429 return count;
1430}
Greg Kroah-Hartman0538dcb2017-07-19 16:43:44 +02001431static DRIVER_ATTR_RW(permissive);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001432
1433static void pcistub_exit(void)
1434{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001435 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1436 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001437 &driver_attr_remove_slot);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001438 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1439 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1440 driver_remove_file(&xen_pcibk_pci_driver.driver,
1441 &driver_attr_permissive);
1442 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001443 &driver_attr_irq_handlers);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001444 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001445 &driver_attr_irq_handler_state);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001446 pci_unregister_driver(&xen_pcibk_pci_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001447}
1448
1449static int __init pcistub_init(void)
1450{
1451 int pos = 0;
1452 int err = 0;
1453 int domain, bus, slot, func;
1454 int parsed;
1455
1456 if (pci_devs_to_hide && *pci_devs_to_hide) {
1457 do {
1458 parsed = 0;
1459
1460 err = sscanf(pci_devs_to_hide + pos,
1461 " (%x:%x:%x.%x) %n",
1462 &domain, &bus, &slot, &func, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001463 switch (err) {
1464 case 3:
1465 func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001466 sscanf(pci_devs_to_hide + pos,
1467 " (%x:%x:%x.*) %n",
1468 &domain, &bus, &slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001469 break;
1470 case 2:
1471 slot = func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001472 sscanf(pci_devs_to_hide + pos,
1473 " (%x:%x:*.*) %n",
1474 &domain, &bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001475 break;
1476 }
1477
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001478 if (!parsed) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001479 domain = 0;
1480 err = sscanf(pci_devs_to_hide + pos,
1481 " (%x:%x.%x) %n",
1482 &bus, &slot, &func, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001483 switch (err) {
1484 case 2:
1485 func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001486 sscanf(pci_devs_to_hide + pos,
1487 " (%x:%x.*) %n",
1488 &bus, &slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001489 break;
1490 case 1:
1491 slot = func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001492 sscanf(pci_devs_to_hide + pos,
1493 " (%x:*.*) %n",
1494 &bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001495 break;
1496 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001497 }
1498
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001499 if (parsed <= 0)
1500 goto parse_error;
1501
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001502 err = pcistub_device_id_add(domain, bus, slot, func);
1503 if (err)
1504 goto out;
1505
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001506 pos += parsed;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001507 } while (pci_devs_to_hide[pos]);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001508 }
1509
1510 /* If we're the first PCI Device Driver to register, we're the
1511 * first one to get offered PCI devices as they become
1512 * available (and thus we can be the first to grab them)
1513 */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001514 err = pci_register_driver(&xen_pcibk_pci_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001515 if (err < 0)
1516 goto out;
1517
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001518 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001519 &driver_attr_new_slot);
1520 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001521 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001522 &driver_attr_remove_slot);
1523 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001524 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001525 &driver_attr_slots);
1526 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001527 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001528 &driver_attr_quirks);
1529 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001530 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001531 &driver_attr_permissive);
1532
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001533 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001534 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001535 &driver_attr_irq_handlers);
1536 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001537 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001538 &driver_attr_irq_handler_state);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001539 if (err)
1540 pcistub_exit();
1541
1542out:
1543 return err;
1544
1545parse_error:
Joe Perches283c0972013-06-28 03:21:41 -07001546 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001547 pci_devs_to_hide + pos);
1548 return -EINVAL;
1549}
1550
1551#ifndef MODULE
1552/*
1553 * fs_initcall happens before device_initcall
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001554 * so xen_pcibk *should* get called first (b/c we
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001555 * want to suck up any device before other drivers
1556 * get a chance by being the first pci device
1557 * driver to register)
1558 */
1559fs_initcall(pcistub_init);
1560#endif
1561
Jan Beulich6945c592014-12-03 16:40:33 -05001562#ifdef CONFIG_PCI_IOV
1563static struct pcistub_device *find_vfs(const struct pci_dev *pdev)
1564{
1565 struct pcistub_device *psdev = NULL;
1566 unsigned long flags;
1567 bool found = false;
1568
1569 spin_lock_irqsave(&pcistub_devices_lock, flags);
1570 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1571 if (!psdev->pdev && psdev->dev != pdev
1572 && pci_physfn(psdev->dev) == pdev) {
1573 found = true;
1574 break;
1575 }
1576 }
1577 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1578 if (found)
1579 return psdev;
1580 return NULL;
1581}
1582
1583static int pci_stub_notifier(struct notifier_block *nb,
1584 unsigned long action, void *data)
1585{
1586 struct device *dev = data;
1587 const struct pci_dev *pdev = to_pci_dev(dev);
1588
1589 if (action != BUS_NOTIFY_UNBIND_DRIVER)
1590 return NOTIFY_DONE;
1591
1592 if (!pdev->is_physfn)
1593 return NOTIFY_DONE;
1594
1595 for (;;) {
1596 struct pcistub_device *psdev = find_vfs(pdev);
1597 if (!psdev)
1598 break;
1599 device_release_driver(&psdev->dev->dev);
1600 }
1601 return NOTIFY_DONE;
1602}
1603
1604static struct notifier_block pci_stub_nb = {
1605 .notifier_call = pci_stub_notifier,
1606};
1607#endif
1608
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001609static int __init xen_pcibk_init(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001610{
1611 int err;
1612
1613 if (!xen_initial_domain())
1614 return -ENODEV;
1615
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001616 err = xen_pcibk_config_init();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001617 if (err)
1618 return err;
1619
1620#ifdef MODULE
1621 err = pcistub_init();
1622 if (err < 0)
1623 return err;
1624#endif
1625
1626 pcistub_init_devices_late();
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001627 err = xen_pcibk_xenbus_register();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001628 if (err)
1629 pcistub_exit();
Jan Beulich6945c592014-12-03 16:40:33 -05001630#ifdef CONFIG_PCI_IOV
1631 else
1632 bus_register_notifier(&pci_bus_type, &pci_stub_nb);
1633#endif
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001634
1635 return err;
1636}
1637
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001638static void __exit xen_pcibk_cleanup(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001639{
Jan Beulich6945c592014-12-03 16:40:33 -05001640#ifdef CONFIG_PCI_IOV
1641 bus_unregister_notifier(&pci_bus_type, &pci_stub_nb);
1642#endif
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001643 xen_pcibk_xenbus_unregister();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001644 pcistub_exit();
1645}
1646
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001647module_init(xen_pcibk_init);
1648module_exit(xen_pcibk_cleanup);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001649
1650MODULE_LICENSE("Dual BSD/GPL");
Jan Beulich402c5e12011-09-21 16:22:11 -04001651MODULE_ALIAS("xen-backend:pci");