0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef _ZCRYPT_API_H_
0015 #define _ZCRYPT_API_H_
0016
0017 #include <linux/atomic.h>
0018 #include <asm/debug.h>
0019 #include <asm/zcrypt.h>
0020 #include "ap_bus.h"
0021
0022
0023
0024
0025 #define ZCRYPT_CEX2C 5
0026 #define ZCRYPT_CEX2A 6
0027 #define ZCRYPT_CEX3C 7
0028 #define ZCRYPT_CEX3A 8
0029 #define ZCRYPT_CEX4 10
0030 #define ZCRYPT_CEX5 11
0031 #define ZCRYPT_CEX6 12
0032 #define ZCRYPT_CEX7 13
0033
0034
0035
0036
0037
0038
0039 #define ZCRYPT_RNG_BUFFER_SIZE 4096
0040
0041
0042
0043
0044 enum crypto_ops {
0045 MEX_1K,
0046 MEX_2K,
0047 MEX_4K,
0048 CRT_1K,
0049 CRT_2K,
0050 CRT_4K,
0051 HWRNG,
0052 SECKEY,
0053 NUM_OPS
0054 };
0055
0056 struct zcrypt_queue;
0057
0058
0059 struct zcrypt_track {
0060 int again_counter;
0061 int last_qid;
0062 int last_rc;
0063 #ifdef CONFIG_ZCRYPT_DEBUG
0064 struct ap_fi fi;
0065 #endif
0066 };
0067
0068
0069 #define TRACK_AGAIN_MAX 10
0070 #define TRACK_AGAIN_CARD_WEIGHT_PENALTY 1000
0071 #define TRACK_AGAIN_QUEUE_WEIGHT_PENALTY 10000
0072
0073 struct zcrypt_ops {
0074 long (*rsa_modexpo)(struct zcrypt_queue *, struct ica_rsa_modexpo *,
0075 struct ap_message *);
0076 long (*rsa_modexpo_crt)(struct zcrypt_queue *,
0077 struct ica_rsa_modexpo_crt *,
0078 struct ap_message *);
0079 long (*send_cprb)(bool userspace, struct zcrypt_queue *, struct ica_xcRB *,
0080 struct ap_message *);
0081 long (*send_ep11_cprb)(bool userspace, struct zcrypt_queue *, struct ep11_urb *,
0082 struct ap_message *);
0083 long (*rng)(struct zcrypt_queue *, char *, struct ap_message *);
0084 struct list_head list;
0085 struct module *owner;
0086 int variant;
0087 char name[128];
0088 };
0089
0090 struct zcrypt_card {
0091 struct list_head list;
0092 struct list_head zqueues;
0093 struct kref refcount;
0094 struct ap_card *card;
0095 int online;
0096
0097 int user_space_type;
0098 char *type_string;
0099 int min_mod_size;
0100 int max_mod_size;
0101 int max_exp_bit_length;
0102 const int *speed_rating;
0103 atomic_t load;
0104
0105 int request_count;
0106 };
0107
0108 struct zcrypt_queue {
0109 struct list_head list;
0110 struct kref refcount;
0111 struct zcrypt_card *zcard;
0112 struct zcrypt_ops *ops;
0113 struct ap_queue *queue;
0114 int online;
0115
0116 atomic_t load;
0117
0118 int request_count;
0119
0120 struct ap_message reply;
0121 };
0122
0123
0124 extern atomic_t zcrypt_rescan_req;
0125
0126 extern spinlock_t zcrypt_list_lock;
0127 extern struct list_head zcrypt_card_list;
0128
0129 #define for_each_zcrypt_card(_zc) \
0130 list_for_each_entry(_zc, &zcrypt_card_list, list)
0131
0132 #define for_each_zcrypt_queue(_zq, _zc) \
0133 list_for_each_entry(_zq, &(_zc)->zqueues, list)
0134
0135 struct zcrypt_card *zcrypt_card_alloc(void);
0136 void zcrypt_card_free(struct zcrypt_card *);
0137 void zcrypt_card_get(struct zcrypt_card *);
0138 int zcrypt_card_put(struct zcrypt_card *);
0139 int zcrypt_card_register(struct zcrypt_card *);
0140 void zcrypt_card_unregister(struct zcrypt_card *);
0141
0142 struct zcrypt_queue *zcrypt_queue_alloc(size_t);
0143 void zcrypt_queue_free(struct zcrypt_queue *);
0144 void zcrypt_queue_get(struct zcrypt_queue *);
0145 int zcrypt_queue_put(struct zcrypt_queue *);
0146 int zcrypt_queue_register(struct zcrypt_queue *);
0147 void zcrypt_queue_unregister(struct zcrypt_queue *);
0148 bool zcrypt_queue_force_online(struct zcrypt_queue *zq, int online);
0149
0150 int zcrypt_rng_device_add(void);
0151 void zcrypt_rng_device_remove(void);
0152
0153 void zcrypt_msgtype_register(struct zcrypt_ops *);
0154 void zcrypt_msgtype_unregister(struct zcrypt_ops *);
0155 struct zcrypt_ops *zcrypt_msgtype(unsigned char *, int);
0156 int zcrypt_api_init(void);
0157 void zcrypt_api_exit(void);
0158 long zcrypt_send_cprb(struct ica_xcRB *xcRB);
0159 long zcrypt_send_ep11_cprb(struct ep11_urb *urb);
0160 void zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext *devstatus);
0161 int zcrypt_device_status_ext(int card, int queue,
0162 struct zcrypt_device_status_ext *devstatus);
0163
0164 int zcrypt_wait_api_operational(void);
0165
0166 static inline unsigned long z_copy_from_user(bool userspace,
0167 void *to,
0168 const void __user *from,
0169 unsigned long n)
0170 {
0171 if (likely(userspace))
0172 return copy_from_user(to, from, n);
0173 memcpy(to, (void __force *)from, n);
0174 return 0;
0175 }
0176
0177 static inline unsigned long z_copy_to_user(bool userspace,
0178 void __user *to,
0179 const void *from,
0180 unsigned long n)
0181 {
0182 if (likely(userspace))
0183 return copy_to_user(to, from, n);
0184 memcpy((void __force *)to, from, n);
0185 return 0;
0186 }
0187
0188 #endif