vfio_pci_config.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  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. int i;
  352. if (pdev->is_virtfn)
  353. return;
  354. pr_info("%s: %s reset recovery - restoring bars\n",
  355. __func__, dev_name(&pdev->dev));
  356. for (i = PCI_BASE_ADDRESS_0; i <= PCI_BASE_ADDRESS_5; i += 4, rbar++)
  357. pci_user_write_config_dword(pdev, i, *rbar);
  358. pci_user_write_config_dword(pdev, PCI_ROM_ADDRESS, *rbar);
  359. }
  360. static __le32 vfio_generate_bar_flags(struct pci_dev *pdev, int bar)
  361. {
  362. unsigned long flags = pci_resource_flags(pdev, bar);
  363. u32 val;
  364. if (flags & IORESOURCE_IO)
  365. return cpu_to_le32(PCI_BASE_ADDRESS_SPACE_IO);
  366. val = PCI_BASE_ADDRESS_SPACE_MEMORY;
  367. if (flags & IORESOURCE_PREFETCH)
  368. val |= PCI_BASE_ADDRESS_MEM_PREFETCH;
  369. if (flags & IORESOURCE_MEM_64)
  370. val |= PCI_BASE_ADDRESS_MEM_TYPE_64;
  371. return cpu_to_le32(val);
  372. }
  373. /*
  374. * Pretend we're hardware and tweak the values of the *virtual* PCI BARs
  375. * to reflect the hardware capabilities. This implements BAR sizing.
  376. */
  377. static void vfio_bar_fixup(struct vfio_pci_device *vdev)
  378. {
  379. struct pci_dev *pdev = vdev->pdev;
  380. int i;
  381. __le32 *bar;
  382. u64 mask;
  383. bar = (__le32 *)&vdev->vconfig[PCI_BASE_ADDRESS_0];
  384. for (i = PCI_STD_RESOURCES; i <= PCI_STD_RESOURCE_END; i++, bar++) {
  385. if (!pci_resource_start(pdev, i)) {
  386. *bar = 0; /* Unmapped by host = unimplemented to user */
  387. continue;
  388. }
  389. mask = ~(pci_resource_len(pdev, i) - 1);
  390. *bar &= cpu_to_le32((u32)mask);
  391. *bar |= vfio_generate_bar_flags(pdev, i);
  392. if (*bar & cpu_to_le32(PCI_BASE_ADDRESS_MEM_TYPE_64)) {
  393. bar++;
  394. *bar &= cpu_to_le32((u32)(mask >> 32));
  395. i++;
  396. }
  397. }
  398. bar = (__le32 *)&vdev->vconfig[PCI_ROM_ADDRESS];
  399. /*
  400. * NB. REGION_INFO will have reported zero size if we weren't able
  401. * to read the ROM, but we still return the actual BAR size here if
  402. * it exists (or the shadow ROM space).
  403. */
  404. if (pci_resource_start(pdev, PCI_ROM_RESOURCE)) {
  405. mask = ~(pci_resource_len(pdev, PCI_ROM_RESOURCE) - 1);
  406. mask |= PCI_ROM_ADDRESS_ENABLE;
  407. *bar &= cpu_to_le32((u32)mask);
  408. } else if (pdev->resource[PCI_ROM_RESOURCE].flags &
  409. IORESOURCE_ROM_SHADOW) {
  410. mask = ~(0x20000 - 1);
  411. mask |= PCI_ROM_ADDRESS_ENABLE;
  412. *bar &= cpu_to_le32((u32)mask);
  413. } else
  414. *bar = 0;
  415. vdev->bardirty = false;
  416. }
  417. static int vfio_basic_config_read(struct vfio_pci_device *vdev, int pos,
  418. int count, struct perm_bits *perm,
  419. int offset, __le32 *val)
  420. {
  421. if (is_bar(offset)) /* pos == offset for basic config */
  422. vfio_bar_fixup(vdev);
  423. count = vfio_default_config_read(vdev, pos, count, perm, offset, val);
  424. /* Mask in virtual memory enable for SR-IOV devices */
  425. if (offset == PCI_COMMAND && vdev->pdev->is_virtfn) {
  426. u16 cmd = le16_to_cpu(*(__le16 *)&vdev->vconfig[PCI_COMMAND]);
  427. u32 tmp_val = le32_to_cpu(*val);
  428. tmp_val |= cmd & PCI_COMMAND_MEMORY;
  429. *val = cpu_to_le32(tmp_val);
  430. }
  431. return count;
  432. }
  433. static int vfio_basic_config_write(struct vfio_pci_device *vdev, int pos,
  434. int count, struct perm_bits *perm,
  435. int offset, __le32 val)
  436. {
  437. struct pci_dev *pdev = vdev->pdev;
  438. __le16 *virt_cmd;
  439. u16 new_cmd = 0;
  440. int ret;
  441. virt_cmd = (__le16 *)&vdev->vconfig[PCI_COMMAND];
  442. if (offset == PCI_COMMAND) {
  443. bool phys_mem, virt_mem, new_mem, phys_io, virt_io, new_io;
  444. u16 phys_cmd;
  445. ret = pci_user_read_config_word(pdev, PCI_COMMAND, &phys_cmd);
  446. if (ret)
  447. return ret;
  448. new_cmd = le32_to_cpu(val);
  449. phys_mem = !!(phys_cmd & PCI_COMMAND_MEMORY);
  450. virt_mem = !!(le16_to_cpu(*virt_cmd) & PCI_COMMAND_MEMORY);
  451. new_mem = !!(new_cmd & PCI_COMMAND_MEMORY);
  452. phys_io = !!(phys_cmd & PCI_COMMAND_IO);
  453. virt_io = !!(le16_to_cpu(*virt_cmd) & PCI_COMMAND_IO);
  454. new_io = !!(new_cmd & PCI_COMMAND_IO);
  455. /*
  456. * If the user is writing mem/io enable (new_mem/io) and we
  457. * think it's already enabled (virt_mem/io), but the hardware
  458. * shows it disabled (phys_mem/io, then the device has
  459. * undergone some kind of backdoor reset and needs to be
  460. * restored before we allow it to enable the bars.
  461. * SR-IOV devices will trigger this, but we catch them later
  462. */
  463. if ((new_mem && virt_mem && !phys_mem) ||
  464. (new_io && virt_io && !phys_io))
  465. vfio_bar_restore(vdev);
  466. }
  467. count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
  468. if (count < 0)
  469. return count;
  470. /*
  471. * Save current memory/io enable bits in vconfig to allow for
  472. * the test above next time.
  473. */
  474. if (offset == PCI_COMMAND) {
  475. u16 mask = PCI_COMMAND_MEMORY | PCI_COMMAND_IO;
  476. *virt_cmd &= cpu_to_le16(~mask);
  477. *virt_cmd |= cpu_to_le16(new_cmd & mask);
  478. }
  479. /* Emulate INTx disable */
  480. if (offset >= PCI_COMMAND && offset <= PCI_COMMAND + 1) {
  481. bool virt_intx_disable;
  482. virt_intx_disable = !!(le16_to_cpu(*virt_cmd) &
  483. PCI_COMMAND_INTX_DISABLE);
  484. if (virt_intx_disable && !vdev->virq_disabled) {
  485. vdev->virq_disabled = true;
  486. vfio_pci_intx_mask(vdev);
  487. } else if (!virt_intx_disable && vdev->virq_disabled) {
  488. vdev->virq_disabled = false;
  489. vfio_pci_intx_unmask(vdev);
  490. }
  491. }
  492. if (is_bar(offset))
  493. vdev->bardirty = true;
  494. return count;
  495. }
  496. /* Permissions for the Basic PCI Header */
  497. static int __init init_pci_cap_basic_perm(struct perm_bits *perm)
  498. {
  499. if (alloc_perm_bits(perm, PCI_STD_HEADER_SIZEOF))
  500. return -ENOMEM;
  501. perm->readfn = vfio_basic_config_read;
  502. perm->writefn = vfio_basic_config_write;
  503. /* Virtualized for SR-IOV functions, which just have FFFF */
  504. p_setw(perm, PCI_VENDOR_ID, (u16)ALL_VIRT, NO_WRITE);
  505. p_setw(perm, PCI_DEVICE_ID, (u16)ALL_VIRT, NO_WRITE);
  506. /*
  507. * Virtualize INTx disable, we use it internally for interrupt
  508. * control and can emulate it for non-PCI 2.3 devices.
  509. */
  510. p_setw(perm, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE, (u16)ALL_WRITE);
  511. /* Virtualize capability list, we might want to skip/disable */
  512. p_setw(perm, PCI_STATUS, PCI_STATUS_CAP_LIST, NO_WRITE);
  513. /* No harm to write */
  514. p_setb(perm, PCI_CACHE_LINE_SIZE, NO_VIRT, (u8)ALL_WRITE);
  515. p_setb(perm, PCI_LATENCY_TIMER, NO_VIRT, (u8)ALL_WRITE);
  516. p_setb(perm, PCI_BIST, NO_VIRT, (u8)ALL_WRITE);
  517. /* Virtualize all bars, can't touch the real ones */
  518. p_setd(perm, PCI_BASE_ADDRESS_0, ALL_VIRT, ALL_WRITE);
  519. p_setd(perm, PCI_BASE_ADDRESS_1, ALL_VIRT, ALL_WRITE);
  520. p_setd(perm, PCI_BASE_ADDRESS_2, ALL_VIRT, ALL_WRITE);
  521. p_setd(perm, PCI_BASE_ADDRESS_3, ALL_VIRT, ALL_WRITE);
  522. p_setd(perm, PCI_BASE_ADDRESS_4, ALL_VIRT, ALL_WRITE);
  523. p_setd(perm, PCI_BASE_ADDRESS_5, ALL_VIRT, ALL_WRITE);
  524. p_setd(perm, PCI_ROM_ADDRESS, ALL_VIRT, ALL_WRITE);
  525. /* Allow us to adjust capability chain */
  526. p_setb(perm, PCI_CAPABILITY_LIST, (u8)ALL_VIRT, NO_WRITE);
  527. /* Sometimes used by sw, just virtualize */
  528. p_setb(perm, PCI_INTERRUPT_LINE, (u8)ALL_VIRT, (u8)ALL_WRITE);
  529. /* Virtualize interrupt pin to allow hiding INTx */
  530. p_setb(perm, PCI_INTERRUPT_PIN, (u8)ALL_VIRT, (u8)NO_WRITE);
  531. return 0;
  532. }
  533. static int vfio_pm_config_write(struct vfio_pci_device *vdev, int pos,
  534. int count, struct perm_bits *perm,
  535. int offset, __le32 val)
  536. {
  537. count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
  538. if (count < 0)
  539. return count;
  540. if (offset == PCI_PM_CTRL) {
  541. pci_power_t state;
  542. switch (le32_to_cpu(val) & PCI_PM_CTRL_STATE_MASK) {
  543. case 0:
  544. state = PCI_D0;
  545. break;
  546. case 1:
  547. state = PCI_D1;
  548. break;
  549. case 2:
  550. state = PCI_D2;
  551. break;
  552. case 3:
  553. state = PCI_D3hot;
  554. break;
  555. }
  556. pci_set_power_state(vdev->pdev, state);
  557. }
  558. return count;
  559. }
  560. /* Permissions for the Power Management capability */
  561. static int __init init_pci_cap_pm_perm(struct perm_bits *perm)
  562. {
  563. if (alloc_perm_bits(perm, pci_cap_length[PCI_CAP_ID_PM]))
  564. return -ENOMEM;
  565. perm->writefn = vfio_pm_config_write;
  566. /*
  567. * We always virtualize the next field so we can remove
  568. * capabilities from the chain if we want to.
  569. */
  570. p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
  571. /*
  572. * Power management is defined *per function*, so we can let
  573. * the user change power state, but we trap and initiate the
  574. * change ourselves, so the state bits are read-only.
  575. */
  576. p_setd(perm, PCI_PM_CTRL, NO_VIRT, ~PCI_PM_CTRL_STATE_MASK);
  577. return 0;
  578. }
  579. static int vfio_vpd_config_write(struct vfio_pci_device *vdev, int pos,
  580. int count, struct perm_bits *perm,
  581. int offset, __le32 val)
  582. {
  583. struct pci_dev *pdev = vdev->pdev;
  584. __le16 *paddr = (__le16 *)(vdev->vconfig + pos - offset + PCI_VPD_ADDR);
  585. __le32 *pdata = (__le32 *)(vdev->vconfig + pos - offset + PCI_VPD_DATA);
  586. u16 addr;
  587. u32 data;
  588. /*
  589. * Write through to emulation. If the write includes the upper byte
  590. * of PCI_VPD_ADDR, then the PCI_VPD_ADDR_F bit is written and we
  591. * have work to do.
  592. */
  593. count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
  594. if (count < 0 || offset > PCI_VPD_ADDR + 1 ||
  595. offset + count <= PCI_VPD_ADDR + 1)
  596. return count;
  597. addr = le16_to_cpu(*paddr);
  598. if (addr & PCI_VPD_ADDR_F) {
  599. data = le32_to_cpu(*pdata);
  600. if (pci_write_vpd(pdev, addr & ~PCI_VPD_ADDR_F, 4, &data) != 4)
  601. return count;
  602. } else {
  603. if (pci_read_vpd(pdev, addr, 4, &data) != 4)
  604. return count;
  605. *pdata = cpu_to_le32(data);
  606. }
  607. /*
  608. * Toggle PCI_VPD_ADDR_F in the emulated PCI_VPD_ADDR register to
  609. * signal completion. If an error occurs above, we assume that not
  610. * toggling this bit will induce a driver timeout.
  611. */
  612. addr ^= PCI_VPD_ADDR_F;
  613. *paddr = cpu_to_le16(addr);
  614. return count;
  615. }
  616. /* Permissions for Vital Product Data capability */
  617. static int __init init_pci_cap_vpd_perm(struct perm_bits *perm)
  618. {
  619. if (alloc_perm_bits(perm, pci_cap_length[PCI_CAP_ID_VPD]))
  620. return -ENOMEM;
  621. perm->writefn = vfio_vpd_config_write;
  622. /*
  623. * We always virtualize the next field so we can remove
  624. * capabilities from the chain if we want to.
  625. */
  626. p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
  627. /*
  628. * Both the address and data registers are virtualized to
  629. * enable access through the pci_vpd_read/write functions
  630. */
  631. p_setw(perm, PCI_VPD_ADDR, (u16)ALL_VIRT, (u16)ALL_WRITE);
  632. p_setd(perm, PCI_VPD_DATA, ALL_VIRT, ALL_WRITE);
  633. return 0;
  634. }
  635. /* Permissions for PCI-X capability */
  636. static int __init init_pci_cap_pcix_perm(struct perm_bits *perm)
  637. {
  638. /* Alloc 24, but only 8 are used in v0 */
  639. if (alloc_perm_bits(perm, PCI_CAP_PCIX_SIZEOF_V2))
  640. return -ENOMEM;
  641. p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
  642. p_setw(perm, PCI_X_CMD, NO_VIRT, (u16)ALL_WRITE);
  643. p_setd(perm, PCI_X_ECC_CSR, NO_VIRT, ALL_WRITE);
  644. return 0;
  645. }
  646. /* Permissions for PCI Express capability */
  647. static int __init init_pci_cap_exp_perm(struct perm_bits *perm)
  648. {
  649. /* Alloc larger of two possible sizes */
  650. if (alloc_perm_bits(perm, PCI_CAP_EXP_ENDPOINT_SIZEOF_V2))
  651. return -ENOMEM;
  652. p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
  653. /*
  654. * Allow writes to device control fields (includes FLR!)
  655. * but not to devctl_phantom which could confuse IOMMU
  656. * or to the ARI bit in devctl2 which is set at probe time
  657. */
  658. p_setw(perm, PCI_EXP_DEVCTL, NO_VIRT, ~PCI_EXP_DEVCTL_PHANTOM);
  659. p_setw(perm, PCI_EXP_DEVCTL2, NO_VIRT, ~PCI_EXP_DEVCTL2_ARI);
  660. return 0;
  661. }
  662. /* Permissions for Advanced Function capability */
  663. static int __init init_pci_cap_af_perm(struct perm_bits *perm)
  664. {
  665. if (alloc_perm_bits(perm, pci_cap_length[PCI_CAP_ID_AF]))
  666. return -ENOMEM;
  667. p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
  668. p_setb(perm, PCI_AF_CTRL, NO_VIRT, PCI_AF_CTRL_FLR);
  669. return 0;
  670. }
  671. /* Permissions for Advanced Error Reporting extended capability */
  672. static int __init init_pci_ext_cap_err_perm(struct perm_bits *perm)
  673. {
  674. u32 mask;
  675. if (alloc_perm_bits(perm, pci_ext_cap_length[PCI_EXT_CAP_ID_ERR]))
  676. return -ENOMEM;
  677. /*
  678. * Virtualize the first dword of all express capabilities
  679. * because it includes the next pointer. This lets us later
  680. * remove capabilities from the chain if we need to.
  681. */
  682. p_setd(perm, 0, ALL_VIRT, NO_WRITE);
  683. /* Writable bits mask */
  684. mask = PCI_ERR_UNC_UND | /* Undefined */
  685. PCI_ERR_UNC_DLP | /* Data Link Protocol */
  686. PCI_ERR_UNC_SURPDN | /* Surprise Down */
  687. PCI_ERR_UNC_POISON_TLP | /* Poisoned TLP */
  688. PCI_ERR_UNC_FCP | /* Flow Control Protocol */
  689. PCI_ERR_UNC_COMP_TIME | /* Completion Timeout */
  690. PCI_ERR_UNC_COMP_ABORT | /* Completer Abort */
  691. PCI_ERR_UNC_UNX_COMP | /* Unexpected Completion */
  692. PCI_ERR_UNC_RX_OVER | /* Receiver Overflow */
  693. PCI_ERR_UNC_MALF_TLP | /* Malformed TLP */
  694. PCI_ERR_UNC_ECRC | /* ECRC Error Status */
  695. PCI_ERR_UNC_UNSUP | /* Unsupported Request */
  696. PCI_ERR_UNC_ACSV | /* ACS Violation */
  697. PCI_ERR_UNC_INTN | /* internal error */
  698. PCI_ERR_UNC_MCBTLP | /* MC blocked TLP */
  699. PCI_ERR_UNC_ATOMEG | /* Atomic egress blocked */
  700. PCI_ERR_UNC_TLPPRE; /* TLP prefix blocked */
  701. p_setd(perm, PCI_ERR_UNCOR_STATUS, NO_VIRT, mask);
  702. p_setd(perm, PCI_ERR_UNCOR_MASK, NO_VIRT, mask);
  703. p_setd(perm, PCI_ERR_UNCOR_SEVER, NO_VIRT, mask);
  704. mask = PCI_ERR_COR_RCVR | /* Receiver Error Status */
  705. PCI_ERR_COR_BAD_TLP | /* Bad TLP Status */
  706. PCI_ERR_COR_BAD_DLLP | /* Bad DLLP Status */
  707. PCI_ERR_COR_REP_ROLL | /* REPLAY_NUM Rollover */
  708. PCI_ERR_COR_REP_TIMER | /* Replay Timer Timeout */
  709. PCI_ERR_COR_ADV_NFAT | /* Advisory Non-Fatal */
  710. PCI_ERR_COR_INTERNAL | /* Corrected Internal */
  711. PCI_ERR_COR_LOG_OVER; /* Header Log Overflow */
  712. p_setd(perm, PCI_ERR_COR_STATUS, NO_VIRT, mask);
  713. p_setd(perm, PCI_ERR_COR_MASK, NO_VIRT, mask);
  714. mask = PCI_ERR_CAP_ECRC_GENE | /* ECRC Generation Enable */
  715. PCI_ERR_CAP_ECRC_CHKE; /* ECRC Check Enable */
  716. p_setd(perm, PCI_ERR_CAP, NO_VIRT, mask);
  717. return 0;
  718. }
  719. /* Permissions for Power Budgeting extended capability */
  720. static int __init init_pci_ext_cap_pwr_perm(struct perm_bits *perm)
  721. {
  722. if (alloc_perm_bits(perm, pci_ext_cap_length[PCI_EXT_CAP_ID_PWR]))
  723. return -ENOMEM;
  724. p_setd(perm, 0, ALL_VIRT, NO_WRITE);
  725. /* Writing the data selector is OK, the info is still read-only */
  726. p_setb(perm, PCI_PWR_DATA, NO_VIRT, (u8)ALL_WRITE);
  727. return 0;
  728. }
  729. /*
  730. * Initialize the shared permission tables
  731. */
  732. void vfio_pci_uninit_perm_bits(void)
  733. {
  734. free_perm_bits(&cap_perms[PCI_CAP_ID_BASIC]);
  735. free_perm_bits(&cap_perms[PCI_CAP_ID_PM]);
  736. free_perm_bits(&cap_perms[PCI_CAP_ID_VPD]);
  737. free_perm_bits(&cap_perms[PCI_CAP_ID_PCIX]);
  738. free_perm_bits(&cap_perms[PCI_CAP_ID_EXP]);
  739. free_perm_bits(&cap_perms[PCI_CAP_ID_AF]);
  740. free_perm_bits(&ecap_perms[PCI_EXT_CAP_ID_ERR]);
  741. free_perm_bits(&ecap_perms[PCI_EXT_CAP_ID_PWR]);
  742. }
  743. int __init vfio_pci_init_perm_bits(void)
  744. {
  745. int ret;
  746. /* Basic config space */
  747. ret = init_pci_cap_basic_perm(&cap_perms[PCI_CAP_ID_BASIC]);
  748. /* Capabilities */
  749. ret |= init_pci_cap_pm_perm(&cap_perms[PCI_CAP_ID_PM]);
  750. ret |= init_pci_cap_vpd_perm(&cap_perms[PCI_CAP_ID_VPD]);
  751. ret |= init_pci_cap_pcix_perm(&cap_perms[PCI_CAP_ID_PCIX]);
  752. cap_perms[PCI_CAP_ID_VNDR].writefn = vfio_raw_config_write;
  753. ret |= init_pci_cap_exp_perm(&cap_perms[PCI_CAP_ID_EXP]);
  754. ret |= init_pci_cap_af_perm(&cap_perms[PCI_CAP_ID_AF]);
  755. /* Extended capabilities */
  756. ret |= init_pci_ext_cap_err_perm(&ecap_perms[PCI_EXT_CAP_ID_ERR]);
  757. ret |= init_pci_ext_cap_pwr_perm(&ecap_perms[PCI_EXT_CAP_ID_PWR]);
  758. ecap_perms[PCI_EXT_CAP_ID_VNDR].writefn = vfio_raw_config_write;
  759. if (ret)
  760. vfio_pci_uninit_perm_bits();
  761. return ret;
  762. }
  763. static int vfio_find_cap_start(struct vfio_pci_device *vdev, int pos)
  764. {
  765. u8 cap;
  766. int base = (pos >= PCI_CFG_SPACE_SIZE) ? PCI_CFG_SPACE_SIZE :
  767. PCI_STD_HEADER_SIZEOF;
  768. cap = vdev->pci_config_map[pos];
  769. if (cap == PCI_CAP_ID_BASIC)
  770. return 0;
  771. /* XXX Can we have to abutting capabilities of the same type? */
  772. while (pos - 1 >= base && vdev->pci_config_map[pos - 1] == cap)
  773. pos--;
  774. return pos;
  775. }
  776. static int vfio_msi_config_read(struct vfio_pci_device *vdev, int pos,
  777. int count, struct perm_bits *perm,
  778. int offset, __le32 *val)
  779. {
  780. /* Update max available queue size from msi_qmax */
  781. if (offset <= PCI_MSI_FLAGS && offset + count >= PCI_MSI_FLAGS) {
  782. __le16 *flags;
  783. int start;
  784. start = vfio_find_cap_start(vdev, pos);
  785. flags = (__le16 *)&vdev->vconfig[start];
  786. *flags &= cpu_to_le16(~PCI_MSI_FLAGS_QMASK);
  787. *flags |= cpu_to_le16(vdev->msi_qmax << 1);
  788. }
  789. return vfio_default_config_read(vdev, pos, count, perm, offset, val);
  790. }
  791. static int vfio_msi_config_write(struct vfio_pci_device *vdev, int pos,
  792. int count, struct perm_bits *perm,
  793. int offset, __le32 val)
  794. {
  795. count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
  796. if (count < 0)
  797. return count;
  798. /* Fixup and write configured queue size and enable to hardware */
  799. if (offset <= PCI_MSI_FLAGS && offset + count >= PCI_MSI_FLAGS) {
  800. __le16 *pflags;
  801. u16 flags;
  802. int start, ret;
  803. start = vfio_find_cap_start(vdev, pos);
  804. pflags = (__le16 *)&vdev->vconfig[start + PCI_MSI_FLAGS];
  805. flags = le16_to_cpu(*pflags);
  806. /* MSI is enabled via ioctl */
  807. if (!is_msi(vdev))
  808. flags &= ~PCI_MSI_FLAGS_ENABLE;
  809. /* Check queue size */
  810. if ((flags & PCI_MSI_FLAGS_QSIZE) >> 4 > vdev->msi_qmax) {
  811. flags &= ~PCI_MSI_FLAGS_QSIZE;
  812. flags |= vdev->msi_qmax << 4;
  813. }
  814. /* Write back to virt and to hardware */
  815. *pflags = cpu_to_le16(flags);
  816. ret = pci_user_write_config_word(vdev->pdev,
  817. start + PCI_MSI_FLAGS,
  818. flags);
  819. if (ret)
  820. return pcibios_err_to_errno(ret);
  821. }
  822. return count;
  823. }
  824. /*
  825. * MSI determination is per-device, so this routine gets used beyond
  826. * initialization time. Don't add __init
  827. */
  828. static int init_pci_cap_msi_perm(struct perm_bits *perm, int len, u16 flags)
  829. {
  830. if (alloc_perm_bits(perm, len))
  831. return -ENOMEM;
  832. perm->readfn = vfio_msi_config_read;
  833. perm->writefn = vfio_msi_config_write;
  834. p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
  835. /*
  836. * The upper byte of the control register is reserved,
  837. * just setup the lower byte.
  838. */
  839. p_setb(perm, PCI_MSI_FLAGS, (u8)ALL_VIRT, (u8)ALL_WRITE);
  840. p_setd(perm, PCI_MSI_ADDRESS_LO, ALL_VIRT, ALL_WRITE);
  841. if (flags & PCI_MSI_FLAGS_64BIT) {
  842. p_setd(perm, PCI_MSI_ADDRESS_HI, ALL_VIRT, ALL_WRITE);
  843. p_setw(perm, PCI_MSI_DATA_64, (u16)ALL_VIRT, (u16)ALL_WRITE);
  844. if (flags & PCI_MSI_FLAGS_MASKBIT) {
  845. p_setd(perm, PCI_MSI_MASK_64, NO_VIRT, ALL_WRITE);
  846. p_setd(perm, PCI_MSI_PENDING_64, NO_VIRT, ALL_WRITE);
  847. }
  848. } else {
  849. p_setw(perm, PCI_MSI_DATA_32, (u16)ALL_VIRT, (u16)ALL_WRITE);
  850. if (flags & PCI_MSI_FLAGS_MASKBIT) {
  851. p_setd(perm, PCI_MSI_MASK_32, NO_VIRT, ALL_WRITE);
  852. p_setd(perm, PCI_MSI_PENDING_32, NO_VIRT, ALL_WRITE);
  853. }
  854. }
  855. return 0;
  856. }
  857. /* Determine MSI CAP field length; initialize msi_perms on 1st call per vdev */
  858. static int vfio_msi_cap_len(struct vfio_pci_device *vdev, u8 pos)
  859. {
  860. struct pci_dev *pdev = vdev->pdev;
  861. int len, ret;
  862. u16 flags;
  863. ret = pci_read_config_word(pdev, pos + PCI_MSI_FLAGS, &flags);
  864. if (ret)
  865. return pcibios_err_to_errno(ret);
  866. len = 10; /* Minimum size */
  867. if (flags & PCI_MSI_FLAGS_64BIT)
  868. len += 4;
  869. if (flags & PCI_MSI_FLAGS_MASKBIT)
  870. len += 10;
  871. if (vdev->msi_perm)
  872. return len;
  873. vdev->msi_perm = kmalloc(sizeof(struct perm_bits), GFP_KERNEL);
  874. if (!vdev->msi_perm)
  875. return -ENOMEM;
  876. ret = init_pci_cap_msi_perm(vdev->msi_perm, len, flags);
  877. if (ret)
  878. return ret;
  879. return len;
  880. }
  881. /* Determine extended capability length for VC (2 & 9) and MFVC */
  882. static int vfio_vc_cap_len(struct vfio_pci_device *vdev, u16 pos)
  883. {
  884. struct pci_dev *pdev = vdev->pdev;
  885. u32 tmp;
  886. int ret, evcc, phases, vc_arb;
  887. int len = PCI_CAP_VC_BASE_SIZEOF;
  888. ret = pci_read_config_dword(pdev, pos + PCI_VC_PORT_CAP1, &tmp);
  889. if (ret)
  890. return pcibios_err_to_errno(ret);
  891. evcc = tmp & PCI_VC_CAP1_EVCC; /* extended vc count */
  892. ret = pci_read_config_dword(pdev, pos + PCI_VC_PORT_CAP2, &tmp);
  893. if (ret)
  894. return pcibios_err_to_errno(ret);
  895. if (tmp & PCI_VC_CAP2_128_PHASE)
  896. phases = 128;
  897. else if (tmp & PCI_VC_CAP2_64_PHASE)
  898. phases = 64;
  899. else if (tmp & PCI_VC_CAP2_32_PHASE)
  900. phases = 32;
  901. else
  902. phases = 0;
  903. vc_arb = phases * 4;
  904. /*
  905. * Port arbitration tables are root & switch only;
  906. * function arbitration tables are function 0 only.
  907. * In either case, we'll never let user write them so
  908. * we don't care how big they are
  909. */
  910. len += (1 + evcc) * PCI_CAP_VC_PER_VC_SIZEOF;
  911. if (vc_arb) {
  912. len = round_up(len, 16);
  913. len += vc_arb / 8;
  914. }
  915. return len;
  916. }
  917. static int vfio_cap_len(struct vfio_pci_device *vdev, u8 cap, u8 pos)
  918. {
  919. struct pci_dev *pdev = vdev->pdev;
  920. u32 dword;
  921. u16 word;
  922. u8 byte;
  923. int ret;
  924. switch (cap) {
  925. case PCI_CAP_ID_MSI:
  926. return vfio_msi_cap_len(vdev, pos);
  927. case PCI_CAP_ID_PCIX:
  928. ret = pci_read_config_word(pdev, pos + PCI_X_CMD, &word);
  929. if (ret)
  930. return pcibios_err_to_errno(ret);
  931. if (PCI_X_CMD_VERSION(word)) {
  932. /* Test for extended capabilities */
  933. pci_read_config_dword(pdev, PCI_CFG_SPACE_SIZE, &dword);
  934. vdev->extended_caps = (dword != 0);
  935. return PCI_CAP_PCIX_SIZEOF_V2;
  936. } else
  937. return PCI_CAP_PCIX_SIZEOF_V0;
  938. case PCI_CAP_ID_VNDR:
  939. /* length follows next field */
  940. ret = pci_read_config_byte(pdev, pos + PCI_CAP_FLAGS, &byte);
  941. if (ret)
  942. return pcibios_err_to_errno(ret);
  943. return byte;
  944. case PCI_CAP_ID_EXP:
  945. /* Test for extended capabilities */
  946. pci_read_config_dword(pdev, PCI_CFG_SPACE_SIZE, &dword);
  947. vdev->extended_caps = (dword != 0);
  948. /* length based on version */
  949. if ((pcie_caps_reg(pdev) & PCI_EXP_FLAGS_VERS) == 1)
  950. return PCI_CAP_EXP_ENDPOINT_SIZEOF_V1;
  951. else
  952. return PCI_CAP_EXP_ENDPOINT_SIZEOF_V2;
  953. case PCI_CAP_ID_HT:
  954. ret = pci_read_config_byte(pdev, pos + 3, &byte);
  955. if (ret)
  956. return pcibios_err_to_errno(ret);
  957. return (byte & HT_3BIT_CAP_MASK) ?
  958. HT_CAP_SIZEOF_SHORT : HT_CAP_SIZEOF_LONG;
  959. case PCI_CAP_ID_SATA:
  960. ret = pci_read_config_byte(pdev, pos + PCI_SATA_REGS, &byte);
  961. if (ret)
  962. return pcibios_err_to_errno(ret);
  963. byte &= PCI_SATA_REGS_MASK;
  964. if (byte == PCI_SATA_REGS_INLINE)
  965. return PCI_SATA_SIZEOF_LONG;
  966. else
  967. return PCI_SATA_SIZEOF_SHORT;
  968. default:
  969. pr_warn("%s: %s unknown length for pci cap 0x%x@0x%x\n",
  970. dev_name(&pdev->dev), __func__, cap, pos);
  971. }
  972. return 0;
  973. }
  974. static int vfio_ext_cap_len(struct vfio_pci_device *vdev, u16 ecap, u16 epos)
  975. {
  976. struct pci_dev *pdev = vdev->pdev;
  977. u8 byte;
  978. u32 dword;
  979. int ret;
  980. switch (ecap) {
  981. case PCI_EXT_CAP_ID_VNDR:
  982. ret = pci_read_config_dword(pdev, epos + PCI_VSEC_HDR, &dword);
  983. if (ret)
  984. return pcibios_err_to_errno(ret);
  985. return dword >> PCI_VSEC_HDR_LEN_SHIFT;
  986. case PCI_EXT_CAP_ID_VC:
  987. case PCI_EXT_CAP_ID_VC9:
  988. case PCI_EXT_CAP_ID_MFVC:
  989. return vfio_vc_cap_len(vdev, epos);
  990. case PCI_EXT_CAP_ID_ACS:
  991. ret = pci_read_config_byte(pdev, epos + PCI_ACS_CAP, &byte);
  992. if (ret)
  993. return pcibios_err_to_errno(ret);
  994. if (byte & PCI_ACS_EC) {
  995. int bits;
  996. ret = pci_read_config_byte(pdev,
  997. epos + PCI_ACS_EGRESS_BITS,
  998. &byte);
  999. if (ret)
  1000. return pcibios_err_to_errno(ret);
  1001. bits = byte ? round_up(byte, 32) : 256;
  1002. return 8 + (bits / 8);
  1003. }
  1004. return 8;
  1005. case PCI_EXT_CAP_ID_REBAR:
  1006. ret = pci_read_config_byte(pdev, epos + PCI_REBAR_CTRL, &byte);
  1007. if (ret)
  1008. return pcibios_err_to_errno(ret);
  1009. byte &= PCI_REBAR_CTRL_NBAR_MASK;
  1010. byte >>= PCI_REBAR_CTRL_NBAR_SHIFT;
  1011. return 4 + (byte * 8);
  1012. case PCI_EXT_CAP_ID_DPA:
  1013. ret = pci_read_config_byte(pdev, epos + PCI_DPA_CAP, &byte);
  1014. if (ret)
  1015. return pcibios_err_to_errno(ret);
  1016. byte &= PCI_DPA_CAP_SUBSTATE_MASK;
  1017. return PCI_DPA_BASE_SIZEOF + byte + 1;
  1018. case PCI_EXT_CAP_ID_TPH:
  1019. ret = pci_read_config_dword(pdev, epos + PCI_TPH_CAP, &dword);
  1020. if (ret)
  1021. return pcibios_err_to_errno(ret);
  1022. if ((dword & PCI_TPH_CAP_LOC_MASK) == PCI_TPH_LOC_CAP) {
  1023. int sts;
  1024. sts = dword & PCI_TPH_CAP_ST_MASK;
  1025. sts >>= PCI_TPH_CAP_ST_SHIFT;
  1026. return PCI_TPH_BASE_SIZEOF + (sts * 2) + 2;
  1027. }
  1028. return PCI_TPH_BASE_SIZEOF;
  1029. default:
  1030. pr_warn("%s: %s unknown length for pci ecap 0x%x@0x%x\n",
  1031. dev_name(&pdev->dev), __func__, ecap, epos);
  1032. }
  1033. return 0;
  1034. }
  1035. static int vfio_fill_vconfig_bytes(struct vfio_pci_device *vdev,
  1036. int offset, int size)
  1037. {
  1038. struct pci_dev *pdev = vdev->pdev;
  1039. int ret = 0;
  1040. /*
  1041. * We try to read physical config space in the largest chunks
  1042. * we can, assuming that all of the fields support dword access.
  1043. * pci_save_state() makes this same assumption and seems to do ok.
  1044. */
  1045. while (size) {
  1046. int filled;
  1047. if (size >= 4 && !(offset % 4)) {
  1048. __le32 *dwordp = (__le32 *)&vdev->vconfig[offset];
  1049. u32 dword;
  1050. ret = pci_read_config_dword(pdev, offset, &dword);
  1051. if (ret)
  1052. return ret;
  1053. *dwordp = cpu_to_le32(dword);
  1054. filled = 4;
  1055. } else if (size >= 2 && !(offset % 2)) {
  1056. __le16 *wordp = (__le16 *)&vdev->vconfig[offset];
  1057. u16 word;
  1058. ret = pci_read_config_word(pdev, offset, &word);
  1059. if (ret)
  1060. return ret;
  1061. *wordp = cpu_to_le16(word);
  1062. filled = 2;
  1063. } else {
  1064. u8 *byte = &vdev->vconfig[offset];
  1065. ret = pci_read_config_byte(pdev, offset, byte);
  1066. if (ret)
  1067. return ret;
  1068. filled = 1;
  1069. }
  1070. offset += filled;
  1071. size -= filled;
  1072. }
  1073. return ret;
  1074. }
  1075. static int vfio_cap_init(struct vfio_pci_device *vdev)
  1076. {
  1077. struct pci_dev *pdev = vdev->pdev;
  1078. u8 *map = vdev->pci_config_map;
  1079. u16 status;
  1080. u8 pos, *prev, cap;
  1081. int loops, ret, caps = 0;
  1082. /* Any capabilities? */
  1083. ret = pci_read_config_word(pdev, PCI_STATUS, &status);
  1084. if (ret)
  1085. return ret;
  1086. if (!(status & PCI_STATUS_CAP_LIST))
  1087. return 0; /* Done */
  1088. ret = pci_read_config_byte(pdev, PCI_CAPABILITY_LIST, &pos);
  1089. if (ret)
  1090. return ret;
  1091. /* Mark the previous position in case we want to skip a capability */
  1092. prev = &vdev->vconfig[PCI_CAPABILITY_LIST];
  1093. /* We can bound our loop, capabilities are dword aligned */
  1094. loops = (PCI_CFG_SPACE_SIZE - PCI_STD_HEADER_SIZEOF) / PCI_CAP_SIZEOF;
  1095. while (pos && loops--) {
  1096. u8 next;
  1097. int i, len = 0;
  1098. ret = pci_read_config_byte(pdev, pos, &cap);
  1099. if (ret)
  1100. return ret;
  1101. ret = pci_read_config_byte(pdev,
  1102. pos + PCI_CAP_LIST_NEXT, &next);
  1103. if (ret)
  1104. return ret;
  1105. if (cap <= PCI_CAP_ID_MAX) {
  1106. len = pci_cap_length[cap];
  1107. if (len == 0xFF) { /* Variable length */
  1108. len = vfio_cap_len(vdev, cap, pos);
  1109. if (len < 0)
  1110. return len;
  1111. }
  1112. }
  1113. if (!len) {
  1114. pr_info("%s: %s hiding cap 0x%x\n",
  1115. __func__, dev_name(&pdev->dev), cap);
  1116. *prev = next;
  1117. pos = next;
  1118. continue;
  1119. }
  1120. /* Sanity check, do we overlap other capabilities? */
  1121. for (i = 0; i < len; i++) {
  1122. if (likely(map[pos + i] == PCI_CAP_ID_INVALID))
  1123. continue;
  1124. pr_warn("%s: %s pci config conflict @0x%x, was cap 0x%x now cap 0x%x\n",
  1125. __func__, dev_name(&pdev->dev),
  1126. pos + i, map[pos + i], cap);
  1127. }
  1128. BUILD_BUG_ON(PCI_CAP_ID_MAX >= PCI_CAP_ID_INVALID_VIRT);
  1129. memset(map + pos, cap, len);
  1130. ret = vfio_fill_vconfig_bytes(vdev, pos, len);
  1131. if (ret)
  1132. return ret;
  1133. prev = &vdev->vconfig[pos + PCI_CAP_LIST_NEXT];
  1134. pos = next;
  1135. caps++;
  1136. }
  1137. /* If we didn't fill any capabilities, clear the status flag */
  1138. if (!caps) {
  1139. __le16 *vstatus = (__le16 *)&vdev->vconfig[PCI_STATUS];
  1140. *vstatus &= ~cpu_to_le16(PCI_STATUS_CAP_LIST);
  1141. }
  1142. return 0;
  1143. }
  1144. static int vfio_ecap_init(struct vfio_pci_device *vdev)
  1145. {
  1146. struct pci_dev *pdev = vdev->pdev;
  1147. u8 *map = vdev->pci_config_map;
  1148. u16 epos;
  1149. __le32 *prev = NULL;
  1150. int loops, ret, ecaps = 0;
  1151. if (!vdev->extended_caps)
  1152. return 0;
  1153. epos = PCI_CFG_SPACE_SIZE;
  1154. loops = (pdev->cfg_size - PCI_CFG_SPACE_SIZE) / PCI_CAP_SIZEOF;
  1155. while (loops-- && epos >= PCI_CFG_SPACE_SIZE) {
  1156. u32 header;
  1157. u16 ecap;
  1158. int i, len = 0;
  1159. bool hidden = false;
  1160. ret = pci_read_config_dword(pdev, epos, &header);
  1161. if (ret)
  1162. return ret;
  1163. ecap = PCI_EXT_CAP_ID(header);
  1164. if (ecap <= PCI_EXT_CAP_ID_MAX) {
  1165. len = pci_ext_cap_length[ecap];
  1166. if (len == 0xFF) {
  1167. len = vfio_ext_cap_len(vdev, ecap, epos);
  1168. if (len < 0)
  1169. return ret;
  1170. }
  1171. }
  1172. if (!len) {
  1173. pr_info("%s: %s hiding ecap 0x%x@0x%x\n",
  1174. __func__, dev_name(&pdev->dev), ecap, epos);
  1175. /* If not the first in the chain, we can skip over it */
  1176. if (prev) {
  1177. u32 val = epos = PCI_EXT_CAP_NEXT(header);
  1178. *prev &= cpu_to_le32(~(0xffcU << 20));
  1179. *prev |= cpu_to_le32(val << 20);
  1180. continue;
  1181. }
  1182. /*
  1183. * Otherwise, fill in a placeholder, the direct
  1184. * readfn will virtualize this automatically
  1185. */
  1186. len = PCI_CAP_SIZEOF;
  1187. hidden = true;
  1188. }
  1189. for (i = 0; i < len; i++) {
  1190. if (likely(map[epos + i] == PCI_CAP_ID_INVALID))
  1191. continue;
  1192. pr_warn("%s: %s pci config conflict @0x%x, was ecap 0x%x now ecap 0x%x\n",
  1193. __func__, dev_name(&pdev->dev),
  1194. epos + i, map[epos + i], ecap);
  1195. }
  1196. /*
  1197. * Even though ecap is 2 bytes, we're currently a long way
  1198. * from exceeding 1 byte capabilities. If we ever make it
  1199. * up to 0xFE we'll need to up this to a two-byte, byte map.
  1200. */
  1201. BUILD_BUG_ON(PCI_EXT_CAP_ID_MAX >= PCI_CAP_ID_INVALID_VIRT);
  1202. memset(map + epos, ecap, len);
  1203. ret = vfio_fill_vconfig_bytes(vdev, epos, len);
  1204. if (ret)
  1205. return ret;
  1206. /*
  1207. * If we're just using this capability to anchor the list,
  1208. * hide the real ID. Only count real ecaps. XXX PCI spec
  1209. * indicates to use cap id = 0, version = 0, next = 0 if
  1210. * ecaps are absent, hope users check all the way to next.
  1211. */
  1212. if (hidden)
  1213. *(__le32 *)&vdev->vconfig[epos] &=
  1214. cpu_to_le32((0xffcU << 20));
  1215. else
  1216. ecaps++;
  1217. prev = (__le32 *)&vdev->vconfig[epos];
  1218. epos = PCI_EXT_CAP_NEXT(header);
  1219. }
  1220. if (!ecaps)
  1221. *(u32 *)&vdev->vconfig[PCI_CFG_SPACE_SIZE] = 0;
  1222. return 0;
  1223. }
  1224. /*
  1225. * For each device we allocate a pci_config_map that indicates the
  1226. * capability occupying each dword and thus the struct perm_bits we
  1227. * use for read and write. We also allocate a virtualized config
  1228. * space which tracks reads and writes to bits that we emulate for
  1229. * the user. Initial values filled from device.
  1230. *
  1231. * Using shared stuct perm_bits between all vfio-pci devices saves
  1232. * us from allocating cfg_size buffers for virt and write for every
  1233. * device. We could remove vconfig and allocate individual buffers
  1234. * for each area requring emulated bits, but the array of pointers
  1235. * would be comparable in size (at least for standard config space).
  1236. */
  1237. int vfio_config_init(struct vfio_pci_device *vdev)
  1238. {
  1239. struct pci_dev *pdev = vdev->pdev;
  1240. u8 *map, *vconfig;
  1241. int ret;
  1242. /*
  1243. * Config space, caps and ecaps are all dword aligned, so we could
  1244. * use one byte per dword to record the type. However, there are
  1245. * no requiremenst on the length of a capability, so the gap between
  1246. * capabilities needs byte granularity.
  1247. */
  1248. map = kmalloc(pdev->cfg_size, GFP_KERNEL);
  1249. if (!map)
  1250. return -ENOMEM;
  1251. vconfig = kmalloc(pdev->cfg_size, GFP_KERNEL);
  1252. if (!vconfig) {
  1253. kfree(map);
  1254. return -ENOMEM;
  1255. }
  1256. vdev->pci_config_map = map;
  1257. vdev->vconfig = vconfig;
  1258. memset(map, PCI_CAP_ID_BASIC, PCI_STD_HEADER_SIZEOF);
  1259. memset(map + PCI_STD_HEADER_SIZEOF, PCI_CAP_ID_INVALID,
  1260. pdev->cfg_size - PCI_STD_HEADER_SIZEOF);
  1261. ret = vfio_fill_vconfig_bytes(vdev, 0, PCI_STD_HEADER_SIZEOF);
  1262. if (ret)
  1263. goto out;
  1264. vdev->bardirty = true;
  1265. /*
  1266. * XXX can we just pci_load_saved_state/pci_restore_state?
  1267. * may need to rebuild vconfig after that
  1268. */
  1269. /* For restore after reset */
  1270. vdev->rbar[0] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_0]);
  1271. vdev->rbar[1] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_1]);
  1272. vdev->rbar[2] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_2]);
  1273. vdev->rbar[3] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_3]);
  1274. vdev->rbar[4] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_4]);
  1275. vdev->rbar[5] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_5]);
  1276. vdev->rbar[6] = le32_to_cpu(*(__le32 *)&vconfig[PCI_ROM_ADDRESS]);
  1277. if (pdev->is_virtfn) {
  1278. *(__le16 *)&vconfig[PCI_VENDOR_ID] = cpu_to_le16(pdev->vendor);
  1279. *(__le16 *)&vconfig[PCI_DEVICE_ID] = cpu_to_le16(pdev->device);
  1280. }
  1281. if (!IS_ENABLED(CONFIG_VFIO_PCI_INTX))
  1282. vconfig[PCI_INTERRUPT_PIN] = 0;
  1283. ret = vfio_cap_init(vdev);
  1284. if (ret)
  1285. goto out;
  1286. ret = vfio_ecap_init(vdev);
  1287. if (ret)
  1288. goto out;
  1289. return 0;
  1290. out:
  1291. kfree(map);
  1292. vdev->pci_config_map = NULL;
  1293. kfree(vconfig);
  1294. vdev->vconfig = NULL;
  1295. return pcibios_err_to_errno(ret);
  1296. }
  1297. void vfio_config_free(struct vfio_pci_device *vdev)
  1298. {
  1299. kfree(vdev->vconfig);
  1300. vdev->vconfig = NULL;
  1301. kfree(vdev->pci_config_map);
  1302. vdev->pci_config_map = NULL;
  1303. kfree(vdev->msi_perm);
  1304. vdev->msi_perm = NULL;
  1305. }
  1306. /*
  1307. * Find the remaining number of bytes in a dword that match the given
  1308. * position. Stop at either the end of the capability or the dword boundary.
  1309. */
  1310. static size_t vfio_pci_cap_remaining_dword(struct vfio_pci_device *vdev,
  1311. loff_t pos)
  1312. {
  1313. u8 cap = vdev->pci_config_map[pos];
  1314. size_t i;
  1315. for (i = 1; (pos + i) % 4 && vdev->pci_config_map[pos + i] == cap; i++)
  1316. /* nop */;
  1317. return i;
  1318. }
  1319. static ssize_t vfio_config_do_rw(struct vfio_pci_device *vdev, char __user *buf,
  1320. size_t count, loff_t *ppos, bool iswrite)
  1321. {
  1322. struct pci_dev *pdev = vdev->pdev;
  1323. struct perm_bits *perm;
  1324. __le32 val = 0;
  1325. int cap_start = 0, offset;
  1326. u8 cap_id;
  1327. ssize_t ret;
  1328. if (*ppos < 0 || *ppos >= pdev->cfg_size ||
  1329. *ppos + count > pdev->cfg_size)
  1330. return -EFAULT;
  1331. /*
  1332. * Chop accesses into aligned chunks containing no more than a
  1333. * single capability. Caller increments to the next chunk.
  1334. */
  1335. count = min(count, vfio_pci_cap_remaining_dword(vdev, *ppos));
  1336. if (count >= 4 && !(*ppos % 4))
  1337. count = 4;
  1338. else if (count >= 2 && !(*ppos % 2))
  1339. count = 2;
  1340. else
  1341. count = 1;
  1342. ret = count;
  1343. cap_id = vdev->pci_config_map[*ppos];
  1344. if (cap_id == PCI_CAP_ID_INVALID) {
  1345. perm = &unassigned_perms;
  1346. cap_start = *ppos;
  1347. } else if (cap_id == PCI_CAP_ID_INVALID_VIRT) {
  1348. perm = &virt_perms;
  1349. cap_start = *ppos;
  1350. } else {
  1351. if (*ppos >= PCI_CFG_SPACE_SIZE) {
  1352. WARN_ON(cap_id > PCI_EXT_CAP_ID_MAX);
  1353. perm = &ecap_perms[cap_id];
  1354. cap_start = vfio_find_cap_start(vdev, *ppos);
  1355. } else {
  1356. WARN_ON(cap_id > PCI_CAP_ID_MAX);
  1357. perm = &cap_perms[cap_id];
  1358. if (cap_id == PCI_CAP_ID_MSI)
  1359. perm = vdev->msi_perm;
  1360. if (cap_id > PCI_CAP_ID_BASIC)
  1361. cap_start = vfio_find_cap_start(vdev, *ppos);
  1362. }
  1363. }
  1364. WARN_ON(!cap_start && cap_id != PCI_CAP_ID_BASIC);
  1365. WARN_ON(cap_start > *ppos);
  1366. offset = *ppos - cap_start;
  1367. if (iswrite) {
  1368. if (!perm->writefn)
  1369. return ret;
  1370. if (copy_from_user(&val, buf, count))
  1371. return -EFAULT;
  1372. ret = perm->writefn(vdev, *ppos, count, perm, offset, val);
  1373. } else {
  1374. if (perm->readfn) {
  1375. ret = perm->readfn(vdev, *ppos, count,
  1376. perm, offset, &val);
  1377. if (ret < 0)
  1378. return ret;
  1379. }
  1380. if (copy_to_user(buf, &val, count))
  1381. return -EFAULT;
  1382. }
  1383. return ret;
  1384. }
  1385. ssize_t vfio_pci_config_rw(struct vfio_pci_device *vdev, char __user *buf,
  1386. size_t count, loff_t *ppos, bool iswrite)
  1387. {
  1388. size_t done = 0;
  1389. int ret = 0;
  1390. loff_t pos = *ppos;
  1391. pos &= VFIO_PCI_OFFSET_MASK;
  1392. while (count) {
  1393. ret = vfio_config_do_rw(vdev, buf, count, &pos, iswrite);
  1394. if (ret < 0)
  1395. return ret;
  1396. count -= ret;
  1397. done += ret;
  1398. buf += ret;
  1399. pos += ret;
  1400. }
  1401. *ppos += done;
  1402. return done;
  1403. }