vfio_pci_config.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  1. /*
  2. * VFIO PCI config space virtualization
  3. *
  4. * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
  5. * Author: Alex Williamson <alex.williamson@redhat.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Derived from original vfio:
  12. * Copyright 2010 Cisco Systems, Inc. All rights reserved.
  13. * Author: Tom Lyon, pugs@cisco.com
  14. */
  15. /*
  16. * This code handles reading and writing of PCI configuration registers.
  17. * This is hairy because we want to allow a lot of flexibility to the
  18. * user driver, but cannot trust it with all of the config fields.
  19. * Tables determine which fields can be read and written, as well as
  20. * which fields are 'virtualized' - special actions and translations to
  21. * make it appear to the user that he has control, when in fact things
  22. * must be negotiated with the underlying OS.
  23. */
  24. #include <linux/fs.h>
  25. #include <linux/pci.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/vfio.h>
  28. #include <linux/slab.h>
  29. #include "vfio_pci_private.h"
  30. #define PCI_CFG_SPACE_SIZE 256
  31. /* Fake capability ID for standard config space */
  32. #define PCI_CAP_ID_BASIC 0
  33. #define is_bar(offset) \
  34. ((offset >= PCI_BASE_ADDRESS_0 && offset < PCI_BASE_ADDRESS_5 + 4) || \
  35. (offset >= PCI_ROM_ADDRESS && offset < PCI_ROM_ADDRESS + 4))
  36. /*
  37. * Lengths of PCI Config Capabilities
  38. * 0: Removed from the user visible capability list
  39. * FF: Variable length
  40. */
  41. static const u8 pci_cap_length[PCI_CAP_ID_MAX + 1] = {
  42. [PCI_CAP_ID_BASIC] = PCI_STD_HEADER_SIZEOF, /* pci config header */
  43. [PCI_CAP_ID_PM] = PCI_PM_SIZEOF,
  44. [PCI_CAP_ID_AGP] = PCI_AGP_SIZEOF,
  45. [PCI_CAP_ID_VPD] = PCI_CAP_VPD_SIZEOF,
  46. [PCI_CAP_ID_SLOTID] = 0, /* bridge - don't care */
  47. [PCI_CAP_ID_MSI] = 0xFF, /* 10, 14, 20, or 24 */
  48. [PCI_CAP_ID_CHSWP] = 0, /* cpci - not yet */
  49. [PCI_CAP_ID_PCIX] = 0xFF, /* 8 or 24 */
  50. [PCI_CAP_ID_HT] = 0xFF, /* hypertransport */
  51. [PCI_CAP_ID_VNDR] = 0xFF, /* variable */
  52. [PCI_CAP_ID_DBG] = 0, /* debug - don't care */
  53. [PCI_CAP_ID_CCRC] = 0, /* cpci - not yet */
  54. [PCI_CAP_ID_SHPC] = 0, /* hotswap - not yet */
  55. [PCI_CAP_ID_SSVID] = 0, /* bridge - don't care */
  56. [PCI_CAP_ID_AGP3] = 0, /* AGP8x - not yet */
  57. [PCI_CAP_ID_SECDEV] = 0, /* secure device not yet */
  58. [PCI_CAP_ID_EXP] = 0xFF, /* 20 or 44 */
  59. [PCI_CAP_ID_MSIX] = PCI_CAP_MSIX_SIZEOF,
  60. [PCI_CAP_ID_SATA] = 0xFF,
  61. [PCI_CAP_ID_AF] = PCI_CAP_AF_SIZEOF,
  62. };
  63. /*
  64. * Lengths of PCIe/PCI-X Extended Config Capabilities
  65. * 0: Removed or masked from the user visible capabilty list
  66. * FF: Variable length
  67. */
  68. static const u16 pci_ext_cap_length[PCI_EXT_CAP_ID_MAX + 1] = {
  69. [PCI_EXT_CAP_ID_ERR] = PCI_ERR_ROOT_COMMAND,
  70. [PCI_EXT_CAP_ID_VC] = 0xFF,
  71. [PCI_EXT_CAP_ID_DSN] = PCI_EXT_CAP_DSN_SIZEOF,
  72. [PCI_EXT_CAP_ID_PWR] = PCI_EXT_CAP_PWR_SIZEOF,
  73. [PCI_EXT_CAP_ID_RCLD] = 0, /* root only - don't care */
  74. [PCI_EXT_CAP_ID_RCILC] = 0, /* root only - don't care */
  75. [PCI_EXT_CAP_ID_RCEC] = 0, /* root only - don't care */
  76. [PCI_EXT_CAP_ID_MFVC] = 0xFF,
  77. [PCI_EXT_CAP_ID_VC9] = 0xFF, /* same as CAP_ID_VC */
  78. [PCI_EXT_CAP_ID_RCRB] = 0, /* root only - don't care */
  79. [PCI_EXT_CAP_ID_VNDR] = 0xFF,
  80. [PCI_EXT_CAP_ID_CAC] = 0, /* obsolete */
  81. [PCI_EXT_CAP_ID_ACS] = 0xFF,
  82. [PCI_EXT_CAP_ID_ARI] = PCI_EXT_CAP_ARI_SIZEOF,
  83. [PCI_EXT_CAP_ID_ATS] = PCI_EXT_CAP_ATS_SIZEOF,
  84. [PCI_EXT_CAP_ID_SRIOV] = PCI_EXT_CAP_SRIOV_SIZEOF,
  85. [PCI_EXT_CAP_ID_MRIOV] = 0, /* not yet */
  86. [PCI_EXT_CAP_ID_MCAST] = PCI_EXT_CAP_MCAST_ENDPOINT_SIZEOF,
  87. [PCI_EXT_CAP_ID_PRI] = PCI_EXT_CAP_PRI_SIZEOF,
  88. [PCI_EXT_CAP_ID_AMD_XXX] = 0, /* not yet */
  89. [PCI_EXT_CAP_ID_REBAR] = 0xFF,
  90. [PCI_EXT_CAP_ID_DPA] = 0xFF,
  91. [PCI_EXT_CAP_ID_TPH] = 0xFF,
  92. [PCI_EXT_CAP_ID_LTR] = PCI_EXT_CAP_LTR_SIZEOF,
  93. [PCI_EXT_CAP_ID_SECPCI] = 0, /* not yet */
  94. [PCI_EXT_CAP_ID_PMUX] = 0, /* not yet */
  95. [PCI_EXT_CAP_ID_PASID] = 0, /* not yet */
  96. };
  97. /*
  98. * Read/Write Permission Bits - one bit for each bit in capability
  99. * Any field can be read if it exists, but what is read depends on
  100. * whether the field is 'virtualized', or just pass thru to the
  101. * hardware. Any virtualized field is also virtualized for writes.
  102. * Writes are only permitted if they have a 1 bit here.
  103. */
  104. struct perm_bits {
  105. u8 *virt; /* read/write virtual data, not hw */
  106. u8 *write; /* writeable bits */
  107. int (*readfn)(struct vfio_pci_device *vdev, int pos, int count,
  108. struct perm_bits *perm, int offset, __le32 *val);
  109. int (*writefn)(struct vfio_pci_device *vdev, int pos, int count,
  110. struct perm_bits *perm, int offset, __le32 val);
  111. };
  112. #define NO_VIRT 0
  113. #define ALL_VIRT 0xFFFFFFFFU
  114. #define NO_WRITE 0
  115. #define ALL_WRITE 0xFFFFFFFFU
  116. static int vfio_user_config_read(struct pci_dev *pdev, int offset,
  117. __le32 *val, int count)
  118. {
  119. int ret = -EINVAL;
  120. u32 tmp_val = 0;
  121. switch (count) {
  122. case 1:
  123. {
  124. u8 tmp;
  125. ret = pci_user_read_config_byte(pdev, offset, &tmp);
  126. tmp_val = tmp;
  127. break;
  128. }
  129. case 2:
  130. {
  131. u16 tmp;
  132. ret = pci_user_read_config_word(pdev, offset, &tmp);
  133. tmp_val = tmp;
  134. break;
  135. }
  136. case 4:
  137. ret = pci_user_read_config_dword(pdev, offset, &tmp_val);
  138. break;
  139. }
  140. *val = cpu_to_le32(tmp_val);
  141. return pcibios_err_to_errno(ret);
  142. }
  143. static int vfio_user_config_write(struct pci_dev *pdev, int offset,
  144. __le32 val, int count)
  145. {
  146. int ret = -EINVAL;
  147. u32 tmp_val = le32_to_cpu(val);
  148. switch (count) {
  149. case 1:
  150. ret = pci_user_write_config_byte(pdev, offset, tmp_val);
  151. break;
  152. case 2:
  153. ret = pci_user_write_config_word(pdev, offset, tmp_val);
  154. break;
  155. case 4:
  156. ret = pci_user_write_config_dword(pdev, offset, tmp_val);
  157. break;
  158. }
  159. return pcibios_err_to_errno(ret);
  160. }
  161. static int vfio_default_config_read(struct vfio_pci_device *vdev, int pos,
  162. int count, struct perm_bits *perm,
  163. int offset, __le32 *val)
  164. {
  165. __le32 virt = 0;
  166. memcpy(val, vdev->vconfig + pos, count);
  167. memcpy(&virt, perm->virt + offset, count);
  168. /* Any non-virtualized bits? */
  169. if (cpu_to_le32(~0U >> (32 - (count * 8))) != virt) {
  170. struct pci_dev *pdev = vdev->pdev;
  171. __le32 phys_val = 0;
  172. int ret;
  173. ret = vfio_user_config_read(pdev, pos, &phys_val, count);
  174. if (ret)
  175. return ret;
  176. *val = (phys_val & ~virt) | (*val & virt);
  177. }
  178. return count;
  179. }
  180. static int vfio_default_config_write(struct vfio_pci_device *vdev, int pos,
  181. int count, struct perm_bits *perm,
  182. int offset, __le32 val)
  183. {
  184. __le32 virt = 0, write = 0;
  185. memcpy(&write, perm->write + offset, count);
  186. if (!write)
  187. return count; /* drop, no writable bits */
  188. memcpy(&virt, perm->virt + offset, count);
  189. /* Virtualized and writable bits go to vconfig */
  190. if (write & virt) {
  191. __le32 virt_val = 0;
  192. memcpy(&virt_val, vdev->vconfig + pos, count);
  193. virt_val &= ~(write & virt);
  194. virt_val |= (val & (write & virt));
  195. memcpy(vdev->vconfig + pos, &virt_val, count);
  196. }
  197. /* Non-virtualzed and writable bits go to hardware */
  198. if (write & ~virt) {
  199. struct pci_dev *pdev = vdev->pdev;
  200. __le32 phys_val = 0;
  201. int ret;
  202. ret = vfio_user_config_read(pdev, pos, &phys_val, count);
  203. if (ret)
  204. return ret;
  205. phys_val &= ~(write & ~virt);
  206. phys_val |= (val & (write & ~virt));
  207. ret = vfio_user_config_write(pdev, pos, phys_val, count);
  208. if (ret)
  209. return ret;
  210. }
  211. return count;
  212. }
  213. /* Allow direct read from hardware, except for capability next pointer */
  214. static int vfio_direct_config_read(struct vfio_pci_device *vdev, int pos,
  215. int count, struct perm_bits *perm,
  216. int offset, __le32 *val)
  217. {
  218. int ret;
  219. ret = vfio_user_config_read(vdev->pdev, pos, val, count);
  220. if (ret)
  221. return pcibios_err_to_errno(ret);
  222. if (pos >= PCI_CFG_SPACE_SIZE) { /* Extended cap header mangling */
  223. if (offset < 4)
  224. memcpy(val, vdev->vconfig + pos, count);
  225. } else if (pos >= PCI_STD_HEADER_SIZEOF) { /* Std cap mangling */
  226. if (offset == PCI_CAP_LIST_ID && count > 1)
  227. memcpy(val, vdev->vconfig + pos,
  228. min(PCI_CAP_FLAGS, count));
  229. else if (offset == PCI_CAP_LIST_NEXT)
  230. memcpy(val, vdev->vconfig + pos, 1);
  231. }
  232. return count;
  233. }
  234. /* Raw access skips any kind of virtualization */
  235. static int vfio_raw_config_write(struct vfio_pci_device *vdev, int pos,
  236. int count, struct perm_bits *perm,
  237. int offset, __le32 val)
  238. {
  239. int ret;
  240. ret = vfio_user_config_write(vdev->pdev, pos, val, count);
  241. if (ret)
  242. return ret;
  243. return count;
  244. }
  245. static int vfio_raw_config_read(struct vfio_pci_device *vdev, int pos,
  246. int count, struct perm_bits *perm,
  247. int offset, __le32 *val)
  248. {
  249. int ret;
  250. ret = vfio_user_config_read(vdev->pdev, pos, val, count);
  251. if (ret)
  252. return pcibios_err_to_errno(ret);
  253. return count;
  254. }
  255. /* Virt access uses only virtualization */
  256. static int vfio_virt_config_write(struct vfio_pci_device *vdev, int pos,
  257. int count, struct perm_bits *perm,
  258. int offset, __le32 val)
  259. {
  260. memcpy(vdev->vconfig + pos, &val, count);
  261. return count;
  262. }
  263. static int vfio_virt_config_read(struct vfio_pci_device *vdev, int pos,
  264. int count, struct perm_bits *perm,
  265. int offset, __le32 *val)
  266. {
  267. memcpy(val, vdev->vconfig + pos, count);
  268. return count;
  269. }
  270. /* Default capability regions to read-only, no-virtualization */
  271. static struct perm_bits cap_perms[PCI_CAP_ID_MAX + 1] = {
  272. [0 ... PCI_CAP_ID_MAX] = { .readfn = vfio_direct_config_read }
  273. };
  274. static struct perm_bits ecap_perms[PCI_EXT_CAP_ID_MAX + 1] = {
  275. [0 ... PCI_EXT_CAP_ID_MAX] = { .readfn = vfio_direct_config_read }
  276. };
  277. /*
  278. * Default unassigned regions to raw read-write access. Some devices
  279. * require this to function as they hide registers between the gaps in
  280. * config space (be2net). Like MMIO and I/O port registers, we have
  281. * to trust the hardware isolation.
  282. */
  283. static struct perm_bits unassigned_perms = {
  284. .readfn = vfio_raw_config_read,
  285. .writefn = vfio_raw_config_write
  286. };
  287. static struct perm_bits virt_perms = {
  288. .readfn = vfio_virt_config_read,
  289. .writefn = vfio_virt_config_write
  290. };
  291. static void free_perm_bits(struct perm_bits *perm)
  292. {
  293. kfree(perm->virt);
  294. kfree(perm->write);
  295. perm->virt = NULL;
  296. perm->write = NULL;
  297. }
  298. static int alloc_perm_bits(struct perm_bits *perm, int size)
  299. {
  300. /*
  301. * Round up all permission bits to the next dword, this lets us
  302. * ignore whether a read/write exceeds the defined capability
  303. * structure. We can do this because:
  304. * - Standard config space is already dword aligned
  305. * - Capabilities are all dword alinged (bits 0:1 of next reserved)
  306. * - Express capabilities defined as dword aligned
  307. */
  308. size = round_up(size, 4);
  309. /*
  310. * Zero state is
  311. * - All Readable, None Writeable, None Virtualized
  312. */
  313. perm->virt = kzalloc(size, GFP_KERNEL);
  314. perm->write = kzalloc(size, GFP_KERNEL);
  315. if (!perm->virt || !perm->write) {
  316. free_perm_bits(perm);
  317. return -ENOMEM;
  318. }
  319. perm->readfn = vfio_default_config_read;
  320. perm->writefn = vfio_default_config_write;
  321. return 0;
  322. }
  323. /*
  324. * Helper functions for filling in permission tables
  325. */
  326. static inline void p_setb(struct perm_bits *p, int off, u8 virt, u8 write)
  327. {
  328. p->virt[off] = virt;
  329. p->write[off] = write;
  330. }
  331. /* Handle endian-ness - pci and tables are little-endian */
  332. static inline void p_setw(struct perm_bits *p, int off, u16 virt, u16 write)
  333. {
  334. *(__le16 *)(&p->virt[off]) = cpu_to_le16(virt);
  335. *(__le16 *)(&p->write[off]) = cpu_to_le16(write);
  336. }
  337. /* Handle endian-ness - pci and tables are little-endian */
  338. static inline void p_setd(struct perm_bits *p, int off, u32 virt, u32 write)
  339. {
  340. *(__le32 *)(&p->virt[off]) = cpu_to_le32(virt);
  341. *(__le32 *)(&p->write[off]) = cpu_to_le32(write);
  342. }
  343. /*
  344. * Restore the *real* BARs after we detect a FLR or backdoor reset.
  345. * (backdoor = some device specific technique that we didn't catch)
  346. */
  347. static void vfio_bar_restore(struct vfio_pci_device *vdev)
  348. {
  349. struct pci_dev *pdev = vdev->pdev;
  350. u32 *rbar = vdev->rbar;
  351. u16 cmd;
  352. int i;
  353. if (pdev->is_virtfn)
  354. return;
  355. pr_info("%s: %s reset recovery - restoring bars\n",
  356. __func__, dev_name(&pdev->dev));
  357. for (i = PCI_BASE_ADDRESS_0; i <= PCI_BASE_ADDRESS_5; i += 4, rbar++)
  358. pci_user_write_config_dword(pdev, i, *rbar);
  359. pci_user_write_config_dword(pdev, PCI_ROM_ADDRESS, *rbar);
  360. if (vdev->nointx) {
  361. pci_user_read_config_word(pdev, PCI_COMMAND, &cmd);
  362. cmd |= PCI_COMMAND_INTX_DISABLE;
  363. pci_user_write_config_word(pdev, PCI_COMMAND, cmd);
  364. }
  365. }
  366. static __le32 vfio_generate_bar_flags(struct pci_dev *pdev, int bar)
  367. {
  368. unsigned long flags = pci_resource_flags(pdev, bar);
  369. u32 val;
  370. if (flags & IORESOURCE_IO)
  371. return cpu_to_le32(PCI_BASE_ADDRESS_SPACE_IO);
  372. val = PCI_BASE_ADDRESS_SPACE_MEMORY;
  373. if (flags & IORESOURCE_PREFETCH)
  374. val |= PCI_BASE_ADDRESS_MEM_PREFETCH;
  375. if (flags & IORESOURCE_MEM_64)
  376. val |= PCI_BASE_ADDRESS_MEM_TYPE_64;
  377. return cpu_to_le32(val);
  378. }
  379. /*
  380. * Pretend we're hardware and tweak the values of the *virtual* PCI BARs
  381. * to reflect the hardware capabilities. This implements BAR sizing.
  382. */
  383. static void vfio_bar_fixup(struct vfio_pci_device *vdev)
  384. {
  385. struct pci_dev *pdev = vdev->pdev;
  386. int i;
  387. __le32 *bar;
  388. u64 mask;
  389. bar = (__le32 *)&vdev->vconfig[PCI_BASE_ADDRESS_0];
  390. for (i = PCI_STD_RESOURCES; i <= PCI_STD_RESOURCE_END; i++, bar++) {
  391. if (!pci_resource_start(pdev, i)) {
  392. *bar = 0; /* Unmapped by host = unimplemented to user */
  393. continue;
  394. }
  395. mask = ~(pci_resource_len(pdev, i) - 1);
  396. *bar &= cpu_to_le32((u32)mask);
  397. *bar |= vfio_generate_bar_flags(pdev, i);
  398. if (*bar & cpu_to_le32(PCI_BASE_ADDRESS_MEM_TYPE_64)) {
  399. bar++;
  400. *bar &= cpu_to_le32((u32)(mask >> 32));
  401. i++;
  402. }
  403. }
  404. bar = (__le32 *)&vdev->vconfig[PCI_ROM_ADDRESS];
  405. /*
  406. * NB. REGION_INFO will have reported zero size if we weren't able
  407. * to read the ROM, but we still return the actual BAR size here if
  408. * it exists (or the shadow ROM space).
  409. */
  410. if (pci_resource_start(pdev, PCI_ROM_RESOURCE)) {
  411. mask = ~(pci_resource_len(pdev, PCI_ROM_RESOURCE) - 1);
  412. mask |= PCI_ROM_ADDRESS_ENABLE;
  413. *bar &= cpu_to_le32((u32)mask);
  414. } else if (pdev->resource[PCI_ROM_RESOURCE].flags &
  415. IORESOURCE_ROM_SHADOW) {
  416. mask = ~(0x20000 - 1);
  417. mask |= PCI_ROM_ADDRESS_ENABLE;
  418. *bar &= cpu_to_le32((u32)mask);
  419. } else
  420. *bar = 0;
  421. vdev->bardirty = false;
  422. }
  423. static int vfio_basic_config_read(struct vfio_pci_device *vdev, int pos,
  424. int count, struct perm_bits *perm,
  425. int offset, __le32 *val)
  426. {
  427. if (is_bar(offset)) /* pos == offset for basic config */
  428. vfio_bar_fixup(vdev);
  429. count = vfio_default_config_read(vdev, pos, count, perm, offset, val);
  430. /* Mask in virtual memory enable for SR-IOV devices */
  431. if (offset == PCI_COMMAND && vdev->pdev->is_virtfn) {
  432. u16 cmd = le16_to_cpu(*(__le16 *)&vdev->vconfig[PCI_COMMAND]);
  433. u32 tmp_val = le32_to_cpu(*val);
  434. tmp_val |= cmd & PCI_COMMAND_MEMORY;
  435. *val = cpu_to_le32(tmp_val);
  436. }
  437. return count;
  438. }
  439. /* Test whether BARs match the value we think they should contain */
  440. static bool vfio_need_bar_restore(struct vfio_pci_device *vdev)
  441. {
  442. int i = 0, pos = PCI_BASE_ADDRESS_0, ret;
  443. u32 bar;
  444. for (; pos <= PCI_BASE_ADDRESS_5; i++, pos += 4) {
  445. if (vdev->rbar[i]) {
  446. ret = pci_user_read_config_dword(vdev->pdev, pos, &bar);
  447. if (ret || vdev->rbar[i] != bar)
  448. return true;
  449. }
  450. }
  451. return false;
  452. }
  453. static int vfio_basic_config_write(struct vfio_pci_device *vdev, int pos,
  454. int count, struct perm_bits *perm,
  455. int offset, __le32 val)
  456. {
  457. struct pci_dev *pdev = vdev->pdev;
  458. __le16 *virt_cmd;
  459. u16 new_cmd = 0;
  460. int ret;
  461. virt_cmd = (__le16 *)&vdev->vconfig[PCI_COMMAND];
  462. if (offset == PCI_COMMAND) {
  463. bool phys_mem, virt_mem, new_mem, phys_io, virt_io, new_io;
  464. u16 phys_cmd;
  465. ret = pci_user_read_config_word(pdev, PCI_COMMAND, &phys_cmd);
  466. if (ret)
  467. return ret;
  468. new_cmd = le32_to_cpu(val);
  469. phys_mem = !!(phys_cmd & PCI_COMMAND_MEMORY);
  470. virt_mem = !!(le16_to_cpu(*virt_cmd) & PCI_COMMAND_MEMORY);
  471. new_mem = !!(new_cmd & PCI_COMMAND_MEMORY);
  472. phys_io = !!(phys_cmd & PCI_COMMAND_IO);
  473. virt_io = !!(le16_to_cpu(*virt_cmd) & PCI_COMMAND_IO);
  474. new_io = !!(new_cmd & PCI_COMMAND_IO);
  475. /*
  476. * If the user is writing mem/io enable (new_mem/io) and we
  477. * think it's already enabled (virt_mem/io), but the hardware
  478. * shows it disabled (phys_mem/io, then the device has
  479. * undergone some kind of backdoor reset and needs to be
  480. * restored before we allow it to enable the bars.
  481. * SR-IOV devices will trigger this, but we catch them later
  482. */
  483. if ((new_mem && virt_mem && !phys_mem) ||
  484. (new_io && virt_io && !phys_io) ||
  485. vfio_need_bar_restore(vdev))
  486. vfio_bar_restore(vdev);
  487. }
  488. count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
  489. if (count < 0)
  490. return count;
  491. /*
  492. * Save current memory/io enable bits in vconfig to allow for
  493. * the test above next time.
  494. */
  495. if (offset == PCI_COMMAND) {
  496. u16 mask = PCI_COMMAND_MEMORY | PCI_COMMAND_IO;
  497. *virt_cmd &= cpu_to_le16(~mask);
  498. *virt_cmd |= cpu_to_le16(new_cmd & mask);
  499. }
  500. /* Emulate INTx disable */
  501. if (offset >= PCI_COMMAND && offset <= PCI_COMMAND + 1) {
  502. bool virt_intx_disable;
  503. virt_intx_disable = !!(le16_to_cpu(*virt_cmd) &
  504. PCI_COMMAND_INTX_DISABLE);
  505. if (virt_intx_disable && !vdev->virq_disabled) {
  506. vdev->virq_disabled = true;
  507. vfio_pci_intx_mask(vdev);
  508. } else if (!virt_intx_disable && vdev->virq_disabled) {
  509. vdev->virq_disabled = false;
  510. vfio_pci_intx_unmask(vdev);
  511. }
  512. }
  513. if (is_bar(offset))
  514. vdev->bardirty = true;
  515. return count;
  516. }
  517. /* Permissions for the Basic PCI Header */
  518. static int __init init_pci_cap_basic_perm(struct perm_bits *perm)
  519. {
  520. if (alloc_perm_bits(perm, PCI_STD_HEADER_SIZEOF))
  521. return -ENOMEM;
  522. perm->readfn = vfio_basic_config_read;
  523. perm->writefn = vfio_basic_config_write;
  524. /* Virtualized for SR-IOV functions, which just have FFFF */
  525. p_setw(perm, PCI_VENDOR_ID, (u16)ALL_VIRT, NO_WRITE);
  526. p_setw(perm, PCI_DEVICE_ID, (u16)ALL_VIRT, NO_WRITE);
  527. /*
  528. * Virtualize INTx disable, we use it internally for interrupt
  529. * control and can emulate it for non-PCI 2.3 devices.
  530. */
  531. p_setw(perm, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE, (u16)ALL_WRITE);
  532. /* Virtualize capability list, we might want to skip/disable */
  533. p_setw(perm, PCI_STATUS, PCI_STATUS_CAP_LIST, NO_WRITE);
  534. /* No harm to write */
  535. p_setb(perm, PCI_CACHE_LINE_SIZE, NO_VIRT, (u8)ALL_WRITE);
  536. p_setb(perm, PCI_LATENCY_TIMER, NO_VIRT, (u8)ALL_WRITE);
  537. p_setb(perm, PCI_BIST, NO_VIRT, (u8)ALL_WRITE);
  538. /* Virtualize all bars, can't touch the real ones */
  539. p_setd(perm, PCI_BASE_ADDRESS_0, ALL_VIRT, ALL_WRITE);
  540. p_setd(perm, PCI_BASE_ADDRESS_1, ALL_VIRT, ALL_WRITE);
  541. p_setd(perm, PCI_BASE_ADDRESS_2, ALL_VIRT, ALL_WRITE);
  542. p_setd(perm, PCI_BASE_ADDRESS_3, ALL_VIRT, ALL_WRITE);
  543. p_setd(perm, PCI_BASE_ADDRESS_4, ALL_VIRT, ALL_WRITE);
  544. p_setd(perm, PCI_BASE_ADDRESS_5, ALL_VIRT, ALL_WRITE);
  545. p_setd(perm, PCI_ROM_ADDRESS, ALL_VIRT, ALL_WRITE);
  546. /* Allow us to adjust capability chain */
  547. p_setb(perm, PCI_CAPABILITY_LIST, (u8)ALL_VIRT, NO_WRITE);
  548. /* Sometimes used by sw, just virtualize */
  549. p_setb(perm, PCI_INTERRUPT_LINE, (u8)ALL_VIRT, (u8)ALL_WRITE);
  550. /* Virtualize interrupt pin to allow hiding INTx */
  551. p_setb(perm, PCI_INTERRUPT_PIN, (u8)ALL_VIRT, (u8)NO_WRITE);
  552. return 0;
  553. }
  554. static int vfio_pm_config_write(struct vfio_pci_device *vdev, int pos,
  555. int count, struct perm_bits *perm,
  556. int offset, __le32 val)
  557. {
  558. count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
  559. if (count < 0)
  560. return count;
  561. if (offset == PCI_PM_CTRL) {
  562. pci_power_t state;
  563. switch (le32_to_cpu(val) & PCI_PM_CTRL_STATE_MASK) {
  564. case 0:
  565. state = PCI_D0;
  566. break;
  567. case 1:
  568. state = PCI_D1;
  569. break;
  570. case 2:
  571. state = PCI_D2;
  572. break;
  573. case 3:
  574. state = PCI_D3hot;
  575. break;
  576. }
  577. pci_set_power_state(vdev->pdev, state);
  578. }
  579. return count;
  580. }
  581. /* Permissions for the Power Management capability */
  582. static int __init init_pci_cap_pm_perm(struct perm_bits *perm)
  583. {
  584. if (alloc_perm_bits(perm, pci_cap_length[PCI_CAP_ID_PM]))
  585. return -ENOMEM;
  586. perm->writefn = vfio_pm_config_write;
  587. /*
  588. * We always virtualize the next field so we can remove
  589. * capabilities from the chain if we want to.
  590. */
  591. p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
  592. /*
  593. * Power management is defined *per function*, so we can let
  594. * the user change power state, but we trap and initiate the
  595. * change ourselves, so the state bits are read-only.
  596. */
  597. p_setd(perm, PCI_PM_CTRL, NO_VIRT, ~PCI_PM_CTRL_STATE_MASK);
  598. return 0;
  599. }
  600. static int vfio_vpd_config_write(struct vfio_pci_device *vdev, int pos,
  601. int count, struct perm_bits *perm,
  602. int offset, __le32 val)
  603. {
  604. struct pci_dev *pdev = vdev->pdev;
  605. __le16 *paddr = (__le16 *)(vdev->vconfig + pos - offset + PCI_VPD_ADDR);
  606. __le32 *pdata = (__le32 *)(vdev->vconfig + pos - offset + PCI_VPD_DATA);
  607. u16 addr;
  608. u32 data;
  609. /*
  610. * Write through to emulation. If the write includes the upper byte
  611. * of PCI_VPD_ADDR, then the PCI_VPD_ADDR_F bit is written and we
  612. * have work to do.
  613. */
  614. count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
  615. if (count < 0 || offset > PCI_VPD_ADDR + 1 ||
  616. offset + count <= PCI_VPD_ADDR + 1)
  617. return count;
  618. addr = le16_to_cpu(*paddr);
  619. if (addr & PCI_VPD_ADDR_F) {
  620. data = le32_to_cpu(*pdata);
  621. if (pci_write_vpd(pdev, addr & ~PCI_VPD_ADDR_F, 4, &data) != 4)
  622. return count;
  623. } else {
  624. data = 0;
  625. if (pci_read_vpd(pdev, addr, 4, &data) < 0)
  626. return count;
  627. *pdata = cpu_to_le32(data);
  628. }
  629. /*
  630. * Toggle PCI_VPD_ADDR_F in the emulated PCI_VPD_ADDR register to
  631. * signal completion. If an error occurs above, we assume that not
  632. * toggling this bit will induce a driver timeout.
  633. */
  634. addr ^= PCI_VPD_ADDR_F;
  635. *paddr = cpu_to_le16(addr);
  636. return count;
  637. }
  638. /* Permissions for Vital Product Data capability */
  639. static int __init init_pci_cap_vpd_perm(struct perm_bits *perm)
  640. {
  641. if (alloc_perm_bits(perm, pci_cap_length[PCI_CAP_ID_VPD]))
  642. return -ENOMEM;
  643. perm->writefn = vfio_vpd_config_write;
  644. /*
  645. * We always virtualize the next field so we can remove
  646. * capabilities from the chain if we want to.
  647. */
  648. p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
  649. /*
  650. * Both the address and data registers are virtualized to
  651. * enable access through the pci_vpd_read/write functions
  652. */
  653. p_setw(perm, PCI_VPD_ADDR, (u16)ALL_VIRT, (u16)ALL_WRITE);
  654. p_setd(perm, PCI_VPD_DATA, ALL_VIRT, ALL_WRITE);
  655. return 0;
  656. }
  657. /* Permissions for PCI-X capability */
  658. static int __init init_pci_cap_pcix_perm(struct perm_bits *perm)
  659. {
  660. /* Alloc 24, but only 8 are used in v0 */
  661. if (alloc_perm_bits(perm, PCI_CAP_PCIX_SIZEOF_V2))
  662. return -ENOMEM;
  663. p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
  664. p_setw(perm, PCI_X_CMD, NO_VIRT, (u16)ALL_WRITE);
  665. p_setd(perm, PCI_X_ECC_CSR, NO_VIRT, ALL_WRITE);
  666. return 0;
  667. }
  668. /* Permissions for PCI Express capability */
  669. static int __init init_pci_cap_exp_perm(struct perm_bits *perm)
  670. {
  671. /* Alloc larger of two possible sizes */
  672. if (alloc_perm_bits(perm, PCI_CAP_EXP_ENDPOINT_SIZEOF_V2))
  673. return -ENOMEM;
  674. p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
  675. /*
  676. * Allow writes to device control fields (includes FLR!)
  677. * but not to devctl_phantom which could confuse IOMMU
  678. * or to the ARI bit in devctl2 which is set at probe time
  679. */
  680. p_setw(perm, PCI_EXP_DEVCTL, NO_VIRT, ~PCI_EXP_DEVCTL_PHANTOM);
  681. p_setw(perm, PCI_EXP_DEVCTL2, NO_VIRT, ~PCI_EXP_DEVCTL2_ARI);
  682. return 0;
  683. }
  684. /* Permissions for Advanced Function capability */
  685. static int __init init_pci_cap_af_perm(struct perm_bits *perm)
  686. {
  687. if (alloc_perm_bits(perm, pci_cap_length[PCI_CAP_ID_AF]))
  688. return -ENOMEM;
  689. p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
  690. p_setb(perm, PCI_AF_CTRL, NO_VIRT, PCI_AF_CTRL_FLR);
  691. return 0;
  692. }
  693. /* Permissions for Advanced Error Reporting extended capability */
  694. static int __init init_pci_ext_cap_err_perm(struct perm_bits *perm)
  695. {
  696. u32 mask;
  697. if (alloc_perm_bits(perm, pci_ext_cap_length[PCI_EXT_CAP_ID_ERR]))
  698. return -ENOMEM;
  699. /*
  700. * Virtualize the first dword of all express capabilities
  701. * because it includes the next pointer. This lets us later
  702. * remove capabilities from the chain if we need to.
  703. */
  704. p_setd(perm, 0, ALL_VIRT, NO_WRITE);
  705. /* Writable bits mask */
  706. mask = PCI_ERR_UNC_UND | /* Undefined */
  707. PCI_ERR_UNC_DLP | /* Data Link Protocol */
  708. PCI_ERR_UNC_SURPDN | /* Surprise Down */
  709. PCI_ERR_UNC_POISON_TLP | /* Poisoned TLP */
  710. PCI_ERR_UNC_FCP | /* Flow Control Protocol */
  711. PCI_ERR_UNC_COMP_TIME | /* Completion Timeout */
  712. PCI_ERR_UNC_COMP_ABORT | /* Completer Abort */
  713. PCI_ERR_UNC_UNX_COMP | /* Unexpected Completion */
  714. PCI_ERR_UNC_RX_OVER | /* Receiver Overflow */
  715. PCI_ERR_UNC_MALF_TLP | /* Malformed TLP */
  716. PCI_ERR_UNC_ECRC | /* ECRC Error Status */
  717. PCI_ERR_UNC_UNSUP | /* Unsupported Request */
  718. PCI_ERR_UNC_ACSV | /* ACS Violation */
  719. PCI_ERR_UNC_INTN | /* internal error */
  720. PCI_ERR_UNC_MCBTLP | /* MC blocked TLP */
  721. PCI_ERR_UNC_ATOMEG | /* Atomic egress blocked */
  722. PCI_ERR_UNC_TLPPRE; /* TLP prefix blocked */
  723. p_setd(perm, PCI_ERR_UNCOR_STATUS, NO_VIRT, mask);
  724. p_setd(perm, PCI_ERR_UNCOR_MASK, NO_VIRT, mask);
  725. p_setd(perm, PCI_ERR_UNCOR_SEVER, NO_VIRT, mask);
  726. mask = PCI_ERR_COR_RCVR | /* Receiver Error Status */
  727. PCI_ERR_COR_BAD_TLP | /* Bad TLP Status */
  728. PCI_ERR_COR_BAD_DLLP | /* Bad DLLP Status */
  729. PCI_ERR_COR_REP_ROLL | /* REPLAY_NUM Rollover */
  730. PCI_ERR_COR_REP_TIMER | /* Replay Timer Timeout */
  731. PCI_ERR_COR_ADV_NFAT | /* Advisory Non-Fatal */
  732. PCI_ERR_COR_INTERNAL | /* Corrected Internal */
  733. PCI_ERR_COR_LOG_OVER; /* Header Log Overflow */
  734. p_setd(perm, PCI_ERR_COR_STATUS, NO_VIRT, mask);
  735. p_setd(perm, PCI_ERR_COR_MASK, NO_VIRT, mask);
  736. mask = PCI_ERR_CAP_ECRC_GENE | /* ECRC Generation Enable */
  737. PCI_ERR_CAP_ECRC_CHKE; /* ECRC Check Enable */
  738. p_setd(perm, PCI_ERR_CAP, NO_VIRT, mask);
  739. return 0;
  740. }
  741. /* Permissions for Power Budgeting extended capability */
  742. static int __init init_pci_ext_cap_pwr_perm(struct perm_bits *perm)
  743. {
  744. if (alloc_perm_bits(perm, pci_ext_cap_length[PCI_EXT_CAP_ID_PWR]))
  745. return -ENOMEM;
  746. p_setd(perm, 0, ALL_VIRT, NO_WRITE);
  747. /* Writing the data selector is OK, the info is still read-only */
  748. p_setb(perm, PCI_PWR_DATA, NO_VIRT, (u8)ALL_WRITE);
  749. return 0;
  750. }
  751. /*
  752. * Initialize the shared permission tables
  753. */
  754. void vfio_pci_uninit_perm_bits(void)
  755. {
  756. free_perm_bits(&cap_perms[PCI_CAP_ID_BASIC]);
  757. free_perm_bits(&cap_perms[PCI_CAP_ID_PM]);
  758. free_perm_bits(&cap_perms[PCI_CAP_ID_VPD]);
  759. free_perm_bits(&cap_perms[PCI_CAP_ID_PCIX]);
  760. free_perm_bits(&cap_perms[PCI_CAP_ID_EXP]);
  761. free_perm_bits(&cap_perms[PCI_CAP_ID_AF]);
  762. free_perm_bits(&ecap_perms[PCI_EXT_CAP_ID_ERR]);
  763. free_perm_bits(&ecap_perms[PCI_EXT_CAP_ID_PWR]);
  764. }
  765. int __init vfio_pci_init_perm_bits(void)
  766. {
  767. int ret;
  768. /* Basic config space */
  769. ret = init_pci_cap_basic_perm(&cap_perms[PCI_CAP_ID_BASIC]);
  770. /* Capabilities */
  771. ret |= init_pci_cap_pm_perm(&cap_perms[PCI_CAP_ID_PM]);
  772. ret |= init_pci_cap_vpd_perm(&cap_perms[PCI_CAP_ID_VPD]);
  773. ret |= init_pci_cap_pcix_perm(&cap_perms[PCI_CAP_ID_PCIX]);
  774. cap_perms[PCI_CAP_ID_VNDR].writefn = vfio_raw_config_write;
  775. ret |= init_pci_cap_exp_perm(&cap_perms[PCI_CAP_ID_EXP]);
  776. ret |= init_pci_cap_af_perm(&cap_perms[PCI_CAP_ID_AF]);
  777. /* Extended capabilities */
  778. ret |= init_pci_ext_cap_err_perm(&ecap_perms[PCI_EXT_CAP_ID_ERR]);
  779. ret |= init_pci_ext_cap_pwr_perm(&ecap_perms[PCI_EXT_CAP_ID_PWR]);
  780. ecap_perms[PCI_EXT_CAP_ID_VNDR].writefn = vfio_raw_config_write;
  781. if (ret)
  782. vfio_pci_uninit_perm_bits();
  783. return ret;
  784. }
  785. static int vfio_find_cap_start(struct vfio_pci_device *vdev, int pos)
  786. {
  787. u8 cap;
  788. int base = (pos >= PCI_CFG_SPACE_SIZE) ? PCI_CFG_SPACE_SIZE :
  789. PCI_STD_HEADER_SIZEOF;
  790. cap = vdev->pci_config_map[pos];
  791. if (cap == PCI_CAP_ID_BASIC)
  792. return 0;
  793. /* XXX Can we have to abutting capabilities of the same type? */
  794. while (pos - 1 >= base && vdev->pci_config_map[pos - 1] == cap)
  795. pos--;
  796. return pos;
  797. }
  798. static int vfio_msi_config_read(struct vfio_pci_device *vdev, int pos,
  799. int count, struct perm_bits *perm,
  800. int offset, __le32 *val)
  801. {
  802. /* Update max available queue size from msi_qmax */
  803. if (offset <= PCI_MSI_FLAGS && offset + count >= PCI_MSI_FLAGS) {
  804. __le16 *flags;
  805. int start;
  806. start = vfio_find_cap_start(vdev, pos);
  807. flags = (__le16 *)&vdev->vconfig[start];
  808. *flags &= cpu_to_le16(~PCI_MSI_FLAGS_QMASK);
  809. *flags |= cpu_to_le16(vdev->msi_qmax << 1);
  810. }
  811. return vfio_default_config_read(vdev, pos, count, perm, offset, val);
  812. }
  813. static int vfio_msi_config_write(struct vfio_pci_device *vdev, int pos,
  814. int count, struct perm_bits *perm,
  815. int offset, __le32 val)
  816. {
  817. count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
  818. if (count < 0)
  819. return count;
  820. /* Fixup and write configured queue size and enable to hardware */
  821. if (offset <= PCI_MSI_FLAGS && offset + count >= PCI_MSI_FLAGS) {
  822. __le16 *pflags;
  823. u16 flags;
  824. int start, ret;
  825. start = vfio_find_cap_start(vdev, pos);
  826. pflags = (__le16 *)&vdev->vconfig[start + PCI_MSI_FLAGS];
  827. flags = le16_to_cpu(*pflags);
  828. /* MSI is enabled via ioctl */
  829. if (!is_msi(vdev))
  830. flags &= ~PCI_MSI_FLAGS_ENABLE;
  831. /* Check queue size */
  832. if ((flags & PCI_MSI_FLAGS_QSIZE) >> 4 > vdev->msi_qmax) {
  833. flags &= ~PCI_MSI_FLAGS_QSIZE;
  834. flags |= vdev->msi_qmax << 4;
  835. }
  836. /* Write back to virt and to hardware */
  837. *pflags = cpu_to_le16(flags);
  838. ret = pci_user_write_config_word(vdev->pdev,
  839. start + PCI_MSI_FLAGS,
  840. flags);
  841. if (ret)
  842. return pcibios_err_to_errno(ret);
  843. }
  844. return count;
  845. }
  846. /*
  847. * MSI determination is per-device, so this routine gets used beyond
  848. * initialization time. Don't add __init
  849. */
  850. static int init_pci_cap_msi_perm(struct perm_bits *perm, int len, u16 flags)
  851. {
  852. if (alloc_perm_bits(perm, len))
  853. return -ENOMEM;
  854. perm->readfn = vfio_msi_config_read;
  855. perm->writefn = vfio_msi_config_write;
  856. p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
  857. /*
  858. * The upper byte of the control register is reserved,
  859. * just setup the lower byte.
  860. */
  861. p_setb(perm, PCI_MSI_FLAGS, (u8)ALL_VIRT, (u8)ALL_WRITE);
  862. p_setd(perm, PCI_MSI_ADDRESS_LO, ALL_VIRT, ALL_WRITE);
  863. if (flags & PCI_MSI_FLAGS_64BIT) {
  864. p_setd(perm, PCI_MSI_ADDRESS_HI, ALL_VIRT, ALL_WRITE);
  865. p_setw(perm, PCI_MSI_DATA_64, (u16)ALL_VIRT, (u16)ALL_WRITE);
  866. if (flags & PCI_MSI_FLAGS_MASKBIT) {
  867. p_setd(perm, PCI_MSI_MASK_64, NO_VIRT, ALL_WRITE);
  868. p_setd(perm, PCI_MSI_PENDING_64, NO_VIRT, ALL_WRITE);
  869. }
  870. } else {
  871. p_setw(perm, PCI_MSI_DATA_32, (u16)ALL_VIRT, (u16)ALL_WRITE);
  872. if (flags & PCI_MSI_FLAGS_MASKBIT) {
  873. p_setd(perm, PCI_MSI_MASK_32, NO_VIRT, ALL_WRITE);
  874. p_setd(perm, PCI_MSI_PENDING_32, NO_VIRT, ALL_WRITE);
  875. }
  876. }
  877. return 0;
  878. }
  879. /* Determine MSI CAP field length; initialize msi_perms on 1st call per vdev */
  880. static int vfio_msi_cap_len(struct vfio_pci_device *vdev, u8 pos)
  881. {
  882. struct pci_dev *pdev = vdev->pdev;
  883. int len, ret;
  884. u16 flags;
  885. ret = pci_read_config_word(pdev, pos + PCI_MSI_FLAGS, &flags);
  886. if (ret)
  887. return pcibios_err_to_errno(ret);
  888. len = 10; /* Minimum size */
  889. if (flags & PCI_MSI_FLAGS_64BIT)
  890. len += 4;
  891. if (flags & PCI_MSI_FLAGS_MASKBIT)
  892. len += 10;
  893. if (vdev->msi_perm)
  894. return len;
  895. vdev->msi_perm = kmalloc(sizeof(struct perm_bits), GFP_KERNEL);
  896. if (!vdev->msi_perm)
  897. return -ENOMEM;
  898. ret = init_pci_cap_msi_perm(vdev->msi_perm, len, flags);
  899. if (ret)
  900. return ret;
  901. return len;
  902. }
  903. /* Determine extended capability length for VC (2 & 9) and MFVC */
  904. static int vfio_vc_cap_len(struct vfio_pci_device *vdev, u16 pos)
  905. {
  906. struct pci_dev *pdev = vdev->pdev;
  907. u32 tmp;
  908. int ret, evcc, phases, vc_arb;
  909. int len = PCI_CAP_VC_BASE_SIZEOF;
  910. ret = pci_read_config_dword(pdev, pos + PCI_VC_PORT_CAP1, &tmp);
  911. if (ret)
  912. return pcibios_err_to_errno(ret);
  913. evcc = tmp & PCI_VC_CAP1_EVCC; /* extended vc count */
  914. ret = pci_read_config_dword(pdev, pos + PCI_VC_PORT_CAP2, &tmp);
  915. if (ret)
  916. return pcibios_err_to_errno(ret);
  917. if (tmp & PCI_VC_CAP2_128_PHASE)
  918. phases = 128;
  919. else if (tmp & PCI_VC_CAP2_64_PHASE)
  920. phases = 64;
  921. else if (tmp & PCI_VC_CAP2_32_PHASE)
  922. phases = 32;
  923. else
  924. phases = 0;
  925. vc_arb = phases * 4;
  926. /*
  927. * Port arbitration tables are root & switch only;
  928. * function arbitration tables are function 0 only.
  929. * In either case, we'll never let user write them so
  930. * we don't care how big they are
  931. */
  932. len += (1 + evcc) * PCI_CAP_VC_PER_VC_SIZEOF;
  933. if (vc_arb) {
  934. len = round_up(len, 16);
  935. len += vc_arb / 8;
  936. }
  937. return len;
  938. }
  939. static int vfio_cap_len(struct vfio_pci_device *vdev, u8 cap, u8 pos)
  940. {
  941. struct pci_dev *pdev = vdev->pdev;
  942. u32 dword;
  943. u16 word;
  944. u8 byte;
  945. int ret;
  946. switch (cap) {
  947. case PCI_CAP_ID_MSI:
  948. return vfio_msi_cap_len(vdev, pos);
  949. case PCI_CAP_ID_PCIX:
  950. ret = pci_read_config_word(pdev, pos + PCI_X_CMD, &word);
  951. if (ret)
  952. return pcibios_err_to_errno(ret);
  953. if (PCI_X_CMD_VERSION(word)) {
  954. if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) {
  955. /* Test for extended capabilities */
  956. pci_read_config_dword(pdev, PCI_CFG_SPACE_SIZE,
  957. &dword);
  958. vdev->extended_caps = (dword != 0);
  959. }
  960. return PCI_CAP_PCIX_SIZEOF_V2;
  961. } else
  962. return PCI_CAP_PCIX_SIZEOF_V0;
  963. case PCI_CAP_ID_VNDR:
  964. /* length follows next field */
  965. ret = pci_read_config_byte(pdev, pos + PCI_CAP_FLAGS, &byte);
  966. if (ret)
  967. return pcibios_err_to_errno(ret);
  968. return byte;
  969. case PCI_CAP_ID_EXP:
  970. if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) {
  971. /* Test for extended capabilities */
  972. pci_read_config_dword(pdev, PCI_CFG_SPACE_SIZE, &dword);
  973. vdev->extended_caps = (dword != 0);
  974. }
  975. /* length based on version */
  976. if ((pcie_caps_reg(pdev) & PCI_EXP_FLAGS_VERS) == 1)
  977. return PCI_CAP_EXP_ENDPOINT_SIZEOF_V1;
  978. else
  979. return PCI_CAP_EXP_ENDPOINT_SIZEOF_V2;
  980. case PCI_CAP_ID_HT:
  981. ret = pci_read_config_byte(pdev, pos + 3, &byte);
  982. if (ret)
  983. return pcibios_err_to_errno(ret);
  984. return (byte & HT_3BIT_CAP_MASK) ?
  985. HT_CAP_SIZEOF_SHORT : HT_CAP_SIZEOF_LONG;
  986. case PCI_CAP_ID_SATA:
  987. ret = pci_read_config_byte(pdev, pos + PCI_SATA_REGS, &byte);
  988. if (ret)
  989. return pcibios_err_to_errno(ret);
  990. byte &= PCI_SATA_REGS_MASK;
  991. if (byte == PCI_SATA_REGS_INLINE)
  992. return PCI_SATA_SIZEOF_LONG;
  993. else
  994. return PCI_SATA_SIZEOF_SHORT;
  995. default:
  996. pr_warn("%s: %s unknown length for pci cap 0x%x@0x%x\n",
  997. dev_name(&pdev->dev), __func__, cap, pos);
  998. }
  999. return 0;
  1000. }
  1001. static int vfio_ext_cap_len(struct vfio_pci_device *vdev, u16 ecap, u16 epos)
  1002. {
  1003. struct pci_dev *pdev = vdev->pdev;
  1004. u8 byte;
  1005. u32 dword;
  1006. int ret;
  1007. switch (ecap) {
  1008. case PCI_EXT_CAP_ID_VNDR:
  1009. ret = pci_read_config_dword(pdev, epos + PCI_VSEC_HDR, &dword);
  1010. if (ret)
  1011. return pcibios_err_to_errno(ret);
  1012. return dword >> PCI_VSEC_HDR_LEN_SHIFT;
  1013. case PCI_EXT_CAP_ID_VC:
  1014. case PCI_EXT_CAP_ID_VC9:
  1015. case PCI_EXT_CAP_ID_MFVC:
  1016. return vfio_vc_cap_len(vdev, epos);
  1017. case PCI_EXT_CAP_ID_ACS:
  1018. ret = pci_read_config_byte(pdev, epos + PCI_ACS_CAP, &byte);
  1019. if (ret)
  1020. return pcibios_err_to_errno(ret);
  1021. if (byte & PCI_ACS_EC) {
  1022. int bits;
  1023. ret = pci_read_config_byte(pdev,
  1024. epos + PCI_ACS_EGRESS_BITS,
  1025. &byte);
  1026. if (ret)
  1027. return pcibios_err_to_errno(ret);
  1028. bits = byte ? round_up(byte, 32) : 256;
  1029. return 8 + (bits / 8);
  1030. }
  1031. return 8;
  1032. case PCI_EXT_CAP_ID_REBAR:
  1033. ret = pci_read_config_byte(pdev, epos + PCI_REBAR_CTRL, &byte);
  1034. if (ret)
  1035. return pcibios_err_to_errno(ret);
  1036. byte &= PCI_REBAR_CTRL_NBAR_MASK;
  1037. byte >>= PCI_REBAR_CTRL_NBAR_SHIFT;
  1038. return 4 + (byte * 8);
  1039. case PCI_EXT_CAP_ID_DPA:
  1040. ret = pci_read_config_byte(pdev, epos + PCI_DPA_CAP, &byte);
  1041. if (ret)
  1042. return pcibios_err_to_errno(ret);
  1043. byte &= PCI_DPA_CAP_SUBSTATE_MASK;
  1044. return PCI_DPA_BASE_SIZEOF + byte + 1;
  1045. case PCI_EXT_CAP_ID_TPH:
  1046. ret = pci_read_config_dword(pdev, epos + PCI_TPH_CAP, &dword);
  1047. if (ret)
  1048. return pcibios_err_to_errno(ret);
  1049. if ((dword & PCI_TPH_CAP_LOC_MASK) == PCI_TPH_LOC_CAP) {
  1050. int sts;
  1051. sts = dword & PCI_TPH_CAP_ST_MASK;
  1052. sts >>= PCI_TPH_CAP_ST_SHIFT;
  1053. return PCI_TPH_BASE_SIZEOF + (sts * 2) + 2;
  1054. }
  1055. return PCI_TPH_BASE_SIZEOF;
  1056. default:
  1057. pr_warn("%s: %s unknown length for pci ecap 0x%x@0x%x\n",
  1058. dev_name(&pdev->dev), __func__, ecap, epos);
  1059. }
  1060. return 0;
  1061. }
  1062. static int vfio_fill_vconfig_bytes(struct vfio_pci_device *vdev,
  1063. int offset, int size)
  1064. {
  1065. struct pci_dev *pdev = vdev->pdev;
  1066. int ret = 0;
  1067. /*
  1068. * We try to read physical config space in the largest chunks
  1069. * we can, assuming that all of the fields support dword access.
  1070. * pci_save_state() makes this same assumption and seems to do ok.
  1071. */
  1072. while (size) {
  1073. int filled;
  1074. if (size >= 4 && !(offset % 4)) {
  1075. __le32 *dwordp = (__le32 *)&vdev->vconfig[offset];
  1076. u32 dword;
  1077. ret = pci_read_config_dword(pdev, offset, &dword);
  1078. if (ret)
  1079. return ret;
  1080. *dwordp = cpu_to_le32(dword);
  1081. filled = 4;
  1082. } else if (size >= 2 && !(offset % 2)) {
  1083. __le16 *wordp = (__le16 *)&vdev->vconfig[offset];
  1084. u16 word;
  1085. ret = pci_read_config_word(pdev, offset, &word);
  1086. if (ret)
  1087. return ret;
  1088. *wordp = cpu_to_le16(word);
  1089. filled = 2;
  1090. } else {
  1091. u8 *byte = &vdev->vconfig[offset];
  1092. ret = pci_read_config_byte(pdev, offset, byte);
  1093. if (ret)
  1094. return ret;
  1095. filled = 1;
  1096. }
  1097. offset += filled;
  1098. size -= filled;
  1099. }
  1100. return ret;
  1101. }
  1102. static int vfio_cap_init(struct vfio_pci_device *vdev)
  1103. {
  1104. struct pci_dev *pdev = vdev->pdev;
  1105. u8 *map = vdev->pci_config_map;
  1106. u16 status;
  1107. u8 pos, *prev, cap;
  1108. int loops, ret, caps = 0;
  1109. /* Any capabilities? */
  1110. ret = pci_read_config_word(pdev, PCI_STATUS, &status);
  1111. if (ret)
  1112. return ret;
  1113. if (!(status & PCI_STATUS_CAP_LIST))
  1114. return 0; /* Done */
  1115. ret = pci_read_config_byte(pdev, PCI_CAPABILITY_LIST, &pos);
  1116. if (ret)
  1117. return ret;
  1118. /* Mark the previous position in case we want to skip a capability */
  1119. prev = &vdev->vconfig[PCI_CAPABILITY_LIST];
  1120. /* We can bound our loop, capabilities are dword aligned */
  1121. loops = (PCI_CFG_SPACE_SIZE - PCI_STD_HEADER_SIZEOF) / PCI_CAP_SIZEOF;
  1122. while (pos && loops--) {
  1123. u8 next;
  1124. int i, len = 0;
  1125. ret = pci_read_config_byte(pdev, pos, &cap);
  1126. if (ret)
  1127. return ret;
  1128. ret = pci_read_config_byte(pdev,
  1129. pos + PCI_CAP_LIST_NEXT, &next);
  1130. if (ret)
  1131. return ret;
  1132. if (cap <= PCI_CAP_ID_MAX) {
  1133. len = pci_cap_length[cap];
  1134. if (len == 0xFF) { /* Variable length */
  1135. len = vfio_cap_len(vdev, cap, pos);
  1136. if (len < 0)
  1137. return len;
  1138. }
  1139. }
  1140. if (!len) {
  1141. pr_info("%s: %s hiding cap 0x%x\n",
  1142. __func__, dev_name(&pdev->dev), cap);
  1143. *prev = next;
  1144. pos = next;
  1145. continue;
  1146. }
  1147. /* Sanity check, do we overlap other capabilities? */
  1148. for (i = 0; i < len; i++) {
  1149. if (likely(map[pos + i] == PCI_CAP_ID_INVALID))
  1150. continue;
  1151. pr_warn("%s: %s pci config conflict @0x%x, was cap 0x%x now cap 0x%x\n",
  1152. __func__, dev_name(&pdev->dev),
  1153. pos + i, map[pos + i], cap);
  1154. }
  1155. BUILD_BUG_ON(PCI_CAP_ID_MAX >= PCI_CAP_ID_INVALID_VIRT);
  1156. memset(map + pos, cap, len);
  1157. ret = vfio_fill_vconfig_bytes(vdev, pos, len);
  1158. if (ret)
  1159. return ret;
  1160. prev = &vdev->vconfig[pos + PCI_CAP_LIST_NEXT];
  1161. pos = next;
  1162. caps++;
  1163. }
  1164. /* If we didn't fill any capabilities, clear the status flag */
  1165. if (!caps) {
  1166. __le16 *vstatus = (__le16 *)&vdev->vconfig[PCI_STATUS];
  1167. *vstatus &= ~cpu_to_le16(PCI_STATUS_CAP_LIST);
  1168. }
  1169. return 0;
  1170. }
  1171. static int vfio_ecap_init(struct vfio_pci_device *vdev)
  1172. {
  1173. struct pci_dev *pdev = vdev->pdev;
  1174. u8 *map = vdev->pci_config_map;
  1175. u16 epos;
  1176. __le32 *prev = NULL;
  1177. int loops, ret, ecaps = 0;
  1178. if (!vdev->extended_caps)
  1179. return 0;
  1180. epos = PCI_CFG_SPACE_SIZE;
  1181. loops = (pdev->cfg_size - PCI_CFG_SPACE_SIZE) / PCI_CAP_SIZEOF;
  1182. while (loops-- && epos >= PCI_CFG_SPACE_SIZE) {
  1183. u32 header;
  1184. u16 ecap;
  1185. int i, len = 0;
  1186. bool hidden = false;
  1187. ret = pci_read_config_dword(pdev, epos, &header);
  1188. if (ret)
  1189. return ret;
  1190. ecap = PCI_EXT_CAP_ID(header);
  1191. if (ecap <= PCI_EXT_CAP_ID_MAX) {
  1192. len = pci_ext_cap_length[ecap];
  1193. if (len == 0xFF) {
  1194. len = vfio_ext_cap_len(vdev, ecap, epos);
  1195. if (len < 0)
  1196. return ret;
  1197. }
  1198. }
  1199. if (!len) {
  1200. pr_info("%s: %s hiding ecap 0x%x@0x%x\n",
  1201. __func__, dev_name(&pdev->dev), ecap, epos);
  1202. /* If not the first in the chain, we can skip over it */
  1203. if (prev) {
  1204. u32 val = epos = PCI_EXT_CAP_NEXT(header);
  1205. *prev &= cpu_to_le32(~(0xffcU << 20));
  1206. *prev |= cpu_to_le32(val << 20);
  1207. continue;
  1208. }
  1209. /*
  1210. * Otherwise, fill in a placeholder, the direct
  1211. * readfn will virtualize this automatically
  1212. */
  1213. len = PCI_CAP_SIZEOF;
  1214. hidden = true;
  1215. }
  1216. for (i = 0; i < len; i++) {
  1217. if (likely(map[epos + i] == PCI_CAP_ID_INVALID))
  1218. continue;
  1219. pr_warn("%s: %s pci config conflict @0x%x, was ecap 0x%x now ecap 0x%x\n",
  1220. __func__, dev_name(&pdev->dev),
  1221. epos + i, map[epos + i], ecap);
  1222. }
  1223. /*
  1224. * Even though ecap is 2 bytes, we're currently a long way
  1225. * from exceeding 1 byte capabilities. If we ever make it
  1226. * up to 0xFE we'll need to up this to a two-byte, byte map.
  1227. */
  1228. BUILD_BUG_ON(PCI_EXT_CAP_ID_MAX >= PCI_CAP_ID_INVALID_VIRT);
  1229. memset(map + epos, ecap, len);
  1230. ret = vfio_fill_vconfig_bytes(vdev, epos, len);
  1231. if (ret)
  1232. return ret;
  1233. /*
  1234. * If we're just using this capability to anchor the list,
  1235. * hide the real ID. Only count real ecaps. XXX PCI spec
  1236. * indicates to use cap id = 0, version = 0, next = 0 if
  1237. * ecaps are absent, hope users check all the way to next.
  1238. */
  1239. if (hidden)
  1240. *(__le32 *)&vdev->vconfig[epos] &=
  1241. cpu_to_le32((0xffcU << 20));
  1242. else
  1243. ecaps++;
  1244. prev = (__le32 *)&vdev->vconfig[epos];
  1245. epos = PCI_EXT_CAP_NEXT(header);
  1246. }
  1247. if (!ecaps)
  1248. *(u32 *)&vdev->vconfig[PCI_CFG_SPACE_SIZE] = 0;
  1249. return 0;
  1250. }
  1251. /*
  1252. * For each device we allocate a pci_config_map that indicates the
  1253. * capability occupying each dword and thus the struct perm_bits we
  1254. * use for read and write. We also allocate a virtualized config
  1255. * space which tracks reads and writes to bits that we emulate for
  1256. * the user. Initial values filled from device.
  1257. *
  1258. * Using shared stuct perm_bits between all vfio-pci devices saves
  1259. * us from allocating cfg_size buffers for virt and write for every
  1260. * device. We could remove vconfig and allocate individual buffers
  1261. * for each area requring emulated bits, but the array of pointers
  1262. * would be comparable in size (at least for standard config space).
  1263. */
  1264. int vfio_config_init(struct vfio_pci_device *vdev)
  1265. {
  1266. struct pci_dev *pdev = vdev->pdev;
  1267. u8 *map, *vconfig;
  1268. int ret;
  1269. /*
  1270. * Config space, caps and ecaps are all dword aligned, so we could
  1271. * use one byte per dword to record the type. However, there are
  1272. * no requiremenst on the length of a capability, so the gap between
  1273. * capabilities needs byte granularity.
  1274. */
  1275. map = kmalloc(pdev->cfg_size, GFP_KERNEL);
  1276. if (!map)
  1277. return -ENOMEM;
  1278. vconfig = kmalloc(pdev->cfg_size, GFP_KERNEL);
  1279. if (!vconfig) {
  1280. kfree(map);
  1281. return -ENOMEM;
  1282. }
  1283. vdev->pci_config_map = map;
  1284. vdev->vconfig = vconfig;
  1285. memset(map, PCI_CAP_ID_BASIC, PCI_STD_HEADER_SIZEOF);
  1286. memset(map + PCI_STD_HEADER_SIZEOF, PCI_CAP_ID_INVALID,
  1287. pdev->cfg_size - PCI_STD_HEADER_SIZEOF);
  1288. ret = vfio_fill_vconfig_bytes(vdev, 0, PCI_STD_HEADER_SIZEOF);
  1289. if (ret)
  1290. goto out;
  1291. vdev->bardirty = true;
  1292. /*
  1293. * XXX can we just pci_load_saved_state/pci_restore_state?
  1294. * may need to rebuild vconfig after that
  1295. */
  1296. /* For restore after reset */
  1297. vdev->rbar[0] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_0]);
  1298. vdev->rbar[1] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_1]);
  1299. vdev->rbar[2] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_2]);
  1300. vdev->rbar[3] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_3]);
  1301. vdev->rbar[4] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_4]);
  1302. vdev->rbar[5] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_5]);
  1303. vdev->rbar[6] = le32_to_cpu(*(__le32 *)&vconfig[PCI_ROM_ADDRESS]);
  1304. if (pdev->is_virtfn) {
  1305. *(__le16 *)&vconfig[PCI_VENDOR_ID] = cpu_to_le16(pdev->vendor);
  1306. *(__le16 *)&vconfig[PCI_DEVICE_ID] = cpu_to_le16(pdev->device);
  1307. }
  1308. if (!IS_ENABLED(CONFIG_VFIO_PCI_INTX) || vdev->nointx)
  1309. vconfig[PCI_INTERRUPT_PIN] = 0;
  1310. ret = vfio_cap_init(vdev);
  1311. if (ret)
  1312. goto out;
  1313. ret = vfio_ecap_init(vdev);
  1314. if (ret)
  1315. goto out;
  1316. return 0;
  1317. out:
  1318. kfree(map);
  1319. vdev->pci_config_map = NULL;
  1320. kfree(vconfig);
  1321. vdev->vconfig = NULL;
  1322. return pcibios_err_to_errno(ret);
  1323. }
  1324. void vfio_config_free(struct vfio_pci_device *vdev)
  1325. {
  1326. kfree(vdev->vconfig);
  1327. vdev->vconfig = NULL;
  1328. kfree(vdev->pci_config_map);
  1329. vdev->pci_config_map = NULL;
  1330. kfree(vdev->msi_perm);
  1331. vdev->msi_perm = NULL;
  1332. }
  1333. /*
  1334. * Find the remaining number of bytes in a dword that match the given
  1335. * position. Stop at either the end of the capability or the dword boundary.
  1336. */
  1337. static size_t vfio_pci_cap_remaining_dword(struct vfio_pci_device *vdev,
  1338. loff_t pos)
  1339. {
  1340. u8 cap = vdev->pci_config_map[pos];
  1341. size_t i;
  1342. for (i = 1; (pos + i) % 4 && vdev->pci_config_map[pos + i] == cap; i++)
  1343. /* nop */;
  1344. return i;
  1345. }
  1346. static ssize_t vfio_config_do_rw(struct vfio_pci_device *vdev, char __user *buf,
  1347. size_t count, loff_t *ppos, bool iswrite)
  1348. {
  1349. struct pci_dev *pdev = vdev->pdev;
  1350. struct perm_bits *perm;
  1351. __le32 val = 0;
  1352. int cap_start = 0, offset;
  1353. u8 cap_id;
  1354. ssize_t ret;
  1355. if (*ppos < 0 || *ppos >= pdev->cfg_size ||
  1356. *ppos + count > pdev->cfg_size)
  1357. return -EFAULT;
  1358. /*
  1359. * Chop accesses into aligned chunks containing no more than a
  1360. * single capability. Caller increments to the next chunk.
  1361. */
  1362. count = min(count, vfio_pci_cap_remaining_dword(vdev, *ppos));
  1363. if (count >= 4 && !(*ppos % 4))
  1364. count = 4;
  1365. else if (count >= 2 && !(*ppos % 2))
  1366. count = 2;
  1367. else
  1368. count = 1;
  1369. ret = count;
  1370. cap_id = vdev->pci_config_map[*ppos];
  1371. if (cap_id == PCI_CAP_ID_INVALID) {
  1372. perm = &unassigned_perms;
  1373. cap_start = *ppos;
  1374. } else if (cap_id == PCI_CAP_ID_INVALID_VIRT) {
  1375. perm = &virt_perms;
  1376. cap_start = *ppos;
  1377. } else {
  1378. if (*ppos >= PCI_CFG_SPACE_SIZE) {
  1379. WARN_ON(cap_id > PCI_EXT_CAP_ID_MAX);
  1380. perm = &ecap_perms[cap_id];
  1381. cap_start = vfio_find_cap_start(vdev, *ppos);
  1382. } else {
  1383. WARN_ON(cap_id > PCI_CAP_ID_MAX);
  1384. perm = &cap_perms[cap_id];
  1385. if (cap_id == PCI_CAP_ID_MSI)
  1386. perm = vdev->msi_perm;
  1387. if (cap_id > PCI_CAP_ID_BASIC)
  1388. cap_start = vfio_find_cap_start(vdev, *ppos);
  1389. }
  1390. }
  1391. WARN_ON(!cap_start && cap_id != PCI_CAP_ID_BASIC);
  1392. WARN_ON(cap_start > *ppos);
  1393. offset = *ppos - cap_start;
  1394. if (iswrite) {
  1395. if (!perm->writefn)
  1396. return ret;
  1397. if (copy_from_user(&val, buf, count))
  1398. return -EFAULT;
  1399. ret = perm->writefn(vdev, *ppos, count, perm, offset, val);
  1400. } else {
  1401. if (perm->readfn) {
  1402. ret = perm->readfn(vdev, *ppos, count,
  1403. perm, offset, &val);
  1404. if (ret < 0)
  1405. return ret;
  1406. }
  1407. if (copy_to_user(buf, &val, count))
  1408. return -EFAULT;
  1409. }
  1410. return ret;
  1411. }
  1412. ssize_t vfio_pci_config_rw(struct vfio_pci_device *vdev, char __user *buf,
  1413. size_t count, loff_t *ppos, bool iswrite)
  1414. {
  1415. size_t done = 0;
  1416. int ret = 0;
  1417. loff_t pos = *ppos;
  1418. pos &= VFIO_PCI_OFFSET_MASK;
  1419. while (count) {
  1420. ret = vfio_config_do_rw(vdev, buf, count, &pos, iswrite);
  1421. if (ret < 0)
  1422. return ret;
  1423. count -= ret;
  1424. done += ret;
  1425. buf += ret;
  1426. pos += ret;
  1427. }
  1428. *ppos += done;
  1429. return done;
  1430. }