ap_bus.c 55 KB

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