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_interruption(): Enable interruption on an 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_interruption(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          unsigned int special)
  73{
  74        if (special == 1)
  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_wait ap_sm_nop(struct ap_queue *aq)
 123{
 124        return AP_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_WAIT_NONE, AP_WAIT_AGAIN, or AP_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
 139        status = ap_dqap(aq->qid, &aq->reply->psmid,
 140                         aq->reply->message, aq->reply->length);
 141        switch (status.response_code) {
 142        case AP_RESPONSE_NORMAL:
 143                aq->queue_count--;
 144                if (aq->queue_count > 0)
 145                        mod_timer(&aq->timeout,
 146                                  jiffies + aq->request_timeout);
 147                list_for_each_entry(ap_msg, &aq->pendingq, list) {
 148                        if (ap_msg->psmid != aq->reply->psmid)
 149                                continue;
 150                        list_del_init(&ap_msg->list);
 151                        aq->pendingq_count--;
 152                        ap_msg->receive(aq, ap_msg, aq->reply);
 153                        break;
 154                }
 155                fallthrough;
 156        case AP_RESPONSE_NO_PENDING_REPLY:
 157                if (!status.queue_empty || aq->queue_count <= 0)
 158                        break;
 159                /* The card shouldn't forget requests but who knows. */
 160                aq->queue_count = 0;
 161                list_splice_init(&aq->pendingq, &aq->requestq);
 162                aq->requestq_count += aq->pendingq_count;
 163                aq->pendingq_count = 0;
 164                break;
 165        default:
 166                break;
 167        }
 168        return status;
 169}
 170
 171/**
 172 * ap_sm_read(): Receive pending reply messages from an AP queue.
 173 * @aq: pointer to the AP queue
 174 *
 175 * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
 176 */
 177static enum ap_wait ap_sm_read(struct ap_queue *aq)
 178{
 179        struct ap_queue_status status;
 180
 181        if (!aq->reply)
 182                return AP_WAIT_NONE;
 183        status = ap_sm_recv(aq);
 184        switch (status.response_code) {
 185        case AP_RESPONSE_NORMAL:
 186                if (aq->queue_count > 0) {
 187                        aq->state = AP_STATE_WORKING;
 188                        return AP_WAIT_AGAIN;
 189                }
 190                aq->state = AP_STATE_IDLE;
 191                return AP_WAIT_NONE;
 192        case AP_RESPONSE_NO_PENDING_REPLY:
 193                if (aq->queue_count > 0)
 194                        return AP_WAIT_INTERRUPT;
 195                aq->state = AP_STATE_IDLE;
 196                return AP_WAIT_NONE;
 197        default:
 198                aq->state = AP_STATE_BORKED;
 199                return AP_WAIT_NONE;
 200        }
 201}
 202
 203/**
 204 * ap_sm_write(): Send messages from the request queue to an AP queue.
 205 * @aq: pointer to the AP queue
 206 *
 207 * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
 208 */
 209static enum ap_wait ap_sm_write(struct ap_queue *aq)
 210{
 211        struct ap_queue_status status;
 212        struct ap_message *ap_msg;
 213
 214        if (aq->requestq_count <= 0)
 215                return AP_WAIT_NONE;
 216        /* Start the next request on the queue. */
 217        ap_msg = list_entry(aq->requestq.next, struct ap_message, list);
 218        status = __ap_send(aq->qid, ap_msg->psmid,
 219                           ap_msg->message, ap_msg->length, ap_msg->special);
 220        switch (status.response_code) {
 221        case AP_RESPONSE_NORMAL:
 222                aq->queue_count++;
 223                if (aq->queue_count == 1)
 224                        mod_timer(&aq->timeout, jiffies + aq->request_timeout);
 225                list_move_tail(&ap_msg->list, &aq->pendingq);
 226                aq->requestq_count--;
 227                aq->pendingq_count++;
 228                if (aq->queue_count < aq->card->queue_depth) {
 229                        aq->state = AP_STATE_WORKING;
 230                        return AP_WAIT_AGAIN;
 231                }
 232                fallthrough;
 233        case AP_RESPONSE_Q_FULL:
 234                aq->state = AP_STATE_QUEUE_FULL;
 235                return AP_WAIT_INTERRUPT;
 236        case AP_RESPONSE_RESET_IN_PROGRESS:
 237                aq->state = AP_STATE_RESET_WAIT;
 238                return AP_WAIT_TIMEOUT;
 239        case AP_RESPONSE_MESSAGE_TOO_BIG:
 240        case AP_RESPONSE_REQ_FAC_NOT_INST:
 241                list_del_init(&ap_msg->list);
 242                aq->requestq_count--;
 243                ap_msg->rc = -EINVAL;
 244                ap_msg->receive(aq, ap_msg, NULL);
 245                return AP_WAIT_AGAIN;
 246        default:
 247                aq->state = AP_STATE_BORKED;
 248                return AP_WAIT_NONE;
 249        }
 250}
 251
 252/**
 253 * ap_sm_read_write(): Send and receive messages to/from an AP queue.
 254 * @aq: pointer to the AP queue
 255 *
 256 * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
 257 */
 258static enum ap_wait ap_sm_read_write(struct ap_queue *aq)
 259{
 260        return min(ap_sm_read(aq), ap_sm_write(aq));
 261}
 262
 263/**
 264 * ap_sm_reset(): Reset an AP queue.
 265 * @qid: The AP queue number
 266 *
 267 * Submit the Reset command to an AP queue.
 268 */
 269static enum ap_wait ap_sm_reset(struct ap_queue *aq)
 270{
 271        struct ap_queue_status status;
 272
 273        status = ap_rapq(aq->qid);
 274        switch (status.response_code) {
 275        case AP_RESPONSE_NORMAL:
 276        case AP_RESPONSE_RESET_IN_PROGRESS:
 277                aq->state = AP_STATE_RESET_WAIT;
 278                aq->interrupt = AP_INTR_DISABLED;
 279                return AP_WAIT_TIMEOUT;
 280        case AP_RESPONSE_BUSY:
 281                return AP_WAIT_TIMEOUT;
 282        case AP_RESPONSE_Q_NOT_AVAIL:
 283        case AP_RESPONSE_DECONFIGURED:
 284        case AP_RESPONSE_CHECKSTOPPED:
 285        default:
 286                aq->state = AP_STATE_BORKED;
 287                return AP_WAIT_NONE;
 288        }
 289}
 290
 291/**
 292 * ap_sm_reset_wait(): Test queue for completion of the reset operation
 293 * @aq: pointer to the AP queue
 294 *
 295 * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
 296 */
 297static enum ap_wait ap_sm_reset_wait(struct ap_queue *aq)
 298{
 299        struct ap_queue_status status;
 300        void *lsi_ptr;
 301
 302        if (aq->queue_count > 0 && aq->reply)
 303                /* Try to read a completed message and get the status */
 304                status = ap_sm_recv(aq);
 305        else
 306                /* Get the status with TAPQ */
 307                status = ap_tapq(aq->qid, NULL);
 308
 309        switch (status.response_code) {
 310        case AP_RESPONSE_NORMAL:
 311                lsi_ptr = ap_airq_ptr();
 312                if (lsi_ptr && ap_queue_enable_interruption(aq, lsi_ptr) == 0)
 313                        aq->state = AP_STATE_SETIRQ_WAIT;
 314                else
 315                        aq->state = (aq->queue_count > 0) ?
 316                                AP_STATE_WORKING : AP_STATE_IDLE;
 317                return AP_WAIT_AGAIN;
 318        case AP_RESPONSE_BUSY:
 319        case AP_RESPONSE_RESET_IN_PROGRESS:
 320                return AP_WAIT_TIMEOUT;
 321        case AP_RESPONSE_Q_NOT_AVAIL:
 322        case AP_RESPONSE_DECONFIGURED:
 323        case AP_RESPONSE_CHECKSTOPPED:
 324        default:
 325                aq->state = AP_STATE_BORKED;
 326                return AP_WAIT_NONE;
 327        }
 328}
 329
 330/**
 331 * ap_sm_setirq_wait(): Test queue for completion of the irq enablement
 332 * @aq: pointer to the AP queue
 333 *
 334 * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
 335 */
 336static enum ap_wait ap_sm_setirq_wait(struct ap_queue *aq)
 337{
 338        struct ap_queue_status status;
 339
 340        if (aq->queue_count > 0 && aq->reply)
 341                /* Try to read a completed message and get the status */
 342                status = ap_sm_recv(aq);
 343        else
 344                /* Get the status with TAPQ */
 345                status = ap_tapq(aq->qid, NULL);
 346
 347        if (status.irq_enabled == 1) {
 348                /* Irqs are now enabled */
 349                aq->interrupt = AP_INTR_ENABLED;
 350                aq->state = (aq->queue_count > 0) ?
 351                        AP_STATE_WORKING : AP_STATE_IDLE;
 352        }
 353
 354        switch (status.response_code) {
 355        case AP_RESPONSE_NORMAL:
 356                if (aq->queue_count > 0)
 357                        return AP_WAIT_AGAIN;
 358                fallthrough;
 359        case AP_RESPONSE_NO_PENDING_REPLY:
 360                return AP_WAIT_TIMEOUT;
 361        default:
 362                aq->state = AP_STATE_BORKED;
 363                return AP_WAIT_NONE;
 364        }
 365}
 366
 367/*
 368 * AP state machine jump table
 369 */
 370static ap_func_t *ap_jumptable[NR_AP_STATES][NR_AP_EVENTS] = {
 371        [AP_STATE_RESET_START] = {
 372                [AP_EVENT_POLL] = ap_sm_reset,
 373                [AP_EVENT_TIMEOUT] = ap_sm_nop,
 374        },
 375        [AP_STATE_RESET_WAIT] = {
 376                [AP_EVENT_POLL] = ap_sm_reset_wait,
 377                [AP_EVENT_TIMEOUT] = ap_sm_nop,
 378        },
 379        [AP_STATE_SETIRQ_WAIT] = {
 380                [AP_EVENT_POLL] = ap_sm_setirq_wait,
 381                [AP_EVENT_TIMEOUT] = ap_sm_nop,
 382        },
 383        [AP_STATE_IDLE] = {
 384                [AP_EVENT_POLL] = ap_sm_write,
 385                [AP_EVENT_TIMEOUT] = ap_sm_nop,
 386        },
 387        [AP_STATE_WORKING] = {
 388                [AP_EVENT_POLL] = ap_sm_read_write,
 389                [AP_EVENT_TIMEOUT] = ap_sm_reset,
 390        },
 391        [AP_STATE_QUEUE_FULL] = {
 392                [AP_EVENT_POLL] = ap_sm_read,
 393                [AP_EVENT_TIMEOUT] = ap_sm_reset,
 394        },
 395        [AP_STATE_REMOVE] = {
 396                [AP_EVENT_POLL] = ap_sm_nop,
 397                [AP_EVENT_TIMEOUT] = ap_sm_nop,
 398        },
 399        [AP_STATE_UNBOUND] = {
 400                [AP_EVENT_POLL] = ap_sm_nop,
 401                [AP_EVENT_TIMEOUT] = ap_sm_nop,
 402        },
 403        [AP_STATE_BORKED] = {
 404                [AP_EVENT_POLL] = ap_sm_nop,
 405                [AP_EVENT_TIMEOUT] = ap_sm_nop,
 406        },
 407};
 408
 409enum ap_wait ap_sm_event(struct ap_queue *aq, enum ap_event event)
 410{
 411        return ap_jumptable[aq->state][event](aq);
 412}
 413
 414enum ap_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_event event)
 415{
 416        enum ap_wait wait;
 417
 418        while ((wait = ap_sm_event(aq, event)) == AP_WAIT_AGAIN)
 419                ;
 420        return wait;
 421}
 422
 423/*
 424 * AP queue related attributes.
 425 */
 426static ssize_t request_count_show(struct device *dev,
 427                                  struct device_attribute *attr,
 428                                  char *buf)
 429{
 430        struct ap_queue *aq = to_ap_queue(dev);
 431        u64 req_cnt;
 432
 433        spin_lock_bh(&aq->lock);
 434        req_cnt = aq->total_request_count;
 435        spin_unlock_bh(&aq->lock);
 436        return scnprintf(buf, PAGE_SIZE, "%llu\n", req_cnt);
 437}
 438
 439static ssize_t request_count_store(struct device *dev,
 440                                   struct device_attribute *attr,
 441                                   const char *buf, size_t count)
 442{
 443        struct ap_queue *aq = to_ap_queue(dev);
 444
 445        spin_lock_bh(&aq->lock);
 446        aq->total_request_count = 0;
 447        spin_unlock_bh(&aq->lock);
 448
 449        return count;
 450}
 451
 452static DEVICE_ATTR_RW(request_count);
 453
 454static ssize_t requestq_count_show(struct device *dev,
 455                                   struct device_attribute *attr, char *buf)
 456{
 457        struct ap_queue *aq = to_ap_queue(dev);
 458        unsigned int reqq_cnt = 0;
 459
 460        spin_lock_bh(&aq->lock);
 461        reqq_cnt = aq->requestq_count;
 462        spin_unlock_bh(&aq->lock);
 463        return scnprintf(buf, PAGE_SIZE, "%d\n", reqq_cnt);
 464}
 465
 466static DEVICE_ATTR_RO(requestq_count);
 467
 468static ssize_t pendingq_count_show(struct device *dev,
 469                                   struct device_attribute *attr, char *buf)
 470{
 471        struct ap_queue *aq = to_ap_queue(dev);
 472        unsigned int penq_cnt = 0;
 473
 474        spin_lock_bh(&aq->lock);
 475        penq_cnt = aq->pendingq_count;
 476        spin_unlock_bh(&aq->lock);
 477        return scnprintf(buf, PAGE_SIZE, "%d\n", penq_cnt);
 478}
 479
 480static DEVICE_ATTR_RO(pendingq_count);
 481
 482static ssize_t reset_show(struct device *dev,
 483                          struct device_attribute *attr, char *buf)
 484{
 485        struct ap_queue *aq = to_ap_queue(dev);
 486        int rc = 0;
 487
 488        spin_lock_bh(&aq->lock);
 489        switch (aq->state) {
 490        case AP_STATE_RESET_START:
 491        case AP_STATE_RESET_WAIT:
 492                rc = scnprintf(buf, PAGE_SIZE, "Reset in progress.\n");
 493                break;
 494        case AP_STATE_WORKING:
 495        case AP_STATE_QUEUE_FULL:
 496                rc = scnprintf(buf, PAGE_SIZE, "Reset Timer armed.\n");
 497                break;
 498        default:
 499                rc = scnprintf(buf, PAGE_SIZE, "No Reset Timer set.\n");
 500        }
 501        spin_unlock_bh(&aq->lock);
 502        return rc;
 503}
 504
 505static ssize_t reset_store(struct device *dev,
 506                           struct device_attribute *attr,
 507                           const char *buf, size_t count)
 508{
 509        struct ap_queue *aq = to_ap_queue(dev);
 510
 511        spin_lock_bh(&aq->lock);
 512        __ap_flush_queue(aq);
 513        aq->state = AP_STATE_RESET_START;
 514        ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
 515        spin_unlock_bh(&aq->lock);
 516
 517        AP_DBF(DBF_INFO, "reset queue=%02x.%04x triggered by user\n",
 518               AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
 519
 520        return count;
 521}
 522
 523static DEVICE_ATTR_RW(reset);
 524
 525static ssize_t interrupt_show(struct device *dev,
 526                              struct device_attribute *attr, char *buf)
 527{
 528        struct ap_queue *aq = to_ap_queue(dev);
 529        int rc = 0;
 530
 531        spin_lock_bh(&aq->lock);
 532        if (aq->state == AP_STATE_SETIRQ_WAIT)
 533                rc = scnprintf(buf, PAGE_SIZE, "Enable Interrupt pending.\n");
 534        else if (aq->interrupt == AP_INTR_ENABLED)
 535                rc = scnprintf(buf, PAGE_SIZE, "Interrupts enabled.\n");
 536        else
 537                rc = scnprintf(buf, PAGE_SIZE, "Interrupts disabled.\n");
 538        spin_unlock_bh(&aq->lock);
 539        return rc;
 540}
 541
 542static DEVICE_ATTR_RO(interrupt);
 543
 544static struct attribute *ap_queue_dev_attrs[] = {
 545        &dev_attr_request_count.attr,
 546        &dev_attr_requestq_count.attr,
 547        &dev_attr_pendingq_count.attr,
 548        &dev_attr_reset.attr,
 549        &dev_attr_interrupt.attr,
 550        NULL
 551};
 552
 553static struct attribute_group ap_queue_dev_attr_group = {
 554        .attrs = ap_queue_dev_attrs
 555};
 556
 557static const struct attribute_group *ap_queue_dev_attr_groups[] = {
 558        &ap_queue_dev_attr_group,
 559        NULL
 560};
 561
 562static struct device_type ap_queue_type = {
 563        .name = "ap_queue",
 564        .groups = ap_queue_dev_attr_groups,
 565};
 566
 567static void ap_queue_device_release(struct device *dev)
 568{
 569        struct ap_queue *aq = to_ap_queue(dev);
 570
 571        if (!list_empty(&aq->list)) {
 572                spin_lock_bh(&ap_list_lock);
 573                list_del_init(&aq->list);
 574                spin_unlock_bh(&ap_list_lock);
 575        }
 576        kfree(aq);
 577}
 578
 579struct ap_queue *ap_queue_create(ap_qid_t qid, int device_type)
 580{
 581        struct ap_queue *aq;
 582
 583        aq = kzalloc(sizeof(*aq), GFP_KERNEL);
 584        if (!aq)
 585                return NULL;
 586        aq->ap_dev.device.release = ap_queue_device_release;
 587        aq->ap_dev.device.type = &ap_queue_type;
 588        aq->ap_dev.device_type = device_type;
 589        aq->qid = qid;
 590        aq->state = AP_STATE_UNBOUND;
 591        aq->interrupt = AP_INTR_DISABLED;
 592        spin_lock_init(&aq->lock);
 593        INIT_LIST_HEAD(&aq->list);
 594        INIT_LIST_HEAD(&aq->pendingq);
 595        INIT_LIST_HEAD(&aq->requestq);
 596        timer_setup(&aq->timeout, ap_request_timeout, 0);
 597
 598        return aq;
 599}
 600
 601void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *reply)
 602{
 603        aq->reply = reply;
 604
 605        spin_lock_bh(&aq->lock);
 606        ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
 607        spin_unlock_bh(&aq->lock);
 608}
 609EXPORT_SYMBOL(ap_queue_init_reply);
 610
 611/**
 612 * ap_queue_message(): Queue a request to an AP device.
 613 * @aq: The AP device to queue the message to
 614 * @ap_msg: The message that is to be added
 615 */
 616void ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg)
 617{
 618        /* For asynchronous message handling a valid receive-callback
 619         * is required.
 620         */
 621        BUG_ON(!ap_msg->receive);
 622
 623        spin_lock_bh(&aq->lock);
 624        /* Queue the message. */
 625        list_add_tail(&ap_msg->list, &aq->requestq);
 626        aq->requestq_count++;
 627        aq->total_request_count++;
 628        atomic64_inc(&aq->card->total_request_count);
 629        /* Send/receive as many request from the queue as possible. */
 630        ap_wait(ap_sm_event_loop(aq, AP_EVENT_POLL));
 631        spin_unlock_bh(&aq->lock);
 632}
 633EXPORT_SYMBOL(ap_queue_message);
 634
 635/**
 636 * ap_cancel_message(): Cancel a crypto request.
 637 * @aq: The AP device that has the message queued
 638 * @ap_msg: The message that is to be removed
 639 *
 640 * Cancel a crypto request. This is done by removing the request
 641 * from the device pending or request queue. Note that the
 642 * request stays on the AP queue. When it finishes the message
 643 * reply will be discarded because the psmid can't be found.
 644 */
 645void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg)
 646{
 647        struct ap_message *tmp;
 648
 649        spin_lock_bh(&aq->lock);
 650        if (!list_empty(&ap_msg->list)) {
 651                list_for_each_entry(tmp, &aq->pendingq, list)
 652                        if (tmp->psmid == ap_msg->psmid) {
 653                                aq->pendingq_count--;
 654                                goto found;
 655                        }
 656                aq->requestq_count--;
 657found:
 658                list_del_init(&ap_msg->list);
 659        }
 660        spin_unlock_bh(&aq->lock);
 661}
 662EXPORT_SYMBOL(ap_cancel_message);
 663
 664/**
 665 * __ap_flush_queue(): Flush requests.
 666 * @aq: Pointer to the AP queue
 667 *
 668 * Flush all requests from the request/pending queue of an AP device.
 669 */
 670static void __ap_flush_queue(struct ap_queue *aq)
 671{
 672        struct ap_message *ap_msg, *next;
 673
 674        list_for_each_entry_safe(ap_msg, next, &aq->pendingq, list) {
 675                list_del_init(&ap_msg->list);
 676                aq->pendingq_count--;
 677                ap_msg->rc = -EAGAIN;
 678                ap_msg->receive(aq, ap_msg, NULL);
 679        }
 680        list_for_each_entry_safe(ap_msg, next, &aq->requestq, list) {
 681                list_del_init(&ap_msg->list);
 682                aq->requestq_count--;
 683                ap_msg->rc = -EAGAIN;
 684                ap_msg->receive(aq, ap_msg, NULL);
 685        }
 686        aq->queue_count = 0;
 687}
 688
 689void ap_flush_queue(struct ap_queue *aq)
 690{
 691        spin_lock_bh(&aq->lock);
 692        __ap_flush_queue(aq);
 693        spin_unlock_bh(&aq->lock);
 694}
 695EXPORT_SYMBOL(ap_flush_queue);
 696
 697void ap_queue_prepare_remove(struct ap_queue *aq)
 698{
 699        spin_lock_bh(&aq->lock);
 700        /* flush queue */
 701        __ap_flush_queue(aq);
 702        /* set REMOVE state to prevent new messages are queued in */
 703        aq->state = AP_STATE_REMOVE;
 704        spin_unlock_bh(&aq->lock);
 705        del_timer_sync(&aq->timeout);
 706}
 707
 708void ap_queue_remove(struct ap_queue *aq)
 709{
 710        /*
 711         * all messages have been flushed and the state is
 712         * AP_STATE_REMOVE. Now reset with zero which also
 713         * clears the irq registration and move the state
 714         * to AP_STATE_UNBOUND to signal that this queue
 715         * is not used by any driver currently.
 716         */
 717        spin_lock_bh(&aq->lock);
 718        ap_zapq(aq->qid);
 719        aq->state = AP_STATE_UNBOUND;
 720        spin_unlock_bh(&aq->lock);
 721}
 722
 723void ap_queue_init_state(struct ap_queue *aq)
 724{
 725        spin_lock_bh(&aq->lock);
 726        aq->state = AP_STATE_RESET_START;
 727        ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
 728        spin_unlock_bh(&aq->lock);
 729}
 730EXPORT_SYMBOL(ap_queue_init_state);
 731