ap_queue.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2016, 2023
  4. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
  5. *
  6. * Adjunct processor bus, queue related code.
  7. */
  8. #define KMSG_COMPONENT "ap"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include <linux/init.h>
  11. #include <linux/slab.h>
  12. #include <asm/facility.h>
  13. #include "ap_bus.h"
  14. #include "ap_debug.h"
  15. static void __ap_flush_queue(struct ap_queue *aq);
  16. /*
  17. * some AP queue helper functions
  18. */
  19. static inline bool ap_q_supported_in_se(struct ap_queue *aq)
  20. {
  21. return aq->card->hwinfo.ep11 || aq->card->hwinfo.accel;
  22. }
  23. static inline bool ap_q_supports_bind(struct ap_queue *aq)
  24. {
  25. return aq->card->hwinfo.ep11 || aq->card->hwinfo.accel;
  26. }
  27. static inline bool ap_q_supports_assoc(struct ap_queue *aq)
  28. {
  29. return aq->card->hwinfo.ep11;
  30. }
  31. static inline bool ap_q_needs_bind(struct ap_queue *aq)
  32. {
  33. return ap_q_supports_bind(aq) && ap_sb_available();
  34. }
  35. /**
  36. * ap_queue_enable_irq(): Enable interrupt support on this AP queue.
  37. * @aq: The AP queue
  38. * @ind: the notification indicator byte
  39. *
  40. * Enables interruption on AP queue via ap_aqic(). Based on the return
  41. * value it waits a while and tests the AP queue if interrupts
  42. * have been switched on using ap_test_queue().
  43. */
  44. static int ap_queue_enable_irq(struct ap_queue *aq, void *ind)
  45. {
  46. union ap_qirq_ctrl qirqctrl = { .value = 0 };
  47. struct ap_queue_status status;
  48. qirqctrl.ir = 1;
  49. qirqctrl.isc = AP_ISC;
  50. status = ap_aqic(aq->qid, qirqctrl, virt_to_phys(ind));
  51. if (status.async)
  52. return -EPERM;
  53. switch (status.response_code) {
  54. case AP_RESPONSE_NORMAL:
  55. case AP_RESPONSE_OTHERWISE_CHANGED:
  56. return 0;
  57. case AP_RESPONSE_Q_NOT_AVAIL:
  58. case AP_RESPONSE_DECONFIGURED:
  59. case AP_RESPONSE_CHECKSTOPPED:
  60. case AP_RESPONSE_INVALID_ADDRESS:
  61. pr_err("Registering adapter interrupts for AP device %02x.%04x failed\n",
  62. AP_QID_CARD(aq->qid),
  63. AP_QID_QUEUE(aq->qid));
  64. return -EOPNOTSUPP;
  65. case AP_RESPONSE_RESET_IN_PROGRESS:
  66. case AP_RESPONSE_BUSY:
  67. default:
  68. return -EBUSY;
  69. }
  70. }
  71. /**
  72. * __ap_send(): Send message to adjunct processor queue.
  73. * @qid: The AP queue number
  74. * @psmid: The program supplied message identifier
  75. * @msg: The message text
  76. * @msglen: The message length
  77. * @special: Special Bit
  78. *
  79. * Returns AP queue status structure.
  80. * Condition code 1 on NQAP can't happen because the L bit is 1.
  81. * Condition code 2 on NQAP also means the send is incomplete,
  82. * because a segment boundary was reached. The NQAP is repeated.
  83. */
  84. static inline struct ap_queue_status
  85. __ap_send(ap_qid_t qid, unsigned long psmid, void *msg, size_t msglen,
  86. int special)
  87. {
  88. if (special)
  89. qid |= 0x400000UL;
  90. return ap_nqap(qid, psmid, msg, msglen);
  91. }
  92. /* State machine definitions and helpers */
  93. static enum ap_sm_wait ap_sm_nop(struct ap_queue *aq)
  94. {
  95. return AP_SM_WAIT_NONE;
  96. }
  97. /**
  98. * ap_sm_recv(): Receive pending reply messages from an AP queue but do
  99. * not change the state of the device.
  100. * @aq: pointer to the AP queue
  101. *
  102. * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
  103. */
  104. static struct ap_queue_status ap_sm_recv(struct ap_queue *aq)
  105. {
  106. struct ap_queue_status status;
  107. struct ap_message *ap_msg;
  108. bool found = false;
  109. size_t reslen;
  110. unsigned long resgr0 = 0;
  111. int parts = 0;
  112. /*
  113. * DQAP loop until response code and resgr0 indicate that
  114. * the msg is totally received. As we use the very same buffer
  115. * the msg is overwritten with each invocation. That's intended
  116. * and the receiver of the msg is informed with a msg rc code
  117. * of EMSGSIZE in such a case.
  118. */
  119. do {
  120. status = ap_dqap(aq->qid, &aq->reply->psmid,
  121. aq->reply->msg, aq->reply->bufsize,
  122. &aq->reply->len, &reslen, &resgr0);
  123. parts++;
  124. } while (status.response_code == 0xFF && resgr0 != 0);
  125. switch (status.response_code) {
  126. case AP_RESPONSE_NORMAL:
  127. print_hex_dump_debug("aprpl: ", DUMP_PREFIX_ADDRESS, 16, 1,
  128. aq->reply->msg, aq->reply->len, false);
  129. aq->queue_count = max_t(int, 0, aq->queue_count - 1);
  130. if (!status.queue_empty && !aq->queue_count)
  131. aq->queue_count++;
  132. if (aq->queue_count > 0)
  133. mod_timer(&aq->timeout,
  134. jiffies + aq->request_timeout);
  135. list_for_each_entry(ap_msg, &aq->pendingq, list) {
  136. if (ap_msg->psmid != aq->reply->psmid)
  137. continue;
  138. list_del_init(&ap_msg->list);
  139. aq->pendingq_count--;
  140. if (parts > 1) {
  141. ap_msg->rc = -EMSGSIZE;
  142. ap_msg->receive(aq, ap_msg, NULL);
  143. } else {
  144. ap_msg->receive(aq, ap_msg, aq->reply);
  145. }
  146. found = true;
  147. break;
  148. }
  149. if (!found) {
  150. AP_DBF_WARN("%s unassociated reply psmid=0x%016lx on 0x%02x.%04x\n",
  151. __func__, aq->reply->psmid,
  152. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  153. }
  154. fallthrough;
  155. case AP_RESPONSE_NO_PENDING_REPLY:
  156. if (!status.queue_empty || aq->queue_count <= 0)
  157. break;
  158. /* The card shouldn't forget requests but who knows. */
  159. aq->queue_count = 0;
  160. list_splice_init(&aq->pendingq, &aq->requestq);
  161. aq->requestq_count += aq->pendingq_count;
  162. pr_debug("queue 0x%02x.%04x rescheduled %d reqs (new req %d)\n",
  163. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid),
  164. aq->pendingq_count, aq->requestq_count);
  165. aq->pendingq_count = 0;
  166. break;
  167. default:
  168. break;
  169. }
  170. return status;
  171. }
  172. /**
  173. * ap_sm_read(): Receive pending reply messages from an AP queue.
  174. * @aq: pointer to the AP queue
  175. *
  176. * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
  177. */
  178. static enum ap_sm_wait ap_sm_read(struct ap_queue *aq)
  179. {
  180. struct ap_queue_status status;
  181. if (!aq->reply)
  182. return AP_SM_WAIT_NONE;
  183. status = ap_sm_recv(aq);
  184. if (status.async)
  185. return AP_SM_WAIT_NONE;
  186. switch (status.response_code) {
  187. case AP_RESPONSE_NORMAL:
  188. if (aq->queue_count > 0) {
  189. aq->sm_state = AP_SM_STATE_WORKING;
  190. return AP_SM_WAIT_AGAIN;
  191. }
  192. aq->sm_state = AP_SM_STATE_IDLE;
  193. break;
  194. case AP_RESPONSE_NO_PENDING_REPLY:
  195. if (aq->queue_count > 0)
  196. return status.irq_enabled ?
  197. AP_SM_WAIT_INTERRUPT : AP_SM_WAIT_HIGH_TIMEOUT;
  198. aq->sm_state = AP_SM_STATE_IDLE;
  199. break;
  200. default:
  201. aq->dev_state = AP_DEV_STATE_ERROR;
  202. aq->last_err_rc = status.response_code;
  203. AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
  204. __func__, status.response_code,
  205. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  206. return AP_SM_WAIT_NONE;
  207. }
  208. /* Check and maybe enable irq support (again) on this queue */
  209. if (!status.irq_enabled && status.queue_empty) {
  210. void *lsi_ptr = ap_airq_ptr();
  211. if (lsi_ptr && ap_queue_enable_irq(aq, lsi_ptr) == 0) {
  212. aq->sm_state = AP_SM_STATE_SETIRQ_WAIT;
  213. return AP_SM_WAIT_AGAIN;
  214. }
  215. }
  216. return AP_SM_WAIT_NONE;
  217. }
  218. /**
  219. * ap_sm_write(): Send messages from the request queue to an AP queue.
  220. * @aq: pointer to the AP queue
  221. *
  222. * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
  223. */
  224. static enum ap_sm_wait ap_sm_write(struct ap_queue *aq)
  225. {
  226. struct ap_queue_status status;
  227. struct ap_message *ap_msg;
  228. ap_qid_t qid = aq->qid;
  229. if (aq->requestq_count <= 0)
  230. return AP_SM_WAIT_NONE;
  231. /* Start the next request on the queue. */
  232. ap_msg = list_entry(aq->requestq.next, struct ap_message, list);
  233. print_hex_dump_debug("apreq: ", DUMP_PREFIX_ADDRESS, 16, 1,
  234. ap_msg->msg, ap_msg->len, false);
  235. status = __ap_send(qid, ap_msg->psmid,
  236. ap_msg->msg, ap_msg->len,
  237. ap_msg->flags & AP_MSG_FLAG_SPECIAL);
  238. if (status.async)
  239. return AP_SM_WAIT_NONE;
  240. switch (status.response_code) {
  241. case AP_RESPONSE_NORMAL:
  242. aq->queue_count = max_t(int, 1, aq->queue_count + 1);
  243. if (aq->queue_count == 1)
  244. mod_timer(&aq->timeout, jiffies + aq->request_timeout);
  245. list_move_tail(&ap_msg->list, &aq->pendingq);
  246. aq->requestq_count--;
  247. aq->pendingq_count++;
  248. if (aq->queue_count < aq->card->hwinfo.qd) {
  249. aq->sm_state = AP_SM_STATE_WORKING;
  250. return AP_SM_WAIT_AGAIN;
  251. }
  252. fallthrough;
  253. case AP_RESPONSE_Q_FULL:
  254. aq->sm_state = AP_SM_STATE_QUEUE_FULL;
  255. return status.irq_enabled ?
  256. AP_SM_WAIT_INTERRUPT : AP_SM_WAIT_HIGH_TIMEOUT;
  257. case AP_RESPONSE_RESET_IN_PROGRESS:
  258. aq->sm_state = AP_SM_STATE_RESET_WAIT;
  259. return AP_SM_WAIT_LOW_TIMEOUT;
  260. case AP_RESPONSE_INVALID_DOMAIN:
  261. AP_DBF_WARN("%s RESPONSE_INVALID_DOMAIN on NQAP\n", __func__);
  262. fallthrough;
  263. case AP_RESPONSE_MESSAGE_TOO_BIG:
  264. case AP_RESPONSE_REQ_FAC_NOT_INST:
  265. list_del_init(&ap_msg->list);
  266. aq->requestq_count--;
  267. ap_msg->rc = -EINVAL;
  268. ap_msg->receive(aq, ap_msg, NULL);
  269. return AP_SM_WAIT_AGAIN;
  270. default:
  271. aq->dev_state = AP_DEV_STATE_ERROR;
  272. aq->last_err_rc = status.response_code;
  273. AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
  274. __func__, status.response_code,
  275. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  276. return AP_SM_WAIT_NONE;
  277. }
  278. }
  279. /**
  280. * ap_sm_read_write(): Send and receive messages to/from an AP queue.
  281. * @aq: pointer to the AP queue
  282. *
  283. * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
  284. */
  285. static enum ap_sm_wait ap_sm_read_write(struct ap_queue *aq)
  286. {
  287. return min(ap_sm_read(aq), ap_sm_write(aq));
  288. }
  289. /**
  290. * ap_sm_reset(): Reset an AP queue.
  291. * @aq: The AP queue
  292. *
  293. * Submit the Reset command to an AP queue.
  294. */
  295. static enum ap_sm_wait ap_sm_reset(struct ap_queue *aq)
  296. {
  297. struct ap_queue_status status;
  298. status = ap_rapq(aq->qid, aq->rapq_fbit);
  299. if (status.async)
  300. return AP_SM_WAIT_NONE;
  301. switch (status.response_code) {
  302. case AP_RESPONSE_NORMAL:
  303. case AP_RESPONSE_RESET_IN_PROGRESS:
  304. aq->sm_state = AP_SM_STATE_RESET_WAIT;
  305. aq->rapq_fbit = 0;
  306. return AP_SM_WAIT_LOW_TIMEOUT;
  307. default:
  308. aq->dev_state = AP_DEV_STATE_ERROR;
  309. aq->last_err_rc = status.response_code;
  310. AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
  311. __func__, status.response_code,
  312. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  313. return AP_SM_WAIT_NONE;
  314. }
  315. }
  316. /**
  317. * ap_sm_reset_wait(): Test queue for completion of the reset operation
  318. * @aq: pointer to the AP queue
  319. *
  320. * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
  321. */
  322. static enum ap_sm_wait ap_sm_reset_wait(struct ap_queue *aq)
  323. {
  324. struct ap_queue_status status;
  325. struct ap_tapq_hwinfo hwinfo;
  326. void *lsi_ptr;
  327. /* Get the status with TAPQ */
  328. status = ap_test_queue(aq->qid, 1, &hwinfo);
  329. switch (status.response_code) {
  330. case AP_RESPONSE_NORMAL:
  331. aq->se_bstate = hwinfo.bs;
  332. lsi_ptr = ap_airq_ptr();
  333. if (lsi_ptr && ap_queue_enable_irq(aq, lsi_ptr) == 0)
  334. aq->sm_state = AP_SM_STATE_SETIRQ_WAIT;
  335. else
  336. aq->sm_state = (aq->queue_count > 0) ?
  337. AP_SM_STATE_WORKING : AP_SM_STATE_IDLE;
  338. return AP_SM_WAIT_AGAIN;
  339. case AP_RESPONSE_BUSY:
  340. case AP_RESPONSE_RESET_IN_PROGRESS:
  341. return AP_SM_WAIT_LOW_TIMEOUT;
  342. case AP_RESPONSE_Q_NOT_AVAIL:
  343. case AP_RESPONSE_DECONFIGURED:
  344. case AP_RESPONSE_CHECKSTOPPED:
  345. default:
  346. aq->dev_state = AP_DEV_STATE_ERROR;
  347. aq->last_err_rc = status.response_code;
  348. AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
  349. __func__, status.response_code,
  350. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  351. return AP_SM_WAIT_NONE;
  352. }
  353. }
  354. /**
  355. * ap_sm_setirq_wait(): Test queue for completion of the irq enablement
  356. * @aq: pointer to the AP queue
  357. *
  358. * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
  359. */
  360. static enum ap_sm_wait ap_sm_setirq_wait(struct ap_queue *aq)
  361. {
  362. struct ap_queue_status status;
  363. if (aq->queue_count > 0 && aq->reply)
  364. /* Try to read a completed message and get the status */
  365. status = ap_sm_recv(aq);
  366. else
  367. /* Get the status with TAPQ */
  368. status = ap_tapq(aq->qid, NULL);
  369. if (status.irq_enabled == 1) {
  370. /* Irqs are now enabled */
  371. aq->sm_state = (aq->queue_count > 0) ?
  372. AP_SM_STATE_WORKING : AP_SM_STATE_IDLE;
  373. }
  374. switch (status.response_code) {
  375. case AP_RESPONSE_NORMAL:
  376. if (aq->queue_count > 0)
  377. return AP_SM_WAIT_AGAIN;
  378. fallthrough;
  379. case AP_RESPONSE_NO_PENDING_REPLY:
  380. return AP_SM_WAIT_LOW_TIMEOUT;
  381. default:
  382. aq->dev_state = AP_DEV_STATE_ERROR;
  383. aq->last_err_rc = status.response_code;
  384. AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
  385. __func__, status.response_code,
  386. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  387. return AP_SM_WAIT_NONE;
  388. }
  389. }
  390. /**
  391. * ap_sm_assoc_wait(): Test queue for completion of a pending
  392. * association request.
  393. * @aq: pointer to the AP queue
  394. */
  395. static enum ap_sm_wait ap_sm_assoc_wait(struct ap_queue *aq)
  396. {
  397. struct ap_queue_status status;
  398. struct ap_tapq_hwinfo hwinfo;
  399. status = ap_test_queue(aq->qid, 1, &hwinfo);
  400. /* handle asynchronous error on this queue */
  401. if (status.async && status.response_code) {
  402. aq->dev_state = AP_DEV_STATE_ERROR;
  403. aq->last_err_rc = status.response_code;
  404. AP_DBF_WARN("%s asynch RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
  405. __func__, status.response_code,
  406. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  407. return AP_SM_WAIT_NONE;
  408. }
  409. if (status.response_code > AP_RESPONSE_BUSY) {
  410. aq->dev_state = AP_DEV_STATE_ERROR;
  411. aq->last_err_rc = status.response_code;
  412. AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
  413. __func__, status.response_code,
  414. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  415. return AP_SM_WAIT_NONE;
  416. }
  417. /* update queue's SE bind state */
  418. aq->se_bstate = hwinfo.bs;
  419. /* check bs bits */
  420. switch (hwinfo.bs) {
  421. case AP_BS_Q_USABLE:
  422. /* association is through */
  423. aq->sm_state = AP_SM_STATE_IDLE;
  424. pr_debug("queue 0x%02x.%04x associated with %u\n",
  425. AP_QID_CARD(aq->qid),
  426. AP_QID_QUEUE(aq->qid), aq->assoc_idx);
  427. return AP_SM_WAIT_NONE;
  428. case AP_BS_Q_USABLE_NO_SECURE_KEY:
  429. /* association still pending */
  430. return AP_SM_WAIT_LOW_TIMEOUT;
  431. default:
  432. /* reset from 'outside' happened or no idea at all */
  433. aq->assoc_idx = ASSOC_IDX_INVALID;
  434. aq->dev_state = AP_DEV_STATE_ERROR;
  435. aq->last_err_rc = status.response_code;
  436. AP_DBF_WARN("%s bs 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
  437. __func__, hwinfo.bs,
  438. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  439. return AP_SM_WAIT_NONE;
  440. }
  441. }
  442. /*
  443. * AP state machine jump table
  444. */
  445. static ap_func_t *ap_jumptable[NR_AP_SM_STATES][NR_AP_SM_EVENTS] = {
  446. [AP_SM_STATE_RESET_START] = {
  447. [AP_SM_EVENT_POLL] = ap_sm_reset,
  448. [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
  449. },
  450. [AP_SM_STATE_RESET_WAIT] = {
  451. [AP_SM_EVENT_POLL] = ap_sm_reset_wait,
  452. [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
  453. },
  454. [AP_SM_STATE_SETIRQ_WAIT] = {
  455. [AP_SM_EVENT_POLL] = ap_sm_setirq_wait,
  456. [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
  457. },
  458. [AP_SM_STATE_IDLE] = {
  459. [AP_SM_EVENT_POLL] = ap_sm_write,
  460. [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
  461. },
  462. [AP_SM_STATE_WORKING] = {
  463. [AP_SM_EVENT_POLL] = ap_sm_read_write,
  464. [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
  465. },
  466. [AP_SM_STATE_QUEUE_FULL] = {
  467. [AP_SM_EVENT_POLL] = ap_sm_read,
  468. [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
  469. },
  470. [AP_SM_STATE_ASSOC_WAIT] = {
  471. [AP_SM_EVENT_POLL] = ap_sm_assoc_wait,
  472. [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
  473. },
  474. };
  475. enum ap_sm_wait ap_sm_event(struct ap_queue *aq, enum ap_sm_event event)
  476. {
  477. if (aq->config && !aq->chkstop &&
  478. aq->dev_state > AP_DEV_STATE_UNINITIATED)
  479. return ap_jumptable[aq->sm_state][event](aq);
  480. else
  481. return AP_SM_WAIT_NONE;
  482. }
  483. enum ap_sm_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_sm_event event)
  484. {
  485. enum ap_sm_wait wait;
  486. while ((wait = ap_sm_event(aq, event)) == AP_SM_WAIT_AGAIN)
  487. ;
  488. return wait;
  489. }
  490. /*
  491. * AP queue related attributes.
  492. */
  493. static ssize_t request_count_show(struct device *dev,
  494. struct device_attribute *attr,
  495. char *buf)
  496. {
  497. struct ap_queue *aq = to_ap_queue(dev);
  498. bool valid = false;
  499. u64 req_cnt;
  500. spin_lock_bh(&aq->lock);
  501. if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
  502. req_cnt = aq->total_request_count;
  503. valid = true;
  504. }
  505. spin_unlock_bh(&aq->lock);
  506. if (valid)
  507. return sysfs_emit(buf, "%llu\n", req_cnt);
  508. else
  509. return sysfs_emit(buf, "-\n");
  510. }
  511. static ssize_t request_count_store(struct device *dev,
  512. struct device_attribute *attr,
  513. const char *buf, size_t count)
  514. {
  515. struct ap_queue *aq = to_ap_queue(dev);
  516. spin_lock_bh(&aq->lock);
  517. aq->total_request_count = 0;
  518. spin_unlock_bh(&aq->lock);
  519. return count;
  520. }
  521. static DEVICE_ATTR_RW(request_count);
  522. static ssize_t requestq_count_show(struct device *dev,
  523. struct device_attribute *attr, char *buf)
  524. {
  525. struct ap_queue *aq = to_ap_queue(dev);
  526. unsigned int reqq_cnt = 0;
  527. spin_lock_bh(&aq->lock);
  528. if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
  529. reqq_cnt = aq->requestq_count;
  530. spin_unlock_bh(&aq->lock);
  531. return sysfs_emit(buf, "%d\n", reqq_cnt);
  532. }
  533. static DEVICE_ATTR_RO(requestq_count);
  534. static ssize_t pendingq_count_show(struct device *dev,
  535. struct device_attribute *attr, char *buf)
  536. {
  537. struct ap_queue *aq = to_ap_queue(dev);
  538. unsigned int penq_cnt = 0;
  539. spin_lock_bh(&aq->lock);
  540. if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
  541. penq_cnt = aq->pendingq_count;
  542. spin_unlock_bh(&aq->lock);
  543. return sysfs_emit(buf, "%d\n", penq_cnt);
  544. }
  545. static DEVICE_ATTR_RO(pendingq_count);
  546. static ssize_t reset_show(struct device *dev,
  547. struct device_attribute *attr, char *buf)
  548. {
  549. struct ap_queue *aq = to_ap_queue(dev);
  550. int rc = 0;
  551. spin_lock_bh(&aq->lock);
  552. switch (aq->sm_state) {
  553. case AP_SM_STATE_RESET_START:
  554. case AP_SM_STATE_RESET_WAIT:
  555. rc = sysfs_emit(buf, "Reset in progress.\n");
  556. break;
  557. case AP_SM_STATE_WORKING:
  558. case AP_SM_STATE_QUEUE_FULL:
  559. rc = sysfs_emit(buf, "Reset Timer armed.\n");
  560. break;
  561. default:
  562. rc = sysfs_emit(buf, "No Reset Timer set.\n");
  563. }
  564. spin_unlock_bh(&aq->lock);
  565. return rc;
  566. }
  567. static ssize_t reset_store(struct device *dev,
  568. struct device_attribute *attr,
  569. const char *buf, size_t count)
  570. {
  571. struct ap_queue *aq = to_ap_queue(dev);
  572. spin_lock_bh(&aq->lock);
  573. __ap_flush_queue(aq);
  574. aq->sm_state = AP_SM_STATE_RESET_START;
  575. ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
  576. spin_unlock_bh(&aq->lock);
  577. AP_DBF_INFO("%s reset queue=%02x.%04x triggered by user\n",
  578. __func__, AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  579. return count;
  580. }
  581. static DEVICE_ATTR_RW(reset);
  582. static ssize_t interrupt_show(struct device *dev,
  583. struct device_attribute *attr, char *buf)
  584. {
  585. struct ap_queue *aq = to_ap_queue(dev);
  586. struct ap_queue_status status;
  587. int rc = 0;
  588. spin_lock_bh(&aq->lock);
  589. if (aq->sm_state == AP_SM_STATE_SETIRQ_WAIT) {
  590. rc = sysfs_emit(buf, "Enable Interrupt pending.\n");
  591. } else {
  592. status = ap_tapq(aq->qid, NULL);
  593. if (status.irq_enabled)
  594. rc = sysfs_emit(buf, "Interrupts enabled.\n");
  595. else
  596. rc = sysfs_emit(buf, "Interrupts disabled.\n");
  597. }
  598. spin_unlock_bh(&aq->lock);
  599. return rc;
  600. }
  601. static DEVICE_ATTR_RO(interrupt);
  602. static ssize_t config_show(struct device *dev,
  603. struct device_attribute *attr, char *buf)
  604. {
  605. struct ap_queue *aq = to_ap_queue(dev);
  606. int rc;
  607. spin_lock_bh(&aq->lock);
  608. rc = sysfs_emit(buf, "%d\n", aq->config ? 1 : 0);
  609. spin_unlock_bh(&aq->lock);
  610. return rc;
  611. }
  612. static DEVICE_ATTR_RO(config);
  613. static ssize_t chkstop_show(struct device *dev,
  614. struct device_attribute *attr, char *buf)
  615. {
  616. struct ap_queue *aq = to_ap_queue(dev);
  617. int rc;
  618. spin_lock_bh(&aq->lock);
  619. rc = sysfs_emit(buf, "%d\n", aq->chkstop ? 1 : 0);
  620. spin_unlock_bh(&aq->lock);
  621. return rc;
  622. }
  623. static DEVICE_ATTR_RO(chkstop);
  624. static ssize_t ap_functions_show(struct device *dev,
  625. struct device_attribute *attr, char *buf)
  626. {
  627. struct ap_queue *aq = to_ap_queue(dev);
  628. struct ap_queue_status status;
  629. struct ap_tapq_hwinfo hwinfo;
  630. status = ap_test_queue(aq->qid, 1, &hwinfo);
  631. if (status.response_code > AP_RESPONSE_BUSY) {
  632. pr_debug("RC 0x%02x on tapq(0x%02x.%04x)\n",
  633. status.response_code,
  634. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  635. return -EIO;
  636. }
  637. return sysfs_emit(buf, "0x%08X\n", hwinfo.fac);
  638. }
  639. static DEVICE_ATTR_RO(ap_functions);
  640. #ifdef CONFIG_AP_DEBUG
  641. static ssize_t states_show(struct device *dev,
  642. struct device_attribute *attr, char *buf)
  643. {
  644. struct ap_queue *aq = to_ap_queue(dev);
  645. int rc = 0;
  646. spin_lock_bh(&aq->lock);
  647. /* queue device state */
  648. switch (aq->dev_state) {
  649. case AP_DEV_STATE_UNINITIATED:
  650. rc = sysfs_emit(buf, "UNINITIATED\n");
  651. break;
  652. case AP_DEV_STATE_OPERATING:
  653. rc = sysfs_emit(buf, "OPERATING");
  654. break;
  655. case AP_DEV_STATE_SHUTDOWN:
  656. rc = sysfs_emit(buf, "SHUTDOWN");
  657. break;
  658. case AP_DEV_STATE_ERROR:
  659. rc = sysfs_emit(buf, "ERROR");
  660. break;
  661. default:
  662. rc = sysfs_emit(buf, "UNKNOWN");
  663. }
  664. /* state machine state */
  665. if (aq->dev_state) {
  666. switch (aq->sm_state) {
  667. case AP_SM_STATE_RESET_START:
  668. rc += sysfs_emit_at(buf, rc, " [RESET_START]\n");
  669. break;
  670. case AP_SM_STATE_RESET_WAIT:
  671. rc += sysfs_emit_at(buf, rc, " [RESET_WAIT]\n");
  672. break;
  673. case AP_SM_STATE_SETIRQ_WAIT:
  674. rc += sysfs_emit_at(buf, rc, " [SETIRQ_WAIT]\n");
  675. break;
  676. case AP_SM_STATE_IDLE:
  677. rc += sysfs_emit_at(buf, rc, " [IDLE]\n");
  678. break;
  679. case AP_SM_STATE_WORKING:
  680. rc += sysfs_emit_at(buf, rc, " [WORKING]\n");
  681. break;
  682. case AP_SM_STATE_QUEUE_FULL:
  683. rc += sysfs_emit_at(buf, rc, " [FULL]\n");
  684. break;
  685. case AP_SM_STATE_ASSOC_WAIT:
  686. rc += sysfs_emit_at(buf, rc, " [ASSOC_WAIT]\n");
  687. break;
  688. default:
  689. rc += sysfs_emit_at(buf, rc, " [UNKNOWN]\n");
  690. }
  691. }
  692. spin_unlock_bh(&aq->lock);
  693. return rc;
  694. }
  695. static DEVICE_ATTR_RO(states);
  696. static ssize_t last_err_rc_show(struct device *dev,
  697. struct device_attribute *attr, char *buf)
  698. {
  699. struct ap_queue *aq = to_ap_queue(dev);
  700. int rc;
  701. spin_lock_bh(&aq->lock);
  702. rc = aq->last_err_rc;
  703. spin_unlock_bh(&aq->lock);
  704. switch (rc) {
  705. case AP_RESPONSE_NORMAL:
  706. return sysfs_emit(buf, "NORMAL\n");
  707. case AP_RESPONSE_Q_NOT_AVAIL:
  708. return sysfs_emit(buf, "Q_NOT_AVAIL\n");
  709. case AP_RESPONSE_RESET_IN_PROGRESS:
  710. return sysfs_emit(buf, "RESET_IN_PROGRESS\n");
  711. case AP_RESPONSE_DECONFIGURED:
  712. return sysfs_emit(buf, "DECONFIGURED\n");
  713. case AP_RESPONSE_CHECKSTOPPED:
  714. return sysfs_emit(buf, "CHECKSTOPPED\n");
  715. case AP_RESPONSE_BUSY:
  716. return sysfs_emit(buf, "BUSY\n");
  717. case AP_RESPONSE_INVALID_ADDRESS:
  718. return sysfs_emit(buf, "INVALID_ADDRESS\n");
  719. case AP_RESPONSE_OTHERWISE_CHANGED:
  720. return sysfs_emit(buf, "OTHERWISE_CHANGED\n");
  721. case AP_RESPONSE_Q_FULL:
  722. return sysfs_emit(buf, "Q_FULL/NO_PENDING_REPLY\n");
  723. case AP_RESPONSE_INDEX_TOO_BIG:
  724. return sysfs_emit(buf, "INDEX_TOO_BIG\n");
  725. case AP_RESPONSE_NO_FIRST_PART:
  726. return sysfs_emit(buf, "NO_FIRST_PART\n");
  727. case AP_RESPONSE_MESSAGE_TOO_BIG:
  728. return sysfs_emit(buf, "MESSAGE_TOO_BIG\n");
  729. case AP_RESPONSE_REQ_FAC_NOT_INST:
  730. return sysfs_emit(buf, "REQ_FAC_NOT_INST\n");
  731. default:
  732. return sysfs_emit(buf, "response code %d\n", rc);
  733. }
  734. }
  735. static DEVICE_ATTR_RO(last_err_rc);
  736. #endif
  737. static struct attribute *ap_queue_dev_attrs[] = {
  738. &dev_attr_request_count.attr,
  739. &dev_attr_requestq_count.attr,
  740. &dev_attr_pendingq_count.attr,
  741. &dev_attr_reset.attr,
  742. &dev_attr_interrupt.attr,
  743. &dev_attr_config.attr,
  744. &dev_attr_chkstop.attr,
  745. &dev_attr_ap_functions.attr,
  746. #ifdef CONFIG_AP_DEBUG
  747. &dev_attr_states.attr,
  748. &dev_attr_last_err_rc.attr,
  749. #endif
  750. NULL
  751. };
  752. static struct attribute_group ap_queue_dev_attr_group = {
  753. .attrs = ap_queue_dev_attrs
  754. };
  755. static const struct attribute_group *ap_queue_dev_attr_groups[] = {
  756. &ap_queue_dev_attr_group,
  757. NULL
  758. };
  759. static struct device_type ap_queue_type = {
  760. .name = "ap_queue",
  761. .groups = ap_queue_dev_attr_groups,
  762. };
  763. static ssize_t se_bind_show(struct device *dev,
  764. struct device_attribute *attr, char *buf)
  765. {
  766. struct ap_queue *aq = to_ap_queue(dev);
  767. struct ap_queue_status status;
  768. struct ap_tapq_hwinfo hwinfo;
  769. if (!ap_q_supports_bind(aq))
  770. return sysfs_emit(buf, "-\n");
  771. status = ap_test_queue(aq->qid, 1, &hwinfo);
  772. if (status.response_code > AP_RESPONSE_BUSY) {
  773. pr_debug("RC 0x%02x on tapq(0x%02x.%04x)\n",
  774. status.response_code,
  775. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  776. return -EIO;
  777. }
  778. /* update queue's SE bind state */
  779. spin_lock_bh(&aq->lock);
  780. aq->se_bstate = hwinfo.bs;
  781. spin_unlock_bh(&aq->lock);
  782. switch (hwinfo.bs) {
  783. case AP_BS_Q_USABLE:
  784. case AP_BS_Q_USABLE_NO_SECURE_KEY:
  785. return sysfs_emit(buf, "bound\n");
  786. default:
  787. return sysfs_emit(buf, "unbound\n");
  788. }
  789. }
  790. static ssize_t se_bind_store(struct device *dev,
  791. struct device_attribute *attr,
  792. const char *buf, size_t count)
  793. {
  794. struct ap_queue *aq = to_ap_queue(dev);
  795. struct ap_queue_status status;
  796. struct ap_tapq_hwinfo hwinfo;
  797. bool value;
  798. int rc;
  799. if (!ap_q_supports_bind(aq))
  800. return -EINVAL;
  801. /* only 0 (unbind) and 1 (bind) allowed */
  802. rc = kstrtobool(buf, &value);
  803. if (rc)
  804. return rc;
  805. if (!value) {
  806. /* Unbind. Set F bit arg and trigger RAPQ */
  807. spin_lock_bh(&aq->lock);
  808. __ap_flush_queue(aq);
  809. aq->rapq_fbit = 1;
  810. _ap_queue_init_state(aq);
  811. rc = count;
  812. goto out;
  813. }
  814. /* Bind. Check current SE bind state */
  815. status = ap_test_queue(aq->qid, 1, &hwinfo);
  816. if (status.response_code) {
  817. AP_DBF_WARN("%s RC 0x%02x on tapq(0x%02x.%04x)\n",
  818. __func__, status.response_code,
  819. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  820. return -EIO;
  821. }
  822. /* Update BS state */
  823. spin_lock_bh(&aq->lock);
  824. aq->se_bstate = hwinfo.bs;
  825. if (hwinfo.bs != AP_BS_Q_AVAIL_FOR_BINDING) {
  826. AP_DBF_WARN("%s bind attempt with bs %d on queue 0x%02x.%04x\n",
  827. __func__, hwinfo.bs,
  828. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  829. rc = -EINVAL;
  830. goto out;
  831. }
  832. /* Check SM state */
  833. if (aq->sm_state < AP_SM_STATE_IDLE) {
  834. rc = -EBUSY;
  835. goto out;
  836. }
  837. /* invoke BAPQ */
  838. status = ap_bapq(aq->qid);
  839. if (status.response_code) {
  840. AP_DBF_WARN("%s RC 0x%02x on bapq(0x%02x.%04x)\n",
  841. __func__, status.response_code,
  842. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  843. rc = -EIO;
  844. goto out;
  845. }
  846. aq->assoc_idx = ASSOC_IDX_INVALID;
  847. /* verify SE bind state */
  848. status = ap_test_queue(aq->qid, 1, &hwinfo);
  849. if (status.response_code) {
  850. AP_DBF_WARN("%s RC 0x%02x on tapq(0x%02x.%04x)\n",
  851. __func__, status.response_code,
  852. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  853. rc = -EIO;
  854. goto out;
  855. }
  856. aq->se_bstate = hwinfo.bs;
  857. if (!(hwinfo.bs == AP_BS_Q_USABLE ||
  858. hwinfo.bs == AP_BS_Q_USABLE_NO_SECURE_KEY)) {
  859. AP_DBF_WARN("%s BAPQ success, but bs shows %d on queue 0x%02x.%04x\n",
  860. __func__, hwinfo.bs,
  861. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  862. rc = -EIO;
  863. goto out;
  864. }
  865. /* SE bind was successful */
  866. AP_DBF_INFO("%s bapq(0x%02x.%04x) success\n", __func__,
  867. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  868. rc = count;
  869. out:
  870. spin_unlock_bh(&aq->lock);
  871. return rc;
  872. }
  873. static DEVICE_ATTR_RW(se_bind);
  874. static ssize_t se_associate_show(struct device *dev,
  875. struct device_attribute *attr, char *buf)
  876. {
  877. struct ap_queue *aq = to_ap_queue(dev);
  878. struct ap_queue_status status;
  879. struct ap_tapq_hwinfo hwinfo;
  880. if (!ap_q_supports_assoc(aq))
  881. return sysfs_emit(buf, "-\n");
  882. status = ap_test_queue(aq->qid, 1, &hwinfo);
  883. if (status.response_code > AP_RESPONSE_BUSY) {
  884. pr_debug("RC 0x%02x on tapq(0x%02x.%04x)\n",
  885. status.response_code,
  886. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  887. return -EIO;
  888. }
  889. /* update queue's SE bind state */
  890. spin_lock_bh(&aq->lock);
  891. aq->se_bstate = hwinfo.bs;
  892. spin_unlock_bh(&aq->lock);
  893. switch (hwinfo.bs) {
  894. case AP_BS_Q_USABLE:
  895. if (aq->assoc_idx == ASSOC_IDX_INVALID) {
  896. AP_DBF_WARN("%s AP_BS_Q_USABLE but invalid assoc_idx\n", __func__);
  897. return -EIO;
  898. }
  899. return sysfs_emit(buf, "associated %u\n", aq->assoc_idx);
  900. case AP_BS_Q_USABLE_NO_SECURE_KEY:
  901. if (aq->assoc_idx != ASSOC_IDX_INVALID)
  902. return sysfs_emit(buf, "association pending\n");
  903. fallthrough;
  904. default:
  905. return sysfs_emit(buf, "unassociated\n");
  906. }
  907. }
  908. static ssize_t se_associate_store(struct device *dev,
  909. struct device_attribute *attr,
  910. const char *buf, size_t count)
  911. {
  912. struct ap_queue *aq = to_ap_queue(dev);
  913. struct ap_queue_status status;
  914. struct ap_tapq_hwinfo hwinfo;
  915. unsigned int value;
  916. int rc;
  917. if (!ap_q_supports_assoc(aq))
  918. return -EINVAL;
  919. /* association index needs to be >= 0 */
  920. rc = kstrtouint(buf, 0, &value);
  921. if (rc)
  922. return rc;
  923. if (value >= ASSOC_IDX_INVALID)
  924. return -EINVAL;
  925. /* check current SE bind state */
  926. status = ap_test_queue(aq->qid, 1, &hwinfo);
  927. if (status.response_code) {
  928. AP_DBF_WARN("%s RC 0x%02x on tapq(0x%02x.%04x)\n",
  929. __func__, status.response_code,
  930. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  931. return -EIO;
  932. }
  933. spin_lock_bh(&aq->lock);
  934. aq->se_bstate = hwinfo.bs;
  935. if (hwinfo.bs != AP_BS_Q_USABLE_NO_SECURE_KEY) {
  936. AP_DBF_WARN("%s association attempt with bs %d on queue 0x%02x.%04x\n",
  937. __func__, hwinfo.bs,
  938. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  939. rc = -EINVAL;
  940. goto out;
  941. }
  942. /* check SM state */
  943. if (aq->sm_state != AP_SM_STATE_IDLE) {
  944. rc = -EBUSY;
  945. goto out;
  946. }
  947. /* trigger the asynchronous association request */
  948. status = ap_aapq(aq->qid, value);
  949. switch (status.response_code) {
  950. case AP_RESPONSE_NORMAL:
  951. case AP_RESPONSE_STATE_CHANGE_IN_PROGRESS:
  952. aq->sm_state = AP_SM_STATE_ASSOC_WAIT;
  953. aq->assoc_idx = value;
  954. ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
  955. break;
  956. default:
  957. AP_DBF_WARN("%s RC 0x%02x on aapq(0x%02x.%04x)\n",
  958. __func__, status.response_code,
  959. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  960. rc = -EIO;
  961. goto out;
  962. }
  963. rc = count;
  964. out:
  965. spin_unlock_bh(&aq->lock);
  966. return rc;
  967. }
  968. static DEVICE_ATTR_RW(se_associate);
  969. static struct attribute *ap_queue_dev_sb_attrs[] = {
  970. &dev_attr_se_bind.attr,
  971. &dev_attr_se_associate.attr,
  972. NULL
  973. };
  974. static struct attribute_group ap_queue_dev_sb_attr_group = {
  975. .attrs = ap_queue_dev_sb_attrs
  976. };
  977. static const struct attribute_group *ap_queue_dev_sb_attr_groups[] = {
  978. &ap_queue_dev_sb_attr_group,
  979. NULL
  980. };
  981. static void ap_queue_device_release(struct device *dev)
  982. {
  983. struct ap_queue *aq = to_ap_queue(dev);
  984. spin_lock_bh(&ap_queues_lock);
  985. hash_del(&aq->hnode);
  986. spin_unlock_bh(&ap_queues_lock);
  987. kfree(aq);
  988. }
  989. struct ap_queue *ap_queue_create(ap_qid_t qid, struct ap_card *ac)
  990. {
  991. struct ap_queue *aq;
  992. aq = kzalloc(sizeof(*aq), GFP_KERNEL);
  993. if (!aq)
  994. return NULL;
  995. aq->card = ac;
  996. aq->ap_dev.device.release = ap_queue_device_release;
  997. aq->ap_dev.device.type = &ap_queue_type;
  998. aq->ap_dev.device_type = ac->ap_dev.device_type;
  999. /* in SE environment add bind/associate attributes group */
  1000. if (ap_is_se_guest() && ap_q_supported_in_se(aq))
  1001. aq->ap_dev.device.groups = ap_queue_dev_sb_attr_groups;
  1002. aq->qid = qid;
  1003. spin_lock_init(&aq->lock);
  1004. INIT_LIST_HEAD(&aq->pendingq);
  1005. INIT_LIST_HEAD(&aq->requestq);
  1006. timer_setup(&aq->timeout, ap_request_timeout, 0);
  1007. return aq;
  1008. }
  1009. void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *reply)
  1010. {
  1011. aq->reply = reply;
  1012. spin_lock_bh(&aq->lock);
  1013. ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
  1014. spin_unlock_bh(&aq->lock);
  1015. }
  1016. EXPORT_SYMBOL(ap_queue_init_reply);
  1017. /**
  1018. * ap_queue_message(): Queue a request to an AP device.
  1019. * @aq: The AP device to queue the message to
  1020. * @ap_msg: The message that is to be added
  1021. */
  1022. int ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg)
  1023. {
  1024. int rc = 0;
  1025. /* msg needs to have a valid receive-callback */
  1026. BUG_ON(!ap_msg->receive);
  1027. spin_lock_bh(&aq->lock);
  1028. /* only allow to queue new messages if device state is ok */
  1029. if (aq->dev_state == AP_DEV_STATE_OPERATING) {
  1030. list_add_tail(&ap_msg->list, &aq->requestq);
  1031. aq->requestq_count++;
  1032. aq->total_request_count++;
  1033. atomic64_inc(&aq->card->total_request_count);
  1034. } else {
  1035. rc = -ENODEV;
  1036. }
  1037. /* Send/receive as many request from the queue as possible. */
  1038. ap_wait(ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
  1039. spin_unlock_bh(&aq->lock);
  1040. return rc;
  1041. }
  1042. EXPORT_SYMBOL(ap_queue_message);
  1043. /**
  1044. * ap_queue_usable(): Check if queue is usable just now.
  1045. * @aq: The AP queue device to test for usability.
  1046. * This function is intended for the scheduler to query if it makes
  1047. * sense to enqueue a message into this AP queue device by calling
  1048. * ap_queue_message(). The perspective is very short-term as the
  1049. * state machine and device state(s) may change at any time.
  1050. */
  1051. bool ap_queue_usable(struct ap_queue *aq)
  1052. {
  1053. bool rc = true;
  1054. spin_lock_bh(&aq->lock);
  1055. /* check for not configured or checkstopped */
  1056. if (!aq->config || aq->chkstop) {
  1057. rc = false;
  1058. goto unlock_and_out;
  1059. }
  1060. /* device state needs to be ok */
  1061. if (aq->dev_state != AP_DEV_STATE_OPERATING) {
  1062. rc = false;
  1063. goto unlock_and_out;
  1064. }
  1065. /* SE guest's queues additionally need to be bound */
  1066. if (ap_is_se_guest()) {
  1067. if (!ap_q_supported_in_se(aq)) {
  1068. rc = false;
  1069. goto unlock_and_out;
  1070. }
  1071. if (ap_q_needs_bind(aq) &&
  1072. !(aq->se_bstate == AP_BS_Q_USABLE ||
  1073. aq->se_bstate == AP_BS_Q_USABLE_NO_SECURE_KEY))
  1074. rc = false;
  1075. }
  1076. unlock_and_out:
  1077. spin_unlock_bh(&aq->lock);
  1078. return rc;
  1079. }
  1080. EXPORT_SYMBOL(ap_queue_usable);
  1081. /**
  1082. * ap_cancel_message(): Cancel a crypto request.
  1083. * @aq: The AP device that has the message queued
  1084. * @ap_msg: The message that is to be removed
  1085. *
  1086. * Cancel a crypto request. This is done by removing the request
  1087. * from the device pending or request queue. Note that the
  1088. * request stays on the AP queue. When it finishes the message
  1089. * reply will be discarded because the psmid can't be found.
  1090. */
  1091. void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg)
  1092. {
  1093. struct ap_message *tmp;
  1094. spin_lock_bh(&aq->lock);
  1095. if (!list_empty(&ap_msg->list)) {
  1096. list_for_each_entry(tmp, &aq->pendingq, list)
  1097. if (tmp->psmid == ap_msg->psmid) {
  1098. aq->pendingq_count--;
  1099. goto found;
  1100. }
  1101. aq->requestq_count--;
  1102. found:
  1103. list_del_init(&ap_msg->list);
  1104. }
  1105. spin_unlock_bh(&aq->lock);
  1106. }
  1107. EXPORT_SYMBOL(ap_cancel_message);
  1108. /**
  1109. * __ap_flush_queue(): Flush requests.
  1110. * @aq: Pointer to the AP queue
  1111. *
  1112. * Flush all requests from the request/pending queue of an AP device.
  1113. */
  1114. static void __ap_flush_queue(struct ap_queue *aq)
  1115. {
  1116. struct ap_message *ap_msg, *next;
  1117. list_for_each_entry_safe(ap_msg, next, &aq->pendingq, list) {
  1118. list_del_init(&ap_msg->list);
  1119. aq->pendingq_count--;
  1120. ap_msg->rc = -EAGAIN;
  1121. ap_msg->receive(aq, ap_msg, NULL);
  1122. }
  1123. list_for_each_entry_safe(ap_msg, next, &aq->requestq, list) {
  1124. list_del_init(&ap_msg->list);
  1125. aq->requestq_count--;
  1126. ap_msg->rc = -EAGAIN;
  1127. ap_msg->receive(aq, ap_msg, NULL);
  1128. }
  1129. aq->queue_count = 0;
  1130. }
  1131. void ap_flush_queue(struct ap_queue *aq)
  1132. {
  1133. spin_lock_bh(&aq->lock);
  1134. __ap_flush_queue(aq);
  1135. spin_unlock_bh(&aq->lock);
  1136. }
  1137. EXPORT_SYMBOL(ap_flush_queue);
  1138. void ap_queue_prepare_remove(struct ap_queue *aq)
  1139. {
  1140. spin_lock_bh(&aq->lock);
  1141. /* flush queue */
  1142. __ap_flush_queue(aq);
  1143. /* move queue device state to SHUTDOWN in progress */
  1144. aq->dev_state = AP_DEV_STATE_SHUTDOWN;
  1145. spin_unlock_bh(&aq->lock);
  1146. del_timer_sync(&aq->timeout);
  1147. }
  1148. void ap_queue_remove(struct ap_queue *aq)
  1149. {
  1150. /*
  1151. * all messages have been flushed and the device state
  1152. * is SHUTDOWN. Now reset with zero which also clears
  1153. * the irq registration and move the device state
  1154. * to the initial value AP_DEV_STATE_UNINITIATED.
  1155. */
  1156. spin_lock_bh(&aq->lock);
  1157. ap_zapq(aq->qid, 0);
  1158. aq->dev_state = AP_DEV_STATE_UNINITIATED;
  1159. spin_unlock_bh(&aq->lock);
  1160. }
  1161. void _ap_queue_init_state(struct ap_queue *aq)
  1162. {
  1163. aq->dev_state = AP_DEV_STATE_OPERATING;
  1164. aq->sm_state = AP_SM_STATE_RESET_START;
  1165. aq->last_err_rc = 0;
  1166. aq->assoc_idx = ASSOC_IDX_INVALID;
  1167. ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
  1168. }
  1169. void ap_queue_init_state(struct ap_queue *aq)
  1170. {
  1171. spin_lock_bh(&aq->lock);
  1172. _ap_queue_init_state(aq);
  1173. spin_unlock_bh(&aq->lock);
  1174. }
  1175. EXPORT_SYMBOL(ap_queue_init_state);