pci-hyperv.c 66 KB

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