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>
22 #define MDP_HDR_LEN 16
23 #define MAMAC_DATA_LEN (1024 - MDP_HDR_LEN)
27 #define PMS_TELID_UNSEGM_MAMAC 0x0A
28 #define PMS_FIFONO_MDP 0x01
29 #define PMS_FIFONO_MEP 0x04
30 #define PMS_MSGTYPE_DATA 0x04
31 #define PMS_DEF_PRIO 0
32 #define MEP_DEF_RETRY 15
34 #define PMS_FIFONO_MASK 0x07
35 #define PMS_FIFONO_SHIFT 3
36 #define PMS_RETRY_SHIFT 4
37 #define PMS_TELID_MASK 0x0F
38 #define PMS_TELID_SHIFT 4
40 #define HB(value) ((u8)((u16)(value) >> 8))
41 #define LB(value) ((u8)(value))
43 #define EXTRACT_BIT_SET(bitset_name, value) \
44 (((value) >> bitset_name##_SHIFT) & bitset_name##_MASK)
46 #define PMS_IS_MEP(buf, len) \
47 ((len) > MEP_HDR_LEN && \
48 EXTRACT_BIT_SET(PMS_FIFONO, (buf)[3]) == PMS_FIFONO_MEP)
50 static inline bool pms_is_mamac(char *buf, u32 len)
52 return (len > MDP_HDR_LEN &&
53 EXTRACT_BIT_SET(PMS_FIFONO, buf[3]) == PMS_FIFONO_MDP &&
54 EXTRACT_BIT_SET(PMS_TELID, buf[14]) == PMS_TELID_UNSEGM_MAMAC);
57 struct net_dev_channel {
62 struct net_dev_context {
63 struct most_interface *iface;
65 struct net_device *dev;
66 struct net_dev_channel rx;
67 struct net_dev_channel tx;
68 struct list_head list;
71 static struct list_head net_devices = LIST_HEAD_INIT(net_devices);
72 static struct mutex probe_disc_mt; /* ch->linked = true, most_nd_open */
73 static DEFINE_SPINLOCK(list_lock); /* list_head, ch->linked = false, dev_hold */
74 static struct most_component comp;
76 static int skb_to_mamac(const struct sk_buff *skb, struct mbo *mbo)
78 u8 *buff = mbo->virt_address;
79 static const u8 broadcast[] = { 0x03, 0xFF };
80 const u8 *dest_addr = skb->data + 4;
81 const u8 *eth_type = skb->data + 12;
82 unsigned int payload_len = skb->len - ETH_HLEN;
83 unsigned int mdp_len = payload_len + MDP_HDR_LEN;
85 if (mdp_len < skb->len) {
86 pr_err("drop: too large packet! (%u)\n", skb->len);
90 if (mbo->buffer_length < mdp_len) {
91 pr_err("drop: too small buffer! (%d for %d)\n",
92 mbo->buffer_length, mdp_len);
96 if (skb->len < ETH_HLEN) {
97 pr_err("drop: too small packet! (%d)\n", skb->len);
101 if (dest_addr[0] == 0xFF && dest_addr[1] == 0xFF)
102 dest_addr = broadcast;
104 *buff++ = HB(mdp_len - 2);
105 *buff++ = LB(mdp_len - 2);
108 *buff++ = (PMS_FIFONO_MDP << PMS_FIFONO_SHIFT) | PMS_MSGTYPE_DATA;
109 *buff++ = PMS_DEF_PRIO;
110 *buff++ = dest_addr[0];
111 *buff++ = dest_addr[1];
114 *buff++ = HB(payload_len + 6);
115 *buff++ = LB(payload_len + 6);
117 /* end of FPH here */
119 *buff++ = eth_type[0];
120 *buff++ = eth_type[1];
124 *buff++ = PMS_TELID_UNSEGM_MAMAC << 4 | HB(payload_len);
125 *buff++ = LB(payload_len);
127 memcpy(buff, skb->data + ETH_HLEN, payload_len);
128 mbo->buffer_length = mdp_len;
132 static int skb_to_mep(const struct sk_buff *skb, struct mbo *mbo)
134 u8 *buff = mbo->virt_address;
135 unsigned int mep_len = skb->len + MEP_HDR_LEN;
137 if (mep_len < skb->len) {
138 pr_err("drop: too large packet! (%u)\n", skb->len);
142 if (mbo->buffer_length < mep_len) {
143 pr_err("drop: too small buffer! (%d for %d)\n",
144 mbo->buffer_length, mep_len);
148 *buff++ = HB(mep_len - 2);
149 *buff++ = LB(mep_len - 2);
152 *buff++ = (PMS_FIFONO_MEP << PMS_FIFONO_SHIFT) | PMS_MSGTYPE_DATA;
153 *buff++ = (MEP_DEF_RETRY << PMS_RETRY_SHIFT) | PMS_DEF_PRIO;
158 memcpy(buff, skb->data, skb->len);
159 mbo->buffer_length = mep_len;
163 static int most_nd_set_mac_address(struct net_device *dev, void *p)
165 struct net_dev_context *nd = netdev_priv(dev);
166 int err = eth_mac_addr(dev, p);
172 (dev->dev_addr[0] == 0 && dev->dev_addr[1] == 0 &&
173 dev->dev_addr[2] == 0 && dev->dev_addr[3] == 0);
176 * Set default MTU for the given packet type.
177 * It is still possible to change MTU using ip tools afterwards.
179 dev->mtu = nd->is_mamac ? MAMAC_DATA_LEN : ETH_DATA_LEN;
184 static void on_netinfo(struct most_interface *iface,
185 unsigned char link_stat, unsigned char *mac_addr);
187 static int most_nd_open(struct net_device *dev)
189 struct net_dev_context *nd = netdev_priv(dev);
192 mutex_lock(&probe_disc_mt);
194 if (most_start_channel(nd->iface, nd->rx.ch_id, &comp)) {
195 netdev_err(dev, "most_start_channel() failed\n");
200 if (most_start_channel(nd->iface, nd->tx.ch_id, &comp)) {
201 netdev_err(dev, "most_start_channel() failed\n");
202 most_stop_channel(nd->iface, nd->rx.ch_id, &comp);
207 netif_carrier_off(dev);
208 if (is_valid_ether_addr(dev->dev_addr))
209 netif_dormant_off(dev);
211 netif_dormant_on(dev);
212 netif_wake_queue(dev);
213 if (nd->iface->request_netinfo)
214 nd->iface->request_netinfo(nd->iface, nd->tx.ch_id, on_netinfo);
217 mutex_unlock(&probe_disc_mt);
221 static int most_nd_stop(struct net_device *dev)
223 struct net_dev_context *nd = netdev_priv(dev);
225 netif_stop_queue(dev);
226 if (nd->iface->request_netinfo)
227 nd->iface->request_netinfo(nd->iface, nd->tx.ch_id, NULL);
228 most_stop_channel(nd->iface, nd->rx.ch_id, &comp);
229 most_stop_channel(nd->iface, nd->tx.ch_id, &comp);
234 static netdev_tx_t most_nd_start_xmit(struct sk_buff *skb,
235 struct net_device *dev)
237 struct net_dev_context *nd = netdev_priv(dev);
241 mbo = most_get_mbo(nd->iface, nd->tx.ch_id, &comp);
244 netif_stop_queue(dev);
245 dev->stats.tx_fifo_errors++;
246 return NETDEV_TX_BUSY;
250 ret = skb_to_mamac(skb, mbo);
252 ret = skb_to_mep(skb, mbo);
256 dev->stats.tx_dropped++;
261 most_submit_mbo(mbo);
262 dev->stats.tx_packets++;
263 dev->stats.tx_bytes += skb->len;
268 static const struct net_device_ops most_nd_ops = {
269 .ndo_open = most_nd_open,
270 .ndo_stop = most_nd_stop,
271 .ndo_start_xmit = most_nd_start_xmit,
272 .ndo_set_mac_address = most_nd_set_mac_address,
275 static void most_nd_setup(struct net_device *dev)
278 dev->netdev_ops = &most_nd_ops;
281 static struct net_dev_context *get_net_dev(struct most_interface *iface)
283 struct net_dev_context *nd;
285 list_for_each_entry(nd, &net_devices, list)
286 if (nd->iface == iface)
291 static struct net_dev_context *get_net_dev_hold(struct most_interface *iface)
293 struct net_dev_context *nd;
296 spin_lock_irqsave(&list_lock, flags);
297 nd = get_net_dev(iface);
298 if (nd && nd->rx.linked && nd->tx.linked)
302 spin_unlock_irqrestore(&list_lock, flags);
306 static int comp_probe_channel(struct most_interface *iface, int channel_idx,
307 struct most_channel_config *ccfg, char *name,
310 struct net_dev_context *nd;
311 struct net_dev_channel *ch;
312 struct net_device *dev;
319 if (ccfg->data_type != MOST_CH_ASYNC)
322 mutex_lock(&probe_disc_mt);
323 nd = get_net_dev(iface);
325 dev = alloc_netdev(sizeof(struct net_dev_context), "meth%d",
326 NET_NAME_UNKNOWN, most_nd_setup);
332 nd = netdev_priv(dev);
336 spin_lock_irqsave(&list_lock, flags);
337 list_add(&nd->list, &net_devices);
338 spin_unlock_irqrestore(&list_lock, flags);
340 ch = ccfg->direction == MOST_CH_TX ? &nd->tx : &nd->rx;
342 ch = ccfg->direction == MOST_CH_TX ? &nd->tx : &nd->rx;
344 pr_err("direction is allocated\n");
349 if (register_netdev(nd->dev)) {
350 pr_err("register_netdev() failed\n");
355 ch->ch_id = channel_idx;
359 mutex_unlock(&probe_disc_mt);
363 static int comp_disconnect_channel(struct most_interface *iface,
366 struct net_dev_context *nd;
367 struct net_dev_channel *ch;
371 mutex_lock(&probe_disc_mt);
372 nd = get_net_dev(iface);
378 if (nd->rx.linked && channel_idx == nd->rx.ch_id) {
380 } else if (nd->tx.linked && channel_idx == nd->tx.ch_id) {
387 if (nd->rx.linked && nd->tx.linked) {
388 spin_lock_irqsave(&list_lock, flags);
390 spin_unlock_irqrestore(&list_lock, flags);
393 * do not call most_stop_channel() here, because channels are
394 * going to be closed in ndo_stop() after unregister_netdev()
396 unregister_netdev(nd->dev);
398 spin_lock_irqsave(&list_lock, flags);
400 spin_unlock_irqrestore(&list_lock, flags);
402 free_netdev(nd->dev);
406 mutex_unlock(&probe_disc_mt);
410 static int comp_resume_tx_channel(struct most_interface *iface,
413 struct net_dev_context *nd;
415 nd = get_net_dev_hold(iface);
419 if (nd->tx.ch_id != channel_idx)
422 netif_wake_queue(nd->dev);
429 static int comp_rx_data(struct mbo *mbo)
432 struct net_dev_context *nd;
433 char *buf = mbo->virt_address;
434 u32 len = mbo->processed_length;
436 struct net_device *dev;
437 unsigned int skb_len;
440 nd = get_net_dev_hold(mbo->ifp);
444 if (nd->rx.ch_id != mbo->hdm_channel_id) {
452 if (!pms_is_mamac(buf, len)) {
457 skb = dev_alloc_skb(len - MDP_HDR_LEN + 2 * ETH_ALEN + 2);
459 if (!PMS_IS_MEP(buf, len)) {
464 skb = dev_alloc_skb(len - MEP_HDR_LEN);
468 dev->stats.rx_dropped++;
469 pr_err_once("drop packet: no memory for skb\n");
477 ether_addr_copy(skb_put(skb, ETH_ALEN), dev->dev_addr);
480 skb_put_data(skb, &zero, 4);
481 skb_put_data(skb, buf + 5, 2);
484 skb_put_data(skb, buf + 10, 2);
493 skb_put_data(skb, buf, len);
494 skb->protocol = eth_type_trans(skb, dev);
496 if (netif_rx(skb) == NET_RX_SUCCESS) {
497 dev->stats.rx_packets++;
498 dev->stats.rx_bytes += skb_len;
500 dev->stats.rx_dropped++;
511 static struct most_component comp = {
514 .probe_channel = comp_probe_channel,
515 .disconnect_channel = comp_disconnect_channel,
516 .tx_completion = comp_resume_tx_channel,
517 .rx_completion = comp_rx_data,
520 static int __init most_net_init(void)
524 mutex_init(&probe_disc_mt);
525 err = most_register_component(&comp);
528 err = most_register_configfs_subsys(&comp);
530 most_deregister_component(&comp);
536 static void __exit most_net_exit(void)
538 most_deregister_configfs_subsys(&comp);
539 most_deregister_component(&comp);
543 * on_netinfo - callback for HDM to be informed about HW's MAC
544 * @param iface - most interface instance
545 * @param link_stat - link status
546 * @param mac_addr - MAC address
548 static void on_netinfo(struct most_interface *iface,
549 unsigned char link_stat, unsigned char *mac_addr)
551 struct net_dev_context *nd;
552 struct net_device *dev;
553 const u8 *m = mac_addr;
555 nd = get_net_dev_hold(iface);
562 netif_carrier_on(dev);
564 netif_carrier_off(dev);
566 if (m && is_valid_ether_addr(m)) {
567 if (!is_valid_ether_addr(dev->dev_addr)) {
568 netdev_info(dev, "set mac %02x-%02x-%02x-%02x-%02x-%02x\n",
569 m[0], m[1], m[2], m[3], m[4], m[5]);
570 ether_addr_copy(dev->dev_addr, m);
571 netif_dormant_off(dev);
572 } else if (!ether_addr_equal(dev->dev_addr, m)) {
573 netdev_warn(dev, "reject mac %02x-%02x-%02x-%02x-%02x-%02x\n",
574 m[0], m[1], m[2], m[3], m[4], m[5]);
581 module_init(most_net_init);
582 module_exit(most_net_exit);
583 MODULE_LICENSE("GPL");
584 MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
585 MODULE_DESCRIPTION("Networking Component Module for Mostcore");