2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003-2012 Broadcom Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/systm.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>
45 #include <sys/mutex.h>
46 #include <sys/sysctl.h>
48 #include <sys/random.h>
53 #include <dev/pci/pcivar.h>
55 #include <opencrypto/cryptodev.h>
57 #include "cryptodev_if.h"
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>
75 static void print_krp_params(struct cryptkop *krp);
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);
87 static int xlp_rsa_probe(device_t);
88 static int xlp_rsa_attach(device_t);
89 static int xlp_rsa_detach(device_t);
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),
98 DEVMETHOD(bus_print_child, bus_generic_print_child),
99 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
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),
109 static driver_t xlp_rsa_driver = {
112 sizeof(struct xlp_rsa_softc)
114 static devclass_t xlp_rsa_devclass;
116 DRIVER_MODULE(nlmrsa, pci, xlp_rsa_driver, xlp_rsa_devclass, 0, 0);
117 MODULE_DEPEND(nlmrsa, crypto, 1, 1, 1);
121 print_krp_params(struct cryptkop *krp)
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));
141 xlp_rsa_init(struct xlp_rsa_softc *sc, int node)
143 struct xlp_rsa_command *cmd = NULL;
144 uint32_t fbvc, dstvc, endsel, regval;
145 struct nlm_fmn_msg m;
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) {
153 printf("Couldn't register rsa/ecc msgring handler\n");
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 */
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]);
164 for (dstvc = sc->rsaecc_vc_start; dstvc <= sc->rsaecc_vc_end; dstvc++) {
165 cmd = malloc(sizeof(struct xlp_rsa_command), M_DEVBUF,
167 KASSERT(cmd != NULL, ("%s:cmd is NULL\n", __func__));
168 cmd->rsasrc = contigmalloc(sizeof(nlm_rsa_ucode_data),
171 0UL /* low address */, -1UL /* high address */,
172 XLP_L2L3_CACHELINE_SIZE /* alignment */,
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;
186 ret = nlm_fmn_msgsend(dstvc, 3, FMN_SWCODE_RSA, &m);
189 printf("%s: msgsnd failed (%x)\n", __func__, ret);
193 /* Configure so that all VCs send request to all RSA pipes */
194 base = nlm_get_rsa_regbase(node);
195 if (nlm_is_xlp3xx()) {
202 for (i = 0; i < endsel; i++)
203 nlm_write_rsa_reg(base, RSA_ENG_SEL_0 + i, regval);
206 xlp_free_cmd_params(cmd);
210 /* This function is called from an interrupt handler */
212 nlm_xlprsaecc_msgring_handler(int vc, int size, int code, int src_id,
213 struct nlm_fmn_msg *msg, void *data)
215 struct xlp_rsa_command *cmd;
216 struct xlp_rsa_softc *sc;
217 struct crparam *outparam;
220 KASSERT(code == FMN_SWCODE_RSA,
221 ("%s: bad code = %d, expected code = %d\n", __func__, code,
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));
229 cmd = (struct xlp_rsa_command *)(uintptr_t)msg->msg[1];
230 KASSERT(cmd != NULL, ("%s:cmd not received properly\n", __func__));
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;
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,
245 howmany(outparam->crp_nbits, 8),
247 crypto_kdone(cmd->krp);
250 xlp_free_cmd_params(cmd);
254 xlp_rsa_probe(device_t dev)
256 struct xlp_rsa_softc *sc;
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);
267 * Attach an interface that successfully probed.
270 xlp_rsa_attach(device_t dev)
272 struct xlp_rsa_softc *sc = device_get_softc(dev);
279 node = nlm_get_device_node(pci_get_slot(dev));
280 freq = nlm_set_device_frequency(node, DFS_DEVICE_RSA, 250);
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");
290 if (crypto_kregister(sc->sc_cid, CRK_MOD_EXP, 0) != 0)
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;
299 if (xlp_rsa_init(sc, node) != 0)
301 device_printf(dev, "RSA Initialization complete!\n");
309 * Detach an interface that successfully probed.
312 xlp_rsa_detach(device_t dev)
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.
323 xlp_rsa_newsession(device_t dev, u_int32_t *sidp, struct cryptoini *cri)
325 struct xlp_rsa_softc *sc = device_get_softc(dev);
326 struct xlp_rsa_session *ses = NULL;
329 if (sidp == NULL || cri == NULL || sc == NULL)
332 if (sc->sc_sessions == NULL) {
333 ses = sc->sc_sessions = malloc(sizeof(struct xlp_rsa_session),
338 sc->sc_nsessions = 1;
340 for (sesn = 0; sesn < sc->sc_nsessions; sesn++) {
341 if (!sc->sc_sessions[sesn].hs_used) {
342 ses = &sc->sc_sessions[sesn];
348 sesn = sc->sc_nsessions;
349 ses = malloc((sesn + 1) * sizeof(*ses),
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];
361 bzero(ses, sizeof(*ses));
362 ses->sessionid = sesn;
365 *sidp = XLP_RSA_SID(device_get_unit(sc->sc_dev), sesn);
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.
375 xlp_rsa_freesession(device_t dev, u_int64_t tid)
377 struct xlp_rsa_softc *sc = device_get_softc(dev);
379 u_int32_t sid = CRYPTO_SESID2LID(tid);
384 session = XLP_RSA_SESSION(sid);
385 if (session >= sc->sc_nsessions)
388 sc->sc_sessions[session].hs_used = 0;
393 xlp_free_cmd_params(struct xlp_rsa_command *cmd)
398 if (cmd->rsasrc != NULL) {
399 if (cmd->krp == NULL) /* Micro code load */
400 contigfree(cmd->rsasrc, sizeof(nlm_rsa_ucode_data),
403 free(cmd->rsasrc, M_DEVBUF);
409 xlp_get_rsa_opsize(struct xlp_rsa_command *cmd, unsigned int bits)
412 if (bits == 0 || bits > 8192)
414 /* XLP hardware expects always a fixed size with unused bytes
415 * zeroed out in the input data */
419 } else if (bits <= 1024) {
421 cmd->rsaopsize = 128;
422 } else if (bits <= 2048) {
424 cmd->rsaopsize = 256;
425 } else if (bits <= 4096) {
427 cmd->rsaopsize = 512;
428 } else if (bits <= 8192) {
430 cmd->rsaopsize = 1024;
436 xlp_rsa_inp2hwformat(uint8_t *src, uint8_t *dst, uint32_t paramsize,
439 uint32_t pdwords, pbytes;
442 pdwords = paramsize / 8;
443 pbytes = paramsize % 8;
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];
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];
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];
466 nlm_crypto_complete_rsa_request(struct xlp_rsa_softc *sc,
467 struct xlp_rsa_command *cmd)
470 struct nlm_fmn_msg m;
473 fbvc = nlm_cpuid() * 4 + XLPGE_FB_VC;
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;
483 /* Send the message to rsa engine vc */
484 ret = nlm_fmn_msgsend(sc->rsaecc_vc_start, 3, FMN_SWCODE_RSA, &m);
487 printf("%s: msgsnd failed (%x)\n", __func__, ret);
495 xlp_rsa_kprocess(device_t dev, struct cryptkop *krp, int hint)
497 struct xlp_rsa_softc *sc = device_get_softc(dev);
498 struct xlp_rsa_command *cmd;
502 if (krp == NULL || krp->krp_callback == NULL)
505 cmd = malloc(sizeof(struct xlp_rsa_command), M_DEVBUF,
507 KASSERT(cmd != NULL, ("%s:cmd is NULL\n", __func__));
511 print_krp_params(krp);
514 switch (krp->krp_op) {
516 if (krp->krp_iparams == 3 && krp->krp_oparams == 1)
520 device_printf(dev, "Op:%d not yet supported\n", krp->krp_op);
524 err = xlp_get_rsa_opsize(cmd,
525 krp->krp_param[krp->krp_iparams - 1].crp_nbits);
530 cmd->rsafn = 0; /* Mod Exp */
531 cmd->rsasrc = malloc(
532 cmd->rsaopsize * (krp->krp_iparams + krp->krp_oparams),
535 if (cmd->rsasrc == NULL) {
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);
547 err = nlm_crypto_complete_rsa_request(sc, cmd);
553 xlp_free_cmd_params(cmd);
554 krp->krp_status = err;