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
22 * struct iosm_netdev_priv - netdev WWAN driver specific private data
23 * @ipc_wwan: Pointer to iosm_wwan struct
24 * @netdev: Pointer to network interface device structure
25 * @if_id: Interface id for device.
26 * @ch_id: IPC channel number for which interface device is created.
28 struct iosm_netdev_priv {
29 struct iosm_wwan *ipc_wwan;
30 struct net_device *netdev;
36 * struct iosm_wwan - This structure contains information about WWAN root device
37 * and interface to the IPC layer.
38 * @ipc_imem: Pointer to imem data-struct
39 * @sub_netlist: List of active netdevs
40 * @dev: Pointer device structure
43 struct iosm_imem *ipc_imem;
44 struct iosm_netdev_priv __rcu *sub_netlist[IP_MUX_SESSION_END + 1];
48 /* Bring-up the wwan net link */
49 static int ipc_wwan_link_open(struct net_device *netdev)
51 struct iosm_netdev_priv *priv = wwan_netdev_drvpriv(netdev);
52 struct iosm_wwan *ipc_wwan = priv->ipc_wwan;
53 int if_id = priv->if_id;
55 if (if_id < IP_MUX_SESSION_START ||
56 if_id >= ARRAY_SIZE(ipc_wwan->sub_netlist))
60 priv->ch_id = ipc_imem_sys_wwan_open(ipc_wwan->ipc_imem, if_id);
62 if (priv->ch_id < 0) {
63 dev_err(ipc_wwan->dev,
64 "cannot connect wwan0 & id %d to the IPC mem layer",
69 /* enable tx path, DL data may follow */
70 netif_start_queue(netdev);
72 dev_dbg(ipc_wwan->dev, "Channel id %d allocated to if_id %d",
73 priv->ch_id, priv->if_id);
78 /* Bring-down the wwan net link */
79 static int ipc_wwan_link_stop(struct net_device *netdev)
81 struct iosm_netdev_priv *priv = wwan_netdev_drvpriv(netdev);
83 netif_stop_queue(netdev);
85 ipc_imem_sys_wwan_close(priv->ipc_wwan->ipc_imem, priv->if_id,
92 /* Transmit a packet */
93 static netdev_tx_t ipc_wwan_link_transmit(struct sk_buff *skb,
94 struct net_device *netdev)
96 struct iosm_netdev_priv *priv = wwan_netdev_drvpriv(netdev);
97 struct iosm_wwan *ipc_wwan = priv->ipc_wwan;
98 unsigned int len = skb->len;
99 int if_id = priv->if_id;
102 /* Interface IDs from 1 to 8 are for IP data
103 * & from 257 to 261 are for non-IP data
105 if (if_id < IP_MUX_SESSION_START ||
106 if_id >= ARRAY_SIZE(ipc_wwan->sub_netlist))
109 /* Send the SKB to device for transmission */
110 ret = ipc_imem_sys_wwan_transmit(ipc_wwan->ipc_imem,
111 if_id, priv->ch_id, skb);
113 /* Return code of zero is success */
115 netdev->stats.tx_packets++;
116 netdev->stats.tx_bytes += len;
118 } else if (ret == -EBUSY) {
119 ret = NETDEV_TX_BUSY;
120 dev_err(ipc_wwan->dev, "unable to push packets");
128 /* Log any skb drop */
130 dev_dbg(ipc_wwan->dev, "skb dropped. IF_ID: %d, ret: %d", if_id,
133 dev_kfree_skb_any(skb);
134 netdev->stats.tx_dropped++;
138 /* Ops structure for wwan net link */
139 static const struct net_device_ops ipc_inm_ops = {
140 .ndo_open = ipc_wwan_link_open,
141 .ndo_stop = ipc_wwan_link_stop,
142 .ndo_start_xmit = ipc_wwan_link_transmit,
145 /* Setup function for creating new net link */
146 static void ipc_wwan_setup(struct net_device *iosm_dev)
148 iosm_dev->header_ops = NULL;
149 iosm_dev->hard_header_len = 0;
150 iosm_dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
152 iosm_dev->type = ARPHRD_NONE;
153 iosm_dev->mtu = ETH_DATA_LEN;
154 iosm_dev->min_mtu = ETH_MIN_MTU;
155 iosm_dev->max_mtu = ETH_MAX_MTU;
157 iosm_dev->flags = IFF_POINTOPOINT | IFF_NOARP;
158 iosm_dev->needs_free_netdev = true;
160 iosm_dev->netdev_ops = &ipc_inm_ops;
163 /* Create new wwan net link */
164 static int ipc_wwan_newlink(void *ctxt, struct net_device *dev,
165 u32 if_id, struct netlink_ext_ack *extack)
167 struct iosm_wwan *ipc_wwan = ctxt;
168 struct iosm_netdev_priv *priv;
171 if (if_id < IP_MUX_SESSION_START ||
172 if_id >= ARRAY_SIZE(ipc_wwan->sub_netlist))
175 priv = wwan_netdev_drvpriv(dev);
178 priv->ipc_wwan = ipc_wwan;
180 if (rcu_access_pointer(ipc_wwan->sub_netlist[if_id]))
183 err = register_netdevice(dev);
187 rcu_assign_pointer(ipc_wwan->sub_netlist[if_id], priv);
188 netif_device_attach(dev);
193 static void ipc_wwan_dellink(void *ctxt, struct net_device *dev,
194 struct list_head *head)
196 struct iosm_netdev_priv *priv = wwan_netdev_drvpriv(dev);
197 struct iosm_wwan *ipc_wwan = ctxt;
198 int if_id = priv->if_id;
200 if (WARN_ON(if_id < IP_MUX_SESSION_START ||
201 if_id >= ARRAY_SIZE(ipc_wwan->sub_netlist)))
204 if (WARN_ON(rcu_access_pointer(ipc_wwan->sub_netlist[if_id]) != priv))
207 RCU_INIT_POINTER(ipc_wwan->sub_netlist[if_id], NULL);
208 /* unregistering includes synchronize_net() */
209 unregister_netdevice_queue(dev, head);
212 static const struct wwan_ops iosm_wwan_ops = {
213 .priv_size = sizeof(struct iosm_netdev_priv),
214 .setup = ipc_wwan_setup,
215 .newlink = ipc_wwan_newlink,
216 .dellink = ipc_wwan_dellink,
219 int ipc_wwan_receive(struct iosm_wwan *ipc_wwan, struct sk_buff *skb_arg,
222 struct sk_buff *skb = skb_arg;
223 struct net_device_stats *stats;
224 struct iosm_netdev_priv *priv;
227 if ((skb->data[0] & IOSM_IP_TYPE_MASK) == IOSM_IP_TYPE_IPV4)
228 skb->protocol = htons(ETH_P_IP);
229 else if ((skb->data[0] & IOSM_IP_TYPE_MASK) ==
231 skb->protocol = htons(ETH_P_IPV6);
233 skb->pkt_type = PACKET_HOST;
235 if (if_id < IP_MUX_SESSION_START ||
236 if_id > IP_MUX_SESSION_END) {
242 priv = rcu_dereference(ipc_wwan->sub_netlist[if_id]);
247 skb->dev = priv->netdev;
248 stats = &priv->netdev->stats;
250 stats->rx_bytes += skb->len;
261 void ipc_wwan_tx_flowctrl(struct iosm_wwan *ipc_wwan, int if_id, bool on)
263 struct net_device *netdev;
264 struct iosm_netdev_priv *priv;
268 priv = rcu_dereference(ipc_wwan->sub_netlist[if_id]);
274 netdev = priv->netdev;
276 is_tx_blk = netif_queue_stopped(netdev);
279 dev_dbg(ipc_wwan->dev, "session id[%d]: flowctrl enable",
282 if (on && !is_tx_blk)
283 netif_stop_queue(netdev);
284 else if (!on && is_tx_blk)
285 netif_wake_queue(netdev);
289 struct iosm_wwan *ipc_wwan_init(struct iosm_imem *ipc_imem, struct device *dev)
291 struct iosm_wwan *ipc_wwan;
293 ipc_wwan = kzalloc(sizeof(*ipc_wwan), GFP_KERNEL);
298 ipc_wwan->ipc_imem = ipc_imem;
300 /* WWAN core will create a netdev for the default IP MUX channel */
301 if (wwan_register_ops(ipc_wwan->dev, &iosm_wwan_ops, ipc_wwan,
302 IP_MUX_SESSION_DEFAULT)) {
310 void ipc_wwan_deinit(struct iosm_wwan *ipc_wwan)
312 /* This call will remove all child netdev(s) */
313 wwan_unregister_ops(ipc_wwan->dev);