ap_bus.c 52 KB

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