pci-hyperv.c 73 KB

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