ap_bus.c 42 KB

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