iov.c 17 KB

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