ap_bus.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116
  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. static int ap_configuration_available(void)
  160. {
  161. return test_facility(2) && test_facility(12);
  162. }
  163. /**
  164. * ap_test_queue(): Test adjunct processor queue.
  165. * @qid: The AP queue number
  166. * @queue_depth: Pointer to queue depth value
  167. * @device_type: Pointer to device type value
  168. *
  169. * Returns AP queue status structure.
  170. */
  171. static inline struct ap_queue_status
  172. ap_test_queue(ap_qid_t qid, int *queue_depth, int *device_type)
  173. {
  174. register unsigned long reg0 asm ("0") = qid;
  175. register struct ap_queue_status reg1 asm ("1");
  176. register unsigned long reg2 asm ("2") = 0UL;
  177. asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */
  178. : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
  179. *device_type = (int) (reg2 >> 24);
  180. *queue_depth = (int) (reg2 & 0xff);
  181. return reg1;
  182. }
  183. /**
  184. * ap_query_facilities(): PQAP(TAPQ) query facilities.
  185. * @qid: The AP queue number
  186. *
  187. * Returns content of general register 2 after the PQAP(TAPQ)
  188. * instruction was called.
  189. */
  190. static inline unsigned long ap_query_facilities(ap_qid_t qid)
  191. {
  192. register unsigned long reg0 asm ("0") = qid | 0x00800000UL;
  193. register unsigned long reg1 asm ("1");
  194. register unsigned long reg2 asm ("2") = 0UL;
  195. asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */
  196. : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
  197. return reg2;
  198. }
  199. /**
  200. * ap_reset_queue(): Reset adjunct processor queue.
  201. * @qid: The AP queue number
  202. *
  203. * Returns AP queue status structure.
  204. */
  205. static inline struct ap_queue_status ap_reset_queue(ap_qid_t qid)
  206. {
  207. register unsigned long reg0 asm ("0") = qid | 0x01000000UL;
  208. register struct ap_queue_status reg1 asm ("1");
  209. register unsigned long reg2 asm ("2") = 0UL;
  210. asm volatile(
  211. ".long 0xb2af0000" /* PQAP(RAPQ) */
  212. : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
  213. return reg1;
  214. }
  215. /**
  216. * ap_queue_interruption_control(): Enable interruption for a specific AP.
  217. * @qid: The AP queue number
  218. * @ind: The notification indicator byte
  219. *
  220. * Returns AP queue status.
  221. */
  222. static inline struct ap_queue_status
  223. ap_queue_interruption_control(ap_qid_t qid, void *ind)
  224. {
  225. register unsigned long reg0 asm ("0") = qid | 0x03000000UL;
  226. register unsigned long reg1_in asm ("1") = 0x0000800000000000UL | AP_ISC;
  227. register struct ap_queue_status reg1_out asm ("1");
  228. register void *reg2 asm ("2") = ind;
  229. asm volatile(
  230. ".long 0xb2af0000" /* PQAP(AQIC) */
  231. : "+d" (reg0), "+d" (reg1_in), "=d" (reg1_out), "+d" (reg2)
  232. :
  233. : "cc" );
  234. return reg1_out;
  235. }
  236. static inline struct ap_queue_status
  237. __ap_query_functions(ap_qid_t qid, unsigned int *functions)
  238. {
  239. register unsigned long reg0 asm ("0") = 0UL | qid | (1UL << 23);
  240. register struct ap_queue_status reg1 asm ("1") = AP_QUEUE_STATUS_INVALID;
  241. register unsigned long reg2 asm ("2");
  242. asm volatile(
  243. ".long 0xb2af0000\n" /* PQAP(TAPQ) */
  244. "0:\n"
  245. EX_TABLE(0b, 0b)
  246. : "+d" (reg0), "+d" (reg1), "=d" (reg2)
  247. :
  248. : "cc");
  249. *functions = (unsigned int)(reg2 >> 32);
  250. return reg1;
  251. }
  252. static inline int __ap_query_configuration(struct ap_config_info *config)
  253. {
  254. register unsigned long reg0 asm ("0") = 0x04000000UL;
  255. register unsigned long reg1 asm ("1") = -EINVAL;
  256. register unsigned char *reg2 asm ("2") = (unsigned char *)config;
  257. asm volatile(
  258. ".long 0xb2af0000\n" /* PQAP(QCI) */
  259. "0: la %1,0\n"
  260. "1:\n"
  261. EX_TABLE(0b, 1b)
  262. : "+d" (reg0), "+d" (reg1), "+d" (reg2)
  263. :
  264. : "cc");
  265. return reg1;
  266. }
  267. /**
  268. * ap_query_functions(): Query supported functions.
  269. * @qid: The AP queue number
  270. * @functions: Pointer to functions field.
  271. *
  272. * Returns
  273. * 0 on success.
  274. * -ENODEV if queue not valid.
  275. * -EBUSY if device busy.
  276. * -EINVAL if query function is not supported
  277. */
  278. static int ap_query_functions(ap_qid_t qid, unsigned int *functions)
  279. {
  280. struct ap_queue_status status;
  281. int i;
  282. status = __ap_query_functions(qid, functions);
  283. for (i = 0; i < AP_MAX_RESET; i++) {
  284. if (ap_queue_status_invalid_test(&status))
  285. return -ENODEV;
  286. switch (status.response_code) {
  287. case AP_RESPONSE_NORMAL:
  288. return 0;
  289. case AP_RESPONSE_RESET_IN_PROGRESS:
  290. case AP_RESPONSE_BUSY:
  291. break;
  292. case AP_RESPONSE_Q_NOT_AVAIL:
  293. case AP_RESPONSE_DECONFIGURED:
  294. case AP_RESPONSE_CHECKSTOPPED:
  295. case AP_RESPONSE_INVALID_ADDRESS:
  296. return -ENODEV;
  297. case AP_RESPONSE_OTHERWISE_CHANGED:
  298. break;
  299. default:
  300. break;
  301. }
  302. if (i < AP_MAX_RESET - 1) {
  303. udelay(5);
  304. status = __ap_query_functions(qid, functions);
  305. }
  306. }
  307. return -EBUSY;
  308. }
  309. /**
  310. * ap_queue_enable_interruption(): Enable interruption on an AP.
  311. * @qid: The AP queue number
  312. * @ind: the notification indicator byte
  313. *
  314. * Enables interruption on AP queue via ap_queue_interruption_control(). Based
  315. * on the return value it waits a while and tests the AP queue if interrupts
  316. * have been switched on using ap_test_queue().
  317. */
  318. static int ap_queue_enable_interruption(ap_qid_t qid, void *ind)
  319. {
  320. struct ap_queue_status status;
  321. int t_depth, t_device_type, rc, i;
  322. rc = -EBUSY;
  323. status = ap_queue_interruption_control(qid, ind);
  324. for (i = 0; i < AP_MAX_RESET; i++) {
  325. switch (status.response_code) {
  326. case AP_RESPONSE_NORMAL:
  327. if (status.int_enabled)
  328. return 0;
  329. break;
  330. case AP_RESPONSE_RESET_IN_PROGRESS:
  331. case AP_RESPONSE_BUSY:
  332. if (i < AP_MAX_RESET - 1) {
  333. udelay(5);
  334. status = ap_queue_interruption_control(qid,
  335. ind);
  336. continue;
  337. }
  338. break;
  339. case AP_RESPONSE_Q_NOT_AVAIL:
  340. case AP_RESPONSE_DECONFIGURED:
  341. case AP_RESPONSE_CHECKSTOPPED:
  342. case AP_RESPONSE_INVALID_ADDRESS:
  343. return -ENODEV;
  344. case AP_RESPONSE_OTHERWISE_CHANGED:
  345. if (status.int_enabled)
  346. return 0;
  347. break;
  348. default:
  349. break;
  350. }
  351. if (i < AP_MAX_RESET - 1) {
  352. udelay(5);
  353. status = ap_test_queue(qid, &t_depth, &t_device_type);
  354. }
  355. }
  356. return rc;
  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_test_config(): helper function to extract the nrth bit
  907. * within the unsigned int array field.
  908. */
  909. static inline int ap_test_config(unsigned int *field, unsigned int nr)
  910. {
  911. if (nr > 0xFFu)
  912. return 0;
  913. return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
  914. }
  915. /*
  916. * ap_test_config_card_id(): Test, whether an AP card ID is configured.
  917. * @id AP card ID
  918. *
  919. * Returns 0 if the card is not configured
  920. * 1 if the card is configured or
  921. * if the configuration information is not available
  922. */
  923. static inline int ap_test_config_card_id(unsigned int id)
  924. {
  925. if (!ap_configuration)
  926. return 1;
  927. return ap_test_config(ap_configuration->apm, id);
  928. }
  929. /*
  930. * ap_test_config_domain(): Test, whether an AP usage domain is configured.
  931. * @domain AP usage domain ID
  932. *
  933. * Returns 0 if the usage domain is not configured
  934. * 1 if the usage domain is configured or
  935. * if the configuration information is not available
  936. */
  937. static inline int ap_test_config_domain(unsigned int domain)
  938. {
  939. if (!ap_configuration) /* QCI not supported */
  940. if (domain < 16)
  941. return 1; /* then domains 0...15 are configured */
  942. else
  943. return 0;
  944. else
  945. return ap_test_config(ap_configuration->aqm, domain);
  946. }
  947. /*
  948. * AP bus attributes.
  949. */
  950. static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
  951. {
  952. return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
  953. }
  954. static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);
  955. static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
  956. {
  957. if (ap_configuration != NULL) { /* QCI not supported */
  958. if (test_facility(76)) { /* format 1 - 256 bit domain field */
  959. return snprintf(buf, PAGE_SIZE,
  960. "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
  961. ap_configuration->adm[0], ap_configuration->adm[1],
  962. ap_configuration->adm[2], ap_configuration->adm[3],
  963. ap_configuration->adm[4], ap_configuration->adm[5],
  964. ap_configuration->adm[6], ap_configuration->adm[7]);
  965. } else { /* format 0 - 16 bit domain field */
  966. return snprintf(buf, PAGE_SIZE, "%08x%08x\n",
  967. ap_configuration->adm[0], ap_configuration->adm[1]);
  968. }
  969. } else {
  970. return snprintf(buf, PAGE_SIZE, "not supported\n");
  971. }
  972. }
  973. static BUS_ATTR(ap_control_domain_mask, 0444,
  974. ap_control_domain_mask_show, NULL);
  975. static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
  976. {
  977. return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
  978. }
  979. static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
  980. {
  981. return snprintf(buf, PAGE_SIZE, "%d\n",
  982. ap_using_interrupts() ? 1 : 0);
  983. }
  984. static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
  985. static ssize_t ap_config_time_store(struct bus_type *bus,
  986. const char *buf, size_t count)
  987. {
  988. int time;
  989. if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
  990. return -EINVAL;
  991. ap_config_time = time;
  992. if (!timer_pending(&ap_config_timer) ||
  993. !mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ)) {
  994. ap_config_timer.expires = jiffies + ap_config_time * HZ;
  995. add_timer(&ap_config_timer);
  996. }
  997. return count;
  998. }
  999. static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
  1000. static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
  1001. {
  1002. return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
  1003. }
  1004. static ssize_t ap_poll_thread_store(struct bus_type *bus,
  1005. const char *buf, size_t count)
  1006. {
  1007. int flag, rc;
  1008. if (sscanf(buf, "%d\n", &flag) != 1)
  1009. return -EINVAL;
  1010. if (flag) {
  1011. rc = ap_poll_thread_start();
  1012. if (rc)
  1013. return rc;
  1014. }
  1015. else
  1016. ap_poll_thread_stop();
  1017. return count;
  1018. }
  1019. static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
  1020. static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
  1021. {
  1022. return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
  1023. }
  1024. static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
  1025. size_t count)
  1026. {
  1027. unsigned long long time;
  1028. ktime_t hr_time;
  1029. /* 120 seconds = maximum poll interval */
  1030. if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
  1031. time > 120000000000ULL)
  1032. return -EINVAL;
  1033. poll_timeout = time;
  1034. hr_time = ktime_set(0, poll_timeout);
  1035. if (!hrtimer_is_queued(&ap_poll_timer) ||
  1036. !hrtimer_forward(&ap_poll_timer, hrtimer_get_expires(&ap_poll_timer), hr_time)) {
  1037. hrtimer_set_expires(&ap_poll_timer, hr_time);
  1038. hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
  1039. }
  1040. return count;
  1041. }
  1042. static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
  1043. static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
  1044. {
  1045. ap_qid_t qid;
  1046. int i, nd, max_domain_id = -1;
  1047. unsigned long fbits;
  1048. if (ap_configuration) {
  1049. if (ap_domain_index >= 0 && ap_domain_index < AP_DOMAINS) {
  1050. for (i = 0; i < AP_DEVICES; i++) {
  1051. if (!ap_test_config_card_id(i))
  1052. continue;
  1053. qid = AP_MKQID(i, ap_domain_index);
  1054. fbits = ap_query_facilities(qid);
  1055. if (fbits & (1UL << 57)) {
  1056. /* the N bit is 0, Nd field is filled */
  1057. nd = (int)((fbits & 0x00FF0000UL)>>16);
  1058. if (nd > 0)
  1059. max_domain_id = nd;
  1060. else
  1061. max_domain_id = 15;
  1062. } else {
  1063. /* N bit is 1, max 16 domains */
  1064. max_domain_id = 15;
  1065. }
  1066. break;
  1067. }
  1068. }
  1069. } else {
  1070. /* no APXA support, older machines with max 16 domains */
  1071. max_domain_id = 15;
  1072. }
  1073. return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
  1074. }
  1075. static BUS_ATTR(ap_max_domain_id, 0444, ap_max_domain_id_show, NULL);
  1076. static struct bus_attribute *const ap_bus_attrs[] = {
  1077. &bus_attr_ap_domain,
  1078. &bus_attr_ap_control_domain_mask,
  1079. &bus_attr_config_time,
  1080. &bus_attr_poll_thread,
  1081. &bus_attr_ap_interrupts,
  1082. &bus_attr_poll_timeout,
  1083. &bus_attr_ap_max_domain_id,
  1084. NULL,
  1085. };
  1086. /**
  1087. * ap_query_configuration(): Query AP configuration information.
  1088. *
  1089. * Query information of installed cards and configured domains from AP.
  1090. */
  1091. static void ap_query_configuration(void)
  1092. {
  1093. if (ap_configuration_available()) {
  1094. if (!ap_configuration)
  1095. ap_configuration =
  1096. kzalloc(sizeof(struct ap_config_info),
  1097. GFP_KERNEL);
  1098. if (ap_configuration)
  1099. __ap_query_configuration(ap_configuration);
  1100. } else
  1101. ap_configuration = NULL;
  1102. }
  1103. /**
  1104. * ap_select_domain(): Select an AP domain.
  1105. *
  1106. * Pick one of the 16 AP domains.
  1107. */
  1108. static int ap_select_domain(void)
  1109. {
  1110. int queue_depth, device_type, count, max_count, best_domain;
  1111. ap_qid_t qid;
  1112. int rc, i, j;
  1113. /* IF APXA isn't installed, only 16 domains could be defined */
  1114. if (!ap_configuration->ap_extended && (ap_domain_index > 15))
  1115. return -EINVAL;
  1116. /*
  1117. * We want to use a single domain. Either the one specified with
  1118. * the "domain=" parameter or the domain with the maximum number
  1119. * of devices.
  1120. */
  1121. if (ap_domain_index >= 0 && ap_domain_index < AP_DOMAINS)
  1122. /* Domain has already been selected. */
  1123. return 0;
  1124. best_domain = -1;
  1125. max_count = 0;
  1126. for (i = 0; i < AP_DOMAINS; i++) {
  1127. if (!ap_test_config_domain(i))
  1128. continue;
  1129. count = 0;
  1130. for (j = 0; j < AP_DEVICES; j++) {
  1131. if (!ap_test_config_card_id(j))
  1132. continue;
  1133. qid = AP_MKQID(j, i);
  1134. rc = ap_query_queue(qid, &queue_depth, &device_type);
  1135. if (rc)
  1136. continue;
  1137. count++;
  1138. }
  1139. if (count > max_count) {
  1140. max_count = count;
  1141. best_domain = i;
  1142. }
  1143. }
  1144. if (best_domain >= 0){
  1145. ap_domain_index = best_domain;
  1146. return 0;
  1147. }
  1148. return -ENODEV;
  1149. }
  1150. /**
  1151. * ap_probe_device_type(): Find the device type of an AP.
  1152. * @ap_dev: pointer to the AP device.
  1153. *
  1154. * Find the device type if query queue returned a device type of 0.
  1155. */
  1156. static int ap_probe_device_type(struct ap_device *ap_dev)
  1157. {
  1158. static unsigned char msg[] = {
  1159. 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
  1160. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1161. 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
  1162. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1163. 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
  1164. 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
  1165. 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
  1166. 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
  1167. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1168. 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
  1169. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1170. 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
  1171. 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
  1172. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1173. 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
  1174. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1175. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1176. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1177. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1178. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1179. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1180. 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
  1181. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1182. 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
  1183. 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
  1184. 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
  1185. 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
  1186. 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
  1187. 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
  1188. 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
  1189. 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
  1190. 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
  1191. 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
  1192. 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
  1193. 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
  1194. 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
  1195. 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
  1196. 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
  1197. 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
  1198. 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
  1199. 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
  1200. 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
  1201. 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
  1202. 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
  1203. 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
  1204. };
  1205. struct ap_queue_status status;
  1206. unsigned long long psmid;
  1207. char *reply;
  1208. int rc, i;
  1209. reply = (void *) get_zeroed_page(GFP_KERNEL);
  1210. if (!reply) {
  1211. rc = -ENOMEM;
  1212. goto out;
  1213. }
  1214. status = __ap_send(ap_dev->qid, 0x0102030405060708ULL,
  1215. msg, sizeof(msg), 0);
  1216. if (status.response_code != AP_RESPONSE_NORMAL) {
  1217. rc = -ENODEV;
  1218. goto out_free;
  1219. }
  1220. /* Wait for the test message to complete. */
  1221. for (i = 0; i < 6; i++) {
  1222. mdelay(300);
  1223. status = __ap_recv(ap_dev->qid, &psmid, reply, 4096);
  1224. if (status.response_code == AP_RESPONSE_NORMAL &&
  1225. psmid == 0x0102030405060708ULL)
  1226. break;
  1227. }
  1228. if (i < 6) {
  1229. /* Got an answer. */
  1230. if (reply[0] == 0x00 && reply[1] == 0x86)
  1231. ap_dev->device_type = AP_DEVICE_TYPE_PCICC;
  1232. else
  1233. ap_dev->device_type = AP_DEVICE_TYPE_PCICA;
  1234. rc = 0;
  1235. } else
  1236. rc = -ENODEV;
  1237. out_free:
  1238. free_page((unsigned long) reply);
  1239. out:
  1240. return rc;
  1241. }
  1242. static void ap_interrupt_handler(struct airq_struct *airq)
  1243. {
  1244. inc_irq_stat(IRQIO_APB);
  1245. tasklet_schedule(&ap_tasklet);
  1246. }
  1247. /**
  1248. * __ap_scan_bus(): Scan the AP bus.
  1249. * @dev: Pointer to device
  1250. * @data: Pointer to data
  1251. *
  1252. * Scan the AP bus for new devices.
  1253. */
  1254. static int __ap_scan_bus(struct device *dev, void *data)
  1255. {
  1256. return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
  1257. }
  1258. static void ap_device_release(struct device *dev)
  1259. {
  1260. struct ap_device *ap_dev = to_ap_dev(dev);
  1261. kfree(ap_dev);
  1262. }
  1263. static void ap_scan_bus(struct work_struct *unused)
  1264. {
  1265. struct ap_device *ap_dev;
  1266. struct device *dev;
  1267. ap_qid_t qid;
  1268. int queue_depth, device_type;
  1269. unsigned int device_functions;
  1270. int rc, i;
  1271. ap_query_configuration();
  1272. if (ap_select_domain() != 0) {
  1273. return;
  1274. }
  1275. for (i = 0; i < AP_DEVICES; i++) {
  1276. qid = AP_MKQID(i, ap_domain_index);
  1277. dev = bus_find_device(&ap_bus_type, NULL,
  1278. (void *)(unsigned long)qid,
  1279. __ap_scan_bus);
  1280. if (ap_test_config_card_id(i))
  1281. rc = ap_query_queue(qid, &queue_depth, &device_type);
  1282. else
  1283. rc = -ENODEV;
  1284. if (dev) {
  1285. if (rc == -EBUSY) {
  1286. set_current_state(TASK_UNINTERRUPTIBLE);
  1287. schedule_timeout(AP_RESET_TIMEOUT);
  1288. rc = ap_query_queue(qid, &queue_depth,
  1289. &device_type);
  1290. }
  1291. ap_dev = to_ap_dev(dev);
  1292. spin_lock_bh(&ap_dev->lock);
  1293. if (rc || ap_dev->unregistered) {
  1294. spin_unlock_bh(&ap_dev->lock);
  1295. if (ap_dev->unregistered)
  1296. i--;
  1297. device_unregister(dev);
  1298. put_device(dev);
  1299. continue;
  1300. }
  1301. spin_unlock_bh(&ap_dev->lock);
  1302. put_device(dev);
  1303. continue;
  1304. }
  1305. if (rc)
  1306. continue;
  1307. rc = ap_init_queue(qid);
  1308. if (rc)
  1309. continue;
  1310. ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
  1311. if (!ap_dev)
  1312. break;
  1313. ap_dev->qid = qid;
  1314. ap_dev->queue_depth = queue_depth;
  1315. ap_dev->unregistered = 1;
  1316. spin_lock_init(&ap_dev->lock);
  1317. INIT_LIST_HEAD(&ap_dev->pendingq);
  1318. INIT_LIST_HEAD(&ap_dev->requestq);
  1319. INIT_LIST_HEAD(&ap_dev->list);
  1320. setup_timer(&ap_dev->timeout, ap_request_timeout,
  1321. (unsigned long) ap_dev);
  1322. switch (device_type) {
  1323. case 0:
  1324. /* device type probing for old cards */
  1325. if (ap_probe_device_type(ap_dev)) {
  1326. kfree(ap_dev);
  1327. continue;
  1328. }
  1329. break;
  1330. default:
  1331. ap_dev->device_type = device_type;
  1332. }
  1333. ap_dev->raw_hwtype = device_type;
  1334. rc = ap_query_functions(qid, &device_functions);
  1335. if (!rc)
  1336. ap_dev->functions = device_functions;
  1337. else
  1338. ap_dev->functions = 0u;
  1339. ap_dev->device.bus = &ap_bus_type;
  1340. ap_dev->device.parent = ap_root_device;
  1341. if (dev_set_name(&ap_dev->device, "card%02x",
  1342. AP_QID_DEVICE(ap_dev->qid))) {
  1343. kfree(ap_dev);
  1344. continue;
  1345. }
  1346. ap_dev->device.release = ap_device_release;
  1347. rc = device_register(&ap_dev->device);
  1348. if (rc) {
  1349. put_device(&ap_dev->device);
  1350. continue;
  1351. }
  1352. /* Add device attributes. */
  1353. rc = sysfs_create_group(&ap_dev->device.kobj,
  1354. &ap_dev_attr_group);
  1355. if (!rc) {
  1356. spin_lock_bh(&ap_dev->lock);
  1357. ap_dev->unregistered = 0;
  1358. spin_unlock_bh(&ap_dev->lock);
  1359. }
  1360. else
  1361. device_unregister(&ap_dev->device);
  1362. }
  1363. }
  1364. static void
  1365. ap_config_timeout(unsigned long ptr)
  1366. {
  1367. queue_work(ap_work_queue, &ap_config_work);
  1368. ap_config_timer.expires = jiffies + ap_config_time * HZ;
  1369. add_timer(&ap_config_timer);
  1370. }
  1371. /**
  1372. * __ap_schedule_poll_timer(): Schedule poll timer.
  1373. *
  1374. * Set up the timer to run the poll tasklet
  1375. */
  1376. static inline void __ap_schedule_poll_timer(void)
  1377. {
  1378. ktime_t hr_time;
  1379. spin_lock_bh(&ap_poll_timer_lock);
  1380. if (hrtimer_is_queued(&ap_poll_timer) || ap_suspend_flag)
  1381. goto out;
  1382. if (ktime_to_ns(hrtimer_expires_remaining(&ap_poll_timer)) <= 0) {
  1383. hr_time = ktime_set(0, poll_timeout);
  1384. hrtimer_forward_now(&ap_poll_timer, hr_time);
  1385. hrtimer_restart(&ap_poll_timer);
  1386. }
  1387. out:
  1388. spin_unlock_bh(&ap_poll_timer_lock);
  1389. }
  1390. /**
  1391. * ap_schedule_poll_timer(): Schedule poll timer.
  1392. *
  1393. * Set up the timer to run the poll tasklet
  1394. */
  1395. static inline void ap_schedule_poll_timer(void)
  1396. {
  1397. if (ap_using_interrupts())
  1398. return;
  1399. __ap_schedule_poll_timer();
  1400. }
  1401. /**
  1402. * ap_poll_read(): Receive pending reply messages from an AP device.
  1403. * @ap_dev: pointer to the AP device
  1404. * @flags: pointer to control flags, bit 2^0 is set if another poll is
  1405. * required, bit 2^1 is set if the poll timer needs to get armed
  1406. *
  1407. * Returns 0 if the device is still present, -ENODEV if not.
  1408. */
  1409. static int ap_poll_read(struct ap_device *ap_dev, unsigned long *flags)
  1410. {
  1411. struct ap_queue_status status;
  1412. struct ap_message *ap_msg;
  1413. if (ap_dev->queue_count <= 0)
  1414. return 0;
  1415. status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
  1416. ap_dev->reply->message, ap_dev->reply->length);
  1417. switch (status.response_code) {
  1418. case AP_RESPONSE_NORMAL:
  1419. atomic_dec(&ap_poll_requests);
  1420. ap_decrease_queue_count(ap_dev);
  1421. list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
  1422. if (ap_msg->psmid != ap_dev->reply->psmid)
  1423. continue;
  1424. list_del_init(&ap_msg->list);
  1425. ap_dev->pendingq_count--;
  1426. ap_msg->receive(ap_dev, ap_msg, ap_dev->reply);
  1427. break;
  1428. }
  1429. if (ap_dev->queue_count > 0)
  1430. *flags |= 1;
  1431. break;
  1432. case AP_RESPONSE_NO_PENDING_REPLY:
  1433. if (status.queue_empty) {
  1434. /* The card shouldn't forget requests but who knows. */
  1435. atomic_sub(ap_dev->queue_count, &ap_poll_requests);
  1436. ap_dev->queue_count = 0;
  1437. list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
  1438. ap_dev->requestq_count += ap_dev->pendingq_count;
  1439. ap_dev->pendingq_count = 0;
  1440. } else
  1441. *flags |= 2;
  1442. break;
  1443. default:
  1444. return -ENODEV;
  1445. }
  1446. return 0;
  1447. }
  1448. /**
  1449. * ap_poll_write(): Send messages from the request queue to an AP device.
  1450. * @ap_dev: pointer to the AP device
  1451. * @flags: pointer to control flags, bit 2^0 is set if another poll is
  1452. * required, bit 2^1 is set if the poll timer needs to get armed
  1453. *
  1454. * Returns 0 if the device is still present, -ENODEV if not.
  1455. */
  1456. static int ap_poll_write(struct ap_device *ap_dev, unsigned long *flags)
  1457. {
  1458. struct ap_queue_status status;
  1459. struct ap_message *ap_msg;
  1460. if (ap_dev->requestq_count <= 0 ||
  1461. ap_dev->queue_count >= ap_dev->queue_depth)
  1462. return 0;
  1463. /* Start the next request on the queue. */
  1464. ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list);
  1465. status = __ap_send(ap_dev->qid, ap_msg->psmid,
  1466. ap_msg->message, ap_msg->length, ap_msg->special);
  1467. switch (status.response_code) {
  1468. case AP_RESPONSE_NORMAL:
  1469. atomic_inc(&ap_poll_requests);
  1470. ap_increase_queue_count(ap_dev);
  1471. list_move_tail(&ap_msg->list, &ap_dev->pendingq);
  1472. ap_dev->requestq_count--;
  1473. ap_dev->pendingq_count++;
  1474. if (ap_dev->queue_count < ap_dev->queue_depth &&
  1475. ap_dev->requestq_count > 0)
  1476. *flags |= 1;
  1477. *flags |= 2;
  1478. break;
  1479. case AP_RESPONSE_RESET_IN_PROGRESS:
  1480. __ap_schedule_poll_timer();
  1481. case AP_RESPONSE_Q_FULL:
  1482. *flags |= 2;
  1483. break;
  1484. case AP_RESPONSE_MESSAGE_TOO_BIG:
  1485. case AP_RESPONSE_REQ_FAC_NOT_INST:
  1486. return -EINVAL;
  1487. default:
  1488. return -ENODEV;
  1489. }
  1490. return 0;
  1491. }
  1492. /**
  1493. * ap_poll_queue(): Poll AP device for pending replies and send new messages.
  1494. * @ap_dev: pointer to the bus device
  1495. * @flags: pointer to control flags, bit 2^0 is set if another poll is
  1496. * required, bit 2^1 is set if the poll timer needs to get armed
  1497. *
  1498. * Poll AP device for pending replies and send new messages. If either
  1499. * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
  1500. * Returns 0.
  1501. */
  1502. static inline int ap_poll_queue(struct ap_device *ap_dev, unsigned long *flags)
  1503. {
  1504. int rc;
  1505. rc = ap_poll_read(ap_dev, flags);
  1506. if (rc)
  1507. return rc;
  1508. return ap_poll_write(ap_dev, flags);
  1509. }
  1510. /**
  1511. * __ap_queue_message(): Queue a message to a device.
  1512. * @ap_dev: pointer to the AP device
  1513. * @ap_msg: the message to be queued
  1514. *
  1515. * Queue a message to a device. Returns 0 if successful.
  1516. */
  1517. static int __ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
  1518. {
  1519. struct ap_queue_status status;
  1520. if (list_empty(&ap_dev->requestq) &&
  1521. ap_dev->queue_count < ap_dev->queue_depth) {
  1522. status = __ap_send(ap_dev->qid, ap_msg->psmid,
  1523. ap_msg->message, ap_msg->length,
  1524. ap_msg->special);
  1525. switch (status.response_code) {
  1526. case AP_RESPONSE_NORMAL:
  1527. list_add_tail(&ap_msg->list, &ap_dev->pendingq);
  1528. atomic_inc(&ap_poll_requests);
  1529. ap_dev->pendingq_count++;
  1530. ap_increase_queue_count(ap_dev);
  1531. ap_dev->total_request_count++;
  1532. break;
  1533. case AP_RESPONSE_Q_FULL:
  1534. case AP_RESPONSE_RESET_IN_PROGRESS:
  1535. list_add_tail(&ap_msg->list, &ap_dev->requestq);
  1536. ap_dev->requestq_count++;
  1537. ap_dev->total_request_count++;
  1538. return -EBUSY;
  1539. case AP_RESPONSE_REQ_FAC_NOT_INST:
  1540. case AP_RESPONSE_MESSAGE_TOO_BIG:
  1541. ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-EINVAL));
  1542. return -EINVAL;
  1543. default: /* Device is gone. */
  1544. ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
  1545. return -ENODEV;
  1546. }
  1547. } else {
  1548. list_add_tail(&ap_msg->list, &ap_dev->requestq);
  1549. ap_dev->requestq_count++;
  1550. ap_dev->total_request_count++;
  1551. return -EBUSY;
  1552. }
  1553. ap_schedule_poll_timer();
  1554. return 0;
  1555. }
  1556. void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
  1557. {
  1558. unsigned long flags;
  1559. int rc;
  1560. /* For asynchronous message handling a valid receive-callback
  1561. * is required. */
  1562. BUG_ON(!ap_msg->receive);
  1563. spin_lock_bh(&ap_dev->lock);
  1564. if (!ap_dev->unregistered) {
  1565. /* Make room on the queue by polling for finished requests. */
  1566. rc = ap_poll_queue(ap_dev, &flags);
  1567. if (!rc)
  1568. rc = __ap_queue_message(ap_dev, ap_msg);
  1569. if (!rc)
  1570. wake_up(&ap_poll_wait);
  1571. if (rc == -ENODEV)
  1572. ap_dev->unregistered = 1;
  1573. } else {
  1574. ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
  1575. rc = -ENODEV;
  1576. }
  1577. spin_unlock_bh(&ap_dev->lock);
  1578. if (rc == -ENODEV)
  1579. device_unregister(&ap_dev->device);
  1580. }
  1581. EXPORT_SYMBOL(ap_queue_message);
  1582. /**
  1583. * ap_cancel_message(): Cancel a crypto request.
  1584. * @ap_dev: The AP device that has the message queued
  1585. * @ap_msg: The message that is to be removed
  1586. *
  1587. * Cancel a crypto request. This is done by removing the request
  1588. * from the device pending or request queue. Note that the
  1589. * request stays on the AP queue. When it finishes the message
  1590. * reply will be discarded because the psmid can't be found.
  1591. */
  1592. void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
  1593. {
  1594. struct ap_message *tmp;
  1595. spin_lock_bh(&ap_dev->lock);
  1596. if (!list_empty(&ap_msg->list)) {
  1597. list_for_each_entry(tmp, &ap_dev->pendingq, list)
  1598. if (tmp->psmid == ap_msg->psmid) {
  1599. ap_dev->pendingq_count--;
  1600. goto found;
  1601. }
  1602. ap_dev->requestq_count--;
  1603. found:
  1604. list_del_init(&ap_msg->list);
  1605. }
  1606. spin_unlock_bh(&ap_dev->lock);
  1607. }
  1608. EXPORT_SYMBOL(ap_cancel_message);
  1609. /**
  1610. * ap_poll_timeout(): AP receive polling for finished AP requests.
  1611. * @unused: Unused pointer.
  1612. *
  1613. * Schedules the AP tasklet using a high resolution timer.
  1614. */
  1615. static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
  1616. {
  1617. tasklet_schedule(&ap_tasklet);
  1618. return HRTIMER_NORESTART;
  1619. }
  1620. /**
  1621. * ap_reset(): Reset a not responding AP device.
  1622. * @ap_dev: Pointer to the AP device
  1623. *
  1624. * Reset a not responding AP device and move all requests from the
  1625. * pending queue to the request queue.
  1626. */
  1627. static void ap_reset(struct ap_device *ap_dev)
  1628. {
  1629. int rc;
  1630. ap_dev->reset = AP_RESET_IGNORE;
  1631. atomic_sub(ap_dev->queue_count, &ap_poll_requests);
  1632. ap_dev->queue_count = 0;
  1633. list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
  1634. ap_dev->requestq_count += ap_dev->pendingq_count;
  1635. ap_dev->pendingq_count = 0;
  1636. rc = ap_init_queue(ap_dev->qid);
  1637. if (rc == -ENODEV)
  1638. ap_dev->unregistered = 1;
  1639. else
  1640. __ap_schedule_poll_timer();
  1641. }
  1642. static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags)
  1643. {
  1644. if (!ap_dev->unregistered) {
  1645. if (ap_poll_queue(ap_dev, flags))
  1646. ap_dev->unregistered = 1;
  1647. if (ap_dev->reset == AP_RESET_DO)
  1648. ap_reset(ap_dev);
  1649. }
  1650. return 0;
  1651. }
  1652. /**
  1653. * ap_poll_all(): Poll all AP devices.
  1654. * @dummy: Unused variable
  1655. *
  1656. * Poll all AP devices on the bus in a round robin fashion. Continue
  1657. * polling until bit 2^0 of the control flags is not set. If bit 2^1
  1658. * of the control flags has been set arm the poll timer.
  1659. */
  1660. static void ap_poll_all(unsigned long dummy)
  1661. {
  1662. unsigned long flags;
  1663. struct ap_device *ap_dev;
  1664. /* Reset the indicator if interrupts are used. Thus new interrupts can
  1665. * be received. Doing it in the beginning of the tasklet is therefor
  1666. * important that no requests on any AP get lost.
  1667. */
  1668. if (ap_using_interrupts())
  1669. xchg(ap_airq.lsi_ptr, 0);
  1670. do {
  1671. flags = 0;
  1672. spin_lock(&ap_device_list_lock);
  1673. list_for_each_entry(ap_dev, &ap_device_list, list) {
  1674. spin_lock(&ap_dev->lock);
  1675. __ap_poll_device(ap_dev, &flags);
  1676. spin_unlock(&ap_dev->lock);
  1677. }
  1678. spin_unlock(&ap_device_list_lock);
  1679. } while (flags & 1);
  1680. if (flags & 2)
  1681. ap_schedule_poll_timer();
  1682. }
  1683. /**
  1684. * ap_poll_thread(): Thread that polls for finished requests.
  1685. * @data: Unused pointer
  1686. *
  1687. * AP bus poll thread. The purpose of this thread is to poll for
  1688. * finished requests in a loop if there is a "free" cpu - that is
  1689. * a cpu that doesn't have anything better to do. The polling stops
  1690. * as soon as there is another task or if all messages have been
  1691. * delivered.
  1692. */
  1693. static int ap_poll_thread(void *data)
  1694. {
  1695. DECLARE_WAITQUEUE(wait, current);
  1696. unsigned long flags;
  1697. int requests;
  1698. struct ap_device *ap_dev;
  1699. set_user_nice(current, MAX_NICE);
  1700. while (1) {
  1701. if (ap_suspend_flag)
  1702. return 0;
  1703. if (need_resched()) {
  1704. schedule();
  1705. continue;
  1706. }
  1707. add_wait_queue(&ap_poll_wait, &wait);
  1708. set_current_state(TASK_INTERRUPTIBLE);
  1709. if (kthread_should_stop())
  1710. break;
  1711. requests = atomic_read(&ap_poll_requests);
  1712. if (requests <= 0)
  1713. schedule();
  1714. set_current_state(TASK_RUNNING);
  1715. remove_wait_queue(&ap_poll_wait, &wait);
  1716. flags = 0;
  1717. spin_lock_bh(&ap_device_list_lock);
  1718. list_for_each_entry(ap_dev, &ap_device_list, list) {
  1719. spin_lock(&ap_dev->lock);
  1720. __ap_poll_device(ap_dev, &flags);
  1721. spin_unlock(&ap_dev->lock);
  1722. }
  1723. spin_unlock_bh(&ap_device_list_lock);
  1724. }
  1725. set_current_state(TASK_RUNNING);
  1726. remove_wait_queue(&ap_poll_wait, &wait);
  1727. return 0;
  1728. }
  1729. static int ap_poll_thread_start(void)
  1730. {
  1731. int rc;
  1732. if (ap_using_interrupts() || ap_suspend_flag)
  1733. return 0;
  1734. mutex_lock(&ap_poll_thread_mutex);
  1735. if (!ap_poll_kthread) {
  1736. ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
  1737. rc = PTR_RET(ap_poll_kthread);
  1738. if (rc)
  1739. ap_poll_kthread = NULL;
  1740. }
  1741. else
  1742. rc = 0;
  1743. mutex_unlock(&ap_poll_thread_mutex);
  1744. return rc;
  1745. }
  1746. static void ap_poll_thread_stop(void)
  1747. {
  1748. mutex_lock(&ap_poll_thread_mutex);
  1749. if (ap_poll_kthread) {
  1750. kthread_stop(ap_poll_kthread);
  1751. ap_poll_kthread = NULL;
  1752. }
  1753. mutex_unlock(&ap_poll_thread_mutex);
  1754. }
  1755. /**
  1756. * ap_request_timeout(): Handling of request timeouts
  1757. * @data: Holds the AP device.
  1758. *
  1759. * Handles request timeouts.
  1760. */
  1761. static void ap_request_timeout(unsigned long data)
  1762. {
  1763. struct ap_device *ap_dev = (struct ap_device *) data;
  1764. if (ap_dev->reset == AP_RESET_ARMED) {
  1765. ap_dev->reset = AP_RESET_DO;
  1766. if (ap_using_interrupts())
  1767. tasklet_schedule(&ap_tasklet);
  1768. }
  1769. }
  1770. static void ap_reset_domain(void)
  1771. {
  1772. int i;
  1773. if (ap_domain_index != -1)
  1774. for (i = 0; i < AP_DEVICES; i++)
  1775. ap_reset_queue(AP_MKQID(i, ap_domain_index));
  1776. }
  1777. static void ap_reset_all(void)
  1778. {
  1779. int i, j;
  1780. for (i = 0; i < AP_DOMAINS; i++) {
  1781. if (!ap_test_config_domain(i))
  1782. continue;
  1783. for (j = 0; j < AP_DEVICES; j++) {
  1784. if (!ap_test_config_card_id(j))
  1785. continue;
  1786. ap_reset_queue(AP_MKQID(j, i));
  1787. }
  1788. }
  1789. }
  1790. static struct reset_call ap_reset_call = {
  1791. .fn = ap_reset_all,
  1792. };
  1793. /**
  1794. * ap_module_init(): The module initialization code.
  1795. *
  1796. * Initializes the module.
  1797. */
  1798. int __init ap_module_init(void)
  1799. {
  1800. int rc, i;
  1801. if (ap_domain_index < -1 || ap_domain_index >= AP_DOMAINS) {
  1802. pr_warning("%d is not a valid cryptographic domain\n",
  1803. ap_domain_index);
  1804. return -EINVAL;
  1805. }
  1806. /* In resume callback we need to know if the user had set the domain.
  1807. * If so, we can not just reset it.
  1808. */
  1809. if (ap_domain_index >= 0)
  1810. user_set_domain = 1;
  1811. if (ap_instructions_available() != 0) {
  1812. pr_warning("The hardware system does not support "
  1813. "AP instructions\n");
  1814. return -ENODEV;
  1815. }
  1816. if (ap_interrupts_available()) {
  1817. rc = register_adapter_interrupt(&ap_airq);
  1818. ap_airq_flag = (rc == 0);
  1819. }
  1820. register_reset_call(&ap_reset_call);
  1821. /* Create /sys/bus/ap. */
  1822. rc = bus_register(&ap_bus_type);
  1823. if (rc)
  1824. goto out;
  1825. for (i = 0; ap_bus_attrs[i]; i++) {
  1826. rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
  1827. if (rc)
  1828. goto out_bus;
  1829. }
  1830. /* Create /sys/devices/ap. */
  1831. ap_root_device = root_device_register("ap");
  1832. rc = PTR_RET(ap_root_device);
  1833. if (rc)
  1834. goto out_bus;
  1835. ap_work_queue = create_singlethread_workqueue("kapwork");
  1836. if (!ap_work_queue) {
  1837. rc = -ENOMEM;
  1838. goto out_root;
  1839. }
  1840. ap_query_configuration();
  1841. if (ap_select_domain() == 0)
  1842. ap_scan_bus(NULL);
  1843. /* Setup the AP bus rescan timer. */
  1844. init_timer(&ap_config_timer);
  1845. ap_config_timer.function = ap_config_timeout;
  1846. ap_config_timer.data = 0;
  1847. ap_config_timer.expires = jiffies + ap_config_time * HZ;
  1848. add_timer(&ap_config_timer);
  1849. /* Setup the high resultion poll timer.
  1850. * If we are running under z/VM adjust polling to z/VM polling rate.
  1851. */
  1852. if (MACHINE_IS_VM)
  1853. poll_timeout = 1500000;
  1854. spin_lock_init(&ap_poll_timer_lock);
  1855. hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  1856. ap_poll_timer.function = ap_poll_timeout;
  1857. /* Start the low priority AP bus poll thread. */
  1858. if (ap_thread_flag) {
  1859. rc = ap_poll_thread_start();
  1860. if (rc)
  1861. goto out_work;
  1862. }
  1863. return 0;
  1864. out_work:
  1865. del_timer_sync(&ap_config_timer);
  1866. hrtimer_cancel(&ap_poll_timer);
  1867. destroy_workqueue(ap_work_queue);
  1868. out_root:
  1869. root_device_unregister(ap_root_device);
  1870. out_bus:
  1871. while (i--)
  1872. bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
  1873. bus_unregister(&ap_bus_type);
  1874. out:
  1875. unregister_reset_call(&ap_reset_call);
  1876. if (ap_using_interrupts())
  1877. unregister_adapter_interrupt(&ap_airq);
  1878. return rc;
  1879. }
  1880. static int __ap_match_all(struct device *dev, void *data)
  1881. {
  1882. return 1;
  1883. }
  1884. /**
  1885. * ap_modules_exit(): The module termination code
  1886. *
  1887. * Terminates the module.
  1888. */
  1889. void ap_module_exit(void)
  1890. {
  1891. int i;
  1892. struct device *dev;
  1893. ap_reset_domain();
  1894. ap_poll_thread_stop();
  1895. del_timer_sync(&ap_config_timer);
  1896. hrtimer_cancel(&ap_poll_timer);
  1897. destroy_workqueue(ap_work_queue);
  1898. tasklet_kill(&ap_tasklet);
  1899. root_device_unregister(ap_root_device);
  1900. while ((dev = bus_find_device(&ap_bus_type, NULL, NULL,
  1901. __ap_match_all)))
  1902. {
  1903. device_unregister(dev);
  1904. put_device(dev);
  1905. }
  1906. for (i = 0; ap_bus_attrs[i]; i++)
  1907. bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
  1908. bus_unregister(&ap_bus_type);
  1909. unregister_reset_call(&ap_reset_call);
  1910. if (ap_using_interrupts())
  1911. unregister_adapter_interrupt(&ap_airq);
  1912. }
  1913. module_init(ap_module_init);
  1914. module_exit(ap_module_exit);