]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/nlm/dev/sec/nlmrsa.c
Update vis(3) the latest from NetBSD.
[FreeBSD/FreeBSD.git] / sys / mips / nlm / dev / sec / nlmrsa.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2003-2012 Broadcom Corporation
5  * All Rights Reserved
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY BROADCOM ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
28  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/proc.h>
38 #include <sys/errno.h>
39 #include <sys/endian.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/mbuf.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/sysctl.h>
47 #include <sys/bus.h>
48 #include <sys/random.h>
49 #include <sys/rman.h>
50 #include <sys/uio.h>
51 #include <sys/kobj.h>
52
53 #include <dev/pci/pcivar.h>
54
55 #include <opencrypto/cryptodev.h>
56
57 #include "cryptodev_if.h"
58
59 #include <vm/vm.h>
60 #include <vm/pmap.h>
61
62 #include <mips/nlm/hal/haldefs.h>
63 #include <mips/nlm/hal/iomap.h>
64 #include <mips/nlm/xlp.h>
65 #include <mips/nlm/hal/sys.h>
66 #include <mips/nlm/hal/fmn.h>
67 #include <mips/nlm/hal/nlmsaelib.h>
68 #include <mips/nlm/dev/sec/rsa_ucode.h>
69 #include <mips/nlm/hal/cop2.h>
70 #include <mips/nlm/hal/mips-extns.h>
71 #include <mips/nlm/msgring.h>
72 #include <mips/nlm/dev/sec/nlmrsalib.h>
73
74 #ifdef NLM_RSA_DEBUG
75 static  void print_krp_params(struct cryptkop *krp);
76 #endif
77
78 static  int xlp_rsa_init(struct xlp_rsa_softc *sc, int node);
79 static  int xlp_rsa_newsession(device_t , uint32_t *, struct cryptoini *);
80 static  int xlp_rsa_freesession(device_t , uint64_t);
81 static  int xlp_rsa_kprocess(device_t , struct cryptkop *, int);
82 static  int xlp_get_rsa_opsize(struct xlp_rsa_command *cmd, unsigned int bits);
83 static  void xlp_free_cmd_params(struct xlp_rsa_command *cmd);
84 static  int xlp_rsa_inp2hwformat(uint8_t *src, uint8_t *dst,
85     uint32_t paramsize, uint8_t result);
86
87 static  int xlp_rsa_probe(device_t);
88 static  int xlp_rsa_attach(device_t);
89 static  int xlp_rsa_detach(device_t);
90
91 static device_method_t xlp_rsa_methods[] = {
92         /* device interface */
93         DEVMETHOD(device_probe, xlp_rsa_probe),
94         DEVMETHOD(device_attach, xlp_rsa_attach),
95         DEVMETHOD(device_detach, xlp_rsa_detach),
96
97         /* bus interface */
98         DEVMETHOD(bus_print_child, bus_generic_print_child),
99         DEVMETHOD(bus_driver_added, bus_generic_driver_added),
100
101         /* crypto device methods */
102         DEVMETHOD(cryptodev_newsession, xlp_rsa_newsession),
103         DEVMETHOD(cryptodev_freesession, xlp_rsa_freesession),
104         DEVMETHOD(cryptodev_kprocess,   xlp_rsa_kprocess),
105
106         DEVMETHOD_END
107 };
108
109 static driver_t xlp_rsa_driver = {
110         "nlmrsa",
111         xlp_rsa_methods,
112         sizeof(struct xlp_rsa_softc)
113 };
114 static devclass_t xlp_rsa_devclass;
115
116 DRIVER_MODULE(nlmrsa, pci, xlp_rsa_driver, xlp_rsa_devclass, 0, 0);
117 MODULE_DEPEND(nlmrsa, crypto, 1, 1, 1);
118
119 #ifdef NLM_RSA_DEBUG
120 static void
121 print_krp_params(struct cryptkop *krp)
122 {
123         int i;
124
125         printf("krp->krp_op     :%d\n", krp->krp_op);
126         printf("krp->krp_status :%d\n", krp->krp_status);
127         printf("krp->krp_iparams:%d\n", krp->krp_iparams);
128         printf("krp->krp_oparams:%d\n", krp->krp_oparams);
129         for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
130                 printf("krp->krp_param[%d].crp_p        :0x%llx\n", i,
131                     (unsigned long long)krp->krp_param[i].crp_p);
132                 printf("krp->krp_param[%d].crp_nbits    :%d\n", i,
133                     krp->krp_param[i].crp_nbits);
134                 printf("krp->krp_param[%d].crp_nbytes   :%d\n", i,
135                     howmany(krp->krp_param[i].crp_nbits, 8));
136         }
137 }
138 #endif
139
140 static int
141 xlp_rsa_init(struct xlp_rsa_softc *sc, int node)
142 {
143         struct xlp_rsa_command *cmd = NULL;
144         uint32_t fbvc, dstvc, endsel, regval;
145         struct nlm_fmn_msg m;
146         int err, ret, i;
147         uint64_t base;
148
149         /* Register interrupt handler for the RSA/ECC CMS messages */
150         if (register_msgring_handler(sc->rsaecc_vc_start,
151             sc->rsaecc_vc_end, nlm_xlprsaecc_msgring_handler, sc) != 0) {
152                 err = -1;
153                 printf("Couldn't register rsa/ecc msgring handler\n");
154                 goto errout;
155         }
156         fbvc = nlm_cpuid() * 4 + XLPGE_FB_VC;
157         /* Do the CMS credit initialization */
158         /* Currently it is configured by default to 50 when kernel comes up */
159
160 #if BYTE_ORDER == LITTLE_ENDIAN
161         for (i = 0; i < nitems(nlm_rsa_ucode_data); i++)
162                 nlm_rsa_ucode_data[i] = htobe64(nlm_rsa_ucode_data[i]);
163 #endif
164         for (dstvc = sc->rsaecc_vc_start; dstvc <= sc->rsaecc_vc_end; dstvc++) {
165                 cmd = malloc(sizeof(struct xlp_rsa_command), M_DEVBUF,
166                     M_NOWAIT | M_ZERO);
167                 KASSERT(cmd != NULL, ("%s:cmd is NULL\n", __func__));
168                 cmd->rsasrc = contigmalloc(sizeof(nlm_rsa_ucode_data),
169                     M_DEVBUF,
170                     (M_WAITOK | M_ZERO),
171                     0UL /* low address */, -1UL /* high address */,
172                     XLP_L2L3_CACHELINE_SIZE /* alignment */,
173                     0UL /* boundary */);
174                 KASSERT(cmd->rsasrc != NULL,
175                     ("%s:cmd->rsasrc is NULL\n", __func__));
176                 memcpy(cmd->rsasrc, nlm_rsa_ucode_data,
177                     sizeof(nlm_rsa_ucode_data));
178                 m.msg[0] = nlm_crypto_form_rsa_ecc_fmn_entry0(1, 0x70, 0,
179                     vtophys(cmd->rsasrc));
180                 m.msg[1] = nlm_crypto_form_rsa_ecc_fmn_entry1(0, 1, fbvc,
181                     vtophys(cmd->rsasrc));
182                 /* Software scratch pad */
183                 m.msg[2] = (uintptr_t)cmd;
184                 m.msg[3] = 0;
185
186                 ret = nlm_fmn_msgsend(dstvc, 3, FMN_SWCODE_RSA, &m);
187                 if (ret != 0) {
188                         err = -1;
189                         printf("%s: msgsnd failed (%x)\n", __func__, ret);
190                         goto errout;
191                 }
192         }
193         /* Configure so that all VCs send request to all RSA pipes */
194         base = nlm_get_rsa_regbase(node);
195         if (nlm_is_xlp3xx()) {
196                 endsel = 1;
197                 regval = 0xFFFF;
198         } else {
199                 endsel = 3;
200                 regval = 0x07FFFFFF;
201         }
202         for (i = 0; i < endsel; i++)
203                 nlm_write_rsa_reg(base, RSA_ENG_SEL_0 + i, regval);
204         return (0);
205 errout:
206         xlp_free_cmd_params(cmd);
207         return (err);
208 }
209
210 /* This function is called from an interrupt handler */
211 void
212 nlm_xlprsaecc_msgring_handler(int vc, int size, int code, int src_id,
213     struct nlm_fmn_msg *msg, void *data)
214 {
215         struct xlp_rsa_command *cmd;
216         struct xlp_rsa_softc *sc;
217         struct crparam *outparam;
218         int ostart;
219
220         KASSERT(code == FMN_SWCODE_RSA,
221             ("%s: bad code = %d, expected code = %d\n", __func__, code,
222             FMN_SWCODE_RSA));
223
224         sc = data;
225         KASSERT(src_id >= sc->rsaecc_vc_start && src_id <= sc->rsaecc_vc_end,
226             ("%s: bad src_id = %d, expect %d - %d\n", __func__,
227             src_id, sc->rsaecc_vc_start, sc->rsaecc_vc_end));
228
229         cmd = (struct xlp_rsa_command *)(uintptr_t)msg->msg[1];
230         KASSERT(cmd != NULL, ("%s:cmd not received properly\n", __func__));
231
232         if (RSA_ERROR(msg->msg[0]) != 0) {
233                 printf("%s: Message rcv msg0 %llx msg1 %llx err %x \n",
234                     __func__, (unsigned long long)msg->msg[0],
235                     (unsigned long long)msg->msg[1],
236                     (int)RSA_ERROR(msg->msg[0]));
237                 cmd->krp->krp_status = EBADMSG;
238         }
239
240         if (cmd->krp != NULL) {
241                 ostart = cmd->krp->krp_iparams;
242                 outparam = &cmd->krp->krp_param[ostart];
243                 xlp_rsa_inp2hwformat(cmd->rsasrc + cmd->rsaopsize * ostart,
244                     outparam->crp_p,
245                     howmany(outparam->crp_nbits, 8),
246                     1);
247                 crypto_kdone(cmd->krp);
248         }
249
250         xlp_free_cmd_params(cmd);
251 }
252
253 static int
254 xlp_rsa_probe(device_t dev)
255 {
256         struct xlp_rsa_softc *sc;
257
258         if (pci_get_vendor(dev) == PCI_VENDOR_NETLOGIC &&
259             pci_get_device(dev) == PCI_DEVICE_ID_NLM_RSA) {
260                 sc = device_get_softc(dev);
261                 return (BUS_PROBE_DEFAULT);
262         }
263         return (ENXIO);
264 }
265
266 /*
267  * Attach an interface that successfully probed.
268  */
269 static int
270 xlp_rsa_attach(device_t dev)
271 {
272         struct xlp_rsa_softc *sc = device_get_softc(dev);
273         uint64_t base;
274         int qstart, qnum;
275         int freq, node;
276
277         sc->sc_dev = dev;
278
279         node = nlm_get_device_node(pci_get_slot(dev));
280         freq = nlm_set_device_frequency(node, DFS_DEVICE_RSA, 250);
281         if (bootverbose)
282                 device_printf(dev, "RSA Freq: %dMHz\n", freq);
283         if (pci_get_device(dev) == PCI_DEVICE_ID_NLM_RSA) {
284                 device_set_desc(dev, "XLP RSA/ECC Accelerator");
285                 if ((sc->sc_cid = crypto_get_driverid(dev,
286                     CRYPTOCAP_F_HARDWARE)) < 0) {
287                         printf("xlp_rsaecc-err:couldn't get the driver id\n");
288                         goto error_exit;
289                 }
290                 if (crypto_kregister(sc->sc_cid, CRK_MOD_EXP, 0) != 0)
291                         goto error_exit;
292
293                 base = nlm_get_rsa_pcibase(node);
294                 qstart = nlm_qidstart(base);
295                 qnum = nlm_qnum(base);
296                 sc->rsaecc_vc_start = qstart;
297                 sc->rsaecc_vc_end = qstart + qnum - 1;
298         }
299         if (xlp_rsa_init(sc, node) != 0)
300                 goto error_exit;
301         device_printf(dev, "RSA Initialization complete!\n");
302         return (0);
303
304 error_exit:
305         return (ENXIO);
306 }
307
308 /*
309  * Detach an interface that successfully probed.
310  */
311 static int
312 xlp_rsa_detach(device_t dev)
313 {
314         return (0);
315 }
316
317 /*
318  * Allocate a new 'session' and return an encoded session id.  'sidp'
319  * contains our registration id, and should contain an encoded session
320  * id on successful allocation.
321  */
322 static int
323 xlp_rsa_newsession(device_t dev, u_int32_t *sidp, struct cryptoini *cri)
324 {
325         struct xlp_rsa_softc *sc = device_get_softc(dev);
326         struct xlp_rsa_session *ses = NULL;
327         int sesn;
328
329         if (sidp == NULL || cri == NULL || sc == NULL)
330                 return (EINVAL);
331
332         if (sc->sc_sessions == NULL) {
333                 ses = sc->sc_sessions = malloc(sizeof(struct xlp_rsa_session),
334                     M_DEVBUF, M_NOWAIT);
335                 if (ses == NULL)
336                         return (ENOMEM);
337                 sesn = 0;
338                 sc->sc_nsessions = 1;
339         } else {
340                 for (sesn = 0; sesn < sc->sc_nsessions; sesn++) {
341                         if (!sc->sc_sessions[sesn].hs_used) {
342                                 ses = &sc->sc_sessions[sesn];
343                                 break;
344                         }
345                 }
346
347                 if (ses == NULL) {
348                         sesn = sc->sc_nsessions;
349                         ses = malloc((sesn + 1) * sizeof(*ses),
350                             M_DEVBUF, M_NOWAIT);
351                         if (ses == NULL)
352                                 return (ENOMEM);
353                         bcopy(sc->sc_sessions, ses, sesn * sizeof(*ses));
354                         bzero(sc->sc_sessions, sesn * sizeof(*ses));
355                         free(sc->sc_sessions, M_DEVBUF);
356                         sc->sc_sessions = ses;
357                         ses = &sc->sc_sessions[sesn];
358                         sc->sc_nsessions++;
359                 }
360         }
361         bzero(ses, sizeof(*ses));
362         ses->sessionid = sesn;
363         ses->hs_used = 1;
364
365         *sidp = XLP_RSA_SID(device_get_unit(sc->sc_dev), sesn);
366         return (0);
367 }
368
369 /*
370  * Deallocate a session.
371  * XXX this routine should run a zero'd mac/encrypt key into context ram.
372  * XXX to blow away any keys already stored there.
373  */
374 static int
375 xlp_rsa_freesession(device_t dev, u_int64_t tid)
376 {
377         struct xlp_rsa_softc *sc = device_get_softc(dev);
378         int session;
379         u_int32_t sid = CRYPTO_SESID2LID(tid);
380
381         if (sc == NULL)
382                 return (EINVAL);
383
384         session = XLP_RSA_SESSION(sid);
385         if (session >= sc->sc_nsessions)
386                 return (EINVAL);
387
388         sc->sc_sessions[session].hs_used = 0;
389         return (0);
390 }
391
392 static void
393 xlp_free_cmd_params(struct xlp_rsa_command *cmd)
394 {
395
396         if (cmd == NULL)
397                 return;
398         if (cmd->rsasrc != NULL) {
399                 if (cmd->krp == NULL) /* Micro code load */
400                         contigfree(cmd->rsasrc, sizeof(nlm_rsa_ucode_data),
401                             M_DEVBUF);
402                 else
403                         free(cmd->rsasrc, M_DEVBUF);
404         }
405         free(cmd, M_DEVBUF);
406 }
407
408 static int
409 xlp_get_rsa_opsize(struct xlp_rsa_command *cmd, unsigned int bits)
410 {
411
412         if (bits == 0 || bits > 8192)
413                 return (-1);
414         /* XLP hardware expects always a fixed size with unused bytes
415          * zeroed out in the input data */
416         if (bits <= 512) {
417                 cmd->rsatype = 0x40;
418                 cmd->rsaopsize = 64;
419         } else if (bits <= 1024) {
420                 cmd->rsatype = 0x41;
421                 cmd->rsaopsize = 128;
422         } else if (bits <= 2048) {
423                 cmd->rsatype = 0x42;
424                 cmd->rsaopsize = 256;
425         } else if (bits <= 4096) {
426                 cmd->rsatype = 0x43;
427                 cmd->rsaopsize = 512;
428         } else if (bits <= 8192) {
429                 cmd->rsatype = 0x44;
430                 cmd->rsaopsize = 1024;
431         }
432         return (0);
433 }
434
435 static int
436 xlp_rsa_inp2hwformat(uint8_t *src, uint8_t *dst, uint32_t paramsize,
437     uint8_t result)
438 {
439         uint32_t pdwords, pbytes;
440         int i, j, k;
441
442         pdwords = paramsize / 8;
443         pbytes = paramsize % 8;
444
445         for (i = 0, k = 0; i < pdwords; i++) {
446                 /* copy dwords of inp/hw to hw/out format */
447                 for (j = 7; j >= 0; j--, k++)
448                         dst[i * 8 + j] = src[k];
449         }
450         if (pbytes) {
451                 if (result == 0) {
452                         /* copy rem bytes of input data to hw format */
453                         for (j = 7; k < paramsize; j--, k++)
454                                 dst[i * 8 + j] = src[k];
455                 } else {
456                         /* copy rem bytes of hw data to exp output format */
457                         for (j = 7; k < paramsize; j--, k++)
458                                 dst[k] = src[i * 8 + j];
459                 }
460         }
461
462         return (0);
463 }
464
465 static int
466 nlm_crypto_complete_rsa_request(struct xlp_rsa_softc *sc,
467     struct xlp_rsa_command *cmd)
468 {
469         unsigned int fbvc;
470         struct nlm_fmn_msg m;
471         int ret;
472
473         fbvc = nlm_cpuid() * 4 + XLPGE_FB_VC;
474
475         m.msg[0] = nlm_crypto_form_rsa_ecc_fmn_entry0(1, cmd->rsatype,
476             cmd->rsafn, vtophys(cmd->rsasrc));
477         m.msg[1] = nlm_crypto_form_rsa_ecc_fmn_entry1(0, 1, fbvc,
478             vtophys(cmd->rsasrc + cmd->rsaopsize * cmd->krp->krp_iparams));
479         /* Software scratch pad */
480         m.msg[2] = (uintptr_t)cmd;
481         m.msg[3] = 0;
482
483         /* Send the message to rsa engine vc */
484         ret = nlm_fmn_msgsend(sc->rsaecc_vc_start, 3, FMN_SWCODE_RSA, &m);
485         if (ret != 0) {
486 #ifdef NLM_SEC_DEBUG
487                 printf("%s: msgsnd failed (%x)\n", __func__, ret);
488 #endif
489                 return (ERESTART);
490         }
491         return (0);
492 }
493
494 static int
495 xlp_rsa_kprocess(device_t dev, struct cryptkop *krp, int hint)
496 {
497         struct xlp_rsa_softc *sc = device_get_softc(dev);
498         struct xlp_rsa_command *cmd;
499         struct crparam *kp;
500         int err, i;
501
502         if (krp == NULL || krp->krp_callback == NULL)
503                 return (EINVAL);
504
505         cmd = malloc(sizeof(struct xlp_rsa_command), M_DEVBUF,
506             M_NOWAIT | M_ZERO);
507         KASSERT(cmd != NULL, ("%s:cmd is NULL\n", __func__));
508         cmd->krp = krp;
509
510 #ifdef NLM_RSA_DEBUG
511         print_krp_params(krp);
512 #endif
513         err = EOPNOTSUPP;
514         switch (krp->krp_op) {
515         case CRK_MOD_EXP:
516                 if (krp->krp_iparams == 3 && krp->krp_oparams == 1)
517                         break;
518                 goto errout;
519         default:
520                 device_printf(dev, "Op:%d not yet supported\n", krp->krp_op);
521                 goto errout;
522         }
523
524         err = xlp_get_rsa_opsize(cmd,
525             krp->krp_param[krp->krp_iparams - 1].crp_nbits);
526         if (err != 0) {
527                 err = EINVAL;
528                 goto errout;
529         }
530         cmd->rsafn = 0; /* Mod Exp */
531         cmd->rsasrc = malloc(
532             cmd->rsaopsize * (krp->krp_iparams + krp->krp_oparams),
533             M_DEVBUF,
534             M_NOWAIT | M_ZERO);
535         if (cmd->rsasrc == NULL) {
536                 err = ENOMEM;
537                 goto errout;
538         }
539
540         for (i = 0, kp = krp->krp_param; i < krp->krp_iparams; i++, kp++) {
541                 KASSERT(kp->crp_nbits != 0,
542                     ("%s: parameter[%d]'s length is zero\n", __func__, i));
543                 xlp_rsa_inp2hwformat(kp->crp_p,
544                     cmd->rsasrc + i * cmd->rsaopsize,
545                     howmany(kp->crp_nbits, 8), 0);
546         }
547         err = nlm_crypto_complete_rsa_request(sc, cmd);
548         if (err != 0)
549                 goto errout;
550
551         return (0);
552 errout:
553         xlp_free_cmd_params(cmd);
554         krp->krp_status = err;
555         crypto_kdone(krp);
556         return (err);
557 }