ap_bus.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright IBM Corp. 2006, 2012
  4. * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
  5. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  6. * Ralph Wuerthner <rwuerthn@de.ibm.com>
  7. * Felix Beck <felix.beck@de.ibm.com>
  8. * Holger Dengler <hd@linux.vnet.ibm.com>
  9. *
  10. * Adjunct processor bus.
  11. */
  12. #define KMSG_COMPONENT "ap"
  13. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  14. #include <linux/kernel_stat.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/init.h>
  17. #include <linux/delay.h>
  18. #include <linux/err.h>
  19. #include <linux/freezer.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/slab.h>
  23. #include <linux/notifier.h>
  24. #include <linux/kthread.h>
  25. #include <linux/mutex.h>
  26. #include <asm/airq.h>
  27. #include <linux/atomic.h>
  28. #include <asm/isc.h>
  29. #include <linux/hrtimer.h>
  30. #include <linux/ktime.h>
  31. #include <asm/facility.h>
  32. #include <linux/crypto.h>
  33. #include <linux/mod_devicetable.h>
  34. #include <linux/debugfs.h>
  35. #include <linux/ctype.h>
  36. #include "ap_bus.h"
  37. #include "ap_debug.h"
  38. /*
  39. * Module parameters; note though this file itself isn't modular.
  40. */
  41. int ap_domain_index = -1; /* Adjunct Processor Domain Index */
  42. static DEFINE_SPINLOCK(ap_domain_lock);
  43. module_param_named(domain, ap_domain_index, int, 0440);
  44. MODULE_PARM_DESC(domain, "domain index for ap devices");
  45. EXPORT_SYMBOL(ap_domain_index);
  46. static int ap_thread_flag;
  47. module_param_named(poll_thread, ap_thread_flag, int, 0440);
  48. MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
  49. static char *apm_str;
  50. module_param_named(apmask, apm_str, charp, 0440);
  51. MODULE_PARM_DESC(apmask, "AP bus adapter mask.");
  52. static char *aqm_str;
  53. module_param_named(aqmask, aqm_str, charp, 0440);
  54. MODULE_PARM_DESC(aqmask, "AP bus domain mask.");
  55. static struct device *ap_root_device;
  56. /* Hashtable of all queue devices on the AP bus */
  57. DEFINE_HASHTABLE(ap_queues, 8);
  58. /* lock used for the ap_queues hashtable */
  59. DEFINE_SPINLOCK(ap_queues_lock);
  60. /* Default permissions (ioctl, card and domain masking) */
  61. struct ap_perms ap_perms;
  62. EXPORT_SYMBOL(ap_perms);
  63. DEFINE_MUTEX(ap_perms_mutex);
  64. EXPORT_SYMBOL(ap_perms_mutex);
  65. static struct ap_config_info *ap_qci_info;
  66. /*
  67. * AP bus related debug feature things.
  68. */
  69. debug_info_t *ap_dbf_info;
  70. /*
  71. * Workqueue timer for bus rescan.
  72. */
  73. static struct timer_list ap_config_timer;
  74. static int ap_config_time = AP_CONFIG_TIME;
  75. static void ap_scan_bus(struct work_struct *);
  76. static DECLARE_WORK(ap_scan_work, ap_scan_bus);
  77. /*
  78. * Tasklet & timer for AP request polling and interrupts
  79. */
  80. static void ap_tasklet_fn(unsigned long);
  81. static DECLARE_TASKLET_OLD(ap_tasklet, ap_tasklet_fn);
  82. static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
  83. static struct task_struct *ap_poll_kthread;
  84. static DEFINE_MUTEX(ap_poll_thread_mutex);
  85. static DEFINE_SPINLOCK(ap_poll_timer_lock);
  86. static struct hrtimer ap_poll_timer;
  87. /*
  88. * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
  89. * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.
  90. */
  91. static unsigned long long poll_timeout = 250000;
  92. /* Maximum domain id, if not given via qci */
  93. static int ap_max_domain_id = 15;
  94. /* Maximum adapter id, if not given via qci */
  95. static int ap_max_adapter_id = 63;
  96. static struct bus_type ap_bus_type;
  97. /* Adapter interrupt definitions */
  98. static void ap_interrupt_handler(struct airq_struct *airq, bool floating);
  99. static bool ap_irq_flag;
  100. static struct airq_struct ap_airq = {
  101. .handler = ap_interrupt_handler,
  102. .isc = AP_ISC,
  103. };
  104. /**
  105. * ap_airq_ptr() - Get the address of the adapter interrupt indicator
  106. *
  107. * Returns the address of the local-summary-indicator of the adapter
  108. * interrupt handler for AP, or NULL if adapter interrupts are not
  109. * available.
  110. */
  111. void *ap_airq_ptr(void)
  112. {
  113. if (ap_irq_flag)
  114. return ap_airq.lsi_ptr;
  115. return NULL;
  116. }
  117. /**
  118. * ap_interrupts_available(): Test if AP interrupts are available.
  119. *
  120. * Returns 1 if AP interrupts are available.
  121. */
  122. static int ap_interrupts_available(void)
  123. {
  124. return test_facility(65);
  125. }
  126. /**
  127. * ap_qci_available(): Test if AP configuration
  128. * information can be queried via QCI subfunction.
  129. *
  130. * Returns 1 if subfunction PQAP(QCI) is available.
  131. */
  132. static int ap_qci_available(void)
  133. {
  134. return test_facility(12);
  135. }
  136. /**
  137. * ap_apft_available(): Test if AP facilities test (APFT)
  138. * facility is available.
  139. *
  140. * Returns 1 if APFT is is available.
  141. */
  142. static int ap_apft_available(void)
  143. {
  144. return test_facility(15);
  145. }
  146. /*
  147. * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
  148. *
  149. * Returns 1 if the QACT subfunction is available.
  150. */
  151. static inline int ap_qact_available(void)
  152. {
  153. if (ap_qci_info)
  154. return ap_qci_info->qact;
  155. return 0;
  156. }
  157. /*
  158. * ap_fetch_qci_info(): Fetch cryptographic config info
  159. *
  160. * Returns the ap configuration info fetched via PQAP(QCI).
  161. * On success 0 is returned, on failure a negative errno
  162. * is returned, e.g. if the PQAP(QCI) instruction is not
  163. * available, the return value will be -EOPNOTSUPP.
  164. */
  165. static inline int ap_fetch_qci_info(struct ap_config_info *info)
  166. {
  167. if (!ap_qci_available())
  168. return -EOPNOTSUPP;
  169. if (!info)
  170. return -EINVAL;
  171. return ap_qci(info);
  172. }
  173. /**
  174. * ap_init_qci_info(): Allocate and query qci config info.
  175. * Does also update the static variables ap_max_domain_id
  176. * and ap_max_adapter_id if this info is available.
  177. */
  178. static void __init ap_init_qci_info(void)
  179. {
  180. if (!ap_qci_available()) {
  181. AP_DBF_INFO("%s QCI not supported\n", __func__);
  182. return;
  183. }
  184. ap_qci_info = kzalloc(sizeof(*ap_qci_info), GFP_KERNEL);
  185. if (!ap_qci_info)
  186. return;
  187. if (ap_fetch_qci_info(ap_qci_info) != 0) {
  188. kfree(ap_qci_info);
  189. ap_qci_info = NULL;
  190. return;
  191. }
  192. AP_DBF_INFO("%s successful fetched initial qci info\n", __func__);
  193. if (ap_qci_info->apxa) {
  194. if (ap_qci_info->Na) {
  195. ap_max_adapter_id = ap_qci_info->Na;
  196. AP_DBF_INFO("%s new ap_max_adapter_id is %d\n",
  197. __func__, ap_max_adapter_id);
  198. }
  199. if (ap_qci_info->Nd) {
  200. ap_max_domain_id = ap_qci_info->Nd;
  201. AP_DBF_INFO("%s new ap_max_domain_id is %d\n",
  202. __func__, ap_max_domain_id);
  203. }
  204. }
  205. }
  206. /*
  207. * ap_test_config(): helper function to extract the nrth bit
  208. * within the unsigned int array field.
  209. */
  210. static inline int ap_test_config(unsigned int *field, unsigned int nr)
  211. {
  212. return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
  213. }
  214. /*
  215. * ap_test_config_card_id(): Test, whether an AP card ID is configured.
  216. *
  217. * Returns 0 if the card is not configured
  218. * 1 if the card is configured or
  219. * if the configuration information is not available
  220. */
  221. static inline int ap_test_config_card_id(unsigned int id)
  222. {
  223. if (id > ap_max_adapter_id)
  224. return 0;
  225. if (ap_qci_info)
  226. return ap_test_config(ap_qci_info->apm, id);
  227. return 1;
  228. }
  229. /*
  230. * ap_test_config_usage_domain(): Test, whether an AP usage domain
  231. * is configured.
  232. *
  233. * Returns 0 if the usage domain is not configured
  234. * 1 if the usage domain is configured or
  235. * if the configuration information is not available
  236. */
  237. int ap_test_config_usage_domain(unsigned int domain)
  238. {
  239. if (domain > ap_max_domain_id)
  240. return 0;
  241. if (ap_qci_info)
  242. return ap_test_config(ap_qci_info->aqm, domain);
  243. return 1;
  244. }
  245. EXPORT_SYMBOL(ap_test_config_usage_domain);
  246. /*
  247. * ap_test_config_ctrl_domain(): Test, whether an AP control domain
  248. * is configured.
  249. * @domain AP control domain ID
  250. *
  251. * Returns 1 if the control domain is configured
  252. * 0 in all other cases
  253. */
  254. int ap_test_config_ctrl_domain(unsigned int domain)
  255. {
  256. if (!ap_qci_info || domain > ap_max_domain_id)
  257. return 0;
  258. return ap_test_config(ap_qci_info->adm, domain);
  259. }
  260. EXPORT_SYMBOL(ap_test_config_ctrl_domain);
  261. /*
  262. * ap_queue_info(): Check and get AP queue info.
  263. * Returns true if TAPQ succeeded and the info is filled or
  264. * false otherwise.
  265. */
  266. static bool ap_queue_info(ap_qid_t qid, int *q_type,
  267. unsigned int *q_fac, int *q_depth, bool *q_decfg)
  268. {
  269. struct ap_queue_status status;
  270. unsigned long info = 0;
  271. /* make sure we don't run into a specifiation exception */
  272. if (AP_QID_CARD(qid) > ap_max_adapter_id ||
  273. AP_QID_QUEUE(qid) > ap_max_domain_id)
  274. return false;
  275. /* call TAPQ on this APQN */
  276. status = ap_test_queue(qid, ap_apft_available(), &info);
  277. switch (status.response_code) {
  278. case AP_RESPONSE_NORMAL:
  279. case AP_RESPONSE_RESET_IN_PROGRESS:
  280. case AP_RESPONSE_DECONFIGURED:
  281. case AP_RESPONSE_CHECKSTOPPED:
  282. case AP_RESPONSE_BUSY:
  283. /*
  284. * According to the architecture in all these cases the
  285. * info should be filled. All bits 0 is not possible as
  286. * there is at least one of the mode bits set.
  287. */
  288. if (WARN_ON_ONCE(!info))
  289. return false;
  290. *q_type = (int)((info >> 24) & 0xff);
  291. *q_fac = (unsigned int)(info >> 32);
  292. *q_depth = (int)(info & 0xff);
  293. *q_decfg = status.response_code == AP_RESPONSE_DECONFIGURED;
  294. switch (*q_type) {
  295. /* For CEX2 and CEX3 the available functions
  296. * are not reflected by the facilities bits.
  297. * Instead it is coded into the type. So here
  298. * modify the function bits based on the type.
  299. */
  300. case AP_DEVICE_TYPE_CEX2A:
  301. case AP_DEVICE_TYPE_CEX3A:
  302. *q_fac |= 0x08000000;
  303. break;
  304. case AP_DEVICE_TYPE_CEX2C:
  305. case AP_DEVICE_TYPE_CEX3C:
  306. *q_fac |= 0x10000000;
  307. break;
  308. default:
  309. break;
  310. }
  311. return true;
  312. default:
  313. /*
  314. * A response code which indicates, there is no info available.
  315. */
  316. return false;
  317. }
  318. }
  319. void ap_wait(enum ap_sm_wait wait)
  320. {
  321. ktime_t hr_time;
  322. switch (wait) {
  323. case AP_SM_WAIT_AGAIN:
  324. case AP_SM_WAIT_INTERRUPT:
  325. if (ap_irq_flag)
  326. break;
  327. if (ap_poll_kthread) {
  328. wake_up(&ap_poll_wait);
  329. break;
  330. }
  331. fallthrough;
  332. case AP_SM_WAIT_TIMEOUT:
  333. spin_lock_bh(&ap_poll_timer_lock);
  334. if (!hrtimer_is_queued(&ap_poll_timer)) {
  335. hr_time = poll_timeout;
  336. hrtimer_forward_now(&ap_poll_timer, hr_time);
  337. hrtimer_restart(&ap_poll_timer);
  338. }
  339. spin_unlock_bh(&ap_poll_timer_lock);
  340. break;
  341. case AP_SM_WAIT_NONE:
  342. default:
  343. break;
  344. }
  345. }
  346. /**
  347. * ap_request_timeout(): Handling of request timeouts
  348. * @t: timer making this callback
  349. *
  350. * Handles request timeouts.
  351. */
  352. void ap_request_timeout(struct timer_list *t)
  353. {
  354. struct ap_queue *aq = from_timer(aq, t, timeout);
  355. spin_lock_bh(&aq->lock);
  356. ap_wait(ap_sm_event(aq, AP_SM_EVENT_TIMEOUT));
  357. spin_unlock_bh(&aq->lock);
  358. }
  359. /**
  360. * ap_poll_timeout(): AP receive polling for finished AP requests.
  361. * @unused: Unused pointer.
  362. *
  363. * Schedules the AP tasklet using a high resolution timer.
  364. */
  365. static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
  366. {
  367. tasklet_schedule(&ap_tasklet);
  368. return HRTIMER_NORESTART;
  369. }
  370. /**
  371. * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
  372. * @airq: pointer to adapter interrupt descriptor
  373. */
  374. static void ap_interrupt_handler(struct airq_struct *airq, bool floating)
  375. {
  376. inc_irq_stat(IRQIO_APB);
  377. tasklet_schedule(&ap_tasklet);
  378. }
  379. /**
  380. * ap_tasklet_fn(): Tasklet to poll all AP devices.
  381. * @dummy: Unused variable
  382. *
  383. * Poll all AP devices on the bus.
  384. */
  385. static void ap_tasklet_fn(unsigned long dummy)
  386. {
  387. int bkt;
  388. struct ap_queue *aq;
  389. enum ap_sm_wait wait = AP_SM_WAIT_NONE;
  390. /* Reset the indicator if interrupts are used. Thus new interrupts can
  391. * be received. Doing it in the beginning of the tasklet is therefor
  392. * important that no requests on any AP get lost.
  393. */
  394. if (ap_irq_flag)
  395. xchg(ap_airq.lsi_ptr, 0);
  396. spin_lock_bh(&ap_queues_lock);
  397. hash_for_each(ap_queues, bkt, aq, hnode) {
  398. spin_lock_bh(&aq->lock);
  399. wait = min(wait, ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
  400. spin_unlock_bh(&aq->lock);
  401. }
  402. spin_unlock_bh(&ap_queues_lock);
  403. ap_wait(wait);
  404. }
  405. static int ap_pending_requests(void)
  406. {
  407. int bkt;
  408. struct ap_queue *aq;
  409. spin_lock_bh(&ap_queues_lock);
  410. hash_for_each(ap_queues, bkt, aq, hnode) {
  411. if (aq->queue_count == 0)
  412. continue;
  413. spin_unlock_bh(&ap_queues_lock);
  414. return 1;
  415. }
  416. spin_unlock_bh(&ap_queues_lock);
  417. return 0;
  418. }
  419. /**
  420. * ap_poll_thread(): Thread that polls for finished requests.
  421. * @data: Unused pointer
  422. *
  423. * AP bus poll thread. The purpose of this thread is to poll for
  424. * finished requests in a loop if there is a "free" cpu - that is
  425. * a cpu that doesn't have anything better to do. The polling stops
  426. * as soon as there is another task or if all messages have been
  427. * delivered.
  428. */
  429. static int ap_poll_thread(void *data)
  430. {
  431. DECLARE_WAITQUEUE(wait, current);
  432. set_user_nice(current, MAX_NICE);
  433. set_freezable();
  434. while (!kthread_should_stop()) {
  435. add_wait_queue(&ap_poll_wait, &wait);
  436. set_current_state(TASK_INTERRUPTIBLE);
  437. if (!ap_pending_requests()) {
  438. schedule();
  439. try_to_freeze();
  440. }
  441. set_current_state(TASK_RUNNING);
  442. remove_wait_queue(&ap_poll_wait, &wait);
  443. if (need_resched()) {
  444. schedule();
  445. try_to_freeze();
  446. continue;
  447. }
  448. ap_tasklet_fn(0);
  449. }
  450. return 0;
  451. }
  452. static int ap_poll_thread_start(void)
  453. {
  454. int rc;
  455. if (ap_irq_flag || ap_poll_kthread)
  456. return 0;
  457. mutex_lock(&ap_poll_thread_mutex);
  458. ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
  459. rc = PTR_ERR_OR_ZERO(ap_poll_kthread);
  460. if (rc)
  461. ap_poll_kthread = NULL;
  462. mutex_unlock(&ap_poll_thread_mutex);
  463. return rc;
  464. }
  465. static void ap_poll_thread_stop(void)
  466. {
  467. if (!ap_poll_kthread)
  468. return;
  469. mutex_lock(&ap_poll_thread_mutex);
  470. kthread_stop(ap_poll_kthread);
  471. ap_poll_kthread = NULL;
  472. mutex_unlock(&ap_poll_thread_mutex);
  473. }
  474. #define is_card_dev(x) ((x)->parent == ap_root_device)
  475. #define is_queue_dev(x) ((x)->parent != ap_root_device)
  476. /**
  477. * ap_bus_match()
  478. * @dev: Pointer to device
  479. * @drv: Pointer to device_driver
  480. *
  481. * AP bus driver registration/unregistration.
  482. */
  483. static int ap_bus_match(struct device *dev, struct device_driver *drv)
  484. {
  485. struct ap_driver *ap_drv = to_ap_drv(drv);
  486. struct ap_device_id *id;
  487. /*
  488. * Compare device type of the device with the list of
  489. * supported types of the device_driver.
  490. */
  491. for (id = ap_drv->ids; id->match_flags; id++) {
  492. if (is_card_dev(dev) &&
  493. id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
  494. id->dev_type == to_ap_dev(dev)->device_type)
  495. return 1;
  496. if (is_queue_dev(dev) &&
  497. id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
  498. id->dev_type == to_ap_dev(dev)->device_type)
  499. return 1;
  500. }
  501. return 0;
  502. }
  503. /**
  504. * ap_uevent(): Uevent function for AP devices.
  505. * @dev: Pointer to device
  506. * @env: Pointer to kobj_uevent_env
  507. *
  508. * It sets up a single environment variable DEV_TYPE which contains the
  509. * hardware device type.
  510. */
  511. static int ap_uevent(struct device *dev, struct kobj_uevent_env *env)
  512. {
  513. struct ap_device *ap_dev = to_ap_dev(dev);
  514. int retval = 0;
  515. if (!ap_dev)
  516. return -ENODEV;
  517. /* Set up DEV_TYPE environment variable. */
  518. retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
  519. if (retval)
  520. return retval;
  521. /* Add MODALIAS= */
  522. retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
  523. return retval;
  524. }
  525. static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
  526. {
  527. if (is_queue_dev(dev) &&
  528. AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
  529. device_unregister(dev);
  530. return 0;
  531. }
  532. static struct bus_type ap_bus_type = {
  533. .name = "ap",
  534. .match = &ap_bus_match,
  535. .uevent = &ap_uevent,
  536. };
  537. static int __ap_revise_reserved(struct device *dev, void *dummy)
  538. {
  539. int rc, card, queue, devres, drvres;
  540. if (is_queue_dev(dev)) {
  541. card = AP_QID_CARD(to_ap_queue(dev)->qid);
  542. queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
  543. mutex_lock(&ap_perms_mutex);
  544. devres = test_bit_inv(card, ap_perms.apm)
  545. && test_bit_inv(queue, ap_perms.aqm);
  546. mutex_unlock(&ap_perms_mutex);
  547. drvres = to_ap_drv(dev->driver)->flags
  548. & AP_DRIVER_FLAG_DEFAULT;
  549. if (!!devres != !!drvres) {
  550. AP_DBF_DBG("reprobing queue=%02x.%04x\n",
  551. card, queue);
  552. rc = device_reprobe(dev);
  553. }
  554. }
  555. return 0;
  556. }
  557. static void ap_bus_revise_bindings(void)
  558. {
  559. bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
  560. }
  561. int ap_owned_by_def_drv(int card, int queue)
  562. {
  563. int rc = 0;
  564. if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
  565. return -EINVAL;
  566. mutex_lock(&ap_perms_mutex);
  567. if (test_bit_inv(card, ap_perms.apm)
  568. && test_bit_inv(queue, ap_perms.aqm))
  569. rc = 1;
  570. mutex_unlock(&ap_perms_mutex);
  571. return rc;
  572. }
  573. EXPORT_SYMBOL(ap_owned_by_def_drv);
  574. int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
  575. unsigned long *aqm)
  576. {
  577. int card, queue, rc = 0;
  578. mutex_lock(&ap_perms_mutex);
  579. for (card = 0; !rc && card < AP_DEVICES; card++)
  580. if (test_bit_inv(card, apm) &&
  581. test_bit_inv(card, ap_perms.apm))
  582. for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
  583. if (test_bit_inv(queue, aqm) &&
  584. test_bit_inv(queue, ap_perms.aqm))
  585. rc = 1;
  586. mutex_unlock(&ap_perms_mutex);
  587. return rc;
  588. }
  589. EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
  590. static int ap_device_probe(struct device *dev)
  591. {
  592. struct ap_device *ap_dev = to_ap_dev(dev);
  593. struct ap_driver *ap_drv = to_ap_drv(dev->driver);
  594. int card, queue, devres, drvres, rc = -ENODEV;
  595. if (!get_device(dev))
  596. return rc;
  597. if (is_queue_dev(dev)) {
  598. /*
  599. * If the apqn is marked as reserved/used by ap bus and
  600. * default drivers, only probe with drivers with the default
  601. * flag set. If it is not marked, only probe with drivers
  602. * with the default flag not set.
  603. */
  604. card = AP_QID_CARD(to_ap_queue(dev)->qid);
  605. queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
  606. mutex_lock(&ap_perms_mutex);
  607. devres = test_bit_inv(card, ap_perms.apm)
  608. && test_bit_inv(queue, ap_perms.aqm);
  609. mutex_unlock(&ap_perms_mutex);
  610. drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
  611. if (!!devres != !!drvres)
  612. goto out;
  613. }
  614. /* Add queue/card to list of active queues/cards */
  615. spin_lock_bh(&ap_queues_lock);
  616. if (is_queue_dev(dev))
  617. hash_add(ap_queues, &to_ap_queue(dev)->hnode,
  618. to_ap_queue(dev)->qid);
  619. spin_unlock_bh(&ap_queues_lock);
  620. ap_dev->drv = ap_drv;
  621. rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
  622. if (rc) {
  623. spin_lock_bh(&ap_queues_lock);
  624. if (is_queue_dev(dev))
  625. hash_del(&to_ap_queue(dev)->hnode);
  626. spin_unlock_bh(&ap_queues_lock);
  627. ap_dev->drv = NULL;
  628. }
  629. out:
  630. if (rc)
  631. put_device(dev);
  632. return rc;
  633. }
  634. static int ap_device_remove(struct device *dev)
  635. {
  636. struct ap_device *ap_dev = to_ap_dev(dev);
  637. struct ap_driver *ap_drv = ap_dev->drv;
  638. /* prepare ap queue device removal */
  639. if (is_queue_dev(dev))
  640. ap_queue_prepare_remove(to_ap_queue(dev));
  641. /* driver's chance to clean up gracefully */
  642. if (ap_drv->remove)
  643. ap_drv->remove(ap_dev);
  644. /* now do the ap queue device remove */
  645. if (is_queue_dev(dev))
  646. ap_queue_remove(to_ap_queue(dev));
  647. /* Remove queue/card from list of active queues/cards */
  648. spin_lock_bh(&ap_queues_lock);
  649. if (is_queue_dev(dev))
  650. hash_del(&to_ap_queue(dev)->hnode);
  651. spin_unlock_bh(&ap_queues_lock);
  652. put_device(dev);
  653. return 0;
  654. }
  655. struct ap_queue *ap_get_qdev(ap_qid_t qid)
  656. {
  657. int bkt;
  658. struct ap_queue *aq;
  659. spin_lock_bh(&ap_queues_lock);
  660. hash_for_each(ap_queues, bkt, aq, hnode) {
  661. if (aq->qid == qid) {
  662. get_device(&aq->ap_dev.device);
  663. spin_unlock_bh(&ap_queues_lock);
  664. return aq;
  665. }
  666. }
  667. spin_unlock_bh(&ap_queues_lock);
  668. return NULL;
  669. }
  670. EXPORT_SYMBOL(ap_get_qdev);
  671. int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
  672. char *name)
  673. {
  674. struct device_driver *drv = &ap_drv->driver;
  675. drv->bus = &ap_bus_type;
  676. drv->probe = ap_device_probe;
  677. drv->remove = ap_device_remove;
  678. drv->owner = owner;
  679. drv->name = name;
  680. return driver_register(drv);
  681. }
  682. EXPORT_SYMBOL(ap_driver_register);
  683. void ap_driver_unregister(struct ap_driver *ap_drv)
  684. {
  685. driver_unregister(&ap_drv->driver);
  686. }
  687. EXPORT_SYMBOL(ap_driver_unregister);
  688. void ap_bus_force_rescan(void)
  689. {
  690. /* processing a asynchronous bus rescan */
  691. del_timer(&ap_config_timer);
  692. queue_work(system_long_wq, &ap_scan_work);
  693. flush_work(&ap_scan_work);
  694. }
  695. EXPORT_SYMBOL(ap_bus_force_rescan);
  696. /*
  697. * A config change has happened, force an ap bus rescan.
  698. */
  699. void ap_bus_cfg_chg(void)
  700. {
  701. AP_DBF_DBG("%s config change, forcing bus rescan\n", __func__);
  702. ap_bus_force_rescan();
  703. }
  704. /*
  705. * hex2bitmap() - parse hex mask string and set bitmap.
  706. * Valid strings are "0x012345678" with at least one valid hex number.
  707. * Rest of the bitmap to the right is padded with 0. No spaces allowed
  708. * within the string, the leading 0x may be omitted.
  709. * Returns the bitmask with exactly the bits set as given by the hex
  710. * string (both in big endian order).
  711. */
  712. static int hex2bitmap(const char *str, unsigned long *bitmap, int bits)
  713. {
  714. int i, n, b;
  715. /* bits needs to be a multiple of 8 */
  716. if (bits & 0x07)
  717. return -EINVAL;
  718. if (str[0] == '0' && str[1] == 'x')
  719. str++;
  720. if (*str == 'x')
  721. str++;
  722. for (i = 0; isxdigit(*str) && i < bits; str++) {
  723. b = hex_to_bin(*str);
  724. for (n = 0; n < 4; n++)
  725. if (b & (0x08 >> n))
  726. set_bit_inv(i + n, bitmap);
  727. i += 4;
  728. }
  729. if (*str == '\n')
  730. str++;
  731. if (*str)
  732. return -EINVAL;
  733. return 0;
  734. }
  735. /*
  736. * modify_bitmap() - parse bitmask argument and modify an existing
  737. * bit mask accordingly. A concatenation (done with ',') of these
  738. * terms is recognized:
  739. * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
  740. * <bitnr> may be any valid number (hex, decimal or octal) in the range
  741. * 0...bits-1; the leading + or - is required. Here are some examples:
  742. * +0-15,+32,-128,-0xFF
  743. * -0-255,+1-16,+0x128
  744. * +1,+2,+3,+4,-5,-7-10
  745. * Returns the new bitmap after all changes have been applied. Every
  746. * positive value in the string will set a bit and every negative value
  747. * in the string will clear a bit. As a bit may be touched more than once,
  748. * the last 'operation' wins:
  749. * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
  750. * cleared again. All other bits are unmodified.
  751. */
  752. static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
  753. {
  754. int a, i, z;
  755. char *np, sign;
  756. /* bits needs to be a multiple of 8 */
  757. if (bits & 0x07)
  758. return -EINVAL;
  759. while (*str) {
  760. sign = *str++;
  761. if (sign != '+' && sign != '-')
  762. return -EINVAL;
  763. a = z = simple_strtoul(str, &np, 0);
  764. if (str == np || a >= bits)
  765. return -EINVAL;
  766. str = np;
  767. if (*str == '-') {
  768. z = simple_strtoul(++str, &np, 0);
  769. if (str == np || a > z || z >= bits)
  770. return -EINVAL;
  771. str = np;
  772. }
  773. for (i = a; i <= z; i++)
  774. if (sign == '+')
  775. set_bit_inv(i, bitmap);
  776. else
  777. clear_bit_inv(i, bitmap);
  778. while (*str == ',' || *str == '\n')
  779. str++;
  780. }
  781. return 0;
  782. }
  783. int ap_parse_mask_str(const char *str,
  784. unsigned long *bitmap, int bits,
  785. struct mutex *lock)
  786. {
  787. unsigned long *newmap, size;
  788. int rc;
  789. /* bits needs to be a multiple of 8 */
  790. if (bits & 0x07)
  791. return -EINVAL;
  792. size = BITS_TO_LONGS(bits)*sizeof(unsigned long);
  793. newmap = kmalloc(size, GFP_KERNEL);
  794. if (!newmap)
  795. return -ENOMEM;
  796. if (mutex_lock_interruptible(lock)) {
  797. kfree(newmap);
  798. return -ERESTARTSYS;
  799. }
  800. if (*str == '+' || *str == '-') {
  801. memcpy(newmap, bitmap, size);
  802. rc = modify_bitmap(str, newmap, bits);
  803. } else {
  804. memset(newmap, 0, size);
  805. rc = hex2bitmap(str, newmap, bits);
  806. }
  807. if (rc == 0)
  808. memcpy(bitmap, newmap, size);
  809. mutex_unlock(lock);
  810. kfree(newmap);
  811. return rc;
  812. }
  813. EXPORT_SYMBOL(ap_parse_mask_str);
  814. /*
  815. * AP bus attributes.
  816. */
  817. static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
  818. {
  819. return scnprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
  820. }
  821. static ssize_t ap_domain_store(struct bus_type *bus,
  822. const char *buf, size_t count)
  823. {
  824. int domain;
  825. if (sscanf(buf, "%i\n", &domain) != 1 ||
  826. domain < 0 || domain > ap_max_domain_id ||
  827. !test_bit_inv(domain, ap_perms.aqm))
  828. return -EINVAL;
  829. spin_lock_bh(&ap_domain_lock);
  830. ap_domain_index = domain;
  831. spin_unlock_bh(&ap_domain_lock);
  832. AP_DBF_INFO("stored new default domain=%d\n", domain);
  833. return count;
  834. }
  835. static BUS_ATTR_RW(ap_domain);
  836. static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
  837. {
  838. if (!ap_qci_info) /* QCI not supported */
  839. return scnprintf(buf, PAGE_SIZE, "not supported\n");
  840. return scnprintf(buf, PAGE_SIZE,
  841. "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
  842. ap_qci_info->adm[0], ap_qci_info->adm[1],
  843. ap_qci_info->adm[2], ap_qci_info->adm[3],
  844. ap_qci_info->adm[4], ap_qci_info->adm[5],
  845. ap_qci_info->adm[6], ap_qci_info->adm[7]);
  846. }
  847. static BUS_ATTR_RO(ap_control_domain_mask);
  848. static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
  849. {
  850. if (!ap_qci_info) /* QCI not supported */
  851. return scnprintf(buf, PAGE_SIZE, "not supported\n");
  852. return scnprintf(buf, PAGE_SIZE,
  853. "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
  854. ap_qci_info->aqm[0], ap_qci_info->aqm[1],
  855. ap_qci_info->aqm[2], ap_qci_info->aqm[3],
  856. ap_qci_info->aqm[4], ap_qci_info->aqm[5],
  857. ap_qci_info->aqm[6], ap_qci_info->aqm[7]);
  858. }
  859. static BUS_ATTR_RO(ap_usage_domain_mask);
  860. static ssize_t ap_adapter_mask_show(struct bus_type *bus, char *buf)
  861. {
  862. if (!ap_qci_info) /* QCI not supported */
  863. return scnprintf(buf, PAGE_SIZE, "not supported\n");
  864. return scnprintf(buf, PAGE_SIZE,
  865. "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
  866. ap_qci_info->apm[0], ap_qci_info->apm[1],
  867. ap_qci_info->apm[2], ap_qci_info->apm[3],
  868. ap_qci_info->apm[4], ap_qci_info->apm[5],
  869. ap_qci_info->apm[6], ap_qci_info->apm[7]);
  870. }
  871. static BUS_ATTR_RO(ap_adapter_mask);
  872. static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
  873. {
  874. return scnprintf(buf, PAGE_SIZE, "%d\n",
  875. ap_irq_flag ? 1 : 0);
  876. }
  877. static BUS_ATTR_RO(ap_interrupts);
  878. static ssize_t config_time_show(struct bus_type *bus, char *buf)
  879. {
  880. return scnprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
  881. }
  882. static ssize_t config_time_store(struct bus_type *bus,
  883. const char *buf, size_t count)
  884. {
  885. int time;
  886. if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
  887. return -EINVAL;
  888. ap_config_time = time;
  889. mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
  890. return count;
  891. }
  892. static BUS_ATTR_RW(config_time);
  893. static ssize_t poll_thread_show(struct bus_type *bus, char *buf)
  894. {
  895. return scnprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
  896. }
  897. static ssize_t poll_thread_store(struct bus_type *bus,
  898. const char *buf, size_t count)
  899. {
  900. int flag, rc;
  901. if (sscanf(buf, "%d\n", &flag) != 1)
  902. return -EINVAL;
  903. if (flag) {
  904. rc = ap_poll_thread_start();
  905. if (rc)
  906. count = rc;
  907. } else
  908. ap_poll_thread_stop();
  909. return count;
  910. }
  911. static BUS_ATTR_RW(poll_thread);
  912. static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
  913. {
  914. return scnprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
  915. }
  916. static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
  917. size_t count)
  918. {
  919. unsigned long long time;
  920. ktime_t hr_time;
  921. /* 120 seconds = maximum poll interval */
  922. if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
  923. time > 120000000000ULL)
  924. return -EINVAL;
  925. poll_timeout = time;
  926. hr_time = poll_timeout;
  927. spin_lock_bh(&ap_poll_timer_lock);
  928. hrtimer_cancel(&ap_poll_timer);
  929. hrtimer_set_expires(&ap_poll_timer, hr_time);
  930. hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
  931. spin_unlock_bh(&ap_poll_timer_lock);
  932. return count;
  933. }
  934. static BUS_ATTR_RW(poll_timeout);
  935. static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
  936. {
  937. return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_domain_id);
  938. }
  939. static BUS_ATTR_RO(ap_max_domain_id);
  940. static ssize_t ap_max_adapter_id_show(struct bus_type *bus, char *buf)
  941. {
  942. return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_adapter_id);
  943. }
  944. static BUS_ATTR_RO(ap_max_adapter_id);
  945. static ssize_t apmask_show(struct bus_type *bus, char *buf)
  946. {
  947. int rc;
  948. if (mutex_lock_interruptible(&ap_perms_mutex))
  949. return -ERESTARTSYS;
  950. rc = scnprintf(buf, PAGE_SIZE,
  951. "0x%016lx%016lx%016lx%016lx\n",
  952. ap_perms.apm[0], ap_perms.apm[1],
  953. ap_perms.apm[2], ap_perms.apm[3]);
  954. mutex_unlock(&ap_perms_mutex);
  955. return rc;
  956. }
  957. static ssize_t apmask_store(struct bus_type *bus, const char *buf,
  958. size_t count)
  959. {
  960. int rc;
  961. rc = ap_parse_mask_str(buf, ap_perms.apm, AP_DEVICES, &ap_perms_mutex);
  962. if (rc)
  963. return rc;
  964. ap_bus_revise_bindings();
  965. return count;
  966. }
  967. static BUS_ATTR_RW(apmask);
  968. static ssize_t aqmask_show(struct bus_type *bus, char *buf)
  969. {
  970. int rc;
  971. if (mutex_lock_interruptible(&ap_perms_mutex))
  972. return -ERESTARTSYS;
  973. rc = scnprintf(buf, PAGE_SIZE,
  974. "0x%016lx%016lx%016lx%016lx\n",
  975. ap_perms.aqm[0], ap_perms.aqm[1],
  976. ap_perms.aqm[2], ap_perms.aqm[3]);
  977. mutex_unlock(&ap_perms_mutex);
  978. return rc;
  979. }
  980. static ssize_t aqmask_store(struct bus_type *bus, const char *buf,
  981. size_t count)
  982. {
  983. int rc;
  984. rc = ap_parse_mask_str(buf, ap_perms.aqm, AP_DOMAINS, &ap_perms_mutex);
  985. if (rc)
  986. return rc;
  987. ap_bus_revise_bindings();
  988. return count;
  989. }
  990. static BUS_ATTR_RW(aqmask);
  991. static struct bus_attribute *const ap_bus_attrs[] = {
  992. &bus_attr_ap_domain,
  993. &bus_attr_ap_control_domain_mask,
  994. &bus_attr_ap_usage_domain_mask,
  995. &bus_attr_ap_adapter_mask,
  996. &bus_attr_config_time,
  997. &bus_attr_poll_thread,
  998. &bus_attr_ap_interrupts,
  999. &bus_attr_poll_timeout,
  1000. &bus_attr_ap_max_domain_id,
  1001. &bus_attr_ap_max_adapter_id,
  1002. &bus_attr_apmask,
  1003. &bus_attr_aqmask,
  1004. NULL,
  1005. };
  1006. /**
  1007. * ap_select_domain(): Select an AP domain if possible and we haven't
  1008. * already done so before.
  1009. */
  1010. static void ap_select_domain(void)
  1011. {
  1012. struct ap_queue_status status;
  1013. int card, dom;
  1014. /*
  1015. * Choose the default domain. Either the one specified with
  1016. * the "domain=" parameter or the first domain with at least
  1017. * one valid APQN.
  1018. */
  1019. spin_lock_bh(&ap_domain_lock);
  1020. if (ap_domain_index >= 0) {
  1021. /* Domain has already been selected. */
  1022. goto out;
  1023. }
  1024. for (dom = 0; dom <= ap_max_domain_id; dom++) {
  1025. if (!ap_test_config_usage_domain(dom) ||
  1026. !test_bit_inv(dom, ap_perms.aqm))
  1027. continue;
  1028. for (card = 0; card <= ap_max_adapter_id; card++) {
  1029. if (!ap_test_config_card_id(card) ||
  1030. !test_bit_inv(card, ap_perms.apm))
  1031. continue;
  1032. status = ap_test_queue(AP_MKQID(card, dom),
  1033. ap_apft_available(),
  1034. NULL);
  1035. if (status.response_code == AP_RESPONSE_NORMAL)
  1036. break;
  1037. }
  1038. if (card <= ap_max_adapter_id)
  1039. break;
  1040. }
  1041. if (dom <= ap_max_domain_id) {
  1042. ap_domain_index = dom;
  1043. AP_DBF_INFO("%s new default domain is %d\n",
  1044. __func__, ap_domain_index);
  1045. }
  1046. out:
  1047. spin_unlock_bh(&ap_domain_lock);
  1048. }
  1049. /*
  1050. * This function checks the type and returns either 0 for not
  1051. * supported or the highest compatible type value (which may
  1052. * include the input type value).
  1053. */
  1054. static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
  1055. {
  1056. int comp_type = 0;
  1057. /* < CEX2A is not supported */
  1058. if (rawtype < AP_DEVICE_TYPE_CEX2A) {
  1059. AP_DBF_WARN("get_comp_type queue=%02x.%04x unsupported type %d\n",
  1060. AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
  1061. return 0;
  1062. }
  1063. /* up to CEX7 known and fully supported */
  1064. if (rawtype <= AP_DEVICE_TYPE_CEX7)
  1065. return rawtype;
  1066. /*
  1067. * unknown new type > CEX7, check for compatibility
  1068. * to the highest known and supported type which is
  1069. * currently CEX7 with the help of the QACT function.
  1070. */
  1071. if (ap_qact_available()) {
  1072. struct ap_queue_status status;
  1073. union ap_qact_ap_info apinfo = {0};
  1074. apinfo.mode = (func >> 26) & 0x07;
  1075. apinfo.cat = AP_DEVICE_TYPE_CEX7;
  1076. status = ap_qact(qid, 0, &apinfo);
  1077. if (status.response_code == AP_RESPONSE_NORMAL
  1078. && apinfo.cat >= AP_DEVICE_TYPE_CEX2A
  1079. && apinfo.cat <= AP_DEVICE_TYPE_CEX7)
  1080. comp_type = apinfo.cat;
  1081. }
  1082. if (!comp_type)
  1083. AP_DBF_WARN("get_comp_type queue=%02x.%04x unable to map type %d\n",
  1084. AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
  1085. else if (comp_type != rawtype)
  1086. AP_DBF_INFO("get_comp_type queue=%02x.%04x map type %d to %d\n",
  1087. AP_QID_CARD(qid), AP_QID_QUEUE(qid),
  1088. rawtype, comp_type);
  1089. return comp_type;
  1090. }
  1091. /*
  1092. * Helper function to be used with bus_find_dev
  1093. * matches for the card device with the given id
  1094. */
  1095. static int __match_card_device_with_id(struct device *dev, const void *data)
  1096. {
  1097. return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *) data;
  1098. }
  1099. /*
  1100. * Helper function to be used with bus_find_dev
  1101. * matches for the queue device with a given qid
  1102. */
  1103. static int __match_queue_device_with_qid(struct device *dev, const void *data)
  1104. {
  1105. return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
  1106. }
  1107. /*
  1108. * Helper function to be used with bus_find_dev
  1109. * matches any queue device with given queue id
  1110. */
  1111. static int __match_queue_device_with_queue_id(struct device *dev, const void *data)
  1112. {
  1113. return is_queue_dev(dev)
  1114. && AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long) data;
  1115. }
  1116. /*
  1117. * Helper function for ap_scan_bus().
  1118. * Remove card device and associated queue devices.
  1119. */
  1120. static inline void ap_scan_rm_card_dev_and_queue_devs(struct ap_card *ac)
  1121. {
  1122. bus_for_each_dev(&ap_bus_type, NULL,
  1123. (void *)(long) ac->id,
  1124. __ap_queue_devices_with_id_unregister);
  1125. device_unregister(&ac->ap_dev.device);
  1126. }
  1127. /*
  1128. * Helper function for ap_scan_bus().
  1129. * Does the scan bus job for all the domains within
  1130. * a valid adapter given by an ap_card ptr.
  1131. */
  1132. static inline void ap_scan_domains(struct ap_card *ac)
  1133. {
  1134. bool decfg;
  1135. ap_qid_t qid;
  1136. unsigned int func;
  1137. struct device *dev;
  1138. struct ap_queue *aq;
  1139. int rc, dom, depth, type;
  1140. /*
  1141. * Go through the configuration for the domains and compare them
  1142. * to the existing queue devices. Also take care of the config
  1143. * and error state for the queue devices.
  1144. */
  1145. for (dom = 0; dom <= ap_max_domain_id; dom++) {
  1146. qid = AP_MKQID(ac->id, dom);
  1147. dev = bus_find_device(&ap_bus_type, NULL,
  1148. (void *)(long) qid,
  1149. __match_queue_device_with_qid);
  1150. aq = dev ? to_ap_queue(dev) : NULL;
  1151. if (!ap_test_config_usage_domain(dom)) {
  1152. if (dev) {
  1153. AP_DBF_INFO("%s(%d,%d) not in config any more, rm queue device\n",
  1154. __func__, ac->id, dom);
  1155. device_unregister(dev);
  1156. put_device(dev);
  1157. }
  1158. continue;
  1159. }
  1160. /* domain is valid, get info from this APQN */
  1161. if (!ap_queue_info(qid, &type, &func, &depth, &decfg)) {
  1162. if (aq) {
  1163. AP_DBF_INFO(
  1164. "%s(%d,%d) ap_queue_info() not successful, rm queue device\n",
  1165. __func__, ac->id, dom);
  1166. device_unregister(dev);
  1167. put_device(dev);
  1168. }
  1169. continue;
  1170. }
  1171. /* if no queue device exists, create a new one */
  1172. if (!aq) {
  1173. aq = ap_queue_create(qid, ac->ap_dev.device_type);
  1174. if (!aq) {
  1175. AP_DBF_WARN("%s(%d,%d) ap_queue_create() failed\n",
  1176. __func__, ac->id, dom);
  1177. continue;
  1178. }
  1179. aq->card = ac;
  1180. aq->config = !decfg;
  1181. dev = &aq->ap_dev.device;
  1182. dev->bus = &ap_bus_type;
  1183. dev->parent = &ac->ap_dev.device;
  1184. dev_set_name(dev, "%02x.%04x", ac->id, dom);
  1185. /* register queue device */
  1186. rc = device_register(dev);
  1187. if (rc) {
  1188. AP_DBF_WARN("%s(%d,%d) device_register() failed\n",
  1189. __func__, ac->id, dom);
  1190. goto put_dev_and_continue;
  1191. }
  1192. /* get it and thus adjust reference counter */
  1193. get_device(dev);
  1194. if (decfg)
  1195. AP_DBF_INFO("%s(%d,%d) new (decfg) queue device created\n",
  1196. __func__, ac->id, dom);
  1197. else
  1198. AP_DBF_INFO("%s(%d,%d) new queue device created\n",
  1199. __func__, ac->id, dom);
  1200. goto put_dev_and_continue;
  1201. }
  1202. /* Check config state on the already existing queue device */
  1203. spin_lock_bh(&aq->lock);
  1204. if (decfg && aq->config) {
  1205. /* config off this queue device */
  1206. aq->config = false;
  1207. if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
  1208. aq->dev_state = AP_DEV_STATE_ERROR;
  1209. aq->last_err_rc = AP_RESPONSE_DECONFIGURED;
  1210. }
  1211. spin_unlock_bh(&aq->lock);
  1212. AP_DBF_INFO("%s(%d,%d) queue device config off\n",
  1213. __func__, ac->id, dom);
  1214. /* 'receive' pending messages with -EAGAIN */
  1215. ap_flush_queue(aq);
  1216. goto put_dev_and_continue;
  1217. }
  1218. if (!decfg && !aq->config) {
  1219. /* config on this queue device */
  1220. aq->config = true;
  1221. if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
  1222. aq->dev_state = AP_DEV_STATE_OPERATING;
  1223. aq->sm_state = AP_SM_STATE_RESET_START;
  1224. }
  1225. spin_unlock_bh(&aq->lock);
  1226. AP_DBF_INFO("%s(%d,%d) queue device config on\n",
  1227. __func__, ac->id, dom);
  1228. goto put_dev_and_continue;
  1229. }
  1230. /* handle other error states */
  1231. if (!decfg && aq->dev_state == AP_DEV_STATE_ERROR) {
  1232. spin_unlock_bh(&aq->lock);
  1233. /* 'receive' pending messages with -EAGAIN */
  1234. ap_flush_queue(aq);
  1235. /* re-init (with reset) the queue device */
  1236. ap_queue_init_state(aq);
  1237. AP_DBF_INFO("%s(%d,%d) queue device reinit enforced\n",
  1238. __func__, ac->id, dom);
  1239. goto put_dev_and_continue;
  1240. }
  1241. spin_unlock_bh(&aq->lock);
  1242. put_dev_and_continue:
  1243. put_device(dev);
  1244. }
  1245. }
  1246. /*
  1247. * Helper function for ap_scan_bus().
  1248. * Does the scan bus job for the given adapter id.
  1249. */
  1250. static inline void ap_scan_adapter(int ap)
  1251. {
  1252. bool decfg;
  1253. ap_qid_t qid;
  1254. unsigned int func;
  1255. struct device *dev;
  1256. struct ap_card *ac;
  1257. int rc, dom, depth, type, comp_type;
  1258. /* Is there currently a card device for this adapter ? */
  1259. dev = bus_find_device(&ap_bus_type, NULL,
  1260. (void *)(long) ap,
  1261. __match_card_device_with_id);
  1262. ac = dev ? to_ap_card(dev) : NULL;
  1263. /* Adapter not in configuration ? */
  1264. if (!ap_test_config_card_id(ap)) {
  1265. if (ac) {
  1266. AP_DBF_INFO("%s(%d) ap not in config any more, rm card and queue devices\n",
  1267. __func__, ap);
  1268. ap_scan_rm_card_dev_and_queue_devs(ac);
  1269. put_device(dev);
  1270. }
  1271. return;
  1272. }
  1273. /*
  1274. * Adapter ap is valid in the current configuration. So do some checks:
  1275. * If no card device exists, build one. If a card device exists, check
  1276. * for type and functions changed. For all this we need to find a valid
  1277. * APQN first.
  1278. */
  1279. for (dom = 0; dom <= ap_max_domain_id; dom++)
  1280. if (ap_test_config_usage_domain(dom)) {
  1281. qid = AP_MKQID(ap, dom);
  1282. if (ap_queue_info(qid, &type, &func, &depth, &decfg))
  1283. break;
  1284. }
  1285. if (dom > ap_max_domain_id) {
  1286. /* Could not find a valid APQN for this adapter */
  1287. if (ac) {
  1288. AP_DBF_INFO(
  1289. "%s(%d) no type info (no APQN found), rm card and queue devices\n",
  1290. __func__, ap);
  1291. ap_scan_rm_card_dev_and_queue_devs(ac);
  1292. put_device(dev);
  1293. } else {
  1294. AP_DBF_DBG("%s(%d) no type info (no APQN found), ignored\n",
  1295. __func__, ap);
  1296. }
  1297. return;
  1298. }
  1299. if (!type) {
  1300. /* No apdater type info available, an unusable adapter */
  1301. if (ac) {
  1302. AP_DBF_INFO("%s(%d) no valid type (0) info, rm card and queue devices\n",
  1303. __func__, ap);
  1304. ap_scan_rm_card_dev_and_queue_devs(ac);
  1305. put_device(dev);
  1306. } else {
  1307. AP_DBF_DBG("%s(%d) no valid type (0) info, ignored\n",
  1308. __func__, ap);
  1309. }
  1310. return;
  1311. }
  1312. if (ac) {
  1313. /* Check APQN against existing card device for changes */
  1314. if (ac->raw_hwtype != type) {
  1315. AP_DBF_INFO("%s(%d) hwtype %d changed, rm card and queue devices\n",
  1316. __func__, ap, type);
  1317. ap_scan_rm_card_dev_and_queue_devs(ac);
  1318. put_device(dev);
  1319. ac = NULL;
  1320. } else if (ac->functions != func) {
  1321. AP_DBF_INFO("%s(%d) functions 0x%08x changed, rm card and queue devices\n",
  1322. __func__, ap, type);
  1323. ap_scan_rm_card_dev_and_queue_devs(ac);
  1324. put_device(dev);
  1325. ac = NULL;
  1326. } else {
  1327. if (decfg && ac->config) {
  1328. ac->config = false;
  1329. AP_DBF_INFO("%s(%d) card device config off\n",
  1330. __func__, ap);
  1331. }
  1332. if (!decfg && !ac->config) {
  1333. ac->config = true;
  1334. AP_DBF_INFO("%s(%d) card device config on\n",
  1335. __func__, ap);
  1336. }
  1337. }
  1338. }
  1339. if (!ac) {
  1340. /* Build a new card device */
  1341. comp_type = ap_get_compatible_type(qid, type, func);
  1342. if (!comp_type) {
  1343. AP_DBF_WARN("%s(%d) type %d, can't get compatibility type\n",
  1344. __func__, ap, type);
  1345. return;
  1346. }
  1347. ac = ap_card_create(ap, depth, type, comp_type, func);
  1348. if (!ac) {
  1349. AP_DBF_WARN("%s(%d) ap_card_create() failed\n",
  1350. __func__, ap);
  1351. return;
  1352. }
  1353. ac->config = !decfg;
  1354. dev = &ac->ap_dev.device;
  1355. dev->bus = &ap_bus_type;
  1356. dev->parent = ap_root_device;
  1357. dev_set_name(dev, "card%02x", ap);
  1358. /* Register the new card device with AP bus */
  1359. rc = device_register(dev);
  1360. if (rc) {
  1361. AP_DBF_WARN("%s(%d) device_register() failed\n",
  1362. __func__, ap);
  1363. put_device(dev);
  1364. return;
  1365. }
  1366. /* get it and thus adjust reference counter */
  1367. get_device(dev);
  1368. if (decfg)
  1369. AP_DBF_INFO("%s(%d) new (decfg) card device type=%d func=0x%08x created\n",
  1370. __func__, ap, type, func);
  1371. else
  1372. AP_DBF_INFO("%s(%d) new card device type=%d func=0x%08x created\n",
  1373. __func__, ap, type, func);
  1374. }
  1375. /* Verify the domains and the queue devices for this card */
  1376. ap_scan_domains(ac);
  1377. /* release the card device */
  1378. put_device(&ac->ap_dev.device);
  1379. }
  1380. /**
  1381. * ap_scan_bus(): Scan the AP bus for new devices
  1382. * Runs periodically, workqueue timer (ap_config_time)
  1383. */
  1384. static void ap_scan_bus(struct work_struct *unused)
  1385. {
  1386. int ap;
  1387. ap_fetch_qci_info(ap_qci_info);
  1388. ap_select_domain();
  1389. AP_DBF_DBG("%s running\n", __func__);
  1390. /* loop over all possible adapters */
  1391. for (ap = 0; ap <= ap_max_adapter_id; ap++)
  1392. ap_scan_adapter(ap);
  1393. /* check if there is at least one queue available with default domain */
  1394. if (ap_domain_index >= 0) {
  1395. struct device *dev =
  1396. bus_find_device(&ap_bus_type, NULL,
  1397. (void *)(long) ap_domain_index,
  1398. __match_queue_device_with_queue_id);
  1399. if (dev)
  1400. put_device(dev);
  1401. else
  1402. AP_DBF_INFO("no queue device with default domain %d available\n",
  1403. ap_domain_index);
  1404. }
  1405. mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
  1406. }
  1407. static void ap_config_timeout(struct timer_list *unused)
  1408. {
  1409. queue_work(system_long_wq, &ap_scan_work);
  1410. }
  1411. static int __init ap_debug_init(void)
  1412. {
  1413. ap_dbf_info = debug_register("ap", 1, 1,
  1414. DBF_MAX_SPRINTF_ARGS * sizeof(long));
  1415. debug_register_view(ap_dbf_info, &debug_sprintf_view);
  1416. debug_set_level(ap_dbf_info, DBF_ERR);
  1417. return 0;
  1418. }
  1419. static void __init ap_perms_init(void)
  1420. {
  1421. /* all resources useable if no kernel parameter string given */
  1422. memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
  1423. memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
  1424. memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
  1425. /* apm kernel parameter string */
  1426. if (apm_str) {
  1427. memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
  1428. ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
  1429. &ap_perms_mutex);
  1430. }
  1431. /* aqm kernel parameter string */
  1432. if (aqm_str) {
  1433. memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
  1434. ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
  1435. &ap_perms_mutex);
  1436. }
  1437. }
  1438. /**
  1439. * ap_module_init(): The module initialization code.
  1440. *
  1441. * Initializes the module.
  1442. */
  1443. static int __init ap_module_init(void)
  1444. {
  1445. int rc, i;
  1446. rc = ap_debug_init();
  1447. if (rc)
  1448. return rc;
  1449. if (!ap_instructions_available()) {
  1450. pr_warn("The hardware system does not support AP instructions\n");
  1451. return -ENODEV;
  1452. }
  1453. /* init ap_queue hashtable */
  1454. hash_init(ap_queues);
  1455. /* set up the AP permissions (ioctls, ap and aq masks) */
  1456. ap_perms_init();
  1457. /* Get AP configuration data if available */
  1458. ap_init_qci_info();
  1459. /* check default domain setting */
  1460. if (ap_domain_index < -1 || ap_domain_index > ap_max_domain_id ||
  1461. (ap_domain_index >= 0 &&
  1462. !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
  1463. pr_warn("%d is not a valid cryptographic domain\n",
  1464. ap_domain_index);
  1465. ap_domain_index = -1;
  1466. }
  1467. /* enable interrupts if available */
  1468. if (ap_interrupts_available()) {
  1469. rc = register_adapter_interrupt(&ap_airq);
  1470. ap_irq_flag = (rc == 0);
  1471. }
  1472. /* Create /sys/bus/ap. */
  1473. rc = bus_register(&ap_bus_type);
  1474. if (rc)
  1475. goto out;
  1476. for (i = 0; ap_bus_attrs[i]; i++) {
  1477. rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
  1478. if (rc)
  1479. goto out_bus;
  1480. }
  1481. /* Create /sys/devices/ap. */
  1482. ap_root_device = root_device_register("ap");
  1483. rc = PTR_ERR_OR_ZERO(ap_root_device);
  1484. if (rc)
  1485. goto out_bus;
  1486. /* Setup the AP bus rescan timer. */
  1487. timer_setup(&ap_config_timer, ap_config_timeout, 0);
  1488. /*
  1489. * Setup the high resultion poll timer.
  1490. * If we are running under z/VM adjust polling to z/VM polling rate.
  1491. */
  1492. if (MACHINE_IS_VM)
  1493. poll_timeout = 1500000;
  1494. hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  1495. ap_poll_timer.function = ap_poll_timeout;
  1496. /* Start the low priority AP bus poll thread. */
  1497. if (ap_thread_flag) {
  1498. rc = ap_poll_thread_start();
  1499. if (rc)
  1500. goto out_work;
  1501. }
  1502. queue_work(system_long_wq, &ap_scan_work);
  1503. return 0;
  1504. out_work:
  1505. hrtimer_cancel(&ap_poll_timer);
  1506. root_device_unregister(ap_root_device);
  1507. out_bus:
  1508. while (i--)
  1509. bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
  1510. bus_unregister(&ap_bus_type);
  1511. out:
  1512. if (ap_irq_flag)
  1513. unregister_adapter_interrupt(&ap_airq);
  1514. kfree(ap_qci_info);
  1515. return rc;
  1516. }
  1517. device_initcall(ap_module_init);