nx-842.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __NX_842_H__
  3. #define __NX_842_H__
  4. #include <linux/kernel.h>
  5. #include <linux/init.h>
  6. #include <linux/module.h>
  7. #include <linux/crypto.h>
  8. #include <linux/of.h>
  9. #include <linux/slab.h>
  10. #include <linux/io.h>
  11. #include <linux/mm.h>
  12. #include <linux/ratelimit.h>
  13. /* Restrictions on Data Descriptor List (DDL) and Entry (DDE) buffers
  14. *
  15. * From NX P8 workbook, sec 4.9.1 "842 details"
  16. * Each DDE buffer is 128 byte aligned
  17. * Each DDE buffer size is a multiple of 32 bytes (except the last)
  18. * The last DDE buffer size is a multiple of 8 bytes
  19. */
  20. #define DDE_BUFFER_ALIGN (128)
  21. #define DDE_BUFFER_SIZE_MULT (32)
  22. #define DDE_BUFFER_LAST_MULT (8)
  23. /* Arbitrary DDL length limit
  24. * Allows max buffer size of MAX-1 to MAX pages
  25. * (depending on alignment)
  26. */
  27. #define DDL_LEN_MAX (17)
  28. /* CCW 842 CI/FC masks
  29. * NX P8 workbook, section 4.3.1, figure 4-6
  30. * "CI/FC Boundary by NX CT type"
  31. */
  32. #define CCW_CI_842 (0x00003ff8)
  33. #define CCW_FC_842 (0x00000007)
  34. /* CCW Function Codes (FC) for 842
  35. * NX P8 workbook, section 4.9, table 4-28
  36. * "Function Code Definitions for 842 Memory Compression"
  37. */
  38. #define CCW_FC_842_COMP_NOCRC (0)
  39. #define CCW_FC_842_COMP_CRC (1)
  40. #define CCW_FC_842_DECOMP_NOCRC (2)
  41. #define CCW_FC_842_DECOMP_CRC (3)
  42. #define CCW_FC_842_MOVE (4)
  43. /* CSB CC Error Types for 842
  44. * NX P8 workbook, section 4.10.3, table 4-30
  45. * "Reported Error Types Summary Table"
  46. */
  47. /* These are all duplicates of existing codes defined in icswx.h. */
  48. #define CSB_CC_TRANSLATION_DUP1 (80)
  49. #define CSB_CC_TRANSLATION_DUP2 (82)
  50. #define CSB_CC_TRANSLATION_DUP3 (84)
  51. #define CSB_CC_TRANSLATION_DUP4 (86)
  52. #define CSB_CC_TRANSLATION_DUP5 (92)
  53. #define CSB_CC_TRANSLATION_DUP6 (94)
  54. #define CSB_CC_PROTECTION_DUP1 (81)
  55. #define CSB_CC_PROTECTION_DUP2 (83)
  56. #define CSB_CC_PROTECTION_DUP3 (85)
  57. #define CSB_CC_PROTECTION_DUP4 (87)
  58. #define CSB_CC_PROTECTION_DUP5 (93)
  59. #define CSB_CC_PROTECTION_DUP6 (95)
  60. #define CSB_CC_RD_EXTERNAL_DUP1 (89)
  61. #define CSB_CC_RD_EXTERNAL_DUP2 (90)
  62. #define CSB_CC_RD_EXTERNAL_DUP3 (91)
  63. /* These are specific to NX */
  64. /* 842 codes */
  65. #define CSB_CC_TPBC_GT_SPBC (64) /* no error, but >1 comp ratio */
  66. #define CSB_CC_CRC_MISMATCH (65) /* decomp crc mismatch */
  67. #define CSB_CC_TEMPL_INVALID (66) /* decomp invalid template value */
  68. #define CSB_CC_TEMPL_OVERFLOW (67) /* decomp template shows data after end */
  69. /* sym crypt codes */
  70. #define CSB_CC_DECRYPT_OVERFLOW (64)
  71. /* asym crypt codes */
  72. #define CSB_CC_MINV_OVERFLOW (128)
  73. /*
  74. * HW error - Job did not finish in the maximum time allowed.
  75. * Job terminated.
  76. */
  77. #define CSB_CC_HW_EXPIRED_TIMER (224)
  78. /* These are reserved for hypervisor use */
  79. #define CSB_CC_HYP_RESERVE_START (240)
  80. #define CSB_CC_HYP_RESERVE_END (253)
  81. #define CSB_CC_HYP_RESERVE_P9_END (251)
  82. /* No valid interrupt server (P9 or later). */
  83. #define CSB_CC_HYP_RESERVE_NO_INTR_SERVER (252)
  84. #define CSB_CC_HYP_NO_HW (254)
  85. #define CSB_CC_HYP_HANG_ABORTED (255)
  86. /* CCB Completion Modes (CM) for 842
  87. * NX P8 workbook, section 4.3, figure 4-5
  88. * "CRB Details - Normal Cop_Req (CL=00, C=1)"
  89. */
  90. #define CCB_CM_EXTRA_WRITE (CCB_CM0_ALL_COMPLETIONS & CCB_CM12_STORE)
  91. #define CCB_CM_INTERRUPT (CCB_CM0_ALL_COMPLETIONS & CCB_CM12_INTERRUPT)
  92. #define LEN_ON_SIZE(pa, size) ((size) - ((pa) & ((size) - 1)))
  93. #define LEN_ON_PAGE(pa) LEN_ON_SIZE(pa, PAGE_SIZE)
  94. static inline unsigned long nx842_get_pa(void *addr)
  95. {
  96. if (!is_vmalloc_addr(addr))
  97. return __pa(addr);
  98. return page_to_phys(vmalloc_to_page(addr)) + offset_in_page(addr);
  99. }
  100. /**
  101. * This provides the driver's constraints. Different nx842 implementations
  102. * may have varying requirements. The constraints are:
  103. * @alignment: All buffers should be aligned to this
  104. * @multiple: All buffer lengths should be a multiple of this
  105. * @minimum: Buffer lengths must not be less than this amount
  106. * @maximum: Buffer lengths must not be more than this amount
  107. *
  108. * The constraints apply to all buffers and lengths, both input and output,
  109. * for both compression and decompression, except for the minimum which
  110. * only applies to compression input and decompression output; the
  111. * compressed data can be less than the minimum constraint. It can be
  112. * assumed that compressed data will always adhere to the multiple
  113. * constraint.
  114. *
  115. * The driver may succeed even if these constraints are violated;
  116. * however the driver can return failure or suffer reduced performance
  117. * if any constraint is not met.
  118. */
  119. struct nx842_constraints {
  120. int alignment;
  121. int multiple;
  122. int minimum;
  123. int maximum;
  124. };
  125. struct nx842_driver {
  126. char *name;
  127. struct module *owner;
  128. size_t workmem_size;
  129. struct nx842_constraints *constraints;
  130. int (*compress)(const unsigned char *in, unsigned int in_len,
  131. unsigned char *out, unsigned int *out_len,
  132. void *wrkmem);
  133. int (*decompress)(const unsigned char *in, unsigned int in_len,
  134. unsigned char *out, unsigned int *out_len,
  135. void *wrkmem);
  136. };
  137. struct nx842_crypto_header_group {
  138. __be16 padding; /* unused bytes at start of group */
  139. __be32 compressed_length; /* compressed bytes in group */
  140. __be32 uncompressed_length; /* bytes after decompression */
  141. } __packed;
  142. struct nx842_crypto_header {
  143. __be16 magic; /* NX842_CRYPTO_MAGIC */
  144. __be16 ignore; /* decompressed end bytes to ignore */
  145. u8 groups; /* total groups in this header */
  146. struct nx842_crypto_header_group group[];
  147. } __packed;
  148. #define NX842_CRYPTO_GROUP_MAX (0x20)
  149. struct nx842_crypto_ctx {
  150. spinlock_t lock;
  151. u8 *wmem;
  152. u8 *sbounce, *dbounce;
  153. struct nx842_crypto_header header;
  154. struct nx842_crypto_header_group group[NX842_CRYPTO_GROUP_MAX];
  155. struct nx842_driver *driver;
  156. };
  157. int nx842_crypto_init(struct crypto_tfm *tfm, struct nx842_driver *driver);
  158. void nx842_crypto_exit(struct crypto_tfm *tfm);
  159. int nx842_crypto_compress(struct crypto_tfm *tfm,
  160. const u8 *src, unsigned int slen,
  161. u8 *dst, unsigned int *dlen);
  162. int nx842_crypto_decompress(struct crypto_tfm *tfm,
  163. const u8 *src, unsigned int slen,
  164. u8 *dst, unsigned int *dlen);
  165. #endif /* __NX_842_H__ */