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>
12 #include "iosm_ipc_chnl_cfg.h"
13 #include "iosm_ipc_imem_ops.h"
14 #include "iosm_ipc_wwan.h"
16 #define IOSM_IP_TYPE_MASK 0xF0
17 #define IOSM_IP_TYPE_IPV4 0x40
18 #define IOSM_IP_TYPE_IPV6 0x60
20 #define IOSM_IF_ID_PAYLOAD 2
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
42 * @if_mutex: Mutex used for add and remove interface id
45 struct iosm_imem *ipc_imem;
46 struct iosm_netdev_priv __rcu *sub_netlist[IP_MUX_SESSION_END + 1];
48 struct mutex if_mutex; /* Mutex used for add and remove interface id */
51 /* Bring-up the wwan net link */
52 static int ipc_wwan_link_open(struct net_device *netdev)
54 struct iosm_netdev_priv *priv = wwan_netdev_drvpriv(netdev);
55 struct iosm_wwan *ipc_wwan = priv->ipc_wwan;
56 int if_id = priv->if_id;
59 if (if_id < IP_MUX_SESSION_START ||
60 if_id >= ARRAY_SIZE(ipc_wwan->sub_netlist))
63 mutex_lock(&ipc_wwan->if_mutex);
66 priv->ch_id = ipc_imem_sys_wwan_open(ipc_wwan->ipc_imem, if_id);
68 if (priv->ch_id < 0) {
69 dev_err(ipc_wwan->dev,
70 "cannot connect wwan0 & id %d to the IPC mem layer",
76 /* enable tx path, DL data may follow */
77 netif_start_queue(netdev);
79 dev_dbg(ipc_wwan->dev, "Channel id %d allocated to if_id %d",
80 priv->ch_id, priv->if_id);
84 mutex_unlock(&ipc_wwan->if_mutex);
88 /* Bring-down the wwan net link */
89 static int ipc_wwan_link_stop(struct net_device *netdev)
91 struct iosm_netdev_priv *priv = wwan_netdev_drvpriv(netdev);
93 netif_stop_queue(netdev);
95 mutex_lock(&priv->ipc_wwan->if_mutex);
96 ipc_imem_sys_wwan_close(priv->ipc_wwan->ipc_imem, priv->if_id,
99 mutex_unlock(&priv->ipc_wwan->if_mutex);
104 /* Transmit a packet */
105 static int ipc_wwan_link_transmit(struct sk_buff *skb,
106 struct net_device *netdev)
108 struct iosm_netdev_priv *priv = wwan_netdev_drvpriv(netdev);
109 struct iosm_wwan *ipc_wwan = priv->ipc_wwan;
110 unsigned int len = skb->len;
111 int if_id = priv->if_id;
114 /* Interface IDs from 1 to 8 are for IP data
115 * & from 257 to 261 are for non-IP data
117 if (if_id < IP_MUX_SESSION_START ||
118 if_id >= ARRAY_SIZE(ipc_wwan->sub_netlist))
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");
140 /* Log any skb drop */
142 dev_dbg(ipc_wwan->dev, "skb dropped. IF_ID: %d, ret: %d", if_id,
145 dev_kfree_skb_any(skb);
146 netdev->stats.tx_dropped++;
150 /* Ops structure for wwan net link */
151 static const struct net_device_ops ipc_inm_ops = {
152 .ndo_open = ipc_wwan_link_open,
153 .ndo_stop = ipc_wwan_link_stop,
154 .ndo_start_xmit = ipc_wwan_link_transmit,
157 /* Setup function for creating new net link */
158 static void ipc_wwan_setup(struct net_device *iosm_dev)
160 iosm_dev->header_ops = NULL;
161 iosm_dev->hard_header_len = 0;
162 iosm_dev->priv_flags |= IFF_NO_QUEUE;
164 iosm_dev->type = ARPHRD_NONE;
165 iosm_dev->mtu = ETH_DATA_LEN;
166 iosm_dev->min_mtu = ETH_MIN_MTU;
167 iosm_dev->max_mtu = ETH_MAX_MTU;
169 iosm_dev->flags = IFF_POINTOPOINT | IFF_NOARP;
171 iosm_dev->netdev_ops = &ipc_inm_ops;
174 /* Create new wwan net link */
175 static int ipc_wwan_newlink(void *ctxt, struct net_device *dev,
176 u32 if_id, struct netlink_ext_ack *extack)
178 struct iosm_wwan *ipc_wwan = ctxt;
179 struct iosm_netdev_priv *priv;
182 if (if_id < IP_MUX_SESSION_START ||
183 if_id >= ARRAY_SIZE(ipc_wwan->sub_netlist))
186 priv = wwan_netdev_drvpriv(dev);
189 priv->ipc_wwan = ipc_wwan;
191 mutex_lock(&ipc_wwan->if_mutex);
192 if (rcu_access_pointer(ipc_wwan->sub_netlist[if_id])) {
197 err = register_netdevice(dev);
201 rcu_assign_pointer(ipc_wwan->sub_netlist[if_id], priv);
202 mutex_unlock(&ipc_wwan->if_mutex);
204 netif_device_attach(dev);
209 mutex_unlock(&ipc_wwan->if_mutex);
213 static void ipc_wwan_dellink(void *ctxt, struct net_device *dev,
214 struct list_head *head)
216 struct iosm_netdev_priv *priv = wwan_netdev_drvpriv(dev);
217 struct iosm_wwan *ipc_wwan = ctxt;
218 int if_id = priv->if_id;
220 if (WARN_ON(if_id < IP_MUX_SESSION_START ||
221 if_id >= ARRAY_SIZE(ipc_wwan->sub_netlist)))
224 mutex_lock(&ipc_wwan->if_mutex);
226 if (WARN_ON(rcu_access_pointer(ipc_wwan->sub_netlist[if_id]) != priv))
229 RCU_INIT_POINTER(ipc_wwan->sub_netlist[if_id], NULL);
230 /* unregistering includes synchronize_net() */
231 unregister_netdevice(dev);
234 mutex_unlock(&ipc_wwan->if_mutex);
237 static const struct wwan_ops iosm_wwan_ops = {
238 .priv_size = sizeof(struct iosm_netdev_priv),
239 .setup = ipc_wwan_setup,
240 .newlink = ipc_wwan_newlink,
241 .dellink = ipc_wwan_dellink,
244 int ipc_wwan_receive(struct iosm_wwan *ipc_wwan, struct sk_buff *skb_arg,
247 struct sk_buff *skb = skb_arg;
248 struct net_device_stats *stats;
249 struct iosm_netdev_priv *priv;
252 if ((skb->data[0] & IOSM_IP_TYPE_MASK) == IOSM_IP_TYPE_IPV4)
253 skb->protocol = htons(ETH_P_IP);
254 else if ((skb->data[0] & IOSM_IP_TYPE_MASK) ==
256 skb->protocol = htons(ETH_P_IPV6);
258 skb->pkt_type = PACKET_HOST;
260 if (if_id < IP_MUX_SESSION_START ||
261 if_id > IP_MUX_SESSION_END) {
267 priv = rcu_dereference(ipc_wwan->sub_netlist[if_id]);
272 skb->dev = priv->netdev;
273 stats = &priv->netdev->stats;
275 stats->rx_bytes += skb->len;
286 void ipc_wwan_tx_flowctrl(struct iosm_wwan *ipc_wwan, int if_id, bool on)
288 struct net_device *netdev;
289 struct iosm_netdev_priv *priv;
293 priv = rcu_dereference(ipc_wwan->sub_netlist[if_id]);
299 netdev = priv->netdev;
301 is_tx_blk = netif_queue_stopped(netdev);
304 dev_dbg(ipc_wwan->dev, "session id[%d]: flowctrl enable",
307 if (on && !is_tx_blk)
308 netif_stop_queue(netdev);
309 else if (!on && is_tx_blk)
310 netif_wake_queue(netdev);
314 struct iosm_wwan *ipc_wwan_init(struct iosm_imem *ipc_imem, struct device *dev)
316 struct iosm_wwan *ipc_wwan;
318 ipc_wwan = kzalloc(sizeof(*ipc_wwan), GFP_KERNEL);
323 ipc_wwan->ipc_imem = ipc_imem;
325 /* WWAN core will create a netdev for the default IP MUX channel */
326 if (wwan_register_ops(ipc_wwan->dev, &iosm_wwan_ops, ipc_wwan,
327 IP_MUX_SESSION_DEFAULT)) {
332 mutex_init(&ipc_wwan->if_mutex);
337 void ipc_wwan_deinit(struct iosm_wwan *ipc_wwan)
339 /* This call will remove all child netdev(s) */
340 wwan_unregister_ops(ipc_wwan->dev);
342 mutex_destroy(&ipc_wwan->if_mutex);