ap_bus.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  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 retval = 0, length = 0, i = 0;
  388. if (!ap_dev)
  389. return -ENODEV;
  390. /* Set up DEV_TYPE environment variable. */
  391. retval = add_uevent_var(envp, num_envp, &i,
  392. buffer, buffer_size, &length,
  393. "DEV_TYPE=%04X", ap_dev->device_type);
  394. if (retval)
  395. return retval;
  396. /* Add MODALIAS= */
  397. retval = add_uevent_var(envp, num_envp, &i,
  398. buffer, buffer_size, &length,
  399. "MODALIAS=ap:t%02X", ap_dev->device_type);
  400. envp[i] = NULL;
  401. return retval;
  402. }
  403. static struct bus_type ap_bus_type = {
  404. .name = "ap",
  405. .match = &ap_bus_match,
  406. .uevent = &ap_uevent,
  407. };
  408. static int ap_device_probe(struct device *dev)
  409. {
  410. struct ap_device *ap_dev = to_ap_dev(dev);
  411. struct ap_driver *ap_drv = to_ap_drv(dev->driver);
  412. int rc;
  413. ap_dev->drv = ap_drv;
  414. spin_lock_bh(&ap_device_lock);
  415. list_add(&ap_dev->list, &ap_device_list);
  416. spin_unlock_bh(&ap_device_lock);
  417. rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
  418. return rc;
  419. }
  420. /**
  421. * Flush all requests from the request/pending queue of an AP device.
  422. * @ap_dev: pointer to the AP device.
  423. */
  424. static void __ap_flush_queue(struct ap_device *ap_dev)
  425. {
  426. struct ap_message *ap_msg, *next;
  427. list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) {
  428. list_del_init(&ap_msg->list);
  429. ap_dev->pendingq_count--;
  430. ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
  431. }
  432. list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) {
  433. list_del_init(&ap_msg->list);
  434. ap_dev->requestq_count--;
  435. ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
  436. }
  437. }
  438. void ap_flush_queue(struct ap_device *ap_dev)
  439. {
  440. spin_lock_bh(&ap_dev->lock);
  441. __ap_flush_queue(ap_dev);
  442. spin_unlock_bh(&ap_dev->lock);
  443. }
  444. EXPORT_SYMBOL(ap_flush_queue);
  445. static int ap_device_remove(struct device *dev)
  446. {
  447. struct ap_device *ap_dev = to_ap_dev(dev);
  448. struct ap_driver *ap_drv = ap_dev->drv;
  449. ap_flush_queue(ap_dev);
  450. if (ap_drv->remove)
  451. ap_drv->remove(ap_dev);
  452. spin_lock_bh(&ap_device_lock);
  453. list_del_init(&ap_dev->list);
  454. spin_unlock_bh(&ap_device_lock);
  455. spin_lock_bh(&ap_dev->lock);
  456. atomic_sub(ap_dev->queue_count, &ap_poll_requests);
  457. spin_unlock_bh(&ap_dev->lock);
  458. return 0;
  459. }
  460. int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
  461. char *name)
  462. {
  463. struct device_driver *drv = &ap_drv->driver;
  464. drv->bus = &ap_bus_type;
  465. drv->probe = ap_device_probe;
  466. drv->remove = ap_device_remove;
  467. drv->owner = owner;
  468. drv->name = name;
  469. return driver_register(drv);
  470. }
  471. EXPORT_SYMBOL(ap_driver_register);
  472. void ap_driver_unregister(struct ap_driver *ap_drv)
  473. {
  474. driver_unregister(&ap_drv->driver);
  475. }
  476. EXPORT_SYMBOL(ap_driver_unregister);
  477. /**
  478. * AP bus attributes.
  479. */
  480. static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
  481. {
  482. return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
  483. }
  484. static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);
  485. static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
  486. {
  487. return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
  488. }
  489. static ssize_t ap_config_time_store(struct bus_type *bus,
  490. const char *buf, size_t count)
  491. {
  492. int time;
  493. if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
  494. return -EINVAL;
  495. ap_config_time = time;
  496. if (!timer_pending(&ap_config_timer) ||
  497. !mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ)) {
  498. ap_config_timer.expires = jiffies + ap_config_time * HZ;
  499. add_timer(&ap_config_timer);
  500. }
  501. return count;
  502. }
  503. static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
  504. static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
  505. {
  506. return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
  507. }
  508. static ssize_t ap_poll_thread_store(struct bus_type *bus,
  509. const char *buf, size_t count)
  510. {
  511. int flag, rc;
  512. if (sscanf(buf, "%d\n", &flag) != 1)
  513. return -EINVAL;
  514. if (flag) {
  515. rc = ap_poll_thread_start();
  516. if (rc)
  517. return rc;
  518. }
  519. else
  520. ap_poll_thread_stop();
  521. return count;
  522. }
  523. static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
  524. static struct bus_attribute *const ap_bus_attrs[] = {
  525. &bus_attr_ap_domain,
  526. &bus_attr_config_time,
  527. &bus_attr_poll_thread,
  528. NULL
  529. };
  530. /**
  531. * Pick one of the 16 ap domains.
  532. */
  533. static int ap_select_domain(void)
  534. {
  535. int queue_depth, device_type, count, max_count, best_domain;
  536. int rc, i, j;
  537. /**
  538. * We want to use a single domain. Either the one specified with
  539. * the "domain=" parameter or the domain with the maximum number
  540. * of devices.
  541. */
  542. if (ap_domain_index >= 0 && ap_domain_index < AP_DOMAINS)
  543. /* Domain has already been selected. */
  544. return 0;
  545. best_domain = -1;
  546. max_count = 0;
  547. for (i = 0; i < AP_DOMAINS; i++) {
  548. count = 0;
  549. for (j = 0; j < AP_DEVICES; j++) {
  550. ap_qid_t qid = AP_MKQID(j, i);
  551. rc = ap_query_queue(qid, &queue_depth, &device_type);
  552. if (rc)
  553. continue;
  554. count++;
  555. }
  556. if (count > max_count) {
  557. max_count = count;
  558. best_domain = i;
  559. }
  560. }
  561. if (best_domain >= 0){
  562. ap_domain_index = best_domain;
  563. return 0;
  564. }
  565. return -ENODEV;
  566. }
  567. /**
  568. * Find the device type if query queue returned a device type of 0.
  569. * @ap_dev: pointer to the AP device.
  570. */
  571. static int ap_probe_device_type(struct ap_device *ap_dev)
  572. {
  573. static unsigned char msg[] = {
  574. 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
  575. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  576. 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
  577. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  578. 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
  579. 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
  580. 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
  581. 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
  582. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  583. 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
  584. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  585. 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
  586. 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
  587. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  588. 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
  589. 0x00,0x00,0x00,0x00,0x00,0x00,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,0x0a,0x00,0x00,0x00,0x00,0x00,
  596. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  597. 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
  598. 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
  599. 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
  600. 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
  601. 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
  602. 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
  603. 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
  604. 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
  605. 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
  606. 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
  607. 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
  608. 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
  609. 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
  610. 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
  611. 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
  612. 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
  613. 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
  614. 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
  615. 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
  616. 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
  617. 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
  618. 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
  619. };
  620. struct ap_queue_status status;
  621. unsigned long long psmid;
  622. char *reply;
  623. int rc, i;
  624. reply = (void *) get_zeroed_page(GFP_KERNEL);
  625. if (!reply) {
  626. rc = -ENOMEM;
  627. goto out;
  628. }
  629. status = __ap_send(ap_dev->qid, 0x0102030405060708ULL,
  630. msg, sizeof(msg));
  631. if (status.response_code != AP_RESPONSE_NORMAL) {
  632. rc = -ENODEV;
  633. goto out_free;
  634. }
  635. /* Wait for the test message to complete. */
  636. for (i = 0; i < 6; i++) {
  637. mdelay(300);
  638. status = __ap_recv(ap_dev->qid, &psmid, reply, 4096);
  639. if (status.response_code == AP_RESPONSE_NORMAL &&
  640. psmid == 0x0102030405060708ULL)
  641. break;
  642. }
  643. if (i < 6) {
  644. /* Got an answer. */
  645. if (reply[0] == 0x00 && reply[1] == 0x86)
  646. ap_dev->device_type = AP_DEVICE_TYPE_PCICC;
  647. else
  648. ap_dev->device_type = AP_DEVICE_TYPE_PCICA;
  649. rc = 0;
  650. } else
  651. rc = -ENODEV;
  652. out_free:
  653. free_page((unsigned long) reply);
  654. out:
  655. return rc;
  656. }
  657. /**
  658. * Scan the ap bus for new devices.
  659. */
  660. static int __ap_scan_bus(struct device *dev, void *data)
  661. {
  662. return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
  663. }
  664. static void ap_device_release(struct device *dev)
  665. {
  666. struct ap_device *ap_dev = to_ap_dev(dev);
  667. kfree(ap_dev);
  668. }
  669. static void ap_scan_bus(struct work_struct *unused)
  670. {
  671. struct ap_device *ap_dev;
  672. struct device *dev;
  673. ap_qid_t qid;
  674. int queue_depth, device_type;
  675. int rc, i;
  676. if (ap_select_domain() != 0)
  677. return;
  678. for (i = 0; i < AP_DEVICES; i++) {
  679. qid = AP_MKQID(i, ap_domain_index);
  680. dev = bus_find_device(&ap_bus_type, NULL,
  681. (void *)(unsigned long)qid,
  682. __ap_scan_bus);
  683. rc = ap_query_queue(qid, &queue_depth, &device_type);
  684. if (dev) {
  685. ap_dev = to_ap_dev(dev);
  686. spin_lock_bh(&ap_dev->lock);
  687. if (rc || ap_dev->unregistered) {
  688. spin_unlock_bh(&ap_dev->lock);
  689. put_device(dev);
  690. device_unregister(dev);
  691. continue;
  692. } else
  693. spin_unlock_bh(&ap_dev->lock);
  694. }
  695. if (dev) {
  696. put_device(dev);
  697. continue;
  698. }
  699. if (rc)
  700. continue;
  701. rc = ap_init_queue(qid);
  702. if (rc)
  703. continue;
  704. ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
  705. if (!ap_dev)
  706. break;
  707. ap_dev->qid = qid;
  708. ap_dev->queue_depth = queue_depth;
  709. ap_dev->unregistered = 1;
  710. spin_lock_init(&ap_dev->lock);
  711. INIT_LIST_HEAD(&ap_dev->pendingq);
  712. INIT_LIST_HEAD(&ap_dev->requestq);
  713. INIT_LIST_HEAD(&ap_dev->list);
  714. if (device_type == 0)
  715. ap_probe_device_type(ap_dev);
  716. else
  717. ap_dev->device_type = device_type;
  718. ap_dev->device.bus = &ap_bus_type;
  719. ap_dev->device.parent = ap_root_device;
  720. snprintf(ap_dev->device.bus_id, BUS_ID_SIZE, "card%02x",
  721. AP_QID_DEVICE(ap_dev->qid));
  722. ap_dev->device.release = ap_device_release;
  723. rc = device_register(&ap_dev->device);
  724. if (rc) {
  725. kfree(ap_dev);
  726. continue;
  727. }
  728. /* Add device attributes. */
  729. rc = sysfs_create_group(&ap_dev->device.kobj,
  730. &ap_dev_attr_group);
  731. if (!rc) {
  732. spin_lock_bh(&ap_dev->lock);
  733. ap_dev->unregistered = 0;
  734. spin_unlock_bh(&ap_dev->lock);
  735. }
  736. else
  737. device_unregister(&ap_dev->device);
  738. }
  739. }
  740. static void
  741. ap_config_timeout(unsigned long ptr)
  742. {
  743. queue_work(ap_work_queue, &ap_config_work);
  744. ap_config_timer.expires = jiffies + ap_config_time * HZ;
  745. add_timer(&ap_config_timer);
  746. }
  747. /**
  748. * Set up the timer to run the poll tasklet
  749. */
  750. static inline void ap_schedule_poll_timer(void)
  751. {
  752. if (timer_pending(&ap_poll_timer))
  753. return;
  754. mod_timer(&ap_poll_timer, jiffies + AP_POLL_TIME);
  755. }
  756. /**
  757. * Receive pending reply messages from an AP device.
  758. * @ap_dev: pointer to the AP device
  759. * @flags: pointer to control flags, bit 2^0 is set if another poll is
  760. * required, bit 2^1 is set if the poll timer needs to get armed
  761. * Returns 0 if the device is still present, -ENODEV if not.
  762. */
  763. static int ap_poll_read(struct ap_device *ap_dev, unsigned long *flags)
  764. {
  765. struct ap_queue_status status;
  766. struct ap_message *ap_msg;
  767. if (ap_dev->queue_count <= 0)
  768. return 0;
  769. status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
  770. ap_dev->reply->message, ap_dev->reply->length);
  771. switch (status.response_code) {
  772. case AP_RESPONSE_NORMAL:
  773. atomic_dec(&ap_poll_requests);
  774. ap_dev->queue_count--;
  775. list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
  776. if (ap_msg->psmid != ap_dev->reply->psmid)
  777. continue;
  778. list_del_init(&ap_msg->list);
  779. ap_dev->pendingq_count--;
  780. ap_dev->drv->receive(ap_dev, ap_msg, ap_dev->reply);
  781. break;
  782. }
  783. if (ap_dev->queue_count > 0)
  784. *flags |= 1;
  785. break;
  786. case AP_RESPONSE_NO_PENDING_REPLY:
  787. if (status.queue_empty) {
  788. /* The card shouldn't forget requests but who knows. */
  789. atomic_sub(ap_dev->queue_count, &ap_poll_requests);
  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