1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /**
3*4882a593Smuzhiyun * Routines supporting the Power 7+ Nest Accelerators driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2011-2012 International Business Machines Inc.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Author: Kent Yoder <yoder1@us.ibm.com>
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <crypto/internal/aead.h>
11*4882a593Smuzhiyun #include <crypto/internal/hash.h>
12*4882a593Smuzhiyun #include <crypto/aes.h>
13*4882a593Smuzhiyun #include <crypto/sha.h>
14*4882a593Smuzhiyun #include <crypto/algapi.h>
15*4882a593Smuzhiyun #include <crypto/scatterwalk.h>
16*4882a593Smuzhiyun #include <linux/module.h>
17*4882a593Smuzhiyun #include <linux/moduleparam.h>
18*4882a593Smuzhiyun #include <linux/types.h>
19*4882a593Smuzhiyun #include <linux/mm.h>
20*4882a593Smuzhiyun #include <linux/scatterlist.h>
21*4882a593Smuzhiyun #include <linux/device.h>
22*4882a593Smuzhiyun #include <linux/of.h>
23*4882a593Smuzhiyun #include <asm/hvcall.h>
24*4882a593Smuzhiyun #include <asm/vio.h>
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #include "nx_csbcpb.h"
27*4882a593Smuzhiyun #include "nx.h"
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun /**
31*4882a593Smuzhiyun * nx_hcall_sync - make an H_COP_OP hcall for the passed in op structure
32*4882a593Smuzhiyun *
33*4882a593Smuzhiyun * @nx_ctx: the crypto context handle
34*4882a593Smuzhiyun * @op: PFO operation struct to pass in
35*4882a593Smuzhiyun * @may_sleep: flag indicating the request can sleep
36*4882a593Smuzhiyun *
37*4882a593Smuzhiyun * Make the hcall, retrying while the hardware is busy. If we cannot yield
38*4882a593Smuzhiyun * the thread, limit the number of retries to 10 here.
39*4882a593Smuzhiyun */
nx_hcall_sync(struct nx_crypto_ctx * nx_ctx,struct vio_pfo_op * op,u32 may_sleep)40*4882a593Smuzhiyun int nx_hcall_sync(struct nx_crypto_ctx *nx_ctx,
41*4882a593Smuzhiyun struct vio_pfo_op *op,
42*4882a593Smuzhiyun u32 may_sleep)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun int rc, retries = 10;
45*4882a593Smuzhiyun struct vio_dev *viodev = nx_driver.viodev;
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun atomic_inc(&(nx_ctx->stats->sync_ops));
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun do {
50*4882a593Smuzhiyun rc = vio_h_cop_sync(viodev, op);
51*4882a593Smuzhiyun } while (rc == -EBUSY && !may_sleep && retries--);
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun if (rc) {
54*4882a593Smuzhiyun dev_dbg(&viodev->dev, "vio_h_cop_sync failed: rc: %d "
55*4882a593Smuzhiyun "hcall rc: %ld\n", rc, op->hcall_err);
56*4882a593Smuzhiyun atomic_inc(&(nx_ctx->stats->errors));
57*4882a593Smuzhiyun atomic_set(&(nx_ctx->stats->last_error), op->hcall_err);
58*4882a593Smuzhiyun atomic_set(&(nx_ctx->stats->last_error_pid), current->pid);
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun return rc;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun /**
65*4882a593Smuzhiyun * nx_build_sg_list - build an NX scatter list describing a single buffer
66*4882a593Smuzhiyun *
67*4882a593Smuzhiyun * @sg_head: pointer to the first scatter list element to build
68*4882a593Smuzhiyun * @start_addr: pointer to the linear buffer
69*4882a593Smuzhiyun * @len: length of the data at @start_addr
70*4882a593Smuzhiyun * @sgmax: the largest number of scatter list elements we're allowed to create
71*4882a593Smuzhiyun *
72*4882a593Smuzhiyun * This function will start writing nx_sg elements at @sg_head and keep
73*4882a593Smuzhiyun * writing them until all of the data from @start_addr is described or
74*4882a593Smuzhiyun * until sgmax elements have been written. Scatter list elements will be
75*4882a593Smuzhiyun * created such that none of the elements describes a buffer that crosses a 4K
76*4882a593Smuzhiyun * boundary.
77*4882a593Smuzhiyun */
nx_build_sg_list(struct nx_sg * sg_head,u8 * start_addr,unsigned int * len,u32 sgmax)78*4882a593Smuzhiyun struct nx_sg *nx_build_sg_list(struct nx_sg *sg_head,
79*4882a593Smuzhiyun u8 *start_addr,
80*4882a593Smuzhiyun unsigned int *len,
81*4882a593Smuzhiyun u32 sgmax)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun unsigned int sg_len = 0;
84*4882a593Smuzhiyun struct nx_sg *sg;
85*4882a593Smuzhiyun u64 sg_addr = (u64)start_addr;
86*4882a593Smuzhiyun u64 end_addr;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun /* determine the start and end for this address range - slightly
89*4882a593Smuzhiyun * different if this is in VMALLOC_REGION */
90*4882a593Smuzhiyun if (is_vmalloc_addr(start_addr))
91*4882a593Smuzhiyun sg_addr = page_to_phys(vmalloc_to_page(start_addr))
92*4882a593Smuzhiyun + offset_in_page(sg_addr);
93*4882a593Smuzhiyun else
94*4882a593Smuzhiyun sg_addr = __pa(sg_addr);
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun end_addr = sg_addr + *len;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /* each iteration will write one struct nx_sg element and add the
99*4882a593Smuzhiyun * length of data described by that element to sg_len. Once @len bytes
100*4882a593Smuzhiyun * have been described (or @sgmax elements have been written), the
101*4882a593Smuzhiyun * loop ends. min_t is used to ensure @end_addr falls on the same page
102*4882a593Smuzhiyun * as sg_addr, if not, we need to create another nx_sg element for the
103*4882a593Smuzhiyun * data on the next page.
104*4882a593Smuzhiyun *
105*4882a593Smuzhiyun * Also when using vmalloc'ed data, every time that a system page
106*4882a593Smuzhiyun * boundary is crossed the physical address needs to be re-calculated.
107*4882a593Smuzhiyun */
108*4882a593Smuzhiyun for (sg = sg_head; sg_len < *len; sg++) {
109*4882a593Smuzhiyun u64 next_page;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun sg->addr = sg_addr;
112*4882a593Smuzhiyun sg_addr = min_t(u64, NX_PAGE_NUM(sg_addr + NX_PAGE_SIZE),
113*4882a593Smuzhiyun end_addr);
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun next_page = (sg->addr & PAGE_MASK) + PAGE_SIZE;
116*4882a593Smuzhiyun sg->len = min_t(u64, sg_addr, next_page) - sg->addr;
117*4882a593Smuzhiyun sg_len += sg->len;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun if (sg_addr >= next_page &&
120*4882a593Smuzhiyun is_vmalloc_addr(start_addr + sg_len)) {
121*4882a593Smuzhiyun sg_addr = page_to_phys(vmalloc_to_page(
122*4882a593Smuzhiyun start_addr + sg_len));
123*4882a593Smuzhiyun end_addr = sg_addr + *len - sg_len;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun if ((sg - sg_head) == sgmax) {
127*4882a593Smuzhiyun pr_err("nx: scatter/gather list overflow, pid: %d\n",
128*4882a593Smuzhiyun current->pid);
129*4882a593Smuzhiyun sg++;
130*4882a593Smuzhiyun break;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun *len = sg_len;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun /* return the moved sg_head pointer */
136*4882a593Smuzhiyun return sg;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun /**
140*4882a593Smuzhiyun * nx_walk_and_build - walk a linux scatterlist and build an nx scatterlist
141*4882a593Smuzhiyun *
142*4882a593Smuzhiyun * @nx_dst: pointer to the first nx_sg element to write
143*4882a593Smuzhiyun * @sglen: max number of nx_sg entries we're allowed to write
144*4882a593Smuzhiyun * @sg_src: pointer to the source linux scatterlist to walk
145*4882a593Smuzhiyun * @start: number of bytes to fast-forward past at the beginning of @sg_src
146*4882a593Smuzhiyun * @src_len: number of bytes to walk in @sg_src
147*4882a593Smuzhiyun */
nx_walk_and_build(struct nx_sg * nx_dst,unsigned int sglen,struct scatterlist * sg_src,unsigned int start,unsigned int * src_len)148*4882a593Smuzhiyun struct nx_sg *nx_walk_and_build(struct nx_sg *nx_dst,
149*4882a593Smuzhiyun unsigned int sglen,
150*4882a593Smuzhiyun struct scatterlist *sg_src,
151*4882a593Smuzhiyun unsigned int start,
152*4882a593Smuzhiyun unsigned int *src_len)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun struct scatter_walk walk;
155*4882a593Smuzhiyun struct nx_sg *nx_sg = nx_dst;
156*4882a593Smuzhiyun unsigned int n, offset = 0, len = *src_len;
157*4882a593Smuzhiyun char *dst;
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun /* we need to fast forward through @start bytes first */
160*4882a593Smuzhiyun for (;;) {
161*4882a593Smuzhiyun scatterwalk_start(&walk, sg_src);
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun if (start < offset + sg_src->length)
164*4882a593Smuzhiyun break;
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun offset += sg_src->length;
167*4882a593Smuzhiyun sg_src = sg_next(sg_src);
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun /* start - offset is the number of bytes to advance in the scatterlist
171*4882a593Smuzhiyun * element we're currently looking at */
172*4882a593Smuzhiyun scatterwalk_advance(&walk, start - offset);
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun while (len && (nx_sg - nx_dst) < sglen) {
175*4882a593Smuzhiyun n = scatterwalk_clamp(&walk, len);
176*4882a593Smuzhiyun if (!n) {
177*4882a593Smuzhiyun /* In cases where we have scatterlist chain sg_next
178*4882a593Smuzhiyun * handles with it properly */
179*4882a593Smuzhiyun scatterwalk_start(&walk, sg_next(walk.sg));
180*4882a593Smuzhiyun n = scatterwalk_clamp(&walk, len);
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun dst = scatterwalk_map(&walk);
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun nx_sg = nx_build_sg_list(nx_sg, dst, &n, sglen - (nx_sg - nx_dst));
185*4882a593Smuzhiyun len -= n;
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun scatterwalk_unmap(dst);
188*4882a593Smuzhiyun scatterwalk_advance(&walk, n);
189*4882a593Smuzhiyun scatterwalk_done(&walk, SCATTERWALK_FROM_SG, len);
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun /* update to_process */
192*4882a593Smuzhiyun *src_len -= len;
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun /* return the moved destination pointer */
195*4882a593Smuzhiyun return nx_sg;
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun /**
199*4882a593Smuzhiyun * trim_sg_list - ensures the bound in sg list.
200*4882a593Smuzhiyun * @sg: sg list head
201*4882a593Smuzhiyun * @end: sg lisg end
202*4882a593Smuzhiyun * @delta: is the amount we need to crop in order to bound the list.
203*4882a593Smuzhiyun *
204*4882a593Smuzhiyun */
trim_sg_list(struct nx_sg * sg,struct nx_sg * end,unsigned int delta,unsigned int * nbytes)205*4882a593Smuzhiyun static long int trim_sg_list(struct nx_sg *sg,
206*4882a593Smuzhiyun struct nx_sg *end,
207*4882a593Smuzhiyun unsigned int delta,
208*4882a593Smuzhiyun unsigned int *nbytes)
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun long int oplen;
211*4882a593Smuzhiyun long int data_back;
212*4882a593Smuzhiyun unsigned int is_delta = delta;
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun while (delta && end > sg) {
215*4882a593Smuzhiyun struct nx_sg *last = end - 1;
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun if (last->len > delta) {
218*4882a593Smuzhiyun last->len -= delta;
219*4882a593Smuzhiyun delta = 0;
220*4882a593Smuzhiyun } else {
221*4882a593Smuzhiyun end--;
222*4882a593Smuzhiyun delta -= last->len;
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun /* There are cases where we need to crop list in order to make it
227*4882a593Smuzhiyun * a block size multiple, but we also need to align data. In order to
228*4882a593Smuzhiyun * that we need to calculate how much we need to put back to be
229*4882a593Smuzhiyun * processed
230*4882a593Smuzhiyun */
231*4882a593Smuzhiyun oplen = (sg - end) * sizeof(struct nx_sg);
232*4882a593Smuzhiyun if (is_delta) {
233*4882a593Smuzhiyun data_back = (abs(oplen) / AES_BLOCK_SIZE) * sg->len;
234*4882a593Smuzhiyun data_back = *nbytes - (data_back & ~(AES_BLOCK_SIZE - 1));
235*4882a593Smuzhiyun *nbytes -= data_back;
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun return oplen;
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun /**
242*4882a593Smuzhiyun * nx_build_sg_lists - walk the input scatterlists and build arrays of NX
243*4882a593Smuzhiyun * scatterlists based on them.
244*4882a593Smuzhiyun *
245*4882a593Smuzhiyun * @nx_ctx: NX crypto context for the lists we're building
246*4882a593Smuzhiyun * @iv: iv data, if the algorithm requires it
247*4882a593Smuzhiyun * @dst: destination scatterlist
248*4882a593Smuzhiyun * @src: source scatterlist
249*4882a593Smuzhiyun * @nbytes: length of data described in the scatterlists
250*4882a593Smuzhiyun * @offset: number of bytes to fast-forward past at the beginning of
251*4882a593Smuzhiyun * scatterlists.
252*4882a593Smuzhiyun * @oiv: destination for the iv data, if the algorithm requires it
253*4882a593Smuzhiyun *
254*4882a593Smuzhiyun * This is common code shared by all the AES algorithms. It uses the crypto
255*4882a593Smuzhiyun * scatterlist walk routines to traverse input and output scatterlists, building
256*4882a593Smuzhiyun * corresponding NX scatterlists
257*4882a593Smuzhiyun */
nx_build_sg_lists(struct nx_crypto_ctx * nx_ctx,const u8 * iv,struct scatterlist * dst,struct scatterlist * src,unsigned int * nbytes,unsigned int offset,u8 * oiv)258*4882a593Smuzhiyun int nx_build_sg_lists(struct nx_crypto_ctx *nx_ctx,
259*4882a593Smuzhiyun const u8 *iv,
260*4882a593Smuzhiyun struct scatterlist *dst,
261*4882a593Smuzhiyun struct scatterlist *src,
262*4882a593Smuzhiyun unsigned int *nbytes,
263*4882a593Smuzhiyun unsigned int offset,
264*4882a593Smuzhiyun u8 *oiv)
265*4882a593Smuzhiyun {
266*4882a593Smuzhiyun unsigned int delta = 0;
267*4882a593Smuzhiyun unsigned int total = *nbytes;
268*4882a593Smuzhiyun struct nx_sg *nx_insg = nx_ctx->in_sg;
269*4882a593Smuzhiyun struct nx_sg *nx_outsg = nx_ctx->out_sg;
270*4882a593Smuzhiyun unsigned int max_sg_len;
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun max_sg_len = min_t(u64, nx_ctx->ap->sglen,
273*4882a593Smuzhiyun nx_driver.of.max_sg_len/sizeof(struct nx_sg));
274*4882a593Smuzhiyun max_sg_len = min_t(u64, max_sg_len,
275*4882a593Smuzhiyun nx_ctx->ap->databytelen/NX_PAGE_SIZE);
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun if (oiv)
278*4882a593Smuzhiyun memcpy(oiv, iv, AES_BLOCK_SIZE);
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun *nbytes = min_t(u64, *nbytes, nx_ctx->ap->databytelen);
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun nx_outsg = nx_walk_and_build(nx_outsg, max_sg_len, dst,
283*4882a593Smuzhiyun offset, nbytes);
284*4882a593Smuzhiyun nx_insg = nx_walk_and_build(nx_insg, max_sg_len, src,
285*4882a593Smuzhiyun offset, nbytes);
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun if (*nbytes < total)
288*4882a593Smuzhiyun delta = *nbytes - (*nbytes & ~(AES_BLOCK_SIZE - 1));
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun /* these lengths should be negative, which will indicate to phyp that
291*4882a593Smuzhiyun * the input and output parameters are scatterlists, not linear
292*4882a593Smuzhiyun * buffers */
293*4882a593Smuzhiyun nx_ctx->op.inlen = trim_sg_list(nx_ctx->in_sg, nx_insg, delta, nbytes);
294*4882a593Smuzhiyun nx_ctx->op.outlen = trim_sg_list(nx_ctx->out_sg, nx_outsg, delta, nbytes);
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun return 0;
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun /**
300*4882a593Smuzhiyun * nx_ctx_init - initialize an nx_ctx's vio_pfo_op struct
301*4882a593Smuzhiyun *
302*4882a593Smuzhiyun * @nx_ctx: the nx context to initialize
303*4882a593Smuzhiyun * @function: the function code for the op
304*4882a593Smuzhiyun */
nx_ctx_init(struct nx_crypto_ctx * nx_ctx,unsigned int function)305*4882a593Smuzhiyun void nx_ctx_init(struct nx_crypto_ctx *nx_ctx, unsigned int function)
306*4882a593Smuzhiyun {
307*4882a593Smuzhiyun spin_lock_init(&nx_ctx->lock);
308*4882a593Smuzhiyun memset(nx_ctx->kmem, 0, nx_ctx->kmem_len);
309*4882a593Smuzhiyun nx_ctx->csbcpb->csb.valid |= NX_CSB_VALID_BIT;
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun nx_ctx->op.flags = function;
312*4882a593Smuzhiyun nx_ctx->op.csbcpb = __pa(nx_ctx->csbcpb);
313*4882a593Smuzhiyun nx_ctx->op.in = __pa(nx_ctx->in_sg);
314*4882a593Smuzhiyun nx_ctx->op.out = __pa(nx_ctx->out_sg);
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun if (nx_ctx->csbcpb_aead) {
317*4882a593Smuzhiyun nx_ctx->csbcpb_aead->csb.valid |= NX_CSB_VALID_BIT;
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun nx_ctx->op_aead.flags = function;
320*4882a593Smuzhiyun nx_ctx->op_aead.csbcpb = __pa(nx_ctx->csbcpb_aead);
321*4882a593Smuzhiyun nx_ctx->op_aead.in = __pa(nx_ctx->in_sg);
322*4882a593Smuzhiyun nx_ctx->op_aead.out = __pa(nx_ctx->out_sg);
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun }
325*4882a593Smuzhiyun
nx_of_update_status(struct device * dev,struct property * p,struct nx_of * props)326*4882a593Smuzhiyun static void nx_of_update_status(struct device *dev,
327*4882a593Smuzhiyun struct property *p,
328*4882a593Smuzhiyun struct nx_of *props)
329*4882a593Smuzhiyun {
330*4882a593Smuzhiyun if (!strncmp(p->value, "okay", p->length)) {
331*4882a593Smuzhiyun props->status = NX_WAITING;
332*4882a593Smuzhiyun props->flags |= NX_OF_FLAG_STATUS_SET;
333*4882a593Smuzhiyun } else {
334*4882a593Smuzhiyun dev_info(dev, "%s: status '%s' is not 'okay'\n", __func__,
335*4882a593Smuzhiyun (char *)p->value);
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun }
338*4882a593Smuzhiyun
nx_of_update_sglen(struct device * dev,struct property * p,struct nx_of * props)339*4882a593Smuzhiyun static void nx_of_update_sglen(struct device *dev,
340*4882a593Smuzhiyun struct property *p,
341*4882a593Smuzhiyun struct nx_of *props)
342*4882a593Smuzhiyun {
343*4882a593Smuzhiyun if (p->length != sizeof(props->max_sg_len)) {
344*4882a593Smuzhiyun dev_err(dev, "%s: unexpected format for "
345*4882a593Smuzhiyun "ibm,max-sg-len property\n", __func__);
346*4882a593Smuzhiyun dev_dbg(dev, "%s: ibm,max-sg-len is %d bytes "
347*4882a593Smuzhiyun "long, expected %zd bytes\n", __func__,
348*4882a593Smuzhiyun p->length, sizeof(props->max_sg_len));
349*4882a593Smuzhiyun return;
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun props->max_sg_len = *(u32 *)p->value;
353*4882a593Smuzhiyun props->flags |= NX_OF_FLAG_MAXSGLEN_SET;
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun
nx_of_update_msc(struct device * dev,struct property * p,struct nx_of * props)356*4882a593Smuzhiyun static void nx_of_update_msc(struct device *dev,
357*4882a593Smuzhiyun struct property *p,
358*4882a593Smuzhiyun struct nx_of *props)
359*4882a593Smuzhiyun {
360*4882a593Smuzhiyun struct msc_triplet *trip;
361*4882a593Smuzhiyun struct max_sync_cop *msc;
362*4882a593Smuzhiyun unsigned int bytes_so_far, i, lenp;
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun msc = (struct max_sync_cop *)p->value;
365*4882a593Smuzhiyun lenp = p->length;
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun /* You can't tell if the data read in for this property is sane by its
368*4882a593Smuzhiyun * size alone. This is because there are sizes embedded in the data
369*4882a593Smuzhiyun * structure. The best we can do is check lengths as we parse and bail
370*4882a593Smuzhiyun * as soon as a length error is detected. */
371*4882a593Smuzhiyun bytes_so_far = 0;
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun while ((bytes_so_far + sizeof(struct max_sync_cop)) <= lenp) {
374*4882a593Smuzhiyun bytes_so_far += sizeof(struct max_sync_cop);
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun trip = msc->trip;
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun for (i = 0;
379*4882a593Smuzhiyun ((bytes_so_far + sizeof(struct msc_triplet)) <= lenp) &&
380*4882a593Smuzhiyun i < msc->triplets;
381*4882a593Smuzhiyun i++) {
382*4882a593Smuzhiyun if (msc->fc >= NX_MAX_FC || msc->mode >= NX_MAX_MODE) {
383*4882a593Smuzhiyun dev_err(dev, "unknown function code/mode "
384*4882a593Smuzhiyun "combo: %d/%d (ignored)\n", msc->fc,
385*4882a593Smuzhiyun msc->mode);
386*4882a593Smuzhiyun goto next_loop;
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun if (!trip->sglen || trip->databytelen < NX_PAGE_SIZE) {
390*4882a593Smuzhiyun dev_warn(dev, "bogus sglen/databytelen: "
391*4882a593Smuzhiyun "%u/%u (ignored)\n", trip->sglen,
392*4882a593Smuzhiyun trip->databytelen);
393*4882a593Smuzhiyun goto next_loop;
394*4882a593Smuzhiyun }
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun switch (trip->keybitlen) {
397*4882a593Smuzhiyun case 128:
398*4882a593Smuzhiyun case 160:
399*4882a593Smuzhiyun props->ap[msc->fc][msc->mode][0].databytelen =
400*4882a593Smuzhiyun trip->databytelen;
401*4882a593Smuzhiyun props->ap[msc->fc][msc->mode][0].sglen =
402*4882a593Smuzhiyun trip->sglen;
403*4882a593Smuzhiyun break;
404*4882a593Smuzhiyun case 192:
405*4882a593Smuzhiyun props->ap[msc->fc][msc->mode][1].databytelen =
406*4882a593Smuzhiyun trip->databytelen;
407*4882a593Smuzhiyun props->ap[msc->fc][msc->mode][1].sglen =
408*4882a593Smuzhiyun trip->sglen;
409*4882a593Smuzhiyun break;
410*4882a593Smuzhiyun case 256:
411*4882a593Smuzhiyun if (msc->fc == NX_FC_AES) {
412*4882a593Smuzhiyun props->ap[msc->fc][msc->mode][2].
413*4882a593Smuzhiyun databytelen = trip->databytelen;
414*4882a593Smuzhiyun props->ap[msc->fc][msc->mode][2].sglen =
415*4882a593Smuzhiyun trip->sglen;
416*4882a593Smuzhiyun } else if (msc->fc == NX_FC_AES_HMAC ||
417*4882a593Smuzhiyun msc->fc == NX_FC_SHA) {
418*4882a593Smuzhiyun props->ap[msc->fc][msc->mode][1].
419*4882a593Smuzhiyun databytelen = trip->databytelen;
420*4882a593Smuzhiyun props->ap[msc->fc][msc->mode][1].sglen =
421*4882a593Smuzhiyun trip->sglen;
422*4882a593Smuzhiyun } else {
423*4882a593Smuzhiyun dev_warn(dev, "unknown function "
424*4882a593Smuzhiyun "code/key bit len combo"
425*4882a593Smuzhiyun ": (%u/256)\n", msc->fc);
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun break;
428*4882a593Smuzhiyun case 512:
429*4882a593Smuzhiyun props->ap[msc->fc][msc->mode][2].databytelen =
430*4882a593Smuzhiyun trip->databytelen;
431*4882a593Smuzhiyun props->ap[msc->fc][msc->mode][2].sglen =
432*4882a593Smuzhiyun trip->sglen;
433*4882a593Smuzhiyun break;
434*4882a593Smuzhiyun default:
435*4882a593Smuzhiyun dev_warn(dev, "unknown function code/key bit "
436*4882a593Smuzhiyun "len combo: (%u/%u)\n", msc->fc,
437*4882a593Smuzhiyun trip->keybitlen);
438*4882a593Smuzhiyun break;
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun next_loop:
441*4882a593Smuzhiyun bytes_so_far += sizeof(struct msc_triplet);
442*4882a593Smuzhiyun trip++;
443*4882a593Smuzhiyun }
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun msc = (struct max_sync_cop *)trip;
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun props->flags |= NX_OF_FLAG_MAXSYNCCOP_SET;
449*4882a593Smuzhiyun }
450*4882a593Smuzhiyun
451*4882a593Smuzhiyun /**
452*4882a593Smuzhiyun * nx_of_init - read openFirmware values from the device tree
453*4882a593Smuzhiyun *
454*4882a593Smuzhiyun * @dev: device handle
455*4882a593Smuzhiyun * @props: pointer to struct to hold the properties values
456*4882a593Smuzhiyun *
457*4882a593Smuzhiyun * Called once at driver probe time, this function will read out the
458*4882a593Smuzhiyun * openFirmware properties we use at runtime. If all the OF properties are
459*4882a593Smuzhiyun * acceptable, when we exit this function props->flags will indicate that
460*4882a593Smuzhiyun * we're ready to register our crypto algorithms.
461*4882a593Smuzhiyun */
nx_of_init(struct device * dev,struct nx_of * props)462*4882a593Smuzhiyun static void nx_of_init(struct device *dev, struct nx_of *props)
463*4882a593Smuzhiyun {
464*4882a593Smuzhiyun struct device_node *base_node = dev->of_node;
465*4882a593Smuzhiyun struct property *p;
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun p = of_find_property(base_node, "status", NULL);
468*4882a593Smuzhiyun if (!p)
469*4882a593Smuzhiyun dev_info(dev, "%s: property 'status' not found\n", __func__);
470*4882a593Smuzhiyun else
471*4882a593Smuzhiyun nx_of_update_status(dev, p, props);
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun p = of_find_property(base_node, "ibm,max-sg-len", NULL);
474*4882a593Smuzhiyun if (!p)
475*4882a593Smuzhiyun dev_info(dev, "%s: property 'ibm,max-sg-len' not found\n",
476*4882a593Smuzhiyun __func__);
477*4882a593Smuzhiyun else
478*4882a593Smuzhiyun nx_of_update_sglen(dev, p, props);
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun p = of_find_property(base_node, "ibm,max-sync-cop", NULL);
481*4882a593Smuzhiyun if (!p)
482*4882a593Smuzhiyun dev_info(dev, "%s: property 'ibm,max-sync-cop' not found\n",
483*4882a593Smuzhiyun __func__);
484*4882a593Smuzhiyun else
485*4882a593Smuzhiyun nx_of_update_msc(dev, p, props);
486*4882a593Smuzhiyun }
487*4882a593Smuzhiyun
nx_check_prop(struct device * dev,u32 fc,u32 mode,int slot)488*4882a593Smuzhiyun static bool nx_check_prop(struct device *dev, u32 fc, u32 mode, int slot)
489*4882a593Smuzhiyun {
490*4882a593Smuzhiyun struct alg_props *props = &nx_driver.of.ap[fc][mode][slot];
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun if (!props->sglen || props->databytelen < NX_PAGE_SIZE) {
493*4882a593Smuzhiyun if (dev)
494*4882a593Smuzhiyun dev_warn(dev, "bogus sglen/databytelen for %u/%u/%u: "
495*4882a593Smuzhiyun "%u/%u (ignored)\n", fc, mode, slot,
496*4882a593Smuzhiyun props->sglen, props->databytelen);
497*4882a593Smuzhiyun return false;
498*4882a593Smuzhiyun }
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun return true;
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun
nx_check_props(struct device * dev,u32 fc,u32 mode)503*4882a593Smuzhiyun static bool nx_check_props(struct device *dev, u32 fc, u32 mode)
504*4882a593Smuzhiyun {
505*4882a593Smuzhiyun int i;
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun for (i = 0; i < 3; i++)
508*4882a593Smuzhiyun if (!nx_check_prop(dev, fc, mode, i))
509*4882a593Smuzhiyun return false;
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun return true;
512*4882a593Smuzhiyun }
513*4882a593Smuzhiyun
nx_register_skcipher(struct skcipher_alg * alg,u32 fc,u32 mode)514*4882a593Smuzhiyun static int nx_register_skcipher(struct skcipher_alg *alg, u32 fc, u32 mode)
515*4882a593Smuzhiyun {
516*4882a593Smuzhiyun return nx_check_props(&nx_driver.viodev->dev, fc, mode) ?
517*4882a593Smuzhiyun crypto_register_skcipher(alg) : 0;
518*4882a593Smuzhiyun }
519*4882a593Smuzhiyun
nx_register_aead(struct aead_alg * alg,u32 fc,u32 mode)520*4882a593Smuzhiyun static int nx_register_aead(struct aead_alg *alg, u32 fc, u32 mode)
521*4882a593Smuzhiyun {
522*4882a593Smuzhiyun return nx_check_props(&nx_driver.viodev->dev, fc, mode) ?
523*4882a593Smuzhiyun crypto_register_aead(alg) : 0;
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun
nx_register_shash(struct shash_alg * alg,u32 fc,u32 mode,int slot)526*4882a593Smuzhiyun static int nx_register_shash(struct shash_alg *alg, u32 fc, u32 mode, int slot)
527*4882a593Smuzhiyun {
528*4882a593Smuzhiyun return (slot >= 0 ? nx_check_prop(&nx_driver.viodev->dev,
529*4882a593Smuzhiyun fc, mode, slot) :
530*4882a593Smuzhiyun nx_check_props(&nx_driver.viodev->dev, fc, mode)) ?
531*4882a593Smuzhiyun crypto_register_shash(alg) : 0;
532*4882a593Smuzhiyun }
533*4882a593Smuzhiyun
nx_unregister_skcipher(struct skcipher_alg * alg,u32 fc,u32 mode)534*4882a593Smuzhiyun static void nx_unregister_skcipher(struct skcipher_alg *alg, u32 fc, u32 mode)
535*4882a593Smuzhiyun {
536*4882a593Smuzhiyun if (nx_check_props(NULL, fc, mode))
537*4882a593Smuzhiyun crypto_unregister_skcipher(alg);
538*4882a593Smuzhiyun }
539*4882a593Smuzhiyun
nx_unregister_aead(struct aead_alg * alg,u32 fc,u32 mode)540*4882a593Smuzhiyun static void nx_unregister_aead(struct aead_alg *alg, u32 fc, u32 mode)
541*4882a593Smuzhiyun {
542*4882a593Smuzhiyun if (nx_check_props(NULL, fc, mode))
543*4882a593Smuzhiyun crypto_unregister_aead(alg);
544*4882a593Smuzhiyun }
545*4882a593Smuzhiyun
nx_unregister_shash(struct shash_alg * alg,u32 fc,u32 mode,int slot)546*4882a593Smuzhiyun static void nx_unregister_shash(struct shash_alg *alg, u32 fc, u32 mode,
547*4882a593Smuzhiyun int slot)
548*4882a593Smuzhiyun {
549*4882a593Smuzhiyun if (slot >= 0 ? nx_check_prop(NULL, fc, mode, slot) :
550*4882a593Smuzhiyun nx_check_props(NULL, fc, mode))
551*4882a593Smuzhiyun crypto_unregister_shash(alg);
552*4882a593Smuzhiyun }
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun /**
555*4882a593Smuzhiyun * nx_register_algs - register algorithms with the crypto API
556*4882a593Smuzhiyun *
557*4882a593Smuzhiyun * Called from nx_probe()
558*4882a593Smuzhiyun *
559*4882a593Smuzhiyun * If all OF properties are in an acceptable state, the driver flags will
560*4882a593Smuzhiyun * indicate that we're ready and we'll create our debugfs files and register
561*4882a593Smuzhiyun * out crypto algorithms.
562*4882a593Smuzhiyun */
nx_register_algs(void)563*4882a593Smuzhiyun static int nx_register_algs(void)
564*4882a593Smuzhiyun {
565*4882a593Smuzhiyun int rc = -1;
566*4882a593Smuzhiyun
567*4882a593Smuzhiyun if (nx_driver.of.flags != NX_OF_FLAG_MASK_READY)
568*4882a593Smuzhiyun goto out;
569*4882a593Smuzhiyun
570*4882a593Smuzhiyun memset(&nx_driver.stats, 0, sizeof(struct nx_stats));
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun NX_DEBUGFS_INIT(&nx_driver);
573*4882a593Smuzhiyun
574*4882a593Smuzhiyun nx_driver.of.status = NX_OKAY;
575*4882a593Smuzhiyun
576*4882a593Smuzhiyun rc = nx_register_skcipher(&nx_ecb_aes_alg, NX_FC_AES, NX_MODE_AES_ECB);
577*4882a593Smuzhiyun if (rc)
578*4882a593Smuzhiyun goto out;
579*4882a593Smuzhiyun
580*4882a593Smuzhiyun rc = nx_register_skcipher(&nx_cbc_aes_alg, NX_FC_AES, NX_MODE_AES_CBC);
581*4882a593Smuzhiyun if (rc)
582*4882a593Smuzhiyun goto out_unreg_ecb;
583*4882a593Smuzhiyun
584*4882a593Smuzhiyun rc = nx_register_skcipher(&nx_ctr3686_aes_alg, NX_FC_AES,
585*4882a593Smuzhiyun NX_MODE_AES_CTR);
586*4882a593Smuzhiyun if (rc)
587*4882a593Smuzhiyun goto out_unreg_cbc;
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun rc = nx_register_aead(&nx_gcm_aes_alg, NX_FC_AES, NX_MODE_AES_GCM);
590*4882a593Smuzhiyun if (rc)
591*4882a593Smuzhiyun goto out_unreg_ctr3686;
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun rc = nx_register_aead(&nx_gcm4106_aes_alg, NX_FC_AES, NX_MODE_AES_GCM);
594*4882a593Smuzhiyun if (rc)
595*4882a593Smuzhiyun goto out_unreg_gcm;
596*4882a593Smuzhiyun
597*4882a593Smuzhiyun rc = nx_register_aead(&nx_ccm_aes_alg, NX_FC_AES, NX_MODE_AES_CCM);
598*4882a593Smuzhiyun if (rc)
599*4882a593Smuzhiyun goto out_unreg_gcm4106;
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun rc = nx_register_aead(&nx_ccm4309_aes_alg, NX_FC_AES, NX_MODE_AES_CCM);
602*4882a593Smuzhiyun if (rc)
603*4882a593Smuzhiyun goto out_unreg_ccm;
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun rc = nx_register_shash(&nx_shash_sha256_alg, NX_FC_SHA, NX_MODE_SHA,
606*4882a593Smuzhiyun NX_PROPS_SHA256);
607*4882a593Smuzhiyun if (rc)
608*4882a593Smuzhiyun goto out_unreg_ccm4309;
609*4882a593Smuzhiyun
610*4882a593Smuzhiyun rc = nx_register_shash(&nx_shash_sha512_alg, NX_FC_SHA, NX_MODE_SHA,
611*4882a593Smuzhiyun NX_PROPS_SHA512);
612*4882a593Smuzhiyun if (rc)
613*4882a593Smuzhiyun goto out_unreg_s256;
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun rc = nx_register_shash(&nx_shash_aes_xcbc_alg,
616*4882a593Smuzhiyun NX_FC_AES, NX_MODE_AES_XCBC_MAC, -1);
617*4882a593Smuzhiyun if (rc)
618*4882a593Smuzhiyun goto out_unreg_s512;
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun goto out;
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun out_unreg_s512:
623*4882a593Smuzhiyun nx_unregister_shash(&nx_shash_sha512_alg, NX_FC_SHA, NX_MODE_SHA,
624*4882a593Smuzhiyun NX_PROPS_SHA512);
625*4882a593Smuzhiyun out_unreg_s256:
626*4882a593Smuzhiyun nx_unregister_shash(&nx_shash_sha256_alg, NX_FC_SHA, NX_MODE_SHA,
627*4882a593Smuzhiyun NX_PROPS_SHA256);
628*4882a593Smuzhiyun out_unreg_ccm4309:
629*4882a593Smuzhiyun nx_unregister_aead(&nx_ccm4309_aes_alg, NX_FC_AES, NX_MODE_AES_CCM);
630*4882a593Smuzhiyun out_unreg_ccm:
631*4882a593Smuzhiyun nx_unregister_aead(&nx_ccm_aes_alg, NX_FC_AES, NX_MODE_AES_CCM);
632*4882a593Smuzhiyun out_unreg_gcm4106:
633*4882a593Smuzhiyun nx_unregister_aead(&nx_gcm4106_aes_alg, NX_FC_AES, NX_MODE_AES_GCM);
634*4882a593Smuzhiyun out_unreg_gcm:
635*4882a593Smuzhiyun nx_unregister_aead(&nx_gcm_aes_alg, NX_FC_AES, NX_MODE_AES_GCM);
636*4882a593Smuzhiyun out_unreg_ctr3686:
637*4882a593Smuzhiyun nx_unregister_skcipher(&nx_ctr3686_aes_alg, NX_FC_AES, NX_MODE_AES_CTR);
638*4882a593Smuzhiyun out_unreg_cbc:
639*4882a593Smuzhiyun nx_unregister_skcipher(&nx_cbc_aes_alg, NX_FC_AES, NX_MODE_AES_CBC);
640*4882a593Smuzhiyun out_unreg_ecb:
641*4882a593Smuzhiyun nx_unregister_skcipher(&nx_ecb_aes_alg, NX_FC_AES, NX_MODE_AES_ECB);
642*4882a593Smuzhiyun out:
643*4882a593Smuzhiyun return rc;
644*4882a593Smuzhiyun }
645*4882a593Smuzhiyun
646*4882a593Smuzhiyun /**
647*4882a593Smuzhiyun * nx_crypto_ctx_init - create and initialize a crypto api context
648*4882a593Smuzhiyun *
649*4882a593Smuzhiyun * @nx_ctx: the crypto api context
650*4882a593Smuzhiyun * @fc: function code for the context
651*4882a593Smuzhiyun * @mode: the function code specific mode for this context
652*4882a593Smuzhiyun */
nx_crypto_ctx_init(struct nx_crypto_ctx * nx_ctx,u32 fc,u32 mode)653*4882a593Smuzhiyun static int nx_crypto_ctx_init(struct nx_crypto_ctx *nx_ctx, u32 fc, u32 mode)
654*4882a593Smuzhiyun {
655*4882a593Smuzhiyun if (nx_driver.of.status != NX_OKAY) {
656*4882a593Smuzhiyun pr_err("Attempt to initialize NX crypto context while device "
657*4882a593Smuzhiyun "is not available!\n");
658*4882a593Smuzhiyun return -ENODEV;
659*4882a593Smuzhiyun }
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun /* we need an extra page for csbcpb_aead for these modes */
662*4882a593Smuzhiyun if (mode == NX_MODE_AES_GCM || mode == NX_MODE_AES_CCM)
663*4882a593Smuzhiyun nx_ctx->kmem_len = (5 * NX_PAGE_SIZE) +
664*4882a593Smuzhiyun sizeof(struct nx_csbcpb);
665*4882a593Smuzhiyun else
666*4882a593Smuzhiyun nx_ctx->kmem_len = (4 * NX_PAGE_SIZE) +
667*4882a593Smuzhiyun sizeof(struct nx_csbcpb);
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun nx_ctx->kmem = kmalloc(nx_ctx->kmem_len, GFP_KERNEL);
670*4882a593Smuzhiyun if (!nx_ctx->kmem)
671*4882a593Smuzhiyun return -ENOMEM;
672*4882a593Smuzhiyun
673*4882a593Smuzhiyun /* the csbcpb and scatterlists must be 4K aligned pages */
674*4882a593Smuzhiyun nx_ctx->csbcpb = (struct nx_csbcpb *)(round_up((u64)nx_ctx->kmem,
675*4882a593Smuzhiyun (u64)NX_PAGE_SIZE));
676*4882a593Smuzhiyun nx_ctx->in_sg = (struct nx_sg *)((u8 *)nx_ctx->csbcpb + NX_PAGE_SIZE);
677*4882a593Smuzhiyun nx_ctx->out_sg = (struct nx_sg *)((u8 *)nx_ctx->in_sg + NX_PAGE_SIZE);
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun if (mode == NX_MODE_AES_GCM || mode == NX_MODE_AES_CCM)
680*4882a593Smuzhiyun nx_ctx->csbcpb_aead =
681*4882a593Smuzhiyun (struct nx_csbcpb *)((u8 *)nx_ctx->out_sg +
682*4882a593Smuzhiyun NX_PAGE_SIZE);
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun /* give each context a pointer to global stats and their OF
685*4882a593Smuzhiyun * properties */
686*4882a593Smuzhiyun nx_ctx->stats = &nx_driver.stats;
687*4882a593Smuzhiyun memcpy(nx_ctx->props, nx_driver.of.ap[fc][mode],
688*4882a593Smuzhiyun sizeof(struct alg_props) * 3);
689*4882a593Smuzhiyun
690*4882a593Smuzhiyun return 0;
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun
693*4882a593Smuzhiyun /* entry points from the crypto tfm initializers */
nx_crypto_ctx_aes_ccm_init(struct crypto_aead * tfm)694*4882a593Smuzhiyun int nx_crypto_ctx_aes_ccm_init(struct crypto_aead *tfm)
695*4882a593Smuzhiyun {
696*4882a593Smuzhiyun crypto_aead_set_reqsize(tfm, sizeof(struct nx_ccm_rctx));
697*4882a593Smuzhiyun return nx_crypto_ctx_init(crypto_aead_ctx(tfm), NX_FC_AES,
698*4882a593Smuzhiyun NX_MODE_AES_CCM);
699*4882a593Smuzhiyun }
700*4882a593Smuzhiyun
nx_crypto_ctx_aes_gcm_init(struct crypto_aead * tfm)701*4882a593Smuzhiyun int nx_crypto_ctx_aes_gcm_init(struct crypto_aead *tfm)
702*4882a593Smuzhiyun {
703*4882a593Smuzhiyun crypto_aead_set_reqsize(tfm, sizeof(struct nx_gcm_rctx));
704*4882a593Smuzhiyun return nx_crypto_ctx_init(crypto_aead_ctx(tfm), NX_FC_AES,
705*4882a593Smuzhiyun NX_MODE_AES_GCM);
706*4882a593Smuzhiyun }
707*4882a593Smuzhiyun
nx_crypto_ctx_aes_ctr_init(struct crypto_skcipher * tfm)708*4882a593Smuzhiyun int nx_crypto_ctx_aes_ctr_init(struct crypto_skcipher *tfm)
709*4882a593Smuzhiyun {
710*4882a593Smuzhiyun return nx_crypto_ctx_init(crypto_skcipher_ctx(tfm), NX_FC_AES,
711*4882a593Smuzhiyun NX_MODE_AES_CTR);
712*4882a593Smuzhiyun }
713*4882a593Smuzhiyun
nx_crypto_ctx_aes_cbc_init(struct crypto_skcipher * tfm)714*4882a593Smuzhiyun int nx_crypto_ctx_aes_cbc_init(struct crypto_skcipher *tfm)
715*4882a593Smuzhiyun {
716*4882a593Smuzhiyun return nx_crypto_ctx_init(crypto_skcipher_ctx(tfm), NX_FC_AES,
717*4882a593Smuzhiyun NX_MODE_AES_CBC);
718*4882a593Smuzhiyun }
719*4882a593Smuzhiyun
nx_crypto_ctx_aes_ecb_init(struct crypto_skcipher * tfm)720*4882a593Smuzhiyun int nx_crypto_ctx_aes_ecb_init(struct crypto_skcipher *tfm)
721*4882a593Smuzhiyun {
722*4882a593Smuzhiyun return nx_crypto_ctx_init(crypto_skcipher_ctx(tfm), NX_FC_AES,
723*4882a593Smuzhiyun NX_MODE_AES_ECB);
724*4882a593Smuzhiyun }
725*4882a593Smuzhiyun
nx_crypto_ctx_sha_init(struct crypto_tfm * tfm)726*4882a593Smuzhiyun int nx_crypto_ctx_sha_init(struct crypto_tfm *tfm)
727*4882a593Smuzhiyun {
728*4882a593Smuzhiyun return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_SHA, NX_MODE_SHA);
729*4882a593Smuzhiyun }
730*4882a593Smuzhiyun
nx_crypto_ctx_aes_xcbc_init(struct crypto_tfm * tfm)731*4882a593Smuzhiyun int nx_crypto_ctx_aes_xcbc_init(struct crypto_tfm *tfm)
732*4882a593Smuzhiyun {
733*4882a593Smuzhiyun return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
734*4882a593Smuzhiyun NX_MODE_AES_XCBC_MAC);
735*4882a593Smuzhiyun }
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun /**
738*4882a593Smuzhiyun * nx_crypto_ctx_exit - destroy a crypto api context
739*4882a593Smuzhiyun *
740*4882a593Smuzhiyun * @tfm: the crypto transform pointer for the context
741*4882a593Smuzhiyun *
742*4882a593Smuzhiyun * As crypto API contexts are destroyed, this exit hook is called to free the
743*4882a593Smuzhiyun * memory associated with it.
744*4882a593Smuzhiyun */
nx_crypto_ctx_exit(struct crypto_tfm * tfm)745*4882a593Smuzhiyun void nx_crypto_ctx_exit(struct crypto_tfm *tfm)
746*4882a593Smuzhiyun {
747*4882a593Smuzhiyun struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
748*4882a593Smuzhiyun
749*4882a593Smuzhiyun kfree_sensitive(nx_ctx->kmem);
750*4882a593Smuzhiyun nx_ctx->csbcpb = NULL;
751*4882a593Smuzhiyun nx_ctx->csbcpb_aead = NULL;
752*4882a593Smuzhiyun nx_ctx->in_sg = NULL;
753*4882a593Smuzhiyun nx_ctx->out_sg = NULL;
754*4882a593Smuzhiyun }
755*4882a593Smuzhiyun
nx_crypto_ctx_skcipher_exit(struct crypto_skcipher * tfm)756*4882a593Smuzhiyun void nx_crypto_ctx_skcipher_exit(struct crypto_skcipher *tfm)
757*4882a593Smuzhiyun {
758*4882a593Smuzhiyun nx_crypto_ctx_exit(crypto_skcipher_ctx(tfm));
759*4882a593Smuzhiyun }
760*4882a593Smuzhiyun
nx_crypto_ctx_aead_exit(struct crypto_aead * tfm)761*4882a593Smuzhiyun void nx_crypto_ctx_aead_exit(struct crypto_aead *tfm)
762*4882a593Smuzhiyun {
763*4882a593Smuzhiyun struct nx_crypto_ctx *nx_ctx = crypto_aead_ctx(tfm);
764*4882a593Smuzhiyun
765*4882a593Smuzhiyun kfree_sensitive(nx_ctx->kmem);
766*4882a593Smuzhiyun }
767*4882a593Smuzhiyun
nx_probe(struct vio_dev * viodev,const struct vio_device_id * id)768*4882a593Smuzhiyun static int nx_probe(struct vio_dev *viodev, const struct vio_device_id *id)
769*4882a593Smuzhiyun {
770*4882a593Smuzhiyun dev_dbg(&viodev->dev, "driver probed: %s resource id: 0x%x\n",
771*4882a593Smuzhiyun viodev->name, viodev->resource_id);
772*4882a593Smuzhiyun
773*4882a593Smuzhiyun if (nx_driver.viodev) {
774*4882a593Smuzhiyun dev_err(&viodev->dev, "%s: Attempt to register more than one "
775*4882a593Smuzhiyun "instance of the hardware\n", __func__);
776*4882a593Smuzhiyun return -EINVAL;
777*4882a593Smuzhiyun }
778*4882a593Smuzhiyun
779*4882a593Smuzhiyun nx_driver.viodev = viodev;
780*4882a593Smuzhiyun
781*4882a593Smuzhiyun nx_of_init(&viodev->dev, &nx_driver.of);
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun return nx_register_algs();
784*4882a593Smuzhiyun }
785*4882a593Smuzhiyun
nx_remove(struct vio_dev * viodev)786*4882a593Smuzhiyun static int nx_remove(struct vio_dev *viodev)
787*4882a593Smuzhiyun {
788*4882a593Smuzhiyun dev_dbg(&viodev->dev, "entering nx_remove for UA 0x%x\n",
789*4882a593Smuzhiyun viodev->unit_address);
790*4882a593Smuzhiyun
791*4882a593Smuzhiyun if (nx_driver.of.status == NX_OKAY) {
792*4882a593Smuzhiyun NX_DEBUGFS_FINI(&nx_driver);
793*4882a593Smuzhiyun
794*4882a593Smuzhiyun nx_unregister_shash(&nx_shash_aes_xcbc_alg,
795*4882a593Smuzhiyun NX_FC_AES, NX_MODE_AES_XCBC_MAC, -1);
796*4882a593Smuzhiyun nx_unregister_shash(&nx_shash_sha512_alg,
797*4882a593Smuzhiyun NX_FC_SHA, NX_MODE_SHA, NX_PROPS_SHA256);
798*4882a593Smuzhiyun nx_unregister_shash(&nx_shash_sha256_alg,
799*4882a593Smuzhiyun NX_FC_SHA, NX_MODE_SHA, NX_PROPS_SHA512);
800*4882a593Smuzhiyun nx_unregister_aead(&nx_ccm4309_aes_alg,
801*4882a593Smuzhiyun NX_FC_AES, NX_MODE_AES_CCM);
802*4882a593Smuzhiyun nx_unregister_aead(&nx_ccm_aes_alg, NX_FC_AES, NX_MODE_AES_CCM);
803*4882a593Smuzhiyun nx_unregister_aead(&nx_gcm4106_aes_alg,
804*4882a593Smuzhiyun NX_FC_AES, NX_MODE_AES_GCM);
805*4882a593Smuzhiyun nx_unregister_aead(&nx_gcm_aes_alg,
806*4882a593Smuzhiyun NX_FC_AES, NX_MODE_AES_GCM);
807*4882a593Smuzhiyun nx_unregister_skcipher(&nx_ctr3686_aes_alg,
808*4882a593Smuzhiyun NX_FC_AES, NX_MODE_AES_CTR);
809*4882a593Smuzhiyun nx_unregister_skcipher(&nx_cbc_aes_alg, NX_FC_AES,
810*4882a593Smuzhiyun NX_MODE_AES_CBC);
811*4882a593Smuzhiyun nx_unregister_skcipher(&nx_ecb_aes_alg, NX_FC_AES,
812*4882a593Smuzhiyun NX_MODE_AES_ECB);
813*4882a593Smuzhiyun }
814*4882a593Smuzhiyun
815*4882a593Smuzhiyun return 0;
816*4882a593Smuzhiyun }
817*4882a593Smuzhiyun
818*4882a593Smuzhiyun
819*4882a593Smuzhiyun /* module wide initialization/cleanup */
nx_init(void)820*4882a593Smuzhiyun static int __init nx_init(void)
821*4882a593Smuzhiyun {
822*4882a593Smuzhiyun return vio_register_driver(&nx_driver.viodriver);
823*4882a593Smuzhiyun }
824*4882a593Smuzhiyun
nx_fini(void)825*4882a593Smuzhiyun static void __exit nx_fini(void)
826*4882a593Smuzhiyun {
827*4882a593Smuzhiyun vio_unregister_driver(&nx_driver.viodriver);
828*4882a593Smuzhiyun }
829*4882a593Smuzhiyun
830*4882a593Smuzhiyun static const struct vio_device_id nx_crypto_driver_ids[] = {
831*4882a593Smuzhiyun { "ibm,sym-encryption-v1", "ibm,sym-encryption" },
832*4882a593Smuzhiyun { "", "" }
833*4882a593Smuzhiyun };
834*4882a593Smuzhiyun MODULE_DEVICE_TABLE(vio, nx_crypto_driver_ids);
835*4882a593Smuzhiyun
836*4882a593Smuzhiyun /* driver state structure */
837*4882a593Smuzhiyun struct nx_crypto_driver nx_driver = {
838*4882a593Smuzhiyun .viodriver = {
839*4882a593Smuzhiyun .id_table = nx_crypto_driver_ids,
840*4882a593Smuzhiyun .probe = nx_probe,
841*4882a593Smuzhiyun .remove = nx_remove,
842*4882a593Smuzhiyun .name = NX_NAME,
843*4882a593Smuzhiyun },
844*4882a593Smuzhiyun };
845*4882a593Smuzhiyun
846*4882a593Smuzhiyun module_init(nx_init);
847*4882a593Smuzhiyun module_exit(nx_fini);
848*4882a593Smuzhiyun
849*4882a593Smuzhiyun MODULE_AUTHOR("Kent Yoder <yoder1@us.ibm.com>");
850*4882a593Smuzhiyun MODULE_DESCRIPTION(NX_STRING);
851*4882a593Smuzhiyun MODULE_LICENSE("GPL");
852*4882a593Smuzhiyun MODULE_VERSION(NX_VERSION);
853