diff --git a/bsps/powerpc/beatnik/headers.am b/bsps/powerpc/beatnik/headers.am index 36df3cd7598..8416bbd626b 100644 --- a/bsps/powerpc/beatnik/headers.am +++ b/bsps/powerpc/beatnik/headers.am @@ -20,3 +20,4 @@ include_bsp_HEADERS += ../../../../../../bsps/powerpc/beatnik/include/bsp/if_em_ include_bsp_HEADERS += ../../../../../../bsps/powerpc/beatnik/include/bsp/if_gfe_pub.h include_bsp_HEADERS += ../../../../../../bsps/powerpc/beatnik/include/bsp/if_mve_pub.h include_bsp_HEADERS += ../../../../../../bsps/powerpc/beatnik/include/bsp/irq.h +include_bsp_HEADERS += ../../../../../../bsps/powerpc/beatnik/include/bsp/mv643xx_eth.h diff --git a/bsps/powerpc/beatnik/include/bsp/if_mve_pub.h b/bsps/powerpc/beatnik/include/bsp/if_mve_pub.h index 46cc4ccd234..b9f0c7101ba 100644 --- a/bsps/powerpc/beatnik/include/bsp/if_mve_pub.h +++ b/bsps/powerpc/beatnik/include/bsp/if_mve_pub.h @@ -1,19 +1,19 @@ #ifndef RTEMS_BSDNET_IF_MVE_PUBLIC_SYMBOLS_H #define RTEMS_BSDNET_IF_MVE_PUBLIC_SYMBOLS_H -/* +/* * Authorship * ---------- * This software ('beatnik' RTEMS BSP for MVME6100 and MVME5500) was * created by Till Straumann , 2005-2007, * Stanford Linear Accelerator Center, Stanford University. - * + * * Acknowledgement of sponsorship * ------------------------------ * The 'beatnik' BSP was produced by * the Stanford Linear Accelerator Center, Stanford University, * under Contract DE-AC03-76SFO0515 with the Department of Energy. - * + * * Government disclaimer of liability * ---------------------------------- * Neither the United States nor the United States Department of Energy, @@ -22,18 +22,18 @@ * completeness, or usefulness of any data, apparatus, product, or process * disclosed, or represents that its use would not infringe privately owned * rights. - * + * * Stanford disclaimer of liability * -------------------------------- * Stanford University makes no representations or warranties, express or * implied, nor assumes any liability for the use of this software. - * + * * Stanford disclaimer of copyright * -------------------------------- * Stanford University, owner of the copyright, hereby disclaims its * copyright and all other rights in this software. Hence, anyone may - * freely use it for any purpose without restriction. - * + * freely use it for any purpose without restriction. + * * Maintenance of notices * ---------------------- * In the interest of clarity regarding the origin and status of this @@ -42,13 +42,14 @@ * or distributed by the recipient and are to be affixed to any copy of * software made or distributed by the recipient that contains a copy or * derivative of this software. - * + * * ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03 - */ + */ #include #include #include #include +#include #ifdef __cplusplus extern "C" { @@ -86,8 +87,8 @@ struct mveth_private; * by BSP_mve_send_buf() earlier. The callback is passed 'cleanup_txbuf_arg' * and a flag indicating whether the send had been successful. * The driver no longer accesses 'user_buf' after invoking this callback. - * CONTEXT: This callback is executed either by BSP_mve_swipe_tx() or - * BSP_mve_send_buf(), BSP_mve_init_hw(), BSP_mve_stop_hw() (the latter + * CONTEXT: This callback is executed either by BSP_mve_swipe_tx() or + * BSP_mve_send_buf(), BSP_mve_init_hw(), BSP_mve_stop_hw() (the latter * ones calling BSP_mve_swipe_tx()). * void *cleanup_txbuf_arg: * Closure argument that is passed on to 'cleanup_txbuf()' callback; @@ -105,7 +106,7 @@ struct mveth_private; * instead of handing it out to 'consume_rxbuf()'. * CONTEXT: Called when initializing the RX ring (BSP_mve_init_hw()) or when * swiping it (BSP_mve_swipe_rx()). - * + * * * void (*consume_rxbuf)(void *user_buf, void *consume_rxbuf_arg, int len); * Pointer to user-supplied callback to pass a received buffer back to @@ -140,7 +141,7 @@ struct mveth_private * BSP_mve_setup( int unit, rtems_id driver_tid, - void (*cleanup_txbuf)(void *user_buf, void *cleanup_txbuf_arg, int error_on_tx_occurred), + void (*cleanup_txbuf)(void *user_buf, void *cleanup_txbuf_arg, int error_on_tx_occurred), void *cleanup_txbuf_arg, void *(*alloc_rxbuf)(int *p_size, uintptr_t *p_data_addr), void (*consume_rxbuf)(void *user_buf, void *consume_rxbuf_arg, int len), @@ -161,7 +162,7 @@ BSP_mve_setup_1( int unit, void (*isr)(void *isr_arg), void *isr_arg, - void (*cleanup_txbuf)(void *user_buf, void *cleanup_txbuf_arg, int error_on_tx_occurred), + void (*cleanup_txbuf)(void *user_buf, void *cleanup_txbuf_arg, int error_on_tx_occurred), void *cleanup_txbuf_arg, void *(*alloc_rxbuf)(int *p_size, uintptr_t *p_data_addr), void (*consume_rxbuf)(void *user_buf, void *consume_rxbuf_arg, int len), @@ -172,81 +173,6 @@ BSP_mve_setup_1( ); -/* - * Initialize interface hardware - * - * 'mp' handle obtained by from BSP_mve_setup(). - * 'promisc' whether to set promiscuous flag. - * 'enaddr' pointer to six bytes with MAC address. Read - * from the device if NULL. - * - * Note: Multicast filters are cleared by this routine. - * However, in promiscuous mode the mcast filters - * are programmed to accept all multicast frames. - */ -void -BSP_mve_init_hw(struct mveth_private *mp, int promisc, unsigned char *enaddr); - -/* - * Clear multicast hash filter. No multicast frames are accepted - * after executing this routine (unless the hardware was initialized - * in 'promiscuous' mode). - */ -void -BSP_mve_mcast_filter_clear(struct mveth_private *mp); - -/* - * Program multicast filter to accept all multicast frames - */ -void -BSP_mve_mcast_filter_accept_all(struct mveth_private *mp); - -/* - * Add a MAC address to the multicast filter. - * Existing entries are not changed but note that - * the filter is imperfect, i.e., multiple MAC addresses - * may alias to a single filter entry. Hence software - * filtering must still be performed. - * - * If a higher-level driver implements IP multicasting - * then multiple IP addresses may alias to the same MAC - * address. This driver maintains a 'reference-count' - * which is incremented every time the same MAC-address - * is passed to this routine; the address is only removed - * from the filter if BSP_mve_mcast_filter_accept_del() - * is called the same number of times (or by BSP_mve_mcast_filter_clear). - */ -void -BSP_mve_mcast_filter_accept_add(struct mveth_private *mp, unsigned char *enaddr); - -/* - * Remove a MAC address from the multicast filter. - * This routine decrements the reference count of the given - * MAC-address and removes it from the filter once the - * count reaches zero. - */ -void -BSP_mve_mcast_filter_accept_del(struct mveth_private *mp, unsigned char *enaddr); - -/* - * Shutdown hardware and clean out the rings - */ -void -BSP_mve_stop_hw(struct mveth_private *mp); - -/* calls BSP_mve_stop_hw(), releases all resources and marks the interface - * as unused. - * RETURNS 0 on success, nonzero on failure. - * NOTE: the handle MUST NOT be used after successful execution of this - * routine. - */ -int -BSP_mve_detach(struct mveth_private *mp); - -int -BSP_mve_send_buf_raw(struct mveth_private *mp, void *head_p, int h_len, - void *data_p, int d_len); - /* * Enqueue a mbuf chain or a raw data buffer for transmission; * RETURN: #bytes sent or -1 if there are not enough free descriptors @@ -268,29 +194,6 @@ BSP_mve_send_buf_raw(struct mveth_private *mp, void *head_p, int h_len, int BSP_mve_send_buf(struct mveth_private *mp, void *m_head, void *data_p, int len); -/* Descriptor scavenger; cleanup the TX ring, passing all buffers - * that have been sent to the cleanup_tx() callback. - * This routine is called from BSP_mve_send_buf(), BSP_mve_init_hw(), - * BSP_mve_stop_hw(). - * - * RETURNS: number of buffers processed. - */ -int -BSP_mve_swipe_tx(struct mveth_private *mp); - -/* Retrieve all received buffers from the RX ring, replacing them - * by fresh ones (obtained from the alloc_rxbuf() callback). The - * received buffers are passed to consume_rxbuf(). - * - * RETURNS: number of buffers processed. - */ -int -BSP_mve_swipe_rx(struct mveth_private *mp); - -/* read ethernet address from hw to buffer */ -void -BSP_mve_read_eaddr(struct mveth_private *mp, unsigned char *eaddr); - /* read/write media word. * 'cmd': can be SIOCGIFMEDIA, SIOCSIFMEDIA, 0 or 1. The latter * are aliased to the former for convenience. @@ -303,62 +206,6 @@ BSP_mve_read_eaddr(struct mveth_private *mp, unsigned char *eaddr); int BSP_mve_media_ioctl(struct mveth_private *mp, int cmd, int *parg); -/* Interrupt related routines */ - -/* Note: the BSP_mve_enable/disable/ack_irqs() entry points - * are deprecated. - * The newer API where the user passes a mask allows - * for more selective control. - */ - -/* Enable all supported interrupts at device */ -void -BSP_mve_enable_irqs(struct mveth_private *mp); - -/* Disable all supported interrupts at device */ -void -BSP_mve_disable_irqs(struct mveth_private *mp); - -/* Acknowledge (and clear) all supported interrupts. - * RETURNS: interrupts that were raised. - */ -uint32_t -BSP_mve_ack_irqs(struct mveth_private *mp); - -/* Enable interrupts included in 'mask' (leaving - * already enabled interrupts on). If the mask - * includes bits that were not passed to the 'setup' - * routine then the behavior is undefined. - */ -void -BSP_mve_enable_irq_mask(struct mveth_private *mp, uint32_t irq_mask); - -/* Disable interrupts included in 'mask' (leaving - * other ones that are currently enabled on). If the - * mask includes bits that were not passed to the 'setup' - * routine then the behavior is undefined. - * - * RETURNS: Bitmask of interrupts that were enabled upon entry - * into this routine. This can be used to restore the - * previous state. - */ -uint32_t -BSP_mve_disable_irq_mask(struct mveth_private *mp, uint32_t irq_mask); - -/* Acknowledge and clear selected interrupts. - * - * RETURNS: All pending interrupts. - * - * NOTE: Only pending interrupts contained in 'mask' - * are cleared. Others are left pending. - * - * This routine can be used to check for pending - * interrupts (pass mask == 0) or to clear all - * interrupts (pass mask == -1). - */ -uint32_t -BSP_mve_ack_irq_mask(struct mveth_private *mp, uint32_t mask); - /* If the PHY link status changes then some * internal settings in the ethernet controller's * serial port need to be updated to match the @@ -378,21 +225,7 @@ BSP_mve_ack_irq_mask(struct mveth_private *mp, uint32_t mask); int BSP_mve_ack_link_chg(struct mveth_private *mp, int *pmedia); -/* Retrieve the driver daemon TID that was passed to - * BSP_mve_setup(). - */ - -rtems_id -BSP_mve_get_tid(struct mveth_private *mp); - -/* Dump statistics to file (stdout if NULL) - * - * NOTE: this routine is not thread safe - */ -void -BSP_mve_dump_stats(struct mveth_private *mp, FILE *f); - -/* +/* * * Example driver task loop (note: no synchronization of * buffer access shown!). diff --git a/bsps/powerpc/beatnik/include/bsp/mv643xx_eth.h b/bsps/powerpc/beatnik/include/bsp/mv643xx_eth.h new file mode 100644 index 00000000000..5bfcd9b26ab --- /dev/null +++ b/bsps/powerpc/beatnik/include/bsp/mv643xx_eth.h @@ -0,0 +1,334 @@ +#ifndef MV643XX_LOWLEVEL_DRIVER_H +#define MV643XX_LOWLEVEL_DRIVER_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define MV643XXETH_NUM_DRIVER_SLOTS 2 + +struct mveth_private; + +/* Create interface. + * Allocates resources for descriptor rings and sets up the driver software structure. + * + * Arguments: + * unit: + * interface # (1..2). The interface must not be attached to BSD. + * + * driver_tid: + * optional driver task-id (can be retrieved with BSP_mve_get_tid()). + * Not used by the low-level driver. + * + * isr(isr_arg): + * user ISR. + * + * void (*cleanup_txbuf)(void *user_buf, void *cleanup_txbuf_arg, int error_on_tx_occurred): + * Pointer to user-supplied callback to release a buffer that had been sent + * by BSP_mve_send_buf() earlier. The callback is passed 'cleanup_txbuf_arg' + * and a flag indicating whether the send had been successful. + * The driver no longer accesses 'user_buf' after invoking this callback. + * CONTEXT: This callback is executed either by BSP_mve_swipe_tx() or + * BSP_mve_send_buf(), BSP_mve_init_hw(), BSP_mve_stop_hw() (the latter + * ones calling BSP_mve_swipe_tx()). + * void *cleanup_txbuf_arg: + * Closure argument that is passed on to 'cleanup_txbuf()' callback; + * + * void *(*alloc_rxbuf)(int *p_size, unsigned long *p_data_addr), + * Pointer to user-supplied callback to allocate a buffer for subsequent + * insertion into the RX ring by the driver. + * RETURNS: opaque handle to the buffer (which may be a more complex object + * such as an 'mbuf'). The handle is not used by the driver directly + * but passed back to the 'consume_rxbuf()' callback. + * Size of the available data area and pointer to buffer's data area + * in '*psize' and '*p_data_area', respectively. + * If no buffer is available, this routine should return NULL in which + * case the driver drops the last packet and re-uses the last buffer + * instead of handing it out to 'consume_rxbuf()'. + * CONTEXT: Called when initializing the RX ring (BSP_mve_init_hw()) or when + * swiping it (BSP_mve_swipe_rx()). + * + * + * void (*consume_rxbuf)(void *user_buf, void *consume_rxbuf_arg, int len); + * Pointer to user-supplied callback to pass a received buffer back to + * the user. The driver no longer accesses the buffer after invoking this + * callback (with 'len'>0, see below). 'user_buf' is the buffer handle + * previously generated by 'alloc_rxbuf()'. + * The callback is passed 'cleanup_rxbuf_arg' and a 'len' + * argument giving the number of bytes that were received. + * 'len' may be <=0 in which case the 'user_buf' argument is NULL. + * 'len' == 0 means that the last 'alloc_rxbuf()' had failed, + * 'len' < 0 indicates a receiver error. In both cases, the last packet + * was dropped/missed and the last buffer will be re-used by the driver. + * NOTE: the data are 'prefixed' with two bytes, i.e., the ethernet packet header + * is stored at offset 2 in the buffer's data area. Also, the FCS (4 bytes) + * is appended. 'len' accounts for both. + * CONTEXT: Called from BSP_mve_swipe_rx(). + * void *cleanup_rxbuf_arg: + * Closure argument that is passed on to 'consume_rxbuf()' callback; + * + * rx_ring_size, tx_ring_size: + * How many big to make the RX and TX descriptor rings. Note that the sizes + * may be 0 in which case a reasonable default will be used. + * If either ring size is < 0 then the RX or TX will be disabled. + * Note that it is illegal in this case to use BSP_mve_swipe_rx() or + * BSP_mve_swipe_tx(), respectively. + * + * irq_mask: + * Interrupts to enable. OR of flags from above. + * + */ + +/* Direct assignment of MVE flags to user API relies on irqs and x-irqs not overlapping */ +#define MV643XX_ETH_IRQ_RX_DONE (1<<2) +#define MV643XX_ETH_EXT_IRQ_TX_DONE (1<<0) +#define MV643XX_ETH_EXT_IRQ_LINK_CHG (1<<16) + +struct mveth_private * +BSP_mve_create( + int unit, + rtems_id tid, + void (*isr)(void*isr_arg), + void *isr_arg, + void (*cleanup_txbuf)(void *user_buf, void *closure, int error_on_tx_occurred), + void *cleanup_txbuf_arg, + void *(*alloc_rxbuf)(int *p_size, uintptr_t *p_data_addr), + void (*consume_rxbuf)(void *user_buf, void *closure, int len), + void *consume_rxbuf_arg, + int rx_ring_size, + int tx_ring_size, + int irq_mask +); + +/* + * Clear multicast hash filter. No multicast frames are accepted + * after executing this routine (unless the hardware was initialized + * in 'promiscuous' mode). + */ +void +BSP_mve_mcast_filter_clear(struct mveth_private *mp); + +/* + * Program multicast filter to accept all multicast frames + */ +void +BSP_mve_mcast_filter_accept_all(struct mveth_private *mp); + +/* + * Add a MAC address to the multicast filter. + * Existing entries are not changed but note that + * the filter is imperfect, i.e., multiple MAC addresses + * may alias to a single filter entry. Hence software + * filtering must still be performed. + * + * If a higher-level driver implements IP multicasting + * then multiple IP addresses may alias to the same MAC + * address. This driver maintains a 'reference-count' + * which is incremented every time the same MAC-address + * is passed to this routine; the address is only removed + * from the filter if BSP_mve_mcast_filter_accept_del() + * is called the same number of times (or by BSP_mve_mcast_filter_clear). + */ +void +BSP_mve_mcast_filter_accept_add(struct mveth_private *mp, unsigned char *enaddr); + +/* + * Remove a MAC address from the multicast filter. + * This routine decrements the reference count of the given + * MAC-address and removes it from the filter once the + * count reaches zero. + */ +void +BSP_mve_mcast_filter_accept_del(struct mveth_private *mp, unsigned char *enaddr); + + +/* Enable/disable promiscuous mode */ +void +BSP_mve_promisc_set(struct mveth_private *mp, int promisc); + +/* calls BSP_mve_stop_hw(), releases all resources and marks the interface + * as unused. + * RETURNS 0 on success, nonzero on failure. + * NOTE: the handle MUST NOT be used after successful execution of this + * routine. + */ +int +BSP_mve_detach(struct mveth_private *mp); + +/* Enqueue a buffer chain for transmission. + * + * RETURN: #bytes sent or -1 if there are not enough descriptors + * -2 is returned if the caller should attempt to + * repackage the chain into a smaller one. + * + * Comments: software cache-flushing incurs a penalty if the + * packet cannot be queued since it is flushed anyways. + * The algorithm is slightly more efficient in the normal + * case, though. + */ + +typedef struct MveEthBufIter { + void *hdl; /* opaque handle for the iterator implementor */ + void *data; /* data to be sent */ + size_t len; /* data size (in octets) */ + void *uptr; /* user-pointer to go into the descriptor; + note: cleanup_txbuf is only called for desriptors + where this field is non-NULL (for historical + reasons) */ +} MveEthBufIter; + +typedef MveEthBufIter *(*MveEthBufIterNext)(MveEthBufIter*); + +int +BSP_mve_send_buf_chain(struct mveth_private *mp, MveEthBufIterNext next, MveEthBufIter *it); + + +/* Legacy entry point to send a header + a buffer */ +int +BSP_mve_send_buf_raw(struct mveth_private *mp, void *head_p, int h_len, void *data_p, int d_len); + +/* Descriptor scavenger; cleanup the TX ring, passing all buffers + * that have been sent to the cleanup_tx() callback. + * This routine is called from BSP_mve_send_buf(), BSP_mve_init_hw(), + * BSP_mve_stop_hw(). + * + * RETURNS: number of buffers processed. + */ +int +BSP_mve_swipe_tx(struct mveth_private *mp); + +/* Retrieve all received buffers from the RX ring, replacing them + * by fresh ones (obtained from the alloc_rxbuf() callback). The + * received buffers are passed to consume_rxbuf(). + * + * RETURNS: number of buffers processed. + */ +int +BSP_mve_swipe_rx(struct mveth_private *mp); + +/* read ethernet address from hw to buffer */ +void +BSP_mve_read_eaddr(struct mveth_private *mp, unsigned char *oeaddr); + +/* Interrupt related routines */ + +/* Note: the BSP_mve_enable/disable/ack_irqs() entry points + * are deprecated. + * The newer API where the user passes a mask allows + * for more selective control. + */ + +/* Enable all supported interrupts at device */ +void +BSP_mve_enable_irqs(struct mveth_private *mp); + +/* Disable all supported interrupts at device */ +void +BSP_mve_disable_irqs(struct mveth_private *mp); + +/* Acknowledge (and clear) all supported interrupts. + * RETURNS: interrupts that were raised. + */ +uint32_t +BSP_mve_ack_irqs(struct mveth_private *mp); + +/* Enable interrupts included in 'mask' (leaving + * already enabled interrupts on). If the mask + * includes bits that were not passed to the 'setup' + * routine then the behavior is undefined. + */ +void +BSP_mve_enable_irq_mask(struct mveth_private *mp, uint32_t irq_mask); + +/* Disable interrupts included in 'mask' (leaving + * other ones that are currently enabled on). If the + * mask includes bits that were not passed to the 'setup' + * routine then the behavior is undefined. + * + * RETURNS: Bitmask of interrupts that were enabled upon entry + * into this routine. This can be used to restore the + * previous state. + */ +uint32_t +BSP_mve_disable_irq_mask(struct mveth_private *mp, uint32_t irq_mask); + +/* Acknowledge and clear selected interrupts. + * + * RETURNS: All pending interrupts. + * + * NOTE: Only pending interrupts contained in 'mask' + * are cleared. Others are left pending. + * + * This routine can be used to check for pending + * interrupts (pass mask == 0) or to clear all + * interrupts (pass mask == -1). + */ +uint32_t +BSP_mve_ack_irq_mask(struct mveth_private *mp, uint32_t mask); + +/* Retrieve the driver daemon TID that was passed to + * BSP_mve_setup(). + */ + +rtems_id +BSP_mve_get_tid(struct mveth_private *mp); + +/* Dump statistics to file (stdout if NULL) + * + * NOTE: this routine is not thread safe + */ +void +BSP_mve_dump_stats(struct mveth_private *mp, FILE *f); + +#define MV643XX_MEDIA_LINK (1<<0) +#define MV643XX_MEDIA_FD (1<<1) +#define MV643XX_MEDIA_10 (1<<8) +#define MV643XX_MEDIA_100 (2<<8) +#define MV643XX_MEDIA_1000 (3<<8) +#define MV643XX_MEDIA_SPEED_MSK (0xff00) + +/* + * Initialize interface hardware + * + * 'mp' handle obtained by from BSP_mve_setup(). + * 'promisc' whether to set promiscuous flag. + * 'enaddr' pointer to six bytes with MAC address. Read + * from the device if NULL. + * 'speed' current speed and link status as read from the PHY. + * + * Note: Multicast filters are cleared by this routine. + * However, in promiscuous mode the mcast filters + * are programmed to accept all multicast frames. + */ +void +BSP_mve_init_hw(struct mveth_private *mp, int promisc, unsigned char *enaddr, int speed); + +/* + * Update the serial port to a new speed (e.g., result of a PHY IRQ) + */ +void +BSP_mve_update_serial_port(struct mveth_private *mp, int speed); + +/* + * Shutdown hardware and clean out the rings + */ +void +BSP_mve_stop_hw(struct mveth_private *mp); + +unsigned +BSP_mve_mii_read(struct mveth_private *mp, unsigned addr); + +unsigned +BSP_mve_mii_write(struct mveth_private *mp, unsigned addr, unsigned v); + +unsigned +BSP_mve_mii_read_early(int port, unsigned addr); + +unsigned +BSP_mve_mii_write_early(int port, unsigned addr, unsigned v); + +#ifdef __cplusplus +}; +#endif + +#endif diff --git a/bsps/powerpc/beatnik/net/if_mve/mv643xx_eth.c b/bsps/powerpc/beatnik/net/if_mve/mv643xx_eth.c index 85ab038bf56..7fc0bed86a9 100644 --- a/bsps/powerpc/beatnik/net/if_mve/mv643xx_eth.c +++ b/bsps/powerpc/beatnik/net/if_mve/mv643xx_eth.c @@ -18,19 +18,19 @@ * new implementation and is the original work by the author. */ -/* +/* * Authorship * ---------- * This software (mv643xx ethernet driver for RTEMS) was * created by Till Straumann , 2005-2007, * Stanford Linear Accelerator Center, Stanford University. - * + * * Acknowledgement of sponsorship * ------------------------------ * The 'mv643xx ethernet driver for RTEMS' was produced by * the Stanford Linear Accelerator Center, Stanford University, * under Contract DE-AC03-76SFO0515 with the Department of Energy. - * + * * Government disclaimer of liability * ---------------------------------- * Neither the United States nor the United States Department of Energy, @@ -39,18 +39,18 @@ * completeness, or usefulness of any data, apparatus, product, or process * disclosed, or represents that its use would not infringe privately owned * rights. - * + * * Stanford disclaimer of liability * -------------------------------- * Stanford University makes no representations or warranties, express or * implied, nor assumes any liability for the use of this software. - * + * * Stanford disclaimer of copyright * -------------------------------- * Stanford University, owner of the copyright, hereby disclaims its * copyright and all other rights in this software. Hence, anyone may - * freely use it for any purpose without restriction. - * + * freely use it for any purpose without restriction. + * * Maintenance of notices * ---------------------- * In the interest of clarity regarding the origin and status of this @@ -59,9 +59,9 @@ * or distributed by the recipient and are to be affixed to any copy of * software made or distributed by the recipient that contains a copy or * derivative of this software. - * + * * ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03 - */ + */ /* * NOTE: Some register (e.g., the SMI register) are SHARED among the @@ -74,12 +74,14 @@ * further down... */ +/* #ifndef KERNEL #define KERNEL #endif #ifndef _KERNEL #define _KERNEL #endif +*/ #include #include @@ -88,44 +90,13 @@ #include #include #include - -#include -#include -#include -#include -#include -#include -#include - -/* Not so nice; would be more elegant not to depend on C library but the - * RTEMS-specific ioctl for dumping statistics needs stdio anyways. - */ - -/*#define NDEBUG effectively removes all assertions - * If defining NDEBUG, MAKE SURE assert() EXPRESSION HAVE NO SIDE_EFFECTS!! - * This driver DOES have side-effects, so DONT DEFINE NDEBUG - * Performance-critical assertions are removed by undefining MVETH_TESTING. - */ - -#undef NDEBUG #include #include #include #include +#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include +#include /* CONFIGURABLE PARAMETERS */ @@ -137,61 +108,11 @@ /* Compile-time debugging features */ /* Enable paranoia assertions and checks; reduce # of descriptors to minimum for stressing */ -#undef MVETH_TESTING +#define MVETH_TESTING -/* Enable debugging messages and some support routines (dump rings etc.) */ +/* Enable debugging messages and some support routines (dump rings etc.) */ #undef MVETH_DEBUG -/* Hack for driver development; rtems bsdnet doesn't implement detaching an interface :-( - * but this hack allows us to unload/reload the driver module which makes development - * a lot less painful. - */ -#undef MVETH_DETACH_HACK - -/* Ring sizes */ - -#ifdef MVETH_TESTING - -/* hard and small defaults */ -#undef MV643XX_RX_RING_SIZE -#define MV643XX_RX_RING_SIZE 2 -#undef MV643XX_TX_RING_SIZE -#define MV643XX_TX_RING_SIZE 4 - -#else /* MVETH_TESTING */ - -/* Define default ring sizes, allow override from bsp.h, Makefile,... and from ifcfg->rbuf_count/xbuf_count */ - -#ifndef MV643XX_RX_RING_SIZE -#define MV643XX_RX_RING_SIZE 40 /* attached buffers are always 2k clusters, i.e., this - * driver - with a configured ring size of 40 - constantly - * locks 80k of cluster memory - your app config better - * provides enough space! - */ -#endif - -#ifndef MV643XX_TX_RING_SIZE -/* NOTE: tx ring size MUST be > max. # of fragments / mbufs in a chain; - * in 'TESTING' mode, special code is compiled in to repackage - * chains that are longer than the ring size. Normally, this is - * disabled for sake of speed. - * I observed chains of >17 entries regularly! - * - * Also, TX_NUM_TAG_SLOTS (1) must be left empty as a marker, hence - * the ring size must be > max. #frags + 1. - */ -#define MV643XX_TX_RING_SIZE 200 /* these are smaller fragments and not occupied when - * the driver is idle. - */ -#endif - -#endif /* MVETH_TESTING */ - -/* How many instances to we support (bsp.h could override) */ -#ifndef MV643XXETH_NUM_DRIVER_SLOTS -#define MV643XXETH_NUM_DRIVER_SLOTS 2 -#endif - #define TX_NUM_TAG_SLOTS 1 /* leave room for tag; must not be 0 */ /* This is REAL; chip reads from 64-bit down-aligned buffer @@ -278,7 +199,7 @@ * * broadcast packet RX: 0x00000005 * last buf: 0x00000c05 - * overrun: 0x00000c00 + * overrun: 0x00000c00 * unicast packet RX: 0x00000005 * bad CRC received: 0x00000005 * @@ -311,11 +232,11 @@ * } * --> sometimes, cmd_sts is STILL != 0 here * - * b) Sometimes, the OWNership flag is *not cleared*. - * + * b) Sometimes, the OWNership flag is *not cleared*. + * * c) Weird things happen if the chip finds a descriptor with 'OWN' * still set (i.e., not properly loaded), i.e., corrupted packets - * are sent [with OK checksum since the chip calculates it]. + * are sent [with OK checksum since the chip calculates it]. * * Combine a+b+c and we end up with a real mess. * @@ -477,20 +398,31 @@ /* not fully understood; RX seems to raise 0x0005 or 0x0c05 if last buffer is filled and 0x0c00 * if there are no buffers */ -#define MV643XX_ETH_ALL_IRQS (0x0007ffff) -#define MV643XX_ETH_KNOWN_IRQS (0x00000c05) +#define MV643XX_ETH_ALL_IRQS (0x07ffffff) +#define MV643XX_ETH_KNOWN_IRQS (0x00080c07) #define MV643XX_ETH_IRQ_EXT_ENA (1<<1) +/* defined in public header #define MV643XX_ETH_IRQ_RX_DONE (1<<2) + */ #define MV643XX_ETH_IRQ_RX_NO_DESC (1<<10) +#define MV643XX_ETH_TX_Q_N_END(n) (1<<((n)+19)) +/* We just use queue 0 */ +#define MV643XX_ETH_TX_Q_END MV643XX_ETH_TX_Q_N_END(0) #define MV643XX_ETH_INTERRUPT_EXTEND_CAUSE_R(port) (0x2464 + ((port)<<10)) /* not fully understood; TX seems to raise 0x0001 and link change is 0x00010000 * if there are no buffers */ #define MV643XX_ETH_ALL_EXT_IRQS (0x0011ffff) -#define MV643XX_ETH_KNOWN_EXT_IRQS (0x00010101) -#define MV643XX_ETH_EXT_IRQ_TX_DONE (1<<0) +/* Recent (2013) linux driver mentions both bits 0x00110000 as 'link change' causes */ +#define MV643XX_ETH_KNOWN_EXT_IRQS (0x00110101) +/* TX queues 0..7 */ +#define MV643XX_ETH_EXT_IRQ_TXN_DONE(n) (1<<(n)) +/* We just use queue 0 */ +/* defined in public header +#define MV643XX_ETH_EXT_IRQ_TX_DONE MV643XX_ETH_EXT_IRQ_TXN_DONE(0) #define MV643XX_ETH_EXT_IRQ_LINK_CHG (1<<16) + */ #define MV643XX_ETH_INTERRUPT_ENBL_R(port) (0x2468 + ((port)<<10)) #define MV643XX_ETH_INTERRUPT_EXTEND_ENBL_R(port) (0x246c + ((port)<<10)) @@ -718,7 +650,7 @@ struct mveth_private { int port_num; int phy; MvEthRxDesc d_rx_t; /* tail of the RX ring; next received packet */ - MvEthTxDesc d_tx_t, d_tx_h; + MvEthTxDesc d_tx_t, d_tx_h; uint32_t rx_desc_dma, tx_desc_dma; /* ring address as seen by DMA; (1:1 on this BSP) */ int avail; void (*isr)(void*); @@ -738,7 +670,7 @@ struct mveth_private { unsigned maxchain; unsigned repack; unsigned packet; - unsigned odrops; /* no counter in core code */ + unsigned idrops; /* no counter in core code */ struct { uint64_t good_octs_rcvd; /* 64-bit */ uint32_t bad_octs_rcvd; @@ -777,48 +709,7 @@ struct mveth_private { } mc_refcnt; }; -/* stuff needed for bsdnet support */ -struct mveth_bsdsupp { - int oif_flags; /* old / cached if_flags */ -}; - -struct mveth_softc { - struct arpcom arpcom; - struct mveth_bsdsupp bsd; - struct mveth_private pvt; -}; - /* GLOBAL VARIABLES */ -#ifdef MVETH_DEBUG_TX_DUMP -int mveth_tx_dump = 0; -#endif - -/* THE array of driver/bsdnet structs */ - -/* If detaching/module unloading is enabled, the main driver data - * structure must remain in memory; hence it must reside in its own - * 'dummy' module... - */ -#ifdef MVETH_DETACH_HACK -extern -#else -STATIC -#endif -struct mveth_softc theMvEths[MV643XXETH_NUM_DRIVER_SLOTS] -#ifndef MVETH_DETACH_HACK -= {{{{0}},}} -#endif -; - -/* daemon task id */ -STATIC rtems_id mveth_tid = 0; -/* register access protection mutex */ -STATIC rtems_id mveth_mtx = 0; -#define REGLOCK() do { \ - if ( RTEMS_SUCCESSFUL != rtems_semaphore_obtain(mveth_mtx, RTEMS_WAIT, RTEMS_NO_TIMEOUT) ) \ - rtems_panic(DRVNAME": unable to lock register protection mutex"); \ - } while (0) -#define REGUNLOCK() rtems_semaphore_release(mveth_mtx) /* Format strings for statistics messages */ static const char *mibfmt[] = { @@ -860,8 +751,16 @@ static const char *mibfmt[] = { /* forward decls + implementation for IRQ API funcs */ +STATIC int +mveth_init_rx_desc_ring(struct mveth_private *mp); + +STATIC int +mveth_init_tx_desc_ring(struct mveth_private *mp); + +int +BSP_mve_dring_nonsync(struct mveth_private *mp); + static void mveth_isr(rtems_irq_hdl_param unit); -static void mveth_isr_1(rtems_irq_hdl_param unit); static void noop(const rtems_irq_connect_data *unused) {} static int noop1(const rtems_irq_connect_data *unused) { return 0; } @@ -892,40 +791,6 @@ static rtems_irq_connect_data irq_data[MAX_NUM_SLOTS] = { }, }; -/* MII Ioctl Interface */ - -STATIC unsigned -mveth_mii_read(struct mveth_private *mp, unsigned addr); - -STATIC unsigned -mveth_mii_write(struct mveth_private *mp, unsigned addr, unsigned v); - - -/* mdio / mii interface wrappers for rtems_mii_ioctl API */ - -static int mveth_mdio_r(int phy, void *uarg, unsigned reg, uint32_t *pval) -{ - if ( phy > 1 ) - return -1; - - *pval = mveth_mii_read(uarg, reg); - return 0; -} - -static int mveth_mdio_w(int phy, void *uarg, unsigned reg, uint32_t val) -{ - if ( phy > 1 ) - return -1; - mveth_mii_write(uarg, reg, val); - return 0; -} - -static struct rtems_mdio_info mveth_mdio = { - mdio_r: mveth_mdio_r, - mdio_w: mveth_mdio_w, - has_gmii: 1, -}; - /* LOW LEVEL SUPPORT ROUTINES */ /* Software Cache Coherency */ @@ -975,7 +840,7 @@ static inline void FLUSH_BUF(register uintptr_t addr, register int len) { asm volatile("":::"memory"); len = MV643XX_ALIGN(len, RX_BUF_ALIGNMENT); - do { + do { asm volatile("dcbf %0, %1"::"b"(addr),"r"(len)); len -= RX_BUF_ALIGNMENT; } while ( len >= 0 ); @@ -1054,6 +919,7 @@ static inline uint32_t mveth_ack_irqs(struct mveth_private *mp, uint32_t mask) { register uint32_t x,xe,p; +register uint32_t rval; p = mp->port_num; /* Get cause */ @@ -1082,29 +948,30 @@ register uint32_t x,xe,p; || ((xe & MV643XX_ETH_ALL_EXT_IRQS) & ~MV643XX_ETH_KNOWN_EXT_IRQS) ) { fprintf(stderr, "Unknown IRQs detected; leaving all disabled for debugging:\n"); fprintf(stderr, "Cause reg was 0x%08x, ext cause 0x%08x\n", x, xe); +/* mp->irq_mask = 0; mp->xirq_mask = 0; +*/ } #endif /* luckily, the extended and 'normal' interrupts we use don't overlap so * we can just OR them into a single word */ - return (xe & mp->xirq_mask) | (x & mp->irq_mask); -} + rval = (xe & mp->xirq_mask) | (x & mp->irq_mask); -static void mveth_isr(rtems_irq_hdl_param arg) -{ -unsigned unit = (unsigned)arg; - mveth_disable_irqs(&theMvEths[unit].pvt, -1); - theMvEths[unit].pvt.stats.irqs++; - rtems_bsdnet_event_send( theMvEths[unit].pvt.tid, 1<stats.irqs++; mp->isr(mp->isr_arg); } @@ -1163,17 +1030,25 @@ uint32_t x; MV_WRITE(MV643XX_ETH_MAC_ADDR_LO(mp->port_num), x); } +static inline int +port2phy(int port) +{ + port &= 0x1f; + /* during early init we may not know the phy and we are given a port number instead! */ + return ( (MV_READ(MV643XX_ETH_PHY_ADDR_R) >> (5*port)) & 0x1f ); +} + /* PHY/MII Interface * * Read/write a PHY register; - * + * * NOTE: The SMI register is shared among the three devices. * Protection is provided by the global networking semaphore. * If non-bsd drivers are running on a subset of IFs proper * locking of all shared registers must be implemented! */ -STATIC unsigned -mveth_mii_read(struct mveth_private *mp, unsigned addr) +static unsigned +do_mii_read(int phy, unsigned addr) { unsigned v; unsigned wc = 0; @@ -1186,7 +1061,7 @@ unsigned wc = 0; wc++; } while ( MV643XX_ETH_SMI_BUSY & v ); - MV_WRITE(MV643XX_ETH_SMI_R, (addr <<21 ) | (mp->phy<<16) | MV643XX_ETH_SMI_OP_RD ); + MV_WRITE(MV643XX_ETH_SMI_R, (addr <<21 ) | (phy<<16) | MV643XX_ETH_SMI_OP_RD ); do { v = MV_READ(MV643XX_ETH_SMI_R); @@ -1198,8 +1073,24 @@ unsigned wc = 0; return (wc<<16) | (v & 0xffff); } -STATIC unsigned -mveth_mii_write(struct mveth_private *mp, unsigned addr, unsigned v) +unsigned +BSP_mve_mii_read(struct mveth_private *mp, unsigned addr) +{ +unsigned rval = do_mii_read(mp->phy, addr); +#ifdef MVETH_DEBUG + printk(DRVNAME": BSP_mve_mii_read(%d): 0x%08x\n", addr, rval); +#endif + return rval; +} + +unsigned +BSP_mve_mii_read_early(int port, unsigned addr) +{ + return do_mii_read(port2phy(port), addr); +} + +static unsigned +do_mii_write(int phy, unsigned addr, unsigned v) { unsigned wc = 0; @@ -1214,11 +1105,27 @@ unsigned wc = 0; while ( MV643XX_ETH_SMI_BUSY & MV_READ(MV643XX_ETH_SMI_R) ) wc++ /* wait */; - MV_WRITE(MV643XX_ETH_SMI_R, (addr <<21 ) | (mp->phy<<16) | MV643XX_ETH_SMI_OP_WR | v ); + MV_WRITE(MV643XX_ETH_SMI_R, (addr <<21 ) | (phy<<16) | MV643XX_ETH_SMI_OP_WR | v ); return wc; } +unsigned +BSP_mve_mii_write(struct mveth_private *mp, unsigned addr, unsigned v) +{ +#ifdef MVETH_DEBUG + printk(DRVNAME": BSP_mve_mii_write(%d): 0x%08x\n", addr, v); +#endif + return do_mii_write( mp->phy, addr, v ); +} + +unsigned +BSP_mve_mii_write_early(int port, unsigned addr, unsigned v) +{ + return do_mii_write(port2phy(port), addr, v); +} + + /* MID-LAYER SUPPORT ROUTINES */ /* Start TX if descriptors are exhausted */ @@ -1255,57 +1162,21 @@ uint32_t active_q; return active_q; } -/* update serial port settings from current link status */ -static void -mveth_update_serial_port(struct mveth_private *mp, int media) +void +BSP_mve_promisc_set(struct mveth_private *mp, int promisc) { -int port = mp->port_num; -uint32_t old, new; - - new = old = MV_READ(MV643XX_ETH_SERIAL_CONTROL_R(port)); - - /* mask speed and duplex settings */ - new &= ~( MV643XX_ETH_SET_GMII_SPEED_1000 - | MV643XX_ETH_SET_MII_SPEED_100 - | MV643XX_ETH_SET_FULL_DUPLEX ); - - if ( IFM_FDX & media ) - new |= MV643XX_ETH_SET_FULL_DUPLEX; - - switch ( IFM_SUBTYPE(media) ) { - default: /* treat as 10 */ - break; - case IFM_100_TX: - new |= MV643XX_ETH_SET_MII_SPEED_100; - break; - case IFM_1000_T: - new |= MV643XX_ETH_SET_GMII_SPEED_1000; - break; - } - - if ( new != old ) { - if ( ! (MV643XX_ETH_SERIAL_PORT_ENBL & new) ) { - /* just write */ - MV_WRITE(MV643XX_ETH_SERIAL_CONTROL_R(port), new); - } else { - uint32_t were_running; - - were_running = mveth_stop_tx(port); - - old &= ~MV643XX_ETH_SERIAL_PORT_ENBL; - MV_WRITE(MV643XX_ETH_SERIAL_CONTROL_R(port), old); - MV_WRITE(MV643XX_ETH_SERIAL_CONTROL_R(port), new); - /* linux driver writes twice... */ - MV_WRITE(MV643XX_ETH_SERIAL_CONTROL_R(port), new); +uint32_t v; - if ( were_running ) { - MV_WRITE(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_R(mp->port_num), MV643XX_ETH_TX_START(0)); - } - } - } + v = MV_READ(MV643XX_ETH_PORT_CONFIG_R(mp->port_num)); + if ( (mp->promisc = promisc) ) + v |= MV643XX_ETH_UNICAST_PROMISC_MODE; + else + v &= ~MV643XX_ETH_UNICAST_PROMISC_MODE; + MV_WRITE(MV643XX_ETH_PORT_CONFIG_R(mp->port_num), v); } -/* Clear multicast filters */ +/* update serial port settings from current link status */ + void BSP_mve_mcast_filter_clear(struct mveth_private *mp) { @@ -1519,36 +1390,8 @@ register uint8_t *d = to, *s = fro; } #endif -/* Assign values (buffer + user data) to a tx descriptor slot */ -static int -mveth_assign_desc(MvEthTxDesc d, struct mbuf *m, unsigned long extra) -{ -int rval = (d->byte_cnt = m->m_len); - -#ifdef MVETH_TESTING - assert( !d->mb ); - assert( m->m_len ); -#endif - - /* set CRC on all descriptors; seems to be necessary */ - d->cmd_sts = extra | (TDESC_GEN_CRC | TDESC_ZERO_PAD); - -#ifdef ENABLE_TX_WORKAROUND_8_BYTE_PROBLEM - /* The buffer must be 64bit aligned if the payload is <8 (??) */ - if ( rval < 8 && ((mtod(m, uintptr_t)) & 7) ) { - d->buf_ptr = CPUADDR2ENET( d->workaround ); - memcpy((void*)d->workaround, mtod(m, void*), rval); - } else -#endif - { - d->buf_ptr = CPUADDR2ENET( mtod(m, unsigned long) ); - } - d->l4i_chk = 0; - return rval; -} - static int -mveth_assign_desc_raw(MvEthTxDesc d, void *buf, int len, unsigned long extra) +mveth_assign_desc_raw(MvEthTxDesc d, void *buf, int len, void *uptr, unsigned long extra) { int rval = (d->byte_cnt = len); @@ -1570,6 +1413,7 @@ int rval = (d->byte_cnt = len); { d->buf_ptr = CPUADDR2ENET( (unsigned long)buf ); } + d->u_buf = uptr; d->l4i_chk = 0; return rval; } @@ -1658,13 +1502,13 @@ MvEthTxDesc d; /* PUBLIC LOW-LEVEL DRIVER ACCESS */ -static struct mveth_private * -mve_setup_internal( +struct mveth_private * +BSP_mve_create( int unit, rtems_id tid, void (*isr)(void*isr_arg), void *isr_arg, - void (*cleanup_txbuf)(void *user_buf, void *closure, int error_on_tx_occurred), + void (*cleanup_txbuf)(void *user_buf, void *closure, int error_on_tx_occurred), void *cleanup_txbuf_arg, void *(*alloc_rxbuf)(int *p_size, uintptr_t *p_data_addr), void (*consume_rxbuf)(void *user_buf, void *closure, int len), @@ -1673,23 +1517,14 @@ mve_setup_internal( int tx_ring_size, int irq_mask ) - { struct mveth_private *mp; -struct ifnet *ifp; int InstallISRSuccessful; if ( unit <= 0 || unit > MV643XXETH_NUM_DRIVER_SLOTS ) { printk(DRVNAME": Bad unit number %i; must be 1..%i\n", unit, MV643XXETH_NUM_DRIVER_SLOTS); return 0; } - ifp = &theMvEths[unit-1].arpcom.ac_if; - if ( ifp->if_init ) { - if ( ifp->if_init ) { - printk(DRVNAME": instance %i already attached.\n", unit); - return 0; - } - } if ( rx_ring_size < 0 && tx_ring_size < 0 ) return 0; @@ -1699,27 +1534,20 @@ int InstallISRSuccessful; return 0; } - /* lazy init of mutex (non thread-safe! - we assume 1st initialization is single-threaded) */ - if ( ! mveth_mtx ) { - rtems_status_code sc; - sc = rtems_semaphore_create( - rtems_build_name('m','v','e','X'), - 1, - RTEMS_BINARY_SEMAPHORE | RTEMS_PRIORITY | RTEMS_INHERIT_PRIORITY | RTEMS_DEFAULT_ATTRIBUTES, - 0, - &mveth_mtx); - if ( RTEMS_SUCCESSFUL != sc ) { - rtems_error(sc,DRVNAME": creating mutex\n"); - rtems_panic("unable to proceed\n"); - } + if ( tx_ring_size < 1 ) { + printk(DRVNAME": tx ring size must not be zero (networking configuration issue?)\n"); + return 0; } - mp = &theMvEths[unit-1].pvt; + if ( rx_ring_size < 1 ) { + printk(DRVNAME": rx ring size must not be zero (networking configuration issue?)\n"); + return 0; + } - memset(mp, 0, sizeof(*mp)); + mp = calloc( 1, sizeof *mp ); mp->port_num = unit-1; - mp->phy = (MV_READ(MV643XX_ETH_PHY_ADDR_R) >> (5*mp->port_num)) & 0x1f; + mp->phy = port2phy(mp->port_num); mp->tid = tid; mp->isr = isr; @@ -1731,8 +1559,8 @@ int InstallISRSuccessful; mp->consume_rxbuf = consume_rxbuf; mp->consume_rxbuf_arg = consume_rxbuf_arg; - mp->rbuf_count = rx_ring_size ? rx_ring_size : MV643XX_RX_RING_SIZE; - mp->xbuf_count = tx_ring_size ? tx_ring_size : MV643XX_TX_RING_SIZE; + mp->rbuf_count = rx_ring_size; + mp->xbuf_count = tx_ring_size; if ( mp->xbuf_count > 0 ) mp->xbuf_count += TX_NUM_TAG_SLOTS; @@ -1744,24 +1572,18 @@ int InstallISRSuccessful; /* allocate ring area; add 1 entry -- room for alignment */ assert( !mp->ring_area ); - mp->ring_area = malloc( - sizeof(*mp->ring_area) * - (mp->rbuf_count + mp->xbuf_count + 1), - M_DEVBUF, - M_WAIT ); + mp->ring_area = malloc( sizeof(*mp->ring_area) * (mp->rbuf_count + mp->xbuf_count + 1) ); assert( mp->ring_area ); BSP_mve_stop_hw(mp); if ( irq_mask ) { - irq_data[mp->port_num].hdl = tid ? mveth_isr : mveth_isr_1; + irq_data[mp->port_num].hdl = mveth_isr; + irq_data[mp->port_num].handle = (rtems_irq_hdl_param)mp; InstallISRSuccessful = BSP_install_rtems_irq_handler( &irq_data[mp->port_num] ); assert( InstallISRSuccessful ); } - /* mark as used */ - ifp->if_init = (void*)(-1); - if ( rx_ring_size < 0 ) irq_mask &= ~ MV643XX_ETH_IRQ_RX_DONE; if ( tx_ring_size < 0 ) @@ -1778,65 +1600,60 @@ int InstallISRSuccessful; return mp; } -struct mveth_private * -BSP_mve_setup( - int unit, - rtems_id tid, - void (*cleanup_txbuf)(void *user_buf, void *closure, int error_on_tx_occurred), - void *cleanup_txbuf_arg, - void *(*alloc_rxbuf)(int *p_size, uintptr_t *p_data_addr), - void (*consume_rxbuf)(void *user_buf, void *closure, int len), - void *consume_rxbuf_arg, - int rx_ring_size, - int tx_ring_size, - int irq_mask -) +void +BSP_mve_update_serial_port(struct mveth_private *mp, int media) { - if ( irq_mask && 0 == tid ) { - printk(DRVNAME": must supply a TID if irq_msk not zero\n"); - return 0; - } +int port = mp->port_num; +uint32_t old, new; - return mve_setup_internal( - unit, - tid, - 0, 0, - cleanup_txbuf, cleanup_txbuf_arg, - alloc_rxbuf, - consume_rxbuf, consume_rxbuf_arg, - rx_ring_size, tx_ring_size, - irq_mask); -} +#ifdef MVETH_DEBUG + printk(DRVNAME": Entering BSP_mve_update_serial_port()\n"); +#endif -struct mveth_private * -BSP_mve_setup_1( - int unit, - void (*isr)(void *isr_arg), - void *isr_arg, - void (*cleanup_txbuf)(void *user_buf, void *closure, int error_on_tx_occurred), - void *cleanup_txbuf_arg, - void *(*alloc_rxbuf)(int *p_size, uintptr_t *p_data_addr), - void (*consume_rxbuf)(void *user_buf, void *closure, int len), - void *consume_rxbuf_arg, - int rx_ring_size, - int tx_ring_size, - int irq_mask -) -{ - if ( irq_mask && 0 == isr ) { - printk(DRVNAME": must supply an ISR if irq_msk not zero\n"); - return 0; + new = old = MV_READ(MV643XX_ETH_SERIAL_CONTROL_R(port)); + + /* mask speed and duplex settings */ + new &= ~( MV643XX_ETH_SET_GMII_SPEED_1000 + | MV643XX_ETH_SET_MII_SPEED_100 + | MV643XX_ETH_SET_FULL_DUPLEX ); + + if ( (MV643XX_MEDIA_FD & media) ) + new |= MV643XX_ETH_SET_FULL_DUPLEX; + + switch ( (media & MV643XX_MEDIA_SPEED_MSK) ) { + default: /* treat as 10 */ + break; + case MV643XX_MEDIA_100: + new |= MV643XX_ETH_SET_MII_SPEED_100; + break; + case MV643XX_MEDIA_1000: + new |= MV643XX_ETH_SET_GMII_SPEED_1000; + break; } - return mve_setup_internal( - unit, - 0, - isr, isr_arg, - cleanup_txbuf, cleanup_txbuf_arg, - alloc_rxbuf, - consume_rxbuf, consume_rxbuf_arg, - rx_ring_size, tx_ring_size, - irq_mask); + + if ( new != old ) { + if ( ! (MV643XX_ETH_SERIAL_PORT_ENBL & new) ) { + /* just write */ + MV_WRITE(MV643XX_ETH_SERIAL_CONTROL_R(port), new); + } else { + uint32_t were_running; + + were_running = mveth_stop_tx(port); + + old &= ~MV643XX_ETH_SERIAL_PORT_ENBL; + MV_WRITE(MV643XX_ETH_SERIAL_CONTROL_R(port), old); + MV_WRITE(MV643XX_ETH_SERIAL_CONTROL_R(port), new); + /* linux driver writes twice... */ + MV_WRITE(MV643XX_ETH_SERIAL_CONTROL_R(port), new); + + if ( were_running ) { + MV_WRITE(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_R(mp->port_num), MV643XX_ETH_TX_START(0)); + } + } + } + /* If TX stalled because there was no buffer then whack it */ + mveth_start_tx(mp); } rtems_id @@ -1848,17 +1665,14 @@ BSP_mve_get_tid(struct mveth_private *mp) int BSP_mve_detach(struct mveth_private *mp) { -int unit = mp->port_num; BSP_mve_stop_hw(mp); if ( mp->irq_mask || mp->xirq_mask ) { if ( !BSP_remove_rtems_irq_handler( &irq_data[mp->port_num] ) ) return -1; } - free( (void*)mp->ring_area, M_DEVBUF ); + free( (void*)mp->ring_area ); memset(mp, 0, sizeof(*mp)); __asm__ __volatile__("":::"memory"); - /* mark as unused */ - theMvEths[unit].arpcom.ac_if.if_init = 0; return 0; } @@ -1901,70 +1715,19 @@ register MvEthTxDesc d; mp->d_tx_t = d; mp->avail += rval; - return rval; + return mp->avail; } -/* allocate a new cluster and copy an existing chain there; - * old chain is released... - */ -static struct mbuf * -repackage_chain(struct mbuf *m_head) +int +BSP_mve_send_buf_chain(struct mveth_private *mp, MveEthBufIterNext next, MveEthBufIter *it) { -struct mbuf *m; - MGETHDR(m, M_DONTWAIT, MT_DATA); - - if ( !m ) { - goto bail; - } - - MCLGET(m, M_DONTWAIT); - - if ( !(M_EXT & m->m_flags) ) { - m_freem(m); - m = 0; - goto bail; - } - - m_copydata(m_head, 0, MCLBYTES, mtod(m, caddr_t)); - m->m_pkthdr.len = m->m_len = m_head->m_pkthdr.len; - -bail: - m_freem(m_head); - return m; -} - -/* Enqueue a mbuf chain or a raw data buffer for transmission; - * RETURN: #bytes sent or -1 if there are not enough descriptors - * - * If 'len' is <=0 then 'm_head' is assumed to point to a mbuf chain. - * OTOH, a raw data packet may be send (non-BSD driver) by pointing - * m_head to the start of the data and passing 'len' > 0. - * - * Comments: software cache-flushing incurs a penalty if the - * packet cannot be queued since it is flushed anyways. - * The algorithm is slightly more efficient in the normal - * case, though. - */ -int -BSP_mve_send_buf(struct mveth_private *mp, void *m_head, void *data_p, int len) -{ -int rval; -register MvEthTxDesc l,d,h; -register struct mbuf *m1; -int nmbs; -int ismbuf = (len <= 0); - -/* Only way to get here is when we discover that the mbuf chain - * is too long for the tx ring - */ -startover: +int rval; +register MvEthTxDesc l,d,h; +int nmbs; +MveEthBufIter head = *it; rval = 0; -#ifdef MVETH_TESTING - assert(m_head); -#endif - /* if no descriptor is available; try to wipe the queue */ if ( (mp->avail < 1) && MVETH_CLEAN_ON_SEND(mp)<=0 ) { /* Maybe TX is stalled and needs to be restarted */ @@ -1974,25 +1737,11 @@ int ismbuf = (len <= 0); h = mp->d_tx_h; -#ifdef MVETH_TESTING +#ifdef MVETH_TESTING assert( !h->buf_ptr ); - assert( !h->mb ); + assert( !h->u_buf ); #endif - if ( ! (m1 = m_head) ) - return 0; - - if ( ismbuf ) { - /* find first mbuf with actual data */ - while ( 0 == m1->m_len ) { - if ( ! (m1 = m1->m_next) ) { - /* end reached and still no data to send ?? */ - m_freem(m_head); - return 0; - } - } - } - /* Don't use the first descriptor yet because BSP_mve_swipe_tx() * needs mp->d_tx_h->buf_ptr == NULL as a marker. Hence, we * start with the second mbuf and fill the first descriptor @@ -2005,82 +1754,67 @@ int ismbuf = (len <= 0); mp->avail--; nmbs = 1; - if ( ismbuf ) { - register struct mbuf *m; - for ( m=m1->m_next; m; m=m->m_next ) { - if ( 0 == m->m_len ) - continue; /* skip empty mbufs */ - - nmbs++; - - if ( mp->avail < 1 && MVETH_CLEAN_ON_SEND(mp)<=0 ) { - /* Maybe TX was stalled - try to restart */ - mveth_start_tx(mp); - - /* not enough descriptors; cleanup... - * the first slot was never used, so we start - * at mp->d_tx_h->next; - */ - for ( l = NEXT_TXD(h); l!=d; l=NEXT_TXD(l) ) { + while ( (it = next(it)) ) { + if ( 0 == it->len ) + continue; /* skip empty mbufs */ + + nmbs++; + + if ( mp->avail < 1 && MVETH_CLEAN_ON_SEND(mp)<=0 ) { + /* Maybe TX was stalled - try to restart */ + mveth_start_tx(mp); + + /* not enough descriptors; cleanup... + * the first slot was never used, so we start + * at mp->d_tx_h->next; + */ + for ( l = NEXT_TXD(h); l!=d; l=NEXT_TXD(l) ) { #ifdef MVETH_TESTING - assert( l->mb == 0 ); + assert( l->u_buf == 0 ); #endif - l->buf_ptr = 0; - l->cmd_sts = 0; - mp->avail++; - } - mp->avail++; - if ( nmbs > TX_AVAILABLE_RING_SIZE(mp) ) { - /* this chain will never fit into the ring */ - if ( nmbs > mp->stats.maxchain ) - mp->stats.maxchain = nmbs; - mp->stats.repack++; - if ( ! (m_head = repackage_chain(m_head)) ) { - /* no cluster available */ - mp->stats.odrops++; - return 0; - } - goto startover; - } - return -1; - } - - mp->avail--; + l->buf_ptr = 0; + l->cmd_sts = 0; + mp->avail++; + } + mp->avail++; + if ( nmbs > TX_AVAILABLE_RING_SIZE(mp) ) { + /* this chain will never fit into the ring */ + if ( nmbs > mp->stats.maxchain ) + mp->stats.maxchain = nmbs; + mp->stats.repack++; + /* caller may reorganize chain */ + return -2; + } + return -1; + } + + mp->avail--; #ifdef MVETH_TESTING - assert( d != h ); - assert( !d->buf_ptr ); + assert( d != h ); + assert( !d->buf_ptr ); #endif - /* fill this slot */ - rval += mveth_assign_desc(d, m, TDESC_DMA_OWNED); + /* fill this slot */ + rval += mveth_assign_desc_raw(d, it->data, it->len, it->uptr, TDESC_DMA_OWNED); - FLUSH_BUF(mtod(m, uint32_t), m->m_len); + FLUSH_BUF( (uint32_t)it->data, it->len ); - l = d; - d = NEXT_TXD(d); - - FLUSH_DESC(l); - } + l = d; + d = NEXT_TXD(d); - /* fill first slot - don't release to DMA yet */ - rval += mveth_assign_desc(h, m1, TDESC_FRST); + FLUSH_DESC(l); + } + /* fill first slot - don't release to DMA yet */ + rval += mveth_assign_desc_raw(h, head.data, head.len, head.uptr, TDESC_FRST); - FLUSH_BUF(mtod(m1, uint32_t), m1->m_len); - } else { - /* fill first slot with raw buffer - don't release to DMA yet */ - rval += mveth_assign_desc_raw(h, data_p, len, TDESC_FRST); + FLUSH_BUF((uint32_t)head.data, head.len); - FLUSH_BUF( (uint32_t)data_p, len); - } /* tag last slot; this covers the case where 1st==last */ l->cmd_sts |= TDESC_LAST | TDESC_INT_ENA; - /* mbuf goes into last desc */ - l->u_buf = m_head; - FLUSH_DESC(l); @@ -2088,29 +1822,6 @@ int ismbuf = (len <= 0); l->next->cmd_sts = 0; FLUSH_DESC(l->next); -#ifdef MVETH_DEBUG_TX_DUMP - if ( (mveth_tx_dump & (1<port_num)) ) { - int ll,kk; - if ( ismbuf ) { - struct mbuf *m; - for ( kk=0, m=m_head; m; m=m->m_next) { - for ( ll=0; llm_len; ll++ ) { - printf("%02X ",*(mtod(m,char*) + ll)); - if ( ((++kk)&0xf) == 0 ) - printf("\n"); - } - } - } else { - for ( ll=0; lld_tx_h; -#ifdef MVETH_TESTING +#ifdef MVETH_TESTING assert( !h->buf_ptr ); - assert( !h->mb ); + assert( !h->u_buf ); #endif /* find the 'first' user buffer */ - if ( (frst_buf = head_p) ) { + if ( (frst_buf = head_p) && (h_len > 0) ) { frst_len = h_len; } else { frst_buf = data_p; frst_len = d_len; } + uarg = (head_p && ! h_len) ? head_p : frst_buf; + + /* Legacy: if h_len == 0 but head_p is not then use that for the user arg */ + /* Don't use the first descriptor yet because BSP_mve_swipe_tx() * needs mp->d_tx_h->buf_ptr == NULL as a marker. Hence, we * start with the second (optional) slot and fill the first @@ -2196,7 +1912,7 @@ int frst_len; assert( d != h ); assert( !d->buf_ptr ); #endif - rval += mveth_assign_desc_raw(d, data_p, d_len, TDESC_DMA_OWNED); + rval += mveth_assign_desc_raw(d, data_p, d_len, 0, TDESC_DMA_OWNED); FLUSH_BUF( (uint32_t)data_p, d_len ); d->u_buf = data_p; @@ -2207,7 +1923,7 @@ int frst_len; } /* fill first slot with raw buffer - don't release to DMA yet */ - rval += mveth_assign_desc_raw(h, frst_buf, frst_len, TDESC_FRST); + rval += mveth_assign_desc_raw(h, frst_buf, frst_len, 0, TDESC_FRST); FLUSH_BUF( (uint32_t)frst_buf, frst_len); @@ -2215,7 +1931,7 @@ int frst_len; l->cmd_sts |= TDESC_LAST | TDESC_INT_ENA; /* first buffer of 'chain' goes into last desc */ - l->u_buf = frst_buf; + l->u_buf = uarg; FLUSH_DESC(l); @@ -2264,7 +1980,7 @@ uintptr_t baddr; for ( d = mp->d_rx_t; ! (INVAL_DESC(d), (RDESC_DMA_OWNED & d->cmd_sts)); d=NEXT_RXD(d) ) { -#ifdef MVETH_TESTING +#ifdef MVETH_TESTING assert(d->u_buf); #endif @@ -2273,7 +1989,7 @@ uintptr_t baddr; if ( err || !(newbuf = mp->alloc_rxbuf(&sz, &baddr)) ) { /* drop packet and recycle buffer */ newbuf = d->u_buf; - mp->consume_rxbuf(0, mp->consume_rxbuf_arg, err ? -1 : 0); + mp->stats.idrops++; } else { #ifdef MVETH_TESTING assert( d->byte_cnt > 0 ); @@ -2326,7 +2042,7 @@ int i; BSP_mve_swipe_tx(mp); -#ifdef MVETH_TESTING +#ifdef MVETH_TESTING assert( mp->d_tx_h == mp->d_tx_t ); for ( i=0, d=mp->tx_ring; ixbuf_count; i++, d++ ) { assert( !d->buf_ptr ); @@ -2380,7 +2096,7 @@ uint32_t mveth_serial_ctrl_config_val = MVETH_SERIAL_CTRL_CONFIG_VAL; */ void -BSP_mve_init_hw(struct mveth_private *mp, int promisc, unsigned char *enaddr) +BSP_mve_init_hw(struct mveth_private *mp, int promisc, unsigned char *enaddr, int media) { int i; uint32_t v; @@ -2469,7 +2185,7 @@ static int inited = 0; assert( maclo && machi && maclo != 0xffffffff && machi != 0xffffffff ); mveth_ucfilter(mp, maclo&0xff, 1/* accept */); } - + /* port, serial and sdma configuration */ v = MVETH_PORT_CONFIG_VAL; if ( promisc ) { @@ -2490,11 +2206,8 @@ static int inited = 0; v |= mveth_serial_ctrl_config_val; MV_WRITE(MV643XX_ETH_SERIAL_CONTROL_R(mp->port_num), v); - i = IFM_MAKEWORD(0, 0, 0, 0); - if ( 0 == BSP_mve_media_ioctl(mp, SIOCGIFMEDIA, &i) ) { - if ( (IFM_LINK_OK & i) ) { - mveth_update_serial_port(mp, i); - } + if ( (MV643XX_MEDIA_LINK & media) ) { + BSP_mve_update_serial_port(mp, media); } /* enable serial port */ @@ -2560,26 +2273,6 @@ unsigned char buf[6], *eaddr; } } -int -BSP_mve_media_ioctl(struct mveth_private *mp, int cmd, int *parg) -{ -int rval; - /* alias cmd == 0,1 */ - switch ( cmd ) { - case 0: cmd = SIOCGIFMEDIA; - break; - case 1: cmd = SIOCSIFMEDIA; - case SIOCGIFMEDIA: - case SIOCSIFMEDIA: - break; - default: return -1; - } - REGLOCK(); - rval = rtems_mii_ioctl(&mveth_mdio, mp, cmd, parg); - REGUNLOCK(); - return rval; -} - void BSP_mve_enable_irqs(struct mveth_private *mp) { @@ -2617,134 +2310,8 @@ BSP_mve_ack_irq_mask(struct mveth_private *mp, uint32_t mask) return mveth_ack_irqs(mp, mask); } -int -BSP_mve_ack_link_chg(struct mveth_private *mp, int *pmedia) -{ -int media = IFM_MAKEWORD(0,0,0,0); - - if ( 0 == BSP_mve_media_ioctl(mp, SIOCGIFMEDIA, &media)) { - if ( IFM_LINK_OK & media ) { - mveth_update_serial_port(mp, media); - /* If TX stalled because there was no buffer then whack it */ - mveth_start_tx(mp); - } - if ( pmedia ) - *pmedia = media; - return 0; - } - return -1; -} - -/* BSDNET SUPPORT/GLUE ROUTINES */ - -static void -mveth_set_filters(struct ifnet *ifp); - -STATIC void -mveth_stop(struct mveth_softc *sc) -{ - BSP_mve_stop_hw(&sc->pvt); - sc->arpcom.ac_if.if_timer = 0; -} - -/* allocate a mbuf for RX with a properly aligned data buffer - * RETURNS 0 if allocation fails - */ -static void * -alloc_mbuf_rx(int *psz, uintptr_t *paddr) -{ -struct mbuf *m; -unsigned long l,o; - - MGETHDR(m, M_DONTWAIT, MT_DATA); - if ( !m ) - return 0; - MCLGET(m, M_DONTWAIT); - if ( ! (m->m_flags & M_EXT) ) { - m_freem(m); - return 0; - } - - o = mtod(m, unsigned long); - l = MV643XX_ALIGN(o, RX_BUF_ALIGNMENT) - o; - - /* align start of buffer */ - m->m_data += l; - - /* reduced length */ - l = MCLBYTES - l; - - m->m_len = m->m_pkthdr.len = l; - *psz = m->m_len; - *paddr = mtod(m, uintptr_t); - - return (void*) m; -} - -static void consume_rx_mbuf(void *buf, void *arg, int len) -{ -struct ifnet *ifp = arg; -struct mbuf *m = buf; - - if ( len <= 0 ) { - ifp->if_iqdrops++; - if ( len < 0 ) { - ifp->if_ierrors++; - } - if ( m ) - m_freem(m); - } else { - struct ether_header *eh; - - eh = (struct ether_header *)(mtod(m, unsigned long) + ETH_RX_OFFSET); - m->m_len = m->m_pkthdr.len = len - sizeof(struct ether_header) - ETH_RX_OFFSET - ETH_CRC_LEN; - m->m_data += sizeof(struct ether_header) + ETH_RX_OFFSET; - m->m_pkthdr.rcvif = ifp; - - ifp->if_ipackets++; - ifp->if_ibytes += m->m_pkthdr.len; - - if (0) { - /* Low-level debugging */ - int i; - for (i=0; i<13; i++) { - printf("%02X:",((char*)eh)[i]); - } - printf("%02X\n",((char*)eh)[i]); - for (i=0; im_len; i++) { - if ( !(i&15) ) - printf("\n"); - printf("0x%02x ",mtod(m,char*)[i]); - } - printf("\n"); - } - - if (0) { - /* Low-level debugging/testing without bsd stack */ - m_freem(m); - } else { - /* send buffer upwards */ - ether_input(ifp, eh, m); - } - } -} - -static void release_tx_mbuf(void *buf, void *arg, int err) -{ -struct ifnet *ifp = arg; -struct mbuf *mb = buf; - - if ( err ) { - ifp->if_oerrors++; - } else { - ifp->if_opackets++; - } - ifp->if_obytes += mb->m_pkthdr.len; - m_freem(mb); -} - -static void -dump_update_stats(struct mveth_private *mp, FILE *f) +void +BSP_mve_dump_stats(struct mveth_private *mp, FILE *f) { int p = mp->port_num; int idx; @@ -2758,6 +2325,7 @@ uint32_t v; fprintf(f, " Max. mbuf chain length: %i\n", mp->stats.maxchain); fprintf(f, " # repacketed: %i\n", mp->stats.repack); fprintf(f, " # packets: %i\n", mp->stats.packet); + fprintf(f, " # buffer alloc failed: %i\n", mp->stats.idrops); fprintf(f, "MIB Counters:\n"); for ( idx = MV643XX_ETH_MIB_GOOD_OCTS_RCVD_LO>>2; idx < MV643XX_ETH_NUM_MIB_COUNTERS; @@ -2784,535 +2352,34 @@ uint32_t v; fprintf(f, "\n"); } -void -BSP_mve_dump_stats(struct mveth_private *mp, FILE *f) -{ - dump_update_stats(mp, f); -} - -/* BSDNET DRIVER CALLBACKS */ - -static void -mveth_init(void *arg) -{ -struct mveth_softc *sc = arg; -struct ifnet *ifp = &sc->arpcom.ac_if; -int media; - - BSP_mve_init_hw(&sc->pvt, ifp->if_flags & IFF_PROMISC, sc->arpcom.ac_enaddr); - - media = IFM_MAKEWORD(0, 0, 0, 0); - if ( 0 == BSP_mve_media_ioctl(&sc->pvt, SIOCGIFMEDIA, &media) ) { - if ( (IFM_LINK_OK & media) ) { - ifp->if_flags &= ~IFF_OACTIVE; - } else { - ifp->if_flags |= IFF_OACTIVE; - } - } - - /* if promiscuous then there is no need to change */ - if ( ! (ifp->if_flags & IFF_PROMISC) ) - mveth_set_filters(ifp); - - ifp->if_flags |= IFF_RUNNING; - sc->arpcom.ac_if.if_timer = 0; -} - -/* bsdnet driver entry to start transmission */ -static void -mveth_start(struct ifnet *ifp) -{ -struct mveth_softc *sc = ifp->if_softc; -struct mbuf *m = 0; - - while ( ifp->if_snd.ifq_head ) { - IF_DEQUEUE( &ifp->if_snd, m ); - if ( BSP_mve_send_buf(&sc->pvt, m, 0, 0) < 0 ) { - IF_PREPEND( &ifp->if_snd, m); - ifp->if_flags |= IFF_OACTIVE; - break; - } - /* need to do this really only once - * but it's cheaper this way. - */ - ifp->if_timer = 2*IFNET_SLOWHZ; - } -} - -/* bsdnet driver entry; */ -static void -mveth_watchdog(struct ifnet *ifp) -{ -struct mveth_softc *sc = ifp->if_softc; - - ifp->if_oerrors++; - printk(DRVNAME"%i: watchdog timeout; resetting\n", ifp->if_unit); - - mveth_init(sc); - mveth_start(ifp); -} - -static void -mveth_set_filters(struct ifnet *ifp) -{ -struct mveth_softc *sc = ifp->if_softc; -uint32_t v; - - v = MV_READ(MV643XX_ETH_PORT_CONFIG_R(sc->pvt.port_num)); - if ( ifp->if_flags & IFF_PROMISC ) - v |= MV643XX_ETH_UNICAST_PROMISC_MODE; - else - v &= ~MV643XX_ETH_UNICAST_PROMISC_MODE; - MV_WRITE(MV643XX_ETH_PORT_CONFIG_R(sc->pvt.port_num), v); - - if ( ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI) ) { - BSP_mve_mcast_filter_accept_all(&sc->pvt); - } else { - struct ether_multi *enm; - struct ether_multistep step; - - BSP_mve_mcast_filter_clear( &sc->pvt ); - - ETHER_FIRST_MULTI(step, (struct arpcom *)ifp, enm); - - while ( enm ) { - if ( memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN) ) - assert( !"Should never get here; IFF_ALLMULTI should be set!" ); - - BSP_mve_mcast_filter_accept_add(&sc->pvt, enm->enm_addrlo); - - ETHER_NEXT_MULTI(step, enm); - } - } -} - -/* bsdnet driver ioctl entry */ -static int -mveth_ioctl(struct ifnet *ifp, ioctl_command_t cmd, caddr_t data) -{ -struct mveth_softc *sc = ifp->if_softc; -struct ifreq *ifr = (struct ifreq *)data; -int error = 0; -int f; - - switch ( cmd ) { - case SIOCSIFFLAGS: - f = ifp->if_flags; - if ( f & IFF_UP ) { - if ( ! ( f & IFF_RUNNING ) ) { - mveth_init(sc); - } else { - if ( (f & IFF_PROMISC) != (sc->bsd.oif_flags & IFF_PROMISC) ) { - /* Note: in all other scenarios the 'promisc' flag - * in the low-level driver [which affects the way - * the multicast filter is setup: accept none vs. - * accept all in promisc mode] is eventually - * set when the IF is brought up... - */ - sc->pvt.promisc = (f & IFF_PROMISC); - - mveth_set_filters(ifp); - } - /* FIXME: other flag changes are ignored/unimplemented */ - } - } else { - if ( f & IFF_RUNNING ) { - mveth_stop(sc); - ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); - } - } - sc->bsd.oif_flags = ifp->if_flags; - break; - - case SIOCGIFMEDIA: - case SIOCSIFMEDIA: - error = BSP_mve_media_ioctl(&sc->pvt, cmd, &ifr->ifr_media); - break; - - case SIOCADDMULTI: - case SIOCDELMULTI: - error = (cmd == SIOCADDMULTI) - ? ether_addmulti(ifr, &sc->arpcom) - : ether_delmulti(ifr, &sc->arpcom); - - if (error == ENETRESET) { - if (ifp->if_flags & IFF_RUNNING) { - mveth_set_filters(ifp); - } - error = 0; - } - break; - - - break; - - case SIO_RTEMS_SHOW_STATS: - dump_update_stats(&sc->pvt, stdout); - break; - - default: - error = ether_ioctl(ifp, cmd, data); - break; - } - - return error; -} - -/* DRIVER TASK */ - -/* Daemon task does all the 'interrupt' work */ -static void mveth_daemon(void *arg) -{ -struct mveth_softc *sc; -struct ifnet *ifp; -rtems_event_set evs; - for (;;) { - rtems_bsdnet_event_receive( 7, RTEMS_WAIT | RTEMS_EVENT_ANY, RTEMS_NO_TIMEOUT, &evs ); - evs &= 7; - for ( sc = theMvEths; evs; evs>>=1, sc++ ) { - if ( (evs & 1) ) { - register uint32_t x; - - ifp = &sc->arpcom.ac_if; - - if ( !(ifp->if_flags & IFF_UP) ) { - mveth_stop(sc); - ifp->if_flags &= ~(IFF_UP|IFF_RUNNING); - continue; - } - - if ( !(ifp->if_flags & IFF_RUNNING) ) { - /* event could have been pending at the time hw was stopped; - * just ignore... - */ - continue; - } - - x = mveth_ack_irqs(&sc->pvt, -1); - - if ( MV643XX_ETH_EXT_IRQ_LINK_CHG & x ) { - /* phy status changed */ - int media; - - if ( 0 == BSP_mve_ack_link_chg(&sc->pvt, &media) ) { - if ( IFM_LINK_OK & media ) { - ifp->if_flags &= ~IFF_OACTIVE; - mveth_start(ifp); - } else { - /* stop sending */ - ifp->if_flags |= IFF_OACTIVE; - } - } - } - /* free tx chain */ - if ( (MV643XX_ETH_EXT_IRQ_TX_DONE & x) && BSP_mve_swipe_tx(&sc->pvt) ) { - ifp->if_flags &= ~IFF_OACTIVE; - if ( TX_AVAILABLE_RING_SIZE(&sc->pvt) == sc->pvt.avail ) - ifp->if_timer = 0; - mveth_start(ifp); - } - if ( (MV643XX_ETH_IRQ_RX_DONE & x) ) - BSP_mve_swipe_rx(&sc->pvt); - - mveth_enable_irqs(&sc->pvt, -1); - } - } - } -} - -#ifdef MVETH_DETACH_HACK -static int mveth_detach(struct mveth_softc *sc); -#endif - - -/* PUBLIC RTEMS BSDNET ATTACH FUNCTION */ -int -rtems_mve_attach(struct rtems_bsdnet_ifconfig *ifcfg, int attaching) -{ -char *unitName; -int unit,i,cfgUnits; -struct mveth_softc *sc; -struct ifnet *ifp; - - unit = rtems_bsdnet_parse_driver_name(ifcfg, &unitName); - if ( unit <= 0 || unit > MV643XXETH_NUM_DRIVER_SLOTS ) { - printk(DRVNAME": Bad unit number %i; must be 1..%i\n", unit, MV643XXETH_NUM_DRIVER_SLOTS); - return 1; - } - - sc = &theMvEths[unit-1]; - ifp = &sc->arpcom.ac_if; - sc->pvt.port_num = unit-1; - sc->pvt.phy = (MV_READ(MV643XX_ETH_PHY_ADDR_R) >> (5*sc->pvt.port_num)) & 0x1f; - - if ( attaching ) { - if ( ifp->if_init ) { - printk(DRVNAME": instance %i already attached.\n", unit); - return -1; - } - - for ( i=cfgUnits = 0; irbuf_count, - ifcfg->xbuf_count, - BSP_MVE_IRQ_TX | BSP_MVE_IRQ_RX | BSP_MVE_IRQ_LINK) ) { - return -1; - } - - if ( nmbclusters < sc->pvt.rbuf_count * cfgUnits + 60 /* arbitrary */ ) { - printk(DRVNAME"%i: (mv643xx ethernet) Your application has not enough mbuf clusters\n", unit); - printk( " configured for this driver.\n"); - return -1; - } - - if ( ifcfg->hardware_address ) { - memcpy(sc->arpcom.ac_enaddr, ifcfg->hardware_address, ETHER_ADDR_LEN); - } else { - /* read back from hardware assuming that MotLoad already had set it up */ - BSP_mve_read_eaddr(&sc->pvt, sc->arpcom.ac_enaddr); - } - - ifp->if_softc = sc; - ifp->if_unit = unit; - ifp->if_name = unitName; - - ifp->if_mtu = ifcfg->mtu ? ifcfg->mtu : ETHERMTU; - - ifp->if_init = mveth_init; - ifp->if_ioctl = mveth_ioctl; - ifp->if_start = mveth_start; - ifp->if_output = ether_output; - /* - * While nonzero, the 'if->if_timer' is decremented - * (by the networking code) at a rate of IFNET_SLOWHZ (1hz) and 'if_watchdog' - * is called when it expires. - * If either of those fields is 0 the feature is disabled. - */ - ifp->if_watchdog = mveth_watchdog; - ifp->if_timer = 0; - - sc->bsd.oif_flags = /* ... */ - ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX; - - /* - * if unset, this set to 10Mbps by ether_ifattach; seems to be unused by bsdnet stack; - * could be updated along with phy speed, though... - ifp->if_baudrate = 10000000; - */ - - /* NOTE: ether_output drops packets if ifq_len >= ifq_maxlen - * but this is the packet count, not the fragment count! - ifp->if_snd.ifq_maxlen = sc->pvt.xbuf_count; - */ - ifp->if_snd.ifq_maxlen = ifqmaxlen; - -#ifdef MVETH_DETACH_HACK - if ( !ifp->if_addrlist ) /* do only the first time [reattach hack] */ -#endif - { - if_attach(ifp); - ether_ifattach(ifp); - } - - } else { -#ifdef MVETH_DETACH_HACK - if ( !ifp->if_init ) { - printk(DRVNAME": instance %i not attached.\n", unit); - return -1; - } - return mveth_detach(sc); -#else - printk(DRVNAME": interface detaching not implemented\n"); - return -1; -#endif - } - - return 0; -} - -/* EARLY PHY ACCESS */ -static int -mveth_early_init(int idx) -{ - if ( idx < 0 || idx >= MV643XXETH_NUM_DRIVER_SLOTS ) - return -1; - - /* determine the phy */ - theMvEths[idx].pvt.phy = (MV_READ(MV643XX_ETH_PHY_ADDR_R) >> (5*idx)) & 0x1f; - return 0; -} - -static int -mveth_early_read_phy(int idx, unsigned reg) -{ -int rval; - - if ( idx < 0 || idx >= MV643XXETH_NUM_DRIVER_SLOTS ) - return -1; - - rval = mveth_mii_read(&theMvEths[idx].pvt, reg); - return rval < 0 ? rval : rval & 0xffff; -} - -static int -mveth_early_write_phy(int idx, unsigned reg, unsigned val) -{ - if ( idx < 0 || idx >= MV643XXETH_NUM_DRIVER_SLOTS ) - return -1; - - mveth_mii_write(&theMvEths[idx].pvt, reg, val); - return 0; -} - -rtems_bsdnet_early_link_check_ops -rtems_mve_early_link_check_ops = { - init: mveth_early_init, - read_phy: mveth_early_read_phy, - write_phy: mveth_early_write_phy, - name: DRVNAME, - num_slots: MAX_NUM_SLOTS -}; - -/* DEBUGGING */ - #ifdef MVETH_DEBUG /* Display/dump descriptor rings */ +/* These low-level routines need to be synchronized with + * any Tx/Rx threads! + */ int -mveth_dring(struct mveth_softc *sc) +BSP_mve_dring_nonsync(struct mveth_private *mp) { int i; if (1) { MvEthRxDesc pr; printf("RX:\n"); - for (i=0, pr=sc->pvt.rx_ring; ipvt.rbuf_count; i++, pr++) { -#ifndef ENABLE_HW_SNOOPING - /* can't just invalidate the descriptor - if it contains - * data that hasn't been flushed yet, we create an inconsistency... - */ - rtems_bsdnet_semaphore_obtain(); - INVAL_DESC(pr); -#endif + for (i=0, pr=mp->rx_ring; irbuf_count; i++, pr++) { printf("cnt: 0x%04x, size: 0x%04x, stat: 0x%08x, next: 0x%08x, buf: 0x%08x\n", pr->byte_cnt, pr->buf_size, pr->cmd_sts, (uint32_t)pr->next_desc_ptr, pr->buf_ptr); - -#ifndef ENABLE_HW_SNOOPING - rtems_bsdnet_semaphore_release(); -#endif } } if (1) { MvEthTxDesc pt; printf("TX:\n"); - for (i=0, pt=sc->pvt.tx_ring; ipvt.xbuf_count; i++, pt++) { -#ifndef ENABLE_HW_SNOOPING - rtems_bsdnet_semaphore_obtain(); - INVAL_DESC(pt); -#endif + for (i=0, pt=mp->tx_ring; ixbuf_count; i++, pt++) { printf("cnt: 0x%04x, stat: 0x%08x, next: 0x%08x, buf: 0x%08x, mb: 0x%08x\n", pt->byte_cnt, pt->cmd_sts, (uint32_t)pt->next_desc_ptr, pt->buf_ptr, - (uint32_t)pt->mb); - -#ifndef ENABLE_HW_SNOOPING - rtems_bsdnet_semaphore_release(); -#endif - } -} - return 0; -} - -#endif - -/* DETACH HACK DETAILS */ - -#ifdef MVETH_DETACH_HACK -int -_cexpModuleFinalize(void *mh) -{ -int i; - for ( i=0; iu_buf); } - /* delete task; since there are no attached interfaces, it should block - * for events and hence not hold the semaphore or other resources... - */ - rtems_task_delete(mveth_tid); - return 0; -} - -/* ugly hack to allow unloading/reloading the driver core. - * needed because rtems' bsdnet release doesn't implement - * if_detach(). Therefore, we bring the interface down but - * keep the device record alive... - */ -static void -ether_ifdetach_pvt(struct ifnet *ifp) -{ - ifp->if_flags = 0; - ifp->if_ioctl = 0; - ifp->if_start = 0; - ifp->if_watchdog = 0; - ifp->if_init = 0; } - -static int -mveth_detach(struct mveth_softc *sc) -{ -struct ifnet *ifp = &sc->arpcom.ac_if; - if ( ifp->if_init ) { - if ( ifp->if_flags & (IFF_UP | IFF_RUNNING) ) { - printf(DRVNAME"%i: refuse to detach; interface still up\n",sc->pvt.port_num+1); - return -1; - } - mveth_stop(sc); -/* not implemented in BSDnet/RTEMS (yet) but declared in header */ -#define ether_ifdetach ether_ifdetach_pvt - ether_ifdetach(ifp); - } - free( (void*)sc->pvt.ring_area, M_DEVBUF ); - sc->pvt.ring_area = 0; - sc->pvt.tx_ring = 0; - sc->pvt.rx_ring = 0; - sc->pvt.d_tx_t = sc->pvt.d_tx_h = 0; - sc->pvt.d_rx_t = 0; - sc->pvt.avail = 0; - /* may fail if ISR was not installed yet */ - BSP_remove_rtems_irq_handler( &irq_data[sc->pvt.port_num] ); return 0; } - -#ifdef MVETH_DEBUG -struct rtems_bsdnet_ifconfig mveth_dbg_config = { - name: DRVNAME"1", - attach: rtems_mve_attach, - ip_address: "192.168.2.10", /* not used by rtems_bsdnet_attach */ - ip_netmask: "255.255.255.0", /* not used by rtems_bsdnet_attach */ - hardware_address: 0, /* (void *) */ - ignore_broadcast: 0, /* TODO driver should honour this */ - mtu: 0, - rbuf_count: 0, /* TODO driver should honour this */ - xbuf_count: 0, /* TODO driver should honour this */ -}; -#endif #endif diff --git a/bsps/powerpc/beatnik/net/if_mve/mv643xx_eth_bsdnet.c b/bsps/powerpc/beatnik/net/if_mve/mv643xx_eth_bsdnet.c new file mode 100644 index 00000000000..41cb1493318 --- /dev/null +++ b/bsps/powerpc/beatnik/net/if_mve/mv643xx_eth_bsdnet.c @@ -0,0 +1,1147 @@ +/* RTEMS driver for the mv643xx gigabit ethernet chip */ + +/* Acknowledgement: + * + * Valuable information for developing this driver was obtained + * from the linux open-source driver mv643xx_eth.c which was written + * by the following people and organizations: + * + * Matthew Dharm + * rabeeh@galileo.co.il + * PMC-Sierra, Inc., Manish Lachwani + * Ralf Baechle + * MontaVista Software, Inc., Dale Farnsworth + * Steven J. Hill / + * + * Note however, that in spite of the identical name of this file + * (and some of the symbols used herein) this file provides a + * new implementation and is the original work by the author. + */ + +/* + * Authorship + * ---------- + * This software (mv643xx ethernet driver for RTEMS) was + * created by Till Straumann , 2005-2007, + * Stanford Linear Accelerator Center, Stanford University. + * + * Acknowledgement of sponsorship + * ------------------------------ + * The 'mv643xx ethernet driver for RTEMS' was produced by + * the Stanford Linear Accelerator Center, Stanford University, + * under Contract DE-AC03-76SFO0515 with the Department of Energy. + * + * Government disclaimer of liability + * ---------------------------------- + * Neither the United States nor the United States Department of Energy, + * nor any of their employees, makes any warranty, express or implied, or + * assumes any legal liability or responsibility for the accuracy, + * completeness, or usefulness of any data, apparatus, product, or process + * disclosed, or represents that its use would not infringe privately owned + * rights. + * + * Stanford disclaimer of liability + * -------------------------------- + * Stanford University makes no representations or warranties, express or + * implied, nor assumes any liability for the use of this software. + * + * Stanford disclaimer of copyright + * -------------------------------- + * Stanford University, owner of the copyright, hereby disclaims its + * copyright and all other rights in this software. Hence, anyone may + * freely use it for any purpose without restriction. + * + * Maintenance of notices + * ---------------------- + * In the interest of clarity regarding the origin and status of this + * SLAC software, this and all the preceding Stanford University notices + * are to remain affixed to any copy or derivative of this software made + * or distributed by the recipient and are to be affixed to any copy of + * software made or distributed by the recipient that contains a copy or + * derivative of this software. + * + * ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03 + */ + +/* + * NOTE: Some register (e.g., the SMI register) are SHARED among the + * three devices. Concurrent access protection is provided by + * the global networking semaphore. + * If other drivers are running on a subset of IFs then proper + * locking of all shared registers must be implemented! + * + * Some things I learned about this hardware can be found + * further down... + */ + +#ifndef KERNEL +#define KERNEL +#endif +#ifndef _KERNEL +#define _KERNEL +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +/* Not so nice; would be more elegant not to depend on C library but the + * RTEMS-specific ioctl for dumping statistics needs stdio anyways. + */ + +/*#define NDEBUG effectively removes all assertions + * If defining NDEBUG, MAKE SURE assert() EXPRESSION HAVE NO SIDE_EFFECTS!! + * This driver DOES have side-effects, so DONT DEFINE NDEBUG + * Performance-critical assertions are removed by undefining MVETH_TESTING. + */ + +#undef NDEBUG +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + +/* CONFIGURABLE PARAMETERS */ + +/* Compile-time debugging features */ + +/* Enable paranoia assertions and checks; reduce # of descriptors to minimum for stressing */ +#undef MVETH_TESTING + +/* Enable debugging messages and some support routines (dump rings etc.) */ +#undef MVETH_DEBUG + +/* Ring sizes */ + +#ifdef MVETH_TESTING + +/* hard and small defaults */ +#undef MV643XX_RX_RING_SIZE +#define MV643XX_RX_RING_SIZE 2 +#undef MV643XX_TX_RING_SIZE +#define MV643XX_TX_RING_SIZE 4 + +#else /* MVETH_TESTING */ + +/* Define default ring sizes, allow override from bsp.h, Makefile,... and from ifcfg->rbuf_count/xbuf_count */ + +#ifndef MV643XX_RX_RING_SIZE +#define MV643XX_RX_RING_SIZE 40 /* attached buffers are always 2k clusters, i.e., this + * driver - with a configured ring size of 40 - constantly + * locks 80k of cluster memory - your app config better + * provides enough space! + */ +#endif + +#ifndef MV643XX_TX_RING_SIZE +/* NOTE: tx ring size MUST be > max. # of fragments / mbufs in a chain; + * in 'TESTING' mode, special code is compiled in to repackage + * chains that are longer than the ring size. Normally, this is + * disabled for sake of speed. + * I observed chains of >17 entries regularly! + */ +#define MV643XX_TX_RING_SIZE 200 /* these are smaller fragments and not occupied when + * the driver is idle. + */ +#endif + +#endif /* MVETH_TESTING */ + +#define DRVNAME "mve" +#define MAX_NUM_SLOTS 3 + +#ifdef NDEBUG +#error "Driver uses assert() statements with side-effects; MUST NOT define NDEBUG" +#endif + +#ifdef MVETH_DEBUG +#define STATIC +#else +#define STATIC static +#endif + +/* macros for ring alignment; proper alignment is a hardware req; . */ + +/* Software cache management */ + +#ifndef __PPC__ +#error "Dont' know how to deal with cache on this CPU architecture" +#endif + +/* Ring entries are 32 bytes; coherency-critical chunks are 16 -> software coherency + * management works for cache line sizes of 16 and 32 bytes only. If the line size + * is bigger, the descriptors could be padded... + */ +#if PPC_CACHE_ALIGMENT != 16 && PPC_CACHE_ALIGNMENT != 32 +#error "Cache line size must be 16 or 32" +#else +#define RX_BUF_ALIGNMENT PPC_CACHE_ALIGNMENT +#endif + +/* HELPER MACROS */ + +/* Align base to alignment 'a' */ +#define MV643XX_ALIGN(b, a) ((((uint32_t)(b)) + (a)-1) & (~((a)-1))) + +/* The chip puts the ethernet header at offset 2 into the buffer so + * that the payload is aligned + */ +#define ETH_RX_OFFSET 2 +#define ETH_CRC_LEN 4 /* strip FCS at end of packet */ + +/* TYPE DEFINITIONS */ + +/* just to make the purpose explicit; vars of this + * type may need CPU-dependent address translation, + * endian conversion etc. + */ +typedef uint32_t Dma_addr_t; + +/* stuff needed for bsdnet support */ +struct mveth_bsdsupp { + int oif_flags; /* old / cached if_flags */ + int tx_ring_size; + int rx_ring_size; +}; + +struct mveth_softc { + struct arpcom arpcom; + struct mveth_bsdsupp bsd; + struct mveth_private *pvt; +}; + +typedef struct BsdMveIter { + MveEthBufIter it; + struct mbuf *next; + struct mbuf *head; +} BsdMveIter; + +/* GLOBAL VARIABLES */ +#ifdef MVETH_DEBUG_TX_DUMP +int mveth_tx_dump = 0; +#endif + +/* register access protection mutex */ +STATIC rtems_id mveth_mtx = 0; + +#define REGLOCK() do { \ + if ( RTEMS_SUCCESSFUL != rtems_semaphore_obtain(mveth_mtx, RTEMS_WAIT, RTEMS_NO_TIMEOUT) ) \ + rtems_panic(DRVNAME": unable to lock register protection mutex"); \ + } while (0) +#define REGUNLOCK() rtems_semaphore_release(mveth_mtx) + +/* THE array of driver/bsdnet structs */ + +STATIC +struct mveth_softc theMvEths[MV643XXETH_NUM_DRIVER_SLOTS] += {{{{0}},}} +; + +/* daemon task id */ +STATIC rtems_id mveth_tid = 0; + +/* MII Ioctl Interface */ + +/* mdio / mii interface wrappers for rtems_mii_ioctl API */ + +static int mveth_mdio_r(int phy, void *uarg, unsigned reg, uint32_t *pval) +{ + if ( phy > 1 ) + return -1; + + *pval = BSP_mve_mii_read(uarg, reg); + return 0; +} + +static int mveth_mdio_w(int phy, void *uarg, unsigned reg, uint32_t val) +{ + if ( phy > 1 ) + return -1; + BSP_mve_mii_write(uarg, reg, val); + return 0; +} + +static struct rtems_mdio_info mveth_mdio = { + mdio_r: mveth_mdio_r, + mdio_w: mveth_mdio_w, + has_gmii: 1, +}; + +static struct mveth_private * +mve_setup_bsd( + int unit, + rtems_id tid, + void (*isr)(void *isr_arg), + void *isr_arg, + void (*cleanup_txbuf)(void *user_buf, void *closure, int error_on_tx_occurred), + void *cleanup_txbuf_arg, + void *(*alloc_rxbuf)(int *p_size, uintptr_t *p_data_addr), + void (*consume_rxbuf)(void *user_buf, void *closure, int len), + void *consume_rxbuf_arg, + int rx_ring_size, + int tx_ring_size, + int irq_mask +) +{ +struct ifnet *ifp; +struct mveth_private *mp; + + if ( unit <= 0 || unit > MV643XXETH_NUM_DRIVER_SLOTS ) { + printk(DRVNAME": Bad unit number %i; must be 1..%i\n", unit, MV643XXETH_NUM_DRIVER_SLOTS); + return 0; + } + + ifp = &theMvEths[unit-1].arpcom.ac_if; + + if ( ifp->if_init ) { + if ( ifp->if_init ) { + printk(DRVNAME": instance %i already attached.\n", unit); + return 0; + } + } + + mp = theMvEths[unit-1].pvt; + + if ( ! (mp = BSP_mve_create( + unit, + tid, + isr, isr_arg, + cleanup_txbuf, cleanup_txbuf_arg, + alloc_rxbuf, + consume_rxbuf, consume_rxbuf_arg, + rx_ring_size, tx_ring_size, + irq_mask)) ) { + return 0; + } + theMvEths[unit-1].pvt = mp; + + /* lazy init of mutex (non thread-safe! - we assume 1st initialization is single-threaded) */ + if ( ! mveth_mtx ) { + rtems_status_code sc; + sc = rtems_semaphore_create( + rtems_build_name('m','v','e','X'), + 1, + RTEMS_BINARY_SEMAPHORE | RTEMS_PRIORITY | RTEMS_INHERIT_PRIORITY | RTEMS_DEFAULT_ATTRIBUTES, + 0, + &mveth_mtx); + if ( RTEMS_SUCCESSFUL != sc ) { + rtems_error(sc,DRVNAME": creating mutex\n"); + rtems_panic("unable to proceed\n"); + } + } + + theMvEths[unit-1].bsd.tx_ring_size = tx_ring_size; + theMvEths[unit-1].bsd.rx_ring_size = rx_ring_size; + + /* mark as used */ + ifp->if_init = (void*)(-1); + + return mp; +} + +struct mveth_private * +BSP_mve_setup( + int unit, + rtems_id tid, + void (*cleanup_txbuf)(void *user_buf, void *closure, int error_on_tx_occurred), + void *cleanup_txbuf_arg, + void *(*alloc_rxbuf)(int *p_size, uintptr_t *p_data_addr), + void (*consume_rxbuf)(void *user_buf, void *closure, int len), + void *consume_rxbuf_arg, + int rx_ring_size, + int tx_ring_size, + int irq_mask +) +{ + if ( irq_mask && 0 == tid ) { + printk(DRVNAME": must supply a TID if irq_msk not zero\n"); + return 0; + } + + return mve_setup_bsd( + unit, + tid, + 0, 0, + cleanup_txbuf, cleanup_txbuf_arg, + alloc_rxbuf, + consume_rxbuf, consume_rxbuf_arg, + rx_ring_size, tx_ring_size, + irq_mask); +} + +struct mveth_private * +BSP_mve_setup_1( + int unit, + void (*isr)(void *isr_arg), + void *isr_arg, + void (*cleanup_txbuf)(void *user_buf, void *closure, int error_on_tx_occurred), + void *cleanup_txbuf_arg, + void *(*alloc_rxbuf)(int *p_size, uintptr_t *p_data_addr), + void (*consume_rxbuf)(void *user_buf, void *closure, int len), + void *consume_rxbuf_arg, + int rx_ring_size, + int tx_ring_size, + int irq_mask +) +{ + if ( irq_mask && 0 == isr ) { + printk(DRVNAME": must supply an ISR if irq_msk not zero\n"); + return 0; + } + + return mve_setup_bsd( + unit, + 0, + isr, isr_arg, + cleanup_txbuf, cleanup_txbuf_arg, + alloc_rxbuf, + consume_rxbuf, consume_rxbuf_arg, + rx_ring_size, tx_ring_size, + irq_mask); +} + +/* allocate a new cluster and copy an existing chain there; + * old chain is released... + */ +static struct mbuf * +repackage_chain(struct mbuf *m_head) +{ +struct mbuf *m; + MGETHDR(m, M_DONTWAIT, MT_DATA); + + if ( !m ) { + goto bail; + } + + MCLGET(m, M_DONTWAIT); + + if ( !(M_EXT & m->m_flags) ) { + m_freem(m); + m = 0; + goto bail; + } + + m_copydata(m_head, 0, MCLBYTES, mtod(m, caddr_t)); + m->m_pkthdr.len = m->m_len = m_head->m_pkthdr.len; + +bail: + m_freem(m_head); + return m; +} + +static inline struct mbuf *skipEmpty(struct mbuf *m) +{ + while ( m && ( 0 == m->m_len ) ) { + m = m->m_next; + } + return m; +} + +static MveEthBufIter *nextBuf(MveEthBufIter *arg) +{ +BsdMveIter *it = (BsdMveIter*)arg; +struct mbuf *m; + if ( (m = it->next) ) { + it->next = skipEmpty( m->m_next ); + it->it.data = mtod(m, void*); + it->it.len = m->m_len; + it->it.uptr = it->next ? 0 : it->head; + return (MveEthBufIter*)it; + } + return 0; +} + +static MveEthBufIter *initIter(BsdMveIter *it, struct mbuf *m) +{ + it->head = m; + it->next = skipEmpty( m ); + /* Fill with first buf info */ + return nextBuf( &it->it ); +} + +int +BSP_mve_send_buf(struct mveth_private *mp, void *m_head, void *data_p, int len) +{ +int rval; +register struct mbuf *m1; +BsdMveIter iter; + +/* Only way to get here is when we discover that the mbuf chain + * is too long for the tx ring + */ +startover: + + rval = 0; + +#ifdef MVETH_TESTING + assert(m_head); +#endif + + if ( ! (m1 = m_head) ) + return 0; + + /* find first mbuf with actual data */ + if ( ! initIter( &iter, m_head ) ) { + /* end reached and still no data to send ?? */ + m_freem(m_head); + return 0; + } + +#ifdef MVETH_DEBUG_TX_DUMP + if ( (mveth_tx_dump & (1<port_num)) ) { + int ll,kk; + struct mbuf *m; + for ( kk=0, m=m_head; m; m=m->m_next) { + for ( ll=0; llm_len; ll++ ) { + printf("%02X ",*(mtod(m,char*) + ll)); + if ( ((++kk)&0xf) == 0 ) + printf("\n"); + } + } + printf("\n"); + } +#endif + + rval = BSP_mve_send_buf_chain( mp, nextBuf, &iter.it ); + + if ( -2 == rval ) { + if ( ! (m_head = repackage_chain( m_head )) ) { + /* no cluster available */ + /* No access to this counter, unfortunately + mp->stats.odrops++; + */ + return 0; + } + goto startover; + } + + return rval; +} + +int +BSP_mve_media_ioctl(struct mveth_private *mp, int cmd, int *parg) +{ +int rval; + /* alias cmd == 0,1 */ + switch ( cmd ) { + case 0: cmd = SIOCGIFMEDIA; + break; + case 1: cmd = SIOCSIFMEDIA; + case SIOCGIFMEDIA: + case SIOCSIFMEDIA: + break; + default: return -1; + } + REGLOCK(); + rval = rtems_mii_ioctl(&mveth_mdio, mp, cmd, parg); + REGUNLOCK(); + return rval; +} + +/* Translate IFFLAGS to low-level driver representation */ +static int +xlateMediaFlags(int media) +{ +int lowLevelFlags = 0; + + if ( IFM_LINK_OK & media ) { + lowLevelFlags |= MV643XX_MEDIA_LINK; + + if ( IFM_FDX & media ) { + lowLevelFlags |= MV643XX_MEDIA_FD; + } + + switch ( IFM_SUBTYPE(media) ) { + default: /* treat as 10 */ + lowLevelFlags |= MV643XX_MEDIA_10; + break; + case IFM_100_TX: + lowLevelFlags |= MV643XX_MEDIA_100; + break; + case IFM_1000_T: + lowLevelFlags |= MV643XX_MEDIA_1000; + break; + } + } + return lowLevelFlags; +} + +int +BSP_mve_ack_link_chg(struct mveth_private *mp, int *pmedia) +{ +int media = IFM_MAKEWORD(0,0,0,0); + + if ( 0 == BSP_mve_media_ioctl(mp, SIOCGIFMEDIA, &media)) { + if ( IFM_LINK_OK & media ) { + int serport_cfg = xlateMediaFlags( media ); + + BSP_mve_update_serial_port(mp, serport_cfg); + } + if ( pmedia ) + *pmedia = media; + return 0; + } + return -1; +} + +/* BSDNET SUPPORT/GLUE ROUTINES */ + +static void +mveth_set_filters(struct ifnet *ifp); + +STATIC void +mveth_stop(struct mveth_softc *sc) +{ + BSP_mve_stop_hw(sc->pvt); + sc->arpcom.ac_if.if_timer = 0; +} + +/* allocate a mbuf for RX with a properly aligned data buffer + * RETURNS 0 if allocation fails + */ +static void * +alloc_mbuf_rx(int *psz, uintptr_t *paddr) +{ +struct mbuf *m; +unsigned long l,o; + + MGETHDR(m, M_DONTWAIT, MT_DATA); + if ( !m ) + return 0; + MCLGET(m, M_DONTWAIT); + if ( ! (m->m_flags & M_EXT) ) { + m_freem(m); + return 0; + } + + o = mtod(m, unsigned long); + l = MV643XX_ALIGN(o, RX_BUF_ALIGNMENT) - o; + + /* align start of buffer */ + m->m_data += l; + + /* reduced length */ + l = MCLBYTES - l; + + m->m_len = m->m_pkthdr.len = l; + *psz = m->m_len; + *paddr = mtod(m, uintptr_t); + + return (void*) m; +} + +static void consume_rx_mbuf(void *buf, void *arg, int len) +{ +struct ifnet *ifp = arg; +struct mbuf *m = buf; + + if ( len <= 0 ) { + ifp->if_iqdrops++; + if ( len < 0 ) { + ifp->if_ierrors++; + } + if ( m ) + m_freem(m); + } else { + struct ether_header *eh; + + eh = (struct ether_header *)(mtod(m, unsigned long) + ETH_RX_OFFSET); + m->m_len = m->m_pkthdr.len = len - sizeof(struct ether_header) - ETH_RX_OFFSET - ETH_CRC_LEN; + m->m_data += sizeof(struct ether_header) + ETH_RX_OFFSET; + m->m_pkthdr.rcvif = ifp; + + ifp->if_ipackets++; + ifp->if_ibytes += m->m_pkthdr.len; + + if (0) { + /* Low-level debugging */ + int i; + for (i=0; i<13; i++) { + printf("%02X:",((char*)eh)[i]); + } + printf("%02X\n",((char*)eh)[i]); + for (i=0; im_len; i++) { + if ( !(i&15) ) + printf("\n"); + printf("0x%02x ",mtod(m,char*)[i]); + } + printf("\n"); + } + + if (0) { + /* Low-level debugging/testing without bsd stack */ + m_freem(m); + } else { + /* send buffer upwards */ + ether_input(ifp, eh, m); + } + } +} + +static void release_tx_mbuf(void *buf, void *arg, int err) +{ +struct ifnet *ifp = arg; +struct mbuf *mb = buf; + + if ( err ) { + ifp->if_oerrors++; + } else { + ifp->if_opackets++; + } + ifp->if_obytes += mb->m_pkthdr.len; + m_freem(mb); +} + +/* BSDNET DRIVER CALLBACKS */ + +static void +mveth_init(void *arg) +{ +struct mveth_softc *sc = arg; +struct ifnet *ifp = &sc->arpcom.ac_if; +int media; +int lowLevelMediaStatus; + + media = IFM_MAKEWORD(0, 0, 0, 0); + if ( 0 == BSP_mve_media_ioctl(sc->pvt, SIOCGIFMEDIA, &media) ) { + if ( (IFM_LINK_OK & media) ) { + ifp->if_flags &= ~IFF_OACTIVE; + } else { + ifp->if_flags |= IFF_OACTIVE; + } + } + + lowLevelMediaStatus = xlateMediaFlags( media ); + + BSP_mve_init_hw(sc->pvt, ifp->if_flags & IFF_PROMISC, sc->arpcom.ac_enaddr, lowLevelMediaStatus); + + + /* if promiscuous then there is no need to change */ + if ( ! (ifp->if_flags & IFF_PROMISC) ) + mveth_set_filters(ifp); + + ifp->if_flags |= IFF_RUNNING; + sc->arpcom.ac_if.if_timer = 0; +} + +/* bsdnet driver entry to start transmission */ +static void +mveth_start(struct ifnet *ifp) +{ +struct mveth_softc *sc = ifp->if_softc; +struct mbuf *m = 0; + + while ( ifp->if_snd.ifq_head ) { + IF_DEQUEUE( &ifp->if_snd, m ); + if ( BSP_mve_send_buf(sc->pvt, m, 0, 0) < 0 ) { + IF_PREPEND( &ifp->if_snd, m); + ifp->if_flags |= IFF_OACTIVE; + break; + } + /* need to do this really only once + * but it's cheaper this way. + */ + ifp->if_timer = 2*IFNET_SLOWHZ; + } +} + +/* bsdnet driver entry; */ +static void +mveth_watchdog(struct ifnet *ifp) +{ +struct mveth_softc *sc = ifp->if_softc; + + ifp->if_oerrors++; + printk(DRVNAME"%i: watchdog timeout; resetting\n", ifp->if_unit); + + mveth_init(sc); + mveth_start(ifp); +} + +static void +mveth_set_filters(struct ifnet *ifp) +{ +struct mveth_softc *sc = ifp->if_softc; + + BSP_mve_promisc_set( sc->pvt, !!(ifp->if_flags & IFF_PROMISC)); + + if ( ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI) ) { + BSP_mve_mcast_filter_accept_all(sc->pvt); + } else { + struct ether_multi *enm; + struct ether_multistep step; + + BSP_mve_mcast_filter_clear( sc->pvt ); + + ETHER_FIRST_MULTI(step, (struct arpcom *)ifp, enm); + + while ( enm ) { + if ( memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN) ) + assert( !"Should never get here; IFF_ALLMULTI should be set!" ); + + BSP_mve_mcast_filter_accept_add(sc->pvt, enm->enm_addrlo); + + ETHER_NEXT_MULTI(step, enm); + } + } +} + +/* bsdnet driver ioctl entry */ +static int +mveth_ioctl(struct ifnet *ifp, ioctl_command_t cmd, caddr_t data) +{ +struct mveth_softc *sc = ifp->if_softc; +struct ifreq *ifr = (struct ifreq *)data; +int error = 0; +int f; + + switch ( cmd ) { + case SIOCSIFFLAGS: + f = ifp->if_flags; + if ( f & IFF_UP ) { + if ( ! ( f & IFF_RUNNING ) ) { + mveth_init(sc); + } else { + if ( (f & IFF_PROMISC) != (sc->bsd.oif_flags & IFF_PROMISC) ) { + mveth_set_filters(ifp); + } + /* FIXME: other flag changes are ignored/unimplemented */ + } + } else { + if ( f & IFF_RUNNING ) { + mveth_stop(sc); + ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); + } + } + sc->bsd.oif_flags = ifp->if_flags; + break; + + case SIOCGIFMEDIA: + case SIOCSIFMEDIA: + error = BSP_mve_media_ioctl(sc->pvt, cmd, &ifr->ifr_media); + break; + + case SIOCADDMULTI: + case SIOCDELMULTI: + error = (cmd == SIOCADDMULTI) + ? ether_addmulti(ifr, &sc->arpcom) + : ether_delmulti(ifr, &sc->arpcom); + + if (error == ENETRESET) { + if (ifp->if_flags & IFF_RUNNING) { + mveth_set_filters(ifp); + } + error = 0; + } + break; + + + break; + + case SIO_RTEMS_SHOW_STATS: + BSP_mve_dump_stats(sc->pvt, stdout); + break; + + default: + error = ether_ioctl(ifp, cmd, data); + break; + } + + return error; +} + +/* DRIVER TASK */ + +static void mveth_isr(void *arg) +{ +struct mveth_softc *sc = (struct mveth_softc*) arg; +int idx = (sc - &theMvEths[0]); + BSP_mve_disable_irqs( sc->pvt ); +#ifdef MVETH_DEBUG + printk(DRVNAME": bsdnet isr; posting event %i to 0x%08x\n", idx, BSP_mve_get_tid( sc->pvt )); +#endif + rtems_bsdnet_event_send( BSP_mve_get_tid( sc->pvt ), (1<>=1, sc++ ) { + if ( (evs & 1) ) { + register uint32_t x; + + ifp = &sc->arpcom.ac_if; + + if ( !(ifp->if_flags & IFF_UP) ) { + mveth_stop(sc); + ifp->if_flags &= ~(IFF_UP|IFF_RUNNING); + continue; + } + + if ( !(ifp->if_flags & IFF_RUNNING) ) { + /* event could have been pending at the time hw was stopped; + * just ignore... + */ + continue; + } + + x = BSP_mve_ack_irqs(sc->pvt); + + if ( MV643XX_ETH_EXT_IRQ_LINK_CHG & x ) { + /* phy status changed */ + int media; + + if ( 0 == BSP_mve_ack_link_chg(sc->pvt, &media) ) { + if ( IFM_LINK_OK & media ) { + ifp->if_flags &= ~IFF_OACTIVE; + mveth_start(ifp); + } else { + /* stop sending */ + ifp->if_flags |= IFF_OACTIVE; + } + } + } + /* free tx chain */ + if ( (MV643XX_ETH_EXT_IRQ_TX_DONE & x) && ((avail = BSP_mve_swipe_tx(sc->pvt)) > 0) ) { + ifp->if_flags &= ~IFF_OACTIVE; + if ( sc->bsd.tx_ring_size == avail ) + ifp->if_timer = 0; + mveth_start(ifp); + } + if ( (MV643XX_ETH_IRQ_RX_DONE & x) ) + BSP_mve_swipe_rx(sc->pvt); + + BSP_mve_enable_irqs(sc->pvt); + } + } + } +} + +/* PUBLIC RTEMS BSDNET ATTACH FUNCTION */ +int +rtems_mve_attach(struct rtems_bsdnet_ifconfig *ifcfg, int attaching) +{ +char *unitName; +int unit,i,cfgUnits; +struct mveth_softc *sc; +struct ifnet *ifp; + + unit = rtems_bsdnet_parse_driver_name(ifcfg, &unitName); + if ( unit <= 0 || unit > MV643XXETH_NUM_DRIVER_SLOTS ) { + printk(DRVNAME": Bad unit number %i; must be 1..%i\n", unit, MV643XXETH_NUM_DRIVER_SLOTS); + return 1; + } + + sc = &theMvEths[unit-1]; + ifp = &sc->arpcom.ac_if; + + if ( attaching ) { + if ( ifp->if_init ) { + printk(DRVNAME": instance %i already attached.\n", unit); + return -1; + } + + for ( i=cfgUnits = 0; irbuf_count ) { + ifcfg->rbuf_count = MV643XX_RX_RING_SIZE; + } + + if ( 0 == ifcfg->xbuf_count ) { + ifcfg->xbuf_count = MV643XX_TX_RING_SIZE; + } + + if ( !mve_setup_bsd( unit, + mveth_tid, + mveth_isr, (void*)sc, + release_tx_mbuf, ifp, + alloc_mbuf_rx, + consume_rx_mbuf, ifp, + ifcfg->rbuf_count, + ifcfg->xbuf_count, + BSP_MVE_IRQ_TX | BSP_MVE_IRQ_RX | BSP_MVE_IRQ_LINK) ) { + return -1; + } + + if ( nmbclusters < ifcfg->rbuf_count * cfgUnits + 60 /* arbitrary */ ) { + printk(DRVNAME"%i: (mv643xx ethernet) Your application has not enough mbuf clusters\n", unit); + printk( " configured for this driver.\n"); + return -1; + } + + if ( ifcfg->hardware_address ) { + memcpy(sc->arpcom.ac_enaddr, ifcfg->hardware_address, ETHER_ADDR_LEN); + } else { + /* read back from hardware assuming that MotLoad already had set it up */ + BSP_mve_read_eaddr(sc->pvt, sc->arpcom.ac_enaddr); + } + + ifp->if_softc = sc; + ifp->if_unit = unit; + ifp->if_name = unitName; + + ifp->if_mtu = ifcfg->mtu ? ifcfg->mtu : ETHERMTU; + + ifp->if_init = mveth_init; + ifp->if_ioctl = mveth_ioctl; + ifp->if_start = mveth_start; + ifp->if_output = ether_output; + /* + * While nonzero, the 'if->if_timer' is decremented + * (by the networking code) at a rate of IFNET_SLOWHZ (1hz) and 'if_watchdog' + * is called when it expires. + * If either of those fields is 0 the feature is disabled. + */ + ifp->if_watchdog = mveth_watchdog; + ifp->if_timer = 0; + + sc->bsd.oif_flags = /* ... */ + ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX; + + /* + * if unset, this set to 10Mbps by ether_ifattach; seems to be unused by bsdnet stack; + * could be updated along with phy speed, though... + ifp->if_baudrate = 10000000; + */ + + /* NOTE: ether_output drops packets if ifq_len >= ifq_maxlen + * but this is the packet count, not the fragment count! + ifp->if_snd.ifq_maxlen = sc->pvt->buf_count; + */ + ifp->if_snd.ifq_maxlen = ifqmaxlen; + + { + if_attach(ifp); + ether_ifattach(ifp); + } + + } else { + printk(DRVNAME": interface detaching not implemented\n"); + return -1; + } + + return 0; +} + +/* EARLY PHY ACCESS */ +static int +mveth_early_init(int idx) +{ + if ( idx < 0 || idx >= MV643XXETH_NUM_DRIVER_SLOTS ) + return -1; + + return 0; +} + +static int +mveth_early_read_phy(int idx, unsigned reg) +{ +int rval; + + if ( idx < 0 || idx >= MV643XXETH_NUM_DRIVER_SLOTS ) + return -1; + + rval = BSP_mve_mii_read_early(idx, reg); + return rval < 0 ? rval : rval & 0xffff; +} + +static int +mveth_early_write_phy(int idx, unsigned reg, unsigned val) +{ + if ( idx < 0 || idx >= MV643XXETH_NUM_DRIVER_SLOTS ) + return -1; + + BSP_mve_mii_write_early(idx, reg, val); + return 0; +} + +rtems_bsdnet_early_link_check_ops +rtems_mve_early_link_check_ops = { + init: mveth_early_init, + read_phy: mveth_early_read_phy, + write_phy: mveth_early_write_phy, + name: DRVNAME, + num_slots: MAX_NUM_SLOTS +}; + +/* DEBUGGING */ + +#ifdef MVETH_DEBUG +/* Display/dump descriptor rings */ + +int +mveth_dring(struct mveth_softc *sc) +{ + /* can't just invalidate the descriptor - if it contains + * data that hasn't been flushed yet, we create an inconsistency... + */ + rtems_bsdnet_semaphore_obtain(); + + BSP_mve_dring_nosync(sc->pvt); + + rtems_bsdnet_semaphore_release(); + return 0; +} + +#endif diff --git a/c/src/lib/libbsp/powerpc/beatnik/Makefile.am b/c/src/lib/libbsp/powerpc/beatnik/Makefile.am index 0a3324d7288..609e15c945b 100644 --- a/c/src/lib/libbsp/powerpc/beatnik/Makefile.am +++ b/c/src/lib/libbsp/powerpc/beatnik/Makefile.am @@ -83,13 +83,15 @@ librtemsbsp_a_SOURCES += ../../../../../../bsps/powerpc/beatnik/pci/motload_fixu librtemsbsp_a_SOURCES += ../../../../../../bsps/powerpc/shared/pci/pcifinddevice.c #network +# low-level driver +librtemsbsp_a_SOURCES += ../../../../../../bsps/powerpc/beatnik/net/if_mve/mv643xx_eth.c if HAS_NETWORKING librtemsbsp_a_SOURCES += ../../../../../../bsps/powerpc/beatnik/net/if_em/if_em.c librtemsbsp_a_SOURCES += ../../../../../../bsps/powerpc/beatnik/net/if_em/if_em_hw.c librtemsbsp_a_SOURCES += ../../../../../../bsps/powerpc/beatnik/net/if_em/if_em_rtems.c librtemsbsp_a_SOURCES += ../../../../../../bsps/powerpc/beatnik/net/if_gfe/if_gfe.c librtemsbsp_a_SOURCES += ../../../../../../bsps/powerpc/beatnik/net/if_gfe/if_gfe_rtems.c -librtemsbsp_a_SOURCES += ../../../../../../bsps/powerpc/beatnik/net/if_mve/mv643xx_eth.c +librtemsbsp_a_SOURCES += ../../../../../../bsps/powerpc/beatnik/net/if_mve/mv643xx_eth_bsdnet.c librtemsbsp_a_SOURCES += ../../../../../../bsps/powerpc/beatnik/net/support/bsp_attach.c librtemsbsp_a_SOURCES += ../../../../../../bsps/powerpc/beatnik/net/support/early_link_status.c endif