ap_bus.c 51 KB

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