ap_bus.c 42 KB

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