ap_bus.c 47 KB

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