pci-sriov.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include <linux/kernel.h>
  3. #include <linux/ioport.h>
  4. #include <linux/bitmap.h>
  5. #include <linux/pci.h>
  6. #include <asm/opal.h>
  7. #include "pci.h"
  8. /*
  9. * The majority of the complexity in supporting SR-IOV on PowerNV comes from
  10. * the need to put the MMIO space for each VF into a separate PE. Internally
  11. * the PHB maps MMIO addresses to a specific PE using the "Memory BAR Table".
  12. * The MBT historically only applied to the 64bit MMIO window of the PHB
  13. * so it's common to see it referred to as the "M64BT".
  14. *
  15. * An MBT entry stores the mapped range as an <base>,<mask> pair. This forces
  16. * the address range that we want to map to be power-of-two sized and aligned.
  17. * For conventional PCI devices this isn't really an issue since PCI device BARs
  18. * have the same requirement.
  19. *
  20. * For a SR-IOV BAR things are a little more awkward since size and alignment
  21. * are not coupled. The alignment is set based on the per-VF BAR size, but
  22. * the total BAR area is: number-of-vfs * per-vf-size. The number of VFs
  23. * isn't necessarily a power of two, so neither is the total size. To fix that
  24. * we need to finesse (read: hack) the Linux BAR allocator so that it will
  25. * allocate the SR-IOV BARs in a way that lets us map them using the MBT.
  26. *
  27. * The changes to size and alignment that we need to do depend on the "mode"
  28. * of MBT entry that we use. We only support SR-IOV on PHB3 (IODA2) and above,
  29. * so as a baseline we can assume that we have the following BAR modes
  30. * available:
  31. *
  32. * NB: $PE_COUNT is the number of PEs that the PHB supports.
  33. *
  34. * a) A segmented BAR that splits the mapped range into $PE_COUNT equally sized
  35. * segments. The n'th segment is mapped to the n'th PE.
  36. * b) An un-segmented BAR that maps the whole address range to a specific PE.
  37. *
  38. *
  39. * We prefer to use mode a) since it only requires one MBT entry per SR-IOV BAR
  40. * For comparison b) requires one entry per-VF per-BAR, or:
  41. * (num-vfs * num-sriov-bars) in total. To use a) we need the size of each segment
  42. * to equal the size of the per-VF BAR area. So:
  43. *
  44. * new_size = per-vf-size * number-of-PEs
  45. *
  46. * The alignment for the SR-IOV BAR also needs to be changed from per-vf-size
  47. * to "new_size", calculated above. Implementing this is a convoluted process
  48. * which requires several hooks in the PCI core:
  49. *
  50. * 1. In pcibios_device_add() we call pnv_pci_ioda_fixup_iov().
  51. *
  52. * At this point the device has been probed and the device's BARs are sized,
  53. * but no resource allocations have been done. The SR-IOV BARs are sized
  54. * based on the maximum number of VFs supported by the device and we need
  55. * to increase that to new_size.
  56. *
  57. * 2. Later, when Linux actually assigns resources it tries to make the resource
  58. * allocations for each PCI bus as compact as possible. As a part of that it
  59. * sorts the BARs on a bus by their required alignment, which is calculated
  60. * using pci_resource_alignment().
  61. *
  62. * For IOV resources this goes:
  63. * pci_resource_alignment()
  64. * pci_sriov_resource_alignment()
  65. * pcibios_sriov_resource_alignment()
  66. * pnv_pci_iov_resource_alignment()
  67. *
  68. * Our hook overrides the default alignment, equal to the per-vf-size, with
  69. * new_size computed above.
  70. *
  71. * 3. When userspace enables VFs for a device:
  72. *
  73. * sriov_enable()
  74. * pcibios_sriov_enable()
  75. * pnv_pcibios_sriov_enable()
  76. *
  77. * This is where we actually allocate PE numbers for each VF and setup the
  78. * MBT mapping for each SR-IOV BAR. In steps 1) and 2) we setup an "arena"
  79. * where each MBT segment is equal in size to the VF BAR so we can shift
  80. * around the actual SR-IOV BAR location within this arena. We need this
  81. * ability because the PE space is shared by all devices on the same PHB.
  82. * When using mode a) described above segment 0 in maps to PE#0 which might
  83. * be already being used by another device on the PHB.
  84. *
  85. * As a result we need allocate a contigious range of PE numbers, then shift
  86. * the address programmed into the SR-IOV BAR of the PF so that the address
  87. * of VF0 matches up with the segment corresponding to the first allocated
  88. * PE number. This is handled in pnv_pci_vf_resource_shift().
  89. *
  90. * Once all that is done we return to the PCI core which then enables VFs,
  91. * scans them and creates pci_devs for each. The init process for a VF is
  92. * largely the same as a normal device, but the VF is inserted into the IODA
  93. * PE that we allocated for it rather than the PE associated with the bus.
  94. *
  95. * 4. When userspace disables VFs we unwind the above in
  96. * pnv_pcibios_sriov_disable(). Fortunately this is relatively simple since
  97. * we don't need to validate anything, just tear down the mappings and
  98. * move SR-IOV resource back to its "proper" location.
  99. *
  100. * That's how mode a) works. In theory mode b) (single PE mapping) is less work
  101. * since we can map each individual VF with a separate BAR. However, there's a
  102. * few limitations:
  103. *
  104. * 1) For IODA2 mode b) has a minimum alignment requirement of 32MB. This makes
  105. * it only usable for devices with very large per-VF BARs. Such devices are
  106. * similar to Big Foot. They definitely exist, but I've never seen one.
  107. *
  108. * 2) The number of MBT entries that we have is limited. PHB3 and PHB4 only
  109. * 16 total and some are needed for. Most SR-IOV capable network cards can support
  110. * more than 16 VFs on each port.
  111. *
  112. * We use b) when using a) would use more than 1/4 of the entire 64 bit MMIO
  113. * window of the PHB.
  114. *
  115. *
  116. *
  117. * PHB4 (IODA3) added a few new features that would be useful for SR-IOV. It
  118. * allowed the MBT to map 32bit MMIO space in addition to 64bit which allows
  119. * us to support SR-IOV BARs in the 32bit MMIO window. This is useful since
  120. * the Linux BAR allocation will place any BAR marked as non-prefetchable into
  121. * the non-prefetchable bridge window, which is 32bit only. It also added two
  122. * new modes:
  123. *
  124. * c) A segmented BAR similar to a), but each segment can be individually
  125. * mapped to any PE. This is matches how the 32bit MMIO window worked on
  126. * IODA1&2.
  127. *
  128. * d) A segmented BAR with 8, 64, or 128 segments. This works similarly to a),
  129. * but with fewer segments and configurable base PE.
  130. *
  131. * i.e. The n'th segment maps to the (n + base)'th PE.
  132. *
  133. * The base PE is also required to be a multiple of the window size.
  134. *
  135. * Unfortunately, the OPAL API doesn't currently (as of skiboot v6.6) allow us
  136. * to exploit any of the IODA3 features.
  137. */
  138. static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
  139. {
  140. struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
  141. struct resource *res;
  142. int i;
  143. resource_size_t vf_bar_sz;
  144. struct pnv_iov_data *iov;
  145. int mul;
  146. iov = kzalloc(sizeof(*iov), GFP_KERNEL);
  147. if (!iov)
  148. goto disable_iov;
  149. pdev->dev.archdata.iov_data = iov;
  150. mul = phb->ioda.total_pe_num;
  151. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  152. res = &pdev->resource[i + PCI_IOV_RESOURCES];
  153. if (!res->flags || res->parent)
  154. continue;
  155. if (!pnv_pci_is_m64_flags(res->flags)) {
  156. dev_warn(&pdev->dev, "Don't support SR-IOV with non M64 VF BAR%d: %pR. \n",
  157. i, res);
  158. goto disable_iov;
  159. }
  160. vf_bar_sz = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
  161. /*
  162. * Generally, one segmented M64 BAR maps one IOV BAR. However,
  163. * if a VF BAR is too large we end up wasting a lot of space.
  164. * If each VF needs more than 1/4 of the default m64 segment
  165. * then each VF BAR should be mapped in single-PE mode to reduce
  166. * the amount of space required. This does however limit the
  167. * number of VFs we can support.
  168. *
  169. * The 1/4 limit is arbitrary and can be tweaked.
  170. */
  171. if (vf_bar_sz > (phb->ioda.m64_segsize >> 2)) {
  172. /*
  173. * On PHB3, the minimum size alignment of M64 BAR in
  174. * single mode is 32MB. If this VF BAR is smaller than
  175. * 32MB, but still too large for a segmented window
  176. * then we can't map it and need to disable SR-IOV for
  177. * this device.
  178. */
  179. if (vf_bar_sz < SZ_32M) {
  180. pci_err(pdev, "VF BAR%d: %pR can't be mapped in single PE mode\n",
  181. i, res);
  182. goto disable_iov;
  183. }
  184. iov->m64_single_mode[i] = true;
  185. continue;
  186. }
  187. /*
  188. * This BAR can be mapped with one segmented window, so adjust
  189. * te resource size to accommodate.
  190. */
  191. pci_dbg(pdev, " Fixing VF BAR%d: %pR to\n", i, res);
  192. res->end = res->start + vf_bar_sz * mul - 1;
  193. pci_dbg(pdev, " %pR\n", res);
  194. pci_info(pdev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)",
  195. i, res, mul);
  196. iov->need_shift = true;
  197. }
  198. return;
  199. disable_iov:
  200. /* Save ourselves some MMIO space by disabling the unusable BARs */
  201. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  202. res = &pdev->resource[i + PCI_IOV_RESOURCES];
  203. res->flags = 0;
  204. res->end = res->start - 1;
  205. }
  206. pdev->dev.archdata.iov_data = NULL;
  207. kfree(iov);
  208. }
  209. void pnv_pci_ioda_fixup_iov(struct pci_dev *pdev)
  210. {
  211. if (pdev->is_virtfn) {
  212. struct pnv_ioda_pe *pe = pnv_ioda_get_pe(pdev);
  213. /*
  214. * VF PEs are single-device PEs so their pdev pointer needs to
  215. * be set. The pdev doesn't exist when the PE is allocated (in
  216. * (pcibios_sriov_enable()) so we fix it up here.
  217. */
  218. pe->pdev = pdev;
  219. WARN_ON(!(pe->flags & PNV_IODA_PE_VF));
  220. } else if (pdev->is_physfn) {
  221. /*
  222. * For PFs adjust their allocated IOV resources to match what
  223. * the PHB can support using it's M64 BAR table.
  224. */
  225. pnv_pci_ioda_fixup_iov_resources(pdev);
  226. }
  227. }
  228. resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev,
  229. int resno)
  230. {
  231. resource_size_t align = pci_iov_resource_size(pdev, resno);
  232. struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
  233. struct pnv_iov_data *iov = pnv_iov_get(pdev);
  234. /*
  235. * iov can be null if we have an SR-IOV device with IOV BAR that can't
  236. * be placed in the m64 space (i.e. The BAR is 32bit or non-prefetch).
  237. * In that case we don't allow VFs to be enabled since one of their
  238. * BARs would not be placed in the correct PE.
  239. */
  240. if (!iov)
  241. return align;
  242. /*
  243. * If we're using single mode then we can just use the native VF BAR
  244. * alignment. We validated that it's possible to use a single PE
  245. * window above when we did the fixup.
  246. */
  247. if (iov->m64_single_mode[resno - PCI_IOV_RESOURCES])
  248. return align;
  249. /*
  250. * On PowerNV platform, IOV BAR is mapped by M64 BAR to enable the
  251. * SR-IOV. While from hardware perspective, the range mapped by M64
  252. * BAR should be size aligned.
  253. *
  254. * This function returns the total IOV BAR size if M64 BAR is in
  255. * Shared PE mode or just VF BAR size if not.
  256. * If the M64 BAR is in Single PE mode, return the VF BAR size or
  257. * M64 segment size if IOV BAR size is less.
  258. */
  259. return phb->ioda.total_pe_num * align;
  260. }
  261. static int pnv_pci_vf_release_m64(struct pci_dev *pdev, u16 num_vfs)
  262. {
  263. struct pnv_iov_data *iov;
  264. struct pnv_phb *phb;
  265. int window_id;
  266. phb = pci_bus_to_pnvhb(pdev->bus);
  267. iov = pnv_iov_get(pdev);
  268. for_each_set_bit(window_id, iov->used_m64_bar_mask, MAX_M64_BARS) {
  269. opal_pci_phb_mmio_enable(phb->opal_id,
  270. OPAL_M64_WINDOW_TYPE,
  271. window_id,
  272. 0);
  273. clear_bit(window_id, &phb->ioda.m64_bar_alloc);
  274. }
  275. return 0;
  276. }
  277. /*
  278. * PHB3 and beyond support segmented windows. The window's address range
  279. * is subdivided into phb->ioda.total_pe_num segments and there's a 1-1
  280. * mapping between PEs and segments.
  281. */
  282. static int64_t pnv_ioda_map_m64_segmented(struct pnv_phb *phb,
  283. int window_id,
  284. resource_size_t start,
  285. resource_size_t size)
  286. {
  287. int64_t rc;
  288. rc = opal_pci_set_phb_mem_window(phb->opal_id,
  289. OPAL_M64_WINDOW_TYPE,
  290. window_id,
  291. start,
  292. 0, /* unused */
  293. size);
  294. if (rc)
  295. goto out;
  296. rc = opal_pci_phb_mmio_enable(phb->opal_id,
  297. OPAL_M64_WINDOW_TYPE,
  298. window_id,
  299. OPAL_ENABLE_M64_SPLIT);
  300. out:
  301. if (rc)
  302. pr_err("Failed to map M64 window #%d: %lld\n", window_id, rc);
  303. return rc;
  304. }
  305. static int64_t pnv_ioda_map_m64_single(struct pnv_phb *phb,
  306. int pe_num,
  307. int window_id,
  308. resource_size_t start,
  309. resource_size_t size)
  310. {
  311. int64_t rc;
  312. /*
  313. * The API for setting up m64 mmio windows seems to have been designed
  314. * with P7-IOC in mind. For that chip each M64 BAR (window) had a fixed
  315. * split of 8 equally sized segments each of which could individually
  316. * assigned to a PE.
  317. *
  318. * The problem with this is that the API doesn't have any way to
  319. * communicate the number of segments we want on a BAR. This wasn't
  320. * a problem for p7-ioc since you didn't have a choice, but the
  321. * single PE windows added in PHB3 don't map cleanly to this API.
  322. *
  323. * As a result we've got this slightly awkward process where we
  324. * call opal_pci_map_pe_mmio_window() to put the single in single
  325. * PE mode, and set the PE for the window before setting the address
  326. * bounds. We need to do it this way because the single PE windows
  327. * for PHB3 have different alignment requirements on PHB3.
  328. */
  329. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  330. pe_num,
  331. OPAL_M64_WINDOW_TYPE,
  332. window_id,
  333. 0);
  334. if (rc)
  335. goto out;
  336. /*
  337. * NB: In single PE mode the window needs to be aligned to 32MB
  338. */
  339. rc = opal_pci_set_phb_mem_window(phb->opal_id,
  340. OPAL_M64_WINDOW_TYPE,
  341. window_id,
  342. start,
  343. 0, /* ignored by FW, m64 is 1-1 */
  344. size);
  345. if (rc)
  346. goto out;
  347. /*
  348. * Now actually enable it. We specified the BAR should be in "non-split"
  349. * mode so FW will validate that the BAR is in single PE mode.
  350. */
  351. rc = opal_pci_phb_mmio_enable(phb->opal_id,
  352. OPAL_M64_WINDOW_TYPE,
  353. window_id,
  354. OPAL_ENABLE_M64_NON_SPLIT);
  355. out:
  356. if (rc)
  357. pr_err("Error mapping single PE BAR\n");
  358. return rc;
  359. }
  360. static int pnv_pci_alloc_m64_bar(struct pnv_phb *phb, struct pnv_iov_data *iov)
  361. {
  362. int win;
  363. do {
  364. win = find_next_zero_bit(&phb->ioda.m64_bar_alloc,
  365. phb->ioda.m64_bar_idx + 1, 0);
  366. if (win >= phb->ioda.m64_bar_idx + 1)
  367. return -1;
  368. } while (test_and_set_bit(win, &phb->ioda.m64_bar_alloc));
  369. set_bit(win, iov->used_m64_bar_mask);
  370. return win;
  371. }
  372. static int pnv_pci_vf_assign_m64(struct pci_dev *pdev, u16 num_vfs)
  373. {
  374. struct pnv_iov_data *iov;
  375. struct pnv_phb *phb;
  376. int win;
  377. struct resource *res;
  378. int i, j;
  379. int64_t rc;
  380. resource_size_t size, start;
  381. int base_pe_num;
  382. phb = pci_bus_to_pnvhb(pdev->bus);
  383. iov = pnv_iov_get(pdev);
  384. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  385. res = &pdev->resource[i + PCI_IOV_RESOURCES];
  386. if (!res->flags || !res->parent)
  387. continue;
  388. /* don't need single mode? map everything in one go! */
  389. if (!iov->m64_single_mode[i]) {
  390. win = pnv_pci_alloc_m64_bar(phb, iov);
  391. if (win < 0)
  392. goto m64_failed;
  393. size = resource_size(res);
  394. start = res->start;
  395. rc = pnv_ioda_map_m64_segmented(phb, win, start, size);
  396. if (rc)
  397. goto m64_failed;
  398. continue;
  399. }
  400. /* otherwise map each VF with single PE BARs */
  401. size = pci_iov_resource_size(pdev, PCI_IOV_RESOURCES + i);
  402. base_pe_num = iov->vf_pe_arr[0].pe_number;
  403. for (j = 0; j < num_vfs; j++) {
  404. win = pnv_pci_alloc_m64_bar(phb, iov);
  405. if (win < 0)
  406. goto m64_failed;
  407. start = res->start + size * j;
  408. rc = pnv_ioda_map_m64_single(phb, win,
  409. base_pe_num + j,
  410. start,
  411. size);
  412. if (rc)
  413. goto m64_failed;
  414. }
  415. }
  416. return 0;
  417. m64_failed:
  418. pnv_pci_vf_release_m64(pdev, num_vfs);
  419. return -EBUSY;
  420. }
  421. static void pnv_ioda_release_vf_PE(struct pci_dev *pdev)
  422. {
  423. struct pnv_phb *phb;
  424. struct pnv_ioda_pe *pe, *pe_n;
  425. phb = pci_bus_to_pnvhb(pdev->bus);
  426. if (!pdev->is_physfn)
  427. return;
  428. /* FIXME: Use pnv_ioda_release_pe()? */
  429. list_for_each_entry_safe(pe, pe_n, &phb->ioda.pe_list, list) {
  430. if (pe->parent_dev != pdev)
  431. continue;
  432. pnv_pci_ioda2_release_pe_dma(pe);
  433. /* Remove from list */
  434. mutex_lock(&phb->ioda.pe_list_mutex);
  435. list_del(&pe->list);
  436. mutex_unlock(&phb->ioda.pe_list_mutex);
  437. pnv_ioda_deconfigure_pe(phb, pe);
  438. pnv_ioda_free_pe(pe);
  439. }
  440. }
  441. static int pnv_pci_vf_resource_shift(struct pci_dev *dev, int offset)
  442. {
  443. struct resource *res, res2;
  444. struct pnv_iov_data *iov;
  445. resource_size_t size;
  446. u16 num_vfs;
  447. int i;
  448. if (!dev->is_physfn)
  449. return -EINVAL;
  450. iov = pnv_iov_get(dev);
  451. /*
  452. * "offset" is in VFs. The M64 windows are sized so that when they
  453. * are segmented, each segment is the same size as the IOV BAR.
  454. * Each segment is in a separate PE, and the high order bits of the
  455. * address are the PE number. Therefore, each VF's BAR is in a
  456. * separate PE, and changing the IOV BAR start address changes the
  457. * range of PEs the VFs are in.
  458. */
  459. num_vfs = iov->num_vfs;
  460. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  461. res = &dev->resource[i + PCI_IOV_RESOURCES];
  462. if (!res->flags || !res->parent)
  463. continue;
  464. if (iov->m64_single_mode[i])
  465. continue;
  466. /*
  467. * The actual IOV BAR range is determined by the start address
  468. * and the actual size for num_vfs VFs BAR. This check is to
  469. * make sure that after shifting, the range will not overlap
  470. * with another device.
  471. */
  472. size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
  473. res2.flags = res->flags;
  474. res2.start = res->start + (size * offset);
  475. res2.end = res2.start + (size * num_vfs) - 1;
  476. if (res2.end > res->end) {
  477. dev_err(&dev->dev, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n",
  478. i, &res2, res, num_vfs, offset);
  479. return -EBUSY;
  480. }
  481. }
  482. /*
  483. * Since M64 BAR shares segments among all possible 256 PEs,
  484. * we have to shift the beginning of PF IOV BAR to make it start from
  485. * the segment which belongs to the PE number assigned to the first VF.
  486. * This creates a "hole" in the /proc/iomem which could be used for
  487. * allocating other resources so we reserve this area below and
  488. * release when IOV is released.
  489. */
  490. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  491. res = &dev->resource[i + PCI_IOV_RESOURCES];
  492. if (!res->flags || !res->parent)
  493. continue;
  494. if (iov->m64_single_mode[i])
  495. continue;
  496. size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
  497. res2 = *res;
  498. res->start += size * offset;
  499. dev_info(&dev->dev, "VF BAR%d: %pR shifted to %pR (%sabling %d VFs shifted by %d)\n",
  500. i, &res2, res, (offset > 0) ? "En" : "Dis",
  501. num_vfs, offset);
  502. if (offset < 0) {
  503. devm_release_resource(&dev->dev, &iov->holes[i]);
  504. memset(&iov->holes[i], 0, sizeof(iov->holes[i]));
  505. }
  506. pci_update_resource(dev, i + PCI_IOV_RESOURCES);
  507. if (offset > 0) {
  508. iov->holes[i].start = res2.start;
  509. iov->holes[i].end = res2.start + size * offset - 1;
  510. iov->holes[i].flags = IORESOURCE_BUS;
  511. iov->holes[i].name = "pnv_iov_reserved";
  512. devm_request_resource(&dev->dev, res->parent,
  513. &iov->holes[i]);
  514. }
  515. }
  516. return 0;
  517. }
  518. static void pnv_pci_sriov_disable(struct pci_dev *pdev)
  519. {
  520. u16 num_vfs, base_pe;
  521. struct pnv_iov_data *iov;
  522. iov = pnv_iov_get(pdev);
  523. if (WARN_ON(!iov))
  524. return;
  525. num_vfs = iov->num_vfs;
  526. base_pe = iov->vf_pe_arr[0].pe_number;
  527. /* Release VF PEs */
  528. pnv_ioda_release_vf_PE(pdev);
  529. /* Un-shift the IOV BARs if we need to */
  530. if (iov->need_shift)
  531. pnv_pci_vf_resource_shift(pdev, -base_pe);
  532. /* Release M64 windows */
  533. pnv_pci_vf_release_m64(pdev, num_vfs);
  534. }
  535. static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs)
  536. {
  537. struct pnv_phb *phb;
  538. struct pnv_ioda_pe *pe;
  539. int pe_num;
  540. u16 vf_index;
  541. struct pnv_iov_data *iov;
  542. struct pci_dn *pdn;
  543. if (!pdev->is_physfn)
  544. return;
  545. phb = pci_bus_to_pnvhb(pdev->bus);
  546. pdn = pci_get_pdn(pdev);
  547. iov = pnv_iov_get(pdev);
  548. /* Reserve PE for each VF */
  549. for (vf_index = 0; vf_index < num_vfs; vf_index++) {
  550. int vf_devfn = pci_iov_virtfn_devfn(pdev, vf_index);
  551. int vf_bus = pci_iov_virtfn_bus(pdev, vf_index);
  552. struct pci_dn *vf_pdn;
  553. pe = &iov->vf_pe_arr[vf_index];
  554. pe->phb = phb;
  555. pe->flags = PNV_IODA_PE_VF;
  556. pe->pbus = NULL;
  557. pe->parent_dev = pdev;
  558. pe->mve_number = -1;
  559. pe->rid = (vf_bus << 8) | vf_devfn;
  560. pe_num = pe->pe_number;
  561. pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%x\n",
  562. pci_domain_nr(pdev->bus), pdev->bus->number,
  563. PCI_SLOT(vf_devfn), PCI_FUNC(vf_devfn), pe_num);
  564. if (pnv_ioda_configure_pe(phb, pe)) {
  565. /* XXX What do we do here ? */
  566. pnv_ioda_free_pe(pe);
  567. pe->pdev = NULL;
  568. continue;
  569. }
  570. /* Put PE to the list */
  571. mutex_lock(&phb->ioda.pe_list_mutex);
  572. list_add_tail(&pe->list, &phb->ioda.pe_list);
  573. mutex_unlock(&phb->ioda.pe_list_mutex);
  574. /* associate this pe to it's pdn */
  575. list_for_each_entry(vf_pdn, &pdn->parent->child_list, list) {
  576. if (vf_pdn->busno == vf_bus &&
  577. vf_pdn->devfn == vf_devfn) {
  578. vf_pdn->pe_number = pe_num;
  579. break;
  580. }
  581. }
  582. pnv_pci_ioda2_setup_dma_pe(phb, pe);
  583. }
  584. }
  585. static int pnv_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
  586. {
  587. struct pnv_ioda_pe *base_pe;
  588. struct pnv_iov_data *iov;
  589. struct pnv_phb *phb;
  590. int ret;
  591. u16 i;
  592. phb = pci_bus_to_pnvhb(pdev->bus);
  593. iov = pnv_iov_get(pdev);
  594. /*
  595. * There's a calls to IODA2 PE setup code littered throughout. We could
  596. * probably fix that, but we'd still have problems due to the
  597. * restriction inherent on IODA1 PHBs.
  598. *
  599. * NB: We class IODA3 as IODA2 since they're very similar.
  600. */
  601. if (phb->type != PNV_PHB_IODA2) {
  602. pci_err(pdev, "SR-IOV is not supported on this PHB\n");
  603. return -ENXIO;
  604. }
  605. if (!iov) {
  606. dev_info(&pdev->dev, "don't support this SRIOV device with non 64bit-prefetchable IOV BAR\n");
  607. return -ENOSPC;
  608. }
  609. /* allocate a contiguous block of PEs for our VFs */
  610. base_pe = pnv_ioda_alloc_pe(phb, num_vfs);
  611. if (!base_pe) {
  612. pci_err(pdev, "Unable to allocate PEs for %d VFs\n", num_vfs);
  613. return -EBUSY;
  614. }
  615. iov->vf_pe_arr = base_pe;
  616. iov->num_vfs = num_vfs;
  617. /* Assign M64 window accordingly */
  618. ret = pnv_pci_vf_assign_m64(pdev, num_vfs);
  619. if (ret) {
  620. dev_info(&pdev->dev, "Not enough M64 window resources\n");
  621. goto m64_failed;
  622. }
  623. /*
  624. * When using one M64 BAR to map one IOV BAR, we need to shift
  625. * the IOV BAR according to the PE# allocated to the VFs.
  626. * Otherwise, the PE# for the VF will conflict with others.
  627. */
  628. if (iov->need_shift) {
  629. ret = pnv_pci_vf_resource_shift(pdev, base_pe->pe_number);
  630. if (ret)
  631. goto shift_failed;
  632. }
  633. /* Setup VF PEs */
  634. pnv_ioda_setup_vf_PE(pdev, num_vfs);
  635. return 0;
  636. shift_failed:
  637. pnv_pci_vf_release_m64(pdev, num_vfs);
  638. m64_failed:
  639. for (i = 0; i < num_vfs; i++)
  640. pnv_ioda_free_pe(&iov->vf_pe_arr[i]);
  641. return ret;
  642. }
  643. int pnv_pcibios_sriov_disable(struct pci_dev *pdev)
  644. {
  645. pnv_pci_sriov_disable(pdev);
  646. /* Release PCI data */
  647. remove_sriov_vf_pdns(pdev);
  648. return 0;
  649. }
  650. int pnv_pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
  651. {
  652. /* Allocate PCI data */
  653. add_sriov_vf_pdns(pdev);
  654. return pnv_pci_sriov_enable(pdev, num_vfs);
  655. }