ap_bus.c 45 KB

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