ap_bus.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608
  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. }
  687. /* Add queue/card to list of active queues/cards */
  688. spin_lock_bh(&ap_list_lock);
  689. if (is_card_dev(dev))
  690. list_add(&to_ap_card(dev)->list, &ap_card_list);
  691. else
  692. list_add(&to_ap_queue(dev)->list,
  693. &to_ap_queue(dev)->card->queues);
  694. spin_unlock_bh(&ap_list_lock);
  695. ap_dev->drv = ap_drv;
  696. rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
  697. if (rc) {
  698. spin_lock_bh(&ap_list_lock);
  699. if (is_card_dev(dev))
  700. list_del_init(&to_ap_card(dev)->list);
  701. else
  702. list_del_init(&to_ap_queue(dev)->list);
  703. spin_unlock_bh(&ap_list_lock);
  704. ap_dev->drv = NULL;
  705. }
  706. return rc;
  707. }
  708. static int ap_device_remove(struct device *dev)
  709. {
  710. struct ap_device *ap_dev = to_ap_dev(dev);
  711. struct ap_driver *ap_drv = ap_dev->drv;
  712. if (ap_drv->remove)
  713. ap_drv->remove(ap_dev);
  714. /* Remove queue/card from list of active queues/cards */
  715. spin_lock_bh(&ap_list_lock);
  716. if (is_card_dev(dev))
  717. list_del_init(&to_ap_card(dev)->list);
  718. else
  719. list_del_init(&to_ap_queue(dev)->list);
  720. spin_unlock_bh(&ap_list_lock);
  721. return 0;
  722. }
  723. int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
  724. char *name)
  725. {
  726. struct device_driver *drv = &ap_drv->driver;
  727. if (!initialised)
  728. return -ENODEV;
  729. drv->bus = &ap_bus_type;
  730. drv->probe = ap_device_probe;
  731. drv->remove = ap_device_remove;
  732. drv->owner = owner;
  733. drv->name = name;
  734. return driver_register(drv);
  735. }
  736. EXPORT_SYMBOL(ap_driver_register);
  737. void ap_driver_unregister(struct ap_driver *ap_drv)
  738. {
  739. driver_unregister(&ap_drv->driver);
  740. }
  741. EXPORT_SYMBOL(ap_driver_unregister);
  742. void ap_bus_force_rescan(void)
  743. {
  744. if (ap_suspend_flag)
  745. return;
  746. /* processing a asynchronous bus rescan */
  747. del_timer(&ap_config_timer);
  748. queue_work(system_long_wq, &ap_scan_work);
  749. flush_work(&ap_scan_work);
  750. }
  751. EXPORT_SYMBOL(ap_bus_force_rescan);
  752. /*
  753. * hex2bitmap() - parse hex mask string and set bitmap.
  754. * Valid strings are "0x012345678" with at least one valid hex number.
  755. * Rest of the bitmap to the right is padded with 0. No spaces allowed
  756. * within the string, the leading 0x may be omitted.
  757. * Returns the bitmask with exactly the bits set as given by the hex
  758. * string (both in big endian order).
  759. */
  760. static int hex2bitmap(const char *str, unsigned long *bitmap, int bits)
  761. {
  762. int i, n, b;
  763. /* bits needs to be a multiple of 8 */
  764. if (bits & 0x07)
  765. return -EINVAL;
  766. if (str[0] == '0' && str[1] == 'x')
  767. str++;
  768. if (*str == 'x')
  769. str++;
  770. for (i = 0; isxdigit(*str) && i < bits; str++) {
  771. b = hex_to_bin(*str);
  772. for (n = 0; n < 4; n++)
  773. if (b & (0x08 >> n))
  774. set_bit_inv(i + n, bitmap);
  775. i += 4;
  776. }
  777. if (*str == '\n')
  778. str++;
  779. if (*str)
  780. return -EINVAL;
  781. return 0;
  782. }
  783. /*
  784. * modify_bitmap() - parse bitmask argument and modify an existing
  785. * bit mask accordingly. A concatenation (done with ',') of these
  786. * terms is recognized:
  787. * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
  788. * <bitnr> may be any valid number (hex, decimal or octal) in the range
  789. * 0...bits-1; the leading + or - is required. Here are some examples:
  790. * +0-15,+32,-128,-0xFF
  791. * -0-255,+1-16,+0x128
  792. * +1,+2,+3,+4,-5,-7-10
  793. * Returns the new bitmap after all changes have been applied. Every
  794. * positive value in the string will set a bit and every negative value
  795. * in the string will clear a bit. As a bit may be touched more than once,
  796. * the last 'operation' wins:
  797. * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
  798. * cleared again. All other bits are unmodified.
  799. */
  800. static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
  801. {
  802. int a, i, z;
  803. char *np, sign;
  804. /* bits needs to be a multiple of 8 */
  805. if (bits & 0x07)
  806. return -EINVAL;
  807. while (*str) {
  808. sign = *str++;
  809. if (sign != '+' && sign != '-')
  810. return -EINVAL;
  811. a = z = simple_strtoul(str, &np, 0);
  812. if (str == np || a >= bits)
  813. return -EINVAL;
  814. str = np;
  815. if (*str == '-') {
  816. z = simple_strtoul(++str, &np, 0);
  817. if (str == np || a > z || z >= bits)
  818. return -EINVAL;
  819. str = np;
  820. }
  821. for (i = a; i <= z; i++)
  822. if (sign == '+')
  823. set_bit_inv(i, bitmap);
  824. else
  825. clear_bit_inv(i, bitmap);
  826. while (*str == ',' || *str == '\n')
  827. str++;
  828. }
  829. return 0;
  830. }
  831. int ap_parse_mask_str(const char *str,
  832. unsigned long *bitmap, int bits,
  833. struct mutex *lock)
  834. {
  835. unsigned long *newmap, size;
  836. int rc;
  837. /* bits needs to be a multiple of 8 */
  838. if (bits & 0x07)
  839. return -EINVAL;
  840. size = BITS_TO_LONGS(bits)*sizeof(unsigned long);
  841. newmap = kmalloc(size, GFP_KERNEL);
  842. if (!newmap)
  843. return -ENOMEM;
  844. if (mutex_lock_interruptible(lock)) {
  845. kfree(newmap);
  846. return -ERESTARTSYS;
  847. }
  848. if (*str == '+' || *str == '-') {
  849. memcpy(newmap, bitmap, size);
  850. rc = modify_bitmap(str, newmap, bits);
  851. } else {
  852. memset(newmap, 0, size);
  853. rc = hex2bitmap(str, newmap, bits);
  854. }
  855. if (rc == 0)
  856. memcpy(bitmap, newmap, size);
  857. mutex_unlock(lock);
  858. kfree(newmap);
  859. return rc;
  860. }
  861. EXPORT_SYMBOL(ap_parse_mask_str);
  862. /*
  863. * AP bus attributes.
  864. */
  865. static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
  866. {
  867. return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
  868. }
  869. static ssize_t ap_domain_store(struct bus_type *bus,
  870. const char *buf, size_t count)
  871. {
  872. int domain;
  873. if (sscanf(buf, "%i\n", &domain) != 1 ||
  874. domain < 0 || domain > ap_max_domain_id ||
  875. !test_bit_inv(domain, ap_perms.aqm))
  876. return -EINVAL;
  877. spin_lock_bh(&ap_domain_lock);
  878. ap_domain_index = domain;
  879. spin_unlock_bh(&ap_domain_lock);
  880. AP_DBF(DBF_DEBUG, "stored new default domain=%d\n", domain);
  881. return count;
  882. }
  883. static BUS_ATTR_RW(ap_domain);
  884. static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
  885. {
  886. if (!ap_configuration) /* QCI not supported */
  887. return snprintf(buf, PAGE_SIZE, "not supported\n");
  888. return snprintf(buf, PAGE_SIZE,
  889. "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
  890. ap_configuration->adm[0], ap_configuration->adm[1],
  891. ap_configuration->adm[2], ap_configuration->adm[3],
  892. ap_configuration->adm[4], ap_configuration->adm[5],
  893. ap_configuration->adm[6], ap_configuration->adm[7]);
  894. }
  895. static BUS_ATTR_RO(ap_control_domain_mask);
  896. static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
  897. {
  898. if (!ap_configuration) /* QCI not supported */
  899. return snprintf(buf, PAGE_SIZE, "not supported\n");
  900. return snprintf(buf, PAGE_SIZE,
  901. "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
  902. ap_configuration->aqm[0], ap_configuration->aqm[1],
  903. ap_configuration->aqm[2], ap_configuration->aqm[3],
  904. ap_configuration->aqm[4], ap_configuration->aqm[5],
  905. ap_configuration->aqm[6], ap_configuration->aqm[7]);
  906. }
  907. static BUS_ATTR_RO(ap_usage_domain_mask);
  908. static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
  909. {
  910. return snprintf(buf, PAGE_SIZE, "%d\n",
  911. ap_using_interrupts() ? 1 : 0);
  912. }
  913. static BUS_ATTR_RO(ap_interrupts);
  914. static ssize_t config_time_show(struct bus_type *bus, char *buf)
  915. {
  916. return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
  917. }
  918. static ssize_t config_time_store(struct bus_type *bus,
  919. const char *buf, size_t count)
  920. {
  921. int time;
  922. if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
  923. return -EINVAL;
  924. ap_config_time = time;
  925. mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
  926. return count;
  927. }
  928. static BUS_ATTR_RW(config_time);
  929. static ssize_t poll_thread_show(struct bus_type *bus, char *buf)
  930. {
  931. return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
  932. }
  933. static ssize_t poll_thread_store(struct bus_type *bus,
  934. const char *buf, size_t count)
  935. {
  936. int flag, rc;
  937. if (sscanf(buf, "%d\n", &flag) != 1)
  938. return -EINVAL;
  939. if (flag) {
  940. rc = ap_poll_thread_start();
  941. if (rc)
  942. count = rc;
  943. } else
  944. ap_poll_thread_stop();
  945. return count;
  946. }
  947. static BUS_ATTR_RW(poll_thread);
  948. static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
  949. {
  950. return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
  951. }
  952. static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
  953. size_t count)
  954. {
  955. unsigned long long time;
  956. ktime_t hr_time;
  957. /* 120 seconds = maximum poll interval */
  958. if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
  959. time > 120000000000ULL)
  960. return -EINVAL;
  961. poll_timeout = time;
  962. hr_time = poll_timeout;
  963. spin_lock_bh(&ap_poll_timer_lock);
  964. hrtimer_cancel(&ap_poll_timer);
  965. hrtimer_set_expires(&ap_poll_timer, hr_time);
  966. hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
  967. spin_unlock_bh(&ap_poll_timer_lock);
  968. return count;
  969. }
  970. static BUS_ATTR_RW(poll_timeout);
  971. static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
  972. {
  973. int max_domain_id;
  974. if (ap_configuration)
  975. max_domain_id = ap_max_domain_id ? : -1;
  976. else
  977. max_domain_id = 15;
  978. return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
  979. }
  980. static BUS_ATTR_RO(ap_max_domain_id);
  981. static ssize_t apmask_show(struct bus_type *bus, char *buf)
  982. {
  983. int rc;
  984. if (mutex_lock_interruptible(&ap_perms_mutex))
  985. return -ERESTARTSYS;
  986. rc = snprintf(buf, PAGE_SIZE,
  987. "0x%016lx%016lx%016lx%016lx\n",
  988. ap_perms.apm[0], ap_perms.apm[1],
  989. ap_perms.apm[2], ap_perms.apm[3]);
  990. mutex_unlock(&ap_perms_mutex);
  991. return rc;
  992. }
  993. static ssize_t apmask_store(struct bus_type *bus, const char *buf,
  994. size_t count)
  995. {
  996. int rc;
  997. rc = ap_parse_mask_str(buf, ap_perms.apm, AP_DEVICES, &ap_perms_mutex);
  998. if (rc)
  999. return rc;
  1000. ap_bus_revise_bindings();
  1001. return count;
  1002. }
  1003. static BUS_ATTR_RW(apmask);
  1004. static ssize_t aqmask_show(struct bus_type *bus, char *buf)
  1005. {
  1006. int rc;
  1007. if (mutex_lock_interruptible(&ap_perms_mutex))
  1008. return -ERESTARTSYS;
  1009. rc = snprintf(buf, PAGE_SIZE,
  1010. "0x%016lx%016lx%016lx%016lx\n",
  1011. ap_perms.aqm[0], ap_perms.aqm[1],
  1012. ap_perms.aqm[2], ap_perms.aqm[3]);
  1013. mutex_unlock(&ap_perms_mutex);
  1014. return rc;
  1015. }
  1016. static ssize_t aqmask_store(struct bus_type *bus, const char *buf,
  1017. size_t count)
  1018. {
  1019. int rc;
  1020. rc = ap_parse_mask_str(buf, ap_perms.aqm, AP_DOMAINS, &ap_perms_mutex);
  1021. if (rc)
  1022. return rc;
  1023. ap_bus_revise_bindings();
  1024. return count;
  1025. }
  1026. static BUS_ATTR_RW(aqmask);
  1027. static struct bus_attribute *const ap_bus_attrs[] = {
  1028. &bus_attr_ap_domain,
  1029. &bus_attr_ap_control_domain_mask,
  1030. &bus_attr_ap_usage_domain_mask,
  1031. &bus_attr_config_time,
  1032. &bus_attr_poll_thread,
  1033. &bus_attr_ap_interrupts,
  1034. &bus_attr_poll_timeout,
  1035. &bus_attr_ap_max_domain_id,
  1036. &bus_attr_apmask,
  1037. &bus_attr_aqmask,
  1038. NULL,
  1039. };
  1040. /**
  1041. * ap_select_domain(): Select an AP domain if possible and we haven't
  1042. * already done so before.
  1043. */
  1044. static void ap_select_domain(void)
  1045. {
  1046. int count, max_count, best_domain;
  1047. struct ap_queue_status status;
  1048. int i, j;
  1049. /*
  1050. * We want to use a single domain. Either the one specified with
  1051. * the "domain=" parameter or the domain with the maximum number
  1052. * of devices.
  1053. */
  1054. spin_lock_bh(&ap_domain_lock);
  1055. if (ap_domain_index >= 0) {
  1056. /* Domain has already been selected. */
  1057. spin_unlock_bh(&ap_domain_lock);
  1058. return;
  1059. }
  1060. best_domain = -1;
  1061. max_count = 0;
  1062. for (i = 0; i < AP_DOMAINS; i++) {
  1063. if (!ap_test_config_domain(i) ||
  1064. !test_bit_inv(i, ap_perms.aqm))
  1065. continue;
  1066. count = 0;
  1067. for (j = 0; j < AP_DEVICES; j++) {
  1068. if (!ap_test_config_card_id(j))
  1069. continue;
  1070. status = ap_test_queue(AP_MKQID(j, i),
  1071. ap_apft_available(),
  1072. NULL);
  1073. if (status.response_code != AP_RESPONSE_NORMAL)
  1074. continue;
  1075. count++;
  1076. }
  1077. if (count > max_count) {
  1078. max_count = count;
  1079. best_domain = i;
  1080. }
  1081. }
  1082. if (best_domain >= 0) {
  1083. ap_domain_index = best_domain;
  1084. AP_DBF(DBF_DEBUG, "new ap_domain_index=%d\n", ap_domain_index);
  1085. }
  1086. spin_unlock_bh(&ap_domain_lock);
  1087. }
  1088. /*
  1089. * This function checks the type and returns either 0 for not
  1090. * supported or the highest compatible type value (which may
  1091. * include the input type value).
  1092. */
  1093. static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
  1094. {
  1095. int comp_type = 0;
  1096. /* < CEX2A is not supported */
  1097. if (rawtype < AP_DEVICE_TYPE_CEX2A)
  1098. return 0;
  1099. /* up to CEX6 known and fully supported */
  1100. if (rawtype <= AP_DEVICE_TYPE_CEX6)
  1101. return rawtype;
  1102. /*
  1103. * unknown new type > CEX6, check for compatibility
  1104. * to the highest known and supported type which is
  1105. * currently CEX6 with the help of the QACT function.
  1106. */
  1107. if (ap_qact_available()) {
  1108. struct ap_queue_status status;
  1109. union ap_qact_ap_info apinfo = {0};
  1110. apinfo.mode = (func >> 26) & 0x07;
  1111. apinfo.cat = AP_DEVICE_TYPE_CEX6;
  1112. status = ap_qact(qid, 0, &apinfo);
  1113. if (status.response_code == AP_RESPONSE_NORMAL
  1114. && apinfo.cat >= AP_DEVICE_TYPE_CEX2A
  1115. && apinfo.cat <= AP_DEVICE_TYPE_CEX6)
  1116. comp_type = apinfo.cat;
  1117. }
  1118. if (!comp_type)
  1119. AP_DBF(DBF_WARN, "queue=%02x.%04x unable to map type %d\n",
  1120. AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
  1121. else if (comp_type != rawtype)
  1122. AP_DBF(DBF_INFO, "queue=%02x.%04x map type %d to %d\n",
  1123. AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype, comp_type);
  1124. return comp_type;
  1125. }
  1126. /*
  1127. * helper function to be used with bus_find_dev
  1128. * matches for the card device with the given id
  1129. */
  1130. static int __match_card_device_with_id(struct device *dev, void *data)
  1131. {
  1132. return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long) data;
  1133. }
  1134. /* helper function to be used with bus_find_dev
  1135. * matches for the queue device with a given qid
  1136. */
  1137. static int __match_queue_device_with_qid(struct device *dev, void *data)
  1138. {
  1139. return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
  1140. }
  1141. /**
  1142. * ap_scan_bus(): Scan the AP bus for new devices
  1143. * Runs periodically, workqueue timer (ap_config_time)
  1144. */
  1145. static void ap_scan_bus(struct work_struct *unused)
  1146. {
  1147. struct ap_queue *aq;
  1148. struct ap_card *ac;
  1149. struct device *dev;
  1150. ap_qid_t qid;
  1151. int comp_type, depth = 0, type = 0;
  1152. unsigned int func = 0;
  1153. int rc, id, dom, borked, domains, defdomdevs = 0;
  1154. AP_DBF(DBF_DEBUG, "%s running\n", __func__);
  1155. ap_query_configuration(ap_configuration);
  1156. ap_select_domain();
  1157. for (id = 0; id < AP_DEVICES; id++) {
  1158. /* check if device is registered */
  1159. dev = bus_find_device(&ap_bus_type, NULL,
  1160. (void *)(long) id,
  1161. __match_card_device_with_id);
  1162. ac = dev ? to_ap_card(dev) : NULL;
  1163. if (!ap_test_config_card_id(id)) {
  1164. if (dev) {
  1165. /* Card device has been removed from
  1166. * configuration, remove the belonging
  1167. * queue devices.
  1168. */
  1169. bus_for_each_dev(&ap_bus_type, NULL,
  1170. (void *)(long) id,
  1171. __ap_queue_devices_with_id_unregister);
  1172. /* now remove the card device */
  1173. device_unregister(dev);
  1174. put_device(dev);
  1175. }
  1176. continue;
  1177. }
  1178. /* According to the configuration there should be a card
  1179. * device, so check if there is at least one valid queue
  1180. * and maybe create queue devices and the card device.
  1181. */
  1182. domains = 0;
  1183. for (dom = 0; dom < AP_DOMAINS; dom++) {
  1184. qid = AP_MKQID(id, dom);
  1185. dev = bus_find_device(&ap_bus_type, NULL,
  1186. (void *)(long) qid,
  1187. __match_queue_device_with_qid);
  1188. aq = dev ? to_ap_queue(dev) : NULL;
  1189. if (!ap_test_config_domain(dom)) {
  1190. if (dev) {
  1191. /* Queue device exists but has been
  1192. * removed from configuration.
  1193. */
  1194. device_unregister(dev);
  1195. put_device(dev);
  1196. }
  1197. continue;
  1198. }
  1199. rc = ap_query_queue(qid, &depth, &type, &func);
  1200. if (dev) {
  1201. spin_lock_bh(&aq->lock);
  1202. if (rc == -ENODEV ||
  1203. /* adapter reconfiguration */
  1204. (ac && ac->functions != func))
  1205. aq->state = AP_STATE_BORKED;
  1206. borked = aq->state == AP_STATE_BORKED;
  1207. spin_unlock_bh(&aq->lock);
  1208. if (borked) /* Remove broken device */
  1209. device_unregister(dev);
  1210. put_device(dev);
  1211. if (!borked) {
  1212. domains++;
  1213. if (dom == ap_domain_index)
  1214. defdomdevs++;
  1215. continue;
  1216. }
  1217. }
  1218. if (rc)
  1219. continue;
  1220. /* a new queue device is needed, check out comp type */
  1221. comp_type = ap_get_compatible_type(qid, type, func);
  1222. if (!comp_type)
  1223. continue;
  1224. /* maybe a card device needs to be created first */
  1225. if (!ac) {
  1226. ac = ap_card_create(id, depth, type,
  1227. comp_type, func);
  1228. if (!ac)
  1229. continue;
  1230. ac->ap_dev.device.bus = &ap_bus_type;
  1231. ac->ap_dev.device.parent = ap_root_device;
  1232. dev_set_name(&ac->ap_dev.device,
  1233. "card%02x", id);
  1234. /* Register card with AP bus */
  1235. rc = device_register(&ac->ap_dev.device);
  1236. if (rc) {
  1237. put_device(&ac->ap_dev.device);
  1238. ac = NULL;
  1239. break;
  1240. }
  1241. /* get it and thus adjust reference counter */
  1242. get_device(&ac->ap_dev.device);
  1243. }
  1244. /* now create the new queue device */
  1245. aq = ap_queue_create(qid, comp_type);
  1246. if (!aq)
  1247. continue;
  1248. aq->card = ac;
  1249. aq->ap_dev.device.bus = &ap_bus_type;
  1250. aq->ap_dev.device.parent = &ac->ap_dev.device;
  1251. dev_set_name(&aq->ap_dev.device,
  1252. "%02x.%04x", id, dom);
  1253. /* Start with a device reset */
  1254. spin_lock_bh(&aq->lock);
  1255. ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
  1256. spin_unlock_bh(&aq->lock);
  1257. /* Register device */
  1258. rc = device_register(&aq->ap_dev.device);
  1259. if (rc) {
  1260. put_device(&aq->ap_dev.device);
  1261. continue;
  1262. }
  1263. domains++;
  1264. if (dom == ap_domain_index)
  1265. defdomdevs++;
  1266. } /* end domain loop */
  1267. if (ac) {
  1268. /* remove card dev if there are no queue devices */
  1269. if (!domains)
  1270. device_unregister(&ac->ap_dev.device);
  1271. put_device(&ac->ap_dev.device);
  1272. }
  1273. } /* end device loop */
  1274. if (ap_domain_index >= 0 && defdomdevs < 1)
  1275. AP_DBF(DBF_INFO,
  1276. "no queue device with default domain %d available\n",
  1277. ap_domain_index);
  1278. mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
  1279. }
  1280. static void ap_config_timeout(struct timer_list *unused)
  1281. {
  1282. if (ap_suspend_flag)
  1283. return;
  1284. queue_work(system_long_wq, &ap_scan_work);
  1285. }
  1286. static int __init ap_debug_init(void)
  1287. {
  1288. ap_dbf_info = debug_register("ap", 1, 1,
  1289. DBF_MAX_SPRINTF_ARGS * sizeof(long));
  1290. debug_register_view(ap_dbf_info, &debug_sprintf_view);
  1291. debug_set_level(ap_dbf_info, DBF_ERR);
  1292. return 0;
  1293. }
  1294. static void __init ap_perms_init(void)
  1295. {
  1296. /* all resources useable if no kernel parameter string given */
  1297. memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
  1298. memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
  1299. memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
  1300. /* apm kernel parameter string */
  1301. if (apm_str) {
  1302. memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
  1303. ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
  1304. &ap_perms_mutex);
  1305. }
  1306. /* aqm kernel parameter string */
  1307. if (aqm_str) {
  1308. memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
  1309. ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
  1310. &ap_perms_mutex);
  1311. }
  1312. }
  1313. /**
  1314. * ap_module_init(): The module initialization code.
  1315. *
  1316. * Initializes the module.
  1317. */
  1318. static int __init ap_module_init(void)
  1319. {
  1320. int max_domain_id;
  1321. int rc, i;
  1322. rc = ap_debug_init();
  1323. if (rc)
  1324. return rc;
  1325. if (!ap_instructions_available()) {
  1326. pr_warn("The hardware system does not support AP instructions\n");
  1327. return -ENODEV;
  1328. }
  1329. /* set up the AP permissions (ioctls, ap and aq masks) */
  1330. ap_perms_init();
  1331. /* Get AP configuration data if available */
  1332. ap_init_configuration();
  1333. if (ap_configuration)
  1334. max_domain_id =
  1335. ap_max_domain_id ? ap_max_domain_id : AP_DOMAINS - 1;
  1336. else
  1337. max_domain_id = 15;
  1338. if (ap_domain_index < -1 || ap_domain_index > max_domain_id ||
  1339. (ap_domain_index >= 0 &&
  1340. !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
  1341. pr_warn("%d is not a valid cryptographic domain\n",
  1342. ap_domain_index);
  1343. ap_domain_index = -1;
  1344. }
  1345. /* In resume callback we need to know if the user had set the domain.
  1346. * If so, we can not just reset it.
  1347. */
  1348. if (ap_domain_index >= 0)
  1349. user_set_domain = 1;
  1350. if (ap_interrupts_available()) {
  1351. rc = register_adapter_interrupt(&ap_airq);
  1352. ap_airq_flag = (rc == 0);
  1353. }
  1354. /* Create /sys/bus/ap. */
  1355. rc = bus_register(&ap_bus_type);
  1356. if (rc)
  1357. goto out;
  1358. for (i = 0; ap_bus_attrs[i]; i++) {
  1359. rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
  1360. if (rc)
  1361. goto out_bus;
  1362. }
  1363. /* Create /sys/devices/ap. */
  1364. ap_root_device = root_device_register("ap");
  1365. rc = PTR_ERR_OR_ZERO(ap_root_device);
  1366. if (rc)
  1367. goto out_bus;
  1368. /* Setup the AP bus rescan timer. */
  1369. timer_setup(&ap_config_timer, ap_config_timeout, 0);
  1370. /*
  1371. * Setup the high resultion poll timer.
  1372. * If we are running under z/VM adjust polling to z/VM polling rate.
  1373. */
  1374. if (MACHINE_IS_VM)
  1375. poll_timeout = 1500000;
  1376. spin_lock_init(&ap_poll_timer_lock);
  1377. hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  1378. ap_poll_timer.function = ap_poll_timeout;
  1379. /* Start the low priority AP bus poll thread. */
  1380. if (ap_thread_flag) {
  1381. rc = ap_poll_thread_start();
  1382. if (rc)
  1383. goto out_work;
  1384. }
  1385. rc = register_pm_notifier(&ap_power_notifier);
  1386. if (rc)
  1387. goto out_pm;
  1388. queue_work(system_long_wq, &ap_scan_work);
  1389. initialised = true;
  1390. return 0;
  1391. out_pm:
  1392. ap_poll_thread_stop();
  1393. out_work:
  1394. hrtimer_cancel(&ap_poll_timer);
  1395. root_device_unregister(ap_root_device);
  1396. out_bus:
  1397. while (i--)
  1398. bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
  1399. bus_unregister(&ap_bus_type);
  1400. out:
  1401. if (ap_using_interrupts())
  1402. unregister_adapter_interrupt(&ap_airq);
  1403. kfree(ap_configuration);
  1404. return rc;
  1405. }
  1406. device_initcall(ap_module_init);