iov.c 15 KB

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