ap_bus.c 38 KB

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