linux/drivers/s390/crypto/zcrypt_cex2c.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 *  Copyright IBM Corp. 2001, 2018
   4 *  Author(s): Robert Burroughs
   5 *             Eric Rossman (edrossma@us.ibm.com)
   6 *
   7 *  Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
   8 *  Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
   9 *                                Ralph Wuerthner <rwuerthn@de.ibm.com>
  10 *  MSGTYPE restruct:             Holger Dengler <hd@linux.vnet.ibm.com>
  11 */
  12
  13#include <linux/module.h>
  14#include <linux/init.h>
  15#include <linux/err.h>
  16#include <linux/delay.h>
  17#include <linux/slab.h>
  18#include <linux/atomic.h>
  19#include <linux/uaccess.h>
  20#include <linux/mod_devicetable.h>
  21
  22#include "ap_bus.h"
  23#include "zcrypt_api.h"
  24#include "zcrypt_error.h"
  25#include "zcrypt_msgtype6.h"
  26#include "zcrypt_cex2c.h"
  27#include "zcrypt_cca_key.h"
  28#include "zcrypt_ccamisc.h"
  29
  30#define CEX2C_MIN_MOD_SIZE       16     /*  128 bits    */
  31#define CEX2C_MAX_MOD_SIZE      256     /* 2048 bits    */
  32#define CEX3C_MIN_MOD_SIZE       16     /*  128 bits    */
  33#define CEX3C_MAX_MOD_SIZE      512     /* 4096 bits    */
  34#define CEX2C_MAX_XCRB_MESSAGE_SIZE (12*1024)
  35#define CEX2C_CLEANUP_TIME      (15*HZ)
  36
  37MODULE_AUTHOR("IBM Corporation");
  38MODULE_DESCRIPTION("CEX2C/CEX3C Cryptographic Coprocessor device driver, " \
  39                   "Copyright IBM Corp. 2001, 2018");
  40MODULE_LICENSE("GPL");
  41
  42static struct ap_device_id zcrypt_cex2c_card_ids[] = {
  43        { .dev_type = AP_DEVICE_TYPE_CEX2C,
  44          .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  45        { .dev_type = AP_DEVICE_TYPE_CEX3C,
  46          .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  47        { /* end of list */ },
  48};
  49
  50MODULE_DEVICE_TABLE(ap, zcrypt_cex2c_card_ids);
  51
  52static struct ap_device_id zcrypt_cex2c_queue_ids[] = {
  53        { .dev_type = AP_DEVICE_TYPE_CEX2C,
  54          .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  55        { .dev_type = AP_DEVICE_TYPE_CEX3C,
  56          .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  57        { /* end of list */ },
  58};
  59
  60MODULE_DEVICE_TABLE(ap, zcrypt_cex2c_queue_ids);
  61
  62/*
  63 * CCA card additional device attributes
  64 */
  65static ssize_t cca_serialnr_show(struct device *dev,
  66                                 struct device_attribute *attr,
  67                                 char *buf)
  68{
  69        struct zcrypt_card *zc = dev_get_drvdata(dev);
  70        struct cca_info ci;
  71        struct ap_card *ac = to_ap_card(dev);
  72
  73        memset(&ci, 0, sizeof(ci));
  74
  75        if (ap_domain_index >= 0)
  76                cca_get_info(ac->id, ap_domain_index, &ci, zc->online);
  77
  78        return scnprintf(buf, PAGE_SIZE, "%s\n", ci.serial);
  79}
  80
  81static struct device_attribute dev_attr_cca_serialnr =
  82        __ATTR(serialnr, 0444, cca_serialnr_show, NULL);
  83
  84static struct attribute *cca_card_attrs[] = {
  85        &dev_attr_cca_serialnr.attr,
  86        NULL,
  87};
  88
  89static const struct attribute_group cca_card_attr_grp = {
  90        .attrs = cca_card_attrs,
  91};
  92
  93 /*
  94  * CCA queue additional device attributes
  95  */
  96static ssize_t cca_mkvps_show(struct device *dev,
  97                              struct device_attribute *attr,
  98                              char *buf)
  99{
 100        struct zcrypt_queue *zq = dev_get_drvdata(dev);
 101        int n = 0;
 102        struct cca_info ci;
 103        static const char * const cao_state[] = { "invalid", "valid" };
 104        static const char * const new_state[] = { "empty", "partial", "full" };
 105
 106        memset(&ci, 0, sizeof(ci));
 107
 108        cca_get_info(AP_QID_CARD(zq->queue->qid),
 109                     AP_QID_QUEUE(zq->queue->qid),
 110                     &ci, zq->online);
 111
 112        if (ci.new_aes_mk_state >= '1' && ci.new_aes_mk_state <= '3')
 113                n = scnprintf(buf, PAGE_SIZE, "AES NEW: %s 0x%016llx\n",
 114                              new_state[ci.new_aes_mk_state - '1'],
 115                              ci.new_aes_mkvp);
 116        else
 117                n = scnprintf(buf, PAGE_SIZE, "AES NEW: - -\n");
 118
 119        if (ci.cur_aes_mk_state >= '1' && ci.cur_aes_mk_state <= '2')
 120                n += scnprintf(buf + n, PAGE_SIZE - n,
 121                               "AES CUR: %s 0x%016llx\n",
 122                               cao_state[ci.cur_aes_mk_state - '1'],
 123                               ci.cur_aes_mkvp);
 124        else
 125                n += scnprintf(buf + n, PAGE_SIZE - n, "AES CUR: - -\n");
 126
 127        if (ci.old_aes_mk_state >= '1' && ci.old_aes_mk_state <= '2')
 128                n += scnprintf(buf + n, PAGE_SIZE - n,
 129                               "AES OLD: %s 0x%016llx\n",
 130                               cao_state[ci.old_aes_mk_state - '1'],
 131                               ci.old_aes_mkvp);
 132        else
 133                n += scnprintf(buf + n, PAGE_SIZE - n, "AES OLD: - -\n");
 134
 135        if (ci.new_apka_mk_state >= '1' && ci.new_apka_mk_state <= '3')
 136                n += scnprintf(buf + n, PAGE_SIZE - n,
 137                               "APKA NEW: %s 0x%016llx\n",
 138                               new_state[ci.new_apka_mk_state - '1'],
 139                               ci.new_apka_mkvp);
 140        else
 141                n += scnprintf(buf + n, PAGE_SIZE - n, "APKA NEW: - -\n");
 142
 143        if (ci.cur_apka_mk_state >= '1' && ci.cur_apka_mk_state <= '2')
 144                n += scnprintf(buf + n, PAGE_SIZE - n,
 145                               "APKA CUR: %s 0x%016llx\n",
 146                               cao_state[ci.cur_apka_mk_state - '1'],
 147                               ci.cur_apka_mkvp);
 148        else
 149                n += scnprintf(buf + n, PAGE_SIZE - n, "APKA CUR: - -\n");
 150
 151        if (ci.old_apka_mk_state >= '1' && ci.old_apka_mk_state <= '2')
 152                n += scnprintf(buf + n, PAGE_SIZE - n,
 153                               "APKA OLD: %s 0x%016llx\n",
 154                               cao_state[ci.old_apka_mk_state - '1'],
 155                               ci.old_apka_mkvp);
 156        else
 157                n += scnprintf(buf + n, PAGE_SIZE - n, "APKA OLD: - -\n");
 158
 159        return n;
 160}
 161
 162static struct device_attribute dev_attr_cca_mkvps =
 163        __ATTR(mkvps, 0444, cca_mkvps_show, NULL);
 164
 165static struct attribute *cca_queue_attrs[] = {
 166        &dev_attr_cca_mkvps.attr,
 167        NULL,
 168};
 169
 170static const struct attribute_group cca_queue_attr_grp = {
 171        .attrs = cca_queue_attrs,
 172};
 173
 174/*
 175 * Large random number detection function. Its sends a message to a CEX2C/CEX3C
 176 * card to find out if large random numbers are supported.
 177 * @ap_dev: pointer to the AP device.
 178 *
 179 * Returns 1 if large random numbers are supported, 0 if not and < 0 on error.
 180 */
 181static int zcrypt_cex2c_rng_supported(struct ap_queue *aq)
 182{
 183        struct ap_message ap_msg;
 184        unsigned long long psmid;
 185        unsigned int domain;
 186        struct {
 187                struct type86_hdr hdr;
 188                struct type86_fmt2_ext fmt2;
 189                struct CPRBX cprbx;
 190        } __packed *reply;
 191        struct {
 192                struct type6_hdr hdr;
 193                struct CPRBX cprbx;
 194                char function_code[2];
 195                short int rule_length;
 196                char rule[8];
 197                short int verb_length;
 198                short int key_length;
 199        } __packed *msg;
 200        int rc, i;
 201
 202        ap_init_message(&ap_msg);
 203        ap_msg.msg = (void *) get_zeroed_page(GFP_KERNEL);
 204        if (!ap_msg.msg)
 205                return -ENOMEM;
 206
 207        rng_type6CPRB_msgX(&ap_msg, 4, &domain);
 208
 209        msg = ap_msg.msg;
 210        msg->cprbx.domain = AP_QID_QUEUE(aq->qid);
 211
 212        rc = ap_send(aq->qid, 0x0102030405060708ULL, ap_msg.msg, ap_msg.len);
 213        if (rc)
 214                goto out_free;
 215
 216        /* Wait for the test message to complete. */
 217        for (i = 0; i < 2 * HZ; i++) {
 218                msleep(1000 / HZ);
 219                rc = ap_recv(aq->qid, &psmid, ap_msg.msg, 4096);
 220                if (rc == 0 && psmid == 0x0102030405060708ULL)
 221                        break;
 222        }
 223
 224        if (i >= 2 * HZ) {
 225                /* Got no answer. */
 226                rc = -ENODEV;
 227                goto out_free;
 228        }
 229
 230        reply = ap_msg.msg;
 231        if (reply->cprbx.ccp_rtcode == 0 && reply->cprbx.ccp_rscode == 0)
 232                rc = 1;
 233        else
 234                rc = 0;
 235out_free:
 236        free_page((unsigned long) ap_msg.msg);
 237        return rc;
 238}
 239
 240/*
 241 * Probe function for CEX2C/CEX3C card devices. It always accepts the
 242 * AP device since the bus_match already checked the hardware type.
 243 * @ap_dev: pointer to the AP card device.
 244 */
 245static int zcrypt_cex2c_card_probe(struct ap_device *ap_dev)
 246{
 247        /*
 248         * Normalized speed ratings per crypto adapter
 249         * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
 250         */
 251        static const int CEX2C_SPEED_IDX[] = {
 252                1000, 1400, 2400, 1100, 1500, 2600, 100, 12};
 253        static const int CEX3C_SPEED_IDX[] = {
 254                500,  700, 1400,  550,  800, 1500,  80, 10};
 255
 256        struct ap_card *ac = to_ap_card(&ap_dev->device);
 257        struct zcrypt_card *zc;
 258        int rc = 0;
 259
 260        zc = zcrypt_card_alloc();
 261        if (!zc)
 262                return -ENOMEM;
 263        zc->card = ac;
 264        dev_set_drvdata(&ap_dev->device, zc);
 265        switch (ac->ap_dev.device_type) {
 266        case AP_DEVICE_TYPE_CEX2C:
 267                zc->user_space_type = ZCRYPT_CEX2C;
 268                zc->type_string = "CEX2C";
 269                zc->speed_rating = CEX2C_SPEED_IDX;
 270                zc->min_mod_size = CEX2C_MIN_MOD_SIZE;
 271                zc->max_mod_size = CEX2C_MAX_MOD_SIZE;
 272                zc->max_exp_bit_length = CEX2C_MAX_MOD_SIZE;
 273                break;
 274        case AP_DEVICE_TYPE_CEX3C:
 275                zc->user_space_type = ZCRYPT_CEX3C;
 276                zc->type_string = "CEX3C";
 277                zc->speed_rating = CEX3C_SPEED_IDX;
 278                zc->min_mod_size = CEX3C_MIN_MOD_SIZE;
 279                zc->max_mod_size = CEX3C_MAX_MOD_SIZE;
 280                zc->max_exp_bit_length = CEX3C_MAX_MOD_SIZE;
 281                break;
 282        default:
 283                zcrypt_card_free(zc);
 284                return -ENODEV;
 285        }
 286        zc->online = 1;
 287
 288        rc = zcrypt_card_register(zc);
 289        if (rc) {
 290                zcrypt_card_free(zc);
 291                return rc;
 292        }
 293
 294        if (ap_test_bit(&ac->functions, AP_FUNC_COPRO)) {
 295                rc = sysfs_create_group(&ap_dev->device.kobj,
 296                                        &cca_card_attr_grp);
 297                if (rc) {
 298                        zcrypt_card_unregister(zc);
 299                        zcrypt_card_free(zc);
 300                }
 301        }
 302
 303        return rc;
 304}
 305
 306/*
 307 * This is called to remove the CEX2C/CEX3C card driver information
 308 * if an AP card device is removed.
 309 */
 310static void zcrypt_cex2c_card_remove(struct ap_device *ap_dev)
 311{
 312        struct zcrypt_card *zc = dev_get_drvdata(&ap_dev->device);
 313        struct ap_card *ac = to_ap_card(&ap_dev->device);
 314
 315        if (ap_test_bit(&ac->functions, AP_FUNC_COPRO))
 316                sysfs_remove_group(&ap_dev->device.kobj, &cca_card_attr_grp);
 317
 318        zcrypt_card_unregister(zc);
 319}
 320
 321static struct ap_driver zcrypt_cex2c_card_driver = {
 322        .probe = zcrypt_cex2c_card_probe,
 323        .remove = zcrypt_cex2c_card_remove,
 324        .ids = zcrypt_cex2c_card_ids,
 325        .flags = AP_DRIVER_FLAG_DEFAULT,
 326};
 327
 328/*
 329 * Probe function for CEX2C/CEX3C queue devices. It always accepts the
 330 * AP device since the bus_match already checked the hardware type.
 331 * @ap_dev: pointer to the AP card device.
 332 */
 333static int zcrypt_cex2c_queue_probe(struct ap_device *ap_dev)
 334{
 335        struct ap_queue *aq = to_ap_queue(&ap_dev->device);
 336        struct zcrypt_queue *zq;
 337        int rc;
 338
 339        zq = zcrypt_queue_alloc(CEX2C_MAX_XCRB_MESSAGE_SIZE);
 340        if (!zq)
 341                return -ENOMEM;
 342        zq->queue = aq;
 343        zq->online = 1;
 344        atomic_set(&zq->load, 0);
 345        ap_rapq(aq->qid);
 346        rc = zcrypt_cex2c_rng_supported(aq);
 347        if (rc < 0) {
 348                zcrypt_queue_free(zq);
 349                return rc;
 350        }
 351        if (rc)
 352                zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
 353                                         MSGTYPE06_VARIANT_DEFAULT);
 354        else
 355                zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
 356                                         MSGTYPE06_VARIANT_NORNG);
 357        ap_queue_init_state(aq);
 358        ap_queue_init_reply(aq, &zq->reply);
 359        aq->request_timeout = CEX2C_CLEANUP_TIME;
 360        dev_set_drvdata(&ap_dev->device, zq);
 361        rc = zcrypt_queue_register(zq);
 362        if (rc) {
 363                zcrypt_queue_free(zq);
 364                return rc;
 365        }
 366
 367        if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO)) {
 368                rc = sysfs_create_group(&ap_dev->device.kobj,
 369                                        &cca_queue_attr_grp);
 370                if (rc) {
 371                        zcrypt_queue_unregister(zq);
 372                        zcrypt_queue_free(zq);
 373                }
 374        }
 375
 376        return rc;
 377}
 378
 379/*
 380 * This is called to remove the CEX2C/CEX3C queue driver information
 381 * if an AP queue device is removed.
 382 */
 383static void zcrypt_cex2c_queue_remove(struct ap_device *ap_dev)
 384{
 385        struct zcrypt_queue *zq = dev_get_drvdata(&ap_dev->device);
 386        struct ap_queue *aq = to_ap_queue(&ap_dev->device);
 387
 388        if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO))
 389                sysfs_remove_group(&ap_dev->device.kobj, &cca_queue_attr_grp);
 390
 391        zcrypt_queue_unregister(zq);
 392}
 393
 394static struct ap_driver zcrypt_cex2c_queue_driver = {
 395        .probe = zcrypt_cex2c_queue_probe,
 396        .remove = zcrypt_cex2c_queue_remove,
 397        .ids = zcrypt_cex2c_queue_ids,
 398        .flags = AP_DRIVER_FLAG_DEFAULT,
 399};
 400
 401int __init zcrypt_cex2c_init(void)
 402{
 403        int rc;
 404
 405        rc = ap_driver_register(&zcrypt_cex2c_card_driver,
 406                                THIS_MODULE, "cex2card");
 407        if (rc)
 408                return rc;
 409
 410        rc = ap_driver_register(&zcrypt_cex2c_queue_driver,
 411                                THIS_MODULE, "cex2cqueue");
 412        if (rc)
 413                ap_driver_unregister(&zcrypt_cex2c_card_driver);
 414
 415        return rc;
 416}
 417
 418void zcrypt_cex2c_exit(void)
 419{
 420        ap_driver_unregister(&zcrypt_cex2c_queue_driver);
 421        ap_driver_unregister(&zcrypt_cex2c_card_driver);
 422}
 423
 424module_init(zcrypt_cex2c_init);
 425module_exit(zcrypt_cex2c_exit);
 426