1 #ifndef _H_MXHW_CRYPTO_DRIVER
2 #define _H_MXHW_CRYPTO_DRIVER
4 #include "mxhw_crypto_io.h"
7 We have some terms defined to make the code readable
9 user process: the process that makes cipher requests in the user space
10 io caller: it represents a user process in the kernel space
11 dispatcher: a daemon thread that repeatedly pulls out requests from a queue
12 exclusively appended to the dispatcher and pass these requests to
13 the underlying engine one-by-one
15 #define CRYPTO_MAJOR 11 /* major number of the driver */
16 #define CRYPTO_MINOR 131 /* minor number of the driver */
17 #define CRYPTO_DEVNAME "mxcrypto"
19 typedef struct _QMBPTR QMBPTR
;
21 /* an io caller at the kernel space */
22 typedef struct _IOCTRL
24 u32 cpid
; /* [index][pid] the index to the array of callers + the pid of the caller */
25 u32 cfid
; /* an id simplifies the cipher information */
26 CIPHER info
; /* the payload information of a cipher request */
27 QMBPTR
*outq
; /* a list of processed packets to be uploaded to the user process */
28 u32 pkt_num
; /* totoal processed packets */
31 /* a wrapper of data to be processed */
32 typedef struct _IOMBUF
34 u8
*virt
; /* pointer to an allocated space where data would be stored */
35 u32 phys
; /* physical address the engine would access */
36 u32 size
; /* the size of allocated space */
37 u32 dlen
; /* the length of data to be encrypted/decrypted */
38 u32 offs
; /* offset the actual data being stored, within the offset,
39 some info might be utilized */
42 /* a request unit (context buffer) the dispatcher handles */
43 typedef struct _QMBCTX
45 u32 status
; /* the status the dispatch reports */
46 IOCTRL ictx
; /* a new copy of the cipher control upon any request. in case,
47 any change is made for the next request */
48 IOMBUF imbuf
; /* a wrapper of input buffer */
49 IOMBUF ombuf
; /* a wrapper of output buffer */
50 struct _QMBCTX
*next
; /* link to the next request to be handled */
53 /* an aggregate data structure maintaining a queue of context buffers with
54 a spin lock and a waiting queue */
57 QMBCTX
*head
; /* where to dequeue */
58 QMBCTX
*tail
; /* where to enqueue */
59 spinlock_t lock
; /* lock upon any operation on this queue */
60 wait_queue_head_t quew
; /* apply sleeping before dequeue and waking up after enqueue */
63 #define IOMBUF_OFFS(m) m->offs
64 #define IOMBUF_VIRT(m) m->virt
65 #define IOMBUF_DATA(m) (m->virt+m->offs)
66 #define IOMBUF_SIZE(m) m->size
67 #define IOMBUF_PHYS(m) m->phys
68 #define IOMBUF_DLEN(m) m->dlen
70 #define QUEUE_LENGTH(s,p,n) \
74 spin_lock(&p->lock); \
76 while (i!=NULL) { i=i->next; n++; } \
77 spin_unlock(&p->lock); \
80 #define WAKE_UP_QUEUE(p) wake_up_interruptible(&p->quew)
82 #define ENQUEUE_CONTEXT(p,i) \
86 spin_lock(&p->lock); \
92 while (p->tail->next!=NULL) p->tail=p->tail->next; \
93 spin_unlock(&p->lock); \
98 #define DEQUEUE_CONTEXT(q,i,j,c) \
101 if (j==0 && wait_event_interruptible(q->quew, q->head!=NULL||(c)) !=0) \
103 spin_lock(&q->lock); \
104 if ((i=q->head)!=NULL) \
108 q->head=q->head->next; \
111 spin_unlock(&q->lock); \
114 /* all global variables */
115 typedef struct _global_p
118 int dspt_thrd
; /* thread id of the dispatcher */
119 u32 dspt_exit
; /* force the thread to die */
121 QMBPTR
*free_ctxq
; /* 1. a list of free contexts that io owners compete with.
122 2. a waiting queue embedded with io owners that are ready
123 to be awaken up by any other owner when it releases a
124 processed packet and by the dispatcher when a packet
126 QMBPTR
*dspt_ctxq
; /* 1. a list of io packets that the dispatcher would handle.
127 2. a waiting queue embedded with io owners that are ready
128 to be awaken up by the dispatcher when a packet is
129 completed with encryption.
130 3. a waiting queue embedded with the dispatcher only that
131 is ready to be awaken up by any of the io owners when
133 IOCTRL pool
[MAX_CIPHER_CLIENTS
]; /* buffers for the callers */
136 extern global_p global
;
138 /* macros defining operations on a list of all current io callers,
139 share with a spin lock, the function that these macros reference to
140 makes sure that a processed packet associated with a caller can be
141 returned back to the free pool if the caller was gone already. */
142 #define IOCTRL_OP_CHKIN 1
143 #define IOCTRL_OP_CLOSE 2
144 #define IOCTRL_OP_ALIVE 3
145 #define IOCTRL_OP_FREEQ 4
146 #define mxhw_crypto_iocall_chkin(i) mxhw_crypto_iocall_op(i,IOCTRL_OP_CHKIN)
147 #define mxhw_crypto_iocall_close(i) mxhw_crypto_iocall_op(i,IOCTRL_OP_CLOSE)
148 #define mxhw_crypto_iocall_alive(i) mxhw_crypto_iocall_op(i,IOCTRL_OP_ALIVE)
149 #define mxhw_crypto_iocall_freeq(i) mxhw_crypto_iocall_op(i,IOCTRL_OP_FREEQ)
151 IOCTRL
* mxhw_crypto_iocall_op(u32 cpid
, int type
);
153 /* this function takes the cipher info (type, algo, mode) as the input, tells the caller the key length,
154 the block length, and outputs a control code (could be an id)
155 return 0 on success, otherwise on failure
157 int mxhw_crypto_engine_register(CIPHER
*info
, u32
*ctrl
);
159 /* the inverse operation of the above function, take the ctrol code as the input if you want to close it */
160 void mxhw_crypto_engine_unregister(u32 ctrl
);
162 /* this function may have a loop, please check the exited code in the loop, if the code is up, then break
163 the loop and return an positive integer
164 return 0 on success, otherwise on failure
166 int mxhw_crypto_engine_perform(u32 ctrl
, CIPHER
*info
, IOMBUF
*imbuf
, IOMBUF
*ombuf
, u32
*exited
);
168 /* any stuff you need to start an engine
169 return 0 on success, otherwise on failure
171 int mxhw_crypto_engine_up(void);
173 /* any stuff you need to cleanup for the engine */
174 void mxhw_crypto_engine_down(void);
176 /* this function takes a body of IOMBUF as input and allocates memory (virtual and physical) with the size
177 set offset if necessary.
178 return 0 on success, otherwise on failure
180 int mxhw_crypto_engine_dma_malloc(IOMBUF
*mbuf
, u32 size
);
181 void mxhw_crypto_engine_dma_mfree(IOMBUF
*mbuf
);
184 void hexit(char *msg
, u_char
*d
, int len
);