o) Add the "octusb" controller which supports the first port of the Octeon

on-board USB controller.  It is not currently enabled because there are
   known problems with device communication and until those are fixed I am not
   certain that it won't destabilize the system. [1]
o) Add the "cryptocteon" opencrypto device based on the OCF device written by
   David McCullough.  It is not currently enabled because until support for
   saving/restoring coprocessor 2 state on context switch is available, it runs
   with interrupts disabled, which tends to pessimize performance over using a
   software crypto facility.  Tests using this driver which are not negatively
   affected by it running with interrupts disabled show it to be substantially
   faster than software for large blocks.

Submitted by:	hps [1]
This commit is contained in:
Juli Mallett 2010-07-20 19:32:25 +00:00
parent cea2b8b915
commit bfa39fa09f
7 changed files with 5142 additions and 0 deletions

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,526 @@
/*
* Octeon Crypto for OCF
*
* Written by David McCullough <david_mccullough@securecomputing.com>
* Copyright (C) 2009 David McCullough
*
* LICENSE TERMS
*
* The free distribution and use of this software in both source and binary
* form is allowed (with or without changes) provided that:
*
* 1. distributions of this source code include the above copyright
* notice, this list of conditions and the following disclaimer;
*
* 2. distributions in binary form include the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other associated materials;
*
* 3. the copyright holder's name is not used to endorse products
* built using this software without specific written permission.
*
* DISCLAIMER
*
* This software is provided 'as is' with no explicit or implied warranties
* in respect of its properties, including, but not limited to, correctness
* and/or fitness for purpose.
* ---------------------------------------------------------------------------
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/uio.h>
#include <opencrypto/cryptodev.h>
#include <contrib/octeon-sdk/cvmx.h>
#include <mips/cavium/cryptocteon/cryptocteonvar.h>
#include "cryptodev_if.h"
struct cryptocteon_softc {
int32_t sc_cid; /* opencrypto id */
struct octo_sess **sc_sessions;
uint32_t sc_sesnum;
};
int cryptocteon_debug = 0;
TUNABLE_INT("hw.cryptocteon.debug", &cryptocteon_debug);
static void cryptocteon_identify(driver_t *, device_t);
static int cryptocteon_probe(device_t);
static int cryptocteon_attach(device_t);
static int cryptocteon_process(device_t, struct cryptop *, int);
static int cryptocteon_newsession(device_t, u_int32_t *, struct cryptoini *);
static int cryptocteon_freesession(device_t, u_int64_t);
static void
cryptocteon_identify(driver_t *drv, device_t parent)
{
if (octeon_has_feature(OCTEON_FEATURE_CRYPTO))
BUS_ADD_CHILD(parent, 0, "cryptocteon", 0);
}
static int
cryptocteon_probe(device_t dev)
{
device_set_desc(dev, "Octeon Secure Coprocessor");
return (0);
}
static int
cryptocteon_attach(device_t dev)
{
struct cryptocteon_softc *sc;
sc = device_get_softc(dev);
sc->sc_cid = crypto_get_driverid(dev, CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SYNC);
if (sc->sc_cid < 0) {
device_printf(dev, "crypto_get_driverid ret %d\n", sc->sc_cid);
return (ENXIO);
}
crypto_register(sc->sc_cid, CRYPTO_MD5_HMAC, 0, 0);
crypto_register(sc->sc_cid, CRYPTO_SHA1_HMAC, 0, 0);
crypto_register(sc->sc_cid, CRYPTO_DES_CBC, 0, 0);
crypto_register(sc->sc_cid, CRYPTO_3DES_CBC, 0, 0);
crypto_register(sc->sc_cid, CRYPTO_AES_CBC, 0, 0);
return (0);
}
/*
* Generate a new octo session. We artifically limit it to a single
* hash/cipher or hash-cipher combo just to make it easier, most callers
* do not expect more than this anyway.
*/
static int
cryptocteon_newsession(device_t dev, u_int32_t *sid, struct cryptoini *cri)
{
struct cryptoini *c, *encini = NULL, *macini = NULL;
struct cryptocteon_softc *sc;
struct octo_sess **ocd;
int i;
sc = device_get_softc(dev);
if (sid == NULL || cri == NULL || sc == NULL)
return (EINVAL);
/*
* To keep it simple, we only handle hash, cipher or hash/cipher in a
* session, you cannot currently do multiple ciphers/hashes in one
* session even though it would be possibel to code this driver to
* handle it.
*/
for (i = 0, c = cri; c && i < 2; i++) {
if (c->cri_alg == CRYPTO_MD5_HMAC ||
c->cri_alg == CRYPTO_SHA1_HMAC ||
c->cri_alg == CRYPTO_NULL_HMAC) {
if (macini) {
break;
}
macini = c;
}
if (c->cri_alg == CRYPTO_DES_CBC ||
c->cri_alg == CRYPTO_3DES_CBC ||
c->cri_alg == CRYPTO_AES_CBC ||
c->cri_alg == CRYPTO_NULL_CBC) {
if (encini) {
break;
}
encini = c;
}
c = c->cri_next;
}
if (!macini && !encini) {
dprintf("%s,%d - EINVAL bad cipher/hash or combination\n",
__FILE__, __LINE__);
return EINVAL;
}
if (c) {
dprintf("%s,%d - EINVAL cannot handle chained cipher/hash combos\n",
__FILE__, __LINE__);
return EINVAL;
}
/*
* So we have something we can do, lets setup the session
*/
if (sc->sc_sessions) {
for (i = 1; i < sc->sc_sesnum; i++)
if (sc->sc_sessions[i] == NULL)
break;
} else
i = 1; /* NB: to silence compiler warning */
if (sc->sc_sessions == NULL || i == sc->sc_sesnum) {
if (sc->sc_sessions == NULL) {
i = 1; /* We leave sc->sc_sessions[0] empty */
sc->sc_sesnum = CRYPTO_SW_SESSIONS;
} else
sc->sc_sesnum *= 2;
ocd = malloc(sc->sc_sesnum * sizeof(struct octo_sess *),
M_DEVBUF, M_NOWAIT | M_ZERO);
if (ocd == NULL) {
/* Reset session number */
if (sc->sc_sesnum == CRYPTO_SW_SESSIONS)
sc->sc_sesnum = 0;
else
sc->sc_sesnum /= 2;
dprintf("%s,%d: ENOBUFS\n", __FILE__, __LINE__);
return ENOBUFS;
}
/* Copy existing sessions */
if (sc->sc_sessions) {
memcpy(ocd, sc->sc_sessions,
(sc->sc_sesnum / 2) * sizeof(struct octo_sess *));
free(sc->sc_sessions, M_DEVBUF);
}
sc->sc_sessions = ocd;
}
ocd = &sc->sc_sessions[i];
*sid = i;
*ocd = malloc(sizeof(struct octo_sess), M_DEVBUF, M_NOWAIT | M_ZERO);
if (*ocd == NULL) {
cryptocteon_freesession(NULL, i);
dprintf("%s,%d: ENOBUFS\n", __FILE__, __LINE__);
return ENOBUFS;
}
if (encini && encini->cri_key) {
(*ocd)->octo_encklen = (encini->cri_klen + 7) / 8;
memcpy((*ocd)->octo_enckey, encini->cri_key, (*ocd)->octo_encklen);
}
if (macini && macini->cri_key) {
(*ocd)->octo_macklen = (macini->cri_klen + 7) / 8;
memcpy((*ocd)->octo_mackey, macini->cri_key, (*ocd)->octo_macklen);
}
(*ocd)->octo_mlen = 0;
if (encini && encini->cri_mlen)
(*ocd)->octo_mlen = encini->cri_mlen;
else if (macini && macini->cri_mlen)
(*ocd)->octo_mlen = macini->cri_mlen;
else
(*ocd)->octo_mlen = 12;
/*
* point c at the enc if it exists, otherwise the mac
*/
c = encini ? encini : macini;
switch (c->cri_alg) {
case CRYPTO_DES_CBC:
case CRYPTO_3DES_CBC:
(*ocd)->octo_ivsize = 8;
switch (macini ? macini->cri_alg : -1) {
case CRYPTO_MD5_HMAC:
(*ocd)->octo_encrypt = octo_des_cbc_md5_encrypt;
(*ocd)->octo_decrypt = octo_des_cbc_md5_decrypt;
octo_calc_hash(0, macini->cri_key, (*ocd)->octo_hminner,
(*ocd)->octo_hmouter);
break;
case CRYPTO_SHA1_HMAC:
(*ocd)->octo_encrypt = octo_des_cbc_sha1_encrypt;
(*ocd)->octo_decrypt = octo_des_cbc_sha1_encrypt;
octo_calc_hash(1, macini->cri_key, (*ocd)->octo_hminner,
(*ocd)->octo_hmouter);
break;
case -1:
(*ocd)->octo_encrypt = octo_des_cbc_encrypt;
(*ocd)->octo_decrypt = octo_des_cbc_decrypt;
break;
default:
cryptocteon_freesession(NULL, i);
dprintf("%s,%d: EINVALn", __FILE__, __LINE__);
return EINVAL;
}
break;
case CRYPTO_AES_CBC:
(*ocd)->octo_ivsize = 16;
switch (macini ? macini->cri_alg : -1) {
case CRYPTO_MD5_HMAC:
(*ocd)->octo_encrypt = octo_aes_cbc_md5_encrypt;
(*ocd)->octo_decrypt = octo_aes_cbc_md5_decrypt;
octo_calc_hash(0, macini->cri_key, (*ocd)->octo_hminner,
(*ocd)->octo_hmouter);
break;
case CRYPTO_SHA1_HMAC:
(*ocd)->octo_encrypt = octo_aes_cbc_sha1_encrypt;
(*ocd)->octo_decrypt = octo_aes_cbc_sha1_decrypt;
octo_calc_hash(1, macini->cri_key, (*ocd)->octo_hminner,
(*ocd)->octo_hmouter);
break;
case -1:
(*ocd)->octo_encrypt = octo_aes_cbc_encrypt;
(*ocd)->octo_decrypt = octo_aes_cbc_decrypt;
break;
default:
cryptocteon_freesession(NULL, i);
dprintf("%s,%d: EINVALn", __FILE__, __LINE__);
return EINVAL;
}
break;
case CRYPTO_MD5_HMAC:
(*ocd)->octo_encrypt = octo_null_md5_encrypt;
(*ocd)->octo_decrypt = octo_null_md5_encrypt;
octo_calc_hash(0, macini->cri_key, (*ocd)->octo_hminner,
(*ocd)->octo_hmouter);
break;
case CRYPTO_SHA1_HMAC:
(*ocd)->octo_encrypt = octo_null_sha1_encrypt;
(*ocd)->octo_decrypt = octo_null_sha1_encrypt;
octo_calc_hash(1, macini->cri_key, (*ocd)->octo_hminner,
(*ocd)->octo_hmouter);
break;
default:
cryptocteon_freesession(NULL, i);
dprintf("%s,%d: EINVALn", __FILE__, __LINE__);
return EINVAL;
}
(*ocd)->octo_encalg = encini ? encini->cri_alg : -1;
(*ocd)->octo_macalg = macini ? macini->cri_alg : -1;
return 0;
}
/*
* Free a session.
*/
static int
cryptocteon_freesession(device_t dev, u_int64_t tid)
{
struct cryptocteon_softc *sc;
u_int32_t sid = CRYPTO_SESID2LID(tid);
sc = device_get_softc(dev);
if (sc == NULL)
return (EINVAL);
if (sid > sc->sc_sesnum || sc->sc_sessions == NULL ||
sc->sc_sessions[sid] == NULL)
return (EINVAL);
/* Silently accept and return */
if (sid == 0)
return(0);
if (sc->sc_sessions[sid])
free(sc->sc_sessions[sid], M_DEVBUF);
sc->sc_sessions[sid] = NULL;
return 0;
}
/*
* Process a request.
*/
static int
cryptocteon_process(device_t dev, struct cryptop *crp, int hint)
{
struct cryptodesc *crd;
struct octo_sess *od;
u_int32_t lid;
size_t iovcnt, iovlen;
struct mbuf *m = NULL;
struct uio *uiop = NULL;
struct cryptodesc *enccrd = NULL, *maccrd = NULL;
unsigned char *ivp = NULL;
unsigned char iv_data[HASH_MAX_LEN];
int auth_off = 0, auth_len = 0, crypt_off = 0, crypt_len = 0, icv_off = 0;
struct cryptocteon_softc *sc;
sc = device_get_softc(dev);
if (sc == NULL || crp == NULL)
return EINVAL;
crp->crp_etype = 0;
if (crp->crp_desc == NULL || crp->crp_buf == NULL) {
dprintf("%s,%d: EINVAL\n", __FILE__, __LINE__);
crp->crp_etype = EINVAL;
goto done;
}
lid = crp->crp_sid & 0xffffffff;
if (lid >= sc->sc_sesnum || lid == 0 || sc->sc_sessions == NULL ||
sc->sc_sessions[lid] == NULL) {
crp->crp_etype = ENOENT;
dprintf("%s,%d: ENOENT\n", __FILE__, __LINE__);
goto done;
}
od = sc->sc_sessions[lid];
/*
* do some error checking outside of the loop for m and IOV processing
* this leaves us with valid m or uiop pointers for later
*/
if (crp->crp_flags & CRYPTO_F_IMBUF) {
unsigned frags;
m = (struct mbuf *) crp->crp_buf;
for (frags = 0; m != NULL; frags++)
m = m->m_next;
if (frags >= UIO_MAXIOV) {
printf("%s,%d: %d frags > UIO_MAXIOV", __FILE__, __LINE__, frags);
goto done;
}
m = (struct mbuf *) crp->crp_buf;
} else if (crp->crp_flags & CRYPTO_F_IOV) {
uiop = (struct uio *) crp->crp_buf;
if (uiop->uio_iovcnt > UIO_MAXIOV) {
printf("%s,%d: %d uio_iovcnt > UIO_MAXIOV", __FILE__, __LINE__,
uiop->uio_iovcnt);
goto done;
}
}
/* point our enccrd and maccrd appropriately */
crd = crp->crp_desc;
if (crd->crd_alg == od->octo_encalg) enccrd = crd;
if (crd->crd_alg == od->octo_macalg) maccrd = crd;
crd = crd->crd_next;
if (crd) {
if (crd->crd_alg == od->octo_encalg) enccrd = crd;
if (crd->crd_alg == od->octo_macalg) maccrd = crd;
crd = crd->crd_next;
}
if (crd) {
crp->crp_etype = EINVAL;
dprintf("%s,%d: ENOENT - descriptors do not match session\n",
__FILE__, __LINE__);
goto done;
}
if (enccrd) {
if (enccrd->crd_flags & CRD_F_IV_EXPLICIT) {
ivp = enccrd->crd_iv;
} else {
ivp = iv_data;
crypto_copydata(crp->crp_flags, crp->crp_buf,
enccrd->crd_inject, od->octo_ivsize, (caddr_t) ivp);
}
if (maccrd) {
auth_off = maccrd->crd_skip;
auth_len = maccrd->crd_len;
icv_off = maccrd->crd_inject;
}
crypt_off = enccrd->crd_skip;
crypt_len = enccrd->crd_len;
} else { /* if (maccrd) */
auth_off = maccrd->crd_skip;
auth_len = maccrd->crd_len;
icv_off = maccrd->crd_inject;
}
/*
* setup the I/O vector to cover the buffer
*/
if (crp->crp_flags & CRYPTO_F_IMBUF) {
iovcnt = 0;
iovlen = 0;
while (m != NULL) {
od->octo_iov[iovcnt].iov_base = mtod(m, void *);
od->octo_iov[iovcnt].iov_len = m->m_len;
m = m->m_next;
iovlen += od->octo_iov[iovcnt++].iov_len;
}
} else if (crp->crp_flags & CRYPTO_F_IOV) {
iovlen = 0;
for (iovcnt = 0; iovcnt < uiop->uio_iovcnt; iovcnt++) {
od->octo_iov[iovcnt].iov_base = uiop->uio_iov[iovcnt].iov_base;
od->octo_iov[iovcnt].iov_len = uiop->uio_iov[iovcnt].iov_len;
iovlen += od->octo_iov[iovcnt].iov_len;
}
} else {
iovlen = crp->crp_ilen;
od->octo_iov[0].iov_base = crp->crp_buf;
od->octo_iov[0].iov_len = crp->crp_ilen;
iovcnt = 1;
}
/*
* setup a new explicit key
*/
if (enccrd) {
if (enccrd->crd_flags & CRD_F_KEY_EXPLICIT) {
od->octo_encklen = (enccrd->crd_klen + 7) / 8;
memcpy(od->octo_enckey, enccrd->crd_key, od->octo_encklen);
}
}
if (maccrd) {
if (maccrd->crd_flags & CRD_F_KEY_EXPLICIT) {
od->octo_macklen = (maccrd->crd_klen + 7) / 8;
memcpy(od->octo_mackey, maccrd->crd_key, od->octo_macklen);
od->octo_mackey_set = 0;
}
if (!od->octo_mackey_set) {
octo_calc_hash(maccrd->crd_alg == CRYPTO_MD5_HMAC ? 0 : 1,
maccrd->crd_key, od->octo_hminner, od->octo_hmouter);
od->octo_mackey_set = 1;
}
}
if (!enccrd || (enccrd->crd_flags & CRD_F_ENCRYPT))
(*od->octo_encrypt)(od, od->octo_iov, iovcnt, iovlen,
auth_off, auth_len, crypt_off, crypt_len, icv_off, ivp);
else
(*od->octo_decrypt)(od, od->octo_iov, iovcnt, iovlen,
auth_off, auth_len, crypt_off, crypt_len, icv_off, ivp);
done:
crypto_done(crp);
return 0;
}
static device_method_t cryptocteon_methods[] = {
/* device methods */
DEVMETHOD(device_identify, cryptocteon_identify),
DEVMETHOD(device_probe, cryptocteon_probe),
DEVMETHOD(device_attach, cryptocteon_attach),
/* crypto device methods */
DEVMETHOD(cryptodev_newsession, cryptocteon_newsession),
DEVMETHOD(cryptodev_freesession,cryptocteon_freesession),
DEVMETHOD(cryptodev_process, cryptocteon_process),
{ 0, 0 }
};
static driver_t cryptocteon_driver = {
"cryptocteon",
cryptocteon_methods,
sizeof (struct cryptocteon_softc),
};
static devclass_t cryptocteon_devclass;
DRIVER_MODULE(cryptocteon, nexus, cryptocteon_driver, cryptocteon_devclass, 0, 0);

View file

@ -0,0 +1,94 @@
/*
* Octeon Crypto for OCF
*
* Written by David McCullough <david_mccullough@securecomputing.com>
* Copyright (C) 2009 David McCullough
*
* LICENSE TERMS
*
* The free distribution and use of this software in both source and binary
* form is allowed (with or without changes) provided that:
*
* 1. distributions of this source code include the above copyright
* notice, this list of conditions and the following disclaimer;
*
* 2. distributions in binary form include the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other associated materials;
*
* 3. the copyright holder's name is not used to endorse products
* built using this software without specific written permission.
*
* DISCLAIMER
*
* This software is provided 'as is' with no explicit or implied warranties
* in respect of its properties, including, but not limited to, correctness
* and/or fitness for purpose.
* ---------------------------------------------------------------------------
*
* $FreeBSD$
*/
#ifndef _MIPS_CAVIUM_CRYPTOCTEON_CRYPTOCTEONVAR_H_
#define _MIPS_CAVIUM_CRYPTOCTEON_CRYPTOCTEONVAR_H_
struct octo_sess;
typedef int octo_encrypt_t(struct octo_sess *od, struct iovec *iov, size_t iovcnt, size_t iovlen, int auth_off, int auth_len, int crypt_off, int crypt_len, int icv_off, uint8_t *ivp);
typedef int octo_decrypt_t(struct octo_sess *od, struct iovec *iov, size_t iovcnt, size_t iovlen, int auth_off, int auth_len, int crypt_off, int crypt_len, int icv_off, uint8_t *ivp);
struct octo_sess {
int octo_encalg;
#define MAX_CIPHER_KEYLEN 64
char octo_enckey[MAX_CIPHER_KEYLEN];
int octo_encklen;
int octo_macalg;
#define MAX_HASH_KEYLEN 64
char octo_mackey[MAX_HASH_KEYLEN];
int octo_macklen;
int octo_mackey_set;
int octo_mlen;
int octo_ivsize;
octo_encrypt_t *octo_encrypt;
octo_decrypt_t *octo_decrypt;
uint64_t octo_hminner[3];
uint64_t octo_hmouter[3];
struct iovec octo_iov[UIO_MAXIOV];
};
#define dprintf(fmt, ...) \
do { \
if (cryptocteon_debug) \
printf("%s: " fmt, __func__, ## __VA_ARGS__); \
} while (0)
extern int cryptocteon_debug;
void octo_calc_hash(uint8_t, unsigned char *, uint64_t *, uint64_t *);
/* XXX Actually just hashing functions, not encryption. */
octo_encrypt_t octo_null_md5_encrypt;
octo_encrypt_t octo_null_sha1_encrypt;
octo_encrypt_t octo_des_cbc_encrypt;
octo_encrypt_t octo_des_cbc_md5_encrypt;
octo_encrypt_t octo_des_cbc_sha1_encrypt;
octo_decrypt_t octo_des_cbc_decrypt;
octo_decrypt_t octo_des_cbc_md5_decrypt;
octo_decrypt_t octo_des_cbc_sha1_decrypt;
octo_encrypt_t octo_aes_cbc_encrypt;
octo_encrypt_t octo_aes_cbc_md5_encrypt;
octo_encrypt_t octo_aes_cbc_sha1_encrypt;
octo_decrypt_t octo_aes_cbc_decrypt;
octo_decrypt_t octo_aes_cbc_md5_decrypt;
octo_decrypt_t octo_aes_cbc_sha1_decrypt;
#endif /* !_MIPS_CAVIUM_CRYPTOCTEON_CRYPTOCTEONVAR_H_ */

View file

@ -17,6 +17,9 @@ mips/mips/tick.c standard
mips/cavium/octeon_rnd.c optional random
mips/cavium/cryptocteon/cavium_crypto.c optional cryptocteon
mips/cavium/cryptocteon/cryptocteon.c optional cryptocteon
mips/cavium/octe/ethernet.c optional octe
mips/cavium/octe/ethernet-common.c optional octe
mips/cavium/octe/ethernet-mdio.c optional octe
@ -33,6 +36,9 @@ mips/cavium/octe/octebus.c optional octe
mips/cavium/octopci.c optional pci
mips/cavium/octopci_bus_space.c optional pci
mips/cavium/usb/octusb.c optional usb octusb
mips/cavium/usb/octusb_octeon.c optional usb octusb
contrib/octeon-sdk/cvmx-cmd-queue.c optional octe
contrib/octeon-sdk/cvmx-fpa.c optional octe
contrib/octeon-sdk/cvmx-helper.c optional octe
@ -51,6 +57,8 @@ contrib/octeon-sdk/cvmx-spi.c optional octe
contrib/octeon-sdk/cvmx-spi4000.c optional octe
contrib/octeon-sdk/cvmx-twsi.c optional octe
contrib/octeon-sdk/cvmx-usb.c optional octusb
# XXX Some files could be excluded in some configurations. Making them
# optional but on in the default config would seem reasonable.
contrib/octeon-sdk/cvmx-bootmem.c standard

1922
sys/mips/cavium/usb/octusb.c Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,147 @@
/* $FreeBSD$ */
/*-
* Copyright (c) 2010 Hans Petter Selasky. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _OCTUSB_H_
#define _OCTUSB_H_
#define OCTUSB_MAX_DEVICES MIN(USB_MAX_DEVICES, 64)
/*
* The second port is on a different IRQ and so we disable it for now.
*/
#if 1
#define OCTUSB_MAX_PORTS 1 /* hardcoded */
#else
#define OCTUSB_MAX_PORTS 2 /* hardcoded */
#endif
#define OCTUSB_MAX_FIXUP 4096 /* bytes */
#define OCTUSB_INTR_ENDPT 0x01
struct octusb_qh;
struct octusb_td;
struct octusb_softc;
typedef uint8_t (octusb_cmd_t)(struct octusb_td *td);
struct octusb_td {
struct octusb_qh *qh;
struct octusb_td *obj_next;
struct usb_page_cache *pc;
octusb_cmd_t *func;
uint32_t remainder;
uint32_t offset;
uint8_t error_any:1;
uint8_t error_stall:1;
uint8_t short_pkt:1;
uint8_t alt_next:1;
uint8_t reserved:4;
};
struct octusb_qh {
uint64_t fixup_phys;
struct octusb_softc *sc;
struct usb_page_cache *fixup_pc;
uint8_t *fixup_buf;
cvmx_usb_iso_packet_t iso_pkt;
uint32_t fixup_off;
uint16_t max_frame_size;
uint16_t max_packet_size;
uint16_t fixup_actlen;
uint16_t fixup_len;
uint16_t ep_interval;
uint8_t dev_addr;
uint8_t dev_speed;
uint8_t ep_allocated;
uint8_t ep_mult;
uint8_t ep_num;
uint8_t ep_type;
uint8_t ep_toggle_next;
uint8_t port_index;
uint8_t fixup_complete;
uint8_t fixup_pending;
uint8_t hs_hub_addr;
uint8_t hs_hub_port;
int fixup_handle;
int ep_handle;
};
struct octusb_config_desc {
struct usb_config_descriptor confd;
struct usb_interface_descriptor ifcd;
struct usb_endpoint_descriptor endpd;
} __packed;
union octusb_hub_desc {
struct usb_status stat;
struct usb_port_status ps;
uint8_t temp[128];
};
struct octusb_port {
cvmx_usb_state_t state;
uint8_t disabled;
};
struct octusb_softc {
struct usb_bus sc_bus; /* base device */
union octusb_hub_desc sc_hub_desc;
struct usb_device *sc_devices[OCTUSB_MAX_DEVICES];
struct resource *sc_irq_res;
void *sc_intr_hdl;
struct octusb_port sc_port[OCTUSB_MAX_PORTS];
device_t sc_dev;
struct usb_hub_descriptor_min sc_hubd;
uint8_t sc_noport; /* number of ports */
uint8_t sc_addr; /* device address */
uint8_t sc_conf; /* device configuration */
uint8_t sc_isreset; /* set if current port is reset */
uint8_t sc_hub_idata[1];
};
usb_bus_mem_cb_t octusb_iterate_hw_softc;
usb_error_t octusb_init(struct octusb_softc *);
usb_error_t octusb_uninit(struct octusb_softc *);
void octusb_suspend(struct octusb_softc *);
void octusb_resume(struct octusb_softc *);
void octusb_interrupt(struct octusb_softc *);
#endif /* _OCTUSB_H_ */

View file

@ -0,0 +1,223 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
/*-
* Copyright (c) 2007-2008 Hans Petter Selasky. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/stdint.h>
#include <sys/stddef.h>
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/linker_set.h>
#include <sys/module.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/sysctl.h>
#include <sys/sx.h>
#include <sys/unistd.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/priv.h>
#include <sys/rman.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usb_core.h>
#include <dev/usb/usb_busdma.h>
#include <dev/usb/usb_process.h>
#include <dev/usb/usb_util.h>
#include <dev/usb/usb_controller.h>
#include <dev/usb/usb_bus.h>
#include <contrib/octeon-sdk/cvmx.h>
#include <contrib/octeon-sdk/cvmx-interrupt.h>
#include <contrib/octeon-sdk/cvmx-usb.h>
#include <mips/cavium/usb/octusb.h>
#define MEM_RID 0
static device_identify_t octusb_octeon_identify;
static device_probe_t octusb_octeon_probe;
static device_attach_t octusb_octeon_attach;
static device_detach_t octusb_octeon_detach;
static device_shutdown_t octusb_octeon_shutdown;
struct octusb_octeon_softc {
struct octusb_softc sc_dci; /* must be first */
};
static void
octusb_octeon_identify(driver_t *drv, device_t parent)
{
if (octeon_has_feature(OCTEON_FEATURE_USB))
BUS_ADD_CHILD(parent, 0, "octusb", 0);
}
static int
octusb_octeon_probe(device_t dev)
{
device_set_desc(dev, "Cavium Octeon USB controller");
return (0);
}
static int
octusb_octeon_attach(device_t dev)
{
struct octusb_octeon_softc *sc = device_get_softc(dev);
int err;
int rid;
/* setup controller interface softc */
/* initialise some bus fields */
sc->sc_dci.sc_bus.parent = dev;
sc->sc_dci.sc_bus.devices = sc->sc_dci.sc_devices;
sc->sc_dci.sc_bus.devices_max = OCTUSB_MAX_DEVICES;
/* get all DMA memory */
if (usb_bus_mem_alloc_all(&sc->sc_dci.sc_bus,
USB_GET_DMA_TAG(dev), NULL)) {
return (ENOMEM);
}
rid = 0;
sc->sc_dci.sc_irq_res =
bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
CVMX_IRQ_USB, CVMX_IRQ_USB, 1, RF_ACTIVE);
if (!(sc->sc_dci.sc_irq_res)) {
goto error;
}
sc->sc_dci.sc_bus.bdev = device_add_child(dev, "usbus", -1);
if (!(sc->sc_dci.sc_bus.bdev)) {
goto error;
}
device_set_ivars(sc->sc_dci.sc_bus.bdev, &sc->sc_dci.sc_bus);
#if (__FreeBSD_version >= 700031)
err = bus_setup_intr(dev, sc->sc_dci.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
NULL, (driver_intr_t *)octusb_interrupt, sc, &sc->sc_dci.sc_intr_hdl);
#else
err = bus_setup_intr(dev, sc->sc_dci.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
(driver_intr_t *)octusb_interrupt, sc, &sc->sc_dci.sc_intr_hdl);
#endif
if (err) {
sc->sc_dci.sc_intr_hdl = NULL;
goto error;
}
err = octusb_init(&sc->sc_dci);
if (!err) {
err = device_probe_and_attach(sc->sc_dci.sc_bus.bdev);
}
if (err) {
goto error;
}
return (0);
error:
octusb_octeon_detach(dev);
return (ENXIO);
}
static int
octusb_octeon_detach(device_t dev)
{
struct octusb_octeon_softc *sc = device_get_softc(dev);
device_t bdev;
int err;
if (sc->sc_dci.sc_bus.bdev) {
bdev = sc->sc_dci.sc_bus.bdev;
device_detach(bdev);
device_delete_child(dev, bdev);
}
/* during module unload there are lots of children leftover */
device_delete_all_children(dev);
if (sc->sc_dci.sc_irq_res && sc->sc_dci.sc_intr_hdl) {
/*
* only call octusb_octeon_uninit() after octusb_octeon_init()
*/
octusb_uninit(&sc->sc_dci);
err = bus_teardown_intr(dev, sc->sc_dci.sc_irq_res,
sc->sc_dci.sc_intr_hdl);
sc->sc_dci.sc_intr_hdl = NULL;
}
if (sc->sc_dci.sc_irq_res) {
bus_release_resource(dev, SYS_RES_IRQ, 0,
sc->sc_dci.sc_irq_res);
sc->sc_dci.sc_irq_res = NULL;
}
usb_bus_mem_free_all(&sc->sc_dci.sc_bus, NULL);
return (0);
}
static int
octusb_octeon_shutdown(device_t dev)
{
struct octusb_octeon_softc *sc = device_get_softc(dev);
int err;
err = bus_generic_shutdown(dev);
if (err)
return (err);
octusb_uninit(&sc->sc_dci);
return (0);
}
static device_method_t octusb_octeon_methods[] = {
/* Device interface */
DEVMETHOD(device_identify, octusb_octeon_identify),
DEVMETHOD(device_probe, octusb_octeon_probe),
DEVMETHOD(device_attach, octusb_octeon_attach),
DEVMETHOD(device_detach, octusb_octeon_detach),
DEVMETHOD(device_shutdown, octusb_octeon_shutdown),
/* Bus interface */
DEVMETHOD(bus_print_child, bus_generic_print_child),
{0, 0}
};
static driver_t octusb_octeon_driver = {
"octusb",
octusb_octeon_methods,
sizeof(struct octusb_octeon_softc),
};
static devclass_t octusb_octeon_devclass;
DRIVER_MODULE(octusb, ciu, octusb_octeon_driver, octusb_octeon_devclass, 0, 0);