pci-hyperv.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * Author:
  6. * Jake Oshins <jakeo@microsoft.com>
  7. *
  8. * This driver acts as a paravirtual front-end for PCI Express root buses.
  9. * When a PCI Express function (either an entire device or an SR-IOV
  10. * Virtual Function) is being passed through to the VM, this driver exposes
  11. * a new bus to the guest VM. This is modeled as a root PCI bus because
  12. * no bridges are being exposed to the VM. In fact, with a "Generation 2"
  13. * VM within Hyper-V, there may seem to be no PCI bus at all in the VM
  14. * until a device as been exposed using this driver.
  15. *
  16. * Each root PCI bus has its own PCI domain, which is called "Segment" in
  17. * the PCI Firmware Specifications. Thus while each device passed through
  18. * to the VM using this front-end will appear at "device 0", the domain will
  19. * be unique. Typically, each bus will have one PCI function on it, though
  20. * this driver does support more than one.
  21. *
  22. * In order to map the interrupts from the device through to the guest VM,
  23. * this driver also implements an IRQ Domain, which handles interrupts (either
  24. * MSI or MSI-X) associated with the functions on the bus. As interrupts are
  25. * set up, torn down, or reaffined, this driver communicates with the
  26. * underlying hypervisor to adjust the mappings in the I/O MMU so that each
  27. * interrupt will be delivered to the correct virtual processor at the right
  28. * vector. This driver does not support level-triggered (line-based)
  29. * interrupts, and will report that the Interrupt Line register in the
  30. * function's configuration space is zero.
  31. *
  32. * The rest of this driver mostly maps PCI concepts onto underlying Hyper-V
  33. * facilities. For instance, the configuration space of a function exposed
  34. * by Hyper-V is mapped into a single page of memory space, and the
  35. * read and write handlers for config space must be aware of this mechanism.
  36. * Similarly, device setup and teardown involves messages sent to and from
  37. * the PCI back-end driver in Hyper-V.
  38. */
  39. #include <linux/kernel.h>
  40. #include <linux/module.h>
  41. #include <linux/pci.h>
  42. #include <linux/delay.h>
  43. #include <linux/semaphore.h>
  44. #include <linux/irqdomain.h>
  45. #include <asm/irqdomain.h>
  46. #include <asm/apic.h>
  47. #include <linux/msi.h>
  48. #include <linux/hyperv.h>
  49. #include <linux/refcount.h>
  50. #include <asm/mshyperv.h>
  51. /*
  52. * Protocol versions. The low word is the minor version, the high word the
  53. * major version.
  54. */
  55. #define PCI_MAKE_VERSION(major, minor) ((u32)(((major) << 16) | (minor)))
  56. #define PCI_MAJOR_VERSION(version) ((u32)(version) >> 16)
  57. #define PCI_MINOR_VERSION(version) ((u32)(version) & 0xff)
  58. enum pci_protocol_version_t {
  59. PCI_PROTOCOL_VERSION_1_1 = PCI_MAKE_VERSION(1, 1), /* Win10 */
  60. PCI_PROTOCOL_VERSION_1_2 = PCI_MAKE_VERSION(1, 2), /* RS1 */
  61. };
  62. #define CPU_AFFINITY_ALL -1ULL
  63. /*
  64. * Supported protocol versions in the order of probing - highest go
  65. * first.
  66. */
  67. static enum pci_protocol_version_t pci_protocol_versions[] = {
  68. PCI_PROTOCOL_VERSION_1_2,
  69. PCI_PROTOCOL_VERSION_1_1,
  70. };
  71. /*
  72. * Protocol version negotiated by hv_pci_protocol_negotiation().
  73. */
  74. static enum pci_protocol_version_t pci_protocol_version;
  75. #define PCI_CONFIG_MMIO_LENGTH 0x2000
  76. #define CFG_PAGE_OFFSET 0x1000
  77. #define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET)
  78. #define MAX_SUPPORTED_MSI_MESSAGES 0x400
  79. #define STATUS_REVISION_MISMATCH 0xC0000059
  80. /*
  81. * Message Types
  82. */
  83. enum pci_message_type {
  84. /*
  85. * Version 1.1
  86. */
  87. PCI_MESSAGE_BASE = 0x42490000,
  88. PCI_BUS_RELATIONS = PCI_MESSAGE_BASE + 0,
  89. PCI_QUERY_BUS_RELATIONS = PCI_MESSAGE_BASE + 1,
  90. PCI_POWER_STATE_CHANGE = PCI_MESSAGE_BASE + 4,
  91. PCI_QUERY_RESOURCE_REQUIREMENTS = PCI_MESSAGE_BASE + 5,
  92. PCI_QUERY_RESOURCE_RESOURCES = PCI_MESSAGE_BASE + 6,
  93. PCI_BUS_D0ENTRY = PCI_MESSAGE_BASE + 7,
  94. PCI_BUS_D0EXIT = PCI_MESSAGE_BASE + 8,
  95. PCI_READ_BLOCK = PCI_MESSAGE_BASE + 9,
  96. PCI_WRITE_BLOCK = PCI_MESSAGE_BASE + 0xA,
  97. PCI_EJECT = PCI_MESSAGE_BASE + 0xB,
  98. PCI_QUERY_STOP = PCI_MESSAGE_BASE + 0xC,
  99. PCI_REENABLE = PCI_MESSAGE_BASE + 0xD,
  100. PCI_QUERY_STOP_FAILED = PCI_MESSAGE_BASE + 0xE,
  101. PCI_EJECTION_COMPLETE = PCI_MESSAGE_BASE + 0xF,
  102. PCI_RESOURCES_ASSIGNED = PCI_MESSAGE_BASE + 0x10,
  103. PCI_RESOURCES_RELEASED = PCI_MESSAGE_BASE + 0x11,
  104. PCI_INVALIDATE_BLOCK = PCI_MESSAGE_BASE + 0x12,
  105. PCI_QUERY_PROTOCOL_VERSION = PCI_MESSAGE_BASE + 0x13,
  106. PCI_CREATE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x14,
  107. PCI_DELETE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x15,
  108. PCI_RESOURCES_ASSIGNED2 = PCI_MESSAGE_BASE + 0x16,
  109. PCI_CREATE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x17,
  110. PCI_DELETE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x18, /* unused */
  111. PCI_MESSAGE_MAXIMUM
  112. };
  113. /*
  114. * Structures defining the virtual PCI Express protocol.
  115. */
  116. union pci_version {
  117. struct {
  118. u16 minor_version;
  119. u16 major_version;
  120. } parts;
  121. u32 version;
  122. } __packed;
  123. /*
  124. * Function numbers are 8-bits wide on Express, as interpreted through ARI,
  125. * which is all this driver does. This representation is the one used in
  126. * Windows, which is what is expected when sending this back and forth with
  127. * the Hyper-V parent partition.
  128. */
  129. union win_slot_encoding {
  130. struct {
  131. u32 dev:5;
  132. u32 func:3;
  133. u32 reserved:24;
  134. } bits;
  135. u32 slot;
  136. } __packed;
  137. /*
  138. * Pretty much as defined in the PCI Specifications.
  139. */
  140. struct pci_function_description {
  141. u16 v_id; /* vendor ID */
  142. u16 d_id; /* device ID */
  143. u8 rev;
  144. u8 prog_intf;
  145. u8 subclass;
  146. u8 base_class;
  147. u32 subsystem_id;
  148. union win_slot_encoding win_slot;
  149. u32 ser; /* serial number */
  150. } __packed;
  151. /**
  152. * struct hv_msi_desc
  153. * @vector: IDT entry
  154. * @delivery_mode: As defined in Intel's Programmer's
  155. * Reference Manual, Volume 3, Chapter 8.
  156. * @vector_count: Number of contiguous entries in the
  157. * Interrupt Descriptor Table that are
  158. * occupied by this Message-Signaled
  159. * Interrupt. For "MSI", as first defined
  160. * in PCI 2.2, this can be between 1 and
  161. * 32. For "MSI-X," as first defined in PCI
  162. * 3.0, this must be 1, as each MSI-X table
  163. * entry would have its own descriptor.
  164. * @reserved: Empty space
  165. * @cpu_mask: All the target virtual processors.
  166. */
  167. struct hv_msi_desc {
  168. u8 vector;
  169. u8 delivery_mode;
  170. u16 vector_count;
  171. u32 reserved;
  172. u64 cpu_mask;
  173. } __packed;
  174. /**
  175. * struct hv_msi_desc2 - 1.2 version of hv_msi_desc
  176. * @vector: IDT entry
  177. * @delivery_mode: As defined in Intel's Programmer's
  178. * Reference Manual, Volume 3, Chapter 8.
  179. * @vector_count: Number of contiguous entries in the
  180. * Interrupt Descriptor Table that are
  181. * occupied by this Message-Signaled
  182. * Interrupt. For "MSI", as first defined
  183. * in PCI 2.2, this can be between 1 and
  184. * 32. For "MSI-X," as first defined in PCI
  185. * 3.0, this must be 1, as each MSI-X table
  186. * entry would have its own descriptor.
  187. * @processor_count: number of bits enabled in array.
  188. * @processor_array: All the target virtual processors.
  189. */
  190. struct hv_msi_desc2 {
  191. u8 vector;
  192. u8 delivery_mode;
  193. u16 vector_count;
  194. u16 processor_count;
  195. u16 processor_array[32];
  196. } __packed;
  197. /**
  198. * struct tran_int_desc
  199. * @reserved: unused, padding
  200. * @vector_count: same as in hv_msi_desc
  201. * @data: This is the "data payload" value that is
  202. * written by the device when it generates
  203. * a message-signaled interrupt, either MSI
  204. * or MSI-X.
  205. * @address: This is the address to which the data
  206. * payload is written on interrupt
  207. * generation.
  208. */
  209. struct tran_int_desc {
  210. u16 reserved;
  211. u16 vector_count;
  212. u32 data;
  213. u64 address;
  214. } __packed;
  215. /*
  216. * A generic message format for virtual PCI.
  217. * Specific message formats are defined later in the file.
  218. */
  219. struct pci_message {
  220. u32 type;
  221. } __packed;
  222. struct pci_child_message {
  223. struct pci_message message_type;
  224. union win_slot_encoding wslot;
  225. } __packed;
  226. struct pci_incoming_message {
  227. struct vmpacket_descriptor hdr;
  228. struct pci_message message_type;
  229. } __packed;
  230. struct pci_response {
  231. struct vmpacket_descriptor hdr;
  232. s32 status; /* negative values are failures */
  233. } __packed;
  234. struct pci_packet {
  235. void (*completion_func)(void *context, struct pci_response *resp,
  236. int resp_packet_size);
  237. void *compl_ctxt;
  238. struct pci_message message[0];
  239. };
  240. /*
  241. * Specific message types supporting the PCI protocol.
  242. */
  243. /*
  244. * Version negotiation message. Sent from the guest to the host.
  245. * The guest is free to try different versions until the host
  246. * accepts the version.
  247. *
  248. * pci_version: The protocol version requested.
  249. * is_last_attempt: If TRUE, this is the last version guest will request.
  250. * reservedz: Reserved field, set to zero.
  251. */
  252. struct pci_version_request {
  253. struct pci_message message_type;
  254. u32 protocol_version;
  255. } __packed;
  256. /*
  257. * Bus D0 Entry. This is sent from the guest to the host when the virtual
  258. * bus (PCI Express port) is ready for action.
  259. */
  260. struct pci_bus_d0_entry {
  261. struct pci_message message_type;
  262. u32 reserved;
  263. u64 mmio_base;
  264. } __packed;
  265. struct pci_bus_relations {
  266. struct pci_incoming_message incoming;
  267. u32 device_count;
  268. struct pci_function_description func[0];
  269. } __packed;
  270. struct pci_q_res_req_response {
  271. struct vmpacket_descriptor hdr;
  272. s32 status; /* negative values are failures */
  273. u32 probed_bar[6];
  274. } __packed;
  275. struct pci_set_power {
  276. struct pci_message message_type;
  277. union win_slot_encoding wslot;
  278. u32 power_state; /* In Windows terms */
  279. u32 reserved;
  280. } __packed;
  281. struct pci_set_power_response {
  282. struct vmpacket_descriptor hdr;
  283. s32 status; /* negative values are failures */
  284. union win_slot_encoding wslot;
  285. u32 resultant_state; /* In Windows terms */
  286. u32 reserved;
  287. } __packed;
  288. struct pci_resources_assigned {
  289. struct pci_message message_type;
  290. union win_slot_encoding wslot;
  291. u8 memory_range[0x14][6]; /* not used here */
  292. u32 msi_descriptors;
  293. u32 reserved[4];
  294. } __packed;
  295. struct pci_resources_assigned2 {
  296. struct pci_message message_type;
  297. union win_slot_encoding wslot;
  298. u8 memory_range[0x14][6]; /* not used here */
  299. u32 msi_descriptor_count;
  300. u8 reserved[70];
  301. } __packed;
  302. struct pci_create_interrupt {
  303. struct pci_message message_type;
  304. union win_slot_encoding wslot;
  305. struct hv_msi_desc int_desc;
  306. } __packed;
  307. struct pci_create_int_response {
  308. struct pci_response response;
  309. u32 reserved;
  310. struct tran_int_desc int_desc;
  311. } __packed;
  312. struct pci_create_interrupt2 {
  313. struct pci_message message_type;
  314. union win_slot_encoding wslot;
  315. struct hv_msi_desc2 int_desc;
  316. } __packed;
  317. struct pci_delete_interrupt {
  318. struct pci_message message_type;
  319. union win_slot_encoding wslot;
  320. struct tran_int_desc int_desc;
  321. } __packed;
  322. struct pci_dev_incoming {
  323. struct pci_incoming_message incoming;
  324. union win_slot_encoding wslot;
  325. } __packed;
  326. struct pci_eject_response {
  327. struct pci_message message_type;
  328. union win_slot_encoding wslot;
  329. u32 status;
  330. } __packed;
  331. static int pci_ring_size = (4 * PAGE_SIZE);
  332. /*
  333. * Definitions or interrupt steering hypercall.
  334. */
  335. #define HV_PARTITION_ID_SELF ((u64)-1)
  336. #define HVCALL_RETARGET_INTERRUPT 0x7e
  337. struct hv_interrupt_entry {
  338. u32 source; /* 1 for MSI(-X) */
  339. u32 reserved1;
  340. u32 address;
  341. u32 data;
  342. };
  343. #define HV_VP_SET_BANK_COUNT_MAX 5 /* current implementation limit */
  344. struct hv_vp_set {
  345. u64 format; /* 0 (HvGenericSetSparse4k) */
  346. u64 valid_banks;
  347. u64 masks[HV_VP_SET_BANK_COUNT_MAX];
  348. };
  349. /*
  350. * flags for hv_device_interrupt_target.flags
  351. */
  352. #define HV_DEVICE_INTERRUPT_TARGET_MULTICAST 1
  353. #define HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET 2
  354. struct hv_device_interrupt_target {
  355. u32 vector;
  356. u32 flags;
  357. union {
  358. u64 vp_mask;
  359. struct hv_vp_set vp_set;
  360. };
  361. };
  362. struct retarget_msi_interrupt {
  363. u64 partition_id; /* use "self" */
  364. u64 device_id;
  365. struct hv_interrupt_entry int_entry;
  366. u64 reserved2;
  367. struct hv_device_interrupt_target int_target;
  368. } __packed;
  369. /*
  370. * Driver specific state.
  371. */
  372. enum hv_pcibus_state {
  373. hv_pcibus_init = 0,
  374. hv_pcibus_probed,
  375. hv_pcibus_installed,
  376. hv_pcibus_removed,
  377. hv_pcibus_maximum
  378. };
  379. struct hv_pcibus_device {
  380. struct pci_sysdata sysdata;
  381. enum hv_pcibus_state state;
  382. atomic_t remove_lock;
  383. struct hv_device *hdev;
  384. resource_size_t low_mmio_space;
  385. resource_size_t high_mmio_space;
  386. struct resource *mem_config;
  387. struct resource *low_mmio_res;
  388. struct resource *high_mmio_res;
  389. struct completion *survey_event;
  390. struct completion remove_event;
  391. struct pci_bus *pci_bus;
  392. spinlock_t config_lock; /* Avoid two threads writing index page */
  393. spinlock_t device_list_lock; /* Protect lists below */
  394. void __iomem *cfg_addr;
  395. struct semaphore enum_sem;
  396. struct list_head resources_for_children;
  397. struct list_head children;
  398. struct list_head dr_list;
  399. struct msi_domain_info msi_info;
  400. struct msi_controller msi_chip;
  401. struct irq_domain *irq_domain;
  402. /* hypercall arg, must not cross page boundary */
  403. struct retarget_msi_interrupt retarget_msi_interrupt_params;
  404. spinlock_t retarget_msi_interrupt_lock;
  405. };
  406. /*
  407. * Tracks "Device Relations" messages from the host, which must be both
  408. * processed in order and deferred so that they don't run in the context
  409. * of the incoming packet callback.
  410. */
  411. struct hv_dr_work {
  412. struct work_struct wrk;
  413. struct hv_pcibus_device *bus;
  414. };
  415. struct hv_dr_state {
  416. struct list_head list_entry;
  417. u32 device_count;
  418. struct pci_function_description func[0];
  419. };
  420. enum hv_pcichild_state {
  421. hv_pcichild_init = 0,
  422. hv_pcichild_requirements,
  423. hv_pcichild_resourced,
  424. hv_pcichild_ejecting,
  425. hv_pcichild_maximum
  426. };
  427. enum hv_pcidev_ref_reason {
  428. hv_pcidev_ref_invalid = 0,
  429. hv_pcidev_ref_initial,
  430. hv_pcidev_ref_by_slot,
  431. hv_pcidev_ref_packet,
  432. hv_pcidev_ref_pnp,
  433. hv_pcidev_ref_childlist,
  434. hv_pcidev_irqdata,
  435. hv_pcidev_ref_max
  436. };
  437. struct hv_pci_dev {
  438. /* List protected by pci_rescan_remove_lock */
  439. struct list_head list_entry;
  440. refcount_t refs;
  441. enum hv_pcichild_state state;
  442. struct pci_function_description desc;
  443. bool reported_missing;
  444. struct hv_pcibus_device *hbus;
  445. struct work_struct wrk;
  446. /*
  447. * What would be observed if one wrote 0xFFFFFFFF to a BAR and then
  448. * read it back, for each of the BAR offsets within config space.
  449. */
  450. u32 probed_bar[6];
  451. };
  452. struct hv_pci_compl {
  453. struct completion host_event;
  454. s32 completion_status;
  455. };
  456. /**
  457. * hv_pci_generic_compl() - Invoked for a completion packet
  458. * @context: Set up by the sender of the packet.
  459. * @resp: The response packet
  460. * @resp_packet_size: Size in bytes of the packet
  461. *
  462. * This function is used to trigger an event and report status
  463. * for any message for which the completion packet contains a
  464. * status and nothing else.
  465. */
  466. static void hv_pci_generic_compl(void *context, struct pci_response *resp,
  467. int resp_packet_size)
  468. {
  469. struct hv_pci_compl *comp_pkt = context;
  470. if (resp_packet_size >= offsetofend(struct pci_response, status))
  471. comp_pkt->completion_status = resp->status;
  472. else
  473. comp_pkt->completion_status = -1;
  474. complete(&comp_pkt->host_event);
  475. }
  476. static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
  477. u32 wslot);
  478. static void get_pcichild(struct hv_pci_dev *hv_pcidev,
  479. enum hv_pcidev_ref_reason reason);
  480. static void put_pcichild(struct hv_pci_dev *hv_pcidev,
  481. enum hv_pcidev_ref_reason reason);
  482. static void get_hvpcibus(struct hv_pcibus_device *hv_pcibus);
  483. static void put_hvpcibus(struct hv_pcibus_device *hv_pcibus);
  484. /**
  485. * devfn_to_wslot() - Convert from Linux PCI slot to Windows
  486. * @devfn: The Linux representation of PCI slot
  487. *
  488. * Windows uses a slightly different representation of PCI slot.
  489. *
  490. * Return: The Windows representation
  491. */
  492. static u32 devfn_to_wslot(int devfn)
  493. {
  494. union win_slot_encoding wslot;
  495. wslot.slot = 0;
  496. wslot.bits.dev = PCI_SLOT(devfn);
  497. wslot.bits.func = PCI_FUNC(devfn);
  498. return wslot.slot;
  499. }
  500. /**
  501. * wslot_to_devfn() - Convert from Windows PCI slot to Linux
  502. * @wslot: The Windows representation of PCI slot
  503. *
  504. * Windows uses a slightly different representation of PCI slot.
  505. *
  506. * Return: The Linux representation
  507. */
  508. static int wslot_to_devfn(u32 wslot)
  509. {
  510. union win_slot_encoding slot_no;
  511. slot_no.slot = wslot;
  512. return PCI_DEVFN(slot_no.bits.dev, slot_no.bits.func);
  513. }
  514. /*
  515. * PCI Configuration Space for these root PCI buses is implemented as a pair
  516. * of pages in memory-mapped I/O space. Writing to the first page chooses
  517. * the PCI function being written or read. Once the first page has been
  518. * written to, the following page maps in the entire configuration space of
  519. * the function.
  520. */
  521. /**
  522. * _hv_pcifront_read_config() - Internal PCI config read
  523. * @hpdev: The PCI driver's representation of the device
  524. * @where: Offset within config space
  525. * @size: Size of the transfer
  526. * @val: Pointer to the buffer receiving the data
  527. */
  528. static void _hv_pcifront_read_config(struct hv_pci_dev *hpdev, int where,
  529. int size, u32 *val)
  530. {
  531. unsigned long flags;
  532. void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
  533. /*
  534. * If the attempt is to read the IDs or the ROM BAR, simulate that.
  535. */
  536. if (where + size <= PCI_COMMAND) {
  537. memcpy(val, ((u8 *)&hpdev->desc.v_id) + where, size);
  538. } else if (where >= PCI_CLASS_REVISION && where + size <=
  539. PCI_CACHE_LINE_SIZE) {
  540. memcpy(val, ((u8 *)&hpdev->desc.rev) + where -
  541. PCI_CLASS_REVISION, size);
  542. } else if (where >= PCI_SUBSYSTEM_VENDOR_ID && where + size <=
  543. PCI_ROM_ADDRESS) {
  544. memcpy(val, (u8 *)&hpdev->desc.subsystem_id + where -
  545. PCI_SUBSYSTEM_VENDOR_ID, size);
  546. } else if (where >= PCI_ROM_ADDRESS && where + size <=
  547. PCI_CAPABILITY_LIST) {
  548. /* ROM BARs are unimplemented */
  549. *val = 0;
  550. } else if (where >= PCI_INTERRUPT_LINE && where + size <=
  551. PCI_INTERRUPT_PIN) {
  552. /*
  553. * Interrupt Line and Interrupt PIN are hard-wired to zero
  554. * because this front-end only supports message-signaled
  555. * interrupts.
  556. */
  557. *val = 0;
  558. } else if (where + size <= CFG_PAGE_SIZE) {
  559. spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
  560. /* Choose the function to be read. (See comment above) */
  561. writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
  562. /* Make sure the function was chosen before we start reading. */
  563. mb();
  564. /* Read from that function's config space. */
  565. switch (size) {
  566. case 1:
  567. *val = readb(addr);
  568. break;
  569. case 2:
  570. *val = readw(addr);
  571. break;
  572. default:
  573. *val = readl(addr);
  574. break;
  575. }
  576. /*
  577. * Make sure the write was done before we release the spinlock
  578. * allowing consecutive reads/writes.
  579. */
  580. mb();
  581. spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
  582. } else {
  583. dev_err(&hpdev->hbus->hdev->device,
  584. "Attempt to read beyond a function's config space.\n");
  585. }
  586. }
  587. /**
  588. * _hv_pcifront_write_config() - Internal PCI config write
  589. * @hpdev: The PCI driver's representation of the device
  590. * @where: Offset within config space
  591. * @size: Size of the transfer
  592. * @val: The data being transferred
  593. */
  594. static void _hv_pcifront_write_config(struct hv_pci_dev *hpdev, int where,
  595. int size, u32 val)
  596. {
  597. unsigned long flags;
  598. void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
  599. if (where >= PCI_SUBSYSTEM_VENDOR_ID &&
  600. where + size <= PCI_CAPABILITY_LIST) {
  601. /* SSIDs and ROM BARs are read-only */
  602. } else if (where >= PCI_COMMAND && where + size <= CFG_PAGE_SIZE) {
  603. spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
  604. /* Choose the function to be written. (See comment above) */
  605. writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
  606. /* Make sure the function was chosen before we start writing. */
  607. wmb();
  608. /* Write to that function's config space. */
  609. switch (size) {
  610. case 1:
  611. writeb(val, addr);
  612. break;
  613. case 2:
  614. writew(val, addr);
  615. break;
  616. default:
  617. writel(val, addr);
  618. break;
  619. }
  620. /*
  621. * Make sure the write was done before we release the spinlock
  622. * allowing consecutive reads/writes.
  623. */
  624. mb();
  625. spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
  626. } else {
  627. dev_err(&hpdev->hbus->hdev->device,
  628. "Attempt to write beyond a function's config space.\n");
  629. }
  630. }
  631. /**
  632. * hv_pcifront_read_config() - Read configuration space
  633. * @bus: PCI Bus structure
  634. * @devfn: Device/function
  635. * @where: Offset from base
  636. * @size: Byte/word/dword
  637. * @val: Value to be read
  638. *
  639. * Return: PCIBIOS_SUCCESSFUL on success
  640. * PCIBIOS_DEVICE_NOT_FOUND on failure
  641. */
  642. static int hv_pcifront_read_config(struct pci_bus *bus, unsigned int devfn,
  643. int where, int size, u32 *val)
  644. {
  645. struct hv_pcibus_device *hbus =
  646. container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
  647. struct hv_pci_dev *hpdev;
  648. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
  649. if (!hpdev)
  650. return PCIBIOS_DEVICE_NOT_FOUND;
  651. _hv_pcifront_read_config(hpdev, where, size, val);
  652. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  653. return PCIBIOS_SUCCESSFUL;
  654. }
  655. /**
  656. * hv_pcifront_write_config() - Write configuration space
  657. * @bus: PCI Bus structure
  658. * @devfn: Device/function
  659. * @where: Offset from base
  660. * @size: Byte/word/dword
  661. * @val: Value to be written to device
  662. *
  663. * Return: PCIBIOS_SUCCESSFUL on success
  664. * PCIBIOS_DEVICE_NOT_FOUND on failure
  665. */
  666. static int hv_pcifront_write_config(struct pci_bus *bus, unsigned int devfn,
  667. int where, int size, u32 val)
  668. {
  669. struct hv_pcibus_device *hbus =
  670. container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
  671. struct hv_pci_dev *hpdev;
  672. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
  673. if (!hpdev)
  674. return PCIBIOS_DEVICE_NOT_FOUND;
  675. _hv_pcifront_write_config(hpdev, where, size, val);
  676. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  677. return PCIBIOS_SUCCESSFUL;
  678. }
  679. /* PCIe operations */
  680. static struct pci_ops hv_pcifront_ops = {
  681. .read = hv_pcifront_read_config,
  682. .write = hv_pcifront_write_config,
  683. };
  684. /* Interrupt management hooks */
  685. static void hv_int_desc_free(struct hv_pci_dev *hpdev,
  686. struct tran_int_desc *int_desc)
  687. {
  688. struct pci_delete_interrupt *int_pkt;
  689. struct {
  690. struct pci_packet pkt;
  691. u8 buffer[sizeof(struct pci_delete_interrupt)];
  692. } ctxt;
  693. memset(&ctxt, 0, sizeof(ctxt));
  694. int_pkt = (struct pci_delete_interrupt *)&ctxt.pkt.message;
  695. int_pkt->message_type.type =
  696. PCI_DELETE_INTERRUPT_MESSAGE;
  697. int_pkt->wslot.slot = hpdev->desc.win_slot.slot;
  698. int_pkt->int_desc = *int_desc;
  699. vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt),
  700. (unsigned long)&ctxt.pkt, VM_PKT_DATA_INBAND, 0);
  701. kfree(int_desc);
  702. }
  703. /**
  704. * hv_msi_free() - Free the MSI.
  705. * @domain: The interrupt domain pointer
  706. * @info: Extra MSI-related context
  707. * @irq: Identifies the IRQ.
  708. *
  709. * The Hyper-V parent partition and hypervisor are tracking the
  710. * messages that are in use, keeping the interrupt redirection
  711. * table up to date. This callback sends a message that frees
  712. * the IRT entry and related tracking nonsense.
  713. */
  714. static void hv_msi_free(struct irq_domain *domain, struct msi_domain_info *info,
  715. unsigned int irq)
  716. {
  717. struct hv_pcibus_device *hbus;
  718. struct hv_pci_dev *hpdev;
  719. struct pci_dev *pdev;
  720. struct tran_int_desc *int_desc;
  721. struct irq_data *irq_data = irq_domain_get_irq_data(domain, irq);
  722. struct msi_desc *msi = irq_data_get_msi_desc(irq_data);
  723. pdev = msi_desc_to_pci_dev(msi);
  724. hbus = info->data;
  725. int_desc = irq_data_get_irq_chip_data(irq_data);
  726. if (!int_desc)
  727. return;
  728. irq_data->chip_data = NULL;
  729. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
  730. if (!hpdev) {
  731. kfree(int_desc);
  732. return;
  733. }
  734. hv_int_desc_free(hpdev, int_desc);
  735. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  736. }
  737. static int hv_set_affinity(struct irq_data *data, const struct cpumask *dest,
  738. bool force)
  739. {
  740. struct irq_data *parent = data->parent_data;
  741. return parent->chip->irq_set_affinity(parent, dest, force);
  742. }
  743. static void hv_irq_mask(struct irq_data *data)
  744. {
  745. pci_msi_mask_irq(data);
  746. }
  747. /**
  748. * hv_irq_unmask() - "Unmask" the IRQ by setting its current
  749. * affinity.
  750. * @data: Describes the IRQ
  751. *
  752. * Build new a destination for the MSI and make a hypercall to
  753. * update the Interrupt Redirection Table. "Device Logical ID"
  754. * is built out of this PCI bus's instance GUID and the function
  755. * number of the device.
  756. */
  757. static void hv_irq_unmask(struct irq_data *data)
  758. {
  759. struct msi_desc *msi_desc = irq_data_get_msi_desc(data);
  760. struct irq_cfg *cfg = irqd_cfg(data);
  761. struct retarget_msi_interrupt *params;
  762. struct hv_pcibus_device *hbus;
  763. struct cpumask *dest;
  764. struct pci_bus *pbus;
  765. struct pci_dev *pdev;
  766. unsigned long flags;
  767. u32 var_size = 0;
  768. int cpu_vmbus;
  769. int cpu;
  770. u64 res;
  771. dest = irq_data_get_effective_affinity_mask(data);
  772. pdev = msi_desc_to_pci_dev(msi_desc);
  773. pbus = pdev->bus;
  774. hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
  775. spin_lock_irqsave(&hbus->retarget_msi_interrupt_lock, flags);
  776. params = &hbus->retarget_msi_interrupt_params;
  777. memset(params, 0, sizeof(*params));
  778. params->partition_id = HV_PARTITION_ID_SELF;
  779. params->int_entry.source = 1; /* MSI(-X) */
  780. params->int_entry.address = msi_desc->msg.address_lo;
  781. params->int_entry.data = msi_desc->msg.data;
  782. params->device_id = (hbus->hdev->dev_instance.b[5] << 24) |
  783. (hbus->hdev->dev_instance.b[4] << 16) |
  784. (hbus->hdev->dev_instance.b[7] << 8) |
  785. (hbus->hdev->dev_instance.b[6] & 0xf8) |
  786. PCI_FUNC(pdev->devfn);
  787. params->int_target.vector = cfg->vector;
  788. /*
  789. * Honoring apic->irq_delivery_mode set to dest_Fixed by
  790. * setting the HV_DEVICE_INTERRUPT_TARGET_MULTICAST flag results in a
  791. * spurious interrupt storm. Not doing so does not seem to have a
  792. * negative effect (yet?).
  793. */
  794. if (pci_protocol_version >= PCI_PROTOCOL_VERSION_1_2) {
  795. /*
  796. * PCI_PROTOCOL_VERSION_1_2 supports the VP_SET version of the
  797. * HVCALL_RETARGET_INTERRUPT hypercall, which also coincides
  798. * with >64 VP support.
  799. * ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED
  800. * is not sufficient for this hypercall.
  801. */
  802. params->int_target.flags |=
  803. HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET;
  804. params->int_target.vp_set.valid_banks =
  805. (1ull << HV_VP_SET_BANK_COUNT_MAX) - 1;
  806. /*
  807. * var-sized hypercall, var-size starts after vp_mask (thus
  808. * vp_set.format does not count, but vp_set.valid_banks does).
  809. */
  810. var_size = 1 + HV_VP_SET_BANK_COUNT_MAX;
  811. for_each_cpu_and(cpu, dest, cpu_online_mask) {
  812. cpu_vmbus = hv_cpu_number_to_vp_number(cpu);
  813. if (cpu_vmbus >= HV_VP_SET_BANK_COUNT_MAX * 64) {
  814. dev_err(&hbus->hdev->device,
  815. "too high CPU %d", cpu_vmbus);
  816. res = 1;
  817. goto exit_unlock;
  818. }
  819. params->int_target.vp_set.masks[cpu_vmbus / 64] |=
  820. (1ULL << (cpu_vmbus & 63));
  821. }
  822. } else {
  823. for_each_cpu_and(cpu, dest, cpu_online_mask) {
  824. params->int_target.vp_mask |=
  825. (1ULL << hv_cpu_number_to_vp_number(cpu));
  826. }
  827. }
  828. res = hv_do_hypercall(HVCALL_RETARGET_INTERRUPT | (var_size << 17),
  829. params, NULL);
  830. exit_unlock:
  831. spin_unlock_irqrestore(&hbus->retarget_msi_interrupt_lock, flags);
  832. if (res) {
  833. dev_err(&hbus->hdev->device,
  834. "%s() failed: %#llx", __func__, res);
  835. return;
  836. }
  837. pci_msi_unmask_irq(data);
  838. }
  839. struct compose_comp_ctxt {
  840. struct hv_pci_compl comp_pkt;
  841. struct tran_int_desc int_desc;
  842. };
  843. static void hv_pci_compose_compl(void *context, struct pci_response *resp,
  844. int resp_packet_size)
  845. {
  846. struct compose_comp_ctxt *comp_pkt = context;
  847. struct pci_create_int_response *int_resp =
  848. (struct pci_create_int_response *)resp;
  849. comp_pkt->comp_pkt.completion_status = resp->status;
  850. comp_pkt->int_desc = int_resp->int_desc;
  851. complete(&comp_pkt->comp_pkt.host_event);
  852. }
  853. static u32 hv_compose_msi_req_v1(
  854. struct pci_create_interrupt *int_pkt, struct cpumask *affinity,
  855. u32 slot, u8 vector)
  856. {
  857. int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE;
  858. int_pkt->wslot.slot = slot;
  859. int_pkt->int_desc.vector = vector;
  860. int_pkt->int_desc.vector_count = 1;
  861. int_pkt->int_desc.delivery_mode =
  862. (apic->irq_delivery_mode == dest_LowestPrio) ?
  863. dest_LowestPrio : dest_Fixed;
  864. /*
  865. * Create MSI w/ dummy vCPU set, overwritten by subsequent retarget in
  866. * hv_irq_unmask().
  867. */
  868. int_pkt->int_desc.cpu_mask = CPU_AFFINITY_ALL;
  869. return sizeof(*int_pkt);
  870. }
  871. static u32 hv_compose_msi_req_v2(
  872. struct pci_create_interrupt2 *int_pkt, struct cpumask *affinity,
  873. u32 slot, u8 vector)
  874. {
  875. int cpu;
  876. int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE2;
  877. int_pkt->wslot.slot = slot;
  878. int_pkt->int_desc.vector = vector;
  879. int_pkt->int_desc.vector_count = 1;
  880. int_pkt->int_desc.delivery_mode =
  881. (apic->irq_delivery_mode == dest_LowestPrio) ?
  882. dest_LowestPrio : dest_Fixed;
  883. /*
  884. * Create MSI w/ dummy vCPU set targeting just one vCPU, overwritten
  885. * by subsequent retarget in hv_irq_unmask().
  886. */
  887. cpu = cpumask_first_and(affinity, cpu_online_mask);
  888. int_pkt->int_desc.processor_array[0] =
  889. hv_cpu_number_to_vp_number(cpu);
  890. int_pkt->int_desc.processor_count = 1;
  891. return sizeof(*int_pkt);
  892. }
  893. /**
  894. * hv_compose_msi_msg() - Supplies a valid MSI address/data
  895. * @data: Everything about this MSI
  896. * @msg: Buffer that is filled in by this function
  897. *
  898. * This function unpacks the IRQ looking for target CPU set, IDT
  899. * vector and mode and sends a message to the parent partition
  900. * asking for a mapping for that tuple in this partition. The
  901. * response supplies a data value and address to which that data
  902. * should be written to trigger that interrupt.
  903. */
  904. static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  905. {
  906. struct irq_cfg *cfg = irqd_cfg(data);
  907. struct hv_pcibus_device *hbus;
  908. struct hv_pci_dev *hpdev;
  909. struct pci_bus *pbus;
  910. struct pci_dev *pdev;
  911. struct cpumask *dest;
  912. struct compose_comp_ctxt comp;
  913. struct tran_int_desc *int_desc;
  914. struct {
  915. struct pci_packet pci_pkt;
  916. union {
  917. struct pci_create_interrupt v1;
  918. struct pci_create_interrupt2 v2;
  919. } int_pkts;
  920. } __packed ctxt;
  921. u32 size;
  922. int ret;
  923. pdev = msi_desc_to_pci_dev(irq_data_get_msi_desc(data));
  924. dest = irq_data_get_effective_affinity_mask(data);
  925. pbus = pdev->bus;
  926. hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
  927. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
  928. if (!hpdev)
  929. goto return_null_message;
  930. /* Free any previous message that might have already been composed. */
  931. if (data->chip_data) {
  932. int_desc = data->chip_data;
  933. data->chip_data = NULL;
  934. hv_int_desc_free(hpdev, int_desc);
  935. }
  936. int_desc = kzalloc(sizeof(*int_desc), GFP_ATOMIC);
  937. if (!int_desc)
  938. goto drop_reference;
  939. memset(&ctxt, 0, sizeof(ctxt));
  940. init_completion(&comp.comp_pkt.host_event);
  941. ctxt.pci_pkt.completion_func = hv_pci_compose_compl;
  942. ctxt.pci_pkt.compl_ctxt = &comp;
  943. switch (pci_protocol_version) {
  944. case PCI_PROTOCOL_VERSION_1_1:
  945. size = hv_compose_msi_req_v1(&ctxt.int_pkts.v1,
  946. dest,
  947. hpdev->desc.win_slot.slot,
  948. cfg->vector);
  949. break;
  950. case PCI_PROTOCOL_VERSION_1_2:
  951. size = hv_compose_msi_req_v2(&ctxt.int_pkts.v2,
  952. dest,
  953. hpdev->desc.win_slot.slot,
  954. cfg->vector);
  955. break;
  956. default:
  957. /* As we only negotiate protocol versions known to this driver,
  958. * this path should never hit. However, this is it not a hot
  959. * path so we print a message to aid future updates.
  960. */
  961. dev_err(&hbus->hdev->device,
  962. "Unexpected vPCI protocol, update driver.");
  963. goto free_int_desc;
  964. }
  965. ret = vmbus_sendpacket(hpdev->hbus->hdev->channel, &ctxt.int_pkts,
  966. size, (unsigned long)&ctxt.pci_pkt,
  967. VM_PKT_DATA_INBAND,
  968. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  969. if (ret) {
  970. dev_err(&hbus->hdev->device,
  971. "Sending request for interrupt failed: 0x%x",
  972. comp.comp_pkt.completion_status);
  973. goto free_int_desc;
  974. }
  975. /*
  976. * Since this function is called with IRQ locks held, can't
  977. * do normal wait for completion; instead poll.
  978. */
  979. while (!try_wait_for_completion(&comp.comp_pkt.host_event))
  980. udelay(100);
  981. if (comp.comp_pkt.completion_status < 0) {
  982. dev_err(&hbus->hdev->device,
  983. "Request for interrupt failed: 0x%x",
  984. comp.comp_pkt.completion_status);
  985. goto free_int_desc;
  986. }
  987. /*
  988. * Record the assignment so that this can be unwound later. Using
  989. * irq_set_chip_data() here would be appropriate, but the lock it takes
  990. * is already held.
  991. */
  992. *int_desc = comp.int_desc;
  993. data->chip_data = int_desc;
  994. /* Pass up the result. */
  995. msg->address_hi = comp.int_desc.address >> 32;
  996. msg->address_lo = comp.int_desc.address & 0xffffffff;
  997. msg->data = comp.int_desc.data;
  998. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  999. return;
  1000. free_int_desc:
  1001. kfree(int_desc);
  1002. drop_reference:
  1003. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  1004. return_null_message:
  1005. msg->address_hi = 0;
  1006. msg->address_lo = 0;
  1007. msg->data = 0;
  1008. }
  1009. /* HW Interrupt Chip Descriptor */
  1010. static struct irq_chip hv_msi_irq_chip = {
  1011. .name = "Hyper-V PCIe MSI",
  1012. .irq_compose_msi_msg = hv_compose_msi_msg,
  1013. .irq_set_affinity = hv_set_affinity,
  1014. .irq_ack = irq_chip_ack_parent,
  1015. .irq_mask = hv_irq_mask,
  1016. .irq_unmask = hv_irq_unmask,
  1017. };
  1018. static irq_hw_number_t hv_msi_domain_ops_get_hwirq(struct msi_domain_info *info,
  1019. msi_alloc_info_t *arg)
  1020. {
  1021. return arg->msi_hwirq;
  1022. }
  1023. static struct msi_domain_ops hv_msi_ops = {
  1024. .get_hwirq = hv_msi_domain_ops_get_hwirq,
  1025. .msi_prepare = pci_msi_prepare,
  1026. .set_desc = pci_msi_set_desc,
  1027. .msi_free = hv_msi_free,
  1028. };
  1029. /**
  1030. * hv_pcie_init_irq_domain() - Initialize IRQ domain
  1031. * @hbus: The root PCI bus
  1032. *
  1033. * This function creates an IRQ domain which will be used for
  1034. * interrupts from devices that have been passed through. These
  1035. * devices only support MSI and MSI-X, not line-based interrupts
  1036. * or simulations of line-based interrupts through PCIe's
  1037. * fabric-layer messages. Because interrupts are remapped, we
  1038. * can support multi-message MSI here.
  1039. *
  1040. * Return: '0' on success and error value on failure
  1041. */
  1042. static int hv_pcie_init_irq_domain(struct hv_pcibus_device *hbus)
  1043. {
  1044. hbus->msi_info.chip = &hv_msi_irq_chip;
  1045. hbus->msi_info.ops = &hv_msi_ops;
  1046. hbus->msi_info.flags = (MSI_FLAG_USE_DEF_DOM_OPS |
  1047. MSI_FLAG_USE_DEF_CHIP_OPS | MSI_FLAG_MULTI_PCI_MSI |
  1048. MSI_FLAG_PCI_MSIX);
  1049. hbus->msi_info.handler = handle_edge_irq;
  1050. hbus->msi_info.handler_name = "edge";
  1051. hbus->msi_info.data = hbus;
  1052. hbus->irq_domain = pci_msi_create_irq_domain(hbus->sysdata.fwnode,
  1053. &hbus->msi_info,
  1054. x86_vector_domain);
  1055. if (!hbus->irq_domain) {
  1056. dev_err(&hbus->hdev->device,
  1057. "Failed to build an MSI IRQ domain\n");
  1058. return -ENODEV;
  1059. }
  1060. return 0;
  1061. }
  1062. /**
  1063. * get_bar_size() - Get the address space consumed by a BAR
  1064. * @bar_val: Value that a BAR returned after -1 was written
  1065. * to it.
  1066. *
  1067. * This function returns the size of the BAR, rounded up to 1
  1068. * page. It has to be rounded up because the hypervisor's page
  1069. * table entry that maps the BAR into the VM can't specify an
  1070. * offset within a page. The invariant is that the hypervisor
  1071. * must place any BARs of smaller than page length at the
  1072. * beginning of a page.
  1073. *
  1074. * Return: Size in bytes of the consumed MMIO space.
  1075. */
  1076. static u64 get_bar_size(u64 bar_val)
  1077. {
  1078. return round_up((1 + ~(bar_val & PCI_BASE_ADDRESS_MEM_MASK)),
  1079. PAGE_SIZE);
  1080. }
  1081. /**
  1082. * survey_child_resources() - Total all MMIO requirements
  1083. * @hbus: Root PCI bus, as understood by this driver
  1084. */
  1085. static void survey_child_resources(struct hv_pcibus_device *hbus)
  1086. {
  1087. struct list_head *iter;
  1088. struct hv_pci_dev *hpdev;
  1089. resource_size_t bar_size = 0;
  1090. unsigned long flags;
  1091. struct completion *event;
  1092. u64 bar_val;
  1093. int i;
  1094. /* If nobody is waiting on the answer, don't compute it. */
  1095. event = xchg(&hbus->survey_event, NULL);
  1096. if (!event)
  1097. return;
  1098. /* If the answer has already been computed, go with it. */
  1099. if (hbus->low_mmio_space || hbus->high_mmio_space) {
  1100. complete(event);
  1101. return;
  1102. }
  1103. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1104. /*
  1105. * Due to an interesting quirk of the PCI spec, all memory regions
  1106. * for a child device are a power of 2 in size and aligned in memory,
  1107. * so it's sufficient to just add them up without tracking alignment.
  1108. */
  1109. list_for_each(iter, &hbus->children) {
  1110. hpdev = container_of(iter, struct hv_pci_dev, list_entry);
  1111. for (i = 0; i < 6; i++) {
  1112. if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO)
  1113. dev_err(&hbus->hdev->device,
  1114. "There's an I/O BAR in this list!\n");
  1115. if (hpdev->probed_bar[i] != 0) {
  1116. /*
  1117. * A probed BAR has all the upper bits set that
  1118. * can be changed.
  1119. */
  1120. bar_val = hpdev->probed_bar[i];
  1121. if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
  1122. bar_val |=
  1123. ((u64)hpdev->probed_bar[++i] << 32);
  1124. else
  1125. bar_val |= 0xffffffff00000000ULL;
  1126. bar_size = get_bar_size(bar_val);
  1127. if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
  1128. hbus->high_mmio_space += bar_size;
  1129. else
  1130. hbus->low_mmio_space += bar_size;
  1131. }
  1132. }
  1133. }
  1134. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1135. complete(event);
  1136. }
  1137. /**
  1138. * prepopulate_bars() - Fill in BARs with defaults
  1139. * @hbus: Root PCI bus, as understood by this driver
  1140. *
  1141. * The core PCI driver code seems much, much happier if the BARs
  1142. * for a device have values upon first scan. So fill them in.
  1143. * The algorithm below works down from large sizes to small,
  1144. * attempting to pack the assignments optimally. The assumption,
  1145. * enforced in other parts of the code, is that the beginning of
  1146. * the memory-mapped I/O space will be aligned on the largest
  1147. * BAR size.
  1148. */
  1149. static void prepopulate_bars(struct hv_pcibus_device *hbus)
  1150. {
  1151. resource_size_t high_size = 0;
  1152. resource_size_t low_size = 0;
  1153. resource_size_t high_base = 0;
  1154. resource_size_t low_base = 0;
  1155. resource_size_t bar_size;
  1156. struct hv_pci_dev *hpdev;
  1157. struct list_head *iter;
  1158. unsigned long flags;
  1159. u64 bar_val;
  1160. u32 command;
  1161. bool high;
  1162. int i;
  1163. if (hbus->low_mmio_space) {
  1164. low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
  1165. low_base = hbus->low_mmio_res->start;
  1166. }
  1167. if (hbus->high_mmio_space) {
  1168. high_size = 1ULL <<
  1169. (63 - __builtin_clzll(hbus->high_mmio_space));
  1170. high_base = hbus->high_mmio_res->start;
  1171. }
  1172. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1173. /* Pick addresses for the BARs. */
  1174. do {
  1175. list_for_each(iter, &hbus->children) {
  1176. hpdev = container_of(iter, struct hv_pci_dev,
  1177. list_entry);
  1178. for (i = 0; i < 6; i++) {
  1179. bar_val = hpdev->probed_bar[i];
  1180. if (bar_val == 0)
  1181. continue;
  1182. high = bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64;
  1183. if (high) {
  1184. bar_val |=
  1185. ((u64)hpdev->probed_bar[i + 1]
  1186. << 32);
  1187. } else {
  1188. bar_val |= 0xffffffffULL << 32;
  1189. }
  1190. bar_size = get_bar_size(bar_val);
  1191. if (high) {
  1192. if (high_size != bar_size) {
  1193. i++;
  1194. continue;
  1195. }
  1196. _hv_pcifront_write_config(hpdev,
  1197. PCI_BASE_ADDRESS_0 + (4 * i),
  1198. 4,
  1199. (u32)(high_base & 0xffffff00));
  1200. i++;
  1201. _hv_pcifront_write_config(hpdev,
  1202. PCI_BASE_ADDRESS_0 + (4 * i),
  1203. 4, (u32)(high_base >> 32));
  1204. high_base += bar_size;
  1205. } else {
  1206. if (low_size != bar_size)
  1207. continue;
  1208. _hv_pcifront_write_config(hpdev,
  1209. PCI_BASE_ADDRESS_0 + (4 * i),
  1210. 4,
  1211. (u32)(low_base & 0xffffff00));
  1212. low_base += bar_size;
  1213. }
  1214. }
  1215. if (high_size <= 1 && low_size <= 1) {
  1216. /* Set the memory enable bit. */
  1217. _hv_pcifront_read_config(hpdev, PCI_COMMAND, 2,
  1218. &command);
  1219. command |= PCI_COMMAND_MEMORY;
  1220. _hv_pcifront_write_config(hpdev, PCI_COMMAND, 2,
  1221. command);
  1222. break;
  1223. }
  1224. }
  1225. high_size >>= 1;
  1226. low_size >>= 1;
  1227. } while (high_size || low_size);
  1228. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1229. }
  1230. /**
  1231. * create_root_hv_pci_bus() - Expose a new root PCI bus
  1232. * @hbus: Root PCI bus, as understood by this driver
  1233. *
  1234. * Return: 0 on success, -errno on failure
  1235. */
  1236. static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus)
  1237. {
  1238. /* Register the device */
  1239. hbus->pci_bus = pci_create_root_bus(&hbus->hdev->device,
  1240. 0, /* bus number is always zero */
  1241. &hv_pcifront_ops,
  1242. &hbus->sysdata,
  1243. &hbus->resources_for_children);
  1244. if (!hbus->pci_bus)
  1245. return -ENODEV;
  1246. hbus->pci_bus->msi = &hbus->msi_chip;
  1247. hbus->pci_bus->msi->dev = &hbus->hdev->device;
  1248. pci_lock_rescan_remove();
  1249. pci_scan_child_bus(hbus->pci_bus);
  1250. pci_bus_assign_resources(hbus->pci_bus);
  1251. pci_bus_add_devices(hbus->pci_bus);
  1252. pci_unlock_rescan_remove();
  1253. hbus->state = hv_pcibus_installed;
  1254. return 0;
  1255. }
  1256. struct q_res_req_compl {
  1257. struct completion host_event;
  1258. struct hv_pci_dev *hpdev;
  1259. };
  1260. /**
  1261. * q_resource_requirements() - Query Resource Requirements
  1262. * @context: The completion context.
  1263. * @resp: The response that came from the host.
  1264. * @resp_packet_size: The size in bytes of resp.
  1265. *
  1266. * This function is invoked on completion of a Query Resource
  1267. * Requirements packet.
  1268. */
  1269. static void q_resource_requirements(void *context, struct pci_response *resp,
  1270. int resp_packet_size)
  1271. {
  1272. struct q_res_req_compl *completion = context;
  1273. struct pci_q_res_req_response *q_res_req =
  1274. (struct pci_q_res_req_response *)resp;
  1275. int i;
  1276. if (resp->status < 0) {
  1277. dev_err(&completion->hpdev->hbus->hdev->device,
  1278. "query resource requirements failed: %x\n",
  1279. resp->status);
  1280. } else {
  1281. for (i = 0; i < 6; i++) {
  1282. completion->hpdev->probed_bar[i] =
  1283. q_res_req->probed_bar[i];
  1284. }
  1285. }
  1286. complete(&completion->host_event);
  1287. }
  1288. static void get_pcichild(struct hv_pci_dev *hpdev,
  1289. enum hv_pcidev_ref_reason reason)
  1290. {
  1291. refcount_inc(&hpdev->refs);
  1292. }
  1293. static void put_pcichild(struct hv_pci_dev *hpdev,
  1294. enum hv_pcidev_ref_reason reason)
  1295. {
  1296. if (refcount_dec_and_test(&hpdev->refs))
  1297. kfree(hpdev);
  1298. }
  1299. /**
  1300. * new_pcichild_device() - Create a new child device
  1301. * @hbus: The internal struct tracking this root PCI bus.
  1302. * @desc: The information supplied so far from the host
  1303. * about the device.
  1304. *
  1305. * This function creates the tracking structure for a new child
  1306. * device and kicks off the process of figuring out what it is.
  1307. *
  1308. * Return: Pointer to the new tracking struct
  1309. */
  1310. static struct hv_pci_dev *new_pcichild_device(struct hv_pcibus_device *hbus,
  1311. struct pci_function_description *desc)
  1312. {
  1313. struct hv_pci_dev *hpdev;
  1314. struct pci_child_message *res_req;
  1315. struct q_res_req_compl comp_pkt;
  1316. struct {
  1317. struct pci_packet init_packet;
  1318. u8 buffer[sizeof(struct pci_child_message)];
  1319. } pkt;
  1320. unsigned long flags;
  1321. int ret;
  1322. hpdev = kzalloc(sizeof(*hpdev), GFP_ATOMIC);
  1323. if (!hpdev)
  1324. return NULL;
  1325. hpdev->hbus = hbus;
  1326. memset(&pkt, 0, sizeof(pkt));
  1327. init_completion(&comp_pkt.host_event);
  1328. comp_pkt.hpdev = hpdev;
  1329. pkt.init_packet.compl_ctxt = &comp_pkt;
  1330. pkt.init_packet.completion_func = q_resource_requirements;
  1331. res_req = (struct pci_child_message *)&pkt.init_packet.message;
  1332. res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS;
  1333. res_req->wslot.slot = desc->win_slot.slot;
  1334. ret = vmbus_sendpacket(hbus->hdev->channel, res_req,
  1335. sizeof(struct pci_child_message),
  1336. (unsigned long)&pkt.init_packet,
  1337. VM_PKT_DATA_INBAND,
  1338. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1339. if (ret)
  1340. goto error;
  1341. wait_for_completion(&comp_pkt.host_event);
  1342. hpdev->desc = *desc;
  1343. refcount_set(&hpdev->refs, 1);
  1344. get_pcichild(hpdev, hv_pcidev_ref_childlist);
  1345. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1346. /*
  1347. * When a device is being added to the bus, we set the PCI domain
  1348. * number to be the device serial number, which is non-zero and
  1349. * unique on the same VM. The serial numbers start with 1, and
  1350. * increase by 1 for each device. So device names including this
  1351. * can have shorter names than based on the bus instance UUID.
  1352. * Only the first device serial number is used for domain, so the
  1353. * domain number will not change after the first device is added.
  1354. */
  1355. if (list_empty(&hbus->children))
  1356. hbus->sysdata.domain = desc->ser;
  1357. list_add_tail(&hpdev->list_entry, &hbus->children);
  1358. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1359. return hpdev;
  1360. error:
  1361. kfree(hpdev);
  1362. return NULL;
  1363. }
  1364. /**
  1365. * get_pcichild_wslot() - Find device from slot
  1366. * @hbus: Root PCI bus, as understood by this driver
  1367. * @wslot: Location on the bus
  1368. *
  1369. * This function looks up a PCI device and returns the internal
  1370. * representation of it. It acquires a reference on it, so that
  1371. * the device won't be deleted while somebody is using it. The
  1372. * caller is responsible for calling put_pcichild() to release
  1373. * this reference.
  1374. *
  1375. * Return: Internal representation of a PCI device
  1376. */
  1377. static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
  1378. u32 wslot)
  1379. {
  1380. unsigned long flags;
  1381. struct hv_pci_dev *iter, *hpdev = NULL;
  1382. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1383. list_for_each_entry(iter, &hbus->children, list_entry) {
  1384. if (iter->desc.win_slot.slot == wslot) {
  1385. hpdev = iter;
  1386. get_pcichild(hpdev, hv_pcidev_ref_by_slot);
  1387. break;
  1388. }
  1389. }
  1390. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1391. return hpdev;
  1392. }
  1393. /**
  1394. * pci_devices_present_work() - Handle new list of child devices
  1395. * @work: Work struct embedded in struct hv_dr_work
  1396. *
  1397. * "Bus Relations" is the Windows term for "children of this
  1398. * bus." The terminology is preserved here for people trying to
  1399. * debug the interaction between Hyper-V and Linux. This
  1400. * function is called when the parent partition reports a list
  1401. * of functions that should be observed under this PCI Express
  1402. * port (bus).
  1403. *
  1404. * This function updates the list, and must tolerate being
  1405. * called multiple times with the same information. The typical
  1406. * number of child devices is one, with very atypical cases
  1407. * involving three or four, so the algorithms used here can be
  1408. * simple and inefficient.
  1409. *
  1410. * It must also treat the omission of a previously observed device as
  1411. * notification that the device no longer exists.
  1412. *
  1413. * Note that this function is a work item, and it may not be
  1414. * invoked in the order that it was queued. Back to back
  1415. * updates of the list of present devices may involve queuing
  1416. * multiple work items, and this one may run before ones that
  1417. * were sent later. As such, this function only does something
  1418. * if is the last one in the queue.
  1419. */
  1420. static void pci_devices_present_work(struct work_struct *work)
  1421. {
  1422. u32 child_no;
  1423. bool found;
  1424. struct list_head *iter;
  1425. struct pci_function_description *new_desc;
  1426. struct hv_pci_dev *hpdev;
  1427. struct hv_pcibus_device *hbus;
  1428. struct list_head removed;
  1429. struct hv_dr_work *dr_wrk;
  1430. struct hv_dr_state *dr = NULL;
  1431. unsigned long flags;
  1432. dr_wrk = container_of(work, struct hv_dr_work, wrk);
  1433. hbus = dr_wrk->bus;
  1434. kfree(dr_wrk);
  1435. INIT_LIST_HEAD(&removed);
  1436. if (down_interruptible(&hbus->enum_sem)) {
  1437. put_hvpcibus(hbus);
  1438. return;
  1439. }
  1440. /* Pull this off the queue and process it if it was the last one. */
  1441. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1442. while (!list_empty(&hbus->dr_list)) {
  1443. dr = list_first_entry(&hbus->dr_list, struct hv_dr_state,
  1444. list_entry);
  1445. list_del(&dr->list_entry);
  1446. /* Throw this away if the list still has stuff in it. */
  1447. if (!list_empty(&hbus->dr_list)) {
  1448. kfree(dr);
  1449. continue;
  1450. }
  1451. }
  1452. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1453. if (!dr) {
  1454. up(&hbus->enum_sem);
  1455. put_hvpcibus(hbus);
  1456. return;
  1457. }
  1458. /* First, mark all existing children as reported missing. */
  1459. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1460. list_for_each(iter, &hbus->children) {
  1461. hpdev = container_of(iter, struct hv_pci_dev,
  1462. list_entry);
  1463. hpdev->reported_missing = true;
  1464. }
  1465. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1466. /* Next, add back any reported devices. */
  1467. for (child_no = 0; child_no < dr->device_count; child_no++) {
  1468. found = false;
  1469. new_desc = &dr->func[child_no];
  1470. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1471. list_for_each(iter, &hbus->children) {
  1472. hpdev = container_of(iter, struct hv_pci_dev,
  1473. list_entry);
  1474. if ((hpdev->desc.win_slot.slot ==
  1475. new_desc->win_slot.slot) &&
  1476. (hpdev->desc.v_id == new_desc->v_id) &&
  1477. (hpdev->desc.d_id == new_desc->d_id) &&
  1478. (hpdev->desc.ser == new_desc->ser)) {
  1479. hpdev->reported_missing = false;
  1480. found = true;
  1481. }
  1482. }
  1483. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1484. if (!found) {
  1485. hpdev = new_pcichild_device(hbus, new_desc);
  1486. if (!hpdev)
  1487. dev_err(&hbus->hdev->device,
  1488. "couldn't record a child device.\n");
  1489. }
  1490. }
  1491. /* Move missing children to a list on the stack. */
  1492. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1493. do {
  1494. found = false;
  1495. list_for_each(iter, &hbus->children) {
  1496. hpdev = container_of(iter, struct hv_pci_dev,
  1497. list_entry);
  1498. if (hpdev->reported_missing) {
  1499. found = true;
  1500. put_pcichild(hpdev, hv_pcidev_ref_childlist);
  1501. list_move_tail(&hpdev->list_entry, &removed);
  1502. break;
  1503. }
  1504. }
  1505. } while (found);
  1506. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1507. /* Delete everything that should no longer exist. */
  1508. while (!list_empty(&removed)) {
  1509. hpdev = list_first_entry(&removed, struct hv_pci_dev,
  1510. list_entry);
  1511. list_del(&hpdev->list_entry);
  1512. put_pcichild(hpdev, hv_pcidev_ref_initial);
  1513. }
  1514. switch (hbus->state) {
  1515. case hv_pcibus_installed:
  1516. /*
  1517. * Tell the core to rescan bus
  1518. * because there may have been changes.
  1519. */
  1520. pci_lock_rescan_remove();
  1521. pci_scan_child_bus(hbus->pci_bus);
  1522. pci_unlock_rescan_remove();
  1523. break;
  1524. case hv_pcibus_init:
  1525. case hv_pcibus_probed:
  1526. survey_child_resources(hbus);
  1527. break;
  1528. default:
  1529. break;
  1530. }
  1531. up(&hbus->enum_sem);
  1532. put_hvpcibus(hbus);
  1533. kfree(dr);
  1534. }
  1535. /**
  1536. * hv_pci_devices_present() - Handles list of new children
  1537. * @hbus: Root PCI bus, as understood by this driver
  1538. * @relations: Packet from host listing children
  1539. *
  1540. * This function is invoked whenever a new list of devices for
  1541. * this bus appears.
  1542. */
  1543. static void hv_pci_devices_present(struct hv_pcibus_device *hbus,
  1544. struct pci_bus_relations *relations)
  1545. {
  1546. struct hv_dr_state *dr;
  1547. struct hv_dr_work *dr_wrk;
  1548. unsigned long flags;
  1549. dr_wrk = kzalloc(sizeof(*dr_wrk), GFP_NOWAIT);
  1550. if (!dr_wrk)
  1551. return;
  1552. dr = kzalloc(offsetof(struct hv_dr_state, func) +
  1553. (sizeof(struct pci_function_description) *
  1554. (relations->device_count)), GFP_NOWAIT);
  1555. if (!dr) {
  1556. kfree(dr_wrk);
  1557. return;
  1558. }
  1559. INIT_WORK(&dr_wrk->wrk, pci_devices_present_work);
  1560. dr_wrk->bus = hbus;
  1561. dr->device_count = relations->device_count;
  1562. if (dr->device_count != 0) {
  1563. memcpy(dr->func, relations->func,
  1564. sizeof(struct pci_function_description) *
  1565. dr->device_count);
  1566. }
  1567. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1568. list_add_tail(&dr->list_entry, &hbus->dr_list);
  1569. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1570. get_hvpcibus(hbus);
  1571. schedule_work(&dr_wrk->wrk);
  1572. }
  1573. /**
  1574. * hv_eject_device_work() - Asynchronously handles ejection
  1575. * @work: Work struct embedded in internal device struct
  1576. *
  1577. * This function handles ejecting a device. Windows will
  1578. * attempt to gracefully eject a device, waiting 60 seconds to
  1579. * hear back from the guest OS that this completed successfully.
  1580. * If this timer expires, the device will be forcibly removed.
  1581. */
  1582. static void hv_eject_device_work(struct work_struct *work)
  1583. {
  1584. struct pci_eject_response *ejct_pkt;
  1585. struct hv_pci_dev *hpdev;
  1586. struct pci_dev *pdev;
  1587. unsigned long flags;
  1588. int wslot;
  1589. struct {
  1590. struct pci_packet pkt;
  1591. u8 buffer[sizeof(struct pci_eject_response)];
  1592. } ctxt;
  1593. hpdev = container_of(work, struct hv_pci_dev, wrk);
  1594. if (hpdev->state != hv_pcichild_ejecting) {
  1595. put_pcichild(hpdev, hv_pcidev_ref_pnp);
  1596. return;
  1597. }
  1598. /*
  1599. * Ejection can come before or after the PCI bus has been set up, so
  1600. * attempt to find it and tear down the bus state, if it exists. This
  1601. * must be done without constructs like pci_domain_nr(hbus->pci_bus)
  1602. * because hbus->pci_bus may not exist yet.
  1603. */
  1604. wslot = wslot_to_devfn(hpdev->desc.win_slot.slot);
  1605. pdev = pci_get_domain_bus_and_slot(hpdev->hbus->sysdata.domain, 0,
  1606. wslot);
  1607. if (pdev) {
  1608. pci_lock_rescan_remove();
  1609. pci_stop_and_remove_bus_device(pdev);
  1610. pci_dev_put(pdev);
  1611. pci_unlock_rescan_remove();
  1612. }
  1613. spin_lock_irqsave(&hpdev->hbus->device_list_lock, flags);
  1614. list_del(&hpdev->list_entry);
  1615. spin_unlock_irqrestore(&hpdev->hbus->device_list_lock, flags);
  1616. memset(&ctxt, 0, sizeof(ctxt));
  1617. ejct_pkt = (struct pci_eject_response *)&ctxt.pkt.message;
  1618. ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE;
  1619. ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot;
  1620. vmbus_sendpacket(hpdev->hbus->hdev->channel, ejct_pkt,
  1621. sizeof(*ejct_pkt), (unsigned long)&ctxt.pkt,
  1622. VM_PKT_DATA_INBAND, 0);
  1623. put_pcichild(hpdev, hv_pcidev_ref_childlist);
  1624. put_pcichild(hpdev, hv_pcidev_ref_pnp);
  1625. put_hvpcibus(hpdev->hbus);
  1626. }
  1627. /**
  1628. * hv_pci_eject_device() - Handles device ejection
  1629. * @hpdev: Internal device tracking struct
  1630. *
  1631. * This function is invoked when an ejection packet arrives. It
  1632. * just schedules work so that we don't re-enter the packet
  1633. * delivery code handling the ejection.
  1634. */
  1635. static void hv_pci_eject_device(struct hv_pci_dev *hpdev)
  1636. {
  1637. hpdev->state = hv_pcichild_ejecting;
  1638. get_pcichild(hpdev, hv_pcidev_ref_pnp);
  1639. INIT_WORK(&hpdev->wrk, hv_eject_device_work);
  1640. get_hvpcibus(hpdev->hbus);
  1641. schedule_work(&hpdev->wrk);
  1642. }
  1643. /**
  1644. * hv_pci_onchannelcallback() - Handles incoming packets
  1645. * @context: Internal bus tracking struct
  1646. *
  1647. * This function is invoked whenever the host sends a packet to
  1648. * this channel (which is private to this root PCI bus).
  1649. */
  1650. static void hv_pci_onchannelcallback(void *context)
  1651. {
  1652. const int packet_size = 0x100;
  1653. int ret;
  1654. struct hv_pcibus_device *hbus = context;
  1655. u32 bytes_recvd;
  1656. u64 req_id;
  1657. struct vmpacket_descriptor *desc;
  1658. unsigned char *buffer;
  1659. int bufferlen = packet_size;
  1660. struct pci_packet *comp_packet;
  1661. struct pci_response *response;
  1662. struct pci_incoming_message *new_message;
  1663. struct pci_bus_relations *bus_rel;
  1664. struct pci_dev_incoming *dev_message;
  1665. struct hv_pci_dev *hpdev;
  1666. buffer = kmalloc(bufferlen, GFP_ATOMIC);
  1667. if (!buffer)
  1668. return;
  1669. while (1) {
  1670. ret = vmbus_recvpacket_raw(hbus->hdev->channel, buffer,
  1671. bufferlen, &bytes_recvd, &req_id);
  1672. if (ret == -ENOBUFS) {
  1673. kfree(buffer);
  1674. /* Handle large packet */
  1675. bufferlen = bytes_recvd;
  1676. buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
  1677. if (!buffer)
  1678. return;
  1679. continue;
  1680. }
  1681. /* Zero length indicates there are no more packets. */
  1682. if (ret || !bytes_recvd)
  1683. break;
  1684. /*
  1685. * All incoming packets must be at least as large as a
  1686. * response.
  1687. */
  1688. if (bytes_recvd <= sizeof(struct pci_response))
  1689. continue;
  1690. desc = (struct vmpacket_descriptor *)buffer;
  1691. switch (desc->type) {
  1692. case VM_PKT_COMP:
  1693. /*
  1694. * The host is trusted, and thus it's safe to interpret
  1695. * this transaction ID as a pointer.
  1696. */
  1697. comp_packet = (struct pci_packet *)req_id;
  1698. response = (struct pci_response *)buffer;
  1699. comp_packet->completion_func(comp_packet->compl_ctxt,
  1700. response,
  1701. bytes_recvd);
  1702. break;
  1703. case VM_PKT_DATA_INBAND:
  1704. new_message = (struct pci_incoming_message *)buffer;
  1705. switch (new_message->message_type.type) {
  1706. case PCI_BUS_RELATIONS:
  1707. bus_rel = (struct pci_bus_relations *)buffer;
  1708. if (bytes_recvd <
  1709. offsetof(struct pci_bus_relations, func) +
  1710. (sizeof(struct pci_function_description) *
  1711. (bus_rel->device_count))) {
  1712. dev_err(&hbus->hdev->device,
  1713. "bus relations too small\n");
  1714. break;
  1715. }
  1716. hv_pci_devices_present(hbus, bus_rel);
  1717. break;
  1718. case PCI_EJECT:
  1719. dev_message = (struct pci_dev_incoming *)buffer;
  1720. hpdev = get_pcichild_wslot(hbus,
  1721. dev_message->wslot.slot);
  1722. if (hpdev) {
  1723. hv_pci_eject_device(hpdev);
  1724. put_pcichild(hpdev,
  1725. hv_pcidev_ref_by_slot);
  1726. }
  1727. break;
  1728. default:
  1729. dev_warn(&hbus->hdev->device,
  1730. "Unimplemented protocol message %x\n",
  1731. new_message->message_type.type);
  1732. break;
  1733. }
  1734. break;
  1735. default:
  1736. dev_err(&hbus->hdev->device,
  1737. "unhandled packet type %d, tid %llx len %d\n",
  1738. desc->type, req_id, bytes_recvd);
  1739. break;
  1740. }
  1741. }
  1742. kfree(buffer);
  1743. }
  1744. /**
  1745. * hv_pci_protocol_negotiation() - Set up protocol
  1746. * @hdev: VMBus's tracking struct for this root PCI bus
  1747. *
  1748. * This driver is intended to support running on Windows 10
  1749. * (server) and later versions. It will not run on earlier
  1750. * versions, as they assume that many of the operations which
  1751. * Linux needs accomplished with a spinlock held were done via
  1752. * asynchronous messaging via VMBus. Windows 10 increases the
  1753. * surface area of PCI emulation so that these actions can take
  1754. * place by suspending a virtual processor for their duration.
  1755. *
  1756. * This function negotiates the channel protocol version,
  1757. * failing if the host doesn't support the necessary protocol
  1758. * level.
  1759. */
  1760. static int hv_pci_protocol_negotiation(struct hv_device *hdev)
  1761. {
  1762. struct pci_version_request *version_req;
  1763. struct hv_pci_compl comp_pkt;
  1764. struct pci_packet *pkt;
  1765. int ret;
  1766. int i;
  1767. /*
  1768. * Initiate the handshake with the host and negotiate
  1769. * a version that the host can support. We start with the
  1770. * highest version number and go down if the host cannot
  1771. * support it.
  1772. */
  1773. pkt = kzalloc(sizeof(*pkt) + sizeof(*version_req), GFP_KERNEL);
  1774. if (!pkt)
  1775. return -ENOMEM;
  1776. init_completion(&comp_pkt.host_event);
  1777. pkt->completion_func = hv_pci_generic_compl;
  1778. pkt->compl_ctxt = &comp_pkt;
  1779. version_req = (struct pci_version_request *)&pkt->message;
  1780. version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION;
  1781. for (i = 0; i < ARRAY_SIZE(pci_protocol_versions); i++) {
  1782. version_req->protocol_version = pci_protocol_versions[i];
  1783. ret = vmbus_sendpacket(hdev->channel, version_req,
  1784. sizeof(struct pci_version_request),
  1785. (unsigned long)pkt, VM_PKT_DATA_INBAND,
  1786. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1787. if (ret) {
  1788. dev_err(&hdev->device,
  1789. "PCI Pass-through VSP failed sending version reqquest: %#x",
  1790. ret);
  1791. goto exit;
  1792. }
  1793. wait_for_completion(&comp_pkt.host_event);
  1794. if (comp_pkt.completion_status >= 0) {
  1795. pci_protocol_version = pci_protocol_versions[i];
  1796. dev_info(&hdev->device,
  1797. "PCI VMBus probing: Using version %#x\n",
  1798. pci_protocol_version);
  1799. goto exit;
  1800. }
  1801. if (comp_pkt.completion_status != STATUS_REVISION_MISMATCH) {
  1802. dev_err(&hdev->device,
  1803. "PCI Pass-through VSP failed version request: %#x",
  1804. comp_pkt.completion_status);
  1805. ret = -EPROTO;
  1806. goto exit;
  1807. }
  1808. reinit_completion(&comp_pkt.host_event);
  1809. }
  1810. dev_err(&hdev->device,
  1811. "PCI pass-through VSP failed to find supported version");
  1812. ret = -EPROTO;
  1813. exit:
  1814. kfree(pkt);
  1815. return ret;
  1816. }
  1817. /**
  1818. * hv_pci_free_bridge_windows() - Release memory regions for the
  1819. * bus
  1820. * @hbus: Root PCI bus, as understood by this driver
  1821. */
  1822. static void hv_pci_free_bridge_windows(struct hv_pcibus_device *hbus)
  1823. {
  1824. /*
  1825. * Set the resources back to the way they looked when they
  1826. * were allocated by setting IORESOURCE_BUSY again.
  1827. */
  1828. if (hbus->low_mmio_space && hbus->low_mmio_res) {
  1829. hbus->low_mmio_res->flags |= IORESOURCE_BUSY;
  1830. vmbus_free_mmio(hbus->low_mmio_res->start,
  1831. resource_size(hbus->low_mmio_res));
  1832. }
  1833. if (hbus->high_mmio_space && hbus->high_mmio_res) {
  1834. hbus->high_mmio_res->flags |= IORESOURCE_BUSY;
  1835. vmbus_free_mmio(hbus->high_mmio_res->start,
  1836. resource_size(hbus->high_mmio_res));
  1837. }
  1838. }
  1839. /**
  1840. * hv_pci_allocate_bridge_windows() - Allocate memory regions
  1841. * for the bus
  1842. * @hbus: Root PCI bus, as understood by this driver
  1843. *
  1844. * This function calls vmbus_allocate_mmio(), which is itself a
  1845. * bit of a compromise. Ideally, we might change the pnp layer
  1846. * in the kernel such that it comprehends either PCI devices
  1847. * which are "grandchildren of ACPI," with some intermediate bus
  1848. * node (in this case, VMBus) or change it such that it
  1849. * understands VMBus. The pnp layer, however, has been declared
  1850. * deprecated, and not subject to change.
  1851. *
  1852. * The workaround, implemented here, is to ask VMBus to allocate
  1853. * MMIO space for this bus. VMBus itself knows which ranges are
  1854. * appropriate by looking at its own ACPI objects. Then, after
  1855. * these ranges are claimed, they're modified to look like they
  1856. * would have looked if the ACPI and pnp code had allocated
  1857. * bridge windows. These descriptors have to exist in this form
  1858. * in order to satisfy the code which will get invoked when the
  1859. * endpoint PCI function driver calls request_mem_region() or
  1860. * request_mem_region_exclusive().
  1861. *
  1862. * Return: 0 on success, -errno on failure
  1863. */
  1864. static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device *hbus)
  1865. {
  1866. resource_size_t align;
  1867. int ret;
  1868. if (hbus->low_mmio_space) {
  1869. align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
  1870. ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0,
  1871. (u64)(u32)0xffffffff,
  1872. hbus->low_mmio_space,
  1873. align, false);
  1874. if (ret) {
  1875. dev_err(&hbus->hdev->device,
  1876. "Need %#llx of low MMIO space. Consider reconfiguring the VM.\n",
  1877. hbus->low_mmio_space);
  1878. return ret;
  1879. }
  1880. /* Modify this resource to become a bridge window. */
  1881. hbus->low_mmio_res->flags |= IORESOURCE_WINDOW;
  1882. hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY;
  1883. pci_add_resource(&hbus->resources_for_children,
  1884. hbus->low_mmio_res);
  1885. }
  1886. if (hbus->high_mmio_space) {
  1887. align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space));
  1888. ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev,
  1889. 0x100000000, -1,
  1890. hbus->high_mmio_space, align,
  1891. false);
  1892. if (ret) {
  1893. dev_err(&hbus->hdev->device,
  1894. "Need %#llx of high MMIO space. Consider reconfiguring the VM.\n",
  1895. hbus->high_mmio_space);
  1896. goto release_low_mmio;
  1897. }
  1898. /* Modify this resource to become a bridge window. */
  1899. hbus->high_mmio_res->flags |= IORESOURCE_WINDOW;
  1900. hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY;
  1901. pci_add_resource(&hbus->resources_for_children,
  1902. hbus->high_mmio_res);
  1903. }
  1904. return 0;
  1905. release_low_mmio:
  1906. if (hbus->low_mmio_res) {
  1907. vmbus_free_mmio(hbus->low_mmio_res->start,
  1908. resource_size(hbus->low_mmio_res));
  1909. }
  1910. return ret;
  1911. }
  1912. /**
  1913. * hv_allocate_config_window() - Find MMIO space for PCI Config
  1914. * @hbus: Root PCI bus, as understood by this driver
  1915. *
  1916. * This function claims memory-mapped I/O space for accessing
  1917. * configuration space for the functions on this bus.
  1918. *
  1919. * Return: 0 on success, -errno on failure
  1920. */
  1921. static int hv_allocate_config_window(struct hv_pcibus_device *hbus)
  1922. {
  1923. int ret;
  1924. /*
  1925. * Set up a region of MMIO space to use for accessing configuration
  1926. * space.
  1927. */
  1928. ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1,
  1929. PCI_CONFIG_MMIO_LENGTH, 0x1000, false);
  1930. if (ret)
  1931. return ret;
  1932. /*
  1933. * vmbus_allocate_mmio() gets used for allocating both device endpoint
  1934. * resource claims (those which cannot be overlapped) and the ranges
  1935. * which are valid for the children of this bus, which are intended
  1936. * to be overlapped by those children. Set the flag on this claim
  1937. * meaning that this region can't be overlapped.
  1938. */
  1939. hbus->mem_config->flags |= IORESOURCE_BUSY;
  1940. return 0;
  1941. }
  1942. static void hv_free_config_window(struct hv_pcibus_device *hbus)
  1943. {
  1944. vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH);
  1945. }
  1946. /**
  1947. * hv_pci_enter_d0() - Bring the "bus" into the D0 power state
  1948. * @hdev: VMBus's tracking struct for this root PCI bus
  1949. *
  1950. * Return: 0 on success, -errno on failure
  1951. */
  1952. static int hv_pci_enter_d0(struct hv_device *hdev)
  1953. {
  1954. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  1955. struct pci_bus_d0_entry *d0_entry;
  1956. struct hv_pci_compl comp_pkt;
  1957. struct pci_packet *pkt;
  1958. int ret;
  1959. /*
  1960. * Tell the host that the bus is ready to use, and moved into the
  1961. * powered-on state. This includes telling the host which region
  1962. * of memory-mapped I/O space has been chosen for configuration space
  1963. * access.
  1964. */
  1965. pkt = kzalloc(sizeof(*pkt) + sizeof(*d0_entry), GFP_KERNEL);
  1966. if (!pkt)
  1967. return -ENOMEM;
  1968. init_completion(&comp_pkt.host_event);
  1969. pkt->completion_func = hv_pci_generic_compl;
  1970. pkt->compl_ctxt = &comp_pkt;
  1971. d0_entry = (struct pci_bus_d0_entry *)&pkt->message;
  1972. d0_entry->message_type.type = PCI_BUS_D0ENTRY;
  1973. d0_entry->mmio_base = hbus->mem_config->start;
  1974. ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry),
  1975. (unsigned long)pkt, VM_PKT_DATA_INBAND,
  1976. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1977. if (ret)
  1978. goto exit;
  1979. wait_for_completion(&comp_pkt.host_event);
  1980. if (comp_pkt.completion_status < 0) {
  1981. dev_err(&hdev->device,
  1982. "PCI Pass-through VSP failed D0 Entry with status %x\n",
  1983. comp_pkt.completion_status);
  1984. ret = -EPROTO;
  1985. goto exit;
  1986. }
  1987. ret = 0;
  1988. exit:
  1989. kfree(pkt);
  1990. return ret;
  1991. }
  1992. /**
  1993. * hv_pci_query_relations() - Ask host to send list of child
  1994. * devices
  1995. * @hdev: VMBus's tracking struct for this root PCI bus
  1996. *
  1997. * Return: 0 on success, -errno on failure
  1998. */
  1999. static int hv_pci_query_relations(struct hv_device *hdev)
  2000. {
  2001. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  2002. struct pci_message message;
  2003. struct completion comp;
  2004. int ret;
  2005. /* Ask the host to send along the list of child devices */
  2006. init_completion(&comp);
  2007. if (cmpxchg(&hbus->survey_event, NULL, &comp))
  2008. return -ENOTEMPTY;
  2009. memset(&message, 0, sizeof(message));
  2010. message.type = PCI_QUERY_BUS_RELATIONS;
  2011. ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message),
  2012. 0, VM_PKT_DATA_INBAND, 0);
  2013. if (ret)
  2014. return ret;
  2015. wait_for_completion(&comp);
  2016. return 0;
  2017. }
  2018. /**
  2019. * hv_send_resources_allocated() - Report local resource choices
  2020. * @hdev: VMBus's tracking struct for this root PCI bus
  2021. *
  2022. * The host OS is expecting to be sent a request as a message
  2023. * which contains all the resources that the device will use.
  2024. * The response contains those same resources, "translated"
  2025. * which is to say, the values which should be used by the
  2026. * hardware, when it delivers an interrupt. (MMIO resources are
  2027. * used in local terms.) This is nice for Windows, and lines up
  2028. * with the FDO/PDO split, which doesn't exist in Linux. Linux
  2029. * is deeply expecting to scan an emulated PCI configuration
  2030. * space. So this message is sent here only to drive the state
  2031. * machine on the host forward.
  2032. *
  2033. * Return: 0 on success, -errno on failure
  2034. */
  2035. static int hv_send_resources_allocated(struct hv_device *hdev)
  2036. {
  2037. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  2038. struct pci_resources_assigned *res_assigned;
  2039. struct pci_resources_assigned2 *res_assigned2;
  2040. struct hv_pci_compl comp_pkt;
  2041. struct hv_pci_dev *hpdev;
  2042. struct pci_packet *pkt;
  2043. size_t size_res;
  2044. u32 wslot;
  2045. int ret;
  2046. size_res = (pci_protocol_version < PCI_PROTOCOL_VERSION_1_2)
  2047. ? sizeof(*res_assigned) : sizeof(*res_assigned2);
  2048. pkt = kmalloc(sizeof(*pkt) + size_res, GFP_KERNEL);
  2049. if (!pkt)
  2050. return -ENOMEM;
  2051. ret = 0;
  2052. for (wslot = 0; wslot < 256; wslot++) {
  2053. hpdev = get_pcichild_wslot(hbus, wslot);
  2054. if (!hpdev)
  2055. continue;
  2056. memset(pkt, 0, sizeof(*pkt) + size_res);
  2057. init_completion(&comp_pkt.host_event);
  2058. pkt->completion_func = hv_pci_generic_compl;
  2059. pkt->compl_ctxt = &comp_pkt;
  2060. if (pci_protocol_version < PCI_PROTOCOL_VERSION_1_2) {
  2061. res_assigned =
  2062. (struct pci_resources_assigned *)&pkt->message;
  2063. res_assigned->message_type.type =
  2064. PCI_RESOURCES_ASSIGNED;
  2065. res_assigned->wslot.slot = hpdev->desc.win_slot.slot;
  2066. } else {
  2067. res_assigned2 =
  2068. (struct pci_resources_assigned2 *)&pkt->message;
  2069. res_assigned2->message_type.type =
  2070. PCI_RESOURCES_ASSIGNED2;
  2071. res_assigned2->wslot.slot = hpdev->desc.win_slot.slot;
  2072. }
  2073. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  2074. ret = vmbus_sendpacket(hdev->channel, &pkt->message,
  2075. size_res, (unsigned long)pkt,
  2076. VM_PKT_DATA_INBAND,
  2077. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  2078. if (ret)
  2079. break;
  2080. wait_for_completion(&comp_pkt.host_event);
  2081. if (comp_pkt.completion_status < 0) {
  2082. ret = -EPROTO;
  2083. dev_err(&hdev->device,
  2084. "resource allocated returned 0x%x",
  2085. comp_pkt.completion_status);
  2086. break;
  2087. }
  2088. }
  2089. kfree(pkt);
  2090. return ret;
  2091. }
  2092. /**
  2093. * hv_send_resources_released() - Report local resources
  2094. * released
  2095. * @hdev: VMBus's tracking struct for this root PCI bus
  2096. *
  2097. * Return: 0 on success, -errno on failure
  2098. */
  2099. static int hv_send_resources_released(struct hv_device *hdev)
  2100. {
  2101. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  2102. struct pci_child_message pkt;
  2103. struct hv_pci_dev *hpdev;
  2104. u32 wslot;
  2105. int ret;
  2106. for (wslot = 0; wslot < 256; wslot++) {
  2107. hpdev = get_pcichild_wslot(hbus, wslot);
  2108. if (!hpdev)
  2109. continue;
  2110. memset(&pkt, 0, sizeof(pkt));
  2111. pkt.message_type.type = PCI_RESOURCES_RELEASED;
  2112. pkt.wslot.slot = hpdev->desc.win_slot.slot;
  2113. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  2114. ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0,
  2115. VM_PKT_DATA_INBAND, 0);
  2116. if (ret)
  2117. return ret;
  2118. }
  2119. return 0;
  2120. }
  2121. static void get_hvpcibus(struct hv_pcibus_device *hbus)
  2122. {
  2123. atomic_inc(&hbus->remove_lock);
  2124. }
  2125. static void put_hvpcibus(struct hv_pcibus_device *hbus)
  2126. {
  2127. if (atomic_dec_and_test(&hbus->remove_lock))
  2128. complete(&hbus->remove_event);
  2129. }
  2130. /**
  2131. * hv_pci_probe() - New VMBus channel probe, for a root PCI bus
  2132. * @hdev: VMBus's tracking struct for this root PCI bus
  2133. * @dev_id: Identifies the device itself
  2134. *
  2135. * Return: 0 on success, -errno on failure
  2136. */
  2137. static int hv_pci_probe(struct hv_device *hdev,
  2138. const struct hv_vmbus_device_id *dev_id)
  2139. {
  2140. struct hv_pcibus_device *hbus;
  2141. int ret;
  2142. /*
  2143. * hv_pcibus_device contains the hypercall arguments for retargeting in
  2144. * hv_irq_unmask(). Those must not cross a page boundary.
  2145. */
  2146. BUILD_BUG_ON(sizeof(*hbus) > PAGE_SIZE);
  2147. hbus = (struct hv_pcibus_device *)get_zeroed_page(GFP_KERNEL);
  2148. if (!hbus)
  2149. return -ENOMEM;
  2150. hbus->state = hv_pcibus_init;
  2151. /*
  2152. * The PCI bus "domain" is what is called "segment" in ACPI and
  2153. * other specs. Pull it from the instance ID, to get something
  2154. * unique. Bytes 8 and 9 are what is used in Windows guests, so
  2155. * do the same thing for consistency. Note that, since this code
  2156. * only runs in a Hyper-V VM, Hyper-V can (and does) guarantee
  2157. * that (1) the only domain in use for something that looks like
  2158. * a physical PCI bus (which is actually emulated by the
  2159. * hypervisor) is domain 0 and (2) there will be no overlap
  2160. * between domains derived from these instance IDs in the same
  2161. * VM.
  2162. */
  2163. hbus->sysdata.domain = hdev->dev_instance.b[9] |
  2164. hdev->dev_instance.b[8] << 8;
  2165. hbus->hdev = hdev;
  2166. atomic_inc(&hbus->remove_lock);
  2167. INIT_LIST_HEAD(&hbus->children);
  2168. INIT_LIST_HEAD(&hbus->dr_list);
  2169. INIT_LIST_HEAD(&hbus->resources_for_children);
  2170. spin_lock_init(&hbus->config_lock);
  2171. spin_lock_init(&hbus->device_list_lock);
  2172. spin_lock_init(&hbus->retarget_msi_interrupt_lock);
  2173. sema_init(&hbus->enum_sem, 1);
  2174. init_completion(&hbus->remove_event);
  2175. ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
  2176. hv_pci_onchannelcallback, hbus);
  2177. if (ret)
  2178. goto free_bus;
  2179. hv_set_drvdata(hdev, hbus);
  2180. ret = hv_pci_protocol_negotiation(hdev);
  2181. if (ret)
  2182. goto close;
  2183. ret = hv_allocate_config_window(hbus);
  2184. if (ret)
  2185. goto close;
  2186. hbus->cfg_addr = ioremap(hbus->mem_config->start,
  2187. PCI_CONFIG_MMIO_LENGTH);
  2188. if (!hbus->cfg_addr) {
  2189. dev_err(&hdev->device,
  2190. "Unable to map a virtual address for config space\n");
  2191. ret = -ENOMEM;
  2192. goto free_config;
  2193. }
  2194. hbus->sysdata.fwnode = irq_domain_alloc_fwnode(hbus);
  2195. if (!hbus->sysdata.fwnode) {
  2196. ret = -ENOMEM;
  2197. goto unmap;
  2198. }
  2199. ret = hv_pcie_init_irq_domain(hbus);
  2200. if (ret)
  2201. goto free_fwnode;
  2202. ret = hv_pci_query_relations(hdev);
  2203. if (ret)
  2204. goto free_irq_domain;
  2205. ret = hv_pci_enter_d0(hdev);
  2206. if (ret)
  2207. goto free_irq_domain;
  2208. ret = hv_pci_allocate_bridge_windows(hbus);
  2209. if (ret)
  2210. goto free_irq_domain;
  2211. ret = hv_send_resources_allocated(hdev);
  2212. if (ret)
  2213. goto free_windows;
  2214. prepopulate_bars(hbus);
  2215. hbus->state = hv_pcibus_probed;
  2216. ret = create_root_hv_pci_bus(hbus);
  2217. if (ret)
  2218. goto free_windows;
  2219. return 0;
  2220. free_windows:
  2221. hv_pci_free_bridge_windows(hbus);
  2222. free_irq_domain:
  2223. irq_domain_remove(hbus->irq_domain);
  2224. free_fwnode:
  2225. irq_domain_free_fwnode(hbus->sysdata.fwnode);
  2226. unmap:
  2227. iounmap(hbus->cfg_addr);
  2228. free_config:
  2229. hv_free_config_window(hbus);
  2230. close:
  2231. vmbus_close(hdev->channel);
  2232. free_bus:
  2233. free_page((unsigned long)hbus);
  2234. return ret;
  2235. }
  2236. static void hv_pci_bus_exit(struct hv_device *hdev)
  2237. {
  2238. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  2239. struct {
  2240. struct pci_packet teardown_packet;
  2241. u8 buffer[sizeof(struct pci_message)];
  2242. } pkt;
  2243. struct pci_bus_relations relations;
  2244. struct hv_pci_compl comp_pkt;
  2245. int ret;
  2246. /*
  2247. * After the host sends the RESCIND_CHANNEL message, it doesn't
  2248. * access the per-channel ringbuffer any longer.
  2249. */
  2250. if (hdev->channel->rescind)
  2251. return;
  2252. /* Delete any children which might still exist. */
  2253. memset(&relations, 0, sizeof(relations));
  2254. hv_pci_devices_present(hbus, &relations);
  2255. ret = hv_send_resources_released(hdev);
  2256. if (ret)
  2257. dev_err(&hdev->device,
  2258. "Couldn't send resources released packet(s)\n");
  2259. memset(&pkt.teardown_packet, 0, sizeof(pkt.teardown_packet));
  2260. init_completion(&comp_pkt.host_event);
  2261. pkt.teardown_packet.completion_func = hv_pci_generic_compl;
  2262. pkt.teardown_packet.compl_ctxt = &comp_pkt;
  2263. pkt.teardown_packet.message[0].type = PCI_BUS_D0EXIT;
  2264. ret = vmbus_sendpacket(hdev->channel, &pkt.teardown_packet.message,
  2265. sizeof(struct pci_message),
  2266. (unsigned long)&pkt.teardown_packet,
  2267. VM_PKT_DATA_INBAND,
  2268. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  2269. if (!ret)
  2270. wait_for_completion_timeout(&comp_pkt.host_event, 10 * HZ);
  2271. }
  2272. /**
  2273. * hv_pci_remove() - Remove routine for this VMBus channel
  2274. * @hdev: VMBus's tracking struct for this root PCI bus
  2275. *
  2276. * Return: 0 on success, -errno on failure
  2277. */
  2278. static int hv_pci_remove(struct hv_device *hdev)
  2279. {
  2280. struct hv_pcibus_device *hbus;
  2281. hbus = hv_get_drvdata(hdev);
  2282. if (hbus->state == hv_pcibus_installed) {
  2283. /* Remove the bus from PCI's point of view. */
  2284. pci_lock_rescan_remove();
  2285. pci_stop_root_bus(hbus->pci_bus);
  2286. pci_remove_root_bus(hbus->pci_bus);
  2287. pci_unlock_rescan_remove();
  2288. hbus->state = hv_pcibus_removed;
  2289. }
  2290. hv_pci_bus_exit(hdev);
  2291. vmbus_close(hdev->channel);
  2292. iounmap(hbus->cfg_addr);
  2293. hv_free_config_window(hbus);
  2294. pci_free_resource_list(&hbus->resources_for_children);
  2295. hv_pci_free_bridge_windows(hbus);
  2296. irq_domain_remove(hbus->irq_domain);
  2297. irq_domain_free_fwnode(hbus->sysdata.fwnode);
  2298. put_hvpcibus(hbus);
  2299. wait_for_completion(&hbus->remove_event);
  2300. free_page((unsigned long)hbus);
  2301. return 0;
  2302. }
  2303. static const struct hv_vmbus_device_id hv_pci_id_table[] = {
  2304. /* PCI Pass-through Class ID */
  2305. /* 44C4F61D-4444-4400-9D52-802E27EDE19F */
  2306. { HV_PCIE_GUID, },
  2307. { },
  2308. };
  2309. MODULE_DEVICE_TABLE(vmbus, hv_pci_id_table);
  2310. static struct hv_driver hv_pci_drv = {
  2311. .name = "hv_pci",
  2312. .id_table = hv_pci_id_table,
  2313. .probe = hv_pci_probe,
  2314. .remove = hv_pci_remove,
  2315. };
  2316. static void __exit exit_hv_pci_drv(void)
  2317. {
  2318. vmbus_driver_unregister(&hv_pci_drv);
  2319. }
  2320. static int __init init_hv_pci_drv(void)
  2321. {
  2322. return vmbus_driver_register(&hv_pci_drv);
  2323. }
  2324. module_init(init_hv_pci_drv);
  2325. module_exit(exit_hv_pci_drv);
  2326. MODULE_DESCRIPTION("Hyper-V PCI");
  2327. MODULE_LICENSE("GPL v2");