xref: /openbmc/linux/drivers/s390/crypto/ap_queue.c (revision f8e17c17)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright IBM Corp. 2016
4  * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
5  *
6  * Adjunct processor bus, queue related code.
7  */
8 
9 #define KMSG_COMPONENT "ap"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11 
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <asm/facility.h>
15 
16 #include "ap_bus.h"
17 #include "ap_debug.h"
18 
19 static void __ap_flush_queue(struct ap_queue *aq);
20 
21 /**
22  * ap_queue_enable_interruption(): Enable interruption on an AP queue.
23  * @qid: The AP queue number
24  * @ind: the notification indicator byte
25  *
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().
29  */
30 static int ap_queue_enable_interruption(struct ap_queue *aq, void *ind)
31 {
32 	struct ap_queue_status status;
33 	struct ap_qirq_ctrl qirqctrl = { 0 };
34 
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;
54 	}
55 }
56 
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
64  *
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.
69  */
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)
73 {
74 	if (special == 1)
75 		qid |= 0x400000UL;
76 	return ap_nqap(qid, psmid, msg, length);
77 }
78 
79 int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
80 {
81 	struct ap_queue_status status;
82 
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;
94 	}
95 }
96 EXPORT_SYMBOL(ap_send);
97 
98 int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
99 {
100 	struct ap_queue_status status;
101 
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;
116 	}
117 }
118 EXPORT_SYMBOL(ap_recv);
119 
120 /* State machine definitions and helpers */
121 
122 static enum ap_wait ap_sm_nop(struct ap_queue *aq)
123 {
124 	return AP_WAIT_NONE;
125 }
126 
127 /**
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
131  *
132  * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
133  */
134 static struct ap_queue_status ap_sm_recv(struct ap_queue *aq)
135 {
136 	struct ap_queue_status status;
137 	struct ap_message *ap_msg;
138 
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;
154 		}
155 		/* fall through */
156 	case AP_RESPONSE_NO_PENDING_REPLY:
157 		if (!status.queue_empty || aq->queue_count <= 0)
158 			break;
159 		/* The card shouldn't forget requests but who knows. */
160 		aq->queue_count = 0;
161 		list_splice_init(&aq->pendingq, &aq->requestq);
162 		aq->requestq_count += aq->pendingq_count;
163 		aq->pendingq_count = 0;
164 		break;
165 	default:
166 		break;
167 	}
168 	return status;
169 }
170 
171 /**
172  * ap_sm_read(): Receive pending reply messages from an AP queue.
173  * @aq: pointer to the AP queue
174  *
175  * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
176  */
177 static enum ap_wait ap_sm_read(struct ap_queue *aq)
178 {
179 	struct ap_queue_status status;
180 
181 	if (!aq->reply)
182 		return AP_WAIT_NONE;
183 	status = ap_sm_recv(aq);
184 	switch (status.response_code) {
185 	case AP_RESPONSE_NORMAL:
186 		if (aq->queue_count > 0) {
187 			aq->state = AP_STATE_WORKING;
188 			return AP_WAIT_AGAIN;
189 		}
190 		aq->state = AP_STATE_IDLE;
191 		return AP_WAIT_NONE;
192 	case AP_RESPONSE_NO_PENDING_REPLY:
193 		if (aq->queue_count > 0)
194 			return AP_WAIT_INTERRUPT;
195 		aq->state = AP_STATE_IDLE;
196 		return AP_WAIT_NONE;
197 	default:
198 		aq->state = AP_STATE_BORKED;
199 		return AP_WAIT_NONE;
200 	}
201 }
202 
203 /**
204  * ap_sm_suspend_read(): Receive pending reply messages from an AP queue
205  * without changing the device state in between. In suspend mode we don't
206  * allow sending new requests, therefore just fetch pending replies.
207  * @aq: pointer to the AP queue
208  *
209  * Returns AP_WAIT_NONE or AP_WAIT_AGAIN
210  */
211 static enum ap_wait ap_sm_suspend_read(struct ap_queue *aq)
212 {
213 	struct ap_queue_status status;
214 
215 	if (!aq->reply)
216 		return AP_WAIT_NONE;
217 	status = ap_sm_recv(aq);
218 	switch (status.response_code) {
219 	case AP_RESPONSE_NORMAL:
220 		if (aq->queue_count > 0)
221 			return AP_WAIT_AGAIN;
222 		/* fall through */
223 	default:
224 		return AP_WAIT_NONE;
225 	}
226 }
227 
228 /**
229  * ap_sm_write(): Send messages from the request queue to an AP queue.
230  * @aq: pointer to the AP queue
231  *
232  * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
233  */
234 static enum ap_wait ap_sm_write(struct ap_queue *aq)
235 {
236 	struct ap_queue_status status;
237 	struct ap_message *ap_msg;
238 
239 	if (aq->requestq_count <= 0)
240 		return AP_WAIT_NONE;
241 	/* Start the next request on the queue. */
242 	ap_msg = list_entry(aq->requestq.next, struct ap_message, list);
243 	status = __ap_send(aq->qid, ap_msg->psmid,
244 			   ap_msg->message, ap_msg->length, ap_msg->special);
245 	switch (status.response_code) {
246 	case AP_RESPONSE_NORMAL:
247 		aq->queue_count++;
248 		if (aq->queue_count == 1)
249 			mod_timer(&aq->timeout, jiffies + aq->request_timeout);
250 		list_move_tail(&ap_msg->list, &aq->pendingq);
251 		aq->requestq_count--;
252 		aq->pendingq_count++;
253 		if (aq->queue_count < aq->card->queue_depth) {
254 			aq->state = AP_STATE_WORKING;
255 			return AP_WAIT_AGAIN;
256 		}
257 		/* fall through */
258 	case AP_RESPONSE_Q_FULL:
259 		aq->state = AP_STATE_QUEUE_FULL;
260 		return AP_WAIT_INTERRUPT;
261 	case AP_RESPONSE_RESET_IN_PROGRESS:
262 		aq->state = AP_STATE_RESET_WAIT;
263 		return AP_WAIT_TIMEOUT;
264 	case AP_RESPONSE_MESSAGE_TOO_BIG:
265 	case AP_RESPONSE_REQ_FAC_NOT_INST:
266 		list_del_init(&ap_msg->list);
267 		aq->requestq_count--;
268 		ap_msg->rc = -EINVAL;
269 		ap_msg->receive(aq, ap_msg, NULL);
270 		return AP_WAIT_AGAIN;
271 	default:
272 		aq->state = AP_STATE_BORKED;
273 		return AP_WAIT_NONE;
274 	}
275 }
276 
277 /**
278  * ap_sm_read_write(): Send and receive messages to/from an AP queue.
279  * @aq: pointer to the AP queue
280  *
281  * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
282  */
283 static enum ap_wait ap_sm_read_write(struct ap_queue *aq)
284 {
285 	return min(ap_sm_read(aq), ap_sm_write(aq));
286 }
287 
288 /**
289  * ap_sm_reset(): Reset an AP queue.
290  * @qid: The AP queue number
291  *
292  * Submit the Reset command to an AP queue.
293  */
294 static enum ap_wait ap_sm_reset(struct ap_queue *aq)
295 {
296 	struct ap_queue_status status;
297 
298 	status = ap_rapq(aq->qid);
299 	switch (status.response_code) {
300 	case AP_RESPONSE_NORMAL:
301 	case AP_RESPONSE_RESET_IN_PROGRESS:
302 		aq->state = AP_STATE_RESET_WAIT;
303 		aq->interrupt = AP_INTR_DISABLED;
304 		return AP_WAIT_TIMEOUT;
305 	case AP_RESPONSE_BUSY:
306 		return AP_WAIT_TIMEOUT;
307 	case AP_RESPONSE_Q_NOT_AVAIL:
308 	case AP_RESPONSE_DECONFIGURED:
309 	case AP_RESPONSE_CHECKSTOPPED:
310 	default:
311 		aq->state = AP_STATE_BORKED;
312 		return AP_WAIT_NONE;
313 	}
314 }
315 
316 /**
317  * ap_sm_reset_wait(): Test queue for completion of the reset operation
318  * @aq: pointer to the AP queue
319  *
320  * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
321  */
322 static enum ap_wait ap_sm_reset_wait(struct ap_queue *aq)
323 {
324 	struct ap_queue_status status;
325 	void *lsi_ptr;
326 
327 	if (aq->queue_count > 0 && aq->reply)
328 		/* Try to read a completed message and get the status */
329 		status = ap_sm_recv(aq);
330 	else
331 		/* Get the status with TAPQ */
332 		status = ap_tapq(aq->qid, NULL);
333 
334 	switch (status.response_code) {
335 	case AP_RESPONSE_NORMAL:
336 		lsi_ptr = ap_airq_ptr();
337 		if (lsi_ptr && ap_queue_enable_interruption(aq, lsi_ptr) == 0)
338 			aq->state = AP_STATE_SETIRQ_WAIT;
339 		else
340 			aq->state = (aq->queue_count > 0) ?
341 				AP_STATE_WORKING : AP_STATE_IDLE;
342 		return AP_WAIT_AGAIN;
343 	case AP_RESPONSE_BUSY:
344 	case AP_RESPONSE_RESET_IN_PROGRESS:
345 		return AP_WAIT_TIMEOUT;
346 	case AP_RESPONSE_Q_NOT_AVAIL:
347 	case AP_RESPONSE_DECONFIGURED:
348 	case AP_RESPONSE_CHECKSTOPPED:
349 	default:
350 		aq->state = AP_STATE_BORKED;
351 		return AP_WAIT_NONE;
352 	}
353 }
354 
355 /**
356  * ap_sm_setirq_wait(): Test queue for completion of the irq enablement
357  * @aq: pointer to the AP queue
358  *
359  * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
360  */
361 static enum ap_wait ap_sm_setirq_wait(struct ap_queue *aq)
362 {
363 	struct ap_queue_status status;
364 
365 	if (aq->queue_count > 0 && aq->reply)
366 		/* Try to read a completed message and get the status */
367 		status = ap_sm_recv(aq);
368 	else
369 		/* Get the status with TAPQ */
370 		status = ap_tapq(aq->qid, NULL);
371 
372 	if (status.irq_enabled == 1) {
373 		/* Irqs are now enabled */
374 		aq->interrupt = AP_INTR_ENABLED;
375 		aq->state = (aq->queue_count > 0) ?
376 			AP_STATE_WORKING : AP_STATE_IDLE;
377 	}
378 
379 	switch (status.response_code) {
380 	case AP_RESPONSE_NORMAL:
381 		if (aq->queue_count > 0)
382 			return AP_WAIT_AGAIN;
383 		/* fallthrough */
384 	case AP_RESPONSE_NO_PENDING_REPLY:
385 		return AP_WAIT_TIMEOUT;
386 	default:
387 		aq->state = AP_STATE_BORKED;
388 		return AP_WAIT_NONE;
389 	}
390 }
391 
392 /*
393  * AP state machine jump table
394  */
395 static ap_func_t *ap_jumptable[NR_AP_STATES][NR_AP_EVENTS] = {
396 	[AP_STATE_RESET_START] = {
397 		[AP_EVENT_POLL] = ap_sm_reset,
398 		[AP_EVENT_TIMEOUT] = ap_sm_nop,
399 	},
400 	[AP_STATE_RESET_WAIT] = {
401 		[AP_EVENT_POLL] = ap_sm_reset_wait,
402 		[AP_EVENT_TIMEOUT] = ap_sm_nop,
403 	},
404 	[AP_STATE_SETIRQ_WAIT] = {
405 		[AP_EVENT_POLL] = ap_sm_setirq_wait,
406 		[AP_EVENT_TIMEOUT] = ap_sm_nop,
407 	},
408 	[AP_STATE_IDLE] = {
409 		[AP_EVENT_POLL] = ap_sm_write,
410 		[AP_EVENT_TIMEOUT] = ap_sm_nop,
411 	},
412 	[AP_STATE_WORKING] = {
413 		[AP_EVENT_POLL] = ap_sm_read_write,
414 		[AP_EVENT_TIMEOUT] = ap_sm_reset,
415 	},
416 	[AP_STATE_QUEUE_FULL] = {
417 		[AP_EVENT_POLL] = ap_sm_read,
418 		[AP_EVENT_TIMEOUT] = ap_sm_reset,
419 	},
420 	[AP_STATE_SUSPEND_WAIT] = {
421 		[AP_EVENT_POLL] = ap_sm_suspend_read,
422 		[AP_EVENT_TIMEOUT] = ap_sm_nop,
423 	},
424 	[AP_STATE_REMOVE] = {
425 		[AP_EVENT_POLL] = ap_sm_nop,
426 		[AP_EVENT_TIMEOUT] = ap_sm_nop,
427 	},
428 	[AP_STATE_UNBOUND] = {
429 		[AP_EVENT_POLL] = ap_sm_nop,
430 		[AP_EVENT_TIMEOUT] = ap_sm_nop,
431 	},
432 	[AP_STATE_BORKED] = {
433 		[AP_EVENT_POLL] = ap_sm_nop,
434 		[AP_EVENT_TIMEOUT] = ap_sm_nop,
435 	},
436 };
437 
438 enum ap_wait ap_sm_event(struct ap_queue *aq, enum ap_event event)
439 {
440 	return ap_jumptable[aq->state][event](aq);
441 }
442 
443 enum ap_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_event event)
444 {
445 	enum ap_wait wait;
446 
447 	while ((wait = ap_sm_event(aq, event)) == AP_WAIT_AGAIN)
448 		;
449 	return wait;
450 }
451 
452 /*
453  * Power management for queue devices
454  */
455 void ap_queue_suspend(struct ap_device *ap_dev)
456 {
457 	struct ap_queue *aq = to_ap_queue(&ap_dev->device);
458 
459 	/* Poll on the device until all requests are finished. */
460 	spin_lock_bh(&aq->lock);
461 	aq->state = AP_STATE_SUSPEND_WAIT;
462 	while (ap_sm_event(aq, AP_EVENT_POLL) != AP_WAIT_NONE)
463 		;
464 	aq->state = AP_STATE_BORKED;
465 	spin_unlock_bh(&aq->lock);
466 }
467 EXPORT_SYMBOL(ap_queue_suspend);
468 
469 void ap_queue_resume(struct ap_device *ap_dev)
470 {
471 }
472 EXPORT_SYMBOL(ap_queue_resume);
473 
474 /*
475  * AP queue related attributes.
476  */
477 static ssize_t request_count_show(struct device *dev,
478 				  struct device_attribute *attr,
479 				  char *buf)
480 {
481 	struct ap_queue *aq = to_ap_queue(dev);
482 	u64 req_cnt;
483 
484 	spin_lock_bh(&aq->lock);
485 	req_cnt = aq->total_request_count;
486 	spin_unlock_bh(&aq->lock);
487 	return snprintf(buf, PAGE_SIZE, "%llu\n", req_cnt);
488 }
489 
490 static ssize_t request_count_store(struct device *dev,
491 				   struct device_attribute *attr,
492 				   const char *buf, size_t count)
493 {
494 	struct ap_queue *aq = to_ap_queue(dev);
495 
496 	spin_lock_bh(&aq->lock);
497 	aq->total_request_count = 0;
498 	spin_unlock_bh(&aq->lock);
499 
500 	return count;
501 }
502 
503 static DEVICE_ATTR_RW(request_count);
504 
505 static ssize_t requestq_count_show(struct device *dev,
506 				   struct device_attribute *attr, char *buf)
507 {
508 	struct ap_queue *aq = to_ap_queue(dev);
509 	unsigned int reqq_cnt = 0;
510 
511 	spin_lock_bh(&aq->lock);
512 	reqq_cnt = aq->requestq_count;
513 	spin_unlock_bh(&aq->lock);
514 	return snprintf(buf, PAGE_SIZE, "%d\n", reqq_cnt);
515 }
516 
517 static DEVICE_ATTR_RO(requestq_count);
518 
519 static ssize_t pendingq_count_show(struct device *dev,
520 				   struct device_attribute *attr, char *buf)
521 {
522 	struct ap_queue *aq = to_ap_queue(dev);
523 	unsigned int penq_cnt = 0;
524 
525 	spin_lock_bh(&aq->lock);
526 	penq_cnt = aq->pendingq_count;
527 	spin_unlock_bh(&aq->lock);
528 	return snprintf(buf, PAGE_SIZE, "%d\n", penq_cnt);
529 }
530 
531 static DEVICE_ATTR_RO(pendingq_count);
532 
533 static ssize_t reset_show(struct device *dev,
534 			  struct device_attribute *attr, char *buf)
535 {
536 	struct ap_queue *aq = to_ap_queue(dev);
537 	int rc = 0;
538 
539 	spin_lock_bh(&aq->lock);
540 	switch (aq->state) {
541 	case AP_STATE_RESET_START:
542 	case AP_STATE_RESET_WAIT:
543 		rc = snprintf(buf, PAGE_SIZE, "Reset in progress.\n");
544 		break;
545 	case AP_STATE_WORKING:
546 	case AP_STATE_QUEUE_FULL:
547 		rc = snprintf(buf, PAGE_SIZE, "Reset Timer armed.\n");
548 		break;
549 	default:
550 		rc = snprintf(buf, PAGE_SIZE, "No Reset Timer set.\n");
551 	}
552 	spin_unlock_bh(&aq->lock);
553 	return rc;
554 }
555 
556 static ssize_t reset_store(struct device *dev,
557 			   struct device_attribute *attr,
558 			   const char *buf, size_t count)
559 {
560 	struct ap_queue *aq = to_ap_queue(dev);
561 
562 	spin_lock_bh(&aq->lock);
563 	__ap_flush_queue(aq);
564 	aq->state = AP_STATE_RESET_START;
565 	ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
566 	spin_unlock_bh(&aq->lock);
567 
568 	AP_DBF(DBF_INFO, "reset queue=%02x.%04x triggered by user\n",
569 	       AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
570 
571 	return count;
572 }
573 
574 static DEVICE_ATTR_RW(reset);
575 
576 static ssize_t interrupt_show(struct device *dev,
577 			      struct device_attribute *attr, char *buf)
578 {
579 	struct ap_queue *aq = to_ap_queue(dev);
580 	int rc = 0;
581 
582 	spin_lock_bh(&aq->lock);
583 	if (aq->state == AP_STATE_SETIRQ_WAIT)
584 		rc = snprintf(buf, PAGE_SIZE, "Enable Interrupt pending.\n");
585 	else if (aq->interrupt == AP_INTR_ENABLED)
586 		rc = snprintf(buf, PAGE_SIZE, "Interrupts enabled.\n");
587 	else
588 		rc = snprintf(buf, PAGE_SIZE, "Interrupts disabled.\n");
589 	spin_unlock_bh(&aq->lock);
590 	return rc;
591 }
592 
593 static DEVICE_ATTR_RO(interrupt);
594 
595 static struct attribute *ap_queue_dev_attrs[] = {
596 	&dev_attr_request_count.attr,
597 	&dev_attr_requestq_count.attr,
598 	&dev_attr_pendingq_count.attr,
599 	&dev_attr_reset.attr,
600 	&dev_attr_interrupt.attr,
601 	NULL
602 };
603 
604 static struct attribute_group ap_queue_dev_attr_group = {
605 	.attrs = ap_queue_dev_attrs
606 };
607 
608 static const struct attribute_group *ap_queue_dev_attr_groups[] = {
609 	&ap_queue_dev_attr_group,
610 	NULL
611 };
612 
613 static struct device_type ap_queue_type = {
614 	.name = "ap_queue",
615 	.groups = ap_queue_dev_attr_groups,
616 };
617 
618 static void ap_queue_device_release(struct device *dev)
619 {
620 	struct ap_queue *aq = to_ap_queue(dev);
621 
622 	if (!list_empty(&aq->list)) {
623 		spin_lock_bh(&ap_list_lock);
624 		list_del_init(&aq->list);
625 		spin_unlock_bh(&ap_list_lock);
626 	}
627 	kfree(aq);
628 }
629 
630 struct ap_queue *ap_queue_create(ap_qid_t qid, int device_type)
631 {
632 	struct ap_queue *aq;
633 
634 	aq = kzalloc(sizeof(*aq), GFP_KERNEL);
635 	if (!aq)
636 		return NULL;
637 	aq->ap_dev.device.release = ap_queue_device_release;
638 	aq->ap_dev.device.type = &ap_queue_type;
639 	aq->ap_dev.device_type = device_type;
640 	aq->qid = qid;
641 	aq->state = AP_STATE_UNBOUND;
642 	aq->interrupt = AP_INTR_DISABLED;
643 	spin_lock_init(&aq->lock);
644 	INIT_LIST_HEAD(&aq->list);
645 	INIT_LIST_HEAD(&aq->pendingq);
646 	INIT_LIST_HEAD(&aq->requestq);
647 	timer_setup(&aq->timeout, ap_request_timeout, 0);
648 
649 	return aq;
650 }
651 
652 void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *reply)
653 {
654 	aq->reply = reply;
655 
656 	spin_lock_bh(&aq->lock);
657 	ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
658 	spin_unlock_bh(&aq->lock);
659 }
660 EXPORT_SYMBOL(ap_queue_init_reply);
661 
662 /**
663  * ap_queue_message(): Queue a request to an AP device.
664  * @aq: The AP device to queue the message to
665  * @ap_msg: The message that is to be added
666  */
667 void ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg)
668 {
669 	/* For asynchronous message handling a valid receive-callback
670 	 * is required.
671 	 */
672 	BUG_ON(!ap_msg->receive);
673 
674 	spin_lock_bh(&aq->lock);
675 	/* Queue the message. */
676 	list_add_tail(&ap_msg->list, &aq->requestq);
677 	aq->requestq_count++;
678 	aq->total_request_count++;
679 	atomic64_inc(&aq->card->total_request_count);
680 	/* Send/receive as many request from the queue as possible. */
681 	ap_wait(ap_sm_event_loop(aq, AP_EVENT_POLL));
682 	spin_unlock_bh(&aq->lock);
683 }
684 EXPORT_SYMBOL(ap_queue_message);
685 
686 /**
687  * ap_cancel_message(): Cancel a crypto request.
688  * @aq: The AP device that has the message queued
689  * @ap_msg: The message that is to be removed
690  *
691  * Cancel a crypto request. This is done by removing the request
692  * from the device pending or request queue. Note that the
693  * request stays on the AP queue. When it finishes the message
694  * reply will be discarded because the psmid can't be found.
695  */
696 void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg)
697 {
698 	struct ap_message *tmp;
699 
700 	spin_lock_bh(&aq->lock);
701 	if (!list_empty(&ap_msg->list)) {
702 		list_for_each_entry(tmp, &aq->pendingq, list)
703 			if (tmp->psmid == ap_msg->psmid) {
704 				aq->pendingq_count--;
705 				goto found;
706 			}
707 		aq->requestq_count--;
708 found:
709 		list_del_init(&ap_msg->list);
710 	}
711 	spin_unlock_bh(&aq->lock);
712 }
713 EXPORT_SYMBOL(ap_cancel_message);
714 
715 /**
716  * __ap_flush_queue(): Flush requests.
717  * @aq: Pointer to the AP queue
718  *
719  * Flush all requests from the request/pending queue of an AP device.
720  */
721 static void __ap_flush_queue(struct ap_queue *aq)
722 {
723 	struct ap_message *ap_msg, *next;
724 
725 	list_for_each_entry_safe(ap_msg, next, &aq->pendingq, list) {
726 		list_del_init(&ap_msg->list);
727 		aq->pendingq_count--;
728 		ap_msg->rc = -EAGAIN;
729 		ap_msg->receive(aq, ap_msg, NULL);
730 	}
731 	list_for_each_entry_safe(ap_msg, next, &aq->requestq, list) {
732 		list_del_init(&ap_msg->list);
733 		aq->requestq_count--;
734 		ap_msg->rc = -EAGAIN;
735 		ap_msg->receive(aq, ap_msg, NULL);
736 	}
737 	aq->queue_count = 0;
738 }
739 
740 void ap_flush_queue(struct ap_queue *aq)
741 {
742 	spin_lock_bh(&aq->lock);
743 	__ap_flush_queue(aq);
744 	spin_unlock_bh(&aq->lock);
745 }
746 EXPORT_SYMBOL(ap_flush_queue);
747 
748 void ap_queue_prepare_remove(struct ap_queue *aq)
749 {
750 	spin_lock_bh(&aq->lock);
751 	/* flush queue */
752 	__ap_flush_queue(aq);
753 	/* set REMOVE state to prevent new messages are queued in */
754 	aq->state = AP_STATE_REMOVE;
755 	spin_unlock_bh(&aq->lock);
756 	del_timer_sync(&aq->timeout);
757 }
758 
759 void ap_queue_remove(struct ap_queue *aq)
760 {
761 	/*
762 	 * all messages have been flushed and the state is
763 	 * AP_STATE_REMOVE. Now reset with zero which also
764 	 * clears the irq registration and move the state
765 	 * to AP_STATE_UNBOUND to signal that this queue
766 	 * is not used by any driver currently.
767 	 */
768 	spin_lock_bh(&aq->lock);
769 	ap_zapq(aq->qid);
770 	aq->state = AP_STATE_UNBOUND;
771 	spin_unlock_bh(&aq->lock);
772 }
773 
774 void ap_queue_init_state(struct ap_queue *aq)
775 {
776 	spin_lock_bh(&aq->lock);
777 	aq->state = AP_STATE_RESET_START;
778 	ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
779 	spin_unlock_bh(&aq->lock);
780 }
781 EXPORT_SYMBOL(ap_queue_init_state);
782