iov.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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/string.h>
  14. #include <linux/delay.h>
  15. #include <linux/pci-ats.h>
  16. #include "pci.h"
  17. #define VIRTFN_ID_LEN 16
  18. static inline u8 virtfn_bus(struct pci_dev *dev, int id)
  19. {
  20. return dev->bus->number + ((dev->devfn + dev->sriov->offset +
  21. dev->sriov->stride * id) >> 8);
  22. }
  23. static inline u8 virtfn_devfn(struct pci_dev *dev, int id)
  24. {
  25. return (dev->devfn + dev->sriov->offset +
  26. dev->sriov->stride * id) & 0xff;
  27. }
  28. static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
  29. {
  30. int rc;
  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. child->subordinate = busnr;
  41. child->dev.parent = bus->bridge;
  42. rc = pci_bus_add_child(child);
  43. if (rc) {
  44. pci_remove_bus(child);
  45. return NULL;
  46. }
  47. return child;
  48. }
  49. static void virtfn_remove_bus(struct pci_bus *bus, int busnr)
  50. {
  51. struct pci_bus *child;
  52. if (bus->number == busnr)
  53. return;
  54. child = pci_find_bus(pci_domain_nr(bus), busnr);
  55. BUG_ON(!child);
  56. if (list_empty(&child->devices))
  57. pci_remove_bus(child);
  58. }
  59. static int virtfn_add(struct pci_dev *dev, int id, int reset)
  60. {
  61. int i;
  62. int rc;
  63. u64 size;
  64. char buf[VIRTFN_ID_LEN];
  65. struct pci_dev *virtfn;
  66. struct resource *res;
  67. struct pci_sriov *iov = dev->sriov;
  68. virtfn = alloc_pci_dev();
  69. if (!virtfn)
  70. return -ENOMEM;
  71. mutex_lock(&iov->dev->sriov->lock);
  72. virtfn->bus = virtfn_add_bus(dev->bus, virtfn_bus(dev, id));
  73. if (!virtfn->bus) {
  74. kfree(virtfn);
  75. mutex_unlock(&iov->dev->sriov->lock);
  76. return -ENOMEM;
  77. }
  78. virtfn->devfn = virtfn_devfn(dev, id);
  79. virtfn->vendor = dev->vendor;
  80. pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_DID, &virtfn->device);
  81. pci_setup_device(virtfn);
  82. virtfn->dev.parent = dev->dev.parent;
  83. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  84. res = dev->resource + PCI_IOV_RESOURCES + i;
  85. if (!res->parent)
  86. continue;
  87. virtfn->resource[i].name = pci_name(virtfn);
  88. virtfn->resource[i].flags = res->flags;
  89. size = resource_size(res);
  90. do_div(size, iov->total);
  91. virtfn->resource[i].start = res->start + size * id;
  92. virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
  93. rc = request_resource(res, &virtfn->resource[i]);
  94. BUG_ON(rc);
  95. }
  96. if (reset)
  97. __pci_reset_function(virtfn);
  98. pci_device_add(virtfn, virtfn->bus);
  99. mutex_unlock(&iov->dev->sriov->lock);
  100. virtfn->physfn = pci_dev_get(dev);
  101. virtfn->is_virtfn = 1;
  102. rc = pci_bus_add_device(virtfn);
  103. if (rc)
  104. goto failed1;
  105. sprintf(buf, "virtfn%u", id);
  106. rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
  107. if (rc)
  108. goto failed1;
  109. rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
  110. if (rc)
  111. goto failed2;
  112. kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
  113. return 0;
  114. failed2:
  115. sysfs_remove_link(&dev->dev.kobj, buf);
  116. failed1:
  117. pci_dev_put(dev);
  118. mutex_lock(&iov->dev->sriov->lock);
  119. pci_remove_bus_device(virtfn);
  120. virtfn_remove_bus(dev->bus, virtfn_bus(dev, id));
  121. mutex_unlock(&iov->dev->sriov->lock);
  122. return rc;
  123. }
  124. static void virtfn_remove(struct pci_dev *dev, int id, int reset)
  125. {
  126. char buf[VIRTFN_ID_LEN];
  127. struct pci_bus *bus;
  128. struct pci_dev *virtfn;
  129. struct pci_sriov *iov = dev->sriov;
  130. bus = pci_find_bus(pci_domain_nr(dev->bus), virtfn_bus(dev, id));
  131. if (!bus)
  132. return;
  133. virtfn = pci_get_slot(bus, virtfn_devfn(dev, id));
  134. if (!virtfn)
  135. return;
  136. pci_dev_put(virtfn);
  137. if (reset) {
  138. device_release_driver(&virtfn->dev);
  139. __pci_reset_function(virtfn);
  140. }
  141. sprintf(buf, "virtfn%u", id);
  142. sysfs_remove_link(&dev->dev.kobj, buf);
  143. sysfs_remove_link(&virtfn->dev.kobj, "physfn");
  144. mutex_lock(&iov->dev->sriov->lock);
  145. pci_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->nr_virtfn)
  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; i < iov->nr_virtfn; 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)
  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. if (!nr_virtfn)
  231. return 0;
  232. if (iov->nr_virtfn)
  233. return -EINVAL;
  234. pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
  235. if (initial > iov->total ||
  236. (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total)))
  237. return -EIO;
  238. if (nr_virtfn < 0 || nr_virtfn > iov->total ||
  239. (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
  240. return -EINVAL;
  241. pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
  242. pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &offset);
  243. pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &stride);
  244. if (!offset || (nr_virtfn > 1 && !stride))
  245. return -EIO;
  246. nres = 0;
  247. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  248. res = dev->resource + PCI_IOV_RESOURCES + i;
  249. if (res->parent)
  250. nres++;
  251. }
  252. if (nres != iov->nres) {
  253. dev_err(&dev->dev, "not enough MMIO resources for SR-IOV\n");
  254. return -ENOMEM;
  255. }
  256. iov->offset = offset;
  257. iov->stride = stride;
  258. if (virtfn_bus(dev, nr_virtfn - 1) > dev->bus->subordinate) {
  259. dev_err(&dev->dev, "SR-IOV: bus number out of range\n");
  260. return -ENOMEM;
  261. }
  262. if (iov->link != dev->devfn) {
  263. pdev = pci_get_slot(dev->bus, iov->link);
  264. if (!pdev)
  265. return -ENODEV;
  266. pci_dev_put(pdev);
  267. if (!pdev->is_physfn)
  268. return -ENODEV;
  269. rc = sysfs_create_link(&dev->dev.kobj,
  270. &pdev->dev.kobj, "dep_link");
  271. if (rc)
  272. return rc;
  273. }
  274. iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
  275. pci_block_user_cfg_access(dev);
  276. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  277. msleep(100);
  278. pci_unblock_user_cfg_access(dev);
  279. iov->initial = initial;
  280. if (nr_virtfn < initial)
  281. initial = nr_virtfn;
  282. for (i = 0; i < initial; i++) {
  283. rc = virtfn_add(dev, i, 0);
  284. if (rc)
  285. goto failed;
  286. }
  287. if (iov->cap & PCI_SRIOV_CAP_VFM) {
  288. rc = sriov_enable_migration(dev, nr_virtfn);
  289. if (rc)
  290. goto failed;
  291. }
  292. kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
  293. iov->nr_virtfn = nr_virtfn;
  294. return 0;
  295. failed:
  296. for (j = 0; j < i; j++)
  297. virtfn_remove(dev, j, 0);
  298. iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
  299. pci_block_user_cfg_access(dev);
  300. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  301. ssleep(1);
  302. pci_unblock_user_cfg_access(dev);
  303. if (iov->link != dev->devfn)
  304. sysfs_remove_link(&dev->dev.kobj, "dep_link");
  305. return rc;
  306. }
  307. static void sriov_disable(struct pci_dev *dev)
  308. {
  309. int i;
  310. struct pci_sriov *iov = dev->sriov;
  311. if (!iov->nr_virtfn)
  312. return;
  313. if (iov->cap & PCI_SRIOV_CAP_VFM)
  314. sriov_disable_migration(dev);
  315. for (i = 0; i < iov->nr_virtfn; i++)
  316. virtfn_remove(dev, i, 0);
  317. iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
  318. pci_block_user_cfg_access(dev);
  319. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  320. ssleep(1);
  321. pci_unblock_user_cfg_access(dev);
  322. if (iov->link != dev->devfn)
  323. sysfs_remove_link(&dev->dev.kobj, "dep_link");
  324. iov->nr_virtfn = 0;
  325. }
  326. static int sriov_init(struct pci_dev *dev, int pos)
  327. {
  328. int i;
  329. int rc;
  330. int nres;
  331. u32 pgsz;
  332. u16 ctrl, total, offset, stride;
  333. struct pci_sriov *iov;
  334. struct resource *res;
  335. struct pci_dev *pdev;
  336. if (dev->pcie_type != PCI_EXP_TYPE_RC_END &&
  337. dev->pcie_type != PCI_EXP_TYPE_ENDPOINT)
  338. return -ENODEV;
  339. pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
  340. if (ctrl & PCI_SRIOV_CTRL_VFE) {
  341. pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
  342. ssleep(1);
  343. }
  344. pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
  345. if (!total)
  346. return 0;
  347. ctrl = 0;
  348. list_for_each_entry(pdev, &dev->bus->devices, bus_list)
  349. if (pdev->is_physfn)
  350. goto found;
  351. pdev = NULL;
  352. if (pci_ari_enabled(dev->bus))
  353. ctrl |= PCI_SRIOV_CTRL_ARI;
  354. found:
  355. pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
  356. pci_write_config_word(dev, pos + PCI_SRIOV_NUM_VF, total);
  357. pci_read_config_word(dev, pos + PCI_SRIOV_VF_OFFSET, &offset);
  358. pci_read_config_word(dev, pos + PCI_SRIOV_VF_STRIDE, &stride);
  359. if (!offset || (total > 1 && !stride))
  360. return -EIO;
  361. pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
  362. i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
  363. pgsz &= ~((1 << i) - 1);
  364. if (!pgsz)
  365. return -EIO;
  366. pgsz &= ~(pgsz - 1);
  367. pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
  368. nres = 0;
  369. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  370. res = dev->resource + PCI_IOV_RESOURCES + i;
  371. i += __pci_read_base(dev, pci_bar_unknown, res,
  372. pos + PCI_SRIOV_BAR + i * 4);
  373. if (!res->flags)
  374. continue;
  375. if (resource_size(res) & (PAGE_SIZE - 1)) {
  376. rc = -EIO;
  377. goto failed;
  378. }
  379. res->end = res->start + resource_size(res) * total - 1;
  380. nres++;
  381. }
  382. iov = kzalloc(sizeof(*iov), GFP_KERNEL);
  383. if (!iov) {
  384. rc = -ENOMEM;
  385. goto failed;
  386. }
  387. iov->pos = pos;
  388. iov->nres = nres;
  389. iov->ctrl = ctrl;
  390. iov->total = total;
  391. iov->offset = offset;
  392. iov->stride = stride;
  393. iov->pgsz = pgsz;
  394. iov->self = dev;
  395. pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
  396. pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
  397. if (dev->pcie_type == PCI_EXP_TYPE_RC_END)
  398. iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
  399. if (pdev)
  400. iov->dev = pci_dev_get(pdev);
  401. else
  402. iov->dev = dev;
  403. mutex_init(&iov->lock);
  404. dev->sriov = iov;
  405. dev->is_physfn = 1;
  406. return 0;
  407. failed:
  408. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  409. res = dev->resource + PCI_IOV_RESOURCES + i;
  410. res->flags = 0;
  411. }
  412. return rc;
  413. }
  414. static void sriov_release(struct pci_dev *dev)
  415. {
  416. BUG_ON(dev->sriov->nr_virtfn);
  417. if (dev != dev->sriov->dev)
  418. pci_dev_put(dev->sriov->dev);
  419. mutex_destroy(&dev->sriov->lock);
  420. kfree(dev->sriov);
  421. dev->sriov = NULL;
  422. }
  423. static void sriov_restore_state(struct pci_dev *dev)
  424. {
  425. int i;
  426. u16 ctrl;
  427. struct pci_sriov *iov = dev->sriov;
  428. pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
  429. if (ctrl & PCI_SRIOV_CTRL_VFE)
  430. return;
  431. for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
  432. pci_update_resource(dev, i);
  433. pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
  434. pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, iov->nr_virtfn);
  435. pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
  436. if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
  437. msleep(100);
  438. }
  439. /**
  440. * pci_iov_init - initialize the IOV capability
  441. * @dev: the PCI device
  442. *
  443. * Returns 0 on success, or negative on failure.
  444. */
  445. int pci_iov_init(struct pci_dev *dev)
  446. {
  447. int pos;
  448. if (!pci_is_pcie(dev))
  449. return -ENODEV;
  450. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
  451. if (pos)
  452. return sriov_init(dev, pos);
  453. return -ENODEV;
  454. }
  455. /**
  456. * pci_iov_release - release resources used by the IOV capability
  457. * @dev: the PCI device
  458. */
  459. void pci_iov_release(struct pci_dev *dev)
  460. {
  461. if (dev->is_physfn)
  462. sriov_release(dev);
  463. }
  464. /**
  465. * pci_iov_resource_bar - get position of the SR-IOV BAR
  466. * @dev: the PCI device
  467. * @resno: the resource number
  468. * @type: the BAR type to be filled in
  469. *
  470. * Returns position of the BAR encapsulated in the SR-IOV capability.
  471. */
  472. int pci_iov_resource_bar(struct pci_dev *dev, int resno,
  473. enum pci_bar_type *type)
  474. {
  475. if (resno < PCI_IOV_RESOURCES || resno > PCI_IOV_RESOURCE_END)
  476. return 0;
  477. BUG_ON(!dev->is_physfn);
  478. *type = pci_bar_unknown;
  479. return dev->sriov->pos + PCI_SRIOV_BAR +
  480. 4 * (resno - PCI_IOV_RESOURCES);
  481. }
  482. /**
  483. * pci_sriov_resource_alignment - get resource alignment for VF BAR
  484. * @dev: the PCI device
  485. * @resno: the resource number
  486. *
  487. * Returns the alignment of the VF BAR found in the SR-IOV capability.
  488. * This is not the same as the resource size which is defined as
  489. * the VF BAR size multiplied by the number of VFs. The alignment
  490. * is just the VF BAR size.
  491. */
  492. resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
  493. {
  494. struct resource tmp;
  495. enum pci_bar_type type;
  496. int reg = pci_iov_resource_bar(dev, resno, &type);
  497. if (!reg)
  498. return 0;
  499. __pci_read_base(dev, type, &tmp, reg);
  500. return resource_alignment(&tmp);
  501. }
  502. /**
  503. * pci_restore_iov_state - restore the state of the IOV capability
  504. * @dev: the PCI device
  505. */
  506. void pci_restore_iov_state(struct pci_dev *dev)
  507. {
  508. if (dev->is_physfn)
  509. sriov_restore_state(dev);
  510. }
  511. /**
  512. * pci_iov_bus_range - find bus range used by Virtual Function
  513. * @bus: the PCI bus
  514. *
  515. * Returns max number of buses (exclude current one) used by Virtual
  516. * Functions.
  517. */
  518. int pci_iov_bus_range(struct pci_bus *bus)
  519. {
  520. int max = 0;
  521. u8 busnr;
  522. struct pci_dev *dev;
  523. list_for_each_entry(dev, &bus->devices, bus_list) {
  524. if (!dev->is_physfn)
  525. continue;
  526. busnr = virtfn_bus(dev, dev->sriov->total - 1);
  527. if (busnr > max)
  528. max = busnr;
  529. }
  530. return max ? max - bus->number : 0;
  531. }
  532. /**
  533. * pci_enable_sriov - enable the SR-IOV capability
  534. * @dev: the PCI device
  535. * @nr_virtfn: number of virtual functions to enable
  536. *
  537. * Returns 0 on success, or negative on failure.
  538. */
  539. int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
  540. {
  541. might_sleep();
  542. if (!dev->is_physfn)
  543. return -ENODEV;
  544. return sriov_enable(dev, nr_virtfn);
  545. }
  546. EXPORT_SYMBOL_GPL(pci_enable_sriov);
  547. /**
  548. * pci_disable_sriov - disable the SR-IOV capability
  549. * @dev: the PCI device
  550. */
  551. void pci_disable_sriov(struct pci_dev *dev)
  552. {
  553. might_sleep();
  554. if (!dev->is_physfn)
  555. return;
  556. sriov_disable(dev);
  557. }
  558. EXPORT_SYMBOL_GPL(pci_disable_sriov);
  559. /**
  560. * pci_sriov_migration - notify SR-IOV core of Virtual Function Migration
  561. * @dev: the PCI device
  562. *
  563. * Returns IRQ_HANDLED if the IRQ is handled, or IRQ_NONE if not.
  564. *
  565. * Physical Function driver is responsible to register IRQ handler using
  566. * VF Migration Interrupt Message Number, and call this function when the
  567. * interrupt is generated by the hardware.
  568. */
  569. irqreturn_t pci_sriov_migration(struct pci_dev *dev)
  570. {
  571. if (!dev->is_physfn)
  572. return IRQ_NONE;
  573. return sriov_migration(dev) ? IRQ_HANDLED : IRQ_NONE;
  574. }
  575. EXPORT_SYMBOL_GPL(pci_sriov_migration);
  576. /**
  577. * pci_num_vf - return number of VFs associated with a PF device_release_driver
  578. * @dev: the PCI device
  579. *
  580. * Returns number of VFs, or 0 if SR-IOV is not enabled.
  581. */
  582. int pci_num_vf(struct pci_dev *dev)
  583. {
  584. if (!dev || !dev->is_physfn)
  585. return 0;
  586. else
  587. return dev->sriov->nr_virtfn;
  588. }
  589. EXPORT_SYMBOL_GPL(pci_num_vf);
  590. static int ats_alloc_one(struct pci_dev *dev, int ps)
  591. {
  592. int pos;
  593. u16 cap;
  594. struct pci_ats *ats;
  595. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS);
  596. if (!pos)
  597. return -ENODEV;
  598. ats = kzalloc(sizeof(*ats), GFP_KERNEL);
  599. if (!ats)
  600. return -ENOMEM;
  601. ats->pos = pos;
  602. ats->stu = ps;
  603. pci_read_config_word(dev, pos + PCI_ATS_CAP, &cap);
  604. ats->qdep = PCI_ATS_CAP_QDEP(cap) ? PCI_ATS_CAP_QDEP(cap) :
  605. PCI_ATS_MAX_QDEP;
  606. dev->ats = ats;
  607. return 0;
  608. }
  609. static void ats_free_one(struct pci_dev *dev)
  610. {
  611. kfree(dev->ats);
  612. dev->ats = NULL;
  613. }
  614. /**
  615. * pci_enable_ats - enable the ATS capability
  616. * @dev: the PCI device
  617. * @ps: the IOMMU page shift
  618. *
  619. * Returns 0 on success, or negative on failure.
  620. */
  621. int pci_enable_ats(struct pci_dev *dev, int ps)
  622. {
  623. int rc;
  624. u16 ctrl;
  625. BUG_ON(dev->ats && dev->ats->is_enabled);
  626. if (ps < PCI_ATS_MIN_STU)
  627. return -EINVAL;
  628. if (dev->is_physfn || dev->is_virtfn) {
  629. struct pci_dev *pdev = dev->is_physfn ? dev : dev->physfn;
  630. mutex_lock(&pdev->sriov->lock);
  631. if (pdev->ats)
  632. rc = pdev->ats->stu == ps ? 0 : -EINVAL;
  633. else
  634. rc = ats_alloc_one(pdev, ps);
  635. if (!rc)
  636. pdev->ats->ref_cnt++;
  637. mutex_unlock(&pdev->sriov->lock);
  638. if (rc)
  639. return rc;
  640. }
  641. if (!dev->is_physfn) {
  642. rc = ats_alloc_one(dev, ps);
  643. if (rc)
  644. return rc;
  645. }
  646. ctrl = PCI_ATS_CTRL_ENABLE;
  647. if (!dev->is_virtfn)
  648. ctrl |= PCI_ATS_CTRL_STU(ps - PCI_ATS_MIN_STU);
  649. pci_write_config_word(dev, dev->ats->pos + PCI_ATS_CTRL, ctrl);
  650. dev->ats->is_enabled = 1;
  651. return 0;
  652. }
  653. /**
  654. * pci_disable_ats - disable the ATS capability
  655. * @dev: the PCI device
  656. */
  657. void pci_disable_ats(struct pci_dev *dev)
  658. {
  659. u16 ctrl;
  660. BUG_ON(!dev->ats || !dev->ats->is_enabled);
  661. pci_read_config_word(dev, dev->ats->pos + PCI_ATS_CTRL, &ctrl);
  662. ctrl &= ~PCI_ATS_CTRL_ENABLE;
  663. pci_write_config_word(dev, dev->ats->pos + PCI_ATS_CTRL, ctrl);
  664. dev->ats->is_enabled = 0;
  665. if (dev->is_physfn || dev->is_virtfn) {
  666. struct pci_dev *pdev = dev->is_physfn ? dev : dev->physfn;
  667. mutex_lock(&pdev->sriov->lock);
  668. pdev->ats->ref_cnt--;
  669. if (!pdev->ats->ref_cnt)
  670. ats_free_one(pdev);
  671. mutex_unlock(&pdev->sriov->lock);
  672. }
  673. if (!dev->is_physfn)
  674. ats_free_one(dev);
  675. }
  676. /**
  677. * pci_ats_queue_depth - query the ATS Invalidate Queue Depth
  678. * @dev: the PCI device
  679. *
  680. * Returns the queue depth on success, or negative on failure.
  681. *
  682. * The ATS spec uses 0 in the Invalidate Queue Depth field to
  683. * indicate that the function can accept 32 Invalidate Request.
  684. * But here we use the `real' values (i.e. 1~32) for the Queue
  685. * Depth; and 0 indicates the function shares the Queue with
  686. * other functions (doesn't exclusively own a Queue).
  687. */
  688. int pci_ats_queue_depth(struct pci_dev *dev)
  689. {
  690. int pos;
  691. u16 cap;
  692. if (dev->is_virtfn)
  693. return 0;
  694. if (dev->ats)
  695. return dev->ats->qdep;
  696. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS);
  697. if (!pos)
  698. return -ENODEV;
  699. pci_read_config_word(dev, pos + PCI_ATS_CAP, &cap);
  700. return PCI_ATS_CAP_QDEP(cap) ? PCI_ATS_CAP_QDEP(cap) :
  701. PCI_ATS_MAX_QDEP;
  702. }