ap_bus.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  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_test_queue(): Test adjunct processor queue.
  150. * @qid: The AP queue number
  151. * @info: Pointer to queue descriptor
  152. *
  153. * Returns AP queue status structure.
  154. */
  155. static inline struct ap_queue_status
  156. ap_test_queue(ap_qid_t qid, unsigned long *info)
  157. {
  158. if (test_facility(15))
  159. qid |= 1UL << 23; /* set APFT T bit*/
  160. return ap_tapq(qid, info);
  161. }
  162. static inline int ap_query_configuration(void)
  163. {
  164. if (!ap_configuration)
  165. return -EOPNOTSUPP;
  166. return ap_qci(ap_configuration);
  167. }
  168. /**
  169. * ap_init_configuration(): Allocate and query configuration array.
  170. */
  171. static void ap_init_configuration(void)
  172. {
  173. if (!ap_configuration_available())
  174. return;
  175. ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL);
  176. if (!ap_configuration)
  177. return;
  178. if (ap_query_configuration() != 0) {
  179. kfree(ap_configuration);
  180. ap_configuration = NULL;
  181. return;
  182. }
  183. }
  184. /*
  185. * ap_test_config(): helper function to extract the nrth bit
  186. * within the unsigned int array field.
  187. */
  188. static inline int ap_test_config(unsigned int *field, unsigned int nr)
  189. {
  190. return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
  191. }
  192. /*
  193. * ap_test_config_card_id(): Test, whether an AP card ID is configured.
  194. * @id AP card ID
  195. *
  196. * Returns 0 if the card is not configured
  197. * 1 if the card is configured or
  198. * if the configuration information is not available
  199. */
  200. static inline int ap_test_config_card_id(unsigned int id)
  201. {
  202. if (!ap_configuration) /* QCI not supported */
  203. return 1;
  204. return ap_test_config(ap_configuration->apm, id);
  205. }
  206. /*
  207. * ap_test_config_domain(): Test, whether an AP usage domain is configured.
  208. * @domain AP usage domain ID
  209. *
  210. * Returns 0 if the usage domain is not configured
  211. * 1 if the usage domain is configured or
  212. * if the configuration information is not available
  213. */
  214. static inline int ap_test_config_domain(unsigned int domain)
  215. {
  216. if (!ap_configuration) /* QCI not supported */
  217. return domain < 16;
  218. return ap_test_config(ap_configuration->aqm, domain);
  219. }
  220. /**
  221. * ap_query_queue(): Check if an AP queue is available.
  222. * @qid: The AP queue number
  223. * @queue_depth: Pointer to queue depth value
  224. * @device_type: Pointer to device type value
  225. * @facilities: Pointer to facility indicator
  226. */
  227. static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
  228. unsigned int *facilities)
  229. {
  230. struct ap_queue_status status;
  231. unsigned long info;
  232. int nd;
  233. if (!ap_test_config_card_id(AP_QID_CARD(qid)))
  234. return -ENODEV;
  235. status = ap_test_queue(qid, &info);
  236. switch (status.response_code) {
  237. case AP_RESPONSE_NORMAL:
  238. *queue_depth = (int)(info & 0xff);
  239. *device_type = (int)((info >> 24) & 0xff);
  240. *facilities = (unsigned int)(info >> 32);
  241. /* Update maximum domain id */
  242. nd = (info >> 16) & 0xff;
  243. /* if N bit is available, z13 and newer */
  244. if ((info & (1UL << 57)) && nd > 0)
  245. ap_max_domain_id = nd;
  246. else /* older machine types */
  247. ap_max_domain_id = 15;
  248. switch (*device_type) {
  249. /* For CEX2 and CEX3 the available functions
  250. * are not refrected by the facilities bits.
  251. * Instead it is coded into the type. So here
  252. * modify the function bits based on the type.
  253. */
  254. case AP_DEVICE_TYPE_CEX2A:
  255. case AP_DEVICE_TYPE_CEX3A:
  256. *facilities |= 0x08000000;
  257. break;
  258. case AP_DEVICE_TYPE_CEX2C:
  259. case AP_DEVICE_TYPE_CEX3C:
  260. *facilities |= 0x10000000;
  261. break;
  262. default:
  263. break;
  264. }
  265. return 0;
  266. case AP_RESPONSE_Q_NOT_AVAIL:
  267. case AP_RESPONSE_DECONFIGURED:
  268. case AP_RESPONSE_CHECKSTOPPED:
  269. case AP_RESPONSE_INVALID_ADDRESS:
  270. return -ENODEV;
  271. case AP_RESPONSE_RESET_IN_PROGRESS:
  272. case AP_RESPONSE_OTHERWISE_CHANGED:
  273. case AP_RESPONSE_BUSY:
  274. return -EBUSY;
  275. default:
  276. BUG();
  277. }
  278. }
  279. void ap_wait(enum ap_wait wait)
  280. {
  281. ktime_t hr_time;
  282. switch (wait) {
  283. case AP_WAIT_AGAIN:
  284. case AP_WAIT_INTERRUPT:
  285. if (ap_using_interrupts())
  286. break;
  287. if (ap_poll_kthread) {
  288. wake_up(&ap_poll_wait);
  289. break;
  290. }
  291. /* Fall through */
  292. case AP_WAIT_TIMEOUT:
  293. spin_lock_bh(&ap_poll_timer_lock);
  294. if (!hrtimer_is_queued(&ap_poll_timer)) {
  295. hr_time = poll_timeout;
  296. hrtimer_forward_now(&ap_poll_timer, hr_time);
  297. hrtimer_restart(&ap_poll_timer);
  298. }
  299. spin_unlock_bh(&ap_poll_timer_lock);
  300. break;
  301. case AP_WAIT_NONE:
  302. default:
  303. break;
  304. }
  305. }
  306. /**
  307. * ap_request_timeout(): Handling of request timeouts
  308. * @data: Holds the AP device.
  309. *
  310. * Handles request timeouts.
  311. */
  312. void ap_request_timeout(unsigned long data)
  313. {
  314. struct ap_queue *aq = (struct ap_queue *) data;
  315. if (ap_suspend_flag)
  316. return;
  317. spin_lock_bh(&aq->lock);
  318. ap_wait(ap_sm_event(aq, AP_EVENT_TIMEOUT));
  319. spin_unlock_bh(&aq->lock);
  320. }
  321. /**
  322. * ap_poll_timeout(): AP receive polling for finished AP requests.
  323. * @unused: Unused pointer.
  324. *
  325. * Schedules the AP tasklet using a high resolution timer.
  326. */
  327. static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
  328. {
  329. if (!ap_suspend_flag)
  330. tasklet_schedule(&ap_tasklet);
  331. return HRTIMER_NORESTART;
  332. }
  333. /**
  334. * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
  335. * @airq: pointer to adapter interrupt descriptor
  336. */
  337. static void ap_interrupt_handler(struct airq_struct *airq)
  338. {
  339. inc_irq_stat(IRQIO_APB);
  340. if (!ap_suspend_flag)
  341. tasklet_schedule(&ap_tasklet);
  342. }
  343. /**
  344. * ap_tasklet_fn(): Tasklet to poll all AP devices.
  345. * @dummy: Unused variable
  346. *
  347. * Poll all AP devices on the bus.
  348. */
  349. static void ap_tasklet_fn(unsigned long dummy)
  350. {
  351. struct ap_card *ac;
  352. struct ap_queue *aq;
  353. enum ap_wait wait = AP_WAIT_NONE;
  354. /* Reset the indicator if interrupts are used. Thus new interrupts can
  355. * be received. Doing it in the beginning of the tasklet is therefor
  356. * important that no requests on any AP get lost.
  357. */
  358. if (ap_using_interrupts())
  359. xchg(ap_airq.lsi_ptr, 0);
  360. spin_lock_bh(&ap_list_lock);
  361. for_each_ap_card(ac) {
  362. for_each_ap_queue(aq, ac) {
  363. spin_lock_bh(&aq->lock);
  364. wait = min(wait, ap_sm_event_loop(aq, AP_EVENT_POLL));
  365. spin_unlock_bh(&aq->lock);
  366. }
  367. }
  368. spin_unlock_bh(&ap_list_lock);
  369. ap_wait(wait);
  370. }
  371. static int ap_pending_requests(void)
  372. {
  373. struct ap_card *ac;
  374. struct ap_queue *aq;
  375. spin_lock_bh(&ap_list_lock);
  376. for_each_ap_card(ac) {
  377. for_each_ap_queue(aq, ac) {
  378. if (aq->queue_count == 0)
  379. continue;
  380. spin_unlock_bh(&ap_list_lock);
  381. return 1;
  382. }
  383. }
  384. spin_unlock_bh(&ap_list_lock);
  385. return 0;
  386. }
  387. /**
  388. * ap_poll_thread(): Thread that polls for finished requests.
  389. * @data: Unused pointer
  390. *
  391. * AP bus poll thread. The purpose of this thread is to poll for
  392. * finished requests in a loop if there is a "free" cpu - that is
  393. * a cpu that doesn't have anything better to do. The polling stops
  394. * as soon as there is another task or if all messages have been
  395. * delivered.
  396. */
  397. static int ap_poll_thread(void *data)
  398. {
  399. DECLARE_WAITQUEUE(wait, current);
  400. set_user_nice(current, MAX_NICE);
  401. set_freezable();
  402. while (!kthread_should_stop()) {
  403. add_wait_queue(&ap_poll_wait, &wait);
  404. set_current_state(TASK_INTERRUPTIBLE);
  405. if (ap_suspend_flag || !ap_pending_requests()) {
  406. schedule();
  407. try_to_freeze();
  408. }
  409. set_current_state(TASK_RUNNING);
  410. remove_wait_queue(&ap_poll_wait, &wait);
  411. if (need_resched()) {
  412. schedule();
  413. try_to_freeze();
  414. continue;
  415. }
  416. ap_tasklet_fn(0);
  417. }
  418. return 0;
  419. }
  420. static int ap_poll_thread_start(void)
  421. {
  422. int rc;
  423. if (ap_using_interrupts() || ap_poll_kthread)
  424. return 0;
  425. mutex_lock(&ap_poll_thread_mutex);
  426. ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
  427. rc = PTR_RET(ap_poll_kthread);
  428. if (rc)
  429. ap_poll_kthread = NULL;
  430. mutex_unlock(&ap_poll_thread_mutex);
  431. return rc;
  432. }
  433. static void ap_poll_thread_stop(void)
  434. {
  435. if (!ap_poll_kthread)
  436. return;
  437. mutex_lock(&ap_poll_thread_mutex);
  438. kthread_stop(ap_poll_kthread);
  439. ap_poll_kthread = NULL;
  440. mutex_unlock(&ap_poll_thread_mutex);
  441. }
  442. #define is_card_dev(x) ((x)->parent == ap_root_device)
  443. #define is_queue_dev(x) ((x)->parent != ap_root_device)
  444. /**
  445. * ap_bus_match()
  446. * @dev: Pointer to device
  447. * @drv: Pointer to device_driver
  448. *
  449. * AP bus driver registration/unregistration.
  450. */
  451. static int ap_bus_match(struct device *dev, struct device_driver *drv)
  452. {
  453. struct ap_driver *ap_drv = to_ap_drv(drv);
  454. struct ap_device_id *id;
  455. /*
  456. * Compare device type of the device with the list of
  457. * supported types of the device_driver.
  458. */
  459. for (id = ap_drv->ids; id->match_flags; id++) {
  460. if (is_card_dev(dev) &&
  461. id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
  462. id->dev_type == to_ap_dev(dev)->device_type)
  463. return 1;
  464. if (is_queue_dev(dev) &&
  465. id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
  466. id->dev_type == to_ap_dev(dev)->device_type)
  467. return 1;
  468. }
  469. return 0;
  470. }
  471. /**
  472. * ap_uevent(): Uevent function for AP devices.
  473. * @dev: Pointer to device
  474. * @env: Pointer to kobj_uevent_env
  475. *
  476. * It sets up a single environment variable DEV_TYPE which contains the
  477. * hardware device type.
  478. */
  479. static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
  480. {
  481. struct ap_device *ap_dev = to_ap_dev(dev);
  482. int retval = 0;
  483. if (!ap_dev)
  484. return -ENODEV;
  485. /* Set up DEV_TYPE environment variable. */
  486. retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
  487. if (retval)
  488. return retval;
  489. /* Add MODALIAS= */
  490. retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
  491. return retval;
  492. }
  493. static int ap_dev_suspend(struct device *dev)
  494. {
  495. struct ap_device *ap_dev = to_ap_dev(dev);
  496. if (ap_dev->drv && ap_dev->drv->suspend)
  497. ap_dev->drv->suspend(ap_dev);
  498. return 0;
  499. }
  500. static int ap_dev_resume(struct device *dev)
  501. {
  502. struct ap_device *ap_dev = to_ap_dev(dev);
  503. if (ap_dev->drv && ap_dev->drv->resume)
  504. ap_dev->drv->resume(ap_dev);
  505. return 0;
  506. }
  507. static void ap_bus_suspend(void)
  508. {
  509. AP_DBF(DBF_DEBUG, "ap_bus_suspend running\n");
  510. ap_suspend_flag = 1;
  511. /*
  512. * Disable scanning for devices, thus we do not want to scan
  513. * for them after removing.
  514. */
  515. flush_work(&ap_scan_work);
  516. tasklet_disable(&ap_tasklet);
  517. }
  518. static int __ap_card_devices_unregister(struct device *dev, void *dummy)
  519. {
  520. if (is_card_dev(dev))
  521. device_unregister(dev);
  522. return 0;
  523. }
  524. static int __ap_queue_devices_unregister(struct device *dev, void *dummy)
  525. {
  526. if (is_queue_dev(dev))
  527. device_unregister(dev);
  528. return 0;
  529. }
  530. static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
  531. {
  532. if (is_queue_dev(dev) &&
  533. AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
  534. device_unregister(dev);
  535. return 0;
  536. }
  537. static void ap_bus_resume(void)
  538. {
  539. int rc;
  540. AP_DBF(DBF_DEBUG, "ap_bus_resume running\n");
  541. /* remove all queue devices */
  542. bus_for_each_dev(&ap_bus_type, NULL, NULL,
  543. __ap_queue_devices_unregister);
  544. /* remove all card devices */
  545. bus_for_each_dev(&ap_bus_type, NULL, NULL,
  546. __ap_card_devices_unregister);
  547. /* Reset thin interrupt setting */
  548. if (ap_interrupts_available() && !ap_using_interrupts()) {
  549. rc = register_adapter_interrupt(&ap_airq);
  550. ap_airq_flag = (rc == 0);
  551. }
  552. if (!ap_interrupts_available() && ap_using_interrupts()) {
  553. unregister_adapter_interrupt(&ap_airq);
  554. ap_airq_flag = 0;
  555. }
  556. /* Reset domain */
  557. if (!user_set_domain)
  558. ap_domain_index = -1;
  559. /* Get things going again */
  560. ap_suspend_flag = 0;
  561. if (ap_airq_flag)
  562. xchg(ap_airq.lsi_ptr, 0);
  563. tasklet_enable(&ap_tasklet);
  564. queue_work(system_long_wq, &ap_scan_work);
  565. }
  566. static int ap_power_event(struct notifier_block *this, unsigned long event,
  567. void *ptr)
  568. {
  569. switch (event) {
  570. case PM_HIBERNATION_PREPARE:
  571. case PM_SUSPEND_PREPARE:
  572. ap_bus_suspend();
  573. break;
  574. case PM_POST_HIBERNATION:
  575. case PM_POST_SUSPEND:
  576. ap_bus_resume();
  577. break;
  578. default:
  579. break;
  580. }
  581. return NOTIFY_DONE;
  582. }
  583. static struct notifier_block ap_power_notifier = {
  584. .notifier_call = ap_power_event,
  585. };
  586. static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops, ap_dev_suspend, ap_dev_resume);
  587. static struct bus_type ap_bus_type = {
  588. .name = "ap",
  589. .match = &ap_bus_match,
  590. .uevent = &ap_uevent,
  591. .pm = &ap_bus_pm_ops,
  592. };
  593. static int ap_device_probe(struct device *dev)
  594. {
  595. struct ap_device *ap_dev = to_ap_dev(dev);
  596. struct ap_driver *ap_drv = to_ap_drv(dev->driver);
  597. int rc;
  598. ap_dev->drv = ap_drv;
  599. rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
  600. if (rc)
  601. ap_dev->drv = NULL;
  602. return rc;
  603. }
  604. static int ap_device_remove(struct device *dev)
  605. {
  606. struct ap_device *ap_dev = to_ap_dev(dev);
  607. struct ap_driver *ap_drv = ap_dev->drv;
  608. spin_lock_bh(&ap_list_lock);
  609. if (is_card_dev(dev))
  610. list_del_init(&to_ap_card(dev)->list);
  611. else
  612. list_del_init(&to_ap_queue(dev)->list);
  613. spin_unlock_bh(&ap_list_lock);
  614. if (ap_drv->remove)
  615. ap_drv->remove(ap_dev);
  616. return 0;
  617. }
  618. int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
  619. char *name)
  620. {
  621. struct device_driver *drv = &ap_drv->driver;
  622. if (!initialised)
  623. return -ENODEV;
  624. drv->bus = &ap_bus_type;
  625. drv->probe = ap_device_probe;
  626. drv->remove = ap_device_remove;
  627. drv->owner = owner;
  628. drv->name = name;
  629. return driver_register(drv);
  630. }
  631. EXPORT_SYMBOL(ap_driver_register);
  632. void ap_driver_unregister(struct ap_driver *ap_drv)
  633. {
  634. driver_unregister(&ap_drv->driver);
  635. }
  636. EXPORT_SYMBOL(ap_driver_unregister);
  637. void ap_bus_force_rescan(void)
  638. {
  639. if (ap_suspend_flag)
  640. return;
  641. /* processing a asynchronous bus rescan */
  642. del_timer(&ap_config_timer);
  643. queue_work(system_long_wq, &ap_scan_work);
  644. flush_work(&ap_scan_work);
  645. }
  646. EXPORT_SYMBOL(ap_bus_force_rescan);
  647. /*
  648. * AP bus attributes.
  649. */
  650. static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
  651. {
  652. return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
  653. }
  654. static ssize_t ap_domain_store(struct bus_type *bus,
  655. const char *buf, size_t count)
  656. {
  657. int domain;
  658. if (sscanf(buf, "%i\n", &domain) != 1 ||
  659. domain < 0 || domain > ap_max_domain_id)
  660. return -EINVAL;
  661. spin_lock_bh(&ap_domain_lock);
  662. ap_domain_index = domain;
  663. spin_unlock_bh(&ap_domain_lock);
  664. AP_DBF(DBF_DEBUG, "store new default domain=%d\n", domain);
  665. return count;
  666. }
  667. static BUS_ATTR(ap_domain, 0644, ap_domain_show, ap_domain_store);
  668. static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
  669. {
  670. if (!ap_configuration) /* QCI not supported */
  671. return snprintf(buf, PAGE_SIZE, "not supported\n");
  672. return snprintf(buf, PAGE_SIZE,
  673. "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
  674. ap_configuration->adm[0], ap_configuration->adm[1],
  675. ap_configuration->adm[2], ap_configuration->adm[3],
  676. ap_configuration->adm[4], ap_configuration->adm[5],
  677. ap_configuration->adm[6], ap_configuration->adm[7]);
  678. }
  679. static BUS_ATTR(ap_control_domain_mask, 0444,
  680. ap_control_domain_mask_show, NULL);
  681. static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
  682. {
  683. if (!ap_configuration) /* QCI not supported */
  684. return snprintf(buf, PAGE_SIZE, "not supported\n");
  685. return snprintf(buf, PAGE_SIZE,
  686. "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
  687. ap_configuration->aqm[0], ap_configuration->aqm[1],
  688. ap_configuration->aqm[2], ap_configuration->aqm[3],
  689. ap_configuration->aqm[4], ap_configuration->aqm[5],
  690. ap_configuration->aqm[6], ap_configuration->aqm[7]);
  691. }
  692. static BUS_ATTR(ap_usage_domain_mask, 0444,
  693. ap_usage_domain_mask_show, NULL);
  694. static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
  695. {
  696. return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
  697. }
  698. static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
  699. {
  700. return snprintf(buf, PAGE_SIZE, "%d\n",
  701. ap_using_interrupts() ? 1 : 0);
  702. }
  703. static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
  704. static ssize_t ap_config_time_store(struct bus_type *bus,
  705. const char *buf, size_t count)
  706. {
  707. int time;
  708. if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
  709. return -EINVAL;
  710. ap_config_time = time;
  711. mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
  712. return count;
  713. }
  714. static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
  715. static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
  716. {
  717. return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
  718. }
  719. static ssize_t ap_poll_thread_store(struct bus_type *bus,
  720. const char *buf, size_t count)
  721. {
  722. int flag, rc;
  723. if (sscanf(buf, "%d\n", &flag) != 1)
  724. return -EINVAL;
  725. if (flag) {
  726. rc = ap_poll_thread_start();
  727. if (rc)
  728. count = rc;
  729. } else
  730. ap_poll_thread_stop();
  731. return count;
  732. }
  733. static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
  734. static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
  735. {
  736. return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
  737. }
  738. static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
  739. size_t count)
  740. {
  741. unsigned long long time;
  742. ktime_t hr_time;
  743. /* 120 seconds = maximum poll interval */
  744. if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
  745. time > 120000000000ULL)
  746. return -EINVAL;
  747. poll_timeout = time;
  748. hr_time = poll_timeout;
  749. spin_lock_bh(&ap_poll_timer_lock);
  750. hrtimer_cancel(&ap_poll_timer);
  751. hrtimer_set_expires(&ap_poll_timer, hr_time);
  752. hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
  753. spin_unlock_bh(&ap_poll_timer_lock);
  754. return count;
  755. }
  756. static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
  757. static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
  758. {
  759. int max_domain_id;
  760. if (ap_configuration)
  761. max_domain_id = ap_max_domain_id ? : -1;
  762. else
  763. max_domain_id = 15;
  764. return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
  765. }
  766. static BUS_ATTR(ap_max_domain_id, 0444, ap_max_domain_id_show, NULL);
  767. static struct bus_attribute *const ap_bus_attrs[] = {
  768. &bus_attr_ap_domain,
  769. &bus_attr_ap_control_domain_mask,
  770. &bus_attr_ap_usage_domain_mask,
  771. &bus_attr_config_time,
  772. &bus_attr_poll_thread,
  773. &bus_attr_ap_interrupts,
  774. &bus_attr_poll_timeout,
  775. &bus_attr_ap_max_domain_id,
  776. NULL,
  777. };
  778. /**
  779. * ap_select_domain(): Select an AP domain.
  780. *
  781. * Pick one of the 16 AP domains.
  782. */
  783. static int ap_select_domain(void)
  784. {
  785. int count, max_count, best_domain;
  786. struct ap_queue_status status;
  787. int i, j;
  788. /*
  789. * We want to use a single domain. Either the one specified with
  790. * the "domain=" parameter or the domain with the maximum number
  791. * of devices.
  792. */
  793. spin_lock_bh(&ap_domain_lock);
  794. if (ap_domain_index >= 0) {
  795. /* Domain has already been selected. */
  796. spin_unlock_bh(&ap_domain_lock);
  797. return 0;
  798. }
  799. best_domain = -1;
  800. max_count = 0;
  801. for (i = 0; i < AP_DOMAINS; i++) {
  802. if (!ap_test_config_domain(i))
  803. continue;
  804. count = 0;
  805. for (j = 0; j < AP_DEVICES; j++) {
  806. if (!ap_test_config_card_id(j))
  807. continue;
  808. status = ap_test_queue(AP_MKQID(j, i), NULL);
  809. if (status.response_code != AP_RESPONSE_NORMAL)
  810. continue;
  811. count++;
  812. }
  813. if (count > max_count) {
  814. max_count = count;
  815. best_domain = i;
  816. }
  817. }
  818. if (best_domain >= 0){
  819. ap_domain_index = best_domain;
  820. spin_unlock_bh(&ap_domain_lock);
  821. return 0;
  822. }
  823. spin_unlock_bh(&ap_domain_lock);
  824. return -ENODEV;
  825. }
  826. /*
  827. * helper function to be used with bus_find_dev
  828. * matches for the card device with the given id
  829. */
  830. static int __match_card_device_with_id(struct device *dev, void *data)
  831. {
  832. return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long) data;
  833. }
  834. /* helper function to be used with bus_find_dev
  835. * matches for the queue device with a given qid
  836. */
  837. static int __match_queue_device_with_qid(struct device *dev, void *data)
  838. {
  839. return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
  840. }
  841. /**
  842. * ap_scan_bus(): Scan the AP bus for new devices
  843. * Runs periodically, workqueue timer (ap_config_time)
  844. */
  845. static void ap_scan_bus(struct work_struct *unused)
  846. {
  847. struct ap_queue *aq;
  848. struct ap_card *ac;
  849. struct device *dev;
  850. ap_qid_t qid;
  851. int depth = 0, type = 0;
  852. unsigned int functions = 0;
  853. int rc, id, dom, borked, domains;
  854. AP_DBF(DBF_DEBUG, "ap_scan_bus running\n");
  855. ap_query_configuration();
  856. if (ap_select_domain() != 0)
  857. goto out;
  858. for (id = 0; id < AP_DEVICES; id++) {
  859. /* check if device is registered */
  860. dev = bus_find_device(&ap_bus_type, NULL,
  861. (void *)(long) id,
  862. __match_card_device_with_id);
  863. ac = dev ? to_ap_card(dev) : NULL;
  864. if (!ap_test_config_card_id(id)) {
  865. if (dev) {
  866. /* Card device has been removed from
  867. * configuration, remove the belonging
  868. * queue devices.
  869. */
  870. bus_for_each_dev(&ap_bus_type, NULL,
  871. (void *)(long) id,
  872. __ap_queue_devices_with_id_unregister);
  873. /* now remove the card device */
  874. device_unregister(dev);
  875. put_device(dev);
  876. }
  877. continue;
  878. }
  879. /* According to the configuration there should be a card
  880. * device, so check if there is at least one valid queue
  881. * and maybe create queue devices and the card device.
  882. */
  883. domains = 0;
  884. for (dom = 0; dom < AP_DOMAINS; dom++) {
  885. qid = AP_MKQID(id, dom);
  886. dev = bus_find_device(&ap_bus_type, NULL,
  887. (void *)(long) qid,
  888. __match_queue_device_with_qid);
  889. aq = dev ? to_ap_queue(dev) : NULL;
  890. if (!ap_test_config_domain(dom)) {
  891. if (dev) {
  892. /* Queue device exists but has been
  893. * removed from configuration.
  894. */
  895. device_unregister(dev);
  896. put_device(dev);
  897. }
  898. continue;
  899. }
  900. rc = ap_query_queue(qid, &depth, &type, &functions);
  901. if (dev) {
  902. spin_lock_bh(&aq->lock);
  903. if (rc == -ENODEV ||
  904. /* adapter reconfiguration */
  905. (ac && ac->functions != functions))
  906. aq->state = AP_STATE_BORKED;
  907. borked = aq->state == AP_STATE_BORKED;
  908. spin_unlock_bh(&aq->lock);
  909. if (borked) /* Remove broken device */
  910. device_unregister(dev);
  911. put_device(dev);
  912. if (!borked) {
  913. domains++;
  914. continue;
  915. }
  916. }
  917. if (rc)
  918. continue;
  919. /* new queue device needed */
  920. if (!ac) {
  921. /* but first create the card device */
  922. ac = ap_card_create(id, depth,
  923. type, functions);
  924. if (!ac)
  925. continue;
  926. ac->ap_dev.device.bus = &ap_bus_type;
  927. ac->ap_dev.device.parent = ap_root_device;
  928. dev_set_name(&ac->ap_dev.device,
  929. "card%02x", id);
  930. /* Register card with AP bus */
  931. rc = device_register(&ac->ap_dev.device);
  932. if (rc) {
  933. put_device(&ac->ap_dev.device);
  934. ac = NULL;
  935. break;
  936. }
  937. /* get it and thus adjust reference counter */
  938. get_device(&ac->ap_dev.device);
  939. /* Add card device to card list */
  940. spin_lock_bh(&ap_list_lock);
  941. list_add(&ac->list, &ap_card_list);
  942. spin_unlock_bh(&ap_list_lock);
  943. }
  944. /* now create the new queue device */
  945. aq = ap_queue_create(qid, type);
  946. if (!aq)
  947. continue;
  948. aq->card = ac;
  949. aq->ap_dev.device.bus = &ap_bus_type;
  950. aq->ap_dev.device.parent = &ac->ap_dev.device;
  951. dev_set_name(&aq->ap_dev.device,
  952. "%02x.%04x", id, dom);
  953. /* Add queue device to card queue list */
  954. spin_lock_bh(&ap_list_lock);
  955. list_add(&aq->list, &ac->queues);
  956. spin_unlock_bh(&ap_list_lock);
  957. /* Start with a device reset */
  958. spin_lock_bh(&aq->lock);
  959. ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
  960. spin_unlock_bh(&aq->lock);
  961. /* Register device */
  962. rc = device_register(&aq->ap_dev.device);
  963. if (rc) {
  964. spin_lock_bh(&ap_list_lock);
  965. list_del_init(&aq->list);
  966. spin_unlock_bh(&ap_list_lock);
  967. put_device(&aq->ap_dev.device);
  968. continue;
  969. }
  970. domains++;
  971. } /* end domain loop */
  972. if (ac) {
  973. /* remove card dev if there are no queue devices */
  974. if (!domains)
  975. device_unregister(&ac->ap_dev.device);
  976. put_device(&ac->ap_dev.device);
  977. }
  978. } /* end device loop */
  979. out:
  980. mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
  981. }
  982. static void ap_config_timeout(unsigned long ptr)
  983. {
  984. if (ap_suspend_flag)
  985. return;
  986. queue_work(system_long_wq, &ap_scan_work);
  987. }
  988. static void ap_reset_all(void)
  989. {
  990. int i, j;
  991. for (i = 0; i < AP_DOMAINS; i++) {
  992. if (!ap_test_config_domain(i))
  993. continue;
  994. for (j = 0; j < AP_DEVICES; j++) {
  995. if (!ap_test_config_card_id(j))
  996. continue;
  997. ap_rapq(AP_MKQID(j, i));
  998. }
  999. }
  1000. }
  1001. static struct reset_call ap_reset_call = {
  1002. .fn = ap_reset_all,
  1003. };
  1004. int __init ap_debug_init(void)
  1005. {
  1006. ap_dbf_info = debug_register("ap", 1, 1,
  1007. DBF_MAX_SPRINTF_ARGS * sizeof(long));
  1008. debug_register_view(ap_dbf_info, &debug_sprintf_view);
  1009. debug_set_level(ap_dbf_info, DBF_ERR);
  1010. return 0;
  1011. }
  1012. void ap_debug_exit(void)
  1013. {
  1014. debug_unregister(ap_dbf_info);
  1015. }
  1016. /**
  1017. * ap_module_init(): The module initialization code.
  1018. *
  1019. * Initializes the module.
  1020. */
  1021. int __init ap_module_init(void)
  1022. {
  1023. int max_domain_id;
  1024. int rc, i;
  1025. rc = ap_debug_init();
  1026. if (rc)
  1027. return rc;
  1028. if (ap_instructions_available() != 0) {
  1029. pr_warn("The hardware system does not support AP instructions\n");
  1030. return -ENODEV;
  1031. }
  1032. /* Get AP configuration data if available */
  1033. ap_init_configuration();
  1034. if (ap_configuration)
  1035. max_domain_id = ap_max_domain_id ? : (AP_DOMAINS - 1);
  1036. else
  1037. max_domain_id = 15;
  1038. if (ap_domain_index < -1 || ap_domain_index > max_domain_id) {
  1039. pr_warn("%d is not a valid cryptographic domain\n",
  1040. ap_domain_index);
  1041. rc = -EINVAL;
  1042. goto out_free;
  1043. }
  1044. /* In resume callback we need to know if the user had set the domain.
  1045. * If so, we can not just reset it.
  1046. */
  1047. if (ap_domain_index >= 0)
  1048. user_set_domain = 1;
  1049. if (ap_interrupts_available()) {
  1050. rc = register_adapter_interrupt(&ap_airq);
  1051. ap_airq_flag = (rc == 0);
  1052. }
  1053. register_reset_call(&ap_reset_call);
  1054. /* Create /sys/bus/ap. */
  1055. rc = bus_register(&ap_bus_type);
  1056. if (rc)
  1057. goto out;
  1058. for (i = 0; ap_bus_attrs[i]; i++) {
  1059. rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
  1060. if (rc)
  1061. goto out_bus;
  1062. }
  1063. /* Create /sys/devices/ap. */
  1064. ap_root_device = root_device_register("ap");
  1065. rc = PTR_RET(ap_root_device);
  1066. if (rc)
  1067. goto out_bus;
  1068. /* Setup the AP bus rescan timer. */
  1069. setup_timer(&ap_config_timer, ap_config_timeout, 0);
  1070. /*
  1071. * Setup the high resultion poll timer.
  1072. * If we are running under z/VM adjust polling to z/VM polling rate.
  1073. */
  1074. if (MACHINE_IS_VM)
  1075. poll_timeout = 1500000;
  1076. spin_lock_init(&ap_poll_timer_lock);
  1077. hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  1078. ap_poll_timer.function = ap_poll_timeout;
  1079. /* Start the low priority AP bus poll thread. */
  1080. if (ap_thread_flag) {
  1081. rc = ap_poll_thread_start();
  1082. if (rc)
  1083. goto out_work;
  1084. }
  1085. rc = register_pm_notifier(&ap_power_notifier);
  1086. if (rc)
  1087. goto out_pm;
  1088. queue_work(system_long_wq, &ap_scan_work);
  1089. initialised = true;
  1090. return 0;
  1091. out_pm:
  1092. ap_poll_thread_stop();
  1093. out_work:
  1094. hrtimer_cancel(&ap_poll_timer);
  1095. root_device_unregister(ap_root_device);
  1096. out_bus:
  1097. while (i--)
  1098. bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
  1099. bus_unregister(&ap_bus_type);
  1100. out:
  1101. unregister_reset_call(&ap_reset_call);
  1102. if (ap_using_interrupts())
  1103. unregister_adapter_interrupt(&ap_airq);
  1104. out_free:
  1105. kfree(ap_configuration);
  1106. return rc;
  1107. }
  1108. device_initcall(ap_module_init);