ap_bus.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright IBM Corp. 2006, 2012
  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. *
  10. * Adjunct processor bus.
  11. */
  12. #define KMSG_COMPONENT "ap"
  13. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  14. #include <linux/kernel_stat.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/init.h>
  17. #include <linux/delay.h>
  18. #include <linux/err.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/slab.h>
  22. #include <linux/notifier.h>
  23. #include <linux/kthread.h>
  24. #include <linux/mutex.h>
  25. #include <linux/suspend.h>
  26. #include <asm/airq.h>
  27. #include <linux/atomic.h>
  28. #include <asm/isc.h>
  29. #include <linux/hrtimer.h>
  30. #include <linux/ktime.h>
  31. #include <asm/facility.h>
  32. #include <linux/crypto.h>
  33. #include <linux/mod_devicetable.h>
  34. #include <linux/debugfs.h>
  35. #include <linux/ctype.h>
  36. #include "ap_bus.h"
  37. #include "ap_debug.h"
  38. /*
  39. * Module parameters; note though this file itself isn't modular.
  40. */
  41. int ap_domain_index = -1; /* Adjunct Processor Domain Index */
  42. static DEFINE_SPINLOCK(ap_domain_lock);
  43. module_param_named(domain, ap_domain_index, int, 0440);
  44. MODULE_PARM_DESC(domain, "domain index for ap devices");
  45. EXPORT_SYMBOL(ap_domain_index);
  46. static int ap_thread_flag;
  47. module_param_named(poll_thread, ap_thread_flag, int, 0440);
  48. MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
  49. static char *apm_str;
  50. module_param_named(apmask, apm_str, charp, 0440);
  51. MODULE_PARM_DESC(apmask, "AP bus adapter mask.");
  52. static char *aqm_str;
  53. module_param_named(aqmask, aqm_str, charp, 0440);
  54. MODULE_PARM_DESC(aqmask, "AP bus domain mask.");
  55. static struct device *ap_root_device;
  56. DEFINE_SPINLOCK(ap_list_lock);
  57. LIST_HEAD(ap_card_list);
  58. /* Default permissions (card and domain masking) */
  59. static struct ap_perms {
  60. DECLARE_BITMAP(apm, AP_DEVICES);
  61. DECLARE_BITMAP(aqm, AP_DOMAINS);
  62. } ap_perms;
  63. static DEFINE_MUTEX(ap_perms_mutex);
  64. static struct ap_config_info *ap_configuration;
  65. static bool initialised;
  66. /*
  67. * AP bus related debug feature things.
  68. */
  69. debug_info_t *ap_dbf_info;
  70. /*
  71. * Workqueue timer for bus rescan.
  72. */
  73. static struct timer_list ap_config_timer;
  74. static int ap_config_time = AP_CONFIG_TIME;
  75. static void ap_scan_bus(struct work_struct *);
  76. static DECLARE_WORK(ap_scan_work, ap_scan_bus);
  77. /*
  78. * Tasklet & timer for AP request polling and interrupts
  79. */
  80. static void ap_tasklet_fn(unsigned long);
  81. static DECLARE_TASKLET(ap_tasklet, ap_tasklet_fn, 0);
  82. static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
  83. static struct task_struct *ap_poll_kthread;
  84. static DEFINE_MUTEX(ap_poll_thread_mutex);
  85. static DEFINE_SPINLOCK(ap_poll_timer_lock);
  86. static struct hrtimer ap_poll_timer;
  87. /*
  88. * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
  89. * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.
  90. */
  91. static unsigned long long poll_timeout = 250000;
  92. /* Suspend flag */
  93. static int ap_suspend_flag;
  94. /* Maximum domain id */
  95. static int ap_max_domain_id;
  96. /*
  97. * Flag to check if domain was set through module parameter domain=. This is
  98. * important when supsend and resume is done in a z/VM environment where the
  99. * domain might change.
  100. */
  101. static int user_set_domain;
  102. static struct bus_type ap_bus_type;
  103. /* Adapter interrupt definitions */
  104. static void ap_interrupt_handler(struct airq_struct *airq);
  105. static int ap_airq_flag;
  106. static struct airq_struct ap_airq = {
  107. .handler = ap_interrupt_handler,
  108. .isc = AP_ISC,
  109. };
  110. /**
  111. * ap_using_interrupts() - Returns non-zero if interrupt support is
  112. * available.
  113. */
  114. static inline int ap_using_interrupts(void)
  115. {
  116. return ap_airq_flag;
  117. }
  118. /**
  119. * ap_airq_ptr() - Get the address of the adapter interrupt indicator
  120. *
  121. * Returns the address of the local-summary-indicator of the adapter
  122. * interrupt handler for AP, or NULL if adapter interrupts are not
  123. * available.
  124. */
  125. void *ap_airq_ptr(void)
  126. {
  127. if (ap_using_interrupts())
  128. return ap_airq.lsi_ptr;
  129. return NULL;
  130. }
  131. /**
  132. * ap_interrupts_available(): Test if AP interrupts are available.
  133. *
  134. * Returns 1 if AP interrupts are available.
  135. */
  136. static int ap_interrupts_available(void)
  137. {
  138. return test_facility(65);
  139. }
  140. /**
  141. * ap_configuration_available(): Test if AP configuration
  142. * information is available.
  143. *
  144. * Returns 1 if AP configuration information is available.
  145. */
  146. static int ap_configuration_available(void)
  147. {
  148. return test_facility(12);
  149. }
  150. /**
  151. * ap_apft_available(): Test if AP facilities test (APFT)
  152. * facility is available.
  153. *
  154. * Returns 1 if APFT is is available.
  155. */
  156. static int ap_apft_available(void)
  157. {
  158. return test_facility(15);
  159. }
  160. /*
  161. * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
  162. *
  163. * Returns 1 if the QACT subfunction is available.
  164. */
  165. static inline int ap_qact_available(void)
  166. {
  167. if (ap_configuration)
  168. return ap_configuration->qact;
  169. return 0;
  170. }
  171. /*
  172. * ap_query_configuration(): Fetch cryptographic config info
  173. *
  174. * Returns the ap configuration info fetched via PQAP(QCI).
  175. * On success 0 is returned, on failure a negative errno
  176. * is returned, e.g. if the PQAP(QCI) instruction is not
  177. * available, the return value will be -EOPNOTSUPP.
  178. */
  179. static inline int ap_query_configuration(struct ap_config_info *info)
  180. {
  181. if (!ap_configuration_available())
  182. return -EOPNOTSUPP;
  183. if (!info)
  184. return -EINVAL;
  185. return ap_qci(info);
  186. }
  187. EXPORT_SYMBOL(ap_query_configuration);
  188. /**
  189. * ap_init_configuration(): Allocate and query configuration array.
  190. */
  191. static void ap_init_configuration(void)
  192. {
  193. if (!ap_configuration_available())
  194. return;
  195. ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL);
  196. if (!ap_configuration)
  197. return;
  198. if (ap_query_configuration(ap_configuration) != 0) {
  199. kfree(ap_configuration);
  200. ap_configuration = NULL;
  201. return;
  202. }
  203. }
  204. /*
  205. * ap_test_config(): helper function to extract the nrth bit
  206. * within the unsigned int array field.
  207. */
  208. static inline int ap_test_config(unsigned int *field, unsigned int nr)
  209. {
  210. return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
  211. }
  212. /*
  213. * ap_test_config_card_id(): Test, whether an AP card ID is configured.
  214. * @id AP card ID
  215. *
  216. * Returns 0 if the card is not configured
  217. * 1 if the card is configured or
  218. * if the configuration information is not available
  219. */
  220. static inline int ap_test_config_card_id(unsigned int id)
  221. {
  222. if (!ap_configuration) /* QCI not supported */
  223. /* only ids 0...3F may be probed */
  224. return id < 0x40 ? 1 : 0;
  225. return ap_test_config(ap_configuration->apm, id);
  226. }
  227. /*
  228. * ap_test_config_domain(): Test, whether an AP usage domain is configured.
  229. * @domain AP usage domain ID
  230. *
  231. * Returns 0 if the usage domain is not configured
  232. * 1 if the usage domain is configured or
  233. * if the configuration information is not available
  234. */
  235. static inline int ap_test_config_domain(unsigned int domain)
  236. {
  237. if (!ap_configuration) /* QCI not supported */
  238. return domain < 16;
  239. return ap_test_config(ap_configuration->aqm, domain);
  240. }
  241. /**
  242. * ap_query_queue(): Check if an AP queue is available.
  243. * @qid: The AP queue number
  244. * @queue_depth: Pointer to queue depth value
  245. * @device_type: Pointer to device type value
  246. * @facilities: Pointer to facility indicator
  247. */
  248. static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
  249. unsigned int *facilities)
  250. {
  251. struct ap_queue_status status;
  252. unsigned long info;
  253. int nd;
  254. if (!ap_test_config_card_id(AP_QID_CARD(qid)))
  255. return -ENODEV;
  256. status = ap_test_queue(qid, ap_apft_available(), &info);
  257. switch (status.response_code) {
  258. case AP_RESPONSE_NORMAL:
  259. *queue_depth = (int)(info & 0xff);
  260. *device_type = (int)((info >> 24) & 0xff);
  261. *facilities = (unsigned int)(info >> 32);
  262. /* Update maximum domain id */
  263. nd = (info >> 16) & 0xff;
  264. /* if N bit is available, z13 and newer */
  265. if ((info & (1UL << 57)) && nd > 0)
  266. ap_max_domain_id = nd;
  267. else /* older machine types */
  268. ap_max_domain_id = 15;
  269. switch (*device_type) {
  270. /* For CEX2 and CEX3 the available functions
  271. * are not refrected by the facilities bits.
  272. * Instead it is coded into the type. So here
  273. * modify the function bits based on the type.
  274. */
  275. case AP_DEVICE_TYPE_CEX2A:
  276. case AP_DEVICE_TYPE_CEX3A:
  277. *facilities |= 0x08000000;
  278. break;
  279. case AP_DEVICE_TYPE_CEX2C:
  280. case AP_DEVICE_TYPE_CEX3C:
  281. *facilities |= 0x10000000;
  282. break;
  283. default:
  284. break;
  285. }
  286. return 0;
  287. case AP_RESPONSE_Q_NOT_AVAIL:
  288. case AP_RESPONSE_DECONFIGURED:
  289. case AP_RESPONSE_CHECKSTOPPED:
  290. case AP_RESPONSE_INVALID_ADDRESS:
  291. return -ENODEV;
  292. case AP_RESPONSE_RESET_IN_PROGRESS:
  293. case AP_RESPONSE_OTHERWISE_CHANGED:
  294. case AP_RESPONSE_BUSY:
  295. return -EBUSY;
  296. default:
  297. BUG();
  298. }
  299. }
  300. void ap_wait(enum ap_wait wait)
  301. {
  302. ktime_t hr_time;
  303. switch (wait) {
  304. case AP_WAIT_AGAIN:
  305. case AP_WAIT_INTERRUPT:
  306. if (ap_using_interrupts())
  307. break;
  308. if (ap_poll_kthread) {
  309. wake_up(&ap_poll_wait);
  310. break;
  311. }
  312. /* Fall through */
  313. case AP_WAIT_TIMEOUT:
  314. spin_lock_bh(&ap_poll_timer_lock);
  315. if (!hrtimer_is_queued(&ap_poll_timer)) {
  316. hr_time = poll_timeout;
  317. hrtimer_forward_now(&ap_poll_timer, hr_time);
  318. hrtimer_restart(&ap_poll_timer);
  319. }
  320. spin_unlock_bh(&ap_poll_timer_lock);
  321. break;
  322. case AP_WAIT_NONE:
  323. default:
  324. break;
  325. }
  326. }
  327. /**
  328. * ap_request_timeout(): Handling of request timeouts
  329. * @t: timer making this callback
  330. *
  331. * Handles request timeouts.
  332. */
  333. void ap_request_timeout(struct timer_list *t)
  334. {
  335. struct ap_queue *aq = from_timer(aq, t, timeout);
  336. if (ap_suspend_flag)
  337. return;
  338. spin_lock_bh(&aq->lock);
  339. ap_wait(ap_sm_event(aq, AP_EVENT_TIMEOUT));
  340. spin_unlock_bh(&aq->lock);
  341. }
  342. /**
  343. * ap_poll_timeout(): AP receive polling for finished AP requests.
  344. * @unused: Unused pointer.
  345. *
  346. * Schedules the AP tasklet using a high resolution timer.
  347. */
  348. static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
  349. {
  350. if (!ap_suspend_flag)
  351. tasklet_schedule(&ap_tasklet);
  352. return HRTIMER_NORESTART;
  353. }
  354. /**
  355. * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
  356. * @airq: pointer to adapter interrupt descriptor
  357. */
  358. static void ap_interrupt_handler(struct airq_struct *airq)
  359. {
  360. inc_irq_stat(IRQIO_APB);
  361. if (!ap_suspend_flag)
  362. tasklet_schedule(&ap_tasklet);
  363. }
  364. /**
  365. * ap_tasklet_fn(): Tasklet to poll all AP devices.
  366. * @dummy: Unused variable
  367. *
  368. * Poll all AP devices on the bus.
  369. */
  370. static void ap_tasklet_fn(unsigned long dummy)
  371. {
  372. struct ap_card *ac;
  373. struct ap_queue *aq;
  374. enum ap_wait wait = AP_WAIT_NONE;
  375. /* Reset the indicator if interrupts are used. Thus new interrupts can
  376. * be received. Doing it in the beginning of the tasklet is therefor
  377. * important that no requests on any AP get lost.
  378. */
  379. if (ap_using_interrupts())
  380. xchg(ap_airq.lsi_ptr, 0);
  381. spin_lock_bh(&ap_list_lock);
  382. for_each_ap_card(ac) {
  383. for_each_ap_queue(aq, ac) {
  384. spin_lock_bh(&aq->lock);
  385. wait = min(wait, ap_sm_event_loop(aq, AP_EVENT_POLL));
  386. spin_unlock_bh(&aq->lock);
  387. }
  388. }
  389. spin_unlock_bh(&ap_list_lock);
  390. ap_wait(wait);
  391. }
  392. static int ap_pending_requests(void)
  393. {
  394. struct ap_card *ac;
  395. struct ap_queue *aq;
  396. spin_lock_bh(&ap_list_lock);
  397. for_each_ap_card(ac) {
  398. for_each_ap_queue(aq, ac) {
  399. if (aq->queue_count == 0)
  400. continue;
  401. spin_unlock_bh(&ap_list_lock);
  402. return 1;
  403. }
  404. }
  405. spin_unlock_bh(&ap_list_lock);
  406. return 0;
  407. }
  408. /**
  409. * ap_poll_thread(): Thread that polls for finished requests.
  410. * @data: Unused pointer
  411. *
  412. * AP bus poll thread. The purpose of this thread is to poll for
  413. * finished requests in a loop if there is a "free" cpu - that is
  414. * a cpu that doesn't have anything better to do. The polling stops
  415. * as soon as there is another task or if all messages have been
  416. * delivered.
  417. */
  418. static int ap_poll_thread(void *data)
  419. {
  420. DECLARE_WAITQUEUE(wait, current);
  421. set_user_nice(current, MAX_NICE);
  422. set_freezable();
  423. while (!kthread_should_stop()) {
  424. add_wait_queue(&ap_poll_wait, &wait);
  425. set_current_state(TASK_INTERRUPTIBLE);
  426. if (ap_suspend_flag || !ap_pending_requests()) {
  427. schedule();
  428. try_to_freeze();
  429. }
  430. set_current_state(TASK_RUNNING);
  431. remove_wait_queue(&ap_poll_wait, &wait);
  432. if (need_resched()) {
  433. schedule();
  434. try_to_freeze();
  435. continue;
  436. }
  437. ap_tasklet_fn(0);
  438. }
  439. return 0;
  440. }
  441. static int ap_poll_thread_start(void)
  442. {
  443. int rc;
  444. if (ap_using_interrupts() || ap_poll_kthread)
  445. return 0;
  446. mutex_lock(&ap_poll_thread_mutex);
  447. ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
  448. rc = PTR_ERR_OR_ZERO(ap_poll_kthread);
  449. if (rc)
  450. ap_poll_kthread = NULL;
  451. mutex_unlock(&ap_poll_thread_mutex);
  452. return rc;
  453. }
  454. static void ap_poll_thread_stop(void)
  455. {
  456. if (!ap_poll_kthread)
  457. return;
  458. mutex_lock(&ap_poll_thread_mutex);
  459. kthread_stop(ap_poll_kthread);
  460. ap_poll_kthread = NULL;
  461. mutex_unlock(&ap_poll_thread_mutex);
  462. }
  463. #define is_card_dev(x) ((x)->parent == ap_root_device)
  464. #define is_queue_dev(x) ((x)->parent != ap_root_device)
  465. /**
  466. * ap_bus_match()
  467. * @dev: Pointer to device
  468. * @drv: Pointer to device_driver
  469. *
  470. * AP bus driver registration/unregistration.
  471. */
  472. static int ap_bus_match(struct device *dev, struct device_driver *drv)
  473. {
  474. struct ap_driver *ap_drv = to_ap_drv(drv);
  475. struct ap_device_id *id;
  476. /*
  477. * Compare device type of the device with the list of
  478. * supported types of the device_driver.
  479. */
  480. for (id = ap_drv->ids; id->match_flags; id++) {
  481. if (is_card_dev(dev) &&
  482. id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
  483. id->dev_type == to_ap_dev(dev)->device_type)
  484. return 1;
  485. if (is_queue_dev(dev) &&
  486. id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
  487. id->dev_type == to_ap_dev(dev)->device_type)
  488. return 1;
  489. }
  490. return 0;
  491. }
  492. /**
  493. * ap_uevent(): Uevent function for AP devices.
  494. * @dev: Pointer to device
  495. * @env: Pointer to kobj_uevent_env
  496. *
  497. * It sets up a single environment variable DEV_TYPE which contains the
  498. * hardware device type.
  499. */
  500. static int ap_uevent(struct device *dev, struct kobj_uevent_env *env)
  501. {
  502. struct ap_device *ap_dev = to_ap_dev(dev);
  503. int retval = 0;
  504. if (!ap_dev)
  505. return -ENODEV;
  506. /* Set up DEV_TYPE environment variable. */
  507. retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
  508. if (retval)
  509. return retval;
  510. /* Add MODALIAS= */
  511. retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
  512. return retval;
  513. }
  514. static int ap_dev_suspend(struct device *dev)
  515. {
  516. struct ap_device *ap_dev = to_ap_dev(dev);
  517. if (ap_dev->drv && ap_dev->drv->suspend)
  518. ap_dev->drv->suspend(ap_dev);
  519. return 0;
  520. }
  521. static int ap_dev_resume(struct device *dev)
  522. {
  523. struct ap_device *ap_dev = to_ap_dev(dev);
  524. if (ap_dev->drv && ap_dev->drv->resume)
  525. ap_dev->drv->resume(ap_dev);
  526. return 0;
  527. }
  528. static void ap_bus_suspend(void)
  529. {
  530. AP_DBF(DBF_DEBUG, "%s running\n", __func__);
  531. ap_suspend_flag = 1;
  532. /*
  533. * Disable scanning for devices, thus we do not want to scan
  534. * for them after removing.
  535. */
  536. flush_work(&ap_scan_work);
  537. tasklet_disable(&ap_tasklet);
  538. }
  539. static int __ap_card_devices_unregister(struct device *dev, void *dummy)
  540. {
  541. if (is_card_dev(dev))
  542. device_unregister(dev);
  543. return 0;
  544. }
  545. static int __ap_queue_devices_unregister(struct device *dev, void *dummy)
  546. {
  547. if (is_queue_dev(dev))
  548. device_unregister(dev);
  549. return 0;
  550. }
  551. static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
  552. {
  553. if (is_queue_dev(dev) &&
  554. AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
  555. device_unregister(dev);
  556. return 0;
  557. }
  558. static void ap_bus_resume(void)
  559. {
  560. int rc;
  561. AP_DBF(DBF_DEBUG, "%s running\n", __func__);
  562. /* remove all queue devices */
  563. bus_for_each_dev(&ap_bus_type, NULL, NULL,
  564. __ap_queue_devices_unregister);
  565. /* remove all card devices */
  566. bus_for_each_dev(&ap_bus_type, NULL, NULL,
  567. __ap_card_devices_unregister);
  568. /* Reset thin interrupt setting */
  569. if (ap_interrupts_available() && !ap_using_interrupts()) {
  570. rc = register_adapter_interrupt(&ap_airq);
  571. ap_airq_flag = (rc == 0);
  572. }
  573. if (!ap_interrupts_available() && ap_using_interrupts()) {
  574. unregister_adapter_interrupt(&ap_airq);
  575. ap_airq_flag = 0;
  576. }
  577. /* Reset domain */
  578. if (!user_set_domain)
  579. ap_domain_index = -1;
  580. /* Get things going again */
  581. ap_suspend_flag = 0;
  582. if (ap_airq_flag)
  583. xchg(ap_airq.lsi_ptr, 0);
  584. tasklet_enable(&ap_tasklet);
  585. queue_work(system_long_wq, &ap_scan_work);
  586. }
  587. static int ap_power_event(struct notifier_block *this, unsigned long event,
  588. void *ptr)
  589. {
  590. switch (event) {
  591. case PM_HIBERNATION_PREPARE:
  592. case PM_SUSPEND_PREPARE:
  593. ap_bus_suspend();
  594. break;
  595. case PM_POST_HIBERNATION:
  596. case PM_POST_SUSPEND:
  597. ap_bus_resume();
  598. break;
  599. default:
  600. break;
  601. }
  602. return NOTIFY_DONE;
  603. }
  604. static struct notifier_block ap_power_notifier = {
  605. .notifier_call = ap_power_event,
  606. };
  607. static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops, ap_dev_suspend, ap_dev_resume);
  608. static struct bus_type ap_bus_type = {
  609. .name = "ap",
  610. .match = &ap_bus_match,
  611. .uevent = &ap_uevent,
  612. .pm = &ap_bus_pm_ops,
  613. };
  614. static int __ap_revise_reserved(struct device *dev, void *dummy)
  615. {
  616. int rc, card, queue, devres, drvres;
  617. if (is_queue_dev(dev)) {
  618. card = AP_QID_CARD(to_ap_queue(dev)->qid);
  619. queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
  620. mutex_lock(&ap_perms_mutex);
  621. devres = test_bit_inv(card, ap_perms.apm)
  622. && test_bit_inv(queue, ap_perms.aqm);
  623. mutex_unlock(&ap_perms_mutex);
  624. drvres = to_ap_drv(dev->driver)->flags
  625. & AP_DRIVER_FLAG_DEFAULT;
  626. if (!!devres != !!drvres) {
  627. AP_DBF(DBF_DEBUG, "reprobing queue=%02x.%04x\n",
  628. card, queue);
  629. rc = device_reprobe(dev);
  630. }
  631. }
  632. return 0;
  633. }
  634. static void ap_bus_revise_bindings(void)
  635. {
  636. bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
  637. }
  638. int ap_owned_by_def_drv(int card, int queue)
  639. {
  640. int rc = 0;
  641. if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
  642. return -EINVAL;
  643. mutex_lock(&ap_perms_mutex);
  644. if (test_bit_inv(card, ap_perms.apm)
  645. && test_bit_inv(queue, ap_perms.aqm))
  646. rc = 1;
  647. mutex_unlock(&ap_perms_mutex);
  648. return rc;
  649. }
  650. EXPORT_SYMBOL(ap_owned_by_def_drv);
  651. int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
  652. unsigned long *aqm)
  653. {
  654. int card, queue, rc = 0;
  655. mutex_lock(&ap_perms_mutex);
  656. for (card = 0; !rc && card < AP_DEVICES; card++)
  657. if (test_bit_inv(card, apm) &&
  658. test_bit_inv(card, ap_perms.apm))
  659. for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
  660. if (test_bit_inv(queue, aqm) &&
  661. test_bit_inv(queue, ap_perms.aqm))
  662. rc = 1;
  663. mutex_unlock(&ap_perms_mutex);
  664. return rc;
  665. }
  666. EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
  667. static int ap_device_probe(struct device *dev)
  668. {
  669. struct ap_device *ap_dev = to_ap_dev(dev);
  670. struct ap_driver *ap_drv = to_ap_drv(dev->driver);
  671. int card, queue, devres, drvres, rc;
  672. if (is_queue_dev(dev)) {
  673. /*
  674. * If the apqn is marked as reserved/used by ap bus and
  675. * default drivers, only probe with drivers with the default
  676. * flag set. If it is not marked, only probe with drivers
  677. * with the default flag not set.
  678. */
  679. card = AP_QID_CARD(to_ap_queue(dev)->qid);
  680. queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
  681. mutex_lock(&ap_perms_mutex);
  682. devres = test_bit_inv(card, ap_perms.apm)
  683. && test_bit_inv(queue, ap_perms.aqm);
  684. mutex_unlock(&ap_perms_mutex);
  685. drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
  686. if (!!devres != !!drvres)
  687. return -ENODEV;
  688. /* (re-)init queue's state machine */
  689. ap_queue_reinit_state(to_ap_queue(dev));
  690. }
  691. /* Add queue/card to list of active queues/cards */
  692. spin_lock_bh(&ap_list_lock);
  693. if (is_card_dev(dev))
  694. list_add(&to_ap_card(dev)->list, &ap_card_list);
  695. else
  696. list_add(&to_ap_queue(dev)->list,
  697. &to_ap_queue(dev)->card->queues);
  698. spin_unlock_bh(&ap_list_lock);
  699. ap_dev->drv = ap_drv;
  700. rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
  701. if (rc) {
  702. spin_lock_bh(&ap_list_lock);
  703. if (is_card_dev(dev))
  704. list_del_init(&to_ap_card(dev)->list);
  705. else
  706. list_del_init(&to_ap_queue(dev)->list);
  707. spin_unlock_bh(&ap_list_lock);
  708. ap_dev->drv = NULL;
  709. }
  710. return rc;
  711. }
  712. static int ap_device_remove(struct device *dev)
  713. {
  714. struct ap_device *ap_dev = to_ap_dev(dev);
  715. struct ap_driver *ap_drv = ap_dev->drv;
  716. if (is_queue_dev(dev))
  717. ap_queue_remove(to_ap_queue(dev));
  718. if (ap_drv->remove)
  719. ap_drv->remove(ap_dev);
  720. /* Remove queue/card from list of active queues/cards */
  721. spin_lock_bh(&ap_list_lock);
  722. if (is_card_dev(dev))
  723. list_del_init(&to_ap_card(dev)->list);
  724. else
  725. list_del_init(&to_ap_queue(dev)->list);
  726. spin_unlock_bh(&ap_list_lock);
  727. return 0;
  728. }
  729. int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
  730. char *name)
  731. {
  732. struct device_driver *drv = &ap_drv->driver;
  733. if (!initialised)
  734. return -ENODEV;
  735. drv->bus = &ap_bus_type;
  736. drv->probe = ap_device_probe;
  737. drv->remove = ap_device_remove;
  738. drv->owner = owner;
  739. drv->name = name;
  740. return driver_register(drv);
  741. }
  742. EXPORT_SYMBOL(ap_driver_register);
  743. void ap_driver_unregister(struct ap_driver *ap_drv)
  744. {
  745. driver_unregister(&ap_drv->driver);
  746. }
  747. EXPORT_SYMBOL(ap_driver_unregister);
  748. void ap_bus_force_rescan(void)
  749. {
  750. if (ap_suspend_flag)
  751. return;
  752. /* processing a asynchronous bus rescan */
  753. del_timer(&ap_config_timer);
  754. queue_work(system_long_wq, &ap_scan_work);
  755. flush_work(&ap_scan_work);
  756. }
  757. EXPORT_SYMBOL(ap_bus_force_rescan);
  758. /*
  759. * hex2bitmap() - parse hex mask string and set bitmap.
  760. * Valid strings are "0x012345678" with at least one valid hex number.
  761. * Rest of the bitmap to the right is padded with 0. No spaces allowed
  762. * within the string, the leading 0x may be omitted.
  763. * Returns the bitmask with exactly the bits set as given by the hex
  764. * string (both in big endian order).
  765. */
  766. static int hex2bitmap(const char *str, unsigned long *bitmap, int bits)
  767. {
  768. int i, n, b;
  769. /* bits needs to be a multiple of 8 */
  770. if (bits & 0x07)
  771. return -EINVAL;
  772. if (str[0] == '0' && str[1] == 'x')
  773. str++;
  774. if (*str == 'x')
  775. str++;
  776. for (i = 0; isxdigit(*str) && i < bits; str++) {
  777. b = hex_to_bin(*str);
  778. for (n = 0; n < 4; n++)
  779. if (b & (0x08 >> n))
  780. set_bit_inv(i + n, bitmap);
  781. i += 4;
  782. }
  783. if (*str == '\n')
  784. str++;
  785. if (*str)
  786. return -EINVAL;
  787. return 0;
  788. }
  789. /*
  790. * modify_bitmap() - parse bitmask argument and modify an existing
  791. * bit mask accordingly. A concatenation (done with ',') of these
  792. * terms is recognized:
  793. * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
  794. * <bitnr> may be any valid number (hex, decimal or octal) in the range
  795. * 0...bits-1; the leading + or - is required. Here are some examples:
  796. * +0-15,+32,-128,-0xFF
  797. * -0-255,+1-16,+0x128
  798. * +1,+2,+3,+4,-5,-7-10
  799. * Returns the new bitmap after all changes have been applied. Every
  800. * positive value in the string will set a bit and every negative value
  801. * in the string will clear a bit. As a bit may be touched more than once,
  802. * the last 'operation' wins:
  803. * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
  804. * cleared again. All other bits are unmodified.
  805. */
  806. static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
  807. {
  808. int a, i, z;
  809. char *np, sign;
  810. /* bits needs to be a multiple of 8 */
  811. if (bits & 0x07)
  812. return -EINVAL;
  813. while (*str) {
  814. sign = *str++;
  815. if (sign != '+' && sign != '-')
  816. return -EINVAL;
  817. a = z = simple_strtoul(str, &np, 0);
  818. if (str == np || a >= bits)
  819. return -EINVAL;
  820. str = np;
  821. if (*str == '-') {
  822. z = simple_strtoul(++str, &np, 0);
  823. if (str == np || a > z || z >= bits)
  824. return -EINVAL;
  825. str = np;
  826. }
  827. for (i = a; i <= z; i++)
  828. if (sign == '+')
  829. set_bit_inv(i, bitmap);
  830. else
  831. clear_bit_inv(i, bitmap);
  832. while (*str == ',' || *str == '\n')
  833. str++;
  834. }
  835. return 0;
  836. }
  837. /*
  838. * process_mask_arg() - parse a bitmap string and clear/set the
  839. * bits in the bitmap accordingly. The string may be given as
  840. * absolute value, a hex string like 0x1F2E3D4C5B6A" simple over-
  841. * writing the current content of the bitmap. Or as relative string
  842. * like "+1-16,-32,-0x40,+128" where only single bits or ranges of
  843. * bits are cleared or set. Distinction is done based on the very
  844. * first character which may be '+' or '-' for the relative string
  845. * and othewise assume to be an absolute value string. If parsing fails
  846. * a negative errno value is returned. All arguments and bitmaps are
  847. * big endian order.
  848. */
  849. static int process_mask_arg(const char *str,
  850. unsigned long *bitmap, int bits,
  851. struct mutex *lock)
  852. {
  853. unsigned long *newmap, size;
  854. int rc;
  855. /* bits needs to be a multiple of 8 */
  856. if (bits & 0x07)
  857. return -EINVAL;
  858. size = BITS_TO_LONGS(bits)*sizeof(unsigned long);
  859. newmap = kmalloc(size, GFP_KERNEL);
  860. if (!newmap)
  861. return -ENOMEM;
  862. if (mutex_lock_interruptible(lock)) {
  863. kfree(newmap);
  864. return -ERESTARTSYS;
  865. }
  866. if (*str == '+' || *str == '-') {
  867. memcpy(newmap, bitmap, size);
  868. rc = modify_bitmap(str, newmap, bits);
  869. } else {
  870. memset(newmap, 0, size);
  871. rc = hex2bitmap(str, newmap, bits);
  872. }
  873. if (rc == 0)
  874. memcpy(bitmap, newmap, size);
  875. mutex_unlock(lock);
  876. kfree(newmap);
  877. return rc;
  878. }
  879. /*
  880. * AP bus attributes.
  881. */
  882. static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
  883. {
  884. return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
  885. }
  886. static ssize_t ap_domain_store(struct bus_type *bus,
  887. const char *buf, size_t count)
  888. {
  889. int domain;
  890. if (sscanf(buf, "%i\n", &domain) != 1 ||
  891. domain < 0 || domain > ap_max_domain_id ||
  892. !test_bit_inv(domain, ap_perms.aqm))
  893. return -EINVAL;
  894. spin_lock_bh(&ap_domain_lock);
  895. ap_domain_index = domain;
  896. spin_unlock_bh(&ap_domain_lock);
  897. AP_DBF(DBF_DEBUG, "stored new default domain=%d\n", domain);
  898. return count;
  899. }
  900. static BUS_ATTR_RW(ap_domain);
  901. static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
  902. {
  903. if (!ap_configuration) /* QCI not supported */
  904. return snprintf(buf, PAGE_SIZE, "not supported\n");
  905. return snprintf(buf, PAGE_SIZE,
  906. "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
  907. ap_configuration->adm[0], ap_configuration->adm[1],
  908. ap_configuration->adm[2], ap_configuration->adm[3],
  909. ap_configuration->adm[4], ap_configuration->adm[5],
  910. ap_configuration->adm[6], ap_configuration->adm[7]);
  911. }
  912. static BUS_ATTR_RO(ap_control_domain_mask);
  913. static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
  914. {
  915. if (!ap_configuration) /* QCI not supported */
  916. return snprintf(buf, PAGE_SIZE, "not supported\n");
  917. return snprintf(buf, PAGE_SIZE,
  918. "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
  919. ap_configuration->aqm[0], ap_configuration->aqm[1],
  920. ap_configuration->aqm[2], ap_configuration->aqm[3],
  921. ap_configuration->aqm[4], ap_configuration->aqm[5],
  922. ap_configuration->aqm[6], ap_configuration->aqm[7]);
  923. }
  924. static BUS_ATTR_RO(ap_usage_domain_mask);
  925. static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
  926. {
  927. return snprintf(buf, PAGE_SIZE, "%d\n",
  928. ap_using_interrupts() ? 1 : 0);
  929. }
  930. static BUS_ATTR_RO(ap_interrupts);
  931. static ssize_t config_time_show(struct bus_type *bus, char *buf)
  932. {
  933. return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
  934. }
  935. static ssize_t config_time_store(struct bus_type *bus,
  936. const char *buf, size_t count)
  937. {
  938. int time;
  939. if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
  940. return -EINVAL;
  941. ap_config_time = time;
  942. mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
  943. return count;
  944. }
  945. static BUS_ATTR_RW(config_time);
  946. static ssize_t poll_thread_show(struct bus_type *bus, char *buf)
  947. {
  948. return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
  949. }
  950. static ssize_t poll_thread_store(struct bus_type *bus,
  951. const char *buf, size_t count)
  952. {
  953. int flag, rc;
  954. if (sscanf(buf, "%d\n", &flag) != 1)
  955. return -EINVAL;
  956. if (flag) {
  957. rc = ap_poll_thread_start();
  958. if (rc)
  959. count = rc;
  960. } else
  961. ap_poll_thread_stop();
  962. return count;
  963. }
  964. static BUS_ATTR_RW(poll_thread);
  965. static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
  966. {
  967. return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
  968. }
  969. static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
  970. size_t count)
  971. {
  972. unsigned long long time;
  973. ktime_t hr_time;
  974. /* 120 seconds = maximum poll interval */
  975. if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
  976. time > 120000000000ULL)
  977. return -EINVAL;
  978. poll_timeout = time;
  979. hr_time = poll_timeout;
  980. spin_lock_bh(&ap_poll_timer_lock);
  981. hrtimer_cancel(&ap_poll_timer);
  982. hrtimer_set_expires(&ap_poll_timer, hr_time);
  983. hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
  984. spin_unlock_bh(&ap_poll_timer_lock);
  985. return count;
  986. }
  987. static BUS_ATTR_RW(poll_timeout);
  988. static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
  989. {
  990. int max_domain_id;
  991. if (ap_configuration)
  992. max_domain_id = ap_max_domain_id ? : -1;
  993. else
  994. max_domain_id = 15;
  995. return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
  996. }
  997. static BUS_ATTR_RO(ap_max_domain_id);
  998. static ssize_t apmask_show(struct bus_type *bus, char *buf)
  999. {
  1000. int rc;
  1001. if (mutex_lock_interruptible(&ap_perms_mutex))
  1002. return -ERESTARTSYS;
  1003. rc = snprintf(buf, PAGE_SIZE,
  1004. "0x%016lx%016lx%016lx%016lx\n",
  1005. ap_perms.apm[0], ap_perms.apm[1],
  1006. ap_perms.apm[2], ap_perms.apm[3]);
  1007. mutex_unlock(&ap_perms_mutex);
  1008. return rc;
  1009. }
  1010. static ssize_t apmask_store(struct bus_type *bus, const char *buf,
  1011. size_t count)
  1012. {
  1013. int rc;
  1014. rc = process_mask_arg(buf, ap_perms.apm, AP_DEVICES, &ap_perms_mutex);
  1015. if (rc)
  1016. return rc;
  1017. ap_bus_revise_bindings();
  1018. return count;
  1019. }
  1020. static BUS_ATTR_RW(apmask);
  1021. static ssize_t aqmask_show(struct bus_type *bus, char *buf)
  1022. {
  1023. int rc;
  1024. if (mutex_lock_interruptible(&ap_perms_mutex))
  1025. return -ERESTARTSYS;
  1026. rc = snprintf(buf, PAGE_SIZE,
  1027. "0x%016lx%016lx%016lx%016lx\n",
  1028. ap_perms.aqm[0], ap_perms.aqm[1],
  1029. ap_perms.aqm[2], ap_perms.aqm[3]);
  1030. mutex_unlock(&ap_perms_mutex);
  1031. return rc;
  1032. }
  1033. static ssize_t aqmask_store(struct bus_type *bus, const char *buf,
  1034. size_t count)
  1035. {
  1036. int rc;
  1037. rc = process_mask_arg(buf, ap_perms.aqm, AP_DOMAINS, &ap_perms_mutex);
  1038. if (rc)
  1039. return rc;
  1040. ap_bus_revise_bindings();
  1041. return count;
  1042. }
  1043. static BUS_ATTR_RW(aqmask);
  1044. static struct bus_attribute *const ap_bus_attrs[] = {
  1045. &bus_attr_ap_domain,
  1046. &bus_attr_ap_control_domain_mask,
  1047. &bus_attr_ap_usage_domain_mask,
  1048. &bus_attr_config_time,
  1049. &bus_attr_poll_thread,
  1050. &bus_attr_ap_interrupts,
  1051. &bus_attr_poll_timeout,
  1052. &bus_attr_ap_max_domain_id,
  1053. &bus_attr_apmask,
  1054. &bus_attr_aqmask,
  1055. NULL,
  1056. };
  1057. /**
  1058. * ap_select_domain(): Select an AP domain if possible and we haven't
  1059. * already done so before.
  1060. */
  1061. static void ap_select_domain(void)
  1062. {
  1063. int count, max_count, best_domain;
  1064. struct ap_queue_status status;
  1065. int i, j;
  1066. /*
  1067. * We want to use a single domain. Either the one specified with
  1068. * the "domain=" parameter or the domain with the maximum number
  1069. * of devices.
  1070. */
  1071. spin_lock_bh(&ap_domain_lock);
  1072. if (ap_domain_index >= 0) {
  1073. /* Domain has already been selected. */
  1074. spin_unlock_bh(&ap_domain_lock);
  1075. return;
  1076. }
  1077. best_domain = -1;
  1078. max_count = 0;
  1079. for (i = 0; i < AP_DOMAINS; i++) {
  1080. if (!ap_test_config_domain(i) ||
  1081. !test_bit_inv(i, ap_perms.aqm))
  1082. continue;
  1083. count = 0;
  1084. for (j = 0; j < AP_DEVICES; j++) {
  1085. if (!ap_test_config_card_id(j))
  1086. continue;
  1087. status = ap_test_queue(AP_MKQID(j, i),
  1088. ap_apft_available(),
  1089. NULL);
  1090. if (status.response_code != AP_RESPONSE_NORMAL)
  1091. continue;
  1092. count++;
  1093. }
  1094. if (count > max_count) {
  1095. max_count = count;
  1096. best_domain = i;
  1097. }
  1098. }
  1099. if (best_domain >= 0) {
  1100. ap_domain_index = best_domain;
  1101. AP_DBF(DBF_DEBUG, "new ap_domain_index=%d\n", ap_domain_index);
  1102. }
  1103. spin_unlock_bh(&ap_domain_lock);
  1104. }
  1105. /*
  1106. * This function checks the type and returns either 0 for not
  1107. * supported or the highest compatible type value (which may
  1108. * include the input type value).
  1109. */
  1110. static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
  1111. {
  1112. int comp_type = 0;
  1113. /* < CEX2A is not supported */
  1114. if (rawtype < AP_DEVICE_TYPE_CEX2A)
  1115. return 0;
  1116. /* up to CEX6 known and fully supported */
  1117. if (rawtype <= AP_DEVICE_TYPE_CEX6)
  1118. return rawtype;
  1119. /*
  1120. * unknown new type > CEX6, check for compatibility
  1121. * to the highest known and supported type which is
  1122. * currently CEX6 with the help of the QACT function.
  1123. */
  1124. if (ap_qact_available()) {
  1125. struct ap_queue_status status;
  1126. union ap_qact_ap_info apinfo = {0};
  1127. apinfo.mode = (func >> 26) & 0x07;
  1128. apinfo.cat = AP_DEVICE_TYPE_CEX6;
  1129. status = ap_qact(qid, 0, &apinfo);
  1130. if (status.response_code == AP_RESPONSE_NORMAL
  1131. && apinfo.cat >= AP_DEVICE_TYPE_CEX2A
  1132. && apinfo.cat <= AP_DEVICE_TYPE_CEX6)
  1133. comp_type = apinfo.cat;
  1134. }
  1135. if (!comp_type)
  1136. AP_DBF(DBF_WARN, "queue=%02x.%04x unable to map type %d\n",
  1137. AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
  1138. else if (comp_type != rawtype)
  1139. AP_DBF(DBF_INFO, "queue=%02x.%04x map type %d to %d\n",
  1140. AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype, comp_type);
  1141. return comp_type;
  1142. }
  1143. /*
  1144. * helper function to be used with bus_find_dev
  1145. * matches for the card device with the given id
  1146. */
  1147. static int __match_card_device_with_id(struct device *dev, void *data)
  1148. {
  1149. return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long) data;
  1150. }
  1151. /* helper function to be used with bus_find_dev
  1152. * matches for the queue device with a given qid
  1153. */
  1154. static int __match_queue_device_with_qid(struct device *dev, void *data)
  1155. {
  1156. return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
  1157. }
  1158. /**
  1159. * ap_scan_bus(): Scan the AP bus for new devices
  1160. * Runs periodically, workqueue timer (ap_config_time)
  1161. */
  1162. static void ap_scan_bus(struct work_struct *unused)
  1163. {
  1164. struct ap_queue *aq;
  1165. struct ap_card *ac;
  1166. struct device *dev;
  1167. ap_qid_t qid;
  1168. int comp_type, depth = 0, type = 0;
  1169. unsigned int func = 0;
  1170. int rc, id, dom, borked, domains, defdomdevs = 0;
  1171. AP_DBF(DBF_DEBUG, "%s running\n", __func__);
  1172. ap_query_configuration(ap_configuration);
  1173. ap_select_domain();
  1174. for (id = 0; id < AP_DEVICES; id++) {
  1175. /* check if device is registered */
  1176. dev = bus_find_device(&ap_bus_type, NULL,
  1177. (void *)(long) id,
  1178. __match_card_device_with_id);
  1179. ac = dev ? to_ap_card(dev) : NULL;
  1180. if (!ap_test_config_card_id(id)) {
  1181. if (dev) {
  1182. /* Card device has been removed from
  1183. * configuration, remove the belonging
  1184. * queue devices.
  1185. */
  1186. bus_for_each_dev(&ap_bus_type, NULL,
  1187. (void *)(long) id,
  1188. __ap_queue_devices_with_id_unregister);
  1189. /* now remove the card device */
  1190. device_unregister(dev);
  1191. put_device(dev);
  1192. }
  1193. continue;
  1194. }
  1195. /* According to the configuration there should be a card
  1196. * device, so check if there is at least one valid queue
  1197. * and maybe create queue devices and the card device.
  1198. */
  1199. domains = 0;
  1200. for (dom = 0; dom < AP_DOMAINS; dom++) {
  1201. qid = AP_MKQID(id, dom);
  1202. dev = bus_find_device(&ap_bus_type, NULL,
  1203. (void *)(long) qid,
  1204. __match_queue_device_with_qid);
  1205. aq = dev ? to_ap_queue(dev) : NULL;
  1206. if (!ap_test_config_domain(dom)) {
  1207. if (dev) {
  1208. /* Queue device exists but has been
  1209. * removed from configuration.
  1210. */
  1211. device_unregister(dev);
  1212. put_device(dev);
  1213. }
  1214. continue;
  1215. }
  1216. rc = ap_query_queue(qid, &depth, &type, &func);
  1217. if (dev) {
  1218. spin_lock_bh(&aq->lock);
  1219. if (rc == -ENODEV ||
  1220. /* adapter reconfiguration */
  1221. (ac && ac->functions != func))
  1222. aq->state = AP_STATE_BORKED;
  1223. borked = aq->state == AP_STATE_BORKED;
  1224. spin_unlock_bh(&aq->lock);
  1225. if (borked) /* Remove broken device */
  1226. device_unregister(dev);
  1227. put_device(dev);
  1228. if (!borked) {
  1229. domains++;
  1230. if (dom == ap_domain_index)
  1231. defdomdevs++;
  1232. continue;
  1233. }
  1234. }
  1235. if (rc)
  1236. continue;
  1237. /* a new queue device is needed, check out comp type */
  1238. comp_type = ap_get_compatible_type(qid, type, func);
  1239. if (!comp_type)
  1240. continue;
  1241. /* maybe a card device needs to be created first */
  1242. if (!ac) {
  1243. ac = ap_card_create(id, depth, type,
  1244. comp_type, func);
  1245. if (!ac)
  1246. continue;
  1247. ac->ap_dev.device.bus = &ap_bus_type;
  1248. ac->ap_dev.device.parent = ap_root_device;
  1249. dev_set_name(&ac->ap_dev.device,
  1250. "card%02x", id);
  1251. /* Register card with AP bus */
  1252. rc = device_register(&ac->ap_dev.device);
  1253. if (rc) {
  1254. put_device(&ac->ap_dev.device);
  1255. ac = NULL;
  1256. break;
  1257. }
  1258. /* get it and thus adjust reference counter */
  1259. get_device(&ac->ap_dev.device);
  1260. }
  1261. /* now create the new queue device */
  1262. aq = ap_queue_create(qid, comp_type);
  1263. if (!aq)
  1264. continue;
  1265. aq->card = ac;
  1266. aq->ap_dev.device.bus = &ap_bus_type;
  1267. aq->ap_dev.device.parent = &ac->ap_dev.device;
  1268. dev_set_name(&aq->ap_dev.device,
  1269. "%02x.%04x", id, dom);
  1270. /* Register device */
  1271. rc = device_register(&aq->ap_dev.device);
  1272. if (rc) {
  1273. put_device(&aq->ap_dev.device);
  1274. continue;
  1275. }
  1276. domains++;
  1277. if (dom == ap_domain_index)
  1278. defdomdevs++;
  1279. } /* end domain loop */
  1280. if (ac) {
  1281. /* remove card dev if there are no queue devices */
  1282. if (!domains)
  1283. device_unregister(&ac->ap_dev.device);
  1284. put_device(&ac->ap_dev.device);
  1285. }
  1286. } /* end device loop */
  1287. if (ap_domain_index >= 0 && defdomdevs < 1)
  1288. AP_DBF(DBF_INFO,
  1289. "no queue device with default domain %d available\n",
  1290. ap_domain_index);
  1291. mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
  1292. }
  1293. static void ap_config_timeout(struct timer_list *unused)
  1294. {
  1295. if (ap_suspend_flag)
  1296. return;
  1297. queue_work(system_long_wq, &ap_scan_work);
  1298. }
  1299. static int __init ap_debug_init(void)
  1300. {
  1301. ap_dbf_info = debug_register("ap", 1, 1,
  1302. DBF_MAX_SPRINTF_ARGS * sizeof(long));
  1303. debug_register_view(ap_dbf_info, &debug_sprintf_view);
  1304. debug_set_level(ap_dbf_info, DBF_ERR);
  1305. return 0;
  1306. }
  1307. static void __init ap_perms_init(void)
  1308. {
  1309. /* all resources useable if no kernel parameter string given */
  1310. memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
  1311. memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
  1312. /* apm kernel parameter string */
  1313. if (apm_str) {
  1314. memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
  1315. process_mask_arg(apm_str, ap_perms.apm, AP_DEVICES,
  1316. &ap_perms_mutex);
  1317. }
  1318. /* aqm kernel parameter string */
  1319. if (aqm_str) {
  1320. memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
  1321. process_mask_arg(aqm_str, ap_perms.aqm, AP_DOMAINS,
  1322. &ap_perms_mutex);
  1323. }
  1324. }
  1325. /**
  1326. * ap_module_init(): The module initialization code.
  1327. *
  1328. * Initializes the module.
  1329. */
  1330. static int __init ap_module_init(void)
  1331. {
  1332. int max_domain_id;
  1333. int rc, i;
  1334. rc = ap_debug_init();
  1335. if (rc)
  1336. return rc;
  1337. if (!ap_instructions_available()) {
  1338. pr_warn("The hardware system does not support AP instructions\n");
  1339. return -ENODEV;
  1340. }
  1341. /* set up the AP permissions (ap and aq masks) */
  1342. ap_perms_init();
  1343. /* Get AP configuration data if available */
  1344. ap_init_configuration();
  1345. if (ap_configuration)
  1346. max_domain_id =
  1347. ap_max_domain_id ? ap_max_domain_id : AP_DOMAINS - 1;
  1348. else
  1349. max_domain_id = 15;
  1350. if (ap_domain_index < -1 || ap_domain_index > max_domain_id ||
  1351. (ap_domain_index >= 0 &&
  1352. !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
  1353. pr_warn("%d is not a valid cryptographic domain\n",
  1354. ap_domain_index);
  1355. ap_domain_index = -1;
  1356. }
  1357. /* In resume callback we need to know if the user had set the domain.
  1358. * If so, we can not just reset it.
  1359. */
  1360. if (ap_domain_index >= 0)
  1361. user_set_domain = 1;
  1362. if (ap_interrupts_available()) {
  1363. rc = register_adapter_interrupt(&ap_airq);
  1364. ap_airq_flag = (rc == 0);
  1365. }
  1366. /* Create /sys/bus/ap. */
  1367. rc = bus_register(&ap_bus_type);
  1368. if (rc)
  1369. goto out;
  1370. for (i = 0; ap_bus_attrs[i]; i++) {
  1371. rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
  1372. if (rc)
  1373. goto out_bus;
  1374. }
  1375. /* Create /sys/devices/ap. */
  1376. ap_root_device = root_device_register("ap");
  1377. rc = PTR_ERR_OR_ZERO(ap_root_device);
  1378. if (rc)
  1379. goto out_bus;
  1380. /* Setup the AP bus rescan timer. */
  1381. timer_setup(&ap_config_timer, ap_config_timeout, 0);
  1382. /*
  1383. * Setup the high resultion poll timer.
  1384. * If we are running under z/VM adjust polling to z/VM polling rate.
  1385. */
  1386. if (MACHINE_IS_VM)
  1387. poll_timeout = 1500000;
  1388. spin_lock_init(&ap_poll_timer_lock);
  1389. hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  1390. ap_poll_timer.function = ap_poll_timeout;
  1391. /* Start the low priority AP bus poll thread. */
  1392. if (ap_thread_flag) {
  1393. rc = ap_poll_thread_start();
  1394. if (rc)
  1395. goto out_work;
  1396. }
  1397. rc = register_pm_notifier(&ap_power_notifier);
  1398. if (rc)
  1399. goto out_pm;
  1400. queue_work(system_long_wq, &ap_scan_work);
  1401. initialised = true;
  1402. return 0;
  1403. out_pm:
  1404. ap_poll_thread_stop();
  1405. out_work:
  1406. hrtimer_cancel(&ap_poll_timer);
  1407. root_device_unregister(ap_root_device);
  1408. out_bus:
  1409. while (i--)
  1410. bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
  1411. bus_unregister(&ap_bus_type);
  1412. out:
  1413. if (ap_using_interrupts())
  1414. unregister_adapter_interrupt(&ap_airq);
  1415. kfree(ap_configuration);
  1416. return rc;
  1417. }
  1418. device_initcall(ap_module_init);