ap_bus.c 33 KB

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