pci-hyperv.c 73 KB

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