iov.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /*
  2. * drivers/pci/iov.c
  3. *
  4. * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
  5. *
  6. * PCI Express I/O Virtualization (IOV) support.
  7. * Single Root IOV 1.0
  8. * Address Translation Service 1.0
  9. */
  10. #include <linux/pci.h>
  11. #include <linux/slab.h>
  12. #include <linux/mutex.h>
  13. #include <linux/export.h>
  14. #include <linux/string.h>
  15. #include <linux/delay.h>
  16. #include <linux/pci-ats.h>
  17. #include "pci.h"
  18. #define VIRTFN_ID_LEN 16
  19. static inline u8 virtfn_bus(struct pci_dev *dev, int id)
  20. {
  21. return dev->bus->number + ((dev->devfn + dev->sriov->offset +
  22. dev->sriov->stride * id) >> 8);
  23. }
  24. static inline u8 virtfn_devfn(struct pci_dev *dev, int id)
  25. {
  26. return (dev->devfn + dev->sriov->offset +
  27. dev->sriov->stride * id) & 0xff;
  28. }
  29. static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
  30. {
  31. struct pci_bus *child;
  32. if (bus->number == busnr)
  33. return bus;
  34. child = pci_find_bus(pci_domain_nr(bus), busnr);
  35. if (child)
  36. return child;
  37. child = pci_add_new_bus(bus, NULL, busnr);
  38. if (!child)
  39. return NULL;
  40. pci_bus_insert_busn_res(child, busnr, busnr);
  41. return child;
  42. }
  43. static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus)
  44. {
  45. if (physbus != virtbus && list_empty(&virtbus->devices))
  46. pci_remove_bus(virtbus);
  47. }
  48. static int virtfn_add(struct pci_dev *dev, int id, int reset)
  49. {
  50. int i;
  51. int rc = -ENOMEM;
  52. u64 size;
  53. char buf[VIRTFN_ID_LEN];
  54. struct pci_dev *virtfn;
  55. struct resource *res;
  56. struct pci_sriov *iov = dev->sriov;
  57. struct pci_bus *bus;
  58. mutex_lock(&iov->dev->sriov->lock);
  59. bus = virtfn_add_bus(dev->bus, virtfn_bus(dev, id));
  60. if (!bus)
  61. goto failed;
  62. virtfn = pci_alloc_dev(bus);
  63. if (!virtfn)
  64. goto failed0;
  65. virtfn->devfn = virtfn_devfn(dev, id);
  66. virtfn->vendor = dev->vendor;
  67. pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_DID, &virtfn->device);
  68. pci_setup_device(virtfn);
  69. virtfn->dev.parent = dev->dev.parent;
  70. virtfn->physfn = pci_dev_get(dev);
  71. virtfn->is_virtfn = 1;
  72. virtfn->multifunction = 0;
  73. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  74. res = dev->resource + PCI_IOV_RESOURCES + i;
  75. if (!res->parent)
  76. continue;
  77. virtfn->resource[i].name = pci_name(virtfn);
  78. virtfn->resource[i].flags = res->flags;
  79. size = resource_size(res);
  80. do_div(size, iov->total_VFs);
  81. virtfn->resource[i].start = res->start + size * id;
  82. virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
  83. rc = request_resource(res, &virtfn->resource[i]);
  84. BUG_ON(rc);
  85. }
  86. if (reset)
  87. __pci_reset_function(virtfn);
  88. pci_device_add(virtfn, virtfn->bus);
  89. mutex_unlock(&iov->dev->sriov->lock);
  90. rc = pci_bus_add_device(virtfn);
  91. sprintf(buf, "virtfn%u", id);
  92. rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
  93. if (rc)
  94. goto failed1;
  95. rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
  96. if (rc)
  97. goto failed2;
  98. kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
  99. return 0;
  100. failed2:
  101. sysfs_remove_link(&dev->dev.kobj, buf);
  102. failed1:
  103. pci_dev_put(dev);
  104. mutex_lock(&iov->dev->sriov->lock);
  105. pci_stop_and_remove_bus_device(virtfn);
  106. failed0:
  107. virtfn_remove_bus(dev->bus, bus);
  108. failed:
  109. mutex_unlock(&iov->dev->sriov->lock);
  110. return rc;
  111. }
  112. static void virtfn_remove(struct pci_dev *dev, int id, int reset)
  113. {
  114. char buf[VIRTFN_ID_LEN];
  115. struct pci_dev *virtfn;
  116. struct pci_sriov *iov = dev->sriov;
  117. virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
  118. virtfn_bus(dev, id),
  119. virtfn_devfn(dev, id));
  120. if (!virtfn)
  121. return;
  122. if (reset) {
  123. device_release_driver(&virtfn->dev);
  124. __pci_reset_function(virtfn);
  125. }
  126. sprintf(buf, "virtfn%u", id);
  127. sysfs_remove_link(&dev->dev.kobj, buf);
  128. /*
  129. * pci_stop_dev() could have been called for this virtfn already,
  130. * so the directory for the virtfn may have been removed before.
  131. * Double check to avoid spurious sysfs warnings.
  132. */
  133. if (virtfn->dev.kobj.sd)
  134. sysfs_remove_link(&virtfn->dev.kobj, "physfn");
  135. mutex_lock(&iov->dev->sriov->lock);
  136. pci_stop_and_remove_bus_device(virtfn);
  137. virtfn_remove_bus(dev->bus, virtfn->bus);
  138. mutex_unlock(&iov->dev->sriov->lock);
  139. /* balance pci_get_domain_bus_and_slot() */
  140. pci_dev_put(virtfn);
  141. pci_dev_put(dev);
  142. }
  143. static int sriov_migration(struct pci_dev *dev)
  144. {
  145. u16 status;
  146. struct pci_sriov *iov = dev->sriov;
  147. if (!iov->num_VFs)
  148. return 0;
  149. if (!(iov->cap & PCI_SRIOV_CAP_VFM))
  150. return 0;
  151. pci_read_config_word(dev, iov->pos + PCI_SRIOV_STATUS, &status);
  152. if (!(status & PCI_SRIOV_STATUS_VFM))
  153. return 0;
  154. schedule_work(&iov->mtask);
  155. return 1;
  156. }
  157. static void sriov_migration_task(struct work_struct *work)
  158. {
  159. int i;
  160. u8 state;
  161. u16 status;
  162. struct pci_sriov *iov = container_of(work, struct pci_sriov, mtask);
  163. for (i = iov->initial_VFs; i < iov->num_VFs; i++) {
  164. state = readb(iov->mstate + i);
  165. if (state == PCI_SRIOV_VFM_MI) {
  166. writeb(PCI_SRIOV_VFM_AV, iov->mstate + i);
  167. state = readb(iov->mstate + i);
  168. if (state == PCI_SRIOV_VFM_AV)
  169. virtfn_add(iov->self, i, 1);
  170. } else if (state == PCI_SRIOV_VFM_MO) {
  171. virtfn_remove(iov->self, i, 1);
  172. writeb(PCI_SRIOV_VFM_UA, iov->mstate + i);
  173. state = readb(iov->mstate + i);
  174. if (state == PCI_SRIOV_VFM_AV)
  175. virtfn_add(iov->self, i, 0);
  176. }
  177. }
  178. pci_read_config_word(iov->self, iov->pos + PCI_SRIOV_STATUS, &status);
  179. status &= ~PCI_SRIOV_STATUS_VFM;
  180. pci_write_config_word(iov->self, iov->pos + PCI_SRIOV_STATUS, status);
  181. }
  182. static int sriov_enable_migration(struct pci_dev *dev, int nr_virtfn)
  183. {
  184. int bir;
  185. u32 table;
  186. resource_size_t pa;
  187. struct pci_sriov *iov = dev->sriov;
  188. if (nr_virtfn <= iov->initial_VFs)
  189. return 0;
  190. pci_read_config_dword(dev, iov->pos + PCI_SRIOV_VFM, &table);
  191. bir = PCI_SRIOV_VFM_BIR(table);
  192. if (bir > PCI_STD_RESOURCE_END)
  193. return -EIO;
  194. table = PCI_SRIOV_VFM_OFFSET(table);
  195. if (table + nr_virtfn > pci_resource_len(dev, bir))
  196. return -EIO;
  197. pa = pci_resource_start(dev, bir) + table;
  198. iov->mstate = ioremap(pa, nr_virtfn);
  199. if (!iov->mstate)
  200. return -ENOMEM;
  201. INIT_WORK(&iov->mtask, sriov_migration_task);
  202. iov->ctrl |= PCI_SRIOV_CTRL_VFM | PCI_SRIOV_CTRL_INTR;
  203. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  204. return 0;
  205. }
  206. static void sriov_disable_migration(struct pci_dev *dev)
  207. {
  208. struct pci_sriov *iov = dev->sriov;
  209. iov->ctrl &= ~(PCI_SRIOV_CTRL_VFM | PCI_SRIOV_CTRL_INTR);
  210. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  211. cancel_work_sync(&iov->mtask);
  212. iounmap(iov->mstate);
  213. }
  214. static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
  215. {
  216. int rc;
  217. int i, j;
  218. int nres;
  219. u16 offset, stride, initial;
  220. struct resource *res;
  221. struct pci_dev *pdev;
  222. struct pci_sriov *iov = dev->sriov;
  223. int bars = 0;
  224. if (!nr_virtfn)
  225. return 0;
  226. if (iov->num_VFs)
  227. return -EINVAL;
  228. pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
  229. if (initial > iov->total_VFs ||
  230. (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
  231. return -EIO;
  232. if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
  233. (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
  234. return -EINVAL;
  235. pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &offset);
  236. pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &stride);
  237. if (!offset || (nr_virtfn > 1 && !stride))
  238. return -EIO;
  239. nres = 0;
  240. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  241. bars |= (1 << (i + PCI_IOV_RESOURCES));
  242. res = dev->resource + PCI_IOV_RESOURCES + i;
  243. if (res->parent)
  244. nres++;
  245. }
  246. if (nres != iov->nres) {
  247. dev_err(&dev->dev, "not enough MMIO resources for SR-IOV\n");
  248. return -ENOMEM;
  249. }
  250. iov->offset = offset;
  251. iov->stride = stride;
  252. if (virtfn_bus(dev, nr_virtfn - 1) > dev->bus->busn_res.end) {
  253. dev_err(&dev->dev, "SR-IOV: bus number out of range\n");
  254. return -ENOMEM;
  255. }
  256. if (pci_enable_resources(dev, bars)) {
  257. dev_err(&dev->dev, "SR-IOV: IOV BARS not allocated\n");
  258. return -ENOMEM;
  259. }
  260. if (iov->link != dev->devfn) {
  261. pdev = pci_get_slot(dev->bus, iov->link);
  262. if (!pdev)
  263. return -ENODEV;
  264. if (!pdev->is_physfn) {
  265. pci_dev_put(pdev);
  266. return -ENOSYS;
  267. }
  268. rc = sysfs_create_link(&dev->dev.kobj,
  269. &pdev->dev.kobj, "dep_link");
  270. pci_dev_put(pdev);
  271. if (rc)
  272. return rc;
  273. }
  274. pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
  275. iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
  276. pci_cfg_access_lock(dev);
  277. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  278. msleep(100);
  279. pci_cfg_access_unlock(dev);
  280. iov->initial_VFs = initial;
  281. if (nr_virtfn < initial)
  282. initial = nr_virtfn;
  283. for (i = 0; i < initial; i++) {
  284. rc = virtfn_add(dev, i, 0);
  285. if (rc)
  286. goto failed;
  287. }
  288. if (iov->cap & PCI_SRIOV_CAP_VFM) {
  289. rc = sriov_enable_migration(dev, nr_virtfn);
  290. if (rc)
  291. goto failed;
  292. }
  293. kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
  294. iov->num_VFs = nr_virtfn;
  295. return 0;
  296. failed:
  297. for (j = 0; j < i; j++)
  298. virtfn_remove(dev, j, 0);
  299. iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
  300. pci_cfg_access_lock(dev);
  301. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  302. pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, 0);
  303. ssleep(1);
  304. pci_cfg_access_unlock(dev);
  305. if (iov->link != dev->devfn)
  306. sysfs_remove_link(&dev->dev.kobj, "dep_link");
  307. return rc;
  308. }
  309. static void sriov_disable(struct pci_dev *dev)
  310. {
  311. int i;
  312. struct pci_sriov *iov = dev->sriov;
  313. if (!iov->num_VFs)
  314. return;
  315. if (iov->cap & PCI_SRIOV_CAP_VFM)
  316. sriov_disable_migration(dev);
  317. for (i = 0; i < iov->num_VFs; i++)
  318. virtfn_remove(dev, i, 0);
  319. iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
  320. pci_cfg_access_lock(dev);
  321. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  322. ssleep(1);
  323. pci_cfg_access_unlock(dev);
  324. if (iov->link != dev->devfn)
  325. sysfs_remove_link(&dev->dev.kobj, "dep_link");
  326. iov->num_VFs = 0;
  327. pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, 0);
  328. }
  329. static int sriov_init(struct pci_dev *dev, int pos)
  330. {
  331. int i;
  332. int rc;
  333. int nres;
  334. u32 pgsz;
  335. u16 ctrl, total, offset, stride;
  336. struct pci_sriov *iov;
  337. struct resource *res;
  338. struct pci_dev *pdev;
  339. if (pci_pcie_type(dev) != PCI_EXP_TYPE_RC_END &&
  340. pci_pcie_type(dev) != PCI_EXP_TYPE_ENDPOINT)
  341. return -ENODEV;
  342. pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
  343. if (ctrl & PCI_SRIOV_CTRL_VFE) {
  344. pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
  345. ssleep(1);
  346. }
  347. pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
  348. if (!total)
  349. return 0;
  350. ctrl = 0;
  351. list_for_each_entry(pdev, &dev->bus->devices, bus_list)
  352. if (pdev->is_physfn)
  353. goto found;
  354. pdev = NULL;
  355. if (pci_ari_enabled(dev->bus))
  356. ctrl |= PCI_SRIOV_CTRL_ARI;
  357. found:
  358. pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
  359. pci_write_config_word(dev, pos + PCI_SRIOV_NUM_VF, 0);
  360. pci_read_config_word(dev, pos + PCI_SRIOV_VF_OFFSET, &offset);
  361. pci_read_config_word(dev, pos + PCI_SRIOV_VF_STRIDE, &stride);
  362. if (!offset || (total > 1 && !stride))
  363. return -EIO;
  364. pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
  365. i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
  366. pgsz &= ~((1 << i) - 1);
  367. if (!pgsz)
  368. return -EIO;
  369. pgsz &= ~(pgsz - 1);
  370. pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
  371. nres = 0;
  372. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  373. res = dev->resource + PCI_IOV_RESOURCES + i;
  374. i += __pci_read_base(dev, pci_bar_unknown, res,
  375. pos + PCI_SRIOV_BAR + i * 4);
  376. if (!res->flags)
  377. continue;
  378. if (resource_size(res) & (PAGE_SIZE - 1)) {
  379. rc = -EIO;
  380. goto failed;
  381. }
  382. res->end = res->start + resource_size(res) * total - 1;
  383. nres++;
  384. }
  385. iov = kzalloc(sizeof(*iov), GFP_KERNEL);
  386. if (!iov) {
  387. rc = -ENOMEM;
  388. goto failed;
  389. }
  390. iov->pos = pos;
  391. iov->nres = nres;
  392. iov->ctrl = ctrl;
  393. iov->total_VFs = total;
  394. iov->offset = offset;
  395. iov->stride = stride;
  396. iov->pgsz = pgsz;
  397. iov->self = dev;
  398. pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
  399. pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
  400. if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
  401. iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
  402. if (pdev)
  403. iov->dev = pci_dev_get(pdev);
  404. else
  405. iov->dev = dev;
  406. mutex_init(&iov->lock);
  407. dev->sriov = iov;
  408. dev->is_physfn = 1;
  409. return 0;
  410. failed:
  411. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  412. res = dev->resource + PCI_IOV_RESOURCES + i;
  413. res->flags = 0;
  414. }
  415. return rc;
  416. }
  417. static void sriov_release(struct pci_dev *dev)
  418. {
  419. BUG_ON(dev->sriov->num_VFs);
  420. if (dev != dev->sriov->dev)
  421. pci_dev_put(dev->sriov->dev);
  422. mutex_destroy(&dev->sriov->lock);
  423. kfree(dev->sriov);
  424. dev->sriov = NULL;
  425. }
  426. static void sriov_restore_state(struct pci_dev *dev)
  427. {
  428. int i;
  429. u16 ctrl;
  430. struct pci_sriov *iov = dev->sriov;
  431. pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
  432. if (ctrl & PCI_SRIOV_CTRL_VFE)
  433. return;
  434. for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
  435. pci_update_resource(dev, i);
  436. pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
  437. pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, iov->num_VFs);
  438. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  439. if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
  440. msleep(100);
  441. }
  442. /**
  443. * pci_iov_init - initialize the IOV capability
  444. * @dev: the PCI device
  445. *
  446. * Returns 0 on success, or negative on failure.
  447. */
  448. int pci_iov_init(struct pci_dev *dev)
  449. {
  450. int pos;
  451. if (!pci_is_pcie(dev))
  452. return -ENODEV;
  453. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
  454. if (pos)
  455. return sriov_init(dev, pos);
  456. return -ENODEV;
  457. }
  458. /**
  459. * pci_iov_release - release resources used by the IOV capability
  460. * @dev: the PCI device
  461. */
  462. void pci_iov_release(struct pci_dev *dev)
  463. {
  464. if (dev->is_physfn)
  465. sriov_release(dev);
  466. }
  467. /**
  468. * pci_iov_resource_bar - get position of the SR-IOV BAR
  469. * @dev: the PCI device
  470. * @resno: the resource number
  471. * @type: the BAR type to be filled in
  472. *
  473. * Returns position of the BAR encapsulated in the SR-IOV capability.
  474. */
  475. int pci_iov_resource_bar(struct pci_dev *dev, int resno,
  476. enum pci_bar_type *type)
  477. {
  478. if (resno < PCI_IOV_RESOURCES || resno > PCI_IOV_RESOURCE_END)
  479. return 0;
  480. BUG_ON(!dev->is_physfn);
  481. *type = pci_bar_unknown;
  482. return dev->sriov->pos + PCI_SRIOV_BAR +
  483. 4 * (resno - PCI_IOV_RESOURCES);
  484. }
  485. /**
  486. * pci_sriov_resource_alignment - get resource alignment for VF BAR
  487. * @dev: the PCI device
  488. * @resno: the resource number
  489. *
  490. * Returns the alignment of the VF BAR found in the SR-IOV capability.
  491. * This is not the same as the resource size which is defined as
  492. * the VF BAR size multiplied by the number of VFs. The alignment
  493. * is just the VF BAR size.
  494. */
  495. resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
  496. {
  497. struct resource tmp;
  498. enum pci_bar_type type;
  499. int reg = pci_iov_resource_bar(dev, resno, &type);
  500. if (!reg)
  501. return 0;
  502. __pci_read_base(dev, type, &tmp, reg);
  503. return resource_alignment(&tmp);
  504. }
  505. /**
  506. * pci_restore_iov_state - restore the state of the IOV capability
  507. * @dev: the PCI device
  508. */
  509. void pci_restore_iov_state(struct pci_dev *dev)
  510. {
  511. if (dev->is_physfn)
  512. sriov_restore_state(dev);
  513. }
  514. /**
  515. * pci_iov_bus_range - find bus range used by Virtual Function
  516. * @bus: the PCI bus
  517. *
  518. * Returns max number of buses (exclude current one) used by Virtual
  519. * Functions.
  520. */
  521. int pci_iov_bus_range(struct pci_bus *bus)
  522. {
  523. int max = 0;
  524. u8 busnr;
  525. struct pci_dev *dev;
  526. list_for_each_entry(dev, &bus->devices, bus_list) {
  527. if (!dev->is_physfn)
  528. continue;
  529. busnr = virtfn_bus(dev, dev->sriov->total_VFs - 1);
  530. if (busnr > max)
  531. max = busnr;
  532. }
  533. return max ? max - bus->number : 0;
  534. }
  535. /**
  536. * pci_enable_sriov - enable the SR-IOV capability
  537. * @dev: the PCI device
  538. * @nr_virtfn: number of virtual functions to enable
  539. *
  540. * Returns 0 on success, or negative on failure.
  541. */
  542. int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
  543. {
  544. might_sleep();
  545. if (!dev->is_physfn)
  546. return -ENOSYS;
  547. return sriov_enable(dev, nr_virtfn);
  548. }
  549. EXPORT_SYMBOL_GPL(pci_enable_sriov);
  550. /**
  551. * pci_disable_sriov - disable the SR-IOV capability
  552. * @dev: the PCI device
  553. */
  554. void pci_disable_sriov(struct pci_dev *dev)
  555. {
  556. might_sleep();
  557. if (!dev->is_physfn)
  558. return;
  559. sriov_disable(dev);
  560. }
  561. EXPORT_SYMBOL_GPL(pci_disable_sriov);
  562. /**
  563. * pci_sriov_migration - notify SR-IOV core of Virtual Function Migration
  564. * @dev: the PCI device
  565. *
  566. * Returns IRQ_HANDLED if the IRQ is handled, or IRQ_NONE if not.
  567. *
  568. * Physical Function driver is responsible to register IRQ handler using
  569. * VF Migration Interrupt Message Number, and call this function when the
  570. * interrupt is generated by the hardware.
  571. */
  572. irqreturn_t pci_sriov_migration(struct pci_dev *dev)
  573. {
  574. if (!dev->is_physfn)
  575. return IRQ_NONE;
  576. return sriov_migration(dev) ? IRQ_HANDLED : IRQ_NONE;
  577. }
  578. EXPORT_SYMBOL_GPL(pci_sriov_migration);
  579. /**
  580. * pci_num_vf - return number of VFs associated with a PF device_release_driver
  581. * @dev: the PCI device
  582. *
  583. * Returns number of VFs, or 0 if SR-IOV is not enabled.
  584. */
  585. int pci_num_vf(struct pci_dev *dev)
  586. {
  587. if (!dev->is_physfn)
  588. return 0;
  589. return dev->sriov->num_VFs;
  590. }
  591. EXPORT_SYMBOL_GPL(pci_num_vf);
  592. /**
  593. * pci_vfs_assigned - returns number of VFs are assigned to a guest
  594. * @dev: the PCI device
  595. *
  596. * Returns number of VFs belonging to this device that are assigned to a guest.
  597. * If device is not a physical function returns 0.
  598. */
  599. int pci_vfs_assigned(struct pci_dev *dev)
  600. {
  601. struct pci_dev *vfdev;
  602. unsigned int vfs_assigned = 0;
  603. unsigned short dev_id;
  604. /* only search if we are a PF */
  605. if (!dev->is_physfn)
  606. return 0;
  607. /*
  608. * determine the device ID for the VFs, the vendor ID will be the
  609. * same as the PF so there is no need to check for that one
  610. */
  611. pci_read_config_word(dev, dev->sriov->pos + PCI_SRIOV_VF_DID, &dev_id);
  612. /* loop through all the VFs to see if we own any that are assigned */
  613. vfdev = pci_get_device(dev->vendor, dev_id, NULL);
  614. while (vfdev) {
  615. /*
  616. * It is considered assigned if it is a virtual function with
  617. * our dev as the physical function and the assigned bit is set
  618. */
  619. if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
  620. (vfdev->dev_flags & PCI_DEV_FLAGS_ASSIGNED))
  621. vfs_assigned++;
  622. vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
  623. }
  624. return vfs_assigned;
  625. }
  626. EXPORT_SYMBOL_GPL(pci_vfs_assigned);
  627. /**
  628. * pci_sriov_set_totalvfs -- reduce the TotalVFs available
  629. * @dev: the PCI PF device
  630. * @numvfs: number that should be used for TotalVFs supported
  631. *
  632. * Should be called from PF driver's probe routine with
  633. * device's mutex held.
  634. *
  635. * Returns 0 if PF is an SRIOV-capable device and
  636. * value of numvfs valid. If not a PF return -ENOSYS;
  637. * if numvfs is invalid return -EINVAL;
  638. * if VFs already enabled, return -EBUSY.
  639. */
  640. int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
  641. {
  642. if (!dev->is_physfn)
  643. return -ENOSYS;
  644. if (numvfs > dev->sriov->total_VFs)
  645. return -EINVAL;
  646. /* Shouldn't change if VFs already enabled */
  647. if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
  648. return -EBUSY;
  649. else
  650. dev->sriov->driver_max_VFs = numvfs;
  651. return 0;
  652. }
  653. EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
  654. /**
  655. * pci_sriov_get_totalvfs -- get total VFs supported on this device
  656. * @dev: the PCI PF device
  657. *
  658. * For a PCIe device with SRIOV support, return the PCIe
  659. * SRIOV capability value of TotalVFs or the value of driver_max_VFs
  660. * if the driver reduced it. Otherwise 0.
  661. */
  662. int pci_sriov_get_totalvfs(struct pci_dev *dev)
  663. {
  664. if (!dev->is_physfn)
  665. return 0;
  666. if (dev->sriov->driver_max_VFs)
  667. return dev->sriov->driver_max_VFs;
  668. return dev->sriov->total_VFs;
  669. }
  670. EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);