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