ap_bus.c 54 KB

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