ap_bus.c 36 KB

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