1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2021, MediaTek Inc.
4 * Copyright (c) 2021-2022, Intel Corporation.
7 * Amir Hanania <amir.hanania@intel.com>
8 * Chandrashekar Devegowda <chandrashekar.devegowda@intel.com>
9 * Haijun Liu <haijun.liu@mediatek.com>
10 * Moises Veleta <moises.veleta@intel.com>
11 * Ricardo Martinez <ricardo.martinez@linux.intel.com>
14 * Andy Shevchenko <andriy.shevchenko@linux.intel.com>
15 * Chiranjeevi Rapolu <chiranjeevi.rapolu@intel.com>
16 * Eliot Lee <eliot.lee@intel.com>
17 * Sreehari Kancharla <sreehari.kancharla@intel.com>
20 #include <linux/atomic.h>
21 #include <linux/bitfield.h>
22 #include <linux/dev_printk.h>
23 #include <linux/err.h>
24 #include <linux/gfp.h>
25 #include <linux/minmax.h>
26 #include <linux/netdevice.h>
27 #include <linux/skbuff.h>
28 #include <linux/spinlock.h>
29 #include <linux/string.h>
30 #include <linux/wwan.h>
32 #include "t7xx_port.h"
33 #include "t7xx_port_proxy.h"
34 #include "t7xx_state_monitor.h"
36 static int t7xx_port_ctrl_start(struct wwan_port *port)
38 struct t7xx_port *port_mtk = wwan_port_get_drvdata(port);
40 if (atomic_read(&port_mtk->usage_cnt))
43 atomic_inc(&port_mtk->usage_cnt);
47 static void t7xx_port_ctrl_stop(struct wwan_port *port)
49 struct t7xx_port *port_mtk = wwan_port_get_drvdata(port);
51 atomic_dec(&port_mtk->usage_cnt);
54 static int t7xx_port_ctrl_tx(struct wwan_port *port, struct sk_buff *skb)
56 struct t7xx_port *port_private = wwan_port_get_drvdata(port);
57 const struct t7xx_port_conf *port_conf;
58 struct sk_buff *cur = skb, *cloned;
59 struct t7xx_fsm_ctl *ctl;
60 enum md_state md_state;
63 if (!port_private->chan_enable)
66 port_conf = port_private->port_conf;
67 ctl = port_private->t7xx_dev->md->fsm_ctl;
68 md_state = t7xx_fsm_get_md_state(ctl);
69 if (md_state == MD_STATE_WAITING_FOR_HS1 || md_state == MD_STATE_WAITING_FOR_HS2) {
70 dev_warn(port_private->dev, "Cannot write to %s port when md_state=%d\n",
71 port_conf->name, md_state);
76 cloned = skb_clone(cur, GFP_KERNEL);
77 cloned->len = skb_headlen(cur);
78 ret = t7xx_port_send_skb(port_private, cloned, 0, 0);
80 dev_kfree_skb(cloned);
81 dev_err(port_private->dev, "Write error on %s port, %d\n",
82 port_conf->name, ret);
83 return cnt ? cnt + ret : ret;
87 cur = skb_shinfo(skb)->frag_list;
96 static const struct wwan_port_ops wwan_ops = {
97 .start = t7xx_port_ctrl_start,
98 .stop = t7xx_port_ctrl_stop,
99 .tx = t7xx_port_ctrl_tx,
102 static int t7xx_port_wwan_init(struct t7xx_port *port)
104 port->rx_length_th = RX_QUEUE_MAXLEN;
108 static void t7xx_port_wwan_uninit(struct t7xx_port *port)
110 if (!port->wwan.wwan_port)
113 port->rx_length_th = 0;
114 wwan_remove_port(port->wwan.wwan_port);
115 port->wwan.wwan_port = NULL;
118 static int t7xx_port_wwan_recv_skb(struct t7xx_port *port, struct sk_buff *skb)
120 if (!atomic_read(&port->usage_cnt) || !port->chan_enable) {
121 const struct t7xx_port_conf *port_conf = port->port_conf;
123 dev_kfree_skb_any(skb);
124 dev_err_ratelimited(port->dev, "Port %s is not opened, drop packets\n",
126 /* Dropping skb, caller should not access skb.*/
130 wwan_port_rx(port->wwan.wwan_port, skb);
134 static int t7xx_port_wwan_enable_chl(struct t7xx_port *port)
136 spin_lock(&port->port_update_lock);
137 port->chan_enable = true;
138 spin_unlock(&port->port_update_lock);
143 static int t7xx_port_wwan_disable_chl(struct t7xx_port *port)
145 spin_lock(&port->port_update_lock);
146 port->chan_enable = false;
147 spin_unlock(&port->port_update_lock);
152 static void t7xx_port_wwan_md_state_notify(struct t7xx_port *port, unsigned int state)
154 const struct t7xx_port_conf *port_conf = port->port_conf;
155 unsigned int header_len = sizeof(struct ccci_header);
156 struct wwan_port_caps caps;
158 if (state != MD_STATE_READY)
161 if (!port->wwan.wwan_port) {
162 caps.frag_len = CLDMA_MTU - header_len;
163 caps.headroom_len = header_len;
164 port->wwan.wwan_port = wwan_create_port(port->dev, port_conf->port_type,
165 &wwan_ops, &caps, port);
166 if (IS_ERR(port->wwan.wwan_port))
167 dev_err(port->dev, "Unable to create WWWAN port %s", port_conf->name);
171 struct port_ops wwan_sub_port_ops = {
172 .init = t7xx_port_wwan_init,
173 .recv_skb = t7xx_port_wwan_recv_skb,
174 .uninit = t7xx_port_wwan_uninit,
175 .enable_chl = t7xx_port_wwan_enable_chl,
176 .disable_chl = t7xx_port_wwan_disable_chl,
177 .md_state_notify = t7xx_port_wwan_md_state_notify,