Linux 5.1.15
[linux/fpc-iii.git] / drivers / s390 / crypto / ap_queue.c
blob5ea83dc4f1d740e9db1288ed9d8f70423312d5ba
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright IBM Corp. 2016
4 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
6 * Adjunct processor bus, queue related code.
7 */
9 #define KMSG_COMPONENT "ap"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <asm/facility.h>
16 #include "ap_bus.h"
17 #include "ap_debug.h"
19 static void __ap_flush_queue(struct ap_queue *aq);
21 /**
22 * ap_queue_enable_interruption(): Enable interruption on an AP queue.
23 * @qid: The AP queue number
24 * @ind: the notification indicator byte
26 * Enables interruption on AP queue via ap_aqic(). Based on the return
27 * value it waits a while and tests the AP queue if interrupts
28 * have been switched on using ap_test_queue().
30 static int ap_queue_enable_interruption(struct ap_queue *aq, void *ind)
32 struct ap_queue_status status;
33 struct ap_qirq_ctrl qirqctrl = { 0 };
35 qirqctrl.ir = 1;
36 qirqctrl.isc = AP_ISC;
37 status = ap_aqic(aq->qid, qirqctrl, ind);
38 switch (status.response_code) {
39 case AP_RESPONSE_NORMAL:
40 case AP_RESPONSE_OTHERWISE_CHANGED:
41 return 0;
42 case AP_RESPONSE_Q_NOT_AVAIL:
43 case AP_RESPONSE_DECONFIGURED:
44 case AP_RESPONSE_CHECKSTOPPED:
45 case AP_RESPONSE_INVALID_ADDRESS:
46 pr_err("Registering adapter interrupts for AP device %02x.%04x failed\n",
47 AP_QID_CARD(aq->qid),
48 AP_QID_QUEUE(aq->qid));
49 return -EOPNOTSUPP;
50 case AP_RESPONSE_RESET_IN_PROGRESS:
51 case AP_RESPONSE_BUSY:
52 default:
53 return -EBUSY;
57 /**
58 * __ap_send(): Send message to adjunct processor queue.
59 * @qid: The AP queue number
60 * @psmid: The program supplied message identifier
61 * @msg: The message text
62 * @length: The message length
63 * @special: Special Bit
65 * Returns AP queue status structure.
66 * Condition code 1 on NQAP can't happen because the L bit is 1.
67 * Condition code 2 on NQAP also means the send is incomplete,
68 * because a segment boundary was reached. The NQAP is repeated.
70 static inline struct ap_queue_status
71 __ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length,
72 unsigned int special)
74 if (special == 1)
75 qid |= 0x400000UL;
76 return ap_nqap(qid, psmid, msg, length);
79 int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
81 struct ap_queue_status status;
83 status = __ap_send(qid, psmid, msg, length, 0);
84 switch (status.response_code) {
85 case AP_RESPONSE_NORMAL:
86 return 0;
87 case AP_RESPONSE_Q_FULL:
88 case AP_RESPONSE_RESET_IN_PROGRESS:
89 return -EBUSY;
90 case AP_RESPONSE_REQ_FAC_NOT_INST:
91 return -EINVAL;
92 default: /* Device is gone. */
93 return -ENODEV;
96 EXPORT_SYMBOL(ap_send);
98 int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
100 struct ap_queue_status status;
102 if (msg == NULL)
103 return -EINVAL;
104 status = ap_dqap(qid, psmid, msg, length);
105 switch (status.response_code) {
106 case AP_RESPONSE_NORMAL:
107 return 0;
108 case AP_RESPONSE_NO_PENDING_REPLY:
109 if (status.queue_empty)
110 return -ENOENT;
111 return -EBUSY;
112 case AP_RESPONSE_RESET_IN_PROGRESS:
113 return -EBUSY;
114 default:
115 return -ENODEV;
118 EXPORT_SYMBOL(ap_recv);
120 /* State machine definitions and helpers */
122 static enum ap_wait ap_sm_nop(struct ap_queue *aq)
124 return AP_WAIT_NONE;
128 * ap_sm_recv(): Receive pending reply messages from an AP queue but do
129 * not change the state of the device.
130 * @aq: pointer to the AP queue
132 * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
134 static struct ap_queue_status ap_sm_recv(struct ap_queue *aq)
136 struct ap_queue_status status;
137 struct ap_message *ap_msg;
139 status = ap_dqap(aq->qid, &aq->reply->psmid,
140 aq->reply->message, aq->reply->length);
141 switch (status.response_code) {
142 case AP_RESPONSE_NORMAL:
143 aq->queue_count--;
144 if (aq->queue_count > 0)
145 mod_timer(&aq->timeout,
146 jiffies + aq->request_timeout);
147 list_for_each_entry(ap_msg, &aq->pendingq, list) {
148 if (ap_msg->psmid != aq->reply->psmid)
149 continue;
150 list_del_init(&ap_msg->list);
151 aq->pendingq_count--;
152 ap_msg->receive(aq, ap_msg, aq->reply);
153 break;
155 case AP_RESPONSE_NO_PENDING_REPLY:
156 if (!status.queue_empty || aq->queue_count <= 0)
157 break;
158 /* The card shouldn't forget requests but who knows. */
159 aq->queue_count = 0;
160 list_splice_init(&aq->pendingq, &aq->requestq);
161 aq->requestq_count += aq->pendingq_count;
162 aq->pendingq_count = 0;
163 break;
164 default:
165 break;
167 return status;
171 * ap_sm_read(): Receive pending reply messages from an AP queue.
172 * @aq: pointer to the AP queue
174 * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
176 static enum ap_wait ap_sm_read(struct ap_queue *aq)
178 struct ap_queue_status status;
180 if (!aq->reply)
181 return AP_WAIT_NONE;
182 status = ap_sm_recv(aq);
183 switch (status.response_code) {
184 case AP_RESPONSE_NORMAL:
185 if (aq->queue_count > 0) {
186 aq->state = AP_STATE_WORKING;
187 return AP_WAIT_AGAIN;
189 aq->state = AP_STATE_IDLE;
190 return AP_WAIT_NONE;
191 case AP_RESPONSE_NO_PENDING_REPLY:
192 if (aq->queue_count > 0)
193 return AP_WAIT_INTERRUPT;
194 aq->state = AP_STATE_IDLE;
195 return AP_WAIT_NONE;
196 default:
197 aq->state = AP_STATE_BORKED;
198 return AP_WAIT_NONE;
203 * ap_sm_suspend_read(): Receive pending reply messages from an AP queue
204 * without changing the device state in between. In suspend mode we don't
205 * allow sending new requests, therefore just fetch pending replies.
206 * @aq: pointer to the AP queue
208 * Returns AP_WAIT_NONE or AP_WAIT_AGAIN
210 static enum ap_wait ap_sm_suspend_read(struct ap_queue *aq)
212 struct ap_queue_status status;
214 if (!aq->reply)
215 return AP_WAIT_NONE;
216 status = ap_sm_recv(aq);
217 switch (status.response_code) {
218 case AP_RESPONSE_NORMAL:
219 if (aq->queue_count > 0)
220 return AP_WAIT_AGAIN;
221 /* fall through */
222 default:
223 return AP_WAIT_NONE;
228 * ap_sm_write(): Send messages from the request queue to an AP queue.
229 * @aq: pointer to the AP queue
231 * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
233 static enum ap_wait ap_sm_write(struct ap_queue *aq)
235 struct ap_queue_status status;
236 struct ap_message *ap_msg;
238 if (aq->requestq_count <= 0)
239 return AP_WAIT_NONE;
240 /* Start the next request on the queue. */
241 ap_msg = list_entry(aq->requestq.next, struct ap_message, list);
242 status = __ap_send(aq->qid, ap_msg->psmid,
243 ap_msg->message, ap_msg->length, ap_msg->special);
244 switch (status.response_code) {
245 case AP_RESPONSE_NORMAL:
246 aq->queue_count++;
247 if (aq->queue_count == 1)
248 mod_timer(&aq->timeout, jiffies + aq->request_timeout);
249 list_move_tail(&ap_msg->list, &aq->pendingq);
250 aq->requestq_count--;
251 aq->pendingq_count++;
252 if (aq->queue_count < aq->card->queue_depth) {
253 aq->state = AP_STATE_WORKING;
254 return AP_WAIT_AGAIN;
256 /* fall through */
257 case AP_RESPONSE_Q_FULL:
258 aq->state = AP_STATE_QUEUE_FULL;
259 return AP_WAIT_INTERRUPT;
260 case AP_RESPONSE_RESET_IN_PROGRESS:
261 aq->state = AP_STATE_RESET_WAIT;
262 return AP_WAIT_TIMEOUT;
263 case AP_RESPONSE_MESSAGE_TOO_BIG:
264 case AP_RESPONSE_REQ_FAC_NOT_INST:
265 list_del_init(&ap_msg->list);
266 aq->requestq_count--;
267 ap_msg->rc = -EINVAL;
268 ap_msg->receive(aq, ap_msg, NULL);
269 return AP_WAIT_AGAIN;
270 default:
271 aq->state = AP_STATE_BORKED;
272 return AP_WAIT_NONE;
277 * ap_sm_read_write(): Send and receive messages to/from an AP queue.
278 * @aq: pointer to the AP queue
280 * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
282 static enum ap_wait ap_sm_read_write(struct ap_queue *aq)
284 return min(ap_sm_read(aq), ap_sm_write(aq));
288 * ap_sm_reset(): Reset an AP queue.
289 * @qid: The AP queue number
291 * Submit the Reset command to an AP queue.
293 static enum ap_wait ap_sm_reset(struct ap_queue *aq)
295 struct ap_queue_status status;
297 status = ap_rapq(aq->qid);
298 switch (status.response_code) {
299 case AP_RESPONSE_NORMAL:
300 case AP_RESPONSE_RESET_IN_PROGRESS:
301 aq->state = AP_STATE_RESET_WAIT;
302 aq->interrupt = AP_INTR_DISABLED;
303 return AP_WAIT_TIMEOUT;
304 case AP_RESPONSE_BUSY:
305 return AP_WAIT_TIMEOUT;
306 case AP_RESPONSE_Q_NOT_AVAIL:
307 case AP_RESPONSE_DECONFIGURED:
308 case AP_RESPONSE_CHECKSTOPPED:
309 default:
310 aq->state = AP_STATE_BORKED;
311 return AP_WAIT_NONE;
316 * ap_sm_reset_wait(): Test queue for completion of the reset operation
317 * @aq: pointer to the AP queue
319 * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
321 static enum ap_wait ap_sm_reset_wait(struct ap_queue *aq)
323 struct ap_queue_status status;
324 void *lsi_ptr;
326 if (aq->queue_count > 0 && aq->reply)
327 /* Try to read a completed message and get the status */
328 status = ap_sm_recv(aq);
329 else
330 /* Get the status with TAPQ */
331 status = ap_tapq(aq->qid, NULL);
333 switch (status.response_code) {
334 case AP_RESPONSE_NORMAL:
335 lsi_ptr = ap_airq_ptr();
336 if (lsi_ptr && ap_queue_enable_interruption(aq, lsi_ptr) == 0)
337 aq->state = AP_STATE_SETIRQ_WAIT;
338 else
339 aq->state = (aq->queue_count > 0) ?
340 AP_STATE_WORKING : AP_STATE_IDLE;
341 return AP_WAIT_AGAIN;
342 case AP_RESPONSE_BUSY:
343 case AP_RESPONSE_RESET_IN_PROGRESS:
344 return AP_WAIT_TIMEOUT;
345 case AP_RESPONSE_Q_NOT_AVAIL:
346 case AP_RESPONSE_DECONFIGURED:
347 case AP_RESPONSE_CHECKSTOPPED:
348 default:
349 aq->state = AP_STATE_BORKED;
350 return AP_WAIT_NONE;
355 * ap_sm_setirq_wait(): Test queue for completion of the irq enablement
356 * @aq: pointer to the AP queue
358 * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
360 static enum ap_wait ap_sm_setirq_wait(struct ap_queue *aq)
362 struct ap_queue_status status;
364 if (aq->queue_count > 0 && aq->reply)
365 /* Try to read a completed message and get the status */
366 status = ap_sm_recv(aq);
367 else
368 /* Get the status with TAPQ */
369 status = ap_tapq(aq->qid, NULL);
371 if (status.irq_enabled == 1) {
372 /* Irqs are now enabled */
373 aq->interrupt = AP_INTR_ENABLED;
374 aq->state = (aq->queue_count > 0) ?
375 AP_STATE_WORKING : AP_STATE_IDLE;
378 switch (status.response_code) {
379 case AP_RESPONSE_NORMAL:
380 if (aq->queue_count > 0)
381 return AP_WAIT_AGAIN;
382 /* fallthrough */
383 case AP_RESPONSE_NO_PENDING_REPLY:
384 return AP_WAIT_TIMEOUT;
385 default:
386 aq->state = AP_STATE_BORKED;
387 return AP_WAIT_NONE;
392 * AP state machine jump table
394 static ap_func_t *ap_jumptable[NR_AP_STATES][NR_AP_EVENTS] = {
395 [AP_STATE_RESET_START] = {
396 [AP_EVENT_POLL] = ap_sm_reset,
397 [AP_EVENT_TIMEOUT] = ap_sm_nop,
399 [AP_STATE_RESET_WAIT] = {
400 [AP_EVENT_POLL] = ap_sm_reset_wait,
401 [AP_EVENT_TIMEOUT] = ap_sm_nop,
403 [AP_STATE_SETIRQ_WAIT] = {
404 [AP_EVENT_POLL] = ap_sm_setirq_wait,
405 [AP_EVENT_TIMEOUT] = ap_sm_nop,
407 [AP_STATE_IDLE] = {
408 [AP_EVENT_POLL] = ap_sm_write,
409 [AP_EVENT_TIMEOUT] = ap_sm_nop,
411 [AP_STATE_WORKING] = {
412 [AP_EVENT_POLL] = ap_sm_read_write,
413 [AP_EVENT_TIMEOUT] = ap_sm_reset,
415 [AP_STATE_QUEUE_FULL] = {
416 [AP_EVENT_POLL] = ap_sm_read,
417 [AP_EVENT_TIMEOUT] = ap_sm_reset,
419 [AP_STATE_SUSPEND_WAIT] = {
420 [AP_EVENT_POLL] = ap_sm_suspend_read,
421 [AP_EVENT_TIMEOUT] = ap_sm_nop,
423 [AP_STATE_REMOVE] = {
424 [AP_EVENT_POLL] = ap_sm_nop,
425 [AP_EVENT_TIMEOUT] = ap_sm_nop,
427 [AP_STATE_UNBOUND] = {
428 [AP_EVENT_POLL] = ap_sm_nop,
429 [AP_EVENT_TIMEOUT] = ap_sm_nop,
431 [AP_STATE_BORKED] = {
432 [AP_EVENT_POLL] = ap_sm_nop,
433 [AP_EVENT_TIMEOUT] = ap_sm_nop,
437 enum ap_wait ap_sm_event(struct ap_queue *aq, enum ap_event event)
439 return ap_jumptable[aq->state][event](aq);
442 enum ap_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_event event)
444 enum ap_wait wait;
446 while ((wait = ap_sm_event(aq, event)) == AP_WAIT_AGAIN)
448 return wait;
452 * Power management for queue devices
454 void ap_queue_suspend(struct ap_device *ap_dev)
456 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
458 /* Poll on the device until all requests are finished. */
459 spin_lock_bh(&aq->lock);
460 aq->state = AP_STATE_SUSPEND_WAIT;
461 while (ap_sm_event(aq, AP_EVENT_POLL) != AP_WAIT_NONE)
463 aq->state = AP_STATE_BORKED;
464 spin_unlock_bh(&aq->lock);
466 EXPORT_SYMBOL(ap_queue_suspend);
468 void ap_queue_resume(struct ap_device *ap_dev)
471 EXPORT_SYMBOL(ap_queue_resume);
474 * AP queue related attributes.
476 static ssize_t request_count_show(struct device *dev,
477 struct device_attribute *attr,
478 char *buf)
480 struct ap_queue *aq = to_ap_queue(dev);
481 unsigned int req_cnt;
483 spin_lock_bh(&aq->lock);
484 req_cnt = aq->total_request_count;
485 spin_unlock_bh(&aq->lock);
486 return snprintf(buf, PAGE_SIZE, "%d\n", req_cnt);
489 static ssize_t request_count_store(struct device *dev,
490 struct device_attribute *attr,
491 const char *buf, size_t count)
493 struct ap_queue *aq = to_ap_queue(dev);
495 spin_lock_bh(&aq->lock);
496 aq->total_request_count = 0;
497 spin_unlock_bh(&aq->lock);
499 return count;
502 static DEVICE_ATTR_RW(request_count);
504 static ssize_t requestq_count_show(struct device *dev,
505 struct device_attribute *attr, char *buf)
507 struct ap_queue *aq = to_ap_queue(dev);
508 unsigned int reqq_cnt = 0;
510 spin_lock_bh(&aq->lock);
511 reqq_cnt = aq->requestq_count;
512 spin_unlock_bh(&aq->lock);
513 return snprintf(buf, PAGE_SIZE, "%d\n", reqq_cnt);
516 static DEVICE_ATTR_RO(requestq_count);
518 static ssize_t pendingq_count_show(struct device *dev,
519 struct device_attribute *attr, char *buf)
521 struct ap_queue *aq = to_ap_queue(dev);
522 unsigned int penq_cnt = 0;
524 spin_lock_bh(&aq->lock);
525 penq_cnt = aq->pendingq_count;
526 spin_unlock_bh(&aq->lock);
527 return snprintf(buf, PAGE_SIZE, "%d\n", penq_cnt);
530 static DEVICE_ATTR_RO(pendingq_count);
532 static ssize_t reset_show(struct device *dev,
533 struct device_attribute *attr, char *buf)
535 struct ap_queue *aq = to_ap_queue(dev);
536 int rc = 0;
538 spin_lock_bh(&aq->lock);
539 switch (aq->state) {
540 case AP_STATE_RESET_START:
541 case AP_STATE_RESET_WAIT:
542 rc = snprintf(buf, PAGE_SIZE, "Reset in progress.\n");
543 break;
544 case AP_STATE_WORKING:
545 case AP_STATE_QUEUE_FULL:
546 rc = snprintf(buf, PAGE_SIZE, "Reset Timer armed.\n");
547 break;
548 default:
549 rc = snprintf(buf, PAGE_SIZE, "No Reset Timer set.\n");
551 spin_unlock_bh(&aq->lock);
552 return rc;
555 static ssize_t reset_store(struct device *dev,
556 struct device_attribute *attr,
557 const char *buf, size_t count)
559 struct ap_queue *aq = to_ap_queue(dev);
561 spin_lock_bh(&aq->lock);
562 __ap_flush_queue(aq);
563 aq->state = AP_STATE_RESET_START;
564 ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
565 spin_unlock_bh(&aq->lock);
567 AP_DBF(DBF_INFO, "reset queue=%02x.%04x triggered by user\n",
568 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
570 return count;
573 static DEVICE_ATTR_RW(reset);
575 static ssize_t interrupt_show(struct device *dev,
576 struct device_attribute *attr, char *buf)
578 struct ap_queue *aq = to_ap_queue(dev);
579 int rc = 0;
581 spin_lock_bh(&aq->lock);
582 if (aq->state == AP_STATE_SETIRQ_WAIT)
583 rc = snprintf(buf, PAGE_SIZE, "Enable Interrupt pending.\n");
584 else if (aq->interrupt == AP_INTR_ENABLED)
585 rc = snprintf(buf, PAGE_SIZE, "Interrupts enabled.\n");
586 else
587 rc = snprintf(buf, PAGE_SIZE, "Interrupts disabled.\n");
588 spin_unlock_bh(&aq->lock);
589 return rc;
592 static DEVICE_ATTR_RO(interrupt);
594 static struct attribute *ap_queue_dev_attrs[] = {
595 &dev_attr_request_count.attr,
596 &dev_attr_requestq_count.attr,
597 &dev_attr_pendingq_count.attr,
598 &dev_attr_reset.attr,
599 &dev_attr_interrupt.attr,
600 NULL
603 static struct attribute_group ap_queue_dev_attr_group = {
604 .attrs = ap_queue_dev_attrs
607 static const struct attribute_group *ap_queue_dev_attr_groups[] = {
608 &ap_queue_dev_attr_group,
609 NULL
612 static struct device_type ap_queue_type = {
613 .name = "ap_queue",
614 .groups = ap_queue_dev_attr_groups,
617 static void ap_queue_device_release(struct device *dev)
619 struct ap_queue *aq = to_ap_queue(dev);
621 if (!list_empty(&aq->list)) {
622 spin_lock_bh(&ap_list_lock);
623 list_del_init(&aq->list);
624 spin_unlock_bh(&ap_list_lock);
626 kfree(aq);
629 struct ap_queue *ap_queue_create(ap_qid_t qid, int device_type)
631 struct ap_queue *aq;
633 aq = kzalloc(sizeof(*aq), GFP_KERNEL);
634 if (!aq)
635 return NULL;
636 aq->ap_dev.device.release = ap_queue_device_release;
637 aq->ap_dev.device.type = &ap_queue_type;
638 aq->ap_dev.device_type = device_type;
639 aq->qid = qid;
640 aq->state = AP_STATE_RESET_START;
641 aq->interrupt = AP_INTR_DISABLED;
642 spin_lock_init(&aq->lock);
643 INIT_LIST_HEAD(&aq->list);
644 INIT_LIST_HEAD(&aq->pendingq);
645 INIT_LIST_HEAD(&aq->requestq);
646 timer_setup(&aq->timeout, ap_request_timeout, 0);
648 return aq;
651 void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *reply)
653 aq->reply = reply;
655 spin_lock_bh(&aq->lock);
656 ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
657 spin_unlock_bh(&aq->lock);
659 EXPORT_SYMBOL(ap_queue_init_reply);
662 * ap_queue_message(): Queue a request to an AP device.
663 * @aq: The AP device to queue the message to
664 * @ap_msg: The message that is to be added
666 void ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg)
668 /* For asynchronous message handling a valid receive-callback
669 * is required.
671 BUG_ON(!ap_msg->receive);
673 spin_lock_bh(&aq->lock);
674 /* Queue the message. */
675 list_add_tail(&ap_msg->list, &aq->requestq);
676 aq->requestq_count++;
677 aq->total_request_count++;
678 atomic_inc(&aq->card->total_request_count);
679 /* Send/receive as many request from the queue as possible. */
680 ap_wait(ap_sm_event_loop(aq, AP_EVENT_POLL));
681 spin_unlock_bh(&aq->lock);
683 EXPORT_SYMBOL(ap_queue_message);
686 * ap_cancel_message(): Cancel a crypto request.
687 * @aq: The AP device that has the message queued
688 * @ap_msg: The message that is to be removed
690 * Cancel a crypto request. This is done by removing the request
691 * from the device pending or request queue. Note that the
692 * request stays on the AP queue. When it finishes the message
693 * reply will be discarded because the psmid can't be found.
695 void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg)
697 struct ap_message *tmp;
699 spin_lock_bh(&aq->lock);
700 if (!list_empty(&ap_msg->list)) {
701 list_for_each_entry(tmp, &aq->pendingq, list)
702 if (tmp->psmid == ap_msg->psmid) {
703 aq->pendingq_count--;
704 goto found;
706 aq->requestq_count--;
707 found:
708 list_del_init(&ap_msg->list);
710 spin_unlock_bh(&aq->lock);
712 EXPORT_SYMBOL(ap_cancel_message);
715 * __ap_flush_queue(): Flush requests.
716 * @aq: Pointer to the AP queue
718 * Flush all requests from the request/pending queue of an AP device.
720 static void __ap_flush_queue(struct ap_queue *aq)
722 struct ap_message *ap_msg, *next;
724 list_for_each_entry_safe(ap_msg, next, &aq->pendingq, list) {
725 list_del_init(&ap_msg->list);
726 aq->pendingq_count--;
727 ap_msg->rc = -EAGAIN;
728 ap_msg->receive(aq, ap_msg, NULL);
730 list_for_each_entry_safe(ap_msg, next, &aq->requestq, list) {
731 list_del_init(&ap_msg->list);
732 aq->requestq_count--;
733 ap_msg->rc = -EAGAIN;
734 ap_msg->receive(aq, ap_msg, NULL);
736 aq->queue_count = 0;
739 void ap_flush_queue(struct ap_queue *aq)
741 spin_lock_bh(&aq->lock);
742 __ap_flush_queue(aq);
743 spin_unlock_bh(&aq->lock);
745 EXPORT_SYMBOL(ap_flush_queue);
747 void ap_queue_prepare_remove(struct ap_queue *aq)
749 spin_lock_bh(&aq->lock);
750 /* flush queue */
751 __ap_flush_queue(aq);
752 /* set REMOVE state to prevent new messages are queued in */
753 aq->state = AP_STATE_REMOVE;
754 spin_unlock_bh(&aq->lock);
755 del_timer_sync(&aq->timeout);
758 void ap_queue_remove(struct ap_queue *aq)
761 * all messages have been flushed and the state is
762 * AP_STATE_REMOVE. Now reset with zero which also
763 * clears the irq registration and move the state
764 * to AP_STATE_UNBOUND to signal that this queue
765 * is not used by any driver currently.
767 spin_lock_bh(&aq->lock);
768 ap_zapq(aq->qid);
769 aq->state = AP_STATE_UNBOUND;
770 spin_unlock_bh(&aq->lock);
773 void ap_queue_reinit_state(struct ap_queue *aq)
775 spin_lock_bh(&aq->lock);
776 aq->state = AP_STATE_RESET_START;
777 ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
778 spin_unlock_bh(&aq->lock);