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