ap_bus.c 48 KB

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