ap_queue.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2016
  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_asm.h"
  15. /**
  16. * ap_queue_irq_ctrl(): Control interruption on a AP queue.
  17. * @qirqctrl: struct ap_qirq_ctrl (64 bit value)
  18. * @ind: The notification indicator byte
  19. *
  20. * Returns AP queue status.
  21. *
  22. * Control interruption on the given AP queue.
  23. * Just a simple wrapper function for the low level PQAP(AQIC)
  24. * instruction available for other kernel modules.
  25. */
  26. struct ap_queue_status ap_queue_irq_ctrl(ap_qid_t qid,
  27. struct ap_qirq_ctrl qirqctrl,
  28. void *ind)
  29. {
  30. return ap_aqic(qid, qirqctrl, ind);
  31. }
  32. EXPORT_SYMBOL(ap_queue_irq_ctrl);
  33. /**
  34. * ap_queue_enable_interruption(): Enable interruption on an AP queue.
  35. * @qid: The AP queue number
  36. * @ind: the notification indicator byte
  37. *
  38. * Enables interruption on AP queue via ap_aqic(). Based on the return
  39. * value it waits a while and tests the AP queue if interrupts
  40. * have been switched on using ap_test_queue().
  41. */
  42. static int ap_queue_enable_interruption(struct ap_queue *aq, void *ind)
  43. {
  44. struct ap_queue_status status;
  45. struct ap_qirq_ctrl qirqctrl = { 0 };
  46. qirqctrl.ir = 1;
  47. qirqctrl.isc = AP_ISC;
  48. status = ap_aqic(aq->qid, qirqctrl, ind);
  49. switch (status.response_code) {
  50. case AP_RESPONSE_NORMAL:
  51. case AP_RESPONSE_OTHERWISE_CHANGED:
  52. return 0;
  53. case AP_RESPONSE_Q_NOT_AVAIL:
  54. case AP_RESPONSE_DECONFIGURED:
  55. case AP_RESPONSE_CHECKSTOPPED:
  56. case AP_RESPONSE_INVALID_ADDRESS:
  57. pr_err("Registering adapter interrupts for AP device %02x.%04x failed\n",
  58. AP_QID_CARD(aq->qid),
  59. AP_QID_QUEUE(aq->qid));
  60. return -EOPNOTSUPP;
  61. case AP_RESPONSE_RESET_IN_PROGRESS:
  62. case AP_RESPONSE_BUSY:
  63. default:
  64. return -EBUSY;
  65. }
  66. }
  67. /**
  68. * __ap_send(): Send message to adjunct processor queue.
  69. * @qid: The AP queue number
  70. * @psmid: The program supplied message identifier
  71. * @msg: The message text
  72. * @length: The message length
  73. * @special: Special Bit
  74. *
  75. * Returns AP queue status structure.
  76. * Condition code 1 on NQAP can't happen because the L bit is 1.
  77. * Condition code 2 on NQAP also means the send is incomplete,
  78. * because a segment boundary was reached. The NQAP is repeated.
  79. */
  80. static inline struct ap_queue_status
  81. __ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length,
  82. unsigned int special)
  83. {
  84. if (special == 1)
  85. qid |= 0x400000UL;
  86. return ap_nqap(qid, psmid, msg, length);
  87. }
  88. int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
  89. {
  90. struct ap_queue_status status;
  91. status = __ap_send(qid, psmid, msg, length, 0);
  92. switch (status.response_code) {
  93. case AP_RESPONSE_NORMAL:
  94. return 0;
  95. case AP_RESPONSE_Q_FULL:
  96. case AP_RESPONSE_RESET_IN_PROGRESS:
  97. return -EBUSY;
  98. case AP_RESPONSE_REQ_FAC_NOT_INST:
  99. return -EINVAL;
  100. default: /* Device is gone. */
  101. return -ENODEV;
  102. }
  103. }
  104. EXPORT_SYMBOL(ap_send);
  105. int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
  106. {
  107. struct ap_queue_status status;
  108. if (msg == NULL)
  109. return -EINVAL;
  110. status = ap_dqap(qid, psmid, msg, length);
  111. switch (status.response_code) {
  112. case AP_RESPONSE_NORMAL:
  113. return 0;
  114. case AP_RESPONSE_NO_PENDING_REPLY:
  115. if (status.queue_empty)
  116. return -ENOENT;
  117. return -EBUSY;
  118. case AP_RESPONSE_RESET_IN_PROGRESS:
  119. return -EBUSY;
  120. default:
  121. return -ENODEV;
  122. }
  123. }
  124. EXPORT_SYMBOL(ap_recv);
  125. /* State machine definitions and helpers */
  126. static enum ap_wait ap_sm_nop(struct ap_queue *aq)
  127. {
  128. return AP_WAIT_NONE;
  129. }
  130. /**
  131. * ap_sm_recv(): Receive pending reply messages from an AP queue but do
  132. * not change the state of the device.
  133. * @aq: pointer to the AP queue
  134. *
  135. * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
  136. */
  137. static struct ap_queue_status ap_sm_recv(struct ap_queue *aq)
  138. {
  139. struct ap_queue_status status;
  140. struct ap_message *ap_msg;
  141. status = ap_dqap(aq->qid, &aq->reply->psmid,
  142. aq->reply->message, aq->reply->length);
  143. switch (status.response_code) {
  144. case AP_RESPONSE_NORMAL:
  145. aq->queue_count--;
  146. if (aq->queue_count > 0)
  147. mod_timer(&aq->timeout,
  148. jiffies + aq->request_timeout);
  149. list_for_each_entry(ap_msg, &aq->pendingq, list) {
  150. if (ap_msg->psmid != aq->reply->psmid)
  151. continue;
  152. list_del_init(&ap_msg->list);
  153. aq->pendingq_count--;
  154. ap_msg->receive(aq, ap_msg, aq->reply);
  155. break;
  156. }
  157. case AP_RESPONSE_NO_PENDING_REPLY:
  158. if (!status.queue_empty || aq->queue_count <= 0)
  159. break;
  160. /* The card shouldn't forget requests but who knows. */
  161. aq->queue_count = 0;
  162. list_splice_init(&aq->pendingq, &aq->requestq);
  163. aq->requestq_count += aq->pendingq_count;
  164. aq->pendingq_count = 0;
  165. break;
  166. default:
  167. break;
  168. }
  169. return status;
  170. }
  171. /**
  172. * ap_sm_read(): Receive pending reply messages from an AP queue.
  173. * @aq: pointer to the AP queue
  174. *
  175. * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
  176. */
  177. static enum ap_wait ap_sm_read(struct ap_queue *aq)
  178. {
  179. struct ap_queue_status status;
  180. if (!aq->reply)
  181. return AP_WAIT_NONE;
  182. status = ap_sm_recv(aq);
  183. switch (status.response_code) {
  184. case AP_RESPONSE_NORMAL:
  185. if (aq->queue_count > 0) {
  186. aq->state = AP_STATE_WORKING;
  187. return AP_WAIT_AGAIN;
  188. }
  189. aq->state = AP_STATE_IDLE;
  190. return AP_WAIT_NONE;
  191. case AP_RESPONSE_NO_PENDING_REPLY:
  192. if (aq->queue_count > 0)
  193. return AP_WAIT_INTERRUPT;
  194. aq->state = AP_STATE_IDLE;
  195. return AP_WAIT_NONE;
  196. default:
  197. aq->state = AP_STATE_BORKED;
  198. return AP_WAIT_NONE;
  199. }
  200. }
  201. /**
  202. * ap_sm_suspend_read(): Receive pending reply messages from an AP queue
  203. * without changing the device state in between. In suspend mode we don't
  204. * allow sending new requests, therefore just fetch pending replies.
  205. * @aq: pointer to the AP queue
  206. *
  207. * Returns AP_WAIT_NONE or AP_WAIT_AGAIN
  208. */
  209. static enum ap_wait ap_sm_suspend_read(struct ap_queue *aq)
  210. {
  211. struct ap_queue_status status;
  212. if (!aq->reply)
  213. return AP_WAIT_NONE;
  214. status = ap_sm_recv(aq);
  215. switch (status.response_code) {
  216. case AP_RESPONSE_NORMAL:
  217. if (aq->queue_count > 0)
  218. return AP_WAIT_AGAIN;
  219. /* fall through */
  220. default:
  221. return AP_WAIT_NONE;
  222. }
  223. }
  224. /**
  225. * ap_sm_write(): Send messages from the request queue to an AP queue.
  226. * @aq: pointer to the AP queue
  227. *
  228. * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
  229. */
  230. static enum ap_wait ap_sm_write(struct ap_queue *aq)
  231. {
  232. struct ap_queue_status status;
  233. struct ap_message *ap_msg;
  234. if (aq->requestq_count <= 0)
  235. return AP_WAIT_NONE;
  236. /* Start the next request on the queue. */
  237. ap_msg = list_entry(aq->requestq.next, struct ap_message, list);
  238. status = __ap_send(aq->qid, ap_msg->psmid,
  239. ap_msg->message, ap_msg->length, ap_msg->special);
  240. switch (status.response_code) {
  241. case AP_RESPONSE_NORMAL:
  242. aq->queue_count++;
  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->queue_depth) {
  249. aq->state = AP_STATE_WORKING;
  250. return AP_WAIT_AGAIN;
  251. }
  252. /* fall through */
  253. case AP_RESPONSE_Q_FULL:
  254. aq->state = AP_STATE_QUEUE_FULL;
  255. return AP_WAIT_INTERRUPT;
  256. case AP_RESPONSE_RESET_IN_PROGRESS:
  257. aq->state = AP_STATE_RESET_WAIT;
  258. return AP_WAIT_TIMEOUT;
  259. case AP_RESPONSE_MESSAGE_TOO_BIG:
  260. case AP_RESPONSE_REQ_FAC_NOT_INST:
  261. list_del_init(&ap_msg->list);
  262. aq->requestq_count--;
  263. ap_msg->rc = -EINVAL;
  264. ap_msg->receive(aq, ap_msg, NULL);
  265. return AP_WAIT_AGAIN;
  266. default:
  267. aq->state = AP_STATE_BORKED;
  268. return AP_WAIT_NONE;
  269. }
  270. }
  271. /**
  272. * ap_sm_read_write(): Send and receive messages to/from an AP queue.
  273. * @aq: pointer to the AP queue
  274. *
  275. * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
  276. */
  277. static enum ap_wait ap_sm_read_write(struct ap_queue *aq)
  278. {
  279. return min(ap_sm_read(aq), ap_sm_write(aq));
  280. }
  281. /**
  282. * ap_sm_reset(): Reset an AP queue.
  283. * @qid: The AP queue number
  284. *
  285. * Submit the Reset command to an AP queue.
  286. */
  287. static enum ap_wait ap_sm_reset(struct ap_queue *aq)
  288. {
  289. struct ap_queue_status status;
  290. status = ap_rapq(aq->qid);
  291. switch (status.response_code) {
  292. case AP_RESPONSE_NORMAL:
  293. case AP_RESPONSE_RESET_IN_PROGRESS:
  294. aq->state = AP_STATE_RESET_WAIT;
  295. aq->interrupt = AP_INTR_DISABLED;
  296. return AP_WAIT_TIMEOUT;
  297. case AP_RESPONSE_BUSY:
  298. return AP_WAIT_TIMEOUT;
  299. case AP_RESPONSE_Q_NOT_AVAIL:
  300. case AP_RESPONSE_DECONFIGURED:
  301. case AP_RESPONSE_CHECKSTOPPED:
  302. default:
  303. aq->state = AP_STATE_BORKED;
  304. return AP_WAIT_NONE;
  305. }
  306. }
  307. /**
  308. * ap_sm_reset_wait(): Test queue for completion of the reset operation
  309. * @aq: pointer to the AP queue
  310. *
  311. * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
  312. */
  313. static enum ap_wait ap_sm_reset_wait(struct ap_queue *aq)
  314. {
  315. struct ap_queue_status status;
  316. void *lsi_ptr;
  317. if (aq->queue_count > 0 && aq->reply)
  318. /* Try to read a completed message and get the status */
  319. status = ap_sm_recv(aq);
  320. else
  321. /* Get the status with TAPQ */
  322. status = ap_tapq(aq->qid, NULL);
  323. switch (status.response_code) {
  324. case AP_RESPONSE_NORMAL:
  325. lsi_ptr = ap_airq_ptr();
  326. if (lsi_ptr && ap_queue_enable_interruption(aq, lsi_ptr) == 0)
  327. aq->state = AP_STATE_SETIRQ_WAIT;
  328. else
  329. aq->state = (aq->queue_count > 0) ?
  330. AP_STATE_WORKING : AP_STATE_IDLE;
  331. return AP_WAIT_AGAIN;
  332. case AP_RESPONSE_BUSY:
  333. case AP_RESPONSE_RESET_IN_PROGRESS:
  334. return AP_WAIT_TIMEOUT;
  335. case AP_RESPONSE_Q_NOT_AVAIL:
  336. case AP_RESPONSE_DECONFIGURED:
  337. case AP_RESPONSE_CHECKSTOPPED:
  338. default:
  339. aq->state = AP_STATE_BORKED;
  340. return AP_WAIT_NONE;
  341. }
  342. }
  343. /**
  344. * ap_sm_setirq_wait(): Test queue for completion of the irq enablement
  345. * @aq: pointer to the AP queue
  346. *
  347. * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
  348. */
  349. static enum ap_wait ap_sm_setirq_wait(struct ap_queue *aq)
  350. {
  351. struct ap_queue_status status;
  352. if (aq->queue_count > 0 && aq->reply)
  353. /* Try to read a completed message and get the status */
  354. status = ap_sm_recv(aq);
  355. else
  356. /* Get the status with TAPQ */
  357. status = ap_tapq(aq->qid, NULL);
  358. if (status.irq_enabled == 1) {
  359. /* Irqs are now enabled */
  360. aq->interrupt = AP_INTR_ENABLED;
  361. aq->state = (aq->queue_count > 0) ?
  362. AP_STATE_WORKING : AP_STATE_IDLE;
  363. }
  364. switch (status.response_code) {
  365. case AP_RESPONSE_NORMAL:
  366. if (aq->queue_count > 0)
  367. return AP_WAIT_AGAIN;
  368. /* fallthrough */
  369. case AP_RESPONSE_NO_PENDING_REPLY:
  370. return AP_WAIT_TIMEOUT;
  371. default:
  372. aq->state = AP_STATE_BORKED;
  373. return AP_WAIT_NONE;
  374. }
  375. }
  376. /*
  377. * AP state machine jump table
  378. */
  379. static ap_func_t *ap_jumptable[NR_AP_STATES][NR_AP_EVENTS] = {
  380. [AP_STATE_RESET_START] = {
  381. [AP_EVENT_POLL] = ap_sm_reset,
  382. [AP_EVENT_TIMEOUT] = ap_sm_nop,
  383. },
  384. [AP_STATE_RESET_WAIT] = {
  385. [AP_EVENT_POLL] = ap_sm_reset_wait,
  386. [AP_EVENT_TIMEOUT] = ap_sm_nop,
  387. },
  388. [AP_STATE_SETIRQ_WAIT] = {
  389. [AP_EVENT_POLL] = ap_sm_setirq_wait,
  390. [AP_EVENT_TIMEOUT] = ap_sm_nop,
  391. },
  392. [AP_STATE_IDLE] = {
  393. [AP_EVENT_POLL] = ap_sm_write,
  394. [AP_EVENT_TIMEOUT] = ap_sm_nop,
  395. },
  396. [AP_STATE_WORKING] = {
  397. [AP_EVENT_POLL] = ap_sm_read_write,
  398. [AP_EVENT_TIMEOUT] = ap_sm_reset,
  399. },
  400. [AP_STATE_QUEUE_FULL] = {
  401. [AP_EVENT_POLL] = ap_sm_read,
  402. [AP_EVENT_TIMEOUT] = ap_sm_reset,
  403. },
  404. [AP_STATE_SUSPEND_WAIT] = {
  405. [AP_EVENT_POLL] = ap_sm_suspend_read,
  406. [AP_EVENT_TIMEOUT] = ap_sm_nop,
  407. },
  408. [AP_STATE_BORKED] = {
  409. [AP_EVENT_POLL] = ap_sm_nop,
  410. [AP_EVENT_TIMEOUT] = ap_sm_nop,
  411. },
  412. };
  413. enum ap_wait ap_sm_event(struct ap_queue *aq, enum ap_event event)
  414. {
  415. return ap_jumptable[aq->state][event](aq);
  416. }
  417. enum ap_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_event event)
  418. {
  419. enum ap_wait wait;
  420. while ((wait = ap_sm_event(aq, event)) == AP_WAIT_AGAIN)
  421. ;
  422. return wait;
  423. }
  424. /*
  425. * Power management for queue devices
  426. */
  427. void ap_queue_suspend(struct ap_device *ap_dev)
  428. {
  429. struct ap_queue *aq = to_ap_queue(&ap_dev->device);
  430. /* Poll on the device until all requests are finished. */
  431. spin_lock_bh(&aq->lock);
  432. aq->state = AP_STATE_SUSPEND_WAIT;
  433. while (ap_sm_event(aq, AP_EVENT_POLL) != AP_WAIT_NONE)
  434. ;
  435. aq->state = AP_STATE_BORKED;
  436. spin_unlock_bh(&aq->lock);
  437. }
  438. EXPORT_SYMBOL(ap_queue_suspend);
  439. void ap_queue_resume(struct ap_device *ap_dev)
  440. {
  441. }
  442. EXPORT_SYMBOL(ap_queue_resume);
  443. /*
  444. * AP queue related attributes.
  445. */
  446. static ssize_t ap_req_count_show(struct device *dev,
  447. struct device_attribute *attr,
  448. char *buf)
  449. {
  450. struct ap_queue *aq = to_ap_queue(dev);
  451. unsigned int req_cnt;
  452. spin_lock_bh(&aq->lock);
  453. req_cnt = aq->total_request_count;
  454. spin_unlock_bh(&aq->lock);
  455. return snprintf(buf, PAGE_SIZE, "%d\n", req_cnt);
  456. }
  457. static ssize_t ap_req_count_store(struct device *dev,
  458. struct device_attribute *attr,
  459. const char *buf, size_t count)
  460. {
  461. struct ap_queue *aq = to_ap_queue(dev);
  462. spin_lock_bh(&aq->lock);
  463. aq->total_request_count = 0;
  464. spin_unlock_bh(&aq->lock);
  465. return count;
  466. }
  467. static DEVICE_ATTR(request_count, 0644, ap_req_count_show, ap_req_count_store);
  468. static ssize_t ap_requestq_count_show(struct device *dev,
  469. struct device_attribute *attr, char *buf)
  470. {
  471. struct ap_queue *aq = to_ap_queue(dev);
  472. unsigned int reqq_cnt = 0;
  473. spin_lock_bh(&aq->lock);
  474. reqq_cnt = aq->requestq_count;
  475. spin_unlock_bh(&aq->lock);
  476. return snprintf(buf, PAGE_SIZE, "%d\n", reqq_cnt);
  477. }
  478. static DEVICE_ATTR(requestq_count, 0444, ap_requestq_count_show, NULL);
  479. static ssize_t ap_pendingq_count_show(struct device *dev,
  480. struct device_attribute *attr, char *buf)
  481. {
  482. struct ap_queue *aq = to_ap_queue(dev);
  483. unsigned int penq_cnt = 0;
  484. spin_lock_bh(&aq->lock);
  485. penq_cnt = aq->pendingq_count;
  486. spin_unlock_bh(&aq->lock);
  487. return snprintf(buf, PAGE_SIZE, "%d\n", penq_cnt);
  488. }
  489. static DEVICE_ATTR(pendingq_count, 0444, ap_pendingq_count_show, NULL);
  490. static ssize_t ap_reset_show(struct device *dev,
  491. struct device_attribute *attr, char *buf)
  492. {
  493. struct ap_queue *aq = to_ap_queue(dev);
  494. int rc = 0;
  495. spin_lock_bh(&aq->lock);
  496. switch (aq->state) {
  497. case AP_STATE_RESET_START:
  498. case AP_STATE_RESET_WAIT:
  499. rc = snprintf(buf, PAGE_SIZE, "Reset in progress.\n");
  500. break;
  501. case AP_STATE_WORKING:
  502. case AP_STATE_QUEUE_FULL:
  503. rc = snprintf(buf, PAGE_SIZE, "Reset Timer armed.\n");
  504. break;
  505. default:
  506. rc = snprintf(buf, PAGE_SIZE, "No Reset Timer set.\n");
  507. }
  508. spin_unlock_bh(&aq->lock);
  509. return rc;
  510. }
  511. static DEVICE_ATTR(reset, 0444, ap_reset_show, NULL);
  512. static ssize_t ap_interrupt_show(struct device *dev,
  513. struct device_attribute *attr, char *buf)
  514. {
  515. struct ap_queue *aq = to_ap_queue(dev);
  516. int rc = 0;
  517. spin_lock_bh(&aq->lock);
  518. if (aq->state == AP_STATE_SETIRQ_WAIT)
  519. rc = snprintf(buf, PAGE_SIZE, "Enable Interrupt pending.\n");
  520. else if (aq->interrupt == AP_INTR_ENABLED)
  521. rc = snprintf(buf, PAGE_SIZE, "Interrupts enabled.\n");
  522. else
  523. rc = snprintf(buf, PAGE_SIZE, "Interrupts disabled.\n");
  524. spin_unlock_bh(&aq->lock);
  525. return rc;
  526. }
  527. static DEVICE_ATTR(interrupt, 0444, ap_interrupt_show, NULL);
  528. static struct attribute *ap_queue_dev_attrs[] = {
  529. &dev_attr_request_count.attr,
  530. &dev_attr_requestq_count.attr,
  531. &dev_attr_pendingq_count.attr,
  532. &dev_attr_reset.attr,
  533. &dev_attr_interrupt.attr,
  534. NULL
  535. };
  536. static struct attribute_group ap_queue_dev_attr_group = {
  537. .attrs = ap_queue_dev_attrs
  538. };
  539. static const struct attribute_group *ap_queue_dev_attr_groups[] = {
  540. &ap_queue_dev_attr_group,
  541. NULL
  542. };
  543. static struct device_type ap_queue_type = {
  544. .name = "ap_queue",
  545. .groups = ap_queue_dev_attr_groups,
  546. };
  547. static void ap_queue_device_release(struct device *dev)
  548. {
  549. struct ap_queue *aq = to_ap_queue(dev);
  550. if (!list_empty(&aq->list)) {
  551. spin_lock_bh(&ap_list_lock);
  552. list_del_init(&aq->list);
  553. spin_unlock_bh(&ap_list_lock);
  554. }
  555. kfree(aq);
  556. }
  557. struct ap_queue *ap_queue_create(ap_qid_t qid, int device_type)
  558. {
  559. struct ap_queue *aq;
  560. aq = kzalloc(sizeof(*aq), GFP_KERNEL);
  561. if (!aq)
  562. return NULL;
  563. aq->ap_dev.device.release = ap_queue_device_release;
  564. aq->ap_dev.device.type = &ap_queue_type;
  565. aq->ap_dev.device_type = device_type;
  566. aq->qid = qid;
  567. aq->state = AP_STATE_RESET_START;
  568. aq->interrupt = AP_INTR_DISABLED;
  569. spin_lock_init(&aq->lock);
  570. INIT_LIST_HEAD(&aq->list);
  571. INIT_LIST_HEAD(&aq->pendingq);
  572. INIT_LIST_HEAD(&aq->requestq);
  573. timer_setup(&aq->timeout, ap_request_timeout, 0);
  574. return aq;
  575. }
  576. void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *reply)
  577. {
  578. aq->reply = reply;
  579. spin_lock_bh(&aq->lock);
  580. ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
  581. spin_unlock_bh(&aq->lock);
  582. }
  583. EXPORT_SYMBOL(ap_queue_init_reply);
  584. /**
  585. * ap_queue_message(): Queue a request to an AP device.
  586. * @aq: The AP device to queue the message to
  587. * @ap_msg: The message that is to be added
  588. */
  589. void ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg)
  590. {
  591. /* For asynchronous message handling a valid receive-callback
  592. * is required.
  593. */
  594. BUG_ON(!ap_msg->receive);
  595. spin_lock_bh(&aq->lock);
  596. /* Queue the message. */
  597. list_add_tail(&ap_msg->list, &aq->requestq);
  598. aq->requestq_count++;
  599. aq->total_request_count++;
  600. atomic_inc(&aq->card->total_request_count);
  601. /* Send/receive as many request from the queue as possible. */
  602. ap_wait(ap_sm_event_loop(aq, AP_EVENT_POLL));
  603. spin_unlock_bh(&aq->lock);
  604. }
  605. EXPORT_SYMBOL(ap_queue_message);
  606. /**
  607. * ap_cancel_message(): Cancel a crypto request.
  608. * @aq: The AP device that has the message queued
  609. * @ap_msg: The message that is to be removed
  610. *
  611. * Cancel a crypto request. This is done by removing the request
  612. * from the device pending or request queue. Note that the
  613. * request stays on the AP queue. When it finishes the message
  614. * reply will be discarded because the psmid can't be found.
  615. */
  616. void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg)
  617. {
  618. struct ap_message *tmp;
  619. spin_lock_bh(&aq->lock);
  620. if (!list_empty(&ap_msg->list)) {
  621. list_for_each_entry(tmp, &aq->pendingq, list)
  622. if (tmp->psmid == ap_msg->psmid) {
  623. aq->pendingq_count--;
  624. goto found;
  625. }
  626. aq->requestq_count--;
  627. found:
  628. list_del_init(&ap_msg->list);
  629. }
  630. spin_unlock_bh(&aq->lock);
  631. }
  632. EXPORT_SYMBOL(ap_cancel_message);
  633. /**
  634. * __ap_flush_queue(): Flush requests.
  635. * @aq: Pointer to the AP queue
  636. *
  637. * Flush all requests from the request/pending queue of an AP device.
  638. */
  639. static void __ap_flush_queue(struct ap_queue *aq)
  640. {
  641. struct ap_message *ap_msg, *next;
  642. list_for_each_entry_safe(ap_msg, next, &aq->pendingq, list) {
  643. list_del_init(&ap_msg->list);
  644. aq->pendingq_count--;
  645. ap_msg->rc = -EAGAIN;
  646. ap_msg->receive(aq, ap_msg, NULL);
  647. }
  648. list_for_each_entry_safe(ap_msg, next, &aq->requestq, list) {
  649. list_del_init(&ap_msg->list);
  650. aq->requestq_count--;
  651. ap_msg->rc = -EAGAIN;
  652. ap_msg->receive(aq, ap_msg, NULL);
  653. }
  654. }
  655. void ap_flush_queue(struct ap_queue *aq)
  656. {
  657. spin_lock_bh(&aq->lock);
  658. __ap_flush_queue(aq);
  659. spin_unlock_bh(&aq->lock);
  660. }
  661. EXPORT_SYMBOL(ap_flush_queue);
  662. void ap_queue_remove(struct ap_queue *aq)
  663. {
  664. ap_flush_queue(aq);
  665. del_timer_sync(&aq->timeout);
  666. }
  667. EXPORT_SYMBOL(ap_queue_remove);