ap_bus.c 42 KB

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