ap_queue.c 19 KB

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