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_irq(): Enable interrupt support on this 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_irq(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, NULL, NULL);
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;
140 unsigned long resgr0 = 0;
144 * DQAP loop until response code and resgr0 indicate that
145 * the msg is totally received. As we use the very same buffer
146 * the msg is overwritten with each invocation. That's intended
147 * and the receiver of the msg is informed with a msg rc code
148 * of EMSGSIZE in such a case.
151 status = ap_dqap(aq->qid, &aq->reply->psmid,
152 aq->reply->msg, aq->reply->bufsize,
155 } while (status.response_code == 0xFF && resgr0 != 0);
157 switch (status.response_code) {
158 case AP_RESPONSE_NORMAL:
159 aq->queue_count = max_t(int, 0, aq->queue_count - 1);
160 if (aq->queue_count > 0)
161 mod_timer(&aq->timeout,
162 jiffies + aq->request_timeout);
163 list_for_each_entry(ap_msg, &aq->pendingq, list) {
164 if (ap_msg->psmid != aq->reply->psmid)
166 list_del_init(&ap_msg->list);
167 aq->pendingq_count--;
169 ap_msg->rc = -EMSGSIZE;
170 ap_msg->receive(aq, ap_msg, NULL);
172 ap_msg->receive(aq, ap_msg, aq->reply);
178 AP_DBF_WARN("%s unassociated reply psmid=0x%016llx on 0x%02x.%04x\n",
179 __func__, aq->reply->psmid,
180 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
183 case AP_RESPONSE_NO_PENDING_REPLY:
184 if (!status.queue_empty || aq->queue_count <= 0)
186 /* The card shouldn't forget requests but who knows. */
188 list_splice_init(&aq->pendingq, &aq->requestq);
189 aq->requestq_count += aq->pendingq_count;
190 aq->pendingq_count = 0;
199 * ap_sm_read(): Receive pending reply messages from an AP queue.
200 * @aq: pointer to the AP queue
202 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
204 static enum ap_sm_wait ap_sm_read(struct ap_queue *aq)
206 struct ap_queue_status status;
209 return AP_SM_WAIT_NONE;
210 status = ap_sm_recv(aq);
211 switch (status.response_code) {
212 case AP_RESPONSE_NORMAL:
213 if (aq->queue_count > 0) {
214 aq->sm_state = AP_SM_STATE_WORKING;
215 return AP_SM_WAIT_AGAIN;
217 aq->sm_state = AP_SM_STATE_IDLE;
218 return AP_SM_WAIT_NONE;
219 case AP_RESPONSE_NO_PENDING_REPLY:
220 if (aq->queue_count > 0)
221 return aq->interrupt ?
222 AP_SM_WAIT_INTERRUPT : AP_SM_WAIT_TIMEOUT;
223 aq->sm_state = AP_SM_STATE_IDLE;
224 return AP_SM_WAIT_NONE;
226 aq->dev_state = AP_DEV_STATE_ERROR;
227 aq->last_err_rc = status.response_code;
228 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
229 __func__, status.response_code,
230 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
231 return AP_SM_WAIT_NONE;
236 * ap_sm_write(): Send messages from the request queue to an AP queue.
237 * @aq: pointer to the AP queue
239 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
241 static enum ap_sm_wait ap_sm_write(struct ap_queue *aq)
243 struct ap_queue_status status;
244 struct ap_message *ap_msg;
245 ap_qid_t qid = aq->qid;
247 if (aq->requestq_count <= 0)
248 return AP_SM_WAIT_NONE;
249 /* Start the next request on the queue. */
250 ap_msg = list_entry(aq->requestq.next, struct ap_message, list);
251 #ifdef CONFIG_ZCRYPT_DEBUG
252 if (ap_msg->fi.action == AP_FI_ACTION_NQAP_QID_INVAL) {
253 AP_DBF_WARN("%s fi cmd 0x%04x: forcing invalid qid 0xFF00\n",
254 __func__, ap_msg->fi.cmd);
258 status = __ap_send(qid, ap_msg->psmid,
259 ap_msg->msg, ap_msg->len,
260 ap_msg->flags & AP_MSG_FLAG_SPECIAL);
261 switch (status.response_code) {
262 case AP_RESPONSE_NORMAL:
263 aq->queue_count = max_t(int, 1, aq->queue_count + 1);
264 if (aq->queue_count == 1)
265 mod_timer(&aq->timeout, jiffies + aq->request_timeout);
266 list_move_tail(&ap_msg->list, &aq->pendingq);
267 aq->requestq_count--;
268 aq->pendingq_count++;
269 if (aq->queue_count < aq->card->queue_depth) {
270 aq->sm_state = AP_SM_STATE_WORKING;
271 return AP_SM_WAIT_AGAIN;
274 case AP_RESPONSE_Q_FULL:
275 aq->sm_state = AP_SM_STATE_QUEUE_FULL;
276 return aq->interrupt ?
277 AP_SM_WAIT_INTERRUPT : AP_SM_WAIT_TIMEOUT;
278 case AP_RESPONSE_RESET_IN_PROGRESS:
279 aq->sm_state = AP_SM_STATE_RESET_WAIT;
280 return AP_SM_WAIT_TIMEOUT;
281 case AP_RESPONSE_INVALID_DOMAIN:
282 AP_DBF(DBF_WARN, "AP_RESPONSE_INVALID_DOMAIN on NQAP\n");
284 case AP_RESPONSE_MESSAGE_TOO_BIG:
285 case AP_RESPONSE_REQ_FAC_NOT_INST:
286 list_del_init(&ap_msg->list);
287 aq->requestq_count--;
288 ap_msg->rc = -EINVAL;
289 ap_msg->receive(aq, ap_msg, NULL);
290 return AP_SM_WAIT_AGAIN;
292 aq->dev_state = AP_DEV_STATE_ERROR;
293 aq->last_err_rc = status.response_code;
294 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
295 __func__, status.response_code,
296 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
297 return AP_SM_WAIT_NONE;
302 * ap_sm_read_write(): Send and receive messages to/from an AP queue.
303 * @aq: pointer to the AP queue
305 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
307 static enum ap_sm_wait ap_sm_read_write(struct ap_queue *aq)
309 return min(ap_sm_read(aq), ap_sm_write(aq));
313 * ap_sm_reset(): Reset an AP queue.
314 * @qid: The AP queue number
316 * Submit the Reset command to an AP queue.
318 static enum ap_sm_wait ap_sm_reset(struct ap_queue *aq)
320 struct ap_queue_status status;
322 status = ap_rapq(aq->qid);
323 switch (status.response_code) {
324 case AP_RESPONSE_NORMAL:
325 case AP_RESPONSE_RESET_IN_PROGRESS:
326 aq->sm_state = AP_SM_STATE_RESET_WAIT;
327 aq->interrupt = false;
328 return AP_SM_WAIT_TIMEOUT;
330 aq->dev_state = AP_DEV_STATE_ERROR;
331 aq->last_err_rc = status.response_code;
332 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
333 __func__, status.response_code,
334 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
335 return AP_SM_WAIT_NONE;
340 * ap_sm_reset_wait(): Test queue for completion of the reset operation
341 * @aq: pointer to the AP queue
343 * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
345 static enum ap_sm_wait ap_sm_reset_wait(struct ap_queue *aq)
347 struct ap_queue_status status;
350 if (aq->queue_count > 0 && aq->reply)
351 /* Try to read a completed message and get the status */
352 status = ap_sm_recv(aq);
354 /* Get the status with TAPQ */
355 status = ap_tapq(aq->qid, NULL);
357 switch (status.response_code) {
358 case AP_RESPONSE_NORMAL:
359 lsi_ptr = ap_airq_ptr();
360 if (lsi_ptr && ap_queue_enable_irq(aq, lsi_ptr) == 0)
361 aq->sm_state = AP_SM_STATE_SETIRQ_WAIT;
363 aq->sm_state = (aq->queue_count > 0) ?
364 AP_SM_STATE_WORKING : AP_SM_STATE_IDLE;
365 return AP_SM_WAIT_AGAIN;
366 case AP_RESPONSE_BUSY:
367 case AP_RESPONSE_RESET_IN_PROGRESS:
368 return AP_SM_WAIT_TIMEOUT;
369 case AP_RESPONSE_Q_NOT_AVAIL:
370 case AP_RESPONSE_DECONFIGURED:
371 case AP_RESPONSE_CHECKSTOPPED:
373 aq->dev_state = AP_DEV_STATE_ERROR;
374 aq->last_err_rc = status.response_code;
375 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
376 __func__, status.response_code,
377 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
378 return AP_SM_WAIT_NONE;
383 * ap_sm_setirq_wait(): Test queue for completion of the irq enablement
384 * @aq: pointer to the AP queue
386 * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
388 static enum ap_sm_wait ap_sm_setirq_wait(struct ap_queue *aq)
390 struct ap_queue_status status;
392 if (aq->queue_count > 0 && aq->reply)
393 /* Try to read a completed message and get the status */
394 status = ap_sm_recv(aq);
396 /* Get the status with TAPQ */
397 status = ap_tapq(aq->qid, NULL);
399 if (status.irq_enabled == 1) {
400 /* Irqs are now enabled */
401 aq->interrupt = true;
402 aq->sm_state = (aq->queue_count > 0) ?
403 AP_SM_STATE_WORKING : AP_SM_STATE_IDLE;
406 switch (status.response_code) {
407 case AP_RESPONSE_NORMAL:
408 if (aq->queue_count > 0)
409 return AP_SM_WAIT_AGAIN;
411 case AP_RESPONSE_NO_PENDING_REPLY:
412 return AP_SM_WAIT_TIMEOUT;
414 aq->dev_state = AP_DEV_STATE_ERROR;
415 aq->last_err_rc = status.response_code;
416 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
417 __func__, status.response_code,
418 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
419 return AP_SM_WAIT_NONE;
424 * AP state machine jump table
426 static ap_func_t *ap_jumptable[NR_AP_SM_STATES][NR_AP_SM_EVENTS] = {
427 [AP_SM_STATE_RESET_START] = {
428 [AP_SM_EVENT_POLL] = ap_sm_reset,
429 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
431 [AP_SM_STATE_RESET_WAIT] = {
432 [AP_SM_EVENT_POLL] = ap_sm_reset_wait,
433 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
435 [AP_SM_STATE_SETIRQ_WAIT] = {
436 [AP_SM_EVENT_POLL] = ap_sm_setirq_wait,
437 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
439 [AP_SM_STATE_IDLE] = {
440 [AP_SM_EVENT_POLL] = ap_sm_write,
441 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
443 [AP_SM_STATE_WORKING] = {
444 [AP_SM_EVENT_POLL] = ap_sm_read_write,
445 [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
447 [AP_SM_STATE_QUEUE_FULL] = {
448 [AP_SM_EVENT_POLL] = ap_sm_read,
449 [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
453 enum ap_sm_wait ap_sm_event(struct ap_queue *aq, enum ap_sm_event event)
455 if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
456 return ap_jumptable[aq->sm_state][event](aq);
458 return AP_SM_WAIT_NONE;
461 enum ap_sm_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_sm_event event)
463 enum ap_sm_wait wait;
465 while ((wait = ap_sm_event(aq, event)) == AP_SM_WAIT_AGAIN)
471 * AP queue related attributes.
473 static ssize_t request_count_show(struct device *dev,
474 struct device_attribute *attr,
477 struct ap_queue *aq = to_ap_queue(dev);
481 spin_lock_bh(&aq->lock);
482 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
483 req_cnt = aq->total_request_count;
486 spin_unlock_bh(&aq->lock);
489 return scnprintf(buf, PAGE_SIZE, "%llu\n", req_cnt);
491 return scnprintf(buf, PAGE_SIZE, "-\n");
494 static ssize_t request_count_store(struct device *dev,
495 struct device_attribute *attr,
496 const char *buf, size_t count)
498 struct ap_queue *aq = to_ap_queue(dev);
500 spin_lock_bh(&aq->lock);
501 aq->total_request_count = 0;
502 spin_unlock_bh(&aq->lock);
507 static DEVICE_ATTR_RW(request_count);
509 static ssize_t requestq_count_show(struct device *dev,
510 struct device_attribute *attr, char *buf)
512 struct ap_queue *aq = to_ap_queue(dev);
513 unsigned int reqq_cnt = 0;
515 spin_lock_bh(&aq->lock);
516 if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
517 reqq_cnt = aq->requestq_count;
518 spin_unlock_bh(&aq->lock);
519 return scnprintf(buf, PAGE_SIZE, "%d\n", reqq_cnt);
522 static DEVICE_ATTR_RO(requestq_count);
524 static ssize_t pendingq_count_show(struct device *dev,
525 struct device_attribute *attr, char *buf)
527 struct ap_queue *aq = to_ap_queue(dev);
528 unsigned int penq_cnt = 0;
530 spin_lock_bh(&aq->lock);
531 if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
532 penq_cnt = aq->pendingq_count;
533 spin_unlock_bh(&aq->lock);
534 return scnprintf(buf, PAGE_SIZE, "%d\n", penq_cnt);
537 static DEVICE_ATTR_RO(pendingq_count);
539 static ssize_t reset_show(struct device *dev,
540 struct device_attribute *attr, char *buf)
542 struct ap_queue *aq = to_ap_queue(dev);
545 spin_lock_bh(&aq->lock);
546 switch (aq->sm_state) {
547 case AP_SM_STATE_RESET_START:
548 case AP_SM_STATE_RESET_WAIT:
549 rc = scnprintf(buf, PAGE_SIZE, "Reset in progress.\n");
551 case AP_SM_STATE_WORKING:
552 case AP_SM_STATE_QUEUE_FULL:
553 rc = scnprintf(buf, PAGE_SIZE, "Reset Timer armed.\n");
556 rc = scnprintf(buf, PAGE_SIZE, "No Reset Timer set.\n");
558 spin_unlock_bh(&aq->lock);
562 static ssize_t reset_store(struct device *dev,
563 struct device_attribute *attr,
564 const char *buf, size_t count)
566 struct ap_queue *aq = to_ap_queue(dev);
568 spin_lock_bh(&aq->lock);
569 __ap_flush_queue(aq);
570 aq->sm_state = AP_SM_STATE_RESET_START;
571 ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
572 spin_unlock_bh(&aq->lock);
574 AP_DBF(DBF_INFO, "reset queue=%02x.%04x triggered by user\n",
575 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
580 static DEVICE_ATTR_RW(reset);
582 static ssize_t interrupt_show(struct device *dev,
583 struct device_attribute *attr, char *buf)
585 struct ap_queue *aq = to_ap_queue(dev);
588 spin_lock_bh(&aq->lock);
589 if (aq->sm_state == AP_SM_STATE_SETIRQ_WAIT)
590 rc = scnprintf(buf, PAGE_SIZE, "Enable Interrupt pending.\n");
591 else if (aq->interrupt)
592 rc = scnprintf(buf, PAGE_SIZE, "Interrupts enabled.\n");
594 rc = scnprintf(buf, PAGE_SIZE, "Interrupts disabled.\n");
595 spin_unlock_bh(&aq->lock);
599 static DEVICE_ATTR_RO(interrupt);
601 static ssize_t config_show(struct device *dev,
602 struct device_attribute *attr, char *buf)
604 struct ap_queue *aq = to_ap_queue(dev);
607 spin_lock_bh(&aq->lock);
608 rc = scnprintf(buf, PAGE_SIZE, "%d\n", aq->config ? 1 : 0);
609 spin_unlock_bh(&aq->lock);
613 static DEVICE_ATTR_RO(config);
615 #ifdef CONFIG_ZCRYPT_DEBUG
616 static ssize_t states_show(struct device *dev,
617 struct device_attribute *attr, char *buf)
619 struct ap_queue *aq = to_ap_queue(dev);
622 spin_lock_bh(&aq->lock);
623 /* queue device state */
624 switch (aq->dev_state) {
625 case AP_DEV_STATE_UNINITIATED:
626 rc = scnprintf(buf, PAGE_SIZE, "UNINITIATED\n");
628 case AP_DEV_STATE_OPERATING:
629 rc = scnprintf(buf, PAGE_SIZE, "OPERATING");
631 case AP_DEV_STATE_SHUTDOWN:
632 rc = scnprintf(buf, PAGE_SIZE, "SHUTDOWN");
634 case AP_DEV_STATE_ERROR:
635 rc = scnprintf(buf, PAGE_SIZE, "ERROR");
638 rc = scnprintf(buf, PAGE_SIZE, "UNKNOWN");
640 /* state machine state */
642 switch (aq->sm_state) {
643 case AP_SM_STATE_RESET_START:
644 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
647 case AP_SM_STATE_RESET_WAIT:
648 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
651 case AP_SM_STATE_SETIRQ_WAIT:
652 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
655 case AP_SM_STATE_IDLE:
656 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
659 case AP_SM_STATE_WORKING:
660 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
663 case AP_SM_STATE_QUEUE_FULL:
664 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
668 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
672 spin_unlock_bh(&aq->lock);
676 static DEVICE_ATTR_RO(states);
678 static ssize_t last_err_rc_show(struct device *dev,
679 struct device_attribute *attr, char *buf)
681 struct ap_queue *aq = to_ap_queue(dev);
684 spin_lock_bh(&aq->lock);
685 rc = aq->last_err_rc;
686 spin_unlock_bh(&aq->lock);
689 case AP_RESPONSE_NORMAL:
690 return scnprintf(buf, PAGE_SIZE, "NORMAL\n");
691 case AP_RESPONSE_Q_NOT_AVAIL:
692 return scnprintf(buf, PAGE_SIZE, "Q_NOT_AVAIL\n");
693 case AP_RESPONSE_RESET_IN_PROGRESS:
694 return scnprintf(buf, PAGE_SIZE, "RESET_IN_PROGRESS\n");
695 case AP_RESPONSE_DECONFIGURED:
696 return scnprintf(buf, PAGE_SIZE, "DECONFIGURED\n");
697 case AP_RESPONSE_CHECKSTOPPED:
698 return scnprintf(buf, PAGE_SIZE, "CHECKSTOPPED\n");
699 case AP_RESPONSE_BUSY:
700 return scnprintf(buf, PAGE_SIZE, "BUSY\n");
701 case AP_RESPONSE_INVALID_ADDRESS:
702 return scnprintf(buf, PAGE_SIZE, "INVALID_ADDRESS\n");
703 case AP_RESPONSE_OTHERWISE_CHANGED:
704 return scnprintf(buf, PAGE_SIZE, "OTHERWISE_CHANGED\n");
705 case AP_RESPONSE_Q_FULL:
706 return scnprintf(buf, PAGE_SIZE, "Q_FULL/NO_PENDING_REPLY\n");
707 case AP_RESPONSE_INDEX_TOO_BIG:
708 return scnprintf(buf, PAGE_SIZE, "INDEX_TOO_BIG\n");
709 case AP_RESPONSE_NO_FIRST_PART:
710 return scnprintf(buf, PAGE_SIZE, "NO_FIRST_PART\n");
711 case AP_RESPONSE_MESSAGE_TOO_BIG:
712 return scnprintf(buf, PAGE_SIZE, "MESSAGE_TOO_BIG\n");
713 case AP_RESPONSE_REQ_FAC_NOT_INST:
714 return scnprintf(buf, PAGE_SIZE, "REQ_FAC_NOT_INST\n");
716 return scnprintf(buf, PAGE_SIZE, "response code %d\n", rc);
719 static DEVICE_ATTR_RO(last_err_rc);
722 static struct attribute *ap_queue_dev_attrs[] = {
723 &dev_attr_request_count.attr,
724 &dev_attr_requestq_count.attr,
725 &dev_attr_pendingq_count.attr,
726 &dev_attr_reset.attr,
727 &dev_attr_interrupt.attr,
728 &dev_attr_config.attr,
729 #ifdef CONFIG_ZCRYPT_DEBUG
730 &dev_attr_states.attr,
731 &dev_attr_last_err_rc.attr,
736 static struct attribute_group ap_queue_dev_attr_group = {
737 .attrs = ap_queue_dev_attrs
740 static const struct attribute_group *ap_queue_dev_attr_groups[] = {
741 &ap_queue_dev_attr_group,
745 static struct device_type ap_queue_type = {
747 .groups = ap_queue_dev_attr_groups,
750 static void ap_queue_device_release(struct device *dev)
752 struct ap_queue *aq = to_ap_queue(dev);
754 spin_lock_bh(&ap_queues_lock);
755 hash_del(&aq->hnode);
756 spin_unlock_bh(&ap_queues_lock);
761 struct ap_queue *ap_queue_create(ap_qid_t qid, int device_type)
765 aq = kzalloc(sizeof(*aq), GFP_KERNEL);
768 aq->ap_dev.device.release = ap_queue_device_release;
769 aq->ap_dev.device.type = &ap_queue_type;
770 aq->ap_dev.device_type = device_type;
772 aq->interrupt = false;
773 spin_lock_init(&aq->lock);
774 INIT_LIST_HEAD(&aq->pendingq);
775 INIT_LIST_HEAD(&aq->requestq);
776 timer_setup(&aq->timeout, ap_request_timeout, 0);
781 void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *reply)
785 spin_lock_bh(&aq->lock);
786 ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
787 spin_unlock_bh(&aq->lock);
789 EXPORT_SYMBOL(ap_queue_init_reply);
792 * ap_queue_message(): Queue a request to an AP device.
793 * @aq: The AP device to queue the message to
794 * @ap_msg: The message that is to be added
796 int ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg)
800 /* msg needs to have a valid receive-callback */
801 BUG_ON(!ap_msg->receive);
803 spin_lock_bh(&aq->lock);
805 /* only allow to queue new messages if device state is ok */
806 if (aq->dev_state == AP_DEV_STATE_OPERATING) {
807 list_add_tail(&ap_msg->list, &aq->requestq);
808 aq->requestq_count++;
809 aq->total_request_count++;
810 atomic64_inc(&aq->card->total_request_count);
814 /* Send/receive as many request from the queue as possible. */
815 ap_wait(ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
817 spin_unlock_bh(&aq->lock);
821 EXPORT_SYMBOL(ap_queue_message);
824 * ap_cancel_message(): Cancel a crypto request.
825 * @aq: The AP device that has the message queued
826 * @ap_msg: The message that is to be removed
828 * Cancel a crypto request. This is done by removing the request
829 * from the device pending or request queue. Note that the
830 * request stays on the AP queue. When it finishes the message
831 * reply will be discarded because the psmid can't be found.
833 void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg)
835 struct ap_message *tmp;
837 spin_lock_bh(&aq->lock);
838 if (!list_empty(&ap_msg->list)) {
839 list_for_each_entry(tmp, &aq->pendingq, list)
840 if (tmp->psmid == ap_msg->psmid) {
841 aq->pendingq_count--;
844 aq->requestq_count--;
846 list_del_init(&ap_msg->list);
848 spin_unlock_bh(&aq->lock);
850 EXPORT_SYMBOL(ap_cancel_message);
853 * __ap_flush_queue(): Flush requests.
854 * @aq: Pointer to the AP queue
856 * Flush all requests from the request/pending queue of an AP device.
858 static void __ap_flush_queue(struct ap_queue *aq)
860 struct ap_message *ap_msg, *next;
862 list_for_each_entry_safe(ap_msg, next, &aq->pendingq, list) {
863 list_del_init(&ap_msg->list);
864 aq->pendingq_count--;
865 ap_msg->rc = -EAGAIN;
866 ap_msg->receive(aq, ap_msg, NULL);
868 list_for_each_entry_safe(ap_msg, next, &aq->requestq, list) {
869 list_del_init(&ap_msg->list);
870 aq->requestq_count--;
871 ap_msg->rc = -EAGAIN;
872 ap_msg->receive(aq, ap_msg, NULL);
877 void ap_flush_queue(struct ap_queue *aq)
879 spin_lock_bh(&aq->lock);
880 __ap_flush_queue(aq);
881 spin_unlock_bh(&aq->lock);
883 EXPORT_SYMBOL(ap_flush_queue);
885 void ap_queue_prepare_remove(struct ap_queue *aq)
887 spin_lock_bh(&aq->lock);
889 __ap_flush_queue(aq);
890 /* move queue device state to SHUTDOWN in progress */
891 aq->dev_state = AP_DEV_STATE_SHUTDOWN;
892 spin_unlock_bh(&aq->lock);
893 del_timer_sync(&aq->timeout);
896 void ap_queue_remove(struct ap_queue *aq)
899 * all messages have been flushed and the device state
900 * is SHUTDOWN. Now reset with zero which also clears
901 * the irq registration and move the device state
902 * to the initial value AP_DEV_STATE_UNINITIATED.
904 spin_lock_bh(&aq->lock);
906 aq->dev_state = AP_DEV_STATE_UNINITIATED;
907 spin_unlock_bh(&aq->lock);
910 void ap_queue_init_state(struct ap_queue *aq)
912 spin_lock_bh(&aq->lock);
913 aq->dev_state = AP_DEV_STATE_OPERATING;
914 aq->sm_state = AP_SM_STATE_RESET_START;
915 ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
916 spin_unlock_bh(&aq->lock);
918 EXPORT_SYMBOL(ap_queue_init_state);