ap_bus.c 52 KB

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