^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0+ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright IBM Corp. 2001, 2019
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Author(s): Robert Burroughs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Eric Rossman (edrossma@us.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Cornelia Huck <cornelia.huck@de.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Ralph Wuerthner <rwuerthn@de.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #ifndef _ZCRYPT_API_H_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define _ZCRYPT_API_H_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <asm/debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/zcrypt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "ap_bus.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * Supported device types
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define ZCRYPT_CEX2C 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define ZCRYPT_CEX2A 6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define ZCRYPT_CEX3C 7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define ZCRYPT_CEX3A 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define ZCRYPT_CEX4 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define ZCRYPT_CEX5 11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define ZCRYPT_CEX6 12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define ZCRYPT_CEX7 13
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * Large random numbers are pulled in 4096 byte chunks from the crypto cards
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * and stored in a page. Be careful when increasing this buffer due to size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * limitations for AP requests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define ZCRYPT_RNG_BUFFER_SIZE 4096
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * Identifier for Crypto Request Performance Index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) enum crypto_ops {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) MEX_1K,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) MEX_2K,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) MEX_4K,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) CRT_1K,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) CRT_2K,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) CRT_4K,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) HWRNG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) SECKEY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) NUM_OPS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct zcrypt_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /* struct to hold tracking information for a userspace request/response */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct zcrypt_track {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) int again_counter; /* retry attempts counter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) int last_qid; /* last qid used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int last_rc; /* last return code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #ifdef CONFIG_ZCRYPT_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct ap_fi fi; /* failure injection cmd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /* defines related to message tracking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) #define TRACK_AGAIN_MAX 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #define TRACK_AGAIN_CARD_WEIGHT_PENALTY 1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #define TRACK_AGAIN_QUEUE_WEIGHT_PENALTY 10000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct zcrypt_ops {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) long (*rsa_modexpo)(struct zcrypt_queue *, struct ica_rsa_modexpo *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct ap_message *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) long (*rsa_modexpo_crt)(struct zcrypt_queue *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct ica_rsa_modexpo_crt *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct ap_message *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) long (*send_cprb)(bool userspace, struct zcrypt_queue *, struct ica_xcRB *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct ap_message *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) long (*send_ep11_cprb)(bool userspace, struct zcrypt_queue *, struct ep11_urb *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct ap_message *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) long (*rng)(struct zcrypt_queue *, char *, struct ap_message *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct list_head list; /* zcrypt ops list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct module *owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) int variant;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) char name[128];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct zcrypt_card {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct list_head list; /* Device list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct list_head zqueues; /* List of zcrypt queues */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct kref refcount; /* device refcounting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct ap_card *card; /* The "real" ap card device. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) int online; /* User online/offline */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) int user_space_type; /* User space device id. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) char *type_string; /* User space device name. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int min_mod_size; /* Min number of bits. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) int max_mod_size; /* Max number of bits. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) int max_exp_bit_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) const int *speed_rating; /* Speed idx of crypto ops. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) atomic_t load; /* Utilization of the crypto device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) int request_count; /* # current requests. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct zcrypt_queue {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct list_head list; /* Device list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct kref refcount; /* device refcounting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct zcrypt_card *zcard;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct zcrypt_ops *ops; /* Crypto operations. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct ap_queue *queue; /* The "real" ap queue device. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int online; /* User online/offline */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) atomic_t load; /* Utilization of the crypto device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) int request_count; /* # current requests. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct ap_message reply; /* Per-device reply structure. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /* transport layer rescanning */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) extern atomic_t zcrypt_rescan_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) extern spinlock_t zcrypt_list_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) extern int zcrypt_device_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) extern struct list_head zcrypt_card_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) #define for_each_zcrypt_card(_zc) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) list_for_each_entry(_zc, &zcrypt_card_list, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) #define for_each_zcrypt_queue(_zq, _zc) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) list_for_each_entry(_zq, &(_zc)->zqueues, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct zcrypt_card *zcrypt_card_alloc(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) void zcrypt_card_free(struct zcrypt_card *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) void zcrypt_card_get(struct zcrypt_card *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) int zcrypt_card_put(struct zcrypt_card *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) int zcrypt_card_register(struct zcrypt_card *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) void zcrypt_card_unregister(struct zcrypt_card *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct zcrypt_queue *zcrypt_queue_alloc(size_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) void zcrypt_queue_free(struct zcrypt_queue *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) void zcrypt_queue_get(struct zcrypt_queue *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) int zcrypt_queue_put(struct zcrypt_queue *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) int zcrypt_queue_register(struct zcrypt_queue *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) void zcrypt_queue_unregister(struct zcrypt_queue *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) void zcrypt_queue_force_online(struct zcrypt_queue *, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) int zcrypt_rng_device_add(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) void zcrypt_rng_device_remove(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) void zcrypt_msgtype_register(struct zcrypt_ops *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) void zcrypt_msgtype_unregister(struct zcrypt_ops *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) struct zcrypt_ops *zcrypt_msgtype(unsigned char *, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) int zcrypt_api_init(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) void zcrypt_api_exit(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) long zcrypt_send_cprb(struct ica_xcRB *xcRB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) long zcrypt_send_ep11_cprb(struct ep11_urb *urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) void zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext *devstatus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) int zcrypt_device_status_ext(int card, int queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct zcrypt_device_status_ext *devstatus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static inline unsigned long z_copy_from_user(bool userspace,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) void *to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) const void __user *from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) unsigned long n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (likely(userspace))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return copy_from_user(to, from, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) memcpy(to, (void __force *) from, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static inline unsigned long z_copy_to_user(bool userspace,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) void __user *to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) const void *from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) unsigned long n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (likely(userspace))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return copy_to_user(to, from, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) memcpy((void __force *) to, from, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) #endif /* _ZCRYPT_API_H_ */