ap_bus.c 32 KB

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