ap_bus.c 51 KB

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