ap_bus.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049
  1. /*
  2. * Copyright IBM Corp. 2006, 2012
  3. * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
  4. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  5. * Ralph Wuerthner <rwuerthn@de.ibm.com>
  6. * Felix Beck <felix.beck@de.ibm.com>
  7. * Holger Dengler <hd@linux.vnet.ibm.com>
  8. *
  9. * Adjunct processor bus.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #define KMSG_COMPONENT "ap"
  26. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  27. #include <linux/kernel_stat.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/delay.h>
  31. #include <linux/err.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/workqueue.h>
  34. #include <linux/slab.h>
  35. #include <linux/notifier.h>
  36. #include <linux/kthread.h>
  37. #include <linux/mutex.h>
  38. #include <asm/reset.h>
  39. #include <asm/airq.h>
  40. #include <linux/atomic.h>
  41. #include <asm/isc.h>
  42. #include <linux/hrtimer.h>
  43. #include <linux/ktime.h>
  44. #include <asm/facility.h>
  45. #include "ap_bus.h"
  46. /* Some prototypes. */
  47. static void ap_scan_bus(struct work_struct *);
  48. static void ap_poll_all(unsigned long);
  49. static enum hrtimer_restart ap_poll_timeout(struct hrtimer *);
  50. static int ap_poll_thread_start(void);
  51. static void ap_poll_thread_stop(void);
  52. static void ap_request_timeout(unsigned long);
  53. static inline void ap_schedule_poll_timer(void);
  54. static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags);
  55. static int ap_device_remove(struct device *dev);
  56. static int ap_device_probe(struct device *dev);
  57. static void ap_interrupt_handler(struct airq_struct *airq);
  58. static void ap_reset(struct ap_device *ap_dev);
  59. static void ap_config_timeout(unsigned long ptr);
  60. static int ap_select_domain(void);
  61. static void ap_query_configuration(void);
  62. /*
  63. * Module description.
  64. */
  65. MODULE_AUTHOR("IBM Corporation");
  66. MODULE_DESCRIPTION("Adjunct Processor Bus driver, " \
  67. "Copyright IBM Corp. 2006, 2012");
  68. MODULE_LICENSE("GPL");
  69. MODULE_ALIAS("z90crypt");
  70. /*
  71. * Module parameter
  72. */
  73. int ap_domain_index = -1; /* Adjunct Processor Domain Index */
  74. module_param_named(domain, ap_domain_index, int, S_IRUSR|S_IRGRP);
  75. MODULE_PARM_DESC(domain, "domain index for ap devices");
  76. EXPORT_SYMBOL(ap_domain_index);
  77. static int ap_thread_flag = 0;
  78. module_param_named(poll_thread, ap_thread_flag, int, S_IRUSR|S_IRGRP);
  79. MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
  80. static struct device *ap_root_device = NULL;
  81. static struct ap_config_info *ap_configuration;
  82. static DEFINE_SPINLOCK(ap_device_list_lock);
  83. static LIST_HEAD(ap_device_list);
  84. /*
  85. * Workqueue & timer for bus rescan.
  86. */
  87. static struct workqueue_struct *ap_work_queue;
  88. static struct timer_list ap_config_timer;
  89. static int ap_config_time = AP_CONFIG_TIME;
  90. static DECLARE_WORK(ap_config_work, ap_scan_bus);
  91. /*
  92. * Tasklet & timer for AP request polling and interrupts
  93. */
  94. static DECLARE_TASKLET(ap_tasklet, ap_poll_all, 0);
  95. static atomic_t ap_poll_requests = ATOMIC_INIT(0);
  96. static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
  97. static struct task_struct *ap_poll_kthread = NULL;
  98. static DEFINE_MUTEX(ap_poll_thread_mutex);
  99. static DEFINE_SPINLOCK(ap_poll_timer_lock);
  100. static struct hrtimer ap_poll_timer;
  101. /* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
  102. * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
  103. static unsigned long long poll_timeout = 250000;
  104. /* Suspend flag */
  105. static int ap_suspend_flag;
  106. /* Flag to check if domain was set through module parameter domain=. This is
  107. * important when supsend and resume is done in a z/VM environment where the
  108. * domain might change. */
  109. static int user_set_domain = 0;
  110. static struct bus_type ap_bus_type;
  111. /* Adapter interrupt definitions */
  112. static int ap_airq_flag;
  113. static struct airq_struct ap_airq = {
  114. .handler = ap_interrupt_handler,
  115. .isc = AP_ISC,
  116. };
  117. /**
  118. * ap_using_interrupts() - Returns non-zero if interrupt support is
  119. * available.
  120. */
  121. static inline int ap_using_interrupts(void)
  122. {
  123. return ap_airq_flag;
  124. }
  125. /**
  126. * ap_intructions_available() - Test if AP instructions are available.
  127. *
  128. * Returns 0 if the AP instructions are installed.
  129. */
  130. static inline int ap_instructions_available(void)
  131. {
  132. register unsigned long reg0 asm ("0") = AP_MKQID(0,0);
  133. register unsigned long reg1 asm ("1") = -ENODEV;
  134. register unsigned long reg2 asm ("2") = 0UL;
  135. asm volatile(
  136. " .long 0xb2af0000\n" /* PQAP(TAPQ) */
  137. "0: la %1,0\n"
  138. "1:\n"
  139. EX_TABLE(0b, 1b)
  140. : "+d" (reg0), "+d" (reg1), "+d" (reg2) : : "cc" );
  141. return reg1;
  142. }
  143. /**
  144. * ap_interrupts_available(): Test if AP interrupts are available.
  145. *
  146. * Returns 1 if AP interrupts are available.
  147. */
  148. static int ap_interrupts_available(void)
  149. {
  150. return test_facility(2) && test_facility(65);
  151. }
  152. /**
  153. * ap_configuration_available(): Test if AP configuration
  154. * information is available.
  155. *
  156. * Returns 1 if AP configuration information is available.
  157. */
  158. #ifdef CONFIG_64BIT
  159. static int ap_configuration_available(void)
  160. {
  161. return test_facility(2) && test_facility(12);
  162. }
  163. #endif
  164. /**
  165. * ap_test_queue(): Test adjunct processor queue.
  166. * @qid: The AP queue number
  167. * @queue_depth: Pointer to queue depth value
  168. * @device_type: Pointer to device type value
  169. *
  170. * Returns AP queue status structure.
  171. */
  172. static inline struct ap_queue_status
  173. ap_test_queue(ap_qid_t qid, int *queue_depth, int *device_type)
  174. {
  175. register unsigned long reg0 asm ("0") = qid;
  176. register struct ap_queue_status reg1 asm ("1");
  177. register unsigned long reg2 asm ("2") = 0UL;
  178. asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */
  179. : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
  180. *device_type = (int) (reg2 >> 24);
  181. *queue_depth = (int) (reg2 & 0xff);
  182. return reg1;
  183. }
  184. /**
  185. * ap_reset_queue(): Reset adjunct processor queue.
  186. * @qid: The AP queue number
  187. *
  188. * Returns AP queue status structure.
  189. */
  190. static inline struct ap_queue_status ap_reset_queue(ap_qid_t qid)
  191. {
  192. register unsigned long reg0 asm ("0") = qid | 0x01000000UL;
  193. register struct ap_queue_status reg1 asm ("1");
  194. register unsigned long reg2 asm ("2") = 0UL;
  195. asm volatile(
  196. ".long 0xb2af0000" /* PQAP(RAPQ) */
  197. : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
  198. return reg1;
  199. }
  200. #ifdef CONFIG_64BIT
  201. /**
  202. * ap_queue_interruption_control(): Enable interruption for a specific AP.
  203. * @qid: The AP queue number
  204. * @ind: The notification indicator byte
  205. *
  206. * Returns AP queue status.
  207. */
  208. static inline struct ap_queue_status
  209. ap_queue_interruption_control(ap_qid_t qid, void *ind)
  210. {
  211. register unsigned long reg0 asm ("0") = qid | 0x03000000UL;
  212. register unsigned long reg1_in asm ("1") = 0x0000800000000000UL | AP_ISC;
  213. register struct ap_queue_status reg1_out asm ("1");
  214. register void *reg2 asm ("2") = ind;
  215. asm volatile(
  216. ".long 0xb2af0000" /* PQAP(AQIC) */
  217. : "+d" (reg0), "+d" (reg1_in), "=d" (reg1_out), "+d" (reg2)
  218. :
  219. : "cc" );
  220. return reg1_out;
  221. }
  222. #endif
  223. #ifdef CONFIG_64BIT
  224. static inline struct ap_queue_status
  225. __ap_query_functions(ap_qid_t qid, unsigned int *functions)
  226. {
  227. register unsigned long reg0 asm ("0") = 0UL | qid | (1UL << 23);
  228. register struct ap_queue_status reg1 asm ("1") = AP_QUEUE_STATUS_INVALID;
  229. register unsigned long reg2 asm ("2");
  230. asm volatile(
  231. ".long 0xb2af0000\n" /* PQAP(TAPQ) */
  232. "0:\n"
  233. EX_TABLE(0b, 0b)
  234. : "+d" (reg0), "+d" (reg1), "=d" (reg2)
  235. :
  236. : "cc");
  237. *functions = (unsigned int)(reg2 >> 32);
  238. return reg1;
  239. }
  240. #endif
  241. #ifdef CONFIG_64BIT
  242. static inline int __ap_query_configuration(struct ap_config_info *config)
  243. {
  244. register unsigned long reg0 asm ("0") = 0x04000000UL;
  245. register unsigned long reg1 asm ("1") = -EINVAL;
  246. register unsigned char *reg2 asm ("2") = (unsigned char *)config;
  247. asm volatile(
  248. ".long 0xb2af0000\n" /* PQAP(QCI) */
  249. "0: la %1,0\n"
  250. "1:\n"
  251. EX_TABLE(0b, 1b)
  252. : "+d" (reg0), "+d" (reg1), "+d" (reg2)
  253. :
  254. : "cc");
  255. return reg1;
  256. }
  257. #endif
  258. /**
  259. * ap_query_functions(): Query supported functions.
  260. * @qid: The AP queue number
  261. * @functions: Pointer to functions field.
  262. *
  263. * Returns
  264. * 0 on success.
  265. * -ENODEV if queue not valid.
  266. * -EBUSY if device busy.
  267. * -EINVAL if query function is not supported
  268. */
  269. static int ap_query_functions(ap_qid_t qid, unsigned int *functions)
  270. {
  271. #ifdef CONFIG_64BIT
  272. struct ap_queue_status status;
  273. int i;
  274. status = __ap_query_functions(qid, functions);
  275. for (i = 0; i < AP_MAX_RESET; i++) {
  276. if (ap_queue_status_invalid_test(&status))
  277. return -ENODEV;
  278. switch (status.response_code) {
  279. case AP_RESPONSE_NORMAL:
  280. return 0;
  281. case AP_RESPONSE_RESET_IN_PROGRESS:
  282. case AP_RESPONSE_BUSY:
  283. break;
  284. case AP_RESPONSE_Q_NOT_AVAIL:
  285. case AP_RESPONSE_DECONFIGURED:
  286. case AP_RESPONSE_CHECKSTOPPED:
  287. case AP_RESPONSE_INVALID_ADDRESS:
  288. return -ENODEV;
  289. case AP_RESPONSE_OTHERWISE_CHANGED:
  290. break;
  291. default:
  292. break;
  293. }
  294. if (i < AP_MAX_RESET - 1) {
  295. udelay(5);
  296. status = __ap_query_functions(qid, functions);
  297. }
  298. }
  299. return -EBUSY;
  300. #else
  301. return -EINVAL;
  302. #endif
  303. }
  304. /**
  305. * ap_queue_enable_interruption(): Enable interruption on an AP.
  306. * @qid: The AP queue number
  307. * @ind: the notification indicator byte
  308. *
  309. * Enables interruption on AP queue via ap_queue_interruption_control(). Based
  310. * on the return value it waits a while and tests the AP queue if interrupts
  311. * have been switched on using ap_test_queue().
  312. */
  313. static int ap_queue_enable_interruption(ap_qid_t qid, void *ind)
  314. {
  315. #ifdef CONFIG_64BIT
  316. struct ap_queue_status status;
  317. int t_depth, t_device_type, rc, i;
  318. rc = -EBUSY;
  319. status = ap_queue_interruption_control(qid, ind);
  320. for (i = 0; i < AP_MAX_RESET; i++) {
  321. switch (status.response_code) {
  322. case AP_RESPONSE_NORMAL:
  323. if (status.int_enabled)
  324. return 0;
  325. break;
  326. case AP_RESPONSE_RESET_IN_PROGRESS:
  327. case AP_RESPONSE_BUSY:
  328. if (i < AP_MAX_RESET - 1) {
  329. udelay(5);
  330. status = ap_queue_interruption_control(qid,
  331. ind);
  332. continue;
  333. }
  334. break;
  335. case AP_RESPONSE_Q_NOT_AVAIL:
  336. case AP_RESPONSE_DECONFIGURED:
  337. case AP_RESPONSE_CHECKSTOPPED:
  338. case AP_RESPONSE_INVALID_ADDRESS:
  339. return -ENODEV;
  340. case AP_RESPONSE_OTHERWISE_CHANGED:
  341. if (status.int_enabled)
  342. return 0;
  343. break;
  344. default:
  345. break;
  346. }
  347. if (i < AP_MAX_RESET - 1) {
  348. udelay(5);
  349. status = ap_test_queue(qid, &t_depth, &t_device_type);
  350. }
  351. }
  352. return rc;
  353. #else
  354. return -EINVAL;
  355. #endif
  356. }
  357. /**
  358. * __ap_send(): Send message to adjunct processor queue.
  359. * @qid: The AP queue number
  360. * @psmid: The program supplied message identifier
  361. * @msg: The message text
  362. * @length: The message length
  363. * @special: Special Bit
  364. *
  365. * Returns AP queue status structure.
  366. * Condition code 1 on NQAP can't happen because the L bit is 1.
  367. * Condition code 2 on NQAP also means the send is incomplete,
  368. * because a segment boundary was reached. The NQAP is repeated.
  369. */
  370. static inline struct ap_queue_status
  371. __ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length,
  372. unsigned int special)
  373. {
  374. typedef struct { char _[length]; } msgblock;
  375. register unsigned long reg0 asm ("0") = qid | 0x40000000UL;
  376. register struct ap_queue_status reg1 asm ("1");
  377. register unsigned long reg2 asm ("2") = (unsigned long) msg;
  378. register unsigned long reg3 asm ("3") = (unsigned long) length;
  379. register unsigned long reg4 asm ("4") = (unsigned int) (psmid >> 32);
  380. register unsigned long reg5 asm ("5") = psmid & 0xffffffff;
  381. if (special == 1)
  382. reg0 |= 0x400000UL;
  383. asm volatile (
  384. "0: .long 0xb2ad0042\n" /* NQAP */
  385. " brc 2,0b"
  386. : "+d" (reg0), "=d" (reg1), "+d" (reg2), "+d" (reg3)
  387. : "d" (reg4), "d" (reg5), "m" (*(msgblock *) msg)
  388. : "cc" );
  389. return reg1;
  390. }
  391. int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
  392. {
  393. struct ap_queue_status status;
  394. status = __ap_send(qid, psmid, msg, length, 0);
  395. switch (status.response_code) {
  396. case AP_RESPONSE_NORMAL:
  397. return 0;
  398. case AP_RESPONSE_Q_FULL:
  399. case AP_RESPONSE_RESET_IN_PROGRESS:
  400. return -EBUSY;
  401. case AP_RESPONSE_REQ_FAC_NOT_INST:
  402. return -EINVAL;
  403. default: /* Device is gone. */
  404. return -ENODEV;
  405. }
  406. }
  407. EXPORT_SYMBOL(ap_send);
  408. /**
  409. * __ap_recv(): Receive message from adjunct processor queue.
  410. * @qid: The AP queue number
  411. * @psmid: Pointer to program supplied message identifier
  412. * @msg: The message text
  413. * @length: The message length
  414. *
  415. * Returns AP queue status structure.
  416. * Condition code 1 on DQAP means the receive has taken place
  417. * but only partially. The response is incomplete, hence the
  418. * DQAP is repeated.
  419. * Condition code 2 on DQAP also means the receive is incomplete,
  420. * this time because a segment boundary was reached. Again, the
  421. * DQAP is repeated.
  422. * Note that gpr2 is used by the DQAP instruction to keep track of
  423. * any 'residual' length, in case the instruction gets interrupted.
  424. * Hence it gets zeroed before the instruction.
  425. */
  426. static inline struct ap_queue_status
  427. __ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
  428. {
  429. typedef struct { char _[length]; } msgblock;
  430. register unsigned long reg0 asm("0") = qid | 0x80000000UL;
  431. register struct ap_queue_status reg1 asm ("1");
  432. register unsigned long reg2 asm("2") = 0UL;
  433. register unsigned long reg4 asm("4") = (unsigned long) msg;
  434. register unsigned long reg5 asm("5") = (unsigned long) length;
  435. register unsigned long reg6 asm("6") = 0UL;
  436. register unsigned long reg7 asm("7") = 0UL;
  437. asm volatile(
  438. "0: .long 0xb2ae0064\n" /* DQAP */
  439. " brc 6,0b\n"
  440. : "+d" (reg0), "=d" (reg1), "+d" (reg2),
  441. "+d" (reg4), "+d" (reg5), "+d" (reg6), "+d" (reg7),
  442. "=m" (*(msgblock *) msg) : : "cc" );
  443. *psmid = (((unsigned long long) reg6) << 32) + reg7;
  444. return reg1;
  445. }
  446. int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
  447. {
  448. struct ap_queue_status status;
  449. status = __ap_recv(qid, psmid, msg, length);
  450. switch (status.response_code) {
  451. case AP_RESPONSE_NORMAL:
  452. return 0;
  453. case AP_RESPONSE_NO_PENDING_REPLY:
  454. if (status.queue_empty)
  455. return -ENOENT;
  456. return -EBUSY;
  457. case AP_RESPONSE_RESET_IN_PROGRESS:
  458. return -EBUSY;
  459. default:
  460. return -ENODEV;
  461. }
  462. }
  463. EXPORT_SYMBOL(ap_recv);
  464. /**
  465. * ap_query_queue(): Check if an AP queue is available.
  466. * @qid: The AP queue number
  467. * @queue_depth: Pointer to queue depth value
  468. * @device_type: Pointer to device type value
  469. *
  470. * The test is repeated for AP_MAX_RESET times.
  471. */
  472. static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type)
  473. {
  474. struct ap_queue_status status;
  475. int t_depth, t_device_type, rc, i;
  476. rc = -EBUSY;
  477. for (i = 0; i < AP_MAX_RESET; i++) {
  478. status = ap_test_queue(qid, &t_depth, &t_device_type);
  479. switch (status.response_code) {
  480. case AP_RESPONSE_NORMAL:
  481. *queue_depth = t_depth + 1;
  482. *device_type = t_device_type;
  483. rc = 0;
  484. break;
  485. case AP_RESPONSE_Q_NOT_AVAIL:
  486. rc = -ENODEV;
  487. break;
  488. case AP_RESPONSE_RESET_IN_PROGRESS:
  489. break;
  490. case AP_RESPONSE_DECONFIGURED:
  491. rc = -ENODEV;
  492. break;
  493. case AP_RESPONSE_CHECKSTOPPED:
  494. rc = -ENODEV;
  495. break;
  496. case AP_RESPONSE_INVALID_ADDRESS:
  497. rc = -ENODEV;
  498. break;
  499. case AP_RESPONSE_OTHERWISE_CHANGED:
  500. break;
  501. case AP_RESPONSE_BUSY:
  502. break;
  503. default:
  504. BUG();
  505. }
  506. if (rc != -EBUSY)
  507. break;
  508. if (i < AP_MAX_RESET - 1)
  509. udelay(5);
  510. }
  511. return rc;
  512. }
  513. /**
  514. * ap_init_queue(): Reset an AP queue.
  515. * @qid: The AP queue number
  516. *
  517. * Reset an AP queue and wait for it to become available again.
  518. */
  519. static int ap_init_queue(ap_qid_t qid)
  520. {
  521. struct ap_queue_status status;
  522. int rc, dummy, i;
  523. rc = -ENODEV;
  524. status = ap_reset_queue(qid);
  525. for (i = 0; i < AP_MAX_RESET; i++) {
  526. switch (status.response_code) {
  527. case AP_RESPONSE_NORMAL:
  528. if (status.queue_empty)
  529. rc = 0;
  530. break;
  531. case AP_RESPONSE_Q_NOT_AVAIL:
  532. case AP_RESPONSE_DECONFIGURED:
  533. case AP_RESPONSE_CHECKSTOPPED:
  534. i = AP_MAX_RESET; /* return with -ENODEV */
  535. break;
  536. case AP_RESPONSE_RESET_IN_PROGRESS:
  537. rc = -EBUSY;
  538. case AP_RESPONSE_BUSY:
  539. default:
  540. break;
  541. }
  542. if (rc != -ENODEV && rc != -EBUSY)
  543. break;
  544. if (i < AP_MAX_RESET - 1) {
  545. /* Time we are waiting until we give up (0.7sec * 90).
  546. * Since the actual request (in progress) will not
  547. * interrupted immediately for the reset command,
  548. * we have to be patient. In worst case we have to
  549. * wait 60sec + reset time (some msec).
  550. */
  551. schedule_timeout(AP_RESET_TIMEOUT);
  552. status = ap_test_queue(qid, &dummy, &dummy);
  553. }
  554. }
  555. if (rc == 0 && ap_using_interrupts()) {
  556. rc = ap_queue_enable_interruption(qid, ap_airq.lsi_ptr);
  557. /* If interruption mode is supported by the machine,
  558. * but an AP can not be enabled for interruption then
  559. * the AP will be discarded. */
  560. if (rc)
  561. pr_err("Registering adapter interrupts for "
  562. "AP %d failed\n", AP_QID_DEVICE(qid));
  563. }
  564. return rc;
  565. }
  566. /**
  567. * ap_increase_queue_count(): Arm request timeout.
  568. * @ap_dev: Pointer to an AP device.
  569. *
  570. * Arm request timeout if an AP device was idle and a new request is submitted.
  571. */
  572. static void ap_increase_queue_count(struct ap_device *ap_dev)
  573. {
  574. int timeout = ap_dev->drv->request_timeout;
  575. ap_dev->queue_count++;
  576. if (ap_dev->queue_count == 1) {
  577. mod_timer(&ap_dev->timeout, jiffies + timeout);
  578. ap_dev->reset = AP_RESET_ARMED;
  579. }
  580. }
  581. /**
  582. * ap_decrease_queue_count(): Decrease queue count.
  583. * @ap_dev: Pointer to an AP device.
  584. *
  585. * If AP device is still alive, re-schedule request timeout if there are still
  586. * pending requests.
  587. */
  588. static void ap_decrease_queue_count(struct ap_device *ap_dev)
  589. {
  590. int timeout = ap_dev->drv->request_timeout;
  591. ap_dev->queue_count--;
  592. if (ap_dev->queue_count > 0)
  593. mod_timer(&ap_dev->timeout, jiffies + timeout);
  594. else
  595. /*
  596. * The timeout timer should to be disabled now - since
  597. * del_timer_sync() is very expensive, we just tell via the
  598. * reset flag to ignore the pending timeout timer.
  599. */
  600. ap_dev->reset = AP_RESET_IGNORE;
  601. }
  602. /*
  603. * AP device related attributes.
  604. */
  605. static ssize_t ap_hwtype_show(struct device *dev,
  606. struct device_attribute *attr, char *buf)
  607. {
  608. struct ap_device *ap_dev = to_ap_dev(dev);
  609. return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->device_type);
  610. }
  611. static DEVICE_ATTR(hwtype, 0444, ap_hwtype_show, NULL);
  612. static ssize_t ap_depth_show(struct device *dev, struct device_attribute *attr,
  613. char *buf)
  614. {
  615. struct ap_device *ap_dev = to_ap_dev(dev);
  616. return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->queue_depth);
  617. }
  618. static DEVICE_ATTR(depth, 0444, ap_depth_show, NULL);
  619. static ssize_t ap_request_count_show(struct device *dev,
  620. struct device_attribute *attr,
  621. char *buf)
  622. {
  623. struct ap_device *ap_dev = to_ap_dev(dev);
  624. int rc;
  625. spin_lock_bh(&ap_dev->lock);
  626. rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->total_request_count);
  627. spin_unlock_bh(&ap_dev->lock);
  628. return rc;
  629. }
  630. static DEVICE_ATTR(request_count, 0444, ap_request_count_show, NULL);
  631. static ssize_t ap_requestq_count_show(struct device *dev,
  632. struct device_attribute *attr, char *buf)
  633. {
  634. struct ap_device *ap_dev = to_ap_dev(dev);
  635. int rc;
  636. spin_lock_bh(&ap_dev->lock);
  637. rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->requestq_count);
  638. spin_unlock_bh(&ap_dev->lock);
  639. return rc;
  640. }
  641. static DEVICE_ATTR(requestq_count, 0444, ap_requestq_count_show, NULL);
  642. static ssize_t ap_pendingq_count_show(struct device *dev,
  643. struct device_attribute *attr, char *buf)
  644. {
  645. struct ap_device *ap_dev = to_ap_dev(dev);
  646. int rc;
  647. spin_lock_bh(&ap_dev->lock);
  648. rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->pendingq_count);
  649. spin_unlock_bh(&ap_dev->lock);
  650. return rc;
  651. }
  652. static DEVICE_ATTR(pendingq_count, 0444, ap_pendingq_count_show, NULL);
  653. static ssize_t ap_modalias_show(struct device *dev,
  654. struct device_attribute *attr, char *buf)
  655. {
  656. return sprintf(buf, "ap:t%02X", to_ap_dev(dev)->device_type);
  657. }
  658. static DEVICE_ATTR(modalias, 0444, ap_modalias_show, NULL);
  659. static ssize_t ap_functions_show(struct device *dev,
  660. struct device_attribute *attr, char *buf)
  661. {
  662. struct ap_device *ap_dev = to_ap_dev(dev);
  663. return snprintf(buf, PAGE_SIZE, "0x%08X\n", ap_dev->functions);
  664. }
  665. static DEVICE_ATTR(ap_functions, 0444, ap_functions_show, NULL);
  666. static struct attribute *ap_dev_attrs[] = {
  667. &dev_attr_hwtype.attr,
  668. &dev_attr_depth.attr,
  669. &dev_attr_request_count.attr,
  670. &dev_attr_requestq_count.attr,
  671. &dev_attr_pendingq_count.attr,
  672. &dev_attr_modalias.attr,
  673. &dev_attr_ap_functions.attr,
  674. NULL
  675. };
  676. static struct attribute_group ap_dev_attr_group = {
  677. .attrs = ap_dev_attrs
  678. };
  679. /**
  680. * ap_bus_match()
  681. * @dev: Pointer to device
  682. * @drv: Pointer to device_driver
  683. *
  684. * AP bus driver registration/unregistration.
  685. */
  686. static int ap_bus_match(struct device *dev, struct device_driver *drv)
  687. {
  688. struct ap_device *ap_dev = to_ap_dev(dev);
  689. struct ap_driver *ap_drv = to_ap_drv(drv);
  690. struct ap_device_id *id;
  691. /*
  692. * Compare device type of the device with the list of
  693. * supported types of the device_driver.
  694. */
  695. for (id = ap_drv->ids; id->match_flags; id++) {
  696. if ((id->match_flags & AP_DEVICE_ID_MATCH_DEVICE_TYPE) &&
  697. (id->dev_type != ap_dev->device_type))
  698. continue;
  699. return 1;
  700. }
  701. return 0;
  702. }
  703. /**
  704. * ap_uevent(): Uevent function for AP devices.
  705. * @dev: Pointer to device
  706. * @env: Pointer to kobj_uevent_env
  707. *
  708. * It sets up a single environment variable DEV_TYPE which contains the
  709. * hardware device type.
  710. */
  711. static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
  712. {
  713. struct ap_device *ap_dev = to_ap_dev(dev);
  714. int retval = 0;
  715. if (!ap_dev)
  716. return -ENODEV;
  717. /* Set up DEV_TYPE environment variable. */
  718. retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
  719. if (retval)
  720. return retval;
  721. /* Add MODALIAS= */
  722. retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
  723. return retval;
  724. }
  725. static int ap_bus_suspend(struct device *dev, pm_message_t state)
  726. {
  727. struct ap_device *ap_dev = to_ap_dev(dev);
  728. unsigned long flags;
  729. if (!ap_suspend_flag) {
  730. ap_suspend_flag = 1;
  731. /* Disable scanning for devices, thus we do not want to scan
  732. * for them after removing.
  733. */
  734. del_timer_sync(&ap_config_timer);
  735. if (ap_work_queue != NULL) {
  736. destroy_workqueue(ap_work_queue);
  737. ap_work_queue = NULL;
  738. }
  739. tasklet_disable(&ap_tasklet);
  740. }
  741. /* Poll on the device until all requests are finished. */
  742. do {
  743. flags = 0;
  744. spin_lock_bh(&ap_dev->lock);
  745. __ap_poll_device(ap_dev, &flags);
  746. spin_unlock_bh(&ap_dev->lock);
  747. } while ((flags & 1) || (flags & 2));
  748. spin_lock_bh(&ap_dev->lock);
  749. ap_dev->unregistered = 1;
  750. spin_unlock_bh(&ap_dev->lock);
  751. return 0;
  752. }
  753. static int ap_bus_resume(struct device *dev)
  754. {
  755. struct ap_device *ap_dev = to_ap_dev(dev);
  756. int rc;
  757. if (ap_suspend_flag) {
  758. ap_suspend_flag = 0;
  759. if (ap_interrupts_available()) {
  760. if (!ap_using_interrupts()) {
  761. rc = register_adapter_interrupt(&ap_airq);
  762. ap_airq_flag = (rc == 0);
  763. }
  764. } else {
  765. if (ap_using_interrupts()) {
  766. unregister_adapter_interrupt(&ap_airq);
  767. ap_airq_flag = 0;
  768. }
  769. }
  770. ap_query_configuration();
  771. if (!user_set_domain) {
  772. ap_domain_index = -1;
  773. ap_select_domain();
  774. }
  775. init_timer(&ap_config_timer);
  776. ap_config_timer.function = ap_config_timeout;
  777. ap_config_timer.data = 0;
  778. ap_config_timer.expires = jiffies + ap_config_time * HZ;
  779. add_timer(&ap_config_timer);
  780. ap_work_queue = create_singlethread_workqueue("kapwork");
  781. if (!ap_work_queue)
  782. return -ENOMEM;
  783. tasklet_enable(&ap_tasklet);
  784. if (!ap_using_interrupts())
  785. ap_schedule_poll_timer();
  786. else
  787. tasklet_schedule(&ap_tasklet);
  788. if (ap_thread_flag)
  789. rc = ap_poll_thread_start();
  790. else
  791. rc = 0;
  792. } else
  793. rc = 0;
  794. if (AP_QID_QUEUE(ap_dev->qid) != ap_domain_index) {
  795. spin_lock_bh(&ap_dev->lock);
  796. ap_dev->qid = AP_MKQID(AP_QID_DEVICE(ap_dev->qid),
  797. ap_domain_index);
  798. spin_unlock_bh(&ap_dev->lock);
  799. }
  800. queue_work(ap_work_queue, &ap_config_work);
  801. return rc;
  802. }
  803. static struct bus_type ap_bus_type = {
  804. .name = "ap",
  805. .match = &ap_bus_match,
  806. .uevent = &ap_uevent,
  807. .suspend = ap_bus_suspend,
  808. .resume = ap_bus_resume
  809. };
  810. static int ap_device_probe(struct device *dev)
  811. {
  812. struct ap_device *ap_dev = to_ap_dev(dev);
  813. struct ap_driver *ap_drv = to_ap_drv(dev->driver);
  814. int rc;
  815. ap_dev->drv = ap_drv;
  816. spin_lock_bh(&ap_device_list_lock);
  817. list_add(&ap_dev->list, &ap_device_list);
  818. spin_unlock_bh(&ap_device_list_lock);
  819. rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
  820. if (rc) {
  821. spin_lock_bh(&ap_device_list_lock);
  822. list_del_init(&ap_dev->list);
  823. spin_unlock_bh(&ap_device_list_lock);
  824. }
  825. return rc;
  826. }
  827. /**
  828. * __ap_flush_queue(): Flush requests.
  829. * @ap_dev: Pointer to the AP device
  830. *
  831. * Flush all requests from the request/pending queue of an AP device.
  832. */
  833. static void __ap_flush_queue(struct ap_device *ap_dev)
  834. {
  835. struct ap_message *ap_msg, *next;
  836. list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) {
  837. list_del_init(&ap_msg->list);
  838. ap_dev->pendingq_count--;
  839. ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
  840. }
  841. list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) {
  842. list_del_init(&ap_msg->list);
  843. ap_dev->requestq_count--;
  844. ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
  845. }
  846. }
  847. void ap_flush_queue(struct ap_device *ap_dev)
  848. {
  849. spin_lock_bh(&ap_dev->lock);
  850. __ap_flush_queue(ap_dev);
  851. spin_unlock_bh(&ap_dev->lock);
  852. }
  853. EXPORT_SYMBOL(ap_flush_queue);
  854. static int ap_device_remove(struct device *dev)
  855. {
  856. struct ap_device *ap_dev = to_ap_dev(dev);
  857. struct ap_driver *ap_drv = ap_dev->drv;
  858. ap_flush_queue(ap_dev);
  859. del_timer_sync(&ap_dev->timeout);
  860. spin_lock_bh(&ap_device_list_lock);
  861. list_del_init(&ap_dev->list);
  862. spin_unlock_bh(&ap_device_list_lock);
  863. if (ap_drv->remove)
  864. ap_drv->remove(ap_dev);
  865. spin_lock_bh(&ap_dev->lock);
  866. atomic_sub(ap_dev->queue_count, &ap_poll_requests);
  867. spin_unlock_bh(&ap_dev->lock);
  868. return 0;
  869. }
  870. int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
  871. char *name)
  872. {
  873. struct device_driver *drv = &ap_drv->driver;
  874. drv->bus = &ap_bus_type;
  875. drv->probe = ap_device_probe;
  876. drv->remove = ap_device_remove;
  877. drv->owner = owner;
  878. drv->name = name;
  879. return driver_register(drv);
  880. }
  881. EXPORT_SYMBOL(ap_driver_register);
  882. void ap_driver_unregister(struct ap_driver *ap_drv)
  883. {
  884. driver_unregister(&ap_drv->driver);
  885. }
  886. EXPORT_SYMBOL(ap_driver_unregister);
  887. void ap_bus_force_rescan(void)
  888. {
  889. /* reconfigure the AP bus rescan timer. */
  890. mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
  891. /* processing a asynchronous bus rescan */
  892. queue_work(ap_work_queue, &ap_config_work);
  893. flush_work(&ap_config_work);
  894. }
  895. EXPORT_SYMBOL(ap_bus_force_rescan);
  896. /*
  897. * AP bus attributes.
  898. */
  899. static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
  900. {
  901. return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
  902. }
  903. static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);
  904. static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
  905. {
  906. if (ap_configuration != NULL) { /* QCI not supported */
  907. if (test_facility(76)) { /* format 1 - 256 bit domain field */
  908. return snprintf(buf, PAGE_SIZE,
  909. "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
  910. ap_configuration->adm[0], ap_configuration->adm[1],
  911. ap_configuration->adm[2], ap_configuration->adm[3],
  912. ap_configuration->adm[4], ap_configuration->adm[5],
  913. ap_configuration->adm[6], ap_configuration->adm[7]);
  914. } else { /* format 0 - 16 bit domain field */
  915. return snprintf(buf, PAGE_SIZE, "%08x%08x\n",
  916. ap_configuration->adm[0], ap_configuration->adm[1]);
  917. }
  918. } else {
  919. return snprintf(buf, PAGE_SIZE, "not supported\n");
  920. }
  921. }
  922. static BUS_ATTR(ap_control_domain_mask, 0444,
  923. ap_control_domain_mask_show, NULL);
  924. static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
  925. {
  926. return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
  927. }
  928. static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
  929. {
  930. return snprintf(buf, PAGE_SIZE, "%d\n",
  931. ap_using_interrupts() ? 1 : 0);
  932. }
  933. static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
  934. static ssize_t ap_config_time_store(struct bus_type *bus,
  935. const char *buf, size_t count)
  936. {
  937. int time;
  938. if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
  939. return -EINVAL;
  940. ap_config_time = time;
  941. if (!timer_pending(&ap_config_timer) ||
  942. !mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ)) {
  943. ap_config_timer.expires = jiffies + ap_config_time * HZ;
  944. add_timer(&ap_config_timer);
  945. }
  946. return count;
  947. }
  948. static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
  949. static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
  950. {
  951. return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
  952. }
  953. static ssize_t ap_poll_thread_store(struct bus_type *bus,
  954. const char *buf, size_t count)
  955. {
  956. int flag, rc;
  957. if (sscanf(buf, "%d\n", &flag) != 1)
  958. return -EINVAL;
  959. if (flag) {
  960. rc = ap_poll_thread_start();
  961. if (rc)
  962. return rc;
  963. }
  964. else
  965. ap_poll_thread_stop();
  966. return count;
  967. }
  968. static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
  969. static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
  970. {
  971. return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
  972. }
  973. static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
  974. size_t count)
  975. {
  976. unsigned long long time;
  977. ktime_t hr_time;
  978. /* 120 seconds = maximum poll interval */
  979. if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
  980. time > 120000000000ULL)
  981. return -EINVAL;
  982. poll_timeout = time;
  983. hr_time = ktime_set(0, poll_timeout);
  984. if (!hrtimer_is_queued(&ap_poll_timer) ||
  985. !hrtimer_forward(&ap_poll_timer, hrtimer_get_expires(&ap_poll_timer), hr_time)) {
  986. hrtimer_set_expires(&ap_poll_timer, hr_time);
  987. hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
  988. }
  989. return count;
  990. }
  991. static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
  992. static struct bus_attribute *const ap_bus_attrs[] = {
  993. &bus_attr_ap_domain,
  994. &bus_attr_ap_control_domain_mask,
  995. &bus_attr_config_time,
  996. &bus_attr_poll_thread,
  997. &bus_attr_ap_interrupts,
  998. &bus_attr_poll_timeout,
  999. NULL,
  1000. };
  1001. static inline int ap_test_config(unsigned int *field, unsigned int nr)
  1002. {
  1003. if (nr > 0xFFu)
  1004. return 0;
  1005. return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
  1006. }
  1007. /*
  1008. * ap_test_config_card_id(): Test, whether an AP card ID is configured.
  1009. * @id AP card ID
  1010. *
  1011. * Returns 0 if the card is not configured
  1012. * 1 if the card is configured or
  1013. * if the configuration information is not available
  1014. */
  1015. static inline int ap_test_config_card_id(unsigned int id)
  1016. {
  1017. if (!ap_configuration)
  1018. return 1;
  1019. return ap_test_config(ap_configuration->apm, id);
  1020. }
  1021. /*
  1022. * ap_test_config_domain(): Test, whether an AP usage domain is configured.
  1023. * @domain AP usage domain ID
  1024. *
  1025. * Returns 0 if the usage domain is not configured
  1026. * 1 if the usage domain is configured or
  1027. * if the configuration information is not available
  1028. */
  1029. static inline int ap_test_config_domain(unsigned int domain)
  1030. {
  1031. if (!ap_configuration)
  1032. return 1;
  1033. return ap_test_config(ap_configuration->aqm, domain);
  1034. }
  1035. /**
  1036. * ap_query_configuration(): Query AP configuration information.
  1037. *
  1038. * Query information of installed cards and configured domains from AP.
  1039. */
  1040. static void ap_query_configuration(void)
  1041. {
  1042. #ifdef CONFIG_64BIT
  1043. if (ap_configuration_available()) {
  1044. if (!ap_configuration)
  1045. ap_configuration =
  1046. kzalloc(sizeof(struct ap_config_info),
  1047. GFP_KERNEL);
  1048. if (ap_configuration)
  1049. __ap_query_configuration(ap_configuration);
  1050. } else
  1051. ap_configuration = NULL;
  1052. #else
  1053. ap_configuration = NULL;
  1054. #endif
  1055. }
  1056. /**
  1057. * ap_select_domain(): Select an AP domain.
  1058. *
  1059. * Pick one of the 16 AP domains.
  1060. */
  1061. static int ap_select_domain(void)
  1062. {
  1063. int queue_depth, device_type, count, max_count, best_domain;
  1064. ap_qid_t qid;
  1065. int rc, i, j;
  1066. /*
  1067. * We want to use a single domain. Either the one specified with
  1068. * the "domain=" parameter or the domain with the maximum number
  1069. * of devices.
  1070. */
  1071. if (ap_domain_index >= 0 && ap_domain_index < AP_DOMAINS)
  1072. /* Domain has already been selected. */
  1073. return 0;
  1074. best_domain = -1;
  1075. max_count = 0;
  1076. for (i = 0; i < AP_DOMAINS; i++) {
  1077. if (!ap_test_config_domain(i))
  1078. continue;
  1079. count = 0;
  1080. for (j = 0; j < AP_DEVICES; j++) {
  1081. if (!ap_test_config_card_id(j))
  1082. continue;
  1083. qid = AP_MKQID(j, i);
  1084. rc = ap_query_queue(qid, &queue_depth, &device_type);
  1085. if (rc)
  1086. continue;
  1087. count++;
  1088. }
  1089. if (count > max_count) {
  1090. max_count = count;
  1091. best_domain = i;
  1092. }
  1093. }
  1094. if (best_domain >= 0){
  1095. ap_domain_index = best_domain;
  1096. return 0;
  1097. }
  1098. return -ENODEV;
  1099. }
  1100. /**
  1101. * ap_probe_device_type(): Find the device type of an AP.
  1102. * @ap_dev: pointer to the AP device.
  1103. *
  1104. * Find the device type if query queue returned a device type of 0.
  1105. */
  1106. static int ap_probe_device_type(struct ap_device *ap_dev)
  1107. {
  1108. static unsigned char msg[] = {
  1109. 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
  1110. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1111. 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
  1112. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1113. 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
  1114. 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
  1115. 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
  1116. 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
  1117. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1118. 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
  1119. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1120. 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
  1121. 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
  1122. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1123. 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
  1124. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1125. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1126. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1127. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1128. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1129. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1130. 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
  1131. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1132. 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
  1133. 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
  1134. 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
  1135. 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
  1136. 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
  1137. 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
  1138. 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
  1139. 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
  1140. 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
  1141. 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
  1142. 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
  1143. 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
  1144. 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
  1145. 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
  1146. 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
  1147. 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
  1148. 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
  1149. 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
  1150. 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
  1151. 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
  1152. 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
  1153. 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
  1154. };
  1155. struct ap_queue_status status;
  1156. unsigned long long psmid;
  1157. char *reply;
  1158. int rc, i;
  1159. reply = (void *) get_zeroed_page(GFP_KERNEL);
  1160. if (!reply) {
  1161. rc = -ENOMEM;
  1162. goto out;
  1163. }
  1164. status = __ap_send(ap_dev->qid, 0x0102030405060708ULL,
  1165. msg, sizeof(msg), 0);
  1166. if (status.response_code != AP_RESPONSE_NORMAL) {
  1167. rc = -ENODEV;
  1168. goto out_free;
  1169. }
  1170. /* Wait for the test message to complete. */
  1171. for (i = 0; i < 6; i++) {
  1172. mdelay(300);
  1173. status = __ap_recv(ap_dev->qid, &psmid, reply, 4096);
  1174. if (status.response_code == AP_RESPONSE_NORMAL &&
  1175. psmid == 0x0102030405060708ULL)
  1176. break;
  1177. }
  1178. if (i < 6) {
  1179. /* Got an answer. */
  1180. if (reply[0] == 0x00 && reply[1] == 0x86)
  1181. ap_dev->device_type = AP_DEVICE_TYPE_PCICC;
  1182. else
  1183. ap_dev->device_type = AP_DEVICE_TYPE_PCICA;
  1184. rc = 0;
  1185. } else
  1186. rc = -ENODEV;
  1187. out_free:
  1188. free_page((unsigned long) reply);
  1189. out:
  1190. return rc;
  1191. }
  1192. static void ap_interrupt_handler(struct airq_struct *airq)
  1193. {
  1194. inc_irq_stat(IRQIO_APB);
  1195. tasklet_schedule(&ap_tasklet);
  1196. }
  1197. /**
  1198. * __ap_scan_bus(): Scan the AP bus.
  1199. * @dev: Pointer to device
  1200. * @data: Pointer to data
  1201. *
  1202. * Scan the AP bus for new devices.
  1203. */
  1204. static int __ap_scan_bus(struct device *dev, void *data)
  1205. {
  1206. return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
  1207. }
  1208. static void ap_device_release(struct device *dev)
  1209. {
  1210. struct ap_device *ap_dev = to_ap_dev(dev);
  1211. kfree(ap_dev);
  1212. }
  1213. static void ap_scan_bus(struct work_struct *unused)
  1214. {
  1215. struct ap_device *ap_dev;
  1216. struct device *dev;
  1217. ap_qid_t qid;
  1218. int queue_depth, device_type;
  1219. unsigned int device_functions;
  1220. int rc, i;
  1221. ap_query_configuration();
  1222. if (ap_select_domain() != 0) {
  1223. return;
  1224. }
  1225. for (i = 0; i < AP_DEVICES; i++) {
  1226. qid = AP_MKQID(i, ap_domain_index);
  1227. dev = bus_find_device(&ap_bus_type, NULL,
  1228. (void *)(unsigned long)qid,
  1229. __ap_scan_bus);
  1230. if (ap_test_config_card_id(i))
  1231. rc = ap_query_queue(qid, &queue_depth, &device_type);
  1232. else
  1233. rc = -ENODEV;
  1234. if (dev) {
  1235. if (rc == -EBUSY) {
  1236. set_current_state(TASK_UNINTERRUPTIBLE);
  1237. schedule_timeout(AP_RESET_TIMEOUT);
  1238. rc = ap_query_queue(qid, &queue_depth,
  1239. &device_type);
  1240. }
  1241. ap_dev = to_ap_dev(dev);
  1242. spin_lock_bh(&ap_dev->lock);
  1243. if (rc || ap_dev->unregistered) {
  1244. spin_unlock_bh(&ap_dev->lock);
  1245. if (ap_dev->unregistered)
  1246. i--;
  1247. device_unregister(dev);
  1248. put_device(dev);
  1249. continue;
  1250. }
  1251. spin_unlock_bh(&ap_dev->lock);
  1252. put_device(dev);
  1253. continue;
  1254. }
  1255. if (rc)
  1256. continue;
  1257. rc = ap_init_queue(qid);
  1258. if (rc)
  1259. continue;
  1260. ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
  1261. if (!ap_dev)
  1262. break;
  1263. ap_dev->qid = qid;
  1264. ap_dev->queue_depth = queue_depth;
  1265. ap_dev->unregistered = 1;
  1266. spin_lock_init(&ap_dev->lock);
  1267. INIT_LIST_HEAD(&ap_dev->pendingq);
  1268. INIT_LIST_HEAD(&ap_dev->requestq);
  1269. INIT_LIST_HEAD(&ap_dev->list);
  1270. setup_timer(&ap_dev->timeout, ap_request_timeout,
  1271. (unsigned long) ap_dev);
  1272. switch (device_type) {
  1273. case 0:
  1274. /* device type probing for old cards */
  1275. if (ap_probe_device_type(ap_dev)) {
  1276. kfree(ap_dev);
  1277. continue;
  1278. }
  1279. break;
  1280. default:
  1281. ap_dev->device_type = device_type;
  1282. }
  1283. rc = ap_query_functions(qid, &device_functions);
  1284. if (!rc)
  1285. ap_dev->functions = device_functions;
  1286. else
  1287. ap_dev->functions = 0u;
  1288. ap_dev->device.bus = &ap_bus_type;
  1289. ap_dev->device.parent = ap_root_device;
  1290. if (dev_set_name(&ap_dev->device, "card%02x",
  1291. AP_QID_DEVICE(ap_dev->qid))) {
  1292. kfree(ap_dev);
  1293. continue;
  1294. }
  1295. ap_dev->device.release = ap_device_release;
  1296. rc = device_register(&ap_dev->device);
  1297. if (rc) {
  1298. put_device(&ap_dev->device);
  1299. continue;
  1300. }
  1301. /* Add device attributes. */
  1302. rc = sysfs_create_group(&ap_dev->device.kobj,
  1303. &ap_dev_attr_group);
  1304. if (!rc) {
  1305. spin_lock_bh(&ap_dev->lock);
  1306. ap_dev->unregistered = 0;
  1307. spin_unlock_bh(&ap_dev->lock);
  1308. }
  1309. else
  1310. device_unregister(&ap_dev->device);
  1311. }
  1312. }
  1313. static void
  1314. ap_config_timeout(unsigned long ptr)
  1315. {
  1316. queue_work(ap_work_queue, &ap_config_work);
  1317. ap_config_timer.expires = jiffies + ap_config_time * HZ;
  1318. add_timer(&ap_config_timer);
  1319. }
  1320. /**
  1321. * __ap_schedule_poll_timer(): Schedule poll timer.
  1322. *
  1323. * Set up the timer to run the poll tasklet
  1324. */
  1325. static inline void __ap_schedule_poll_timer(void)
  1326. {
  1327. ktime_t hr_time;
  1328. spin_lock_bh(&ap_poll_timer_lock);
  1329. if (hrtimer_is_queued(&ap_poll_timer) || ap_suspend_flag)
  1330. goto out;
  1331. if (ktime_to_ns(hrtimer_expires_remaining(&ap_poll_timer)) <= 0) {
  1332. hr_time = ktime_set(0, poll_timeout);
  1333. hrtimer_forward_now(&ap_poll_timer, hr_time);
  1334. hrtimer_restart(&ap_poll_timer);
  1335. }
  1336. out:
  1337. spin_unlock_bh(&ap_poll_timer_lock);
  1338. }
  1339. /**
  1340. * ap_schedule_poll_timer(): Schedule poll timer.
  1341. *
  1342. * Set up the timer to run the poll tasklet
  1343. */
  1344. static inline void ap_schedule_poll_timer(void)
  1345. {
  1346. if (ap_using_interrupts())
  1347. return;
  1348. __ap_schedule_poll_timer();
  1349. }
  1350. /**
  1351. * ap_poll_read(): Receive pending reply messages from an AP device.
  1352. * @ap_dev: pointer to the AP device
  1353. * @flags: pointer to control flags, bit 2^0 is set if another poll is
  1354. * required, bit 2^1 is set if the poll timer needs to get armed
  1355. *
  1356. * Returns 0 if the device is still present, -ENODEV if not.
  1357. */
  1358. static int ap_poll_read(struct ap_device *ap_dev, unsigned long *flags)
  1359. {
  1360. struct ap_queue_status status;
  1361. struct ap_message *ap_msg;
  1362. if (ap_dev->queue_count <= 0)
  1363. return 0;
  1364. status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
  1365. ap_dev->reply->message, ap_dev->reply->length);
  1366. switch (status.response_code) {
  1367. case AP_RESPONSE_NORMAL:
  1368. atomic_dec(&ap_poll_requests);
  1369. ap_decrease_queue_count(ap_dev);
  1370. list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
  1371. if (ap_msg->psmid != ap_dev->reply->psmid)
  1372. continue;
  1373. list_del_init(&ap_msg->list);
  1374. ap_dev->pendingq_count--;
  1375. ap_msg->receive(ap_dev, ap_msg, ap_dev->reply);
  1376. break;
  1377. }
  1378. if (ap_dev->queue_count > 0)
  1379. *flags |= 1;
  1380. break;
  1381. case AP_RESPONSE_NO_PENDING_REPLY:
  1382. if (status.queue_empty) {
  1383. /* The card shouldn't forget requests but who knows. */
  1384. atomic_sub(ap_dev->queue_count, &ap_poll_requests);
  1385. ap_dev->queue_count = 0;
  1386. list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
  1387. ap_dev->requestq_count += ap_dev->pendingq_count;
  1388. ap_dev->pendingq_count = 0;
  1389. } else
  1390. *flags |= 2;
  1391. break;
  1392. default:
  1393. return -ENODEV;
  1394. }
  1395. return 0;
  1396. }
  1397. /**
  1398. * ap_poll_write(): Send messages from the request queue to an AP device.
  1399. * @ap_dev: pointer to the AP device
  1400. * @flags: pointer to control flags, bit 2^0 is set if another poll is
  1401. * required, bit 2^1 is set if the poll timer needs to get armed
  1402. *
  1403. * Returns 0 if the device is still present, -ENODEV if not.
  1404. */
  1405. static int ap_poll_write(struct ap_device *ap_dev, unsigned long *flags)
  1406. {
  1407. struct ap_queue_status status;
  1408. struct ap_message *ap_msg;
  1409. if (ap_dev->requestq_count <= 0 ||
  1410. ap_dev->queue_count >= ap_dev->queue_depth)
  1411. return 0;
  1412. /* Start the next request on the queue. */
  1413. ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list);
  1414. status = __ap_send(ap_dev->qid, ap_msg->psmid,
  1415. ap_msg->message, ap_msg->length, ap_msg->special);
  1416. switch (status.response_code) {
  1417. case AP_RESPONSE_NORMAL:
  1418. atomic_inc(&ap_poll_requests);
  1419. ap_increase_queue_count(ap_dev);
  1420. list_move_tail(&ap_msg->list, &ap_dev->pendingq);
  1421. ap_dev->requestq_count--;
  1422. ap_dev->pendingq_count++;
  1423. if (ap_dev->queue_count < ap_dev->queue_depth &&
  1424. ap_dev->requestq_count > 0)
  1425. *flags |= 1;
  1426. *flags |= 2;
  1427. break;
  1428. case AP_RESPONSE_RESET_IN_PROGRESS:
  1429. __ap_schedule_poll_timer();
  1430. case AP_RESPONSE_Q_FULL:
  1431. *flags |= 2;
  1432. break;
  1433. case AP_RESPONSE_MESSAGE_TOO_BIG:
  1434. case AP_RESPONSE_REQ_FAC_NOT_INST:
  1435. return -EINVAL;
  1436. default:
  1437. return -ENODEV;
  1438. }
  1439. return 0;
  1440. }
  1441. /**
  1442. * ap_poll_queue(): Poll AP device for pending replies and send new messages.
  1443. * @ap_dev: pointer to the bus device
  1444. * @flags: pointer to control flags, bit 2^0 is set if another poll is
  1445. * required, bit 2^1 is set if the poll timer needs to get armed
  1446. *
  1447. * Poll AP device for pending replies and send new messages. If either
  1448. * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
  1449. * Returns 0.
  1450. */
  1451. static inline int ap_poll_queue(struct ap_device *ap_dev, unsigned long *flags)
  1452. {
  1453. int rc;
  1454. rc = ap_poll_read(ap_dev, flags);
  1455. if (rc)
  1456. return rc;
  1457. return ap_poll_write(ap_dev, flags);
  1458. }
  1459. /**
  1460. * __ap_queue_message(): Queue a message to a device.
  1461. * @ap_dev: pointer to the AP device
  1462. * @ap_msg: the message to be queued
  1463. *
  1464. * Queue a message to a device. Returns 0 if successful.
  1465. */
  1466. static int __ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
  1467. {
  1468. struct ap_queue_status status;
  1469. if (list_empty(&ap_dev->requestq) &&
  1470. ap_dev->queue_count < ap_dev->queue_depth) {
  1471. status = __ap_send(ap_dev->qid, ap_msg->psmid,
  1472. ap_msg->message, ap_msg->length,
  1473. ap_msg->special);
  1474. switch (status.response_code) {
  1475. case AP_RESPONSE_NORMAL:
  1476. list_add_tail(&ap_msg->list, &ap_dev->pendingq);
  1477. atomic_inc(&ap_poll_requests);
  1478. ap_dev->pendingq_count++;
  1479. ap_increase_queue_count(ap_dev);
  1480. ap_dev->total_request_count++;
  1481. break;
  1482. case AP_RESPONSE_Q_FULL:
  1483. case AP_RESPONSE_RESET_IN_PROGRESS:
  1484. list_add_tail(&ap_msg->list, &ap_dev->requestq);
  1485. ap_dev->requestq_count++;
  1486. ap_dev->total_request_count++;
  1487. return -EBUSY;
  1488. case AP_RESPONSE_REQ_FAC_NOT_INST:
  1489. case AP_RESPONSE_MESSAGE_TOO_BIG:
  1490. ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-EINVAL));
  1491. return -EINVAL;
  1492. default: /* Device is gone. */
  1493. ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
  1494. return -ENODEV;
  1495. }
  1496. } else {
  1497. list_add_tail(&ap_msg->list, &ap_dev->requestq);
  1498. ap_dev->requestq_count++;
  1499. ap_dev->total_request_count++;
  1500. return -EBUSY;
  1501. }
  1502. ap_schedule_poll_timer();
  1503. return 0;
  1504. }
  1505. void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
  1506. {
  1507. unsigned long flags;
  1508. int rc;
  1509. /* For asynchronous message handling a valid receive-callback
  1510. * is required. */
  1511. BUG_ON(!ap_msg->receive);
  1512. spin_lock_bh(&ap_dev->lock);
  1513. if (!ap_dev->unregistered) {
  1514. /* Make room on the queue by polling for finished requests. */
  1515. rc = ap_poll_queue(ap_dev, &flags);
  1516. if (!rc)
  1517. rc = __ap_queue_message(ap_dev, ap_msg);
  1518. if (!rc)
  1519. wake_up(&ap_poll_wait);
  1520. if (rc == -ENODEV)
  1521. ap_dev->unregistered = 1;
  1522. } else {
  1523. ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
  1524. rc = -ENODEV;
  1525. }
  1526. spin_unlock_bh(&ap_dev->lock);
  1527. if (rc == -ENODEV)
  1528. device_unregister(&ap_dev->device);
  1529. }
  1530. EXPORT_SYMBOL(ap_queue_message);
  1531. /**
  1532. * ap_cancel_message(): Cancel a crypto request.
  1533. * @ap_dev: The AP device that has the message queued
  1534. * @ap_msg: The message that is to be removed
  1535. *
  1536. * Cancel a crypto request. This is done by removing the request
  1537. * from the device pending or request queue. Note that the
  1538. * request stays on the AP queue. When it finishes the message
  1539. * reply will be discarded because the psmid can't be found.
  1540. */
  1541. void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
  1542. {
  1543. struct ap_message *tmp;
  1544. spin_lock_bh(&ap_dev->lock);
  1545. if (!list_empty(&ap_msg->list)) {
  1546. list_for_each_entry(tmp, &ap_dev->pendingq, list)
  1547. if (tmp->psmid == ap_msg->psmid) {
  1548. ap_dev->pendingq_count--;
  1549. goto found;
  1550. }
  1551. ap_dev->requestq_count--;
  1552. found:
  1553. list_del_init(&ap_msg->list);
  1554. }
  1555. spin_unlock_bh(&ap_dev->lock);
  1556. }
  1557. EXPORT_SYMBOL(ap_cancel_message);
  1558. /**
  1559. * ap_poll_timeout(): AP receive polling for finished AP requests.
  1560. * @unused: Unused pointer.
  1561. *
  1562. * Schedules the AP tasklet using a high resolution timer.
  1563. */
  1564. static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
  1565. {
  1566. tasklet_schedule(&ap_tasklet);
  1567. return HRTIMER_NORESTART;
  1568. }
  1569. /**
  1570. * ap_reset(): Reset a not responding AP device.
  1571. * @ap_dev: Pointer to the AP device
  1572. *
  1573. * Reset a not responding AP device and move all requests from the
  1574. * pending queue to the request queue.
  1575. */
  1576. static void ap_reset(struct ap_device *ap_dev)
  1577. {
  1578. int rc;
  1579. ap_dev->reset = AP_RESET_IGNORE;
  1580. atomic_sub(ap_dev->queue_count, &ap_poll_requests);
  1581. ap_dev->queue_count = 0;
  1582. list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
  1583. ap_dev->requestq_count += ap_dev->pendingq_count;
  1584. ap_dev->pendingq_count = 0;
  1585. rc = ap_init_queue(ap_dev->qid);
  1586. if (rc == -ENODEV)
  1587. ap_dev->unregistered = 1;
  1588. else
  1589. __ap_schedule_poll_timer();
  1590. }
  1591. static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags)
  1592. {
  1593. if (!ap_dev->unregistered) {
  1594. if (ap_poll_queue(ap_dev, flags))
  1595. ap_dev->unregistered = 1;
  1596. if (ap_dev->reset == AP_RESET_DO)
  1597. ap_reset(ap_dev);
  1598. }
  1599. return 0;
  1600. }
  1601. /**
  1602. * ap_poll_all(): Poll all AP devices.
  1603. * @dummy: Unused variable
  1604. *
  1605. * Poll all AP devices on the bus in a round robin fashion. Continue
  1606. * polling until bit 2^0 of the control flags is not set. If bit 2^1
  1607. * of the control flags has been set arm the poll timer.
  1608. */
  1609. static void ap_poll_all(unsigned long dummy)
  1610. {
  1611. unsigned long flags;
  1612. struct ap_device *ap_dev;
  1613. /* Reset the indicator if interrupts are used. Thus new interrupts can
  1614. * be received. Doing it in the beginning of the tasklet is therefor
  1615. * important that no requests on any AP get lost.
  1616. */
  1617. if (ap_using_interrupts())
  1618. xchg(ap_airq.lsi_ptr, 0);
  1619. do {
  1620. flags = 0;
  1621. spin_lock(&ap_device_list_lock);
  1622. list_for_each_entry(ap_dev, &ap_device_list, list) {
  1623. spin_lock(&ap_dev->lock);
  1624. __ap_poll_device(ap_dev, &flags);
  1625. spin_unlock(&ap_dev->lock);
  1626. }
  1627. spin_unlock(&ap_device_list_lock);
  1628. } while (flags & 1);
  1629. if (flags & 2)
  1630. ap_schedule_poll_timer();
  1631. }
  1632. /**
  1633. * ap_poll_thread(): Thread that polls for finished requests.
  1634. * @data: Unused pointer
  1635. *
  1636. * AP bus poll thread. The purpose of this thread is to poll for
  1637. * finished requests in a loop if there is a "free" cpu - that is
  1638. * a cpu that doesn't have anything better to do. The polling stops
  1639. * as soon as there is another task or if all messages have been
  1640. * delivered.
  1641. */
  1642. static int ap_poll_thread(void *data)
  1643. {
  1644. DECLARE_WAITQUEUE(wait, current);
  1645. unsigned long flags;
  1646. int requests;
  1647. struct ap_device *ap_dev;
  1648. set_user_nice(current, MAX_NICE);
  1649. while (1) {
  1650. if (ap_suspend_flag)
  1651. return 0;
  1652. if (need_resched()) {
  1653. schedule();
  1654. continue;
  1655. }
  1656. add_wait_queue(&ap_poll_wait, &wait);
  1657. set_current_state(TASK_INTERRUPTIBLE);
  1658. if (kthread_should_stop())
  1659. break;
  1660. requests = atomic_read(&ap_poll_requests);
  1661. if (requests <= 0)
  1662. schedule();
  1663. set_current_state(TASK_RUNNING);
  1664. remove_wait_queue(&ap_poll_wait, &wait);
  1665. flags = 0;
  1666. spin_lock_bh(&ap_device_list_lock);
  1667. list_for_each_entry(ap_dev, &ap_device_list, list) {
  1668. spin_lock(&ap_dev->lock);
  1669. __ap_poll_device(ap_dev, &flags);
  1670. spin_unlock(&ap_dev->lock);
  1671. }
  1672. spin_unlock_bh(&ap_device_list_lock);
  1673. }
  1674. set_current_state(TASK_RUNNING);
  1675. remove_wait_queue(&ap_poll_wait, &wait);
  1676. return 0;
  1677. }
  1678. static int ap_poll_thread_start(void)
  1679. {
  1680. int rc;
  1681. if (ap_using_interrupts() || ap_suspend_flag)
  1682. return 0;
  1683. mutex_lock(&ap_poll_thread_mutex);
  1684. if (!ap_poll_kthread) {
  1685. ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
  1686. rc = PTR_RET(ap_poll_kthread);
  1687. if (rc)
  1688. ap_poll_kthread = NULL;
  1689. }
  1690. else
  1691. rc = 0;
  1692. mutex_unlock(&ap_poll_thread_mutex);
  1693. return rc;
  1694. }
  1695. static void ap_poll_thread_stop(void)
  1696. {
  1697. mutex_lock(&ap_poll_thread_mutex);
  1698. if (ap_poll_kthread) {
  1699. kthread_stop(ap_poll_kthread);
  1700. ap_poll_kthread = NULL;
  1701. }
  1702. mutex_unlock(&ap_poll_thread_mutex);
  1703. }
  1704. /**
  1705. * ap_request_timeout(): Handling of request timeouts
  1706. * @data: Holds the AP device.
  1707. *
  1708. * Handles request timeouts.
  1709. */
  1710. static void ap_request_timeout(unsigned long data)
  1711. {
  1712. struct ap_device *ap_dev = (struct ap_device *) data;
  1713. if (ap_dev->reset == AP_RESET_ARMED) {
  1714. ap_dev->reset = AP_RESET_DO;
  1715. if (ap_using_interrupts())
  1716. tasklet_schedule(&ap_tasklet);
  1717. }
  1718. }
  1719. static void ap_reset_domain(void)
  1720. {
  1721. int i;
  1722. if (ap_domain_index != -1)
  1723. for (i = 0; i < AP_DEVICES; i++)
  1724. ap_reset_queue(AP_MKQID(i, ap_domain_index));
  1725. }
  1726. static void ap_reset_all(void)
  1727. {
  1728. int i, j;
  1729. for (i = 0; i < AP_DOMAINS; i++)
  1730. for (j = 0; j < AP_DEVICES; j++)
  1731. ap_reset_queue(AP_MKQID(j, i));
  1732. }
  1733. static struct reset_call ap_reset_call = {
  1734. .fn = ap_reset_all,
  1735. };
  1736. /**
  1737. * ap_module_init(): The module initialization code.
  1738. *
  1739. * Initializes the module.
  1740. */
  1741. int __init ap_module_init(void)
  1742. {
  1743. int rc, i;
  1744. if (ap_domain_index < -1 || ap_domain_index >= AP_DOMAINS) {
  1745. pr_warning("%d is not a valid cryptographic domain\n",
  1746. ap_domain_index);
  1747. return -EINVAL;
  1748. }
  1749. /* In resume callback we need to know if the user had set the domain.
  1750. * If so, we can not just reset it.
  1751. */
  1752. if (ap_domain_index >= 0)
  1753. user_set_domain = 1;
  1754. if (ap_instructions_available() != 0) {
  1755. pr_warning("The hardware system does not support "
  1756. "AP instructions\n");
  1757. return -ENODEV;
  1758. }
  1759. if (ap_interrupts_available()) {
  1760. rc = register_adapter_interrupt(&ap_airq);
  1761. ap_airq_flag = (rc == 0);
  1762. }
  1763. register_reset_call(&ap_reset_call);
  1764. /* Create /sys/bus/ap. */
  1765. rc = bus_register(&ap_bus_type);
  1766. if (rc)
  1767. goto out;
  1768. for (i = 0; ap_bus_attrs[i]; i++) {
  1769. rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
  1770. if (rc)
  1771. goto out_bus;
  1772. }
  1773. /* Create /sys/devices/ap. */
  1774. ap_root_device = root_device_register("ap");
  1775. rc = PTR_RET(ap_root_device);
  1776. if (rc)
  1777. goto out_bus;
  1778. ap_work_queue = create_singlethread_workqueue("kapwork");
  1779. if (!ap_work_queue) {
  1780. rc = -ENOMEM;
  1781. goto out_root;
  1782. }
  1783. ap_query_configuration();
  1784. if (ap_select_domain() == 0)
  1785. ap_scan_bus(NULL);
  1786. /* Setup the AP bus rescan timer. */
  1787. init_timer(&ap_config_timer);
  1788. ap_config_timer.function = ap_config_timeout;
  1789. ap_config_timer.data = 0;
  1790. ap_config_timer.expires = jiffies + ap_config_time * HZ;
  1791. add_timer(&ap_config_timer);
  1792. /* Setup the high resultion poll timer.
  1793. * If we are running under z/VM adjust polling to z/VM polling rate.
  1794. */
  1795. if (MACHINE_IS_VM)
  1796. poll_timeout = 1500000;
  1797. spin_lock_init(&ap_poll_timer_lock);
  1798. hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  1799. ap_poll_timer.function = ap_poll_timeout;
  1800. /* Start the low priority AP bus poll thread. */
  1801. if (ap_thread_flag) {
  1802. rc = ap_poll_thread_start();
  1803. if (rc)
  1804. goto out_work;
  1805. }
  1806. return 0;
  1807. out_work:
  1808. del_timer_sync(&ap_config_timer);
  1809. hrtimer_cancel(&ap_poll_timer);
  1810. destroy_workqueue(ap_work_queue);
  1811. out_root:
  1812. root_device_unregister(ap_root_device);
  1813. out_bus:
  1814. while (i--)
  1815. bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
  1816. bus_unregister(&ap_bus_type);
  1817. out:
  1818. unregister_reset_call(&ap_reset_call);
  1819. if (ap_using_interrupts())
  1820. unregister_adapter_interrupt(&ap_airq);
  1821. return rc;
  1822. }
  1823. static int __ap_match_all(struct device *dev, void *data)
  1824. {
  1825. return 1;
  1826. }
  1827. /**
  1828. * ap_modules_exit(): The module termination code
  1829. *
  1830. * Terminates the module.
  1831. */
  1832. void ap_module_exit(void)
  1833. {
  1834. int i;
  1835. struct device *dev;
  1836. ap_reset_domain();
  1837. ap_poll_thread_stop();
  1838. del_timer_sync(&ap_config_timer);
  1839. hrtimer_cancel(&ap_poll_timer);
  1840. destroy_workqueue(ap_work_queue);
  1841. tasklet_kill(&ap_tasklet);
  1842. root_device_unregister(ap_root_device);
  1843. while ((dev = bus_find_device(&ap_bus_type, NULL, NULL,
  1844. __ap_match_all)))
  1845. {
  1846. device_unregister(dev);
  1847. put_device(dev);
  1848. }
  1849. for (i = 0; ap_bus_attrs[i]; i++)
  1850. bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
  1851. bus_unregister(&ap_bus_type);
  1852. unregister_reset_call(&ap_reset_call);
  1853. if (ap_using_interrupts())
  1854. unregister_adapter_interrupt(&ap_airq);
  1855. }
  1856. module_init(ap_module_init);
  1857. module_exit(ap_module_exit);