ap_bus.c 52 KB

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