ap_bus.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  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, char **envp, int num_envp,
  419. char *buffer, int buffer_size)
  420. {
  421. struct ap_device *ap_dev = to_ap_dev(dev);
  422. int retval = 0, length = 0, i = 0;
  423. if (!ap_dev)
  424. return -ENODEV;
  425. /* Set up DEV_TYPE environment variable. */
  426. retval = add_uevent_var(envp, num_envp, &i,
  427. buffer, buffer_size, &length,
  428. "DEV_TYPE=%04X", ap_dev->device_type);
  429. if (retval)
  430. return retval;
  431. /* Add MODALIAS= */
  432. retval = add_uevent_var(envp, num_envp, &i,
  433. buffer, buffer_size, &length,
  434. "MODALIAS=ap:t%02X", ap_dev->device_type);
  435. envp[i] = NULL;
  436. return retval;
  437. }
  438. static struct bus_type ap_bus_type = {
  439. .name = "ap",
  440. .match = &ap_bus_match,
  441. .uevent = &ap_uevent,
  442. };
  443. static int ap_device_probe(struct device *dev)
  444. {
  445. struct ap_device *ap_dev = to_ap_dev(dev);
  446. struct ap_driver *ap_drv = to_ap_drv(dev->driver);
  447. int rc;
  448. ap_dev->drv = ap_drv;
  449. spin_lock_bh(&ap_device_lock);
  450. list_add(&ap_dev->list, &ap_device_list);
  451. spin_unlock_bh(&ap_device_lock);
  452. rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
  453. return rc;
  454. }
  455. /**
  456. * Flush all requests from the request/pending queue of an AP device.
  457. * @ap_dev: pointer to the AP device.
  458. */
  459. static void __ap_flush_queue(struct ap_device *ap_dev)
  460. {
  461. struct ap_message *ap_msg, *next;
  462. list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) {
  463. list_del_init(&ap_msg->list);
  464. ap_dev->pendingq_count--;
  465. ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
  466. }
  467. list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) {
  468. list_del_init(&ap_msg->list);
  469. ap_dev->requestq_count--;
  470. ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
  471. }
  472. }
  473. void ap_flush_queue(struct ap_device *ap_dev)
  474. {
  475. spin_lock_bh(&ap_dev->lock);
  476. __ap_flush_queue(ap_dev);
  477. spin_unlock_bh(&ap_dev->lock);
  478. }
  479. EXPORT_SYMBOL(ap_flush_queue);
  480. static int ap_device_remove(struct device *dev)
  481. {
  482. struct ap_device *ap_dev = to_ap_dev(dev);
  483. struct ap_driver *ap_drv = ap_dev->drv;
  484. ap_flush_queue(ap_dev);
  485. del_timer_sync(&ap_dev->timeout);
  486. if (ap_drv->remove)
  487. ap_drv->remove(ap_dev);
  488. spin_lock_bh(&ap_device_lock);
  489. list_del_init(&ap_dev->list);
  490. spin_unlock_bh(&ap_device_lock);
  491. spin_lock_bh(&ap_dev->lock);
  492. atomic_sub(ap_dev->queue_count, &ap_poll_requests);
  493. spin_unlock_bh(&ap_dev->lock);
  494. return 0;
  495. }
  496. int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
  497. char *name)
  498. {
  499. struct device_driver *drv = &ap_drv->driver;
  500. drv->bus = &ap_bus_type;
  501. drv->probe = ap_device_probe;
  502. drv->remove = ap_device_remove;
  503. drv->owner = owner;
  504. drv->name = name;
  505. return driver_register(drv);
  506. }
  507. EXPORT_SYMBOL(ap_driver_register);
  508. void ap_driver_unregister(struct ap_driver *ap_drv)
  509. {
  510. driver_unregister(&ap_drv->driver);
  511. }
  512. EXPORT_SYMBOL(ap_driver_unregister);
  513. /**
  514. * AP bus attributes.
  515. */
  516. static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
  517. {
  518. return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
  519. }
  520. static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);
  521. static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
  522. {
  523. return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
  524. }
  525. static ssize_t ap_config_time_store(struct bus_type *bus,
  526. const char *buf, size_t count)
  527. {
  528. int time;
  529. if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
  530. return -EINVAL;
  531. ap_config_time = time;
  532. if (!timer_pending(&ap_config_timer) ||
  533. !mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ)) {
  534. ap_config_timer.expires = jiffies + ap_config_time * HZ;
  535. add_timer(&ap_config_timer);
  536. }
  537. return count;
  538. }
  539. static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
  540. static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
  541. {
  542. return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
  543. }
  544. static ssize_t ap_poll_thread_store(struct bus_type *bus,
  545. const char *buf, size_t count)
  546. {
  547. int flag, rc;
  548. if (sscanf(buf, "%d\n", &flag) != 1)
  549. return -EINVAL;
  550. if (flag) {
  551. rc = ap_poll_thread_start();
  552. if (rc)
  553. return rc;
  554. }
  555. else
  556. ap_poll_thread_stop();
  557. return count;
  558. }
  559. static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
  560. static struct bus_attribute *const ap_bus_attrs[] = {
  561. &bus_attr_ap_domain,
  562. &bus_attr_config_time,
  563. &bus_attr_poll_thread,
  564. NULL
  565. };
  566. /**
  567. * Pick one of the 16 ap domains.
  568. */
  569. static int ap_select_domain(void)
  570. {
  571. int queue_depth, device_type, count, max_count, best_domain;
  572. int rc, i, j;
  573. /**
  574. * We want to use a single domain. Either the one specified with
  575. * the "domain=" parameter or the domain with the maximum number
  576. * of devices.
  577. */
  578. if (ap_domain_index >= 0 && ap_domain_index < AP_DOMAINS)
  579. /* Domain has already been selected. */
  580. return 0;
  581. best_domain = -1;
  582. max_count = 0;
  583. for (i = 0; i < AP_DOMAINS; i++) {
  584. count = 0;
  585. for (j = 0; j < AP_DEVICES; j++) {
  586. ap_qid_t qid = AP_MKQID(j, i);
  587. rc = ap_query_queue(qid, &queue_depth, &device_type);
  588. if (rc)
  589. continue;
  590. count++;
  591. }
  592. if (count > max_count) {
  593. max_count = count;
  594. best_domain = i;
  595. }
  596. }
  597. if (best_domain >= 0){
  598. ap_domain_index = best_domain;
  599. return 0;
  600. }
  601. return -ENODEV;
  602. }
  603. /**
  604. * Find the device type if query queue returned a device type of 0.
  605. * @ap_dev: pointer to the AP device.
  606. */
  607. static int ap_probe_device_type(struct ap_device *ap_dev)
  608. {
  609. static unsigned char msg[] = {
  610. 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
  611. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  612. 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
  613. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  614. 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
  615. 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
  616. 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
  617. 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
  618. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  619. 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
  620. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  621. 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
  622. 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
  623. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  624. 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
  625. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  626. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  627. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  628. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  629. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  630. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  631. 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
  632. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  633. 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
  634. 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
  635. 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
  636. 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
  637. 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
  638. 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
  639. 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
  640. 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
  641. 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
  642. 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
  643. 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
  644. 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
  645. 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
  646. 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
  647. 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
  648. 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
  649. 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
  650. 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
  651. 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
  652. 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
  653. 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
  654. 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
  655. };
  656. struct ap_queue_status status;
  657. unsigned long long psmid;
  658. char *reply;
  659. int rc, i;
  660. reply = (void *) get_zeroed_page(GFP_KERNEL);
  661. if (!reply) {
  662. rc = -ENOMEM;
  663. goto out;
  664. }
  665. status = __ap_send(ap_dev->qid, 0x0102030405060708ULL,
  666. msg, sizeof(msg));
  667. if (status.response_code != AP_RESPONSE_NORMAL) {
  668. rc = -ENODEV;
  669. goto out_free;
  670. }
  671. /* Wait for the test message to complete. */
  672. for (i = 0; i < 6; i++) {
  673. mdelay(300);
  674. status = __ap_recv(ap_dev->qid, &psmid, reply, 4096);
  675. if (status.response_code == AP_RESPONSE_NORMAL &&
  676. psmid == 0x0102030405060708ULL)
  677. break;
  678. }
  679. if (i < 6) {
  680. /* Got an answer. */
  681. if (reply[0] == 0x00 && reply[1] == 0x86)
  682. ap_dev->device_type = AP_DEVICE_TYPE_PCICC;
  683. else
  684. ap_dev->device_type = AP_DEVICE_TYPE_PCICA;
  685. rc = 0;
  686. } else
  687. rc = -ENODEV;
  688. out_free:
  689. free_page((unsigned long) reply);
  690. out:
  691. return rc;
  692. }
  693. /**
  694. * Scan the ap bus for new devices.
  695. */
  696. static int __ap_scan_bus(struct device *dev, void *data)
  697. {
  698. return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
  699. }
  700. static void ap_device_release(struct device *dev)
  701. {
  702. struct ap_device *ap_dev = to_ap_dev(dev);
  703. kfree(ap_dev);
  704. }
  705. static void ap_scan_bus(struct work_struct *unused)
  706. {
  707. struct ap_device *ap_dev;
  708. struct device *dev;
  709. ap_qid_t qid;
  710. int queue_depth, device_type;
  711. int rc, i;
  712. if (ap_select_domain() != 0)
  713. return;
  714. for (i = 0; i < AP_DEVICES; i++) {
  715. qid = AP_MKQID(i, ap_domain_index);
  716. dev = bus_find_device(&ap_bus_type, NULL,
  717. (void *)(unsigned long)qid,
  718. __ap_scan_bus);
  719. rc = ap_query_queue(qid, &queue_depth, &device_type);
  720. if (dev) {
  721. if (rc == -EBUSY) {
  722. set_current_state(TASK_UNINTERRUPTIBLE);
  723. schedule_timeout(AP_RESET_TIMEOUT);
  724. rc = ap_query_queue(qid, &queue_depth,
  725. &device_type);
  726. }
  727. ap_dev = to_ap_dev(dev);
  728. spin_lock_bh(&ap_dev->lock);
  729. if (rc || ap_dev->unregistered) {
  730. spin_unlock_bh(&ap_dev->lock);
  731. device_unregister(dev);
  732. put_device(dev);
  733. continue;
  734. }
  735. spin_unlock_bh(&ap_dev->lock);
  736. put_device(dev);
  737. continue;
  738. }
  739. if (rc)
  740. continue;
  741. rc = ap_init_queue(qid);
  742. if (rc)
  743. continue;
  744. ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
  745. if (!ap_dev)
  746. break;
  747. ap_dev->qid = qid;
  748. ap_dev->queue_depth = queue_depth;
  749. ap_dev->unregistered = 1;
  750. spin_lock_init(&ap_dev->lock);
  751. INIT_LIST_HEAD(&ap_dev->pendingq);
  752. INIT_LIST_HEAD(&ap_dev->requestq);
  753. INIT_LIST_HEAD(&ap_dev->list);
  754. setup_timer(&ap_dev->timeout, ap_request_timeout,
  755. (unsigned long) ap_dev);
  756. if (device_type == 0)
  757. ap_probe_device_type(ap_dev);
  758. else
  759. ap_dev->device_type = device_type;
  760. ap_dev->device.bus = &ap_bus_type;
  761. ap_dev->device.parent = ap_root_device;
  762. snprintf(ap_dev->device.bus_id, BUS_ID_SIZE, "card%02x",
  763. AP_QID_DEVICE(ap_dev->qid));
  764. ap_dev->device.release = ap_device_release;
  765. rc = device_register(&ap_dev->device);
  766. if (rc) {
  767. kfree(ap_dev);
  768. continue;
  769. }
  770. /* Add device attributes. */
  771. rc = sysfs_create_group(&ap_dev->device.kobj,
  772. &ap_dev_attr_group);
  773. if (!rc) {
  774. spin_lock_bh(&ap_dev->lock);
  775. ap_dev->unregistered = 0;
  776. spin_unlock_bh(&ap_dev->lock);
  777. }
  778. else
  779. device_unregister(&ap_dev->device);
  780. }
  781. }
  782. static void
  783. ap_config_timeout(unsigned long ptr)
  784. {
  785. queue_work(ap_work_queue, &ap_config_work);
  786. ap_config_timer.expires = jiffies + ap_config_time * HZ;
  787. add_timer(&ap_config_timer);
  788. }
  789. /**
  790. * Set up the timer to run the poll tasklet
  791. */
  792. static inline void ap_schedule_poll_timer(void)
  793. {
  794. if (timer_pending(&ap_poll_timer))
  795. return;
  796. mod_timer(&ap_poll_timer, jiffies + AP_POLL_TIME);
  797. }
  798. /**
  799. * Receive pending reply messages from an AP device.
  800. * @ap_dev: pointer to the AP device
  801. * @flags: pointer to control flags, bit 2^0 is set if another poll is
  802. * required, bit 2^1 is set if the poll timer needs to get armed
  803. * Returns 0 if the device is still present, -ENODEV if not.
  804. */
  805. static int ap_poll_read(struct ap_device *ap_dev, unsigned long *flags)
  806. {
  807. struct ap_queue_status status;
  808. struct ap_message *ap_msg;
  809. if (ap_dev->queue_count <= 0)
  810. return 0;
  811. status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
  812. ap_dev->reply->message, ap_dev->reply->length);
  813. switch (status.response_code) {
  814. case AP_RESPONSE_NORMAL:
  815. atomic_dec(&ap_poll_requests);
  816. ap_decrease_queue_count(ap_dev);
  817. list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
  818. if (ap_msg->psmid != ap_dev->reply->psmid)
  819. continue;
  820. list_del_init(&ap_msg->list);
  821. ap_dev->pendingq_count--;
  822. ap_dev->drv->receive(ap_dev, ap_msg, ap_dev->reply);
  823. break;
  824. }
  825. if (ap_dev->queue_count > 0)
  826. *flags |= 1;
  827. break;
  828. case AP_RESPONSE_NO_PENDING_REPLY:
  829. if (status.queue_empty) {
  830. /* The card shouldn't forget requests but who knows. */
  831. atomic_sub(ap_dev->queue_count, &ap_poll_requests);
  832. ap_dev->queue_count = 0;
  833. list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
  834. ap_dev->requestq_count += ap_dev->pendingq_count;
  835. ap_dev->pendingq_count = 0;
  836. } else
  837. *flags |= 2;
  838. break;
  839. default:
  840. return -ENODEV;
  841. }
  842. return 0;
  843. }
  844. /**
  845. * Send messages from the request queue to an AP device.
  846. * @ap_dev: pointer to the AP device
  847. * @flags: pointer to control flags, bit 2^0 is set if another poll is
  848. * required, bit 2^1 is set if the poll timer needs to get armed
  849. * Returns 0 if the device is still present, -ENODEV if not.
  850. */
  851. static int ap_poll_write(struct ap_device *ap_dev, unsigned long *flags)
  852. {
  853. struct ap_queue_status status;
  854. struct ap_message *ap_msg;
  855. if (ap_dev->requestq_count <= 0 ||
  856. ap_dev->queue_count >= ap_dev->queue_depth)
  857. return 0;
  858. /* Start the next request on the queue. */
  859. ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list);
  860. status = __ap_send(ap_dev->qid, ap_msg->psmid,
  861. ap_msg->message, ap_msg->length);
  862. switch (status.response_code) {
  863. case AP_RESPONSE_NORMAL:
  864. atomic_inc(&ap_poll_requests);
  865. ap_increase_queue_count(ap_dev);
  866. list_move_tail(&ap_msg->list, &ap_dev->pendingq);
  867. ap_dev->requestq_count--;
  868. ap_dev->pendingq_count++;
  869. if (ap_dev->queue_count < ap_dev->queue_depth &&
  870. ap_dev->requestq_count > 0)
  871. *flags |= 1;
  872. *flags |= 2;
  873. break;
  874. case AP_RESPONSE_Q_FULL:
  875. case AP_RESPONSE_RESET_IN_PROGRESS:
  876. *flags |= 2;
  877. break;
  878. case AP_RESPONSE_MESSAGE_TOO_BIG:
  879. return -EINVAL;
  880. default:
  881. return -ENODEV;
  882. }
  883. return 0;
  884. }
  885. /**
  886. * Poll AP device for pending replies and send new messages. If either
  887. * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
  888. * @ap_dev: pointer to the bus device
  889. * @flags: pointer to control flags, bit 2^0 is set if another poll is
  890. * required, bit 2^1 is set if the poll timer needs to get armed
  891. * Returns 0.
  892. */
  893. static inline int ap_poll_queue(struct ap_device *ap_dev, unsigned long *flags)
  894. {
  895. int rc;
  896. rc = ap_poll_read(ap_dev, flags);
  897. if (rc)
  898. return rc;
  899. return ap_poll_write(ap_dev, flags);
  900. }
  901. /**
  902. * Queue a message to a device.
  903. * @ap_dev: pointer to the AP device
  904. * @ap_msg: the message to be queued
  905. */
  906. static int __ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
  907. {
  908. struct ap_queue_status status;
  909. if (list_empty(&ap_dev->requestq) &&
  910. ap_dev->queue_count < ap_dev->queue_depth) {
  911. status = __ap_send(ap_dev->qid, ap_msg->psmid,
  912. ap_msg->message, ap_msg->length);
  913. switch (status.response_code) {
  914. case AP_RESPONSE_NORMAL:
  915. list_add_tail(&ap_msg->list, &ap_dev->pendingq);
  916. atomic_inc(&ap_poll_requests);
  917. ap_dev->pendingq_count++;
  918. ap_increase_queue_count(ap_dev);
  919. ap_dev->total_request_count++;
  920. break;
  921. case AP_RESPONSE_Q_FULL:
  922. case AP_RESPONSE_RESET_IN_PROGRESS:
  923. list_add_tail(&ap_msg->list, &ap_dev->requestq);
  924. ap_dev->requestq_count++;
  925. ap_dev->total_request_count++;
  926. return -EBUSY;
  927. case AP_RESPONSE_MESSAGE_TOO_BIG:
  928. ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-EINVAL));
  929. return -EINVAL;
  930. default: /* Device is gone. */
  931. ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
  932. return -ENODEV;
  933. }
  934. } else {
  935. list_add_tail(&ap_msg->list, &ap_dev->requestq);
  936. ap_dev->requestq_count++;
  937. ap_dev->total_request_count++;
  938. return -EBUSY;
  939. }
  940. ap_schedule_poll_timer();
  941. return 0;
  942. }
  943. void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
  944. {
  945. unsigned long flags;
  946. int rc;
  947. spin_lock_bh(&ap_dev->lock);
  948. if (!ap_dev->unregistered) {
  949. /* Make room on the queue by polling for finished requests. */
  950. rc = ap_poll_queue(ap_dev, &flags);
  951. if (!rc)
  952. rc = __ap_queue_message(ap_dev, ap_msg);
  953. if (!rc)
  954. wake_up(&ap_poll_wait);
  955. if (rc == -ENODEV)
  956. ap_dev->unregistered = 1;
  957. } else {
  958. ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
  959. rc = -ENODEV;
  960. }
  961. spin_unlock_bh(&ap_dev->lock);
  962. if (rc == -ENODEV)
  963. device_unregister(&ap_dev->device);
  964. }
  965. EXPORT_SYMBOL(ap_queue_message);
  966. /**
  967. * Cancel a crypto request. This is done by removing the request
  968. * from the devive pendingq or requestq queue. Note that the
  969. * request stays on the AP queue. When it finishes the message
  970. * reply will be discarded because the psmid can't be found.
  971. * @ap_dev: AP device that has the message queued
  972. * @ap_msg: the message that is to be removed
  973. */
  974. void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
  975. {
  976. struct ap_message *tmp;
  977. spin_lock_bh(&ap_dev->lock);
  978. if (!list_empty(&ap_msg->list)) {
  979. list_for_each_entry(tmp, &ap_dev->pendingq, list)
  980. if (tmp->psmid == ap_msg->psmid) {
  981. ap_dev->pendingq_count--;
  982. goto found;
  983. }
  984. ap_dev->requestq_count--;
  985. found:
  986. list_del_init(&ap_msg->list);
  987. }
  988. spin_unlock_bh(&ap_dev->lock);
  989. }
  990. EXPORT_SYMBOL(ap_cancel_message);
  991. /**
  992. * AP receive polling for finished AP requests
  993. */
  994. static void ap_poll_timeout(unsigned long unused)
  995. {
  996. tasklet_schedule(&ap_tasklet);
  997. }
  998. /**
  999. * Reset a not responding AP device and move all requests from the
  1000. * pending queue to the request queue.
  1001. */
  1002. static void ap_reset(struct ap_device *ap_dev)
  1003. {
  1004. int rc;
  1005. ap_dev->reset = AP_RESET_IGNORE;
  1006. atomic_sub(ap_dev->queue_count, &ap_poll_requests);
  1007. ap_dev->queue_count = 0;
  1008. list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
  1009. ap_dev->requestq_count += ap_dev->pendingq_count;
  1010. ap_dev->pendingq_count = 0;
  1011. rc = ap_init_queue(ap_dev->qid);
  1012. if (rc == -ENODEV)
  1013. ap_dev->unregistered = 1;
  1014. }
  1015. /**
  1016. * Poll all AP devices on the bus in a round robin fashion. Continue
  1017. * polling until bit 2^0 of the control flags is not set. If bit 2^1
  1018. * of the control flags has been set arm the poll timer.
  1019. */
  1020. static int __ap_poll_all(struct ap_device *ap_dev, unsigned long *flags)
  1021. {
  1022. spin_lock(&ap_dev->lock);
  1023. if (!ap_dev->unregistered) {
  1024. if (ap_poll_queue(ap_dev, flags))
  1025. ap_dev->unregistered = 1;
  1026. if (ap_dev->reset == AP_RESET_DO)
  1027. ap_reset(ap_dev);
  1028. }
  1029. spin_unlock(&ap_dev->lock);
  1030. return 0;
  1031. }
  1032. static void ap_poll_all(unsigned long dummy)
  1033. {
  1034. unsigned long flags;
  1035. struct ap_device *ap_dev;
  1036. do {
  1037. flags = 0;
  1038. spin_lock(&ap_device_lock);
  1039. list_for_each_entry(ap_dev, &ap_device_list, list) {
  1040. __ap_poll_all(ap_dev, &flags);
  1041. }
  1042. spin_unlock(&ap_device_lock);
  1043. } while (flags & 1);
  1044. if (flags & 2)
  1045. ap_schedule_poll_timer();
  1046. }
  1047. /**
  1048. * AP bus poll thread. The purpose of this thread is to poll for
  1049. * finished requests in a loop if there is a "free" cpu - that is
  1050. * a cpu that doesn't have anything better to do. The polling stops
  1051. * as soon as there is another task or if all messages have been
  1052. * delivered.
  1053. */
  1054. static int ap_poll_thread(void *data)
  1055. {
  1056. DECLARE_WAITQUEUE(wait, current);
  1057. unsigned long flags;
  1058. int requests;
  1059. struct ap_device *ap_dev;
  1060. set_user_nice(current, 19);
  1061. while (1) {
  1062. if (need_resched()) {
  1063. schedule();
  1064. continue;
  1065. }
  1066. add_wait_queue(&ap_poll_wait, &wait);
  1067. set_current_state(TASK_INTERRUPTIBLE);
  1068. if (kthread_should_stop())
  1069. break;
  1070. requests = atomic_read(&ap_poll_requests);
  1071. if (requests <= 0)
  1072. schedule();
  1073. set_current_state(TASK_RUNNING);
  1074. remove_wait_queue(&ap_poll_wait, &wait);
  1075. flags = 0;
  1076. spin_lock_bh(&ap_device_lock);
  1077. list_for_each_entry(ap_dev, &ap_device_list, list) {
  1078. __ap_poll_all(ap_dev, &flags);
  1079. }
  1080. spin_unlock_bh(&ap_device_lock);
  1081. }
  1082. set_current_state(TASK_RUNNING);
  1083. remove_wait_queue(&ap_poll_wait, &wait);
  1084. return 0;
  1085. }
  1086. static int ap_poll_thread_start(void)
  1087. {
  1088. int rc;
  1089. mutex_lock(&ap_poll_thread_mutex);
  1090. if (!ap_poll_kthread) {
  1091. ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
  1092. rc = IS_ERR(ap_poll_kthread) ? PTR_ERR(ap_poll_kthread) : 0;
  1093. if (rc)
  1094. ap_poll_kthread = NULL;
  1095. }
  1096. else
  1097. rc = 0;
  1098. mutex_unlock(&ap_poll_thread_mutex);
  1099. return rc;
  1100. }
  1101. static void ap_poll_thread_stop(void)
  1102. {
  1103. mutex_lock(&ap_poll_thread_mutex);
  1104. if (ap_poll_kthread) {
  1105. kthread_stop(ap_poll_kthread);
  1106. ap_poll_kthread = NULL;
  1107. }
  1108. mutex_unlock(&ap_poll_thread_mutex);
  1109. }
  1110. /**
  1111. * Handling of request timeouts
  1112. */
  1113. static void ap_request_timeout(unsigned long data)
  1114. {
  1115. struct ap_device *ap_dev = (struct ap_device *) data;
  1116. if (ap_dev->reset == AP_RESET_ARMED)
  1117. ap_dev->reset = AP_RESET_DO;
  1118. }
  1119. static void ap_reset_domain(void)
  1120. {
  1121. int i;
  1122. if (ap_domain_index != -1)
  1123. for (i = 0; i < AP_DEVICES; i++)
  1124. ap_reset_queue(AP_MKQID(i, ap_domain_index));
  1125. }
  1126. static void ap_reset_all(void)
  1127. {
  1128. int i, j;
  1129. for (i = 0; i < AP_DOMAINS; i++)
  1130. for (j = 0; j < AP_DEVICES; j++)
  1131. ap_reset_queue(AP_MKQID(j, i));
  1132. }
  1133. static struct reset_call ap_reset_call = {
  1134. .fn = ap_reset_all,
  1135. };
  1136. /**
  1137. * The module initialization code.
  1138. */
  1139. int __init ap_module_init(void)
  1140. {
  1141. int rc, i;
  1142. if (ap_domain_index < -1 || ap_domain_index >= AP_DOMAINS) {
  1143. printk(KERN_WARNING "Invalid param: domain = %d. "
  1144. " Not loading.\n", ap_domain_index);
  1145. return -EINVAL;
  1146. }
  1147. if (ap_instructions_available() != 0) {
  1148. printk(KERN_WARNING "AP instructions not installed.\n");
  1149. return -ENODEV;
  1150. }
  1151. register_reset_call(&ap_reset_call);
  1152. /* Create /sys/bus/ap. */
  1153. rc = bus_register(&ap_bus_type);
  1154. if (rc)
  1155. goto out;
  1156. for (i = 0; ap_bus_attrs[i]; i++) {
  1157. rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
  1158. if (rc)
  1159. goto out_bus;
  1160. }
  1161. /* Create /sys/devices/ap. */
  1162. ap_root_device = s390_root_dev_register("ap");
  1163. rc = IS_ERR(ap_root_device) ? PTR_ERR(ap_root_device) : 0;
  1164. if (rc)
  1165. goto out_bus;
  1166. ap_work_queue = create_singlethread_workqueue("kapwork");
  1167. if (!ap_work_queue) {
  1168. rc = -ENOMEM;
  1169. goto out_root;
  1170. }
  1171. if (ap_select_domain() == 0)
  1172. ap_scan_bus(NULL);
  1173. /* Setup the ap bus rescan timer. */
  1174. init_timer(&ap_config_timer);
  1175. ap_config_timer.function = ap_config_timeout;
  1176. ap_config_timer.data = 0;
  1177. ap_config_timer.expires = jiffies + ap_config_time * HZ;
  1178. add_timer(&ap_config_timer);
  1179. /* Start the low priority AP bus poll thread. */
  1180. if (ap_thread_flag) {
  1181. rc = ap_poll_thread_start();
  1182. if (rc)
  1183. goto out_work;
  1184. }
  1185. return 0;
  1186. out_work:
  1187. del_timer_sync(&ap_config_timer);
  1188. del_timer_sync(&ap_poll_timer);
  1189. destroy_workqueue(ap_work_queue);
  1190. out_root:
  1191. s390_root_dev_unregister(ap_root_device);
  1192. out_bus:
  1193. while (i--)
  1194. bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
  1195. bus_unregister(&ap_bus_type);
  1196. out:
  1197. unregister_reset_call(&ap_reset_call);
  1198. return rc;
  1199. }
  1200. static int __ap_match_all(struct device *dev, void *data)
  1201. {
  1202. return 1;
  1203. }
  1204. /**
  1205. * The module termination code
  1206. */
  1207. void ap_module_exit(void)
  1208. {
  1209. int i;
  1210. struct device *dev;
  1211. ap_reset_domain();
  1212. ap_poll_thread_stop();
  1213. del_timer_sync(&ap_config_timer);
  1214. del_timer_sync(&ap_poll_timer);
  1215. destroy_workqueue(ap_work_queue);
  1216. tasklet_kill(&ap_tasklet);
  1217. s390_root_dev_unregister(ap_root_device);
  1218. while ((dev = bus_find_device(&ap_bus_type, NULL, NULL,
  1219. __ap_match_all)))
  1220. {
  1221. device_unregister(dev);
  1222. put_device(dev);
  1223. }
  1224. for (i = 0; ap_bus_attrs[i]; i++)
  1225. bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
  1226. bus_unregister(&ap_bus_type);
  1227. unregister_reset_call(&ap_reset_call);
  1228. }
  1229. #ifndef CONFIG_ZCRYPT_MONOLITHIC
  1230. module_init(ap_module_init);
  1231. module_exit(ap_module_exit);
  1232. #endif