devices/ccat/module.h
changeset 2589 2b9c78543663
equal deleted inserted replaced
2415:af21f0bdc7c9 2589:2b9c78543663
       
     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/hrtimer.h>
       
    26 #include <linux/kernel.h>
       
    27 #include <linux/pci.h>
       
    28 #include "../ecdev.h"
       
    29 
       
    30 #define DRV_EXTRAVERSION "-ec"
       
    31 #define DRV_VERSION      "0.10" DRV_EXTRAVERSION
       
    32 #define DRV_DESCRIPTION  "Beckhoff CCAT Ethernet/EtherCAT Network Driver"
       
    33 
       
    34 #undef pr_fmt
       
    35 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
       
    36 
       
    37 /**
       
    38  * CCAT function type identifiers (u16)
       
    39  */
       
    40 enum ccat_info_t {
       
    41 	CCATINFO_NOTUSED = 0,
       
    42 	CCATINFO_EPCS_PROM = 0xf,
       
    43 	CCATINFO_ETHERCAT_MASTER_DMA = 0x14,
       
    44 	CCATINFO_COPY_BLOCK = 0x17,
       
    45 	CCATINFO_MAX
       
    46 };
       
    47 
       
    48 /**
       
    49  * struct ccat_bar - CCAT PCI Base Address Register(BAR) configuration
       
    50  * @start: start address of this BAR
       
    51  * @end: end address of this BAR
       
    52  * @len: length of this BAR
       
    53  * @flags: flags set on this BAR
       
    54  * @ioaddr: ioremapped address of this bar
       
    55  */
       
    56 struct ccat_bar {
       
    57 	unsigned long start;
       
    58 	unsigned long end;
       
    59 	unsigned long len;
       
    60 	unsigned long flags;
       
    61 	void __iomem *ioaddr;
       
    62 };
       
    63 
       
    64 /**
       
    65  * struct ccat_dma - CCAT DMA channel configuration
       
    66  * @phys: device-viewed address(physical) of the associated DMA memory
       
    67  * @virt: CPU-viewed address(virtual) of the associated DMA memory
       
    68  * @size: number of bytes in the associated DMA memory
       
    69  * @channel: CCAT DMA channel number
       
    70  * @dev: valid struct device pointer
       
    71  */
       
    72 struct ccat_dma {
       
    73 	dma_addr_t phys;
       
    74 	void *virt;
       
    75 	size_t size;
       
    76 	size_t channel;
       
    77 	struct device *dev;
       
    78 };
       
    79 
       
    80 extern void ccat_dma_free(struct ccat_dma *const dma);
       
    81 extern int ccat_dma_init(struct ccat_dma *const dma, size_t channel,
       
    82 			 void __iomem * const ioaddr, struct device *const dev);
       
    83 
       
    84 /**
       
    85  * struct ccat_eth_frame - Ethernet frame with DMA descriptor header in front
       
    86  * @reservedn: is not used and should always be set to 0
       
    87  * @received: used for reception, is set to 1 by the CCAT when data was written
       
    88  * @length: number of bytes in the frame including the DMA header
       
    89  * @sent: is set to 1 by the CCAT when data was transmitted
       
    90  * @timestamp: a 64 bit EtherCAT timestamp
       
    91  * @data: the bytes of the ethernet frame
       
    92  */
       
    93 struct ccat_eth_frame {
       
    94 	__le32 reserved1;
       
    95 	__le32 rx_flags;
       
    96 #define CCAT_FRAME_RECEIVED 0x1
       
    97 	__le16 length;
       
    98 	__le16 reserved3;
       
    99 	__le32 tx_flags;
       
   100 #define CCAT_FRAME_SENT 0x1
       
   101 	__le64 timestamp;
       
   102 	u8 data[0x800 - 3 * sizeof(u64)];
       
   103 #define CCAT_ETH_FRAME_HEAD_LEN offsetof(struct ccat_eth_frame, data)
       
   104 };
       
   105 
       
   106 /**
       
   107  * struct ccat_eth_register - CCAT register addresses in the PCI BAR
       
   108  * @mii: address of the CCAT management interface register
       
   109  * @tx_fifo: address of the CCAT TX DMA fifo register
       
   110  * @rx_fifo: address of the CCAT RX DMA fifo register
       
   111  * @mac: address of the CCAT media access control register
       
   112  * @rx_mem: address of the CCAT register holding the RX DMA address
       
   113  * @tx_mem: address of the CCAT register holding the TX DMA address
       
   114  * @misc: address of a CCAT register holding miscellaneous information
       
   115  */
       
   116 struct ccat_eth_register {
       
   117 	void __iomem *mii;
       
   118 	void __iomem *tx_fifo;
       
   119 	void __iomem *rx_fifo;
       
   120 	void __iomem *mac;
       
   121 	void __iomem *rx_mem;
       
   122 	void __iomem *tx_mem;
       
   123 	void __iomem *misc;
       
   124 };
       
   125 
       
   126 /**
       
   127  * struct ccat_eth_dma_fifo - CCAT RX or TX DMA fifo
       
   128  * @add: callback used to add a frame to this fifo
       
   129  * @reg: PCI register address of this DMA fifo
       
   130  * @dma: information about the associated DMA memory
       
   131  */
       
   132 struct ccat_eth_dma_fifo {
       
   133 	void (*add) (struct ccat_eth_dma_fifo *, struct ccat_eth_frame *);
       
   134 	void __iomem *reg;
       
   135 	const struct ccat_eth_frame *end;
       
   136 	struct ccat_eth_frame *next;
       
   137 	struct ccat_dma dma;
       
   138 };
       
   139 
       
   140 /**
       
   141  * struct ccat_device - CCAT device representation
       
   142  * @pdev: pointer to the pci object allocated by the kernel
       
   143  * @ethdev: CCAT Ethernet/EtherCAT Master (with DMA) function, NULL if function is not available or failed to initialize
       
   144  * @update: CCAT Update function, NULL if function is not available or failed to initialize
       
   145  * @bar [0] and [2] holding information about PCI BARs 0 and 2.
       
   146  *
       
   147  * One instance of a ccat_device should represent a physical CCAT. Since
       
   148  * a CCAT is implemented as FPGA the available functions can vary so
       
   149  * the function object pointers can be NULL.
       
   150  * Extra note: you will recognize that PCI BAR1 is not used and is a
       
   151  * waste of memory, thats true but right now, its very easy to use it
       
   152  * this way. So we might optimize it later.
       
   153  */
       
   154 struct ccat_device {
       
   155 	struct pci_dev *pdev;
       
   156 	struct ccat_eth_priv *ethdev;
       
   157 	struct ccat_update *update;
       
   158 	struct ccat_bar bar[3];	//TODO optimize this
       
   159 };
       
   160 
       
   161 struct ccat_info_block {
       
   162 	u16 type;
       
   163 	u16 rev;
       
   164 	union {
       
   165 		u32 config;
       
   166 		struct {
       
   167 			u8 tx_dma_chan;
       
   168 			u8 rx_dma_chan;
       
   169 		};
       
   170 	};
       
   171 	u32 addr;
       
   172 	u32 size;
       
   173 };
       
   174 
       
   175 /**
       
   176  * struct ccat_eth_priv - CCAT Ethernet/EtherCAT Master function (netdev)
       
   177  * @ccatdev: pointer to the parent struct ccat_device
       
   178  * @netdev: the net_device structure used by the kernel networking stack
       
   179  * @info: holds a copy of the CCAT Ethernet/EtherCAT Master function information block (read from PCI config space)
       
   180  * @reg: register addresses in PCI config space of the Ethernet/EtherCAT Master function
       
   181  * @rx_fifo: DMA fifo used for RX DMA descriptors
       
   182  * @tx_fifo: DMA fifo used for TX DMA descriptors
       
   183  * @poll_timer: interval timer used to poll CCAT for events like link changed, rx done, tx done
       
   184  * @rx_bytes: number of bytes received -> reported with ndo_get_stats64()
       
   185  * @rx_dropped: number of received frames, which were dropped -> reported with ndo_get_stats64()
       
   186  * @tx_bytes: number of bytes send -> reported with ndo_get_stats64()
       
   187  * @tx_dropped: number of frames requested to send, which were dropped -> reported with ndo_get_stats64()
       
   188  */
       
   189 struct ccat_eth_priv {
       
   190 	const struct ccat_device *ccatdev;
       
   191 	struct net_device *netdev;
       
   192 	struct ccat_info_block info;
       
   193 	struct ccat_eth_register reg;
       
   194 	struct ccat_eth_dma_fifo rx_fifo;
       
   195 	struct ccat_eth_dma_fifo tx_fifo;
       
   196 	struct hrtimer poll_timer;
       
   197 	atomic64_t rx_bytes;
       
   198 	atomic64_t rx_dropped;
       
   199 	atomic64_t tx_bytes;
       
   200 	atomic64_t tx_dropped;
       
   201 	ec_device_t *ecdev;
       
   202 	void (*carrier_off) (struct net_device * netdev);
       
   203 	bool (*carrier_ok) (const struct net_device * netdev);
       
   204 	void (*carrier_on) (struct net_device * netdev);
       
   205 	void (*kfree_skb_any) (struct sk_buff * skb);
       
   206 	void (*start_queue) (struct net_device * netdev);
       
   207 	void (*stop_queue) (struct net_device * netdev);
       
   208 	void (*unregister) (struct net_device * netdev);
       
   209 };
       
   210 
       
   211 /**
       
   212  * same as: typedef struct _CCatInfoBlockOffs from CCatDefinitions.h
       
   213  * TODO add some checking facility outside of the linux tree
       
   214  */
       
   215 struct ccat_mac_infoblock {
       
   216 	u32 reserved;
       
   217 	u32 mii;
       
   218 	u32 tx_fifo;
       
   219 	u32 mac;
       
   220 	u32 rx_mem;
       
   221 	u32 tx_mem;
       
   222 	u32 misc;
       
   223 };
       
   224 
       
   225 struct ccat_mac_register {
       
   226 	/** MAC error register     @+0x0 */
       
   227 	u8 frame_len_err;
       
   228 	u8 rx_err;
       
   229 	u8 crc_err;
       
   230 	u8 link_lost_err;
       
   231 	u32 reserved1;
       
   232 	/** Buffer overflow errors @+0x8 */
       
   233 	u8 rx_mem_full;
       
   234 	u8 reserved2[7];
       
   235 	/** MAC frame counter      @+0x10 */
       
   236 	u32 tx_frames;
       
   237 	u32 rx_frames;
       
   238 	u64 reserved3;
       
   239 	/** MAC fifo level         @+0x20 */
       
   240 	u8 tx_fifo_level:7;
       
   241 	u8 reserved4:1;
       
   242 	u8 reserved5[7];
       
   243 	/** TX memory full error   @+0x28 */
       
   244 	u8 tx_mem_full;
       
   245 	u8 reserved6[7];
       
   246 	u64 reserved8[9];
       
   247 	/** Connection             @+0x78 */
       
   248 	u8 mii_connected;
       
   249 };
       
   250 
       
   251 /**
       
   252  * struct ccat_update - CCAT Update function (update)
       
   253  * @ccatdev: pointer to the parent struct ccat_device
       
   254  * @ioaddr: PCI base address of the CCAT Update function
       
   255  * dev: device number for this update function
       
   256  * cdev: character device used for the CCAT Update function
       
   257  * class: pointer to a device class used when registering the CCAT Update device
       
   258  * @info: holds a copy of the CCAT Update function information block (read from PCI config space)
       
   259  */
       
   260 struct ccat_update {
       
   261 	struct kref refcount;
       
   262 	void __iomem *ioaddr;
       
   263 	dev_t dev;
       
   264 	struct cdev cdev;
       
   265 	struct class *class;
       
   266 	struct ccat_info_block info;
       
   267 };
       
   268 #endif /* #ifndef _CCAT_H_ */