zcrypt_api.h 5.4 KB

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