ap_bus.c 54 KB

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