From 058e923ddac69db7828b907ff417432389df7cce Mon Sep 17 00:00:00 2001 From: Patrick Bruenn Date: Thu, 5 Jun 2014 16:07:52 +0200 Subject: [PATCH] - use short kernel types, add empty lines after variable declaration to conform better with linux coding style - replace polling threads with hrtimer callback --- devices/ccat/CCatDefinitions.h | 14 +-- devices/ccat/module.c | 41 ++++---- devices/ccat/module.h | 32 +++--- devices/ccat/netdev.c | 177 +++++++++++++++------------------ devices/ccat/update.c | 62 +++++++----- devices/ccat/update.h | 2 +- 6 files changed, 159 insertions(+), 169 deletions(-) diff --git a/devices/ccat/CCatDefinitions.h b/devices/ccat/CCatDefinitions.h index 8c204ad0..158e680c 100644 --- a/devices/ccat/CCatDefinitions.h +++ b/devices/ccat/CCatDefinitions.h @@ -22,13 +22,13 @@ #ifndef WINDOWS #include -typedef uint8_t BYTE; -typedef uint32_t ULONG; -typedef uint16_t USHORT; -typedef uint8_t UINT8; -typedef uint16_t UINT16; -typedef uint32_t UINT32; -typedef uint64_t UINT64; +typedef u8 BYTE; +typedef u32 ULONG; +typedef u16 USHORT; +typedef u8 UINT8; +typedef u16 UINT16; +typedef u32 UINT32; +typedef u64 UINT64; #endif #define CCAT_DMA_FRAME_HEADER_LENGTH (196 / 8) // 196 bit diff --git a/devices/ccat/module.c b/devices/ccat/module.c index ea9eccb4..80c49fe0 100644 --- a/devices/ccat/module.c +++ b/devices/ccat/module.c @@ -56,13 +56,13 @@ static int ccat_bar_init(struct ccat_bar *bar, size_t index, struct pci_dev *pdev) { struct resource *res; + bar->start = pci_resource_start(pdev, index); bar->end = pci_resource_end(pdev, index); bar->len = pci_resource_len(pdev, index); bar->flags = pci_resource_flags(pdev, index); if (!(IORESOURCE_MEM & bar->flags)) { - pr_info("bar%llu is no mem_region -> abort.\n", - (uint64_t) index); + pr_info("bar%llu is no mem_region -> abort.\n", (u64) index); return -EIO; } @@ -71,23 +71,23 @@ static int ccat_bar_init(struct ccat_bar *bar, size_t index, pr_info("allocate mem_region failed.\n"); return -EIO; } - pr_debug("bar%llu at [%lx,%lx] len=%lu res: %p.\n", (uint64_t) index, + pr_debug("bar%llu at [%lx,%lx] len=%lu res: %p.\n", (u64) index, bar->start, bar->end, bar->len, res); bar->ioaddr = ioremap(bar->start, bar->len); if (!bar->ioaddr) { - pr_info("bar%llu ioremap failed.\n", (uint64_t) index); + pr_info("bar%llu ioremap failed.\n", (u64) index); release_mem_region(bar->start, bar->len); return -EIO; } - pr_debug("bar%llu I/O mem mapped to %p.\n", (uint64_t) index, - bar->ioaddr); + pr_debug("bar%llu I/O mem mapped to %p.\n", (u64) index, bar->ioaddr); return 0; } void ccat_dma_free(struct ccat_dma *const dma) { const struct ccat_dma tmp = *dma; + free_dma(dma->channel); memset(dma, 0, sizeof(*dma)); dma_free_coherent(tmp.dev, tmp.size, tmp.virt, tmp.phys); @@ -104,12 +104,12 @@ int ccat_dma_init(struct ccat_dma *const dma, size_t channel, void __iomem * const ioaddr, struct device *const dev) { void *frame; - uint64_t addr; - uint32_t translateAddr; - uint32_t memTranslate; - uint32_t memSize; - uint32_t data = 0xffffffff; - uint32_t offset = (sizeof(uint64_t) * channel) + 0x1000; + u64 addr; + u32 translateAddr; + u32 memTranslate; + u32 memSize; + u32 data = 0xffffffff; + u32 offset = (sizeof(u64) * channel) + 0x1000; dma->channel = channel; dma->dev = dev; @@ -123,13 +123,12 @@ int ccat_dma_init(struct ccat_dma *const dma, size_t channel, dma->size = 2 * memSize - PAGE_SIZE; dma->virt = dma_zalloc_coherent(dev, dma->size, &dma->phys, GFP_KERNEL); if (!dma->virt || !dma->phys) { - pr_info("init DMA%llu memory failed.\n", (uint64_t) channel); + pr_info("init DMA%llu memory failed.\n", (u64) channel); return -1; } if (request_dma(channel, KBUILD_MODNAME)) { - pr_info("request dma channel %llu failed\n", - (uint64_t) channel); + pr_info("request dma channel %llu failed\n", (u64) channel); ccat_dma_free(dma); return -1; } @@ -140,9 +139,9 @@ int ccat_dma_init(struct ccat_dma *const dma, size_t channel, frame = dma->virt + translateAddr - dma->phys; pr_debug ("DMA%llu mem initialized\n virt: 0x%p\n phys: 0x%llx\n translated: 0x%llx\n pci addr: 0x%08x%x\n memTranslate: 0x%x\n size: %llu bytes.\n", - (uint64_t) channel, dma->virt, (uint64_t) (dma->phys), addr, + (u64) channel, dma->virt, (u64) (dma->phys), addr, ioread32(ioaddr + offset + 4), ioread32(ioaddr + offset), - memTranslate, (uint64_t) dma->size); + memTranslate, (u64) dma->size); return 0; } @@ -154,13 +153,13 @@ int ccat_dma_init(struct ccat_dma *const dma, size_t channel, static int ccat_functions_init(struct ccat_device *const ccatdev) { /* read CCatInfoBlock.nMaxEntries from ccat */ - const uint8_t num_func = ioread8(ccatdev->bar[0].ioaddr + 4); + const u8 num_func = ioread8(ccatdev->bar[0].ioaddr + 4); void __iomem *addr = ccatdev->bar[0].ioaddr; const void __iomem *end = addr + (sizeof(CCatInfoBlock) * num_func); - int status = 0; //count init function failures + int status = 0; /* count init function failures */ while (addr < end) { - const uint8_t type = ioread16(addr); + const u8 type = ioread16(addr); switch (type) { case CCATINFO_NOTUSED: break; @@ -209,6 +208,7 @@ static int ccat_probe(struct pci_dev *pdev, const struct pci_device_id *id) int status; u8 revision; struct ccat_device *ccatdev = kmalloc(sizeof(*ccatdev), GFP_KERNEL); + if (!ccatdev) { pr_err("%s() out of memory.\n", __FUNCTION__); return -ENOMEM; @@ -261,6 +261,7 @@ static int ccat_probe(struct pci_dev *pdev, const struct pci_device_id *id) static void ccat_remove(struct pci_dev *pdev) { struct ccat_device *ccatdev = pci_get_drvdata(pdev); + if (ccatdev) { ccat_functions_remove(ccatdev); ccat_bar_free(&ccatdev->bar[2]); diff --git a/devices/ccat/module.h b/devices/ccat/module.h index ab99d5c9..fd573a6d 100644 --- a/devices/ccat/module.h +++ b/devices/ccat/module.h @@ -22,6 +22,7 @@ #define _CCAT_H_ #include +#include #include #include #include "CCatDefinitions.h" @@ -80,15 +81,15 @@ extern int ccat_dma_init(struct ccat_dma *const dma, size_t channel, * @data: the bytes of the ethernet frame */ struct ccat_eth_frame { - uint32_t reserved1; - uint32_t received:1; - uint32_t reserved2:31; - uint16_t length; - uint16_t reserved3; - uint32_t sent:1; - uint32_t reserved4:31; - uint64_t timestamp; - uint8_t data[0x800 - 3 * sizeof(uint64_t)]; + u32 reserved1; + u32 received:1; + u32 reserved2:31; + u16 length; + u16 reserved3; + u32 sent:1; + u32 reserved4:31; + u64 timestamp; + u8 data[0x800 - 3 * sizeof(u64)]; }; /** @@ -148,14 +149,12 @@ struct ccat_device { * struct ccat_eth_priv - CCAT Ethernet/EtherCAT Master function (netdev) * @ccatdev: pointer to the parent struct ccat_device * @netdev: the net_device structure used by the kernel networking stack - * @poll_thread: is used to poll status registers like link state - * @rx_thread: thread which does housekeeping of RX DMA descriptors - * @tx_thread: thread which does housekeeping of TX DMA descriptors * @next_tx_frame: pointer to the next TX DMA descriptor, which the tx_thread should check for availablity * @info: holds a copy of the CCAT Ethernet/EtherCAT Master function information block (read from PCI config space) * @reg: register addresses in PCI config space of the Ethernet/EtherCAT Master function * @rx_fifo: DMA fifo used for RX DMA descriptors * @tx_fifo: DMA fifo used for TX DMA descriptors + * @poll_timer: interval timer used to poll CCAT for events like link changed, rx done, tx done * @rx_bytes: number of bytes received -> reported with ndo_get_stats64() * @rx_dropped: number of received frames, which were dropped -> reported with ndo_get_stats64() * @tx_bytes: number of bytes send -> reported with ndo_get_stats64() @@ -164,25 +163,24 @@ struct ccat_device { struct ccat_eth_priv { const struct ccat_device *ccatdev; struct net_device *netdev; - struct task_struct *poll_thread; - struct task_struct *rx_thread; - struct task_struct *tx_thread; - const struct ccat_eth_frame *next_tx_frame; /* next frame the tx_thread should check for availability */ + const struct ccat_eth_frame *next_tx_frame; CCatInfoBlock info; struct ccat_eth_register reg; struct ccat_eth_dma_fifo rx_fifo; struct ccat_eth_dma_fifo tx_fifo; + struct hrtimer poll_timer; atomic64_t rx_bytes; atomic64_t rx_dropped; atomic64_t tx_bytes; atomic64_t tx_dropped; ec_device_t *ecdev; void (*carrier_off) (struct net_device * const netdev); + bool (*carrier_ok) (struct net_device *const netdev); void (*carrier_on) (struct net_device * const netdev); void (*kfree_skb_any) (struct sk_buff * skb); void (*start_queue) (struct net_device * const netdev); void (*stop_queue) (struct net_device * const netdev); - void (*tx_fifo_full) (struct net_device * const dev, + void (*tx_fifo_full) (struct ccat_eth_priv * const priv, const struct ccat_eth_frame * const frame); void (*unregister) (struct net_device * const netdev); }; diff --git a/devices/ccat/netdev.c b/devices/ccat/netdev.c index 6e4d8855..10d8ded0 100644 --- a/devices/ccat/netdev.c +++ b/devices/ccat/netdev.c @@ -21,8 +21,6 @@ #include #include #include -#include -#include #include #include #include @@ -35,7 +33,7 @@ /** * EtherCAT frame to enable forwarding on EtherCAT Terminals */ -static const UINT8 frameForwardEthernetFrames[] = { +static const u8 frameForwardEthernetFrames[] = { 0x01, 0x01, 0x05, 0x01, 0x00, 0x00, 0x00, 0x1b, 0x21, 0x36, 0x1b, 0xce, 0x88, 0xa4, 0x0e, 0x10, @@ -50,13 +48,8 @@ static const UINT8 frameForwardEthernetFrames[] = { }; #define FIFO_LENGTH 64 -#define DMA_POLL_DELAY_RANGE_USECS 100, 100 /* time to sleep between rx/tx DMA polls */ -#define POLL_DELAY_RANGE_USECS 500, 1000 /* time to sleep between link state polls */ - static void ec_poll(struct net_device *dev); -static int run_poll_thread(void *data); -static int run_rx_thread(void *data); -static int run_tx_thread(void *data); +static enum hrtimer_restart poll_timer_callback(struct hrtimer *timer); #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35) static struct rtnl_link_stats64 *ccat_eth_get_stats64(struct net_device *dev, struct rtnl_link_stats64 @@ -83,6 +76,12 @@ static void ecdev_kfree_skb_any(struct sk_buff *skb) /* never release a skb in EtherCAT mode */ } +static bool ecdev_carrier_ok(struct net_device *const netdev) +{ + struct ccat_eth_priv *const priv = netdev_priv(netdev); + return ecdev_get_link(priv->ecdev); +} + static void ecdev_carrier_on(struct net_device *const netdev) { struct ccat_eth_priv *const priv = netdev_priv(netdev); @@ -100,7 +99,7 @@ static void ecdev_nop(struct net_device *const netdev) /* dummy called if nothing has to be done in EtherCAT operation mode */ } -static void ecdev_tx_fifo_full(struct net_device *const dev, +static void ecdev_tx_fifo_full(struct ccat_eth_priv *const priv, const struct ccat_eth_frame *const frame) { /* we are polled -> there is nothing we can do in EtherCAT mode */ @@ -120,7 +119,8 @@ static void ccat_eth_rx_fifo_add(struct ccat_eth_frame *frame, struct ccat_eth_dma_fifo *fifo) { const size_t offset = ((void *)(frame) - fifo->dma.virt); - const uint32_t addr_and_length = (1 << 31) | offset; + const u32 addr_and_length = (1 << 31) | offset; + frame->received = 0; iowrite32(addr_and_length, fifo->reg); } @@ -132,13 +132,11 @@ static void ccat_eth_tx_fifo_add_free(struct ccat_eth_frame *frame, frame->sent = 1; } -static void ccat_eth_tx_fifo_full(struct net_device *const dev, +static void ccat_eth_tx_fifo_full(struct ccat_eth_priv *const priv, const struct ccat_eth_frame *const frame) { - struct ccat_eth_priv *const priv = netdev_priv(dev); - netif_stop_queue(dev); + priv->stop_queue(priv->netdev); priv->next_tx_frame = frame; - wake_up_process(priv->tx_thread); } static void ccat_eth_dma_fifo_reset(struct ccat_eth_dma_fifo *fifo) @@ -166,7 +164,7 @@ static int ccat_eth_dma_fifo_init(struct ccat_eth_dma_fifo *fifo, if (0 != ccat_dma_init(&fifo->dma, channel, priv->ccatdev->bar[2].ioaddr, &priv->ccatdev->pdev->dev)) { - pr_info("init DMA%llu memory failed.\n", (uint64_t) channel); + pr_info("init DMA%llu memory failed.\n", (u64) channel); return -1; } fifo->add = add; @@ -187,7 +185,6 @@ static void ccat_eth_priv_free_dma(struct ccat_eth_priv *priv) /* release dma */ ccat_dma_free(&priv->rx_fifo.dma); ccat_dma_free(&priv->tx_fifo.dma); - pr_debug("DMA fifo's stopped.\n"); } /** @@ -225,6 +222,7 @@ static void ccat_eth_priv_init_mappings(struct ccat_eth_priv *priv) CCatInfoBlockOffs offsets; void __iomem *const func_base = priv->ccatdev->bar[0].ioaddr + priv->info.nAddr; + memcpy_fromio(&offsets, func_base, sizeof(offsets)); priv->reg.mii = func_base + offsets.nMMIOffs; priv->reg.tx_fifo = func_base + offsets.nTxFifoOffs; @@ -251,6 +249,7 @@ static struct rtnl_link_stats64 *ccat_eth_get_stats64(struct net_device *dev, st { struct ccat_eth_priv *const priv = netdev_priv(dev); CCatMacRegs mac; + memcpy_fromio(&mac, priv->reg.mac, sizeof(mac)); storage->rx_packets = mac.rxFrameCnt; /* total packets received */ storage->tx_packets = mac.txFrameCnt; /* total packets transmitted */ @@ -290,6 +289,7 @@ struct ccat_eth_priv *ccat_eth_init(const struct ccat_device *const ccatdev, { struct ccat_eth_priv *priv; struct net_device *const netdev = alloc_etherdev(sizeof(*priv)); + priv = netdev_priv(netdev); priv->netdev = netdev; priv->ccatdev = ccatdev; @@ -313,6 +313,7 @@ struct ccat_eth_priv *ccat_eth_init(const struct ccat_device *const ccatdev, priv->ecdev = ecdev_offer(netdev, ec_poll, THIS_MODULE); if (priv->ecdev) { priv->carrier_off = ecdev_carrier_off; + priv->carrier_ok = ecdev_carrier_ok; priv->carrier_on = ecdev_carrier_on; priv->kfree_skb_any = ecdev_kfree_skb_any; priv->start_queue = ecdev_nop; @@ -331,6 +332,7 @@ struct ccat_eth_priv *ccat_eth_init(const struct ccat_device *const ccatdev, /* EtherCAT disabled -> prepare normal ethernet mode */ priv->carrier_off = netif_carrier_off; + priv->carrier_ok = netif_carrier_ok; priv->carrier_on = netif_carrier_on; priv->kfree_skb_any = dev_kfree_skb_any; priv->start_queue = netif_start_queue; @@ -344,19 +346,11 @@ struct ccat_eth_priv *ccat_eth_init(const struct ccat_device *const ccatdev, return NULL; } pr_info("registered %s as network device.\n", netdev->name); - priv->rx_thread = kthread_run(run_rx_thread, netdev, "%s_rx", KBUILD_MODNAME); - priv->tx_thread = kthread_run(run_tx_thread, netdev, "%s_tx", KBUILD_MODNAME); return priv; } void ccat_eth_remove(struct ccat_eth_priv *const priv) { - if (priv->rx_thread) { - kthread_stop(priv->rx_thread); - } - if (priv->tx_thread) { - kthread_stop(priv->tx_thread); - } priv->unregister(priv->netdev); ccat_eth_priv_free_dma(priv); free_netdev(priv->netdev); @@ -366,11 +360,12 @@ void ccat_eth_remove(struct ccat_eth_priv *const priv) static int ccat_eth_open(struct net_device *dev) { struct ccat_eth_priv *const priv = netdev_priv(dev); - priv->carrier_off(dev); - priv->poll_thread = - kthread_run(run_poll_thread, dev, "%s_poll", KBUILD_MODNAME); - //TODO + priv->carrier_off(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; } @@ -381,6 +376,7 @@ static void ccat_eth_receive(struct net_device *const dev, 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); @@ -424,7 +420,7 @@ static netdev_tx_t ccat_eth_start_xmit(struct sk_buff *skb, struct ccat_eth_priv *const priv = netdev_priv(dev); struct ccat_eth_frame *const frame = ((struct ccat_eth_frame *)priv->tx_fifo.dma.virt); - uint32_t addr_and_length; + u32 addr_and_length; if (skb_is_nonlinear(skb)) { pr_warn("Non linear skb not supported -> drop frame.\n"); @@ -435,7 +431,7 @@ static netdev_tx_t ccat_eth_start_xmit(struct sk_buff *skb, if (skb->len > sizeof(frame->data)) { pr_warn("skb.len %llu exceeds dma buffer %llu -> drop frame.\n", - (uint64_t) skb->len, (uint64_t) sizeof(frame->data)); + (u64) skb->len, (u64) sizeof(frame->data)); atomic64_inc(&priv->tx_dropped); priv->kfree_skb_any(skb); return NETDEV_TX_OK; @@ -443,7 +439,7 @@ static netdev_tx_t ccat_eth_start_xmit(struct sk_buff *skb, if (!frame[next].sent) { netdev_err(dev, "BUG! Tx Ring full when queue awake!\n"); - ccat_eth_tx_fifo_full(dev, &frame[next]); + ccat_eth_tx_fifo_full(priv, &frame[next]); return NETDEV_TX_BUSY; } @@ -463,7 +459,7 @@ static netdev_tx_t ccat_eth_start_xmit(struct sk_buff *skb, next = (next + 1) % FIFO_LENGTH; /* stop queue if tx ring is full */ if (!frame[next].sent) { - ccat_eth_tx_fifo_full(dev, &frame[next]); + ccat_eth_tx_fifo_full(priv, &frame[next]); } return NETDEV_TX_OK; } @@ -471,12 +467,9 @@ static netdev_tx_t ccat_eth_start_xmit(struct sk_buff *skb, static int ccat_eth_stop(struct net_device *dev) { struct ccat_eth_priv *const priv = netdev_priv(dev); + priv->stop_queue(dev); - if (priv->poll_thread) { - /* TODO care about smp context? */ - kthread_stop(priv->poll_thread); - priv->poll_thread = NULL; - } + hrtimer_cancel(&priv->poll_timer); netdev_info(dev, "stopped.\n"); return 0; } @@ -484,6 +477,7 @@ static int ccat_eth_stop(struct net_device *dev) 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"); @@ -492,6 +486,7 @@ static void ccat_eth_link_down(struct net_device *dev) 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, @@ -515,6 +510,7 @@ 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); @@ -522,74 +518,61 @@ static void ccat_eth_xmit_raw(struct net_device *dev, const char *const data, } /** - * Since CCAT doesn't support interrupts until now, we have to poll - * some status bits to recognize things like link change etc. + * Poll for link state changes */ -static int run_poll_thread(void *data) +static void poll_link(struct ccat_eth_priv *const priv) { - struct net_device *const dev = (struct net_device *)data; - struct ccat_eth_priv *const priv = netdev_priv(dev); - size_t link = 0; + const size_t link = ccat_eth_priv_read_link_state(priv); - while (!kthread_should_stop()) { - if (ccat_eth_priv_read_link_state(priv) != link) { - link = !link; - link ? ccat_eth_link_up(dev) : ccat_eth_link_down(dev); - } - usleep_range(POLL_DELAY_RANGE_USECS); + if (link != priv->carrier_ok(priv->netdev)) { + if (link) + ccat_eth_link_up(priv->netdev); + else + ccat_eth_link_down(priv->netdev); } - pr_debug("%s() stopped.\n", __FUNCTION__); - return 0; -} - -static int run_rx_thread(void *data) -{ - struct net_device *const dev = (struct net_device *)data; - struct ccat_eth_priv *const priv = netdev_priv(dev); - struct ccat_eth_frame *frame = priv->rx_fifo.dma.virt; - const struct ccat_eth_frame *const end = frame + FIFO_LENGTH; - - while (!kthread_should_stop()) { - /* wait until frame was used by DMA for Rx */ - while (!kthread_should_stop() && !frame->received) { - usleep_range(DMA_POLL_DELAY_RANGE_USECS); - } - - /* can be NULL, if we are asked to stop! */ - if (frame->received) { - ccat_eth_receive(dev, frame); - frame->received = 0; - ccat_eth_rx_fifo_add(frame, &priv->rx_fifo); - } - if (++frame >= end) { - frame = priv->rx_fifo.dma.virt; - } - } - pr_debug("%s() stopped.\n", __FUNCTION__); - return 0; } /** - * Polling of tx dma descriptors in ethernet operating mode + * Poll for available rx dma descriptors in ethernet operating mode */ -static int run_tx_thread(void *data) +static void poll_rx(struct ccat_eth_priv *const priv) { - struct net_device *const dev = (struct net_device *)data; - struct ccat_eth_priv *const priv = netdev_priv(dev); + struct ccat_eth_frame *const frame = priv->rx_fifo.dma.virt; + static size_t next = 0; - set_current_state(TASK_INTERRUPTIBLE); - while (!kthread_should_stop()) { - const struct ccat_eth_frame *const frame = priv->next_tx_frame; - if (frame) { - while (!kthread_should_stop() && !frame->sent) { - usleep_range(DMA_POLL_DELAY_RANGE_USECS); - } - } - netif_wake_queue(dev); - schedule(); - set_current_state(TASK_INTERRUPTIBLE); + /* 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; } - set_current_state(TASK_RUNNING); - pr_debug("%s() stopped.\n", __FUNCTION__); - return 0; +} + +/** + * 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; } diff --git a/devices/ccat/update.c b/devices/ccat/update.c index 7ccdd08a..90a04262 100644 --- a/devices/ccat/update.c +++ b/devices/ccat/update.c @@ -61,10 +61,9 @@ struct update_buffer { static void ccat_wait_status_cleared(void __iomem * const ioaddr); static int ccat_read_flash(void __iomem * const ioaddr, char __user * buf, - uint32_t len, loff_t * off); + u32 len, loff_t * off); static void ccat_write_flash(const struct update_buffer *const buf); -static void ccat_update_cmd(void __iomem * const ioaddr, uint8_t cmd, - uint16_t clocks); +static void ccat_update_cmd(void __iomem * const ioaddr, u8 cmd, u16 clocks); static void ccat_update_destroy(struct kref *ref); /** @@ -84,6 +83,7 @@ static int ccat_update_open(struct inode *const i, struct file *const f) struct ccat_update *update = container_of(i->i_cdev, struct ccat_update, cdev); struct update_buffer *buf; + kref_get(&update->refcount); if (atomic_read(&update->refcount.refcount) > 2) { kref_put(&update->refcount, ccat_update_destroy); @@ -105,6 +105,7 @@ static int ccat_update_release(struct inode *const i, struct file *const f) { const struct update_buffer *const buf = f->private_data; struct ccat_update *const update = buf->update; + if (buf->size > 0) { ccat_update_cmd(update->ioaddr, CCAT_WRITE_ENABLE); ccat_update_cmd(update->ioaddr, CCAT_BULK_ERASE); @@ -133,6 +134,7 @@ static ssize_t ccat_update_read(struct file *const f, char __user * buf, size_t len, loff_t * off) { struct update_buffer *update = f->private_data; + if (!buf || !off) { return -EINVAL; } @@ -162,6 +164,7 @@ static ssize_t ccat_update_write(struct file *const f, const char __user * buf, size_t len, loff_t * off) { struct update_buffer *const update = f->private_data; + if (*off + len > sizeof(update->data)) return 0; @@ -190,8 +193,8 @@ static struct file_operations update_ops = { * * no write memory barrier is called and the busy flag is not evaluated */ -static inline void __ccat_update_cmd(void __iomem * const ioaddr, uint8_t cmd, - uint16_t clocks) +static inline void __ccat_update_cmd(void __iomem * const ioaddr, u8 cmd, + u16 clocks) { iowrite8((0xff00 & clocks) >> 8, ioaddr); iowrite8(0x00ff & clocks, ioaddr + 0x8); @@ -207,8 +210,8 @@ static inline void __ccat_update_cmd(void __iomem * const ioaddr, uint8_t cmd, * Triggers a full flash command cycle with write memory barrier and * command activate. This call blocks until the busy flag is reset. */ -static inline void ccat_update_cmd(void __iomem * const ioaddr, uint8_t cmd, - uint16_t clocks) +static inline void ccat_update_cmd(void __iomem * const ioaddr, u8 cmd, + u16 clocks) { __ccat_update_cmd(ioaddr, cmd, clocks); wmb(); @@ -227,12 +230,12 @@ static inline void ccat_update_cmd(void __iomem * const ioaddr, uint8_t cmd, * command activate. This call blocks until the busy flag is reset. */ static inline void ccat_update_cmd_addr(void __iomem * const ioaddr, - uint8_t cmd, uint16_t clocks, - uint32_t addr) + u8 cmd, u16 clocks, u32 addr) { - const uint8_t addr_0 = SWAP_BITS(addr & 0xff); - const uint8_t addr_1 = SWAP_BITS((addr & 0xff00) >> 8); - const uint8_t addr_2 = SWAP_BITS((addr & 0xff0000) >> 16); + const u8 addr_0 = SWAP_BITS(addr & 0xff); + const u8 addr_1 = SWAP_BITS((addr & 0xff00) >> 8); + const u8 addr_2 = SWAP_BITS((addr & 0xff0000) >> 16); + __ccat_update_cmd(ioaddr, cmd, clocks); iowrite8(addr_2, ioaddr + 0x18); iowrite8(addr_1, ioaddr + 0x20); @@ -248,7 +251,7 @@ static inline void ccat_update_cmd_addr(void __iomem * const ioaddr, * * Return: the CCAT FPGA's PROM identifier */ -uint8_t ccat_get_prom_id(void __iomem * const ioaddr) +u8 ccat_get_prom_id(void __iomem * const ioaddr) { ccat_update_cmd(ioaddr, CCAT_GET_PROM_ID); return ioread8(ioaddr + 0x38); @@ -260,7 +263,7 @@ uint8_t ccat_get_prom_id(void __iomem * const ioaddr) * * Return: the current status of the CCAT Update function */ -static uint8_t ccat_get_status(void __iomem * const ioaddr) +static u8 ccat_get_status(void __iomem * const ioaddr) { ccat_update_cmd(ioaddr, CCAT_READ_STATUS); return ioread8(ioaddr + 0x20); @@ -272,10 +275,10 @@ static uint8_t ccat_get_status(void __iomem * const ioaddr) * * Blocks until bit 7 of the CCAT Update status is reset */ - static void ccat_wait_status_cleared(void __iomem * const ioaddr) { - uint8_t status; + u8 status; + do { status = ccat_get_status(ioaddr); } while (status & (1 << 7)); @@ -296,11 +299,12 @@ static void ccat_wait_status_cleared(void __iomem * const ioaddr) * Return: the number of bytes copied */ static int ccat_read_flash_block(void __iomem * const ioaddr, - const uint32_t addr, const uint16_t len, + const u32 addr, const u16 len, char __user * const buf) { - uint16_t i; - const uint16_t clocks = 8 * len; + u16 i; + const u16 clocks = 8 * len; + ccat_update_cmd_addr(ioaddr, CCAT_READ_FLASH + clocks, addr); for (i = 0; i < len; i++) { put_user(ioread8(ioaddr + CCAT_DATA_IN_4 + 8 * i), buf + i); @@ -321,9 +325,10 @@ static int ccat_read_flash_block(void __iomem * const ioaddr, * Return: the number of bytes copied */ static int ccat_read_flash(void __iomem * const ioaddr, char __user * buf, - uint32_t len, loff_t * off) + u32 len, loff_t * off) { const loff_t start = *off; + while (len > CCAT_DATA_BLOCK_SIZE) { *off += ccat_read_flash_block(ioaddr, *off, CCAT_DATA_BLOCK_SIZE, @@ -347,11 +352,12 @@ static int ccat_read_flash(void __iomem * const ioaddr, char __user * buf, * Return: the number of bytes copied */ static int ccat_write_flash_block(void __iomem * const ioaddr, - const uint32_t addr, const uint16_t len, + const u32 addr, const u16 len, const char *const buf) { - const uint16_t clocks = 8 * len; - uint16_t i; + const u16 clocks = 8 * len; + u16 i; + ccat_update_cmd(ioaddr, CCAT_WRITE_ENABLE); for (i = 0; i < len; i++) { iowrite8(buf[i], ioaddr + CCAT_DATA_OUT_4 + 8 * i); @@ -368,17 +374,17 @@ static int ccat_write_flash_block(void __iomem * const ioaddr, static void ccat_write_flash(const struct update_buffer *const update) { const char *buf = update->data; - uint32_t off = 0; + u32 off = 0; size_t len = update->size; + while (len > CCAT_WRITE_BLOCK_SIZE) { ccat_write_flash_block(update->update->ioaddr, off, - (uint16_t) CCAT_WRITE_BLOCK_SIZE, buf); + (u16) CCAT_WRITE_BLOCK_SIZE, buf); off += CCAT_WRITE_BLOCK_SIZE; buf += CCAT_WRITE_BLOCK_SIZE; len -= CCAT_WRITE_BLOCK_SIZE; } - ccat_write_flash_block(update->update->ioaddr, off, (uint16_t) len, - buf); + ccat_write_flash_block(update->update->ioaddr, off, (u16) len, buf); } /** @@ -388,6 +394,7 @@ struct ccat_update *ccat_update_init(const struct ccat_device *const ccatdev, void __iomem * const addr) { struct ccat_update *const update = kzalloc(sizeof(*update), GFP_KERNEL); + if (!update) { return NULL; } @@ -443,6 +450,7 @@ static void ccat_update_destroy(struct kref *ref) { struct ccat_update *update = container_of(ref, struct ccat_update, refcount); + cdev_del(&update->cdev); device_destroy(update->class, update->dev); class_destroy(update->class); diff --git a/devices/ccat/update.h b/devices/ccat/update.h index 1456bf65..52348835 100644 --- a/devices/ccat/update.h +++ b/devices/ccat/update.h @@ -20,7 +20,7 @@ #ifndef _UPDATE_H_ #define _UPDATE_H_ -extern uint8_t ccat_get_prom_id(void __iomem * const ioaddr); +extern u8 ccat_get_prom_id(void __iomem * const ioaddr); extern struct ccat_update *ccat_update_init(const struct ccat_device *ccatdev, void __iomem * addr); extern void ccat_update_remove(struct ccat_update *update);