diff --git a/sys/mips/nlm/dev/sec/nlmrsa.c b/sys/mips/nlm/dev/sec/nlmrsa.c index 2438fdf1cfd..72e478bc9f1 100644 --- a/sys/mips/nlm/dev/sec/nlmrsa.c +++ b/sys/mips/nlm/dev/sec/nlmrsa.c @@ -62,14 +62,14 @@ __FBSDID("$FreeBSD$"); #include #include #include -#include #include #include #include #include +#include #ifdef NLM_RSA_DEBUG -int print_krp_params(struct cryptkop *krp); +static int print_krp_params(struct cryptkop *krp); #endif static int xlp_rsa_init(struct xlp_rsa_softc *sc, int node); @@ -97,7 +97,7 @@ static device_method_t xlp_rsa_methods[] = { /* crypto device methods */ DEVMETHOD(cryptodev_newsession, xlp_rsa_newsession), - DEVMETHOD(cryptodev_freesession,xlp_rsa_freesession), + DEVMETHOD(cryptodev_freesession, xlp_rsa_freesession), DEVMETHOD(cryptodev_kprocess, xlp_rsa_kprocess), DEVMETHOD_END @@ -113,29 +113,25 @@ static devclass_t xlp_rsa_devclass; DRIVER_MODULE(nlmrsa, pci, xlp_rsa_driver, xlp_rsa_devclass, 0, 0); MODULE_DEPEND(nlmrsa, crypto, 1, 1, 1); -void -nlm_xlprsaecc_msgring_handler(int vc, int size, int code, int src_id, - struct nlm_fmn_msg *msg, void *data); - #ifdef NLM_RSA_DEBUG -int +static int print_krp_params(struct cryptkop *krp) { int i; - printf("krp->krp_op :%d\n",krp->krp_op); - printf("krp->krp_status :%d\n",krp->krp_status); - printf("krp->krp_iparams:%d\n",krp->krp_iparams); - printf("krp->krp_oparams:%d\n",krp->krp_oparams); - for (i=0;ikrp_iparams+krp->krp_oparams;i++) { - printf("krp->krp_param[%d].crp_p :0x%llx\n",i, + printf("krp->krp_op :%d\n", krp->krp_op); + printf("krp->krp_status :%d\n", krp->krp_status); + printf("krp->krp_iparams:%d\n", krp->krp_iparams); + printf("krp->krp_oparams:%d\n", krp->krp_oparams); + for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) { + printf("krp->krp_param[%d].crp_p :0x%llx\n", i, (unsigned long long)krp->krp_param[i].crp_p); - printf("krp->krp_param[%d].crp_nbits :%d\n",i, + printf("krp->krp_param[%d].crp_nbits :%d\n", i, krp->krp_param[i].crp_nbits); - printf("krp->krp_param[%d].crp_nbytes :%d\n",i, - (krp->krp_param[i].crp_nbits+7)/8); + printf("krp->krp_param[%d].crp_nbytes :%d\n", i, + howmany(krp->krp_param[i].crp_nbits, 8)); } - return 0; + return (0); } #endif @@ -155,19 +151,20 @@ xlp_rsa_init(struct xlp_rsa_softc *sc, int node) printf("Couldn't register rsa/ecc msgring handler\n"); goto errout; } - m.msg[0] = m.msg[1] = m.msg[2] = m.msg[3] = 0; fbvc = nlm_cpuid() / CMS_MAX_VCPU_VC; /* Do the CMS credit initialization */ /* Currently it is configured by default to 50 when kernel comes up */ - if ((cmd = malloc(sizeof(struct xlp_rsa_command), M_DEVBUF, - M_NOWAIT | M_ZERO)) == NULL) { + cmd = malloc(sizeof(struct xlp_rsa_command), M_DEVBUF, + M_NOWAIT | M_ZERO); + if (cmd == NULL) { err = ENOMEM; printf("Failed to allocate mem for cmd\n"); goto errout; } size = sizeof(nlm_rsa_ucode_data); - if ((cmd->rsasrc = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO)) == NULL) { + cmd->rsasrc = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO); + if (cmd->rsasrc == NULL) { err = ENOMEM; printf("Failed to allocate mem for cmd->rsasrc\n"); goto errout; @@ -184,6 +181,7 @@ xlp_rsa_init(struct xlp_rsa_softc *sc, int node) vtophys(cmd->rsasrc)); /* Software scratch pad */ m.msg[2] = (uintptr_t)cmd; + m.msg[3] = 0; for (dstvc = sc->rsaecc_vc_start; dstvc <= sc->rsaecc_vc_end; dstvc++) { ret = nlm_fmn_msgsend(dstvc, 3, FMN_SWCODE_RSA, &m); @@ -215,38 +213,38 @@ void nlm_xlprsaecc_msgring_handler(int vc, int size, int code, int src_id, struct nlm_fmn_msg *msg, void *data) { - struct xlp_rsa_command *cmd = NULL; - struct xlp_rsa_softc *sc = NULL; + struct xlp_rsa_command *cmd; + struct xlp_rsa_softc *sc; + struct crparam *outparam; + int ostart; KASSERT(code == FMN_SWCODE_RSA, - ("%s: bad code = %d, expected code = %d\n", __FUNCTION__, code, + ("%s: bad code = %d, expected code = %d\n", __func__, code, FMN_SWCODE_RSA)); - sc = (struct xlp_rsa_softc *)data; + sc = data; KASSERT(src_id >= sc->rsaecc_vc_start && src_id <= sc->rsaecc_vc_end, - ("%s: bad src_id = %d, expect %d - %d\n", __FUNCTION__, + ("%s: bad src_id = %d, expect %d - %d\n", __func__, src_id, sc->rsaecc_vc_start, sc->rsaecc_vc_end)); cmd = (struct xlp_rsa_command *)(uintptr_t)msg->msg[1]; - KASSERT(cmd != NULL, ("%s:cmd not received properly\n", - __FUNCTION__)); + KASSERT(cmd != NULL, ("%s:cmd not received properly\n", __func__)); KASSERT(RSA_ERROR(msg->msg[0]) == 0, - ("%s: Message rcv msg0 %llx msg1 %llx err %x \n", __FUNCTION__, + ("%s: Message rcv msg0 %llx msg1 %llx err %x \n", __func__, (unsigned long long)msg->msg[0], (unsigned long long)msg->msg[1], (int)RSA_ERROR(msg->msg[0]))); - xlp_rsa_inp2hwformat(((uint8_t *)cmd->rsasrc+ - (cmd->rsaopsize*cmd->krp->krp_iparams)), - cmd->krp->krp_param[cmd->krp->krp_iparams].crp_p, - ((cmd->krp->krp_param[cmd->krp->krp_iparams].crp_nbits+7)/8), 1); - + ostart = cmd->krp->krp_iparams; + outparam = &cmd->krp->krp_param[ostart]; + xlp_rsa_inp2hwformat(cmd->rsasrc + cmd->rsaopsize * ostart, + outparam->crp_p, + howmany(outparam->crp_nbits, 8), + 1); if (cmd->krp != NULL) crypto_kdone(cmd->krp); xlp_free_cmd_params(cmd); - - return; } static int @@ -302,7 +300,6 @@ xlp_rsa_attach(device_t dev) error_exit: return (ENXIO); - } /* @@ -323,8 +320,8 @@ static int xlp_rsa_newsession(device_t dev, u_int32_t *sidp, struct cryptoini *cri) { struct xlp_rsa_softc *sc = device_get_softc(dev); - int sesn; struct xlp_rsa_session *ses = NULL; + int sesn; if (sidp == NULL || cri == NULL || sc == NULL) return (EINVAL); @@ -346,7 +343,7 @@ xlp_rsa_newsession(device_t dev, u_int32_t *sidp, struct cryptoini *cri) if (ses == NULL) { sesn = sc->sc_nsessions; - ses = malloc((sesn + 1)*sizeof(struct xlp_rsa_session), + ses = malloc((sesn + 1) * sizeof(*ses), M_DEVBUF, M_NOWAIT); if (ses == NULL) return (ENOMEM); @@ -392,16 +389,17 @@ xlp_rsa_freesession(device_t dev, u_int64_t tid) static void xlp_free_cmd_params(struct xlp_rsa_command *cmd) { + if (cmd->rsasrc != NULL) free(cmd->rsasrc, M_DEVBUF); if (cmd != NULL) free(cmd, M_DEVBUF); - return; } static int xlp_get_rsa_opsize(struct xlp_rsa_command *cmd, unsigned int bits) { + if (bits == 0) return (-1); /* XLP hardware expects always a fixed size with unused bytes @@ -436,25 +434,25 @@ xlp_rsa_inp2hwformat(uint8_t *src, uint8_t *dst, uint32_t paramsize, uint8_t result) { uint32_t pdwords, pbytes; - int i=0, j=0, k=0; + int i, j, k; - pdwords = (paramsize / 8); - pbytes = (paramsize % 8); + pdwords = paramsize / 8; + pbytes = paramsize % 8; for (i = 0, k = 0; i < pdwords; i++) { /* copy dwords of inp/hw to hw/out format */ for (j = 7; j >= 0; j--, k++) - dst[(i*8)+j] = src[k]; + dst[i * 8 + j] = src[k]; } if (pbytes) { - if (!result) { + if (result == 0) { /* copy rem bytes of input data to hw format */ for (j = 7; k < paramsize; j--, k++) - dst[(i*8)+j] = src[k]; + dst[i * 8 + j] = src[k]; } else { /* copy rem bytes of hw data to exp output format */ for (j = 7; k < paramsize; j--, k++) - dst[k] = src[(i*8)+j]; + dst[k] = src[i * 8 + j]; } } @@ -470,14 +468,13 @@ nlm_crypto_complete_rsa_request(struct xlp_rsa_softc *sc, int ret; fbvc = nlm_cpuid() / CMS_MAX_VCPU_VC; - m.msg[0] = m.msg[1] = m.msg[2] = m.msg[3] = 0; - m.msg[0] = nlm_crypto_form_rsa_ecc_fmn_entry0(1, cmd->rsatype, cmd->rsafn, vtophys(cmd->rsasrc)); m.msg[1] = nlm_crypto_form_rsa_ecc_fmn_entry1(0, 1, fbvc, - vtophys(cmd->rsasrc + (cmd->rsaopsize * cmd->krp->krp_iparams))); + vtophys(cmd->rsasrc + cmd->rsaopsize * cmd->krp->krp_iparams)); /* Software scratch pad */ m.msg[2] = (uintptr_t)cmd; + m.msg[3] = 0; /* Send the message to rsa engine vc */ ret = nlm_fmn_msgsend(sc->rsaecc_vc_start, 3, FMN_SWCODE_RSA, &m); @@ -494,14 +491,16 @@ static int xlp_rsa_kprocess(device_t dev, struct cryptkop *krp, int hint) { struct xlp_rsa_softc *sc = device_get_softc(dev); - struct xlp_rsa_command *cmd = NULL; + struct xlp_rsa_command *cmd; + struct crparam *kp; int err = -1, i; if (krp == NULL || krp->krp_callback == NULL) return (EINVAL); - if ((cmd = malloc(sizeof(struct xlp_rsa_command), M_DEVBUF, - M_NOWAIT | M_ZERO)) == NULL) { + cmd = malloc(sizeof(struct xlp_rsa_command), M_DEVBUF, + M_NOWAIT | M_ZERO); + if (cmd == NULL) { err = ENOMEM; goto errout; } @@ -516,19 +515,22 @@ xlp_rsa_kprocess(device_t dev, struct cryptkop *krp, int hint) break; goto errout; default: - printf("Op:%d not yet supported\n", krp->krp_op); + device_printf(dev, "Op:%d not yet supported\n", krp->krp_op); goto errout; } - if ((xlp_get_rsa_opsize(cmd, - krp->krp_param[krp->krp_iparams-1].crp_nbits)) != 0) { + err = xlp_get_rsa_opsize(cmd, + krp->krp_param[krp->krp_iparams - 1].crp_nbits); + if (err != 0) { err = EINVAL; goto errout; } cmd->rsafn = 0; /* Mod Exp */ - if ((cmd->rsasrc = malloc((cmd->rsaopsize * - (krp->krp_iparams+krp->krp_oparams)), M_DEVBUF, - M_NOWAIT | M_ZERO)) == NULL) { + cmd->rsasrc = malloc( + cmd->rsaopsize * (krp->krp_iparams + krp->krp_oparams), + M_DEVBUF, + M_NOWAIT | M_ZERO); + if (cmd->rsasrc == NULL) { err = ENOMEM; goto errout; } @@ -536,13 +538,12 @@ xlp_rsa_kprocess(device_t dev, struct cryptkop *krp, int hint) err = EINVAL; goto errout; } - - for (i=0;ikrp_iparams;i++) { - KASSERT(krp->krp_param[i].crp_nbits != 0, - ("%s: parameter[%d]'s length is zero\n", __FUNCTION__, i)); - xlp_rsa_inp2hwformat(krp->krp_param[i].crp_p, - ((uint8_t *)cmd->rsasrc+(i*cmd->rsaopsize)), - ((krp->krp_param[i].crp_nbits+7)/8), 0); + for (i = 0, kp = krp->krp_param; i < krp->krp_iparams; i++, kp++) { + KASSERT(kp->crp_nbits != 0, + ("%s: parameter[%d]'s length is zero\n", __func__, i)); + xlp_rsa_inp2hwformat(kp->crp_p, + cmd->rsasrc + i * cmd->rsaopsize, + howmany(kp->crp_nbits, 8), 0); } if (nlm_crypto_complete_rsa_request(sc, cmd) != 0) goto errout; diff --git a/sys/mips/nlm/dev/sec/nlmrsalib.h b/sys/mips/nlm/dev/sec/nlmrsalib.h index 24c42e13479..03c470f8253 100644 --- a/sys/mips/nlm/dev/sec/nlmrsalib.h +++ b/sys/mips/nlm/dev/sec/nlmrsalib.h @@ -30,12 +30,10 @@ #ifndef _NLMRSALIB_H_ #define _NLMRSALIB_H_ -#define XLP_RSA_SESSION(sid) ((sid) & 0x000007ff) -#define XLP_RSA_SID(crd,ses) (((crd) << 28) | ((ses) & 0x7ff)) +#define XLP_RSA_SESSION(sid) ((sid) & 0x000007ff) +#define XLP_RSA_SID(crd,ses) (((crd) << 28) | ((ses) & 0x7ff)) -#define NUM_RSAECC_VC 9 - -#define RSA_ERROR(__msg0) ((__msg0 >> 53) & 0x1f) +#define RSA_ERROR(msg0) (((msg0) >> 53) & 0x1f) struct xlp_rsa_session { uint32_t sessionid; @@ -65,4 +63,8 @@ struct xlp_rsa_softc { int rsaecc_vc_end; }; +void +nlm_xlprsaecc_msgring_handler(int vc, int size, int code, int src_id, + struct nlm_fmn_msg *msg, void *data); + #endif /* _NLMRSALIB_H_ */