linux/drivers/s390/crypto/ap_queue.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Copyright IBM Corp. 2016
   4 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
   5 *
   6 * Adjunct processor bus, queue related code.
   7 */
   8
   9#define KMSG_COMPONENT "ap"
  10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  11
  12#include <linux/init.h>
  13#include <linux/slab.h>
  14#include <asm/facility.h>
  15
  16#include "ap_bus.h"
  17#include "ap_debug.h"
  18
  19static void __ap_flush_queue(struct ap_queue *aq);
  20
  21/**
  22 * ap_queue_enable_irq(): Enable interrupt support on this AP queue.
  23 * @qid: The AP queue number
  24 * @ind: the notification indicator byte
  25 *
  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().
  29 */
  30static int ap_queue_enable_irq(struct ap_queue *aq, void *ind)
  31{
  32        struct ap_queue_status status;
  33        struct ap_qirq_ctrl qirqctrl = { 0 };
  34
  35        qirqctrl.ir = 1;
  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:
  41                return 0;
  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",
  47                       AP_QID_CARD(aq->qid),
  48                       AP_QID_QUEUE(aq->qid));
  49                return -EOPNOTSUPP;
  50        case AP_RESPONSE_RESET_IN_PROGRESS:
  51        case AP_RESPONSE_BUSY:
  52        default:
  53                return -EBUSY;
  54        }
  55}
  56
  57/**
  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
  64 *
  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.
  69 */
  70static inline struct ap_queue_status
  71__ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length,
  72          int special)
  73{
  74        if (special)
  75                qid |= 0x400000UL;
  76        return ap_nqap(qid, psmid, msg, length);
  77}
  78
  79int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
  80{
  81        struct ap_queue_status status;
  82
  83        status = __ap_send(qid, psmid, msg, length, 0);
  84        switch (status.response_code) {
  85        case AP_RESPONSE_NORMAL:
  86                return 0;
  87        case AP_RESPONSE_Q_FULL:
  88        case AP_RESPONSE_RESET_IN_PROGRESS:
  89                return -EBUSY;
  90        case AP_RESPONSE_REQ_FAC_NOT_INST:
  91                return -EINVAL;
  92        default:        /* Device is gone. */
  93                return -ENODEV;
  94        }
  95}
  96EXPORT_SYMBOL(ap_send);
  97
  98int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
  99{
 100        struct ap_queue_status status;
 101
 102        if (msg == NULL)
 103                return -EINVAL;
 104        status = ap_dqap(qid, psmid, msg, length);
 105        switch (status.response_code) {
 106        case AP_RESPONSE_NORMAL:
 107                return 0;
 108        case AP_RESPONSE_NO_PENDING_REPLY:
 109                if (status.queue_empty)
 110                        return -ENOENT;
 111                return -EBUSY;
 112        case AP_RESPONSE_RESET_IN_PROGRESS:
 113                return -EBUSY;
 114        default:
 115                return -ENODEV;
 116        }
 117}
 118EXPORT_SYMBOL(ap_recv);
 119
 120/* State machine definitions and helpers */
 121
 122static enum ap_sm_wait ap_sm_nop(struct ap_queue *aq)
 123{
 124        return AP_SM_WAIT_NONE;
 125}
 126
 127/**
 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
 131 *
 132 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
 133 */
 134static struct ap_queue_status ap_sm_recv(struct ap_queue *aq)
 135{
 136        struct ap_queue_status status;
 137        struct ap_message *ap_msg;
 138        bool found = false;
 139
 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)
 150                                continue;
 151                        list_del_init(&ap_msg->list);
 152                        aq->pendingq_count--;
 153                        ap_msg->receive(aq, ap_msg, aq->reply);
 154                        found = true;
 155                        break;
 156                }
 157                if (!found) {
 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));
 161                }
 162                fallthrough;
 163        case AP_RESPONSE_NO_PENDING_REPLY:
 164                if (!status.queue_empty || aq->queue_count <= 0)
 165                        break;
 166                /* The card shouldn't forget requests but who knows. */
 167                aq->queue_count = 0;
 168                list_splice_init(&aq->pendingq, &aq->requestq);
 169                aq->requestq_count += aq->pendingq_count;
 170                aq->pendingq_count = 0;
 171                break;
 172        default:
 173                break;
 174        }
 175        return status;
 176}
 177
 178/**
 179 * ap_sm_read(): Receive pending reply messages from an AP queue.
 180 * @aq: pointer to the AP queue
 181 *
 182 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
 183 */
 184static enum ap_sm_wait ap_sm_read(struct ap_queue *aq)
 185{
 186        struct ap_queue_status status;
 187
 188        if (!aq->reply)
 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;
 196                }
 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 aq->interrupt ?
 202                                AP_SM_WAIT_INTERRUPT : AP_SM_WAIT_TIMEOUT;
 203                aq->sm_state = AP_SM_STATE_IDLE;
 204                return AP_SM_WAIT_NONE;
 205        default:
 206                aq->dev_state = AP_DEV_STATE_ERROR;
 207                aq->last_err_rc = status.response_code;
 208                AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
 209                            __func__, status.response_code,
 210                            AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
 211                return AP_SM_WAIT_NONE;
 212        }
 213}
 214
 215/**
 216 * ap_sm_write(): Send messages from the request queue to an AP queue.
 217 * @aq: pointer to the AP queue
 218 *
 219 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
 220 */
 221static enum ap_sm_wait ap_sm_write(struct ap_queue *aq)
 222{
 223        struct ap_queue_status status;
 224        struct ap_message *ap_msg;
 225        ap_qid_t qid = aq->qid;
 226
 227        if (aq->requestq_count <= 0)
 228                return AP_SM_WAIT_NONE;
 229        /* Start the next request on the queue. */
 230        ap_msg = list_entry(aq->requestq.next, struct ap_message, list);
 231#ifdef CONFIG_ZCRYPT_DEBUG
 232        if (ap_msg->fi.action == AP_FI_ACTION_NQAP_QID_INVAL) {
 233                AP_DBF_WARN("%s fi cmd 0x%04x: forcing invalid qid 0xFF00\n",
 234                            __func__, ap_msg->fi.cmd);
 235                qid = 0xFF00;
 236        }
 237#endif
 238        status = __ap_send(qid, ap_msg->psmid,
 239                           ap_msg->msg, ap_msg->len,
 240                           ap_msg->flags & AP_MSG_FLAG_SPECIAL);
 241        switch (status.response_code) {
 242        case AP_RESPONSE_NORMAL:
 243                aq->queue_count = max_t(int, 1, aq->queue_count + 1);
 244                if (aq->queue_count == 1)
 245                        mod_timer(&aq->timeout, jiffies + aq->request_timeout);
 246                list_move_tail(&ap_msg->list, &aq->pendingq);
 247                aq->requestq_count--;
 248                aq->pendingq_count++;
 249                if (aq->queue_count < aq->card->queue_depth) {
 250                        aq->sm_state = AP_SM_STATE_WORKING;
 251                        return AP_SM_WAIT_AGAIN;
 252                }
 253                fallthrough;
 254        case AP_RESPONSE_Q_FULL:
 255                aq->sm_state = AP_SM_STATE_QUEUE_FULL;
 256                return aq->interrupt ?
 257                        AP_SM_WAIT_INTERRUPT : AP_SM_WAIT_TIMEOUT;
 258        case AP_RESPONSE_RESET_IN_PROGRESS:
 259                aq->sm_state = AP_SM_STATE_RESET_WAIT;
 260                return AP_SM_WAIT_TIMEOUT;
 261        case AP_RESPONSE_INVALID_DOMAIN:
 262                AP_DBF(DBF_WARN, "AP_RESPONSE_INVALID_DOMAIN on NQAP\n");
 263                fallthrough;
 264        case AP_RESPONSE_MESSAGE_TOO_BIG:
 265        case AP_RESPONSE_REQ_FAC_NOT_INST:
 266                list_del_init(&ap_msg->list);
 267                aq->requestq_count--;
 268                ap_msg->rc = -EINVAL;
 269                ap_msg->receive(aq, ap_msg, NULL);
 270                return AP_SM_WAIT_AGAIN;
 271        default:
 272                aq->dev_state = AP_DEV_STATE_ERROR;
 273                aq->last_err_rc = status.response_code;
 274                AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
 275                            __func__, status.response_code,
 276                            AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
 277                return AP_SM_WAIT_NONE;
 278        }
 279}
 280
 281/**
 282 * ap_sm_read_write(): Send and receive messages to/from an AP queue.
 283 * @aq: pointer to the AP queue
 284 *
 285 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
 286 */
 287static enum ap_sm_wait ap_sm_read_write(struct ap_queue *aq)
 288{
 289        return min(ap_sm_read(aq), ap_sm_write(aq));
 290}
 291
 292/**
 293 * ap_sm_reset(): Reset an AP queue.
 294 * @qid: The AP queue number
 295 *
 296 * Submit the Reset command to an AP queue.
 297 */
 298static enum ap_sm_wait ap_sm_reset(struct ap_queue *aq)
 299{
 300        struct ap_queue_status status;
 301
 302        status = ap_rapq(aq->qid);
 303        switch (status.response_code) {
 304        case AP_RESPONSE_NORMAL:
 305        case AP_RESPONSE_RESET_IN_PROGRESS:
 306                aq->sm_state = AP_SM_STATE_RESET_WAIT;
 307                aq->interrupt = false;
 308                return AP_SM_WAIT_TIMEOUT;
 309        default:
 310                aq->dev_state = AP_DEV_STATE_ERROR;
 311                aq->last_err_rc = status.response_code;
 312                AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
 313                            __func__, status.response_code,
 314                            AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
 315                return AP_SM_WAIT_NONE;
 316        }
 317}
 318
 319/**
 320 * ap_sm_reset_wait(): Test queue for completion of the reset operation
 321 * @aq: pointer to the AP queue
 322 *
 323 * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
 324 */
 325static enum ap_sm_wait ap_sm_reset_wait(struct ap_queue *aq)
 326{
 327        struct ap_queue_status status;
 328        void *lsi_ptr;
 329
 330        if (aq->queue_count > 0 && aq->reply)
 331                /* Try to read a completed message and get the status */
 332                status = ap_sm_recv(aq);
 333        else
 334                /* Get the status with TAPQ */
 335                status = ap_tapq(aq->qid, NULL);
 336
 337        switch (status.response_code) {
 338        case AP_RESPONSE_NORMAL:
 339                lsi_ptr = ap_airq_ptr();
 340                if (lsi_ptr && ap_queue_enable_irq(aq, lsi_ptr) == 0)
 341                        aq->sm_state = AP_SM_STATE_SETIRQ_WAIT;
 342                else
 343                        aq->sm_state = (aq->queue_count > 0) ?
 344                                AP_SM_STATE_WORKING : AP_SM_STATE_IDLE;
 345                return AP_SM_WAIT_AGAIN;
 346        case AP_RESPONSE_BUSY:
 347        case AP_RESPONSE_RESET_IN_PROGRESS:
 348                return AP_SM_WAIT_TIMEOUT;
 349        case AP_RESPONSE_Q_NOT_AVAIL:
 350        case AP_RESPONSE_DECONFIGURED:
 351        case AP_RESPONSE_CHECKSTOPPED:
 352        default:
 353                aq->dev_state = AP_DEV_STATE_ERROR;
 354                aq->last_err_rc = status.response_code;
 355                AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
 356                            __func__, status.response_code,
 357                            AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
 358                return AP_SM_WAIT_NONE;
 359        }
 360}
 361
 362/**
 363 * ap_sm_setirq_wait(): Test queue for completion of the irq enablement
 364 * @aq: pointer to the AP queue
 365 *
 366 * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
 367 */
 368static enum ap_sm_wait ap_sm_setirq_wait(struct ap_queue *aq)
 369{
 370        struct ap_queue_status status;
 371
 372        if (aq->queue_count > 0 && aq->reply)
 373                /* Try to read a completed message and get the status */
 374                status = ap_sm_recv(aq);
 375        else
 376                /* Get the status with TAPQ */
 377                status = ap_tapq(aq->qid, NULL);
 378
 379        if (status.irq_enabled == 1) {
 380                /* Irqs are now enabled */
 381                aq->interrupt = true;
 382                aq->sm_state = (aq->queue_count > 0) ?
 383                        AP_SM_STATE_WORKING : AP_SM_STATE_IDLE;
 384        }
 385
 386        switch (status.response_code) {
 387        case AP_RESPONSE_NORMAL:
 388                if (aq->queue_count > 0)
 389                        return AP_SM_WAIT_AGAIN;
 390                fallthrough;
 391        case AP_RESPONSE_NO_PENDING_REPLY:
 392                return AP_SM_WAIT_TIMEOUT;
 393        default:
 394                aq->dev_state = AP_DEV_STATE_ERROR;
 395                aq->last_err_rc = status.response_code;
 396                AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
 397                            __func__, status.response_code,
 398                            AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
 399                return AP_SM_WAIT_NONE;
 400        }
 401}
 402
 403/*
 404 * AP state machine jump table
 405 */
 406static ap_func_t *ap_jumptable[NR_AP_SM_STATES][NR_AP_SM_EVENTS] = {
 407        [AP_SM_STATE_RESET_START] = {
 408                [AP_SM_EVENT_POLL] = ap_sm_reset,
 409                [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
 410        },
 411        [AP_SM_STATE_RESET_WAIT] = {
 412                [AP_SM_EVENT_POLL] = ap_sm_reset_wait,
 413                [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
 414        },
 415        [AP_SM_STATE_SETIRQ_WAIT] = {
 416                [AP_SM_EVENT_POLL] = ap_sm_setirq_wait,
 417                [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
 418        },
 419        [AP_SM_STATE_IDLE] = {
 420                [AP_SM_EVENT_POLL] = ap_sm_write,
 421                [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
 422        },
 423        [AP_SM_STATE_WORKING] = {
 424                [AP_SM_EVENT_POLL] = ap_sm_read_write,
 425                [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
 426        },
 427        [AP_SM_STATE_QUEUE_FULL] = {
 428                [AP_SM_EVENT_POLL] = ap_sm_read,
 429                [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
 430        },
 431};
 432
 433enum ap_sm_wait ap_sm_event(struct ap_queue *aq, enum ap_sm_event event)
 434{
 435        if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
 436                return ap_jumptable[aq->sm_state][event](aq);
 437        else
 438                return AP_SM_WAIT_NONE;
 439}
 440
 441enum ap_sm_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_sm_event event)
 442{
 443        enum ap_sm_wait wait;
 444
 445        while ((wait = ap_sm_event(aq, event)) == AP_SM_WAIT_AGAIN)
 446                ;
 447        return wait;
 448}
 449
 450/*
 451 * AP queue related attributes.
 452 */
 453static ssize_t request_count_show(struct device *dev,
 454                                  struct device_attribute *attr,
 455                                  char *buf)
 456{
 457        struct ap_queue *aq = to_ap_queue(dev);
 458        bool valid = false;
 459        u64 req_cnt;
 460
 461        spin_lock_bh(&aq->lock);
 462        if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
 463                req_cnt = aq->total_request_count;
 464                valid = true;
 465        }
 466        spin_unlock_bh(&aq->lock);
 467
 468        if (valid)
 469                return scnprintf(buf, PAGE_SIZE, "%llu\n", req_cnt);
 470        else
 471                return scnprintf(buf, PAGE_SIZE, "-\n");
 472}
 473
 474static ssize_t request_count_store(struct device *dev,
 475                                   struct device_attribute *attr,
 476                                   const char *buf, size_t count)
 477{
 478        struct ap_queue *aq = to_ap_queue(dev);
 479
 480        spin_lock_bh(&aq->lock);
 481        aq->total_request_count = 0;
 482        spin_unlock_bh(&aq->lock);
 483
 484        return count;
 485}
 486
 487static DEVICE_ATTR_RW(request_count);
 488
 489static ssize_t requestq_count_show(struct device *dev,
 490                                   struct device_attribute *attr, char *buf)
 491{
 492        struct ap_queue *aq = to_ap_queue(dev);
 493        unsigned int reqq_cnt = 0;
 494
 495        spin_lock_bh(&aq->lock);
 496        if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
 497                reqq_cnt = aq->requestq_count;
 498        spin_unlock_bh(&aq->lock);
 499        return scnprintf(buf, PAGE_SIZE, "%d\n", reqq_cnt);
 500}
 501
 502static DEVICE_ATTR_RO(requestq_count);
 503
 504static ssize_t pendingq_count_show(struct device *dev,
 505                                   struct device_attribute *attr, char *buf)
 506{
 507        struct ap_queue *aq = to_ap_queue(dev);
 508        unsigned int penq_cnt = 0;
 509
 510        spin_lock_bh(&aq->lock);
 511        if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
 512                penq_cnt = aq->pendingq_count;
 513        spin_unlock_bh(&aq->lock);
 514        return scnprintf(buf, PAGE_SIZE, "%d\n", penq_cnt);
 515}
 516
 517static DEVICE_ATTR_RO(pendingq_count);
 518
 519static ssize_t reset_show(struct device *dev,
 520                          struct device_attribute *attr, char *buf)
 521{
 522        struct ap_queue *aq = to_ap_queue(dev);
 523        int rc = 0;
 524
 525        spin_lock_bh(&aq->lock);
 526        switch (aq->sm_state) {
 527        case AP_SM_STATE_RESET_START:
 528        case AP_SM_STATE_RESET_WAIT:
 529                rc = scnprintf(buf, PAGE_SIZE, "Reset in progress.\n");
 530                break;
 531        case AP_SM_STATE_WORKING:
 532        case AP_SM_STATE_QUEUE_FULL:
 533                rc = scnprintf(buf, PAGE_SIZE, "Reset Timer armed.\n");
 534                break;
 535        default:
 536                rc = scnprintf(buf, PAGE_SIZE, "No Reset Timer set.\n");
 537        }
 538        spin_unlock_bh(&aq->lock);
 539        return rc;
 540}
 541
 542static ssize_t reset_store(struct device *dev,
 543                           struct device_attribute *attr,
 544                           const char *buf, size_t count)
 545{
 546        struct ap_queue *aq = to_ap_queue(dev);
 547
 548        spin_lock_bh(&aq->lock);
 549        __ap_flush_queue(aq);
 550        aq->sm_state = AP_SM_STATE_RESET_START;
 551        ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
 552        spin_unlock_bh(&aq->lock);
 553
 554        AP_DBF(DBF_INFO, "reset queue=%02x.%04x triggered by user\n",
 555               AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
 556
 557        return count;
 558}
 559
 560static DEVICE_ATTR_RW(reset);
 561
 562static ssize_t interrupt_show(struct device *dev,
 563                              struct device_attribute *attr, char *buf)
 564{
 565        struct ap_queue *aq = to_ap_queue(dev);
 566        int rc = 0;
 567
 568        spin_lock_bh(&aq->lock);
 569        if (aq->sm_state == AP_SM_STATE_SETIRQ_WAIT)
 570                rc = scnprintf(buf, PAGE_SIZE, "Enable Interrupt pending.\n");
 571        else if (aq->interrupt)
 572                rc = scnprintf(buf, PAGE_SIZE, "Interrupts enabled.\n");
 573        else
 574                rc = scnprintf(buf, PAGE_SIZE, "Interrupts disabled.\n");
 575        spin_unlock_bh(&aq->lock);
 576        return rc;
 577}
 578
 579static DEVICE_ATTR_RO(interrupt);
 580
 581static ssize_t config_show(struct device *dev,
 582                             struct device_attribute *attr, char *buf)
 583{
 584        struct ap_queue *aq = to_ap_queue(dev);
 585        int rc;
 586
 587        spin_lock_bh(&aq->lock);
 588        rc = scnprintf(buf, PAGE_SIZE, "%d\n", aq->config ? 1 : 0);
 589        spin_unlock_bh(&aq->lock);
 590        return rc;
 591}
 592
 593static DEVICE_ATTR_RO(config);
 594
 595#ifdef CONFIG_ZCRYPT_DEBUG
 596static ssize_t states_show(struct device *dev,
 597                           struct device_attribute *attr, char *buf)
 598{
 599        struct ap_queue *aq = to_ap_queue(dev);
 600        int rc = 0;
 601
 602        spin_lock_bh(&aq->lock);
 603        /* queue device state */
 604        switch (aq->dev_state) {
 605        case AP_DEV_STATE_UNINITIATED:
 606                rc = scnprintf(buf, PAGE_SIZE, "UNINITIATED\n");
 607                break;
 608        case AP_DEV_STATE_OPERATING:
 609                rc = scnprintf(buf, PAGE_SIZE, "OPERATING");
 610                break;
 611        case AP_DEV_STATE_SHUTDOWN:
 612                rc = scnprintf(buf, PAGE_SIZE, "SHUTDOWN");
 613                break;
 614        case AP_DEV_STATE_ERROR:
 615                rc = scnprintf(buf, PAGE_SIZE, "ERROR");
 616                break;
 617        default:
 618                rc = scnprintf(buf, PAGE_SIZE, "UNKNOWN");
 619        }
 620        /* state machine state */
 621        if (aq->dev_state) {
 622                switch (aq->sm_state) {
 623                case AP_SM_STATE_RESET_START:
 624                        rc += scnprintf(buf + rc, PAGE_SIZE - rc,
 625                                        " [RESET_START]\n");
 626                        break;
 627                case AP_SM_STATE_RESET_WAIT:
 628                        rc += scnprintf(buf + rc, PAGE_SIZE - rc,
 629                                        " [RESET_WAIT]\n");
 630                        break;
 631                case AP_SM_STATE_SETIRQ_WAIT:
 632                        rc += scnprintf(buf + rc, PAGE_SIZE - rc,
 633                                        " [SETIRQ_WAIT]\n");
 634                        break;
 635                case AP_SM_STATE_IDLE:
 636                        rc += scnprintf(buf + rc, PAGE_SIZE - rc,
 637                                        " [IDLE]\n");
 638                        break;
 639                case AP_SM_STATE_WORKING:
 640                        rc += scnprintf(buf + rc, PAGE_SIZE - rc,
 641                                        " [WORKING]\n");
 642                        break;
 643                case AP_SM_STATE_QUEUE_FULL:
 644                        rc += scnprintf(buf + rc, PAGE_SIZE - rc,
 645                                        " [FULL]\n");
 646                        break;
 647                default:
 648                        rc += scnprintf(buf + rc, PAGE_SIZE - rc,
 649                                        " [UNKNOWN]\n");
 650                }
 651        }
 652        spin_unlock_bh(&aq->lock);
 653
 654        return rc;
 655}
 656static DEVICE_ATTR_RO(states);
 657
 658static ssize_t last_err_rc_show(struct device *dev,
 659                                struct device_attribute *attr, char *buf)
 660{
 661        struct ap_queue *aq = to_ap_queue(dev);
 662        int rc;
 663
 664        spin_lock_bh(&aq->lock);
 665        rc = aq->last_err_rc;
 666        spin_unlock_bh(&aq->lock);
 667
 668        switch (rc) {
 669        case AP_RESPONSE_NORMAL:
 670                return scnprintf(buf, PAGE_SIZE, "NORMAL\n");
 671        case AP_RESPONSE_Q_NOT_AVAIL:
 672                return scnprintf(buf, PAGE_SIZE, "Q_NOT_AVAIL\n");
 673        case AP_RESPONSE_RESET_IN_PROGRESS:
 674                return scnprintf(buf, PAGE_SIZE, "RESET_IN_PROGRESS\n");
 675        case AP_RESPONSE_DECONFIGURED:
 676                return scnprintf(buf, PAGE_SIZE, "DECONFIGURED\n");
 677        case AP_RESPONSE_CHECKSTOPPED:
 678                return scnprintf(buf, PAGE_SIZE, "CHECKSTOPPED\n");
 679        case AP_RESPONSE_BUSY:
 680                return scnprintf(buf, PAGE_SIZE, "BUSY\n");
 681        case AP_RESPONSE_INVALID_ADDRESS:
 682                return scnprintf(buf, PAGE_SIZE, "INVALID_ADDRESS\n");
 683        case AP_RESPONSE_OTHERWISE_CHANGED:
 684                return scnprintf(buf, PAGE_SIZE, "OTHERWISE_CHANGED\n");
 685        case AP_RESPONSE_Q_FULL:
 686                return scnprintf(buf, PAGE_SIZE, "Q_FULL/NO_PENDING_REPLY\n");
 687        case AP_RESPONSE_INDEX_TOO_BIG:
 688                return scnprintf(buf, PAGE_SIZE, "INDEX_TOO_BIG\n");
 689        case AP_RESPONSE_NO_FIRST_PART:
 690                return scnprintf(buf, PAGE_SIZE, "NO_FIRST_PART\n");
 691        case AP_RESPONSE_MESSAGE_TOO_BIG:
 692                return scnprintf(buf, PAGE_SIZE, "MESSAGE_TOO_BIG\n");
 693        case AP_RESPONSE_REQ_FAC_NOT_INST:
 694                return scnprintf(buf, PAGE_SIZE, "REQ_FAC_NOT_INST\n");
 695        default:
 696                return scnprintf(buf, PAGE_SIZE, "response code %d\n", rc);
 697        }
 698}
 699static DEVICE_ATTR_RO(last_err_rc);
 700#endif
 701
 702static struct attribute *ap_queue_dev_attrs[] = {
 703        &dev_attr_request_count.attr,
 704        &dev_attr_requestq_count.attr,
 705        &dev_attr_pendingq_count.attr,
 706        &dev_attr_reset.attr,
 707        &dev_attr_interrupt.attr,
 708        &dev_attr_config.attr,
 709#ifdef CONFIG_ZCRYPT_DEBUG
 710        &dev_attr_states.attr,
 711        &dev_attr_last_err_rc.attr,
 712#endif
 713        NULL
 714};
 715
 716static struct attribute_group ap_queue_dev_attr_group = {
 717        .attrs = ap_queue_dev_attrs
 718};
 719
 720static const struct attribute_group *ap_queue_dev_attr_groups[] = {
 721        &ap_queue_dev_attr_group,
 722        NULL
 723};
 724
 725static struct device_type ap_queue_type = {
 726        .name = "ap_queue",
 727        .groups = ap_queue_dev_attr_groups,
 728};
 729
 730static void ap_queue_device_release(struct device *dev)
 731{
 732        struct ap_queue *aq = to_ap_queue(dev);
 733
 734        spin_lock_bh(&ap_queues_lock);
 735        hash_del(&aq->hnode);
 736        spin_unlock_bh(&ap_queues_lock);
 737
 738        kfree(aq);
 739}
 740
 741struct ap_queue *ap_queue_create(ap_qid_t qid, int device_type)
 742{
 743        struct ap_queue *aq;
 744
 745        aq = kzalloc(sizeof(*aq), GFP_KERNEL);
 746        if (!aq)
 747                return NULL;
 748        aq->ap_dev.device.release = ap_queue_device_release;
 749        aq->ap_dev.device.type = &ap_queue_type;
 750        aq->ap_dev.device_type = device_type;
 751        aq->qid = qid;
 752        aq->interrupt = false;
 753        spin_lock_init(&aq->lock);
 754        INIT_LIST_HEAD(&aq->pendingq);
 755        INIT_LIST_HEAD(&aq->requestq);
 756        timer_setup(&aq->timeout, ap_request_timeout, 0);
 757
 758        return aq;
 759}
 760
 761void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *reply)
 762{
 763        aq->reply = reply;
 764
 765        spin_lock_bh(&aq->lock);
 766        ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
 767        spin_unlock_bh(&aq->lock);
 768}
 769EXPORT_SYMBOL(ap_queue_init_reply);
 770
 771/**
 772 * ap_queue_message(): Queue a request to an AP device.
 773 * @aq: The AP device to queue the message to
 774 * @ap_msg: The message that is to be added
 775 */
 776int ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg)
 777{
 778        int rc = 0;
 779
 780        /* msg needs to have a valid receive-callback */
 781        BUG_ON(!ap_msg->receive);
 782
 783        spin_lock_bh(&aq->lock);
 784
 785        /* only allow to queue new messages if device state is ok */
 786        if (aq->dev_state == AP_DEV_STATE_OPERATING) {
 787                list_add_tail(&ap_msg->list, &aq->requestq);
 788                aq->requestq_count++;
 789                aq->total_request_count++;
 790                atomic64_inc(&aq->card->total_request_count);
 791        } else
 792                rc = -ENODEV;
 793
 794        /* Send/receive as many request from the queue as possible. */
 795        ap_wait(ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
 796
 797        spin_unlock_bh(&aq->lock);
 798
 799        return rc;
 800}
 801EXPORT_SYMBOL(ap_queue_message);
 802
 803/**
 804 * ap_cancel_message(): Cancel a crypto request.
 805 * @aq: The AP device that has the message queued
 806 * @ap_msg: The message that is to be removed
 807 *
 808 * Cancel a crypto request. This is done by removing the request
 809 * from the device pending or request queue. Note that the
 810 * request stays on the AP queue. When it finishes the message
 811 * reply will be discarded because the psmid can't be found.
 812 */
 813void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg)
 814{
 815        struct ap_message *tmp;
 816
 817        spin_lock_bh(&aq->lock);
 818        if (!list_empty(&ap_msg->list)) {
 819                list_for_each_entry(tmp, &aq->pendingq, list)
 820                        if (tmp->psmid == ap_msg->psmid) {
 821                                aq->pendingq_count--;
 822                                goto found;
 823                        }
 824                aq->requestq_count--;
 825found:
 826                list_del_init(&ap_msg->list);
 827        }
 828        spin_unlock_bh(&aq->lock);
 829}
 830EXPORT_SYMBOL(ap_cancel_message);
 831
 832/**
 833 * __ap_flush_queue(): Flush requests.
 834 * @aq: Pointer to the AP queue
 835 *
 836 * Flush all requests from the request/pending queue of an AP device.
 837 */
 838static void __ap_flush_queue(struct ap_queue *aq)
 839{
 840        struct ap_message *ap_msg, *next;
 841
 842        list_for_each_entry_safe(ap_msg, next, &aq->pendingq, list) {
 843                list_del_init(&ap_msg->list);
 844                aq->pendingq_count--;
 845                ap_msg->rc = -EAGAIN;
 846                ap_msg->receive(aq, ap_msg, NULL);
 847        }
 848        list_for_each_entry_safe(ap_msg, next, &aq->requestq, list) {
 849                list_del_init(&ap_msg->list);
 850                aq->requestq_count--;
 851                ap_msg->rc = -EAGAIN;
 852                ap_msg->receive(aq, ap_msg, NULL);
 853        }
 854        aq->queue_count = 0;
 855}
 856
 857void ap_flush_queue(struct ap_queue *aq)
 858{
 859        spin_lock_bh(&aq->lock);
 860        __ap_flush_queue(aq);
 861        spin_unlock_bh(&aq->lock);
 862}
 863EXPORT_SYMBOL(ap_flush_queue);
 864
 865void ap_queue_prepare_remove(struct ap_queue *aq)
 866{
 867        spin_lock_bh(&aq->lock);
 868        /* flush queue */
 869        __ap_flush_queue(aq);
 870        /* move queue device state to SHUTDOWN in progress */
 871        aq->dev_state = AP_DEV_STATE_SHUTDOWN;
 872        spin_unlock_bh(&aq->lock);
 873        del_timer_sync(&aq->timeout);
 874}
 875
 876void ap_queue_remove(struct ap_queue *aq)
 877{
 878        /*
 879         * all messages have been flushed and the device state
 880         * is SHUTDOWN. Now reset with zero which also clears
 881         * the irq registration and move the device state
 882         * to the initial value AP_DEV_STATE_UNINITIATED.
 883         */
 884        spin_lock_bh(&aq->lock);
 885        ap_zapq(aq->qid);
 886        aq->dev_state = AP_DEV_STATE_UNINITIATED;
 887        spin_unlock_bh(&aq->lock);
 888}
 889
 890void ap_queue_init_state(struct ap_queue *aq)
 891{
 892        spin_lock_bh(&aq->lock);
 893        aq->dev_state = AP_DEV_STATE_OPERATING;
 894        aq->sm_state = AP_SM_STATE_RESET_START;
 895        ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
 896        spin_unlock_bh(&aq->lock);
 897}
 898EXPORT_SYMBOL(ap_queue_init_state);
 899