ap_bus.c 35 KB

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