1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020-21 Intel Corporation.
6 #include <linux/etherdevice.h>
7 #include <linux/if_arp.h>
8 #include <linux/if_link.h>
9 #include <linux/rtnetlink.h>
10 #include <linux/wwan.h>
11 #include <net/pkt_sched.h>
13 #include "iosm_ipc_chnl_cfg.h"
14 #include "iosm_ipc_imem_ops.h"
15 #include "iosm_ipc_wwan.h"
17 #define IOSM_IP_TYPE_MASK 0xF0
18 #define IOSM_IP_TYPE_IPV4 0x40
19 #define IOSM_IP_TYPE_IPV6 0x60
21 #define IOSM_IF_ID_PAYLOAD 2
24 * struct iosm_netdev_priv - netdev WWAN driver specific private data
25 * @ipc_wwan: Pointer to iosm_wwan struct
26 * @netdev: Pointer to network interface device structure
27 * @if_id: Interface id for device.
28 * @ch_id: IPC channel number for which interface device is created.
30 struct iosm_netdev_priv {
31 struct iosm_wwan *ipc_wwan;
32 struct net_device *netdev;
38 * struct iosm_wwan - This structure contains information about WWAN root device
39 * and interface to the IPC layer.
40 * @ipc_imem: Pointer to imem data-struct
41 * @sub_netlist: List of active netdevs
42 * @dev: Pointer device structure
45 struct iosm_imem *ipc_imem;
46 struct iosm_netdev_priv __rcu *sub_netlist[IP_MUX_SESSION_END + 1];
50 /* Bring-up the wwan net link */
51 static int ipc_wwan_link_open(struct net_device *netdev)
53 struct iosm_netdev_priv *priv = wwan_netdev_drvpriv(netdev);
54 struct iosm_wwan *ipc_wwan = priv->ipc_wwan;
55 int if_id = priv->if_id;
57 if (if_id < IP_MUX_SESSION_START ||
58 if_id >= ARRAY_SIZE(ipc_wwan->sub_netlist))
62 priv->ch_id = ipc_imem_sys_wwan_open(ipc_wwan->ipc_imem, if_id);
64 if (priv->ch_id < 0) {
65 dev_err(ipc_wwan->dev,
66 "cannot connect wwan0 & id %d to the IPC mem layer",
71 /* enable tx path, DL data may follow */
72 netif_start_queue(netdev);
74 dev_dbg(ipc_wwan->dev, "Channel id %d allocated to if_id %d",
75 priv->ch_id, priv->if_id);
80 /* Bring-down the wwan net link */
81 static int ipc_wwan_link_stop(struct net_device *netdev)
83 struct iosm_netdev_priv *priv = wwan_netdev_drvpriv(netdev);
85 netif_stop_queue(netdev);
87 ipc_imem_sys_wwan_close(priv->ipc_wwan->ipc_imem, priv->if_id,
94 /* Transmit a packet */
95 static netdev_tx_t ipc_wwan_link_transmit(struct sk_buff *skb,
96 struct net_device *netdev)
98 struct iosm_netdev_priv *priv = wwan_netdev_drvpriv(netdev);
99 struct iosm_wwan *ipc_wwan = priv->ipc_wwan;
100 unsigned int len = skb->len;
101 int if_id = priv->if_id;
104 /* Interface IDs from 1 to 8 are for IP data
105 * & from 257 to 261 are for non-IP data
107 if (if_id < IP_MUX_SESSION_START ||
108 if_id >= ARRAY_SIZE(ipc_wwan->sub_netlist))
111 /* Send the SKB to device for transmission */
112 ret = ipc_imem_sys_wwan_transmit(ipc_wwan->ipc_imem,
113 if_id, priv->ch_id, skb);
115 /* Return code of zero is success */
117 netdev->stats.tx_packets++;
118 netdev->stats.tx_bytes += len;
120 } else if (ret == -EBUSY) {
121 ret = NETDEV_TX_BUSY;
122 dev_err(ipc_wwan->dev, "unable to push packets");
130 /* Log any skb drop */
132 dev_dbg(ipc_wwan->dev, "skb dropped. IF_ID: %d, ret: %d", if_id,
135 dev_kfree_skb_any(skb);
136 netdev->stats.tx_dropped++;
140 /* Ops structure for wwan net link */
141 static const struct net_device_ops ipc_inm_ops = {
142 .ndo_open = ipc_wwan_link_open,
143 .ndo_stop = ipc_wwan_link_stop,
144 .ndo_start_xmit = ipc_wwan_link_transmit,
147 /* Setup function for creating new net link */
148 static void ipc_wwan_setup(struct net_device *iosm_dev)
150 iosm_dev->header_ops = NULL;
151 iosm_dev->hard_header_len = 0;
152 iosm_dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
154 iosm_dev->type = ARPHRD_NONE;
155 iosm_dev->mtu = ETH_DATA_LEN;
156 iosm_dev->min_mtu = ETH_MIN_MTU;
157 iosm_dev->max_mtu = ETH_MAX_MTU;
159 iosm_dev->flags = IFF_POINTOPOINT | IFF_NOARP;
160 iosm_dev->needs_free_netdev = true;
162 iosm_dev->netdev_ops = &ipc_inm_ops;
165 /* Create new wwan net link */
166 static int ipc_wwan_newlink(void *ctxt, struct net_device *dev,
167 u32 if_id, struct netlink_ext_ack *extack)
169 struct iosm_wwan *ipc_wwan = ctxt;
170 struct iosm_netdev_priv *priv;
173 if (if_id < IP_MUX_SESSION_START ||
174 if_id >= ARRAY_SIZE(ipc_wwan->sub_netlist))
177 priv = wwan_netdev_drvpriv(dev);
180 priv->ipc_wwan = ipc_wwan;
182 if (rcu_access_pointer(ipc_wwan->sub_netlist[if_id]))
185 err = register_netdevice(dev);
189 rcu_assign_pointer(ipc_wwan->sub_netlist[if_id], priv);
190 netif_device_attach(dev);
195 static void ipc_wwan_dellink(void *ctxt, struct net_device *dev,
196 struct list_head *head)
198 struct iosm_netdev_priv *priv = wwan_netdev_drvpriv(dev);
199 struct iosm_wwan *ipc_wwan = ctxt;
200 int if_id = priv->if_id;
202 if (WARN_ON(if_id < IP_MUX_SESSION_START ||
203 if_id >= ARRAY_SIZE(ipc_wwan->sub_netlist)))
206 if (WARN_ON(rcu_access_pointer(ipc_wwan->sub_netlist[if_id]) != priv))
209 RCU_INIT_POINTER(ipc_wwan->sub_netlist[if_id], NULL);
210 /* unregistering includes synchronize_net() */
211 unregister_netdevice_queue(dev, head);
214 static const struct wwan_ops iosm_wwan_ops = {
215 .priv_size = sizeof(struct iosm_netdev_priv),
216 .setup = ipc_wwan_setup,
217 .newlink = ipc_wwan_newlink,
218 .dellink = ipc_wwan_dellink,
221 int ipc_wwan_receive(struct iosm_wwan *ipc_wwan, struct sk_buff *skb_arg,
224 struct sk_buff *skb = skb_arg;
225 struct net_device_stats *stats;
226 struct iosm_netdev_priv *priv;
229 if ((skb->data[0] & IOSM_IP_TYPE_MASK) == IOSM_IP_TYPE_IPV4)
230 skb->protocol = htons(ETH_P_IP);
231 else if ((skb->data[0] & IOSM_IP_TYPE_MASK) ==
233 skb->protocol = htons(ETH_P_IPV6);
235 skb->pkt_type = PACKET_HOST;
237 if (if_id < IP_MUX_SESSION_START ||
238 if_id > IP_MUX_SESSION_END) {
244 priv = rcu_dereference(ipc_wwan->sub_netlist[if_id]);
249 skb->dev = priv->netdev;
250 stats = &priv->netdev->stats;
252 stats->rx_bytes += skb->len;
263 void ipc_wwan_tx_flowctrl(struct iosm_wwan *ipc_wwan, int if_id, bool on)
265 struct net_device *netdev;
266 struct iosm_netdev_priv *priv;
270 priv = rcu_dereference(ipc_wwan->sub_netlist[if_id]);
276 netdev = priv->netdev;
278 is_tx_blk = netif_queue_stopped(netdev);
281 dev_dbg(ipc_wwan->dev, "session id[%d]: flowctrl enable",
284 if (on && !is_tx_blk)
285 netif_stop_queue(netdev);
286 else if (!on && is_tx_blk)
287 netif_wake_queue(netdev);
291 struct iosm_wwan *ipc_wwan_init(struct iosm_imem *ipc_imem, struct device *dev)
293 struct iosm_wwan *ipc_wwan;
295 ipc_wwan = kzalloc(sizeof(*ipc_wwan), GFP_KERNEL);
300 ipc_wwan->ipc_imem = ipc_imem;
302 /* WWAN core will create a netdev for the default IP MUX channel */
303 if (wwan_register_ops(ipc_wwan->dev, &iosm_wwan_ops, ipc_wwan,
304 IP_MUX_SESSION_DEFAULT)) {
312 void ipc_wwan_deinit(struct iosm_wwan *ipc_wwan)
314 /* This call will remove all child netdev(s) */
315 wwan_unregister_ops(ipc_wwan->dev);