2 * Copyright IBM Corp. 2016
3 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
5 * Adjunct processor bus, queue related code.
8 #define KMSG_COMPONENT "ap"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11 #include <linux/init.h>
12 #include <linux/slab.h>
13 #include <asm/facility.h>
19 * ap_queue_enable_interruption(): Enable interruption on an AP queue.
20 * @qid: The AP queue number
21 * @ind: the notification indicator byte
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().
27 static int ap_queue_enable_interruption(struct ap_queue
*aq
, void *ind
)
29 struct ap_queue_status status
;
31 status
= ap_aqic(aq
->qid
, ind
);
32 switch (status
.response_code
) {
33 case AP_RESPONSE_NORMAL
:
34 case AP_RESPONSE_OTHERWISE_CHANGED
:
36 case AP_RESPONSE_Q_NOT_AVAIL
:
37 case AP_RESPONSE_DECONFIGURED
:
38 case AP_RESPONSE_CHECKSTOPPED
:
39 case AP_RESPONSE_INVALID_ADDRESS
:
40 pr_err("Registering adapter interrupts for AP device %02x.%04x failed\n",
42 AP_QID_QUEUE(aq
->qid
));
44 case AP_RESPONSE_RESET_IN_PROGRESS
:
45 case AP_RESPONSE_BUSY
:
52 * __ap_send(): Send message to adjunct processor queue.
53 * @qid: The AP queue number
54 * @psmid: The program supplied message identifier
55 * @msg: The message text
56 * @length: The message length
57 * @special: Special Bit
59 * Returns AP queue status structure.
60 * Condition code 1 on NQAP can't happen because the L bit is 1.
61 * Condition code 2 on NQAP also means the send is incomplete,
62 * because a segment boundary was reached. The NQAP is repeated.
64 static inline struct ap_queue_status
65 __ap_send(ap_qid_t qid
, unsigned long long psmid
, void *msg
, size_t length
,
70 return ap_nqap(qid
, psmid
, msg
, length
);
73 int ap_send(ap_qid_t qid
, unsigned long long psmid
, void *msg
, size_t length
)
75 struct ap_queue_status status
;
77 status
= __ap_send(qid
, psmid
, msg
, length
, 0);
78 switch (status
.response_code
) {
79 case AP_RESPONSE_NORMAL
:
81 case AP_RESPONSE_Q_FULL
:
82 case AP_RESPONSE_RESET_IN_PROGRESS
:
84 case AP_RESPONSE_REQ_FAC_NOT_INST
:
86 default: /* Device is gone. */
90 EXPORT_SYMBOL(ap_send
);
92 int ap_recv(ap_qid_t qid
, unsigned long long *psmid
, void *msg
, size_t length
)
94 struct ap_queue_status status
;
98 status
= ap_dqap(qid
, psmid
, msg
, length
);
99 switch (status
.response_code
) {
100 case AP_RESPONSE_NORMAL
:
102 case AP_RESPONSE_NO_PENDING_REPLY
:
103 if (status
.queue_empty
)
106 case AP_RESPONSE_RESET_IN_PROGRESS
:
112 EXPORT_SYMBOL(ap_recv
);
114 /* State machine definitions and helpers */
116 static enum ap_wait
ap_sm_nop(struct ap_queue
*aq
)
122 * ap_sm_recv(): Receive pending reply messages from an AP queue but do
123 * not change the state of the device.
124 * @aq: pointer to the AP queue
126 * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
128 static struct ap_queue_status
ap_sm_recv(struct ap_queue
*aq
)
130 struct ap_queue_status status
;
131 struct ap_message
*ap_msg
;
133 status
= ap_dqap(aq
->qid
, &aq
->reply
->psmid
,
134 aq
->reply
->message
, aq
->reply
->length
);
135 switch (status
.response_code
) {
136 case AP_RESPONSE_NORMAL
:
138 if (aq
->queue_count
> 0)
139 mod_timer(&aq
->timeout
,
140 jiffies
+ aq
->request_timeout
);
141 list_for_each_entry(ap_msg
, &aq
->pendingq
, list
) {
142 if (ap_msg
->psmid
!= aq
->reply
->psmid
)
144 list_del_init(&ap_msg
->list
);
145 aq
->pendingq_count
--;
146 ap_msg
->receive(aq
, ap_msg
, aq
->reply
);
149 case AP_RESPONSE_NO_PENDING_REPLY
:
150 if (!status
.queue_empty
|| aq
->queue_count
<= 0)
152 /* The card shouldn't forget requests but who knows. */
154 list_splice_init(&aq
->pendingq
, &aq
->requestq
);
155 aq
->requestq_count
+= aq
->pendingq_count
;
156 aq
->pendingq_count
= 0;
165 * ap_sm_read(): Receive pending reply messages from an AP queue.
166 * @aq: pointer to the AP queue
168 * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
170 static enum ap_wait
ap_sm_read(struct ap_queue
*aq
)
172 struct ap_queue_status status
;
176 status
= ap_sm_recv(aq
);
177 switch (status
.response_code
) {
178 case AP_RESPONSE_NORMAL
:
179 if (aq
->queue_count
> 0) {
180 aq
->state
= AP_STATE_WORKING
;
181 return AP_WAIT_AGAIN
;
183 aq
->state
= AP_STATE_IDLE
;
185 case AP_RESPONSE_NO_PENDING_REPLY
:
186 if (aq
->queue_count
> 0)
187 return AP_WAIT_INTERRUPT
;
188 aq
->state
= AP_STATE_IDLE
;
191 aq
->state
= AP_STATE_BORKED
;
197 * ap_sm_suspend_read(): Receive pending reply messages from an AP queue
198 * without changing the device state in between. In suspend mode we don't
199 * allow sending new requests, therefore just fetch pending replies.
200 * @aq: pointer to the AP queue
202 * Returns AP_WAIT_NONE or AP_WAIT_AGAIN
204 static enum ap_wait
ap_sm_suspend_read(struct ap_queue
*aq
)
206 struct ap_queue_status status
;
210 status
= ap_sm_recv(aq
);
211 switch (status
.response_code
) {
212 case AP_RESPONSE_NORMAL
:
213 if (aq
->queue_count
> 0)
214 return AP_WAIT_AGAIN
;
222 * ap_sm_write(): Send messages from the request queue to an AP queue.
223 * @aq: pointer to the AP queue
225 * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
227 static enum ap_wait
ap_sm_write(struct ap_queue
*aq
)
229 struct ap_queue_status status
;
230 struct ap_message
*ap_msg
;
232 if (aq
->requestq_count
<= 0)
234 /* Start the next request on the queue. */
235 ap_msg
= list_entry(aq
->requestq
.next
, struct ap_message
, list
);
236 status
= __ap_send(aq
->qid
, ap_msg
->psmid
,
237 ap_msg
->message
, ap_msg
->length
, ap_msg
->special
);
238 switch (status
.response_code
) {
239 case AP_RESPONSE_NORMAL
:
241 if (aq
->queue_count
== 1)
242 mod_timer(&aq
->timeout
, jiffies
+ aq
->request_timeout
);
243 list_move_tail(&ap_msg
->list
, &aq
->pendingq
);
244 aq
->requestq_count
--;
245 aq
->pendingq_count
++;
246 if (aq
->queue_count
< aq
->card
->queue_depth
) {
247 aq
->state
= AP_STATE_WORKING
;
248 return AP_WAIT_AGAIN
;
251 case AP_RESPONSE_Q_FULL
:
252 aq
->state
= AP_STATE_QUEUE_FULL
;
253 return AP_WAIT_INTERRUPT
;
254 case AP_RESPONSE_RESET_IN_PROGRESS
:
255 aq
->state
= AP_STATE_RESET_WAIT
;
256 return AP_WAIT_TIMEOUT
;
257 case AP_RESPONSE_MESSAGE_TOO_BIG
:
258 case AP_RESPONSE_REQ_FAC_NOT_INST
:
259 list_del_init(&ap_msg
->list
);
260 aq
->requestq_count
--;
261 ap_msg
->rc
= -EINVAL
;
262 ap_msg
->receive(aq
, ap_msg
, NULL
);
263 return AP_WAIT_AGAIN
;
265 aq
->state
= AP_STATE_BORKED
;
271 * ap_sm_read_write(): Send and receive messages to/from an AP queue.
272 * @aq: pointer to the AP queue
274 * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
276 static enum ap_wait
ap_sm_read_write(struct ap_queue
*aq
)
278 return min(ap_sm_read(aq
), ap_sm_write(aq
));
282 * ap_sm_reset(): Reset an AP queue.
283 * @qid: The AP queue number
285 * Submit the Reset command to an AP queue.
287 static enum ap_wait
ap_sm_reset(struct ap_queue
*aq
)
289 struct ap_queue_status status
;
291 status
= ap_rapq(aq
->qid
);
292 switch (status
.response_code
) {
293 case AP_RESPONSE_NORMAL
:
294 case AP_RESPONSE_RESET_IN_PROGRESS
:
295 aq
->state
= AP_STATE_RESET_WAIT
;
296 aq
->interrupt
= AP_INTR_DISABLED
;
297 return AP_WAIT_TIMEOUT
;
298 case AP_RESPONSE_BUSY
:
299 return AP_WAIT_TIMEOUT
;
300 case AP_RESPONSE_Q_NOT_AVAIL
:
301 case AP_RESPONSE_DECONFIGURED
:
302 case AP_RESPONSE_CHECKSTOPPED
:
304 aq
->state
= AP_STATE_BORKED
;
310 * ap_sm_reset_wait(): Test queue for completion of the reset operation
311 * @aq: pointer to the AP queue
313 * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
315 static enum ap_wait
ap_sm_reset_wait(struct ap_queue
*aq
)
317 struct ap_queue_status status
;
320 if (aq
->queue_count
> 0 && aq
->reply
)
321 /* Try to read a completed message and get the status */
322 status
= ap_sm_recv(aq
);
324 /* Get the status with TAPQ */
325 status
= ap_tapq(aq
->qid
, NULL
);
327 switch (status
.response_code
) {
328 case AP_RESPONSE_NORMAL
:
329 lsi_ptr
= ap_airq_ptr();
330 if (lsi_ptr
&& ap_queue_enable_interruption(aq
, lsi_ptr
) == 0)
331 aq
->state
= AP_STATE_SETIRQ_WAIT
;
333 aq
->state
= (aq
->queue_count
> 0) ?
334 AP_STATE_WORKING
: AP_STATE_IDLE
;
335 return AP_WAIT_AGAIN
;
336 case AP_RESPONSE_BUSY
:
337 case AP_RESPONSE_RESET_IN_PROGRESS
:
338 return AP_WAIT_TIMEOUT
;
339 case AP_RESPONSE_Q_NOT_AVAIL
:
340 case AP_RESPONSE_DECONFIGURED
:
341 case AP_RESPONSE_CHECKSTOPPED
:
343 aq
->state
= AP_STATE_BORKED
;
349 * ap_sm_setirq_wait(): Test queue for completion of the irq enablement
350 * @aq: pointer to the AP queue
352 * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
354 static enum ap_wait
ap_sm_setirq_wait(struct ap_queue
*aq
)
356 struct ap_queue_status status
;
358 if (aq
->queue_count
> 0 && aq
->reply
)
359 /* Try to read a completed message and get the status */
360 status
= ap_sm_recv(aq
);
362 /* Get the status with TAPQ */
363 status
= ap_tapq(aq
->qid
, NULL
);
365 if (status
.int_enabled
== 1) {
366 /* Irqs are now enabled */
367 aq
->interrupt
= AP_INTR_ENABLED
;
368 aq
->state
= (aq
->queue_count
> 0) ?
369 AP_STATE_WORKING
: AP_STATE_IDLE
;
372 switch (status
.response_code
) {
373 case AP_RESPONSE_NORMAL
:
374 if (aq
->queue_count
> 0)
375 return AP_WAIT_AGAIN
;
377 case AP_RESPONSE_NO_PENDING_REPLY
:
378 return AP_WAIT_TIMEOUT
;
380 aq
->state
= AP_STATE_BORKED
;
386 * AP state machine jump table
388 static ap_func_t
*ap_jumptable
[NR_AP_STATES
][NR_AP_EVENTS
] = {
389 [AP_STATE_RESET_START
] = {
390 [AP_EVENT_POLL
] = ap_sm_reset
,
391 [AP_EVENT_TIMEOUT
] = ap_sm_nop
,
393 [AP_STATE_RESET_WAIT
] = {
394 [AP_EVENT_POLL
] = ap_sm_reset_wait
,
395 [AP_EVENT_TIMEOUT
] = ap_sm_nop
,
397 [AP_STATE_SETIRQ_WAIT
] = {
398 [AP_EVENT_POLL
] = ap_sm_setirq_wait
,
399 [AP_EVENT_TIMEOUT
] = ap_sm_nop
,
402 [AP_EVENT_POLL
] = ap_sm_write
,
403 [AP_EVENT_TIMEOUT
] = ap_sm_nop
,
405 [AP_STATE_WORKING
] = {
406 [AP_EVENT_POLL
] = ap_sm_read_write
,
407 [AP_EVENT_TIMEOUT
] = ap_sm_reset
,
409 [AP_STATE_QUEUE_FULL
] = {
410 [AP_EVENT_POLL
] = ap_sm_read
,
411 [AP_EVENT_TIMEOUT
] = ap_sm_reset
,
413 [AP_STATE_SUSPEND_WAIT
] = {
414 [AP_EVENT_POLL
] = ap_sm_suspend_read
,
415 [AP_EVENT_TIMEOUT
] = ap_sm_nop
,
417 [AP_STATE_BORKED
] = {
418 [AP_EVENT_POLL
] = ap_sm_nop
,
419 [AP_EVENT_TIMEOUT
] = ap_sm_nop
,
423 enum ap_wait
ap_sm_event(struct ap_queue
*aq
, enum ap_event event
)
425 return ap_jumptable
[aq
->state
][event
](aq
);
428 enum ap_wait
ap_sm_event_loop(struct ap_queue
*aq
, enum ap_event event
)
432 while ((wait
= ap_sm_event(aq
, event
)) == AP_WAIT_AGAIN
)
438 * Power management for queue devices
440 void ap_queue_suspend(struct ap_device
*ap_dev
)
442 struct ap_queue
*aq
= to_ap_queue(&ap_dev
->device
);
444 /* Poll on the device until all requests are finished. */
445 spin_lock_bh(&aq
->lock
);
446 aq
->state
= AP_STATE_SUSPEND_WAIT
;
447 while (ap_sm_event(aq
, AP_EVENT_POLL
) != AP_WAIT_NONE
)
449 aq
->state
= AP_STATE_BORKED
;
450 spin_unlock_bh(&aq
->lock
);
452 EXPORT_SYMBOL(ap_queue_suspend
);
454 void ap_queue_resume(struct ap_device
*ap_dev
)
457 EXPORT_SYMBOL(ap_queue_resume
);
460 * AP queue related attributes.
462 static ssize_t
ap_request_count_show(struct device
*dev
,
463 struct device_attribute
*attr
,
466 struct ap_queue
*aq
= to_ap_queue(dev
);
467 unsigned int req_cnt
;
469 spin_lock_bh(&aq
->lock
);
470 req_cnt
= aq
->total_request_count
;
471 spin_unlock_bh(&aq
->lock
);
472 return snprintf(buf
, PAGE_SIZE
, "%d\n", req_cnt
);
475 static DEVICE_ATTR(request_count
, 0444, ap_request_count_show
, NULL
);
477 static ssize_t
ap_requestq_count_show(struct device
*dev
,
478 struct device_attribute
*attr
, char *buf
)
480 struct ap_queue
*aq
= to_ap_queue(dev
);
481 unsigned int reqq_cnt
= 0;
483 spin_lock_bh(&aq
->lock
);
484 reqq_cnt
= aq
->requestq_count
;
485 spin_unlock_bh(&aq
->lock
);
486 return snprintf(buf
, PAGE_SIZE
, "%d\n", reqq_cnt
);
489 static DEVICE_ATTR(requestq_count
, 0444, ap_requestq_count_show
, NULL
);
491 static ssize_t
ap_pendingq_count_show(struct device
*dev
,
492 struct device_attribute
*attr
, char *buf
)
494 struct ap_queue
*aq
= to_ap_queue(dev
);
495 unsigned int penq_cnt
= 0;
497 spin_lock_bh(&aq
->lock
);
498 penq_cnt
= aq
->pendingq_count
;
499 spin_unlock_bh(&aq
->lock
);
500 return snprintf(buf
, PAGE_SIZE
, "%d\n", penq_cnt
);
503 static DEVICE_ATTR(pendingq_count
, 0444, ap_pendingq_count_show
, NULL
);
505 static ssize_t
ap_reset_show(struct device
*dev
,
506 struct device_attribute
*attr
, char *buf
)
508 struct ap_queue
*aq
= to_ap_queue(dev
);
511 spin_lock_bh(&aq
->lock
);
513 case AP_STATE_RESET_START
:
514 case AP_STATE_RESET_WAIT
:
515 rc
= snprintf(buf
, PAGE_SIZE
, "Reset in progress.\n");
517 case AP_STATE_WORKING
:
518 case AP_STATE_QUEUE_FULL
:
519 rc
= snprintf(buf
, PAGE_SIZE
, "Reset Timer armed.\n");
522 rc
= snprintf(buf
, PAGE_SIZE
, "No Reset Timer set.\n");
524 spin_unlock_bh(&aq
->lock
);
528 static DEVICE_ATTR(reset
, 0444, ap_reset_show
, NULL
);
530 static ssize_t
ap_interrupt_show(struct device
*dev
,
531 struct device_attribute
*attr
, char *buf
)
533 struct ap_queue
*aq
= to_ap_queue(dev
);
536 spin_lock_bh(&aq
->lock
);
537 if (aq
->state
== AP_STATE_SETIRQ_WAIT
)
538 rc
= snprintf(buf
, PAGE_SIZE
, "Enable Interrupt pending.\n");
539 else if (aq
->interrupt
== AP_INTR_ENABLED
)
540 rc
= snprintf(buf
, PAGE_SIZE
, "Interrupts enabled.\n");
542 rc
= snprintf(buf
, PAGE_SIZE
, "Interrupts disabled.\n");
543 spin_unlock_bh(&aq
->lock
);
547 static DEVICE_ATTR(interrupt
, 0444, ap_interrupt_show
, NULL
);
549 static struct attribute
*ap_queue_dev_attrs
[] = {
550 &dev_attr_request_count
.attr
,
551 &dev_attr_requestq_count
.attr
,
552 &dev_attr_pendingq_count
.attr
,
553 &dev_attr_reset
.attr
,
554 &dev_attr_interrupt
.attr
,
558 static struct attribute_group ap_queue_dev_attr_group
= {
559 .attrs
= ap_queue_dev_attrs
562 static const struct attribute_group
*ap_queue_dev_attr_groups
[] = {
563 &ap_queue_dev_attr_group
,
567 struct device_type ap_queue_type
= {
569 .groups
= ap_queue_dev_attr_groups
,
572 static void ap_queue_device_release(struct device
*dev
)
574 kfree(to_ap_queue(dev
));
577 struct ap_queue
*ap_queue_create(ap_qid_t qid
, int device_type
)
581 aq
= kzalloc(sizeof(*aq
), GFP_KERNEL
);
584 aq
->ap_dev
.device
.release
= ap_queue_device_release
;
585 aq
->ap_dev
.device
.type
= &ap_queue_type
;
586 aq
->ap_dev
.device_type
= device_type
;
587 /* CEX6 toleration: map to CEX5 */
588 if (device_type
== AP_DEVICE_TYPE_CEX6
)
589 aq
->ap_dev
.device_type
= AP_DEVICE_TYPE_CEX5
;
591 aq
->state
= AP_STATE_RESET_START
;
592 aq
->interrupt
= AP_INTR_DISABLED
;
593 spin_lock_init(&aq
->lock
);
594 INIT_LIST_HEAD(&aq
->pendingq
);
595 INIT_LIST_HEAD(&aq
->requestq
);
596 setup_timer(&aq
->timeout
, ap_request_timeout
, (unsigned long) aq
);
601 void ap_queue_init_reply(struct ap_queue
*aq
, struct ap_message
*reply
)
605 spin_lock_bh(&aq
->lock
);
606 ap_wait(ap_sm_event(aq
, AP_EVENT_POLL
));
607 spin_unlock_bh(&aq
->lock
);
609 EXPORT_SYMBOL(ap_queue_init_reply
);
612 * ap_queue_message(): Queue a request to an AP device.
613 * @aq: The AP device to queue the message to
614 * @ap_msg: The message that is to be added
616 void ap_queue_message(struct ap_queue
*aq
, struct ap_message
*ap_msg
)
618 /* For asynchronous message handling a valid receive-callback
621 BUG_ON(!ap_msg
->receive
);
623 spin_lock_bh(&aq
->lock
);
624 /* Queue the message. */
625 list_add_tail(&ap_msg
->list
, &aq
->requestq
);
626 aq
->requestq_count
++;
627 aq
->total_request_count
++;
628 atomic_inc(&aq
->card
->total_request_count
);
629 /* Send/receive as many request from the queue as possible. */
630 ap_wait(ap_sm_event_loop(aq
, AP_EVENT_POLL
));
631 spin_unlock_bh(&aq
->lock
);
633 EXPORT_SYMBOL(ap_queue_message
);
636 * ap_cancel_message(): Cancel a crypto request.
637 * @aq: The AP device that has the message queued
638 * @ap_msg: The message that is to be removed
640 * Cancel a crypto request. This is done by removing the request
641 * from the device pending or request queue. Note that the
642 * request stays on the AP queue. When it finishes the message
643 * reply will be discarded because the psmid can't be found.
645 void ap_cancel_message(struct ap_queue
*aq
, struct ap_message
*ap_msg
)
647 struct ap_message
*tmp
;
649 spin_lock_bh(&aq
->lock
);
650 if (!list_empty(&ap_msg
->list
)) {
651 list_for_each_entry(tmp
, &aq
->pendingq
, list
)
652 if (tmp
->psmid
== ap_msg
->psmid
) {
653 aq
->pendingq_count
--;
656 aq
->requestq_count
--;
658 list_del_init(&ap_msg
->list
);
660 spin_unlock_bh(&aq
->lock
);
662 EXPORT_SYMBOL(ap_cancel_message
);
665 * __ap_flush_queue(): Flush requests.
666 * @aq: Pointer to the AP queue
668 * Flush all requests from the request/pending queue of an AP device.
670 static void __ap_flush_queue(struct ap_queue
*aq
)
672 struct ap_message
*ap_msg
, *next
;
674 list_for_each_entry_safe(ap_msg
, next
, &aq
->pendingq
, list
) {
675 list_del_init(&ap_msg
->list
);
676 aq
->pendingq_count
--;
677 ap_msg
->rc
= -EAGAIN
;
678 ap_msg
->receive(aq
, ap_msg
, NULL
);
680 list_for_each_entry_safe(ap_msg
, next
, &aq
->requestq
, list
) {
681 list_del_init(&ap_msg
->list
);
682 aq
->requestq_count
--;
683 ap_msg
->rc
= -EAGAIN
;
684 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_remove(struct ap_queue
*aq
)
699 del_timer_sync(&aq
->timeout
);
701 EXPORT_SYMBOL(ap_queue_remove
);