nx-842.h 5.8 KB

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