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 , crypto_session_t, struct cryptoini *);
80 static int xlp_rsa_kprocess(device_t , struct cryptkop *, int);
81 static int xlp_get_rsa_opsize(struct xlp_rsa_command *cmd, unsigned int bits);
82 static void xlp_free_cmd_params(struct xlp_rsa_command *cmd);
83 static int xlp_rsa_inp2hwformat(uint8_t *src, uint8_t *dst,
84 uint32_t paramsize, uint8_t result);
85
86 static int xlp_rsa_probe(device_t);
87 static int xlp_rsa_attach(device_t);
88 static int xlp_rsa_detach(device_t);
89
90 static device_method_t xlp_rsa_methods[] = {
91 /* device interface */
92 DEVMETHOD(device_probe, xlp_rsa_probe),
93 DEVMETHOD(device_attach, xlp_rsa_attach),
94 DEVMETHOD(device_detach, xlp_rsa_detach),
95
96 /* bus interface */
97 DEVMETHOD(bus_print_child, bus_generic_print_child),
98 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
99
100 /* crypto device methods */
101 DEVMETHOD(cryptodev_newsession, xlp_rsa_newsession),
102 DEVMETHOD(cryptodev_kprocess, xlp_rsa_kprocess),
103
104 DEVMETHOD_END
105 };
106
107 static driver_t xlp_rsa_driver = {
108 "nlmrsa",
109 xlp_rsa_methods,
110 sizeof(struct xlp_rsa_softc)
111 };
112 static devclass_t xlp_rsa_devclass;
113
114 DRIVER_MODULE(nlmrsa, pci, xlp_rsa_driver, xlp_rsa_devclass, 0, 0);
115 MODULE_DEPEND(nlmrsa, crypto, 1, 1, 1);
116
117 #ifdef NLM_RSA_DEBUG
118 static void
print_krp_params(struct cryptkop * krp)119 print_krp_params(struct cryptkop *krp)
120 {
121 int i;
122
123 printf("krp->krp_op :%d\n", krp->krp_op);
124 printf("krp->krp_status :%d\n", krp->krp_status);
125 printf("krp->krp_iparams:%d\n", krp->krp_iparams);
126 printf("krp->krp_oparams:%d\n", krp->krp_oparams);
127 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
128 printf("krp->krp_param[%d].crp_p :0x%llx\n", i,
129 (unsigned long long)krp->krp_param[i].crp_p);
130 printf("krp->krp_param[%d].crp_nbits :%d\n", i,
131 krp->krp_param[i].crp_nbits);
132 printf("krp->krp_param[%d].crp_nbytes :%d\n", i,
133 howmany(krp->krp_param[i].crp_nbits, 8));
134 }
135 }
136 #endif
137
138 static int
xlp_rsa_init(struct xlp_rsa_softc * sc,int node)139 xlp_rsa_init(struct xlp_rsa_softc *sc, int node)
140 {
141 struct xlp_rsa_command *cmd = NULL;
142 uint32_t fbvc, dstvc, endsel, regval;
143 struct nlm_fmn_msg m;
144 int err, ret, i;
145 uint64_t base;
146
147 /* Register interrupt handler for the RSA/ECC CMS messages */
148 if (register_msgring_handler(sc->rsaecc_vc_start,
149 sc->rsaecc_vc_end, nlm_xlprsaecc_msgring_handler, sc) != 0) {
150 err = -1;
151 printf("Couldn't register rsa/ecc msgring handler\n");
152 goto errout;
153 }
154 fbvc = nlm_cpuid() * 4 + XLPGE_FB_VC;
155 /* Do the CMS credit initialization */
156 /* Currently it is configured by default to 50 when kernel comes up */
157
158 #if BYTE_ORDER == LITTLE_ENDIAN
159 for (i = 0; i < nitems(nlm_rsa_ucode_data); i++)
160 nlm_rsa_ucode_data[i] = htobe64(nlm_rsa_ucode_data[i]);
161 #endif
162 for (dstvc = sc->rsaecc_vc_start; dstvc <= sc->rsaecc_vc_end; dstvc++) {
163 cmd = malloc(sizeof(struct xlp_rsa_command), M_DEVBUF,
164 M_NOWAIT | M_ZERO);
165 KASSERT(cmd != NULL, ("%s:cmd is NULL\n", __func__));
166 cmd->rsasrc = contigmalloc(sizeof(nlm_rsa_ucode_data),
167 M_DEVBUF,
168 (M_WAITOK | M_ZERO),
169 0UL /* low address */, -1UL /* high address */,
170 XLP_L2L3_CACHELINE_SIZE /* alignment */,
171 0UL /* boundary */);
172 KASSERT(cmd->rsasrc != NULL,
173 ("%s:cmd->rsasrc is NULL\n", __func__));
174 memcpy(cmd->rsasrc, nlm_rsa_ucode_data,
175 sizeof(nlm_rsa_ucode_data));
176 m.msg[0] = nlm_crypto_form_rsa_ecc_fmn_entry0(1, 0x70, 0,
177 vtophys(cmd->rsasrc));
178 m.msg[1] = nlm_crypto_form_rsa_ecc_fmn_entry1(0, 1, fbvc,
179 vtophys(cmd->rsasrc));
180 /* Software scratch pad */
181 m.msg[2] = (uintptr_t)cmd;
182 m.msg[3] = 0;
183
184 ret = nlm_fmn_msgsend(dstvc, 3, FMN_SWCODE_RSA, &m);
185 if (ret != 0) {
186 err = -1;
187 printf("%s: msgsnd failed (%x)\n", __func__, ret);
188 goto errout;
189 }
190 }
191 /* Configure so that all VCs send request to all RSA pipes */
192 base = nlm_get_rsa_regbase(node);
193 if (nlm_is_xlp3xx()) {
194 endsel = 1;
195 regval = 0xFFFF;
196 } else {
197 endsel = 3;
198 regval = 0x07FFFFFF;
199 }
200 for (i = 0; i < endsel; i++)
201 nlm_write_rsa_reg(base, RSA_ENG_SEL_0 + i, regval);
202 return (0);
203 errout:
204 xlp_free_cmd_params(cmd);
205 return (err);
206 }
207
208 /* This function is called from an interrupt handler */
209 void
nlm_xlprsaecc_msgring_handler(int vc,int size,int code,int src_id,struct nlm_fmn_msg * msg,void * data)210 nlm_xlprsaecc_msgring_handler(int vc, int size, int code, int src_id,
211 struct nlm_fmn_msg *msg, void *data)
212 {
213 struct xlp_rsa_command *cmd;
214 struct xlp_rsa_softc *sc;
215 struct crparam *outparam;
216 int ostart;
217
218 KASSERT(code == FMN_SWCODE_RSA,
219 ("%s: bad code = %d, expected code = %d\n", __func__, code,
220 FMN_SWCODE_RSA));
221
222 sc = data;
223 KASSERT(src_id >= sc->rsaecc_vc_start && src_id <= sc->rsaecc_vc_end,
224 ("%s: bad src_id = %d, expect %d - %d\n", __func__,
225 src_id, sc->rsaecc_vc_start, sc->rsaecc_vc_end));
226
227 cmd = (struct xlp_rsa_command *)(uintptr_t)msg->msg[1];
228 KASSERT(cmd != NULL, ("%s:cmd not received properly\n", __func__));
229
230 if (RSA_ERROR(msg->msg[0]) != 0) {
231 printf("%s: Message rcv msg0 %llx msg1 %llx err %x \n",
232 __func__, (unsigned long long)msg->msg[0],
233 (unsigned long long)msg->msg[1],
234 (int)RSA_ERROR(msg->msg[0]));
235 cmd->krp->krp_status = EBADMSG;
236 }
237
238 if (cmd->krp != NULL) {
239 ostart = cmd->krp->krp_iparams;
240 outparam = &cmd->krp->krp_param[ostart];
241 xlp_rsa_inp2hwformat(cmd->rsasrc + cmd->rsaopsize * ostart,
242 outparam->crp_p,
243 howmany(outparam->crp_nbits, 8),
244 1);
245 crypto_kdone(cmd->krp);
246 }
247
248 xlp_free_cmd_params(cmd);
249 }
250
251 static int
xlp_rsa_probe(device_t dev)252 xlp_rsa_probe(device_t dev)
253 {
254 struct xlp_rsa_softc *sc;
255
256 if (pci_get_vendor(dev) == PCI_VENDOR_NETLOGIC &&
257 pci_get_device(dev) == PCI_DEVICE_ID_NLM_RSA) {
258 sc = device_get_softc(dev);
259 return (BUS_PROBE_DEFAULT);
260 }
261 return (ENXIO);
262 }
263
264 /*
265 * Attach an interface that successfully probed.
266 */
267 static int
xlp_rsa_attach(device_t dev)268 xlp_rsa_attach(device_t dev)
269 {
270 struct xlp_rsa_softc *sc = device_get_softc(dev);
271 uint64_t base;
272 int qstart, qnum;
273 int freq, node;
274
275 sc->sc_dev = dev;
276
277 node = nlm_get_device_node(pci_get_slot(dev));
278 freq = nlm_set_device_frequency(node, DFS_DEVICE_RSA, 250);
279 if (bootverbose)
280 device_printf(dev, "RSA Freq: %dMHz\n", freq);
281 if (pci_get_device(dev) == PCI_DEVICE_ID_NLM_RSA) {
282 device_set_desc(dev, "XLP RSA/ECC Accelerator");
283 sc->sc_cid = crypto_get_driverid(dev,
284 sizeof(struct xlp_rsa_session), CRYPTOCAP_F_HARDWARE);
285 if (sc->sc_cid < 0) {
286 printf("xlp_rsaecc-err:couldn't get the driver id\n");
287 goto error_exit;
288 }
289 if (crypto_kregister(sc->sc_cid, CRK_MOD_EXP, 0) != 0)
290 goto error_exit;
291
292 base = nlm_get_rsa_pcibase(node);
293 qstart = nlm_qidstart(base);
294 qnum = nlm_qnum(base);
295 sc->rsaecc_vc_start = qstart;
296 sc->rsaecc_vc_end = qstart + qnum - 1;
297 }
298 if (xlp_rsa_init(sc, node) != 0)
299 goto error_exit;
300 device_printf(dev, "RSA Initialization complete!\n");
301 return (0);
302
303 error_exit:
304 return (ENXIO);
305 }
306
307 /*
308 * Detach an interface that successfully probed.
309 */
310 static int
xlp_rsa_detach(device_t dev)311 xlp_rsa_detach(device_t dev)
312 {
313 return (0);
314 }
315
316 /*
317 * Allocate a new 'session' (unused).
318 */
319 static int
xlp_rsa_newsession(device_t dev,crypto_session_t cses,struct cryptoini * cri)320 xlp_rsa_newsession(device_t dev, crypto_session_t cses, struct cryptoini *cri)
321 {
322 struct xlp_rsa_softc *sc = device_get_softc(dev);
323
324 if (cri == NULL || sc == NULL)
325 return (EINVAL);
326
327 return (0);
328 }
329
330 /*
331 * XXX freesession should run a zero'd mac/encrypt key into context ram.
332 * XXX to blow away any keys already stored there.
333 */
334
335 static void
xlp_free_cmd_params(struct xlp_rsa_command * cmd)336 xlp_free_cmd_params(struct xlp_rsa_command *cmd)
337 {
338
339 if (cmd == NULL)
340 return;
341 if (cmd->rsasrc != NULL) {
342 if (cmd->krp == NULL) /* Micro code load */
343 contigfree(cmd->rsasrc, sizeof(nlm_rsa_ucode_data),
344 M_DEVBUF);
345 else
346 free(cmd->rsasrc, M_DEVBUF);
347 }
348 free(cmd, M_DEVBUF);
349 }
350
351 static int
xlp_get_rsa_opsize(struct xlp_rsa_command * cmd,unsigned int bits)352 xlp_get_rsa_opsize(struct xlp_rsa_command *cmd, unsigned int bits)
353 {
354
355 if (bits == 0 || bits > 8192)
356 return (-1);
357 /* XLP hardware expects always a fixed size with unused bytes
358 * zeroed out in the input data */
359 if (bits <= 512) {
360 cmd->rsatype = 0x40;
361 cmd->rsaopsize = 64;
362 } else if (bits <= 1024) {
363 cmd->rsatype = 0x41;
364 cmd->rsaopsize = 128;
365 } else if (bits <= 2048) {
366 cmd->rsatype = 0x42;
367 cmd->rsaopsize = 256;
368 } else if (bits <= 4096) {
369 cmd->rsatype = 0x43;
370 cmd->rsaopsize = 512;
371 } else if (bits <= 8192) {
372 cmd->rsatype = 0x44;
373 cmd->rsaopsize = 1024;
374 }
375 return (0);
376 }
377
378 static int
xlp_rsa_inp2hwformat(uint8_t * src,uint8_t * dst,uint32_t paramsize,uint8_t result)379 xlp_rsa_inp2hwformat(uint8_t *src, uint8_t *dst, uint32_t paramsize,
380 uint8_t result)
381 {
382 uint32_t pdwords, pbytes;
383 int i, j, k;
384
385 pdwords = paramsize / 8;
386 pbytes = paramsize % 8;
387
388 for (i = 0, k = 0; i < pdwords; i++) {
389 /* copy dwords of inp/hw to hw/out format */
390 for (j = 7; j >= 0; j--, k++)
391 dst[i * 8 + j] = src[k];
392 }
393 if (pbytes) {
394 if (result == 0) {
395 /* copy rem bytes of input data to hw format */
396 for (j = 7; k < paramsize; j--, k++)
397 dst[i * 8 + j] = src[k];
398 } else {
399 /* copy rem bytes of hw data to exp output format */
400 for (j = 7; k < paramsize; j--, k++)
401 dst[k] = src[i * 8 + j];
402 }
403 }
404
405 return (0);
406 }
407
408 static int
nlm_crypto_complete_rsa_request(struct xlp_rsa_softc * sc,struct xlp_rsa_command * cmd)409 nlm_crypto_complete_rsa_request(struct xlp_rsa_softc *sc,
410 struct xlp_rsa_command *cmd)
411 {
412 unsigned int fbvc;
413 struct nlm_fmn_msg m;
414 int ret;
415
416 fbvc = nlm_cpuid() * 4 + XLPGE_FB_VC;
417
418 m.msg[0] = nlm_crypto_form_rsa_ecc_fmn_entry0(1, cmd->rsatype,
419 cmd->rsafn, vtophys(cmd->rsasrc));
420 m.msg[1] = nlm_crypto_form_rsa_ecc_fmn_entry1(0, 1, fbvc,
421 vtophys(cmd->rsasrc + cmd->rsaopsize * cmd->krp->krp_iparams));
422 /* Software scratch pad */
423 m.msg[2] = (uintptr_t)cmd;
424 m.msg[3] = 0;
425
426 /* Send the message to rsa engine vc */
427 ret = nlm_fmn_msgsend(sc->rsaecc_vc_start, 3, FMN_SWCODE_RSA, &m);
428 if (ret != 0) {
429 #ifdef NLM_SEC_DEBUG
430 printf("%s: msgsnd failed (%x)\n", __func__, ret);
431 #endif
432 return (ERESTART);
433 }
434 return (0);
435 }
436
437 static int
xlp_rsa_kprocess(device_t dev,struct cryptkop * krp,int hint)438 xlp_rsa_kprocess(device_t dev, struct cryptkop *krp, int hint)
439 {
440 struct xlp_rsa_softc *sc = device_get_softc(dev);
441 struct xlp_rsa_command *cmd;
442 struct crparam *kp;
443 int err, i;
444
445 if (krp == NULL || krp->krp_callback == NULL)
446 return (EINVAL);
447
448 cmd = malloc(sizeof(struct xlp_rsa_command), M_DEVBUF,
449 M_NOWAIT | M_ZERO);
450 KASSERT(cmd != NULL, ("%s:cmd is NULL\n", __func__));
451 cmd->krp = krp;
452
453 #ifdef NLM_RSA_DEBUG
454 print_krp_params(krp);
455 #endif
456 err = EOPNOTSUPP;
457 switch (krp->krp_op) {
458 case CRK_MOD_EXP:
459 if (krp->krp_iparams == 3 && krp->krp_oparams == 1)
460 break;
461 goto errout;
462 default:
463 device_printf(dev, "Op:%d not yet supported\n", krp->krp_op);
464 goto errout;
465 }
466
467 err = xlp_get_rsa_opsize(cmd,
468 krp->krp_param[krp->krp_iparams - 1].crp_nbits);
469 if (err != 0) {
470 err = EINVAL;
471 goto errout;
472 }
473 cmd->rsafn = 0; /* Mod Exp */
474 cmd->rsasrc = malloc(
475 cmd->rsaopsize * (krp->krp_iparams + krp->krp_oparams),
476 M_DEVBUF,
477 M_NOWAIT | M_ZERO);
478 if (cmd->rsasrc == NULL) {
479 err = ENOMEM;
480 goto errout;
481 }
482
483 for (i = 0, kp = krp->krp_param; i < krp->krp_iparams; i++, kp++) {
484 KASSERT(kp->crp_nbits != 0,
485 ("%s: parameter[%d]'s length is zero\n", __func__, i));
486 xlp_rsa_inp2hwformat(kp->crp_p,
487 cmd->rsasrc + i * cmd->rsaopsize,
488 howmany(kp->crp_nbits, 8), 0);
489 }
490 err = nlm_crypto_complete_rsa_request(sc, cmd);
491 if (err != 0)
492 goto errout;
493
494 return (0);
495 errout:
496 xlp_free_cmd_params(cmd);
497 krp->krp_status = err;
498 crypto_kdone(krp);
499 return (err);
500 }
501