ap_bus.c 48 KB

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