pci-hyperv.c 73 KB

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