1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2016
4 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
6 * Adjunct processor bus, queue related code.
9 #define KMSG_COMPONENT "ap"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <asm/facility.h>
19 static void __ap_flush_queue(struct ap_queue *aq);
22 * ap_queue_enable_interruption(): Enable interruption on an AP queue.
23 * @qid: The AP queue number
24 * @ind: the notification indicator byte
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().
30 static int ap_queue_enable_interruption(struct ap_queue *aq, void *ind)
32 struct ap_queue_status status;
33 struct ap_qirq_ctrl qirqctrl = { 0 };
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:
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",
48 AP_QID_QUEUE(aq->qid));
50 case AP_RESPONSE_RESET_IN_PROGRESS:
51 case AP_RESPONSE_BUSY:
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
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.
70 static inline struct ap_queue_status
71 __ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length,
76 return ap_nqap(qid, psmid, msg, length);
79 int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
81 struct ap_queue_status status;
83 status = __ap_send(qid, psmid, msg, length, 0);
84 switch (status.response_code) {
85 case AP_RESPONSE_NORMAL:
87 case AP_RESPONSE_Q_FULL:
88 case AP_RESPONSE_RESET_IN_PROGRESS:
90 case AP_RESPONSE_REQ_FAC_NOT_INST:
92 default: /* Device is gone. */
96 EXPORT_SYMBOL(ap_send);
98 int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
100 struct ap_queue_status status;
104 status = ap_dqap(qid, psmid, msg, length);
105 switch (status.response_code) {
106 case AP_RESPONSE_NORMAL:
108 case AP_RESPONSE_NO_PENDING_REPLY:
109 if (status.queue_empty)
112 case AP_RESPONSE_RESET_IN_PROGRESS:
118 EXPORT_SYMBOL(ap_recv);
120 /* State machine definitions and helpers */
122 static enum ap_wait ap_sm_nop(struct ap_queue *aq)
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
132 * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
134 static struct ap_queue_status ap_sm_recv(struct ap_queue *aq)
136 struct ap_queue_status status;
137 struct ap_message *ap_msg;
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:
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)
150 list_del_init(&ap_msg->list);
151 aq->pendingq_count--;
152 ap_msg->receive(aq, ap_msg, aq->reply);
155 case AP_RESPONSE_NO_PENDING_REPLY:
156 if (!status.queue_empty || aq->queue_count <= 0)
158 /* The card shouldn't forget requests but who knows. */
160 list_splice_init(&aq->pendingq, &aq->requestq);
161 aq->requestq_count += aq->pendingq_count;
162 aq->pendingq_count = 0;
171 * ap_sm_read(): Receive pending reply messages from an AP queue.
172 * @aq: pointer to the AP queue
174 * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
176 static enum ap_wait ap_sm_read(struct ap_queue *aq)
178 struct ap_queue_status status;
182 status = ap_sm_recv(aq);
183 switch (status.response_code) {
184 case AP_RESPONSE_NORMAL:
185 if (aq->queue_count > 0) {
186 aq->state = AP_STATE_WORKING;
187 return AP_WAIT_AGAIN;
189 aq->state = AP_STATE_IDLE;
191 case AP_RESPONSE_NO_PENDING_REPLY:
192 if (aq->queue_count > 0)
193 return AP_WAIT_INTERRUPT;
194 aq->state = AP_STATE_IDLE;
197 aq->state = AP_STATE_BORKED;
203 * ap_sm_suspend_read(): Receive pending reply messages from an AP queue
204 * without changing the device state in between. In suspend mode we don't
205 * allow sending new requests, therefore just fetch pending replies.
206 * @aq: pointer to the AP queue
208 * Returns AP_WAIT_NONE or AP_WAIT_AGAIN
210 static enum ap_wait ap_sm_suspend_read(struct ap_queue *aq)
212 struct ap_queue_status status;
216 status = ap_sm_recv(aq);
217 switch (status.response_code) {
218 case AP_RESPONSE_NORMAL:
219 if (aq->queue_count > 0)
220 return AP_WAIT_AGAIN;
228 * ap_sm_write(): Send messages from the request queue to an AP queue.
229 * @aq: pointer to the AP queue
231 * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
233 static enum ap_wait ap_sm_write(struct ap_queue *aq)
235 struct ap_queue_status status;
236 struct ap_message *ap_msg;
238 if (aq->requestq_count <= 0)
240 /* Start the next request on the queue. */
241 ap_msg = list_entry(aq->requestq.next, struct ap_message, list);
242 status = __ap_send(aq->qid, ap_msg->psmid,
243 ap_msg->message, ap_msg->length, ap_msg->special);
244 switch (status.response_code) {
245 case AP_RESPONSE_NORMAL:
247 if (aq->queue_count == 1)
248 mod_timer(&aq->timeout, jiffies + aq->request_timeout);
249 list_move_tail(&ap_msg->list, &aq->pendingq);
250 aq->requestq_count--;
251 aq->pendingq_count++;
252 if (aq->queue_count < aq->card->queue_depth) {
253 aq->state = AP_STATE_WORKING;
254 return AP_WAIT_AGAIN;
257 case AP_RESPONSE_Q_FULL:
258 aq->state = AP_STATE_QUEUE_FULL;
259 return AP_WAIT_INTERRUPT;
260 case AP_RESPONSE_RESET_IN_PROGRESS:
261 aq->state = AP_STATE_RESET_WAIT;
262 return AP_WAIT_TIMEOUT;
263 case AP_RESPONSE_MESSAGE_TOO_BIG:
264 case AP_RESPONSE_REQ_FAC_NOT_INST:
265 list_del_init(&ap_msg->list);
266 aq->requestq_count--;
267 ap_msg->rc = -EINVAL;
268 ap_msg->receive(aq, ap_msg, NULL);
269 return AP_WAIT_AGAIN;
271 aq->state = AP_STATE_BORKED;
277 * ap_sm_read_write(): Send and receive messages to/from an AP queue.
278 * @aq: pointer to the AP queue
280 * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
282 static enum ap_wait ap_sm_read_write(struct ap_queue *aq)
284 return min(ap_sm_read(aq), ap_sm_write(aq));
288 * ap_sm_reset(): Reset an AP queue.
289 * @qid: The AP queue number
291 * Submit the Reset command to an AP queue.
293 static enum ap_wait ap_sm_reset(struct ap_queue *aq)
295 struct ap_queue_status status;
297 status = ap_rapq(aq->qid);
298 switch (status.response_code) {
299 case AP_RESPONSE_NORMAL:
300 case AP_RESPONSE_RESET_IN_PROGRESS:
301 aq->state = AP_STATE_RESET_WAIT;
302 aq->interrupt = AP_INTR_DISABLED;
303 return AP_WAIT_TIMEOUT;
304 case AP_RESPONSE_BUSY:
305 return AP_WAIT_TIMEOUT;
306 case AP_RESPONSE_Q_NOT_AVAIL:
307 case AP_RESPONSE_DECONFIGURED:
308 case AP_RESPONSE_CHECKSTOPPED:
310 aq->state = AP_STATE_BORKED;
316 * ap_sm_reset_wait(): Test queue for completion of the reset operation
317 * @aq: pointer to the AP queue
319 * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
321 static enum ap_wait ap_sm_reset_wait(struct ap_queue *aq)
323 struct ap_queue_status status;
326 if (aq->queue_count > 0 && aq->reply)
327 /* Try to read a completed message and get the status */
328 status = ap_sm_recv(aq);
330 /* Get the status with TAPQ */
331 status = ap_tapq(aq->qid, NULL);
333 switch (status.response_code) {
334 case AP_RESPONSE_NORMAL:
335 lsi_ptr = ap_airq_ptr();
336 if (lsi_ptr && ap_queue_enable_interruption(aq, lsi_ptr) == 0)
337 aq->state = AP_STATE_SETIRQ_WAIT;
339 aq->state = (aq->queue_count > 0) ?
340 AP_STATE_WORKING : AP_STATE_IDLE;
341 return AP_WAIT_AGAIN;
342 case AP_RESPONSE_BUSY:
343 case AP_RESPONSE_RESET_IN_PROGRESS:
344 return AP_WAIT_TIMEOUT;
345 case AP_RESPONSE_Q_NOT_AVAIL:
346 case AP_RESPONSE_DECONFIGURED:
347 case AP_RESPONSE_CHECKSTOPPED:
349 aq->state = AP_STATE_BORKED;
355 * ap_sm_setirq_wait(): Test queue for completion of the irq enablement
356 * @aq: pointer to the AP queue
358 * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
360 static enum ap_wait ap_sm_setirq_wait(struct ap_queue *aq)
362 struct ap_queue_status status;
364 if (aq->queue_count > 0 && aq->reply)
365 /* Try to read a completed message and get the status */
366 status = ap_sm_recv(aq);
368 /* Get the status with TAPQ */
369 status = ap_tapq(aq->qid, NULL);
371 if (status.irq_enabled == 1) {
372 /* Irqs are now enabled */
373 aq->interrupt = AP_INTR_ENABLED;
374 aq->state = (aq->queue_count > 0) ?
375 AP_STATE_WORKING : AP_STATE_IDLE;
378 switch (status.response_code) {
379 case AP_RESPONSE_NORMAL:
380 if (aq->queue_count > 0)
381 return AP_WAIT_AGAIN;
383 case AP_RESPONSE_NO_PENDING_REPLY:
384 return AP_WAIT_TIMEOUT;
386 aq->state = AP_STATE_BORKED;
392 * AP state machine jump table
394 static ap_func_t *ap_jumptable[NR_AP_STATES][NR_AP_EVENTS] = {
395 [AP_STATE_RESET_START] = {
396 [AP_EVENT_POLL] = ap_sm_reset,
397 [AP_EVENT_TIMEOUT] = ap_sm_nop,
399 [AP_STATE_RESET_WAIT] = {
400 [AP_EVENT_POLL] = ap_sm_reset_wait,
401 [AP_EVENT_TIMEOUT] = ap_sm_nop,
403 [AP_STATE_SETIRQ_WAIT] = {
404 [AP_EVENT_POLL] = ap_sm_setirq_wait,
405 [AP_EVENT_TIMEOUT] = ap_sm_nop,
408 [AP_EVENT_POLL] = ap_sm_write,
409 [AP_EVENT_TIMEOUT] = ap_sm_nop,
411 [AP_STATE_WORKING] = {
412 [AP_EVENT_POLL] = ap_sm_read_write,
413 [AP_EVENT_TIMEOUT] = ap_sm_reset,
415 [AP_STATE_QUEUE_FULL] = {
416 [AP_EVENT_POLL] = ap_sm_read,
417 [AP_EVENT_TIMEOUT] = ap_sm_reset,
419 [AP_STATE_SUSPEND_WAIT] = {
420 [AP_EVENT_POLL] = ap_sm_suspend_read,
421 [AP_EVENT_TIMEOUT] = ap_sm_nop,
423 [AP_STATE_BORKED] = {
424 [AP_EVENT_POLL] = ap_sm_nop,
425 [AP_EVENT_TIMEOUT] = ap_sm_nop,
429 enum ap_wait ap_sm_event(struct ap_queue *aq, enum ap_event event)
431 return ap_jumptable[aq->state][event](aq);
434 enum ap_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_event event)
438 while ((wait = ap_sm_event(aq, event)) == AP_WAIT_AGAIN)
444 * Power management for queue devices
446 void ap_queue_suspend(struct ap_device *ap_dev)
448 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
450 /* Poll on the device until all requests are finished. */
451 spin_lock_bh(&aq->lock);
452 aq->state = AP_STATE_SUSPEND_WAIT;
453 while (ap_sm_event(aq, AP_EVENT_POLL) != AP_WAIT_NONE)
455 aq->state = AP_STATE_BORKED;
456 spin_unlock_bh(&aq->lock);
458 EXPORT_SYMBOL(ap_queue_suspend);
460 void ap_queue_resume(struct ap_device *ap_dev)
463 EXPORT_SYMBOL(ap_queue_resume);
466 * AP queue related attributes.
468 static ssize_t request_count_show(struct device *dev,
469 struct device_attribute *attr,
472 struct ap_queue *aq = to_ap_queue(dev);
473 unsigned int req_cnt;
475 spin_lock_bh(&aq->lock);
476 req_cnt = aq->total_request_count;
477 spin_unlock_bh(&aq->lock);
478 return snprintf(buf, PAGE_SIZE, "%d\n", req_cnt);
481 static ssize_t request_count_store(struct device *dev,
482 struct device_attribute *attr,
483 const char *buf, size_t count)
485 struct ap_queue *aq = to_ap_queue(dev);
487 spin_lock_bh(&aq->lock);
488 aq->total_request_count = 0;
489 spin_unlock_bh(&aq->lock);
494 static DEVICE_ATTR_RW(request_count);
496 static ssize_t requestq_count_show(struct device *dev,
497 struct device_attribute *attr, char *buf)
499 struct ap_queue *aq = to_ap_queue(dev);
500 unsigned int reqq_cnt = 0;
502 spin_lock_bh(&aq->lock);
503 reqq_cnt = aq->requestq_count;
504 spin_unlock_bh(&aq->lock);
505 return snprintf(buf, PAGE_SIZE, "%d\n", reqq_cnt);
508 static DEVICE_ATTR_RO(requestq_count);
510 static ssize_t pendingq_count_show(struct device *dev,
511 struct device_attribute *attr, char *buf)
513 struct ap_queue *aq = to_ap_queue(dev);
514 unsigned int penq_cnt = 0;
516 spin_lock_bh(&aq->lock);
517 penq_cnt = aq->pendingq_count;
518 spin_unlock_bh(&aq->lock);
519 return snprintf(buf, PAGE_SIZE, "%d\n", penq_cnt);
522 static DEVICE_ATTR_RO(pendingq_count);
524 static ssize_t reset_show(struct device *dev,
525 struct device_attribute *attr, char *buf)
527 struct ap_queue *aq = to_ap_queue(dev);
530 spin_lock_bh(&aq->lock);
532 case AP_STATE_RESET_START:
533 case AP_STATE_RESET_WAIT:
534 rc = snprintf(buf, PAGE_SIZE, "Reset in progress.\n");
536 case AP_STATE_WORKING:
537 case AP_STATE_QUEUE_FULL:
538 rc = snprintf(buf, PAGE_SIZE, "Reset Timer armed.\n");
541 rc = snprintf(buf, PAGE_SIZE, "No Reset Timer set.\n");
543 spin_unlock_bh(&aq->lock);
547 static ssize_t reset_store(struct device *dev,
548 struct device_attribute *attr,
549 const char *buf, size_t count)
551 struct ap_queue *aq = to_ap_queue(dev);
553 spin_lock_bh(&aq->lock);
554 __ap_flush_queue(aq);
555 aq->state = AP_STATE_RESET_START;
556 ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
557 spin_unlock_bh(&aq->lock);
559 AP_DBF(DBF_INFO, "reset queue=%02x.%04x triggered by user\n",
560 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
565 static DEVICE_ATTR_RW(reset);
567 static ssize_t interrupt_show(struct device *dev,
568 struct device_attribute *attr, char *buf)
570 struct ap_queue *aq = to_ap_queue(dev);
573 spin_lock_bh(&aq->lock);
574 if (aq->state == AP_STATE_SETIRQ_WAIT)
575 rc = snprintf(buf, PAGE_SIZE, "Enable Interrupt pending.\n");
576 else if (aq->interrupt == AP_INTR_ENABLED)
577 rc = snprintf(buf, PAGE_SIZE, "Interrupts enabled.\n");
579 rc = snprintf(buf, PAGE_SIZE, "Interrupts disabled.\n");
580 spin_unlock_bh(&aq->lock);
584 static DEVICE_ATTR_RO(interrupt);
586 static struct attribute *ap_queue_dev_attrs[] = {
587 &dev_attr_request_count.attr,
588 &dev_attr_requestq_count.attr,
589 &dev_attr_pendingq_count.attr,
590 &dev_attr_reset.attr,
591 &dev_attr_interrupt.attr,
595 static struct attribute_group ap_queue_dev_attr_group = {
596 .attrs = ap_queue_dev_attrs
599 static const struct attribute_group *ap_queue_dev_attr_groups[] = {
600 &ap_queue_dev_attr_group,
604 static struct device_type ap_queue_type = {
606 .groups = ap_queue_dev_attr_groups,
609 static void ap_queue_device_release(struct device *dev)
611 struct ap_queue *aq = to_ap_queue(dev);
613 if (!list_empty(&aq->list)) {
614 spin_lock_bh(&ap_list_lock);
615 list_del_init(&aq->list);
616 spin_unlock_bh(&ap_list_lock);
621 struct ap_queue *ap_queue_create(ap_qid_t qid, int device_type)
625 aq = kzalloc(sizeof(*aq), GFP_KERNEL);
628 aq->ap_dev.device.release = ap_queue_device_release;
629 aq->ap_dev.device.type = &ap_queue_type;
630 aq->ap_dev.device_type = device_type;
632 aq->state = AP_STATE_RESET_START;
633 aq->interrupt = AP_INTR_DISABLED;
634 spin_lock_init(&aq->lock);
635 INIT_LIST_HEAD(&aq->list);
636 INIT_LIST_HEAD(&aq->pendingq);
637 INIT_LIST_HEAD(&aq->requestq);
638 timer_setup(&aq->timeout, ap_request_timeout, 0);
643 void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *reply)
647 spin_lock_bh(&aq->lock);
648 ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
649 spin_unlock_bh(&aq->lock);
651 EXPORT_SYMBOL(ap_queue_init_reply);
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
658 void ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg)
660 /* For asynchronous message handling a valid receive-callback
663 BUG_ON(!ap_msg->receive);
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);
675 EXPORT_SYMBOL(ap_queue_message);
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
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.
687 void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg)
689 struct ap_message *tmp;
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--;
698 aq->requestq_count--;
700 list_del_init(&ap_msg->list);
702 spin_unlock_bh(&aq->lock);
704 EXPORT_SYMBOL(ap_cancel_message);
707 * __ap_flush_queue(): Flush requests.
708 * @aq: Pointer to the AP queue
710 * Flush all requests from the request/pending queue of an AP device.
712 static void __ap_flush_queue(struct ap_queue *aq)
714 struct ap_message *ap_msg, *next;
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);
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);
730 void ap_flush_queue(struct ap_queue *aq)
732 spin_lock_bh(&aq->lock);
733 __ap_flush_queue(aq);
734 spin_unlock_bh(&aq->lock);
736 EXPORT_SYMBOL(ap_flush_queue);
738 void ap_queue_remove(struct ap_queue *aq)
741 del_timer_sync(&aq->timeout);
743 /* reset with zero, also clears irq registration */
744 spin_lock_bh(&aq->lock);
746 aq->state = AP_STATE_BORKED;
747 spin_unlock_bh(&aq->lock);
749 EXPORT_SYMBOL(ap_queue_remove);
751 void ap_queue_reinit_state(struct ap_queue *aq)
753 spin_lock_bh(&aq->lock);
754 aq->state = AP_STATE_RESET_START;
755 ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
756 spin_unlock_bh(&aq->lock);
758 EXPORT_SYMBOL(ap_queue_reinit_state);