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