linux/drivers/s390/crypto/zcrypt_cex4.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 *  Copyright IBM Corp. 2012, 2019
   4 *  Author(s): Holger Dengler <hd@linux.vnet.ibm.com>
   5 */
   6
   7#include <linux/module.h>
   8#include <linux/slab.h>
   9#include <linux/init.h>
  10#include <linux/err.h>
  11#include <linux/atomic.h>
  12#include <linux/uaccess.h>
  13#include <linux/mod_devicetable.h>
  14
  15#include "ap_bus.h"
  16#include "zcrypt_api.h"
  17#include "zcrypt_msgtype6.h"
  18#include "zcrypt_msgtype50.h"
  19#include "zcrypt_error.h"
  20#include "zcrypt_cex4.h"
  21#include "zcrypt_ccamisc.h"
  22#include "zcrypt_ep11misc.h"
  23
  24#define CEX4A_MIN_MOD_SIZE        1     /*    8 bits    */
  25#define CEX4A_MAX_MOD_SIZE_2K   256     /* 2048 bits    */
  26#define CEX4A_MAX_MOD_SIZE_4K   512     /* 4096 bits    */
  27
  28#define CEX4C_MIN_MOD_SIZE       16     /*  256 bits    */
  29#define CEX4C_MAX_MOD_SIZE      512     /* 4096 bits    */
  30
  31#define CEX4A_MAX_MESSAGE_SIZE  MSGTYPE50_CRB3_MAX_MSG_SIZE
  32#define CEX4C_MAX_MESSAGE_SIZE  MSGTYPE06_MAX_MSG_SIZE
  33
  34/* Waiting time for requests to be processed.
  35 * Currently there are some types of request which are not deterministic.
  36 * But the maximum time limit managed by the stomper code is set to 60sec.
  37 * Hence we have to wait at least that time period.
  38 */
  39#define CEX4_CLEANUP_TIME       (900*HZ)
  40
  41MODULE_AUTHOR("IBM Corporation");
  42MODULE_DESCRIPTION("CEX4/CEX5/CEX6/CEX7 Cryptographic Card device driver, " \
  43                   "Copyright IBM Corp. 2019");
  44MODULE_LICENSE("GPL");
  45
  46static struct ap_device_id zcrypt_cex4_card_ids[] = {
  47        { .dev_type = AP_DEVICE_TYPE_CEX4,
  48          .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  49        { .dev_type = AP_DEVICE_TYPE_CEX5,
  50          .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  51        { .dev_type = AP_DEVICE_TYPE_CEX6,
  52          .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  53        { .dev_type = AP_DEVICE_TYPE_CEX7,
  54          .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  55        { /* end of list */ },
  56};
  57
  58MODULE_DEVICE_TABLE(ap, zcrypt_cex4_card_ids);
  59
  60static struct ap_device_id zcrypt_cex4_queue_ids[] = {
  61        { .dev_type = AP_DEVICE_TYPE_CEX4,
  62          .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  63        { .dev_type = AP_DEVICE_TYPE_CEX5,
  64          .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  65        { .dev_type = AP_DEVICE_TYPE_CEX6,
  66          .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  67        { .dev_type = AP_DEVICE_TYPE_CEX7,
  68          .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  69        { /* end of list */ },
  70};
  71
  72MODULE_DEVICE_TABLE(ap, zcrypt_cex4_queue_ids);
  73
  74/*
  75 * CCA card additional device attributes
  76 */
  77static ssize_t cca_serialnr_show(struct device *dev,
  78                                 struct device_attribute *attr,
  79                                 char *buf)
  80{
  81        struct cca_info ci;
  82        struct ap_card *ac = to_ap_card(dev);
  83        struct zcrypt_card *zc = ac->private;
  84
  85        memset(&ci, 0, sizeof(ci));
  86
  87        if (ap_domain_index >= 0)
  88                cca_get_info(ac->id, ap_domain_index, &ci, zc->online);
  89
  90        return scnprintf(buf, PAGE_SIZE, "%s\n", ci.serial);
  91}
  92
  93static struct device_attribute dev_attr_cca_serialnr =
  94        __ATTR(serialnr, 0444, cca_serialnr_show, NULL);
  95
  96static struct attribute *cca_card_attrs[] = {
  97        &dev_attr_cca_serialnr.attr,
  98        NULL,
  99};
 100
 101static const struct attribute_group cca_card_attr_grp = {
 102        .attrs = cca_card_attrs,
 103};
 104
 105 /*
 106  * CCA queue additional device attributes
 107  */
 108static ssize_t cca_mkvps_show(struct device *dev,
 109                              struct device_attribute *attr,
 110                              char *buf)
 111{
 112        int n = 0;
 113        struct cca_info ci;
 114        struct zcrypt_queue *zq = to_ap_queue(dev)->private;
 115        static const char * const cao_state[] = { "invalid", "valid" };
 116        static const char * const new_state[] = { "empty", "partial", "full" };
 117
 118        memset(&ci, 0, sizeof(ci));
 119
 120        cca_get_info(AP_QID_CARD(zq->queue->qid),
 121                     AP_QID_QUEUE(zq->queue->qid),
 122                     &ci, zq->online);
 123
 124        if (ci.new_mk_state >= '1' && ci.new_mk_state <= '3')
 125                n = scnprintf(buf, PAGE_SIZE, "AES NEW: %s 0x%016llx\n",
 126                              new_state[ci.new_mk_state - '1'], ci.new_mkvp);
 127        else
 128                n = scnprintf(buf, PAGE_SIZE, "AES NEW: - -\n");
 129
 130        if (ci.cur_mk_state >= '1' && ci.cur_mk_state <= '2')
 131                n += scnprintf(buf + n, PAGE_SIZE - n,
 132                               "AES CUR: %s 0x%016llx\n",
 133                               cao_state[ci.cur_mk_state - '1'], ci.cur_mkvp);
 134        else
 135                n += scnprintf(buf + n, PAGE_SIZE - n, "AES CUR: - -\n");
 136
 137        if (ci.old_mk_state >= '1' && ci.old_mk_state <= '2')
 138                n += scnprintf(buf + n, PAGE_SIZE - n,
 139                               "AES OLD: %s 0x%016llx\n",
 140                               cao_state[ci.old_mk_state - '1'], ci.old_mkvp);
 141        else
 142                n += scnprintf(buf + n, PAGE_SIZE - n, "AES OLD: - -\n");
 143
 144        return n;
 145}
 146
 147static struct device_attribute dev_attr_cca_mkvps =
 148        __ATTR(mkvps, 0444, cca_mkvps_show, NULL);
 149
 150static struct attribute *cca_queue_attrs[] = {
 151        &dev_attr_cca_mkvps.attr,
 152        NULL,
 153};
 154
 155static const struct attribute_group cca_queue_attr_grp = {
 156        .attrs = cca_queue_attrs,
 157};
 158
 159/*
 160 * EP11 card additional device attributes
 161 */
 162static ssize_t ep11_api_ordinalnr_show(struct device *dev,
 163                                       struct device_attribute *attr,
 164                                       char *buf)
 165{
 166        struct ep11_card_info ci;
 167        struct ap_card *ac = to_ap_card(dev);
 168        struct zcrypt_card *zc = ac->private;
 169
 170        memset(&ci, 0, sizeof(ci));
 171
 172        ep11_get_card_info(ac->id, &ci, zc->online);
 173
 174        if (ci.API_ord_nr > 0)
 175                return scnprintf(buf, PAGE_SIZE, "%u\n", ci.API_ord_nr);
 176        else
 177                return scnprintf(buf, PAGE_SIZE, "\n");
 178}
 179
 180static struct device_attribute dev_attr_ep11_api_ordinalnr =
 181        __ATTR(API_ordinalnr, 0444, ep11_api_ordinalnr_show, NULL);
 182
 183static ssize_t ep11_fw_version_show(struct device *dev,
 184                                    struct device_attribute *attr,
 185                                    char *buf)
 186{
 187        struct ep11_card_info ci;
 188        struct ap_card *ac = to_ap_card(dev);
 189        struct zcrypt_card *zc = ac->private;
 190
 191        memset(&ci, 0, sizeof(ci));
 192
 193        ep11_get_card_info(ac->id, &ci, zc->online);
 194
 195        if (ci.FW_version > 0)
 196                return scnprintf(buf, PAGE_SIZE, "%d.%d\n",
 197                                 (int)(ci.FW_version >> 8),
 198                                 (int)(ci.FW_version & 0xFF));
 199        else
 200                return scnprintf(buf, PAGE_SIZE, "\n");
 201}
 202
 203static struct device_attribute dev_attr_ep11_fw_version =
 204        __ATTR(FW_version, 0444, ep11_fw_version_show, NULL);
 205
 206static ssize_t ep11_serialnr_show(struct device *dev,
 207                                  struct device_attribute *attr,
 208                                  char *buf)
 209{
 210        struct ep11_card_info ci;
 211        struct ap_card *ac = to_ap_card(dev);
 212        struct zcrypt_card *zc = ac->private;
 213
 214        memset(&ci, 0, sizeof(ci));
 215
 216        ep11_get_card_info(ac->id, &ci, zc->online);
 217
 218        if (ci.serial[0])
 219                return scnprintf(buf, PAGE_SIZE, "%16.16s\n", ci.serial);
 220        else
 221                return scnprintf(buf, PAGE_SIZE, "\n");
 222}
 223
 224static struct device_attribute dev_attr_ep11_serialnr =
 225        __ATTR(serialnr, 0444, ep11_serialnr_show, NULL);
 226
 227static const struct {
 228        int         mode_bit;
 229        const char *mode_txt;
 230} ep11_op_modes[] = {
 231        { 0, "FIPS2009" },
 232        { 1, "BSI2009" },
 233        { 2, "FIPS2011" },
 234        { 3, "BSI2011" },
 235        { 6, "BSICC2017" },
 236        { 0, NULL }
 237};
 238
 239static ssize_t ep11_card_op_modes_show(struct device *dev,
 240                                       struct device_attribute *attr,
 241                                       char *buf)
 242{
 243        int i, n = 0;
 244        struct ep11_card_info ci;
 245        struct ap_card *ac = to_ap_card(dev);
 246        struct zcrypt_card *zc = ac->private;
 247
 248        memset(&ci, 0, sizeof(ci));
 249
 250        ep11_get_card_info(ac->id, &ci, zc->online);
 251
 252        for (i = 0; ep11_op_modes[i].mode_txt; i++) {
 253                if (ci.op_mode & (1ULL << ep11_op_modes[i].mode_bit)) {
 254                        if (n > 0)
 255                                buf[n++] = ' ';
 256                        n += scnprintf(buf + n, PAGE_SIZE - n,
 257                                       "%s", ep11_op_modes[i].mode_txt);
 258                }
 259        }
 260        n += scnprintf(buf + n, PAGE_SIZE - n, "\n");
 261
 262        return n;
 263}
 264
 265static struct device_attribute dev_attr_ep11_card_op_modes =
 266        __ATTR(op_modes, 0444, ep11_card_op_modes_show, NULL);
 267
 268static struct attribute *ep11_card_attrs[] = {
 269        &dev_attr_ep11_api_ordinalnr.attr,
 270        &dev_attr_ep11_fw_version.attr,
 271        &dev_attr_ep11_serialnr.attr,
 272        &dev_attr_ep11_card_op_modes.attr,
 273        NULL,
 274};
 275
 276static const struct attribute_group ep11_card_attr_grp = {
 277        .attrs = ep11_card_attrs,
 278};
 279
 280/*
 281 * EP11 queue additional device attributes
 282 */
 283
 284static ssize_t ep11_mkvps_show(struct device *dev,
 285                               struct device_attribute *attr,
 286                               char *buf)
 287{
 288        int n = 0;
 289        struct ep11_domain_info di;
 290        struct zcrypt_queue *zq = to_ap_queue(dev)->private;
 291        static const char * const cwk_state[] = { "invalid", "valid" };
 292        static const char * const nwk_state[] = { "empty", "uncommitted",
 293                                                  "committed" };
 294
 295        memset(&di, 0, sizeof(di));
 296
 297        if (zq->online)
 298                ep11_get_domain_info(AP_QID_CARD(zq->queue->qid),
 299                                     AP_QID_QUEUE(zq->queue->qid),
 300                                     &di);
 301
 302        if (di.cur_wk_state == '0') {
 303                n = scnprintf(buf, PAGE_SIZE, "WK CUR: %s -\n",
 304                              cwk_state[di.cur_wk_state - '0']);
 305        } else if (di.cur_wk_state == '1') {
 306                n = scnprintf(buf, PAGE_SIZE, "WK CUR: %s 0x",
 307                              cwk_state[di.cur_wk_state - '0']);
 308                bin2hex(buf + n, di.cur_wkvp, sizeof(di.cur_wkvp));
 309                n += 2 * sizeof(di.cur_wkvp);
 310                n += scnprintf(buf + n, PAGE_SIZE - n, "\n");
 311        } else
 312                n = scnprintf(buf, PAGE_SIZE, "WK CUR: - -\n");
 313
 314        if (di.new_wk_state == '0') {
 315                n += scnprintf(buf + n, PAGE_SIZE - n, "WK NEW: %s -\n",
 316                               nwk_state[di.new_wk_state - '0']);
 317        } else if (di.new_wk_state >= '1' && di.new_wk_state <= '2') {
 318                n += scnprintf(buf + n, PAGE_SIZE - n, "WK NEW: %s 0x",
 319                               nwk_state[di.new_wk_state - '0']);
 320                bin2hex(buf + n, di.new_wkvp, sizeof(di.new_wkvp));
 321                n += 2 * sizeof(di.new_wkvp);
 322                n += scnprintf(buf + n, PAGE_SIZE - n, "\n");
 323        } else
 324                n += scnprintf(buf + n, PAGE_SIZE - n, "WK NEW: - -\n");
 325
 326        return n;
 327}
 328
 329static struct device_attribute dev_attr_ep11_mkvps =
 330        __ATTR(mkvps, 0444, ep11_mkvps_show, NULL);
 331
 332static ssize_t ep11_queue_op_modes_show(struct device *dev,
 333                                        struct device_attribute *attr,
 334                                        char *buf)
 335{
 336        int i, n = 0;
 337        struct ep11_domain_info di;
 338        struct zcrypt_queue *zq = to_ap_queue(dev)->private;
 339
 340        memset(&di, 0, sizeof(di));
 341
 342        if (zq->online)
 343                ep11_get_domain_info(AP_QID_CARD(zq->queue->qid),
 344                                     AP_QID_QUEUE(zq->queue->qid),
 345                                     &di);
 346
 347        for (i = 0; ep11_op_modes[i].mode_txt; i++) {
 348                if (di.op_mode & (1ULL << ep11_op_modes[i].mode_bit)) {
 349                        if (n > 0)
 350                                buf[n++] = ' ';
 351                        n += scnprintf(buf + n, PAGE_SIZE - n,
 352                                       "%s", ep11_op_modes[i].mode_txt);
 353                }
 354        }
 355        n += scnprintf(buf + n, PAGE_SIZE - n, "\n");
 356
 357        return n;
 358}
 359
 360static struct device_attribute dev_attr_ep11_queue_op_modes =
 361        __ATTR(op_modes, 0444, ep11_queue_op_modes_show, NULL);
 362
 363static struct attribute *ep11_queue_attrs[] = {
 364        &dev_attr_ep11_mkvps.attr,
 365        &dev_attr_ep11_queue_op_modes.attr,
 366        NULL,
 367};
 368
 369static const struct attribute_group ep11_queue_attr_grp = {
 370        .attrs = ep11_queue_attrs,
 371};
 372
 373/**
 374 * Probe function for CEX4/CEX5/CEX6/CEX7 card device. It always
 375 * accepts the AP device since the bus_match already checked
 376 * the hardware type.
 377 * @ap_dev: pointer to the AP device.
 378 */
 379static int zcrypt_cex4_card_probe(struct ap_device *ap_dev)
 380{
 381        /*
 382         * Normalized speed ratings per crypto adapter
 383         * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
 384         */
 385        static const int CEX4A_SPEED_IDX[NUM_OPS] = {
 386                 14,  19, 249, 42, 228, 1458, 0, 0};
 387        static const int CEX5A_SPEED_IDX[NUM_OPS] = {
 388                  8,   9,  20, 18,  66,  458, 0, 0};
 389        static const int CEX6A_SPEED_IDX[NUM_OPS] = {
 390                  6,   9,  20, 17,  65,  438, 0, 0};
 391        static const int CEX7A_SPEED_IDX[NUM_OPS] = {
 392                  6,   8,  17, 15,  54,  362, 0, 0};
 393
 394        static const int CEX4C_SPEED_IDX[NUM_OPS] = {
 395                 59,  69, 308, 83, 278, 2204, 209, 40};
 396        static const int CEX5C_SPEED_IDX[] = {
 397                 24,  31,  50, 37,  90,  479,  27, 10};
 398        static const int CEX6C_SPEED_IDX[NUM_OPS] = {
 399                 16,  20,  32, 27,  77,  455,  24,  9};
 400        static const int CEX7C_SPEED_IDX[NUM_OPS] = {
 401                 14,  16,  26, 23,  64,  376,  23,  8};
 402
 403        static const int CEX4P_SPEED_IDX[NUM_OPS] = {
 404                  0,   0,   0,   0,   0,   0,   0,  50};
 405        static const int CEX5P_SPEED_IDX[NUM_OPS] = {
 406                  0,   0,   0,   0,   0,   0,   0,  10};
 407        static const int CEX6P_SPEED_IDX[NUM_OPS] = {
 408                  0,   0,   0,   0,   0,   0,   0,   9};
 409        static const int CEX7P_SPEED_IDX[NUM_OPS] = {
 410                  0,   0,   0,   0,   0,   0,   0,   8};
 411
 412        struct ap_card *ac = to_ap_card(&ap_dev->device);
 413        struct zcrypt_card *zc;
 414        int rc = 0;
 415
 416        zc = zcrypt_card_alloc();
 417        if (!zc)
 418                return -ENOMEM;
 419        zc->card = ac;
 420        ac->private = zc;
 421        if (ap_test_bit(&ac->functions, AP_FUNC_ACCEL)) {
 422                if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
 423                        zc->type_string = "CEX4A";
 424                        zc->user_space_type = ZCRYPT_CEX4;
 425                        zc->speed_rating = CEX4A_SPEED_IDX;
 426                } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) {
 427                        zc->type_string = "CEX5A";
 428                        zc->user_space_type = ZCRYPT_CEX5;
 429                        zc->speed_rating = CEX5A_SPEED_IDX;
 430                } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX6) {
 431                        zc->type_string = "CEX6A";
 432                        zc->user_space_type = ZCRYPT_CEX6;
 433                        zc->speed_rating = CEX6A_SPEED_IDX;
 434                } else {
 435                        zc->type_string = "CEX7A";
 436                        /* wrong user space type, just for compatibility
 437                         * with the ZCRYPT_STATUS_MASK ioctl.
 438                         */
 439                        zc->user_space_type = ZCRYPT_CEX6;
 440                        zc->speed_rating = CEX7A_SPEED_IDX;
 441                }
 442                zc->min_mod_size = CEX4A_MIN_MOD_SIZE;
 443                if (ap_test_bit(&ac->functions, AP_FUNC_MEX4K) &&
 444                    ap_test_bit(&ac->functions, AP_FUNC_CRT4K)) {
 445                        zc->max_mod_size = CEX4A_MAX_MOD_SIZE_4K;
 446                        zc->max_exp_bit_length =
 447                                CEX4A_MAX_MOD_SIZE_4K;
 448                } else {
 449                        zc->max_mod_size = CEX4A_MAX_MOD_SIZE_2K;
 450                        zc->max_exp_bit_length =
 451                                CEX4A_MAX_MOD_SIZE_2K;
 452                }
 453        } else if (ap_test_bit(&ac->functions, AP_FUNC_COPRO)) {
 454                if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
 455                        zc->type_string = "CEX4C";
 456                        /* wrong user space type, must be CEX4
 457                         * just keep it for cca compatibility
 458                         */
 459                        zc->user_space_type = ZCRYPT_CEX3C;
 460                        zc->speed_rating = CEX4C_SPEED_IDX;
 461                } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) {
 462                        zc->type_string = "CEX5C";
 463                        /* wrong user space type, must be CEX5
 464                         * just keep it for cca compatibility
 465                         */
 466                        zc->user_space_type = ZCRYPT_CEX3C;
 467                        zc->speed_rating = CEX5C_SPEED_IDX;
 468                } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX6) {
 469                        zc->type_string = "CEX6C";
 470                        /* wrong user space type, must be CEX6
 471                         * just keep it for cca compatibility
 472                         */
 473                        zc->user_space_type = ZCRYPT_CEX3C;
 474                        zc->speed_rating = CEX6C_SPEED_IDX;
 475                } else {
 476                        zc->type_string = "CEX7C";
 477                        /* wrong user space type, must be CEX7
 478                         * just keep it for cca compatibility
 479                         */
 480                        zc->user_space_type = ZCRYPT_CEX3C;
 481                        zc->speed_rating = CEX7C_SPEED_IDX;
 482                }
 483                zc->min_mod_size = CEX4C_MIN_MOD_SIZE;
 484                zc->max_mod_size = CEX4C_MAX_MOD_SIZE;
 485                zc->max_exp_bit_length = CEX4C_MAX_MOD_SIZE;
 486        } else if (ap_test_bit(&ac->functions, AP_FUNC_EP11)) {
 487                if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
 488                        zc->type_string = "CEX4P";
 489                        zc->user_space_type = ZCRYPT_CEX4;
 490                        zc->speed_rating = CEX4P_SPEED_IDX;
 491                } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) {
 492                        zc->type_string = "CEX5P";
 493                        zc->user_space_type = ZCRYPT_CEX5;
 494                        zc->speed_rating = CEX5P_SPEED_IDX;
 495                } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX6) {
 496                        zc->type_string = "CEX6P";
 497                        zc->user_space_type = ZCRYPT_CEX6;
 498                        zc->speed_rating = CEX6P_SPEED_IDX;
 499                } else {
 500                        zc->type_string = "CEX7P";
 501                        /* wrong user space type, just for compatibility
 502                         * with the ZCRYPT_STATUS_MASK ioctl.
 503                         */
 504                        zc->user_space_type = ZCRYPT_CEX6;
 505                        zc->speed_rating = CEX7P_SPEED_IDX;
 506                }
 507                zc->min_mod_size = CEX4C_MIN_MOD_SIZE;
 508                zc->max_mod_size = CEX4C_MAX_MOD_SIZE;
 509                zc->max_exp_bit_length = CEX4C_MAX_MOD_SIZE;
 510        } else {
 511                zcrypt_card_free(zc);
 512                return -ENODEV;
 513        }
 514        zc->online = 1;
 515
 516        rc = zcrypt_card_register(zc);
 517        if (rc) {
 518                ac->private = NULL;
 519                zcrypt_card_free(zc);
 520                return rc;
 521        }
 522
 523        if (ap_test_bit(&ac->functions, AP_FUNC_COPRO)) {
 524                rc = sysfs_create_group(&ap_dev->device.kobj,
 525                                        &cca_card_attr_grp);
 526                if (rc) {
 527                        zcrypt_card_unregister(zc);
 528                        ac->private = NULL;
 529                        zcrypt_card_free(zc);
 530                }
 531        } else if (ap_test_bit(&ac->functions, AP_FUNC_EP11)) {
 532                rc = sysfs_create_group(&ap_dev->device.kobj,
 533                                        &ep11_card_attr_grp);
 534                if (rc) {
 535                        zcrypt_card_unregister(zc);
 536                        ac->private = NULL;
 537                        zcrypt_card_free(zc);
 538                }
 539        }
 540
 541        return rc;
 542}
 543
 544/**
 545 * This is called to remove the CEX4/CEX5/CEX6/CEX7 card driver
 546 * information if an AP card device is removed.
 547 */
 548static void zcrypt_cex4_card_remove(struct ap_device *ap_dev)
 549{
 550        struct ap_card *ac = to_ap_card(&ap_dev->device);
 551        struct zcrypt_card *zc = ac->private;
 552
 553        if (ap_test_bit(&ac->functions, AP_FUNC_COPRO))
 554                sysfs_remove_group(&ap_dev->device.kobj, &cca_card_attr_grp);
 555        else if (ap_test_bit(&ac->functions, AP_FUNC_EP11))
 556                sysfs_remove_group(&ap_dev->device.kobj, &ep11_card_attr_grp);
 557        if (zc)
 558                zcrypt_card_unregister(zc);
 559}
 560
 561static struct ap_driver zcrypt_cex4_card_driver = {
 562        .probe = zcrypt_cex4_card_probe,
 563        .remove = zcrypt_cex4_card_remove,
 564        .ids = zcrypt_cex4_card_ids,
 565        .flags = AP_DRIVER_FLAG_DEFAULT,
 566};
 567
 568/**
 569 * Probe function for CEX4/CEX5/CEX6/CEX7 queue device. It always
 570 * accepts the AP device since the bus_match already checked
 571 * the hardware type.
 572 * @ap_dev: pointer to the AP device.
 573 */
 574static int zcrypt_cex4_queue_probe(struct ap_device *ap_dev)
 575{
 576        struct ap_queue *aq = to_ap_queue(&ap_dev->device);
 577        struct zcrypt_queue *zq;
 578        int rc;
 579
 580        if (ap_test_bit(&aq->card->functions, AP_FUNC_ACCEL)) {
 581                zq = zcrypt_queue_alloc(CEX4A_MAX_MESSAGE_SIZE);
 582                if (!zq)
 583                        return -ENOMEM;
 584                zq->ops = zcrypt_msgtype(MSGTYPE50_NAME,
 585                                         MSGTYPE50_VARIANT_DEFAULT);
 586        } else if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO)) {
 587                zq = zcrypt_queue_alloc(CEX4C_MAX_MESSAGE_SIZE);
 588                if (!zq)
 589                        return -ENOMEM;
 590                zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
 591                                         MSGTYPE06_VARIANT_DEFAULT);
 592        } else if (ap_test_bit(&aq->card->functions, AP_FUNC_EP11)) {
 593                zq = zcrypt_queue_alloc(CEX4C_MAX_MESSAGE_SIZE);
 594                if (!zq)
 595                        return -ENOMEM;
 596                zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
 597                                         MSGTYPE06_VARIANT_EP11);
 598        } else {
 599                return -ENODEV;
 600        }
 601
 602        zq->queue = aq;
 603        zq->online = 1;
 604        atomic_set(&zq->load, 0);
 605        ap_queue_init_state(aq);
 606        ap_queue_init_reply(aq, &zq->reply);
 607        aq->request_timeout = CEX4_CLEANUP_TIME,
 608        aq->private = zq;
 609        rc = zcrypt_queue_register(zq);
 610        if (rc) {
 611                aq->private = NULL;
 612                zcrypt_queue_free(zq);
 613                return rc;
 614        }
 615
 616        if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO)) {
 617                rc = sysfs_create_group(&ap_dev->device.kobj,
 618                                        &cca_queue_attr_grp);
 619                if (rc) {
 620                        zcrypt_queue_unregister(zq);
 621                        aq->private = NULL;
 622                        zcrypt_queue_free(zq);
 623                }
 624        } else if (ap_test_bit(&aq->card->functions, AP_FUNC_EP11)) {
 625                rc = sysfs_create_group(&ap_dev->device.kobj,
 626                                        &ep11_queue_attr_grp);
 627                if (rc) {
 628                        zcrypt_queue_unregister(zq);
 629                        aq->private = NULL;
 630                        zcrypt_queue_free(zq);
 631                }
 632        }
 633
 634        return rc;
 635}
 636
 637/**
 638 * This is called to remove the CEX4/CEX5/CEX6/CEX7 queue driver
 639 * information if an AP queue device is removed.
 640 */
 641static void zcrypt_cex4_queue_remove(struct ap_device *ap_dev)
 642{
 643        struct ap_queue *aq = to_ap_queue(&ap_dev->device);
 644        struct zcrypt_queue *zq = aq->private;
 645
 646        if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO))
 647                sysfs_remove_group(&ap_dev->device.kobj, &cca_queue_attr_grp);
 648        else if (ap_test_bit(&aq->card->functions, AP_FUNC_EP11))
 649                sysfs_remove_group(&ap_dev->device.kobj, &ep11_queue_attr_grp);
 650        if (zq)
 651                zcrypt_queue_unregister(zq);
 652}
 653
 654static struct ap_driver zcrypt_cex4_queue_driver = {
 655        .probe = zcrypt_cex4_queue_probe,
 656        .remove = zcrypt_cex4_queue_remove,
 657        .suspend = ap_queue_suspend,
 658        .resume = ap_queue_resume,
 659        .ids = zcrypt_cex4_queue_ids,
 660        .flags = AP_DRIVER_FLAG_DEFAULT,
 661};
 662
 663int __init zcrypt_cex4_init(void)
 664{
 665        int rc;
 666
 667        rc = ap_driver_register(&zcrypt_cex4_card_driver,
 668                                THIS_MODULE, "cex4card");
 669        if (rc)
 670                return rc;
 671
 672        rc = ap_driver_register(&zcrypt_cex4_queue_driver,
 673                                THIS_MODULE, "cex4queue");
 674        if (rc)
 675                ap_driver_unregister(&zcrypt_cex4_card_driver);
 676
 677        return rc;
 678}
 679
 680void __exit zcrypt_cex4_exit(void)
 681{
 682        ap_driver_unregister(&zcrypt_cex4_queue_driver);
 683        ap_driver_unregister(&zcrypt_cex4_card_driver);
 684}
 685
 686module_init(zcrypt_cex4_init);
 687module_exit(zcrypt_cex4_exit);
 688