1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2016
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_sm_wait ap_sm_nop(struct ap_queue *aq)
124 return AP_SM_WAIT_NONE;
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_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_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->msg, aq->reply->len);
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);
156 case AP_RESPONSE_NO_PENDING_REPLY:
157 if (!status.queue_empty || aq->queue_count <= 0)
159 /* The card shouldn't forget requests but who knows. */
161 list_splice_init(&aq->pendingq, &aq->requestq);
162 aq->requestq_count += aq->pendingq_count;
163 aq->pendingq_count = 0;
172 * ap_sm_read(): Receive pending reply messages from an AP queue.
173 * @aq: pointer to the AP queue
175 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
177 static enum ap_sm_wait ap_sm_read(struct ap_queue *aq)
179 struct ap_queue_status status;
182 return AP_SM_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->sm_state = AP_SM_STATE_WORKING;
188 return AP_SM_WAIT_AGAIN;
190 aq->sm_state = AP_SM_STATE_IDLE;
191 return AP_SM_WAIT_NONE;
192 case AP_RESPONSE_NO_PENDING_REPLY:
193 if (aq->queue_count > 0)
194 return AP_SM_WAIT_INTERRUPT;
195 aq->sm_state = AP_SM_STATE_IDLE;
196 return AP_SM_WAIT_NONE;
198 aq->sm_state = AP_SM_STATE_BORKED;
199 return AP_SM_WAIT_NONE;
204 * ap_sm_write(): Send messages from the request queue to an AP queue.
205 * @aq: pointer to the AP queue
207 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
209 static enum ap_sm_wait ap_sm_write(struct ap_queue *aq)
211 struct ap_queue_status status;
212 struct ap_message *ap_msg;
214 if (aq->requestq_count <= 0)
215 return AP_SM_WAIT_NONE;
216 /* Start the next request on the queue. */
217 ap_msg = list_entry(aq->requestq.next, struct ap_message, list);
218 status = __ap_send(aq->qid, ap_msg->psmid,
219 ap_msg->msg, ap_msg->len,
220 ap_msg->flags & AP_MSG_FLAG_SPECIAL);
221 switch (status.response_code) {
222 case AP_RESPONSE_NORMAL:
224 if (aq->queue_count == 1)
225 mod_timer(&aq->timeout, jiffies + aq->request_timeout);
226 list_move_tail(&ap_msg->list, &aq->pendingq);
227 aq->requestq_count--;
228 aq->pendingq_count++;
229 if (aq->queue_count < aq->card->queue_depth) {
230 aq->sm_state = AP_SM_STATE_WORKING;
231 return AP_SM_WAIT_AGAIN;
234 case AP_RESPONSE_Q_FULL:
235 aq->sm_state = AP_SM_STATE_QUEUE_FULL;
236 return AP_SM_WAIT_INTERRUPT;
237 case AP_RESPONSE_RESET_IN_PROGRESS:
238 aq->sm_state = AP_SM_STATE_RESET_WAIT;
239 return AP_SM_WAIT_TIMEOUT;
240 case AP_RESPONSE_MESSAGE_TOO_BIG:
241 case AP_RESPONSE_REQ_FAC_NOT_INST:
242 list_del_init(&ap_msg->list);
243 aq->requestq_count--;
244 ap_msg->rc = -EINVAL;
245 ap_msg->receive(aq, ap_msg, NULL);
246 return AP_SM_WAIT_AGAIN;
248 aq->sm_state = AP_SM_STATE_BORKED;
249 return AP_SM_WAIT_NONE;
254 * ap_sm_read_write(): Send and receive messages to/from an AP queue.
255 * @aq: pointer to the AP queue
257 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
259 static enum ap_sm_wait ap_sm_read_write(struct ap_queue *aq)
261 return min(ap_sm_read(aq), ap_sm_write(aq));
265 * ap_sm_reset(): Reset an AP queue.
266 * @qid: The AP queue number
268 * Submit the Reset command to an AP queue.
270 static enum ap_sm_wait ap_sm_reset(struct ap_queue *aq)
272 struct ap_queue_status status;
274 status = ap_rapq(aq->qid);
275 switch (status.response_code) {
276 case AP_RESPONSE_NORMAL:
277 case AP_RESPONSE_RESET_IN_PROGRESS:
278 aq->sm_state = AP_SM_STATE_RESET_WAIT;
279 aq->interrupt = AP_INTR_DISABLED;
280 return AP_SM_WAIT_TIMEOUT;
281 case AP_RESPONSE_BUSY:
282 return AP_SM_WAIT_TIMEOUT;
283 case AP_RESPONSE_Q_NOT_AVAIL:
284 case AP_RESPONSE_DECONFIGURED:
285 case AP_RESPONSE_CHECKSTOPPED:
287 aq->sm_state = AP_SM_STATE_BORKED;
288 return AP_SM_WAIT_NONE;
293 * ap_sm_reset_wait(): Test queue for completion of the reset operation
294 * @aq: pointer to the AP queue
296 * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
298 static enum ap_sm_wait ap_sm_reset_wait(struct ap_queue *aq)
300 struct ap_queue_status status;
303 if (aq->queue_count > 0 && aq->reply)
304 /* Try to read a completed message and get the status */
305 status = ap_sm_recv(aq);
307 /* Get the status with TAPQ */
308 status = ap_tapq(aq->qid, NULL);
310 switch (status.response_code) {
311 case AP_RESPONSE_NORMAL:
312 lsi_ptr = ap_airq_ptr();
313 if (lsi_ptr && ap_queue_enable_interruption(aq, lsi_ptr) == 0)
314 aq->sm_state = AP_SM_STATE_SETIRQ_WAIT;
316 aq->sm_state = (aq->queue_count > 0) ?
317 AP_SM_STATE_WORKING : AP_SM_STATE_IDLE;
318 return AP_SM_WAIT_AGAIN;
319 case AP_RESPONSE_BUSY:
320 case AP_RESPONSE_RESET_IN_PROGRESS:
321 return AP_SM_WAIT_TIMEOUT;
322 case AP_RESPONSE_Q_NOT_AVAIL:
323 case AP_RESPONSE_DECONFIGURED:
324 case AP_RESPONSE_CHECKSTOPPED:
326 aq->sm_state = AP_SM_STATE_BORKED;
327 return AP_SM_WAIT_NONE;
332 * ap_sm_setirq_wait(): Test queue for completion of the irq enablement
333 * @aq: pointer to the AP queue
335 * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
337 static enum ap_sm_wait ap_sm_setirq_wait(struct ap_queue *aq)
339 struct ap_queue_status status;
341 if (aq->queue_count > 0 && aq->reply)
342 /* Try to read a completed message and get the status */
343 status = ap_sm_recv(aq);
345 /* Get the status with TAPQ */
346 status = ap_tapq(aq->qid, NULL);
348 if (status.irq_enabled == 1) {
349 /* Irqs are now enabled */
350 aq->interrupt = AP_INTR_ENABLED;
351 aq->sm_state = (aq->queue_count > 0) ?
352 AP_SM_STATE_WORKING : AP_SM_STATE_IDLE;
355 switch (status.response_code) {
356 case AP_RESPONSE_NORMAL:
357 if (aq->queue_count > 0)
358 return AP_SM_WAIT_AGAIN;
360 case AP_RESPONSE_NO_PENDING_REPLY:
361 return AP_SM_WAIT_TIMEOUT;
363 aq->sm_state = AP_SM_STATE_BORKED;
364 return AP_SM_WAIT_NONE;
369 * AP state machine jump table
371 static ap_func_t *ap_jumptable[NR_AP_SM_STATES][NR_AP_SM_EVENTS] = {
372 [AP_SM_STATE_RESET_START] = {
373 [AP_SM_EVENT_POLL] = ap_sm_reset,
374 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
376 [AP_SM_STATE_RESET_WAIT] = {
377 [AP_SM_EVENT_POLL] = ap_sm_reset_wait,
378 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
380 [AP_SM_STATE_SETIRQ_WAIT] = {
381 [AP_SM_EVENT_POLL] = ap_sm_setirq_wait,
382 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
384 [AP_SM_STATE_IDLE] = {
385 [AP_SM_EVENT_POLL] = ap_sm_write,
386 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
388 [AP_SM_STATE_WORKING] = {
389 [AP_SM_EVENT_POLL] = ap_sm_read_write,
390 [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
392 [AP_SM_STATE_QUEUE_FULL] = {
393 [AP_SM_EVENT_POLL] = ap_sm_read,
394 [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
396 [AP_SM_STATE_REMOVE] = {
397 [AP_SM_EVENT_POLL] = ap_sm_nop,
398 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
400 [AP_SM_STATE_UNBOUND] = {
401 [AP_SM_EVENT_POLL] = ap_sm_nop,
402 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
404 [AP_SM_STATE_BORKED] = {
405 [AP_SM_EVENT_POLL] = ap_sm_nop,
406 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
410 enum ap_sm_wait ap_sm_event(struct ap_queue *aq, enum ap_sm_event event)
412 return ap_jumptable[aq->sm_state][event](aq);
415 enum ap_sm_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_sm_event event)
417 enum ap_sm_wait wait;
419 while ((wait = ap_sm_event(aq, event)) == AP_SM_WAIT_AGAIN)
425 * AP queue related attributes.
427 static ssize_t request_count_show(struct device *dev,
428 struct device_attribute *attr,
431 struct ap_queue *aq = to_ap_queue(dev);
434 spin_lock_bh(&aq->lock);
435 req_cnt = aq->total_request_count;
436 spin_unlock_bh(&aq->lock);
437 return scnprintf(buf, PAGE_SIZE, "%llu\n", req_cnt);
440 static ssize_t request_count_store(struct device *dev,
441 struct device_attribute *attr,
442 const char *buf, size_t count)
444 struct ap_queue *aq = to_ap_queue(dev);
446 spin_lock_bh(&aq->lock);
447 aq->total_request_count = 0;
448 spin_unlock_bh(&aq->lock);
453 static DEVICE_ATTR_RW(request_count);
455 static ssize_t requestq_count_show(struct device *dev,
456 struct device_attribute *attr, char *buf)
458 struct ap_queue *aq = to_ap_queue(dev);
459 unsigned int reqq_cnt = 0;
461 spin_lock_bh(&aq->lock);
462 reqq_cnt = aq->requestq_count;
463 spin_unlock_bh(&aq->lock);
464 return scnprintf(buf, PAGE_SIZE, "%d\n", reqq_cnt);
467 static DEVICE_ATTR_RO(requestq_count);
469 static ssize_t pendingq_count_show(struct device *dev,
470 struct device_attribute *attr, char *buf)
472 struct ap_queue *aq = to_ap_queue(dev);
473 unsigned int penq_cnt = 0;
475 spin_lock_bh(&aq->lock);
476 penq_cnt = aq->pendingq_count;
477 spin_unlock_bh(&aq->lock);
478 return scnprintf(buf, PAGE_SIZE, "%d\n", penq_cnt);
481 static DEVICE_ATTR_RO(pendingq_count);
483 static ssize_t reset_show(struct device *dev,
484 struct device_attribute *attr, char *buf)
486 struct ap_queue *aq = to_ap_queue(dev);
489 spin_lock_bh(&aq->lock);
490 switch (aq->sm_state) {
491 case AP_SM_STATE_RESET_START:
492 case AP_SM_STATE_RESET_WAIT:
493 rc = scnprintf(buf, PAGE_SIZE, "Reset in progress.\n");
495 case AP_SM_STATE_WORKING:
496 case AP_SM_STATE_QUEUE_FULL:
497 rc = scnprintf(buf, PAGE_SIZE, "Reset Timer armed.\n");
500 rc = scnprintf(buf, PAGE_SIZE, "No Reset Timer set.\n");
502 spin_unlock_bh(&aq->lock);
506 static ssize_t reset_store(struct device *dev,
507 struct device_attribute *attr,
508 const char *buf, size_t count)
510 struct ap_queue *aq = to_ap_queue(dev);
512 spin_lock_bh(&aq->lock);
513 __ap_flush_queue(aq);
514 aq->sm_state = AP_SM_STATE_RESET_START;
515 ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
516 spin_unlock_bh(&aq->lock);
518 AP_DBF(DBF_INFO, "reset queue=%02x.%04x triggered by user\n",
519 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
524 static DEVICE_ATTR_RW(reset);
526 static ssize_t interrupt_show(struct device *dev,
527 struct device_attribute *attr, char *buf)
529 struct ap_queue *aq = to_ap_queue(dev);
532 spin_lock_bh(&aq->lock);
533 if (aq->sm_state == AP_SM_STATE_SETIRQ_WAIT)
534 rc = scnprintf(buf, PAGE_SIZE, "Enable Interrupt pending.\n");
535 else if (aq->interrupt == AP_INTR_ENABLED)
536 rc = scnprintf(buf, PAGE_SIZE, "Interrupts enabled.\n");
538 rc = scnprintf(buf, PAGE_SIZE, "Interrupts disabled.\n");
539 spin_unlock_bh(&aq->lock);
543 static DEVICE_ATTR_RO(interrupt);
545 static struct attribute *ap_queue_dev_attrs[] = {
546 &dev_attr_request_count.attr,
547 &dev_attr_requestq_count.attr,
548 &dev_attr_pendingq_count.attr,
549 &dev_attr_reset.attr,
550 &dev_attr_interrupt.attr,
554 static struct attribute_group ap_queue_dev_attr_group = {
555 .attrs = ap_queue_dev_attrs
558 static const struct attribute_group *ap_queue_dev_attr_groups[] = {
559 &ap_queue_dev_attr_group,
563 static struct device_type ap_queue_type = {
565 .groups = ap_queue_dev_attr_groups,
568 static void ap_queue_device_release(struct device *dev)
570 struct ap_queue *aq = to_ap_queue(dev);
572 spin_lock_bh(&ap_queues_lock);
573 hash_del(&aq->hnode);
574 spin_unlock_bh(&ap_queues_lock);
579 struct ap_queue *ap_queue_create(ap_qid_t qid, int device_type)
583 aq = kzalloc(sizeof(*aq), GFP_KERNEL);
586 aq->ap_dev.device.release = ap_queue_device_release;
587 aq->ap_dev.device.type = &ap_queue_type;
588 aq->ap_dev.device_type = device_type;
590 aq->sm_state = AP_SM_STATE_UNBOUND;
591 aq->interrupt = AP_INTR_DISABLED;
592 spin_lock_init(&aq->lock);
593 INIT_LIST_HEAD(&aq->pendingq);
594 INIT_LIST_HEAD(&aq->requestq);
595 timer_setup(&aq->timeout, ap_request_timeout, 0);
600 void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *reply)
604 spin_lock_bh(&aq->lock);
605 ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
606 spin_unlock_bh(&aq->lock);
608 EXPORT_SYMBOL(ap_queue_init_reply);
611 * ap_queue_message(): Queue a request to an AP device.
612 * @aq: The AP device to queue the message to
613 * @ap_msg: The message that is to be added
615 void ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg)
617 /* For asynchronous message handling a valid receive-callback
620 BUG_ON(!ap_msg->receive);
622 spin_lock_bh(&aq->lock);
623 /* Queue the message. */
624 list_add_tail(&ap_msg->list, &aq->requestq);
625 aq->requestq_count++;
626 aq->total_request_count++;
627 atomic64_inc(&aq->card->total_request_count);
628 /* Send/receive as many request from the queue as possible. */
629 ap_wait(ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
630 spin_unlock_bh(&aq->lock);
632 EXPORT_SYMBOL(ap_queue_message);
635 * ap_cancel_message(): Cancel a crypto request.
636 * @aq: The AP device that has the message queued
637 * @ap_msg: The message that is to be removed
639 * Cancel a crypto request. This is done by removing the request
640 * from the device pending or request queue. Note that the
641 * request stays on the AP queue. When it finishes the message
642 * reply will be discarded because the psmid can't be found.
644 void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg)
646 struct ap_message *tmp;
648 spin_lock_bh(&aq->lock);
649 if (!list_empty(&ap_msg->list)) {
650 list_for_each_entry(tmp, &aq->pendingq, list)
651 if (tmp->psmid == ap_msg->psmid) {
652 aq->pendingq_count--;
655 aq->requestq_count--;
657 list_del_init(&ap_msg->list);
659 spin_unlock_bh(&aq->lock);
661 EXPORT_SYMBOL(ap_cancel_message);
664 * __ap_flush_queue(): Flush requests.
665 * @aq: Pointer to the AP queue
667 * Flush all requests from the request/pending queue of an AP device.
669 static void __ap_flush_queue(struct ap_queue *aq)
671 struct ap_message *ap_msg, *next;
673 list_for_each_entry_safe(ap_msg, next, &aq->pendingq, list) {
674 list_del_init(&ap_msg->list);
675 aq->pendingq_count--;
676 ap_msg->rc = -EAGAIN;
677 ap_msg->receive(aq, ap_msg, NULL);
679 list_for_each_entry_safe(ap_msg, next, &aq->requestq, list) {
680 list_del_init(&ap_msg->list);
681 aq->requestq_count--;
682 ap_msg->rc = -EAGAIN;
683 ap_msg->receive(aq, ap_msg, NULL);
688 void ap_flush_queue(struct ap_queue *aq)
690 spin_lock_bh(&aq->lock);
691 __ap_flush_queue(aq);
692 spin_unlock_bh(&aq->lock);
694 EXPORT_SYMBOL(ap_flush_queue);
696 void ap_queue_prepare_remove(struct ap_queue *aq)
698 spin_lock_bh(&aq->lock);
700 __ap_flush_queue(aq);
701 /* set REMOVE state to prevent new messages are queued in */
702 aq->sm_state = AP_SM_STATE_REMOVE;
703 spin_unlock_bh(&aq->lock);
704 del_timer_sync(&aq->timeout);
707 void ap_queue_remove(struct ap_queue *aq)
710 * all messages have been flushed and the state is
711 * AP_SM_STATE_REMOVE. Now reset with zero which also
712 * clears the irq registration and move the state
713 * to AP_SM_STATE_UNBOUND to signal that this queue
714 * is not used by any driver currently.
716 spin_lock_bh(&aq->lock);
718 aq->sm_state = AP_SM_STATE_UNBOUND;
719 spin_unlock_bh(&aq->lock);
722 void ap_queue_init_state(struct ap_queue *aq)
724 spin_lock_bh(&aq->lock);
725 aq->sm_state = AP_SM_STATE_RESET_START;
726 ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
727 spin_unlock_bh(&aq->lock);
729 EXPORT_SYMBOL(ap_queue_init_state);