1d1b054daSYu Zhao /* 2d1b054daSYu Zhao * drivers/pci/iov.c 3d1b054daSYu Zhao * 4d1b054daSYu Zhao * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com> 5d1b054daSYu Zhao * 6d1b054daSYu Zhao * PCI Express I/O Virtualization (IOV) support. 7d1b054daSYu Zhao * Single Root IOV 1.0 8302b4215SYu Zhao * Address Translation Service 1.0 9d1b054daSYu Zhao */ 10d1b054daSYu Zhao 11d1b054daSYu Zhao #include <linux/pci.h> 125a0e3ad6STejun Heo #include <linux/slab.h> 13d1b054daSYu Zhao #include <linux/mutex.h> 14d1b054daSYu Zhao #include <linux/string.h> 15d1b054daSYu Zhao #include <linux/delay.h> 16d1b054daSYu Zhao #include "pci.h" 17d1b054daSYu Zhao 18dd7cc44dSYu Zhao #define VIRTFN_ID_LEN 16 19d1b054daSYu Zhao 20a28724b0SYu Zhao static inline u8 virtfn_bus(struct pci_dev *dev, int id) 21a28724b0SYu Zhao { 22a28724b0SYu Zhao return dev->bus->number + ((dev->devfn + dev->sriov->offset + 23a28724b0SYu Zhao dev->sriov->stride * id) >> 8); 24a28724b0SYu Zhao } 25a28724b0SYu Zhao 26a28724b0SYu Zhao static inline u8 virtfn_devfn(struct pci_dev *dev, int id) 27a28724b0SYu Zhao { 28a28724b0SYu Zhao return (dev->devfn + dev->sriov->offset + 29a28724b0SYu Zhao dev->sriov->stride * id) & 0xff; 30a28724b0SYu Zhao } 31a28724b0SYu Zhao 32dd7cc44dSYu Zhao static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr) 33dd7cc44dSYu Zhao { 34dd7cc44dSYu Zhao int rc; 35dd7cc44dSYu Zhao struct pci_bus *child; 36dd7cc44dSYu Zhao 37dd7cc44dSYu Zhao if (bus->number == busnr) 38dd7cc44dSYu Zhao return bus; 39dd7cc44dSYu Zhao 40dd7cc44dSYu Zhao child = pci_find_bus(pci_domain_nr(bus), busnr); 41dd7cc44dSYu Zhao if (child) 42dd7cc44dSYu Zhao return child; 43dd7cc44dSYu Zhao 44dd7cc44dSYu Zhao child = pci_add_new_bus(bus, NULL, busnr); 45dd7cc44dSYu Zhao if (!child) 46dd7cc44dSYu Zhao return NULL; 47dd7cc44dSYu Zhao 48dd7cc44dSYu Zhao child->subordinate = busnr; 49dd7cc44dSYu Zhao child->dev.parent = bus->bridge; 50dd7cc44dSYu Zhao rc = pci_bus_add_child(child); 51dd7cc44dSYu Zhao if (rc) { 52dd7cc44dSYu Zhao pci_remove_bus(child); 53dd7cc44dSYu Zhao return NULL; 54dd7cc44dSYu Zhao } 55dd7cc44dSYu Zhao 56dd7cc44dSYu Zhao return child; 57dd7cc44dSYu Zhao } 58dd7cc44dSYu Zhao 59dd7cc44dSYu Zhao static void virtfn_remove_bus(struct pci_bus *bus, int busnr) 60dd7cc44dSYu Zhao { 61dd7cc44dSYu Zhao struct pci_bus *child; 62dd7cc44dSYu Zhao 63dd7cc44dSYu Zhao if (bus->number == busnr) 64dd7cc44dSYu Zhao return; 65dd7cc44dSYu Zhao 66dd7cc44dSYu Zhao child = pci_find_bus(pci_domain_nr(bus), busnr); 67dd7cc44dSYu Zhao BUG_ON(!child); 68dd7cc44dSYu Zhao 69dd7cc44dSYu Zhao if (list_empty(&child->devices)) 70dd7cc44dSYu Zhao pci_remove_bus(child); 71dd7cc44dSYu Zhao } 72dd7cc44dSYu Zhao 73dd7cc44dSYu Zhao static int virtfn_add(struct pci_dev *dev, int id, int reset) 74dd7cc44dSYu Zhao { 75dd7cc44dSYu Zhao int i; 76dd7cc44dSYu Zhao int rc; 77dd7cc44dSYu Zhao u64 size; 78dd7cc44dSYu Zhao char buf[VIRTFN_ID_LEN]; 79dd7cc44dSYu Zhao struct pci_dev *virtfn; 80dd7cc44dSYu Zhao struct resource *res; 81dd7cc44dSYu Zhao struct pci_sriov *iov = dev->sriov; 82dd7cc44dSYu Zhao 83dd7cc44dSYu Zhao virtfn = alloc_pci_dev(); 84dd7cc44dSYu Zhao if (!virtfn) 85dd7cc44dSYu Zhao return -ENOMEM; 86dd7cc44dSYu Zhao 87dd7cc44dSYu Zhao mutex_lock(&iov->dev->sriov->lock); 88dd7cc44dSYu Zhao virtfn->bus = virtfn_add_bus(dev->bus, virtfn_bus(dev, id)); 89dd7cc44dSYu Zhao if (!virtfn->bus) { 90dd7cc44dSYu Zhao kfree(virtfn); 91dd7cc44dSYu Zhao mutex_unlock(&iov->dev->sriov->lock); 92dd7cc44dSYu Zhao return -ENOMEM; 93dd7cc44dSYu Zhao } 94dd7cc44dSYu Zhao virtfn->devfn = virtfn_devfn(dev, id); 95dd7cc44dSYu Zhao virtfn->vendor = dev->vendor; 96dd7cc44dSYu Zhao pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_DID, &virtfn->device); 97dd7cc44dSYu Zhao pci_setup_device(virtfn); 98dd7cc44dSYu Zhao virtfn->dev.parent = dev->dev.parent; 99dd7cc44dSYu Zhao 100dd7cc44dSYu Zhao for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { 101dd7cc44dSYu Zhao res = dev->resource + PCI_IOV_RESOURCES + i; 102dd7cc44dSYu Zhao if (!res->parent) 103dd7cc44dSYu Zhao continue; 104dd7cc44dSYu Zhao virtfn->resource[i].name = pci_name(virtfn); 105dd7cc44dSYu Zhao virtfn->resource[i].flags = res->flags; 106dd7cc44dSYu Zhao size = resource_size(res); 107dd7cc44dSYu Zhao do_div(size, iov->total); 108dd7cc44dSYu Zhao virtfn->resource[i].start = res->start + size * id; 109dd7cc44dSYu Zhao virtfn->resource[i].end = virtfn->resource[i].start + size - 1; 110dd7cc44dSYu Zhao rc = request_resource(res, &virtfn->resource[i]); 111dd7cc44dSYu Zhao BUG_ON(rc); 112dd7cc44dSYu Zhao } 113dd7cc44dSYu Zhao 114dd7cc44dSYu Zhao if (reset) 1158c1c699fSYu Zhao __pci_reset_function(virtfn); 116dd7cc44dSYu Zhao 117dd7cc44dSYu Zhao pci_device_add(virtfn, virtfn->bus); 118dd7cc44dSYu Zhao mutex_unlock(&iov->dev->sriov->lock); 119dd7cc44dSYu Zhao 120dd7cc44dSYu Zhao virtfn->physfn = pci_dev_get(dev); 121dd7cc44dSYu Zhao virtfn->is_virtfn = 1; 122dd7cc44dSYu Zhao 123dd7cc44dSYu Zhao rc = pci_bus_add_device(virtfn); 124dd7cc44dSYu Zhao if (rc) 125dd7cc44dSYu Zhao goto failed1; 126dd7cc44dSYu Zhao sprintf(buf, "virtfn%u", id); 127dd7cc44dSYu Zhao rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf); 128dd7cc44dSYu Zhao if (rc) 129dd7cc44dSYu Zhao goto failed1; 130dd7cc44dSYu Zhao rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn"); 131dd7cc44dSYu Zhao if (rc) 132dd7cc44dSYu Zhao goto failed2; 133dd7cc44dSYu Zhao 134dd7cc44dSYu Zhao kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE); 135dd7cc44dSYu Zhao 136dd7cc44dSYu Zhao return 0; 137dd7cc44dSYu Zhao 138dd7cc44dSYu Zhao failed2: 139dd7cc44dSYu Zhao sysfs_remove_link(&dev->dev.kobj, buf); 140dd7cc44dSYu Zhao failed1: 141dd7cc44dSYu Zhao pci_dev_put(dev); 142dd7cc44dSYu Zhao mutex_lock(&iov->dev->sriov->lock); 143dd7cc44dSYu Zhao pci_remove_bus_device(virtfn); 144dd7cc44dSYu Zhao virtfn_remove_bus(dev->bus, virtfn_bus(dev, id)); 145dd7cc44dSYu Zhao mutex_unlock(&iov->dev->sriov->lock); 146dd7cc44dSYu Zhao 147dd7cc44dSYu Zhao return rc; 148dd7cc44dSYu Zhao } 149dd7cc44dSYu Zhao 150dd7cc44dSYu Zhao static void virtfn_remove(struct pci_dev *dev, int id, int reset) 151dd7cc44dSYu Zhao { 152dd7cc44dSYu Zhao char buf[VIRTFN_ID_LEN]; 153dd7cc44dSYu Zhao struct pci_bus *bus; 154dd7cc44dSYu Zhao struct pci_dev *virtfn; 155dd7cc44dSYu Zhao struct pci_sriov *iov = dev->sriov; 156dd7cc44dSYu Zhao 157dd7cc44dSYu Zhao bus = pci_find_bus(pci_domain_nr(dev->bus), virtfn_bus(dev, id)); 158dd7cc44dSYu Zhao if (!bus) 159dd7cc44dSYu Zhao return; 160dd7cc44dSYu Zhao 161dd7cc44dSYu Zhao virtfn = pci_get_slot(bus, virtfn_devfn(dev, id)); 162dd7cc44dSYu Zhao if (!virtfn) 163dd7cc44dSYu Zhao return; 164dd7cc44dSYu Zhao 165dd7cc44dSYu Zhao pci_dev_put(virtfn); 166dd7cc44dSYu Zhao 167dd7cc44dSYu Zhao if (reset) { 168dd7cc44dSYu Zhao device_release_driver(&virtfn->dev); 1698c1c699fSYu Zhao __pci_reset_function(virtfn); 170dd7cc44dSYu Zhao } 171dd7cc44dSYu Zhao 172dd7cc44dSYu Zhao sprintf(buf, "virtfn%u", id); 173dd7cc44dSYu Zhao sysfs_remove_link(&dev->dev.kobj, buf); 174dd7cc44dSYu Zhao sysfs_remove_link(&virtfn->dev.kobj, "physfn"); 175dd7cc44dSYu Zhao 176dd7cc44dSYu Zhao mutex_lock(&iov->dev->sriov->lock); 177dd7cc44dSYu Zhao pci_remove_bus_device(virtfn); 178dd7cc44dSYu Zhao virtfn_remove_bus(dev->bus, virtfn_bus(dev, id)); 179dd7cc44dSYu Zhao mutex_unlock(&iov->dev->sriov->lock); 180dd7cc44dSYu Zhao 181dd7cc44dSYu Zhao pci_dev_put(dev); 182dd7cc44dSYu Zhao } 183dd7cc44dSYu Zhao 18474bb1bccSYu Zhao static int sriov_migration(struct pci_dev *dev) 18574bb1bccSYu Zhao { 18674bb1bccSYu Zhao u16 status; 18774bb1bccSYu Zhao struct pci_sriov *iov = dev->sriov; 18874bb1bccSYu Zhao 18974bb1bccSYu Zhao if (!iov->nr_virtfn) 19074bb1bccSYu Zhao return 0; 19174bb1bccSYu Zhao 19274bb1bccSYu Zhao if (!(iov->cap & PCI_SRIOV_CAP_VFM)) 19374bb1bccSYu Zhao return 0; 19474bb1bccSYu Zhao 19574bb1bccSYu Zhao pci_read_config_word(dev, iov->pos + PCI_SRIOV_STATUS, &status); 19674bb1bccSYu Zhao if (!(status & PCI_SRIOV_STATUS_VFM)) 19774bb1bccSYu Zhao return 0; 19874bb1bccSYu Zhao 19974bb1bccSYu Zhao schedule_work(&iov->mtask); 20074bb1bccSYu Zhao 20174bb1bccSYu Zhao return 1; 20274bb1bccSYu Zhao } 20374bb1bccSYu Zhao 20474bb1bccSYu Zhao static void sriov_migration_task(struct work_struct *work) 20574bb1bccSYu Zhao { 20674bb1bccSYu Zhao int i; 20774bb1bccSYu Zhao u8 state; 20874bb1bccSYu Zhao u16 status; 20974bb1bccSYu Zhao struct pci_sriov *iov = container_of(work, struct pci_sriov, mtask); 21074bb1bccSYu Zhao 21174bb1bccSYu Zhao for (i = iov->initial; i < iov->nr_virtfn; i++) { 21274bb1bccSYu Zhao state = readb(iov->mstate + i); 21374bb1bccSYu Zhao if (state == PCI_SRIOV_VFM_MI) { 21474bb1bccSYu Zhao writeb(PCI_SRIOV_VFM_AV, iov->mstate + i); 21574bb1bccSYu Zhao state = readb(iov->mstate + i); 21674bb1bccSYu Zhao if (state == PCI_SRIOV_VFM_AV) 21774bb1bccSYu Zhao virtfn_add(iov->self, i, 1); 21874bb1bccSYu Zhao } else if (state == PCI_SRIOV_VFM_MO) { 21974bb1bccSYu Zhao virtfn_remove(iov->self, i, 1); 22074bb1bccSYu Zhao writeb(PCI_SRIOV_VFM_UA, iov->mstate + i); 22174bb1bccSYu Zhao state = readb(iov->mstate + i); 22274bb1bccSYu Zhao if (state == PCI_SRIOV_VFM_AV) 22374bb1bccSYu Zhao virtfn_add(iov->self, i, 0); 22474bb1bccSYu Zhao } 22574bb1bccSYu Zhao } 22674bb1bccSYu Zhao 22774bb1bccSYu Zhao pci_read_config_word(iov->self, iov->pos + PCI_SRIOV_STATUS, &status); 22874bb1bccSYu Zhao status &= ~PCI_SRIOV_STATUS_VFM; 22974bb1bccSYu Zhao pci_write_config_word(iov->self, iov->pos + PCI_SRIOV_STATUS, status); 23074bb1bccSYu Zhao } 23174bb1bccSYu Zhao 23274bb1bccSYu Zhao static int sriov_enable_migration(struct pci_dev *dev, int nr_virtfn) 23374bb1bccSYu Zhao { 23474bb1bccSYu Zhao int bir; 23574bb1bccSYu Zhao u32 table; 23674bb1bccSYu Zhao resource_size_t pa; 23774bb1bccSYu Zhao struct pci_sriov *iov = dev->sriov; 23874bb1bccSYu Zhao 23974bb1bccSYu Zhao if (nr_virtfn <= iov->initial) 24074bb1bccSYu Zhao return 0; 24174bb1bccSYu Zhao 24274bb1bccSYu Zhao pci_read_config_dword(dev, iov->pos + PCI_SRIOV_VFM, &table); 24374bb1bccSYu Zhao bir = PCI_SRIOV_VFM_BIR(table); 24474bb1bccSYu Zhao if (bir > PCI_STD_RESOURCE_END) 24574bb1bccSYu Zhao return -EIO; 24674bb1bccSYu Zhao 24774bb1bccSYu Zhao table = PCI_SRIOV_VFM_OFFSET(table); 24874bb1bccSYu Zhao if (table + nr_virtfn > pci_resource_len(dev, bir)) 24974bb1bccSYu Zhao return -EIO; 25074bb1bccSYu Zhao 25174bb1bccSYu Zhao pa = pci_resource_start(dev, bir) + table; 25274bb1bccSYu Zhao iov->mstate = ioremap(pa, nr_virtfn); 25374bb1bccSYu Zhao if (!iov->mstate) 25474bb1bccSYu Zhao return -ENOMEM; 25574bb1bccSYu Zhao 25674bb1bccSYu Zhao INIT_WORK(&iov->mtask, sriov_migration_task); 25774bb1bccSYu Zhao 25874bb1bccSYu Zhao iov->ctrl |= PCI_SRIOV_CTRL_VFM | PCI_SRIOV_CTRL_INTR; 25974bb1bccSYu Zhao pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); 26074bb1bccSYu Zhao 26174bb1bccSYu Zhao return 0; 26274bb1bccSYu Zhao } 26374bb1bccSYu Zhao 26474bb1bccSYu Zhao static void sriov_disable_migration(struct pci_dev *dev) 26574bb1bccSYu Zhao { 26674bb1bccSYu Zhao struct pci_sriov *iov = dev->sriov; 26774bb1bccSYu Zhao 26874bb1bccSYu Zhao iov->ctrl &= ~(PCI_SRIOV_CTRL_VFM | PCI_SRIOV_CTRL_INTR); 26974bb1bccSYu Zhao pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); 27074bb1bccSYu Zhao 27174bb1bccSYu Zhao cancel_work_sync(&iov->mtask); 27274bb1bccSYu Zhao iounmap(iov->mstate); 27374bb1bccSYu Zhao } 27474bb1bccSYu Zhao 275dd7cc44dSYu Zhao static int sriov_enable(struct pci_dev *dev, int nr_virtfn) 276dd7cc44dSYu Zhao { 277dd7cc44dSYu Zhao int rc; 278dd7cc44dSYu Zhao int i, j; 279dd7cc44dSYu Zhao int nres; 280dd7cc44dSYu Zhao u16 offset, stride, initial; 281dd7cc44dSYu Zhao struct resource *res; 282dd7cc44dSYu Zhao struct pci_dev *pdev; 283dd7cc44dSYu Zhao struct pci_sriov *iov = dev->sriov; 284dd7cc44dSYu Zhao 285dd7cc44dSYu Zhao if (!nr_virtfn) 286dd7cc44dSYu Zhao return 0; 287dd7cc44dSYu Zhao 288dd7cc44dSYu Zhao if (iov->nr_virtfn) 289dd7cc44dSYu Zhao return -EINVAL; 290dd7cc44dSYu Zhao 291dd7cc44dSYu Zhao pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial); 292dd7cc44dSYu Zhao if (initial > iov->total || 293dd7cc44dSYu Zhao (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total))) 294dd7cc44dSYu Zhao return -EIO; 295dd7cc44dSYu Zhao 296dd7cc44dSYu Zhao if (nr_virtfn < 0 || nr_virtfn > iov->total || 297dd7cc44dSYu Zhao (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial))) 298dd7cc44dSYu Zhao return -EINVAL; 299dd7cc44dSYu Zhao 300dd7cc44dSYu Zhao pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn); 301dd7cc44dSYu Zhao pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &offset); 302dd7cc44dSYu Zhao pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &stride); 303dd7cc44dSYu Zhao if (!offset || (nr_virtfn > 1 && !stride)) 304dd7cc44dSYu Zhao return -EIO; 305dd7cc44dSYu Zhao 306dd7cc44dSYu Zhao nres = 0; 307dd7cc44dSYu Zhao for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { 308dd7cc44dSYu Zhao res = dev->resource + PCI_IOV_RESOURCES + i; 309dd7cc44dSYu Zhao if (res->parent) 310dd7cc44dSYu Zhao nres++; 311dd7cc44dSYu Zhao } 312dd7cc44dSYu Zhao if (nres != iov->nres) { 313dd7cc44dSYu Zhao dev_err(&dev->dev, "not enough MMIO resources for SR-IOV\n"); 314dd7cc44dSYu Zhao return -ENOMEM; 315dd7cc44dSYu Zhao } 316dd7cc44dSYu Zhao 317dd7cc44dSYu Zhao iov->offset = offset; 318dd7cc44dSYu Zhao iov->stride = stride; 319dd7cc44dSYu Zhao 320dd7cc44dSYu Zhao if (virtfn_bus(dev, nr_virtfn - 1) > dev->bus->subordinate) { 321dd7cc44dSYu Zhao dev_err(&dev->dev, "SR-IOV: bus number out of range\n"); 322dd7cc44dSYu Zhao return -ENOMEM; 323dd7cc44dSYu Zhao } 324dd7cc44dSYu Zhao 325dd7cc44dSYu Zhao if (iov->link != dev->devfn) { 326dd7cc44dSYu Zhao pdev = pci_get_slot(dev->bus, iov->link); 327dd7cc44dSYu Zhao if (!pdev) 328dd7cc44dSYu Zhao return -ENODEV; 329dd7cc44dSYu Zhao 330dd7cc44dSYu Zhao pci_dev_put(pdev); 331dd7cc44dSYu Zhao 332dd7cc44dSYu Zhao if (!pdev->is_physfn) 333dd7cc44dSYu Zhao return -ENODEV; 334dd7cc44dSYu Zhao 335dd7cc44dSYu Zhao rc = sysfs_create_link(&dev->dev.kobj, 336dd7cc44dSYu Zhao &pdev->dev.kobj, "dep_link"); 337dd7cc44dSYu Zhao if (rc) 338dd7cc44dSYu Zhao return rc; 339dd7cc44dSYu Zhao } 340dd7cc44dSYu Zhao 341dd7cc44dSYu Zhao iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE; 342dd7cc44dSYu Zhao pci_block_user_cfg_access(dev); 343dd7cc44dSYu Zhao pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); 344dd7cc44dSYu Zhao msleep(100); 345dd7cc44dSYu Zhao pci_unblock_user_cfg_access(dev); 346dd7cc44dSYu Zhao 347dd7cc44dSYu Zhao iov->initial = initial; 348dd7cc44dSYu Zhao if (nr_virtfn < initial) 349dd7cc44dSYu Zhao initial = nr_virtfn; 350dd7cc44dSYu Zhao 351dd7cc44dSYu Zhao for (i = 0; i < initial; i++) { 352dd7cc44dSYu Zhao rc = virtfn_add(dev, i, 0); 353dd7cc44dSYu Zhao if (rc) 354dd7cc44dSYu Zhao goto failed; 355dd7cc44dSYu Zhao } 356dd7cc44dSYu Zhao 35774bb1bccSYu Zhao if (iov->cap & PCI_SRIOV_CAP_VFM) { 35874bb1bccSYu Zhao rc = sriov_enable_migration(dev, nr_virtfn); 35974bb1bccSYu Zhao if (rc) 36074bb1bccSYu Zhao goto failed; 36174bb1bccSYu Zhao } 36274bb1bccSYu Zhao 363dd7cc44dSYu Zhao kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE); 364dd7cc44dSYu Zhao iov->nr_virtfn = nr_virtfn; 365dd7cc44dSYu Zhao 366dd7cc44dSYu Zhao return 0; 367dd7cc44dSYu Zhao 368dd7cc44dSYu Zhao failed: 369dd7cc44dSYu Zhao for (j = 0; j < i; j++) 370dd7cc44dSYu Zhao virtfn_remove(dev, j, 0); 371dd7cc44dSYu Zhao 372dd7cc44dSYu Zhao iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE); 373dd7cc44dSYu Zhao pci_block_user_cfg_access(dev); 374dd7cc44dSYu Zhao pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); 375dd7cc44dSYu Zhao ssleep(1); 376dd7cc44dSYu Zhao pci_unblock_user_cfg_access(dev); 377dd7cc44dSYu Zhao 378dd7cc44dSYu Zhao if (iov->link != dev->devfn) 379dd7cc44dSYu Zhao sysfs_remove_link(&dev->dev.kobj, "dep_link"); 380dd7cc44dSYu Zhao 381dd7cc44dSYu Zhao return rc; 382dd7cc44dSYu Zhao } 383dd7cc44dSYu Zhao 384dd7cc44dSYu Zhao static void sriov_disable(struct pci_dev *dev) 385dd7cc44dSYu Zhao { 386dd7cc44dSYu Zhao int i; 387dd7cc44dSYu Zhao struct pci_sriov *iov = dev->sriov; 388dd7cc44dSYu Zhao 389dd7cc44dSYu Zhao if (!iov->nr_virtfn) 390dd7cc44dSYu Zhao return; 391dd7cc44dSYu Zhao 39274bb1bccSYu Zhao if (iov->cap & PCI_SRIOV_CAP_VFM) 39374bb1bccSYu Zhao sriov_disable_migration(dev); 39474bb1bccSYu Zhao 395dd7cc44dSYu Zhao for (i = 0; i < iov->nr_virtfn; i++) 396dd7cc44dSYu Zhao virtfn_remove(dev, i, 0); 397dd7cc44dSYu Zhao 398dd7cc44dSYu Zhao iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE); 399dd7cc44dSYu Zhao pci_block_user_cfg_access(dev); 400dd7cc44dSYu Zhao pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); 401dd7cc44dSYu Zhao ssleep(1); 402dd7cc44dSYu Zhao pci_unblock_user_cfg_access(dev); 403dd7cc44dSYu Zhao 404dd7cc44dSYu Zhao if (iov->link != dev->devfn) 405dd7cc44dSYu Zhao sysfs_remove_link(&dev->dev.kobj, "dep_link"); 406dd7cc44dSYu Zhao 407dd7cc44dSYu Zhao iov->nr_virtfn = 0; 408dd7cc44dSYu Zhao } 409dd7cc44dSYu Zhao 410d1b054daSYu Zhao static int sriov_init(struct pci_dev *dev, int pos) 411d1b054daSYu Zhao { 412d1b054daSYu Zhao int i; 413d1b054daSYu Zhao int rc; 414d1b054daSYu Zhao int nres; 415d1b054daSYu Zhao u32 pgsz; 416d1b054daSYu Zhao u16 ctrl, total, offset, stride; 417d1b054daSYu Zhao struct pci_sriov *iov; 418d1b054daSYu Zhao struct resource *res; 419d1b054daSYu Zhao struct pci_dev *pdev; 420d1b054daSYu Zhao 421d1b054daSYu Zhao if (dev->pcie_type != PCI_EXP_TYPE_RC_END && 422d1b054daSYu Zhao dev->pcie_type != PCI_EXP_TYPE_ENDPOINT) 423d1b054daSYu Zhao return -ENODEV; 424d1b054daSYu Zhao 425d1b054daSYu Zhao pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl); 426d1b054daSYu Zhao if (ctrl & PCI_SRIOV_CTRL_VFE) { 427d1b054daSYu Zhao pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0); 428d1b054daSYu Zhao ssleep(1); 429d1b054daSYu Zhao } 430d1b054daSYu Zhao 431d1b054daSYu Zhao pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total); 432d1b054daSYu Zhao if (!total) 433d1b054daSYu Zhao return 0; 434d1b054daSYu Zhao 435d1b054daSYu Zhao ctrl = 0; 436d1b054daSYu Zhao list_for_each_entry(pdev, &dev->bus->devices, bus_list) 437d1b054daSYu Zhao if (pdev->is_physfn) 438d1b054daSYu Zhao goto found; 439d1b054daSYu Zhao 440d1b054daSYu Zhao pdev = NULL; 441d1b054daSYu Zhao if (pci_ari_enabled(dev->bus)) 442d1b054daSYu Zhao ctrl |= PCI_SRIOV_CTRL_ARI; 443d1b054daSYu Zhao 444d1b054daSYu Zhao found: 445d1b054daSYu Zhao pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl); 446d1b054daSYu Zhao pci_write_config_word(dev, pos + PCI_SRIOV_NUM_VF, total); 447d1b054daSYu Zhao pci_read_config_word(dev, pos + PCI_SRIOV_VF_OFFSET, &offset); 448d1b054daSYu Zhao pci_read_config_word(dev, pos + PCI_SRIOV_VF_STRIDE, &stride); 449d1b054daSYu Zhao if (!offset || (total > 1 && !stride)) 450d1b054daSYu Zhao return -EIO; 451d1b054daSYu Zhao 452d1b054daSYu Zhao pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz); 453d1b054daSYu Zhao i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0; 454d1b054daSYu Zhao pgsz &= ~((1 << i) - 1); 455d1b054daSYu Zhao if (!pgsz) 456d1b054daSYu Zhao return -EIO; 457d1b054daSYu Zhao 458d1b054daSYu Zhao pgsz &= ~(pgsz - 1); 459d1b054daSYu Zhao pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz); 460d1b054daSYu Zhao 461d1b054daSYu Zhao nres = 0; 462d1b054daSYu Zhao for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { 463d1b054daSYu Zhao res = dev->resource + PCI_IOV_RESOURCES + i; 464d1b054daSYu Zhao i += __pci_read_base(dev, pci_bar_unknown, res, 465d1b054daSYu Zhao pos + PCI_SRIOV_BAR + i * 4); 466d1b054daSYu Zhao if (!res->flags) 467d1b054daSYu Zhao continue; 468d1b054daSYu Zhao if (resource_size(res) & (PAGE_SIZE - 1)) { 469d1b054daSYu Zhao rc = -EIO; 470d1b054daSYu Zhao goto failed; 471d1b054daSYu Zhao } 472d1b054daSYu Zhao res->end = res->start + resource_size(res) * total - 1; 473d1b054daSYu Zhao nres++; 474d1b054daSYu Zhao } 475d1b054daSYu Zhao 476d1b054daSYu Zhao iov = kzalloc(sizeof(*iov), GFP_KERNEL); 477d1b054daSYu Zhao if (!iov) { 478d1b054daSYu Zhao rc = -ENOMEM; 479d1b054daSYu Zhao goto failed; 480d1b054daSYu Zhao } 481d1b054daSYu Zhao 482d1b054daSYu Zhao iov->pos = pos; 483d1b054daSYu Zhao iov->nres = nres; 484d1b054daSYu Zhao iov->ctrl = ctrl; 485d1b054daSYu Zhao iov->total = total; 486d1b054daSYu Zhao iov->offset = offset; 487d1b054daSYu Zhao iov->stride = stride; 488d1b054daSYu Zhao iov->pgsz = pgsz; 489d1b054daSYu Zhao iov->self = dev; 490d1b054daSYu Zhao pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap); 491d1b054daSYu Zhao pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link); 4924d135dbeSYu Zhao if (dev->pcie_type == PCI_EXP_TYPE_RC_END) 4934d135dbeSYu Zhao iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link); 494d1b054daSYu Zhao 495d1b054daSYu Zhao if (pdev) 496d1b054daSYu Zhao iov->dev = pci_dev_get(pdev); 497e277d2fcSYu Zhao else 498d1b054daSYu Zhao iov->dev = dev; 499e277d2fcSYu Zhao 500d1b054daSYu Zhao mutex_init(&iov->lock); 501d1b054daSYu Zhao 502d1b054daSYu Zhao dev->sriov = iov; 503d1b054daSYu Zhao dev->is_physfn = 1; 504d1b054daSYu Zhao 505d1b054daSYu Zhao return 0; 506d1b054daSYu Zhao 507d1b054daSYu Zhao failed: 508d1b054daSYu Zhao for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { 509d1b054daSYu Zhao res = dev->resource + PCI_IOV_RESOURCES + i; 510d1b054daSYu Zhao res->flags = 0; 511d1b054daSYu Zhao } 512d1b054daSYu Zhao 513d1b054daSYu Zhao return rc; 514d1b054daSYu Zhao } 515d1b054daSYu Zhao 516d1b054daSYu Zhao static void sriov_release(struct pci_dev *dev) 517d1b054daSYu Zhao { 518dd7cc44dSYu Zhao BUG_ON(dev->sriov->nr_virtfn); 519dd7cc44dSYu Zhao 520e277d2fcSYu Zhao if (dev != dev->sriov->dev) 521d1b054daSYu Zhao pci_dev_put(dev->sriov->dev); 522d1b054daSYu Zhao 523e277d2fcSYu Zhao mutex_destroy(&dev->sriov->lock); 524e277d2fcSYu Zhao 525d1b054daSYu Zhao kfree(dev->sriov); 526d1b054daSYu Zhao dev->sriov = NULL; 527d1b054daSYu Zhao } 528d1b054daSYu Zhao 5298c5cdb6aSYu Zhao static void sriov_restore_state(struct pci_dev *dev) 5308c5cdb6aSYu Zhao { 5318c5cdb6aSYu Zhao int i; 5328c5cdb6aSYu Zhao u16 ctrl; 5338c5cdb6aSYu Zhao struct pci_sriov *iov = dev->sriov; 5348c5cdb6aSYu Zhao 5358c5cdb6aSYu Zhao pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl); 5368c5cdb6aSYu Zhao if (ctrl & PCI_SRIOV_CTRL_VFE) 5378c5cdb6aSYu Zhao return; 5388c5cdb6aSYu Zhao 5398c5cdb6aSYu Zhao for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++) 5408c5cdb6aSYu Zhao pci_update_resource(dev, i); 5418c5cdb6aSYu Zhao 5428c5cdb6aSYu Zhao pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz); 543dd7cc44dSYu Zhao pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, iov->nr_virtfn); 5448c5cdb6aSYu Zhao pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); 5458c5cdb6aSYu Zhao if (iov->ctrl & PCI_SRIOV_CTRL_VFE) 5468c5cdb6aSYu Zhao msleep(100); 5478c5cdb6aSYu Zhao } 5488c5cdb6aSYu Zhao 549d1b054daSYu Zhao /** 550d1b054daSYu Zhao * pci_iov_init - initialize the IOV capability 551d1b054daSYu Zhao * @dev: the PCI device 552d1b054daSYu Zhao * 553d1b054daSYu Zhao * Returns 0 on success, or negative on failure. 554d1b054daSYu Zhao */ 555d1b054daSYu Zhao int pci_iov_init(struct pci_dev *dev) 556d1b054daSYu Zhao { 557d1b054daSYu Zhao int pos; 558d1b054daSYu Zhao 5595f4d91a1SKenji Kaneshige if (!pci_is_pcie(dev)) 560d1b054daSYu Zhao return -ENODEV; 561d1b054daSYu Zhao 562d1b054daSYu Zhao pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV); 563d1b054daSYu Zhao if (pos) 564d1b054daSYu Zhao return sriov_init(dev, pos); 565d1b054daSYu Zhao 566d1b054daSYu Zhao return -ENODEV; 567d1b054daSYu Zhao } 568d1b054daSYu Zhao 569d1b054daSYu Zhao /** 570d1b054daSYu Zhao * pci_iov_release - release resources used by the IOV capability 571d1b054daSYu Zhao * @dev: the PCI device 572d1b054daSYu Zhao */ 573d1b054daSYu Zhao void pci_iov_release(struct pci_dev *dev) 574d1b054daSYu Zhao { 575d1b054daSYu Zhao if (dev->is_physfn) 576d1b054daSYu Zhao sriov_release(dev); 577d1b054daSYu Zhao } 578d1b054daSYu Zhao 579d1b054daSYu Zhao /** 580d1b054daSYu Zhao * pci_iov_resource_bar - get position of the SR-IOV BAR 581d1b054daSYu Zhao * @dev: the PCI device 582d1b054daSYu Zhao * @resno: the resource number 583d1b054daSYu Zhao * @type: the BAR type to be filled in 584d1b054daSYu Zhao * 585d1b054daSYu Zhao * Returns position of the BAR encapsulated in the SR-IOV capability. 586d1b054daSYu Zhao */ 587d1b054daSYu Zhao int pci_iov_resource_bar(struct pci_dev *dev, int resno, 588d1b054daSYu Zhao enum pci_bar_type *type) 589d1b054daSYu Zhao { 590d1b054daSYu Zhao if (resno < PCI_IOV_RESOURCES || resno > PCI_IOV_RESOURCE_END) 591d1b054daSYu Zhao return 0; 592d1b054daSYu Zhao 593d1b054daSYu Zhao BUG_ON(!dev->is_physfn); 594d1b054daSYu Zhao 595d1b054daSYu Zhao *type = pci_bar_unknown; 596d1b054daSYu Zhao 597d1b054daSYu Zhao return dev->sriov->pos + PCI_SRIOV_BAR + 598d1b054daSYu Zhao 4 * (resno - PCI_IOV_RESOURCES); 599d1b054daSYu Zhao } 6008c5cdb6aSYu Zhao 6018c5cdb6aSYu Zhao /** 6026faf17f6SChris Wright * pci_sriov_resource_alignment - get resource alignment for VF BAR 6036faf17f6SChris Wright * @dev: the PCI device 6046faf17f6SChris Wright * @resno: the resource number 6056faf17f6SChris Wright * 6066faf17f6SChris Wright * Returns the alignment of the VF BAR found in the SR-IOV capability. 6076faf17f6SChris Wright * This is not the same as the resource size which is defined as 6086faf17f6SChris Wright * the VF BAR size multiplied by the number of VFs. The alignment 6096faf17f6SChris Wright * is just the VF BAR size. 6106faf17f6SChris Wright */ 611*0e52247aSCam Macdonell resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno) 6126faf17f6SChris Wright { 6136faf17f6SChris Wright struct resource tmp; 6146faf17f6SChris Wright enum pci_bar_type type; 6156faf17f6SChris Wright int reg = pci_iov_resource_bar(dev, resno, &type); 6166faf17f6SChris Wright 6176faf17f6SChris Wright if (!reg) 6186faf17f6SChris Wright return 0; 6196faf17f6SChris Wright 6206faf17f6SChris Wright __pci_read_base(dev, type, &tmp, reg); 6216faf17f6SChris Wright return resource_alignment(&tmp); 6226faf17f6SChris Wright } 6236faf17f6SChris Wright 6246faf17f6SChris Wright /** 6258c5cdb6aSYu Zhao * pci_restore_iov_state - restore the state of the IOV capability 6268c5cdb6aSYu Zhao * @dev: the PCI device 6278c5cdb6aSYu Zhao */ 6288c5cdb6aSYu Zhao void pci_restore_iov_state(struct pci_dev *dev) 6298c5cdb6aSYu Zhao { 6308c5cdb6aSYu Zhao if (dev->is_physfn) 6318c5cdb6aSYu Zhao sriov_restore_state(dev); 6328c5cdb6aSYu Zhao } 633a28724b0SYu Zhao 634a28724b0SYu Zhao /** 635a28724b0SYu Zhao * pci_iov_bus_range - find bus range used by Virtual Function 636a28724b0SYu Zhao * @bus: the PCI bus 637a28724b0SYu Zhao * 638a28724b0SYu Zhao * Returns max number of buses (exclude current one) used by Virtual 639a28724b0SYu Zhao * Functions. 640a28724b0SYu Zhao */ 641a28724b0SYu Zhao int pci_iov_bus_range(struct pci_bus *bus) 642a28724b0SYu Zhao { 643a28724b0SYu Zhao int max = 0; 644a28724b0SYu Zhao u8 busnr; 645a28724b0SYu Zhao struct pci_dev *dev; 646a28724b0SYu Zhao 647a28724b0SYu Zhao list_for_each_entry(dev, &bus->devices, bus_list) { 648a28724b0SYu Zhao if (!dev->is_physfn) 649a28724b0SYu Zhao continue; 650a28724b0SYu Zhao busnr = virtfn_bus(dev, dev->sriov->total - 1); 651a28724b0SYu Zhao if (busnr > max) 652a28724b0SYu Zhao max = busnr; 653a28724b0SYu Zhao } 654a28724b0SYu Zhao 655a28724b0SYu Zhao return max ? max - bus->number : 0; 656a28724b0SYu Zhao } 657dd7cc44dSYu Zhao 658dd7cc44dSYu Zhao /** 659dd7cc44dSYu Zhao * pci_enable_sriov - enable the SR-IOV capability 660dd7cc44dSYu Zhao * @dev: the PCI device 66152a8873bSRandy Dunlap * @nr_virtfn: number of virtual functions to enable 662dd7cc44dSYu Zhao * 663dd7cc44dSYu Zhao * Returns 0 on success, or negative on failure. 664dd7cc44dSYu Zhao */ 665dd7cc44dSYu Zhao int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn) 666dd7cc44dSYu Zhao { 667dd7cc44dSYu Zhao might_sleep(); 668dd7cc44dSYu Zhao 669dd7cc44dSYu Zhao if (!dev->is_physfn) 670dd7cc44dSYu Zhao return -ENODEV; 671dd7cc44dSYu Zhao 672dd7cc44dSYu Zhao return sriov_enable(dev, nr_virtfn); 673dd7cc44dSYu Zhao } 674dd7cc44dSYu Zhao EXPORT_SYMBOL_GPL(pci_enable_sriov); 675dd7cc44dSYu Zhao 676dd7cc44dSYu Zhao /** 677dd7cc44dSYu Zhao * pci_disable_sriov - disable the SR-IOV capability 678dd7cc44dSYu Zhao * @dev: the PCI device 679dd7cc44dSYu Zhao */ 680dd7cc44dSYu Zhao void pci_disable_sriov(struct pci_dev *dev) 681dd7cc44dSYu Zhao { 682dd7cc44dSYu Zhao might_sleep(); 683dd7cc44dSYu Zhao 684dd7cc44dSYu Zhao if (!dev->is_physfn) 685dd7cc44dSYu Zhao return; 686dd7cc44dSYu Zhao 687dd7cc44dSYu Zhao sriov_disable(dev); 688dd7cc44dSYu Zhao } 689dd7cc44dSYu Zhao EXPORT_SYMBOL_GPL(pci_disable_sriov); 69074bb1bccSYu Zhao 69174bb1bccSYu Zhao /** 69274bb1bccSYu Zhao * pci_sriov_migration - notify SR-IOV core of Virtual Function Migration 69374bb1bccSYu Zhao * @dev: the PCI device 69474bb1bccSYu Zhao * 69574bb1bccSYu Zhao * Returns IRQ_HANDLED if the IRQ is handled, or IRQ_NONE if not. 69674bb1bccSYu Zhao * 69774bb1bccSYu Zhao * Physical Function driver is responsible to register IRQ handler using 69874bb1bccSYu Zhao * VF Migration Interrupt Message Number, and call this function when the 69974bb1bccSYu Zhao * interrupt is generated by the hardware. 70074bb1bccSYu Zhao */ 70174bb1bccSYu Zhao irqreturn_t pci_sriov_migration(struct pci_dev *dev) 70274bb1bccSYu Zhao { 70374bb1bccSYu Zhao if (!dev->is_physfn) 70474bb1bccSYu Zhao return IRQ_NONE; 70574bb1bccSYu Zhao 70674bb1bccSYu Zhao return sriov_migration(dev) ? IRQ_HANDLED : IRQ_NONE; 70774bb1bccSYu Zhao } 70874bb1bccSYu Zhao EXPORT_SYMBOL_GPL(pci_sriov_migration); 709302b4215SYu Zhao 710fb8a0d9dSWilliams, Mitch A /** 711fb8a0d9dSWilliams, Mitch A * pci_num_vf - return number of VFs associated with a PF device_release_driver 712fb8a0d9dSWilliams, Mitch A * @dev: the PCI device 713fb8a0d9dSWilliams, Mitch A * 714fb8a0d9dSWilliams, Mitch A * Returns number of VFs, or 0 if SR-IOV is not enabled. 715fb8a0d9dSWilliams, Mitch A */ 716fb8a0d9dSWilliams, Mitch A int pci_num_vf(struct pci_dev *dev) 717fb8a0d9dSWilliams, Mitch A { 718fb8a0d9dSWilliams, Mitch A if (!dev || !dev->is_physfn) 719fb8a0d9dSWilliams, Mitch A return 0; 720fb8a0d9dSWilliams, Mitch A else 721fb8a0d9dSWilliams, Mitch A return dev->sriov->nr_virtfn; 722fb8a0d9dSWilliams, Mitch A } 723fb8a0d9dSWilliams, Mitch A EXPORT_SYMBOL_GPL(pci_num_vf); 724fb8a0d9dSWilliams, Mitch A 725302b4215SYu Zhao static int ats_alloc_one(struct pci_dev *dev, int ps) 726302b4215SYu Zhao { 727302b4215SYu Zhao int pos; 728302b4215SYu Zhao u16 cap; 729302b4215SYu Zhao struct pci_ats *ats; 730302b4215SYu Zhao 731302b4215SYu Zhao pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS); 732302b4215SYu Zhao if (!pos) 733302b4215SYu Zhao return -ENODEV; 734302b4215SYu Zhao 735302b4215SYu Zhao ats = kzalloc(sizeof(*ats), GFP_KERNEL); 736302b4215SYu Zhao if (!ats) 737302b4215SYu Zhao return -ENOMEM; 738302b4215SYu Zhao 739302b4215SYu Zhao ats->pos = pos; 740302b4215SYu Zhao ats->stu = ps; 741302b4215SYu Zhao pci_read_config_word(dev, pos + PCI_ATS_CAP, &cap); 742302b4215SYu Zhao ats->qdep = PCI_ATS_CAP_QDEP(cap) ? PCI_ATS_CAP_QDEP(cap) : 743302b4215SYu Zhao PCI_ATS_MAX_QDEP; 744302b4215SYu Zhao dev->ats = ats; 745302b4215SYu Zhao 746302b4215SYu Zhao return 0; 747302b4215SYu Zhao } 748302b4215SYu Zhao 749302b4215SYu Zhao static void ats_free_one(struct pci_dev *dev) 750302b4215SYu Zhao { 751302b4215SYu Zhao kfree(dev->ats); 752302b4215SYu Zhao dev->ats = NULL; 753302b4215SYu Zhao } 754302b4215SYu Zhao 755302b4215SYu Zhao /** 756302b4215SYu Zhao * pci_enable_ats - enable the ATS capability 757302b4215SYu Zhao * @dev: the PCI device 758302b4215SYu Zhao * @ps: the IOMMU page shift 759302b4215SYu Zhao * 760302b4215SYu Zhao * Returns 0 on success, or negative on failure. 761302b4215SYu Zhao */ 762302b4215SYu Zhao int pci_enable_ats(struct pci_dev *dev, int ps) 763302b4215SYu Zhao { 764302b4215SYu Zhao int rc; 765302b4215SYu Zhao u16 ctrl; 766302b4215SYu Zhao 767e277d2fcSYu Zhao BUG_ON(dev->ats && dev->ats->is_enabled); 768302b4215SYu Zhao 769302b4215SYu Zhao if (ps < PCI_ATS_MIN_STU) 770302b4215SYu Zhao return -EINVAL; 771302b4215SYu Zhao 772e277d2fcSYu Zhao if (dev->is_physfn || dev->is_virtfn) { 773e277d2fcSYu Zhao struct pci_dev *pdev = dev->is_physfn ? dev : dev->physfn; 774e277d2fcSYu Zhao 775e277d2fcSYu Zhao mutex_lock(&pdev->sriov->lock); 776e277d2fcSYu Zhao if (pdev->ats) 777e277d2fcSYu Zhao rc = pdev->ats->stu == ps ? 0 : -EINVAL; 778e277d2fcSYu Zhao else 779e277d2fcSYu Zhao rc = ats_alloc_one(pdev, ps); 780e277d2fcSYu Zhao 781e277d2fcSYu Zhao if (!rc) 782e277d2fcSYu Zhao pdev->ats->ref_cnt++; 783e277d2fcSYu Zhao mutex_unlock(&pdev->sriov->lock); 784e277d2fcSYu Zhao if (rc) 785e277d2fcSYu Zhao return rc; 786e277d2fcSYu Zhao } 787e277d2fcSYu Zhao 788e277d2fcSYu Zhao if (!dev->is_physfn) { 789302b4215SYu Zhao rc = ats_alloc_one(dev, ps); 790302b4215SYu Zhao if (rc) 791302b4215SYu Zhao return rc; 792e277d2fcSYu Zhao } 793302b4215SYu Zhao 794302b4215SYu Zhao ctrl = PCI_ATS_CTRL_ENABLE; 795e277d2fcSYu Zhao if (!dev->is_virtfn) 796302b4215SYu Zhao ctrl |= PCI_ATS_CTRL_STU(ps - PCI_ATS_MIN_STU); 797302b4215SYu Zhao pci_write_config_word(dev, dev->ats->pos + PCI_ATS_CTRL, ctrl); 798302b4215SYu Zhao 799e277d2fcSYu Zhao dev->ats->is_enabled = 1; 800e277d2fcSYu Zhao 801302b4215SYu Zhao return 0; 802302b4215SYu Zhao } 803302b4215SYu Zhao 804302b4215SYu Zhao /** 805302b4215SYu Zhao * pci_disable_ats - disable the ATS capability 806302b4215SYu Zhao * @dev: the PCI device 807302b4215SYu Zhao */ 808302b4215SYu Zhao void pci_disable_ats(struct pci_dev *dev) 809302b4215SYu Zhao { 810302b4215SYu Zhao u16 ctrl; 811302b4215SYu Zhao 812e277d2fcSYu Zhao BUG_ON(!dev->ats || !dev->ats->is_enabled); 813302b4215SYu Zhao 814302b4215SYu Zhao pci_read_config_word(dev, dev->ats->pos + PCI_ATS_CTRL, &ctrl); 815302b4215SYu Zhao ctrl &= ~PCI_ATS_CTRL_ENABLE; 816302b4215SYu Zhao pci_write_config_word(dev, dev->ats->pos + PCI_ATS_CTRL, ctrl); 817302b4215SYu Zhao 818e277d2fcSYu Zhao dev->ats->is_enabled = 0; 819e277d2fcSYu Zhao 820e277d2fcSYu Zhao if (dev->is_physfn || dev->is_virtfn) { 821e277d2fcSYu Zhao struct pci_dev *pdev = dev->is_physfn ? dev : dev->physfn; 822e277d2fcSYu Zhao 823e277d2fcSYu Zhao mutex_lock(&pdev->sriov->lock); 824e277d2fcSYu Zhao pdev->ats->ref_cnt--; 825e277d2fcSYu Zhao if (!pdev->ats->ref_cnt) 826e277d2fcSYu Zhao ats_free_one(pdev); 827e277d2fcSYu Zhao mutex_unlock(&pdev->sriov->lock); 828e277d2fcSYu Zhao } 829e277d2fcSYu Zhao 830e277d2fcSYu Zhao if (!dev->is_physfn) 831302b4215SYu Zhao ats_free_one(dev); 832302b4215SYu Zhao } 833302b4215SYu Zhao 834302b4215SYu Zhao /** 835302b4215SYu Zhao * pci_ats_queue_depth - query the ATS Invalidate Queue Depth 836302b4215SYu Zhao * @dev: the PCI device 837302b4215SYu Zhao * 838302b4215SYu Zhao * Returns the queue depth on success, or negative on failure. 839302b4215SYu Zhao * 840302b4215SYu Zhao * The ATS spec uses 0 in the Invalidate Queue Depth field to 841302b4215SYu Zhao * indicate that the function can accept 32 Invalidate Request. 842302b4215SYu Zhao * But here we use the `real' values (i.e. 1~32) for the Queue 843e277d2fcSYu Zhao * Depth; and 0 indicates the function shares the Queue with 844e277d2fcSYu Zhao * other functions (doesn't exclusively own a Queue). 845302b4215SYu Zhao */ 846302b4215SYu Zhao int pci_ats_queue_depth(struct pci_dev *dev) 847302b4215SYu Zhao { 848302b4215SYu Zhao int pos; 849302b4215SYu Zhao u16 cap; 850302b4215SYu Zhao 851e277d2fcSYu Zhao if (dev->is_virtfn) 852e277d2fcSYu Zhao return 0; 853e277d2fcSYu Zhao 854302b4215SYu Zhao if (dev->ats) 855302b4215SYu Zhao return dev->ats->qdep; 856302b4215SYu Zhao 857302b4215SYu Zhao pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS); 858302b4215SYu Zhao if (!pos) 859302b4215SYu Zhao return -ENODEV; 860302b4215SYu Zhao 861302b4215SYu Zhao pci_read_config_word(dev, pos + PCI_ATS_CAP, &cap); 862302b4215SYu Zhao 863302b4215SYu Zhao return PCI_ATS_CAP_QDEP(cap) ? PCI_ATS_CAP_QDEP(cap) : 864302b4215SYu Zhao PCI_ATS_MAX_QDEP; 865302b4215SYu Zhao } 866