ap_bus.c 33 KB

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