ap_bus.c 56 KB

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