ap_bus.c 53 KB

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