xref: /openbmc/linux/drivers/s390/crypto/ap_queue.c (revision 4da722ca)
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_req_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 ssize_t ap_req_count_store(struct device *dev,
476 				  struct device_attribute *attr,
477 				  const char *buf, size_t count)
478 {
479 	struct ap_queue *aq = to_ap_queue(dev);
480 
481 	spin_lock_bh(&aq->lock);
482 	aq->total_request_count = 0;
483 	spin_unlock_bh(&aq->lock);
484 
485 	return count;
486 }
487 
488 static DEVICE_ATTR(request_count, 0644, ap_req_count_show, ap_req_count_store);
489 
490 static ssize_t ap_requestq_count_show(struct device *dev,
491 				      struct device_attribute *attr, char *buf)
492 {
493 	struct ap_queue *aq = to_ap_queue(dev);
494 	unsigned int reqq_cnt = 0;
495 
496 	spin_lock_bh(&aq->lock);
497 	reqq_cnt = aq->requestq_count;
498 	spin_unlock_bh(&aq->lock);
499 	return snprintf(buf, PAGE_SIZE, "%d\n", reqq_cnt);
500 }
501 
502 static DEVICE_ATTR(requestq_count, 0444, ap_requestq_count_show, NULL);
503 
504 static ssize_t ap_pendingq_count_show(struct device *dev,
505 				      struct device_attribute *attr, char *buf)
506 {
507 	struct ap_queue *aq = to_ap_queue(dev);
508 	unsigned int penq_cnt = 0;
509 
510 	spin_lock_bh(&aq->lock);
511 	penq_cnt = aq->pendingq_count;
512 	spin_unlock_bh(&aq->lock);
513 	return snprintf(buf, PAGE_SIZE, "%d\n", penq_cnt);
514 }
515 
516 static DEVICE_ATTR(pendingq_count, 0444, ap_pendingq_count_show, NULL);
517 
518 static ssize_t ap_reset_show(struct device *dev,
519 				      struct device_attribute *attr, char *buf)
520 {
521 	struct ap_queue *aq = to_ap_queue(dev);
522 	int rc = 0;
523 
524 	spin_lock_bh(&aq->lock);
525 	switch (aq->state) {
526 	case AP_STATE_RESET_START:
527 	case AP_STATE_RESET_WAIT:
528 		rc = snprintf(buf, PAGE_SIZE, "Reset in progress.\n");
529 		break;
530 	case AP_STATE_WORKING:
531 	case AP_STATE_QUEUE_FULL:
532 		rc = snprintf(buf, PAGE_SIZE, "Reset Timer armed.\n");
533 		break;
534 	default:
535 		rc = snprintf(buf, PAGE_SIZE, "No Reset Timer set.\n");
536 	}
537 	spin_unlock_bh(&aq->lock);
538 	return rc;
539 }
540 
541 static DEVICE_ATTR(reset, 0444, ap_reset_show, NULL);
542 
543 static ssize_t ap_interrupt_show(struct device *dev,
544 				 struct device_attribute *attr, char *buf)
545 {
546 	struct ap_queue *aq = to_ap_queue(dev);
547 	int rc = 0;
548 
549 	spin_lock_bh(&aq->lock);
550 	if (aq->state == AP_STATE_SETIRQ_WAIT)
551 		rc = snprintf(buf, PAGE_SIZE, "Enable Interrupt pending.\n");
552 	else if (aq->interrupt == AP_INTR_ENABLED)
553 		rc = snprintf(buf, PAGE_SIZE, "Interrupts enabled.\n");
554 	else
555 		rc = snprintf(buf, PAGE_SIZE, "Interrupts disabled.\n");
556 	spin_unlock_bh(&aq->lock);
557 	return rc;
558 }
559 
560 static DEVICE_ATTR(interrupt, 0444, ap_interrupt_show, NULL);
561 
562 static struct attribute *ap_queue_dev_attrs[] = {
563 	&dev_attr_request_count.attr,
564 	&dev_attr_requestq_count.attr,
565 	&dev_attr_pendingq_count.attr,
566 	&dev_attr_reset.attr,
567 	&dev_attr_interrupt.attr,
568 	NULL
569 };
570 
571 static struct attribute_group ap_queue_dev_attr_group = {
572 	.attrs = ap_queue_dev_attrs
573 };
574 
575 static const struct attribute_group *ap_queue_dev_attr_groups[] = {
576 	&ap_queue_dev_attr_group,
577 	NULL
578 };
579 
580 static struct device_type ap_queue_type = {
581 	.name = "ap_queue",
582 	.groups = ap_queue_dev_attr_groups,
583 };
584 
585 static void ap_queue_device_release(struct device *dev)
586 {
587 	struct ap_queue *aq = to_ap_queue(dev);
588 
589 	if (!list_empty(&aq->list)) {
590 		spin_lock_bh(&ap_list_lock);
591 		list_del_init(&aq->list);
592 		spin_unlock_bh(&ap_list_lock);
593 	}
594 	kfree(aq);
595 }
596 
597 struct ap_queue *ap_queue_create(ap_qid_t qid, int device_type)
598 {
599 	struct ap_queue *aq;
600 
601 	aq = kzalloc(sizeof(*aq), GFP_KERNEL);
602 	if (!aq)
603 		return NULL;
604 	aq->ap_dev.device.release = ap_queue_device_release;
605 	aq->ap_dev.device.type = &ap_queue_type;
606 	aq->ap_dev.device_type = device_type;
607 	/* CEX6 toleration: map to CEX5 */
608 	if (device_type == AP_DEVICE_TYPE_CEX6)
609 		aq->ap_dev.device_type = AP_DEVICE_TYPE_CEX5;
610 	aq->qid = qid;
611 	aq->state = AP_STATE_RESET_START;
612 	aq->interrupt = AP_INTR_DISABLED;
613 	spin_lock_init(&aq->lock);
614 	INIT_LIST_HEAD(&aq->pendingq);
615 	INIT_LIST_HEAD(&aq->requestq);
616 	setup_timer(&aq->timeout, ap_request_timeout, (unsigned long) aq);
617 
618 	return aq;
619 }
620 
621 void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *reply)
622 {
623 	aq->reply = reply;
624 
625 	spin_lock_bh(&aq->lock);
626 	ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
627 	spin_unlock_bh(&aq->lock);
628 }
629 EXPORT_SYMBOL(ap_queue_init_reply);
630 
631 /**
632  * ap_queue_message(): Queue a request to an AP device.
633  * @aq: The AP device to queue the message to
634  * @ap_msg: The message that is to be added
635  */
636 void ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg)
637 {
638 	/* For asynchronous message handling a valid receive-callback
639 	 * is required.
640 	 */
641 	BUG_ON(!ap_msg->receive);
642 
643 	spin_lock_bh(&aq->lock);
644 	/* Queue the message. */
645 	list_add_tail(&ap_msg->list, &aq->requestq);
646 	aq->requestq_count++;
647 	aq->total_request_count++;
648 	atomic_inc(&aq->card->total_request_count);
649 	/* Send/receive as many request from the queue as possible. */
650 	ap_wait(ap_sm_event_loop(aq, AP_EVENT_POLL));
651 	spin_unlock_bh(&aq->lock);
652 }
653 EXPORT_SYMBOL(ap_queue_message);
654 
655 /**
656  * ap_cancel_message(): Cancel a crypto request.
657  * @aq: The AP device that has the message queued
658  * @ap_msg: The message that is to be removed
659  *
660  * Cancel a crypto request. This is done by removing the request
661  * from the device pending or request queue. Note that the
662  * request stays on the AP queue. When it finishes the message
663  * reply will be discarded because the psmid can't be found.
664  */
665 void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg)
666 {
667 	struct ap_message *tmp;
668 
669 	spin_lock_bh(&aq->lock);
670 	if (!list_empty(&ap_msg->list)) {
671 		list_for_each_entry(tmp, &aq->pendingq, list)
672 			if (tmp->psmid == ap_msg->psmid) {
673 				aq->pendingq_count--;
674 				goto found;
675 			}
676 		aq->requestq_count--;
677 found:
678 		list_del_init(&ap_msg->list);
679 	}
680 	spin_unlock_bh(&aq->lock);
681 }
682 EXPORT_SYMBOL(ap_cancel_message);
683 
684 /**
685  * __ap_flush_queue(): Flush requests.
686  * @aq: Pointer to the AP queue
687  *
688  * Flush all requests from the request/pending queue of an AP device.
689  */
690 static void __ap_flush_queue(struct ap_queue *aq)
691 {
692 	struct ap_message *ap_msg, *next;
693 
694 	list_for_each_entry_safe(ap_msg, next, &aq->pendingq, list) {
695 		list_del_init(&ap_msg->list);
696 		aq->pendingq_count--;
697 		ap_msg->rc = -EAGAIN;
698 		ap_msg->receive(aq, ap_msg, NULL);
699 	}
700 	list_for_each_entry_safe(ap_msg, next, &aq->requestq, list) {
701 		list_del_init(&ap_msg->list);
702 		aq->requestq_count--;
703 		ap_msg->rc = -EAGAIN;
704 		ap_msg->receive(aq, ap_msg, NULL);
705 	}
706 }
707 
708 void ap_flush_queue(struct ap_queue *aq)
709 {
710 	spin_lock_bh(&aq->lock);
711 	__ap_flush_queue(aq);
712 	spin_unlock_bh(&aq->lock);
713 }
714 EXPORT_SYMBOL(ap_flush_queue);
715 
716 void ap_queue_remove(struct ap_queue *aq)
717 {
718 	ap_flush_queue(aq);
719 	del_timer_sync(&aq->timeout);
720 }
721 EXPORT_SYMBOL(ap_queue_remove);
722