ap_bus.c 44 KB

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