linux/drivers/s390/crypto/zcrypt_pcixcc.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 *  zcrypt 2.1.0
   4 *
   5 *  Copyright IBM Corp. 2001, 2012
   6 *  Author(s): Robert Burroughs
   7 *             Eric Rossman (edrossma@us.ibm.com)
   8 *
   9 *  Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
  10 *  Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
  11 *                                Ralph Wuerthner <rwuerthn@de.ibm.com>
  12 *  MSGTYPE restruct:             Holger Dengler <hd@linux.vnet.ibm.com>
  13 */
  14
  15#include <linux/module.h>
  16#include <linux/init.h>
  17#include <linux/err.h>
  18#include <linux/delay.h>
  19#include <linux/slab.h>
  20#include <linux/atomic.h>
  21#include <linux/uaccess.h>
  22#include <linux/mod_devicetable.h>
  23
  24#include "ap_bus.h"
  25#include "zcrypt_api.h"
  26#include "zcrypt_error.h"
  27#include "zcrypt_msgtype6.h"
  28#include "zcrypt_pcixcc.h"
  29#include "zcrypt_cca_key.h"
  30
  31#define PCIXCC_MIN_MOD_SIZE      16     /*  128 bits    */
  32#define PCIXCC_MIN_MOD_SIZE_OLD  64     /*  512 bits    */
  33#define PCIXCC_MAX_MOD_SIZE     256     /* 2048 bits    */
  34#define CEX3C_MIN_MOD_SIZE      PCIXCC_MIN_MOD_SIZE
  35#define CEX3C_MAX_MOD_SIZE      512     /* 4096 bits    */
  36
  37#define PCIXCC_MAX_ICA_MESSAGE_SIZE 0x77c  /* max size type6 v2 crt message */
  38#define PCIXCC_MAX_ICA_RESPONSE_SIZE 0x77c /* max size type86 v2 reply      */
  39
  40#define PCIXCC_MAX_XCRB_MESSAGE_SIZE (12*1024)
  41
  42#define PCIXCC_CLEANUP_TIME     (15*HZ)
  43
  44#define CEIL4(x) ((((x)+3)/4)*4)
  45
  46struct response_type {
  47        struct completion work;
  48        int type;
  49};
  50#define PCIXCC_RESPONSE_TYPE_ICA  0
  51#define PCIXCC_RESPONSE_TYPE_XCRB 1
  52
  53MODULE_AUTHOR("IBM Corporation");
  54MODULE_DESCRIPTION("PCIXCC Cryptographic Coprocessor device driver, " \
  55                   "Copyright IBM Corp. 2001, 2012");
  56MODULE_LICENSE("GPL");
  57
  58static struct ap_device_id zcrypt_pcixcc_card_ids[] = {
  59        { .dev_type = AP_DEVICE_TYPE_PCIXCC,
  60          .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  61        { .dev_type = AP_DEVICE_TYPE_CEX2C,
  62          .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  63        { .dev_type = AP_DEVICE_TYPE_CEX3C,
  64          .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  65        { /* end of list */ },
  66};
  67
  68MODULE_DEVICE_TABLE(ap, zcrypt_pcixcc_card_ids);
  69
  70static struct ap_device_id zcrypt_pcixcc_queue_ids[] = {
  71        { .dev_type = AP_DEVICE_TYPE_PCIXCC,
  72          .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  73        { .dev_type = AP_DEVICE_TYPE_CEX2C,
  74          .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  75        { .dev_type = AP_DEVICE_TYPE_CEX3C,
  76          .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  77        { /* end of list */ },
  78};
  79
  80MODULE_DEVICE_TABLE(ap, zcrypt_pcixcc_queue_ids);
  81
  82/**
  83 * Large random number detection function. Its sends a message to a pcixcc
  84 * card to find out if large random numbers are supported.
  85 * @ap_dev: pointer to the AP device.
  86 *
  87 * Returns 1 if large random numbers are supported, 0 if not and < 0 on error.
  88 */
  89static int zcrypt_pcixcc_rng_supported(struct ap_queue *aq)
  90{
  91        struct ap_message ap_msg;
  92        unsigned long long psmid;
  93        unsigned int domain;
  94        struct {
  95                struct type86_hdr hdr;
  96                struct type86_fmt2_ext fmt2;
  97                struct CPRBX cprbx;
  98        } __attribute__((packed)) *reply;
  99        struct {
 100                struct type6_hdr hdr;
 101                struct CPRBX cprbx;
 102                char function_code[2];
 103                short int rule_length;
 104                char rule[8];
 105                short int verb_length;
 106                short int key_length;
 107        } __packed * msg;
 108        int rc, i;
 109
 110        ap_init_message(&ap_msg);
 111        ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
 112        if (!ap_msg.message)
 113                return -ENOMEM;
 114
 115        rng_type6CPRB_msgX(&ap_msg, 4, &domain);
 116
 117        msg = ap_msg.message;
 118        msg->cprbx.domain = AP_QID_QUEUE(aq->qid);
 119
 120        rc = ap_send(aq->qid, 0x0102030405060708ULL, ap_msg.message,
 121                     ap_msg.length);
 122        if (rc)
 123                goto out_free;
 124
 125        /* Wait for the test message to complete. */
 126        for (i = 0; i < 2 * HZ; i++) {
 127                msleep(1000 / HZ);
 128                rc = ap_recv(aq->qid, &psmid, ap_msg.message, 4096);
 129                if (rc == 0 && psmid == 0x0102030405060708ULL)
 130                        break;
 131        }
 132
 133        if (i >= 2 * HZ) {
 134                /* Got no answer. */
 135                rc = -ENODEV;
 136                goto out_free;
 137        }
 138
 139        reply = ap_msg.message;
 140        if (reply->cprbx.ccp_rtcode == 0 && reply->cprbx.ccp_rscode == 0)
 141                rc = 1;
 142        else
 143                rc = 0;
 144out_free:
 145        free_page((unsigned long) ap_msg.message);
 146        return rc;
 147}
 148
 149/**
 150 * Probe function for PCIXCC/CEX2C card devices. It always accepts the
 151 * AP device since the bus_match already checked the hardware type. The
 152 * PCIXCC cards come in two flavours: micro code level 2 and micro code
 153 * level 3. This is checked by sending a test message to the device.
 154 * @ap_dev: pointer to the AP card device.
 155 */
 156static int zcrypt_pcixcc_card_probe(struct ap_device *ap_dev)
 157{
 158        /*
 159         * Normalized speed ratings per crypto adapter
 160         * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
 161         */
 162        static const int CEX2C_SPEED_IDX[] = {
 163                1000, 1400, 2400, 1100, 1500, 2600, 100, 12};
 164        static const int CEX3C_SPEED_IDX[] = {
 165                500,  700, 1400,  550,  800, 1500,  80, 10};
 166
 167        struct ap_card *ac = to_ap_card(&ap_dev->device);
 168        struct zcrypt_card *zc;
 169        int rc = 0;
 170
 171        zc = zcrypt_card_alloc();
 172        if (!zc)
 173                return -ENOMEM;
 174        zc->card = ac;
 175        ac->private = zc;
 176        switch (ac->ap_dev.device_type) {
 177        case AP_DEVICE_TYPE_CEX2C:
 178                zc->user_space_type = ZCRYPT_CEX2C;
 179                zc->type_string = "CEX2C";
 180                memcpy(zc->speed_rating, CEX2C_SPEED_IDX,
 181                       sizeof(CEX2C_SPEED_IDX));
 182                zc->min_mod_size = PCIXCC_MIN_MOD_SIZE;
 183                zc->max_mod_size = PCIXCC_MAX_MOD_SIZE;
 184                zc->max_exp_bit_length = PCIXCC_MAX_MOD_SIZE;
 185                break;
 186        case AP_DEVICE_TYPE_CEX3C:
 187                zc->user_space_type = ZCRYPT_CEX3C;
 188                zc->type_string = "CEX3C";
 189                memcpy(zc->speed_rating, CEX3C_SPEED_IDX,
 190                       sizeof(CEX3C_SPEED_IDX));
 191                zc->min_mod_size = CEX3C_MIN_MOD_SIZE;
 192                zc->max_mod_size = CEX3C_MAX_MOD_SIZE;
 193                zc->max_exp_bit_length = CEX3C_MAX_MOD_SIZE;
 194                break;
 195        default:
 196                zcrypt_card_free(zc);
 197                return -ENODEV;
 198        }
 199        zc->online = 1;
 200
 201        rc = zcrypt_card_register(zc);
 202        if (rc) {
 203                ac->private = NULL;
 204                zcrypt_card_free(zc);
 205        }
 206
 207        return rc;
 208}
 209
 210/**
 211 * This is called to remove the PCIXCC/CEX2C card driver information
 212 * if an AP card device is removed.
 213 */
 214static void zcrypt_pcixcc_card_remove(struct ap_device *ap_dev)
 215{
 216        struct zcrypt_card *zc = to_ap_card(&ap_dev->device)->private;
 217
 218        if (zc)
 219                zcrypt_card_unregister(zc);
 220}
 221
 222static struct ap_driver zcrypt_pcixcc_card_driver = {
 223        .probe = zcrypt_pcixcc_card_probe,
 224        .remove = zcrypt_pcixcc_card_remove,
 225        .ids = zcrypt_pcixcc_card_ids,
 226};
 227
 228/**
 229 * Probe function for PCIXCC/CEX2C queue devices. It always accepts the
 230 * AP device since the bus_match already checked the hardware type. The
 231 * PCIXCC cards come in two flavours: micro code level 2 and micro code
 232 * level 3. This is checked by sending a test message to the device.
 233 * @ap_dev: pointer to the AP card device.
 234 */
 235static int zcrypt_pcixcc_queue_probe(struct ap_device *ap_dev)
 236{
 237        struct ap_queue *aq = to_ap_queue(&ap_dev->device);
 238        struct zcrypt_queue *zq;
 239        int rc;
 240
 241        zq = zcrypt_queue_alloc(PCIXCC_MAX_XCRB_MESSAGE_SIZE);
 242        if (!zq)
 243                return -ENOMEM;
 244        zq->queue = aq;
 245        zq->online = 1;
 246        atomic_set(&zq->load, 0);
 247        rc = zcrypt_pcixcc_rng_supported(aq);
 248        if (rc < 0) {
 249                zcrypt_queue_free(zq);
 250                return rc;
 251        }
 252        if (rc)
 253                zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
 254                                         MSGTYPE06_VARIANT_DEFAULT);
 255        else
 256                zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
 257                                         MSGTYPE06_VARIANT_NORNG);
 258        ap_queue_init_reply(aq, &zq->reply);
 259        aq->request_timeout = PCIXCC_CLEANUP_TIME,
 260        aq->private = zq;
 261        rc = zcrypt_queue_register(zq);
 262        if (rc) {
 263                aq->private = NULL;
 264                zcrypt_queue_free(zq);
 265        }
 266        return rc;
 267}
 268
 269/**
 270 * This is called to remove the PCIXCC/CEX2C queue driver information
 271 * if an AP queue device is removed.
 272 */
 273static void zcrypt_pcixcc_queue_remove(struct ap_device *ap_dev)
 274{
 275        struct ap_queue *aq = to_ap_queue(&ap_dev->device);
 276        struct zcrypt_queue *zq = aq->private;
 277
 278        ap_queue_remove(aq);
 279        if (zq)
 280                zcrypt_queue_unregister(zq);
 281}
 282
 283static struct ap_driver zcrypt_pcixcc_queue_driver = {
 284        .probe = zcrypt_pcixcc_queue_probe,
 285        .remove = zcrypt_pcixcc_queue_remove,
 286        .suspend = ap_queue_suspend,
 287        .resume = ap_queue_resume,
 288        .ids = zcrypt_pcixcc_queue_ids,
 289};
 290
 291int __init zcrypt_pcixcc_init(void)
 292{
 293        int rc;
 294
 295        rc = ap_driver_register(&zcrypt_pcixcc_card_driver,
 296                                THIS_MODULE, "pcixcccard");
 297        if (rc)
 298                return rc;
 299
 300        rc = ap_driver_register(&zcrypt_pcixcc_queue_driver,
 301                                THIS_MODULE, "pcixccqueue");
 302        if (rc)
 303                ap_driver_unregister(&zcrypt_pcixcc_card_driver);
 304
 305        return rc;
 306}
 307
 308void zcrypt_pcixcc_exit(void)
 309{
 310        ap_driver_unregister(&zcrypt_pcixcc_queue_driver);
 311        ap_driver_unregister(&zcrypt_pcixcc_card_driver);
 312}
 313
 314module_init(zcrypt_pcixcc_init);
 315module_exit(zcrypt_pcixcc_exit);
 316