ap_queue.c 17 KB

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