# HG changeset patch # User Patrick Bruenn # Date 1401982036 -7200 # Node ID 0bc1ec2711b1a2a539b3af948ef743d69a359ba4 # Parent a4fdad543554982007f99a30510f9cd623fad341 reorder functions in netdev.c to avoid forward declarations diff -r a4fdad543554 -r 0bc1ec2711b1 devices/ccat/netdev.c --- a/devices/ccat/netdev.c Thu Jun 05 17:08:54 2014 +0200 +++ b/devices/ccat/netdev.c Thu Jun 05 17:27:16 2014 +0200 @@ -46,24 +46,6 @@ }; #define FIFO_LENGTH 64 -static void ec_poll(struct net_device *dev); -static enum hrtimer_restart poll_timer_callback(struct hrtimer *timer); - -static struct rtnl_link_stats64 *ccat_eth_get_stats64(struct net_device *dev, struct rtnl_link_stats64 - *storage); -static int ccat_eth_open(struct net_device *dev); -static netdev_tx_t ccat_eth_start_xmit(struct sk_buff *skb, - struct net_device *dev); -static int ccat_eth_stop(struct net_device *dev); -static void ccat_eth_xmit_raw(struct net_device *dev, const char *data, - size_t len); - -static const struct net_device_ops ccat_eth_netdev_ops = { - .ndo_get_stats64 = ccat_eth_get_stats64, - .ndo_open = ccat_eth_open, - .ndo_start_xmit = ccat_eth_start_xmit, - .ndo_stop = ccat_eth_stop, -}; static void ecdev_kfree_skb_any(struct sk_buff *skb) { @@ -227,6 +209,126 @@ priv->reg.misc = func_base + offsets.misc; } +static netdev_tx_t ccat_eth_start_xmit(struct sk_buff *skb, + struct net_device *dev) +{ + static size_t next = 0; + struct ccat_eth_priv *const priv = netdev_priv(dev); + struct ccat_eth_frame *const frame = + ((struct ccat_eth_frame *)priv->tx_fifo.dma.virt); + u32 addr_and_length; + + if (skb_is_nonlinear(skb)) { + pr_warn("Non linear skb not supported -> drop frame.\n"); + atomic64_inc(&priv->tx_dropped); + priv->kfree_skb_any(skb); + return NETDEV_TX_OK; + } + + if (skb->len > sizeof(frame->data)) { + pr_warn("skb.len %llu exceeds dma buffer %llu -> drop frame.\n", + (u64) skb->len, (u64) sizeof(frame->data)); + atomic64_inc(&priv->tx_dropped); + priv->kfree_skb_any(skb); + return NETDEV_TX_OK; + } + + if (!frame[next].sent) { + netdev_err(dev, "BUG! Tx Ring full when queue awake!\n"); + ccat_eth_tx_fifo_full(priv, &frame[next]); + return NETDEV_TX_BUSY; + } + + /* prepare frame in DMA memory */ + frame[next].sent = 0; + frame[next].length = skb->len; + memcpy(frame[next].data, skb->data, skb->len); + + priv->kfree_skb_any(skb); + + addr_and_length = 8 + (next * sizeof(*frame)); + addr_and_length += + ((frame[next].length + CCAT_ETH_FRAME_HEAD_LEN) / 8) << 24; + iowrite32(addr_and_length, priv->reg.tx_fifo); /* add to DMA fifo */ + atomic64_add(frame[next].length, &priv->tx_bytes); /* update stats */ + + next = (next + 1) % FIFO_LENGTH; + /* stop queue if tx ring is full */ + if (!frame[next].sent) { + ccat_eth_tx_fifo_full(priv, &frame[next]); + } + return NETDEV_TX_OK; +} + +/** + * Function to transmit a raw buffer to the network (f.e. frameForwardEthernetFrames) + * @dev a valid net_device + * @data pointer to your raw buffer + * @len number of bytes in the raw buffer to transmit + */ +static void ccat_eth_xmit_raw(struct net_device *dev, const char *const data, + size_t len) +{ + struct sk_buff *skb = dev_alloc_skb(len); + + skb->dev = dev; + skb_copy_to_linear_data(skb, data, len); + skb_put(skb, len); + ccat_eth_start_xmit(skb, dev); +} + +static const size_t CCATRXDESC_HEADER_LEN = 20; +static void ccat_eth_receive(struct net_device *const dev, + const struct ccat_eth_frame *const frame) +{ + struct ccat_eth_priv *const priv = netdev_priv(dev); + const size_t len = frame->length - CCATRXDESC_HEADER_LEN; + struct sk_buff *skb = dev_alloc_skb(len + NET_IP_ALIGN); + + if (!skb) { + pr_info("%s() out of memory :-(\n", __FUNCTION__); + atomic64_inc(&priv->rx_dropped); + return; + } + skb->dev = dev; + skb_reserve(skb, NET_IP_ALIGN); + skb_copy_to_linear_data(skb, frame->data, len); + skb_put(skb, len); + skb->protocol = eth_type_trans(skb, dev); + skb->ip_summed = CHECKSUM_UNNECESSARY; + atomic64_add(len, &priv->rx_bytes); + netif_rx(skb); +} + +static void ccat_eth_link_down(struct net_device *dev) +{ + struct ccat_eth_priv *const priv = netdev_priv(dev); + + priv->stop_queue(dev); + priv->carrier_off(dev); + netdev_info(dev, "NIC Link is Down\n"); +} + +static void ccat_eth_link_up(struct net_device *const dev) +{ + struct ccat_eth_priv *const priv = netdev_priv(dev); + + netdev_info(dev, "NIC Link is Up\n"); + /* TODO netdev_info(dev, "NIC Link is Up %u Mbps %s Duplex\n", + speed == SPEED_100 ? 100 : 10, + cmd.duplex == DUPLEX_FULL ? "Full" : "Half"); */ + + ccat_eth_dma_fifo_reset(&priv->rx_fifo); + ccat_eth_dma_fifo_reset(&priv->tx_fifo); + + /* TODO reset CCAT MAC register */ + + ccat_eth_xmit_raw(dev, frameForwardEthernetFrames, + sizeof(frameForwardEthernetFrames)); + priv->carrier_on(dev); + priv->start_queue(dev); +} + /** * Read link state from CCAT hardware * @return 1 if link is up, 0 if not @@ -237,6 +339,87 @@ return (1 << 24) == (ioread32(priv->reg.mii + 0x8 + 4) & (1 << 24)); } +/** + * Poll for link state changes + */ +static void poll_link(struct ccat_eth_priv *const priv) +{ + const size_t link = ccat_eth_priv_read_link_state(priv); + + if (link != priv->carrier_ok(priv->netdev)) { + if (link) + ccat_eth_link_up(priv->netdev); + else + ccat_eth_link_down(priv->netdev); + } +} + +/** + * Rx handler in EtherCAT operation mode + * priv->ecdev should always be valid! + */ +static void ec_poll_rx(struct net_device *dev) +{ + static size_t next = 0; + struct ccat_eth_priv *const priv = netdev_priv(dev); + struct ccat_eth_frame *frame = + ((struct ccat_eth_frame *)priv->rx_fifo.dma.virt) + next; + if (frame->received) { + ecdev_receive(priv->ecdev, frame->data, + frame->length - CCATRXDESC_HEADER_LEN); + frame->received = 0; + ccat_eth_rx_fifo_add(frame, &priv->rx_fifo); + next = (next + 1) % FIFO_LENGTH; + } else { + //TODO dev_warn(&dev->dev, "%s(): frame was not ready\n", __FUNCTION__); + } +} + +/** + * Poll for available rx dma descriptors in ethernet operating mode + */ +static void poll_rx(struct ccat_eth_priv *const priv) +{ + struct ccat_eth_frame *const frame = priv->rx_fifo.dma.virt; + static size_t next = 0; + + /* TODO omit possible deadlock in situations with heavy traffic */ + while (frame[next].received) { + ccat_eth_receive(priv->netdev, frame + next); + frame[next].received = 0; + ccat_eth_rx_fifo_add(frame + next, &priv->rx_fifo); + next = (next + 1) % FIFO_LENGTH; + } +} + +/** + * Poll for available tx dma descriptors in ethernet operating mode + */ +static void poll_tx(struct ccat_eth_priv *const priv) +{ + if (priv->next_tx_frame && priv->next_tx_frame->sent) { + priv->next_tx_frame = NULL; + netif_wake_queue(priv->netdev); + } +} + +/** + * Since CCAT doesn't support interrupts until now, we have to poll + * some status bits to recognize things like link change etc. + */ +static enum hrtimer_restart poll_timer_callback(struct hrtimer *timer) +{ + struct ccat_eth_priv *priv = container_of(timer, struct ccat_eth_priv, + poll_timer); + + poll_link(priv); + if(!priv->ecdev) + poll_rx(priv); + poll_tx(priv); + hrtimer_forward_now(timer, ktime_set(0, 100 * NSEC_PER_USEC)); + return HRTIMER_RESTART; +} + static struct rtnl_link_stats64 *ccat_eth_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *storage) { @@ -276,6 +459,34 @@ return storage; } +static int ccat_eth_open(struct net_device *dev) +{ + struct ccat_eth_priv *const priv = netdev_priv(dev); + + hrtimer_init(&priv->poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); + priv->poll_timer.function = poll_timer_callback; + hrtimer_start(&priv->poll_timer, ktime_set(0, 100000), + HRTIMER_MODE_REL); + return 0; +} + +static int ccat_eth_stop(struct net_device *dev) +{ + struct ccat_eth_priv *const priv = netdev_priv(dev); + + priv->stop_queue(dev); + hrtimer_cancel(&priv->poll_timer); + netdev_info(dev, "stopped.\n"); + return 0; +} + +static const struct net_device_ops ccat_eth_netdev_ops = { + .ndo_get_stats64 = ccat_eth_get_stats64, + .ndo_open = ccat_eth_open, + .ndo_start_xmit = ccat_eth_start_xmit, + .ndo_stop = ccat_eth_stop, +}; + struct ccat_eth_priv *ccat_eth_init(const struct ccat_device *const ccatdev, const void __iomem * const addr) { @@ -304,7 +515,7 @@ netdev->netdev_ops = &ccat_eth_netdev_ops; /* use as EtherCAT device? */ - priv->ecdev = ecdev_offer(netdev, ec_poll, THIS_MODULE); + priv->ecdev = ecdev_offer(netdev, ec_poll_rx, THIS_MODULE); if (priv->ecdev) { priv->carrier_off = ecdev_carrier_off; priv->carrier_ok = ecdev_carrier_ok; @@ -354,225 +565,3 @@ free_netdev(priv->netdev); pr_debug("%s(): done\n", __FUNCTION__); } - -static int ccat_eth_open(struct net_device *dev) -{ - struct ccat_eth_priv *const priv = netdev_priv(dev); - - hrtimer_init(&priv->poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); - priv->poll_timer.function = poll_timer_callback; - hrtimer_start(&priv->poll_timer, ktime_set(0, 100000), - HRTIMER_MODE_REL); - return 0; -} - -static const size_t CCATRXDESC_HEADER_LEN = 20; -static void ccat_eth_receive(struct net_device *const dev, - const struct ccat_eth_frame *const frame) -{ - struct ccat_eth_priv *const priv = netdev_priv(dev); - const size_t len = frame->length - CCATRXDESC_HEADER_LEN; - struct sk_buff *skb = dev_alloc_skb(len + NET_IP_ALIGN); - - if (!skb) { - pr_info("%s() out of memory :-(\n", __FUNCTION__); - atomic64_inc(&priv->rx_dropped); - return; - } - skb->dev = dev; - skb_reserve(skb, NET_IP_ALIGN); - skb_copy_to_linear_data(skb, frame->data, len); - skb_put(skb, len); - skb->protocol = eth_type_trans(skb, dev); - skb->ip_summed = CHECKSUM_UNNECESSARY; - atomic64_add(len, &priv->rx_bytes); - netif_rx(skb); -} - -/** - * Rx handler in EtherCAT operation mode - * priv->ecdev should always be valid! - */ -static void ec_poll(struct net_device *dev) -{ - static size_t next = 0; - struct ccat_eth_priv *const priv = netdev_priv(dev); - struct ccat_eth_frame *frame = - ((struct ccat_eth_frame *)priv->rx_fifo.dma.virt) + next; - if (frame->received) { - ecdev_receive(priv->ecdev, frame->data, - frame->length - CCATRXDESC_HEADER_LEN); - frame->received = 0; - ccat_eth_rx_fifo_add(frame, &priv->rx_fifo); - next = (next + 1) % FIFO_LENGTH; - } else { - //TODO dev_warn(&dev->dev, "%s(): frame was not ready\n", __FUNCTION__); - } -} - -static netdev_tx_t ccat_eth_start_xmit(struct sk_buff *skb, - struct net_device *dev) -{ - static size_t next = 0; - struct ccat_eth_priv *const priv = netdev_priv(dev); - struct ccat_eth_frame *const frame = - ((struct ccat_eth_frame *)priv->tx_fifo.dma.virt); - u32 addr_and_length; - - if (skb_is_nonlinear(skb)) { - pr_warn("Non linear skb not supported -> drop frame.\n"); - atomic64_inc(&priv->tx_dropped); - priv->kfree_skb_any(skb); - return NETDEV_TX_OK; - } - - if (skb->len > sizeof(frame->data)) { - pr_warn("skb.len %llu exceeds dma buffer %llu -> drop frame.\n", - (u64) skb->len, (u64) sizeof(frame->data)); - atomic64_inc(&priv->tx_dropped); - priv->kfree_skb_any(skb); - return NETDEV_TX_OK; - } - - if (!frame[next].sent) { - netdev_err(dev, "BUG! Tx Ring full when queue awake!\n"); - ccat_eth_tx_fifo_full(priv, &frame[next]); - return NETDEV_TX_BUSY; - } - - /* prepare frame in DMA memory */ - frame[next].sent = 0; - frame[next].length = skb->len; - memcpy(frame[next].data, skb->data, skb->len); - - priv->kfree_skb_any(skb); - - addr_and_length = 8 + (next * sizeof(*frame)); - addr_and_length += - ((frame[next].length + CCAT_ETH_FRAME_HEAD_LEN) / 8) << 24; - iowrite32(addr_and_length, priv->reg.tx_fifo); /* add to DMA fifo */ - atomic64_add(frame[next].length, &priv->tx_bytes); /* update stats */ - - next = (next + 1) % FIFO_LENGTH; - /* stop queue if tx ring is full */ - if (!frame[next].sent) { - ccat_eth_tx_fifo_full(priv, &frame[next]); - } - return NETDEV_TX_OK; -} - -static int ccat_eth_stop(struct net_device *dev) -{ - struct ccat_eth_priv *const priv = netdev_priv(dev); - - priv->stop_queue(dev); - hrtimer_cancel(&priv->poll_timer); - netdev_info(dev, "stopped.\n"); - return 0; -} - -static void ccat_eth_link_down(struct net_device *dev) -{ - struct ccat_eth_priv *const priv = netdev_priv(dev); - - priv->stop_queue(dev); - priv->carrier_off(dev); - netdev_info(dev, "NIC Link is Down\n"); -} - -static void ccat_eth_link_up(struct net_device *const dev) -{ - struct ccat_eth_priv *const priv = netdev_priv(dev); - - netdev_info(dev, "NIC Link is Up\n"); - /* TODO netdev_info(dev, "NIC Link is Up %u Mbps %s Duplex\n", - speed == SPEED_100 ? 100 : 10, - cmd.duplex == DUPLEX_FULL ? "Full" : "Half"); */ - - ccat_eth_dma_fifo_reset(&priv->rx_fifo); - ccat_eth_dma_fifo_reset(&priv->tx_fifo); - - /* TODO reset CCAT MAC register */ - - ccat_eth_xmit_raw(dev, frameForwardEthernetFrames, - sizeof(frameForwardEthernetFrames)); - priv->carrier_on(dev); - priv->start_queue(dev); -} - -/** - * Function to transmit a raw buffer to the network (f.e. frameForwardEthernetFrames) - * @dev a valid net_device - * @data pointer to your raw buffer - * @len number of bytes in the raw buffer to transmit - */ -static void ccat_eth_xmit_raw(struct net_device *dev, const char *const data, - size_t len) -{ - struct sk_buff *skb = dev_alloc_skb(len); - - skb->dev = dev; - skb_copy_to_linear_data(skb, data, len); - skb_put(skb, len); - ccat_eth_start_xmit(skb, dev); -} - -/** - * Poll for link state changes - */ -static void poll_link(struct ccat_eth_priv *const priv) -{ - const size_t link = ccat_eth_priv_read_link_state(priv); - - if (link != priv->carrier_ok(priv->netdev)) { - if (link) - ccat_eth_link_up(priv->netdev); - else - ccat_eth_link_down(priv->netdev); - } -} - -/** - * Poll for available rx dma descriptors in ethernet operating mode - */ -static void poll_rx(struct ccat_eth_priv *const priv) -{ - struct ccat_eth_frame *const frame = priv->rx_fifo.dma.virt; - static size_t next = 0; - - /* TODO omit possible deadlock in situations with heavy traffic */ - while (frame[next].received) { - ccat_eth_receive(priv->netdev, frame + next); - frame[next].received = 0; - ccat_eth_rx_fifo_add(frame + next, &priv->rx_fifo); - next = (next + 1) % FIFO_LENGTH; - } -} - -/** - * Poll for available tx dma descriptors in ethernet operating mode - */ -static void poll_tx(struct ccat_eth_priv *const priv) -{ - if (priv->next_tx_frame && priv->next_tx_frame->sent) { - priv->next_tx_frame = NULL; - netif_wake_queue(priv->netdev); - } -} - -/** - * Since CCAT doesn't support interrupts until now, we have to poll - * some status bits to recognize things like link change etc. - */ -static enum hrtimer_restart poll_timer_callback(struct hrtimer *timer) -{ - struct ccat_eth_priv *priv = container_of(timer, struct ccat_eth_priv, - poll_timer); - - poll_link(priv); - if(!priv->ecdev) - poll_rx(priv); - poll_tx(priv); - hrtimer_forward_now(timer, ktime_set(0, 100 * NSEC_PER_USEC)); - return HRTIMER_RESTART; -}