ap_queue.c 18 KB

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