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