iov.c 18 KB

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