ap_bus.c 31 KB

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