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