ap_bus.c 54 KB

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