ap_bus.c 49 KB

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