pci-hyperv.c 74 KB

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