Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright IBM Corp. 2016
0004  * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
0005  *
0006  * Adjunct processor bus, queue related code.
0007  */
0008 
0009 #define KMSG_COMPONENT "ap"
0010 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
0011 
0012 #include <linux/init.h>
0013 #include <linux/slab.h>
0014 #include <asm/facility.h>
0015 
0016 #include "ap_bus.h"
0017 #include "ap_debug.h"
0018 
0019 static void __ap_flush_queue(struct ap_queue *aq);
0020 
0021 /**
0022  * ap_queue_enable_irq(): Enable interrupt support on this AP queue.
0023  * @aq: The AP queue
0024  * @ind: the notification indicator byte
0025  *
0026  * Enables interruption on AP queue via ap_aqic(). Based on the return
0027  * value it waits a while and tests the AP queue if interrupts
0028  * have been switched on using ap_test_queue().
0029  */
0030 static int ap_queue_enable_irq(struct ap_queue *aq, void *ind)
0031 {
0032     struct ap_queue_status status;
0033     struct ap_qirq_ctrl qirqctrl = { 0 };
0034 
0035     qirqctrl.ir = 1;
0036     qirqctrl.isc = AP_ISC;
0037     status = ap_aqic(aq->qid, qirqctrl, virt_to_phys(ind));
0038     switch (status.response_code) {
0039     case AP_RESPONSE_NORMAL:
0040     case AP_RESPONSE_OTHERWISE_CHANGED:
0041         return 0;
0042     case AP_RESPONSE_Q_NOT_AVAIL:
0043     case AP_RESPONSE_DECONFIGURED:
0044     case AP_RESPONSE_CHECKSTOPPED:
0045     case AP_RESPONSE_INVALID_ADDRESS:
0046         pr_err("Registering adapter interrupts for AP device %02x.%04x failed\n",
0047                AP_QID_CARD(aq->qid),
0048                AP_QID_QUEUE(aq->qid));
0049         return -EOPNOTSUPP;
0050     case AP_RESPONSE_RESET_IN_PROGRESS:
0051     case AP_RESPONSE_BUSY:
0052     default:
0053         return -EBUSY;
0054     }
0055 }
0056 
0057 /**
0058  * __ap_send(): Send message to adjunct processor queue.
0059  * @qid: The AP queue number
0060  * @psmid: The program supplied message identifier
0061  * @msg: The message text
0062  * @length: The message length
0063  * @special: Special Bit
0064  *
0065  * Returns AP queue status structure.
0066  * Condition code 1 on NQAP can't happen because the L bit is 1.
0067  * Condition code 2 on NQAP also means the send is incomplete,
0068  * because a segment boundary was reached. The NQAP is repeated.
0069  */
0070 static inline struct ap_queue_status
0071 __ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length,
0072       int special)
0073 {
0074     if (special)
0075         qid |= 0x400000UL;
0076     return ap_nqap(qid, psmid, msg, length);
0077 }
0078 
0079 int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
0080 {
0081     struct ap_queue_status status;
0082 
0083     status = __ap_send(qid, psmid, msg, length, 0);
0084     switch (status.response_code) {
0085     case AP_RESPONSE_NORMAL:
0086         return 0;
0087     case AP_RESPONSE_Q_FULL:
0088     case AP_RESPONSE_RESET_IN_PROGRESS:
0089         return -EBUSY;
0090     case AP_RESPONSE_REQ_FAC_NOT_INST:
0091         return -EINVAL;
0092     default:    /* Device is gone. */
0093         return -ENODEV;
0094     }
0095 }
0096 EXPORT_SYMBOL(ap_send);
0097 
0098 int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
0099 {
0100     struct ap_queue_status status;
0101 
0102     if (!msg)
0103         return -EINVAL;
0104     status = ap_dqap(qid, psmid, msg, length, NULL, NULL);
0105     switch (status.response_code) {
0106     case AP_RESPONSE_NORMAL:
0107         return 0;
0108     case AP_RESPONSE_NO_PENDING_REPLY:
0109         if (status.queue_empty)
0110             return -ENOENT;
0111         return -EBUSY;
0112     case AP_RESPONSE_RESET_IN_PROGRESS:
0113         return -EBUSY;
0114     default:
0115         return -ENODEV;
0116     }
0117 }
0118 EXPORT_SYMBOL(ap_recv);
0119 
0120 /* State machine definitions and helpers */
0121 
0122 static enum ap_sm_wait ap_sm_nop(struct ap_queue *aq)
0123 {
0124     return AP_SM_WAIT_NONE;
0125 }
0126 
0127 /**
0128  * ap_sm_recv(): Receive pending reply messages from an AP queue but do
0129  *  not change the state of the device.
0130  * @aq: pointer to the AP queue
0131  *
0132  * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
0133  */
0134 static struct ap_queue_status ap_sm_recv(struct ap_queue *aq)
0135 {
0136     struct ap_queue_status status;
0137     struct ap_message *ap_msg;
0138     bool found = false;
0139     size_t reslen;
0140     unsigned long resgr0 = 0;
0141     int parts = 0;
0142 
0143     /*
0144      * DQAP loop until response code and resgr0 indicate that
0145      * the msg is totally received. As we use the very same buffer
0146      * the msg is overwritten with each invocation. That's intended
0147      * and the receiver of the msg is informed with a msg rc code
0148      * of EMSGSIZE in such a case.
0149      */
0150     do {
0151         status = ap_dqap(aq->qid, &aq->reply->psmid,
0152                  aq->reply->msg, aq->reply->bufsize,
0153                  &reslen, &resgr0);
0154         parts++;
0155     } while (status.response_code == 0xFF && resgr0 != 0);
0156 
0157     switch (status.response_code) {
0158     case AP_RESPONSE_NORMAL:
0159         aq->queue_count = max_t(int, 0, aq->queue_count - 1);
0160         if (!status.queue_empty && !aq->queue_count)
0161             aq->queue_count++;
0162         if (aq->queue_count > 0)
0163             mod_timer(&aq->timeout,
0164                   jiffies + aq->request_timeout);
0165         list_for_each_entry(ap_msg, &aq->pendingq, list) {
0166             if (ap_msg->psmid != aq->reply->psmid)
0167                 continue;
0168             list_del_init(&ap_msg->list);
0169             aq->pendingq_count--;
0170             if (parts > 1) {
0171                 ap_msg->rc = -EMSGSIZE;
0172                 ap_msg->receive(aq, ap_msg, NULL);
0173             } else {
0174                 ap_msg->receive(aq, ap_msg, aq->reply);
0175             }
0176             found = true;
0177             break;
0178         }
0179         if (!found) {
0180             AP_DBF_WARN("%s unassociated reply psmid=0x%016llx on 0x%02x.%04x\n",
0181                     __func__, aq->reply->psmid,
0182                     AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
0183         }
0184         fallthrough;
0185     case AP_RESPONSE_NO_PENDING_REPLY:
0186         if (!status.queue_empty || aq->queue_count <= 0)
0187             break;
0188         /* The card shouldn't forget requests but who knows. */
0189         aq->queue_count = 0;
0190         list_splice_init(&aq->pendingq, &aq->requestq);
0191         aq->requestq_count += aq->pendingq_count;
0192         aq->pendingq_count = 0;
0193         break;
0194     default:
0195         break;
0196     }
0197     return status;
0198 }
0199 
0200 /**
0201  * ap_sm_read(): Receive pending reply messages from an AP queue.
0202  * @aq: pointer to the AP queue
0203  *
0204  * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
0205  */
0206 static enum ap_sm_wait ap_sm_read(struct ap_queue *aq)
0207 {
0208     struct ap_queue_status status;
0209 
0210     if (!aq->reply)
0211         return AP_SM_WAIT_NONE;
0212     status = ap_sm_recv(aq);
0213     switch (status.response_code) {
0214     case AP_RESPONSE_NORMAL:
0215         if (aq->queue_count > 0) {
0216             aq->sm_state = AP_SM_STATE_WORKING;
0217             return AP_SM_WAIT_AGAIN;
0218         }
0219         aq->sm_state = AP_SM_STATE_IDLE;
0220         return AP_SM_WAIT_NONE;
0221     case AP_RESPONSE_NO_PENDING_REPLY:
0222         if (aq->queue_count > 0)
0223             return aq->interrupt ?
0224                 AP_SM_WAIT_INTERRUPT : AP_SM_WAIT_TIMEOUT;
0225         aq->sm_state = AP_SM_STATE_IDLE;
0226         return AP_SM_WAIT_NONE;
0227     default:
0228         aq->dev_state = AP_DEV_STATE_ERROR;
0229         aq->last_err_rc = status.response_code;
0230         AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
0231                 __func__, status.response_code,
0232                 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
0233         return AP_SM_WAIT_NONE;
0234     }
0235 }
0236 
0237 /**
0238  * ap_sm_write(): Send messages from the request queue to an AP queue.
0239  * @aq: pointer to the AP queue
0240  *
0241  * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
0242  */
0243 static enum ap_sm_wait ap_sm_write(struct ap_queue *aq)
0244 {
0245     struct ap_queue_status status;
0246     struct ap_message *ap_msg;
0247     ap_qid_t qid = aq->qid;
0248 
0249     if (aq->requestq_count <= 0)
0250         return AP_SM_WAIT_NONE;
0251 
0252     /* Start the next request on the queue. */
0253     ap_msg = list_entry(aq->requestq.next, struct ap_message, list);
0254 #ifdef CONFIG_ZCRYPT_DEBUG
0255     if (ap_msg->fi.action == AP_FI_ACTION_NQAP_QID_INVAL) {
0256         AP_DBF_WARN("%s fi cmd 0x%04x: forcing invalid qid 0xFF00\n",
0257                 __func__, ap_msg->fi.cmd);
0258         qid = 0xFF00;
0259     }
0260 #endif
0261     status = __ap_send(qid, ap_msg->psmid,
0262                ap_msg->msg, ap_msg->len,
0263                ap_msg->flags & AP_MSG_FLAG_SPECIAL);
0264     switch (status.response_code) {
0265     case AP_RESPONSE_NORMAL:
0266         aq->queue_count = max_t(int, 1, aq->queue_count + 1);
0267         if (aq->queue_count == 1)
0268             mod_timer(&aq->timeout, jiffies + aq->request_timeout);
0269         list_move_tail(&ap_msg->list, &aq->pendingq);
0270         aq->requestq_count--;
0271         aq->pendingq_count++;
0272         if (aq->queue_count < aq->card->queue_depth) {
0273             aq->sm_state = AP_SM_STATE_WORKING;
0274             return AP_SM_WAIT_AGAIN;
0275         }
0276         fallthrough;
0277     case AP_RESPONSE_Q_FULL:
0278         aq->sm_state = AP_SM_STATE_QUEUE_FULL;
0279         return aq->interrupt ?
0280             AP_SM_WAIT_INTERRUPT : AP_SM_WAIT_TIMEOUT;
0281     case AP_RESPONSE_RESET_IN_PROGRESS:
0282         aq->sm_state = AP_SM_STATE_RESET_WAIT;
0283         return AP_SM_WAIT_TIMEOUT;
0284     case AP_RESPONSE_INVALID_DOMAIN:
0285         AP_DBF_WARN("%s RESPONSE_INVALID_DOMAIN on NQAP\n", __func__);
0286         fallthrough;
0287     case AP_RESPONSE_MESSAGE_TOO_BIG:
0288     case AP_RESPONSE_REQ_FAC_NOT_INST:
0289         list_del_init(&ap_msg->list);
0290         aq->requestq_count--;
0291         ap_msg->rc = -EINVAL;
0292         ap_msg->receive(aq, ap_msg, NULL);
0293         return AP_SM_WAIT_AGAIN;
0294     default:
0295         aq->dev_state = AP_DEV_STATE_ERROR;
0296         aq->last_err_rc = status.response_code;
0297         AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
0298                 __func__, status.response_code,
0299                 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
0300         return AP_SM_WAIT_NONE;
0301     }
0302 }
0303 
0304 /**
0305  * ap_sm_read_write(): Send and receive messages to/from an AP queue.
0306  * @aq: pointer to the AP queue
0307  *
0308  * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
0309  */
0310 static enum ap_sm_wait ap_sm_read_write(struct ap_queue *aq)
0311 {
0312     return min(ap_sm_read(aq), ap_sm_write(aq));
0313 }
0314 
0315 /**
0316  * ap_sm_reset(): Reset an AP queue.
0317  * @aq: The AP queue
0318  *
0319  * Submit the Reset command to an AP queue.
0320  */
0321 static enum ap_sm_wait ap_sm_reset(struct ap_queue *aq)
0322 {
0323     struct ap_queue_status status;
0324 
0325     status = ap_rapq(aq->qid);
0326     switch (status.response_code) {
0327     case AP_RESPONSE_NORMAL:
0328     case AP_RESPONSE_RESET_IN_PROGRESS:
0329         aq->sm_state = AP_SM_STATE_RESET_WAIT;
0330         aq->interrupt = false;
0331         return AP_SM_WAIT_TIMEOUT;
0332     default:
0333         aq->dev_state = AP_DEV_STATE_ERROR;
0334         aq->last_err_rc = status.response_code;
0335         AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
0336                 __func__, status.response_code,
0337                 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
0338         return AP_SM_WAIT_NONE;
0339     }
0340 }
0341 
0342 /**
0343  * ap_sm_reset_wait(): Test queue for completion of the reset operation
0344  * @aq: pointer to the AP queue
0345  *
0346  * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
0347  */
0348 static enum ap_sm_wait ap_sm_reset_wait(struct ap_queue *aq)
0349 {
0350     struct ap_queue_status status;
0351     void *lsi_ptr;
0352 
0353     if (aq->queue_count > 0 && aq->reply)
0354         /* Try to read a completed message and get the status */
0355         status = ap_sm_recv(aq);
0356     else
0357         /* Get the status with TAPQ */
0358         status = ap_tapq(aq->qid, NULL);
0359 
0360     switch (status.response_code) {
0361     case AP_RESPONSE_NORMAL:
0362         lsi_ptr = ap_airq_ptr();
0363         if (lsi_ptr && ap_queue_enable_irq(aq, lsi_ptr) == 0)
0364             aq->sm_state = AP_SM_STATE_SETIRQ_WAIT;
0365         else
0366             aq->sm_state = (aq->queue_count > 0) ?
0367                 AP_SM_STATE_WORKING : AP_SM_STATE_IDLE;
0368         return AP_SM_WAIT_AGAIN;
0369     case AP_RESPONSE_BUSY:
0370     case AP_RESPONSE_RESET_IN_PROGRESS:
0371         return AP_SM_WAIT_TIMEOUT;
0372     case AP_RESPONSE_Q_NOT_AVAIL:
0373     case AP_RESPONSE_DECONFIGURED:
0374     case AP_RESPONSE_CHECKSTOPPED:
0375     default:
0376         aq->dev_state = AP_DEV_STATE_ERROR;
0377         aq->last_err_rc = status.response_code;
0378         AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
0379                 __func__, status.response_code,
0380                 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
0381         return AP_SM_WAIT_NONE;
0382     }
0383 }
0384 
0385 /**
0386  * ap_sm_setirq_wait(): Test queue for completion of the irq enablement
0387  * @aq: pointer to the AP queue
0388  *
0389  * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
0390  */
0391 static enum ap_sm_wait ap_sm_setirq_wait(struct ap_queue *aq)
0392 {
0393     struct ap_queue_status status;
0394 
0395     if (aq->queue_count > 0 && aq->reply)
0396         /* Try to read a completed message and get the status */
0397         status = ap_sm_recv(aq);
0398     else
0399         /* Get the status with TAPQ */
0400         status = ap_tapq(aq->qid, NULL);
0401 
0402     if (status.irq_enabled == 1) {
0403         /* Irqs are now enabled */
0404         aq->interrupt = true;
0405         aq->sm_state = (aq->queue_count > 0) ?
0406             AP_SM_STATE_WORKING : AP_SM_STATE_IDLE;
0407     }
0408 
0409     switch (status.response_code) {
0410     case AP_RESPONSE_NORMAL:
0411         if (aq->queue_count > 0)
0412             return AP_SM_WAIT_AGAIN;
0413         fallthrough;
0414     case AP_RESPONSE_NO_PENDING_REPLY:
0415         return AP_SM_WAIT_TIMEOUT;
0416     default:
0417         aq->dev_state = AP_DEV_STATE_ERROR;
0418         aq->last_err_rc = status.response_code;
0419         AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
0420                 __func__, status.response_code,
0421                 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
0422         return AP_SM_WAIT_NONE;
0423     }
0424 }
0425 
0426 /*
0427  * AP state machine jump table
0428  */
0429 static ap_func_t *ap_jumptable[NR_AP_SM_STATES][NR_AP_SM_EVENTS] = {
0430     [AP_SM_STATE_RESET_START] = {
0431         [AP_SM_EVENT_POLL] = ap_sm_reset,
0432         [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
0433     },
0434     [AP_SM_STATE_RESET_WAIT] = {
0435         [AP_SM_EVENT_POLL] = ap_sm_reset_wait,
0436         [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
0437     },
0438     [AP_SM_STATE_SETIRQ_WAIT] = {
0439         [AP_SM_EVENT_POLL] = ap_sm_setirq_wait,
0440         [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
0441     },
0442     [AP_SM_STATE_IDLE] = {
0443         [AP_SM_EVENT_POLL] = ap_sm_write,
0444         [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
0445     },
0446     [AP_SM_STATE_WORKING] = {
0447         [AP_SM_EVENT_POLL] = ap_sm_read_write,
0448         [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
0449     },
0450     [AP_SM_STATE_QUEUE_FULL] = {
0451         [AP_SM_EVENT_POLL] = ap_sm_read,
0452         [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
0453     },
0454 };
0455 
0456 enum ap_sm_wait ap_sm_event(struct ap_queue *aq, enum ap_sm_event event)
0457 {
0458     if (aq->config && !aq->chkstop &&
0459         aq->dev_state > AP_DEV_STATE_UNINITIATED)
0460         return ap_jumptable[aq->sm_state][event](aq);
0461     else
0462         return AP_SM_WAIT_NONE;
0463 }
0464 
0465 enum ap_sm_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_sm_event event)
0466 {
0467     enum ap_sm_wait wait;
0468 
0469     while ((wait = ap_sm_event(aq, event)) == AP_SM_WAIT_AGAIN)
0470         ;
0471     return wait;
0472 }
0473 
0474 /*
0475  * AP queue related attributes.
0476  */
0477 static ssize_t request_count_show(struct device *dev,
0478                   struct device_attribute *attr,
0479                   char *buf)
0480 {
0481     struct ap_queue *aq = to_ap_queue(dev);
0482     bool valid = false;
0483     u64 req_cnt;
0484 
0485     spin_lock_bh(&aq->lock);
0486     if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
0487         req_cnt = aq->total_request_count;
0488         valid = true;
0489     }
0490     spin_unlock_bh(&aq->lock);
0491 
0492     if (valid)
0493         return scnprintf(buf, PAGE_SIZE, "%llu\n", req_cnt);
0494     else
0495         return scnprintf(buf, PAGE_SIZE, "-\n");
0496 }
0497 
0498 static ssize_t request_count_store(struct device *dev,
0499                    struct device_attribute *attr,
0500                    const char *buf, size_t count)
0501 {
0502     struct ap_queue *aq = to_ap_queue(dev);
0503 
0504     spin_lock_bh(&aq->lock);
0505     aq->total_request_count = 0;
0506     spin_unlock_bh(&aq->lock);
0507 
0508     return count;
0509 }
0510 
0511 static DEVICE_ATTR_RW(request_count);
0512 
0513 static ssize_t requestq_count_show(struct device *dev,
0514                    struct device_attribute *attr, char *buf)
0515 {
0516     struct ap_queue *aq = to_ap_queue(dev);
0517     unsigned int reqq_cnt = 0;
0518 
0519     spin_lock_bh(&aq->lock);
0520     if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
0521         reqq_cnt = aq->requestq_count;
0522     spin_unlock_bh(&aq->lock);
0523     return scnprintf(buf, PAGE_SIZE, "%d\n", reqq_cnt);
0524 }
0525 
0526 static DEVICE_ATTR_RO(requestq_count);
0527 
0528 static ssize_t pendingq_count_show(struct device *dev,
0529                    struct device_attribute *attr, char *buf)
0530 {
0531     struct ap_queue *aq = to_ap_queue(dev);
0532     unsigned int penq_cnt = 0;
0533 
0534     spin_lock_bh(&aq->lock);
0535     if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
0536         penq_cnt = aq->pendingq_count;
0537     spin_unlock_bh(&aq->lock);
0538     return scnprintf(buf, PAGE_SIZE, "%d\n", penq_cnt);
0539 }
0540 
0541 static DEVICE_ATTR_RO(pendingq_count);
0542 
0543 static ssize_t reset_show(struct device *dev,
0544               struct device_attribute *attr, char *buf)
0545 {
0546     struct ap_queue *aq = to_ap_queue(dev);
0547     int rc = 0;
0548 
0549     spin_lock_bh(&aq->lock);
0550     switch (aq->sm_state) {
0551     case AP_SM_STATE_RESET_START:
0552     case AP_SM_STATE_RESET_WAIT:
0553         rc = scnprintf(buf, PAGE_SIZE, "Reset in progress.\n");
0554         break;
0555     case AP_SM_STATE_WORKING:
0556     case AP_SM_STATE_QUEUE_FULL:
0557         rc = scnprintf(buf, PAGE_SIZE, "Reset Timer armed.\n");
0558         break;
0559     default:
0560         rc = scnprintf(buf, PAGE_SIZE, "No Reset Timer set.\n");
0561     }
0562     spin_unlock_bh(&aq->lock);
0563     return rc;
0564 }
0565 
0566 static ssize_t reset_store(struct device *dev,
0567                struct device_attribute *attr,
0568                const char *buf, size_t count)
0569 {
0570     struct ap_queue *aq = to_ap_queue(dev);
0571 
0572     spin_lock_bh(&aq->lock);
0573     __ap_flush_queue(aq);
0574     aq->sm_state = AP_SM_STATE_RESET_START;
0575     ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
0576     spin_unlock_bh(&aq->lock);
0577 
0578     AP_DBF_INFO("%s reset queue=%02x.%04x triggered by user\n",
0579             __func__, AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
0580 
0581     return count;
0582 }
0583 
0584 static DEVICE_ATTR_RW(reset);
0585 
0586 static ssize_t interrupt_show(struct device *dev,
0587                   struct device_attribute *attr, char *buf)
0588 {
0589     struct ap_queue *aq = to_ap_queue(dev);
0590     int rc = 0;
0591 
0592     spin_lock_bh(&aq->lock);
0593     if (aq->sm_state == AP_SM_STATE_SETIRQ_WAIT)
0594         rc = scnprintf(buf, PAGE_SIZE, "Enable Interrupt pending.\n");
0595     else if (aq->interrupt)
0596         rc = scnprintf(buf, PAGE_SIZE, "Interrupts enabled.\n");
0597     else
0598         rc = scnprintf(buf, PAGE_SIZE, "Interrupts disabled.\n");
0599     spin_unlock_bh(&aq->lock);
0600     return rc;
0601 }
0602 
0603 static DEVICE_ATTR_RO(interrupt);
0604 
0605 static ssize_t config_show(struct device *dev,
0606                struct device_attribute *attr, char *buf)
0607 {
0608     struct ap_queue *aq = to_ap_queue(dev);
0609     int rc;
0610 
0611     spin_lock_bh(&aq->lock);
0612     rc = scnprintf(buf, PAGE_SIZE, "%d\n", aq->config ? 1 : 0);
0613     spin_unlock_bh(&aq->lock);
0614     return rc;
0615 }
0616 
0617 static DEVICE_ATTR_RO(config);
0618 
0619 static ssize_t chkstop_show(struct device *dev,
0620                 struct device_attribute *attr, char *buf)
0621 {
0622     struct ap_queue *aq = to_ap_queue(dev);
0623     int rc;
0624 
0625     spin_lock_bh(&aq->lock);
0626     rc = scnprintf(buf, PAGE_SIZE, "%d\n", aq->chkstop ? 1 : 0);
0627     spin_unlock_bh(&aq->lock);
0628     return rc;
0629 }
0630 
0631 static DEVICE_ATTR_RO(chkstop);
0632 
0633 #ifdef CONFIG_ZCRYPT_DEBUG
0634 static ssize_t states_show(struct device *dev,
0635                struct device_attribute *attr, char *buf)
0636 {
0637     struct ap_queue *aq = to_ap_queue(dev);
0638     int rc = 0;
0639 
0640     spin_lock_bh(&aq->lock);
0641     /* queue device state */
0642     switch (aq->dev_state) {
0643     case AP_DEV_STATE_UNINITIATED:
0644         rc = scnprintf(buf, PAGE_SIZE, "UNINITIATED\n");
0645         break;
0646     case AP_DEV_STATE_OPERATING:
0647         rc = scnprintf(buf, PAGE_SIZE, "OPERATING");
0648         break;
0649     case AP_DEV_STATE_SHUTDOWN:
0650         rc = scnprintf(buf, PAGE_SIZE, "SHUTDOWN");
0651         break;
0652     case AP_DEV_STATE_ERROR:
0653         rc = scnprintf(buf, PAGE_SIZE, "ERROR");
0654         break;
0655     default:
0656         rc = scnprintf(buf, PAGE_SIZE, "UNKNOWN");
0657     }
0658     /* state machine state */
0659     if (aq->dev_state) {
0660         switch (aq->sm_state) {
0661         case AP_SM_STATE_RESET_START:
0662             rc += scnprintf(buf + rc, PAGE_SIZE - rc,
0663                     " [RESET_START]\n");
0664             break;
0665         case AP_SM_STATE_RESET_WAIT:
0666             rc += scnprintf(buf + rc, PAGE_SIZE - rc,
0667                     " [RESET_WAIT]\n");
0668             break;
0669         case AP_SM_STATE_SETIRQ_WAIT:
0670             rc += scnprintf(buf + rc, PAGE_SIZE - rc,
0671                     " [SETIRQ_WAIT]\n");
0672             break;
0673         case AP_SM_STATE_IDLE:
0674             rc += scnprintf(buf + rc, PAGE_SIZE - rc,
0675                     " [IDLE]\n");
0676             break;
0677         case AP_SM_STATE_WORKING:
0678             rc += scnprintf(buf + rc, PAGE_SIZE - rc,
0679                     " [WORKING]\n");
0680             break;
0681         case AP_SM_STATE_QUEUE_FULL:
0682             rc += scnprintf(buf + rc, PAGE_SIZE - rc,
0683                     " [FULL]\n");
0684             break;
0685         default:
0686             rc += scnprintf(buf + rc, PAGE_SIZE - rc,
0687                     " [UNKNOWN]\n");
0688         }
0689     }
0690     spin_unlock_bh(&aq->lock);
0691 
0692     return rc;
0693 }
0694 static DEVICE_ATTR_RO(states);
0695 
0696 static ssize_t last_err_rc_show(struct device *dev,
0697                 struct device_attribute *attr, char *buf)
0698 {
0699     struct ap_queue *aq = to_ap_queue(dev);
0700     int rc;
0701 
0702     spin_lock_bh(&aq->lock);
0703     rc = aq->last_err_rc;
0704     spin_unlock_bh(&aq->lock);
0705 
0706     switch (rc) {
0707     case AP_RESPONSE_NORMAL:
0708         return scnprintf(buf, PAGE_SIZE, "NORMAL\n");
0709     case AP_RESPONSE_Q_NOT_AVAIL:
0710         return scnprintf(buf, PAGE_SIZE, "Q_NOT_AVAIL\n");
0711     case AP_RESPONSE_RESET_IN_PROGRESS:
0712         return scnprintf(buf, PAGE_SIZE, "RESET_IN_PROGRESS\n");
0713     case AP_RESPONSE_DECONFIGURED:
0714         return scnprintf(buf, PAGE_SIZE, "DECONFIGURED\n");
0715     case AP_RESPONSE_CHECKSTOPPED:
0716         return scnprintf(buf, PAGE_SIZE, "CHECKSTOPPED\n");
0717     case AP_RESPONSE_BUSY:
0718         return scnprintf(buf, PAGE_SIZE, "BUSY\n");
0719     case AP_RESPONSE_INVALID_ADDRESS:
0720         return scnprintf(buf, PAGE_SIZE, "INVALID_ADDRESS\n");
0721     case AP_RESPONSE_OTHERWISE_CHANGED:
0722         return scnprintf(buf, PAGE_SIZE, "OTHERWISE_CHANGED\n");
0723     case AP_RESPONSE_Q_FULL:
0724         return scnprintf(buf, PAGE_SIZE, "Q_FULL/NO_PENDING_REPLY\n");
0725     case AP_RESPONSE_INDEX_TOO_BIG:
0726         return scnprintf(buf, PAGE_SIZE, "INDEX_TOO_BIG\n");
0727     case AP_RESPONSE_NO_FIRST_PART:
0728         return scnprintf(buf, PAGE_SIZE, "NO_FIRST_PART\n");
0729     case AP_RESPONSE_MESSAGE_TOO_BIG:
0730         return scnprintf(buf, PAGE_SIZE, "MESSAGE_TOO_BIG\n");
0731     case AP_RESPONSE_REQ_FAC_NOT_INST:
0732         return scnprintf(buf, PAGE_SIZE, "REQ_FAC_NOT_INST\n");
0733     default:
0734         return scnprintf(buf, PAGE_SIZE, "response code %d\n", rc);
0735     }
0736 }
0737 static DEVICE_ATTR_RO(last_err_rc);
0738 #endif
0739 
0740 static struct attribute *ap_queue_dev_attrs[] = {
0741     &dev_attr_request_count.attr,
0742     &dev_attr_requestq_count.attr,
0743     &dev_attr_pendingq_count.attr,
0744     &dev_attr_reset.attr,
0745     &dev_attr_interrupt.attr,
0746     &dev_attr_config.attr,
0747     &dev_attr_chkstop.attr,
0748 #ifdef CONFIG_ZCRYPT_DEBUG
0749     &dev_attr_states.attr,
0750     &dev_attr_last_err_rc.attr,
0751 #endif
0752     NULL
0753 };
0754 
0755 static struct attribute_group ap_queue_dev_attr_group = {
0756     .attrs = ap_queue_dev_attrs
0757 };
0758 
0759 static const struct attribute_group *ap_queue_dev_attr_groups[] = {
0760     &ap_queue_dev_attr_group,
0761     NULL
0762 };
0763 
0764 static struct device_type ap_queue_type = {
0765     .name = "ap_queue",
0766     .groups = ap_queue_dev_attr_groups,
0767 };
0768 
0769 static void ap_queue_device_release(struct device *dev)
0770 {
0771     struct ap_queue *aq = to_ap_queue(dev);
0772 
0773     spin_lock_bh(&ap_queues_lock);
0774     hash_del(&aq->hnode);
0775     spin_unlock_bh(&ap_queues_lock);
0776 
0777     kfree(aq);
0778 }
0779 
0780 struct ap_queue *ap_queue_create(ap_qid_t qid, int device_type)
0781 {
0782     struct ap_queue *aq;
0783 
0784     aq = kzalloc(sizeof(*aq), GFP_KERNEL);
0785     if (!aq)
0786         return NULL;
0787     aq->ap_dev.device.release = ap_queue_device_release;
0788     aq->ap_dev.device.type = &ap_queue_type;
0789     aq->ap_dev.device_type = device_type;
0790     aq->qid = qid;
0791     aq->interrupt = false;
0792     spin_lock_init(&aq->lock);
0793     INIT_LIST_HEAD(&aq->pendingq);
0794     INIT_LIST_HEAD(&aq->requestq);
0795     timer_setup(&aq->timeout, ap_request_timeout, 0);
0796 
0797     return aq;
0798 }
0799 
0800 void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *reply)
0801 {
0802     aq->reply = reply;
0803 
0804     spin_lock_bh(&aq->lock);
0805     ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
0806     spin_unlock_bh(&aq->lock);
0807 }
0808 EXPORT_SYMBOL(ap_queue_init_reply);
0809 
0810 /**
0811  * ap_queue_message(): Queue a request to an AP device.
0812  * @aq: The AP device to queue the message to
0813  * @ap_msg: The message that is to be added
0814  */
0815 int ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg)
0816 {
0817     int rc = 0;
0818 
0819     /* msg needs to have a valid receive-callback */
0820     BUG_ON(!ap_msg->receive);
0821 
0822     spin_lock_bh(&aq->lock);
0823 
0824     /* only allow to queue new messages if device state is ok */
0825     if (aq->dev_state == AP_DEV_STATE_OPERATING) {
0826         list_add_tail(&ap_msg->list, &aq->requestq);
0827         aq->requestq_count++;
0828         aq->total_request_count++;
0829         atomic64_inc(&aq->card->total_request_count);
0830     } else {
0831         rc = -ENODEV;
0832     }
0833 
0834     /* Send/receive as many request from the queue as possible. */
0835     ap_wait(ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
0836 
0837     spin_unlock_bh(&aq->lock);
0838 
0839     return rc;
0840 }
0841 EXPORT_SYMBOL(ap_queue_message);
0842 
0843 /**
0844  * ap_cancel_message(): Cancel a crypto request.
0845  * @aq: The AP device that has the message queued
0846  * @ap_msg: The message that is to be removed
0847  *
0848  * Cancel a crypto request. This is done by removing the request
0849  * from the device pending or request queue. Note that the
0850  * request stays on the AP queue. When it finishes the message
0851  * reply will be discarded because the psmid can't be found.
0852  */
0853 void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg)
0854 {
0855     struct ap_message *tmp;
0856 
0857     spin_lock_bh(&aq->lock);
0858     if (!list_empty(&ap_msg->list)) {
0859         list_for_each_entry(tmp, &aq->pendingq, list)
0860             if (tmp->psmid == ap_msg->psmid) {
0861                 aq->pendingq_count--;
0862                 goto found;
0863             }
0864         aq->requestq_count--;
0865 found:
0866         list_del_init(&ap_msg->list);
0867     }
0868     spin_unlock_bh(&aq->lock);
0869 }
0870 EXPORT_SYMBOL(ap_cancel_message);
0871 
0872 /**
0873  * __ap_flush_queue(): Flush requests.
0874  * @aq: Pointer to the AP queue
0875  *
0876  * Flush all requests from the request/pending queue of an AP device.
0877  */
0878 static void __ap_flush_queue(struct ap_queue *aq)
0879 {
0880     struct ap_message *ap_msg, *next;
0881 
0882     list_for_each_entry_safe(ap_msg, next, &aq->pendingq, list) {
0883         list_del_init(&ap_msg->list);
0884         aq->pendingq_count--;
0885         ap_msg->rc = -EAGAIN;
0886         ap_msg->receive(aq, ap_msg, NULL);
0887     }
0888     list_for_each_entry_safe(ap_msg, next, &aq->requestq, list) {
0889         list_del_init(&ap_msg->list);
0890         aq->requestq_count--;
0891         ap_msg->rc = -EAGAIN;
0892         ap_msg->receive(aq, ap_msg, NULL);
0893     }
0894     aq->queue_count = 0;
0895 }
0896 
0897 void ap_flush_queue(struct ap_queue *aq)
0898 {
0899     spin_lock_bh(&aq->lock);
0900     __ap_flush_queue(aq);
0901     spin_unlock_bh(&aq->lock);
0902 }
0903 EXPORT_SYMBOL(ap_flush_queue);
0904 
0905 void ap_queue_prepare_remove(struct ap_queue *aq)
0906 {
0907     spin_lock_bh(&aq->lock);
0908     /* flush queue */
0909     __ap_flush_queue(aq);
0910     /* move queue device state to SHUTDOWN in progress */
0911     aq->dev_state = AP_DEV_STATE_SHUTDOWN;
0912     spin_unlock_bh(&aq->lock);
0913     del_timer_sync(&aq->timeout);
0914 }
0915 
0916 void ap_queue_remove(struct ap_queue *aq)
0917 {
0918     /*
0919      * all messages have been flushed and the device state
0920      * is SHUTDOWN. Now reset with zero which also clears
0921      * the irq registration and move the device state
0922      * to the initial value AP_DEV_STATE_UNINITIATED.
0923      */
0924     spin_lock_bh(&aq->lock);
0925     ap_zapq(aq->qid);
0926     aq->dev_state = AP_DEV_STATE_UNINITIATED;
0927     spin_unlock_bh(&aq->lock);
0928 }
0929 
0930 void ap_queue_init_state(struct ap_queue *aq)
0931 {
0932     spin_lock_bh(&aq->lock);
0933     aq->dev_state = AP_DEV_STATE_OPERATING;
0934     aq->sm_state = AP_SM_STATE_RESET_START;
0935     aq->last_err_rc = 0;
0936     ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
0937     spin_unlock_bh(&aq->lock);
0938 }
0939 EXPORT_SYMBOL(ap_queue_init_state);