devices/ccat/module.h
branchstable-1.5
changeset 2556 674fcdccc0f3
parent 2554 770881d887c5
child 2565 f7b06b264646
equal deleted inserted replaced
2540:fca12d7035d1 2556:674fcdccc0f3
       
     1 /**
       
     2     Network Driver for Beckhoff CCAT communication controller
       
     3     Copyright (C) 2014  Beckhoff Automation GmbH
       
     4     Author: Patrick Bruenn <p.bruenn@beckhoff.com>
       
     5 
       
     6     This program is free software; you can redistribute it and/or modify
       
     7     it under the terms of the GNU General Public License as published by
       
     8     the Free Software Foundation; either version 2 of the License, or
       
     9     (at your option) any later version.
       
    10 
       
    11     This program is distributed in the hope that it will be useful,
       
    12     but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14     GNU General Public License for more details.
       
    15 
       
    16     You should have received a copy of the GNU General Public License along
       
    17     with this program; if not, write to the Free Software Foundation, Inc.,
       
    18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
       
    19 */
       
    20 
       
    21 #ifndef _CCAT_H_
       
    22 #define _CCAT_H_
       
    23 
       
    24 #include <linux/cdev.h>
       
    25 #include <linux/kernel.h>
       
    26 #include <linux/pci.h>
       
    27 #include "CCatDefinitions.h"
       
    28 #include "../ecdev.h"
       
    29 
       
    30 #define DRV_NAME         "ec_ccat"
       
    31 #define DRV_EXTRAVERSION "-ec"
       
    32 #define DRV_VERSION      "0.8" DRV_EXTRAVERSION
       
    33 #define DRV_DESCRIPTION  "Beckhoff CCAT Ethernet/EtherCAT Network Driver"
       
    34 
       
    35 #undef pr_fmt
       
    36 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
       
    37 
       
    38 /**
       
    39  * struct ccat_bar - CCAT PCI Base Address Register(BAR) configuration
       
    40  * @start: start address of this BAR
       
    41  * @end: end address of this BAR
       
    42  * @len: length of this BAR
       
    43  * @flags: flags set on this BAR
       
    44  * @ioaddr: ioremapped address of this bar
       
    45  */
       
    46 struct ccat_bar {
       
    47 	unsigned long start;
       
    48 	unsigned long end;
       
    49 	unsigned long len;
       
    50 	unsigned long flags;
       
    51 	void __iomem *ioaddr;
       
    52 };
       
    53 
       
    54 /**
       
    55  * struct ccat_dma - CCAT DMA channel configuration
       
    56  * @phys: device-viewed address(physical) of the associated DMA memory
       
    57  * @virt: CPU-viewed address(virtual) of the associated DMA memory
       
    58  * @size: number of bytes in the associated DMA memory
       
    59  * @channel: CCAT DMA channel number
       
    60  * @dev: valid struct device pointer
       
    61  */
       
    62 struct ccat_dma {
       
    63 	dma_addr_t phys;
       
    64 	void *virt;
       
    65 	size_t size;
       
    66 	size_t channel;
       
    67 	struct device *dev;
       
    68 };
       
    69 
       
    70 extern void ccat_dma_free(struct ccat_dma *const dma);
       
    71 extern int ccat_dma_init(struct ccat_dma *const dma, size_t channel,
       
    72 			 void __iomem * const ioaddr, struct device *const dev);
       
    73 
       
    74 /**
       
    75  * struct ccat_eth_frame - Ethernet frame with DMA descriptor header in front
       
    76  * @reservedn: is not used and should always be set to 0
       
    77  * @received: used for reception, is set to 1 by the CCAT when data was written
       
    78  * @length: number of bytes in the frame including the DMA header
       
    79  * @sent: is set to 1 by the CCAT when data was transmitted
       
    80  * @timestamp: a 64 bit EtherCAT timestamp
       
    81  * @data: the bytes of the ethernet frame
       
    82  */
       
    83 struct ccat_eth_frame {
       
    84 	uint32_t reserved1;
       
    85 	uint32_t received:1;
       
    86 	uint32_t reserved2:31;
       
    87 	uint16_t length;
       
    88 	uint16_t reserved3;
       
    89 	uint32_t sent:1;
       
    90 	uint32_t reserved4:31;
       
    91 	uint64_t timestamp;
       
    92 	uint8_t data[0x800 - 3 * sizeof(uint64_t)];
       
    93 };
       
    94 
       
    95 /**
       
    96  * struct ccat_eth_register - CCAT register addresses in the PCI BAR
       
    97  * @mii: address of the CCAT management interface register
       
    98  * @tx_fifo: address of the CCAT TX DMA fifo register
       
    99  * @rx_fifo: address of the CCAT RX DMA fifo register
       
   100  * @mac: address of the CCAT media access control register
       
   101  * @rx_mem: address of the CCAT register holding the RX DMA address
       
   102  * @tx_mem: address of the CCAT register holding the TX DMA address
       
   103  * @misc: address of a CCAT register holding miscellaneous information
       
   104  */
       
   105 struct ccat_eth_register {
       
   106 	void __iomem *mii;
       
   107 	void __iomem *tx_fifo;
       
   108 	void __iomem *rx_fifo;
       
   109 	void __iomem *mac;
       
   110 	void __iomem *rx_mem;
       
   111 	void __iomem *tx_mem;
       
   112 	void __iomem *misc;
       
   113 };
       
   114 
       
   115 /**
       
   116  * struct ccat_eth_dma_fifo - CCAT RX or TX DMA fifo
       
   117  * @add: callback used to add a frame to this fifo
       
   118  * @reg: PCI register address of this DMA fifo
       
   119  * @dma: information about the associated DMA memory
       
   120  */
       
   121 struct ccat_eth_dma_fifo {
       
   122 	void (*add) (struct ccat_eth_frame *, struct ccat_eth_dma_fifo *);
       
   123 	void __iomem *reg;
       
   124 	struct ccat_dma dma;
       
   125 };
       
   126 
       
   127 /**
       
   128  * struct ccat_device - CCAT device representation
       
   129  * @pdev: pointer to the pci object allocated by the kernel
       
   130  * @ethdev: CCAT Ethernet/EtherCAT Master (with DMA) function, NULL if function is not available or failed to initialize
       
   131  * @update: CCAT Update function, NULL if function is not available or failed to initialize
       
   132  * @bar [0] and [2] holding information about PCI BARs 0 and 2.
       
   133  *
       
   134  * One instance of a ccat_device should represent a physical CCAT. Since
       
   135  * a CCAT is implemented as FPGA the available functions can vary so
       
   136  * the function object pointers can be NULL.
       
   137  * Extra note: you will recognize that PCI BAR1 is not used and is a
       
   138  * waste of memory, thats true but right now, its very easy to use it
       
   139  * this way. So we might optimize it later.
       
   140  */
       
   141 struct ccat_device {
       
   142 	struct pci_dev *pdev;
       
   143 	struct ccat_eth_priv *ethdev;
       
   144 	struct ccat_update *update;
       
   145 	struct ccat_bar bar[3];	//TODO optimize this
       
   146 };
       
   147 
       
   148 /**
       
   149  * struct ccat_eth_priv - CCAT Ethernet/EtherCAT Master function (netdev)
       
   150  * @ccatdev: pointer to the parent struct ccat_device
       
   151  * @netdev: the net_device structure used by the kernel networking stack
       
   152  * @poll_thread: is used to poll status registers like link state
       
   153  * @rx_thread: thread which does housekeeping of RX DMA descriptors
       
   154  * @tx_thread: thread which does housekeeping of TX DMA descriptors
       
   155  * @next_tx_frame: pointer to the next TX DMA descriptor, which the tx_thread should check for availablity
       
   156  * @info: holds a copy of the CCAT Ethernet/EtherCAT Master function information block (read from PCI config space)
       
   157  * @reg: register addresses in PCI config space of the Ethernet/EtherCAT Master function
       
   158  * @rx_fifo: DMA fifo used for RX DMA descriptors
       
   159  * @tx_fifo: DMA fifo used for TX DMA descriptors
       
   160  * @rx_bytes: number of bytes received -> reported with ndo_get_stats64()
       
   161  * @rx_dropped: number of received frames, which were dropped -> reported with ndo_get_stats64()
       
   162  * @tx_bytes: number of bytes send -> reported with ndo_get_stats64()
       
   163  * @tx_dropped: number of frames requested to send, which were dropped -> reported with ndo_get_stats64()
       
   164  */
       
   165 struct ccat_eth_priv {
       
   166 	const struct ccat_device *ccatdev;
       
   167 	struct net_device *netdev;
       
   168 	struct task_struct *poll_thread;
       
   169 	struct task_struct *rx_thread;
       
   170 	struct task_struct *tx_thread;
       
   171 	const struct ccat_eth_frame *next_tx_frame;	/* next frame the tx_thread should check for availability */
       
   172 	CCatInfoBlock info;
       
   173 	struct ccat_eth_register reg;
       
   174 	struct ccat_eth_dma_fifo rx_fifo;
       
   175 	struct ccat_eth_dma_fifo tx_fifo;
       
   176 	atomic64_t rx_bytes;
       
   177 	atomic64_t rx_dropped;
       
   178 	atomic64_t tx_bytes;
       
   179 	atomic64_t tx_dropped;
       
   180 	ec_device_t *ecdev;
       
   181 	void (*carrier_off) (struct net_device * const netdev);
       
   182 	void (*carrier_on) (struct net_device * const netdev);
       
   183 	void (*kfree_skb_any) (struct sk_buff * skb);
       
   184 	void (*start_queue) (struct net_device * const netdev);
       
   185 	void (*stop_queue) (struct net_device * const netdev);
       
   186 	void (*tx_fifo_full) (struct net_device * const dev,
       
   187 			      const struct ccat_eth_frame * const frame);
       
   188 	void (*unregister) (struct net_device * const netdev);
       
   189 };
       
   190 
       
   191 /**
       
   192  * struct ccat_update - CCAT Update function (update)
       
   193  * @ccatdev: pointer to the parent struct ccat_device
       
   194  * @ioaddr: PCI base address of the CCAT Update function
       
   195  * dev: device number for this update function
       
   196  * cdev: character device used for the CCAT Update function
       
   197  * class: pointer to a device class used when registering the CCAT Update device
       
   198  * @info: holds a copy of the CCAT Update function information block (read from PCI config space)
       
   199  */
       
   200 struct ccat_update {
       
   201 	struct kref refcount;
       
   202 	void __iomem *ioaddr;
       
   203 	dev_t dev;
       
   204 	struct cdev cdev;
       
   205 	struct class *class;
       
   206 	CCatInfoBlock info;
       
   207 };
       
   208 #endif /* #ifndef _CCAT_H_ */