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/pm_runtime.h>
10 #include <linux/rtnetlink.h>
11 #include <linux/wwan.h>
12 #include <net/pkt_sched.h>
14 #include "iosm_ipc_chnl_cfg.h"
15 #include "iosm_ipc_imem_ops.h"
16 #include "iosm_ipc_wwan.h"
18 #define IOSM_IP_TYPE_MASK 0xF0
19 #define IOSM_IP_TYPE_IPV4 0x40
20 #define IOSM_IP_TYPE_IPV6 0x60
23 * struct iosm_netdev_priv - netdev WWAN driver specific private data
24 * @ipc_wwan: Pointer to iosm_wwan struct
25 * @netdev: Pointer to network interface device structure
26 * @if_id: Interface id for device.
27 * @ch_id: IPC channel number for which interface device is created.
29 struct iosm_netdev_priv {
30 struct iosm_wwan *ipc_wwan;
31 struct net_device *netdev;
37 * struct iosm_wwan - This structure contains information about WWAN root device
38 * and interface to the IPC layer.
39 * @ipc_imem: Pointer to imem data-struct
40 * @sub_netlist: List of active netdevs
41 * @dev: Pointer device structure
44 struct iosm_imem *ipc_imem;
45 struct iosm_netdev_priv __rcu *sub_netlist[IP_MUX_SESSION_END + 1];
49 /* Bring-up the wwan net link */
50 static int ipc_wwan_link_open(struct net_device *netdev)
52 struct iosm_netdev_priv *priv = wwan_netdev_drvpriv(netdev);
53 struct iosm_wwan *ipc_wwan = priv->ipc_wwan;
54 int if_id = priv->if_id;
57 if (if_id < IP_MUX_SESSION_START ||
58 if_id >= ARRAY_SIZE(ipc_wwan->sub_netlist))
61 pm_runtime_get_sync(ipc_wwan->ipc_imem->dev);
63 priv->ch_id = ipc_imem_sys_wwan_open(ipc_wwan->ipc_imem, if_id);
65 if (priv->ch_id < 0) {
66 dev_err(ipc_wwan->dev,
67 "cannot connect wwan0 & id %d to the IPC mem layer",
73 /* enable tx path, DL data may follow */
74 netif_start_queue(netdev);
76 dev_dbg(ipc_wwan->dev, "Channel id %d allocated to if_id %d",
77 priv->ch_id, priv->if_id);
80 pm_runtime_mark_last_busy(ipc_wwan->ipc_imem->dev);
81 pm_runtime_put_autosuspend(ipc_wwan->ipc_imem->dev);
86 /* Bring-down the wwan net link */
87 static int ipc_wwan_link_stop(struct net_device *netdev)
89 struct iosm_netdev_priv *priv = wwan_netdev_drvpriv(netdev);
91 netif_stop_queue(netdev);
93 pm_runtime_get_sync(priv->ipc_wwan->ipc_imem->dev);
94 ipc_imem_sys_wwan_close(priv->ipc_wwan->ipc_imem, priv->if_id,
97 pm_runtime_mark_last_busy(priv->ipc_wwan->ipc_imem->dev);
98 pm_runtime_put_autosuspend(priv->ipc_wwan->ipc_imem->dev);
103 /* Transmit a packet */
104 static netdev_tx_t ipc_wwan_link_transmit(struct sk_buff *skb,
105 struct net_device *netdev)
107 struct iosm_netdev_priv *priv = wwan_netdev_drvpriv(netdev);
108 struct iosm_wwan *ipc_wwan = priv->ipc_wwan;
109 unsigned int len = skb->len;
110 int if_id = priv->if_id;
113 /* Interface IDs from 1 to 8 are for IP data
114 * & from 257 to 261 are for non-IP data
116 if (if_id < IP_MUX_SESSION_START ||
117 if_id >= ARRAY_SIZE(ipc_wwan->sub_netlist))
120 pm_runtime_get(ipc_wwan->ipc_imem->dev);
121 /* Send the SKB to device for transmission */
122 ret = ipc_imem_sys_wwan_transmit(ipc_wwan->ipc_imem,
123 if_id, priv->ch_id, skb);
125 /* Return code of zero is success */
127 netdev->stats.tx_packets++;
128 netdev->stats.tx_bytes += len;
130 } else if (ret == -EBUSY) {
131 ret = NETDEV_TX_BUSY;
132 dev_err(ipc_wwan->dev, "unable to push packets");
134 pm_runtime_mark_last_busy(ipc_wwan->ipc_imem->dev);
135 pm_runtime_put_autosuspend(ipc_wwan->ipc_imem->dev);
139 pm_runtime_mark_last_busy(ipc_wwan->ipc_imem->dev);
140 pm_runtime_put_autosuspend(ipc_wwan->ipc_imem->dev);
145 /* Log any skb drop */
147 dev_dbg(ipc_wwan->dev, "skb dropped. IF_ID: %d, ret: %d", if_id,
150 dev_kfree_skb_any(skb);
151 netdev->stats.tx_dropped++;
155 /* Ops structure for wwan net link */
156 static const struct net_device_ops ipc_inm_ops = {
157 .ndo_open = ipc_wwan_link_open,
158 .ndo_stop = ipc_wwan_link_stop,
159 .ndo_start_xmit = ipc_wwan_link_transmit,
162 /* Setup function for creating new net link */
163 static void ipc_wwan_setup(struct net_device *iosm_dev)
165 iosm_dev->header_ops = NULL;
166 iosm_dev->hard_header_len = 0;
167 iosm_dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
169 iosm_dev->type = ARPHRD_NONE;
170 iosm_dev->mtu = ETH_DATA_LEN;
171 iosm_dev->min_mtu = ETH_MIN_MTU;
172 iosm_dev->max_mtu = ETH_MAX_MTU;
174 iosm_dev->flags = IFF_POINTOPOINT | IFF_NOARP;
175 iosm_dev->needs_free_netdev = true;
177 iosm_dev->netdev_ops = &ipc_inm_ops;
180 /* Create new wwan net link */
181 static int ipc_wwan_newlink(void *ctxt, struct net_device *dev,
182 u32 if_id, struct netlink_ext_ack *extack)
184 struct iosm_wwan *ipc_wwan = ctxt;
185 struct iosm_netdev_priv *priv;
188 if (if_id < IP_MUX_SESSION_START ||
189 if_id >= ARRAY_SIZE(ipc_wwan->sub_netlist))
192 priv = wwan_netdev_drvpriv(dev);
195 priv->ipc_wwan = ipc_wwan;
197 if (rcu_access_pointer(ipc_wwan->sub_netlist[if_id]))
200 err = register_netdevice(dev);
204 rcu_assign_pointer(ipc_wwan->sub_netlist[if_id], priv);
205 netif_device_attach(dev);
210 static void ipc_wwan_dellink(void *ctxt, struct net_device *dev,
211 struct list_head *head)
213 struct iosm_netdev_priv *priv = wwan_netdev_drvpriv(dev);
214 struct iosm_wwan *ipc_wwan = ctxt;
215 int if_id = priv->if_id;
217 if (WARN_ON(if_id < IP_MUX_SESSION_START ||
218 if_id >= ARRAY_SIZE(ipc_wwan->sub_netlist)))
221 if (WARN_ON(rcu_access_pointer(ipc_wwan->sub_netlist[if_id]) != priv))
224 RCU_INIT_POINTER(ipc_wwan->sub_netlist[if_id], NULL);
225 /* unregistering includes synchronize_net() */
226 unregister_netdevice_queue(dev, head);
229 static const struct wwan_ops iosm_wwan_ops = {
230 .priv_size = sizeof(struct iosm_netdev_priv),
231 .setup = ipc_wwan_setup,
232 .newlink = ipc_wwan_newlink,
233 .dellink = ipc_wwan_dellink,
236 int ipc_wwan_receive(struct iosm_wwan *ipc_wwan, struct sk_buff *skb_arg,
239 struct sk_buff *skb = skb_arg;
240 struct net_device_stats *stats;
241 struct iosm_netdev_priv *priv;
244 if ((skb->data[0] & IOSM_IP_TYPE_MASK) == IOSM_IP_TYPE_IPV4)
245 skb->protocol = htons(ETH_P_IP);
246 else if ((skb->data[0] & IOSM_IP_TYPE_MASK) ==
248 skb->protocol = htons(ETH_P_IPV6);
250 skb->pkt_type = PACKET_HOST;
252 if (if_id < IP_MUX_SESSION_START ||
253 if_id > IP_MUX_SESSION_END) {
259 priv = rcu_dereference(ipc_wwan->sub_netlist[if_id]);
264 skb->dev = priv->netdev;
265 stats = &priv->netdev->stats;
267 stats->rx_bytes += skb->len;
278 void ipc_wwan_tx_flowctrl(struct iosm_wwan *ipc_wwan, int if_id, bool on)
280 struct net_device *netdev;
281 struct iosm_netdev_priv *priv;
285 priv = rcu_dereference(ipc_wwan->sub_netlist[if_id]);
291 netdev = priv->netdev;
293 is_tx_blk = netif_queue_stopped(netdev);
296 dev_dbg(ipc_wwan->dev, "session id[%d]: flowctrl enable",
299 if (on && !is_tx_blk)
300 netif_stop_queue(netdev);
301 else if (!on && is_tx_blk)
302 netif_wake_queue(netdev);
306 struct iosm_wwan *ipc_wwan_init(struct iosm_imem *ipc_imem, struct device *dev)
308 struct iosm_wwan *ipc_wwan;
310 ipc_wwan = kzalloc(sizeof(*ipc_wwan), GFP_KERNEL);
315 ipc_wwan->ipc_imem = ipc_imem;
317 /* WWAN core will create a netdev for the default IP MUX channel */
318 if (wwan_register_ops(ipc_wwan->dev, &iosm_wwan_ops, ipc_wwan,
319 IP_MUX_SESSION_DEFAULT)) {
327 void ipc_wwan_deinit(struct iosm_wwan *ipc_wwan)
329 /* This call will remove all child netdev(s) */
330 wwan_unregister_ops(ipc_wwan->dev);