Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/rwsem.h> |
| 10 | #include <linux/list.h> |
| 11 | #include <linux/spinlock.h> |
| 12 | #include <linux/kref.h> |
| 13 | #include <linux/pci.h> |
| 14 | #include <linux/wait.h> |
| 15 | #include <linux/sched.h> |
Konrad Rzeszutek Wilk | 8bfd4e0 | 2011-07-19 20:09:43 -0400 | [diff] [blame] | 16 | #include <linux/atomic.h> |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 17 | #include <xen/events.h> |
| 18 | #include <asm/xen/pci.h> |
| 19 | #include <asm/xen/hypervisor.h> |
Jan Beulich | 909b3fd | 2013-03-12 15:06:23 +0000 | [diff] [blame] | 20 | #include <xen/interface/physdev.h> |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 21 | #include "pciback.h" |
| 22 | #include "conf_space.h" |
| 23 | #include "conf_space_quirks.h" |
| 24 | |
| 25 | static char *pci_devs_to_hide; |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 26 | wait_queue_head_t xen_pcibk_aer_wait_queue; |
| 27 | /*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops, |
| 28 | * We want to avoid in middle of AER ops, xen_pcibk devices is being removed |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 29 | */ |
| 30 | static DECLARE_RWSEM(pcistub_sem); |
| 31 | module_param_named(hide, pci_devs_to_hide, charp, 0444); |
| 32 | |
| 33 | struct pcistub_device_id { |
| 34 | struct list_head slot_list; |
| 35 | int domain; |
| 36 | unsigned char bus; |
| 37 | unsigned int devfn; |
| 38 | }; |
| 39 | static LIST_HEAD(pcistub_device_ids); |
| 40 | static DEFINE_SPINLOCK(device_ids_lock); |
| 41 | |
| 42 | struct pcistub_device { |
| 43 | struct kref kref; |
| 44 | struct list_head dev_list; |
| 45 | spinlock_t lock; |
| 46 | |
| 47 | struct pci_dev *dev; |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 48 | struct xen_pcibk_device *pdev;/* non-NULL if struct pci_dev is in use */ |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | /* Access to pcistub_devices & seized_devices lists and the initialize_devices |
| 52 | * flag must be locked with pcistub_devices_lock |
| 53 | */ |
| 54 | static DEFINE_SPINLOCK(pcistub_devices_lock); |
| 55 | static LIST_HEAD(pcistub_devices); |
| 56 | |
| 57 | /* wait for device_initcall before initializing our devices |
| 58 | * (see pcistub_init_devices_late) |
| 59 | */ |
| 60 | static int initialize_devices; |
| 61 | static LIST_HEAD(seized_devices); |
| 62 | |
| 63 | static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev) |
| 64 | { |
| 65 | struct pcistub_device *psdev; |
| 66 | |
| 67 | dev_dbg(&dev->dev, "pcistub_device_alloc\n"); |
| 68 | |
| 69 | psdev = kzalloc(sizeof(*psdev), GFP_ATOMIC); |
| 70 | if (!psdev) |
| 71 | return NULL; |
| 72 | |
| 73 | psdev->dev = pci_dev_get(dev); |
| 74 | if (!psdev->dev) { |
| 75 | kfree(psdev); |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | kref_init(&psdev->kref); |
| 80 | spin_lock_init(&psdev->lock); |
| 81 | |
| 82 | return psdev; |
| 83 | } |
| 84 | |
| 85 | /* Don't call this directly as it's called by pcistub_device_put */ |
| 86 | static void pcistub_device_release(struct kref *kref) |
| 87 | { |
| 88 | struct pcistub_device *psdev; |
Jan Beulich | 909b3fd | 2013-03-12 15:06:23 +0000 | [diff] [blame] | 89 | struct pci_dev *dev; |
Konrad Rzeszutek Wilk | cd9db80 | 2012-01-04 14:30:58 -0500 | [diff] [blame] | 90 | struct xen_pcibk_dev_data *dev_data; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 91 | |
| 92 | psdev = container_of(kref, struct pcistub_device, kref); |
Jan Beulich | 909b3fd | 2013-03-12 15:06:23 +0000 | [diff] [blame] | 93 | dev = psdev->dev; |
| 94 | dev_data = pci_get_drvdata(dev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 95 | |
Jan Beulich | 909b3fd | 2013-03-12 15:06:23 +0000 | [diff] [blame] | 96 | dev_dbg(&dev->dev, "pcistub_device_release\n"); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 97 | |
Jan Beulich | 909b3fd | 2013-03-12 15:06:23 +0000 | [diff] [blame] | 98 | xen_unregister_device_domain_owner(dev); |
Konrad Rzeszutek Wilk | 6221a9b | 2009-12-09 17:43:15 -0500 | [diff] [blame] | 99 | |
Konrad Rzeszutek Wilk | cd9db80 | 2012-01-04 14:30:58 -0500 | [diff] [blame] | 100 | /* Call the reset function which does not take lock as this |
| 101 | * is called from "unbind" which takes a device_lock mutex. |
| 102 | */ |
Jan Beulich | 909b3fd | 2013-03-12 15:06:23 +0000 | [diff] [blame] | 103 | __pci_reset_function_locked(dev); |
| 104 | if (pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state)) |
| 105 | dev_dbg(&dev->dev, "Could not reload PCI state\n"); |
| 106 | else |
| 107 | pci_restore_state(dev); |
| 108 | |
Jan Beulich | d69c0e3 | 2013-05-29 13:31:15 +0100 | [diff] [blame] | 109 | if (dev->msix_cap) { |
Jan Beulich | 909b3fd | 2013-03-12 15:06:23 +0000 | [diff] [blame] | 110 | struct physdev_pci_device ppdev = { |
| 111 | .seg = pci_domain_nr(dev->bus), |
| 112 | .bus = dev->bus->number, |
| 113 | .devfn = dev->devfn |
| 114 | }; |
| 115 | int err = HYPERVISOR_physdev_op(PHYSDEVOP_release_msix, |
| 116 | &ppdev); |
| 117 | |
| 118 | if (err) |
| 119 | dev_warn(&dev->dev, "MSI-X release failed (%d)\n", |
| 120 | err); |
| 121 | } |
Konrad Rzeszutek Wilk | cd9db80 | 2012-01-04 14:30:58 -0500 | [diff] [blame] | 122 | |
| 123 | /* Disable the device */ |
Jan Beulich | 909b3fd | 2013-03-12 15:06:23 +0000 | [diff] [blame] | 124 | xen_pcibk_reset_device(dev); |
Konrad Rzeszutek Wilk | cd9db80 | 2012-01-04 14:30:58 -0500 | [diff] [blame] | 125 | |
| 126 | kfree(dev_data); |
Jan Beulich | 909b3fd | 2013-03-12 15:06:23 +0000 | [diff] [blame] | 127 | pci_set_drvdata(dev, NULL); |
Konrad Rzeszutek Wilk | cd9db80 | 2012-01-04 14:30:58 -0500 | [diff] [blame] | 128 | |
| 129 | /* Clean-up the device */ |
Jan Beulich | 909b3fd | 2013-03-12 15:06:23 +0000 | [diff] [blame] | 130 | xen_pcibk_config_free_dyn_fields(dev); |
| 131 | xen_pcibk_config_free_dev(dev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 132 | |
Jan Beulich | 909b3fd | 2013-03-12 15:06:23 +0000 | [diff] [blame] | 133 | dev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED; |
| 134 | pci_dev_put(dev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 135 | |
| 136 | kfree(psdev); |
| 137 | } |
| 138 | |
| 139 | static inline void pcistub_device_get(struct pcistub_device *psdev) |
| 140 | { |
| 141 | kref_get(&psdev->kref); |
| 142 | } |
| 143 | |
| 144 | static inline void pcistub_device_put(struct pcistub_device *psdev) |
| 145 | { |
| 146 | kref_put(&psdev->kref, pcistub_device_release); |
| 147 | } |
| 148 | |
| 149 | static struct pcistub_device *pcistub_device_find(int domain, int bus, |
| 150 | int slot, int func) |
| 151 | { |
| 152 | struct pcistub_device *psdev = NULL; |
| 153 | unsigned long flags; |
| 154 | |
| 155 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 156 | |
| 157 | list_for_each_entry(psdev, &pcistub_devices, dev_list) { |
| 158 | if (psdev->dev != NULL |
| 159 | && domain == pci_domain_nr(psdev->dev->bus) |
| 160 | && bus == psdev->dev->bus->number |
Jan Beulich | b3e40b7 | 2012-11-02 14:37:13 +0000 | [diff] [blame] | 161 | && slot == PCI_SLOT(psdev->dev->devfn) |
| 162 | && func == PCI_FUNC(psdev->dev->devfn)) { |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 163 | pcistub_device_get(psdev); |
| 164 | goto out; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /* didn't find it */ |
| 169 | psdev = NULL; |
| 170 | |
| 171 | out: |
| 172 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 173 | return psdev; |
| 174 | } |
| 175 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 176 | static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 177 | struct pcistub_device *psdev) |
| 178 | { |
| 179 | struct pci_dev *pci_dev = NULL; |
| 180 | unsigned long flags; |
| 181 | |
| 182 | pcistub_device_get(psdev); |
| 183 | |
| 184 | spin_lock_irqsave(&psdev->lock, flags); |
| 185 | if (!psdev->pdev) { |
| 186 | psdev->pdev = pdev; |
| 187 | pci_dev = psdev->dev; |
| 188 | } |
| 189 | spin_unlock_irqrestore(&psdev->lock, flags); |
| 190 | |
| 191 | if (!pci_dev) |
| 192 | pcistub_device_put(psdev); |
| 193 | |
| 194 | return pci_dev; |
| 195 | } |
| 196 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 197 | struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 198 | int domain, int bus, |
| 199 | int slot, int func) |
| 200 | { |
| 201 | struct pcistub_device *psdev; |
| 202 | struct pci_dev *found_dev = NULL; |
| 203 | unsigned long flags; |
| 204 | |
| 205 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 206 | |
| 207 | list_for_each_entry(psdev, &pcistub_devices, dev_list) { |
| 208 | if (psdev->dev != NULL |
| 209 | && domain == pci_domain_nr(psdev->dev->bus) |
| 210 | && bus == psdev->dev->bus->number |
Jan Beulich | b3e40b7 | 2012-11-02 14:37:13 +0000 | [diff] [blame] | 211 | && slot == PCI_SLOT(psdev->dev->devfn) |
| 212 | && func == PCI_FUNC(psdev->dev->devfn)) { |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 213 | found_dev = pcistub_device_get_pci_dev(pdev, psdev); |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 219 | return found_dev; |
| 220 | } |
| 221 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 222 | struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 223 | struct pci_dev *dev) |
| 224 | { |
| 225 | struct pcistub_device *psdev; |
| 226 | struct pci_dev *found_dev = NULL; |
| 227 | unsigned long flags; |
| 228 | |
| 229 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 230 | |
| 231 | list_for_each_entry(psdev, &pcistub_devices, dev_list) { |
| 232 | if (psdev->dev == dev) { |
| 233 | found_dev = pcistub_device_get_pci_dev(pdev, psdev); |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 239 | return found_dev; |
| 240 | } |
| 241 | |
| 242 | void pcistub_put_pci_dev(struct pci_dev *dev) |
| 243 | { |
| 244 | struct pcistub_device *psdev, *found_psdev = NULL; |
| 245 | unsigned long flags; |
| 246 | |
| 247 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 248 | |
| 249 | list_for_each_entry(psdev, &pcistub_devices, dev_list) { |
| 250 | if (psdev->dev == dev) { |
| 251 | found_psdev = psdev; |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
Konrad Rzeszutek Wilk | 4645bf3 | 2011-09-29 13:12:43 -0400 | [diff] [blame] | 257 | if (WARN_ON(!found_psdev)) |
| 258 | return; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 259 | |
| 260 | /*hold this lock for avoiding breaking link between |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 261 | * pcistub and xen_pcibk when AER is in processing |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 262 | */ |
| 263 | down_write(&pcistub_sem); |
| 264 | /* Cleanup our device |
| 265 | * (so it's ready for the next domain) |
| 266 | */ |
Konrad Rzeszutek Wilk | cd9db80 | 2012-01-04 14:30:58 -0500 | [diff] [blame] | 267 | |
| 268 | /* This is OK - we are running from workqueue context |
| 269 | * and want to inhibit the user from fiddling with 'reset' |
| 270 | */ |
| 271 | pci_reset_function(dev); |
| 272 | pci_restore_state(psdev->dev); |
| 273 | |
| 274 | /* This disables the device. */ |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 275 | xen_pcibk_reset_device(found_psdev->dev); |
Konrad Rzeszutek Wilk | cd9db80 | 2012-01-04 14:30:58 -0500 | [diff] [blame] | 276 | |
| 277 | /* And cleanup up our emulated fields. */ |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 278 | xen_pcibk_config_free_dyn_fields(found_psdev->dev); |
| 279 | xen_pcibk_config_reset_dev(found_psdev->dev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 280 | |
Konrad Rzeszutek Wilk | 3167355 | 2012-01-04 15:11:02 -0500 | [diff] [blame] | 281 | xen_unregister_device_domain_owner(found_psdev->dev); |
| 282 | |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 283 | spin_lock_irqsave(&found_psdev->lock, flags); |
| 284 | found_psdev->pdev = NULL; |
| 285 | spin_unlock_irqrestore(&found_psdev->lock, flags); |
| 286 | |
| 287 | pcistub_device_put(found_psdev); |
| 288 | up_write(&pcistub_sem); |
| 289 | } |
| 290 | |
Greg Kroah-Hartman | 345a525 | 2012-12-21 13:00:00 -0800 | [diff] [blame] | 291 | static int pcistub_match_one(struct pci_dev *dev, |
| 292 | struct pcistub_device_id *pdev_id) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 293 | { |
| 294 | /* Match the specified device by domain, bus, slot, func and also if |
| 295 | * any of the device's parent bridges match. |
| 296 | */ |
| 297 | for (; dev != NULL; dev = dev->bus->self) { |
| 298 | if (pci_domain_nr(dev->bus) == pdev_id->domain |
| 299 | && dev->bus->number == pdev_id->bus |
| 300 | && dev->devfn == pdev_id->devfn) |
| 301 | return 1; |
| 302 | |
| 303 | /* Sometimes topmost bridge links to itself. */ |
| 304 | if (dev == dev->bus->self) |
| 305 | break; |
| 306 | } |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
Greg Kroah-Hartman | 345a525 | 2012-12-21 13:00:00 -0800 | [diff] [blame] | 311 | static int pcistub_match(struct pci_dev *dev) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 312 | { |
| 313 | struct pcistub_device_id *pdev_id; |
| 314 | unsigned long flags; |
| 315 | int found = 0; |
| 316 | |
| 317 | spin_lock_irqsave(&device_ids_lock, flags); |
| 318 | list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) { |
| 319 | if (pcistub_match_one(dev, pdev_id)) { |
| 320 | found = 1; |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | spin_unlock_irqrestore(&device_ids_lock, flags); |
| 325 | |
| 326 | return found; |
| 327 | } |
| 328 | |
Greg Kroah-Hartman | 345a525 | 2012-12-21 13:00:00 -0800 | [diff] [blame] | 329 | static int pcistub_init_device(struct pci_dev *dev) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 330 | { |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 331 | struct xen_pcibk_dev_data *dev_data; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 332 | int err = 0; |
| 333 | |
| 334 | dev_dbg(&dev->dev, "initializing...\n"); |
| 335 | |
| 336 | /* The PCI backend is not intended to be a module (or to work with |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 337 | * removable PCI devices (yet). If it were, xen_pcibk_config_free() |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 338 | * would need to be called somewhere to free the memory allocated |
| 339 | * here and then to call kfree(pci_get_drvdata(psdev->dev)). |
| 340 | */ |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 341 | dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]") |
| 342 | + strlen(pci_name(dev)) + 1, GFP_ATOMIC); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 343 | if (!dev_data) { |
| 344 | err = -ENOMEM; |
| 345 | goto out; |
| 346 | } |
| 347 | pci_set_drvdata(dev, dev_data); |
| 348 | |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 349 | /* |
| 350 | * Setup name for fake IRQ handler. It will only be enabled |
| 351 | * once the device is turned on by the guest. |
| 352 | */ |
| 353 | sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev)); |
| 354 | |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 355 | dev_dbg(&dev->dev, "initializing config\n"); |
| 356 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 357 | init_waitqueue_head(&xen_pcibk_aer_wait_queue); |
| 358 | err = xen_pcibk_config_init_dev(dev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 359 | if (err) |
| 360 | goto out; |
| 361 | |
| 362 | /* HACK: Force device (& ACPI) to determine what IRQ it's on - we |
| 363 | * must do this here because pcibios_enable_device may specify |
| 364 | * the pci device's true irq (and possibly its other resources) |
| 365 | * if they differ from what's in the configuration space. |
| 366 | * This makes the assumption that the device's resources won't |
| 367 | * change after this point (otherwise this code may break!) |
| 368 | */ |
| 369 | dev_dbg(&dev->dev, "enabling device\n"); |
| 370 | err = pci_enable_device(dev); |
| 371 | if (err) |
| 372 | goto config_release; |
| 373 | |
Jan Beulich | d69c0e3 | 2013-05-29 13:31:15 +0100 | [diff] [blame] | 374 | if (dev->msix_cap) { |
Jan Beulich | 909b3fd | 2013-03-12 15:06:23 +0000 | [diff] [blame] | 375 | struct physdev_pci_device ppdev = { |
| 376 | .seg = pci_domain_nr(dev->bus), |
| 377 | .bus = dev->bus->number, |
| 378 | .devfn = dev->devfn |
| 379 | }; |
| 380 | |
| 381 | err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev); |
| 382 | if (err) |
| 383 | dev_err(&dev->dev, "MSI-X preparation failed (%d)\n", |
| 384 | err); |
| 385 | } |
| 386 | |
Konrad Rzeszutek Wilk | cd9db80 | 2012-01-04 14:30:58 -0500 | [diff] [blame] | 387 | /* We need the device active to save the state. */ |
| 388 | dev_dbg(&dev->dev, "save state of device\n"); |
| 389 | pci_save_state(dev); |
| 390 | dev_data->pci_saved_state = pci_store_saved_state(dev); |
| 391 | if (!dev_data->pci_saved_state) |
| 392 | dev_err(&dev->dev, "Could not store PCI conf saved state!\n"); |
Konrad Rzeszutek Wilk | 80ba77d | 2012-09-05 16:35:20 -0400 | [diff] [blame] | 393 | else { |
Masanari Iida | 744627e9 | 2012-11-05 23:30:40 +0900 | [diff] [blame] | 394 | dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n"); |
Konrad Rzeszutek Wilk | 80ba77d | 2012-09-05 16:35:20 -0400 | [diff] [blame] | 395 | __pci_reset_function_locked(dev); |
Konrad Rzeszutek Wilk | c341ca4 | 2012-09-25 16:48:24 -0400 | [diff] [blame] | 396 | pci_restore_state(dev); |
Konrad Rzeszutek Wilk | 80ba77d | 2012-09-05 16:35:20 -0400 | [diff] [blame] | 397 | } |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 398 | /* Now disable the device (this also ensures some private device |
| 399 | * data is setup before we export) |
| 400 | */ |
| 401 | dev_dbg(&dev->dev, "reset device\n"); |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 402 | xen_pcibk_reset_device(dev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 403 | |
Konrad Rzeszutek Wilk | 97309d3 | 2012-01-04 14:10:32 -0500 | [diff] [blame] | 404 | dev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 405 | return 0; |
| 406 | |
| 407 | config_release: |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 408 | xen_pcibk_config_free_dev(dev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 409 | |
| 410 | out: |
| 411 | pci_set_drvdata(dev, NULL); |
| 412 | kfree(dev_data); |
| 413 | return err; |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * Because some initialization still happens on |
| 418 | * devices during fs_initcall, we need to defer |
| 419 | * full initialization of our devices until |
| 420 | * device_initcall. |
| 421 | */ |
| 422 | static int __init pcistub_init_devices_late(void) |
| 423 | { |
| 424 | struct pcistub_device *psdev; |
| 425 | unsigned long flags; |
| 426 | int err = 0; |
| 427 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 428 | pr_debug(DRV_NAME ": pcistub_init_devices_late\n"); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 429 | |
| 430 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 431 | |
| 432 | while (!list_empty(&seized_devices)) { |
| 433 | psdev = container_of(seized_devices.next, |
| 434 | struct pcistub_device, dev_list); |
| 435 | list_del(&psdev->dev_list); |
| 436 | |
| 437 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 438 | |
| 439 | err = pcistub_init_device(psdev->dev); |
| 440 | if (err) { |
| 441 | dev_err(&psdev->dev->dev, |
| 442 | "error %d initializing device\n", err); |
| 443 | kfree(psdev); |
| 444 | psdev = NULL; |
| 445 | } |
| 446 | |
| 447 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 448 | |
| 449 | if (psdev) |
| 450 | list_add_tail(&psdev->dev_list, &pcistub_devices); |
| 451 | } |
| 452 | |
| 453 | initialize_devices = 1; |
| 454 | |
| 455 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
Greg Kroah-Hartman | 345a525 | 2012-12-21 13:00:00 -0800 | [diff] [blame] | 460 | static int pcistub_seize(struct pci_dev *dev) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 461 | { |
| 462 | struct pcistub_device *psdev; |
| 463 | unsigned long flags; |
| 464 | int err = 0; |
| 465 | |
| 466 | psdev = pcistub_device_alloc(dev); |
| 467 | if (!psdev) |
| 468 | return -ENOMEM; |
| 469 | |
| 470 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 471 | |
| 472 | if (initialize_devices) { |
| 473 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 474 | |
| 475 | /* don't want irqs disabled when calling pcistub_init_device */ |
| 476 | err = pcistub_init_device(psdev->dev); |
| 477 | |
| 478 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 479 | |
| 480 | if (!err) |
| 481 | list_add(&psdev->dev_list, &pcistub_devices); |
| 482 | } else { |
| 483 | dev_dbg(&dev->dev, "deferring initialization\n"); |
| 484 | list_add(&psdev->dev_list, &seized_devices); |
| 485 | } |
| 486 | |
| 487 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 488 | |
| 489 | if (err) |
| 490 | pcistub_device_put(psdev); |
| 491 | |
| 492 | return err; |
| 493 | } |
| 494 | |
Greg Kroah-Hartman | 345a525 | 2012-12-21 13:00:00 -0800 | [diff] [blame] | 495 | static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 496 | { |
| 497 | int err = 0; |
| 498 | |
| 499 | dev_dbg(&dev->dev, "probing...\n"); |
| 500 | |
| 501 | if (pcistub_match(dev)) { |
| 502 | |
| 503 | if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL |
| 504 | && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) { |
| 505 | dev_err(&dev->dev, "can't export pci devices that " |
| 506 | "don't have a normal (0) or bridge (1) " |
| 507 | "header type!\n"); |
| 508 | err = -ENODEV; |
| 509 | goto out; |
| 510 | } |
| 511 | |
| 512 | dev_info(&dev->dev, "seizing device\n"); |
| 513 | err = pcistub_seize(dev); |
| 514 | } else |
| 515 | /* Didn't find the device */ |
| 516 | err = -ENODEV; |
| 517 | |
| 518 | out: |
| 519 | return err; |
| 520 | } |
| 521 | |
| 522 | static void pcistub_remove(struct pci_dev *dev) |
| 523 | { |
| 524 | struct pcistub_device *psdev, *found_psdev = NULL; |
| 525 | unsigned long flags; |
| 526 | |
| 527 | dev_dbg(&dev->dev, "removing\n"); |
| 528 | |
| 529 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 530 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 531 | xen_pcibk_config_quirk_release(dev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 532 | |
| 533 | list_for_each_entry(psdev, &pcistub_devices, dev_list) { |
| 534 | if (psdev->dev == dev) { |
| 535 | found_psdev = psdev; |
| 536 | break; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 541 | |
| 542 | if (found_psdev) { |
| 543 | dev_dbg(&dev->dev, "found device to remove - in use? %p\n", |
| 544 | found_psdev->pdev); |
| 545 | |
| 546 | if (found_psdev->pdev) { |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 547 | printk(KERN_WARNING DRV_NAME ": ****** removing device " |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 548 | "%s while still in-use! ******\n", |
| 549 | pci_name(found_psdev->dev)); |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 550 | printk(KERN_WARNING DRV_NAME ": ****** driver domain may" |
| 551 | " still access this device's i/o resources!\n"); |
| 552 | printk(KERN_WARNING DRV_NAME ": ****** shutdown driver " |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 553 | "domain before binding device\n"); |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 554 | printk(KERN_WARNING DRV_NAME ": ****** to other drivers " |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 555 | "or domains\n"); |
| 556 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 557 | xen_pcibk_release_pci_dev(found_psdev->pdev, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 558 | found_psdev->dev); |
| 559 | } |
| 560 | |
| 561 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 562 | list_del(&found_psdev->dev_list); |
| 563 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 564 | |
| 565 | /* the final put for releasing from the list */ |
| 566 | pcistub_device_put(found_psdev); |
| 567 | } |
| 568 | } |
| 569 | |
Konrad Rzeszutek Wilk | 8bfd4e0 | 2011-07-19 20:09:43 -0400 | [diff] [blame] | 570 | static DEFINE_PCI_DEVICE_TABLE(pcistub_ids) = { |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 571 | { |
| 572 | .vendor = PCI_ANY_ID, |
| 573 | .device = PCI_ANY_ID, |
| 574 | .subvendor = PCI_ANY_ID, |
| 575 | .subdevice = PCI_ANY_ID, |
| 576 | }, |
| 577 | {0,}, |
| 578 | }; |
| 579 | |
| 580 | #define PCI_NODENAME_MAX 40 |
| 581 | static void kill_domain_by_device(struct pcistub_device *psdev) |
| 582 | { |
| 583 | struct xenbus_transaction xbt; |
| 584 | int err; |
| 585 | char nodename[PCI_NODENAME_MAX]; |
| 586 | |
Konrad Rzeszutek Wilk | 72bf809 | 2011-09-29 13:43:28 -0400 | [diff] [blame] | 587 | BUG_ON(!psdev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 588 | snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0", |
| 589 | psdev->pdev->xdev->otherend_id); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 590 | |
| 591 | again: |
| 592 | err = xenbus_transaction_start(&xbt); |
| 593 | if (err) { |
| 594 | dev_err(&psdev->dev->dev, |
| 595 | "error %d when start xenbus transaction\n", err); |
| 596 | return; |
| 597 | } |
| 598 | /*PV AER handlers will set this flag*/ |
| 599 | xenbus_printf(xbt, nodename, "aerState" , "aerfail"); |
| 600 | err = xenbus_transaction_end(xbt, 0); |
| 601 | if (err) { |
| 602 | if (err == -EAGAIN) |
| 603 | goto again; |
| 604 | dev_err(&psdev->dev->dev, |
| 605 | "error %d when end xenbus transaction\n", err); |
| 606 | return; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 611 | * backend need to have cooperation. In xen_pcibk, those steps will do similar |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 612 | * jobs: send service request and waiting for front_end response. |
| 613 | */ |
| 614 | static pci_ers_result_t common_process(struct pcistub_device *psdev, |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 615 | pci_channel_state_t state, int aer_cmd, |
| 616 | pci_ers_result_t result) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 617 | { |
| 618 | pci_ers_result_t res = result; |
| 619 | struct xen_pcie_aer_op *aer_op; |
| 620 | int ret; |
| 621 | |
| 622 | /*with PV AER drivers*/ |
| 623 | aer_op = &(psdev->pdev->sh_info->aer_op); |
| 624 | aer_op->cmd = aer_cmd ; |
| 625 | /*useful for error_detected callback*/ |
| 626 | aer_op->err = state; |
| 627 | /*pcifront_end BDF*/ |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 628 | ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 629 | &aer_op->domain, &aer_op->bus, &aer_op->devfn); |
| 630 | if (!ret) { |
| 631 | dev_err(&psdev->dev->dev, |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 632 | DRV_NAME ": failed to get pcifront device\n"); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 633 | return PCI_ERS_RESULT_NONE; |
| 634 | } |
| 635 | wmb(); |
| 636 | |
| 637 | dev_dbg(&psdev->dev->dev, |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 638 | DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n", |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 639 | aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn); |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 640 | /*local flag to mark there's aer request, xen_pcibk callback will use |
| 641 | * this flag to judge whether we need to check pci-front give aer |
| 642 | * service ack signal |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 643 | */ |
| 644 | set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags); |
| 645 | |
| 646 | /*It is possible that a pcifront conf_read_write ops request invokes |
| 647 | * the callback which cause the spurious execution of wake_up. |
| 648 | * Yet it is harmless and better than a spinlock here |
| 649 | */ |
| 650 | set_bit(_XEN_PCIB_active, |
| 651 | (unsigned long *)&psdev->pdev->sh_info->flags); |
| 652 | wmb(); |
| 653 | notify_remote_via_irq(psdev->pdev->evtchn_irq); |
| 654 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 655 | ret = wait_event_timeout(xen_pcibk_aer_wait_queue, |
| 656 | !(test_bit(_XEN_PCIB_active, (unsigned long *) |
| 657 | &psdev->pdev->sh_info->flags)), 300*HZ); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 658 | |
| 659 | if (!ret) { |
| 660 | if (test_bit(_XEN_PCIB_active, |
| 661 | (unsigned long *)&psdev->pdev->sh_info->flags)) { |
| 662 | dev_err(&psdev->dev->dev, |
| 663 | "pcifront aer process not responding!\n"); |
| 664 | clear_bit(_XEN_PCIB_active, |
| 665 | (unsigned long *)&psdev->pdev->sh_info->flags); |
| 666 | aer_op->err = PCI_ERS_RESULT_NONE; |
| 667 | return res; |
| 668 | } |
| 669 | } |
| 670 | clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags); |
| 671 | |
| 672 | if (test_bit(_XEN_PCIF_active, |
| 673 | (unsigned long *)&psdev->pdev->sh_info->flags)) { |
| 674 | dev_dbg(&psdev->dev->dev, |
Jan Beulich | 402c5e1 | 2011-09-21 16:22:11 -0400 | [diff] [blame] | 675 | "schedule pci_conf service in " DRV_NAME "\n"); |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 676 | xen_pcibk_test_and_schedule_op(psdev->pdev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | res = (pci_ers_result_t)aer_op->err; |
| 680 | return res; |
| 681 | } |
| 682 | |
| 683 | /* |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 684 | * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 685 | * of the device driver could provide this service, and then wait for pcifront |
| 686 | * ack. |
| 687 | * @dev: pointer to PCI devices |
| 688 | * return value is used by aer_core do_recovery policy |
| 689 | */ |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 690 | static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 691 | { |
| 692 | struct pcistub_device *psdev; |
| 693 | pci_ers_result_t result; |
| 694 | |
| 695 | result = PCI_ERS_RESULT_RECOVERED; |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 696 | dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n", |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 697 | dev->bus->number, dev->devfn); |
| 698 | |
| 699 | down_write(&pcistub_sem); |
| 700 | psdev = pcistub_device_find(pci_domain_nr(dev->bus), |
| 701 | dev->bus->number, |
| 702 | PCI_SLOT(dev->devfn), |
| 703 | PCI_FUNC(dev->devfn)); |
| 704 | |
| 705 | if (!psdev || !psdev->pdev) { |
| 706 | dev_err(&dev->dev, |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 707 | DRV_NAME " device is not found/assigned\n"); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 708 | goto end; |
| 709 | } |
| 710 | |
| 711 | if (!psdev->pdev->sh_info) { |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 712 | dev_err(&dev->dev, DRV_NAME " device is not connected or owned" |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 713 | " by HVM, kill it\n"); |
| 714 | kill_domain_by_device(psdev); |
Jan Beulich | e6aa70a | 2012-09-24 15:55:37 +0100 | [diff] [blame] | 715 | goto end; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | if (!test_bit(_XEN_PCIB_AERHANDLER, |
| 719 | (unsigned long *)&psdev->pdev->sh_info->flags)) { |
| 720 | dev_err(&dev->dev, |
| 721 | "guest with no AER driver should have been killed\n"); |
Jan Beulich | e6aa70a | 2012-09-24 15:55:37 +0100 | [diff] [blame] | 722 | goto end; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 723 | } |
| 724 | result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result); |
| 725 | |
| 726 | if (result == PCI_ERS_RESULT_NONE || |
| 727 | result == PCI_ERS_RESULT_DISCONNECT) { |
| 728 | dev_dbg(&dev->dev, |
| 729 | "No AER slot_reset service or disconnected!\n"); |
| 730 | kill_domain_by_device(psdev); |
| 731 | } |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 732 | end: |
Jan Beulich | e6aa70a | 2012-09-24 15:55:37 +0100 | [diff] [blame] | 733 | if (psdev) |
| 734 | pcistub_device_put(psdev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 735 | up_write(&pcistub_sem); |
| 736 | return result; |
| 737 | |
| 738 | } |
| 739 | |
| 740 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 741 | /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 742 | * in case of the device driver could provide this service, and then wait |
| 743 | * for pcifront ack |
| 744 | * @dev: pointer to PCI devices |
| 745 | * return value is used by aer_core do_recovery policy |
| 746 | */ |
| 747 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 748 | static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 749 | { |
| 750 | struct pcistub_device *psdev; |
| 751 | pci_ers_result_t result; |
| 752 | |
| 753 | result = PCI_ERS_RESULT_RECOVERED; |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 754 | dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n", |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 755 | dev->bus->number, dev->devfn); |
| 756 | |
| 757 | down_write(&pcistub_sem); |
| 758 | psdev = pcistub_device_find(pci_domain_nr(dev->bus), |
| 759 | dev->bus->number, |
| 760 | PCI_SLOT(dev->devfn), |
| 761 | PCI_FUNC(dev->devfn)); |
| 762 | |
| 763 | if (!psdev || !psdev->pdev) { |
| 764 | dev_err(&dev->dev, |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 765 | DRV_NAME " device is not found/assigned\n"); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 766 | goto end; |
| 767 | } |
| 768 | |
| 769 | if (!psdev->pdev->sh_info) { |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 770 | dev_err(&dev->dev, DRV_NAME " device is not connected or owned" |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 771 | " by HVM, kill it\n"); |
| 772 | kill_domain_by_device(psdev); |
Jan Beulich | e6aa70a | 2012-09-24 15:55:37 +0100 | [diff] [blame] | 773 | goto end; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | if (!test_bit(_XEN_PCIB_AERHANDLER, |
| 777 | (unsigned long *)&psdev->pdev->sh_info->flags)) { |
| 778 | dev_err(&dev->dev, |
| 779 | "guest with no AER driver should have been killed\n"); |
Jan Beulich | e6aa70a | 2012-09-24 15:55:37 +0100 | [diff] [blame] | 780 | goto end; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 781 | } |
| 782 | result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result); |
| 783 | |
| 784 | if (result == PCI_ERS_RESULT_NONE || |
| 785 | result == PCI_ERS_RESULT_DISCONNECT) { |
| 786 | dev_dbg(&dev->dev, |
| 787 | "No AER mmio_enabled service or disconnected!\n"); |
| 788 | kill_domain_by_device(psdev); |
| 789 | } |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 790 | end: |
Jan Beulich | e6aa70a | 2012-09-24 15:55:37 +0100 | [diff] [blame] | 791 | if (psdev) |
| 792 | pcistub_device_put(psdev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 793 | up_write(&pcistub_sem); |
| 794 | return result; |
| 795 | } |
| 796 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 797 | /*xen_pcibk_error_detected: it will send the error_detected request to pcifront |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 798 | * in case of the device driver could provide this service, and then wait |
| 799 | * for pcifront ack. |
| 800 | * @dev: pointer to PCI devices |
| 801 | * @error: the current PCI connection state |
| 802 | * return value is used by aer_core do_recovery policy |
| 803 | */ |
| 804 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 805 | static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 806 | pci_channel_state_t error) |
| 807 | { |
| 808 | struct pcistub_device *psdev; |
| 809 | pci_ers_result_t result; |
| 810 | |
| 811 | result = PCI_ERS_RESULT_CAN_RECOVER; |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 812 | dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n", |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 813 | dev->bus->number, dev->devfn); |
| 814 | |
| 815 | down_write(&pcistub_sem); |
| 816 | psdev = pcistub_device_find(pci_domain_nr(dev->bus), |
| 817 | dev->bus->number, |
| 818 | PCI_SLOT(dev->devfn), |
| 819 | PCI_FUNC(dev->devfn)); |
| 820 | |
| 821 | if (!psdev || !psdev->pdev) { |
| 822 | dev_err(&dev->dev, |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 823 | DRV_NAME " device is not found/assigned\n"); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 824 | goto end; |
| 825 | } |
| 826 | |
| 827 | if (!psdev->pdev->sh_info) { |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 828 | dev_err(&dev->dev, DRV_NAME " device is not connected or owned" |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 829 | " by HVM, kill it\n"); |
| 830 | kill_domain_by_device(psdev); |
Jan Beulich | e6aa70a | 2012-09-24 15:55:37 +0100 | [diff] [blame] | 831 | goto end; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | /*Guest owns the device yet no aer handler regiested, kill guest*/ |
| 835 | if (!test_bit(_XEN_PCIB_AERHANDLER, |
| 836 | (unsigned long *)&psdev->pdev->sh_info->flags)) { |
| 837 | dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n"); |
| 838 | kill_domain_by_device(psdev); |
Jan Beulich | e6aa70a | 2012-09-24 15:55:37 +0100 | [diff] [blame] | 839 | goto end; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 840 | } |
| 841 | result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result); |
| 842 | |
| 843 | if (result == PCI_ERS_RESULT_NONE || |
| 844 | result == PCI_ERS_RESULT_DISCONNECT) { |
| 845 | dev_dbg(&dev->dev, |
| 846 | "No AER error_detected service or disconnected!\n"); |
| 847 | kill_domain_by_device(psdev); |
| 848 | } |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 849 | end: |
Jan Beulich | e6aa70a | 2012-09-24 15:55:37 +0100 | [diff] [blame] | 850 | if (psdev) |
| 851 | pcistub_device_put(psdev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 852 | up_write(&pcistub_sem); |
| 853 | return result; |
| 854 | } |
| 855 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 856 | /*xen_pcibk_error_resume: it will send the error_resume request to pcifront |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 857 | * in case of the device driver could provide this service, and then wait |
| 858 | * for pcifront ack. |
| 859 | * @dev: pointer to PCI devices |
| 860 | */ |
| 861 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 862 | static void xen_pcibk_error_resume(struct pci_dev *dev) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 863 | { |
| 864 | struct pcistub_device *psdev; |
| 865 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 866 | dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n", |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 867 | dev->bus->number, dev->devfn); |
| 868 | |
| 869 | down_write(&pcistub_sem); |
| 870 | psdev = pcistub_device_find(pci_domain_nr(dev->bus), |
| 871 | dev->bus->number, |
| 872 | PCI_SLOT(dev->devfn), |
| 873 | PCI_FUNC(dev->devfn)); |
| 874 | |
| 875 | if (!psdev || !psdev->pdev) { |
| 876 | dev_err(&dev->dev, |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 877 | DRV_NAME " device is not found/assigned\n"); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 878 | goto end; |
| 879 | } |
| 880 | |
| 881 | if (!psdev->pdev->sh_info) { |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 882 | dev_err(&dev->dev, DRV_NAME " device is not connected or owned" |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 883 | " by HVM, kill it\n"); |
| 884 | kill_domain_by_device(psdev); |
Jan Beulich | e6aa70a | 2012-09-24 15:55:37 +0100 | [diff] [blame] | 885 | goto end; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | if (!test_bit(_XEN_PCIB_AERHANDLER, |
| 889 | (unsigned long *)&psdev->pdev->sh_info->flags)) { |
| 890 | dev_err(&dev->dev, |
| 891 | "guest with no AER driver should have been killed\n"); |
| 892 | kill_domain_by_device(psdev); |
Jan Beulich | e6aa70a | 2012-09-24 15:55:37 +0100 | [diff] [blame] | 893 | goto end; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 894 | } |
| 895 | common_process(psdev, 1, XEN_PCI_OP_aer_resume, |
| 896 | PCI_ERS_RESULT_RECOVERED); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 897 | end: |
Jan Beulich | e6aa70a | 2012-09-24 15:55:37 +0100 | [diff] [blame] | 898 | if (psdev) |
| 899 | pcistub_device_put(psdev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 900 | up_write(&pcistub_sem); |
| 901 | return; |
| 902 | } |
| 903 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 904 | /*add xen_pcibk AER handling*/ |
Stephen Hemminger | 1d35203 | 2012-09-07 09:33:17 -0700 | [diff] [blame] | 905 | static const struct pci_error_handlers xen_pcibk_error_handler = { |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 906 | .error_detected = xen_pcibk_error_detected, |
| 907 | .mmio_enabled = xen_pcibk_mmio_enabled, |
| 908 | .slot_reset = xen_pcibk_slot_reset, |
| 909 | .resume = xen_pcibk_error_resume, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 910 | }; |
| 911 | |
| 912 | /* |
| 913 | * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't |
| 914 | * for a normal device. I don't want it to be loaded automatically. |
| 915 | */ |
| 916 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 917 | static struct pci_driver xen_pcibk_pci_driver = { |
| 918 | /* The name should be xen_pciback, but until the tools are updated |
| 919 | * we will keep it as pciback. */ |
| 920 | .name = "pciback", |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 921 | .id_table = pcistub_ids, |
| 922 | .probe = pcistub_probe, |
| 923 | .remove = pcistub_remove, |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 924 | .err_handler = &xen_pcibk_error_handler, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 925 | }; |
| 926 | |
| 927 | static inline int str_to_slot(const char *buf, int *domain, int *bus, |
| 928 | int *slot, int *func) |
| 929 | { |
Jan Beulich | 5b71fbd | 2012-11-02 14:36:38 +0000 | [diff] [blame] | 930 | int parsed = 0; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 931 | |
Jan Beulich | 5b71fbd | 2012-11-02 14:36:38 +0000 | [diff] [blame] | 932 | switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func, |
| 933 | &parsed)) { |
Jan Beulich | c3cb470 | 2012-09-18 12:29:03 +0100 | [diff] [blame] | 934 | case 3: |
| 935 | *func = -1; |
Jan Beulich | 5b71fbd | 2012-11-02 14:36:38 +0000 | [diff] [blame] | 936 | sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed); |
Jan Beulich | c3cb470 | 2012-09-18 12:29:03 +0100 | [diff] [blame] | 937 | break; |
| 938 | case 2: |
| 939 | *slot = *func = -1; |
Jan Beulich | 5b71fbd | 2012-11-02 14:36:38 +0000 | [diff] [blame] | 940 | sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed); |
Jan Beulich | c3cb470 | 2012-09-18 12:29:03 +0100 | [diff] [blame] | 941 | break; |
| 942 | } |
Jan Beulich | 5b71fbd | 2012-11-02 14:36:38 +0000 | [diff] [blame] | 943 | if (parsed && !buf[parsed]) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 944 | return 0; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 945 | |
| 946 | /* try again without domain */ |
| 947 | *domain = 0; |
Jan Beulich | 5b71fbd | 2012-11-02 14:36:38 +0000 | [diff] [blame] | 948 | switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) { |
Jan Beulich | c3cb470 | 2012-09-18 12:29:03 +0100 | [diff] [blame] | 949 | case 2: |
| 950 | *func = -1; |
Jan Beulich | 5b71fbd | 2012-11-02 14:36:38 +0000 | [diff] [blame] | 951 | sscanf(buf, " %x:%x.* %n", bus, slot, &parsed); |
Jan Beulich | c3cb470 | 2012-09-18 12:29:03 +0100 | [diff] [blame] | 952 | break; |
| 953 | case 1: |
| 954 | *slot = *func = -1; |
Jan Beulich | 5b71fbd | 2012-11-02 14:36:38 +0000 | [diff] [blame] | 955 | sscanf(buf, " %x:*.* %n", bus, &parsed); |
Jan Beulich | c3cb470 | 2012-09-18 12:29:03 +0100 | [diff] [blame] | 956 | break; |
| 957 | } |
Jan Beulich | 5b71fbd | 2012-11-02 14:36:38 +0000 | [diff] [blame] | 958 | if (parsed && !buf[parsed]) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 959 | return 0; |
| 960 | |
| 961 | return -EINVAL; |
| 962 | } |
| 963 | |
| 964 | static inline int str_to_quirk(const char *buf, int *domain, int *bus, int |
| 965 | *slot, int *func, int *reg, int *size, int *mask) |
| 966 | { |
Jan Beulich | 5b71fbd | 2012-11-02 14:36:38 +0000 | [diff] [blame] | 967 | int parsed = 0; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 968 | |
Jan Beulich | b3e40b7 | 2012-11-02 14:37:13 +0000 | [diff] [blame] | 969 | sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func, |
Jan Beulich | 5b71fbd | 2012-11-02 14:36:38 +0000 | [diff] [blame] | 970 | reg, size, mask, &parsed); |
| 971 | if (parsed && !buf[parsed]) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 972 | return 0; |
Jan Beulich | 5b71fbd | 2012-11-02 14:36:38 +0000 | [diff] [blame] | 973 | |
| 974 | /* try again without domain */ |
| 975 | *domain = 0; |
Jan Beulich | b3e40b7 | 2012-11-02 14:37:13 +0000 | [diff] [blame] | 976 | sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size, |
Jan Beulich | 5b71fbd | 2012-11-02 14:36:38 +0000 | [diff] [blame] | 977 | mask, &parsed); |
| 978 | if (parsed && !buf[parsed]) |
| 979 | return 0; |
| 980 | |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 981 | return -EINVAL; |
| 982 | } |
| 983 | |
| 984 | static int pcistub_device_id_add(int domain, int bus, int slot, int func) |
| 985 | { |
| 986 | struct pcistub_device_id *pci_dev_id; |
| 987 | unsigned long flags; |
Jan Beulich | b3e40b7 | 2012-11-02 14:37:13 +0000 | [diff] [blame] | 988 | int rc = 0, devfn = PCI_DEVFN(slot, func); |
Jan Beulich | c3cb470 | 2012-09-18 12:29:03 +0100 | [diff] [blame] | 989 | |
| 990 | if (slot < 0) { |
| 991 | for (slot = 0; !rc && slot < 32; ++slot) |
| 992 | rc = pcistub_device_id_add(domain, bus, slot, func); |
| 993 | return rc; |
| 994 | } |
| 995 | |
| 996 | if (func < 0) { |
| 997 | for (func = 0; !rc && func < 8; ++func) |
| 998 | rc = pcistub_device_id_add(domain, bus, slot, func); |
| 999 | return rc; |
| 1000 | } |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1001 | |
Jan Beulich | b3e40b7 | 2012-11-02 14:37:13 +0000 | [diff] [blame] | 1002 | if (( |
| 1003 | #if !defined(MODULE) /* pci_domains_supported is not being exported */ \ |
| 1004 | || !defined(CONFIG_PCI_DOMAINS) |
| 1005 | !pci_domains_supported ? domain : |
| 1006 | #endif |
| 1007 | domain < 0 || domain > 0xffff) |
| 1008 | || bus < 0 || bus > 0xff |
| 1009 | || PCI_SLOT(devfn) != slot |
| 1010 | || PCI_FUNC(devfn) != func) |
| 1011 | return -EINVAL; |
| 1012 | |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1013 | pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL); |
| 1014 | if (!pci_dev_id) |
| 1015 | return -ENOMEM; |
| 1016 | |
| 1017 | pci_dev_id->domain = domain; |
| 1018 | pci_dev_id->bus = bus; |
Jan Beulich | b3e40b7 | 2012-11-02 14:37:13 +0000 | [diff] [blame] | 1019 | pci_dev_id->devfn = devfn; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1020 | |
Konrad Rzeszutek Wilk | e4de866 | 2012-01-25 16:00:00 -0500 | [diff] [blame] | 1021 | pr_debug(DRV_NAME ": wants to seize %04x:%02x:%02x.%d\n", |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1022 | domain, bus, slot, func); |
| 1023 | |
| 1024 | spin_lock_irqsave(&device_ids_lock, flags); |
| 1025 | list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids); |
| 1026 | spin_unlock_irqrestore(&device_ids_lock, flags); |
| 1027 | |
| 1028 | return 0; |
| 1029 | } |
| 1030 | |
| 1031 | static int pcistub_device_id_remove(int domain, int bus, int slot, int func) |
| 1032 | { |
| 1033 | struct pcistub_device_id *pci_dev_id, *t; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1034 | int err = -ENOENT; |
| 1035 | unsigned long flags; |
| 1036 | |
| 1037 | spin_lock_irqsave(&device_ids_lock, flags); |
| 1038 | list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids, |
| 1039 | slot_list) { |
Jan Beulich | c3cb470 | 2012-09-18 12:29:03 +0100 | [diff] [blame] | 1040 | if (pci_dev_id->domain == domain && pci_dev_id->bus == bus |
| 1041 | && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot) |
| 1042 | && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) { |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1043 | /* Don't break; here because it's possible the same |
| 1044 | * slot could be in the list more than once |
| 1045 | */ |
| 1046 | list_del(&pci_dev_id->slot_list); |
| 1047 | kfree(pci_dev_id); |
| 1048 | |
| 1049 | err = 0; |
| 1050 | |
Konrad Rzeszutek Wilk | e4de866 | 2012-01-25 16:00:00 -0500 | [diff] [blame] | 1051 | pr_debug(DRV_NAME ": removed %04x:%02x:%02x.%d from " |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1052 | "seize list\n", domain, bus, slot, func); |
| 1053 | } |
| 1054 | } |
| 1055 | spin_unlock_irqrestore(&device_ids_lock, flags); |
| 1056 | |
| 1057 | return err; |
| 1058 | } |
| 1059 | |
Jan Beulich | b3e40b7 | 2012-11-02 14:37:13 +0000 | [diff] [blame] | 1060 | static int pcistub_reg_add(int domain, int bus, int slot, int func, |
| 1061 | unsigned int reg, unsigned int size, |
| 1062 | unsigned int mask) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1063 | { |
| 1064 | int err = 0; |
| 1065 | struct pcistub_device *psdev; |
| 1066 | struct pci_dev *dev; |
| 1067 | struct config_field *field; |
| 1068 | |
Jan Beulich | b3e40b7 | 2012-11-02 14:37:13 +0000 | [diff] [blame] | 1069 | if (reg > 0xfff || (size < 4 && (mask >> (size * 8)))) |
| 1070 | return -EINVAL; |
| 1071 | |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1072 | psdev = pcistub_device_find(domain, bus, slot, func); |
Jan Beulich | e6aa70a | 2012-09-24 15:55:37 +0100 | [diff] [blame] | 1073 | if (!psdev) { |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1074 | err = -ENODEV; |
| 1075 | goto out; |
| 1076 | } |
| 1077 | dev = psdev->dev; |
| 1078 | |
| 1079 | field = kzalloc(sizeof(*field), GFP_ATOMIC); |
| 1080 | if (!field) { |
| 1081 | err = -ENOMEM; |
| 1082 | goto out; |
| 1083 | } |
| 1084 | |
| 1085 | field->offset = reg; |
| 1086 | field->size = size; |
| 1087 | field->mask = mask; |
| 1088 | field->init = NULL; |
| 1089 | field->reset = NULL; |
| 1090 | field->release = NULL; |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1091 | field->clean = xen_pcibk_config_field_free; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1092 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1093 | err = xen_pcibk_config_quirks_add_field(dev, field); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1094 | if (err) |
| 1095 | kfree(field); |
| 1096 | out: |
Jan Beulich | e6aa70a | 2012-09-24 15:55:37 +0100 | [diff] [blame] | 1097 | if (psdev) |
| 1098 | pcistub_device_put(psdev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1099 | return err; |
| 1100 | } |
| 1101 | |
| 1102 | static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf, |
| 1103 | size_t count) |
| 1104 | { |
| 1105 | int domain, bus, slot, func; |
| 1106 | int err; |
| 1107 | |
| 1108 | err = str_to_slot(buf, &domain, &bus, &slot, &func); |
| 1109 | if (err) |
| 1110 | goto out; |
| 1111 | |
| 1112 | err = pcistub_device_id_add(domain, bus, slot, func); |
| 1113 | |
| 1114 | out: |
| 1115 | if (!err) |
| 1116 | err = count; |
| 1117 | return err; |
| 1118 | } |
Jan Beulich | 402c5e1 | 2011-09-21 16:22:11 -0400 | [diff] [blame] | 1119 | static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1120 | |
| 1121 | static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf, |
| 1122 | size_t count) |
| 1123 | { |
| 1124 | int domain, bus, slot, func; |
| 1125 | int err; |
| 1126 | |
| 1127 | err = str_to_slot(buf, &domain, &bus, &slot, &func); |
| 1128 | if (err) |
| 1129 | goto out; |
| 1130 | |
| 1131 | err = pcistub_device_id_remove(domain, bus, slot, func); |
| 1132 | |
| 1133 | out: |
| 1134 | if (!err) |
| 1135 | err = count; |
| 1136 | return err; |
| 1137 | } |
Jan Beulich | 402c5e1 | 2011-09-21 16:22:11 -0400 | [diff] [blame] | 1138 | static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1139 | |
| 1140 | static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf) |
| 1141 | { |
| 1142 | struct pcistub_device_id *pci_dev_id; |
| 1143 | size_t count = 0; |
| 1144 | unsigned long flags; |
| 1145 | |
| 1146 | spin_lock_irqsave(&device_ids_lock, flags); |
| 1147 | list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) { |
| 1148 | if (count >= PAGE_SIZE) |
| 1149 | break; |
| 1150 | |
| 1151 | count += scnprintf(buf + count, PAGE_SIZE - count, |
Konrad Rzeszutek Wilk | e4de866 | 2012-01-25 16:00:00 -0500 | [diff] [blame] | 1152 | "%04x:%02x:%02x.%d\n", |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1153 | pci_dev_id->domain, pci_dev_id->bus, |
| 1154 | PCI_SLOT(pci_dev_id->devfn), |
| 1155 | PCI_FUNC(pci_dev_id->devfn)); |
| 1156 | } |
| 1157 | spin_unlock_irqrestore(&device_ids_lock, flags); |
| 1158 | |
| 1159 | return count; |
| 1160 | } |
Jan Beulich | 402c5e1 | 2011-09-21 16:22:11 -0400 | [diff] [blame] | 1161 | static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1162 | |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1163 | static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf) |
| 1164 | { |
| 1165 | struct pcistub_device *psdev; |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1166 | struct xen_pcibk_dev_data *dev_data; |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1167 | size_t count = 0; |
| 1168 | unsigned long flags; |
| 1169 | |
| 1170 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 1171 | list_for_each_entry(psdev, &pcistub_devices, dev_list) { |
| 1172 | if (count >= PAGE_SIZE) |
| 1173 | break; |
| 1174 | if (!psdev->dev) |
| 1175 | continue; |
| 1176 | dev_data = pci_get_drvdata(psdev->dev); |
| 1177 | if (!dev_data) |
| 1178 | continue; |
| 1179 | count += |
| 1180 | scnprintf(buf + count, PAGE_SIZE - count, |
| 1181 | "%s:%s:%sing:%ld\n", |
| 1182 | pci_name(psdev->dev), |
| 1183 | dev_data->isr_on ? "on" : "off", |
| 1184 | dev_data->ack_intr ? "ack" : "not ack", |
| 1185 | dev_data->handled); |
| 1186 | } |
| 1187 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 1188 | return count; |
| 1189 | } |
Jan Beulich | 402c5e1 | 2011-09-21 16:22:11 -0400 | [diff] [blame] | 1190 | static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL); |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1191 | |
| 1192 | static ssize_t pcistub_irq_handler_switch(struct device_driver *drv, |
| 1193 | const char *buf, |
| 1194 | size_t count) |
| 1195 | { |
| 1196 | struct pcistub_device *psdev; |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1197 | struct xen_pcibk_dev_data *dev_data; |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1198 | int domain, bus, slot, func; |
Wei Yongjun | 405010d | 2013-05-31 19:59:20 +0800 | [diff] [blame] | 1199 | int err; |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1200 | |
| 1201 | err = str_to_slot(buf, &domain, &bus, &slot, &func); |
| 1202 | if (err) |
Jan Beulich | e6aa70a | 2012-09-24 15:55:37 +0100 | [diff] [blame] | 1203 | return err; |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1204 | |
| 1205 | psdev = pcistub_device_find(domain, bus, slot, func); |
Wei Yongjun | 405010d | 2013-05-31 19:59:20 +0800 | [diff] [blame] | 1206 | if (!psdev) { |
| 1207 | err = -ENOENT; |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1208 | goto out; |
Wei Yongjun | 405010d | 2013-05-31 19:59:20 +0800 | [diff] [blame] | 1209 | } |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1210 | |
| 1211 | dev_data = pci_get_drvdata(psdev->dev); |
Wei Yongjun | 405010d | 2013-05-31 19:59:20 +0800 | [diff] [blame] | 1212 | if (!dev_data) { |
| 1213 | err = -ENOENT; |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1214 | goto out; |
Wei Yongjun | 405010d | 2013-05-31 19:59:20 +0800 | [diff] [blame] | 1215 | } |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1216 | |
| 1217 | dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n", |
| 1218 | dev_data->irq_name, dev_data->isr_on, |
| 1219 | !dev_data->isr_on); |
| 1220 | |
| 1221 | dev_data->isr_on = !(dev_data->isr_on); |
| 1222 | if (dev_data->isr_on) |
| 1223 | dev_data->ack_intr = 1; |
| 1224 | out: |
Jan Beulich | e6aa70a | 2012-09-24 15:55:37 +0100 | [diff] [blame] | 1225 | if (psdev) |
| 1226 | pcistub_device_put(psdev); |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1227 | if (!err) |
| 1228 | err = count; |
| 1229 | return err; |
| 1230 | } |
Jan Beulich | 402c5e1 | 2011-09-21 16:22:11 -0400 | [diff] [blame] | 1231 | static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL, |
| 1232 | pcistub_irq_handler_switch); |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1233 | |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1234 | static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf, |
| 1235 | size_t count) |
| 1236 | { |
| 1237 | int domain, bus, slot, func, reg, size, mask; |
| 1238 | int err; |
| 1239 | |
| 1240 | err = str_to_quirk(buf, &domain, &bus, &slot, &func, ®, &size, |
| 1241 | &mask); |
| 1242 | if (err) |
| 1243 | goto out; |
| 1244 | |
| 1245 | err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask); |
| 1246 | |
| 1247 | out: |
| 1248 | if (!err) |
| 1249 | err = count; |
| 1250 | return err; |
| 1251 | } |
| 1252 | |
| 1253 | static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf) |
| 1254 | { |
| 1255 | int count = 0; |
| 1256 | unsigned long flags; |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1257 | struct xen_pcibk_config_quirk *quirk; |
| 1258 | struct xen_pcibk_dev_data *dev_data; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1259 | const struct config_field *field; |
| 1260 | const struct config_field_entry *cfg_entry; |
| 1261 | |
| 1262 | spin_lock_irqsave(&device_ids_lock, flags); |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1263 | list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) { |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1264 | if (count >= PAGE_SIZE) |
| 1265 | goto out; |
| 1266 | |
| 1267 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 1268 | "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n", |
| 1269 | quirk->pdev->bus->number, |
| 1270 | PCI_SLOT(quirk->pdev->devfn), |
| 1271 | PCI_FUNC(quirk->pdev->devfn), |
| 1272 | quirk->devid.vendor, quirk->devid.device, |
| 1273 | quirk->devid.subvendor, |
| 1274 | quirk->devid.subdevice); |
| 1275 | |
| 1276 | dev_data = pci_get_drvdata(quirk->pdev); |
| 1277 | |
| 1278 | list_for_each_entry(cfg_entry, &dev_data->config_fields, list) { |
| 1279 | field = cfg_entry->field; |
| 1280 | if (count >= PAGE_SIZE) |
| 1281 | goto out; |
| 1282 | |
| 1283 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 1284 | "\t\t%08x:%01x:%08x\n", |
| 1285 | cfg_entry->base_offset + |
| 1286 | field->offset, field->size, |
| 1287 | field->mask); |
| 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | out: |
| 1292 | spin_unlock_irqrestore(&device_ids_lock, flags); |
| 1293 | |
| 1294 | return count; |
| 1295 | } |
Jan Beulich | 402c5e1 | 2011-09-21 16:22:11 -0400 | [diff] [blame] | 1296 | static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show, |
| 1297 | pcistub_quirk_add); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1298 | |
| 1299 | static ssize_t permissive_add(struct device_driver *drv, const char *buf, |
| 1300 | size_t count) |
| 1301 | { |
| 1302 | int domain, bus, slot, func; |
| 1303 | int err; |
| 1304 | struct pcistub_device *psdev; |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1305 | struct xen_pcibk_dev_data *dev_data; |
Jan Beulich | b3e40b7 | 2012-11-02 14:37:13 +0000 | [diff] [blame] | 1306 | |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1307 | err = str_to_slot(buf, &domain, &bus, &slot, &func); |
| 1308 | if (err) |
| 1309 | goto out; |
Jan Beulich | b3e40b7 | 2012-11-02 14:37:13 +0000 | [diff] [blame] | 1310 | |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1311 | psdev = pcistub_device_find(domain, bus, slot, func); |
| 1312 | if (!psdev) { |
| 1313 | err = -ENODEV; |
| 1314 | goto out; |
| 1315 | } |
Jan Beulich | e6aa70a | 2012-09-24 15:55:37 +0100 | [diff] [blame] | 1316 | |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1317 | dev_data = pci_get_drvdata(psdev->dev); |
| 1318 | /* the driver data for a device should never be null at this point */ |
| 1319 | if (!dev_data) { |
| 1320 | err = -ENXIO; |
| 1321 | goto release; |
| 1322 | } |
| 1323 | if (!dev_data->permissive) { |
| 1324 | dev_data->permissive = 1; |
| 1325 | /* Let user know that what they're doing could be unsafe */ |
| 1326 | dev_warn(&psdev->dev->dev, "enabling permissive mode " |
| 1327 | "configuration space accesses!\n"); |
| 1328 | dev_warn(&psdev->dev->dev, |
| 1329 | "permissive mode is potentially unsafe!\n"); |
| 1330 | } |
| 1331 | release: |
| 1332 | pcistub_device_put(psdev); |
| 1333 | out: |
| 1334 | if (!err) |
| 1335 | err = count; |
| 1336 | return err; |
| 1337 | } |
| 1338 | |
| 1339 | static ssize_t permissive_show(struct device_driver *drv, char *buf) |
| 1340 | { |
| 1341 | struct pcistub_device *psdev; |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1342 | struct xen_pcibk_dev_data *dev_data; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1343 | size_t count = 0; |
| 1344 | unsigned long flags; |
| 1345 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 1346 | list_for_each_entry(psdev, &pcistub_devices, dev_list) { |
| 1347 | if (count >= PAGE_SIZE) |
| 1348 | break; |
| 1349 | if (!psdev->dev) |
| 1350 | continue; |
| 1351 | dev_data = pci_get_drvdata(psdev->dev); |
| 1352 | if (!dev_data || !dev_data->permissive) |
| 1353 | continue; |
| 1354 | count += |
| 1355 | scnprintf(buf + count, PAGE_SIZE - count, "%s\n", |
| 1356 | pci_name(psdev->dev)); |
| 1357 | } |
| 1358 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 1359 | return count; |
| 1360 | } |
Jan Beulich | 402c5e1 | 2011-09-21 16:22:11 -0400 | [diff] [blame] | 1361 | static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show, |
| 1362 | permissive_add); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1363 | |
| 1364 | static void pcistub_exit(void) |
| 1365 | { |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1366 | driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot); |
| 1367 | driver_remove_file(&xen_pcibk_pci_driver.driver, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1368 | &driver_attr_remove_slot); |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1369 | driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots); |
| 1370 | driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks); |
| 1371 | driver_remove_file(&xen_pcibk_pci_driver.driver, |
| 1372 | &driver_attr_permissive); |
| 1373 | driver_remove_file(&xen_pcibk_pci_driver.driver, |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1374 | &driver_attr_irq_handlers); |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1375 | driver_remove_file(&xen_pcibk_pci_driver.driver, |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1376 | &driver_attr_irq_handler_state); |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1377 | pci_unregister_driver(&xen_pcibk_pci_driver); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | static int __init pcistub_init(void) |
| 1381 | { |
| 1382 | int pos = 0; |
| 1383 | int err = 0; |
| 1384 | int domain, bus, slot, func; |
| 1385 | int parsed; |
| 1386 | |
| 1387 | if (pci_devs_to_hide && *pci_devs_to_hide) { |
| 1388 | do { |
| 1389 | parsed = 0; |
| 1390 | |
| 1391 | err = sscanf(pci_devs_to_hide + pos, |
| 1392 | " (%x:%x:%x.%x) %n", |
| 1393 | &domain, &bus, &slot, &func, &parsed); |
Jan Beulich | c3cb470 | 2012-09-18 12:29:03 +0100 | [diff] [blame] | 1394 | switch (err) { |
| 1395 | case 3: |
| 1396 | func = -1; |
Jan Beulich | 5b71fbd | 2012-11-02 14:36:38 +0000 | [diff] [blame] | 1397 | sscanf(pci_devs_to_hide + pos, |
| 1398 | " (%x:%x:%x.*) %n", |
| 1399 | &domain, &bus, &slot, &parsed); |
Jan Beulich | c3cb470 | 2012-09-18 12:29:03 +0100 | [diff] [blame] | 1400 | break; |
| 1401 | case 2: |
| 1402 | slot = func = -1; |
Jan Beulich | 5b71fbd | 2012-11-02 14:36:38 +0000 | [diff] [blame] | 1403 | sscanf(pci_devs_to_hide + pos, |
| 1404 | " (%x:%x:*.*) %n", |
| 1405 | &domain, &bus, &parsed); |
Jan Beulich | c3cb470 | 2012-09-18 12:29:03 +0100 | [diff] [blame] | 1406 | break; |
| 1407 | } |
| 1408 | |
Jan Beulich | 5b71fbd | 2012-11-02 14:36:38 +0000 | [diff] [blame] | 1409 | if (!parsed) { |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1410 | domain = 0; |
| 1411 | err = sscanf(pci_devs_to_hide + pos, |
| 1412 | " (%x:%x.%x) %n", |
| 1413 | &bus, &slot, &func, &parsed); |
Jan Beulich | c3cb470 | 2012-09-18 12:29:03 +0100 | [diff] [blame] | 1414 | switch (err) { |
| 1415 | case 2: |
| 1416 | func = -1; |
Jan Beulich | 5b71fbd | 2012-11-02 14:36:38 +0000 | [diff] [blame] | 1417 | sscanf(pci_devs_to_hide + pos, |
| 1418 | " (%x:%x.*) %n", |
| 1419 | &bus, &slot, &parsed); |
Jan Beulich | c3cb470 | 2012-09-18 12:29:03 +0100 | [diff] [blame] | 1420 | break; |
| 1421 | case 1: |
| 1422 | slot = func = -1; |
Jan Beulich | 5b71fbd | 2012-11-02 14:36:38 +0000 | [diff] [blame] | 1423 | sscanf(pci_devs_to_hide + pos, |
| 1424 | " (%x:*.*) %n", |
| 1425 | &bus, &parsed); |
Jan Beulich | c3cb470 | 2012-09-18 12:29:03 +0100 | [diff] [blame] | 1426 | break; |
| 1427 | } |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1428 | } |
| 1429 | |
Jan Beulich | 5b71fbd | 2012-11-02 14:36:38 +0000 | [diff] [blame] | 1430 | if (parsed <= 0) |
| 1431 | goto parse_error; |
| 1432 | |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1433 | err = pcistub_device_id_add(domain, bus, slot, func); |
| 1434 | if (err) |
| 1435 | goto out; |
| 1436 | |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1437 | pos += parsed; |
Jan Beulich | 5b71fbd | 2012-11-02 14:36:38 +0000 | [diff] [blame] | 1438 | } while (pci_devs_to_hide[pos]); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | /* If we're the first PCI Device Driver to register, we're the |
| 1442 | * first one to get offered PCI devices as they become |
| 1443 | * available (and thus we can be the first to grab them) |
| 1444 | */ |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1445 | err = pci_register_driver(&xen_pcibk_pci_driver); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1446 | if (err < 0) |
| 1447 | goto out; |
| 1448 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1449 | err = driver_create_file(&xen_pcibk_pci_driver.driver, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1450 | &driver_attr_new_slot); |
| 1451 | if (!err) |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1452 | err = driver_create_file(&xen_pcibk_pci_driver.driver, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1453 | &driver_attr_remove_slot); |
| 1454 | if (!err) |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1455 | err = driver_create_file(&xen_pcibk_pci_driver.driver, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1456 | &driver_attr_slots); |
| 1457 | if (!err) |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1458 | err = driver_create_file(&xen_pcibk_pci_driver.driver, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1459 | &driver_attr_quirks); |
| 1460 | if (!err) |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1461 | err = driver_create_file(&xen_pcibk_pci_driver.driver, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1462 | &driver_attr_permissive); |
| 1463 | |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1464 | if (!err) |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1465 | err = driver_create_file(&xen_pcibk_pci_driver.driver, |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1466 | &driver_attr_irq_handlers); |
| 1467 | if (!err) |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1468 | err = driver_create_file(&xen_pcibk_pci_driver.driver, |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1469 | &driver_attr_irq_handler_state); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1470 | if (err) |
| 1471 | pcistub_exit(); |
| 1472 | |
| 1473 | out: |
| 1474 | return err; |
| 1475 | |
| 1476 | parse_error: |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1477 | printk(KERN_ERR DRV_NAME ": Error parsing pci_devs_to_hide at \"%s\"\n", |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1478 | pci_devs_to_hide + pos); |
| 1479 | return -EINVAL; |
| 1480 | } |
| 1481 | |
| 1482 | #ifndef MODULE |
| 1483 | /* |
| 1484 | * fs_initcall happens before device_initcall |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1485 | * so xen_pcibk *should* get called first (b/c we |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1486 | * want to suck up any device before other drivers |
| 1487 | * get a chance by being the first pci device |
| 1488 | * driver to register) |
| 1489 | */ |
| 1490 | fs_initcall(pcistub_init); |
| 1491 | #endif |
| 1492 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1493 | static int __init xen_pcibk_init(void) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1494 | { |
| 1495 | int err; |
| 1496 | |
| 1497 | if (!xen_initial_domain()) |
| 1498 | return -ENODEV; |
| 1499 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1500 | err = xen_pcibk_config_init(); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1501 | if (err) |
| 1502 | return err; |
| 1503 | |
| 1504 | #ifdef MODULE |
| 1505 | err = pcistub_init(); |
| 1506 | if (err < 0) |
| 1507 | return err; |
| 1508 | #endif |
| 1509 | |
| 1510 | pcistub_init_devices_late(); |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1511 | err = xen_pcibk_xenbus_register(); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1512 | if (err) |
| 1513 | pcistub_exit(); |
| 1514 | |
| 1515 | return err; |
| 1516 | } |
| 1517 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1518 | static void __exit xen_pcibk_cleanup(void) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1519 | { |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1520 | xen_pcibk_xenbus_unregister(); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1521 | pcistub_exit(); |
| 1522 | } |
| 1523 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1524 | module_init(xen_pcibk_init); |
| 1525 | module_exit(xen_pcibk_cleanup); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1526 | |
| 1527 | MODULE_LICENSE("Dual BSD/GPL"); |
Jan Beulich | 402c5e1 | 2011-09-21 16:22:11 -0400 | [diff] [blame] | 1528 | MODULE_ALIAS("xen-backend:pci"); |