ap_queue.c 18 KB

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