ap_bus.c 48 KB

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