ap_bus.c 40 KB

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