ap_bus.c 55 KB

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