icp_ocf.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /***************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2007,2008,2009 Intel Corporation. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  22. * The full GNU General Public License is included in this distribution
  23. * in the file called LICENSE.GPL.
  24. *
  25. * Contact Information:
  26. * Intel Corporation
  27. *
  28. * BSD LICENSE
  29. *
  30. * Copyright(c) 2007,2008,2009 Intel Corporation. All rights reserved.
  31. * All rights reserved.
  32. *
  33. * Redistribution and use in source and binary forms, with or without
  34. * modification, are permitted provided that the following conditions
  35. * are met:
  36. *
  37. * * Redistributions of source code must retain the above copyright
  38. * notice, this list of conditions and the following disclaimer.
  39. * * Redistributions in binary form must reproduce the above copyright
  40. * notice, this list of conditions and the following disclaimer in
  41. * the documentation and/or other materials provided with the
  42. * distribution.
  43. * * Neither the name of Intel Corporation nor the names of its
  44. * contributors may be used to endorse or promote products derived
  45. * from this software without specific prior written permission.
  46. *
  47. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  48. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  49. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  50. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  51. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  52. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  53. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  54. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  55. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  56. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  57. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  58. *
  59. *
  60. * version: Security.L.1.0.2-229
  61. *
  62. ***************************************************************************/
  63. /*
  64. * OCF driver header file for the Intel ICP processor.
  65. */
  66. #ifndef ICP_OCF_H_
  67. #define ICP_OCF_H_
  68. #include <cpa.h>
  69. #include <cpa_cy_im.h>
  70. #include <cpa_cy_sym.h>
  71. #include <cpa_cy_rand.h>
  72. #include <cpa_cy_dh.h>
  73. #include <cpa_cy_rsa.h>
  74. #include <cpa_cy_ln.h>
  75. #include <cpa_cy_common.h>
  76. #include <cpa_cy_dsa.h>
  77. #include "icp_os.h"
  78. #define NUM_BITS_IN_BYTE (8)
  79. #define NUM_BITS_IN_BYTE_MINUS_ONE (NUM_BITS_IN_BYTE -1)
  80. #define INVALID_DRIVER_ID (-1)
  81. #define RETURN_RAND_NUM_GEN_FAILED (-1)
  82. /*This is the max block cipher initialisation vector*/
  83. #define MAX_IV_LEN_IN_BYTES (20)
  84. /*This is used to check whether the OCF to this driver session limit has
  85. been disabled*/
  86. #define NO_OCF_TO_DRV_MAX_SESSIONS (0)
  87. /*OCF values mapped here*/
  88. #define ICP_SHA1_DIGEST_SIZE_IN_BYTES (SHA1_HASH_LEN)
  89. #define ICP_SHA256_DIGEST_SIZE_IN_BYTES (SHA2_256_HASH_LEN)
  90. #define ICP_SHA384_DIGEST_SIZE_IN_BYTES (SHA2_384_HASH_LEN)
  91. #define ICP_SHA512_DIGEST_SIZE_IN_BYTES (SHA2_512_HASH_LEN)
  92. #define ICP_MD5_DIGEST_SIZE_IN_BYTES (MD5_HASH_LEN)
  93. #define ARC4_COUNTER_LEN (ARC4_BLOCK_LEN)
  94. #define OCF_REGISTRATION_STATUS_SUCCESS (0)
  95. #define OCF_ZERO_FUNCTIONALITY_REGISTERED (0)
  96. #define ICP_OCF_DRV_NO_CRYPTO_PROCESS_ERROR (0)
  97. #define ICP_OCF_DRV_STATUS_SUCCESS (0)
  98. #define ICP_OCF_DRV_STATUS_FAIL (1)
  99. /*Turn on/off debug options*/
  100. #define ICP_OCF_PRINT_DEBUG_MESSAGES (0)
  101. #define ICP_OCF_PRINT_KERN_ALERT (1)
  102. #define ICP_OCF_PRINT_KERN_ERRS (1)
  103. #if ICP_OCF_PRINT_DEBUG_MESSAGES == 1
  104. #define DPRINTK(args...) \
  105. { \
  106. ICP_IPRINTK(args); \
  107. }
  108. #else //ICP_OCF_PRINT_DEBUG_MESSAGES == 1
  109. #define DPRINTK(args...)
  110. #endif //ICP_OCF_PRINT_DEBUG_MESSAGES == 1
  111. #if ICP_OCF_PRINT_KERN_ALERT == 1
  112. #define APRINTK(args...) \
  113. { \
  114. ICP_APRINTK(args); \
  115. }
  116. #else //ICP_OCF_PRINT_KERN_ALERT == 1
  117. #define APRINTK(args...)
  118. #endif //ICP_OCF_PRINT_KERN_ALERT == 1
  119. #if ICP_OCF_PRINT_KERN_ERRS == 1
  120. #define EPRINTK(args...) \
  121. { \
  122. ICP_EPRINTK(args); \
  123. }
  124. #else //ICP_OCF_PRINT_KERN_ERRS == 1
  125. #define EPRINTK(args...)
  126. #endif //ICP_OCF_PRINT_KERN_ERRS == 1
  127. #define IPRINTK(args...) \
  128. { \
  129. ICP_IPRINTK(args); \
  130. }
  131. /*DSA Prime Q size in bytes (as defined in the standard) */
  132. #define DSA_RS_SIGN_PRIMEQ_SIZE_IN_BYTES (20)
  133. #define BITS_TO_BYTES(bytes, bits) \
  134. bytes = (bits + NUM_BITS_IN_BYTE_MINUS_ONE) / NUM_BITS_IN_BYTE
  135. typedef enum {
  136. ICP_OCF_DRV_ALG_CIPHER = 0,
  137. ICP_OCF_DRV_ALG_HASH
  138. } icp_ocf_drv_alg_type_t;
  139. typedef ICP_LIST_HEAD(icp_drvSessionListHead_s,
  140. icp_drvSessionData) icp_drvSessionListHead_t;
  141. /*Values used to derisk chances of performs being called against
  142. deregistered sessions (for which the slab page has been reclaimed)
  143. This is not a fix - since page frames are reclaimed from a slab, one cannot
  144. rely on that memory not being re-used by another app.*/
  145. typedef enum {
  146. ICP_SESSION_INITIALISED = 0x5C5C5C,
  147. ICP_SESSION_RUNNING = 0x005C00,
  148. ICP_SESSION_DEREGISTERED = 0xC5C5C5
  149. } usage_derisk;
  150. /* This struct is required for deferred session
  151. deregistration as a work queue function can
  152. only have one argument*/
  153. struct icp_ocfDrvFreeLacSession {
  154. CpaCySymSessionCtx sessionToDeregister;
  155. icp_workstruct work;
  156. };
  157. /*
  158. This is the OCF<->OCF_DRV session object:
  159. 1.listNode
  160. The first member is a listNode. These session objects are added to a linked
  161. list in order to make it easier to remove them all at session exit time.
  162. 2.inUse
  163. The second member is used to give the session object state and derisk the
  164. possibility of OCF batch calls executing against a deregistered session (as
  165. described above).
  166. 3.sessHandle
  167. The third member is a LAC<->OCF_DRV session handle (initialised with the first
  168. perform request for that session).
  169. 4.lacSessCtx
  170. The fourth is the LAC session context. All the parameters for this structure
  171. are only known when the first perform request for this session occurs. That is
  172. why the OCF EP80579 Driver only registers a new LAC session at perform time
  173. */
  174. struct icp_drvSessionData {
  175. ICP_LIST_ENTRY(icp_drvSessionData) listNode;
  176. usage_derisk inUse;
  177. CpaCySymSessionCtx sessHandle;
  178. CpaCySymSessionSetupData lacSessCtx;
  179. };
  180. /* These are all defined in icp_common.c */
  181. extern icp_atomic_t lac_session_failed_dereg_count;
  182. extern icp_atomic_t icp_ocfDrvIsExiting;
  183. extern icp_atomic_t num_ocf_to_drv_registered_sessions;
  184. extern int32_t icp_ocfDrvDriverId;
  185. extern icp_drvSessionListHead_t icp_ocfDrvGlobalSymListHead;
  186. extern icp_drvSessionListHead_t icp_ocfDrvGlobalSymListHead_FreeMemList;
  187. extern icp_workqueue *icp_ocfDrvFreeLacSessionWorkQ;
  188. extern icp_spinlock_t icp_ocfDrvSymSessInfoListSpinlock;
  189. /*Slab zones for symettric functionality, instantiated in icp_common.c*/
  190. extern icp_kmem_cache drvSessionData_zone;
  191. extern icp_kmem_cache drvOpData_zone;
  192. /*Slabs zones for asymettric functionality, instantiated in icp_common.c*/
  193. extern icp_kmem_cache drvDH_zone;
  194. extern icp_kmem_cache drvLnModExp_zone;
  195. extern icp_kmem_cache drvRSADecrypt_zone;
  196. extern icp_kmem_cache drvRSAPrivateKey_zone;
  197. extern icp_kmem_cache drvDSARSSign_zone;
  198. extern icp_kmem_cache drvDSARSSignKValue_zone;
  199. extern icp_kmem_cache drvDSAVerify_zone;
  200. /* Module parameters defined in icp_cpmmon.c*/
  201. /* Module parameters - gives the number of times LAC deregistration shall be
  202. re-tried */
  203. extern int num_dereg_retries;
  204. /* Module parameter - gives the delay time in jiffies before a LAC session
  205. shall be attempted to be deregistered again */
  206. extern int dereg_retry_delay_in_jiffies;
  207. /* Module parameter - gives the maximum number of sessions possible between
  208. OCF and the OCF EP80579 Driver. If set to zero, there is no limit.*/
  209. extern int max_sessions;
  210. /*Slab zones for flatbuffers and bufferlist*/
  211. extern icp_kmem_cache drvFlatBuffer_zone;
  212. #define ICP_OCF_DRV_DEFAULT_BUFFLIST_ARRAYS (16)
  213. struct icp_drvBuffListInfo {
  214. Cpa16U numBuffers;
  215. Cpa32U metaSize;
  216. Cpa32U metaOffset;
  217. Cpa32U buffListSize;
  218. };
  219. extern struct icp_drvBuffListInfo defBuffListInfo;
  220. /* This struct is used to keep a reference to the relevant node in the list
  221. of sessionData structs, to the buffer type required by OCF and to the OCF
  222. provided crp struct that needs to be returned. All this info is needed in
  223. the callback function.*/
  224. struct icp_drvOpData {
  225. CpaCySymOpData lacOpData;
  226. uint32_t digestSizeInBytes;
  227. struct cryptop *crp;
  228. uint8_t bufferType;
  229. uint8_t ivData[MAX_IV_LEN_IN_BYTES];
  230. uint16_t numBufferListArray;
  231. CpaBufferList srcBuffer;
  232. CpaFlatBuffer bufferListArray[ICP_OCF_DRV_DEFAULT_BUFFLIST_ARRAYS];
  233. CpaBoolean verifyResult;
  234. };
  235. /* Create a new session between OCF and this driver*/
  236. int icp_ocfDrvNewSession(icp_device_t dev, uint32_t * sild,
  237. struct cryptoini *cri);
  238. /* Free a session between this driver and the Quick Assist Framework*/
  239. int icp_ocfDrvFreeLACSession(icp_device_t dev, uint64_t sid);
  240. /* Defer freeing a Quick Assist session*/
  241. void icp_ocfDrvDeferedFreeLacSessionProcess(void *arg);
  242. /* Process OCF cryptographic request for a symmetric algorithm*/
  243. int icp_ocfDrvSymProcess(icp_device_t dev, struct cryptop *crp, int hint);
  244. /* Process OCF cryptographic request for an asymmetric algorithm*/
  245. int icp_ocfDrvPkeProcess(icp_device_t dev, struct cryptkop *krp, int hint);
  246. /* Populate a buffer with random data*/
  247. int icp_ocfDrvReadRandom(void *arg, uint32_t * buf, int maxwords);
  248. /* Retry Quick Assist session deregistration*/
  249. int icp_ocfDrvDeregRetry(CpaCySymSessionCtx sessionToDeregister);
  250. /* Convert an OS scatter gather list to a CPA buffer list*/
  251. int icp_ocfDrvPacketBuffToBufferList(icp_packet_buffer_t * pPacketBuffer,
  252. CpaBufferList * bufferList);
  253. /* Convert a CPA buffer list to an OS scatter gather list*/
  254. int icp_ocfDrvBufferListToPacketBuff(CpaBufferList * bufferList,
  255. icp_packet_buffer_t ** pPacketBuffer);
  256. /* Get the number of buffers in an OS scatter gather list*/
  257. uint16_t icp_ocfDrvGetPacketBuffFrags(icp_packet_buffer_t * pPacketBuffer);
  258. /* Convert a single OS buffer to a CPA Flat Buffer*/
  259. void icp_ocfDrvSinglePacketBuffToFlatBuffer(icp_packet_buffer_t * pPacketBuffer,
  260. CpaFlatBuffer * pFlatBuffer);
  261. /* Add pointer and length to a CPA Flat Buffer structure*/
  262. void icp_ocfDrvPtrAndLenToFlatBuffer(void *pData, uint32_t len,
  263. CpaFlatBuffer * pFlatBuffer);
  264. /* Convert pointer and length values to a CPA buffer list*/
  265. void icp_ocfDrvPtrAndLenToBufferList(void *pDataIn, uint32_t length,
  266. CpaBufferList * pBufferList);
  267. /* Convert a CPA buffer list to pointer and length values*/
  268. void icp_ocfDrvBufferListToPtrAndLen(CpaBufferList * pBufferList,
  269. void **ppDataOut, uint32_t * pLength);
  270. /* Set the number of flat buffers in bufferlist and the size of memory
  271. to allocate for the pPrivateMetaData member of the CpaBufferList.*/
  272. int icp_ocfDrvBufferListMemInfo(uint16_t numBuffers,
  273. struct icp_drvBuffListInfo *buffListInfo);
  274. /* Find pointer position of the digest within an OS scatter gather list*/
  275. uint8_t *icp_ocfDrvPacketBufferDigestPointerFind(struct icp_drvOpData
  276. *drvOpData,
  277. int offsetInBytes,
  278. uint32_t digestSizeInBytes);
  279. /*This top level function is used to find a pointer to where a digest is
  280. stored/needs to be inserted. */
  281. uint8_t *icp_ocfDrvDigestPointerFind(struct icp_drvOpData *drvOpData,
  282. struct cryptodesc *crp_desc);
  283. /* Free a CPA flat buffer*/
  284. void icp_ocfDrvFreeFlatBuffer(CpaFlatBuffer * pFlatBuffer);
  285. /* This function will allocate memory for the pPrivateMetaData
  286. member of CpaBufferList. */
  287. int icp_ocfDrvAllocMetaData(CpaBufferList * pBufferList,
  288. struct icp_drvOpData *pOpData);
  289. /* Free data allocated for the pPrivateMetaData
  290. member of CpaBufferList.*/
  291. void icp_ocfDrvFreeMetaData(CpaBufferList * pBufferList);
  292. #define ICP_CACHE_CREATE(cache_ID, cache_name) \
  293. icp_kmem_cache_create(cache_ID, sizeof(cache_name),ICP_KERNEL_CACHE_ALIGN,\
  294. ICP_KERNEL_CACHE_NOINIT)
  295. #define ICP_CACHE_FREE(args...) \
  296. icp_kmem_cache_free (args)
  297. #define ICP_CACHE_DESTROY(slab_zone)\
  298. {\
  299. if(NULL != slab_zone){\
  300. icp_kmem_cache_destroy(slab_zone);\
  301. slab_zone = NULL;\
  302. }\
  303. }
  304. #endif
  305. /* ICP_OCF_H_ */