vfio_ap_ops.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Adjunct processor matrix VFIO device driver callbacks.
  4. *
  5. * Copyright IBM Corp. 2018
  6. *
  7. * Author(s): Tony Krowiak <akrowiak@linux.ibm.com>
  8. * Halil Pasic <pasic@linux.ibm.com>
  9. * Pierre Morel <pmorel@linux.ibm.com>
  10. */
  11. #include <linux/string.h>
  12. #include <linux/vfio.h>
  13. #include <linux/device.h>
  14. #include <linux/list.h>
  15. #include <linux/ctype.h>
  16. #include <linux/bitops.h>
  17. #include <linux/kvm_host.h>
  18. #include <linux/module.h>
  19. #include <linux/uuid.h>
  20. #include <asm/kvm.h>
  21. #include <asm/zcrypt.h>
  22. #include "vfio_ap_private.h"
  23. #include "vfio_ap_debug.h"
  24. #define VFIO_AP_MDEV_TYPE_HWVIRT "passthrough"
  25. #define VFIO_AP_MDEV_NAME_HWVIRT "VFIO AP Passthrough Device"
  26. #define AP_QUEUE_ASSIGNED "assigned"
  27. #define AP_QUEUE_UNASSIGNED "unassigned"
  28. #define AP_QUEUE_IN_USE "in use"
  29. #define AP_RESET_INTERVAL 20 /* Reset sleep interval (20ms) */
  30. static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev);
  31. static int vfio_ap_mdev_reset_qlist(struct list_head *qlist);
  32. static struct vfio_ap_queue *vfio_ap_find_queue(int apqn);
  33. static const struct vfio_device_ops vfio_ap_matrix_dev_ops;
  34. static void vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q);
  35. /**
  36. * get_update_locks_for_kvm: Acquire the locks required to dynamically update a
  37. * KVM guest's APCB in the proper order.
  38. *
  39. * @kvm: a pointer to a struct kvm object containing the KVM guest's APCB.
  40. *
  41. * The proper locking order is:
  42. * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
  43. * guest's APCB.
  44. * 2. kvm->lock: required to update a guest's APCB
  45. * 3. matrix_dev->mdevs_lock: required to access data stored in a matrix_mdev
  46. *
  47. * Note: If @kvm is NULL, the KVM lock will not be taken.
  48. */
  49. static inline void get_update_locks_for_kvm(struct kvm *kvm)
  50. {
  51. mutex_lock(&matrix_dev->guests_lock);
  52. if (kvm)
  53. mutex_lock(&kvm->lock);
  54. mutex_lock(&matrix_dev->mdevs_lock);
  55. }
  56. /**
  57. * release_update_locks_for_kvm: Release the locks used to dynamically update a
  58. * KVM guest's APCB in the proper order.
  59. *
  60. * @kvm: a pointer to a struct kvm object containing the KVM guest's APCB.
  61. *
  62. * The proper unlocking order is:
  63. * 1. matrix_dev->mdevs_lock
  64. * 2. kvm->lock
  65. * 3. matrix_dev->guests_lock
  66. *
  67. * Note: If @kvm is NULL, the KVM lock will not be released.
  68. */
  69. static inline void release_update_locks_for_kvm(struct kvm *kvm)
  70. {
  71. mutex_unlock(&matrix_dev->mdevs_lock);
  72. if (kvm)
  73. mutex_unlock(&kvm->lock);
  74. mutex_unlock(&matrix_dev->guests_lock);
  75. }
  76. /**
  77. * get_update_locks_for_mdev: Acquire the locks required to dynamically update a
  78. * KVM guest's APCB in the proper order.
  79. *
  80. * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP
  81. * configuration data to use to update a KVM guest's APCB.
  82. *
  83. * The proper locking order is:
  84. * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
  85. * guest's APCB.
  86. * 2. matrix_mdev->kvm->lock: required to update a guest's APCB
  87. * 3. matrix_dev->mdevs_lock: required to access data stored in a matrix_mdev
  88. *
  89. * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM
  90. * lock will not be taken.
  91. */
  92. static inline void get_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)
  93. {
  94. mutex_lock(&matrix_dev->guests_lock);
  95. if (matrix_mdev && matrix_mdev->kvm)
  96. mutex_lock(&matrix_mdev->kvm->lock);
  97. mutex_lock(&matrix_dev->mdevs_lock);
  98. }
  99. /**
  100. * release_update_locks_for_mdev: Release the locks used to dynamically update a
  101. * KVM guest's APCB in the proper order.
  102. *
  103. * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP
  104. * configuration data to use to update a KVM guest's APCB.
  105. *
  106. * The proper unlocking order is:
  107. * 1. matrix_dev->mdevs_lock
  108. * 2. matrix_mdev->kvm->lock
  109. * 3. matrix_dev->guests_lock
  110. *
  111. * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM
  112. * lock will not be released.
  113. */
  114. static inline void release_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)
  115. {
  116. mutex_unlock(&matrix_dev->mdevs_lock);
  117. if (matrix_mdev && matrix_mdev->kvm)
  118. mutex_unlock(&matrix_mdev->kvm->lock);
  119. mutex_unlock(&matrix_dev->guests_lock);
  120. }
  121. /**
  122. * get_update_locks_by_apqn: Find the mdev to which an APQN is assigned and
  123. * acquire the locks required to update the APCB of
  124. * the KVM guest to which the mdev is attached.
  125. *
  126. * @apqn: the APQN of a queue device.
  127. *
  128. * The proper locking order is:
  129. * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
  130. * guest's APCB.
  131. * 2. matrix_mdev->kvm->lock: required to update a guest's APCB
  132. * 3. matrix_dev->mdevs_lock: required to access data stored in a matrix_mdev
  133. *
  134. * Note: If @apqn is not assigned to a matrix_mdev, the matrix_mdev->kvm->lock
  135. * will not be taken.
  136. *
  137. * Return: the ap_matrix_mdev object to which @apqn is assigned or NULL if @apqn
  138. * is not assigned to an ap_matrix_mdev.
  139. */
  140. static struct ap_matrix_mdev *get_update_locks_by_apqn(int apqn)
  141. {
  142. struct ap_matrix_mdev *matrix_mdev;
  143. mutex_lock(&matrix_dev->guests_lock);
  144. list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
  145. if (test_bit_inv(AP_QID_CARD(apqn), matrix_mdev->matrix.apm) &&
  146. test_bit_inv(AP_QID_QUEUE(apqn), matrix_mdev->matrix.aqm)) {
  147. if (matrix_mdev->kvm)
  148. mutex_lock(&matrix_mdev->kvm->lock);
  149. mutex_lock(&matrix_dev->mdevs_lock);
  150. return matrix_mdev;
  151. }
  152. }
  153. mutex_lock(&matrix_dev->mdevs_lock);
  154. return NULL;
  155. }
  156. /**
  157. * get_update_locks_for_queue: get the locks required to update the APCB of the
  158. * KVM guest to which the matrix mdev linked to a
  159. * vfio_ap_queue object is attached.
  160. *
  161. * @q: a pointer to a vfio_ap_queue object.
  162. *
  163. * The proper locking order is:
  164. * 1. q->matrix_dev->guests_lock: required to use the KVM pointer to update a
  165. * KVM guest's APCB.
  166. * 2. q->matrix_mdev->kvm->lock: required to update a guest's APCB
  167. * 3. matrix_dev->mdevs_lock: required to access data stored in matrix_mdev
  168. *
  169. * Note: if @queue is not linked to an ap_matrix_mdev object, the KVM lock
  170. * will not be taken.
  171. */
  172. static inline void get_update_locks_for_queue(struct vfio_ap_queue *q)
  173. {
  174. mutex_lock(&matrix_dev->guests_lock);
  175. if (q->matrix_mdev && q->matrix_mdev->kvm)
  176. mutex_lock(&q->matrix_mdev->kvm->lock);
  177. mutex_lock(&matrix_dev->mdevs_lock);
  178. }
  179. /**
  180. * vfio_ap_mdev_get_queue - retrieve a queue with a specific APQN from a
  181. * hash table of queues assigned to a matrix mdev
  182. * @matrix_mdev: the matrix mdev
  183. * @apqn: The APQN of a queue device
  184. *
  185. * Return: the pointer to the vfio_ap_queue struct representing the queue or
  186. * NULL if the queue is not assigned to @matrix_mdev
  187. */
  188. static struct vfio_ap_queue *vfio_ap_mdev_get_queue(
  189. struct ap_matrix_mdev *matrix_mdev,
  190. int apqn)
  191. {
  192. struct vfio_ap_queue *q;
  193. hash_for_each_possible(matrix_mdev->qtable.queues, q, mdev_qnode,
  194. apqn) {
  195. if (q && q->apqn == apqn)
  196. return q;
  197. }
  198. return NULL;
  199. }
  200. /**
  201. * vfio_ap_wait_for_irqclear - clears the IR bit or gives up after 5 tries
  202. * @apqn: The AP Queue number
  203. *
  204. * Checks the IRQ bit for the status of this APQN using ap_tapq.
  205. * Returns if the ap_tapq function succeeded and the bit is clear.
  206. * Returns if ap_tapq function failed with invalid, deconfigured or
  207. * checkstopped AP.
  208. * Otherwise retries up to 5 times after waiting 20ms.
  209. */
  210. static void vfio_ap_wait_for_irqclear(int apqn)
  211. {
  212. struct ap_queue_status status;
  213. int retry = 5;
  214. do {
  215. status = ap_tapq(apqn, NULL);
  216. switch (status.response_code) {
  217. case AP_RESPONSE_NORMAL:
  218. case AP_RESPONSE_RESET_IN_PROGRESS:
  219. if (!status.irq_enabled)
  220. return;
  221. fallthrough;
  222. case AP_RESPONSE_BUSY:
  223. msleep(20);
  224. break;
  225. case AP_RESPONSE_Q_NOT_AVAIL:
  226. case AP_RESPONSE_DECONFIGURED:
  227. case AP_RESPONSE_CHECKSTOPPED:
  228. default:
  229. WARN_ONCE(1, "%s: tapq rc %02x: %04x\n", __func__,
  230. status.response_code, apqn);
  231. return;
  232. }
  233. } while (--retry);
  234. WARN_ONCE(1, "%s: tapq rc %02x: %04x could not clear IR bit\n",
  235. __func__, status.response_code, apqn);
  236. }
  237. /**
  238. * vfio_ap_free_aqic_resources - free vfio_ap_queue resources
  239. * @q: The vfio_ap_queue
  240. *
  241. * Unregisters the ISC in the GIB when the saved ISC not invalid.
  242. * Unpins the guest's page holding the NIB when it exists.
  243. * Resets the saved_iova and saved_isc to invalid values.
  244. */
  245. static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q)
  246. {
  247. if (!q)
  248. return;
  249. if (q->saved_isc != VFIO_AP_ISC_INVALID &&
  250. !WARN_ON(!(q->matrix_mdev && q->matrix_mdev->kvm))) {
  251. kvm_s390_gisc_unregister(q->matrix_mdev->kvm, q->saved_isc);
  252. q->saved_isc = VFIO_AP_ISC_INVALID;
  253. }
  254. if (q->saved_iova && !WARN_ON(!q->matrix_mdev)) {
  255. vfio_unpin_pages(&q->matrix_mdev->vdev, q->saved_iova, 1);
  256. q->saved_iova = 0;
  257. }
  258. }
  259. /**
  260. * vfio_ap_irq_disable - disables and clears an ap_queue interrupt
  261. * @q: The vfio_ap_queue
  262. *
  263. * Uses ap_aqic to disable the interruption and in case of success, reset
  264. * in progress or IRQ disable command already proceeded: calls
  265. * vfio_ap_wait_for_irqclear() to check for the IRQ bit to be clear
  266. * and calls vfio_ap_free_aqic_resources() to free the resources associated
  267. * with the AP interrupt handling.
  268. *
  269. * In the case the AP is busy, or a reset is in progress,
  270. * retries after 20ms, up to 5 times.
  271. *
  272. * Returns if ap_aqic function failed with invalid, deconfigured or
  273. * checkstopped AP.
  274. *
  275. * Return: &struct ap_queue_status
  276. */
  277. static struct ap_queue_status vfio_ap_irq_disable(struct vfio_ap_queue *q)
  278. {
  279. union ap_qirq_ctrl aqic_gisa = { .value = 0 };
  280. struct ap_queue_status status;
  281. int retries = 5;
  282. do {
  283. status = ap_aqic(q->apqn, aqic_gisa, 0);
  284. switch (status.response_code) {
  285. case AP_RESPONSE_OTHERWISE_CHANGED:
  286. case AP_RESPONSE_NORMAL:
  287. vfio_ap_wait_for_irqclear(q->apqn);
  288. goto end_free;
  289. case AP_RESPONSE_RESET_IN_PROGRESS:
  290. case AP_RESPONSE_BUSY:
  291. msleep(20);
  292. break;
  293. case AP_RESPONSE_Q_NOT_AVAIL:
  294. case AP_RESPONSE_DECONFIGURED:
  295. case AP_RESPONSE_CHECKSTOPPED:
  296. case AP_RESPONSE_INVALID_ADDRESS:
  297. default:
  298. /* All cases in default means AP not operational */
  299. WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
  300. status.response_code);
  301. goto end_free;
  302. }
  303. } while (retries--);
  304. WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
  305. status.response_code);
  306. end_free:
  307. vfio_ap_free_aqic_resources(q);
  308. return status;
  309. }
  310. /**
  311. * vfio_ap_validate_nib - validate a notification indicator byte (nib) address.
  312. *
  313. * @vcpu: the object representing the vcpu executing the PQAP(AQIC) instruction.
  314. * @nib: the location for storing the nib address.
  315. *
  316. * When the PQAP(AQIC) instruction is executed, general register 2 contains the
  317. * address of the notification indicator byte (nib) used for IRQ notification.
  318. * This function parses and validates the nib from gr2.
  319. *
  320. * Return: returns zero if the nib address is a valid; otherwise, returns
  321. * -EINVAL.
  322. */
  323. static int vfio_ap_validate_nib(struct kvm_vcpu *vcpu, dma_addr_t *nib)
  324. {
  325. *nib = vcpu->run->s.regs.gprs[2];
  326. if (!*nib)
  327. return -EINVAL;
  328. if (kvm_is_error_hva(gfn_to_hva(vcpu->kvm, *nib >> PAGE_SHIFT)))
  329. return -EINVAL;
  330. return 0;
  331. }
  332. static int ensure_nib_shared(unsigned long addr, struct gmap *gmap)
  333. {
  334. int ret;
  335. /*
  336. * The nib has to be located in shared storage since guest and
  337. * host access it. vfio_pin_pages() will do a pin shared and
  338. * if that fails (possibly because it's not a shared page) it
  339. * calls export. We try to do a second pin shared here so that
  340. * the UV gives us an error code if we try to pin a non-shared
  341. * page.
  342. *
  343. * If the page is already pinned shared the UV will return a success.
  344. */
  345. ret = uv_pin_shared(addr);
  346. if (ret) {
  347. /* vfio_pin_pages() likely exported the page so let's re-import */
  348. gmap_convert_to_secure(gmap, addr);
  349. }
  350. return ret;
  351. }
  352. /**
  353. * vfio_ap_irq_enable - Enable Interruption for a APQN
  354. *
  355. * @q: the vfio_ap_queue holding AQIC parameters
  356. * @isc: the guest ISC to register with the GIB interface
  357. * @vcpu: the vcpu object containing the registers specifying the parameters
  358. * passed to the PQAP(AQIC) instruction.
  359. *
  360. * Pin the NIB saved in *q
  361. * Register the guest ISC to GIB interface and retrieve the
  362. * host ISC to issue the host side PQAP/AQIC
  363. *
  364. * status.response_code may be set to AP_RESPONSE_INVALID_ADDRESS in case the
  365. * vfio_pin_pages or kvm_s390_gisc_register failed.
  366. *
  367. * Otherwise return the ap_queue_status returned by the ap_aqic(),
  368. * all retry handling will be done by the guest.
  369. *
  370. * Return: &struct ap_queue_status
  371. */
  372. static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
  373. int isc,
  374. struct kvm_vcpu *vcpu)
  375. {
  376. union ap_qirq_ctrl aqic_gisa = { .value = 0 };
  377. struct ap_queue_status status = {};
  378. struct kvm_s390_gisa *gisa;
  379. struct page *h_page;
  380. int nisc;
  381. struct kvm *kvm;
  382. phys_addr_t h_nib;
  383. dma_addr_t nib;
  384. int ret;
  385. /* Verify that the notification indicator byte address is valid */
  386. if (vfio_ap_validate_nib(vcpu, &nib)) {
  387. VFIO_AP_DBF_WARN("%s: invalid NIB address: nib=%pad, apqn=%#04x\n",
  388. __func__, &nib, q->apqn);
  389. status.response_code = AP_RESPONSE_INVALID_ADDRESS;
  390. return status;
  391. }
  392. ret = vfio_pin_pages(&q->matrix_mdev->vdev, nib, 1,
  393. IOMMU_READ | IOMMU_WRITE, &h_page);
  394. switch (ret) {
  395. case 1:
  396. break;
  397. default:
  398. VFIO_AP_DBF_WARN("%s: vfio_pin_pages failed: rc=%d,"
  399. "nib=%pad, apqn=%#04x\n",
  400. __func__, ret, &nib, q->apqn);
  401. status.response_code = AP_RESPONSE_INVALID_ADDRESS;
  402. return status;
  403. }
  404. kvm = q->matrix_mdev->kvm;
  405. gisa = kvm->arch.gisa_int.origin;
  406. h_nib = page_to_phys(h_page) | (nib & ~PAGE_MASK);
  407. aqic_gisa.gisc = isc;
  408. /* NIB in non-shared storage is a rc 6 for PV guests */
  409. if (kvm_s390_pv_cpu_is_protected(vcpu) &&
  410. ensure_nib_shared(h_nib & PAGE_MASK, kvm->arch.gmap)) {
  411. vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
  412. status.response_code = AP_RESPONSE_INVALID_ADDRESS;
  413. return status;
  414. }
  415. nisc = kvm_s390_gisc_register(kvm, isc);
  416. if (nisc < 0) {
  417. VFIO_AP_DBF_WARN("%s: gisc registration failed: nisc=%d, isc=%d, apqn=%#04x\n",
  418. __func__, nisc, isc, q->apqn);
  419. vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
  420. status.response_code = AP_RESPONSE_INVALID_ADDRESS;
  421. return status;
  422. }
  423. aqic_gisa.isc = nisc;
  424. aqic_gisa.ir = 1;
  425. aqic_gisa.gisa = virt_to_phys(gisa) >> 4;
  426. status = ap_aqic(q->apqn, aqic_gisa, h_nib);
  427. switch (status.response_code) {
  428. case AP_RESPONSE_NORMAL:
  429. /* See if we did clear older IRQ configuration */
  430. vfio_ap_free_aqic_resources(q);
  431. q->saved_iova = nib;
  432. q->saved_isc = isc;
  433. break;
  434. case AP_RESPONSE_OTHERWISE_CHANGED:
  435. /* We could not modify IRQ settings: clear new configuration */
  436. ret = kvm_s390_gisc_unregister(kvm, isc);
  437. if (ret)
  438. VFIO_AP_DBF_WARN("%s: kvm_s390_gisc_unregister: rc=%d isc=%d, apqn=%#04x\n",
  439. __func__, ret, isc, q->apqn);
  440. vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
  441. break;
  442. default:
  443. pr_warn("%s: apqn %04x: response: %02x\n", __func__, q->apqn,
  444. status.response_code);
  445. vfio_ap_irq_disable(q);
  446. break;
  447. }
  448. if (status.response_code != AP_RESPONSE_NORMAL) {
  449. VFIO_AP_DBF_WARN("%s: PQAP(AQIC) failed with status=%#02x: "
  450. "zone=%#x, ir=%#x, gisc=%#x, f=%#x,"
  451. "gisa=%#x, isc=%#x, apqn=%#04x\n",
  452. __func__, status.response_code,
  453. aqic_gisa.zone, aqic_gisa.ir, aqic_gisa.gisc,
  454. aqic_gisa.gf, aqic_gisa.gisa, aqic_gisa.isc,
  455. q->apqn);
  456. }
  457. return status;
  458. }
  459. /**
  460. * vfio_ap_le_guid_to_be_uuid - convert a little endian guid array into an array
  461. * of big endian elements that can be passed by
  462. * value to an s390dbf sprintf event function to
  463. * format a UUID string.
  464. *
  465. * @guid: the object containing the little endian guid
  466. * @uuid: a six-element array of long values that can be passed by value as
  467. * arguments for a formatting string specifying a UUID.
  468. *
  469. * The S390 Debug Feature (s390dbf) allows the use of "%s" in the sprintf
  470. * event functions if the memory for the passed string is available as long as
  471. * the debug feature exists. Since a mediated device can be removed at any
  472. * time, it's name can not be used because %s passes the reference to the string
  473. * in memory and the reference will go stale once the device is removed .
  474. *
  475. * The s390dbf string formatting function allows a maximum of 9 arguments for a
  476. * message to be displayed in the 'sprintf' view. In order to use the bytes
  477. * comprising the mediated device's UUID to display the mediated device name,
  478. * they will have to be converted into an array whose elements can be passed by
  479. * value to sprintf. For example:
  480. *
  481. * guid array: { 83, 78, 17, 62, bb, f1, f0, 47, 91, 4d, 32, a2, 2e, 3a, 88, 04 }
  482. * mdev name: 62177883-f1bb-47f0-914d-32a22e3a8804
  483. * array returned: { 62177883, f1bb, 47f0, 914d, 32a2, 2e3a8804 }
  484. * formatting string: "%08lx-%04lx-%04lx-%04lx-%02lx%04lx"
  485. */
  486. static void vfio_ap_le_guid_to_be_uuid(guid_t *guid, unsigned long *uuid)
  487. {
  488. /*
  489. * The input guid is ordered in little endian, so it needs to be
  490. * reordered for displaying a UUID as a string. This specifies the
  491. * guid indices in proper order.
  492. */
  493. uuid[0] = le32_to_cpup((__le32 *)guid);
  494. uuid[1] = le16_to_cpup((__le16 *)&guid->b[4]);
  495. uuid[2] = le16_to_cpup((__le16 *)&guid->b[6]);
  496. uuid[3] = *((__u16 *)&guid->b[8]);
  497. uuid[4] = *((__u16 *)&guid->b[10]);
  498. uuid[5] = *((__u32 *)&guid->b[12]);
  499. }
  500. /**
  501. * handle_pqap - PQAP instruction callback
  502. *
  503. * @vcpu: The vcpu on which we received the PQAP instruction
  504. *
  505. * Get the general register contents to initialize internal variables.
  506. * REG[0]: APQN
  507. * REG[1]: IR and ISC
  508. * REG[2]: NIB
  509. *
  510. * Response.status may be set to following Response Code:
  511. * - AP_RESPONSE_Q_NOT_AVAIL: if the queue is not available
  512. * - AP_RESPONSE_DECONFIGURED: if the queue is not configured
  513. * - AP_RESPONSE_NORMAL (0) : in case of success
  514. * Check vfio_ap_setirq() and vfio_ap_clrirq() for other possible RC.
  515. * We take the matrix_dev lock to ensure serialization on queues and
  516. * mediated device access.
  517. *
  518. * Return: 0 if we could handle the request inside KVM.
  519. * Otherwise, returns -EOPNOTSUPP to let QEMU handle the fault.
  520. */
  521. static int handle_pqap(struct kvm_vcpu *vcpu)
  522. {
  523. uint64_t status;
  524. uint16_t apqn;
  525. unsigned long uuid[6];
  526. struct vfio_ap_queue *q;
  527. struct ap_queue_status qstatus = {
  528. .response_code = AP_RESPONSE_Q_NOT_AVAIL, };
  529. struct ap_matrix_mdev *matrix_mdev;
  530. apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
  531. /* If we do not use the AIV facility just go to userland */
  532. if (!(vcpu->arch.sie_block->eca & ECA_AIV)) {
  533. VFIO_AP_DBF_WARN("%s: AIV facility not installed: apqn=0x%04x, eca=0x%04x\n",
  534. __func__, apqn, vcpu->arch.sie_block->eca);
  535. return -EOPNOTSUPP;
  536. }
  537. mutex_lock(&matrix_dev->mdevs_lock);
  538. if (!vcpu->kvm->arch.crypto.pqap_hook) {
  539. VFIO_AP_DBF_WARN("%s: PQAP(AQIC) hook not registered with the vfio_ap driver: apqn=0x%04x\n",
  540. __func__, apqn);
  541. goto out_unlock;
  542. }
  543. matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
  544. struct ap_matrix_mdev, pqap_hook);
  545. /* If the there is no guest using the mdev, there is nothing to do */
  546. if (!matrix_mdev->kvm) {
  547. vfio_ap_le_guid_to_be_uuid(&matrix_mdev->mdev->uuid, uuid);
  548. VFIO_AP_DBF_WARN("%s: mdev %08lx-%04lx-%04lx-%04lx-%04lx%08lx not in use: apqn=0x%04x\n",
  549. __func__, uuid[0], uuid[1], uuid[2],
  550. uuid[3], uuid[4], uuid[5], apqn);
  551. goto out_unlock;
  552. }
  553. q = vfio_ap_mdev_get_queue(matrix_mdev, apqn);
  554. if (!q) {
  555. VFIO_AP_DBF_WARN("%s: Queue %02x.%04x not bound to the vfio_ap driver\n",
  556. __func__, AP_QID_CARD(apqn),
  557. AP_QID_QUEUE(apqn));
  558. goto out_unlock;
  559. }
  560. status = vcpu->run->s.regs.gprs[1];
  561. /* If IR bit(16) is set we enable the interrupt */
  562. if ((status >> (63 - 16)) & 0x01)
  563. qstatus = vfio_ap_irq_enable(q, status & 0x07, vcpu);
  564. else
  565. qstatus = vfio_ap_irq_disable(q);
  566. out_unlock:
  567. memcpy(&vcpu->run->s.regs.gprs[1], &qstatus, sizeof(qstatus));
  568. vcpu->run->s.regs.gprs[1] >>= 32;
  569. mutex_unlock(&matrix_dev->mdevs_lock);
  570. return 0;
  571. }
  572. static void vfio_ap_matrix_init(struct ap_config_info *info,
  573. struct ap_matrix *matrix)
  574. {
  575. matrix->apm_max = info->apxa ? info->na : 63;
  576. matrix->aqm_max = info->apxa ? info->nd : 15;
  577. matrix->adm_max = info->apxa ? info->nd : 15;
  578. }
  579. static void vfio_ap_mdev_update_guest_apcb(struct ap_matrix_mdev *matrix_mdev)
  580. {
  581. if (matrix_mdev->kvm)
  582. kvm_arch_crypto_set_masks(matrix_mdev->kvm,
  583. matrix_mdev->shadow_apcb.apm,
  584. matrix_mdev->shadow_apcb.aqm,
  585. matrix_mdev->shadow_apcb.adm);
  586. }
  587. static bool vfio_ap_mdev_filter_cdoms(struct ap_matrix_mdev *matrix_mdev)
  588. {
  589. DECLARE_BITMAP(prev_shadow_adm, AP_DOMAINS);
  590. bitmap_copy(prev_shadow_adm, matrix_mdev->shadow_apcb.adm, AP_DOMAINS);
  591. bitmap_and(matrix_mdev->shadow_apcb.adm, matrix_mdev->matrix.adm,
  592. (unsigned long *)matrix_dev->info.adm, AP_DOMAINS);
  593. return !bitmap_equal(prev_shadow_adm, matrix_mdev->shadow_apcb.adm,
  594. AP_DOMAINS);
  595. }
  596. static bool _queue_passable(struct vfio_ap_queue *q)
  597. {
  598. if (!q)
  599. return false;
  600. switch (q->reset_status.response_code) {
  601. case AP_RESPONSE_NORMAL:
  602. case AP_RESPONSE_DECONFIGURED:
  603. case AP_RESPONSE_CHECKSTOPPED:
  604. return true;
  605. default:
  606. return false;
  607. }
  608. }
  609. /*
  610. * vfio_ap_mdev_filter_matrix - filter the APQNs assigned to the matrix mdev
  611. * to ensure no queue devices are passed through to
  612. * the guest that are not bound to the vfio_ap
  613. * device driver.
  614. *
  615. * @matrix_mdev: the matrix mdev whose matrix is to be filtered.
  616. * @apm_filtered: a 256-bit bitmap for storing the APIDs filtered from the
  617. * guest's AP configuration that are still in the host's AP
  618. * configuration.
  619. *
  620. * Note: If an APQN referencing a queue device that is not bound to the vfio_ap
  621. * driver, its APID will be filtered from the guest's APCB. The matrix
  622. * structure precludes filtering an individual APQN, so its APID will be
  623. * filtered. Consequently, all queues associated with the adapter that
  624. * are in the host's AP configuration must be reset. If queues are
  625. * subsequently made available again to the guest, they should re-appear
  626. * in a reset state
  627. *
  628. * Return: a boolean value indicating whether the KVM guest's APCB was changed
  629. * by the filtering or not.
  630. */
  631. static bool vfio_ap_mdev_filter_matrix(struct ap_matrix_mdev *matrix_mdev,
  632. unsigned long *apm_filtered)
  633. {
  634. unsigned long apid, apqi, apqn;
  635. DECLARE_BITMAP(prev_shadow_apm, AP_DEVICES);
  636. DECLARE_BITMAP(prev_shadow_aqm, AP_DOMAINS);
  637. bitmap_copy(prev_shadow_apm, matrix_mdev->shadow_apcb.apm, AP_DEVICES);
  638. bitmap_copy(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm, AP_DOMAINS);
  639. vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
  640. bitmap_clear(apm_filtered, 0, AP_DEVICES);
  641. /*
  642. * Copy the adapters, domains and control domains to the shadow_apcb
  643. * from the matrix mdev, but only those that are assigned to the host's
  644. * AP configuration.
  645. */
  646. bitmap_and(matrix_mdev->shadow_apcb.apm, matrix_mdev->matrix.apm,
  647. (unsigned long *)matrix_dev->info.apm, AP_DEVICES);
  648. bitmap_and(matrix_mdev->shadow_apcb.aqm, matrix_mdev->matrix.aqm,
  649. (unsigned long *)matrix_dev->info.aqm, AP_DOMAINS);
  650. for_each_set_bit_inv(apid, matrix_mdev->shadow_apcb.apm, AP_DEVICES) {
  651. for_each_set_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm,
  652. AP_DOMAINS) {
  653. /*
  654. * If the APQN is not bound to the vfio_ap device
  655. * driver, then we can't assign it to the guest's
  656. * AP configuration. The AP architecture won't
  657. * allow filtering of a single APQN, so let's filter
  658. * the APID since an adapter represents a physical
  659. * hardware device.
  660. */
  661. apqn = AP_MKQID(apid, apqi);
  662. if (!_queue_passable(vfio_ap_mdev_get_queue(matrix_mdev, apqn))) {
  663. clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
  664. /*
  665. * If the adapter was previously plugged into
  666. * the guest, let's let the caller know that
  667. * the APID was filtered.
  668. */
  669. if (test_bit_inv(apid, prev_shadow_apm))
  670. set_bit_inv(apid, apm_filtered);
  671. break;
  672. }
  673. }
  674. }
  675. return !bitmap_equal(prev_shadow_apm, matrix_mdev->shadow_apcb.apm,
  676. AP_DEVICES) ||
  677. !bitmap_equal(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm,
  678. AP_DOMAINS);
  679. }
  680. static int vfio_ap_mdev_init_dev(struct vfio_device *vdev)
  681. {
  682. struct ap_matrix_mdev *matrix_mdev =
  683. container_of(vdev, struct ap_matrix_mdev, vdev);
  684. matrix_mdev->mdev = to_mdev_device(vdev->dev);
  685. vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
  686. matrix_mdev->pqap_hook = handle_pqap;
  687. vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
  688. hash_init(matrix_mdev->qtable.queues);
  689. return 0;
  690. }
  691. static int vfio_ap_mdev_probe(struct mdev_device *mdev)
  692. {
  693. struct ap_matrix_mdev *matrix_mdev;
  694. int ret;
  695. matrix_mdev = vfio_alloc_device(ap_matrix_mdev, vdev, &mdev->dev,
  696. &vfio_ap_matrix_dev_ops);
  697. if (IS_ERR(matrix_mdev))
  698. return PTR_ERR(matrix_mdev);
  699. ret = vfio_register_emulated_iommu_dev(&matrix_mdev->vdev);
  700. if (ret)
  701. goto err_put_vdev;
  702. matrix_mdev->req_trigger = NULL;
  703. dev_set_drvdata(&mdev->dev, matrix_mdev);
  704. mutex_lock(&matrix_dev->mdevs_lock);
  705. list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
  706. mutex_unlock(&matrix_dev->mdevs_lock);
  707. return 0;
  708. err_put_vdev:
  709. vfio_put_device(&matrix_mdev->vdev);
  710. return ret;
  711. }
  712. static void vfio_ap_mdev_link_queue(struct ap_matrix_mdev *matrix_mdev,
  713. struct vfio_ap_queue *q)
  714. {
  715. if (!q || vfio_ap_mdev_get_queue(matrix_mdev, q->apqn))
  716. return;
  717. q->matrix_mdev = matrix_mdev;
  718. hash_add(matrix_mdev->qtable.queues, &q->mdev_qnode, q->apqn);
  719. }
  720. static void vfio_ap_mdev_link_apqn(struct ap_matrix_mdev *matrix_mdev, int apqn)
  721. {
  722. struct vfio_ap_queue *q;
  723. q = vfio_ap_find_queue(apqn);
  724. vfio_ap_mdev_link_queue(matrix_mdev, q);
  725. }
  726. static void vfio_ap_unlink_queue_fr_mdev(struct vfio_ap_queue *q)
  727. {
  728. hash_del(&q->mdev_qnode);
  729. }
  730. static void vfio_ap_unlink_mdev_fr_queue(struct vfio_ap_queue *q)
  731. {
  732. q->matrix_mdev = NULL;
  733. }
  734. static void vfio_ap_mdev_unlink_fr_queues(struct ap_matrix_mdev *matrix_mdev)
  735. {
  736. struct vfio_ap_queue *q;
  737. unsigned long apid, apqi;
  738. for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
  739. for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
  740. AP_DOMAINS) {
  741. q = vfio_ap_mdev_get_queue(matrix_mdev,
  742. AP_MKQID(apid, apqi));
  743. if (q)
  744. q->matrix_mdev = NULL;
  745. }
  746. }
  747. }
  748. static void vfio_ap_mdev_remove(struct mdev_device *mdev)
  749. {
  750. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(&mdev->dev);
  751. vfio_unregister_group_dev(&matrix_mdev->vdev);
  752. mutex_lock(&matrix_dev->guests_lock);
  753. mutex_lock(&matrix_dev->mdevs_lock);
  754. vfio_ap_mdev_reset_queues(matrix_mdev);
  755. vfio_ap_mdev_unlink_fr_queues(matrix_mdev);
  756. list_del(&matrix_mdev->node);
  757. mutex_unlock(&matrix_dev->mdevs_lock);
  758. mutex_unlock(&matrix_dev->guests_lock);
  759. vfio_put_device(&matrix_mdev->vdev);
  760. }
  761. #define MDEV_SHARING_ERR "Userspace may not assign queue %02lx.%04lx to mdev: already assigned to %s"
  762. #define MDEV_IN_USE_ERR "Can not reserve queue %02lx.%04lx for host driver: in use by mdev"
  763. static void vfio_ap_mdev_log_sharing_err(struct ap_matrix_mdev *assignee,
  764. struct ap_matrix_mdev *assigned_to,
  765. unsigned long *apm, unsigned long *aqm)
  766. {
  767. unsigned long apid, apqi;
  768. for_each_set_bit_inv(apid, apm, AP_DEVICES) {
  769. for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) {
  770. dev_warn(mdev_dev(assignee->mdev), MDEV_SHARING_ERR,
  771. apid, apqi, dev_name(mdev_dev(assigned_to->mdev)));
  772. }
  773. }
  774. }
  775. static void vfio_ap_mdev_log_in_use_err(struct ap_matrix_mdev *assignee,
  776. unsigned long *apm, unsigned long *aqm)
  777. {
  778. unsigned long apid, apqi;
  779. for_each_set_bit_inv(apid, apm, AP_DEVICES) {
  780. for_each_set_bit_inv(apqi, aqm, AP_DOMAINS)
  781. dev_warn(mdev_dev(assignee->mdev), MDEV_IN_USE_ERR, apid, apqi);
  782. }
  783. }
  784. /**
  785. * vfio_ap_mdev_verify_no_sharing - verify APQNs are not shared by matrix mdevs
  786. *
  787. * @assignee: the matrix mdev to which @mdev_apm and @mdev_aqm are being
  788. * assigned; or, NULL if this function was called by the AP bus
  789. * driver in_use callback to verify none of the APQNs being reserved
  790. * for the host device driver are in use by a vfio_ap mediated device
  791. * @mdev_apm: mask indicating the APIDs of the APQNs to be verified
  792. * @mdev_aqm: mask indicating the APQIs of the APQNs to be verified
  793. *
  794. * Verifies that each APQN derived from the Cartesian product of APIDs
  795. * represented by the bits set in @mdev_apm and the APQIs of the bits set in
  796. * @mdev_aqm is not assigned to a mediated device other than the mdev to which
  797. * the APQN is being assigned (@assignee). AP queue sharing is not allowed.
  798. *
  799. * Return: 0 if the APQNs are not shared; otherwise return -EADDRINUSE.
  800. */
  801. static int vfio_ap_mdev_verify_no_sharing(struct ap_matrix_mdev *assignee,
  802. unsigned long *mdev_apm,
  803. unsigned long *mdev_aqm)
  804. {
  805. struct ap_matrix_mdev *assigned_to;
  806. DECLARE_BITMAP(apm, AP_DEVICES);
  807. DECLARE_BITMAP(aqm, AP_DOMAINS);
  808. list_for_each_entry(assigned_to, &matrix_dev->mdev_list, node) {
  809. /*
  810. * If the mdev to which the mdev_apm and mdev_aqm is being
  811. * assigned is the same as the mdev being verified
  812. */
  813. if (assignee == assigned_to)
  814. continue;
  815. memset(apm, 0, sizeof(apm));
  816. memset(aqm, 0, sizeof(aqm));
  817. /*
  818. * We work on full longs, as we can only exclude the leftover
  819. * bits in non-inverse order. The leftover is all zeros.
  820. */
  821. if (!bitmap_and(apm, mdev_apm, assigned_to->matrix.apm, AP_DEVICES))
  822. continue;
  823. if (!bitmap_and(aqm, mdev_aqm, assigned_to->matrix.aqm, AP_DOMAINS))
  824. continue;
  825. if (assignee)
  826. vfio_ap_mdev_log_sharing_err(assignee, assigned_to, apm, aqm);
  827. else
  828. vfio_ap_mdev_log_in_use_err(assigned_to, apm, aqm);
  829. return -EADDRINUSE;
  830. }
  831. return 0;
  832. }
  833. /**
  834. * vfio_ap_mdev_validate_masks - verify that the APQNs assigned to the mdev are
  835. * not reserved for the default zcrypt driver and
  836. * are not assigned to another mdev.
  837. *
  838. * @matrix_mdev: the mdev to which the APQNs being validated are assigned.
  839. *
  840. * Return: One of the following values:
  841. * o the error returned from the ap_apqn_in_matrix_owned_by_def_drv() function,
  842. * most likely -EBUSY indicating the ap_perms_mutex lock is already held.
  843. * o EADDRNOTAVAIL if an APQN assigned to @matrix_mdev is reserved for the
  844. * zcrypt default driver.
  845. * o EADDRINUSE if an APQN assigned to @matrix_mdev is assigned to another mdev
  846. * o A zero indicating validation succeeded.
  847. */
  848. static int vfio_ap_mdev_validate_masks(struct ap_matrix_mdev *matrix_mdev)
  849. {
  850. if (ap_apqn_in_matrix_owned_by_def_drv(matrix_mdev->matrix.apm,
  851. matrix_mdev->matrix.aqm))
  852. return -EADDRNOTAVAIL;
  853. return vfio_ap_mdev_verify_no_sharing(matrix_mdev,
  854. matrix_mdev->matrix.apm,
  855. matrix_mdev->matrix.aqm);
  856. }
  857. static void vfio_ap_mdev_link_adapter(struct ap_matrix_mdev *matrix_mdev,
  858. unsigned long apid)
  859. {
  860. unsigned long apqi;
  861. for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS)
  862. vfio_ap_mdev_link_apqn(matrix_mdev,
  863. AP_MKQID(apid, apqi));
  864. }
  865. static void collect_queues_to_reset(struct ap_matrix_mdev *matrix_mdev,
  866. unsigned long apid,
  867. struct list_head *qlist)
  868. {
  869. struct vfio_ap_queue *q;
  870. unsigned long apqi;
  871. for_each_set_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm, AP_DOMAINS) {
  872. q = vfio_ap_mdev_get_queue(matrix_mdev, AP_MKQID(apid, apqi));
  873. if (q)
  874. list_add_tail(&q->reset_qnode, qlist);
  875. }
  876. }
  877. static void reset_queues_for_apid(struct ap_matrix_mdev *matrix_mdev,
  878. unsigned long apid)
  879. {
  880. struct list_head qlist;
  881. INIT_LIST_HEAD(&qlist);
  882. collect_queues_to_reset(matrix_mdev, apid, &qlist);
  883. vfio_ap_mdev_reset_qlist(&qlist);
  884. }
  885. static int reset_queues_for_apids(struct ap_matrix_mdev *matrix_mdev,
  886. unsigned long *apm_reset)
  887. {
  888. struct list_head qlist;
  889. unsigned long apid;
  890. if (bitmap_empty(apm_reset, AP_DEVICES))
  891. return 0;
  892. INIT_LIST_HEAD(&qlist);
  893. for_each_set_bit_inv(apid, apm_reset, AP_DEVICES)
  894. collect_queues_to_reset(matrix_mdev, apid, &qlist);
  895. return vfio_ap_mdev_reset_qlist(&qlist);
  896. }
  897. /**
  898. * assign_adapter_store - parses the APID from @buf and sets the
  899. * corresponding bit in the mediated matrix device's APM
  900. *
  901. * @dev: the matrix device
  902. * @attr: the mediated matrix device's assign_adapter attribute
  903. * @buf: a buffer containing the AP adapter number (APID) to
  904. * be assigned
  905. * @count: the number of bytes in @buf
  906. *
  907. * Return: the number of bytes processed if the APID is valid; otherwise,
  908. * returns one of the following errors:
  909. *
  910. * 1. -EINVAL
  911. * The APID is not a valid number
  912. *
  913. * 2. -ENODEV
  914. * The APID exceeds the maximum value configured for the system
  915. *
  916. * 3. -EADDRNOTAVAIL
  917. * An APQN derived from the cross product of the APID being assigned
  918. * and the APQIs previously assigned is not bound to the vfio_ap device
  919. * driver; or, if no APQIs have yet been assigned, the APID is not
  920. * contained in an APQN bound to the vfio_ap device driver.
  921. *
  922. * 4. -EADDRINUSE
  923. * An APQN derived from the cross product of the APID being assigned
  924. * and the APQIs previously assigned is being used by another mediated
  925. * matrix device
  926. *
  927. * 5. -EAGAIN
  928. * A lock required to validate the mdev's AP configuration could not
  929. * be obtained.
  930. */
  931. static ssize_t assign_adapter_store(struct device *dev,
  932. struct device_attribute *attr,
  933. const char *buf, size_t count)
  934. {
  935. int ret;
  936. unsigned long apid;
  937. DECLARE_BITMAP(apm_filtered, AP_DEVICES);
  938. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
  939. mutex_lock(&ap_perms_mutex);
  940. get_update_locks_for_mdev(matrix_mdev);
  941. ret = kstrtoul(buf, 0, &apid);
  942. if (ret)
  943. goto done;
  944. if (apid > matrix_mdev->matrix.apm_max) {
  945. ret = -ENODEV;
  946. goto done;
  947. }
  948. if (test_bit_inv(apid, matrix_mdev->matrix.apm)) {
  949. ret = count;
  950. goto done;
  951. }
  952. set_bit_inv(apid, matrix_mdev->matrix.apm);
  953. ret = vfio_ap_mdev_validate_masks(matrix_mdev);
  954. if (ret) {
  955. clear_bit_inv(apid, matrix_mdev->matrix.apm);
  956. goto done;
  957. }
  958. vfio_ap_mdev_link_adapter(matrix_mdev, apid);
  959. if (vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered)) {
  960. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  961. reset_queues_for_apids(matrix_mdev, apm_filtered);
  962. }
  963. ret = count;
  964. done:
  965. release_update_locks_for_mdev(matrix_mdev);
  966. mutex_unlock(&ap_perms_mutex);
  967. return ret;
  968. }
  969. static DEVICE_ATTR_WO(assign_adapter);
  970. static struct vfio_ap_queue
  971. *vfio_ap_unlink_apqn_fr_mdev(struct ap_matrix_mdev *matrix_mdev,
  972. unsigned long apid, unsigned long apqi)
  973. {
  974. struct vfio_ap_queue *q = NULL;
  975. q = vfio_ap_mdev_get_queue(matrix_mdev, AP_MKQID(apid, apqi));
  976. /* If the queue is assigned to the matrix mdev, unlink it. */
  977. if (q)
  978. vfio_ap_unlink_queue_fr_mdev(q);
  979. return q;
  980. }
  981. /**
  982. * vfio_ap_mdev_unlink_adapter - unlink all queues associated with unassigned
  983. * adapter from the matrix mdev to which the
  984. * adapter was assigned.
  985. * @matrix_mdev: the matrix mediated device to which the adapter was assigned.
  986. * @apid: the APID of the unassigned adapter.
  987. * @qlist: list for storing queues associated with unassigned adapter that
  988. * need to be reset.
  989. */
  990. static void vfio_ap_mdev_unlink_adapter(struct ap_matrix_mdev *matrix_mdev,
  991. unsigned long apid,
  992. struct list_head *qlist)
  993. {
  994. unsigned long apqi;
  995. struct vfio_ap_queue *q;
  996. for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS) {
  997. q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
  998. if (q && qlist) {
  999. if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
  1000. test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
  1001. list_add_tail(&q->reset_qnode, qlist);
  1002. }
  1003. }
  1004. }
  1005. static void vfio_ap_mdev_hot_unplug_adapters(struct ap_matrix_mdev *matrix_mdev,
  1006. unsigned long *apids)
  1007. {
  1008. struct vfio_ap_queue *q, *tmpq;
  1009. struct list_head qlist;
  1010. unsigned long apid;
  1011. bool apcb_update = false;
  1012. INIT_LIST_HEAD(&qlist);
  1013. for_each_set_bit_inv(apid, apids, AP_DEVICES) {
  1014. vfio_ap_mdev_unlink_adapter(matrix_mdev, apid, &qlist);
  1015. if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm)) {
  1016. clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
  1017. apcb_update = true;
  1018. }
  1019. }
  1020. /* Only update apcb if needed to avoid impacting guest */
  1021. if (apcb_update)
  1022. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  1023. vfio_ap_mdev_reset_qlist(&qlist);
  1024. list_for_each_entry_safe(q, tmpq, &qlist, reset_qnode) {
  1025. vfio_ap_unlink_mdev_fr_queue(q);
  1026. list_del(&q->reset_qnode);
  1027. }
  1028. }
  1029. static void vfio_ap_mdev_hot_unplug_adapter(struct ap_matrix_mdev *matrix_mdev,
  1030. unsigned long apid)
  1031. {
  1032. DECLARE_BITMAP(apids, AP_DEVICES);
  1033. bitmap_zero(apids, AP_DEVICES);
  1034. set_bit_inv(apid, apids);
  1035. vfio_ap_mdev_hot_unplug_adapters(matrix_mdev, apids);
  1036. }
  1037. /**
  1038. * unassign_adapter_store - parses the APID from @buf and clears the
  1039. * corresponding bit in the mediated matrix device's APM
  1040. *
  1041. * @dev: the matrix device
  1042. * @attr: the mediated matrix device's unassign_adapter attribute
  1043. * @buf: a buffer containing the adapter number (APID) to be unassigned
  1044. * @count: the number of bytes in @buf
  1045. *
  1046. * Return: the number of bytes processed if the APID is valid; otherwise,
  1047. * returns one of the following errors:
  1048. * -EINVAL if the APID is not a number
  1049. * -ENODEV if the APID it exceeds the maximum value configured for the
  1050. * system
  1051. */
  1052. static ssize_t unassign_adapter_store(struct device *dev,
  1053. struct device_attribute *attr,
  1054. const char *buf, size_t count)
  1055. {
  1056. int ret;
  1057. unsigned long apid;
  1058. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
  1059. get_update_locks_for_mdev(matrix_mdev);
  1060. ret = kstrtoul(buf, 0, &apid);
  1061. if (ret)
  1062. goto done;
  1063. if (apid > matrix_mdev->matrix.apm_max) {
  1064. ret = -ENODEV;
  1065. goto done;
  1066. }
  1067. if (!test_bit_inv(apid, matrix_mdev->matrix.apm)) {
  1068. ret = count;
  1069. goto done;
  1070. }
  1071. clear_bit_inv((unsigned long)apid, matrix_mdev->matrix.apm);
  1072. vfio_ap_mdev_hot_unplug_adapter(matrix_mdev, apid);
  1073. ret = count;
  1074. done:
  1075. release_update_locks_for_mdev(matrix_mdev);
  1076. return ret;
  1077. }
  1078. static DEVICE_ATTR_WO(unassign_adapter);
  1079. static void vfio_ap_mdev_link_domain(struct ap_matrix_mdev *matrix_mdev,
  1080. unsigned long apqi)
  1081. {
  1082. unsigned long apid;
  1083. for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES)
  1084. vfio_ap_mdev_link_apqn(matrix_mdev,
  1085. AP_MKQID(apid, apqi));
  1086. }
  1087. /**
  1088. * assign_domain_store - parses the APQI from @buf and sets the
  1089. * corresponding bit in the mediated matrix device's AQM
  1090. *
  1091. * @dev: the matrix device
  1092. * @attr: the mediated matrix device's assign_domain attribute
  1093. * @buf: a buffer containing the AP queue index (APQI) of the domain to
  1094. * be assigned
  1095. * @count: the number of bytes in @buf
  1096. *
  1097. * Return: the number of bytes processed if the APQI is valid; otherwise returns
  1098. * one of the following errors:
  1099. *
  1100. * 1. -EINVAL
  1101. * The APQI is not a valid number
  1102. *
  1103. * 2. -ENODEV
  1104. * The APQI exceeds the maximum value configured for the system
  1105. *
  1106. * 3. -EADDRNOTAVAIL
  1107. * An APQN derived from the cross product of the APQI being assigned
  1108. * and the APIDs previously assigned is not bound to the vfio_ap device
  1109. * driver; or, if no APIDs have yet been assigned, the APQI is not
  1110. * contained in an APQN bound to the vfio_ap device driver.
  1111. *
  1112. * 4. -EADDRINUSE
  1113. * An APQN derived from the cross product of the APQI being assigned
  1114. * and the APIDs previously assigned is being used by another mediated
  1115. * matrix device
  1116. *
  1117. * 5. -EAGAIN
  1118. * The lock required to validate the mdev's AP configuration could not
  1119. * be obtained.
  1120. */
  1121. static ssize_t assign_domain_store(struct device *dev,
  1122. struct device_attribute *attr,
  1123. const char *buf, size_t count)
  1124. {
  1125. int ret;
  1126. unsigned long apqi;
  1127. DECLARE_BITMAP(apm_filtered, AP_DEVICES);
  1128. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
  1129. mutex_lock(&ap_perms_mutex);
  1130. get_update_locks_for_mdev(matrix_mdev);
  1131. ret = kstrtoul(buf, 0, &apqi);
  1132. if (ret)
  1133. goto done;
  1134. if (apqi > matrix_mdev->matrix.aqm_max) {
  1135. ret = -ENODEV;
  1136. goto done;
  1137. }
  1138. if (test_bit_inv(apqi, matrix_mdev->matrix.aqm)) {
  1139. ret = count;
  1140. goto done;
  1141. }
  1142. set_bit_inv(apqi, matrix_mdev->matrix.aqm);
  1143. ret = vfio_ap_mdev_validate_masks(matrix_mdev);
  1144. if (ret) {
  1145. clear_bit_inv(apqi, matrix_mdev->matrix.aqm);
  1146. goto done;
  1147. }
  1148. vfio_ap_mdev_link_domain(matrix_mdev, apqi);
  1149. if (vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered)) {
  1150. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  1151. reset_queues_for_apids(matrix_mdev, apm_filtered);
  1152. }
  1153. ret = count;
  1154. done:
  1155. release_update_locks_for_mdev(matrix_mdev);
  1156. mutex_unlock(&ap_perms_mutex);
  1157. return ret;
  1158. }
  1159. static DEVICE_ATTR_WO(assign_domain);
  1160. static void vfio_ap_mdev_unlink_domain(struct ap_matrix_mdev *matrix_mdev,
  1161. unsigned long apqi,
  1162. struct list_head *qlist)
  1163. {
  1164. unsigned long apid;
  1165. struct vfio_ap_queue *q;
  1166. for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
  1167. q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
  1168. if (q && qlist) {
  1169. if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
  1170. test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
  1171. list_add_tail(&q->reset_qnode, qlist);
  1172. }
  1173. }
  1174. }
  1175. static void vfio_ap_mdev_hot_unplug_domains(struct ap_matrix_mdev *matrix_mdev,
  1176. unsigned long *apqis)
  1177. {
  1178. struct vfio_ap_queue *q, *tmpq;
  1179. struct list_head qlist;
  1180. unsigned long apqi;
  1181. bool apcb_update = false;
  1182. INIT_LIST_HEAD(&qlist);
  1183. for_each_set_bit_inv(apqi, apqis, AP_DOMAINS) {
  1184. vfio_ap_mdev_unlink_domain(matrix_mdev, apqi, &qlist);
  1185. if (test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
  1186. clear_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm);
  1187. apcb_update = true;
  1188. }
  1189. }
  1190. /* Only update apcb if needed to avoid impacting guest */
  1191. if (apcb_update)
  1192. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  1193. vfio_ap_mdev_reset_qlist(&qlist);
  1194. list_for_each_entry_safe(q, tmpq, &qlist, reset_qnode) {
  1195. vfio_ap_unlink_mdev_fr_queue(q);
  1196. list_del(&q->reset_qnode);
  1197. }
  1198. }
  1199. static void vfio_ap_mdev_hot_unplug_domain(struct ap_matrix_mdev *matrix_mdev,
  1200. unsigned long apqi)
  1201. {
  1202. DECLARE_BITMAP(apqis, AP_DOMAINS);
  1203. bitmap_zero(apqis, AP_DEVICES);
  1204. set_bit_inv(apqi, apqis);
  1205. vfio_ap_mdev_hot_unplug_domains(matrix_mdev, apqis);
  1206. }
  1207. /**
  1208. * unassign_domain_store - parses the APQI from @buf and clears the
  1209. * corresponding bit in the mediated matrix device's AQM
  1210. *
  1211. * @dev: the matrix device
  1212. * @attr: the mediated matrix device's unassign_domain attribute
  1213. * @buf: a buffer containing the AP queue index (APQI) of the domain to
  1214. * be unassigned
  1215. * @count: the number of bytes in @buf
  1216. *
  1217. * Return: the number of bytes processed if the APQI is valid; otherwise,
  1218. * returns one of the following errors:
  1219. * -EINVAL if the APQI is not a number
  1220. * -ENODEV if the APQI exceeds the maximum value configured for the system
  1221. */
  1222. static ssize_t unassign_domain_store(struct device *dev,
  1223. struct device_attribute *attr,
  1224. const char *buf, size_t count)
  1225. {
  1226. int ret;
  1227. unsigned long apqi;
  1228. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
  1229. get_update_locks_for_mdev(matrix_mdev);
  1230. ret = kstrtoul(buf, 0, &apqi);
  1231. if (ret)
  1232. goto done;
  1233. if (apqi > matrix_mdev->matrix.aqm_max) {
  1234. ret = -ENODEV;
  1235. goto done;
  1236. }
  1237. if (!test_bit_inv(apqi, matrix_mdev->matrix.aqm)) {
  1238. ret = count;
  1239. goto done;
  1240. }
  1241. clear_bit_inv((unsigned long)apqi, matrix_mdev->matrix.aqm);
  1242. vfio_ap_mdev_hot_unplug_domain(matrix_mdev, apqi);
  1243. ret = count;
  1244. done:
  1245. release_update_locks_for_mdev(matrix_mdev);
  1246. return ret;
  1247. }
  1248. static DEVICE_ATTR_WO(unassign_domain);
  1249. /**
  1250. * assign_control_domain_store - parses the domain ID from @buf and sets
  1251. * the corresponding bit in the mediated matrix device's ADM
  1252. *
  1253. * @dev: the matrix device
  1254. * @attr: the mediated matrix device's assign_control_domain attribute
  1255. * @buf: a buffer containing the domain ID to be assigned
  1256. * @count: the number of bytes in @buf
  1257. *
  1258. * Return: the number of bytes processed if the domain ID is valid; otherwise,
  1259. * returns one of the following errors:
  1260. * -EINVAL if the ID is not a number
  1261. * -ENODEV if the ID exceeds the maximum value configured for the system
  1262. */
  1263. static ssize_t assign_control_domain_store(struct device *dev,
  1264. struct device_attribute *attr,
  1265. const char *buf, size_t count)
  1266. {
  1267. int ret;
  1268. unsigned long id;
  1269. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
  1270. get_update_locks_for_mdev(matrix_mdev);
  1271. ret = kstrtoul(buf, 0, &id);
  1272. if (ret)
  1273. goto done;
  1274. if (id > matrix_mdev->matrix.adm_max) {
  1275. ret = -ENODEV;
  1276. goto done;
  1277. }
  1278. if (test_bit_inv(id, matrix_mdev->matrix.adm)) {
  1279. ret = count;
  1280. goto done;
  1281. }
  1282. /* Set the bit in the ADM (bitmask) corresponding to the AP control
  1283. * domain number (id). The bits in the mask, from most significant to
  1284. * least significant, correspond to IDs 0 up to the one less than the
  1285. * number of control domains that can be assigned.
  1286. */
  1287. set_bit_inv(id, matrix_mdev->matrix.adm);
  1288. if (vfio_ap_mdev_filter_cdoms(matrix_mdev))
  1289. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  1290. ret = count;
  1291. done:
  1292. release_update_locks_for_mdev(matrix_mdev);
  1293. return ret;
  1294. }
  1295. static DEVICE_ATTR_WO(assign_control_domain);
  1296. /**
  1297. * unassign_control_domain_store - parses the domain ID from @buf and
  1298. * clears the corresponding bit in the mediated matrix device's ADM
  1299. *
  1300. * @dev: the matrix device
  1301. * @attr: the mediated matrix device's unassign_control_domain attribute
  1302. * @buf: a buffer containing the domain ID to be unassigned
  1303. * @count: the number of bytes in @buf
  1304. *
  1305. * Return: the number of bytes processed if the domain ID is valid; otherwise,
  1306. * returns one of the following errors:
  1307. * -EINVAL if the ID is not a number
  1308. * -ENODEV if the ID exceeds the maximum value configured for the system
  1309. */
  1310. static ssize_t unassign_control_domain_store(struct device *dev,
  1311. struct device_attribute *attr,
  1312. const char *buf, size_t count)
  1313. {
  1314. int ret;
  1315. unsigned long domid;
  1316. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
  1317. get_update_locks_for_mdev(matrix_mdev);
  1318. ret = kstrtoul(buf, 0, &domid);
  1319. if (ret)
  1320. goto done;
  1321. if (domid > matrix_mdev->matrix.adm_max) {
  1322. ret = -ENODEV;
  1323. goto done;
  1324. }
  1325. if (!test_bit_inv(domid, matrix_mdev->matrix.adm)) {
  1326. ret = count;
  1327. goto done;
  1328. }
  1329. clear_bit_inv(domid, matrix_mdev->matrix.adm);
  1330. if (test_bit_inv(domid, matrix_mdev->shadow_apcb.adm)) {
  1331. clear_bit_inv(domid, matrix_mdev->shadow_apcb.adm);
  1332. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  1333. }
  1334. ret = count;
  1335. done:
  1336. release_update_locks_for_mdev(matrix_mdev);
  1337. return ret;
  1338. }
  1339. static DEVICE_ATTR_WO(unassign_control_domain);
  1340. static ssize_t control_domains_show(struct device *dev,
  1341. struct device_attribute *dev_attr,
  1342. char *buf)
  1343. {
  1344. unsigned long id;
  1345. int nchars = 0;
  1346. int n;
  1347. char *bufpos = buf;
  1348. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
  1349. unsigned long max_domid = matrix_mdev->matrix.adm_max;
  1350. mutex_lock(&matrix_dev->mdevs_lock);
  1351. for_each_set_bit_inv(id, matrix_mdev->matrix.adm, max_domid + 1) {
  1352. n = sprintf(bufpos, "%04lx\n", id);
  1353. bufpos += n;
  1354. nchars += n;
  1355. }
  1356. mutex_unlock(&matrix_dev->mdevs_lock);
  1357. return nchars;
  1358. }
  1359. static DEVICE_ATTR_RO(control_domains);
  1360. static ssize_t vfio_ap_mdev_matrix_show(struct ap_matrix *matrix, char *buf)
  1361. {
  1362. char *bufpos = buf;
  1363. unsigned long apid;
  1364. unsigned long apqi;
  1365. unsigned long apid1;
  1366. unsigned long apqi1;
  1367. unsigned long napm_bits = matrix->apm_max + 1;
  1368. unsigned long naqm_bits = matrix->aqm_max + 1;
  1369. int nchars = 0;
  1370. int n;
  1371. apid1 = find_first_bit_inv(matrix->apm, napm_bits);
  1372. apqi1 = find_first_bit_inv(matrix->aqm, naqm_bits);
  1373. if ((apid1 < napm_bits) && (apqi1 < naqm_bits)) {
  1374. for_each_set_bit_inv(apid, matrix->apm, napm_bits) {
  1375. for_each_set_bit_inv(apqi, matrix->aqm,
  1376. naqm_bits) {
  1377. n = sprintf(bufpos, "%02lx.%04lx\n", apid,
  1378. apqi);
  1379. bufpos += n;
  1380. nchars += n;
  1381. }
  1382. }
  1383. } else if (apid1 < napm_bits) {
  1384. for_each_set_bit_inv(apid, matrix->apm, napm_bits) {
  1385. n = sprintf(bufpos, "%02lx.\n", apid);
  1386. bufpos += n;
  1387. nchars += n;
  1388. }
  1389. } else if (apqi1 < naqm_bits) {
  1390. for_each_set_bit_inv(apqi, matrix->aqm, naqm_bits) {
  1391. n = sprintf(bufpos, ".%04lx\n", apqi);
  1392. bufpos += n;
  1393. nchars += n;
  1394. }
  1395. }
  1396. return nchars;
  1397. }
  1398. static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,
  1399. char *buf)
  1400. {
  1401. ssize_t nchars;
  1402. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
  1403. mutex_lock(&matrix_dev->mdevs_lock);
  1404. nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->matrix, buf);
  1405. mutex_unlock(&matrix_dev->mdevs_lock);
  1406. return nchars;
  1407. }
  1408. static DEVICE_ATTR_RO(matrix);
  1409. static ssize_t guest_matrix_show(struct device *dev,
  1410. struct device_attribute *attr, char *buf)
  1411. {
  1412. ssize_t nchars;
  1413. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
  1414. mutex_lock(&matrix_dev->mdevs_lock);
  1415. nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->shadow_apcb, buf);
  1416. mutex_unlock(&matrix_dev->mdevs_lock);
  1417. return nchars;
  1418. }
  1419. static DEVICE_ATTR_RO(guest_matrix);
  1420. static ssize_t write_ap_bitmap(unsigned long *bitmap, char *buf, int offset, char sep)
  1421. {
  1422. return sysfs_emit_at(buf, offset, "0x%016lx%016lx%016lx%016lx%c",
  1423. bitmap[0], bitmap[1], bitmap[2], bitmap[3], sep);
  1424. }
  1425. static ssize_t ap_config_show(struct device *dev, struct device_attribute *attr,
  1426. char *buf)
  1427. {
  1428. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
  1429. int idx = 0;
  1430. idx += write_ap_bitmap(matrix_mdev->matrix.apm, buf, idx, ',');
  1431. idx += write_ap_bitmap(matrix_mdev->matrix.aqm, buf, idx, ',');
  1432. idx += write_ap_bitmap(matrix_mdev->matrix.adm, buf, idx, '\n');
  1433. return idx;
  1434. }
  1435. /* Number of characters needed for a complete hex mask representing the bits in .. */
  1436. #define AP_DEVICES_STRLEN (AP_DEVICES / 4 + 3)
  1437. #define AP_DOMAINS_STRLEN (AP_DOMAINS / 4 + 3)
  1438. #define AP_CONFIG_STRLEN (AP_DEVICES_STRLEN + 2 * AP_DOMAINS_STRLEN)
  1439. static int parse_bitmap(char **strbufptr, unsigned long *bitmap, int nbits)
  1440. {
  1441. char *curmask;
  1442. curmask = strsep(strbufptr, ",\n");
  1443. if (!curmask)
  1444. return -EINVAL;
  1445. bitmap_clear(bitmap, 0, nbits);
  1446. return ap_hex2bitmap(curmask, bitmap, nbits);
  1447. }
  1448. static int ap_matrix_overflow_check(struct ap_matrix_mdev *matrix_mdev)
  1449. {
  1450. unsigned long bit;
  1451. for_each_set_bit_inv(bit, matrix_mdev->matrix.apm, AP_DEVICES) {
  1452. if (bit > matrix_mdev->matrix.apm_max)
  1453. return -ENODEV;
  1454. }
  1455. for_each_set_bit_inv(bit, matrix_mdev->matrix.aqm, AP_DOMAINS) {
  1456. if (bit > matrix_mdev->matrix.aqm_max)
  1457. return -ENODEV;
  1458. }
  1459. for_each_set_bit_inv(bit, matrix_mdev->matrix.adm, AP_DOMAINS) {
  1460. if (bit > matrix_mdev->matrix.adm_max)
  1461. return -ENODEV;
  1462. }
  1463. return 0;
  1464. }
  1465. static void ap_matrix_copy(struct ap_matrix *dst, struct ap_matrix *src)
  1466. {
  1467. /* This check works around false positive gcc -Wstringop-overread */
  1468. if (!src)
  1469. return;
  1470. bitmap_copy(dst->apm, src->apm, AP_DEVICES);
  1471. bitmap_copy(dst->aqm, src->aqm, AP_DOMAINS);
  1472. bitmap_copy(dst->adm, src->adm, AP_DOMAINS);
  1473. }
  1474. static ssize_t ap_config_store(struct device *dev, struct device_attribute *attr,
  1475. const char *buf, size_t count)
  1476. {
  1477. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
  1478. struct ap_matrix m_new, m_old, m_added, m_removed;
  1479. DECLARE_BITMAP(apm_filtered, AP_DEVICES);
  1480. unsigned long newbit;
  1481. char *newbuf, *rest;
  1482. int rc = count;
  1483. bool do_update;
  1484. newbuf = kstrndup(buf, AP_CONFIG_STRLEN, GFP_KERNEL);
  1485. if (!newbuf)
  1486. return -ENOMEM;
  1487. rest = newbuf;
  1488. mutex_lock(&ap_perms_mutex);
  1489. get_update_locks_for_mdev(matrix_mdev);
  1490. /* Save old state */
  1491. ap_matrix_copy(&m_old, &matrix_mdev->matrix);
  1492. if (parse_bitmap(&rest, m_new.apm, AP_DEVICES) ||
  1493. parse_bitmap(&rest, m_new.aqm, AP_DOMAINS) ||
  1494. parse_bitmap(&rest, m_new.adm, AP_DOMAINS)) {
  1495. rc = -EINVAL;
  1496. goto out;
  1497. }
  1498. bitmap_andnot(m_removed.apm, m_old.apm, m_new.apm, AP_DEVICES);
  1499. bitmap_andnot(m_removed.aqm, m_old.aqm, m_new.aqm, AP_DOMAINS);
  1500. bitmap_andnot(m_added.apm, m_new.apm, m_old.apm, AP_DEVICES);
  1501. bitmap_andnot(m_added.aqm, m_new.aqm, m_old.aqm, AP_DOMAINS);
  1502. /* Need new bitmaps in matrix_mdev for validation */
  1503. ap_matrix_copy(&matrix_mdev->matrix, &m_new);
  1504. /* Ensure new state is valid, else undo new state */
  1505. rc = vfio_ap_mdev_validate_masks(matrix_mdev);
  1506. if (rc) {
  1507. ap_matrix_copy(&matrix_mdev->matrix, &m_old);
  1508. goto out;
  1509. }
  1510. rc = ap_matrix_overflow_check(matrix_mdev);
  1511. if (rc) {
  1512. ap_matrix_copy(&matrix_mdev->matrix, &m_old);
  1513. goto out;
  1514. }
  1515. rc = count;
  1516. /* Need old bitmaps in matrix_mdev for unplug/unlink */
  1517. ap_matrix_copy(&matrix_mdev->matrix, &m_old);
  1518. /* Unlink removed adapters/domains */
  1519. vfio_ap_mdev_hot_unplug_adapters(matrix_mdev, m_removed.apm);
  1520. vfio_ap_mdev_hot_unplug_domains(matrix_mdev, m_removed.aqm);
  1521. /* Need new bitmaps in matrix_mdev for linking new adapters/domains */
  1522. ap_matrix_copy(&matrix_mdev->matrix, &m_new);
  1523. /* Link newly added adapters */
  1524. for_each_set_bit_inv(newbit, m_added.apm, AP_DEVICES)
  1525. vfio_ap_mdev_link_adapter(matrix_mdev, newbit);
  1526. for_each_set_bit_inv(newbit, m_added.aqm, AP_DOMAINS)
  1527. vfio_ap_mdev_link_domain(matrix_mdev, newbit);
  1528. /* filter resources not bound to vfio-ap */
  1529. do_update = vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered);
  1530. do_update |= vfio_ap_mdev_filter_cdoms(matrix_mdev);
  1531. /* Apply changes to shadow apbc if things changed */
  1532. if (do_update) {
  1533. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  1534. reset_queues_for_apids(matrix_mdev, apm_filtered);
  1535. }
  1536. out:
  1537. release_update_locks_for_mdev(matrix_mdev);
  1538. mutex_unlock(&ap_perms_mutex);
  1539. kfree(newbuf);
  1540. return rc;
  1541. }
  1542. static DEVICE_ATTR_RW(ap_config);
  1543. static struct attribute *vfio_ap_mdev_attrs[] = {
  1544. &dev_attr_assign_adapter.attr,
  1545. &dev_attr_unassign_adapter.attr,
  1546. &dev_attr_assign_domain.attr,
  1547. &dev_attr_unassign_domain.attr,
  1548. &dev_attr_assign_control_domain.attr,
  1549. &dev_attr_unassign_control_domain.attr,
  1550. &dev_attr_ap_config.attr,
  1551. &dev_attr_control_domains.attr,
  1552. &dev_attr_matrix.attr,
  1553. &dev_attr_guest_matrix.attr,
  1554. NULL,
  1555. };
  1556. static struct attribute_group vfio_ap_mdev_attr_group = {
  1557. .attrs = vfio_ap_mdev_attrs
  1558. };
  1559. static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
  1560. &vfio_ap_mdev_attr_group,
  1561. NULL
  1562. };
  1563. /**
  1564. * vfio_ap_mdev_set_kvm - sets all data for @matrix_mdev that are needed
  1565. * to manage AP resources for the guest whose state is represented by @kvm
  1566. *
  1567. * @matrix_mdev: a mediated matrix device
  1568. * @kvm: reference to KVM instance
  1569. *
  1570. * Return: 0 if no other mediated matrix device has a reference to @kvm;
  1571. * otherwise, returns an -EPERM.
  1572. */
  1573. static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
  1574. struct kvm *kvm)
  1575. {
  1576. struct ap_matrix_mdev *m;
  1577. if (kvm->arch.crypto.crycbd) {
  1578. down_write(&kvm->arch.crypto.pqap_hook_rwsem);
  1579. kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
  1580. up_write(&kvm->arch.crypto.pqap_hook_rwsem);
  1581. get_update_locks_for_kvm(kvm);
  1582. list_for_each_entry(m, &matrix_dev->mdev_list, node) {
  1583. if (m != matrix_mdev && m->kvm == kvm) {
  1584. release_update_locks_for_kvm(kvm);
  1585. return -EPERM;
  1586. }
  1587. }
  1588. kvm_get_kvm(kvm);
  1589. matrix_mdev->kvm = kvm;
  1590. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  1591. release_update_locks_for_kvm(kvm);
  1592. }
  1593. return 0;
  1594. }
  1595. static void unmap_iova(struct ap_matrix_mdev *matrix_mdev, u64 iova, u64 length)
  1596. {
  1597. struct ap_queue_table *qtable = &matrix_mdev->qtable;
  1598. struct vfio_ap_queue *q;
  1599. int loop_cursor;
  1600. hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
  1601. if (q->saved_iova >= iova && q->saved_iova < iova + length)
  1602. vfio_ap_irq_disable(q);
  1603. }
  1604. }
  1605. static void vfio_ap_mdev_dma_unmap(struct vfio_device *vdev, u64 iova,
  1606. u64 length)
  1607. {
  1608. struct ap_matrix_mdev *matrix_mdev =
  1609. container_of(vdev, struct ap_matrix_mdev, vdev);
  1610. mutex_lock(&matrix_dev->mdevs_lock);
  1611. unmap_iova(matrix_mdev, iova, length);
  1612. mutex_unlock(&matrix_dev->mdevs_lock);
  1613. }
  1614. /**
  1615. * vfio_ap_mdev_unset_kvm - performs clean-up of resources no longer needed
  1616. * by @matrix_mdev.
  1617. *
  1618. * @matrix_mdev: a matrix mediated device
  1619. */
  1620. static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
  1621. {
  1622. struct kvm *kvm = matrix_mdev->kvm;
  1623. if (kvm && kvm->arch.crypto.crycbd) {
  1624. down_write(&kvm->arch.crypto.pqap_hook_rwsem);
  1625. kvm->arch.crypto.pqap_hook = NULL;
  1626. up_write(&kvm->arch.crypto.pqap_hook_rwsem);
  1627. get_update_locks_for_kvm(kvm);
  1628. kvm_arch_crypto_clear_masks(kvm);
  1629. vfio_ap_mdev_reset_queues(matrix_mdev);
  1630. kvm_put_kvm(kvm);
  1631. matrix_mdev->kvm = NULL;
  1632. release_update_locks_for_kvm(kvm);
  1633. }
  1634. }
  1635. static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)
  1636. {
  1637. struct ap_queue *queue;
  1638. struct vfio_ap_queue *q = NULL;
  1639. queue = ap_get_qdev(apqn);
  1640. if (!queue)
  1641. return NULL;
  1642. if (queue->ap_dev.device.driver == &matrix_dev->vfio_ap_drv->driver)
  1643. q = dev_get_drvdata(&queue->ap_dev.device);
  1644. put_device(&queue->ap_dev.device);
  1645. return q;
  1646. }
  1647. static int apq_status_check(int apqn, struct ap_queue_status *status)
  1648. {
  1649. switch (status->response_code) {
  1650. case AP_RESPONSE_NORMAL:
  1651. case AP_RESPONSE_DECONFIGURED:
  1652. case AP_RESPONSE_CHECKSTOPPED:
  1653. return 0;
  1654. case AP_RESPONSE_RESET_IN_PROGRESS:
  1655. case AP_RESPONSE_BUSY:
  1656. return -EBUSY;
  1657. case AP_RESPONSE_ASSOC_SECRET_NOT_UNIQUE:
  1658. case AP_RESPONSE_ASSOC_FAILED:
  1659. /*
  1660. * These asynchronous response codes indicate a PQAP(AAPQ)
  1661. * instruction to associate a secret with the guest failed. All
  1662. * subsequent AP instructions will end with the asynchronous
  1663. * response code until the AP queue is reset; so, let's return
  1664. * a value indicating a reset needs to be performed again.
  1665. */
  1666. return -EAGAIN;
  1667. default:
  1668. WARN(true,
  1669. "failed to verify reset of queue %02x.%04x: TAPQ rc=%u\n",
  1670. AP_QID_CARD(apqn), AP_QID_QUEUE(apqn),
  1671. status->response_code);
  1672. return -EIO;
  1673. }
  1674. }
  1675. #define WAIT_MSG "Waited %dms for reset of queue %02x.%04x (%u, %u, %u)"
  1676. static void apq_reset_check(struct work_struct *reset_work)
  1677. {
  1678. int ret = -EBUSY, elapsed = 0;
  1679. struct ap_queue_status status;
  1680. struct vfio_ap_queue *q;
  1681. q = container_of(reset_work, struct vfio_ap_queue, reset_work);
  1682. memcpy(&status, &q->reset_status, sizeof(status));
  1683. while (true) {
  1684. msleep(AP_RESET_INTERVAL);
  1685. elapsed += AP_RESET_INTERVAL;
  1686. status = ap_tapq(q->apqn, NULL);
  1687. ret = apq_status_check(q->apqn, &status);
  1688. if (ret == -EIO)
  1689. return;
  1690. if (ret == -EBUSY) {
  1691. pr_notice_ratelimited(WAIT_MSG, elapsed,
  1692. AP_QID_CARD(q->apqn),
  1693. AP_QID_QUEUE(q->apqn),
  1694. status.response_code,
  1695. status.queue_empty,
  1696. status.irq_enabled);
  1697. } else {
  1698. if (q->reset_status.response_code == AP_RESPONSE_RESET_IN_PROGRESS ||
  1699. q->reset_status.response_code == AP_RESPONSE_BUSY ||
  1700. q->reset_status.response_code == AP_RESPONSE_STATE_CHANGE_IN_PROGRESS ||
  1701. ret == -EAGAIN) {
  1702. status = ap_zapq(q->apqn, 0);
  1703. memcpy(&q->reset_status, &status, sizeof(status));
  1704. continue;
  1705. }
  1706. if (q->saved_isc != VFIO_AP_ISC_INVALID)
  1707. vfio_ap_free_aqic_resources(q);
  1708. break;
  1709. }
  1710. }
  1711. }
  1712. static void vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q)
  1713. {
  1714. struct ap_queue_status status;
  1715. if (!q)
  1716. return;
  1717. status = ap_zapq(q->apqn, 0);
  1718. memcpy(&q->reset_status, &status, sizeof(status));
  1719. switch (status.response_code) {
  1720. case AP_RESPONSE_NORMAL:
  1721. case AP_RESPONSE_RESET_IN_PROGRESS:
  1722. case AP_RESPONSE_BUSY:
  1723. case AP_RESPONSE_STATE_CHANGE_IN_PROGRESS:
  1724. /*
  1725. * Let's verify whether the ZAPQ completed successfully on a work queue.
  1726. */
  1727. queue_work(system_long_wq, &q->reset_work);
  1728. break;
  1729. case AP_RESPONSE_DECONFIGURED:
  1730. case AP_RESPONSE_CHECKSTOPPED:
  1731. vfio_ap_free_aqic_resources(q);
  1732. break;
  1733. default:
  1734. WARN(true,
  1735. "PQAP/ZAPQ for %02x.%04x failed with invalid rc=%u\n",
  1736. AP_QID_CARD(q->apqn), AP_QID_QUEUE(q->apqn),
  1737. status.response_code);
  1738. }
  1739. }
  1740. static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev)
  1741. {
  1742. int ret = 0, loop_cursor;
  1743. struct vfio_ap_queue *q;
  1744. hash_for_each(matrix_mdev->qtable.queues, loop_cursor, q, mdev_qnode)
  1745. vfio_ap_mdev_reset_queue(q);
  1746. hash_for_each(matrix_mdev->qtable.queues, loop_cursor, q, mdev_qnode) {
  1747. flush_work(&q->reset_work);
  1748. if (q->reset_status.response_code)
  1749. ret = -EIO;
  1750. }
  1751. return ret;
  1752. }
  1753. static int vfio_ap_mdev_reset_qlist(struct list_head *qlist)
  1754. {
  1755. int ret = 0;
  1756. struct vfio_ap_queue *q;
  1757. list_for_each_entry(q, qlist, reset_qnode)
  1758. vfio_ap_mdev_reset_queue(q);
  1759. list_for_each_entry(q, qlist, reset_qnode) {
  1760. flush_work(&q->reset_work);
  1761. if (q->reset_status.response_code)
  1762. ret = -EIO;
  1763. }
  1764. return ret;
  1765. }
  1766. static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
  1767. {
  1768. struct ap_matrix_mdev *matrix_mdev =
  1769. container_of(vdev, struct ap_matrix_mdev, vdev);
  1770. if (!vdev->kvm)
  1771. return -EINVAL;
  1772. return vfio_ap_mdev_set_kvm(matrix_mdev, vdev->kvm);
  1773. }
  1774. static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
  1775. {
  1776. struct ap_matrix_mdev *matrix_mdev =
  1777. container_of(vdev, struct ap_matrix_mdev, vdev);
  1778. vfio_ap_mdev_unset_kvm(matrix_mdev);
  1779. }
  1780. static void vfio_ap_mdev_request(struct vfio_device *vdev, unsigned int count)
  1781. {
  1782. struct device *dev = vdev->dev;
  1783. struct ap_matrix_mdev *matrix_mdev;
  1784. matrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);
  1785. if (matrix_mdev->req_trigger) {
  1786. if (!(count % 10))
  1787. dev_notice_ratelimited(dev,
  1788. "Relaying device request to user (#%u)\n",
  1789. count);
  1790. eventfd_signal(matrix_mdev->req_trigger);
  1791. } else if (count == 0) {
  1792. dev_notice(dev,
  1793. "No device request registered, blocked until released by user\n");
  1794. }
  1795. }
  1796. static int vfio_ap_mdev_get_device_info(unsigned long arg)
  1797. {
  1798. unsigned long minsz;
  1799. struct vfio_device_info info;
  1800. minsz = offsetofend(struct vfio_device_info, num_irqs);
  1801. if (copy_from_user(&info, (void __user *)arg, minsz))
  1802. return -EFAULT;
  1803. if (info.argsz < minsz)
  1804. return -EINVAL;
  1805. info.flags = VFIO_DEVICE_FLAGS_AP | VFIO_DEVICE_FLAGS_RESET;
  1806. info.num_regions = 0;
  1807. info.num_irqs = VFIO_AP_NUM_IRQS;
  1808. return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
  1809. }
  1810. static ssize_t vfio_ap_get_irq_info(unsigned long arg)
  1811. {
  1812. unsigned long minsz;
  1813. struct vfio_irq_info info;
  1814. minsz = offsetofend(struct vfio_irq_info, count);
  1815. if (copy_from_user(&info, (void __user *)arg, minsz))
  1816. return -EFAULT;
  1817. if (info.argsz < minsz || info.index >= VFIO_AP_NUM_IRQS)
  1818. return -EINVAL;
  1819. switch (info.index) {
  1820. case VFIO_AP_REQ_IRQ_INDEX:
  1821. info.count = 1;
  1822. info.flags = VFIO_IRQ_INFO_EVENTFD;
  1823. break;
  1824. default:
  1825. return -EINVAL;
  1826. }
  1827. return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
  1828. }
  1829. static int vfio_ap_irq_set_init(struct vfio_irq_set *irq_set, unsigned long arg)
  1830. {
  1831. int ret;
  1832. size_t data_size;
  1833. unsigned long minsz;
  1834. minsz = offsetofend(struct vfio_irq_set, count);
  1835. if (copy_from_user(irq_set, (void __user *)arg, minsz))
  1836. return -EFAULT;
  1837. ret = vfio_set_irqs_validate_and_prepare(irq_set, 1, VFIO_AP_NUM_IRQS,
  1838. &data_size);
  1839. if (ret)
  1840. return ret;
  1841. if (!(irq_set->flags & VFIO_IRQ_SET_ACTION_TRIGGER))
  1842. return -EINVAL;
  1843. return 0;
  1844. }
  1845. static int vfio_ap_set_request_irq(struct ap_matrix_mdev *matrix_mdev,
  1846. unsigned long arg)
  1847. {
  1848. s32 fd;
  1849. void __user *data;
  1850. unsigned long minsz;
  1851. struct eventfd_ctx *req_trigger;
  1852. minsz = offsetofend(struct vfio_irq_set, count);
  1853. data = (void __user *)(arg + minsz);
  1854. if (get_user(fd, (s32 __user *)data))
  1855. return -EFAULT;
  1856. if (fd == -1) {
  1857. if (matrix_mdev->req_trigger)
  1858. eventfd_ctx_put(matrix_mdev->req_trigger);
  1859. matrix_mdev->req_trigger = NULL;
  1860. } else if (fd >= 0) {
  1861. req_trigger = eventfd_ctx_fdget(fd);
  1862. if (IS_ERR(req_trigger))
  1863. return PTR_ERR(req_trigger);
  1864. if (matrix_mdev->req_trigger)
  1865. eventfd_ctx_put(matrix_mdev->req_trigger);
  1866. matrix_mdev->req_trigger = req_trigger;
  1867. } else {
  1868. return -EINVAL;
  1869. }
  1870. return 0;
  1871. }
  1872. static int vfio_ap_set_irqs(struct ap_matrix_mdev *matrix_mdev,
  1873. unsigned long arg)
  1874. {
  1875. int ret;
  1876. struct vfio_irq_set irq_set;
  1877. ret = vfio_ap_irq_set_init(&irq_set, arg);
  1878. if (ret)
  1879. return ret;
  1880. switch (irq_set.flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
  1881. case VFIO_IRQ_SET_DATA_EVENTFD:
  1882. switch (irq_set.index) {
  1883. case VFIO_AP_REQ_IRQ_INDEX:
  1884. return vfio_ap_set_request_irq(matrix_mdev, arg);
  1885. default:
  1886. return -EINVAL;
  1887. }
  1888. default:
  1889. return -EINVAL;
  1890. }
  1891. }
  1892. static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,
  1893. unsigned int cmd, unsigned long arg)
  1894. {
  1895. struct ap_matrix_mdev *matrix_mdev =
  1896. container_of(vdev, struct ap_matrix_mdev, vdev);
  1897. int ret;
  1898. mutex_lock(&matrix_dev->mdevs_lock);
  1899. switch (cmd) {
  1900. case VFIO_DEVICE_GET_INFO:
  1901. ret = vfio_ap_mdev_get_device_info(arg);
  1902. break;
  1903. case VFIO_DEVICE_RESET:
  1904. ret = vfio_ap_mdev_reset_queues(matrix_mdev);
  1905. break;
  1906. case VFIO_DEVICE_GET_IRQ_INFO:
  1907. ret = vfio_ap_get_irq_info(arg);
  1908. break;
  1909. case VFIO_DEVICE_SET_IRQS:
  1910. ret = vfio_ap_set_irqs(matrix_mdev, arg);
  1911. break;
  1912. default:
  1913. ret = -EOPNOTSUPP;
  1914. break;
  1915. }
  1916. mutex_unlock(&matrix_dev->mdevs_lock);
  1917. return ret;
  1918. }
  1919. static struct ap_matrix_mdev *vfio_ap_mdev_for_queue(struct vfio_ap_queue *q)
  1920. {
  1921. struct ap_matrix_mdev *matrix_mdev;
  1922. unsigned long apid = AP_QID_CARD(q->apqn);
  1923. unsigned long apqi = AP_QID_QUEUE(q->apqn);
  1924. list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
  1925. if (test_bit_inv(apid, matrix_mdev->matrix.apm) &&
  1926. test_bit_inv(apqi, matrix_mdev->matrix.aqm))
  1927. return matrix_mdev;
  1928. }
  1929. return NULL;
  1930. }
  1931. static ssize_t status_show(struct device *dev,
  1932. struct device_attribute *attr,
  1933. char *buf)
  1934. {
  1935. ssize_t nchars = 0;
  1936. struct vfio_ap_queue *q;
  1937. unsigned long apid, apqi;
  1938. struct ap_matrix_mdev *matrix_mdev;
  1939. struct ap_device *apdev = to_ap_dev(dev);
  1940. mutex_lock(&matrix_dev->mdevs_lock);
  1941. q = dev_get_drvdata(&apdev->device);
  1942. matrix_mdev = vfio_ap_mdev_for_queue(q);
  1943. /* If the queue is assigned to the matrix mediated device, then
  1944. * determine whether it is passed through to a guest; otherwise,
  1945. * indicate that it is unassigned.
  1946. */
  1947. if (matrix_mdev) {
  1948. apid = AP_QID_CARD(q->apqn);
  1949. apqi = AP_QID_QUEUE(q->apqn);
  1950. /*
  1951. * If the queue is passed through to the guest, then indicate
  1952. * that it is in use; otherwise, indicate that it is
  1953. * merely assigned to a matrix mediated device.
  1954. */
  1955. if (matrix_mdev->kvm &&
  1956. test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
  1957. test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
  1958. nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
  1959. AP_QUEUE_IN_USE);
  1960. else
  1961. nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
  1962. AP_QUEUE_ASSIGNED);
  1963. } else {
  1964. nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
  1965. AP_QUEUE_UNASSIGNED);
  1966. }
  1967. mutex_unlock(&matrix_dev->mdevs_lock);
  1968. return nchars;
  1969. }
  1970. static DEVICE_ATTR_RO(status);
  1971. static struct attribute *vfio_queue_attrs[] = {
  1972. &dev_attr_status.attr,
  1973. NULL,
  1974. };
  1975. static const struct attribute_group vfio_queue_attr_group = {
  1976. .attrs = vfio_queue_attrs,
  1977. };
  1978. static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
  1979. .init = vfio_ap_mdev_init_dev,
  1980. .open_device = vfio_ap_mdev_open_device,
  1981. .close_device = vfio_ap_mdev_close_device,
  1982. .ioctl = vfio_ap_mdev_ioctl,
  1983. .dma_unmap = vfio_ap_mdev_dma_unmap,
  1984. .bind_iommufd = vfio_iommufd_emulated_bind,
  1985. .unbind_iommufd = vfio_iommufd_emulated_unbind,
  1986. .attach_ioas = vfio_iommufd_emulated_attach_ioas,
  1987. .detach_ioas = vfio_iommufd_emulated_detach_ioas,
  1988. .request = vfio_ap_mdev_request
  1989. };
  1990. static struct mdev_driver vfio_ap_matrix_driver = {
  1991. .device_api = VFIO_DEVICE_API_AP_STRING,
  1992. .max_instances = MAX_ZDEV_ENTRIES_EXT,
  1993. .driver = {
  1994. .name = "vfio_ap_mdev",
  1995. .owner = THIS_MODULE,
  1996. .mod_name = KBUILD_MODNAME,
  1997. .dev_groups = vfio_ap_mdev_attr_groups,
  1998. },
  1999. .probe = vfio_ap_mdev_probe,
  2000. .remove = vfio_ap_mdev_remove,
  2001. };
  2002. int vfio_ap_mdev_register(void)
  2003. {
  2004. int ret;
  2005. ret = mdev_register_driver(&vfio_ap_matrix_driver);
  2006. if (ret)
  2007. return ret;
  2008. matrix_dev->mdev_type.sysfs_name = VFIO_AP_MDEV_TYPE_HWVIRT;
  2009. matrix_dev->mdev_type.pretty_name = VFIO_AP_MDEV_NAME_HWVIRT;
  2010. matrix_dev->mdev_types[0] = &matrix_dev->mdev_type;
  2011. ret = mdev_register_parent(&matrix_dev->parent, &matrix_dev->device,
  2012. &vfio_ap_matrix_driver,
  2013. matrix_dev->mdev_types, 1);
  2014. if (ret)
  2015. goto err_driver;
  2016. return 0;
  2017. err_driver:
  2018. mdev_unregister_driver(&vfio_ap_matrix_driver);
  2019. return ret;
  2020. }
  2021. void vfio_ap_mdev_unregister(void)
  2022. {
  2023. mdev_unregister_parent(&matrix_dev->parent);
  2024. mdev_unregister_driver(&vfio_ap_matrix_driver);
  2025. }
  2026. int vfio_ap_mdev_probe_queue(struct ap_device *apdev)
  2027. {
  2028. int ret;
  2029. struct vfio_ap_queue *q;
  2030. DECLARE_BITMAP(apm_filtered, AP_DEVICES);
  2031. struct ap_matrix_mdev *matrix_mdev;
  2032. ret = sysfs_create_group(&apdev->device.kobj, &vfio_queue_attr_group);
  2033. if (ret)
  2034. return ret;
  2035. q = kzalloc(sizeof(*q), GFP_KERNEL);
  2036. if (!q) {
  2037. ret = -ENOMEM;
  2038. goto err_remove_group;
  2039. }
  2040. q->apqn = to_ap_queue(&apdev->device)->qid;
  2041. q->saved_isc = VFIO_AP_ISC_INVALID;
  2042. memset(&q->reset_status, 0, sizeof(q->reset_status));
  2043. INIT_WORK(&q->reset_work, apq_reset_check);
  2044. matrix_mdev = get_update_locks_by_apqn(q->apqn);
  2045. if (matrix_mdev) {
  2046. vfio_ap_mdev_link_queue(matrix_mdev, q);
  2047. /*
  2048. * If we're in the process of handling the adding of adapters or
  2049. * domains to the host's AP configuration, then let the
  2050. * vfio_ap device driver's on_scan_complete callback filter the
  2051. * matrix and update the guest's AP configuration after all of
  2052. * the new queue devices are probed.
  2053. */
  2054. if (!bitmap_empty(matrix_mdev->apm_add, AP_DEVICES) ||
  2055. !bitmap_empty(matrix_mdev->aqm_add, AP_DOMAINS))
  2056. goto done;
  2057. if (vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered)) {
  2058. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  2059. reset_queues_for_apids(matrix_mdev, apm_filtered);
  2060. }
  2061. }
  2062. done:
  2063. dev_set_drvdata(&apdev->device, q);
  2064. release_update_locks_for_mdev(matrix_mdev);
  2065. return ret;
  2066. err_remove_group:
  2067. sysfs_remove_group(&apdev->device.kobj, &vfio_queue_attr_group);
  2068. return ret;
  2069. }
  2070. void vfio_ap_mdev_remove_queue(struct ap_device *apdev)
  2071. {
  2072. unsigned long apid, apqi;
  2073. struct vfio_ap_queue *q;
  2074. struct ap_matrix_mdev *matrix_mdev;
  2075. sysfs_remove_group(&apdev->device.kobj, &vfio_queue_attr_group);
  2076. q = dev_get_drvdata(&apdev->device);
  2077. get_update_locks_for_queue(q);
  2078. matrix_mdev = q->matrix_mdev;
  2079. apid = AP_QID_CARD(q->apqn);
  2080. apqi = AP_QID_QUEUE(q->apqn);
  2081. if (matrix_mdev) {
  2082. /* If the queue is assigned to the guest's AP configuration */
  2083. if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
  2084. test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
  2085. /*
  2086. * Since the queues are defined via a matrix of adapters
  2087. * and domains, it is not possible to hot unplug a
  2088. * single queue; so, let's unplug the adapter.
  2089. */
  2090. clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
  2091. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  2092. reset_queues_for_apid(matrix_mdev, apid);
  2093. goto done;
  2094. }
  2095. }
  2096. /*
  2097. * If the queue is not in the host's AP configuration, then resetting
  2098. * it will fail with response code 01, (APQN not valid); so, let's make
  2099. * sure it is in the host's config.
  2100. */
  2101. if (test_bit_inv(apid, (unsigned long *)matrix_dev->info.apm) &&
  2102. test_bit_inv(apqi, (unsigned long *)matrix_dev->info.aqm)) {
  2103. vfio_ap_mdev_reset_queue(q);
  2104. flush_work(&q->reset_work);
  2105. }
  2106. done:
  2107. if (matrix_mdev)
  2108. vfio_ap_unlink_queue_fr_mdev(q);
  2109. dev_set_drvdata(&apdev->device, NULL);
  2110. kfree(q);
  2111. release_update_locks_for_mdev(matrix_mdev);
  2112. }
  2113. /**
  2114. * vfio_ap_mdev_resource_in_use: check whether any of a set of APQNs is
  2115. * assigned to a mediated device under the control
  2116. * of the vfio_ap device driver.
  2117. *
  2118. * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check.
  2119. * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check.
  2120. *
  2121. * Return:
  2122. * * -EADDRINUSE if one or more of the APQNs specified via @apm/@aqm are
  2123. * assigned to a mediated device under the control of the vfio_ap
  2124. * device driver.
  2125. * * Otherwise, return 0.
  2126. */
  2127. int vfio_ap_mdev_resource_in_use(unsigned long *apm, unsigned long *aqm)
  2128. {
  2129. int ret;
  2130. mutex_lock(&matrix_dev->guests_lock);
  2131. mutex_lock(&matrix_dev->mdevs_lock);
  2132. ret = vfio_ap_mdev_verify_no_sharing(NULL, apm, aqm);
  2133. mutex_unlock(&matrix_dev->mdevs_lock);
  2134. mutex_unlock(&matrix_dev->guests_lock);
  2135. return ret;
  2136. }
  2137. /**
  2138. * vfio_ap_mdev_hot_unplug_cfg - hot unplug the adapters, domains and control
  2139. * domains that have been removed from the host's
  2140. * AP configuration from a guest.
  2141. *
  2142. * @matrix_mdev: an ap_matrix_mdev object attached to a KVM guest.
  2143. * @aprem: the adapters that have been removed from the host's AP configuration
  2144. * @aqrem: the domains that have been removed from the host's AP configuration
  2145. * @cdrem: the control domains that have been removed from the host's AP
  2146. * configuration.
  2147. */
  2148. static void vfio_ap_mdev_hot_unplug_cfg(struct ap_matrix_mdev *matrix_mdev,
  2149. unsigned long *aprem,
  2150. unsigned long *aqrem,
  2151. unsigned long *cdrem)
  2152. {
  2153. int do_hotplug = 0;
  2154. if (!bitmap_empty(aprem, AP_DEVICES)) {
  2155. do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.apm,
  2156. matrix_mdev->shadow_apcb.apm,
  2157. aprem, AP_DEVICES);
  2158. }
  2159. if (!bitmap_empty(aqrem, AP_DOMAINS)) {
  2160. do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.aqm,
  2161. matrix_mdev->shadow_apcb.aqm,
  2162. aqrem, AP_DEVICES);
  2163. }
  2164. if (!bitmap_empty(cdrem, AP_DOMAINS))
  2165. do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.adm,
  2166. matrix_mdev->shadow_apcb.adm,
  2167. cdrem, AP_DOMAINS);
  2168. if (do_hotplug)
  2169. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  2170. }
  2171. /**
  2172. * vfio_ap_mdev_cfg_remove - determines which guests are using the adapters,
  2173. * domains and control domains that have been removed
  2174. * from the host AP configuration and unplugs them
  2175. * from those guests.
  2176. *
  2177. * @ap_remove: bitmap specifying which adapters have been removed from the host
  2178. * config.
  2179. * @aq_remove: bitmap specifying which domains have been removed from the host
  2180. * config.
  2181. * @cd_remove: bitmap specifying which control domains have been removed from
  2182. * the host config.
  2183. */
  2184. static void vfio_ap_mdev_cfg_remove(unsigned long *ap_remove,
  2185. unsigned long *aq_remove,
  2186. unsigned long *cd_remove)
  2187. {
  2188. struct ap_matrix_mdev *matrix_mdev;
  2189. DECLARE_BITMAP(aprem, AP_DEVICES);
  2190. DECLARE_BITMAP(aqrem, AP_DOMAINS);
  2191. DECLARE_BITMAP(cdrem, AP_DOMAINS);
  2192. int do_remove = 0;
  2193. list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
  2194. mutex_lock(&matrix_mdev->kvm->lock);
  2195. mutex_lock(&matrix_dev->mdevs_lock);
  2196. do_remove |= bitmap_and(aprem, ap_remove,
  2197. matrix_mdev->matrix.apm,
  2198. AP_DEVICES);
  2199. do_remove |= bitmap_and(aqrem, aq_remove,
  2200. matrix_mdev->matrix.aqm,
  2201. AP_DOMAINS);
  2202. do_remove |= bitmap_andnot(cdrem, cd_remove,
  2203. matrix_mdev->matrix.adm,
  2204. AP_DOMAINS);
  2205. if (do_remove)
  2206. vfio_ap_mdev_hot_unplug_cfg(matrix_mdev, aprem, aqrem,
  2207. cdrem);
  2208. mutex_unlock(&matrix_dev->mdevs_lock);
  2209. mutex_unlock(&matrix_mdev->kvm->lock);
  2210. }
  2211. }
  2212. /**
  2213. * vfio_ap_mdev_on_cfg_remove - responds to the removal of adapters, domains and
  2214. * control domains from the host AP configuration
  2215. * by unplugging them from the guests that are
  2216. * using them.
  2217. * @cur_config_info: the current host AP configuration information
  2218. * @prev_config_info: the previous host AP configuration information
  2219. */
  2220. static void vfio_ap_mdev_on_cfg_remove(struct ap_config_info *cur_config_info,
  2221. struct ap_config_info *prev_config_info)
  2222. {
  2223. int do_remove;
  2224. DECLARE_BITMAP(aprem, AP_DEVICES);
  2225. DECLARE_BITMAP(aqrem, AP_DOMAINS);
  2226. DECLARE_BITMAP(cdrem, AP_DOMAINS);
  2227. do_remove = bitmap_andnot(aprem,
  2228. (unsigned long *)prev_config_info->apm,
  2229. (unsigned long *)cur_config_info->apm,
  2230. AP_DEVICES);
  2231. do_remove |= bitmap_andnot(aqrem,
  2232. (unsigned long *)prev_config_info->aqm,
  2233. (unsigned long *)cur_config_info->aqm,
  2234. AP_DEVICES);
  2235. do_remove |= bitmap_andnot(cdrem,
  2236. (unsigned long *)prev_config_info->adm,
  2237. (unsigned long *)cur_config_info->adm,
  2238. AP_DEVICES);
  2239. if (do_remove)
  2240. vfio_ap_mdev_cfg_remove(aprem, aqrem, cdrem);
  2241. }
  2242. /**
  2243. * vfio_ap_filter_apid_by_qtype: filter APIDs from an AP mask for adapters that
  2244. * are older than AP type 10 (CEX4).
  2245. * @apm: a bitmap of the APIDs to examine
  2246. * @aqm: a bitmap of the APQIs of the queues to query for the AP type.
  2247. */
  2248. static void vfio_ap_filter_apid_by_qtype(unsigned long *apm, unsigned long *aqm)
  2249. {
  2250. bool apid_cleared;
  2251. struct ap_queue_status status;
  2252. unsigned long apid, apqi;
  2253. struct ap_tapq_hwinfo info;
  2254. for_each_set_bit_inv(apid, apm, AP_DEVICES) {
  2255. apid_cleared = false;
  2256. for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) {
  2257. status = ap_test_queue(AP_MKQID(apid, apqi), 1, &info);
  2258. switch (status.response_code) {
  2259. /*
  2260. * According to the architecture in each case
  2261. * below, the queue's info should be filled.
  2262. */
  2263. case AP_RESPONSE_NORMAL:
  2264. case AP_RESPONSE_RESET_IN_PROGRESS:
  2265. case AP_RESPONSE_DECONFIGURED:
  2266. case AP_RESPONSE_CHECKSTOPPED:
  2267. case AP_RESPONSE_BUSY:
  2268. /*
  2269. * The vfio_ap device driver only
  2270. * supports CEX4 and newer adapters, so
  2271. * remove the APID if the adapter is
  2272. * older than a CEX4.
  2273. */
  2274. if (info.at < AP_DEVICE_TYPE_CEX4) {
  2275. clear_bit_inv(apid, apm);
  2276. apid_cleared = true;
  2277. }
  2278. break;
  2279. default:
  2280. /*
  2281. * If we don't know the adapter type,
  2282. * clear its APID since it can't be
  2283. * determined whether the vfio_ap
  2284. * device driver supports it.
  2285. */
  2286. clear_bit_inv(apid, apm);
  2287. apid_cleared = true;
  2288. break;
  2289. }
  2290. /*
  2291. * If we've already cleared the APID from the apm, there
  2292. * is no need to continue examining the remainin AP
  2293. * queues to determine the type of the adapter.
  2294. */
  2295. if (apid_cleared)
  2296. continue;
  2297. }
  2298. }
  2299. }
  2300. /**
  2301. * vfio_ap_mdev_cfg_add - store bitmaps specifying the adapters, domains and
  2302. * control domains that have been added to the host's
  2303. * AP configuration for each matrix mdev to which they
  2304. * are assigned.
  2305. *
  2306. * @apm_add: a bitmap specifying the adapters that have been added to the AP
  2307. * configuration.
  2308. * @aqm_add: a bitmap specifying the domains that have been added to the AP
  2309. * configuration.
  2310. * @adm_add: a bitmap specifying the control domains that have been added to the
  2311. * AP configuration.
  2312. */
  2313. static void vfio_ap_mdev_cfg_add(unsigned long *apm_add, unsigned long *aqm_add,
  2314. unsigned long *adm_add)
  2315. {
  2316. struct ap_matrix_mdev *matrix_mdev;
  2317. if (list_empty(&matrix_dev->mdev_list))
  2318. return;
  2319. vfio_ap_filter_apid_by_qtype(apm_add, aqm_add);
  2320. list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
  2321. bitmap_and(matrix_mdev->apm_add,
  2322. matrix_mdev->matrix.apm, apm_add, AP_DEVICES);
  2323. bitmap_and(matrix_mdev->aqm_add,
  2324. matrix_mdev->matrix.aqm, aqm_add, AP_DOMAINS);
  2325. bitmap_and(matrix_mdev->adm_add,
  2326. matrix_mdev->matrix.adm, adm_add, AP_DEVICES);
  2327. }
  2328. }
  2329. /**
  2330. * vfio_ap_mdev_on_cfg_add - responds to the addition of adapters, domains and
  2331. * control domains to the host AP configuration
  2332. * by updating the bitmaps that specify what adapters,
  2333. * domains and control domains have been added so they
  2334. * can be hot plugged into the guest when the AP bus
  2335. * scan completes (see vfio_ap_on_scan_complete
  2336. * function).
  2337. * @cur_config_info: the current AP configuration information
  2338. * @prev_config_info: the previous AP configuration information
  2339. */
  2340. static void vfio_ap_mdev_on_cfg_add(struct ap_config_info *cur_config_info,
  2341. struct ap_config_info *prev_config_info)
  2342. {
  2343. bool do_add;
  2344. DECLARE_BITMAP(apm_add, AP_DEVICES);
  2345. DECLARE_BITMAP(aqm_add, AP_DOMAINS);
  2346. DECLARE_BITMAP(adm_add, AP_DOMAINS);
  2347. do_add = bitmap_andnot(apm_add,
  2348. (unsigned long *)cur_config_info->apm,
  2349. (unsigned long *)prev_config_info->apm,
  2350. AP_DEVICES);
  2351. do_add |= bitmap_andnot(aqm_add,
  2352. (unsigned long *)cur_config_info->aqm,
  2353. (unsigned long *)prev_config_info->aqm,
  2354. AP_DOMAINS);
  2355. do_add |= bitmap_andnot(adm_add,
  2356. (unsigned long *)cur_config_info->adm,
  2357. (unsigned long *)prev_config_info->adm,
  2358. AP_DOMAINS);
  2359. if (do_add)
  2360. vfio_ap_mdev_cfg_add(apm_add, aqm_add, adm_add);
  2361. }
  2362. /**
  2363. * vfio_ap_on_cfg_changed - handles notification of changes to the host AP
  2364. * configuration.
  2365. *
  2366. * @cur_cfg_info: the current host AP configuration
  2367. * @prev_cfg_info: the previous host AP configuration
  2368. */
  2369. void vfio_ap_on_cfg_changed(struct ap_config_info *cur_cfg_info,
  2370. struct ap_config_info *prev_cfg_info)
  2371. {
  2372. if (!cur_cfg_info || !prev_cfg_info)
  2373. return;
  2374. mutex_lock(&matrix_dev->guests_lock);
  2375. vfio_ap_mdev_on_cfg_remove(cur_cfg_info, prev_cfg_info);
  2376. vfio_ap_mdev_on_cfg_add(cur_cfg_info, prev_cfg_info);
  2377. memcpy(&matrix_dev->info, cur_cfg_info, sizeof(*cur_cfg_info));
  2378. mutex_unlock(&matrix_dev->guests_lock);
  2379. }
  2380. static void vfio_ap_mdev_hot_plug_cfg(struct ap_matrix_mdev *matrix_mdev)
  2381. {
  2382. DECLARE_BITMAP(apm_filtered, AP_DEVICES);
  2383. bool filter_domains, filter_adapters, filter_cdoms, do_hotplug = false;
  2384. mutex_lock(&matrix_mdev->kvm->lock);
  2385. mutex_lock(&matrix_dev->mdevs_lock);
  2386. filter_adapters = bitmap_intersects(matrix_mdev->matrix.apm,
  2387. matrix_mdev->apm_add, AP_DEVICES);
  2388. filter_domains = bitmap_intersects(matrix_mdev->matrix.aqm,
  2389. matrix_mdev->aqm_add, AP_DOMAINS);
  2390. filter_cdoms = bitmap_intersects(matrix_mdev->matrix.adm,
  2391. matrix_mdev->adm_add, AP_DOMAINS);
  2392. if (filter_adapters || filter_domains)
  2393. do_hotplug = vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered);
  2394. if (filter_cdoms)
  2395. do_hotplug |= vfio_ap_mdev_filter_cdoms(matrix_mdev);
  2396. if (do_hotplug)
  2397. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  2398. reset_queues_for_apids(matrix_mdev, apm_filtered);
  2399. mutex_unlock(&matrix_dev->mdevs_lock);
  2400. mutex_unlock(&matrix_mdev->kvm->lock);
  2401. }
  2402. void vfio_ap_on_scan_complete(struct ap_config_info *new_config_info,
  2403. struct ap_config_info *old_config_info)
  2404. {
  2405. struct ap_matrix_mdev *matrix_mdev;
  2406. mutex_lock(&matrix_dev->guests_lock);
  2407. list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
  2408. if (bitmap_empty(matrix_mdev->apm_add, AP_DEVICES) &&
  2409. bitmap_empty(matrix_mdev->aqm_add, AP_DOMAINS) &&
  2410. bitmap_empty(matrix_mdev->adm_add, AP_DOMAINS))
  2411. continue;
  2412. vfio_ap_mdev_hot_plug_cfg(matrix_mdev);
  2413. bitmap_clear(matrix_mdev->apm_add, 0, AP_DEVICES);
  2414. bitmap_clear(matrix_mdev->aqm_add, 0, AP_DOMAINS);
  2415. bitmap_clear(matrix_mdev->adm_add, 0, AP_DOMAINS);
  2416. }
  2417. mutex_unlock(&matrix_dev->guests_lock);
  2418. }