17 with this program; if not, write to the Free Software Foundation, Inc., |
17 with this program; if not, write to the Free Software Foundation, Inc., |
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 */ |
19 */ |
20 |
20 |
21 #include <linux/etherdevice.h> |
21 #include <linux/etherdevice.h> |
22 #include <linux/init.h> |
|
23 #include <linux/kernel.h> |
22 #include <linux/kernel.h> |
24 #include <linux/kfifo.h> |
|
25 #include <linux/kthread.h> |
|
26 #include <linux/module.h> |
23 #include <linux/module.h> |
27 #include <linux/netdevice.h> |
24 #include <linux/netdevice.h> |
28 #include <linux/spinlock.h> |
25 |
29 |
|
30 #include "compat.h" |
|
31 #include "module.h" |
26 #include "module.h" |
32 #include "netdev.h" |
27 #include "netdev.h" |
33 #include "print.h" |
|
34 |
28 |
35 /** |
29 /** |
36 * EtherCAT frame to enable forwarding on EtherCAT Terminals |
30 * EtherCAT frame to enable forwarding on EtherCAT Terminals |
37 */ |
31 */ |
38 static const UINT8 frameForwardEthernetFrames[] = { |
32 static const u8 frameForwardEthernetFrames[] = { |
39 0x01, 0x01, 0x05, 0x01, 0x00, 0x00, |
33 0x01, 0x01, 0x05, 0x01, 0x00, 0x00, |
40 0x00, 0x1b, 0x21, 0x36, 0x1b, 0xce, |
34 0x00, 0x1b, 0x21, 0x36, 0x1b, 0xce, |
41 0x88, 0xa4, 0x0e, 0x10, |
35 0x88, 0xa4, 0x0e, 0x10, |
42 0x08, |
36 0x08, |
43 0x00, |
37 0x00, |
48 0x00, 0x00, |
42 0x00, 0x00, |
49 0x00, 0x00 |
43 0x00, 0x00 |
50 }; |
44 }; |
51 |
45 |
52 #define FIFO_LENGTH 64 |
46 #define FIFO_LENGTH 64 |
53 #define DMA_POLL_DELAY_RANGE_USECS 100, 100 /* time to sleep between rx/tx DMA polls */ |
47 #define POLL_TIME ktime_set(0, 100 * NSEC_PER_USEC) |
54 #define POLL_DELAY_RANGE_USECS 500, 1000 /* time to sleep between link state polls */ |
48 |
55 |
49 /** |
56 static void ec_poll(struct net_device *dev); |
50 * Helper to check if frame in tx dma memory was already marked as sent by CCAT |
57 static int run_poll_thread(void *data); |
51 */ |
58 static int run_rx_thread(void *data); |
52 static inline bool ccat_eth_frame_sent(const struct ccat_eth_frame *const frame) |
59 static int run_tx_thread(void *data); |
53 { |
60 |
54 return le32_to_cpu(frame->tx_flags) & CCAT_FRAME_SENT; |
61 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35) |
55 } |
62 static struct rtnl_link_stats64 *ccat_eth_get_stats64(struct net_device *dev, struct rtnl_link_stats64 |
56 |
63 *storage); |
57 /** |
64 #endif |
58 * Helper to check if frame in tx dma memory was already marked as sent by CCAT |
65 static int ccat_eth_open(struct net_device *dev); |
59 */ |
66 static netdev_tx_t ccat_eth_start_xmit(struct sk_buff *skb, |
60 static inline bool ccat_eth_frame_received(const struct ccat_eth_frame *const |
67 struct net_device *dev); |
61 frame) |
68 static int ccat_eth_stop(struct net_device *dev); |
62 { |
69 static void ccat_eth_xmit_raw(struct net_device *dev, const char *data, |
63 return le32_to_cpu(frame->rx_flags) & CCAT_FRAME_RECEIVED; |
70 size_t len); |
64 } |
71 |
|
72 static const struct net_device_ops ccat_eth_netdev_ops = { |
|
73 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35) |
|
74 .ndo_get_stats64 = ccat_eth_get_stats64, |
|
75 #endif |
|
76 .ndo_open = ccat_eth_open, |
|
77 .ndo_start_xmit = ccat_eth_start_xmit, |
|
78 .ndo_stop = ccat_eth_stop, |
|
79 }; |
|
80 |
65 |
81 static void ecdev_kfree_skb_any(struct sk_buff *skb) |
66 static void ecdev_kfree_skb_any(struct sk_buff *skb) |
82 { |
67 { |
83 /* never release a skb in EtherCAT mode */ |
68 /* never release a skb in EtherCAT mode */ |
|
69 } |
|
70 |
|
71 static bool ecdev_carrier_ok(const struct net_device *const netdev) |
|
72 { |
|
73 struct ccat_eth_priv *const priv = netdev_priv(netdev); |
|
74 return ecdev_get_link(priv->ecdev); |
84 } |
75 } |
85 |
76 |
86 static void ecdev_carrier_on(struct net_device *const netdev) |
77 static void ecdev_carrier_on(struct net_device *const netdev) |
87 { |
78 { |
88 struct ccat_eth_priv *const priv = netdev_priv(netdev); |
79 struct ccat_eth_priv *const priv = netdev_priv(netdev); |
96 } |
87 } |
97 |
88 |
98 static void ecdev_nop(struct net_device *const netdev) |
89 static void ecdev_nop(struct net_device *const netdev) |
99 { |
90 { |
100 /* dummy called if nothing has to be done in EtherCAT operation mode */ |
91 /* dummy called if nothing has to be done in EtherCAT operation mode */ |
101 } |
|
102 |
|
103 static void ecdev_tx_fifo_full(struct net_device *const dev, |
|
104 const struct ccat_eth_frame *const frame) |
|
105 { |
|
106 /* we are polled -> there is nothing we can do in EtherCAT mode */ |
|
107 } |
92 } |
108 |
93 |
109 static void unregister_ecdev(struct net_device *const netdev) |
94 static void unregister_ecdev(struct net_device *const netdev) |
110 { |
95 { |
111 struct ccat_eth_priv *const priv = netdev_priv(netdev); |
96 struct ccat_eth_priv *const priv = netdev_priv(netdev); |
112 ecdev_close(priv->ecdev); |
97 ecdev_close(priv->ecdev); |
113 ecdev_withdraw(priv->ecdev); |
98 ecdev_withdraw(priv->ecdev); |
114 } |
99 } |
115 |
100 |
116 typedef void (*fifo_add_function) (struct ccat_eth_frame *, |
101 static void ccat_eth_fifo_inc(struct ccat_eth_dma_fifo *fifo) |
117 struct ccat_eth_dma_fifo *); |
102 { |
118 |
103 if (++fifo->next >= fifo->end) |
119 static void ccat_eth_rx_fifo_add(struct ccat_eth_frame *frame, |
104 fifo->next = fifo->dma.virt; |
120 struct ccat_eth_dma_fifo *fifo) |
105 } |
|
106 |
|
107 typedef void (*fifo_add_function) (struct ccat_eth_dma_fifo *, |
|
108 struct ccat_eth_frame *); |
|
109 |
|
110 static void ccat_eth_rx_fifo_add(struct ccat_eth_dma_fifo *fifo, |
|
111 struct ccat_eth_frame *frame) |
121 { |
112 { |
122 const size_t offset = ((void *)(frame) - fifo->dma.virt); |
113 const size_t offset = ((void *)(frame) - fifo->dma.virt); |
123 const uint32_t addr_and_length = (1 << 31) | offset; |
114 const u32 addr_and_length = (1 << 31) | offset; |
124 frame->received = 0; |
115 |
|
116 frame->rx_flags = cpu_to_le32(0); |
125 iowrite32(addr_and_length, fifo->reg); |
117 iowrite32(addr_and_length, fifo->reg); |
126 } |
118 } |
127 |
119 |
128 static void ccat_eth_tx_fifo_add_free(struct ccat_eth_frame *frame, |
120 static void ccat_eth_tx_fifo_add_free(struct ccat_eth_dma_fifo *fifo, |
129 struct ccat_eth_dma_fifo *fifo) |
121 struct ccat_eth_frame *frame) |
130 { |
122 { |
131 /* mark frame as ready to use for tx */ |
123 /* mark frame as ready to use for tx */ |
132 frame->sent = 1; |
124 frame->tx_flags = cpu_to_le32(CCAT_FRAME_SENT); |
133 } |
|
134 |
|
135 static void ccat_eth_tx_fifo_full(struct net_device *const dev, |
|
136 const struct ccat_eth_frame *const frame) |
|
137 { |
|
138 struct ccat_eth_priv *const priv = netdev_priv(dev); |
|
139 netif_stop_queue(dev); |
|
140 priv->next_tx_frame = frame; |
|
141 wake_up_process(priv->tx_thread); |
|
142 } |
125 } |
143 |
126 |
144 static void ccat_eth_dma_fifo_reset(struct ccat_eth_dma_fifo *fifo) |
127 static void ccat_eth_dma_fifo_reset(struct ccat_eth_dma_fifo *fifo) |
145 { |
128 { |
146 struct ccat_eth_frame *frame = fifo->dma.virt; |
|
147 const struct ccat_eth_frame *const end = frame + FIFO_LENGTH; |
|
148 |
|
149 /* reset hw fifo */ |
129 /* reset hw fifo */ |
150 iowrite32(0, fifo->reg + 0x8); |
130 iowrite32(0, fifo->reg + 0x8); |
151 wmb(); |
131 wmb(); |
152 |
132 |
153 if (fifo->add) { |
133 if (fifo->add) { |
154 while (frame < end) { |
134 fifo->next = fifo->dma.virt; |
155 fifo->add(frame, fifo); |
135 do { |
156 ++frame; |
136 fifo->add(fifo, fifo->next); |
157 } |
137 ccat_eth_fifo_inc(fifo); |
|
138 } while (fifo->next != fifo->dma.virt); |
158 } |
139 } |
159 } |
140 } |
160 |
141 |
161 static int ccat_eth_dma_fifo_init(struct ccat_eth_dma_fifo *fifo, |
142 static int ccat_eth_dma_fifo_init(struct ccat_eth_dma_fifo *fifo, |
162 void __iomem * const fifo_reg, |
143 void __iomem * const fifo_reg, |
220 * Initializes the CCat... members of the ccat_eth_priv structure. |
201 * Initializes the CCat... members of the ccat_eth_priv structure. |
221 * Call this function only if info and ioaddr are already initialized! |
202 * Call this function only if info and ioaddr are already initialized! |
222 */ |
203 */ |
223 static void ccat_eth_priv_init_mappings(struct ccat_eth_priv *priv) |
204 static void ccat_eth_priv_init_mappings(struct ccat_eth_priv *priv) |
224 { |
205 { |
225 CCatInfoBlockOffs offsets; |
206 struct ccat_mac_infoblock offsets; |
226 void __iomem *const func_base = |
207 void __iomem *const func_base = |
227 priv->ccatdev->bar[0].ioaddr + priv->info.nAddr; |
208 priv->ccatdev->bar[0].ioaddr + priv->info.addr; |
|
209 |
228 memcpy_fromio(&offsets, func_base, sizeof(offsets)); |
210 memcpy_fromio(&offsets, func_base, sizeof(offsets)); |
229 priv->reg.mii = func_base + offsets.nMMIOffs; |
211 priv->reg.mii = func_base + offsets.mii; |
230 priv->reg.tx_fifo = func_base + offsets.nTxFifoOffs; |
212 priv->reg.tx_fifo = func_base + offsets.tx_fifo; |
231 priv->reg.rx_fifo = func_base + offsets.nTxFifoOffs + 0x10; |
213 priv->reg.rx_fifo = func_base + offsets.tx_fifo + 0x10; |
232 priv->reg.mac = func_base + offsets.nMacRegOffs; |
214 priv->reg.mac = func_base + offsets.mac; |
233 priv->reg.rx_mem = func_base + offsets.nRxMemOffs; |
215 priv->reg.rx_mem = func_base + offsets.rx_mem; |
234 priv->reg.tx_mem = func_base + offsets.nTxMemOffs; |
216 priv->reg.tx_mem = func_base + offsets.tx_mem; |
235 priv->reg.misc = func_base + offsets.nMiscOffs; |
217 priv->reg.misc = func_base + offsets.misc; |
|
218 } |
|
219 |
|
220 static netdev_tx_t ccat_eth_start_xmit(struct sk_buff *skb, |
|
221 struct net_device *dev) |
|
222 { |
|
223 struct ccat_eth_priv *const priv = netdev_priv(dev); |
|
224 struct ccat_eth_dma_fifo *const fifo = &priv->tx_fifo; |
|
225 u32 addr_and_length; |
|
226 |
|
227 if (skb_is_nonlinear(skb)) { |
|
228 pr_warn("Non linear skb not supported -> drop frame.\n"); |
|
229 atomic64_inc(&priv->tx_dropped); |
|
230 priv->kfree_skb_any(skb); |
|
231 return NETDEV_TX_OK; |
|
232 } |
|
233 |
|
234 if (skb->len > sizeof(fifo->next->data)) { |
|
235 pr_warn("skb.len %llu exceeds dma buffer %llu -> drop frame.\n", |
|
236 (u64) skb->len, (u64) sizeof(fifo->next->data)); |
|
237 atomic64_inc(&priv->tx_dropped); |
|
238 priv->kfree_skb_any(skb); |
|
239 return NETDEV_TX_OK; |
|
240 } |
|
241 |
|
242 if (!ccat_eth_frame_sent(fifo->next)) { |
|
243 netdev_err(dev, "BUG! Tx Ring full when queue awake!\n"); |
|
244 priv->stop_queue(priv->netdev); |
|
245 return NETDEV_TX_BUSY; |
|
246 } |
|
247 |
|
248 /* prepare frame in DMA memory */ |
|
249 fifo->next->tx_flags = cpu_to_le32(0); |
|
250 fifo->next->length = cpu_to_le16(skb->len); |
|
251 memcpy(fifo->next->data, skb->data, skb->len); |
|
252 |
|
253 /* Queue frame into CCAT TX-FIFO, CCAT ignores the first 8 bytes of the tx descriptor */ |
|
254 addr_and_length = offsetof(struct ccat_eth_frame, length); |
|
255 addr_and_length += ((void *)fifo->next - fifo->dma.virt); |
|
256 addr_and_length += ((skb->len + CCAT_ETH_FRAME_HEAD_LEN) / 8) << 24; |
|
257 iowrite32(addr_and_length, priv->reg.tx_fifo); |
|
258 |
|
259 /* update stats */ |
|
260 atomic64_add(skb->len, &priv->tx_bytes); |
|
261 |
|
262 priv->kfree_skb_any(skb); |
|
263 |
|
264 ccat_eth_fifo_inc(fifo); |
|
265 /* stop queue if tx ring is full */ |
|
266 if (!ccat_eth_frame_sent(fifo->next)) { |
|
267 priv->stop_queue(priv->netdev); |
|
268 } |
|
269 return NETDEV_TX_OK; |
|
270 } |
|
271 |
|
272 /** |
|
273 * Function to transmit a raw buffer to the network (f.e. frameForwardEthernetFrames) |
|
274 * @dev a valid net_device |
|
275 * @data pointer to your raw buffer |
|
276 * @len number of bytes in the raw buffer to transmit |
|
277 */ |
|
278 static void ccat_eth_xmit_raw(struct net_device *dev, const char *const data, |
|
279 size_t len) |
|
280 { |
|
281 struct sk_buff *skb = dev_alloc_skb(len); |
|
282 |
|
283 skb->dev = dev; |
|
284 skb_copy_to_linear_data(skb, data, len); |
|
285 skb_put(skb, len); |
|
286 ccat_eth_start_xmit(skb, dev); |
|
287 } |
|
288 |
|
289 static void ccat_eth_receive(struct net_device *const dev, |
|
290 const void *const data, const size_t len) |
|
291 { |
|
292 struct sk_buff *const skb = dev_alloc_skb(len + NET_IP_ALIGN); |
|
293 struct ccat_eth_priv *const priv = netdev_priv(dev); |
|
294 |
|
295 if (!skb) { |
|
296 pr_info("%s() out of memory :-(\n", __FUNCTION__); |
|
297 atomic64_inc(&priv->rx_dropped); |
|
298 return; |
|
299 } |
|
300 skb->dev = dev; |
|
301 skb_reserve(skb, NET_IP_ALIGN); |
|
302 skb_copy_to_linear_data(skb, data, len); |
|
303 skb_put(skb, len); |
|
304 skb->protocol = eth_type_trans(skb, dev); |
|
305 skb->ip_summed = CHECKSUM_UNNECESSARY; |
|
306 atomic64_add(len, &priv->rx_bytes); |
|
307 netif_rx(skb); |
|
308 } |
|
309 |
|
310 static void ccat_eth_link_down(struct net_device *const dev) |
|
311 { |
|
312 struct ccat_eth_priv *const priv = netdev_priv(dev); |
|
313 |
|
314 priv->stop_queue(dev); |
|
315 priv->carrier_off(dev); |
|
316 netdev_info(dev, "NIC Link is Down\n"); |
|
317 } |
|
318 |
|
319 static void ccat_eth_link_up(struct net_device *const dev) |
|
320 { |
|
321 struct ccat_eth_priv *const priv = netdev_priv(dev); |
|
322 |
|
323 netdev_info(dev, "NIC Link is Up\n"); |
|
324 /* TODO netdev_info(dev, "NIC Link is Up %u Mbps %s Duplex\n", |
|
325 speed == SPEED_100 ? 100 : 10, |
|
326 cmd.duplex == DUPLEX_FULL ? "Full" : "Half"); */ |
|
327 |
|
328 ccat_eth_dma_fifo_reset(&priv->rx_fifo); |
|
329 ccat_eth_dma_fifo_reset(&priv->tx_fifo); |
|
330 |
|
331 /* TODO reset CCAT MAC register */ |
|
332 |
|
333 ccat_eth_xmit_raw(dev, frameForwardEthernetFrames, |
|
334 sizeof(frameForwardEthernetFrames)); |
|
335 priv->carrier_on(dev); |
|
336 priv->start_queue(dev); |
236 } |
337 } |
237 |
338 |
238 /** |
339 /** |
239 * Read link state from CCAT hardware |
340 * Read link state from CCAT hardware |
240 * @return 1 if link is up, 0 if not |
341 * @return 1 if link is up, 0 if not |
243 *const priv) |
344 *const priv) |
244 { |
345 { |
245 return (1 << 24) == (ioread32(priv->reg.mii + 0x8 + 4) & (1 << 24)); |
346 return (1 << 24) == (ioread32(priv->reg.mii + 0x8 + 4) & (1 << 24)); |
246 } |
347 } |
247 |
348 |
248 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35) |
349 /** |
|
350 * Poll for link state changes |
|
351 */ |
|
352 static void poll_link(struct ccat_eth_priv *const priv) |
|
353 { |
|
354 const size_t link = ccat_eth_priv_read_link_state(priv); |
|
355 |
|
356 if (link != priv->carrier_ok(priv->netdev)) { |
|
357 if (link) |
|
358 ccat_eth_link_up(priv->netdev); |
|
359 else |
|
360 ccat_eth_link_down(priv->netdev); |
|
361 } |
|
362 } |
|
363 |
|
364 /** |
|
365 * Poll for available rx dma descriptors in ethernet operating mode |
|
366 */ |
|
367 static void poll_rx(struct ccat_eth_priv *const priv) |
|
368 { |
|
369 static const size_t overhead = CCAT_ETH_FRAME_HEAD_LEN - 4; |
|
370 struct ccat_eth_dma_fifo *const fifo = &priv->rx_fifo; |
|
371 |
|
372 /* TODO omit possible deadlock in situations with heavy traffic */ |
|
373 while (ccat_eth_frame_received(fifo->next)) { |
|
374 const size_t len = le16_to_cpu(fifo->next->length) - overhead; |
|
375 if (priv->ecdev) { |
|
376 ecdev_receive(priv->ecdev, fifo->next->data, len); |
|
377 } else { |
|
378 ccat_eth_receive(priv->netdev, fifo->next->data, len); |
|
379 } |
|
380 ccat_eth_rx_fifo_add(fifo, fifo->next); |
|
381 ccat_eth_fifo_inc(fifo); |
|
382 } |
|
383 } |
|
384 |
|
385 static void ec_poll_rx(struct net_device *dev) |
|
386 { |
|
387 struct ccat_eth_priv *const priv = netdev_priv(dev); |
|
388 poll_rx(priv); |
|
389 } |
|
390 |
|
391 /** |
|
392 * Poll for available tx dma descriptors in ethernet operating mode |
|
393 */ |
|
394 static void poll_tx(struct ccat_eth_priv *const priv) |
|
395 { |
|
396 if (ccat_eth_frame_sent(priv->tx_fifo.next)) { |
|
397 netif_wake_queue(priv->netdev); |
|
398 } |
|
399 } |
|
400 |
|
401 /** |
|
402 * Since CCAT doesn't support interrupts until now, we have to poll |
|
403 * some status bits to recognize things like link change etc. |
|
404 */ |
|
405 static enum hrtimer_restart poll_timer_callback(struct hrtimer *timer) |
|
406 { |
|
407 struct ccat_eth_priv *const priv = |
|
408 container_of(timer, struct ccat_eth_priv, poll_timer); |
|
409 |
|
410 poll_link(priv); |
|
411 if(!priv->ecdev) { |
|
412 poll_rx(priv); |
|
413 poll_tx(priv); |
|
414 } |
|
415 hrtimer_forward_now(timer, POLL_TIME); |
|
416 return HRTIMER_RESTART; |
|
417 } |
|
418 |
249 static struct rtnl_link_stats64 *ccat_eth_get_stats64(struct net_device *dev, struct rtnl_link_stats64 |
419 static struct rtnl_link_stats64 *ccat_eth_get_stats64(struct net_device *dev, struct rtnl_link_stats64 |
250 *storage) |
420 *storage) |
251 { |
421 { |
252 struct ccat_eth_priv *const priv = netdev_priv(dev); |
422 struct ccat_eth_priv *const priv = netdev_priv(dev); |
253 CCatMacRegs mac; |
423 struct ccat_mac_register mac; |
254 memcpy_fromio(&mac, priv->reg.mac, sizeof(mac)); |
424 memcpy_fromio(&mac, priv->reg.mac, sizeof(mac)); |
255 storage->rx_packets = mac.rxFrameCnt; /* total packets received */ |
425 storage->rx_packets = mac.rx_frames; /* total packets received */ |
256 storage->tx_packets = mac.txFrameCnt; /* total packets transmitted */ |
426 storage->tx_packets = mac.tx_frames; /* total packets transmitted */ |
257 storage->rx_bytes = atomic64_read(&priv->rx_bytes); /* total bytes received */ |
427 storage->rx_bytes = atomic64_read(&priv->rx_bytes); /* total bytes received */ |
258 storage->tx_bytes = atomic64_read(&priv->tx_bytes); /* total bytes transmitted */ |
428 storage->tx_bytes = atomic64_read(&priv->tx_bytes); /* total bytes transmitted */ |
259 storage->rx_errors = mac.frameLenErrCnt + mac.dropFrameErrCnt + mac.crcErrCnt + mac.rxErrCnt; /* bad packets received */ |
429 storage->rx_errors = mac.frame_len_err + mac.rx_mem_full + mac.crc_err + mac.rx_err; /* bad packets received */ |
260 //TODO __u64 tx_errors; /* packet transmit problems */ |
430 storage->tx_errors = mac.tx_mem_full; /* packet transmit problems */ |
261 storage->rx_dropped = atomic64_read(&priv->rx_dropped); /* no space in linux buffers */ |
431 storage->rx_dropped = atomic64_read(&priv->rx_dropped); /* no space in linux buffers */ |
262 storage->tx_dropped = atomic64_read(&priv->tx_dropped); /* no space available in linux */ |
432 storage->tx_dropped = atomic64_read(&priv->tx_dropped); /* no space available in linux */ |
263 //TODO __u64 multicast; /* multicast packets received */ |
433 //TODO __u64 multicast; /* multicast packets received */ |
264 //TODO __u64 collisions; |
434 //TODO __u64 collisions; |
265 |
435 |
266 /* detailed rx_errors: */ |
436 /* detailed rx_errors: */ |
267 storage->rx_length_errors = mac.frameLenErrCnt; |
437 storage->rx_length_errors = mac.frame_len_err; |
268 storage->rx_over_errors = mac.dropFrameErrCnt; /* receiver ring buff overflow */ |
438 storage->rx_over_errors = mac.rx_mem_full; /* receiver ring buff overflow */ |
269 storage->rx_crc_errors = mac.crcErrCnt; /* recved pkt with crc error */ |
439 storage->rx_crc_errors = mac.crc_err; /* recved pkt with crc error */ |
270 storage->rx_frame_errors = mac.rxErrCnt; /* recv'd frame alignment error */ |
440 storage->rx_frame_errors = mac.rx_err; /* recv'd frame alignment error */ |
271 storage->rx_fifo_errors = mac.dropFrameErrCnt; /* recv'r fifo overrun */ |
441 storage->rx_fifo_errors = mac.rx_mem_full; /* recv'r fifo overrun */ |
272 //TODO __u64 rx_missed_errors; /* receiver missed packet */ |
442 //TODO __u64 rx_missed_errors; /* receiver missed packet */ |
273 |
443 |
274 /* detailed tx_errors */ |
444 /* detailed tx_errors */ |
275 //TODO __u64 tx_aborted_errors; |
445 //TODO __u64 tx_aborted_errors; |
276 //TODO __u64 tx_carrier_errors; |
446 //TODO __u64 tx_carrier_errors; |
281 /* for cslip etc */ |
451 /* for cslip etc */ |
282 //TODO __u64 rx_compressed; |
452 //TODO __u64 rx_compressed; |
283 //TODO __u64 tx_compressed; |
453 //TODO __u64 tx_compressed; |
284 return storage; |
454 return storage; |
285 } |
455 } |
286 #endif |
456 |
|
457 static int ccat_eth_open(struct net_device *dev) |
|
458 { |
|
459 struct ccat_eth_priv *const priv = netdev_priv(dev); |
|
460 |
|
461 hrtimer_init(&priv->poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
|
462 priv->poll_timer.function = poll_timer_callback; |
|
463 hrtimer_start(&priv->poll_timer, POLL_TIME, HRTIMER_MODE_REL); |
|
464 return 0; |
|
465 } |
|
466 |
|
467 static int ccat_eth_stop(struct net_device *dev) |
|
468 { |
|
469 struct ccat_eth_priv *const priv = netdev_priv(dev); |
|
470 |
|
471 priv->stop_queue(dev); |
|
472 hrtimer_cancel(&priv->poll_timer); |
|
473 return 0; |
|
474 } |
|
475 |
|
476 static const struct net_device_ops ccat_eth_netdev_ops = { |
|
477 .ndo_get_stats64 = ccat_eth_get_stats64, |
|
478 .ndo_open = ccat_eth_open, |
|
479 .ndo_start_xmit = ccat_eth_start_xmit, |
|
480 .ndo_stop = ccat_eth_stop, |
|
481 }; |
287 |
482 |
288 struct ccat_eth_priv *ccat_eth_init(const struct ccat_device *const ccatdev, |
483 struct ccat_eth_priv *ccat_eth_init(const struct ccat_device *const ccatdev, |
289 const void __iomem * const addr) |
484 const void __iomem * const addr) |
290 { |
485 { |
291 struct ccat_eth_priv *priv; |
486 struct ccat_eth_priv *priv; |
292 struct net_device *const netdev = alloc_etherdev(sizeof(*priv)); |
487 struct net_device *const netdev = alloc_etherdev(sizeof(*priv)); |
|
488 |
293 priv = netdev_priv(netdev); |
489 priv = netdev_priv(netdev); |
294 priv->netdev = netdev; |
490 priv->netdev = netdev; |
295 priv->ccatdev = ccatdev; |
491 priv->ccatdev = ccatdev; |
296 |
492 |
297 /* ccat register mappings */ |
493 /* ccat register mappings */ |
298 memcpy_fromio(&priv->info, addr, sizeof(priv->info)); |
494 memcpy_fromio(&priv->info, addr, sizeof(priv->info)); |
299 ccat_eth_priv_init_mappings(priv); |
495 ccat_eth_priv_init_mappings(priv); |
300 ccat_print_function_info(priv); |
|
301 |
496 |
302 if (ccat_eth_priv_init_dma(priv)) { |
497 if (ccat_eth_priv_init_dma(priv)) { |
303 pr_warn("%s(): DMA initialization failed.\n", __FUNCTION__); |
498 pr_warn("%s(): DMA initialization failed.\n", __FUNCTION__); |
304 free_netdev(netdev); |
499 free_netdev(netdev); |
305 return NULL; |
500 return NULL; |
306 } |
501 } |
307 |
502 |
308 /* init netdev with MAC and stack callbacks */ |
503 /* init netdev with MAC and stack callbacks */ |
309 memcpy_fromio(netdev->dev_addr, priv->reg.mii + 8, 6); |
504 memcpy_fromio(netdev->dev_addr, priv->reg.mii + 8, netdev->addr_len); |
310 netdev->netdev_ops = &ccat_eth_netdev_ops; |
505 netdev->netdev_ops = &ccat_eth_netdev_ops; |
311 |
506 |
312 /* use as EtherCAT device? */ |
507 /* use as EtherCAT device? */ |
313 priv->ecdev = ecdev_offer(netdev, ec_poll, THIS_MODULE); |
508 priv->ecdev = ecdev_offer(netdev, ec_poll_rx, THIS_MODULE); |
314 if (priv->ecdev) { |
509 if (priv->ecdev) { |
315 priv->carrier_off = ecdev_carrier_off; |
510 priv->carrier_off = ecdev_carrier_off; |
|
511 priv->carrier_ok = ecdev_carrier_ok; |
316 priv->carrier_on = ecdev_carrier_on; |
512 priv->carrier_on = ecdev_carrier_on; |
317 priv->kfree_skb_any = ecdev_kfree_skb_any; |
513 priv->kfree_skb_any = ecdev_kfree_skb_any; |
318 priv->start_queue = ecdev_nop; |
514 priv->start_queue = ecdev_nop; |
319 priv->stop_queue = ecdev_nop; |
515 priv->stop_queue = ecdev_nop; |
320 priv->tx_fifo_full = ecdev_tx_fifo_full; |
|
321 priv->unregister = unregister_ecdev; |
516 priv->unregister = unregister_ecdev; |
|
517 |
|
518 priv->carrier_off(netdev); |
322 if (ecdev_open(priv->ecdev)) { |
519 if (ecdev_open(priv->ecdev)) { |
323 pr_info("unable to register network device.\n"); |
520 pr_info("unable to register network device.\n"); |
324 ecdev_withdraw(priv->ecdev); |
521 ecdev_withdraw(priv->ecdev); |
325 ccat_eth_priv_free_dma(priv); |
522 ccat_eth_priv_free_dma(priv); |
326 free_netdev(netdev); |
523 free_netdev(netdev); |
329 return priv; |
526 return priv; |
330 } |
527 } |
331 |
528 |
332 /* EtherCAT disabled -> prepare normal ethernet mode */ |
529 /* EtherCAT disabled -> prepare normal ethernet mode */ |
333 priv->carrier_off = netif_carrier_off; |
530 priv->carrier_off = netif_carrier_off; |
|
531 priv->carrier_ok = netif_carrier_ok; |
334 priv->carrier_on = netif_carrier_on; |
532 priv->carrier_on = netif_carrier_on; |
335 priv->kfree_skb_any = dev_kfree_skb_any; |
533 priv->kfree_skb_any = dev_kfree_skb_any; |
336 priv->start_queue = netif_start_queue; |
534 priv->start_queue = netif_start_queue; |
337 priv->stop_queue = netif_stop_queue; |
535 priv->stop_queue = netif_stop_queue; |
338 priv->tx_fifo_full = ccat_eth_tx_fifo_full; |
|
339 priv->unregister = unregister_netdev; |
536 priv->unregister = unregister_netdev; |
|
537 |
|
538 priv->carrier_off(netdev); |
340 if (register_netdev(netdev)) { |
539 if (register_netdev(netdev)) { |
341 pr_info("unable to register network device.\n"); |
540 pr_info("unable to register network device.\n"); |
342 ccat_eth_priv_free_dma(priv); |
541 ccat_eth_priv_free_dma(priv); |
343 free_netdev(netdev); |
542 free_netdev(netdev); |
344 return NULL; |
543 return NULL; |
345 } |
544 } |
346 pr_info("registered %s as network device.\n", netdev->name); |
545 pr_info("registered %s as network device.\n", netdev->name); |
347 priv->rx_thread = kthread_run(run_rx_thread, netdev, "%s_rx", DRV_NAME); |
|
348 priv->tx_thread = kthread_run(run_tx_thread, netdev, "%s_tx", DRV_NAME); |
|
349 return priv; |
546 return priv; |
350 } |
547 } |
351 |
548 |
352 void ccat_eth_remove(struct ccat_eth_priv *const priv) |
549 void ccat_eth_remove(struct ccat_eth_priv *const priv) |
353 { |
550 { |
354 if (priv->rx_thread) { |
|
355 kthread_stop(priv->rx_thread); |
|
356 } |
|
357 if (priv->tx_thread) { |
|
358 kthread_stop(priv->tx_thread); |
|
359 } |
|
360 priv->unregister(priv->netdev); |
551 priv->unregister(priv->netdev); |
361 ccat_eth_priv_free_dma(priv); |
552 ccat_eth_priv_free_dma(priv); |
362 free_netdev(priv->netdev); |
553 free_netdev(priv->netdev); |
363 pr_debug("%s(): done\n", __FUNCTION__); |
554 } |
364 } |
|
365 |
|
366 static int ccat_eth_open(struct net_device *dev) |
|
367 { |
|
368 struct ccat_eth_priv *const priv = netdev_priv(dev); |
|
369 priv->carrier_off(dev); |
|
370 priv->poll_thread = |
|
371 kthread_run(run_poll_thread, dev, "%s_poll", DRV_NAME); |
|
372 |
|
373 //TODO |
|
374 return 0; |
|
375 } |
|
376 |
|
377 static const size_t CCATRXDESC_HEADER_LEN = 20; |
|
378 static void ccat_eth_receive(struct net_device *const dev, |
|
379 const struct ccat_eth_frame *const frame) |
|
380 { |
|
381 struct ccat_eth_priv *const priv = netdev_priv(dev); |
|
382 const size_t len = frame->length - CCATRXDESC_HEADER_LEN; |
|
383 struct sk_buff *skb = dev_alloc_skb(len + NET_IP_ALIGN); |
|
384 if (!skb) { |
|
385 pr_info("%s() out of memory :-(\n", __FUNCTION__); |
|
386 atomic64_inc(&priv->rx_dropped); |
|
387 return; |
|
388 } |
|
389 skb->dev = dev; |
|
390 skb_reserve(skb, NET_IP_ALIGN); |
|
391 skb_copy_to_linear_data(skb, frame->data, len); |
|
392 skb_put(skb, len); |
|
393 skb->protocol = eth_type_trans(skb, dev); |
|
394 skb->ip_summed = CHECKSUM_UNNECESSARY; |
|
395 atomic64_add(len, &priv->rx_bytes); |
|
396 netif_rx(skb); |
|
397 } |
|
398 |
|
399 /** |
|
400 * Rx handler in EtherCAT operation mode |
|
401 * priv->ecdev should always be valid! |
|
402 */ |
|
403 static void ec_poll(struct net_device *dev) |
|
404 { |
|
405 static size_t next = 0; |
|
406 struct ccat_eth_priv *const priv = netdev_priv(dev); |
|
407 struct ccat_eth_frame *frame = |
|
408 ((struct ccat_eth_frame *)priv->rx_fifo.dma.virt) + next; |
|
409 if (frame->received) { |
|
410 ecdev_receive(priv->ecdev, frame->data, |
|
411 frame->length - CCATRXDESC_HEADER_LEN); |
|
412 frame->received = 0; |
|
413 ccat_eth_rx_fifo_add(frame, &priv->rx_fifo); |
|
414 next = (next + 1) % FIFO_LENGTH; |
|
415 } else { |
|
416 //TODO dev_warn(&dev->dev, "%s(): frame was not ready\n", __FUNCTION__); |
|
417 } |
|
418 } |
|
419 |
|
420 static netdev_tx_t ccat_eth_start_xmit(struct sk_buff *skb, |
|
421 struct net_device *dev) |
|
422 { |
|
423 static size_t next = 0; |
|
424 struct ccat_eth_priv *const priv = netdev_priv(dev); |
|
425 struct ccat_eth_frame *const frame = |
|
426 ((struct ccat_eth_frame *)priv->tx_fifo.dma.virt); |
|
427 uint32_t addr_and_length; |
|
428 |
|
429 if (skb_is_nonlinear(skb)) { |
|
430 pr_warn("Non linear skb not supported -> drop frame.\n"); |
|
431 atomic64_inc(&priv->tx_dropped); |
|
432 priv->kfree_skb_any(skb); |
|
433 return NETDEV_TX_OK; |
|
434 } |
|
435 |
|
436 if (skb->len > sizeof(frame->data)) { |
|
437 pr_warn("skb.len %llu exceeds dma buffer %llu -> drop frame.\n", |
|
438 (uint64_t) skb->len, (uint64_t) sizeof(frame->data)); |
|
439 atomic64_inc(&priv->tx_dropped); |
|
440 priv->kfree_skb_any(skb); |
|
441 return NETDEV_TX_OK; |
|
442 } |
|
443 |
|
444 if (!frame[next].sent) { |
|
445 netdev_err(dev, "BUG! Tx Ring full when queue awake!\n"); |
|
446 ccat_eth_tx_fifo_full(dev, &frame[next]); |
|
447 return NETDEV_TX_BUSY; |
|
448 } |
|
449 |
|
450 /* prepare frame in DMA memory */ |
|
451 frame[next].sent = 0; |
|
452 frame[next].length = skb->len; |
|
453 memcpy(frame[next].data, skb->data, skb->len); |
|
454 |
|
455 priv->kfree_skb_any(skb); |
|
456 |
|
457 addr_and_length = 8 + (next * sizeof(*frame)); |
|
458 addr_and_length += |
|
459 ((frame[next].length + CCAT_DMA_FRAME_HEADER_LENGTH) / 8) << 24; |
|
460 iowrite32(addr_and_length, priv->reg.tx_fifo); /* add to DMA fifo */ |
|
461 atomic64_add(frame[next].length, &priv->tx_bytes); /* update stats */ |
|
462 |
|
463 next = (next + 1) % FIFO_LENGTH; |
|
464 /* stop queue if tx ring is full */ |
|
465 if (!frame[next].sent) { |
|
466 ccat_eth_tx_fifo_full(dev, &frame[next]); |
|
467 } |
|
468 return NETDEV_TX_OK; |
|
469 } |
|
470 |
|
471 static int ccat_eth_stop(struct net_device *dev) |
|
472 { |
|
473 struct ccat_eth_priv *const priv = netdev_priv(dev); |
|
474 priv->stop_queue(dev); |
|
475 if (priv->poll_thread) { |
|
476 /* TODO care about smp context? */ |
|
477 kthread_stop(priv->poll_thread); |
|
478 priv->poll_thread = NULL; |
|
479 } |
|
480 netdev_info(dev, "stopped.\n"); |
|
481 return 0; |
|
482 } |
|
483 |
|
484 static void ccat_eth_link_down(struct net_device *dev) |
|
485 { |
|
486 struct ccat_eth_priv *const priv = netdev_priv(dev); |
|
487 priv->stop_queue(dev); |
|
488 priv->carrier_off(dev); |
|
489 netdev_info(dev, "NIC Link is Down\n"); |
|
490 } |
|
491 |
|
492 static void ccat_eth_link_up(struct net_device *const dev) |
|
493 { |
|
494 struct ccat_eth_priv *const priv = netdev_priv(dev); |
|
495 netdev_info(dev, "NIC Link is Up\n"); |
|
496 /* TODO netdev_info(dev, "NIC Link is Up %u Mbps %s Duplex\n", |
|
497 speed == SPEED_100 ? 100 : 10, |
|
498 cmd.duplex == DUPLEX_FULL ? "Full" : "Half"); */ |
|
499 |
|
500 ccat_eth_dma_fifo_reset(&priv->rx_fifo); |
|
501 ccat_eth_dma_fifo_reset(&priv->tx_fifo); |
|
502 ccat_eth_xmit_raw(dev, frameForwardEthernetFrames, |
|
503 sizeof(frameForwardEthernetFrames)); |
|
504 priv->carrier_on(dev); |
|
505 priv->start_queue(dev); |
|
506 } |
|
507 |
|
508 /** |
|
509 * Function to transmit a raw buffer to the network (f.e. frameForwardEthernetFrames) |
|
510 * @dev a valid net_device |
|
511 * @data pointer to your raw buffer |
|
512 * @len number of bytes in the raw buffer to transmit |
|
513 */ |
|
514 static void ccat_eth_xmit_raw(struct net_device *dev, const char *const data, |
|
515 size_t len) |
|
516 { |
|
517 struct sk_buff *skb = dev_alloc_skb(len); |
|
518 skb->dev = dev; |
|
519 skb_copy_to_linear_data(skb, data, len); |
|
520 skb_put(skb, len); |
|
521 ccat_eth_start_xmit(skb, dev); |
|
522 } |
|
523 |
|
524 /** |
|
525 * Since CCAT doesn't support interrupts until now, we have to poll |
|
526 * some status bits to recognize things like link change etc. |
|
527 */ |
|
528 static int run_poll_thread(void *data) |
|
529 { |
|
530 struct net_device *const dev = (struct net_device *)data; |
|
531 struct ccat_eth_priv *const priv = netdev_priv(dev); |
|
532 size_t link = 0; |
|
533 |
|
534 while (!kthread_should_stop()) { |
|
535 if (ccat_eth_priv_read_link_state(priv) != link) { |
|
536 link = !link; |
|
537 link ? ccat_eth_link_up(dev) : ccat_eth_link_down(dev); |
|
538 } |
|
539 usleep_range(POLL_DELAY_RANGE_USECS); |
|
540 } |
|
541 pr_debug("%s() stopped.\n", __FUNCTION__); |
|
542 return 0; |
|
543 } |
|
544 |
|
545 static int run_rx_thread(void *data) |
|
546 { |
|
547 struct net_device *const dev = (struct net_device *)data; |
|
548 struct ccat_eth_priv *const priv = netdev_priv(dev); |
|
549 struct ccat_eth_frame *frame = priv->rx_fifo.dma.virt; |
|
550 const struct ccat_eth_frame *const end = frame + FIFO_LENGTH; |
|
551 |
|
552 while (!kthread_should_stop()) { |
|
553 /* wait until frame was used by DMA for Rx */ |
|
554 while (!kthread_should_stop() && !frame->received) { |
|
555 usleep_range(DMA_POLL_DELAY_RANGE_USECS); |
|
556 } |
|
557 |
|
558 /* can be NULL, if we are asked to stop! */ |
|
559 if (frame->received) { |
|
560 ccat_eth_receive(dev, frame); |
|
561 frame->received = 0; |
|
562 ccat_eth_rx_fifo_add(frame, &priv->rx_fifo); |
|
563 } |
|
564 if (++frame >= end) { |
|
565 frame = priv->rx_fifo.dma.virt; |
|
566 } |
|
567 } |
|
568 pr_debug("%s() stopped.\n", __FUNCTION__); |
|
569 return 0; |
|
570 } |
|
571 |
|
572 /** |
|
573 * Polling of tx dma descriptors in ethernet operating mode |
|
574 */ |
|
575 static int run_tx_thread(void *data) |
|
576 { |
|
577 struct net_device *const dev = (struct net_device *)data; |
|
578 struct ccat_eth_priv *const priv = netdev_priv(dev); |
|
579 |
|
580 set_current_state(TASK_INTERRUPTIBLE); |
|
581 while (!kthread_should_stop()) { |
|
582 const struct ccat_eth_frame *const frame = priv->next_tx_frame; |
|
583 if (frame) { |
|
584 while (!kthread_should_stop() && !frame->sent) { |
|
585 usleep_range(DMA_POLL_DELAY_RANGE_USECS); |
|
586 } |
|
587 } |
|
588 netif_wake_queue(dev); |
|
589 schedule(); |
|
590 set_current_state(TASK_INTERRUPTIBLE); |
|
591 } |
|
592 set_current_state(TASK_RUNNING); |
|
593 pr_debug("%s() stopped.\n", __FUNCTION__); |
|
594 return 0; |
|
595 } |
|