linux/drivers/s390/crypto/zcrypt_cex2a.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 *  Copyright IBM Corp. 2001, 2012
   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/slab.h>
  15#include <linux/init.h>
  16#include <linux/err.h>
  17#include <linux/atomic.h>
  18#include <linux/uaccess.h>
  19#include <linux/mod_devicetable.h>
  20
  21#include "ap_bus.h"
  22#include "zcrypt_api.h"
  23#include "zcrypt_error.h"
  24#include "zcrypt_cex2a.h"
  25#include "zcrypt_msgtype50.h"
  26
  27#define CEX2A_MIN_MOD_SIZE        1     /*    8 bits    */
  28#define CEX2A_MAX_MOD_SIZE      256     /* 2048 bits    */
  29#define CEX3A_MIN_MOD_SIZE      CEX2A_MIN_MOD_SIZE
  30#define CEX3A_MAX_MOD_SIZE      512     /* 4096 bits    */
  31
  32#define CEX2A_MAX_MESSAGE_SIZE  0x390   /* sizeof(struct type50_crb2_msg)    */
  33#define CEX2A_MAX_RESPONSE_SIZE 0x110   /* max outputdatalength + type80_hdr */
  34
  35#define CEX3A_MAX_RESPONSE_SIZE 0x210   /* 512 bit modulus
  36                                         * (max outputdatalength) +
  37                                         * type80_hdr*/
  38#define CEX3A_MAX_MESSAGE_SIZE  sizeof(struct type50_crb3_msg)
  39
  40#define CEX2A_CLEANUP_TIME      (15*HZ)
  41#define CEX3A_CLEANUP_TIME      CEX2A_CLEANUP_TIME
  42
  43MODULE_AUTHOR("IBM Corporation");
  44MODULE_DESCRIPTION("CEX2A/CEX3A Cryptographic Coprocessor device driver, " \
  45                   "Copyright IBM Corp. 2001, 2018");
  46MODULE_LICENSE("GPL");
  47
  48static struct ap_device_id zcrypt_cex2a_card_ids[] = {
  49        { .dev_type = AP_DEVICE_TYPE_CEX2A,
  50          .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  51        { .dev_type = AP_DEVICE_TYPE_CEX3A,
  52          .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  53        { /* end of list */ },
  54};
  55
  56MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_card_ids);
  57
  58static struct ap_device_id zcrypt_cex2a_queue_ids[] = {
  59        { .dev_type = AP_DEVICE_TYPE_CEX2A,
  60          .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  61        { .dev_type = AP_DEVICE_TYPE_CEX3A,
  62          .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  63        { /* end of list */ },
  64};
  65
  66MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_queue_ids);
  67
  68/**
  69 * Probe function for CEX2A card devices. It always accepts the AP device
  70 * since the bus_match already checked the card type.
  71 * @ap_dev: pointer to the AP device.
  72 */
  73static int zcrypt_cex2a_card_probe(struct ap_device *ap_dev)
  74{
  75        /*
  76         * Normalized speed ratings per crypto adapter
  77         * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
  78         */
  79        static const int CEX2A_SPEED_IDX[] = {
  80                800, 1000, 2000,  900, 1200, 2400, 0, 0};
  81        static const int CEX3A_SPEED_IDX[] = {
  82                400,  500, 1000,  450,  550, 1200, 0, 0};
  83
  84        struct ap_card *ac = to_ap_card(&ap_dev->device);
  85        struct zcrypt_card *zc;
  86        int rc = 0;
  87
  88        zc = zcrypt_card_alloc();
  89        if (!zc)
  90                return -ENOMEM;
  91        zc->card = ac;
  92        ac->private = zc;
  93
  94        if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX2A) {
  95                zc->min_mod_size = CEX2A_MIN_MOD_SIZE;
  96                zc->max_mod_size = CEX2A_MAX_MOD_SIZE;
  97                memcpy(zc->speed_rating, CEX2A_SPEED_IDX,
  98                       sizeof(CEX2A_SPEED_IDX));
  99                zc->max_exp_bit_length = CEX2A_MAX_MOD_SIZE;
 100                zc->type_string = "CEX2A";
 101                zc->user_space_type = ZCRYPT_CEX2A;
 102        } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX3A) {
 103                zc->min_mod_size = CEX2A_MIN_MOD_SIZE;
 104                zc->max_mod_size = CEX2A_MAX_MOD_SIZE;
 105                zc->max_exp_bit_length = CEX2A_MAX_MOD_SIZE;
 106                if (ap_test_bit(&ac->functions, AP_FUNC_MEX4K) &&
 107                    ap_test_bit(&ac->functions, AP_FUNC_CRT4K)) {
 108                        zc->max_mod_size = CEX3A_MAX_MOD_SIZE;
 109                        zc->max_exp_bit_length = CEX3A_MAX_MOD_SIZE;
 110                }
 111                memcpy(zc->speed_rating, CEX3A_SPEED_IDX,
 112                       sizeof(CEX3A_SPEED_IDX));
 113                zc->type_string = "CEX3A";
 114                zc->user_space_type = ZCRYPT_CEX3A;
 115        } else {
 116                zcrypt_card_free(zc);
 117                return -ENODEV;
 118        }
 119        zc->online = 1;
 120
 121        rc = zcrypt_card_register(zc);
 122        if (rc) {
 123                ac->private = NULL;
 124                zcrypt_card_free(zc);
 125        }
 126
 127        return rc;
 128}
 129
 130/**
 131 * This is called to remove the CEX2A card driver information
 132 * if an AP card device is removed.
 133 */
 134static void zcrypt_cex2a_card_remove(struct ap_device *ap_dev)
 135{
 136        struct zcrypt_card *zc = to_ap_card(&ap_dev->device)->private;
 137
 138        if (zc)
 139                zcrypt_card_unregister(zc);
 140}
 141
 142static struct ap_driver zcrypt_cex2a_card_driver = {
 143        .probe = zcrypt_cex2a_card_probe,
 144        .remove = zcrypt_cex2a_card_remove,
 145        .ids = zcrypt_cex2a_card_ids,
 146        .flags = AP_DRIVER_FLAG_DEFAULT,
 147};
 148
 149/**
 150 * Probe function for CEX2A queue devices. It always accepts the AP device
 151 * since the bus_match already checked the queue type.
 152 * @ap_dev: pointer to the AP device.
 153 */
 154static int zcrypt_cex2a_queue_probe(struct ap_device *ap_dev)
 155{
 156        struct ap_queue *aq = to_ap_queue(&ap_dev->device);
 157        struct zcrypt_queue *zq = NULL;
 158        int rc;
 159
 160        switch (ap_dev->device_type) {
 161        case AP_DEVICE_TYPE_CEX2A:
 162                zq = zcrypt_queue_alloc(CEX2A_MAX_RESPONSE_SIZE);
 163                if (!zq)
 164                        return -ENOMEM;
 165                break;
 166        case AP_DEVICE_TYPE_CEX3A:
 167                zq = zcrypt_queue_alloc(CEX3A_MAX_RESPONSE_SIZE);
 168                if (!zq)
 169                        return -ENOMEM;
 170                break;
 171        }
 172        if (!zq)
 173                return -ENODEV;
 174        zq->ops = zcrypt_msgtype(MSGTYPE50_NAME, MSGTYPE50_VARIANT_DEFAULT);
 175        zq->queue = aq;
 176        zq->online = 1;
 177        atomic_set(&zq->load, 0);
 178        ap_queue_init_reply(aq, &zq->reply);
 179        aq->request_timeout = CEX2A_CLEANUP_TIME,
 180        aq->private = zq;
 181        rc = zcrypt_queue_register(zq);
 182        if (rc) {
 183                aq->private = NULL;
 184                zcrypt_queue_free(zq);
 185        }
 186
 187        return rc;
 188}
 189
 190/**
 191 * This is called to remove the CEX2A queue driver information
 192 * if an AP queue device is removed.
 193 */
 194static void zcrypt_cex2a_queue_remove(struct ap_device *ap_dev)
 195{
 196        struct ap_queue *aq = to_ap_queue(&ap_dev->device);
 197        struct zcrypt_queue *zq = aq->private;
 198
 199        if (zq)
 200                zcrypt_queue_unregister(zq);
 201}
 202
 203static struct ap_driver zcrypt_cex2a_queue_driver = {
 204        .probe = zcrypt_cex2a_queue_probe,
 205        .remove = zcrypt_cex2a_queue_remove,
 206        .suspend = ap_queue_suspend,
 207        .resume = ap_queue_resume,
 208        .ids = zcrypt_cex2a_queue_ids,
 209        .flags = AP_DRIVER_FLAG_DEFAULT,
 210};
 211
 212int __init zcrypt_cex2a_init(void)
 213{
 214        int rc;
 215
 216        rc = ap_driver_register(&zcrypt_cex2a_card_driver,
 217                                THIS_MODULE, "cex2acard");
 218        if (rc)
 219                return rc;
 220
 221        rc = ap_driver_register(&zcrypt_cex2a_queue_driver,
 222                                THIS_MODULE, "cex2aqueue");
 223        if (rc)
 224                ap_driver_unregister(&zcrypt_cex2a_card_driver);
 225
 226        return rc;
 227}
 228
 229void __exit zcrypt_cex2a_exit(void)
 230{
 231        ap_driver_unregister(&zcrypt_cex2a_queue_driver);
 232        ap_driver_unregister(&zcrypt_cex2a_card_driver);
 233}
 234
 235module_init(zcrypt_cex2a_init);
 236module_exit(zcrypt_cex2a_exit);
 237