1 From 136d46d2fa27815cc4cc3a57d5e3d54523028768 Mon Sep 17 00:00:00 2001
2 From: Sachin Saxena <sachin.saxena@nxp.com>
3 Date: Thu, 19 Dec 2019 12:57:35 +0530
4 Subject: [PATCH] fsl_qbman: Framework for enabling Link status notification
6 - This will enable link update event notification for
7 user space USDPAA application.
9 Signed-off-by: Sachin Saxena <sachin.saxena@nxp.com>
11 (cherry picked from commit fb53c813a779cc3fc28c8ed3fc8bc0dde24db0ea)
13 drivers/staging/fsl_qbman/Makefile | 4 +
14 drivers/staging/fsl_qbman/fsl_usdpaa.c | 278 ++++++++++++++++++++++++++++++++-
15 include/linux/fsl_usdpaa.h | 32 ++++
16 3 files changed, 313 insertions(+), 1 deletion(-)
18 --- a/drivers/staging/fsl_qbman/Makefile
19 +++ b/drivers/staging/fsl_qbman/Makefile
21 subdir-ccflags-y := -Werror
23 +# Include netcomm SW specific definitions
24 +include $(srctree)/drivers/net/ethernet/freescale/sdk_fman/ncsw_config.mk
25 +ccflags-y += -I$(NET_DPA)
28 obj-$(CONFIG_FSL_SDK_DPA) += dpa_alloc.o
29 obj-$(CONFIG_FSL_SDK_DPA) += qbman_driver.o
30 --- a/drivers/staging/fsl_qbman/fsl_usdpaa.c
31 +++ b/drivers/staging/fsl_qbman/fsl_usdpaa.c
33 #include <linux/slab.h>
34 #include <linux/mman.h>
35 #include <linux/of_reserved_mem.h>
36 +#include <linux/eventfd.h>
37 +#include <linux/fdtable.h>
39 #if !(defined(CONFIG_ARM) || defined(CONFIG_ARM64))
40 #include <mm/mmu_decl.h>
42 #include <linux/fsl_usdpaa.h>
45 +/* Headers requires for
46 + * Link status support
48 +#include <linux/device.h>
49 +#include <linux/of_mdio.h>
51 +#include "dpaa_eth_common.h"
53 +/* Private data for Proxy Interface */
54 +struct dpa_proxy_priv_s {
55 + struct mac_device *mac_dev;
56 + struct eventfd_ctx *efd_ctx;
58 +/* Interface Helpers */
59 +static inline struct device *get_dev_ptr(char *if_name);
60 +static void phy_link_updates(struct net_device *net_dev);
62 +static inline int ioctl_usdpaa_get_link_status(char *if_name);
63 +static int ioctl_en_if_link_status(struct usdpaa_ioctl_link_status *args);
64 +static int ioctl_disable_if_link_status(char *if_name);
66 /* Physical address range of the memory reservation, exported for mm/mem.c */
67 static u64 phys_start;
68 @@ -556,7 +578,6 @@ static bool check_portal_channel(void *c
73 static int usdpaa_release(struct inode *inode, struct file *filp)
76 @@ -1656,6 +1677,220 @@ found:
81 +static inline struct device *get_dev_ptr(char *if_name)
84 + char node[NODE_NAME_LEN];
86 + sprintf(node, "soc:fsl,dpaa:%s",if_name);
87 + dev = bus_find_device_by_name(&platform_bus_type, NULL, node);
89 + pr_err(KBUILD_MODNAME "IF %s not found\n", if_name);
92 + pr_debug("%s: found dev 0x%lX for If %s ,dev->platform_data %p\n",
93 + __func__, (unsigned long)dev,
94 + if_name, dev->platform_data);
99 +/* This function will return Current link status of the device
100 + * '1' if Link is UP, '0' otherwise.
103 + * if_name: Interface node name
106 +static inline int ioctl_usdpaa_get_link_status(char *if_name)
108 + struct net_device *net_dev = NULL;
109 + struct device *dev;
111 + dev = get_dev_ptr(if_name);
114 + net_dev = dev->platform_data;
115 + if (net_dev == NULL)
118 + if (test_bit(__LINK_STATE_NOCARRIER, &net_dev->state))
119 + return 0; /* Link is DOWN */
121 + return 1; /* Link is UP */
125 +/* Link Status Callback Function
126 + * This function will be resgitered to PHY framework to get
127 + * Link update notifications and should be responsible for waking up
128 + * user space task when there is a link update notification.
130 +static void phy_link_updates(struct net_device *net_dev)
132 + struct dpa_proxy_priv_s *priv = NULL;
134 + pr_debug("%s: Link '%s': Speed '%d-Mbps': Autoneg '%d': Duplex '%d'\n",
136 + ioctl_usdpaa_get_link_status(net_dev->name)?"UP":"DOWN",
137 + net_dev->phydev->speed,
138 + net_dev->phydev->autoneg,
139 + net_dev->phydev->duplex);
141 + /* Wake up the user space context to notify PHY update */
142 + priv = netdev_priv(net_dev);
143 + eventfd_signal(priv->efd_ctx, 1);
147 +/* IOCTL handler for enabling Link status request for a given interface
148 + * Input parameters:
149 + * args->if_name: This the network interface node name as defind in
150 + * device tree file. Currently, it has format of
151 + * "ethernet@x" type for each interface.
152 + * args->efd: The eventfd value which should be waked up when
153 + * there is any link update received.
155 +static int ioctl_en_if_link_status(struct usdpaa_ioctl_link_status *args)
157 + struct net_device *net_dev = NULL;
158 + struct dpa_proxy_priv_s *priv = NULL;
159 + struct device *dev;
160 + struct mac_device *mac_dev;
161 + struct proxy_device *proxy_dev;
162 + struct task_struct *userspace_task = NULL;
163 + struct file *efd_file = NULL;
165 + dev = get_dev_ptr(args->if_name);
168 + /* Utilize dev->platform_data to save netdevice
169 + pointer as it will not be registered */
170 + if (dev->platform_data) {
171 + pr_debug("%s: IF %s already initialized\n",
172 + __func__, args->if_name);
173 + /* This will happen when application is not able to initiate
174 + * cleanup in last run. We still need to save the new
177 + net_dev = dev->platform_data;
178 + priv = netdev_priv(net_dev);
180 + /* Get current task context from which IOCTL was called */
181 + userspace_task = current;
184 + efd_file = fcheck_files(userspace_task->files, args->efd);
187 + priv->efd_ctx = eventfd_ctx_fileget(efd_file);
188 + if (!priv->efd_ctx) {
189 + pr_err(KBUILD_MODNAME "get eventfd context failed\n");
190 + /* Free the allocated memory for net device */
191 + dev->platform_data = NULL;
192 + free_netdev(net_dev);
195 + /* Since there will be NO PHY update as link is already setup,
196 + * wake user context once so that current PHY status can
199 + phy_link_updates(net_dev);
203 + proxy_dev = dev_get_drvdata(dev);
204 + mac_dev = proxy_dev->mac_dev;
205 + /* Allocate an dummy net device for proxy interface */
206 + net_dev = alloc_etherdev(sizeof(*priv));
208 + pr_err(KBUILD_MODNAME "alloc_etherdev failed\n");
211 + SET_NETDEV_DEV(net_dev, dev);
212 + priv = netdev_priv(net_dev);
213 + priv->mac_dev = mac_dev;
214 + /* Get current task context from which IOCTL was called */
215 + userspace_task = current;
218 + efd_file = fcheck_files(userspace_task->files, args->efd);
221 + priv->efd_ctx = eventfd_ctx_fileget(efd_file);
223 + if (!priv->efd_ctx) {
224 + pr_err(KBUILD_MODNAME "get eventfd context failed\n");
225 + /* Free the allocated memory for net device */
226 + free_netdev(net_dev);
229 + strncpy(net_dev->name, args->if_name, IF_NAME_MAX_LEN);
230 + dev->platform_data = net_dev;
233 + pr_debug("%s: mac_dev %p cell_index %d\n",
234 + __func__, mac_dev, mac_dev->cell_index);
235 + mac_dev->phy_dev = of_phy_connect(net_dev, mac_dev->phy_node,
236 + phy_link_updates, 0, mac_dev->phy_if);
237 + if (unlikely(mac_dev->phy_dev == NULL) || IS_ERR(mac_dev->phy_dev)) {
238 + pr_err("%s: --------Error in PHY Connect\n", __func__);
239 + /* Free the allocated memory for net device */
240 + free_netdev(net_dev);
243 + net_dev->phydev = mac_dev->phy_dev;
244 + mac_dev->start(mac_dev);
245 + pr_debug("%s: --- PHY connected for %s\n", __func__, args->if_name);
250 +/* IOCTL handler for disabling Link status for a given interface
251 + * Input parameters:
252 + * if_name: This the network interface node name as defind in
253 + * device tree file. Currently, it has format of
254 + * "ethernet@x" type for each interface.
256 +static int ioctl_disable_if_link_status(char *if_name)
258 + struct net_device *net_dev = NULL;
259 + struct device *dev;
260 + struct mac_device *mac_dev;
261 + struct proxy_device *proxy_dev;
262 + struct dpa_proxy_priv_s *priv = NULL;
264 + dev = get_dev_ptr(if_name);
267 + /* Utilize dev->platform_data to save netdevice
268 + pointer as it will not be registered */
269 + if (!dev->platform_data) {
270 + pr_debug("%s: IF %s already Disabled for Link status\n",
271 + __func__, if_name);
275 + net_dev = dev->platform_data;
276 + proxy_dev = dev_get_drvdata(dev);
277 + mac_dev = proxy_dev->mac_dev;
278 + mac_dev->stop(mac_dev);
280 + priv = netdev_priv(net_dev);
281 + eventfd_ctx_put(priv->efd_ctx);
283 + /* This will also deregister the call back */
284 + phy_disconnect(mac_dev->phy_dev);
285 + phy_resume(mac_dev->phy_dev);
287 + free_netdev(net_dev);
288 + dev->platform_data = NULL;
290 + pr_debug("%s: Link status Disabled for %s\n", __func__, if_name);
294 static long usdpaa_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
296 struct ctx *ctx = fp->private_data;
297 @@ -1722,6 +1957,47 @@ static long usdpaa_ioctl(struct file *fp
299 return ioctl_free_raw_portal(fp, ctx, &input);
301 + case USDPAA_IOCTL_ENABLE_LINK_STATUS_INTERRUPT:
303 + struct usdpaa_ioctl_link_status input;
306 + if (copy_from_user(&input, a, sizeof(input)))
308 + ret = ioctl_en_if_link_status(&input);
310 + pr_err("Error(%d) enable link interrupt:IF: %s\n",
311 + ret, input.if_name);
314 + case USDPAA_IOCTL_DISABLE_LINK_STATUS_INTERRUPT:
319 + if (copy_from_user(&input, a, sizeof(input)))
321 + ret = ioctl_disable_if_link_status(input);
323 + pr_err("Error(%d) Disabling link interrupt:IF: %s\n",
327 + case USDPAA_IOCTL_GET_LINK_STATUS:
329 + struct usdpaa_ioctl_link_status_args input;
331 + if (copy_from_user(&input, a, sizeof(input)))
334 + input.link_status = ioctl_usdpaa_get_link_status(input.if_name);
335 + if (input.link_status < 0)
336 + return input.link_status;
337 + if (copy_to_user(a, &input, sizeof(input)))
345 --- a/include/linux/fsl_usdpaa.h
346 +++ b/include/linux/fsl_usdpaa.h
347 @@ -365,6 +365,38 @@ int dpa_alloc_pop(struct dpa_alloc *allo
348 int dpa_alloc_check(struct dpa_alloc *list, u32 id);
349 #endif /* __KERNEL__ */
352 +/************************************
353 + * Link Status support for user space
355 + ************************************/
356 +#define IF_NAME_MAX_LEN 16
357 +#define NODE_NAME_LEN 32
359 +struct usdpaa_ioctl_link_status {
360 + /* network device node name */
361 + char if_name[IF_NAME_MAX_LEN];
362 + /* Eventfd value */
366 +#define USDPAA_IOCTL_ENABLE_LINK_STATUS_INTERRUPT \
367 + _IOW(USDPAA_IOCTL_MAGIC, 0x0E, struct usdpaa_ioctl_link_status)
369 +#define USDPAA_IOCTL_DISABLE_LINK_STATUS_INTERRUPT \
370 + _IOW(USDPAA_IOCTL_MAGIC, 0x0F, char *)
372 +struct usdpaa_ioctl_link_status_args {
373 + /* network device node name */
374 + char if_name[IF_NAME_MAX_LEN];
375 + /* link status(UP/DOWN) */
379 +#define USDPAA_IOCTL_GET_LINK_STATUS \
380 + _IOWR(USDPAA_IOCTL_MAGIC, 0x10, struct usdpaa_ioctl_link_status_args)