1 // SPDX-License-Identifier: GPL-2.0
3 * net.c - Networking component for Mostcore
5 * Copyright (C) 2015, Microchip Technology Germany II GmbH & Co. KG
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/netdevice.h>
12 #include <linux/etherdevice.h>
13 #include <linux/slab.h>
14 #include <linux/init.h>
15 #include <linux/list.h>
16 #include <linux/wait.h>
17 #include <linux/kobject.h>
18 #include "most/core.h"
21 #define MDP_HDR_LEN 16
22 #define MAMAC_DATA_LEN (1024 - MDP_HDR_LEN)
26 #define PMS_TELID_UNSEGM_MAMAC 0x0A
27 #define PMS_FIFONO_MDP 0x01
28 #define PMS_FIFONO_MEP 0x04
29 #define PMS_MSGTYPE_DATA 0x04
30 #define PMS_DEF_PRIO 0
31 #define MEP_DEF_RETRY 15
33 #define PMS_FIFONO_MASK 0x07
34 #define PMS_FIFONO_SHIFT 3
35 #define PMS_RETRY_SHIFT 4
36 #define PMS_TELID_MASK 0x0F
37 #define PMS_TELID_SHIFT 4
39 #define HB(value) ((u8)((u16)(value) >> 8))
40 #define LB(value) ((u8)(value))
42 #define EXTRACT_BIT_SET(bitset_name, value) \
43 (((value) >> bitset_name##_SHIFT) & bitset_name##_MASK)
45 #define PMS_IS_MEP(buf, len) \
46 ((len) > MEP_HDR_LEN && \
47 EXTRACT_BIT_SET(PMS_FIFONO, (buf)[3]) == PMS_FIFONO_MEP)
49 static inline bool pms_is_mamac(char *buf, u32 len)
51 return (len > MDP_HDR_LEN &&
52 EXTRACT_BIT_SET(PMS_FIFONO, buf[3]) == PMS_FIFONO_MDP &&
53 EXTRACT_BIT_SET(PMS_TELID, buf[14]) == PMS_TELID_UNSEGM_MAMAC);
56 struct net_dev_channel {
61 struct net_dev_context {
62 struct most_interface *iface;
64 struct net_device *dev;
65 struct net_dev_channel rx;
66 struct net_dev_channel tx;
67 struct list_head list;
70 static struct list_head net_devices = LIST_HEAD_INIT(net_devices);
71 static struct mutex probe_disc_mt; /* ch->linked = true, most_nd_open */
72 static struct spinlock list_lock; /* list_head, ch->linked = false, dev_hold */
73 static struct core_component comp;
75 static int skb_to_mamac(const struct sk_buff *skb, struct mbo *mbo)
77 u8 *buff = mbo->virt_address;
78 const u8 broadcast[] = { 0x03, 0xFF };
79 const u8 *dest_addr = skb->data + 4;
80 const u8 *eth_type = skb->data + 12;
81 unsigned int payload_len = skb->len - ETH_HLEN;
82 unsigned int mdp_len = payload_len + MDP_HDR_LEN;
84 if (mbo->buffer_length < mdp_len) {
85 pr_err("drop: too small buffer! (%d for %d)\n",
86 mbo->buffer_length, mdp_len);
90 if (skb->len < ETH_HLEN) {
91 pr_err("drop: too small packet! (%d)\n", skb->len);
95 if (dest_addr[0] == 0xFF && dest_addr[1] == 0xFF)
96 dest_addr = broadcast;
98 *buff++ = HB(mdp_len - 2);
99 *buff++ = LB(mdp_len - 2);
102 *buff++ = (PMS_FIFONO_MDP << PMS_FIFONO_SHIFT) | PMS_MSGTYPE_DATA;
103 *buff++ = PMS_DEF_PRIO;
104 *buff++ = dest_addr[0];
105 *buff++ = dest_addr[1];
108 *buff++ = HB(payload_len + 6);
109 *buff++ = LB(payload_len + 6);
111 /* end of FPH here */
113 *buff++ = eth_type[0];
114 *buff++ = eth_type[1];
118 *buff++ = PMS_TELID_UNSEGM_MAMAC << 4 | HB(payload_len);
119 *buff++ = LB(payload_len);
121 memcpy(buff, skb->data + ETH_HLEN, payload_len);
122 mbo->buffer_length = mdp_len;
126 static int skb_to_mep(const struct sk_buff *skb, struct mbo *mbo)
128 u8 *buff = mbo->virt_address;
129 unsigned int mep_len = skb->len + MEP_HDR_LEN;
131 if (mbo->buffer_length < mep_len) {
132 pr_err("drop: too small buffer! (%d for %d)\n",
133 mbo->buffer_length, mep_len);
137 *buff++ = HB(mep_len - 2);
138 *buff++ = LB(mep_len - 2);
141 *buff++ = (PMS_FIFONO_MEP << PMS_FIFONO_SHIFT) | PMS_MSGTYPE_DATA;
142 *buff++ = (MEP_DEF_RETRY << PMS_RETRY_SHIFT) | PMS_DEF_PRIO;
147 memcpy(buff, skb->data, skb->len);
148 mbo->buffer_length = mep_len;
152 static int most_nd_set_mac_address(struct net_device *dev, void *p)
154 struct net_dev_context *nd = netdev_priv(dev);
155 int err = eth_mac_addr(dev, p);
161 (dev->dev_addr[0] == 0 && dev->dev_addr[1] == 0 &&
162 dev->dev_addr[2] == 0 && dev->dev_addr[3] == 0);
165 * Set default MTU for the given packet type.
166 * It is still possible to change MTU using ip tools afterwards.
168 dev->mtu = nd->is_mamac ? MAMAC_DATA_LEN : ETH_DATA_LEN;
173 static void on_netinfo(struct most_interface *iface,
174 unsigned char link_stat, unsigned char *mac_addr);
176 static int most_nd_open(struct net_device *dev)
178 struct net_dev_context *nd = netdev_priv(dev);
181 mutex_lock(&probe_disc_mt);
183 if (most_start_channel(nd->iface, nd->rx.ch_id, &comp)) {
184 netdev_err(dev, "most_start_channel() failed\n");
189 if (most_start_channel(nd->iface, nd->tx.ch_id, &comp)) {
190 netdev_err(dev, "most_start_channel() failed\n");
191 most_stop_channel(nd->iface, nd->rx.ch_id, &comp);
196 netif_carrier_off(dev);
197 if (is_valid_ether_addr(dev->dev_addr))
198 netif_dormant_off(dev);
200 netif_dormant_on(dev);
201 netif_wake_queue(dev);
202 if (nd->iface->request_netinfo)
203 nd->iface->request_netinfo(nd->iface, nd->tx.ch_id, on_netinfo);
206 mutex_unlock(&probe_disc_mt);
210 static int most_nd_stop(struct net_device *dev)
212 struct net_dev_context *nd = netdev_priv(dev);
214 netif_stop_queue(dev);
215 if (nd->iface->request_netinfo)
216 nd->iface->request_netinfo(nd->iface, nd->tx.ch_id, NULL);
217 most_stop_channel(nd->iface, nd->rx.ch_id, &comp);
218 most_stop_channel(nd->iface, nd->tx.ch_id, &comp);
223 static netdev_tx_t most_nd_start_xmit(struct sk_buff *skb,
224 struct net_device *dev)
226 struct net_dev_context *nd = netdev_priv(dev);
230 mbo = most_get_mbo(nd->iface, nd->tx.ch_id, &comp);
233 netif_stop_queue(dev);
234 dev->stats.tx_fifo_errors++;
235 return NETDEV_TX_BUSY;
239 ret = skb_to_mamac(skb, mbo);
241 ret = skb_to_mep(skb, mbo);
245 dev->stats.tx_dropped++;
250 most_submit_mbo(mbo);
251 dev->stats.tx_packets++;
252 dev->stats.tx_bytes += skb->len;
257 static const struct net_device_ops most_nd_ops = {
258 .ndo_open = most_nd_open,
259 .ndo_stop = most_nd_stop,
260 .ndo_start_xmit = most_nd_start_xmit,
261 .ndo_set_mac_address = most_nd_set_mac_address,
264 static void most_nd_setup(struct net_device *dev)
267 dev->netdev_ops = &most_nd_ops;
270 static struct net_dev_context *get_net_dev(struct most_interface *iface)
272 struct net_dev_context *nd;
274 list_for_each_entry(nd, &net_devices, list)
275 if (nd->iface == iface)
280 static struct net_dev_context *get_net_dev_hold(struct most_interface *iface)
282 struct net_dev_context *nd;
285 spin_lock_irqsave(&list_lock, flags);
286 nd = get_net_dev(iface);
287 if (nd && nd->rx.linked && nd->tx.linked)
291 spin_unlock_irqrestore(&list_lock, flags);
295 static int comp_probe_channel(struct most_interface *iface, int channel_idx,
296 struct most_channel_config *ccfg, char *name)
298 struct net_dev_context *nd;
299 struct net_dev_channel *ch;
300 struct net_device *dev;
307 if (ccfg->data_type != MOST_CH_ASYNC)
310 mutex_lock(&probe_disc_mt);
311 nd = get_net_dev(iface);
313 dev = alloc_netdev(sizeof(struct net_dev_context), "meth%d",
314 NET_NAME_UNKNOWN, most_nd_setup);
320 nd = netdev_priv(dev);
324 spin_lock_irqsave(&list_lock, flags);
325 list_add(&nd->list, &net_devices);
326 spin_unlock_irqrestore(&list_lock, flags);
328 ch = ccfg->direction == MOST_CH_TX ? &nd->tx : &nd->rx;
330 ch = ccfg->direction == MOST_CH_TX ? &nd->tx : &nd->rx;
332 pr_err("direction is allocated\n");
337 if (register_netdev(nd->dev)) {
338 pr_err("register_netdev() failed\n");
343 ch->ch_id = channel_idx;
347 mutex_unlock(&probe_disc_mt);
351 static int comp_disconnect_channel(struct most_interface *iface,
354 struct net_dev_context *nd;
355 struct net_dev_channel *ch;
359 mutex_lock(&probe_disc_mt);
360 nd = get_net_dev(iface);
366 if (nd->rx.linked && channel_idx == nd->rx.ch_id) {
368 } else if (nd->tx.linked && channel_idx == nd->tx.ch_id) {
375 if (nd->rx.linked && nd->tx.linked) {
376 spin_lock_irqsave(&list_lock, flags);
378 spin_unlock_irqrestore(&list_lock, flags);
381 * do not call most_stop_channel() here, because channels are
382 * going to be closed in ndo_stop() after unregister_netdev()
384 unregister_netdev(nd->dev);
386 spin_lock_irqsave(&list_lock, flags);
388 spin_unlock_irqrestore(&list_lock, flags);
390 free_netdev(nd->dev);
394 mutex_unlock(&probe_disc_mt);
398 static int comp_resume_tx_channel(struct most_interface *iface,
401 struct net_dev_context *nd;
403 nd = get_net_dev_hold(iface);
407 if (nd->tx.ch_id != channel_idx)
410 netif_wake_queue(nd->dev);
417 static int comp_rx_data(struct mbo *mbo)
420 struct net_dev_context *nd;
421 char *buf = mbo->virt_address;
422 u32 len = mbo->processed_length;
424 struct net_device *dev;
425 unsigned int skb_len;
428 nd = get_net_dev_hold(mbo->ifp);
432 if (nd->rx.ch_id != mbo->hdm_channel_id) {
440 if (!pms_is_mamac(buf, len)) {
445 skb = dev_alloc_skb(len - MDP_HDR_LEN + 2 * ETH_ALEN + 2);
447 if (!PMS_IS_MEP(buf, len)) {
452 skb = dev_alloc_skb(len - MEP_HDR_LEN);
456 dev->stats.rx_dropped++;
457 pr_err_once("drop packet: no memory for skb\n");
465 ether_addr_copy(skb_put(skb, ETH_ALEN), dev->dev_addr);
468 skb_put_data(skb, &zero, 4);
469 skb_put_data(skb, buf + 5, 2);
472 skb_put_data(skb, buf + 10, 2);
481 skb_put_data(skb, buf, len);
482 skb->protocol = eth_type_trans(skb, dev);
484 if (netif_rx(skb) == NET_RX_SUCCESS) {
485 dev->stats.rx_packets++;
486 dev->stats.rx_bytes += skb_len;
488 dev->stats.rx_dropped++;
499 static struct core_component comp = {
501 .probe_channel = comp_probe_channel,
502 .disconnect_channel = comp_disconnect_channel,
503 .tx_completion = comp_resume_tx_channel,
504 .rx_completion = comp_rx_data,
507 static int __init most_net_init(void)
509 spin_lock_init(&list_lock);
510 mutex_init(&probe_disc_mt);
511 return most_register_component(&comp);
514 static void __exit most_net_exit(void)
516 most_deregister_component(&comp);
520 * on_netinfo - callback for HDM to be informed about HW's MAC
521 * @param iface - most interface instance
522 * @param link_stat - link status
523 * @param mac_addr - MAC address
525 static void on_netinfo(struct most_interface *iface,
526 unsigned char link_stat, unsigned char *mac_addr)
528 struct net_dev_context *nd;
529 struct net_device *dev;
530 const u8 *m = mac_addr;
532 nd = get_net_dev_hold(iface);
539 netif_carrier_on(dev);
541 netif_carrier_off(dev);
543 if (m && is_valid_ether_addr(m)) {
544 if (!is_valid_ether_addr(dev->dev_addr)) {
545 netdev_info(dev, "set mac %02x-%02x-%02x-%02x-%02x-%02x\n",
546 m[0], m[1], m[2], m[3], m[4], m[5]);
547 ether_addr_copy(dev->dev_addr, m);
548 netif_dormant_off(dev);
549 } else if (!ether_addr_equal(dev->dev_addr, m)) {
550 netdev_warn(dev, "reject mac %02x-%02x-%02x-%02x-%02x-%02x\n",
551 m[0], m[1], m[2], m[3], m[4], m[5]);
558 module_init(most_net_init);
559 module_exit(most_net_exit);
560 MODULE_LICENSE("GPL");
561 MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
562 MODULE_DESCRIPTION("Networking Component Module for Mostcore");