ap_bus.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright IBM Corp. 2006, 2023
  4. * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
  5. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  6. * Ralph Wuerthner <rwuerthn@de.ibm.com>
  7. * Felix Beck <felix.beck@de.ibm.com>
  8. * Holger Dengler <hd@linux.vnet.ibm.com>
  9. * Harald Freudenberger <freude@linux.ibm.com>
  10. *
  11. * Adjunct processor bus.
  12. */
  13. #define pr_fmt(fmt) "ap: " fmt
  14. #include <linux/kernel_stat.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/export.h>
  17. #include <linux/hex.h>
  18. #include <linux/init.h>
  19. #include <linux/delay.h>
  20. #include <linux/err.h>
  21. #include <linux/freezer.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/slab.h>
  25. #include <linux/notifier.h>
  26. #include <linux/kthread.h>
  27. #include <linux/mutex.h>
  28. #include <asm/machine.h>
  29. #include <asm/airq.h>
  30. #include <asm/tpi.h>
  31. #include <linux/atomic.h>
  32. #include <asm/isc.h>
  33. #include <linux/hrtimer.h>
  34. #include <linux/ktime.h>
  35. #include <asm/facility.h>
  36. #include <linux/crypto.h>
  37. #include <linux/mod_devicetable.h>
  38. #include <linux/debugfs.h>
  39. #include <linux/ctype.h>
  40. #include <linux/module.h>
  41. #include <asm/uv.h>
  42. #include <asm/chsc.h>
  43. #include <linux/mempool.h>
  44. #include "ap_bus.h"
  45. #include "ap_debug.h"
  46. MODULE_AUTHOR("IBM Corporation");
  47. MODULE_DESCRIPTION("Adjunct Processor Bus driver");
  48. MODULE_LICENSE("GPL");
  49. int ap_domain_index = -1; /* Adjunct Processor Domain Index */
  50. static DEFINE_SPINLOCK(ap_domain_lock);
  51. module_param_named(domain, ap_domain_index, int, 0444);
  52. MODULE_PARM_DESC(domain, "domain index for ap devices");
  53. EXPORT_SYMBOL(ap_domain_index);
  54. static int ap_thread_flag;
  55. module_param_named(poll_thread, ap_thread_flag, int, 0444);
  56. MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
  57. static char *apm_str;
  58. module_param_named(apmask, apm_str, charp, 0444);
  59. MODULE_PARM_DESC(apmask, "AP bus adapter mask.");
  60. static char *aqm_str;
  61. module_param_named(aqmask, aqm_str, charp, 0444);
  62. MODULE_PARM_DESC(aqmask, "AP bus domain mask.");
  63. static int ap_useirq = 1;
  64. module_param_named(useirq, ap_useirq, int, 0444);
  65. MODULE_PARM_DESC(useirq, "Use interrupt if available, default is 1 (on).");
  66. atomic_t ap_max_msg_size = ATOMIC_INIT(AP_DEFAULT_MAX_MSG_SIZE);
  67. EXPORT_SYMBOL(ap_max_msg_size);
  68. static struct device *ap_root_device;
  69. /* Hashtable of all queue devices on the AP bus */
  70. DEFINE_HASHTABLE(ap_queues, 8);
  71. /* lock used for the ap_queues hashtable */
  72. DEFINE_SPINLOCK(ap_queues_lock);
  73. /* Default permissions (ioctl, card and domain masking) */
  74. struct ap_perms ap_perms;
  75. EXPORT_SYMBOL(ap_perms);
  76. /* true if apmask and/or aqmask are NOT default */
  77. bool ap_apmask_aqmask_in_use;
  78. /* counter for how many driver_overrides are currently active */
  79. int ap_driver_override_ctr;
  80. /*
  81. * Mutex for consistent read and write of the ap_perms struct,
  82. * ap_apmask_aqmask_in_use, ap_driver_override_ctr
  83. * and the ap bus sysfs attributes apmask and aqmask.
  84. */
  85. DEFINE_MUTEX(ap_attr_mutex);
  86. EXPORT_SYMBOL(ap_attr_mutex);
  87. /* # of bindings complete since init */
  88. static atomic64_t ap_bindings_complete_count = ATOMIC64_INIT(0);
  89. /* completion for APQN bindings complete */
  90. static DECLARE_COMPLETION(ap_apqn_bindings_complete);
  91. static struct ap_config_info qci[2];
  92. static struct ap_config_info *const ap_qci_info = &qci[0];
  93. static struct ap_config_info *const ap_qci_info_old = &qci[1];
  94. /*
  95. * AP bus related debug feature things.
  96. */
  97. debug_info_t *ap_dbf_info;
  98. /*
  99. * There is a need for a do-not-allocate-memory path through the AP bus
  100. * layer. The pkey layer may be triggered via the in-kernel interface from
  101. * a protected key crypto algorithm (namely PAES) to convert a secure key
  102. * into a protected key. This happens in a workqueue context, so sleeping
  103. * is allowed but memory allocations causing IO operations are not permitted.
  104. * To accomplish this, an AP message memory pool with pre-allocated space
  105. * is established. When ap_init_apmsg() with use_mempool set to true is
  106. * called, instead of kmalloc() the ap message buffer is allocated from
  107. * the ap_msg_pool. This pool only holds a limited amount of buffers:
  108. * ap_msg_pool_min_items with the item size AP_DEFAULT_MAX_MSG_SIZE and
  109. * exactly one of these items (if available) is returned if ap_init_apmsg()
  110. * with the use_mempool arg set to true is called. When this pool is exhausted
  111. * and use_mempool is set true, ap_init_apmsg() returns -ENOMEM without
  112. * any attempt to allocate memory and the caller has to deal with that.
  113. */
  114. static mempool_t *ap_msg_pool;
  115. static unsigned int ap_msg_pool_min_items = 8;
  116. module_param_named(msgpool_min_items, ap_msg_pool_min_items, uint, 0400);
  117. MODULE_PARM_DESC(msgpool_min_items, "AP message pool minimal items");
  118. /*
  119. * AP bus rescan related things.
  120. */
  121. static bool ap_scan_bus(void);
  122. static bool ap_scan_bus_result; /* result of last ap_scan_bus() */
  123. static DEFINE_MUTEX(ap_scan_bus_mutex); /* mutex ap_scan_bus() invocations */
  124. static struct task_struct *ap_scan_bus_task; /* thread holding the scan mutex */
  125. static atomic64_t ap_scan_bus_count; /* counter ap_scan_bus() invocations */
  126. static int ap_scan_bus_time = AP_CONFIG_TIME;
  127. static struct timer_list ap_scan_bus_timer;
  128. static void ap_scan_bus_wq_callback(struct work_struct *);
  129. static DECLARE_WORK(ap_scan_bus_work, ap_scan_bus_wq_callback);
  130. /*
  131. * Tasklet & timer for AP request polling and interrupts
  132. */
  133. static void ap_tasklet_fn(unsigned long);
  134. static DECLARE_TASKLET_OLD(ap_tasklet, ap_tasklet_fn);
  135. static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
  136. static struct task_struct *ap_poll_kthread;
  137. static DEFINE_MUTEX(ap_poll_thread_mutex);
  138. static DEFINE_SPINLOCK(ap_poll_timer_lock);
  139. static struct hrtimer ap_poll_timer;
  140. /*
  141. * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
  142. * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.
  143. */
  144. static unsigned long poll_high_timeout = 250000UL;
  145. /*
  146. * Some state machine states only require a low frequency polling.
  147. * We use 25 Hz frequency for these.
  148. */
  149. static unsigned long poll_low_timeout = 40000000UL;
  150. /* Maximum domain id, if not given via qci */
  151. static int ap_max_domain_id = 15;
  152. /* Maximum adapter id, if not given via qci */
  153. static int ap_max_adapter_id = 63;
  154. static const struct bus_type ap_bus_type;
  155. /* Adapter interrupt definitions */
  156. static void ap_interrupt_handler(struct airq_struct *airq,
  157. struct tpi_info *tpi_info);
  158. static bool ap_irq_flag;
  159. static struct airq_struct ap_airq = {
  160. .handler = ap_interrupt_handler,
  161. .isc = AP_ISC,
  162. };
  163. /**
  164. * ap_airq_ptr() - Get the address of the adapter interrupt indicator
  165. *
  166. * Returns the address of the local-summary-indicator of the adapter
  167. * interrupt handler for AP, or NULL if adapter interrupts are not
  168. * available.
  169. */
  170. void *ap_airq_ptr(void)
  171. {
  172. if (ap_irq_flag)
  173. return ap_airq.lsi_ptr;
  174. return NULL;
  175. }
  176. /**
  177. * ap_interrupts_available(): Test if AP interrupts are available.
  178. *
  179. * Returns 1 if AP interrupts are available.
  180. */
  181. static int ap_interrupts_available(void)
  182. {
  183. return test_facility(65);
  184. }
  185. /**
  186. * ap_qci_available(): Test if AP configuration
  187. * information can be queried via QCI subfunction.
  188. *
  189. * Returns 1 if subfunction PQAP(QCI) is available.
  190. */
  191. static int ap_qci_available(void)
  192. {
  193. return test_facility(12);
  194. }
  195. /**
  196. * ap_apft_available(): Test if AP facilities test (APFT)
  197. * facility is available.
  198. *
  199. * Returns 1 if APFT is available.
  200. */
  201. static int ap_apft_available(void)
  202. {
  203. return test_facility(15);
  204. }
  205. /*
  206. * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
  207. *
  208. * Returns 1 if the QACT subfunction is available.
  209. */
  210. static inline int ap_qact_available(void)
  211. {
  212. return ap_qci_info->qact;
  213. }
  214. /*
  215. * ap_sb_available(): Test if the AP secure binding facility is available.
  216. *
  217. * Returns 1 if secure binding facility is available.
  218. */
  219. int ap_sb_available(void)
  220. {
  221. return ap_qci_info->apsb;
  222. }
  223. /*
  224. * ap_is_se_guest(): Check for SE guest with AP pass-through support.
  225. */
  226. bool ap_is_se_guest(void)
  227. {
  228. return is_prot_virt_guest() && ap_sb_available();
  229. }
  230. EXPORT_SYMBOL(ap_is_se_guest);
  231. /**
  232. * ap_init_qci_info(): Allocate and query qci config info.
  233. * Does also update the static variables ap_max_domain_id
  234. * and ap_max_adapter_id if this info is available.
  235. */
  236. static void __init ap_init_qci_info(void)
  237. {
  238. if (!ap_qci_available() ||
  239. ap_qci(ap_qci_info)) {
  240. AP_DBF_INFO("%s QCI not supported\n", __func__);
  241. return;
  242. }
  243. memcpy(ap_qci_info_old, ap_qci_info, sizeof(*ap_qci_info));
  244. AP_DBF_INFO("%s successful fetched initial qci info\n", __func__);
  245. if (ap_qci_info->apxa) {
  246. if (ap_qci_info->na) {
  247. ap_max_adapter_id = ap_qci_info->na;
  248. AP_DBF_INFO("%s new ap_max_adapter_id is %d\n",
  249. __func__, ap_max_adapter_id);
  250. }
  251. if (ap_qci_info->nd) {
  252. ap_max_domain_id = ap_qci_info->nd;
  253. AP_DBF_INFO("%s new ap_max_domain_id is %d\n",
  254. __func__, ap_max_domain_id);
  255. }
  256. }
  257. }
  258. /*
  259. * ap_test_config(): helper function to extract the nrth bit
  260. * within the unsigned int array field.
  261. */
  262. static inline int ap_test_config(unsigned int *field, unsigned int nr)
  263. {
  264. return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
  265. }
  266. /*
  267. * ap_test_config_card_id(): Test, whether an AP card ID is configured.
  268. *
  269. * Returns 0 if the card is not configured
  270. * 1 if the card is configured or
  271. * if the configuration information is not available
  272. */
  273. static inline int ap_test_config_card_id(unsigned int id)
  274. {
  275. if (id > ap_max_adapter_id)
  276. return 0;
  277. if (ap_qci_info->flags)
  278. return ap_test_config(ap_qci_info->apm, id);
  279. return 1;
  280. }
  281. /*
  282. * ap_test_config_usage_domain(): Test, whether an AP usage domain
  283. * is configured.
  284. *
  285. * Returns 0 if the usage domain is not configured
  286. * 1 if the usage domain is configured or
  287. * if the configuration information is not available
  288. */
  289. int ap_test_config_usage_domain(unsigned int domain)
  290. {
  291. if (domain > ap_max_domain_id)
  292. return 0;
  293. if (ap_qci_info->flags)
  294. return ap_test_config(ap_qci_info->aqm, domain);
  295. return 1;
  296. }
  297. EXPORT_SYMBOL(ap_test_config_usage_domain);
  298. /*
  299. * ap_test_config_ctrl_domain(): Test, whether an AP control domain
  300. * is configured.
  301. * @domain AP control domain ID
  302. *
  303. * Returns 1 if the control domain is configured
  304. * 0 in all other cases
  305. */
  306. int ap_test_config_ctrl_domain(unsigned int domain)
  307. {
  308. if (!ap_qci_info || domain > ap_max_domain_id)
  309. return 0;
  310. return ap_test_config(ap_qci_info->adm, domain);
  311. }
  312. EXPORT_SYMBOL(ap_test_config_ctrl_domain);
  313. /*
  314. * ap_queue_info(): Check and get AP queue info.
  315. * Returns: 1 if APQN exists and info is filled,
  316. * 0 if APQN seems to exist but there is no info
  317. * available (eg. caused by an asynch pending error)
  318. * -1 invalid APQN, TAPQ error or AP queue status which
  319. * indicates there is no APQN.
  320. */
  321. static int ap_queue_info(ap_qid_t qid, struct ap_tapq_hwinfo *hwinfo,
  322. bool *decfg, bool *cstop)
  323. {
  324. struct ap_queue_status status;
  325. hwinfo->value = 0;
  326. /* make sure we don't run into a specifiation exception */
  327. if (AP_QID_CARD(qid) > ap_max_adapter_id ||
  328. AP_QID_QUEUE(qid) > ap_max_domain_id)
  329. return -1;
  330. /* call TAPQ on this APQN */
  331. status = ap_test_queue(qid, ap_apft_available(), hwinfo);
  332. switch (status.response_code) {
  333. case AP_RESPONSE_NORMAL:
  334. case AP_RESPONSE_RESET_IN_PROGRESS:
  335. case AP_RESPONSE_DECONFIGURED:
  336. case AP_RESPONSE_CHECKSTOPPED:
  337. case AP_RESPONSE_BUSY:
  338. /* For all these RCs the tapq info should be available */
  339. break;
  340. default:
  341. /* On a pending async error the info should be available */
  342. if (!status.async)
  343. return -1;
  344. break;
  345. }
  346. /* There should be at least one of the mode bits set */
  347. if (WARN_ON_ONCE(!hwinfo->value))
  348. return 0;
  349. *decfg = status.response_code == AP_RESPONSE_DECONFIGURED;
  350. *cstop = status.response_code == AP_RESPONSE_CHECKSTOPPED;
  351. return 1;
  352. }
  353. void ap_wait(enum ap_sm_wait wait)
  354. {
  355. ktime_t hr_time;
  356. switch (wait) {
  357. case AP_SM_WAIT_AGAIN:
  358. case AP_SM_WAIT_INTERRUPT:
  359. if (ap_irq_flag)
  360. break;
  361. if (ap_poll_kthread) {
  362. wake_up(&ap_poll_wait);
  363. break;
  364. }
  365. fallthrough;
  366. case AP_SM_WAIT_LOW_TIMEOUT:
  367. case AP_SM_WAIT_HIGH_TIMEOUT:
  368. spin_lock_bh(&ap_poll_timer_lock);
  369. if (!hrtimer_is_queued(&ap_poll_timer)) {
  370. hr_time =
  371. wait == AP_SM_WAIT_LOW_TIMEOUT ?
  372. poll_low_timeout : poll_high_timeout;
  373. hrtimer_forward_now(&ap_poll_timer, hr_time);
  374. hrtimer_restart(&ap_poll_timer);
  375. }
  376. spin_unlock_bh(&ap_poll_timer_lock);
  377. break;
  378. case AP_SM_WAIT_NONE:
  379. default:
  380. break;
  381. }
  382. }
  383. /**
  384. * ap_request_timeout(): Handling of request timeouts
  385. * @t: timer making this callback
  386. *
  387. * Handles request timeouts.
  388. */
  389. void ap_request_timeout(struct timer_list *t)
  390. {
  391. struct ap_queue *aq = timer_container_of(aq, t, timeout);
  392. spin_lock_bh(&aq->lock);
  393. ap_wait(ap_sm_event(aq, AP_SM_EVENT_TIMEOUT));
  394. spin_unlock_bh(&aq->lock);
  395. }
  396. /**
  397. * ap_poll_timeout(): AP receive polling for finished AP requests.
  398. * @unused: Unused pointer.
  399. *
  400. * Schedules the AP tasklet using a high resolution timer.
  401. */
  402. static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
  403. {
  404. tasklet_schedule(&ap_tasklet);
  405. return HRTIMER_NORESTART;
  406. }
  407. /**
  408. * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
  409. * @airq: pointer to adapter interrupt descriptor
  410. * @tpi_info: ignored
  411. */
  412. static void ap_interrupt_handler(struct airq_struct *airq,
  413. struct tpi_info *tpi_info)
  414. {
  415. inc_irq_stat(IRQIO_APB);
  416. tasklet_schedule(&ap_tasklet);
  417. }
  418. /**
  419. * ap_tasklet_fn(): Tasklet to poll all AP devices.
  420. * @dummy: Unused variable
  421. *
  422. * Poll all AP devices on the bus.
  423. */
  424. static void ap_tasklet_fn(unsigned long dummy)
  425. {
  426. int bkt;
  427. struct ap_queue *aq;
  428. enum ap_sm_wait wait = AP_SM_WAIT_NONE;
  429. /* Reset the indicator if interrupts are used. Thus new interrupts can
  430. * be received. Doing it in the beginning of the tasklet is therefore
  431. * important that no requests on any AP get lost.
  432. */
  433. if (ap_irq_flag)
  434. WRITE_ONCE(*ap_airq.lsi_ptr, 0);
  435. spin_lock_bh(&ap_queues_lock);
  436. hash_for_each(ap_queues, bkt, aq, hnode) {
  437. spin_lock_bh(&aq->lock);
  438. wait = min(wait, ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
  439. spin_unlock_bh(&aq->lock);
  440. }
  441. spin_unlock_bh(&ap_queues_lock);
  442. ap_wait(wait);
  443. }
  444. static int ap_pending_requests(void)
  445. {
  446. int bkt;
  447. struct ap_queue *aq;
  448. spin_lock_bh(&ap_queues_lock);
  449. hash_for_each(ap_queues, bkt, aq, hnode) {
  450. if (aq->queue_count == 0)
  451. continue;
  452. spin_unlock_bh(&ap_queues_lock);
  453. return 1;
  454. }
  455. spin_unlock_bh(&ap_queues_lock);
  456. return 0;
  457. }
  458. /**
  459. * ap_poll_thread(): Thread that polls for finished requests.
  460. * @data: Unused pointer
  461. *
  462. * AP bus poll thread. The purpose of this thread is to poll for
  463. * finished requests in a loop if there is a "free" cpu - that is
  464. * a cpu that doesn't have anything better to do. The polling stops
  465. * as soon as there is another task or if all messages have been
  466. * delivered.
  467. */
  468. static int ap_poll_thread(void *data)
  469. {
  470. DECLARE_WAITQUEUE(wait, current);
  471. set_user_nice(current, MAX_NICE);
  472. set_freezable();
  473. while (!kthread_should_stop()) {
  474. add_wait_queue(&ap_poll_wait, &wait);
  475. set_current_state(TASK_INTERRUPTIBLE);
  476. if (!ap_pending_requests()) {
  477. schedule();
  478. try_to_freeze();
  479. }
  480. set_current_state(TASK_RUNNING);
  481. remove_wait_queue(&ap_poll_wait, &wait);
  482. if (need_resched()) {
  483. schedule();
  484. try_to_freeze();
  485. continue;
  486. }
  487. ap_tasklet_fn(0);
  488. }
  489. return 0;
  490. }
  491. static int ap_poll_thread_start(void)
  492. {
  493. int rc;
  494. if (ap_irq_flag || ap_poll_kthread)
  495. return 0;
  496. mutex_lock(&ap_poll_thread_mutex);
  497. ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
  498. rc = PTR_ERR_OR_ZERO(ap_poll_kthread);
  499. if (rc)
  500. ap_poll_kthread = NULL;
  501. mutex_unlock(&ap_poll_thread_mutex);
  502. return rc;
  503. }
  504. static void ap_poll_thread_stop(void)
  505. {
  506. if (!ap_poll_kthread)
  507. return;
  508. mutex_lock(&ap_poll_thread_mutex);
  509. kthread_stop(ap_poll_kthread);
  510. ap_poll_kthread = NULL;
  511. mutex_unlock(&ap_poll_thread_mutex);
  512. }
  513. #define is_card_dev(x) ((x)->parent == ap_root_device)
  514. #define is_queue_dev(x) ((x)->parent != ap_root_device)
  515. /*
  516. * ap_init_apmsg() - Initialize ap_message.
  517. */
  518. int ap_init_apmsg(struct ap_message *ap_msg, u32 flags)
  519. {
  520. unsigned int maxmsgsize;
  521. memset(ap_msg, 0, sizeof(*ap_msg));
  522. ap_msg->flags = flags;
  523. if (flags & AP_MSG_FLAG_MEMPOOL) {
  524. ap_msg->msg = mempool_alloc_preallocated(ap_msg_pool);
  525. if (!ap_msg->msg)
  526. return -ENOMEM;
  527. ap_msg->bufsize = AP_DEFAULT_MAX_MSG_SIZE;
  528. return 0;
  529. }
  530. maxmsgsize = atomic_read(&ap_max_msg_size);
  531. ap_msg->msg = kmalloc(maxmsgsize, GFP_KERNEL);
  532. if (!ap_msg->msg)
  533. return -ENOMEM;
  534. ap_msg->bufsize = maxmsgsize;
  535. return 0;
  536. }
  537. EXPORT_SYMBOL(ap_init_apmsg);
  538. /*
  539. * ap_release_apmsg() - Release ap_message.
  540. */
  541. void ap_release_apmsg(struct ap_message *ap_msg)
  542. {
  543. if (ap_msg->flags & AP_MSG_FLAG_MEMPOOL) {
  544. memzero_explicit(ap_msg->msg, ap_msg->bufsize);
  545. mempool_free(ap_msg->msg, ap_msg_pool);
  546. } else {
  547. kfree_sensitive(ap_msg->msg);
  548. }
  549. }
  550. EXPORT_SYMBOL(ap_release_apmsg);
  551. /**
  552. * ap_bus_match()
  553. * @dev: Pointer to device
  554. * @drv: Pointer to device_driver
  555. *
  556. * AP bus driver registration/unregistration.
  557. */
  558. static int ap_bus_match(struct device *dev, const struct device_driver *drv)
  559. {
  560. const struct ap_driver *ap_drv = to_ap_drv(drv);
  561. struct ap_device_id *id;
  562. /*
  563. * Compare device type of the device with the list of
  564. * supported types of the device_driver.
  565. */
  566. for (id = ap_drv->ids; id->match_flags; id++) {
  567. if (is_card_dev(dev) &&
  568. id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
  569. id->dev_type == to_ap_dev(dev)->device_type)
  570. return 1;
  571. if (is_queue_dev(dev) &&
  572. id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
  573. id->dev_type == to_ap_dev(dev)->device_type)
  574. return 1;
  575. }
  576. return 0;
  577. }
  578. /**
  579. * ap_uevent(): Uevent function for AP devices.
  580. * @dev: Pointer to device
  581. * @env: Pointer to kobj_uevent_env
  582. *
  583. * It sets up a single environment variable DEV_TYPE which contains the
  584. * hardware device type.
  585. */
  586. static int ap_uevent(const struct device *dev, struct kobj_uevent_env *env)
  587. {
  588. int rc = 0;
  589. const struct ap_device *ap_dev = to_ap_dev(dev);
  590. /* Uevents from ap bus core don't need extensions to the env */
  591. if (dev == ap_root_device)
  592. return 0;
  593. if (is_card_dev(dev)) {
  594. struct ap_card *ac = to_ap_card(&ap_dev->device);
  595. /* Set up DEV_TYPE environment variable. */
  596. rc = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
  597. if (rc)
  598. return rc;
  599. /* Add MODALIAS= */
  600. rc = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
  601. if (rc)
  602. return rc;
  603. /* Add MODE=<accel|cca|ep11> */
  604. if (ac->hwinfo.accel)
  605. rc = add_uevent_var(env, "MODE=accel");
  606. else if (ac->hwinfo.cca)
  607. rc = add_uevent_var(env, "MODE=cca");
  608. else if (ac->hwinfo.ep11)
  609. rc = add_uevent_var(env, "MODE=ep11");
  610. if (rc)
  611. return rc;
  612. } else {
  613. struct ap_queue *aq = to_ap_queue(&ap_dev->device);
  614. /* Add MODE=<accel|cca|ep11> */
  615. if (aq->card->hwinfo.accel)
  616. rc = add_uevent_var(env, "MODE=accel");
  617. else if (aq->card->hwinfo.cca)
  618. rc = add_uevent_var(env, "MODE=cca");
  619. else if (aq->card->hwinfo.ep11)
  620. rc = add_uevent_var(env, "MODE=ep11");
  621. if (rc)
  622. return rc;
  623. }
  624. return 0;
  625. }
  626. static void ap_send_init_scan_done_uevent(void)
  627. {
  628. char *envp[] = { "INITSCAN=done", NULL };
  629. kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
  630. }
  631. static void ap_send_bindings_complete_uevent(void)
  632. {
  633. char buf[32];
  634. char *envp[] = { "BINDINGS=complete", buf, NULL };
  635. snprintf(buf, sizeof(buf), "COMPLETECOUNT=%llu",
  636. atomic64_inc_return(&ap_bindings_complete_count));
  637. kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
  638. }
  639. void ap_send_config_uevent(struct ap_device *ap_dev, bool cfg)
  640. {
  641. char buf[16];
  642. char *envp[] = { buf, NULL };
  643. snprintf(buf, sizeof(buf), "CONFIG=%d", cfg ? 1 : 0);
  644. kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
  645. }
  646. EXPORT_SYMBOL(ap_send_config_uevent);
  647. void ap_send_online_uevent(struct ap_device *ap_dev, int online)
  648. {
  649. char buf[16];
  650. char *envp[] = { buf, NULL };
  651. snprintf(buf, sizeof(buf), "ONLINE=%d", online ? 1 : 0);
  652. kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
  653. }
  654. EXPORT_SYMBOL(ap_send_online_uevent);
  655. static void ap_send_mask_changed_uevent(unsigned long *newapm,
  656. unsigned long *newaqm)
  657. {
  658. char buf[100];
  659. char *envp[] = { buf, NULL };
  660. if (newapm)
  661. snprintf(buf, sizeof(buf),
  662. "APMASK=0x%016lx%016lx%016lx%016lx\n",
  663. newapm[0], newapm[1], newapm[2], newapm[3]);
  664. else
  665. snprintf(buf, sizeof(buf),
  666. "AQMASK=0x%016lx%016lx%016lx%016lx\n",
  667. newaqm[0], newaqm[1], newaqm[2], newaqm[3]);
  668. kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
  669. }
  670. /*
  671. * calc # of bound APQNs
  672. */
  673. struct __ap_calc_ctrs {
  674. unsigned int apqns;
  675. unsigned int bound;
  676. };
  677. static int __ap_calc_helper(struct device *dev, void *arg)
  678. {
  679. struct __ap_calc_ctrs *pctrs = (struct __ap_calc_ctrs *)arg;
  680. if (is_queue_dev(dev)) {
  681. pctrs->apqns++;
  682. if (dev->driver)
  683. pctrs->bound++;
  684. }
  685. return 0;
  686. }
  687. static void ap_calc_bound_apqns(unsigned int *apqns, unsigned int *bound)
  688. {
  689. struct __ap_calc_ctrs ctrs;
  690. memset(&ctrs, 0, sizeof(ctrs));
  691. bus_for_each_dev(&ap_bus_type, NULL, (void *)&ctrs, __ap_calc_helper);
  692. *apqns = ctrs.apqns;
  693. *bound = ctrs.bound;
  694. }
  695. /*
  696. * After ap bus scan do check if all existing APQNs are
  697. * bound to device drivers.
  698. */
  699. static void ap_check_bindings_complete(void)
  700. {
  701. unsigned int apqns, bound;
  702. if (atomic64_read(&ap_scan_bus_count) >= 1) {
  703. ap_calc_bound_apqns(&apqns, &bound);
  704. if (bound == apqns) {
  705. if (!completion_done(&ap_apqn_bindings_complete)) {
  706. complete_all(&ap_apqn_bindings_complete);
  707. ap_send_bindings_complete_uevent();
  708. pr_debug("all apqn bindings complete\n");
  709. }
  710. }
  711. }
  712. }
  713. /*
  714. * Interface to wait for the AP bus to have done one initial ap bus
  715. * scan and all detected APQNs have been bound to device drivers.
  716. * If these both conditions are not fulfilled, this function blocks
  717. * on a condition with wait_for_completion_interruptible_timeout().
  718. * If these both conditions are fulfilled (before the timeout hits)
  719. * the return value is 0. If the timeout (in jiffies) hits instead
  720. * -ETIME is returned. On failures negative return values are
  721. * returned to the caller.
  722. */
  723. int ap_wait_apqn_bindings_complete(unsigned long timeout)
  724. {
  725. int rc = 0;
  726. long l;
  727. if (completion_done(&ap_apqn_bindings_complete))
  728. return 0;
  729. if (timeout)
  730. l = wait_for_completion_interruptible_timeout(
  731. &ap_apqn_bindings_complete, timeout);
  732. else
  733. l = wait_for_completion_interruptible(
  734. &ap_apqn_bindings_complete);
  735. if (l < 0)
  736. rc = l == -ERESTARTSYS ? -EINTR : l;
  737. else if (l == 0 && timeout)
  738. rc = -ETIME;
  739. pr_debug("rc=%d\n", rc);
  740. return rc;
  741. }
  742. EXPORT_SYMBOL(ap_wait_apqn_bindings_complete);
  743. static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
  744. {
  745. if (is_queue_dev(dev) &&
  746. AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long)data)
  747. device_unregister(dev);
  748. return 0;
  749. }
  750. static int __ap_revise_reserved(struct device *dev, void *dummy)
  751. {
  752. int rc, card, queue, devres, drvres;
  753. if (is_queue_dev(dev)) {
  754. struct ap_driver *ap_drv = to_ap_drv(dev->driver);
  755. struct ap_queue *aq = to_ap_queue(dev);
  756. struct ap_device *ap_dev = &aq->ap_dev;
  757. card = AP_QID_CARD(aq->qid);
  758. queue = AP_QID_QUEUE(aq->qid);
  759. if (ap_dev->driver_override) {
  760. if (strcmp(ap_dev->driver_override,
  761. ap_drv->driver.name)) {
  762. pr_debug("reprobing queue=%02x.%04x\n", card, queue);
  763. rc = device_reprobe(dev);
  764. if (rc) {
  765. AP_DBF_WARN("%s reprobing queue=%02x.%04x failed\n",
  766. __func__, card, queue);
  767. }
  768. }
  769. } else {
  770. mutex_lock(&ap_attr_mutex);
  771. devres = test_bit_inv(card, ap_perms.apm) &&
  772. test_bit_inv(queue, ap_perms.aqm);
  773. mutex_unlock(&ap_attr_mutex);
  774. drvres = to_ap_drv(dev->driver)->flags
  775. & AP_DRIVER_FLAG_DEFAULT;
  776. if (!!devres != !!drvres) {
  777. pr_debug("reprobing queue=%02x.%04x\n", card, queue);
  778. rc = device_reprobe(dev);
  779. if (rc) {
  780. AP_DBF_WARN("%s reprobing queue=%02x.%04x failed\n",
  781. __func__, card, queue);
  782. }
  783. }
  784. }
  785. }
  786. return 0;
  787. }
  788. static void ap_bus_revise_bindings(void)
  789. {
  790. bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
  791. }
  792. /**
  793. * ap_owned_by_def_drv: indicates whether an AP adapter is reserved for the
  794. * default host driver or not.
  795. * @card: the APID of the adapter card to check
  796. * @queue: the APQI of the queue to check
  797. *
  798. * Note: the ap_attr_mutex must be locked by the caller of this function.
  799. *
  800. * Return: an int specifying whether the AP adapter is reserved for the host (1)
  801. * or not (0).
  802. */
  803. int ap_owned_by_def_drv(int card, int queue)
  804. {
  805. struct ap_queue *aq;
  806. int rc = 0;
  807. if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
  808. return -EINVAL;
  809. aq = ap_get_qdev(AP_MKQID(card, queue));
  810. if (aq) {
  811. const struct device_driver *drv = aq->ap_dev.device.driver;
  812. const struct ap_driver *ap_drv = to_ap_drv(drv);
  813. bool override = !!aq->ap_dev.driver_override;
  814. if (override && drv && ap_drv->flags & AP_DRIVER_FLAG_DEFAULT)
  815. rc = 1;
  816. put_device(&aq->ap_dev.device);
  817. if (override)
  818. goto out;
  819. }
  820. if (test_bit_inv(card, ap_perms.apm) &&
  821. test_bit_inv(queue, ap_perms.aqm))
  822. rc = 1;
  823. out:
  824. return rc;
  825. }
  826. EXPORT_SYMBOL(ap_owned_by_def_drv);
  827. /**
  828. * ap_apqn_in_matrix_owned_by_def_drv: indicates whether every APQN contained in
  829. * a set is reserved for the host drivers
  830. * or not.
  831. * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check
  832. * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check
  833. *
  834. * Note: the ap_attr_mutex must be locked by the caller of this function.
  835. *
  836. * Return: an int specifying whether each APQN is reserved for the host (1) or
  837. * not (0)
  838. */
  839. int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
  840. unsigned long *aqm)
  841. {
  842. int card, queue, rc = 0;
  843. for (card = 0; !rc && card < AP_DEVICES; card++)
  844. if (test_bit_inv(card, apm))
  845. for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
  846. if (test_bit_inv(queue, aqm))
  847. rc = ap_owned_by_def_drv(card, queue);
  848. return rc;
  849. }
  850. EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
  851. static int ap_device_probe(struct device *dev)
  852. {
  853. struct ap_device *ap_dev = to_ap_dev(dev);
  854. struct ap_driver *ap_drv = to_ap_drv(dev->driver);
  855. int card, queue, devres, drvres, rc = -ENODEV;
  856. if (!get_device(dev))
  857. return rc;
  858. if (is_queue_dev(dev)) {
  859. /*
  860. * If the apqn is marked as reserved/used by ap bus and
  861. * default drivers, only probe with drivers with the default
  862. * flag set. If it is not marked, only probe with drivers
  863. * with the default flag not set.
  864. */
  865. card = AP_QID_CARD(to_ap_queue(dev)->qid);
  866. queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
  867. if (ap_dev->driver_override) {
  868. if (strcmp(ap_dev->driver_override,
  869. ap_drv->driver.name))
  870. goto out;
  871. } else {
  872. mutex_lock(&ap_attr_mutex);
  873. devres = test_bit_inv(card, ap_perms.apm) &&
  874. test_bit_inv(queue, ap_perms.aqm);
  875. mutex_unlock(&ap_attr_mutex);
  876. drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
  877. if (!!devres != !!drvres)
  878. goto out;
  879. }
  880. }
  881. /*
  882. * Rearm the bindings complete completion to trigger
  883. * bindings complete when all devices are bound again
  884. */
  885. reinit_completion(&ap_apqn_bindings_complete);
  886. /* Add queue/card to list of active queues/cards */
  887. spin_lock_bh(&ap_queues_lock);
  888. if (is_queue_dev(dev))
  889. hash_add(ap_queues, &to_ap_queue(dev)->hnode,
  890. to_ap_queue(dev)->qid);
  891. spin_unlock_bh(&ap_queues_lock);
  892. rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
  893. if (rc) {
  894. spin_lock_bh(&ap_queues_lock);
  895. if (is_queue_dev(dev))
  896. hash_del(&to_ap_queue(dev)->hnode);
  897. spin_unlock_bh(&ap_queues_lock);
  898. }
  899. out:
  900. if (rc) {
  901. put_device(dev);
  902. } else {
  903. if (is_queue_dev(dev)) {
  904. pr_debug("queue=%02x.%04x new driver=%s\n",
  905. card, queue, ap_drv->driver.name);
  906. } else {
  907. pr_debug("card=%02x new driver=%s\n",
  908. to_ap_card(dev)->id, ap_drv->driver.name);
  909. }
  910. }
  911. return rc;
  912. }
  913. static void ap_device_remove(struct device *dev)
  914. {
  915. struct ap_device *ap_dev = to_ap_dev(dev);
  916. struct ap_driver *ap_drv = to_ap_drv(dev->driver);
  917. /* prepare ap queue device removal */
  918. if (is_queue_dev(dev))
  919. ap_queue_prepare_remove(to_ap_queue(dev));
  920. /* driver's chance to clean up gracefully */
  921. if (ap_drv->remove)
  922. ap_drv->remove(ap_dev);
  923. /* now do the ap queue device remove */
  924. if (is_queue_dev(dev))
  925. ap_queue_remove(to_ap_queue(dev));
  926. /* Remove queue/card from list of active queues/cards */
  927. spin_lock_bh(&ap_queues_lock);
  928. if (is_queue_dev(dev))
  929. hash_del(&to_ap_queue(dev)->hnode);
  930. spin_unlock_bh(&ap_queues_lock);
  931. put_device(dev);
  932. }
  933. struct ap_queue *ap_get_qdev(ap_qid_t qid)
  934. {
  935. int bkt;
  936. struct ap_queue *aq;
  937. spin_lock_bh(&ap_queues_lock);
  938. hash_for_each(ap_queues, bkt, aq, hnode) {
  939. if (aq->qid == qid) {
  940. get_device(&aq->ap_dev.device);
  941. spin_unlock_bh(&ap_queues_lock);
  942. return aq;
  943. }
  944. }
  945. spin_unlock_bh(&ap_queues_lock);
  946. return NULL;
  947. }
  948. EXPORT_SYMBOL(ap_get_qdev);
  949. int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
  950. char *name)
  951. {
  952. struct device_driver *drv = &ap_drv->driver;
  953. int rc;
  954. drv->bus = &ap_bus_type;
  955. drv->owner = owner;
  956. drv->name = name;
  957. rc = driver_register(drv);
  958. ap_check_bindings_complete();
  959. return rc;
  960. }
  961. EXPORT_SYMBOL(ap_driver_register);
  962. void ap_driver_unregister(struct ap_driver *ap_drv)
  963. {
  964. driver_unregister(&ap_drv->driver);
  965. }
  966. EXPORT_SYMBOL(ap_driver_unregister);
  967. /*
  968. * Enforce a synchronous AP bus rescan.
  969. * Returns true if the bus scan finds a change in the AP configuration
  970. * and AP devices have been added or deleted when this function returns.
  971. */
  972. bool ap_bus_force_rescan(void)
  973. {
  974. unsigned long scan_counter = atomic64_read(&ap_scan_bus_count);
  975. bool rc = false;
  976. pr_debug("> scan counter=%lu\n", scan_counter);
  977. /* Only trigger AP bus scans after the initial scan is done */
  978. if (scan_counter <= 0)
  979. goto out;
  980. /*
  981. * There is one unlikely but nevertheless valid scenario where the
  982. * thread holding the mutex may try to send some crypto load but
  983. * all cards are offline so a rescan is triggered which causes
  984. * a recursive call of ap_bus_force_rescan(). A simple return if
  985. * the mutex is already locked by this thread solves this.
  986. */
  987. if (mutex_is_locked(&ap_scan_bus_mutex)) {
  988. if (ap_scan_bus_task == current)
  989. goto out;
  990. }
  991. /* Try to acquire the AP scan bus mutex */
  992. if (mutex_trylock(&ap_scan_bus_mutex)) {
  993. /* mutex acquired, run the AP bus scan */
  994. ap_scan_bus_task = current;
  995. ap_scan_bus_result = ap_scan_bus();
  996. rc = ap_scan_bus_result;
  997. ap_scan_bus_task = NULL;
  998. mutex_unlock(&ap_scan_bus_mutex);
  999. goto out;
  1000. }
  1001. /*
  1002. * Mutex acquire failed. So there is currently another task
  1003. * already running the AP bus scan. Then let's simple wait
  1004. * for the lock which means the other task has finished and
  1005. * stored the result in ap_scan_bus_result.
  1006. */
  1007. if (mutex_lock_interruptible(&ap_scan_bus_mutex)) {
  1008. /* some error occurred, ignore and go out */
  1009. goto out;
  1010. }
  1011. rc = ap_scan_bus_result;
  1012. mutex_unlock(&ap_scan_bus_mutex);
  1013. out:
  1014. pr_debug("rc=%d\n", rc);
  1015. return rc;
  1016. }
  1017. EXPORT_SYMBOL(ap_bus_force_rescan);
  1018. /*
  1019. * A config change has happened, force an ap bus rescan.
  1020. */
  1021. static int ap_bus_cfg_chg(struct notifier_block *nb,
  1022. unsigned long action, void *data)
  1023. {
  1024. if (action != CHSC_NOTIFY_AP_CFG)
  1025. return NOTIFY_DONE;
  1026. pr_debug("config change, forcing bus rescan\n");
  1027. ap_bus_force_rescan();
  1028. return NOTIFY_OK;
  1029. }
  1030. static struct notifier_block ap_bus_nb = {
  1031. .notifier_call = ap_bus_cfg_chg,
  1032. };
  1033. int ap_hex2bitmap(const char *str, unsigned long *bitmap, int bits)
  1034. {
  1035. int i, n, b;
  1036. /* bits needs to be a multiple of 8 */
  1037. if (bits & 0x07)
  1038. return -EINVAL;
  1039. if (str[0] == '0' && str[1] == 'x')
  1040. str++;
  1041. if (*str == 'x')
  1042. str++;
  1043. for (i = 0; isxdigit(*str) && i < bits; str++) {
  1044. b = hex_to_bin(*str);
  1045. for (n = 0; n < 4; n++)
  1046. if (b & (0x08 >> n))
  1047. set_bit_inv(i + n, bitmap);
  1048. i += 4;
  1049. }
  1050. if (*str == '\n')
  1051. str++;
  1052. if (*str)
  1053. return -EINVAL;
  1054. return 0;
  1055. }
  1056. EXPORT_SYMBOL(ap_hex2bitmap);
  1057. /*
  1058. * modify_bitmap() - parse bitmask argument and modify an existing
  1059. * bit mask accordingly. A concatenation (done with ',') of these
  1060. * terms is recognized:
  1061. * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
  1062. * <bitnr> may be any valid number (hex, decimal or octal) in the range
  1063. * 0...bits-1; the leading + or - is required. Here are some examples:
  1064. * +0-15,+32,-128,-0xFF
  1065. * -0-255,+1-16,+0x128
  1066. * +1,+2,+3,+4,-5,-7-10
  1067. * Returns the new bitmap after all changes have been applied. Every
  1068. * positive value in the string will set a bit and every negative value
  1069. * in the string will clear a bit. As a bit may be touched more than once,
  1070. * the last 'operation' wins:
  1071. * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
  1072. * cleared again. All other bits are unmodified.
  1073. */
  1074. static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
  1075. {
  1076. unsigned long a, i, z;
  1077. char *np, sign;
  1078. /* bits needs to be a multiple of 8 */
  1079. if (bits & 0x07)
  1080. return -EINVAL;
  1081. while (*str) {
  1082. sign = *str++;
  1083. if (sign != '+' && sign != '-')
  1084. return -EINVAL;
  1085. a = z = simple_strtoul(str, &np, 0);
  1086. if (str == np || a >= bits)
  1087. return -EINVAL;
  1088. str = np;
  1089. if (*str == '-') {
  1090. z = simple_strtoul(++str, &np, 0);
  1091. if (str == np || a > z || z >= bits)
  1092. return -EINVAL;
  1093. str = np;
  1094. }
  1095. for (i = a; i <= z; i++)
  1096. if (sign == '+')
  1097. set_bit_inv(i, bitmap);
  1098. else
  1099. clear_bit_inv(i, bitmap);
  1100. while (*str == ',' || *str == '\n')
  1101. str++;
  1102. }
  1103. return 0;
  1104. }
  1105. static int ap_parse_bitmap_str(const char *str, unsigned long *bitmap, int bits,
  1106. unsigned long *newmap)
  1107. {
  1108. unsigned long size;
  1109. int rc;
  1110. size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
  1111. if (*str == '+' || *str == '-') {
  1112. memcpy(newmap, bitmap, size);
  1113. rc = modify_bitmap(str, newmap, bits);
  1114. } else {
  1115. memset(newmap, 0, size);
  1116. rc = ap_hex2bitmap(str, newmap, bits);
  1117. }
  1118. return rc;
  1119. }
  1120. int ap_parse_mask_str(const char *str,
  1121. unsigned long *bitmap, int bits,
  1122. struct mutex *lock)
  1123. {
  1124. unsigned long *newmap, size;
  1125. int rc;
  1126. /* bits needs to be a multiple of 8 */
  1127. if (bits & 0x07)
  1128. return -EINVAL;
  1129. size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
  1130. newmap = kmalloc(size, GFP_KERNEL);
  1131. if (!newmap)
  1132. return -ENOMEM;
  1133. if (mutex_lock_interruptible(lock)) {
  1134. kfree(newmap);
  1135. return -ERESTARTSYS;
  1136. }
  1137. rc = ap_parse_bitmap_str(str, bitmap, bits, newmap);
  1138. if (rc == 0)
  1139. memcpy(bitmap, newmap, size);
  1140. mutex_unlock(lock);
  1141. kfree(newmap);
  1142. return rc;
  1143. }
  1144. EXPORT_SYMBOL(ap_parse_mask_str);
  1145. /*
  1146. * AP bus attributes.
  1147. */
  1148. static ssize_t ap_domain_show(const struct bus_type *bus, char *buf)
  1149. {
  1150. return sysfs_emit(buf, "%d\n", ap_domain_index);
  1151. }
  1152. static ssize_t ap_domain_store(const struct bus_type *bus,
  1153. const char *buf, size_t count)
  1154. {
  1155. int domain;
  1156. if (sscanf(buf, "%i\n", &domain) != 1 ||
  1157. domain < 0 || domain > ap_max_domain_id ||
  1158. !test_bit_inv(domain, ap_perms.aqm))
  1159. return -EINVAL;
  1160. spin_lock_bh(&ap_domain_lock);
  1161. ap_domain_index = domain;
  1162. spin_unlock_bh(&ap_domain_lock);
  1163. AP_DBF_INFO("%s stored new default domain=%d\n",
  1164. __func__, domain);
  1165. return count;
  1166. }
  1167. static BUS_ATTR_RW(ap_domain);
  1168. static ssize_t ap_control_domain_mask_show(const struct bus_type *bus, char *buf)
  1169. {
  1170. if (!ap_qci_info->flags) /* QCI not supported */
  1171. return sysfs_emit(buf, "not supported\n");
  1172. return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
  1173. ap_qci_info->adm[0], ap_qci_info->adm[1],
  1174. ap_qci_info->adm[2], ap_qci_info->adm[3],
  1175. ap_qci_info->adm[4], ap_qci_info->adm[5],
  1176. ap_qci_info->adm[6], ap_qci_info->adm[7]);
  1177. }
  1178. static BUS_ATTR_RO(ap_control_domain_mask);
  1179. static ssize_t ap_usage_domain_mask_show(const struct bus_type *bus, char *buf)
  1180. {
  1181. if (!ap_qci_info->flags) /* QCI not supported */
  1182. return sysfs_emit(buf, "not supported\n");
  1183. return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
  1184. ap_qci_info->aqm[0], ap_qci_info->aqm[1],
  1185. ap_qci_info->aqm[2], ap_qci_info->aqm[3],
  1186. ap_qci_info->aqm[4], ap_qci_info->aqm[5],
  1187. ap_qci_info->aqm[6], ap_qci_info->aqm[7]);
  1188. }
  1189. static BUS_ATTR_RO(ap_usage_domain_mask);
  1190. static ssize_t ap_adapter_mask_show(const struct bus_type *bus, char *buf)
  1191. {
  1192. if (!ap_qci_info->flags) /* QCI not supported */
  1193. return sysfs_emit(buf, "not supported\n");
  1194. return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
  1195. ap_qci_info->apm[0], ap_qci_info->apm[1],
  1196. ap_qci_info->apm[2], ap_qci_info->apm[3],
  1197. ap_qci_info->apm[4], ap_qci_info->apm[5],
  1198. ap_qci_info->apm[6], ap_qci_info->apm[7]);
  1199. }
  1200. static BUS_ATTR_RO(ap_adapter_mask);
  1201. static ssize_t ap_interrupts_show(const struct bus_type *bus, char *buf)
  1202. {
  1203. return sysfs_emit(buf, "%d\n", ap_irq_flag ? 1 : 0);
  1204. }
  1205. static BUS_ATTR_RO(ap_interrupts);
  1206. static ssize_t config_time_show(const struct bus_type *bus, char *buf)
  1207. {
  1208. return sysfs_emit(buf, "%d\n", ap_scan_bus_time);
  1209. }
  1210. static ssize_t config_time_store(const struct bus_type *bus,
  1211. const char *buf, size_t count)
  1212. {
  1213. int time;
  1214. if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
  1215. return -EINVAL;
  1216. ap_scan_bus_time = time;
  1217. mod_timer(&ap_scan_bus_timer, jiffies + ap_scan_bus_time * HZ);
  1218. return count;
  1219. }
  1220. static BUS_ATTR_RW(config_time);
  1221. static ssize_t poll_thread_show(const struct bus_type *bus, char *buf)
  1222. {
  1223. return sysfs_emit(buf, "%d\n", ap_poll_kthread ? 1 : 0);
  1224. }
  1225. static ssize_t poll_thread_store(const struct bus_type *bus,
  1226. const char *buf, size_t count)
  1227. {
  1228. bool value;
  1229. int rc;
  1230. rc = kstrtobool(buf, &value);
  1231. if (rc)
  1232. return rc;
  1233. if (value) {
  1234. rc = ap_poll_thread_start();
  1235. if (rc)
  1236. count = rc;
  1237. } else {
  1238. ap_poll_thread_stop();
  1239. }
  1240. return count;
  1241. }
  1242. static BUS_ATTR_RW(poll_thread);
  1243. static ssize_t poll_timeout_show(const struct bus_type *bus, char *buf)
  1244. {
  1245. return sysfs_emit(buf, "%lu\n", poll_high_timeout);
  1246. }
  1247. static ssize_t poll_timeout_store(const struct bus_type *bus, const char *buf,
  1248. size_t count)
  1249. {
  1250. unsigned long value;
  1251. ktime_t hr_time;
  1252. int rc;
  1253. rc = kstrtoul(buf, 0, &value);
  1254. if (rc)
  1255. return rc;
  1256. /* 120 seconds = maximum poll interval */
  1257. if (value > 120000000000UL)
  1258. return -EINVAL;
  1259. poll_high_timeout = value;
  1260. hr_time = poll_high_timeout;
  1261. spin_lock_bh(&ap_poll_timer_lock);
  1262. hrtimer_cancel(&ap_poll_timer);
  1263. hrtimer_set_expires(&ap_poll_timer, hr_time);
  1264. hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
  1265. spin_unlock_bh(&ap_poll_timer_lock);
  1266. return count;
  1267. }
  1268. static BUS_ATTR_RW(poll_timeout);
  1269. static ssize_t ap_max_domain_id_show(const struct bus_type *bus, char *buf)
  1270. {
  1271. return sysfs_emit(buf, "%d\n", ap_max_domain_id);
  1272. }
  1273. static BUS_ATTR_RO(ap_max_domain_id);
  1274. static ssize_t ap_max_adapter_id_show(const struct bus_type *bus, char *buf)
  1275. {
  1276. return sysfs_emit(buf, "%d\n", ap_max_adapter_id);
  1277. }
  1278. static BUS_ATTR_RO(ap_max_adapter_id);
  1279. static ssize_t apmask_show(const struct bus_type *bus, char *buf)
  1280. {
  1281. int rc;
  1282. if (mutex_lock_interruptible(&ap_attr_mutex))
  1283. return -ERESTARTSYS;
  1284. rc = sysfs_emit(buf, "0x%016lx%016lx%016lx%016lx\n",
  1285. ap_perms.apm[0], ap_perms.apm[1],
  1286. ap_perms.apm[2], ap_perms.apm[3]);
  1287. mutex_unlock(&ap_attr_mutex);
  1288. return rc;
  1289. }
  1290. static int __verify_card_reservations(struct device_driver *drv, void *data)
  1291. {
  1292. int rc = 0;
  1293. struct ap_driver *ap_drv = to_ap_drv(drv);
  1294. unsigned long *newapm = (unsigned long *)data;
  1295. unsigned long aqm_any[BITS_TO_LONGS(AP_DOMAINS)];
  1296. /*
  1297. * increase the driver's module refcounter to be sure it is not
  1298. * going away when we invoke the callback function.
  1299. */
  1300. if (!try_module_get(drv->owner))
  1301. return 0;
  1302. if (ap_drv->in_use) {
  1303. bitmap_fill(aqm_any, AP_DOMAINS);
  1304. rc = ap_drv->in_use(newapm, aqm_any);
  1305. if (rc)
  1306. rc = -EBUSY;
  1307. }
  1308. /* release the driver's module */
  1309. module_put(drv->owner);
  1310. return rc;
  1311. }
  1312. static int apmask_commit(unsigned long *newapm)
  1313. {
  1314. int rc;
  1315. unsigned long reserved[BITS_TO_LONGS(AP_DEVICES)];
  1316. /*
  1317. * Check if any bits in the apmask have been set which will
  1318. * result in queues being removed from non-default drivers
  1319. */
  1320. if (bitmap_andnot(reserved, newapm, ap_perms.apm, AP_DEVICES)) {
  1321. rc = bus_for_each_drv(&ap_bus_type, NULL, reserved,
  1322. __verify_card_reservations);
  1323. if (rc)
  1324. return rc;
  1325. }
  1326. memcpy(ap_perms.apm, newapm, APMASKSIZE);
  1327. /*
  1328. * Update ap_apmask_aqmask_in_use. Note that the
  1329. * ap_attr_mutex has to be obtained here.
  1330. */
  1331. ap_apmask_aqmask_in_use =
  1332. bitmap_full(ap_perms.apm, AP_DEVICES) &&
  1333. bitmap_full(ap_perms.aqm, AP_DOMAINS) ?
  1334. false : true;
  1335. return 0;
  1336. }
  1337. static ssize_t apmask_store(const struct bus_type *bus, const char *buf,
  1338. size_t count)
  1339. {
  1340. DECLARE_BITMAP(newapm, AP_DEVICES);
  1341. int rc = -EINVAL, changes = 0;
  1342. if (mutex_lock_interruptible(&ap_attr_mutex))
  1343. return -ERESTARTSYS;
  1344. /* Do not allow apmask/aqmask if driver override is active */
  1345. if (ap_driver_override_ctr)
  1346. goto done;
  1347. rc = ap_parse_bitmap_str(buf, ap_perms.apm, AP_DEVICES, newapm);
  1348. if (rc)
  1349. goto done;
  1350. changes = memcmp(ap_perms.apm, newapm, APMASKSIZE);
  1351. if (changes)
  1352. rc = apmask_commit(newapm);
  1353. done:
  1354. mutex_unlock(&ap_attr_mutex);
  1355. if (rc)
  1356. return rc;
  1357. if (changes) {
  1358. ap_bus_revise_bindings();
  1359. ap_send_mask_changed_uevent(newapm, NULL);
  1360. }
  1361. return count;
  1362. }
  1363. static BUS_ATTR_RW(apmask);
  1364. static ssize_t aqmask_show(const struct bus_type *bus, char *buf)
  1365. {
  1366. int rc;
  1367. if (mutex_lock_interruptible(&ap_attr_mutex))
  1368. return -ERESTARTSYS;
  1369. rc = sysfs_emit(buf, "0x%016lx%016lx%016lx%016lx\n",
  1370. ap_perms.aqm[0], ap_perms.aqm[1],
  1371. ap_perms.aqm[2], ap_perms.aqm[3]);
  1372. mutex_unlock(&ap_attr_mutex);
  1373. return rc;
  1374. }
  1375. static int __verify_queue_reservations(struct device_driver *drv, void *data)
  1376. {
  1377. int rc = 0;
  1378. struct ap_driver *ap_drv = to_ap_drv(drv);
  1379. unsigned long *newaqm = (unsigned long *)data;
  1380. unsigned long apm_any[BITS_TO_LONGS(AP_DEVICES)];
  1381. /*
  1382. * increase the driver's module refcounter to be sure it is not
  1383. * going away when we invoke the callback function.
  1384. */
  1385. if (!try_module_get(drv->owner))
  1386. return 0;
  1387. if (ap_drv->in_use) {
  1388. bitmap_fill(apm_any, AP_DEVICES);
  1389. rc = ap_drv->in_use(apm_any, newaqm);
  1390. if (rc)
  1391. rc = -EBUSY;
  1392. }
  1393. /* release the driver's module */
  1394. module_put(drv->owner);
  1395. return rc;
  1396. }
  1397. static int aqmask_commit(unsigned long *newaqm)
  1398. {
  1399. int rc;
  1400. unsigned long reserved[BITS_TO_LONGS(AP_DOMAINS)];
  1401. /*
  1402. * Check if any bits in the aqmask have been set which will
  1403. * result in queues being removed from non-default drivers
  1404. */
  1405. if (bitmap_andnot(reserved, newaqm, ap_perms.aqm, AP_DOMAINS)) {
  1406. rc = bus_for_each_drv(&ap_bus_type, NULL, reserved,
  1407. __verify_queue_reservations);
  1408. if (rc)
  1409. return rc;
  1410. }
  1411. memcpy(ap_perms.aqm, newaqm, AQMASKSIZE);
  1412. /*
  1413. * Update ap_apmask_aqmask_in_use. Note that the
  1414. * ap_attr_mutex has to be obtained here.
  1415. */
  1416. ap_apmask_aqmask_in_use =
  1417. bitmap_full(ap_perms.apm, AP_DEVICES) &&
  1418. bitmap_full(ap_perms.aqm, AP_DOMAINS) ?
  1419. false : true;
  1420. return 0;
  1421. }
  1422. static ssize_t aqmask_store(const struct bus_type *bus, const char *buf,
  1423. size_t count)
  1424. {
  1425. DECLARE_BITMAP(newaqm, AP_DOMAINS);
  1426. int rc = -EINVAL, changes = 0;
  1427. if (mutex_lock_interruptible(&ap_attr_mutex))
  1428. return -ERESTARTSYS;
  1429. /* Do not allow apmask/aqmask if driver override is active */
  1430. if (ap_driver_override_ctr)
  1431. goto done;
  1432. rc = ap_parse_bitmap_str(buf, ap_perms.aqm, AP_DOMAINS, newaqm);
  1433. if (rc)
  1434. goto done;
  1435. changes = memcmp(ap_perms.aqm, newaqm, APMASKSIZE);
  1436. if (changes)
  1437. rc = aqmask_commit(newaqm);
  1438. done:
  1439. mutex_unlock(&ap_attr_mutex);
  1440. if (rc)
  1441. return rc;
  1442. if (changes) {
  1443. ap_bus_revise_bindings();
  1444. ap_send_mask_changed_uevent(NULL, newaqm);
  1445. }
  1446. return count;
  1447. }
  1448. static BUS_ATTR_RW(aqmask);
  1449. static ssize_t scans_show(const struct bus_type *bus, char *buf)
  1450. {
  1451. return sysfs_emit(buf, "%llu\n", atomic64_read(&ap_scan_bus_count));
  1452. }
  1453. static ssize_t scans_store(const struct bus_type *bus, const char *buf,
  1454. size_t count)
  1455. {
  1456. AP_DBF_INFO("%s force AP bus rescan\n", __func__);
  1457. ap_bus_force_rescan();
  1458. return count;
  1459. }
  1460. static BUS_ATTR_RW(scans);
  1461. static ssize_t bindings_show(const struct bus_type *bus, char *buf)
  1462. {
  1463. int rc;
  1464. unsigned int apqns, n;
  1465. ap_calc_bound_apqns(&apqns, &n);
  1466. if (atomic64_read(&ap_scan_bus_count) >= 1 && n == apqns)
  1467. rc = sysfs_emit(buf, "%u/%u (complete)\n", n, apqns);
  1468. else
  1469. rc = sysfs_emit(buf, "%u/%u\n", n, apqns);
  1470. return rc;
  1471. }
  1472. static BUS_ATTR_RO(bindings);
  1473. static ssize_t bindings_complete_count_show(const struct bus_type *bus,
  1474. char *buf)
  1475. {
  1476. return sysfs_emit(buf, "%llu\n",
  1477. atomic64_read(&ap_bindings_complete_count));
  1478. }
  1479. static BUS_ATTR_RO(bindings_complete_count);
  1480. static ssize_t features_show(const struct bus_type *bus, char *buf)
  1481. {
  1482. int n = 0;
  1483. if (!ap_qci_info->flags) /* QCI not supported */
  1484. return sysfs_emit(buf, "-\n");
  1485. if (ap_qci_info->apsc)
  1486. n += sysfs_emit_at(buf, n, "APSC ");
  1487. if (ap_qci_info->apxa)
  1488. n += sysfs_emit_at(buf, n, "APXA ");
  1489. if (ap_qci_info->qact)
  1490. n += sysfs_emit_at(buf, n, "QACT ");
  1491. if (ap_qci_info->rc8a)
  1492. n += sysfs_emit_at(buf, n, "RC8A ");
  1493. if (ap_qci_info->apsb)
  1494. n += sysfs_emit_at(buf, n, "APSB ");
  1495. sysfs_emit_at(buf, n == 0 ? 0 : n - 1, "\n");
  1496. return n;
  1497. }
  1498. static BUS_ATTR_RO(features);
  1499. static struct attribute *ap_bus_attrs[] = {
  1500. &bus_attr_ap_domain.attr,
  1501. &bus_attr_ap_control_domain_mask.attr,
  1502. &bus_attr_ap_usage_domain_mask.attr,
  1503. &bus_attr_ap_adapter_mask.attr,
  1504. &bus_attr_config_time.attr,
  1505. &bus_attr_poll_thread.attr,
  1506. &bus_attr_ap_interrupts.attr,
  1507. &bus_attr_poll_timeout.attr,
  1508. &bus_attr_ap_max_domain_id.attr,
  1509. &bus_attr_ap_max_adapter_id.attr,
  1510. &bus_attr_apmask.attr,
  1511. &bus_attr_aqmask.attr,
  1512. &bus_attr_scans.attr,
  1513. &bus_attr_bindings.attr,
  1514. &bus_attr_bindings_complete_count.attr,
  1515. &bus_attr_features.attr,
  1516. NULL,
  1517. };
  1518. ATTRIBUTE_GROUPS(ap_bus);
  1519. static const struct bus_type ap_bus_type = {
  1520. .name = "ap",
  1521. .bus_groups = ap_bus_groups,
  1522. .match = &ap_bus_match,
  1523. .uevent = &ap_uevent,
  1524. .probe = ap_device_probe,
  1525. .remove = ap_device_remove,
  1526. };
  1527. /**
  1528. * ap_select_domain(): Select an AP domain if possible and we haven't
  1529. * already done so before.
  1530. */
  1531. static void ap_select_domain(void)
  1532. {
  1533. struct ap_queue_status status;
  1534. int card, dom;
  1535. /*
  1536. * Choose the default domain. Either the one specified with
  1537. * the "domain=" parameter or the first domain with at least
  1538. * one valid APQN.
  1539. */
  1540. spin_lock_bh(&ap_domain_lock);
  1541. if (ap_domain_index >= 0) {
  1542. /* Domain has already been selected. */
  1543. goto out;
  1544. }
  1545. for (dom = 0; dom <= ap_max_domain_id; dom++) {
  1546. if (!ap_test_config_usage_domain(dom) ||
  1547. !test_bit_inv(dom, ap_perms.aqm))
  1548. continue;
  1549. for (card = 0; card <= ap_max_adapter_id; card++) {
  1550. if (!ap_test_config_card_id(card) ||
  1551. !test_bit_inv(card, ap_perms.apm))
  1552. continue;
  1553. status = ap_test_queue(AP_MKQID(card, dom),
  1554. ap_apft_available(),
  1555. NULL);
  1556. if (status.response_code == AP_RESPONSE_NORMAL)
  1557. break;
  1558. }
  1559. if (card <= ap_max_adapter_id)
  1560. break;
  1561. }
  1562. if (dom <= ap_max_domain_id) {
  1563. ap_domain_index = dom;
  1564. AP_DBF_INFO("%s new default domain is %d\n",
  1565. __func__, ap_domain_index);
  1566. }
  1567. out:
  1568. spin_unlock_bh(&ap_domain_lock);
  1569. }
  1570. /*
  1571. * This function checks the type and returns either 0 for not
  1572. * supported or the highest compatible type value (which may
  1573. * include the input type value).
  1574. */
  1575. static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
  1576. {
  1577. int comp_type = 0;
  1578. /* < CEX4 is not supported */
  1579. if (rawtype < AP_DEVICE_TYPE_CEX4) {
  1580. AP_DBF_WARN("%s queue=%02x.%04x unsupported type %d\n",
  1581. __func__, AP_QID_CARD(qid),
  1582. AP_QID_QUEUE(qid), rawtype);
  1583. return 0;
  1584. }
  1585. /* up to CEX8 known and fully supported */
  1586. if (rawtype <= AP_DEVICE_TYPE_CEX8)
  1587. return rawtype;
  1588. /*
  1589. * unknown new type > CEX8, check for compatibility
  1590. * to the highest known and supported type which is
  1591. * currently CEX8 with the help of the QACT function.
  1592. */
  1593. if (ap_qact_available()) {
  1594. struct ap_queue_status status;
  1595. union ap_qact_ap_info apinfo = {0};
  1596. apinfo.mode = (func >> 26) & 0x07;
  1597. apinfo.cat = AP_DEVICE_TYPE_CEX8;
  1598. status = ap_qact(qid, 0, &apinfo);
  1599. if (status.response_code == AP_RESPONSE_NORMAL &&
  1600. apinfo.cat >= AP_DEVICE_TYPE_CEX4 &&
  1601. apinfo.cat <= AP_DEVICE_TYPE_CEX8)
  1602. comp_type = apinfo.cat;
  1603. }
  1604. if (!comp_type)
  1605. AP_DBF_WARN("%s queue=%02x.%04x unable to map type %d\n",
  1606. __func__, AP_QID_CARD(qid),
  1607. AP_QID_QUEUE(qid), rawtype);
  1608. else if (comp_type != rawtype)
  1609. AP_DBF_INFO("%s queue=%02x.%04x map type %d to %d\n",
  1610. __func__, AP_QID_CARD(qid), AP_QID_QUEUE(qid),
  1611. rawtype, comp_type);
  1612. return comp_type;
  1613. }
  1614. /*
  1615. * Helper function to be used with bus_find_dev
  1616. * matches for the card device with the given id
  1617. */
  1618. static int __match_card_device_with_id(struct device *dev, const void *data)
  1619. {
  1620. return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *)data;
  1621. }
  1622. /*
  1623. * Helper function to be used with bus_find_dev
  1624. * matches for the queue device with a given qid
  1625. */
  1626. static int __match_queue_device_with_qid(struct device *dev, const void *data)
  1627. {
  1628. return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long)data;
  1629. }
  1630. /*
  1631. * Helper function to be used with bus_find_dev
  1632. * matches any queue device with given queue id
  1633. */
  1634. static int __match_queue_device_with_queue_id(struct device *dev, const void *data)
  1635. {
  1636. return is_queue_dev(dev) &&
  1637. AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long)data;
  1638. }
  1639. /* Helper function for notify_config_changed */
  1640. static int __drv_notify_config_changed(struct device_driver *drv, void *data)
  1641. {
  1642. struct ap_driver *ap_drv = to_ap_drv(drv);
  1643. if (try_module_get(drv->owner)) {
  1644. if (ap_drv->on_config_changed)
  1645. ap_drv->on_config_changed(ap_qci_info, ap_qci_info_old);
  1646. module_put(drv->owner);
  1647. }
  1648. return 0;
  1649. }
  1650. /* Notify all drivers about an qci config change */
  1651. static inline void notify_config_changed(void)
  1652. {
  1653. bus_for_each_drv(&ap_bus_type, NULL, NULL,
  1654. __drv_notify_config_changed);
  1655. }
  1656. /* Helper function for notify_scan_complete */
  1657. static int __drv_notify_scan_complete(struct device_driver *drv, void *data)
  1658. {
  1659. struct ap_driver *ap_drv = to_ap_drv(drv);
  1660. if (try_module_get(drv->owner)) {
  1661. if (ap_drv->on_scan_complete)
  1662. ap_drv->on_scan_complete(ap_qci_info,
  1663. ap_qci_info_old);
  1664. module_put(drv->owner);
  1665. }
  1666. return 0;
  1667. }
  1668. /* Notify all drivers about bus scan complete */
  1669. static inline void notify_scan_complete(void)
  1670. {
  1671. bus_for_each_drv(&ap_bus_type, NULL, NULL,
  1672. __drv_notify_scan_complete);
  1673. }
  1674. /*
  1675. * Helper function for ap_scan_bus().
  1676. * Remove card device and associated queue devices.
  1677. */
  1678. static inline void ap_scan_rm_card_dev_and_queue_devs(struct ap_card *ac)
  1679. {
  1680. bus_for_each_dev(&ap_bus_type, NULL,
  1681. (void *)(long)ac->id,
  1682. __ap_queue_devices_with_id_unregister);
  1683. device_unregister(&ac->ap_dev.device);
  1684. }
  1685. /*
  1686. * Helper function for ap_scan_bus().
  1687. * Does the scan bus job for all the domains within
  1688. * a valid adapter given by an ap_card ptr.
  1689. */
  1690. static inline void ap_scan_domains(struct ap_card *ac)
  1691. {
  1692. struct ap_tapq_hwinfo hwinfo;
  1693. bool decfg, chkstop;
  1694. struct ap_queue *aq;
  1695. struct device *dev;
  1696. ap_qid_t qid;
  1697. int rc, dom;
  1698. /*
  1699. * Go through the configuration for the domains and compare them
  1700. * to the existing queue devices. Also take care of the config
  1701. * and error state for the queue devices.
  1702. */
  1703. for (dom = 0; dom <= ap_max_domain_id; dom++) {
  1704. qid = AP_MKQID(ac->id, dom);
  1705. dev = bus_find_device(&ap_bus_type, NULL,
  1706. (void *)(long)qid,
  1707. __match_queue_device_with_qid);
  1708. aq = dev ? to_ap_queue(dev) : NULL;
  1709. if (!ap_test_config_usage_domain(dom)) {
  1710. if (dev) {
  1711. AP_DBF_INFO("%s(%d,%d) not in config anymore, rm queue dev\n",
  1712. __func__, ac->id, dom);
  1713. device_unregister(dev);
  1714. }
  1715. goto put_dev_and_continue;
  1716. }
  1717. /* domain is valid, get info from this APQN */
  1718. rc = ap_queue_info(qid, &hwinfo, &decfg, &chkstop);
  1719. switch (rc) {
  1720. case -1:
  1721. if (dev) {
  1722. AP_DBF_INFO("%s(%d,%d) queue_info() failed, rm queue dev\n",
  1723. __func__, ac->id, dom);
  1724. device_unregister(dev);
  1725. }
  1726. fallthrough;
  1727. case 0:
  1728. goto put_dev_and_continue;
  1729. default:
  1730. break;
  1731. }
  1732. /* if no queue device exists, create a new one */
  1733. if (!aq) {
  1734. aq = ap_queue_create(qid, ac);
  1735. if (!aq) {
  1736. AP_DBF_WARN("%s(%d,%d) ap_queue_create() failed\n",
  1737. __func__, ac->id, dom);
  1738. continue;
  1739. }
  1740. aq->config = !decfg;
  1741. aq->chkstop = chkstop;
  1742. aq->se_bstate = hwinfo.bs;
  1743. dev = &aq->ap_dev.device;
  1744. dev->bus = &ap_bus_type;
  1745. dev->parent = &ac->ap_dev.device;
  1746. dev_set_name(dev, "%02x.%04x", ac->id, dom);
  1747. /* register queue device */
  1748. rc = device_register(dev);
  1749. if (rc) {
  1750. AP_DBF_WARN("%s(%d,%d) device_register() failed\n",
  1751. __func__, ac->id, dom);
  1752. goto put_dev_and_continue;
  1753. }
  1754. /* get it and thus adjust reference counter */
  1755. get_device(dev);
  1756. if (decfg) {
  1757. AP_DBF_INFO("%s(%d,%d) new (decfg) queue dev created\n",
  1758. __func__, ac->id, dom);
  1759. } else if (chkstop) {
  1760. AP_DBF_INFO("%s(%d,%d) new (chkstop) queue dev created\n",
  1761. __func__, ac->id, dom);
  1762. } else {
  1763. /* nudge the queue's state machine */
  1764. ap_queue_init_state(aq);
  1765. AP_DBF_INFO("%s(%d,%d) new queue dev created\n",
  1766. __func__, ac->id, dom);
  1767. }
  1768. goto put_dev_and_continue;
  1769. }
  1770. /* handle state changes on already existing queue device */
  1771. spin_lock_bh(&aq->lock);
  1772. /* SE bind state */
  1773. aq->se_bstate = hwinfo.bs;
  1774. /* checkstop state */
  1775. if (chkstop && !aq->chkstop) {
  1776. /* checkstop on */
  1777. aq->chkstop = true;
  1778. if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
  1779. aq->dev_state = AP_DEV_STATE_ERROR;
  1780. aq->last_err_rc = AP_RESPONSE_CHECKSTOPPED;
  1781. }
  1782. spin_unlock_bh(&aq->lock);
  1783. pr_debug("(%d,%d) queue dev checkstop on\n",
  1784. ac->id, dom);
  1785. /* 'receive' pending messages with -EAGAIN */
  1786. ap_flush_queue(aq);
  1787. goto put_dev_and_continue;
  1788. } else if (!chkstop && aq->chkstop) {
  1789. /* checkstop off */
  1790. aq->chkstop = false;
  1791. if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
  1792. _ap_queue_init_state(aq);
  1793. spin_unlock_bh(&aq->lock);
  1794. pr_debug("(%d,%d) queue dev checkstop off\n",
  1795. ac->id, dom);
  1796. goto put_dev_and_continue;
  1797. }
  1798. /* config state change */
  1799. if (decfg && aq->config) {
  1800. /* config off this queue device */
  1801. aq->config = false;
  1802. if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
  1803. aq->dev_state = AP_DEV_STATE_ERROR;
  1804. aq->last_err_rc = AP_RESPONSE_DECONFIGURED;
  1805. }
  1806. spin_unlock_bh(&aq->lock);
  1807. pr_debug("(%d,%d) queue dev config off\n",
  1808. ac->id, dom);
  1809. ap_send_config_uevent(&aq->ap_dev, aq->config);
  1810. /* 'receive' pending messages with -EAGAIN */
  1811. ap_flush_queue(aq);
  1812. goto put_dev_and_continue;
  1813. } else if (!decfg && !aq->config) {
  1814. /* config on this queue device */
  1815. aq->config = true;
  1816. if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
  1817. _ap_queue_init_state(aq);
  1818. spin_unlock_bh(&aq->lock);
  1819. pr_debug("(%d,%d) queue dev config on\n",
  1820. ac->id, dom);
  1821. ap_send_config_uevent(&aq->ap_dev, aq->config);
  1822. goto put_dev_and_continue;
  1823. }
  1824. /* handle other error states */
  1825. if (!decfg && aq->dev_state == AP_DEV_STATE_ERROR) {
  1826. spin_unlock_bh(&aq->lock);
  1827. /* 'receive' pending messages with -EAGAIN */
  1828. ap_flush_queue(aq);
  1829. /* re-init (with reset) the queue device */
  1830. ap_queue_init_state(aq);
  1831. AP_DBF_INFO("%s(%d,%d) queue dev reinit enforced\n",
  1832. __func__, ac->id, dom);
  1833. goto put_dev_and_continue;
  1834. }
  1835. spin_unlock_bh(&aq->lock);
  1836. put_dev_and_continue:
  1837. put_device(dev);
  1838. }
  1839. }
  1840. /*
  1841. * Helper function for ap_scan_bus().
  1842. * Does the scan bus job for the given adapter id.
  1843. */
  1844. static inline void ap_scan_adapter(int ap)
  1845. {
  1846. struct ap_tapq_hwinfo hwinfo;
  1847. int rc, dom, comp_type;
  1848. bool decfg, chkstop;
  1849. struct ap_card *ac;
  1850. struct device *dev;
  1851. ap_qid_t qid;
  1852. /* Is there currently a card device for this adapter ? */
  1853. dev = bus_find_device(&ap_bus_type, NULL,
  1854. (void *)(long)ap,
  1855. __match_card_device_with_id);
  1856. ac = dev ? to_ap_card(dev) : NULL;
  1857. /* Adapter not in configuration ? */
  1858. if (!ap_test_config_card_id(ap)) {
  1859. if (ac) {
  1860. AP_DBF_INFO("%s(%d) ap not in config any more, rm card and queue devs\n",
  1861. __func__, ap);
  1862. ap_scan_rm_card_dev_and_queue_devs(ac);
  1863. put_device(dev);
  1864. }
  1865. return;
  1866. }
  1867. /*
  1868. * Adapter ap is valid in the current configuration. So do some checks:
  1869. * If no card device exists, build one. If a card device exists, check
  1870. * for type and functions changed. For all this we need to find a valid
  1871. * APQN first.
  1872. */
  1873. for (dom = 0; dom <= ap_max_domain_id; dom++)
  1874. if (ap_test_config_usage_domain(dom)) {
  1875. qid = AP_MKQID(ap, dom);
  1876. if (ap_queue_info(qid, &hwinfo, &decfg, &chkstop) > 0)
  1877. break;
  1878. }
  1879. if (dom > ap_max_domain_id) {
  1880. /* Could not find one valid APQN for this adapter */
  1881. if (ac) {
  1882. AP_DBF_INFO("%s(%d) no type info (no APQN found), rm card and queue devs\n",
  1883. __func__, ap);
  1884. ap_scan_rm_card_dev_and_queue_devs(ac);
  1885. put_device(dev);
  1886. } else {
  1887. pr_debug("(%d) no type info (no APQN found), ignored\n",
  1888. ap);
  1889. }
  1890. return;
  1891. }
  1892. if (!hwinfo.at) {
  1893. /* No apdater type info available, an unusable adapter */
  1894. if (ac) {
  1895. AP_DBF_INFO("%s(%d) no valid type (0) info, rm card and queue devs\n",
  1896. __func__, ap);
  1897. ap_scan_rm_card_dev_and_queue_devs(ac);
  1898. put_device(dev);
  1899. } else {
  1900. pr_debug("(%d) no valid type (0) info, ignored\n", ap);
  1901. }
  1902. return;
  1903. }
  1904. hwinfo.value &= TAPQ_CARD_HWINFO_MASK; /* filter card specific hwinfo */
  1905. if (ac) {
  1906. /* Check APQN against existing card device for changes */
  1907. if (ac->hwinfo.at != hwinfo.at) {
  1908. AP_DBF_INFO("%s(%d) hwtype %d changed, rm card and queue devs\n",
  1909. __func__, ap, hwinfo.at);
  1910. ap_scan_rm_card_dev_and_queue_devs(ac);
  1911. put_device(dev);
  1912. ac = NULL;
  1913. } else if (ac->hwinfo.fac != hwinfo.fac) {
  1914. AP_DBF_INFO("%s(%d) functions 0x%08x changed, rm card and queue devs\n",
  1915. __func__, ap, hwinfo.fac);
  1916. ap_scan_rm_card_dev_and_queue_devs(ac);
  1917. put_device(dev);
  1918. ac = NULL;
  1919. } else {
  1920. /* handle checkstop state change */
  1921. if (chkstop && !ac->chkstop) {
  1922. /* checkstop on */
  1923. ac->chkstop = true;
  1924. AP_DBF_INFO("%s(%d) card dev checkstop on\n",
  1925. __func__, ap);
  1926. } else if (!chkstop && ac->chkstop) {
  1927. /* checkstop off */
  1928. ac->chkstop = false;
  1929. AP_DBF_INFO("%s(%d) card dev checkstop off\n",
  1930. __func__, ap);
  1931. }
  1932. /* handle config state change */
  1933. if (decfg && ac->config) {
  1934. ac->config = false;
  1935. AP_DBF_INFO("%s(%d) card dev config off\n",
  1936. __func__, ap);
  1937. ap_send_config_uevent(&ac->ap_dev, ac->config);
  1938. } else if (!decfg && !ac->config) {
  1939. ac->config = true;
  1940. AP_DBF_INFO("%s(%d) card dev config on\n",
  1941. __func__, ap);
  1942. ap_send_config_uevent(&ac->ap_dev, ac->config);
  1943. }
  1944. }
  1945. }
  1946. if (!ac) {
  1947. /* Build a new card device */
  1948. comp_type = ap_get_compatible_type(qid, hwinfo.at, hwinfo.fac);
  1949. if (!comp_type) {
  1950. AP_DBF_WARN("%s(%d) type %d, can't get compatibility type\n",
  1951. __func__, ap, hwinfo.at);
  1952. return;
  1953. }
  1954. ac = ap_card_create(ap, hwinfo, comp_type);
  1955. if (!ac) {
  1956. AP_DBF_WARN("%s(%d) ap_card_create() failed\n",
  1957. __func__, ap);
  1958. return;
  1959. }
  1960. ac->config = !decfg;
  1961. ac->chkstop = chkstop;
  1962. dev = &ac->ap_dev.device;
  1963. dev->bus = &ap_bus_type;
  1964. dev->parent = ap_root_device;
  1965. dev_set_name(dev, "card%02x", ap);
  1966. /* maybe enlarge ap_max_msg_size to support this card */
  1967. if (ac->maxmsgsize > atomic_read(&ap_max_msg_size)) {
  1968. atomic_set(&ap_max_msg_size, ac->maxmsgsize);
  1969. AP_DBF_INFO("%s(%d) ap_max_msg_size update to %d byte\n",
  1970. __func__, ap,
  1971. atomic_read(&ap_max_msg_size));
  1972. }
  1973. /* Register the new card device with AP bus */
  1974. rc = device_register(dev);
  1975. if (rc) {
  1976. AP_DBF_WARN("%s(%d) device_register() failed\n",
  1977. __func__, ap);
  1978. put_device(dev);
  1979. return;
  1980. }
  1981. /* get it and thus adjust reference counter */
  1982. get_device(dev);
  1983. if (decfg)
  1984. AP_DBF_INFO("%s(%d) new (decfg) card dev type=%d func=0x%08x created\n",
  1985. __func__, ap, hwinfo.at, hwinfo.fac);
  1986. else if (chkstop)
  1987. AP_DBF_INFO("%s(%d) new (chkstop) card dev type=%d func=0x%08x created\n",
  1988. __func__, ap, hwinfo.at, hwinfo.fac);
  1989. else
  1990. AP_DBF_INFO("%s(%d) new card dev type=%d func=0x%08x created\n",
  1991. __func__, ap, hwinfo.at, hwinfo.fac);
  1992. }
  1993. /* Verify the domains and the queue devices for this card */
  1994. ap_scan_domains(ac);
  1995. /* release the card device */
  1996. put_device(&ac->ap_dev.device);
  1997. }
  1998. /**
  1999. * ap_get_configuration - get the host AP configuration
  2000. *
  2001. * Stores the host AP configuration information returned from the previous call
  2002. * to Query Configuration Information (QCI), then retrieves and stores the
  2003. * current AP configuration returned from QCI.
  2004. *
  2005. * Return: true if the host AP configuration changed between calls to QCI;
  2006. * otherwise, return false.
  2007. */
  2008. static bool ap_get_configuration(void)
  2009. {
  2010. if (!ap_qci_info->flags) /* QCI not supported */
  2011. return false;
  2012. memcpy(ap_qci_info_old, ap_qci_info, sizeof(*ap_qci_info));
  2013. ap_qci(ap_qci_info);
  2014. return memcmp(ap_qci_info, ap_qci_info_old,
  2015. sizeof(struct ap_config_info)) != 0;
  2016. }
  2017. /*
  2018. * ap_config_has_new_aps - Check current against old qci info if
  2019. * new adapters have appeared. Returns true if at least one new
  2020. * adapter in the apm mask is showing up. Existing adapters or
  2021. * receding adapters are not counted.
  2022. */
  2023. static bool ap_config_has_new_aps(void)
  2024. {
  2025. unsigned long m[BITS_TO_LONGS(AP_DEVICES)];
  2026. if (!ap_qci_info->flags)
  2027. return false;
  2028. bitmap_andnot(m, (unsigned long *)ap_qci_info->apm,
  2029. (unsigned long *)ap_qci_info_old->apm, AP_DEVICES);
  2030. if (!bitmap_empty(m, AP_DEVICES))
  2031. return true;
  2032. return false;
  2033. }
  2034. /*
  2035. * ap_config_has_new_doms - Check current against old qci info if
  2036. * new (usage) domains have appeared. Returns true if at least one
  2037. * new domain in the aqm mask is showing up. Existing domains or
  2038. * receding domains are not counted.
  2039. */
  2040. static bool ap_config_has_new_doms(void)
  2041. {
  2042. unsigned long m[BITS_TO_LONGS(AP_DOMAINS)];
  2043. if (!ap_qci_info->flags)
  2044. return false;
  2045. bitmap_andnot(m, (unsigned long *)ap_qci_info->aqm,
  2046. (unsigned long *)ap_qci_info_old->aqm, AP_DOMAINS);
  2047. if (!bitmap_empty(m, AP_DOMAINS))
  2048. return true;
  2049. return false;
  2050. }
  2051. /**
  2052. * ap_scan_bus(): Scan the AP bus for new devices
  2053. * Always run under mutex ap_scan_bus_mutex protection
  2054. * which needs to get locked/unlocked by the caller!
  2055. * Returns true if any config change has been detected
  2056. * during the scan, otherwise false.
  2057. */
  2058. static bool ap_scan_bus(void)
  2059. {
  2060. bool config_changed;
  2061. int ap;
  2062. pr_debug(">\n");
  2063. /* (re-)fetch configuration via QCI */
  2064. config_changed = ap_get_configuration();
  2065. if (config_changed) {
  2066. if (ap_config_has_new_aps() || ap_config_has_new_doms()) {
  2067. /*
  2068. * Appearance of new adapters and/or domains need to
  2069. * build new ap devices which need to get bound to an
  2070. * device driver. Thus reset the APQN bindings complete
  2071. * completion.
  2072. */
  2073. reinit_completion(&ap_apqn_bindings_complete);
  2074. }
  2075. /* post a config change notify */
  2076. notify_config_changed();
  2077. }
  2078. ap_select_domain();
  2079. /* loop over all possible adapters */
  2080. for (ap = 0; ap <= ap_max_adapter_id; ap++)
  2081. ap_scan_adapter(ap);
  2082. /* scan complete notify */
  2083. if (config_changed)
  2084. notify_scan_complete();
  2085. /* check if there is at least one queue available with default domain */
  2086. if (ap_domain_index >= 0) {
  2087. struct device *dev =
  2088. bus_find_device(&ap_bus_type, NULL,
  2089. (void *)(long)ap_domain_index,
  2090. __match_queue_device_with_queue_id);
  2091. if (dev)
  2092. put_device(dev);
  2093. else
  2094. AP_DBF_INFO("%s no queue device with default domain %d available\n",
  2095. __func__, ap_domain_index);
  2096. }
  2097. if (atomic64_inc_return(&ap_scan_bus_count) == 1) {
  2098. pr_debug("init scan complete\n");
  2099. ap_send_init_scan_done_uevent();
  2100. }
  2101. ap_check_bindings_complete();
  2102. mod_timer(&ap_scan_bus_timer, jiffies + ap_scan_bus_time * HZ);
  2103. pr_debug("< config_changed=%d\n", config_changed);
  2104. return config_changed;
  2105. }
  2106. /*
  2107. * Callback for the ap_scan_bus_timer
  2108. * Runs periodically, workqueue timer (ap_scan_bus_time)
  2109. */
  2110. static void ap_scan_bus_timer_callback(struct timer_list *unused)
  2111. {
  2112. /*
  2113. * schedule work into the system long wq which when
  2114. * the work is finally executed, calls the AP bus scan.
  2115. */
  2116. queue_work(system_long_wq, &ap_scan_bus_work);
  2117. }
  2118. /*
  2119. * Callback for the ap_scan_bus_work
  2120. */
  2121. static void ap_scan_bus_wq_callback(struct work_struct *unused)
  2122. {
  2123. /*
  2124. * Try to invoke an ap_scan_bus(). If the mutex acquisition
  2125. * fails there is currently another task already running the
  2126. * AP scan bus and there is no need to wait and re-trigger the
  2127. * scan again. Please note at the end of the scan bus function
  2128. * the AP scan bus timer is re-armed which triggers then the
  2129. * ap_scan_bus_timer_callback which enqueues a work into the
  2130. * system_long_wq which invokes this function here again.
  2131. */
  2132. if (mutex_trylock(&ap_scan_bus_mutex)) {
  2133. ap_scan_bus_task = current;
  2134. ap_scan_bus_result = ap_scan_bus();
  2135. ap_scan_bus_task = NULL;
  2136. mutex_unlock(&ap_scan_bus_mutex);
  2137. }
  2138. }
  2139. static inline void __exit ap_async_exit(void)
  2140. {
  2141. if (ap_thread_flag)
  2142. ap_poll_thread_stop();
  2143. chsc_notifier_unregister(&ap_bus_nb);
  2144. cancel_work(&ap_scan_bus_work);
  2145. hrtimer_cancel(&ap_poll_timer);
  2146. timer_delete(&ap_scan_bus_timer);
  2147. }
  2148. static inline int __init ap_async_init(void)
  2149. {
  2150. int rc;
  2151. /* Setup the AP bus rescan timer. */
  2152. timer_setup(&ap_scan_bus_timer, ap_scan_bus_timer_callback, 0);
  2153. /*
  2154. * Setup the high resolution poll timer.
  2155. * If we are running under z/VM adjust polling to z/VM polling rate.
  2156. */
  2157. if (machine_is_vm())
  2158. poll_high_timeout = 1500000;
  2159. hrtimer_setup(&ap_poll_timer, ap_poll_timeout, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  2160. queue_work(system_long_wq, &ap_scan_bus_work);
  2161. rc = chsc_notifier_register(&ap_bus_nb);
  2162. if (rc)
  2163. goto out;
  2164. /* Start the low priority AP bus poll thread. */
  2165. if (!ap_thread_flag)
  2166. return 0;
  2167. rc = ap_poll_thread_start();
  2168. if (rc)
  2169. goto out_notifier;
  2170. return 0;
  2171. out_notifier:
  2172. chsc_notifier_unregister(&ap_bus_nb);
  2173. out:
  2174. cancel_work(&ap_scan_bus_work);
  2175. hrtimer_cancel(&ap_poll_timer);
  2176. timer_delete(&ap_scan_bus_timer);
  2177. return rc;
  2178. }
  2179. static inline void ap_irq_exit(void)
  2180. {
  2181. if (ap_irq_flag)
  2182. unregister_adapter_interrupt(&ap_airq);
  2183. }
  2184. static inline int __init ap_irq_init(void)
  2185. {
  2186. int rc;
  2187. if (!ap_interrupts_available() || !ap_useirq)
  2188. return 0;
  2189. rc = register_adapter_interrupt(&ap_airq);
  2190. ap_irq_flag = (rc == 0);
  2191. return rc;
  2192. }
  2193. static inline void ap_debug_exit(void)
  2194. {
  2195. debug_unregister(ap_dbf_info);
  2196. }
  2197. static inline int __init ap_debug_init(void)
  2198. {
  2199. ap_dbf_info = debug_register("ap", 2, 1,
  2200. AP_DBF_MAX_SPRINTF_ARGS * sizeof(long));
  2201. debug_register_view(ap_dbf_info, &debug_sprintf_view);
  2202. debug_set_level(ap_dbf_info, DBF_ERR);
  2203. return 0;
  2204. }
  2205. static void __init ap_perms_init(void)
  2206. {
  2207. /* all resources usable if no kernel parameter string given */
  2208. memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
  2209. memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
  2210. memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
  2211. /* apm kernel parameter string */
  2212. if (apm_str) {
  2213. memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
  2214. ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
  2215. &ap_attr_mutex);
  2216. }
  2217. /* aqm kernel parameter string */
  2218. if (aqm_str) {
  2219. memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
  2220. ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
  2221. &ap_attr_mutex);
  2222. }
  2223. }
  2224. /**
  2225. * ap_module_init(): The module initialization code.
  2226. *
  2227. * Initializes the module.
  2228. */
  2229. static int __init ap_module_init(void)
  2230. {
  2231. int rc;
  2232. if (!ap_instructions_available()) {
  2233. pr_warn("The hardware system does not support AP instructions\n");
  2234. return -ENODEV;
  2235. }
  2236. rc = ap_debug_init();
  2237. if (rc)
  2238. return rc;
  2239. /* init ap_queue hashtable */
  2240. hash_init(ap_queues);
  2241. /* create ap msg buffer memory pool */
  2242. ap_msg_pool = mempool_create_kmalloc_pool(ap_msg_pool_min_items,
  2243. AP_DEFAULT_MAX_MSG_SIZE);
  2244. if (!ap_msg_pool) {
  2245. rc = -ENOMEM;
  2246. goto out;
  2247. }
  2248. /* set up the AP permissions (ioctls, ap and aq masks) */
  2249. ap_perms_init();
  2250. /* Get AP configuration data if available */
  2251. ap_init_qci_info();
  2252. /* check default domain setting */
  2253. if (ap_domain_index < -1 || ap_domain_index > ap_max_domain_id ||
  2254. (ap_domain_index >= 0 &&
  2255. !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
  2256. pr_warn("%d is not a valid cryptographic domain\n",
  2257. ap_domain_index);
  2258. ap_domain_index = -1;
  2259. }
  2260. /* Create /sys/bus/ap. */
  2261. rc = bus_register(&ap_bus_type);
  2262. if (rc)
  2263. goto out;
  2264. /* Create /sys/devices/ap. */
  2265. ap_root_device = root_device_register("ap");
  2266. rc = PTR_ERR_OR_ZERO(ap_root_device);
  2267. if (rc)
  2268. goto out_bus;
  2269. ap_root_device->bus = &ap_bus_type;
  2270. /* enable interrupts if available */
  2271. rc = ap_irq_init();
  2272. if (rc)
  2273. goto out_device;
  2274. /* Setup asynchronous work (timers, workqueue, etc). */
  2275. rc = ap_async_init();
  2276. if (rc)
  2277. goto out_irq;
  2278. return 0;
  2279. out_irq:
  2280. ap_irq_exit();
  2281. out_device:
  2282. root_device_unregister(ap_root_device);
  2283. out_bus:
  2284. bus_unregister(&ap_bus_type);
  2285. out:
  2286. mempool_destroy(ap_msg_pool);
  2287. ap_debug_exit();
  2288. return rc;
  2289. }
  2290. static void __exit ap_module_exit(void)
  2291. {
  2292. ap_async_exit();
  2293. ap_irq_exit();
  2294. root_device_unregister(ap_root_device);
  2295. bus_unregister(&ap_bus_type);
  2296. mempool_destroy(ap_msg_pool);
  2297. ap_debug_exit();
  2298. }
  2299. module_init(ap_module_init);
  2300. module_exit(ap_module_exit);