ap_bus.c 40 KB

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