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_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 status = ap_dqap(aq->qid, &aq->reply->psmid,
141 aq->reply->msg, aq->reply->len);
142 switch (status.response_code) {
143 case AP_RESPONSE_NORMAL:
144 aq->queue_count = max_t(int, 0, aq->queue_count - 1);
145 if (aq->queue_count > 0)
146 mod_timer(&aq->timeout,
147 jiffies + aq->request_timeout);
148 list_for_each_entry(ap_msg, &aq->pendingq, list) {
149 if (ap_msg->psmid != aq->reply->psmid)
151 list_del_init(&ap_msg->list);
152 aq->pendingq_count--;
153 ap_msg->receive(aq, ap_msg, aq->reply);
158 AP_DBF_WARN("%s unassociated reply psmid=0x%016llx on 0x%02x.%04x\n",
159 __func__, aq->reply->psmid,
160 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
163 case AP_RESPONSE_NO_PENDING_REPLY:
164 if (!status.queue_empty || aq->queue_count <= 0)
166 /* The card shouldn't forget requests but who knows. */
168 list_splice_init(&aq->pendingq, &aq->requestq);
169 aq->requestq_count += aq->pendingq_count;
170 aq->pendingq_count = 0;
179 * ap_sm_read(): Receive pending reply messages from an AP queue.
180 * @aq: pointer to the AP queue
182 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
184 static enum ap_sm_wait ap_sm_read(struct ap_queue *aq)
186 struct ap_queue_status status;
189 return AP_SM_WAIT_NONE;
190 status = ap_sm_recv(aq);
191 switch (status.response_code) {
192 case AP_RESPONSE_NORMAL:
193 if (aq->queue_count > 0) {
194 aq->sm_state = AP_SM_STATE_WORKING;
195 return AP_SM_WAIT_AGAIN;
197 aq->sm_state = AP_SM_STATE_IDLE;
198 return AP_SM_WAIT_NONE;
199 case AP_RESPONSE_NO_PENDING_REPLY:
200 if (aq->queue_count > 0)
201 return AP_SM_WAIT_INTERRUPT;
202 aq->sm_state = AP_SM_STATE_IDLE;
203 return AP_SM_WAIT_NONE;
205 aq->dev_state = AP_DEV_STATE_ERROR;
206 aq->last_err_rc = status.response_code;
207 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
208 __func__, status.response_code,
209 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
210 return AP_SM_WAIT_NONE;
215 * ap_sm_write(): Send messages from the request queue to an AP queue.
216 * @aq: pointer to the AP queue
218 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
220 static enum ap_sm_wait ap_sm_write(struct ap_queue *aq)
222 struct ap_queue_status status;
223 struct ap_message *ap_msg;
224 ap_qid_t qid = aq->qid;
226 if (aq->requestq_count <= 0)
227 return AP_SM_WAIT_NONE;
228 /* Start the next request on the queue. */
229 ap_msg = list_entry(aq->requestq.next, struct ap_message, list);
230 #ifdef CONFIG_ZCRYPT_DEBUG
231 if (ap_msg->fi.action == AP_FI_ACTION_NQAP_QID_INVAL) {
232 AP_DBF_WARN("%s fi cmd 0x%04x: forcing invalid qid 0xFF00\n",
233 __func__, ap_msg->fi.cmd);
237 status = __ap_send(qid, ap_msg->psmid,
238 ap_msg->msg, ap_msg->len,
239 ap_msg->flags & AP_MSG_FLAG_SPECIAL);
240 switch (status.response_code) {
241 case AP_RESPONSE_NORMAL:
242 aq->queue_count = max_t(int, 1, aq->queue_count + 1);
243 if (aq->queue_count == 1)
244 mod_timer(&aq->timeout, jiffies + aq->request_timeout);
245 list_move_tail(&ap_msg->list, &aq->pendingq);
246 aq->requestq_count--;
247 aq->pendingq_count++;
248 if (aq->queue_count < aq->card->queue_depth) {
249 aq->sm_state = AP_SM_STATE_WORKING;
250 return AP_SM_WAIT_AGAIN;
253 case AP_RESPONSE_Q_FULL:
254 aq->sm_state = AP_SM_STATE_QUEUE_FULL;
255 return AP_SM_WAIT_INTERRUPT;
256 case AP_RESPONSE_RESET_IN_PROGRESS:
257 aq->sm_state = AP_SM_STATE_RESET_WAIT;
258 return AP_SM_WAIT_TIMEOUT;
259 case AP_RESPONSE_INVALID_DOMAIN:
260 AP_DBF(DBF_WARN, "AP_RESPONSE_INVALID_DOMAIN on NQAP\n");
262 case AP_RESPONSE_MESSAGE_TOO_BIG:
263 case AP_RESPONSE_REQ_FAC_NOT_INST:
264 list_del_init(&ap_msg->list);
265 aq->requestq_count--;
266 ap_msg->rc = -EINVAL;
267 ap_msg->receive(aq, ap_msg, NULL);
268 return AP_SM_WAIT_AGAIN;
270 aq->dev_state = AP_DEV_STATE_ERROR;
271 aq->last_err_rc = status.response_code;
272 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
273 __func__, status.response_code,
274 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
275 return AP_SM_WAIT_NONE;
280 * ap_sm_read_write(): Send and receive messages to/from an AP queue.
281 * @aq: pointer to the AP queue
283 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
285 static enum ap_sm_wait ap_sm_read_write(struct ap_queue *aq)
287 return min(ap_sm_read(aq), ap_sm_write(aq));
291 * ap_sm_reset(): Reset an AP queue.
292 * @qid: The AP queue number
294 * Submit the Reset command to an AP queue.
296 static enum ap_sm_wait ap_sm_reset(struct ap_queue *aq)
298 struct ap_queue_status status;
300 status = ap_rapq(aq->qid);
301 switch (status.response_code) {
302 case AP_RESPONSE_NORMAL:
303 case AP_RESPONSE_RESET_IN_PROGRESS:
304 aq->sm_state = AP_SM_STATE_RESET_WAIT;
305 aq->interrupt = AP_INTR_DISABLED;
306 return AP_SM_WAIT_TIMEOUT;
308 aq->dev_state = AP_DEV_STATE_ERROR;
309 aq->last_err_rc = status.response_code;
310 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
311 __func__, status.response_code,
312 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
313 return AP_SM_WAIT_NONE;
318 * ap_sm_reset_wait(): Test queue for completion of the reset operation
319 * @aq: pointer to the AP queue
321 * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
323 static enum ap_sm_wait ap_sm_reset_wait(struct ap_queue *aq)
325 struct ap_queue_status status;
328 if (aq->queue_count > 0 && aq->reply)
329 /* Try to read a completed message and get the status */
330 status = ap_sm_recv(aq);
332 /* Get the status with TAPQ */
333 status = ap_tapq(aq->qid, NULL);
335 switch (status.response_code) {
336 case AP_RESPONSE_NORMAL:
337 lsi_ptr = ap_airq_ptr();
338 if (lsi_ptr && ap_queue_enable_interruption(aq, lsi_ptr) == 0)
339 aq->sm_state = AP_SM_STATE_SETIRQ_WAIT;
341 aq->sm_state = (aq->queue_count > 0) ?
342 AP_SM_STATE_WORKING : AP_SM_STATE_IDLE;
343 return AP_SM_WAIT_AGAIN;
344 case AP_RESPONSE_BUSY:
345 case AP_RESPONSE_RESET_IN_PROGRESS:
346 return AP_SM_WAIT_TIMEOUT;
347 case AP_RESPONSE_Q_NOT_AVAIL:
348 case AP_RESPONSE_DECONFIGURED:
349 case AP_RESPONSE_CHECKSTOPPED:
351 aq->dev_state = AP_DEV_STATE_ERROR;
352 aq->last_err_rc = status.response_code;
353 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
354 __func__, status.response_code,
355 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
356 return AP_SM_WAIT_NONE;
361 * ap_sm_setirq_wait(): Test queue for completion of the irq enablement
362 * @aq: pointer to the AP queue
364 * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
366 static enum ap_sm_wait ap_sm_setirq_wait(struct ap_queue *aq)
368 struct ap_queue_status status;
370 if (aq->queue_count > 0 && aq->reply)
371 /* Try to read a completed message and get the status */
372 status = ap_sm_recv(aq);
374 /* Get the status with TAPQ */
375 status = ap_tapq(aq->qid, NULL);
377 if (status.irq_enabled == 1) {
378 /* Irqs are now enabled */
379 aq->interrupt = AP_INTR_ENABLED;
380 aq->sm_state = (aq->queue_count > 0) ?
381 AP_SM_STATE_WORKING : AP_SM_STATE_IDLE;
384 switch (status.response_code) {
385 case AP_RESPONSE_NORMAL:
386 if (aq->queue_count > 0)
387 return AP_SM_WAIT_AGAIN;
389 case AP_RESPONSE_NO_PENDING_REPLY:
390 return AP_SM_WAIT_TIMEOUT;
392 aq->dev_state = AP_DEV_STATE_ERROR;
393 aq->last_err_rc = status.response_code;
394 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
395 __func__, status.response_code,
396 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
397 return AP_SM_WAIT_NONE;
402 * AP state machine jump table
404 static ap_func_t *ap_jumptable[NR_AP_SM_STATES][NR_AP_SM_EVENTS] = {
405 [AP_SM_STATE_RESET_START] = {
406 [AP_SM_EVENT_POLL] = ap_sm_reset,
407 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
409 [AP_SM_STATE_RESET_WAIT] = {
410 [AP_SM_EVENT_POLL] = ap_sm_reset_wait,
411 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
413 [AP_SM_STATE_SETIRQ_WAIT] = {
414 [AP_SM_EVENT_POLL] = ap_sm_setirq_wait,
415 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
417 [AP_SM_STATE_IDLE] = {
418 [AP_SM_EVENT_POLL] = ap_sm_write,
419 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
421 [AP_SM_STATE_WORKING] = {
422 [AP_SM_EVENT_POLL] = ap_sm_read_write,
423 [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
425 [AP_SM_STATE_QUEUE_FULL] = {
426 [AP_SM_EVENT_POLL] = ap_sm_read,
427 [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
431 enum ap_sm_wait ap_sm_event(struct ap_queue *aq, enum ap_sm_event event)
433 if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
434 return ap_jumptable[aq->sm_state][event](aq);
436 return AP_SM_WAIT_NONE;
439 enum ap_sm_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_sm_event event)
441 enum ap_sm_wait wait;
443 while ((wait = ap_sm_event(aq, event)) == AP_SM_WAIT_AGAIN)
449 * AP queue related attributes.
451 static ssize_t request_count_show(struct device *dev,
452 struct device_attribute *attr,
455 struct ap_queue *aq = to_ap_queue(dev);
459 spin_lock_bh(&aq->lock);
460 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
461 req_cnt = aq->total_request_count;
464 spin_unlock_bh(&aq->lock);
467 return scnprintf(buf, PAGE_SIZE, "%llu\n", req_cnt);
469 return scnprintf(buf, PAGE_SIZE, "-\n");
472 static ssize_t request_count_store(struct device *dev,
473 struct device_attribute *attr,
474 const char *buf, size_t count)
476 struct ap_queue *aq = to_ap_queue(dev);
478 spin_lock_bh(&aq->lock);
479 aq->total_request_count = 0;
480 spin_unlock_bh(&aq->lock);
485 static DEVICE_ATTR_RW(request_count);
487 static ssize_t requestq_count_show(struct device *dev,
488 struct device_attribute *attr, char *buf)
490 struct ap_queue *aq = to_ap_queue(dev);
491 unsigned int reqq_cnt = 0;
493 spin_lock_bh(&aq->lock);
494 if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
495 reqq_cnt = aq->requestq_count;
496 spin_unlock_bh(&aq->lock);
497 return scnprintf(buf, PAGE_SIZE, "%d\n", reqq_cnt);
500 static DEVICE_ATTR_RO(requestq_count);
502 static ssize_t pendingq_count_show(struct device *dev,
503 struct device_attribute *attr, char *buf)
505 struct ap_queue *aq = to_ap_queue(dev);
506 unsigned int penq_cnt = 0;
508 spin_lock_bh(&aq->lock);
509 if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
510 penq_cnt = aq->pendingq_count;
511 spin_unlock_bh(&aq->lock);
512 return scnprintf(buf, PAGE_SIZE, "%d\n", penq_cnt);
515 static DEVICE_ATTR_RO(pendingq_count);
517 static ssize_t reset_show(struct device *dev,
518 struct device_attribute *attr, char *buf)
520 struct ap_queue *aq = to_ap_queue(dev);
523 spin_lock_bh(&aq->lock);
524 switch (aq->sm_state) {
525 case AP_SM_STATE_RESET_START:
526 case AP_SM_STATE_RESET_WAIT:
527 rc = scnprintf(buf, PAGE_SIZE, "Reset in progress.\n");
529 case AP_SM_STATE_WORKING:
530 case AP_SM_STATE_QUEUE_FULL:
531 rc = scnprintf(buf, PAGE_SIZE, "Reset Timer armed.\n");
534 rc = scnprintf(buf, PAGE_SIZE, "No Reset Timer set.\n");
536 spin_unlock_bh(&aq->lock);
540 static ssize_t reset_store(struct device *dev,
541 struct device_attribute *attr,
542 const char *buf, size_t count)
544 struct ap_queue *aq = to_ap_queue(dev);
546 spin_lock_bh(&aq->lock);
547 __ap_flush_queue(aq);
548 aq->sm_state = AP_SM_STATE_RESET_START;
549 ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
550 spin_unlock_bh(&aq->lock);
552 AP_DBF(DBF_INFO, "reset queue=%02x.%04x triggered by user\n",
553 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
558 static DEVICE_ATTR_RW(reset);
560 static ssize_t interrupt_show(struct device *dev,
561 struct device_attribute *attr, char *buf)
563 struct ap_queue *aq = to_ap_queue(dev);
566 spin_lock_bh(&aq->lock);
567 if (aq->sm_state == AP_SM_STATE_SETIRQ_WAIT)
568 rc = scnprintf(buf, PAGE_SIZE, "Enable Interrupt pending.\n");
569 else if (aq->interrupt == AP_INTR_ENABLED)
570 rc = scnprintf(buf, PAGE_SIZE, "Interrupts enabled.\n");
572 rc = scnprintf(buf, PAGE_SIZE, "Interrupts disabled.\n");
573 spin_unlock_bh(&aq->lock);
577 static DEVICE_ATTR_RO(interrupt);
579 static ssize_t config_show(struct device *dev,
580 struct device_attribute *attr, char *buf)
582 struct ap_queue *aq = to_ap_queue(dev);
585 spin_lock_bh(&aq->lock);
586 rc = scnprintf(buf, PAGE_SIZE, "%d\n", aq->config ? 1 : 0);
587 spin_unlock_bh(&aq->lock);
591 static DEVICE_ATTR_RO(config);
593 #ifdef CONFIG_ZCRYPT_DEBUG
594 static ssize_t states_show(struct device *dev,
595 struct device_attribute *attr, char *buf)
597 struct ap_queue *aq = to_ap_queue(dev);
600 spin_lock_bh(&aq->lock);
601 /* queue device state */
602 switch (aq->dev_state) {
603 case AP_DEV_STATE_UNINITIATED:
604 rc = scnprintf(buf, PAGE_SIZE, "UNINITIATED\n");
606 case AP_DEV_STATE_OPERATING:
607 rc = scnprintf(buf, PAGE_SIZE, "OPERATING");
609 case AP_DEV_STATE_SHUTDOWN:
610 rc = scnprintf(buf, PAGE_SIZE, "SHUTDOWN");
612 case AP_DEV_STATE_ERROR:
613 rc = scnprintf(buf, PAGE_SIZE, "ERROR");
616 rc = scnprintf(buf, PAGE_SIZE, "UNKNOWN");
618 /* state machine state */
620 switch (aq->sm_state) {
621 case AP_SM_STATE_RESET_START:
622 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
625 case AP_SM_STATE_RESET_WAIT:
626 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
629 case AP_SM_STATE_SETIRQ_WAIT:
630 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
633 case AP_SM_STATE_IDLE:
634 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
637 case AP_SM_STATE_WORKING:
638 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
641 case AP_SM_STATE_QUEUE_FULL:
642 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
646 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
650 spin_unlock_bh(&aq->lock);
654 static DEVICE_ATTR_RO(states);
656 static ssize_t last_err_rc_show(struct device *dev,
657 struct device_attribute *attr, char *buf)
659 struct ap_queue *aq = to_ap_queue(dev);
662 spin_lock_bh(&aq->lock);
663 rc = aq->last_err_rc;
664 spin_unlock_bh(&aq->lock);
667 case AP_RESPONSE_NORMAL:
668 return scnprintf(buf, PAGE_SIZE, "NORMAL\n");
669 case AP_RESPONSE_Q_NOT_AVAIL:
670 return scnprintf(buf, PAGE_SIZE, "Q_NOT_AVAIL\n");
671 case AP_RESPONSE_RESET_IN_PROGRESS:
672 return scnprintf(buf, PAGE_SIZE, "RESET_IN_PROGRESS\n");
673 case AP_RESPONSE_DECONFIGURED:
674 return scnprintf(buf, PAGE_SIZE, "DECONFIGURED\n");
675 case AP_RESPONSE_CHECKSTOPPED:
676 return scnprintf(buf, PAGE_SIZE, "CHECKSTOPPED\n");
677 case AP_RESPONSE_BUSY:
678 return scnprintf(buf, PAGE_SIZE, "BUSY\n");
679 case AP_RESPONSE_INVALID_ADDRESS:
680 return scnprintf(buf, PAGE_SIZE, "INVALID_ADDRESS\n");
681 case AP_RESPONSE_OTHERWISE_CHANGED:
682 return scnprintf(buf, PAGE_SIZE, "OTHERWISE_CHANGED\n");
683 case AP_RESPONSE_Q_FULL:
684 return scnprintf(buf, PAGE_SIZE, "Q_FULL/NO_PENDING_REPLY\n");
685 case AP_RESPONSE_INDEX_TOO_BIG:
686 return scnprintf(buf, PAGE_SIZE, "INDEX_TOO_BIG\n");
687 case AP_RESPONSE_NO_FIRST_PART:
688 return scnprintf(buf, PAGE_SIZE, "NO_FIRST_PART\n");
689 case AP_RESPONSE_MESSAGE_TOO_BIG:
690 return scnprintf(buf, PAGE_SIZE, "MESSAGE_TOO_BIG\n");
691 case AP_RESPONSE_REQ_FAC_NOT_INST:
692 return scnprintf(buf, PAGE_SIZE, "REQ_FAC_NOT_INST\n");
694 return scnprintf(buf, PAGE_SIZE, "response code %d\n", rc);
697 static DEVICE_ATTR_RO(last_err_rc);
700 static struct attribute *ap_queue_dev_attrs[] = {
701 &dev_attr_request_count.attr,
702 &dev_attr_requestq_count.attr,
703 &dev_attr_pendingq_count.attr,
704 &dev_attr_reset.attr,
705 &dev_attr_interrupt.attr,
706 &dev_attr_config.attr,
707 #ifdef CONFIG_ZCRYPT_DEBUG
708 &dev_attr_states.attr,
709 &dev_attr_last_err_rc.attr,
714 static struct attribute_group ap_queue_dev_attr_group = {
715 .attrs = ap_queue_dev_attrs
718 static const struct attribute_group *ap_queue_dev_attr_groups[] = {
719 &ap_queue_dev_attr_group,
723 static struct device_type ap_queue_type = {
725 .groups = ap_queue_dev_attr_groups,
728 static void ap_queue_device_release(struct device *dev)
730 struct ap_queue *aq = to_ap_queue(dev);
732 spin_lock_bh(&ap_queues_lock);
733 hash_del(&aq->hnode);
734 spin_unlock_bh(&ap_queues_lock);
739 struct ap_queue *ap_queue_create(ap_qid_t qid, int device_type)
743 aq = kzalloc(sizeof(*aq), GFP_KERNEL);
746 aq->ap_dev.device.release = ap_queue_device_release;
747 aq->ap_dev.device.type = &ap_queue_type;
748 aq->ap_dev.device_type = device_type;
750 aq->interrupt = AP_INTR_DISABLED;
751 spin_lock_init(&aq->lock);
752 INIT_LIST_HEAD(&aq->pendingq);
753 INIT_LIST_HEAD(&aq->requestq);
754 timer_setup(&aq->timeout, ap_request_timeout, 0);
759 void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *reply)
763 spin_lock_bh(&aq->lock);
764 ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
765 spin_unlock_bh(&aq->lock);
767 EXPORT_SYMBOL(ap_queue_init_reply);
770 * ap_queue_message(): Queue a request to an AP device.
771 * @aq: The AP device to queue the message to
772 * @ap_msg: The message that is to be added
774 int ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg)
778 /* msg needs to have a valid receive-callback */
779 BUG_ON(!ap_msg->receive);
781 spin_lock_bh(&aq->lock);
783 /* only allow to queue new messages if device state is ok */
784 if (aq->dev_state == AP_DEV_STATE_OPERATING) {
785 list_add_tail(&ap_msg->list, &aq->requestq);
786 aq->requestq_count++;
787 aq->total_request_count++;
788 atomic64_inc(&aq->card->total_request_count);
792 /* Send/receive as many request from the queue as possible. */
793 ap_wait(ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
795 spin_unlock_bh(&aq->lock);
799 EXPORT_SYMBOL(ap_queue_message);
802 * ap_cancel_message(): Cancel a crypto request.
803 * @aq: The AP device that has the message queued
804 * @ap_msg: The message that is to be removed
806 * Cancel a crypto request. This is done by removing the request
807 * from the device pending or request queue. Note that the
808 * request stays on the AP queue. When it finishes the message
809 * reply will be discarded because the psmid can't be found.
811 void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg)
813 struct ap_message *tmp;
815 spin_lock_bh(&aq->lock);
816 if (!list_empty(&ap_msg->list)) {
817 list_for_each_entry(tmp, &aq->pendingq, list)
818 if (tmp->psmid == ap_msg->psmid) {
819 aq->pendingq_count--;
822 aq->requestq_count--;
824 list_del_init(&ap_msg->list);
826 spin_unlock_bh(&aq->lock);
828 EXPORT_SYMBOL(ap_cancel_message);
831 * __ap_flush_queue(): Flush requests.
832 * @aq: Pointer to the AP queue
834 * Flush all requests from the request/pending queue of an AP device.
836 static void __ap_flush_queue(struct ap_queue *aq)
838 struct ap_message *ap_msg, *next;
840 list_for_each_entry_safe(ap_msg, next, &aq->pendingq, list) {
841 list_del_init(&ap_msg->list);
842 aq->pendingq_count--;
843 ap_msg->rc = -EAGAIN;
844 ap_msg->receive(aq, ap_msg, NULL);
846 list_for_each_entry_safe(ap_msg, next, &aq->requestq, list) {
847 list_del_init(&ap_msg->list);
848 aq->requestq_count--;
849 ap_msg->rc = -EAGAIN;
850 ap_msg->receive(aq, ap_msg, NULL);
855 void ap_flush_queue(struct ap_queue *aq)
857 spin_lock_bh(&aq->lock);
858 __ap_flush_queue(aq);
859 spin_unlock_bh(&aq->lock);
861 EXPORT_SYMBOL(ap_flush_queue);
863 void ap_queue_prepare_remove(struct ap_queue *aq)
865 spin_lock_bh(&aq->lock);
867 __ap_flush_queue(aq);
868 /* move queue device state to SHUTDOWN in progress */
869 aq->dev_state = AP_DEV_STATE_SHUTDOWN;
870 spin_unlock_bh(&aq->lock);
871 del_timer_sync(&aq->timeout);
874 void ap_queue_remove(struct ap_queue *aq)
877 * all messages have been flushed and the device state
878 * is SHUTDOWN. Now reset with zero which also clears
879 * the irq registration and move the device state
880 * to the initial value AP_DEV_STATE_UNINITIATED.
882 spin_lock_bh(&aq->lock);
884 aq->dev_state = AP_DEV_STATE_UNINITIATED;
885 spin_unlock_bh(&aq->lock);
888 void ap_queue_init_state(struct ap_queue *aq)
890 spin_lock_bh(&aq->lock);
891 aq->dev_state = AP_DEV_STATE_OPERATING;
892 aq->sm_state = AP_SM_STATE_RESET_START;
893 ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
894 spin_unlock_bh(&aq->lock);
896 EXPORT_SYMBOL(ap_queue_init_state);