ap_bus.c 40 KB

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