ap_bus.c 45 KB

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