1956a9202SRyan Wilson /* 2956a9202SRyan Wilson * Xen PCI Frontend. 3956a9202SRyan Wilson * 4956a9202SRyan Wilson * Author: Ryan Wilson <hap9@epoch.ncsc.mil> 5956a9202SRyan Wilson */ 6956a9202SRyan Wilson #include <linux/module.h> 7956a9202SRyan Wilson #include <linux/init.h> 8956a9202SRyan Wilson #include <linux/mm.h> 9956a9202SRyan Wilson #include <xen/xenbus.h> 10956a9202SRyan Wilson #include <xen/events.h> 11956a9202SRyan Wilson #include <xen/grant_table.h> 12956a9202SRyan Wilson #include <xen/page.h> 13956a9202SRyan Wilson #include <linux/spinlock.h> 14956a9202SRyan Wilson #include <linux/pci.h> 15956a9202SRyan Wilson #include <linux/msi.h> 16956a9202SRyan Wilson #include <xen/interface/io/pciif.h> 17956a9202SRyan Wilson #include <asm/xen/pci.h> 18956a9202SRyan Wilson #include <linux/interrupt.h> 1960063497SArun Sharma #include <linux/atomic.h> 20956a9202SRyan Wilson #include <linux/workqueue.h> 21956a9202SRyan Wilson #include <linux/bitops.h> 22956a9202SRyan Wilson #include <linux/time.h> 23956a9202SRyan Wilson 243d925320SKonrad Rzeszutek Wilk #include <asm/xen/swiotlb-xen.h> 25956a9202SRyan Wilson #define INVALID_GRANT_REF (0) 26956a9202SRyan Wilson #define INVALID_EVTCHN (-1) 27956a9202SRyan Wilson 28956a9202SRyan Wilson struct pci_bus_entry { 29956a9202SRyan Wilson struct list_head list; 30956a9202SRyan Wilson struct pci_bus *bus; 31956a9202SRyan Wilson }; 32956a9202SRyan Wilson 33956a9202SRyan Wilson #define _PDEVB_op_active (0) 34956a9202SRyan Wilson #define PDEVB_op_active (1 << (_PDEVB_op_active)) 35956a9202SRyan Wilson 36956a9202SRyan Wilson struct pcifront_device { 37956a9202SRyan Wilson struct xenbus_device *xdev; 38956a9202SRyan Wilson struct list_head root_buses; 39956a9202SRyan Wilson 40956a9202SRyan Wilson int evtchn; 41956a9202SRyan Wilson int gnt_ref; 42956a9202SRyan Wilson 43956a9202SRyan Wilson int irq; 44956a9202SRyan Wilson 45956a9202SRyan Wilson /* Lock this when doing any operations in sh_info */ 46956a9202SRyan Wilson spinlock_t sh_info_lock; 47956a9202SRyan Wilson struct xen_pci_sharedinfo *sh_info; 48956a9202SRyan Wilson struct work_struct op_work; 49956a9202SRyan Wilson unsigned long flags; 50956a9202SRyan Wilson 51956a9202SRyan Wilson }; 52956a9202SRyan Wilson 53956a9202SRyan Wilson struct pcifront_sd { 54956a9202SRyan Wilson int domain; 55956a9202SRyan Wilson struct pcifront_device *pdev; 56956a9202SRyan Wilson }; 57956a9202SRyan Wilson 58956a9202SRyan Wilson static inline struct pcifront_device * 59956a9202SRyan Wilson pcifront_get_pdev(struct pcifront_sd *sd) 60956a9202SRyan Wilson { 61956a9202SRyan Wilson return sd->pdev; 62956a9202SRyan Wilson } 63956a9202SRyan Wilson 64956a9202SRyan Wilson static inline void pcifront_init_sd(struct pcifront_sd *sd, 65956a9202SRyan Wilson unsigned int domain, unsigned int bus, 66956a9202SRyan Wilson struct pcifront_device *pdev) 67956a9202SRyan Wilson { 68956a9202SRyan Wilson sd->domain = domain; 69956a9202SRyan Wilson sd->pdev = pdev; 70956a9202SRyan Wilson } 71956a9202SRyan Wilson 72956a9202SRyan Wilson static DEFINE_SPINLOCK(pcifront_dev_lock); 73956a9202SRyan Wilson static struct pcifront_device *pcifront_dev; 74956a9202SRyan Wilson 75956a9202SRyan Wilson static int verbose_request; 76956a9202SRyan Wilson module_param(verbose_request, int, 0644); 77956a9202SRyan Wilson 78956a9202SRyan Wilson static int errno_to_pcibios_err(int errno) 79956a9202SRyan Wilson { 80956a9202SRyan Wilson switch (errno) { 81956a9202SRyan Wilson case XEN_PCI_ERR_success: 82956a9202SRyan Wilson return PCIBIOS_SUCCESSFUL; 83956a9202SRyan Wilson 84956a9202SRyan Wilson case XEN_PCI_ERR_dev_not_found: 85956a9202SRyan Wilson return PCIBIOS_DEVICE_NOT_FOUND; 86956a9202SRyan Wilson 87956a9202SRyan Wilson case XEN_PCI_ERR_invalid_offset: 88956a9202SRyan Wilson case XEN_PCI_ERR_op_failed: 89956a9202SRyan Wilson return PCIBIOS_BAD_REGISTER_NUMBER; 90956a9202SRyan Wilson 91956a9202SRyan Wilson case XEN_PCI_ERR_not_implemented: 92956a9202SRyan Wilson return PCIBIOS_FUNC_NOT_SUPPORTED; 93956a9202SRyan Wilson 94956a9202SRyan Wilson case XEN_PCI_ERR_access_denied: 95956a9202SRyan Wilson return PCIBIOS_SET_FAILED; 96956a9202SRyan Wilson } 97956a9202SRyan Wilson return errno; 98956a9202SRyan Wilson } 99956a9202SRyan Wilson 100956a9202SRyan Wilson static inline void schedule_pcifront_aer_op(struct pcifront_device *pdev) 101956a9202SRyan Wilson { 102956a9202SRyan Wilson if (test_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags) 103956a9202SRyan Wilson && !test_and_set_bit(_PDEVB_op_active, &pdev->flags)) { 104956a9202SRyan Wilson dev_dbg(&pdev->xdev->dev, "schedule aer frontend job\n"); 105956a9202SRyan Wilson schedule_work(&pdev->op_work); 106956a9202SRyan Wilson } 107956a9202SRyan Wilson } 108956a9202SRyan Wilson 109956a9202SRyan Wilson static int do_pci_op(struct pcifront_device *pdev, struct xen_pci_op *op) 110956a9202SRyan Wilson { 111956a9202SRyan Wilson int err = 0; 112956a9202SRyan Wilson struct xen_pci_op *active_op = &pdev->sh_info->op; 113956a9202SRyan Wilson unsigned long irq_flags; 114956a9202SRyan Wilson evtchn_port_t port = pdev->evtchn; 115956a9202SRyan Wilson unsigned irq = pdev->irq; 116956a9202SRyan Wilson s64 ns, ns_timeout; 117956a9202SRyan Wilson struct timeval tv; 118956a9202SRyan Wilson 119956a9202SRyan Wilson spin_lock_irqsave(&pdev->sh_info_lock, irq_flags); 120956a9202SRyan Wilson 121956a9202SRyan Wilson memcpy(active_op, op, sizeof(struct xen_pci_op)); 122956a9202SRyan Wilson 123956a9202SRyan Wilson /* Go */ 124956a9202SRyan Wilson wmb(); 125956a9202SRyan Wilson set_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags); 126956a9202SRyan Wilson notify_remote_via_evtchn(port); 127956a9202SRyan Wilson 128956a9202SRyan Wilson /* 129956a9202SRyan Wilson * We set a poll timeout of 3 seconds but give up on return after 130956a9202SRyan Wilson * 2 seconds. It is better to time out too late rather than too early 131956a9202SRyan Wilson * (in the latter case we end up continually re-executing poll() with a 132956a9202SRyan Wilson * timeout in the past). 1s difference gives plenty of slack for error. 133956a9202SRyan Wilson */ 134956a9202SRyan Wilson do_gettimeofday(&tv); 135956a9202SRyan Wilson ns_timeout = timeval_to_ns(&tv) + 2 * (s64)NSEC_PER_SEC; 136956a9202SRyan Wilson 137956a9202SRyan Wilson xen_clear_irq_pending(irq); 138956a9202SRyan Wilson 139956a9202SRyan Wilson while (test_bit(_XEN_PCIF_active, 140956a9202SRyan Wilson (unsigned long *)&pdev->sh_info->flags)) { 141956a9202SRyan Wilson xen_poll_irq_timeout(irq, jiffies + 3*HZ); 142956a9202SRyan Wilson xen_clear_irq_pending(irq); 143956a9202SRyan Wilson do_gettimeofday(&tv); 144956a9202SRyan Wilson ns = timeval_to_ns(&tv); 145956a9202SRyan Wilson if (ns > ns_timeout) { 146956a9202SRyan Wilson dev_err(&pdev->xdev->dev, 147956a9202SRyan Wilson "pciback not responding!!!\n"); 148956a9202SRyan Wilson clear_bit(_XEN_PCIF_active, 149956a9202SRyan Wilson (unsigned long *)&pdev->sh_info->flags); 150956a9202SRyan Wilson err = XEN_PCI_ERR_dev_not_found; 151956a9202SRyan Wilson goto out; 152956a9202SRyan Wilson } 153956a9202SRyan Wilson } 154956a9202SRyan Wilson 155956a9202SRyan Wilson /* 156956a9202SRyan Wilson * We might lose backend service request since we 157956a9202SRyan Wilson * reuse same evtchn with pci_conf backend response. So re-schedule 158956a9202SRyan Wilson * aer pcifront service. 159956a9202SRyan Wilson */ 160956a9202SRyan Wilson if (test_bit(_XEN_PCIB_active, 161956a9202SRyan Wilson (unsigned long *)&pdev->sh_info->flags)) { 162956a9202SRyan Wilson dev_err(&pdev->xdev->dev, 163956a9202SRyan Wilson "schedule aer pcifront service\n"); 164956a9202SRyan Wilson schedule_pcifront_aer_op(pdev); 165956a9202SRyan Wilson } 166956a9202SRyan Wilson 167956a9202SRyan Wilson memcpy(op, active_op, sizeof(struct xen_pci_op)); 168956a9202SRyan Wilson 169956a9202SRyan Wilson err = op->err; 170956a9202SRyan Wilson out: 171956a9202SRyan Wilson spin_unlock_irqrestore(&pdev->sh_info_lock, irq_flags); 172956a9202SRyan Wilson return err; 173956a9202SRyan Wilson } 174956a9202SRyan Wilson 175956a9202SRyan Wilson /* Access to this function is spinlocked in drivers/pci/access.c */ 176956a9202SRyan Wilson static int pcifront_bus_read(struct pci_bus *bus, unsigned int devfn, 177956a9202SRyan Wilson int where, int size, u32 *val) 178956a9202SRyan Wilson { 179956a9202SRyan Wilson int err = 0; 180956a9202SRyan Wilson struct xen_pci_op op = { 181956a9202SRyan Wilson .cmd = XEN_PCI_OP_conf_read, 182956a9202SRyan Wilson .domain = pci_domain_nr(bus), 183956a9202SRyan Wilson .bus = bus->number, 184956a9202SRyan Wilson .devfn = devfn, 185956a9202SRyan Wilson .offset = where, 186956a9202SRyan Wilson .size = size, 187956a9202SRyan Wilson }; 188956a9202SRyan Wilson struct pcifront_sd *sd = bus->sysdata; 189956a9202SRyan Wilson struct pcifront_device *pdev = pcifront_get_pdev(sd); 190956a9202SRyan Wilson 191956a9202SRyan Wilson if (verbose_request) 192956a9202SRyan Wilson dev_info(&pdev->xdev->dev, 193e4de866aSKonrad Rzeszutek Wilk "read dev=%04x:%02x:%02x.%d - offset %x size %d\n", 194956a9202SRyan Wilson pci_domain_nr(bus), bus->number, PCI_SLOT(devfn), 195956a9202SRyan Wilson PCI_FUNC(devfn), where, size); 196956a9202SRyan Wilson 197956a9202SRyan Wilson err = do_pci_op(pdev, &op); 198956a9202SRyan Wilson 199956a9202SRyan Wilson if (likely(!err)) { 200956a9202SRyan Wilson if (verbose_request) 201956a9202SRyan Wilson dev_info(&pdev->xdev->dev, "read got back value %x\n", 202956a9202SRyan Wilson op.value); 203956a9202SRyan Wilson 204956a9202SRyan Wilson *val = op.value; 205956a9202SRyan Wilson } else if (err == -ENODEV) { 206956a9202SRyan Wilson /* No device here, pretend that it just returned 0 */ 207956a9202SRyan Wilson err = 0; 208956a9202SRyan Wilson *val = 0; 209956a9202SRyan Wilson } 210956a9202SRyan Wilson 211956a9202SRyan Wilson return errno_to_pcibios_err(err); 212956a9202SRyan Wilson } 213956a9202SRyan Wilson 214956a9202SRyan Wilson /* Access to this function is spinlocked in drivers/pci/access.c */ 215956a9202SRyan Wilson static int pcifront_bus_write(struct pci_bus *bus, unsigned int devfn, 216956a9202SRyan Wilson int where, int size, u32 val) 217956a9202SRyan Wilson { 218956a9202SRyan Wilson struct xen_pci_op op = { 219956a9202SRyan Wilson .cmd = XEN_PCI_OP_conf_write, 220956a9202SRyan Wilson .domain = pci_domain_nr(bus), 221956a9202SRyan Wilson .bus = bus->number, 222956a9202SRyan Wilson .devfn = devfn, 223956a9202SRyan Wilson .offset = where, 224956a9202SRyan Wilson .size = size, 225956a9202SRyan Wilson .value = val, 226956a9202SRyan Wilson }; 227956a9202SRyan Wilson struct pcifront_sd *sd = bus->sysdata; 228956a9202SRyan Wilson struct pcifront_device *pdev = pcifront_get_pdev(sd); 229956a9202SRyan Wilson 230956a9202SRyan Wilson if (verbose_request) 231956a9202SRyan Wilson dev_info(&pdev->xdev->dev, 232e4de866aSKonrad Rzeszutek Wilk "write dev=%04x:%02x:%02x.%d - " 233956a9202SRyan Wilson "offset %x size %d val %x\n", 234956a9202SRyan Wilson pci_domain_nr(bus), bus->number, 235956a9202SRyan Wilson PCI_SLOT(devfn), PCI_FUNC(devfn), where, size, val); 236956a9202SRyan Wilson 237956a9202SRyan Wilson return errno_to_pcibios_err(do_pci_op(pdev, &op)); 238956a9202SRyan Wilson } 239956a9202SRyan Wilson 240b8b0f559SKonrad Rzeszutek Wilk static struct pci_ops pcifront_bus_ops = { 241956a9202SRyan Wilson .read = pcifront_bus_read, 242956a9202SRyan Wilson .write = pcifront_bus_write, 243956a9202SRyan Wilson }; 244956a9202SRyan Wilson 245956a9202SRyan Wilson #ifdef CONFIG_PCI_MSI 246956a9202SRyan Wilson static int pci_frontend_enable_msix(struct pci_dev *dev, 247cc0f89c4SKonrad Rzeszutek Wilk int vector[], int nvec) 248956a9202SRyan Wilson { 249956a9202SRyan Wilson int err; 250956a9202SRyan Wilson int i; 251956a9202SRyan Wilson struct xen_pci_op op = { 252956a9202SRyan Wilson .cmd = XEN_PCI_OP_enable_msix, 253956a9202SRyan Wilson .domain = pci_domain_nr(dev->bus), 254956a9202SRyan Wilson .bus = dev->bus->number, 255956a9202SRyan Wilson .devfn = dev->devfn, 256956a9202SRyan Wilson .value = nvec, 257956a9202SRyan Wilson }; 258956a9202SRyan Wilson struct pcifront_sd *sd = dev->bus->sysdata; 259956a9202SRyan Wilson struct pcifront_device *pdev = pcifront_get_pdev(sd); 260956a9202SRyan Wilson struct msi_desc *entry; 261956a9202SRyan Wilson 262956a9202SRyan Wilson if (nvec > SH_INFO_MAX_VEC) { 263956a9202SRyan Wilson dev_err(&dev->dev, "too much vector for pci frontend: %x." 264956a9202SRyan Wilson " Increase SH_INFO_MAX_VEC.\n", nvec); 265956a9202SRyan Wilson return -EINVAL; 266956a9202SRyan Wilson } 267956a9202SRyan Wilson 268956a9202SRyan Wilson i = 0; 269956a9202SRyan Wilson list_for_each_entry(entry, &dev->msi_list, list) { 270956a9202SRyan Wilson op.msix_entries[i].entry = entry->msi_attrib.entry_nr; 271956a9202SRyan Wilson /* Vector is useless at this point. */ 272956a9202SRyan Wilson op.msix_entries[i].vector = -1; 273956a9202SRyan Wilson i++; 274956a9202SRyan Wilson } 275956a9202SRyan Wilson 276956a9202SRyan Wilson err = do_pci_op(pdev, &op); 277956a9202SRyan Wilson 278956a9202SRyan Wilson if (likely(!err)) { 279956a9202SRyan Wilson if (likely(!op.value)) { 280956a9202SRyan Wilson /* we get the result */ 2811d461052SKonrad Rzeszutek Wilk for (i = 0; i < nvec; i++) { 2821d461052SKonrad Rzeszutek Wilk if (op.msix_entries[i].vector <= 0) { 2831d461052SKonrad Rzeszutek Wilk dev_warn(&dev->dev, "MSI-X entry %d is invalid: %d!\n", 2841d461052SKonrad Rzeszutek Wilk i, op.msix_entries[i].vector); 2851d461052SKonrad Rzeszutek Wilk err = -EINVAL; 286cc0f89c4SKonrad Rzeszutek Wilk vector[i] = -1; 2871d461052SKonrad Rzeszutek Wilk continue; 2881d461052SKonrad Rzeszutek Wilk } 289cc0f89c4SKonrad Rzeszutek Wilk vector[i] = op.msix_entries[i].vector; 2901d461052SKonrad Rzeszutek Wilk } 291956a9202SRyan Wilson } else { 292956a9202SRyan Wilson printk(KERN_DEBUG "enable msix get value %x\n", 293956a9202SRyan Wilson op.value); 294f09d8432SJan Beulich err = op.value; 295956a9202SRyan Wilson } 296956a9202SRyan Wilson } else { 297956a9202SRyan Wilson dev_err(&dev->dev, "enable msix get err %x\n", err); 298956a9202SRyan Wilson } 2991d461052SKonrad Rzeszutek Wilk return err; 300956a9202SRyan Wilson } 301956a9202SRyan Wilson 302956a9202SRyan Wilson static void pci_frontend_disable_msix(struct pci_dev *dev) 303956a9202SRyan Wilson { 304956a9202SRyan Wilson int err; 305956a9202SRyan Wilson struct xen_pci_op op = { 306956a9202SRyan Wilson .cmd = XEN_PCI_OP_disable_msix, 307956a9202SRyan Wilson .domain = pci_domain_nr(dev->bus), 308956a9202SRyan Wilson .bus = dev->bus->number, 309956a9202SRyan Wilson .devfn = dev->devfn, 310956a9202SRyan Wilson }; 311956a9202SRyan Wilson struct pcifront_sd *sd = dev->bus->sysdata; 312956a9202SRyan Wilson struct pcifront_device *pdev = pcifront_get_pdev(sd); 313956a9202SRyan Wilson 314956a9202SRyan Wilson err = do_pci_op(pdev, &op); 315956a9202SRyan Wilson 316956a9202SRyan Wilson /* What should do for error ? */ 317956a9202SRyan Wilson if (err) 318956a9202SRyan Wilson dev_err(&dev->dev, "pci_disable_msix get err %x\n", err); 319956a9202SRyan Wilson } 320956a9202SRyan Wilson 321cc0f89c4SKonrad Rzeszutek Wilk static int pci_frontend_enable_msi(struct pci_dev *dev, int vector[]) 322956a9202SRyan Wilson { 323956a9202SRyan Wilson int err; 324956a9202SRyan Wilson struct xen_pci_op op = { 325956a9202SRyan Wilson .cmd = XEN_PCI_OP_enable_msi, 326956a9202SRyan Wilson .domain = pci_domain_nr(dev->bus), 327956a9202SRyan Wilson .bus = dev->bus->number, 328956a9202SRyan Wilson .devfn = dev->devfn, 329956a9202SRyan Wilson }; 330956a9202SRyan Wilson struct pcifront_sd *sd = dev->bus->sysdata; 331956a9202SRyan Wilson struct pcifront_device *pdev = pcifront_get_pdev(sd); 332956a9202SRyan Wilson 333956a9202SRyan Wilson err = do_pci_op(pdev, &op); 334956a9202SRyan Wilson if (likely(!err)) { 335cc0f89c4SKonrad Rzeszutek Wilk vector[0] = op.value; 3361d461052SKonrad Rzeszutek Wilk if (op.value <= 0) { 3371d461052SKonrad Rzeszutek Wilk dev_warn(&dev->dev, "MSI entry is invalid: %d!\n", 3381d461052SKonrad Rzeszutek Wilk op.value); 3391d461052SKonrad Rzeszutek Wilk err = -EINVAL; 340cc0f89c4SKonrad Rzeszutek Wilk vector[0] = -1; 3411d461052SKonrad Rzeszutek Wilk } 342956a9202SRyan Wilson } else { 343956a9202SRyan Wilson dev_err(&dev->dev, "pci frontend enable msi failed for dev " 344956a9202SRyan Wilson "%x:%x\n", op.bus, op.devfn); 345956a9202SRyan Wilson err = -EINVAL; 346956a9202SRyan Wilson } 347956a9202SRyan Wilson return err; 348956a9202SRyan Wilson } 349956a9202SRyan Wilson 350956a9202SRyan Wilson static void pci_frontend_disable_msi(struct pci_dev *dev) 351956a9202SRyan Wilson { 352956a9202SRyan Wilson int err; 353956a9202SRyan Wilson struct xen_pci_op op = { 354956a9202SRyan Wilson .cmd = XEN_PCI_OP_disable_msi, 355956a9202SRyan Wilson .domain = pci_domain_nr(dev->bus), 356956a9202SRyan Wilson .bus = dev->bus->number, 357956a9202SRyan Wilson .devfn = dev->devfn, 358956a9202SRyan Wilson }; 359956a9202SRyan Wilson struct pcifront_sd *sd = dev->bus->sysdata; 360956a9202SRyan Wilson struct pcifront_device *pdev = pcifront_get_pdev(sd); 361956a9202SRyan Wilson 362956a9202SRyan Wilson err = do_pci_op(pdev, &op); 363956a9202SRyan Wilson if (err == XEN_PCI_ERR_dev_not_found) { 364956a9202SRyan Wilson /* XXX No response from backend, what shall we do? */ 365956a9202SRyan Wilson printk(KERN_DEBUG "get no response from backend for disable MSI\n"); 366956a9202SRyan Wilson return; 367956a9202SRyan Wilson } 368956a9202SRyan Wilson if (err) 369956a9202SRyan Wilson /* how can pciback notify us fail? */ 370956a9202SRyan Wilson printk(KERN_DEBUG "get fake response frombackend\n"); 371956a9202SRyan Wilson } 372956a9202SRyan Wilson 373956a9202SRyan Wilson static struct xen_pci_frontend_ops pci_frontend_ops = { 374956a9202SRyan Wilson .enable_msi = pci_frontend_enable_msi, 375956a9202SRyan Wilson .disable_msi = pci_frontend_disable_msi, 376956a9202SRyan Wilson .enable_msix = pci_frontend_enable_msix, 377956a9202SRyan Wilson .disable_msix = pci_frontend_disable_msix, 378956a9202SRyan Wilson }; 379956a9202SRyan Wilson 380956a9202SRyan Wilson static void pci_frontend_registrar(int enable) 381956a9202SRyan Wilson { 382956a9202SRyan Wilson if (enable) 383956a9202SRyan Wilson xen_pci_frontend = &pci_frontend_ops; 384956a9202SRyan Wilson else 385956a9202SRyan Wilson xen_pci_frontend = NULL; 386956a9202SRyan Wilson }; 387956a9202SRyan Wilson #else 388956a9202SRyan Wilson static inline void pci_frontend_registrar(int enable) { }; 389956a9202SRyan Wilson #endif /* CONFIG_PCI_MSI */ 390956a9202SRyan Wilson 391956a9202SRyan Wilson /* Claim resources for the PCI frontend as-is, backend won't allow changes */ 392956a9202SRyan Wilson static int pcifront_claim_resource(struct pci_dev *dev, void *data) 393956a9202SRyan Wilson { 394956a9202SRyan Wilson struct pcifront_device *pdev = data; 395956a9202SRyan Wilson int i; 396956a9202SRyan Wilson struct resource *r; 397956a9202SRyan Wilson 398956a9202SRyan Wilson for (i = 0; i < PCI_NUM_RESOURCES; i++) { 399956a9202SRyan Wilson r = &dev->resource[i]; 400956a9202SRyan Wilson 401956a9202SRyan Wilson if (!r->parent && r->start && r->flags) { 402956a9202SRyan Wilson dev_info(&pdev->xdev->dev, "claiming resource %s/%d\n", 403956a9202SRyan Wilson pci_name(dev), i); 404956a9202SRyan Wilson if (pci_claim_resource(dev, i)) { 405917e3e65SKonrad Rzeszutek Wilk dev_err(&pdev->xdev->dev, "Could not claim resource %s/%d! " 406917e3e65SKonrad Rzeszutek Wilk "Device offline. Try using e820_host=1 in the guest config.\n", 407956a9202SRyan Wilson pci_name(dev), i); 408956a9202SRyan Wilson } 409956a9202SRyan Wilson } 410956a9202SRyan Wilson } 411956a9202SRyan Wilson 412956a9202SRyan Wilson return 0; 413956a9202SRyan Wilson } 414956a9202SRyan Wilson 41515856ad5SBill Pemberton static int pcifront_scan_bus(struct pcifront_device *pdev, 416956a9202SRyan Wilson unsigned int domain, unsigned int bus, 417956a9202SRyan Wilson struct pci_bus *b) 418956a9202SRyan Wilson { 419956a9202SRyan Wilson struct pci_dev *d; 420956a9202SRyan Wilson unsigned int devfn; 421956a9202SRyan Wilson 422956a9202SRyan Wilson /* Scan the bus for functions and add. 423956a9202SRyan Wilson * We omit handling of PCI bridge attachment because pciback prevents 424956a9202SRyan Wilson * bridges from being exported. 425956a9202SRyan Wilson */ 426956a9202SRyan Wilson for (devfn = 0; devfn < 0x100; devfn++) { 427956a9202SRyan Wilson d = pci_get_slot(b, devfn); 428956a9202SRyan Wilson if (d) { 429956a9202SRyan Wilson /* Device is already known. */ 430956a9202SRyan Wilson pci_dev_put(d); 431956a9202SRyan Wilson continue; 432956a9202SRyan Wilson } 433956a9202SRyan Wilson 434956a9202SRyan Wilson d = pci_scan_single_device(b, devfn); 435956a9202SRyan Wilson if (d) 436956a9202SRyan Wilson dev_info(&pdev->xdev->dev, "New device on " 437e4de866aSKonrad Rzeszutek Wilk "%04x:%02x:%02x.%d found.\n", domain, bus, 438956a9202SRyan Wilson PCI_SLOT(devfn), PCI_FUNC(devfn)); 439956a9202SRyan Wilson } 440956a9202SRyan Wilson 441956a9202SRyan Wilson return 0; 442956a9202SRyan Wilson } 443956a9202SRyan Wilson 44415856ad5SBill Pemberton static int pcifront_scan_root(struct pcifront_device *pdev, 445956a9202SRyan Wilson unsigned int domain, unsigned int bus) 446956a9202SRyan Wilson { 447956a9202SRyan Wilson struct pci_bus *b; 448956a9202SRyan Wilson struct pcifront_sd *sd = NULL; 449956a9202SRyan Wilson struct pci_bus_entry *bus_entry = NULL; 450956a9202SRyan Wilson int err = 0; 451956a9202SRyan Wilson 452956a9202SRyan Wilson #ifndef CONFIG_PCI_DOMAINS 453956a9202SRyan Wilson if (domain != 0) { 454956a9202SRyan Wilson dev_err(&pdev->xdev->dev, 455956a9202SRyan Wilson "PCI Root in non-zero PCI Domain! domain=%d\n", domain); 456956a9202SRyan Wilson dev_err(&pdev->xdev->dev, 457956a9202SRyan Wilson "Please compile with CONFIG_PCI_DOMAINS\n"); 458956a9202SRyan Wilson err = -EINVAL; 459956a9202SRyan Wilson goto err_out; 460956a9202SRyan Wilson } 461956a9202SRyan Wilson #endif 462956a9202SRyan Wilson 463956a9202SRyan Wilson dev_info(&pdev->xdev->dev, "Creating PCI Frontend Bus %04x:%02x\n", 464956a9202SRyan Wilson domain, bus); 465956a9202SRyan Wilson 466956a9202SRyan Wilson bus_entry = kmalloc(sizeof(*bus_entry), GFP_KERNEL); 467956a9202SRyan Wilson sd = kmalloc(sizeof(*sd), GFP_KERNEL); 468956a9202SRyan Wilson if (!bus_entry || !sd) { 469956a9202SRyan Wilson err = -ENOMEM; 470956a9202SRyan Wilson goto err_out; 471956a9202SRyan Wilson } 472956a9202SRyan Wilson pcifront_init_sd(sd, domain, bus, pdev); 473956a9202SRyan Wilson 474*a83919e0SRafael J. Wysocki pci_lock_rescan_remove(); 475*a83919e0SRafael J. Wysocki 476956a9202SRyan Wilson b = pci_scan_bus_parented(&pdev->xdev->dev, bus, 477956a9202SRyan Wilson &pcifront_bus_ops, sd); 478956a9202SRyan Wilson if (!b) { 479956a9202SRyan Wilson dev_err(&pdev->xdev->dev, 480956a9202SRyan Wilson "Error creating PCI Frontend Bus!\n"); 481956a9202SRyan Wilson err = -ENOMEM; 482*a83919e0SRafael J. Wysocki pci_unlock_rescan_remove(); 483956a9202SRyan Wilson goto err_out; 484956a9202SRyan Wilson } 485956a9202SRyan Wilson 486956a9202SRyan Wilson bus_entry->bus = b; 487956a9202SRyan Wilson 488956a9202SRyan Wilson list_add(&bus_entry->list, &pdev->root_buses); 489956a9202SRyan Wilson 490956a9202SRyan Wilson /* pci_scan_bus_parented skips devices which do not have a have 491956a9202SRyan Wilson * devfn==0. The pcifront_scan_bus enumerates all devfn. */ 492956a9202SRyan Wilson err = pcifront_scan_bus(pdev, domain, bus, b); 493956a9202SRyan Wilson 494956a9202SRyan Wilson /* Claim resources before going "live" with our devices */ 495956a9202SRyan Wilson pci_walk_bus(b, pcifront_claim_resource, pdev); 496956a9202SRyan Wilson 497956a9202SRyan Wilson /* Create SysFS and notify udev of the devices. Aka: "going live" */ 498956a9202SRyan Wilson pci_bus_add_devices(b); 499956a9202SRyan Wilson 500*a83919e0SRafael J. Wysocki pci_unlock_rescan_remove(); 501956a9202SRyan Wilson return err; 502956a9202SRyan Wilson 503956a9202SRyan Wilson err_out: 504956a9202SRyan Wilson kfree(bus_entry); 505956a9202SRyan Wilson kfree(sd); 506956a9202SRyan Wilson 507956a9202SRyan Wilson return err; 508956a9202SRyan Wilson } 509956a9202SRyan Wilson 51015856ad5SBill Pemberton static int pcifront_rescan_root(struct pcifront_device *pdev, 511956a9202SRyan Wilson unsigned int domain, unsigned int bus) 512956a9202SRyan Wilson { 513956a9202SRyan Wilson int err; 514956a9202SRyan Wilson struct pci_bus *b; 515956a9202SRyan Wilson 516956a9202SRyan Wilson #ifndef CONFIG_PCI_DOMAINS 517956a9202SRyan Wilson if (domain != 0) { 518956a9202SRyan Wilson dev_err(&pdev->xdev->dev, 519956a9202SRyan Wilson "PCI Root in non-zero PCI Domain! domain=%d\n", domain); 520956a9202SRyan Wilson dev_err(&pdev->xdev->dev, 521956a9202SRyan Wilson "Please compile with CONFIG_PCI_DOMAINS\n"); 522956a9202SRyan Wilson return -EINVAL; 523956a9202SRyan Wilson } 524956a9202SRyan Wilson #endif 525956a9202SRyan Wilson 526956a9202SRyan Wilson dev_info(&pdev->xdev->dev, "Rescanning PCI Frontend Bus %04x:%02x\n", 527956a9202SRyan Wilson domain, bus); 528956a9202SRyan Wilson 529956a9202SRyan Wilson b = pci_find_bus(domain, bus); 530956a9202SRyan Wilson if (!b) 531956a9202SRyan Wilson /* If the bus is unknown, create it. */ 532956a9202SRyan Wilson return pcifront_scan_root(pdev, domain, bus); 533956a9202SRyan Wilson 534956a9202SRyan Wilson err = pcifront_scan_bus(pdev, domain, bus, b); 535956a9202SRyan Wilson 536956a9202SRyan Wilson /* Claim resources before going "live" with our devices */ 537956a9202SRyan Wilson pci_walk_bus(b, pcifront_claim_resource, pdev); 538956a9202SRyan Wilson 539956a9202SRyan Wilson /* Create SysFS and notify udev of the devices. Aka: "going live" */ 540956a9202SRyan Wilson pci_bus_add_devices(b); 541956a9202SRyan Wilson 542956a9202SRyan Wilson return err; 543956a9202SRyan Wilson } 544956a9202SRyan Wilson 545956a9202SRyan Wilson static void free_root_bus_devs(struct pci_bus *bus) 546956a9202SRyan Wilson { 547956a9202SRyan Wilson struct pci_dev *dev; 548956a9202SRyan Wilson 549956a9202SRyan Wilson while (!list_empty(&bus->devices)) { 550956a9202SRyan Wilson dev = container_of(bus->devices.next, struct pci_dev, 551956a9202SRyan Wilson bus_list); 552956a9202SRyan Wilson dev_dbg(&dev->dev, "removing device\n"); 553210647afSYinghai Lu pci_stop_and_remove_bus_device(dev); 554956a9202SRyan Wilson } 555956a9202SRyan Wilson } 556956a9202SRyan Wilson 557956a9202SRyan Wilson static void pcifront_free_roots(struct pcifront_device *pdev) 558956a9202SRyan Wilson { 559956a9202SRyan Wilson struct pci_bus_entry *bus_entry, *t; 560956a9202SRyan Wilson 561956a9202SRyan Wilson dev_dbg(&pdev->xdev->dev, "cleaning up root buses\n"); 562956a9202SRyan Wilson 563*a83919e0SRafael J. Wysocki pci_lock_rescan_remove(); 564956a9202SRyan Wilson list_for_each_entry_safe(bus_entry, t, &pdev->root_buses, list) { 565956a9202SRyan Wilson list_del(&bus_entry->list); 566956a9202SRyan Wilson 567956a9202SRyan Wilson free_root_bus_devs(bus_entry->bus); 568956a9202SRyan Wilson 569956a9202SRyan Wilson kfree(bus_entry->bus->sysdata); 570956a9202SRyan Wilson 571956a9202SRyan Wilson device_unregister(bus_entry->bus->bridge); 572956a9202SRyan Wilson pci_remove_bus(bus_entry->bus); 573956a9202SRyan Wilson 574956a9202SRyan Wilson kfree(bus_entry); 575956a9202SRyan Wilson } 576*a83919e0SRafael J. Wysocki pci_unlock_rescan_remove(); 577956a9202SRyan Wilson } 578956a9202SRyan Wilson 579956a9202SRyan Wilson static pci_ers_result_t pcifront_common_process(int cmd, 580956a9202SRyan Wilson struct pcifront_device *pdev, 581956a9202SRyan Wilson pci_channel_state_t state) 582956a9202SRyan Wilson { 583956a9202SRyan Wilson pci_ers_result_t result; 584956a9202SRyan Wilson struct pci_driver *pdrv; 585956a9202SRyan Wilson int bus = pdev->sh_info->aer_op.bus; 586956a9202SRyan Wilson int devfn = pdev->sh_info->aer_op.devfn; 587956a9202SRyan Wilson struct pci_dev *pcidev; 588956a9202SRyan Wilson int flag = 0; 589956a9202SRyan Wilson 590956a9202SRyan Wilson dev_dbg(&pdev->xdev->dev, 591956a9202SRyan Wilson "pcifront AER process: cmd %x (bus:%x, devfn%x)", 592956a9202SRyan Wilson cmd, bus, devfn); 593956a9202SRyan Wilson result = PCI_ERS_RESULT_NONE; 594956a9202SRyan Wilson 595956a9202SRyan Wilson pcidev = pci_get_bus_and_slot(bus, devfn); 596956a9202SRyan Wilson if (!pcidev || !pcidev->driver) { 5972a63dd72SJiri Slaby dev_err(&pdev->xdev->dev, "device or AER driver is NULL\n"); 5982a63dd72SJiri Slaby if (pcidev) 5992a63dd72SJiri Slaby pci_dev_put(pcidev); 600956a9202SRyan Wilson return result; 601956a9202SRyan Wilson } 602956a9202SRyan Wilson pdrv = pcidev->driver; 603956a9202SRyan Wilson 60407d25146SAlan Stern if (pdrv) { 605956a9202SRyan Wilson if (pdrv->err_handler && pdrv->err_handler->error_detected) { 606956a9202SRyan Wilson dev_dbg(&pcidev->dev, 607956a9202SRyan Wilson "trying to call AER service\n"); 608956a9202SRyan Wilson if (pcidev) { 609956a9202SRyan Wilson flag = 1; 610956a9202SRyan Wilson switch (cmd) { 611956a9202SRyan Wilson case XEN_PCI_OP_aer_detected: 612956a9202SRyan Wilson result = pdrv->err_handler-> 613956a9202SRyan Wilson error_detected(pcidev, state); 614956a9202SRyan Wilson break; 615956a9202SRyan Wilson case XEN_PCI_OP_aer_mmio: 616956a9202SRyan Wilson result = pdrv->err_handler-> 617956a9202SRyan Wilson mmio_enabled(pcidev); 618956a9202SRyan Wilson break; 619956a9202SRyan Wilson case XEN_PCI_OP_aer_slotreset: 620956a9202SRyan Wilson result = pdrv->err_handler-> 621956a9202SRyan Wilson slot_reset(pcidev); 622956a9202SRyan Wilson break; 623956a9202SRyan Wilson case XEN_PCI_OP_aer_resume: 624956a9202SRyan Wilson pdrv->err_handler->resume(pcidev); 625956a9202SRyan Wilson break; 626956a9202SRyan Wilson default: 627956a9202SRyan Wilson dev_err(&pdev->xdev->dev, 628956a9202SRyan Wilson "bad request in aer recovery " 629956a9202SRyan Wilson "operation!\n"); 630956a9202SRyan Wilson 631956a9202SRyan Wilson } 632956a9202SRyan Wilson } 633956a9202SRyan Wilson } 634956a9202SRyan Wilson } 635956a9202SRyan Wilson if (!flag) 636956a9202SRyan Wilson result = PCI_ERS_RESULT_NONE; 637956a9202SRyan Wilson 638956a9202SRyan Wilson return result; 639956a9202SRyan Wilson } 640956a9202SRyan Wilson 641956a9202SRyan Wilson 642956a9202SRyan Wilson static void pcifront_do_aer(struct work_struct *data) 643956a9202SRyan Wilson { 644956a9202SRyan Wilson struct pcifront_device *pdev = 645956a9202SRyan Wilson container_of(data, struct pcifront_device, op_work); 646956a9202SRyan Wilson int cmd = pdev->sh_info->aer_op.cmd; 647956a9202SRyan Wilson pci_channel_state_t state = 648956a9202SRyan Wilson (pci_channel_state_t)pdev->sh_info->aer_op.err; 649956a9202SRyan Wilson 650956a9202SRyan Wilson /*If a pci_conf op is in progress, 651956a9202SRyan Wilson we have to wait until it is done before service aer op*/ 652956a9202SRyan Wilson dev_dbg(&pdev->xdev->dev, 653956a9202SRyan Wilson "pcifront service aer bus %x devfn %x\n", 654956a9202SRyan Wilson pdev->sh_info->aer_op.bus, pdev->sh_info->aer_op.devfn); 655956a9202SRyan Wilson 656956a9202SRyan Wilson pdev->sh_info->aer_op.err = pcifront_common_process(cmd, pdev, state); 657956a9202SRyan Wilson 658956a9202SRyan Wilson /* Post the operation to the guest. */ 659956a9202SRyan Wilson wmb(); 660956a9202SRyan Wilson clear_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags); 661956a9202SRyan Wilson notify_remote_via_evtchn(pdev->evtchn); 662956a9202SRyan Wilson 663956a9202SRyan Wilson /*in case of we lost an aer request in four lines time_window*/ 664956a9202SRyan Wilson smp_mb__before_clear_bit(); 665956a9202SRyan Wilson clear_bit(_PDEVB_op_active, &pdev->flags); 666956a9202SRyan Wilson smp_mb__after_clear_bit(); 667956a9202SRyan Wilson 668956a9202SRyan Wilson schedule_pcifront_aer_op(pdev); 669956a9202SRyan Wilson 670956a9202SRyan Wilson } 671956a9202SRyan Wilson 672956a9202SRyan Wilson static irqreturn_t pcifront_handler_aer(int irq, void *dev) 673956a9202SRyan Wilson { 674956a9202SRyan Wilson struct pcifront_device *pdev = dev; 675956a9202SRyan Wilson schedule_pcifront_aer_op(pdev); 676956a9202SRyan Wilson return IRQ_HANDLED; 677956a9202SRyan Wilson } 6783d925320SKonrad Rzeszutek Wilk static int pcifront_connect_and_init_dma(struct pcifront_device *pdev) 679956a9202SRyan Wilson { 680956a9202SRyan Wilson int err = 0; 681956a9202SRyan Wilson 682956a9202SRyan Wilson spin_lock(&pcifront_dev_lock); 683956a9202SRyan Wilson 684956a9202SRyan Wilson if (!pcifront_dev) { 685956a9202SRyan Wilson dev_info(&pdev->xdev->dev, "Installing PCI frontend\n"); 686956a9202SRyan Wilson pcifront_dev = pdev; 687098b1aeaSKonrad Rzeszutek Wilk } else 688956a9202SRyan Wilson err = -EEXIST; 689098b1aeaSKonrad Rzeszutek Wilk 690956a9202SRyan Wilson spin_unlock(&pcifront_dev_lock); 691956a9202SRyan Wilson 6923d925320SKonrad Rzeszutek Wilk if (!err && !swiotlb_nr_tbl()) { 6933d925320SKonrad Rzeszutek Wilk err = pci_xen_swiotlb_init_late(); 6943d925320SKonrad Rzeszutek Wilk if (err) 6953d925320SKonrad Rzeszutek Wilk dev_err(&pdev->xdev->dev, "Could not setup SWIOTLB!\n"); 6963d925320SKonrad Rzeszutek Wilk } 697956a9202SRyan Wilson return err; 698956a9202SRyan Wilson } 699956a9202SRyan Wilson 700956a9202SRyan Wilson static void pcifront_disconnect(struct pcifront_device *pdev) 701956a9202SRyan Wilson { 702956a9202SRyan Wilson spin_lock(&pcifront_dev_lock); 703956a9202SRyan Wilson 704956a9202SRyan Wilson if (pdev == pcifront_dev) { 705956a9202SRyan Wilson dev_info(&pdev->xdev->dev, 706956a9202SRyan Wilson "Disconnecting PCI Frontend Buses\n"); 707956a9202SRyan Wilson pcifront_dev = NULL; 708956a9202SRyan Wilson } 709956a9202SRyan Wilson 710956a9202SRyan Wilson spin_unlock(&pcifront_dev_lock); 711956a9202SRyan Wilson } 712956a9202SRyan Wilson static struct pcifront_device *alloc_pdev(struct xenbus_device *xdev) 713956a9202SRyan Wilson { 714956a9202SRyan Wilson struct pcifront_device *pdev; 715956a9202SRyan Wilson 716956a9202SRyan Wilson pdev = kzalloc(sizeof(struct pcifront_device), GFP_KERNEL); 717956a9202SRyan Wilson if (pdev == NULL) 718956a9202SRyan Wilson goto out; 719956a9202SRyan Wilson 720956a9202SRyan Wilson pdev->sh_info = 721956a9202SRyan Wilson (struct xen_pci_sharedinfo *)__get_free_page(GFP_KERNEL); 722956a9202SRyan Wilson if (pdev->sh_info == NULL) { 723956a9202SRyan Wilson kfree(pdev); 724956a9202SRyan Wilson pdev = NULL; 725956a9202SRyan Wilson goto out; 726956a9202SRyan Wilson } 727956a9202SRyan Wilson pdev->sh_info->flags = 0; 728956a9202SRyan Wilson 729956a9202SRyan Wilson /*Flag for registering PV AER handler*/ 730956a9202SRyan Wilson set_bit(_XEN_PCIB_AERHANDLER, (void *)&pdev->sh_info->flags); 731956a9202SRyan Wilson 732956a9202SRyan Wilson dev_set_drvdata(&xdev->dev, pdev); 733956a9202SRyan Wilson pdev->xdev = xdev; 734956a9202SRyan Wilson 735956a9202SRyan Wilson INIT_LIST_HEAD(&pdev->root_buses); 736956a9202SRyan Wilson 737956a9202SRyan Wilson spin_lock_init(&pdev->sh_info_lock); 738956a9202SRyan Wilson 739956a9202SRyan Wilson pdev->evtchn = INVALID_EVTCHN; 740956a9202SRyan Wilson pdev->gnt_ref = INVALID_GRANT_REF; 741956a9202SRyan Wilson pdev->irq = -1; 742956a9202SRyan Wilson 743956a9202SRyan Wilson INIT_WORK(&pdev->op_work, pcifront_do_aer); 744956a9202SRyan Wilson 745956a9202SRyan Wilson dev_dbg(&xdev->dev, "Allocated pdev @ 0x%p pdev->sh_info @ 0x%p\n", 746956a9202SRyan Wilson pdev, pdev->sh_info); 747956a9202SRyan Wilson out: 748956a9202SRyan Wilson return pdev; 749956a9202SRyan Wilson } 750956a9202SRyan Wilson 751956a9202SRyan Wilson static void free_pdev(struct pcifront_device *pdev) 752956a9202SRyan Wilson { 753956a9202SRyan Wilson dev_dbg(&pdev->xdev->dev, "freeing pdev @ 0x%p\n", pdev); 754956a9202SRyan Wilson 755956a9202SRyan Wilson pcifront_free_roots(pdev); 756956a9202SRyan Wilson 757db2e2e6eSTejun Heo cancel_work_sync(&pdev->op_work); 758956a9202SRyan Wilson 759956a9202SRyan Wilson if (pdev->irq >= 0) 760956a9202SRyan Wilson unbind_from_irqhandler(pdev->irq, pdev); 761956a9202SRyan Wilson 762956a9202SRyan Wilson if (pdev->evtchn != INVALID_EVTCHN) 763956a9202SRyan Wilson xenbus_free_evtchn(pdev->xdev, pdev->evtchn); 764956a9202SRyan Wilson 765956a9202SRyan Wilson if (pdev->gnt_ref != INVALID_GRANT_REF) 766956a9202SRyan Wilson gnttab_end_foreign_access(pdev->gnt_ref, 0 /* r/w page */, 767956a9202SRyan Wilson (unsigned long)pdev->sh_info); 768956a9202SRyan Wilson else 769956a9202SRyan Wilson free_page((unsigned long)pdev->sh_info); 770956a9202SRyan Wilson 771956a9202SRyan Wilson dev_set_drvdata(&pdev->xdev->dev, NULL); 772956a9202SRyan Wilson 773956a9202SRyan Wilson kfree(pdev); 774956a9202SRyan Wilson } 775956a9202SRyan Wilson 776956a9202SRyan Wilson static int pcifront_publish_info(struct pcifront_device *pdev) 777956a9202SRyan Wilson { 778956a9202SRyan Wilson int err = 0; 779956a9202SRyan Wilson struct xenbus_transaction trans; 780956a9202SRyan Wilson 781956a9202SRyan Wilson err = xenbus_grant_ring(pdev->xdev, virt_to_mfn(pdev->sh_info)); 782956a9202SRyan Wilson if (err < 0) 783956a9202SRyan Wilson goto out; 784956a9202SRyan Wilson 785956a9202SRyan Wilson pdev->gnt_ref = err; 786956a9202SRyan Wilson 787956a9202SRyan Wilson err = xenbus_alloc_evtchn(pdev->xdev, &pdev->evtchn); 788956a9202SRyan Wilson if (err) 789956a9202SRyan Wilson goto out; 790956a9202SRyan Wilson 791956a9202SRyan Wilson err = bind_evtchn_to_irqhandler(pdev->evtchn, pcifront_handler_aer, 792956a9202SRyan Wilson 0, "pcifront", pdev); 793956a9202SRyan Wilson 794956a9202SRyan Wilson if (err < 0) 795956a9202SRyan Wilson return err; 796956a9202SRyan Wilson 797956a9202SRyan Wilson pdev->irq = err; 798956a9202SRyan Wilson 799956a9202SRyan Wilson do_publish: 800956a9202SRyan Wilson err = xenbus_transaction_start(&trans); 801956a9202SRyan Wilson if (err) { 802956a9202SRyan Wilson xenbus_dev_fatal(pdev->xdev, err, 803956a9202SRyan Wilson "Error writing configuration for backend " 804956a9202SRyan Wilson "(start transaction)"); 805956a9202SRyan Wilson goto out; 806956a9202SRyan Wilson } 807956a9202SRyan Wilson 808956a9202SRyan Wilson err = xenbus_printf(trans, pdev->xdev->nodename, 809956a9202SRyan Wilson "pci-op-ref", "%u", pdev->gnt_ref); 810956a9202SRyan Wilson if (!err) 811956a9202SRyan Wilson err = xenbus_printf(trans, pdev->xdev->nodename, 812956a9202SRyan Wilson "event-channel", "%u", pdev->evtchn); 813956a9202SRyan Wilson if (!err) 814956a9202SRyan Wilson err = xenbus_printf(trans, pdev->xdev->nodename, 815956a9202SRyan Wilson "magic", XEN_PCI_MAGIC); 816956a9202SRyan Wilson 817956a9202SRyan Wilson if (err) { 818956a9202SRyan Wilson xenbus_transaction_end(trans, 1); 819956a9202SRyan Wilson xenbus_dev_fatal(pdev->xdev, err, 820956a9202SRyan Wilson "Error writing configuration for backend"); 821956a9202SRyan Wilson goto out; 822956a9202SRyan Wilson } else { 823956a9202SRyan Wilson err = xenbus_transaction_end(trans, 0); 824956a9202SRyan Wilson if (err == -EAGAIN) 825956a9202SRyan Wilson goto do_publish; 826956a9202SRyan Wilson else if (err) { 827956a9202SRyan Wilson xenbus_dev_fatal(pdev->xdev, err, 828956a9202SRyan Wilson "Error completing transaction " 829956a9202SRyan Wilson "for backend"); 830956a9202SRyan Wilson goto out; 831956a9202SRyan Wilson } 832956a9202SRyan Wilson } 833956a9202SRyan Wilson 834956a9202SRyan Wilson xenbus_switch_state(pdev->xdev, XenbusStateInitialised); 835956a9202SRyan Wilson 836956a9202SRyan Wilson dev_dbg(&pdev->xdev->dev, "publishing successful!\n"); 837956a9202SRyan Wilson 838956a9202SRyan Wilson out: 839956a9202SRyan Wilson return err; 840956a9202SRyan Wilson } 841956a9202SRyan Wilson 84215856ad5SBill Pemberton static int pcifront_try_connect(struct pcifront_device *pdev) 843956a9202SRyan Wilson { 844956a9202SRyan Wilson int err = -EFAULT; 845956a9202SRyan Wilson int i, num_roots, len; 846956a9202SRyan Wilson char str[64]; 847956a9202SRyan Wilson unsigned int domain, bus; 848956a9202SRyan Wilson 849956a9202SRyan Wilson 850956a9202SRyan Wilson /* Only connect once */ 851956a9202SRyan Wilson if (xenbus_read_driver_state(pdev->xdev->nodename) != 852956a9202SRyan Wilson XenbusStateInitialised) 853956a9202SRyan Wilson goto out; 854956a9202SRyan Wilson 8553d925320SKonrad Rzeszutek Wilk err = pcifront_connect_and_init_dma(pdev); 856098b1aeaSKonrad Rzeszutek Wilk if (err && err != -EEXIST) { 857956a9202SRyan Wilson xenbus_dev_fatal(pdev->xdev, err, 8583d925320SKonrad Rzeszutek Wilk "Error setting up PCI Frontend"); 859956a9202SRyan Wilson goto out; 860956a9202SRyan Wilson } 861956a9202SRyan Wilson 862956a9202SRyan Wilson err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, 863956a9202SRyan Wilson "root_num", "%d", &num_roots); 864956a9202SRyan Wilson if (err == -ENOENT) { 865956a9202SRyan Wilson xenbus_dev_error(pdev->xdev, err, 866956a9202SRyan Wilson "No PCI Roots found, trying 0000:00"); 867956a9202SRyan Wilson err = pcifront_scan_root(pdev, 0, 0); 868956a9202SRyan Wilson num_roots = 0; 869956a9202SRyan Wilson } else if (err != 1) { 870956a9202SRyan Wilson if (err == 0) 871956a9202SRyan Wilson err = -EINVAL; 872956a9202SRyan Wilson xenbus_dev_fatal(pdev->xdev, err, 873956a9202SRyan Wilson "Error reading number of PCI roots"); 874956a9202SRyan Wilson goto out; 875956a9202SRyan Wilson } 876956a9202SRyan Wilson 877956a9202SRyan Wilson for (i = 0; i < num_roots; i++) { 878956a9202SRyan Wilson len = snprintf(str, sizeof(str), "root-%d", i); 879956a9202SRyan Wilson if (unlikely(len >= (sizeof(str) - 1))) { 880956a9202SRyan Wilson err = -ENOMEM; 881956a9202SRyan Wilson goto out; 882956a9202SRyan Wilson } 883956a9202SRyan Wilson 884956a9202SRyan Wilson err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str, 885956a9202SRyan Wilson "%x:%x", &domain, &bus); 886956a9202SRyan Wilson if (err != 2) { 887956a9202SRyan Wilson if (err >= 0) 888956a9202SRyan Wilson err = -EINVAL; 889956a9202SRyan Wilson xenbus_dev_fatal(pdev->xdev, err, 890956a9202SRyan Wilson "Error reading PCI root %d", i); 891956a9202SRyan Wilson goto out; 892956a9202SRyan Wilson } 893956a9202SRyan Wilson 894956a9202SRyan Wilson err = pcifront_scan_root(pdev, domain, bus); 895956a9202SRyan Wilson if (err) { 896956a9202SRyan Wilson xenbus_dev_fatal(pdev->xdev, err, 897956a9202SRyan Wilson "Error scanning PCI root %04x:%02x", 898956a9202SRyan Wilson domain, bus); 899956a9202SRyan Wilson goto out; 900956a9202SRyan Wilson } 901956a9202SRyan Wilson } 902956a9202SRyan Wilson 903956a9202SRyan Wilson err = xenbus_switch_state(pdev->xdev, XenbusStateConnected); 904956a9202SRyan Wilson 905956a9202SRyan Wilson out: 906956a9202SRyan Wilson return err; 907956a9202SRyan Wilson } 908956a9202SRyan Wilson 909956a9202SRyan Wilson static int pcifront_try_disconnect(struct pcifront_device *pdev) 910956a9202SRyan Wilson { 911956a9202SRyan Wilson int err = 0; 912956a9202SRyan Wilson enum xenbus_state prev_state; 913956a9202SRyan Wilson 914956a9202SRyan Wilson 915956a9202SRyan Wilson prev_state = xenbus_read_driver_state(pdev->xdev->nodename); 916956a9202SRyan Wilson 917956a9202SRyan Wilson if (prev_state >= XenbusStateClosing) 918956a9202SRyan Wilson goto out; 919956a9202SRyan Wilson 920956a9202SRyan Wilson if (prev_state == XenbusStateConnected) { 921956a9202SRyan Wilson pcifront_free_roots(pdev); 922956a9202SRyan Wilson pcifront_disconnect(pdev); 923956a9202SRyan Wilson } 924956a9202SRyan Wilson 925956a9202SRyan Wilson err = xenbus_switch_state(pdev->xdev, XenbusStateClosed); 926956a9202SRyan Wilson 927956a9202SRyan Wilson out: 928956a9202SRyan Wilson 929956a9202SRyan Wilson return err; 930956a9202SRyan Wilson } 931956a9202SRyan Wilson 93215856ad5SBill Pemberton static int pcifront_attach_devices(struct pcifront_device *pdev) 933956a9202SRyan Wilson { 934956a9202SRyan Wilson int err = -EFAULT; 935956a9202SRyan Wilson int i, num_roots, len; 936956a9202SRyan Wilson unsigned int domain, bus; 937956a9202SRyan Wilson char str[64]; 938956a9202SRyan Wilson 939956a9202SRyan Wilson if (xenbus_read_driver_state(pdev->xdev->nodename) != 940956a9202SRyan Wilson XenbusStateReconfiguring) 941956a9202SRyan Wilson goto out; 942956a9202SRyan Wilson 943956a9202SRyan Wilson err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, 944956a9202SRyan Wilson "root_num", "%d", &num_roots); 945956a9202SRyan Wilson if (err == -ENOENT) { 946956a9202SRyan Wilson xenbus_dev_error(pdev->xdev, err, 947956a9202SRyan Wilson "No PCI Roots found, trying 0000:00"); 948956a9202SRyan Wilson err = pcifront_rescan_root(pdev, 0, 0); 949956a9202SRyan Wilson num_roots = 0; 950956a9202SRyan Wilson } else if (err != 1) { 951956a9202SRyan Wilson if (err == 0) 952956a9202SRyan Wilson err = -EINVAL; 953956a9202SRyan Wilson xenbus_dev_fatal(pdev->xdev, err, 954956a9202SRyan Wilson "Error reading number of PCI roots"); 955956a9202SRyan Wilson goto out; 956956a9202SRyan Wilson } 957956a9202SRyan Wilson 958956a9202SRyan Wilson for (i = 0; i < num_roots; i++) { 959956a9202SRyan Wilson len = snprintf(str, sizeof(str), "root-%d", i); 960956a9202SRyan Wilson if (unlikely(len >= (sizeof(str) - 1))) { 961956a9202SRyan Wilson err = -ENOMEM; 962956a9202SRyan Wilson goto out; 963956a9202SRyan Wilson } 964956a9202SRyan Wilson 965956a9202SRyan Wilson err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str, 966956a9202SRyan Wilson "%x:%x", &domain, &bus); 967956a9202SRyan Wilson if (err != 2) { 968956a9202SRyan Wilson if (err >= 0) 969956a9202SRyan Wilson err = -EINVAL; 970956a9202SRyan Wilson xenbus_dev_fatal(pdev->xdev, err, 971956a9202SRyan Wilson "Error reading PCI root %d", i); 972956a9202SRyan Wilson goto out; 973956a9202SRyan Wilson } 974956a9202SRyan Wilson 975956a9202SRyan Wilson err = pcifront_rescan_root(pdev, domain, bus); 976956a9202SRyan Wilson if (err) { 977956a9202SRyan Wilson xenbus_dev_fatal(pdev->xdev, err, 978956a9202SRyan Wilson "Error scanning PCI root %04x:%02x", 979956a9202SRyan Wilson domain, bus); 980956a9202SRyan Wilson goto out; 981956a9202SRyan Wilson } 982956a9202SRyan Wilson } 983956a9202SRyan Wilson 984956a9202SRyan Wilson xenbus_switch_state(pdev->xdev, XenbusStateConnected); 985956a9202SRyan Wilson 986956a9202SRyan Wilson out: 987956a9202SRyan Wilson return err; 988956a9202SRyan Wilson } 989956a9202SRyan Wilson 990956a9202SRyan Wilson static int pcifront_detach_devices(struct pcifront_device *pdev) 991956a9202SRyan Wilson { 992956a9202SRyan Wilson int err = 0; 993956a9202SRyan Wilson int i, num_devs; 994956a9202SRyan Wilson unsigned int domain, bus, slot, func; 995956a9202SRyan Wilson struct pci_dev *pci_dev; 996956a9202SRyan Wilson char str[64]; 997956a9202SRyan Wilson 998956a9202SRyan Wilson if (xenbus_read_driver_state(pdev->xdev->nodename) != 999956a9202SRyan Wilson XenbusStateConnected) 1000956a9202SRyan Wilson goto out; 1001956a9202SRyan Wilson 1002956a9202SRyan Wilson err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, "num_devs", "%d", 1003956a9202SRyan Wilson &num_devs); 1004956a9202SRyan Wilson if (err != 1) { 1005956a9202SRyan Wilson if (err >= 0) 1006956a9202SRyan Wilson err = -EINVAL; 1007956a9202SRyan Wilson xenbus_dev_fatal(pdev->xdev, err, 1008956a9202SRyan Wilson "Error reading number of PCI devices"); 1009956a9202SRyan Wilson goto out; 1010956a9202SRyan Wilson } 1011956a9202SRyan Wilson 1012956a9202SRyan Wilson /* Find devices being detached and remove them. */ 1013956a9202SRyan Wilson for (i = 0; i < num_devs; i++) { 1014956a9202SRyan Wilson int l, state; 1015956a9202SRyan Wilson l = snprintf(str, sizeof(str), "state-%d", i); 1016956a9202SRyan Wilson if (unlikely(l >= (sizeof(str) - 1))) { 1017956a9202SRyan Wilson err = -ENOMEM; 1018956a9202SRyan Wilson goto out; 1019956a9202SRyan Wilson } 1020956a9202SRyan Wilson err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str, "%d", 1021956a9202SRyan Wilson &state); 1022956a9202SRyan Wilson if (err != 1) 1023956a9202SRyan Wilson state = XenbusStateUnknown; 1024956a9202SRyan Wilson 1025956a9202SRyan Wilson if (state != XenbusStateClosing) 1026956a9202SRyan Wilson continue; 1027956a9202SRyan Wilson 1028956a9202SRyan Wilson /* Remove device. */ 1029956a9202SRyan Wilson l = snprintf(str, sizeof(str), "vdev-%d", i); 1030956a9202SRyan Wilson if (unlikely(l >= (sizeof(str) - 1))) { 1031956a9202SRyan Wilson err = -ENOMEM; 1032956a9202SRyan Wilson goto out; 1033956a9202SRyan Wilson } 1034956a9202SRyan Wilson err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str, 1035956a9202SRyan Wilson "%x:%x:%x.%x", &domain, &bus, &slot, &func); 1036956a9202SRyan Wilson if (err != 4) { 1037956a9202SRyan Wilson if (err >= 0) 1038956a9202SRyan Wilson err = -EINVAL; 1039956a9202SRyan Wilson xenbus_dev_fatal(pdev->xdev, err, 1040956a9202SRyan Wilson "Error reading PCI device %d", i); 1041956a9202SRyan Wilson goto out; 1042956a9202SRyan Wilson } 1043956a9202SRyan Wilson 10442ccc246dSJiang Liu pci_dev = pci_get_domain_bus_and_slot(domain, bus, 10452ccc246dSJiang Liu PCI_DEVFN(slot, func)); 1046956a9202SRyan Wilson if (!pci_dev) { 1047956a9202SRyan Wilson dev_dbg(&pdev->xdev->dev, 1048e4de866aSKonrad Rzeszutek Wilk "Cannot get PCI device %04x:%02x:%02x.%d\n", 1049956a9202SRyan Wilson domain, bus, slot, func); 1050956a9202SRyan Wilson continue; 1051956a9202SRyan Wilson } 1052*a83919e0SRafael J. Wysocki pci_lock_rescan_remove(); 1053210647afSYinghai Lu pci_stop_and_remove_bus_device(pci_dev); 1054956a9202SRyan Wilson pci_dev_put(pci_dev); 1055*a83919e0SRafael J. Wysocki pci_unlock_rescan_remove(); 1056956a9202SRyan Wilson 1057956a9202SRyan Wilson dev_dbg(&pdev->xdev->dev, 1058e4de866aSKonrad Rzeszutek Wilk "PCI device %04x:%02x:%02x.%d removed.\n", 1059956a9202SRyan Wilson domain, bus, slot, func); 1060956a9202SRyan Wilson } 1061956a9202SRyan Wilson 1062956a9202SRyan Wilson err = xenbus_switch_state(pdev->xdev, XenbusStateReconfiguring); 1063956a9202SRyan Wilson 1064956a9202SRyan Wilson out: 1065956a9202SRyan Wilson return err; 1066956a9202SRyan Wilson } 1067956a9202SRyan Wilson 1068956a9202SRyan Wilson static void __init_refok pcifront_backend_changed(struct xenbus_device *xdev, 1069956a9202SRyan Wilson enum xenbus_state be_state) 1070956a9202SRyan Wilson { 1071956a9202SRyan Wilson struct pcifront_device *pdev = dev_get_drvdata(&xdev->dev); 1072956a9202SRyan Wilson 1073956a9202SRyan Wilson switch (be_state) { 1074956a9202SRyan Wilson case XenbusStateUnknown: 1075956a9202SRyan Wilson case XenbusStateInitialising: 1076956a9202SRyan Wilson case XenbusStateInitWait: 1077956a9202SRyan Wilson case XenbusStateInitialised: 1078956a9202SRyan Wilson break; 1079956a9202SRyan Wilson 1080956a9202SRyan Wilson case XenbusStateConnected: 1081956a9202SRyan Wilson pcifront_try_connect(pdev); 1082956a9202SRyan Wilson break; 1083956a9202SRyan Wilson 1084d5af64deSDavid Vrabel case XenbusStateClosed: 1085d5af64deSDavid Vrabel if (xdev->state == XenbusStateClosed) 1086d5af64deSDavid Vrabel break; 1087d5af64deSDavid Vrabel /* Missed the backend's CLOSING state -- fallthrough */ 1088956a9202SRyan Wilson case XenbusStateClosing: 1089956a9202SRyan Wilson dev_warn(&xdev->dev, "backend going away!\n"); 1090956a9202SRyan Wilson pcifront_try_disconnect(pdev); 1091956a9202SRyan Wilson break; 1092956a9202SRyan Wilson 1093956a9202SRyan Wilson case XenbusStateReconfiguring: 1094956a9202SRyan Wilson pcifront_detach_devices(pdev); 1095956a9202SRyan Wilson break; 1096956a9202SRyan Wilson 1097956a9202SRyan Wilson case XenbusStateReconfigured: 1098956a9202SRyan Wilson pcifront_attach_devices(pdev); 1099956a9202SRyan Wilson break; 1100956a9202SRyan Wilson } 1101956a9202SRyan Wilson } 1102956a9202SRyan Wilson 1103956a9202SRyan Wilson static int pcifront_xenbus_probe(struct xenbus_device *xdev, 1104956a9202SRyan Wilson const struct xenbus_device_id *id) 1105956a9202SRyan Wilson { 1106956a9202SRyan Wilson int err = 0; 1107956a9202SRyan Wilson struct pcifront_device *pdev = alloc_pdev(xdev); 1108956a9202SRyan Wilson 1109956a9202SRyan Wilson if (pdev == NULL) { 1110956a9202SRyan Wilson err = -ENOMEM; 1111956a9202SRyan Wilson xenbus_dev_fatal(xdev, err, 1112956a9202SRyan Wilson "Error allocating pcifront_device struct"); 1113956a9202SRyan Wilson goto out; 1114956a9202SRyan Wilson } 1115956a9202SRyan Wilson 1116956a9202SRyan Wilson err = pcifront_publish_info(pdev); 1117956a9202SRyan Wilson if (err) 1118956a9202SRyan Wilson free_pdev(pdev); 1119956a9202SRyan Wilson 1120956a9202SRyan Wilson out: 1121956a9202SRyan Wilson return err; 1122956a9202SRyan Wilson } 1123956a9202SRyan Wilson 1124956a9202SRyan Wilson static int pcifront_xenbus_remove(struct xenbus_device *xdev) 1125956a9202SRyan Wilson { 1126956a9202SRyan Wilson struct pcifront_device *pdev = dev_get_drvdata(&xdev->dev); 1127956a9202SRyan Wilson if (pdev) 1128956a9202SRyan Wilson free_pdev(pdev); 1129956a9202SRyan Wilson 1130956a9202SRyan Wilson return 0; 1131956a9202SRyan Wilson } 1132956a9202SRyan Wilson 1133956a9202SRyan Wilson static const struct xenbus_device_id xenpci_ids[] = { 1134956a9202SRyan Wilson {"pci"}, 1135956a9202SRyan Wilson {""}, 1136956a9202SRyan Wilson }; 1137956a9202SRyan Wilson 113873db144bSJan Beulich static DEFINE_XENBUS_DRIVER(xenpci, "pcifront", 1139956a9202SRyan Wilson .probe = pcifront_xenbus_probe, 1140956a9202SRyan Wilson .remove = pcifront_xenbus_remove, 1141956a9202SRyan Wilson .otherend_changed = pcifront_backend_changed, 114273db144bSJan Beulich ); 1143956a9202SRyan Wilson 1144956a9202SRyan Wilson static int __init pcifront_init(void) 1145956a9202SRyan Wilson { 1146956a9202SRyan Wilson if (!xen_pv_domain() || xen_initial_domain()) 1147956a9202SRyan Wilson return -ENODEV; 1148956a9202SRyan Wilson 1149956a9202SRyan Wilson pci_frontend_registrar(1 /* enable */); 1150956a9202SRyan Wilson 115173db144bSJan Beulich return xenbus_register_frontend(&xenpci_driver); 1152956a9202SRyan Wilson } 1153956a9202SRyan Wilson 1154956a9202SRyan Wilson static void __exit pcifront_cleanup(void) 1155956a9202SRyan Wilson { 115673db144bSJan Beulich xenbus_unregister_driver(&xenpci_driver); 1157956a9202SRyan Wilson pci_frontend_registrar(0 /* disable */); 1158956a9202SRyan Wilson } 1159956a9202SRyan Wilson module_init(pcifront_init); 1160956a9202SRyan Wilson module_exit(pcifront_cleanup); 1161956a9202SRyan Wilson 1162956a9202SRyan Wilson MODULE_DESCRIPTION("Xen PCI passthrough frontend."); 1163956a9202SRyan Wilson MODULE_LICENSE("GPL"); 1164956a9202SRyan Wilson MODULE_ALIAS("xen:pci"); 1165