xref: /linux/drivers/net/ethernet/ti/netcp_core.c (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
1 /*
2  * Keystone NetCP Core driver
3  *
4  * Copyright (C) 2014 Texas Instruments Incorporated
5  * Authors:	Sandeep Nair <sandeep_n@ti.com>
6  *		Sandeep Paulraj <s-paulraj@ti.com>
7  *		Cyril Chemparathy <cyril@ti.com>
8  *		Santosh Shilimkar <santosh.shilimkar@ti.com>
9  *		Murali Karicheri <m-karicheri2@ti.com>
10  *		Wingman Kwok <w-kwok2@ti.com>
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License as
14  * published by the Free Software Foundation version 2.
15  *
16  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
17  * kind, whether express or implied; without even the implied warranty
18  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  */
21 
22 #include <linux/io.h>
23 #include <linux/module.h>
24 #include <linux/of_net.h>
25 #include <linux/of_address.h>
26 #include <linux/if_vlan.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/platform_device.h>
29 #include <linux/soc/ti/knav_qmss.h>
30 #include <linux/soc/ti/knav_dma.h>
31 
32 #include "netcp.h"
33 
34 #define NETCP_SOP_OFFSET	(NET_IP_ALIGN + NET_SKB_PAD)
35 #define NETCP_NAPI_WEIGHT	64
36 #define NETCP_TX_TIMEOUT	(5 * HZ)
37 #define NETCP_PACKET_SIZE	(ETH_FRAME_LEN + ETH_FCS_LEN)
38 #define NETCP_MIN_PACKET_SIZE	ETH_ZLEN
39 #define NETCP_MAX_MCAST_ADDR	16
40 
41 #define NETCP_EFUSE_REG_INDEX	0
42 
43 #define NETCP_MOD_PROBE_SKIPPED	1
44 #define NETCP_MOD_PROBE_FAILED	2
45 
46 #define NETCP_DEBUG (NETIF_MSG_HW	| NETIF_MSG_WOL		|	\
47 		    NETIF_MSG_DRV	| NETIF_MSG_LINK	|	\
48 		    NETIF_MSG_IFUP	| NETIF_MSG_INTR	|	\
49 		    NETIF_MSG_PROBE	| NETIF_MSG_TIMER	|	\
50 		    NETIF_MSG_IFDOWN	| NETIF_MSG_RX_ERR	|	\
51 		    NETIF_MSG_TX_ERR	| NETIF_MSG_TX_DONE	|	\
52 		    NETIF_MSG_PKTDATA	| NETIF_MSG_TX_QUEUED	|	\
53 		    NETIF_MSG_RX_STATUS)
54 
55 #define NETCP_EFUSE_ADDR_SWAP	2
56 
57 #define knav_queue_get_id(q)	knav_queue_device_control(q, \
58 				KNAV_QUEUE_GET_ID, (unsigned long)NULL)
59 
60 #define knav_queue_enable_notify(q) knav_queue_device_control(q,	\
61 					KNAV_QUEUE_ENABLE_NOTIFY,	\
62 					(unsigned long)NULL)
63 
64 #define knav_queue_disable_notify(q) knav_queue_device_control(q,	\
65 					KNAV_QUEUE_DISABLE_NOTIFY,	\
66 					(unsigned long)NULL)
67 
68 #define knav_queue_get_count(q)	knav_queue_device_control(q, \
69 				KNAV_QUEUE_GET_COUNT, (unsigned long)NULL)
70 
71 #define for_each_netcp_module(module)			\
72 	list_for_each_entry(module, &netcp_modules, module_list)
73 
74 #define for_each_netcp_device_module(netcp_device, inst_modpriv) \
75 	list_for_each_entry(inst_modpriv, \
76 		&((netcp_device)->modpriv_head), inst_list)
77 
78 #define for_each_module(netcp, intf_modpriv)			\
79 	list_for_each_entry(intf_modpriv, &netcp->module_head, intf_list)
80 
81 /* Module management structures */
82 struct netcp_device {
83 	struct list_head	device_list;
84 	struct list_head	interface_head;
85 	struct list_head	modpriv_head;
86 	struct device		*device;
87 };
88 
89 struct netcp_inst_modpriv {
90 	struct netcp_device	*netcp_device;
91 	struct netcp_module	*netcp_module;
92 	struct list_head	inst_list;
93 	void			*module_priv;
94 };
95 
96 struct netcp_intf_modpriv {
97 	struct netcp_intf	*netcp_priv;
98 	struct netcp_module	*netcp_module;
99 	struct list_head	intf_list;
100 	void			*module_priv;
101 };
102 
103 struct netcp_tx_cb {
104 	void	*ts_context;
105 	void	(*txtstamp)(void *context, struct sk_buff *skb);
106 };
107 
108 static LIST_HEAD(netcp_devices);
109 static LIST_HEAD(netcp_modules);
110 static DEFINE_MUTEX(netcp_modules_lock);
111 
112 static int netcp_debug_level = -1;
113 module_param(netcp_debug_level, int, 0);
114 MODULE_PARM_DESC(netcp_debug_level, "Netcp debug level (NETIF_MSG bits) (0=none,...,16=all)");
115 
116 /* Helper functions - Get/Set */
117 static void get_pkt_info(dma_addr_t *buff, u32 *buff_len, dma_addr_t *ndesc,
118 			 struct knav_dma_desc *desc)
119 {
120 	*buff_len = le32_to_cpu(desc->buff_len);
121 	*buff = le32_to_cpu(desc->buff);
122 	*ndesc = le32_to_cpu(desc->next_desc);
123 }
124 
125 static void get_desc_info(u32 *desc_info, u32 *pkt_info,
126 			  struct knav_dma_desc *desc)
127 {
128 	*desc_info = le32_to_cpu(desc->desc_info);
129 	*pkt_info = le32_to_cpu(desc->packet_info);
130 }
131 
132 static u32 get_sw_data(int index, struct knav_dma_desc *desc)
133 {
134 	/* No Endian conversion needed as this data is untouched by hw */
135 	return desc->sw_data[index];
136 }
137 
138 /* use these macros to get sw data */
139 #define GET_SW_DATA0(desc) get_sw_data(0, desc)
140 #define GET_SW_DATA1(desc) get_sw_data(1, desc)
141 #define GET_SW_DATA2(desc) get_sw_data(2, desc)
142 #define GET_SW_DATA3(desc) get_sw_data(3, desc)
143 
144 static void get_org_pkt_info(dma_addr_t *buff, u32 *buff_len,
145 			     struct knav_dma_desc *desc)
146 {
147 	*buff = le32_to_cpu(desc->orig_buff);
148 	*buff_len = le32_to_cpu(desc->orig_len);
149 }
150 
151 static void get_words(dma_addr_t *words, int num_words, __le32 *desc)
152 {
153 	int i;
154 
155 	for (i = 0; i < num_words; i++)
156 		words[i] = le32_to_cpu(desc[i]);
157 }
158 
159 static void set_pkt_info(dma_addr_t buff, u32 buff_len, u32 ndesc,
160 			 struct knav_dma_desc *desc)
161 {
162 	desc->buff_len = cpu_to_le32(buff_len);
163 	desc->buff = cpu_to_le32(buff);
164 	desc->next_desc = cpu_to_le32(ndesc);
165 }
166 
167 static void set_desc_info(u32 desc_info, u32 pkt_info,
168 			  struct knav_dma_desc *desc)
169 {
170 	desc->desc_info = cpu_to_le32(desc_info);
171 	desc->packet_info = cpu_to_le32(pkt_info);
172 }
173 
174 static void set_sw_data(int index, u32 data, struct knav_dma_desc *desc)
175 {
176 	/* No Endian conversion needed as this data is untouched by hw */
177 	desc->sw_data[index] = data;
178 }
179 
180 /* use these macros to set sw data */
181 #define SET_SW_DATA0(data, desc) set_sw_data(0, data, desc)
182 #define SET_SW_DATA1(data, desc) set_sw_data(1, data, desc)
183 #define SET_SW_DATA2(data, desc) set_sw_data(2, data, desc)
184 #define SET_SW_DATA3(data, desc) set_sw_data(3, data, desc)
185 
186 static void set_org_pkt_info(dma_addr_t buff, u32 buff_len,
187 			     struct knav_dma_desc *desc)
188 {
189 	desc->orig_buff = cpu_to_le32(buff);
190 	desc->orig_len = cpu_to_le32(buff_len);
191 }
192 
193 static void set_words(u32 *words, int num_words, __le32 *desc)
194 {
195 	int i;
196 
197 	for (i = 0; i < num_words; i++)
198 		desc[i] = cpu_to_le32(words[i]);
199 }
200 
201 /* Read the e-fuse value as 32 bit values to be endian independent */
202 static int emac_arch_get_mac_addr(char *x, void __iomem *efuse_mac, u32 swap)
203 {
204 	unsigned int addr0, addr1;
205 
206 	addr1 = readl(efuse_mac + 4);
207 	addr0 = readl(efuse_mac);
208 
209 	switch (swap) {
210 	case NETCP_EFUSE_ADDR_SWAP:
211 		addr0 = addr1;
212 		addr1 = readl(efuse_mac);
213 		break;
214 	default:
215 		break;
216 	}
217 
218 	x[0] = (addr1 & 0x0000ff00) >> 8;
219 	x[1] = addr1 & 0x000000ff;
220 	x[2] = (addr0 & 0xff000000) >> 24;
221 	x[3] = (addr0 & 0x00ff0000) >> 16;
222 	x[4] = (addr0 & 0x0000ff00) >> 8;
223 	x[5] = addr0 & 0x000000ff;
224 
225 	return 0;
226 }
227 
228 static const char *netcp_node_name(struct device_node *node)
229 {
230 	const char *name;
231 
232 	if (of_property_read_string(node, "label", &name) < 0)
233 		name = node->name;
234 	if (!name)
235 		name = "unknown";
236 	return name;
237 }
238 
239 /* Module management routines */
240 static int netcp_register_interface(struct netcp_intf *netcp)
241 {
242 	int ret;
243 
244 	ret = register_netdev(netcp->ndev);
245 	if (!ret)
246 		netcp->netdev_registered = true;
247 	return ret;
248 }
249 
250 static int netcp_module_probe(struct netcp_device *netcp_device,
251 			      struct netcp_module *module)
252 {
253 	struct device *dev = netcp_device->device;
254 	struct device_node *devices, *interface, *node = dev->of_node;
255 	struct device_node *child;
256 	struct netcp_inst_modpriv *inst_modpriv;
257 	struct netcp_intf *netcp_intf;
258 	struct netcp_module *tmp;
259 	bool primary_module_registered = false;
260 	int ret;
261 
262 	/* Find this module in the sub-tree for this device */
263 	devices = of_get_child_by_name(node, "netcp-devices");
264 	if (!devices) {
265 		dev_err(dev, "could not find netcp-devices node\n");
266 		return NETCP_MOD_PROBE_SKIPPED;
267 	}
268 
269 	for_each_available_child_of_node(devices, child) {
270 		const char *name = netcp_node_name(child);
271 
272 		if (!strcasecmp(module->name, name))
273 			break;
274 	}
275 
276 	of_node_put(devices);
277 	/* If module not used for this device, skip it */
278 	if (!child) {
279 		dev_warn(dev, "module(%s) not used for device\n", module->name);
280 		return NETCP_MOD_PROBE_SKIPPED;
281 	}
282 
283 	inst_modpriv = devm_kzalloc(dev, sizeof(*inst_modpriv), GFP_KERNEL);
284 	if (!inst_modpriv) {
285 		of_node_put(child);
286 		return -ENOMEM;
287 	}
288 
289 	inst_modpriv->netcp_device = netcp_device;
290 	inst_modpriv->netcp_module = module;
291 	list_add_tail(&inst_modpriv->inst_list, &netcp_device->modpriv_head);
292 
293 	ret = module->probe(netcp_device, dev, child,
294 			    &inst_modpriv->module_priv);
295 	of_node_put(child);
296 	if (ret) {
297 		dev_err(dev, "Probe of module(%s) failed with %d\n",
298 			module->name, ret);
299 		list_del(&inst_modpriv->inst_list);
300 		devm_kfree(dev, inst_modpriv);
301 		return NETCP_MOD_PROBE_FAILED;
302 	}
303 
304 	/* Attach modules only if the primary module is probed */
305 	for_each_netcp_module(tmp) {
306 		if (tmp->primary)
307 			primary_module_registered = true;
308 	}
309 
310 	if (!primary_module_registered)
311 		return 0;
312 
313 	/* Attach module to interfaces */
314 	list_for_each_entry(netcp_intf, &netcp_device->interface_head,
315 			    interface_list) {
316 		struct netcp_intf_modpriv *intf_modpriv;
317 
318 		intf_modpriv = devm_kzalloc(dev, sizeof(*intf_modpriv),
319 					    GFP_KERNEL);
320 		if (!intf_modpriv)
321 			return -ENOMEM;
322 
323 		interface = of_parse_phandle(netcp_intf->node_interface,
324 					     module->name, 0);
325 
326 		if (!interface) {
327 			devm_kfree(dev, intf_modpriv);
328 			continue;
329 		}
330 
331 		intf_modpriv->netcp_priv = netcp_intf;
332 		intf_modpriv->netcp_module = module;
333 		list_add_tail(&intf_modpriv->intf_list,
334 			      &netcp_intf->module_head);
335 
336 		ret = module->attach(inst_modpriv->module_priv,
337 				     netcp_intf->ndev, interface,
338 				     &intf_modpriv->module_priv);
339 		of_node_put(interface);
340 		if (ret) {
341 			dev_dbg(dev, "Attach of module %s declined with %d\n",
342 				module->name, ret);
343 			list_del(&intf_modpriv->intf_list);
344 			devm_kfree(dev, intf_modpriv);
345 			continue;
346 		}
347 	}
348 
349 	/* Now register the interface with netdev */
350 	list_for_each_entry(netcp_intf,
351 			    &netcp_device->interface_head,
352 			    interface_list) {
353 		/* If interface not registered then register now */
354 		if (!netcp_intf->netdev_registered) {
355 			ret = netcp_register_interface(netcp_intf);
356 			if (ret)
357 				return -ENODEV;
358 		}
359 	}
360 	return 0;
361 }
362 
363 int netcp_register_module(struct netcp_module *module)
364 {
365 	struct netcp_device *netcp_device;
366 	struct netcp_module *tmp;
367 	int ret;
368 
369 	if (!module->name) {
370 		WARN(1, "error registering netcp module: no name\n");
371 		return -EINVAL;
372 	}
373 
374 	if (!module->probe) {
375 		WARN(1, "error registering netcp module: no probe\n");
376 		return -EINVAL;
377 	}
378 
379 	mutex_lock(&netcp_modules_lock);
380 
381 	for_each_netcp_module(tmp) {
382 		if (!strcasecmp(tmp->name, module->name)) {
383 			mutex_unlock(&netcp_modules_lock);
384 			return -EEXIST;
385 		}
386 	}
387 	list_add_tail(&module->module_list, &netcp_modules);
388 
389 	list_for_each_entry(netcp_device, &netcp_devices, device_list) {
390 		ret = netcp_module_probe(netcp_device, module);
391 		if (ret < 0)
392 			goto fail;
393 	}
394 	mutex_unlock(&netcp_modules_lock);
395 	return 0;
396 
397 fail:
398 	mutex_unlock(&netcp_modules_lock);
399 	netcp_unregister_module(module);
400 	return ret;
401 }
402 EXPORT_SYMBOL_GPL(netcp_register_module);
403 
404 static void netcp_release_module(struct netcp_device *netcp_device,
405 				 struct netcp_module *module)
406 {
407 	struct netcp_inst_modpriv *inst_modpriv, *inst_tmp;
408 	struct netcp_intf *netcp_intf, *netcp_tmp;
409 	struct device *dev = netcp_device->device;
410 
411 	/* Release the module from each interface */
412 	list_for_each_entry_safe(netcp_intf, netcp_tmp,
413 				 &netcp_device->interface_head,
414 				 interface_list) {
415 		struct netcp_intf_modpriv *intf_modpriv, *intf_tmp;
416 
417 		list_for_each_entry_safe(intf_modpriv, intf_tmp,
418 					 &netcp_intf->module_head,
419 					 intf_list) {
420 			if (intf_modpriv->netcp_module == module) {
421 				module->release(intf_modpriv->module_priv);
422 				list_del(&intf_modpriv->intf_list);
423 				devm_kfree(dev, intf_modpriv);
424 				break;
425 			}
426 		}
427 	}
428 
429 	/* Remove the module from each instance */
430 	list_for_each_entry_safe(inst_modpriv, inst_tmp,
431 				 &netcp_device->modpriv_head, inst_list) {
432 		if (inst_modpriv->netcp_module == module) {
433 			module->remove(netcp_device,
434 				       inst_modpriv->module_priv);
435 			list_del(&inst_modpriv->inst_list);
436 			devm_kfree(dev, inst_modpriv);
437 			break;
438 		}
439 	}
440 }
441 
442 void netcp_unregister_module(struct netcp_module *module)
443 {
444 	struct netcp_device *netcp_device;
445 	struct netcp_module *module_tmp;
446 
447 	mutex_lock(&netcp_modules_lock);
448 
449 	list_for_each_entry(netcp_device, &netcp_devices, device_list) {
450 		netcp_release_module(netcp_device, module);
451 	}
452 
453 	/* Remove the module from the module list */
454 	for_each_netcp_module(module_tmp) {
455 		if (module == module_tmp) {
456 			list_del(&module->module_list);
457 			break;
458 		}
459 	}
460 
461 	mutex_unlock(&netcp_modules_lock);
462 }
463 EXPORT_SYMBOL_GPL(netcp_unregister_module);
464 
465 void *netcp_module_get_intf_data(struct netcp_module *module,
466 				 struct netcp_intf *intf)
467 {
468 	struct netcp_intf_modpriv *intf_modpriv;
469 
470 	list_for_each_entry(intf_modpriv, &intf->module_head, intf_list)
471 		if (intf_modpriv->netcp_module == module)
472 			return intf_modpriv->module_priv;
473 	return NULL;
474 }
475 EXPORT_SYMBOL_GPL(netcp_module_get_intf_data);
476 
477 /* Module TX and RX Hook management */
478 struct netcp_hook_list {
479 	struct list_head	 list;
480 	netcp_hook_rtn		*hook_rtn;
481 	void			*hook_data;
482 	int			 order;
483 };
484 
485 int netcp_register_txhook(struct netcp_intf *netcp_priv, int order,
486 			  netcp_hook_rtn *hook_rtn, void *hook_data)
487 {
488 	struct netcp_hook_list *entry;
489 	struct netcp_hook_list *next;
490 	unsigned long flags;
491 
492 	entry = devm_kzalloc(netcp_priv->dev, sizeof(*entry), GFP_KERNEL);
493 	if (!entry)
494 		return -ENOMEM;
495 
496 	entry->hook_rtn  = hook_rtn;
497 	entry->hook_data = hook_data;
498 	entry->order     = order;
499 
500 	spin_lock_irqsave(&netcp_priv->lock, flags);
501 	list_for_each_entry(next, &netcp_priv->txhook_list_head, list) {
502 		if (next->order > order)
503 			break;
504 	}
505 	__list_add(&entry->list, next->list.prev, &next->list);
506 	spin_unlock_irqrestore(&netcp_priv->lock, flags);
507 
508 	return 0;
509 }
510 EXPORT_SYMBOL_GPL(netcp_register_txhook);
511 
512 int netcp_unregister_txhook(struct netcp_intf *netcp_priv, int order,
513 			    netcp_hook_rtn *hook_rtn, void *hook_data)
514 {
515 	struct netcp_hook_list *next, *n;
516 	unsigned long flags;
517 
518 	spin_lock_irqsave(&netcp_priv->lock, flags);
519 	list_for_each_entry_safe(next, n, &netcp_priv->txhook_list_head, list) {
520 		if ((next->order     == order) &&
521 		    (next->hook_rtn  == hook_rtn) &&
522 		    (next->hook_data == hook_data)) {
523 			list_del(&next->list);
524 			spin_unlock_irqrestore(&netcp_priv->lock, flags);
525 			devm_kfree(netcp_priv->dev, next);
526 			return 0;
527 		}
528 	}
529 	spin_unlock_irqrestore(&netcp_priv->lock, flags);
530 	return -ENOENT;
531 }
532 EXPORT_SYMBOL_GPL(netcp_unregister_txhook);
533 
534 int netcp_register_rxhook(struct netcp_intf *netcp_priv, int order,
535 			  netcp_hook_rtn *hook_rtn, void *hook_data)
536 {
537 	struct netcp_hook_list *entry;
538 	struct netcp_hook_list *next;
539 	unsigned long flags;
540 
541 	entry = devm_kzalloc(netcp_priv->dev, sizeof(*entry), GFP_KERNEL);
542 	if (!entry)
543 		return -ENOMEM;
544 
545 	entry->hook_rtn  = hook_rtn;
546 	entry->hook_data = hook_data;
547 	entry->order     = order;
548 
549 	spin_lock_irqsave(&netcp_priv->lock, flags);
550 	list_for_each_entry(next, &netcp_priv->rxhook_list_head, list) {
551 		if (next->order > order)
552 			break;
553 	}
554 	__list_add(&entry->list, next->list.prev, &next->list);
555 	spin_unlock_irqrestore(&netcp_priv->lock, flags);
556 
557 	return 0;
558 }
559 EXPORT_SYMBOL_GPL(netcp_register_rxhook);
560 
561 int netcp_unregister_rxhook(struct netcp_intf *netcp_priv, int order,
562 			    netcp_hook_rtn *hook_rtn, void *hook_data)
563 {
564 	struct netcp_hook_list *next, *n;
565 	unsigned long flags;
566 
567 	spin_lock_irqsave(&netcp_priv->lock, flags);
568 	list_for_each_entry_safe(next, n, &netcp_priv->rxhook_list_head, list) {
569 		if ((next->order     == order) &&
570 		    (next->hook_rtn  == hook_rtn) &&
571 		    (next->hook_data == hook_data)) {
572 			list_del(&next->list);
573 			spin_unlock_irqrestore(&netcp_priv->lock, flags);
574 			devm_kfree(netcp_priv->dev, next);
575 			return 0;
576 		}
577 	}
578 	spin_unlock_irqrestore(&netcp_priv->lock, flags);
579 
580 	return -ENOENT;
581 }
582 EXPORT_SYMBOL_GPL(netcp_unregister_rxhook);
583 
584 static void netcp_frag_free(bool is_frag, void *ptr)
585 {
586 	if (is_frag)
587 		skb_free_frag(ptr);
588 	else
589 		kfree(ptr);
590 }
591 
592 static void netcp_free_rx_desc_chain(struct netcp_intf *netcp,
593 				     struct knav_dma_desc *desc)
594 {
595 	struct knav_dma_desc *ndesc;
596 	dma_addr_t dma_desc, dma_buf;
597 	unsigned int buf_len, dma_sz = sizeof(*ndesc);
598 	void *buf_ptr;
599 	u32 tmp;
600 
601 	get_words(&dma_desc, 1, &desc->next_desc);
602 
603 	while (dma_desc) {
604 		ndesc = knav_pool_desc_unmap(netcp->rx_pool, dma_desc, dma_sz);
605 		if (unlikely(!ndesc)) {
606 			dev_err(netcp->ndev_dev, "failed to unmap Rx desc\n");
607 			break;
608 		}
609 		get_pkt_info(&dma_buf, &tmp, &dma_desc, ndesc);
610 		/* warning!!!! We are retrieving the virtual ptr in the sw_data
611 		 * field as a 32bit value. Will not work on 64bit machines
612 		 */
613 		buf_ptr = (void *)GET_SW_DATA0(ndesc);
614 		buf_len = (int)GET_SW_DATA1(desc);
615 		dma_unmap_page(netcp->dev, dma_buf, PAGE_SIZE, DMA_FROM_DEVICE);
616 		__free_page(buf_ptr);
617 		knav_pool_desc_put(netcp->rx_pool, desc);
618 	}
619 	/* warning!!!! We are retrieving the virtual ptr in the sw_data
620 	 * field as a 32bit value. Will not work on 64bit machines
621 	 */
622 	buf_ptr = (void *)GET_SW_DATA0(desc);
623 	buf_len = (int)GET_SW_DATA1(desc);
624 
625 	if (buf_ptr)
626 		netcp_frag_free(buf_len <= PAGE_SIZE, buf_ptr);
627 	knav_pool_desc_put(netcp->rx_pool, desc);
628 }
629 
630 static void netcp_empty_rx_queue(struct netcp_intf *netcp)
631 {
632 	struct netcp_stats *rx_stats = &netcp->stats;
633 	struct knav_dma_desc *desc;
634 	unsigned int dma_sz;
635 	dma_addr_t dma;
636 
637 	for (; ;) {
638 		dma = knav_queue_pop(netcp->rx_queue, &dma_sz);
639 		if (!dma)
640 			break;
641 
642 		desc = knav_pool_desc_unmap(netcp->rx_pool, dma, dma_sz);
643 		if (unlikely(!desc)) {
644 			dev_err(netcp->ndev_dev, "%s: failed to unmap Rx desc\n",
645 				__func__);
646 			rx_stats->rx_errors++;
647 			continue;
648 		}
649 		netcp_free_rx_desc_chain(netcp, desc);
650 		rx_stats->rx_dropped++;
651 	}
652 }
653 
654 static int netcp_process_one_rx_packet(struct netcp_intf *netcp)
655 {
656 	struct netcp_stats *rx_stats = &netcp->stats;
657 	unsigned int dma_sz, buf_len, org_buf_len;
658 	struct knav_dma_desc *desc, *ndesc;
659 	unsigned int pkt_sz = 0, accum_sz;
660 	struct netcp_hook_list *rx_hook;
661 	dma_addr_t dma_desc, dma_buff;
662 	struct netcp_packet p_info;
663 	struct sk_buff *skb;
664 	void *org_buf_ptr;
665 	u32 tmp;
666 
667 	dma_desc = knav_queue_pop(netcp->rx_queue, &dma_sz);
668 	if (!dma_desc)
669 		return -1;
670 
671 	desc = knav_pool_desc_unmap(netcp->rx_pool, dma_desc, dma_sz);
672 	if (unlikely(!desc)) {
673 		dev_err(netcp->ndev_dev, "failed to unmap Rx desc\n");
674 		return 0;
675 	}
676 
677 	get_pkt_info(&dma_buff, &buf_len, &dma_desc, desc);
678 	/* warning!!!! We are retrieving the virtual ptr in the sw_data
679 	 * field as a 32bit value. Will not work on 64bit machines
680 	 */
681 	org_buf_ptr = (void *)GET_SW_DATA0(desc);
682 	org_buf_len = (int)GET_SW_DATA1(desc);
683 
684 	if (unlikely(!org_buf_ptr)) {
685 		dev_err(netcp->ndev_dev, "NULL bufptr in desc\n");
686 		goto free_desc;
687 	}
688 
689 	pkt_sz &= KNAV_DMA_DESC_PKT_LEN_MASK;
690 	accum_sz = buf_len;
691 	dma_unmap_single(netcp->dev, dma_buff, buf_len, DMA_FROM_DEVICE);
692 
693 	/* Build a new sk_buff for the primary buffer */
694 	skb = build_skb(org_buf_ptr, org_buf_len);
695 	if (unlikely(!skb)) {
696 		dev_err(netcp->ndev_dev, "build_skb() failed\n");
697 		goto free_desc;
698 	}
699 
700 	/* update data, tail and len */
701 	skb_reserve(skb, NETCP_SOP_OFFSET);
702 	__skb_put(skb, buf_len);
703 
704 	/* Fill in the page fragment list */
705 	while (dma_desc) {
706 		struct page *page;
707 
708 		ndesc = knav_pool_desc_unmap(netcp->rx_pool, dma_desc, dma_sz);
709 		if (unlikely(!ndesc)) {
710 			dev_err(netcp->ndev_dev, "failed to unmap Rx desc\n");
711 			goto free_desc;
712 		}
713 
714 		get_pkt_info(&dma_buff, &buf_len, &dma_desc, ndesc);
715 		/* warning!!!! We are retrieving the virtual ptr in the sw_data
716 		 * field as a 32bit value. Will not work on 64bit machines
717 		 */
718 		page = (struct page *)GET_SW_DATA0(desc);
719 
720 		if (likely(dma_buff && buf_len && page)) {
721 			dma_unmap_page(netcp->dev, dma_buff, PAGE_SIZE,
722 				       DMA_FROM_DEVICE);
723 		} else {
724 			dev_err(netcp->ndev_dev, "Bad Rx desc dma_buff(%pad), len(%d), page(%p)\n",
725 				&dma_buff, buf_len, page);
726 			goto free_desc;
727 		}
728 
729 		skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
730 				offset_in_page(dma_buff), buf_len, PAGE_SIZE);
731 		accum_sz += buf_len;
732 
733 		/* Free the descriptor */
734 		knav_pool_desc_put(netcp->rx_pool, ndesc);
735 	}
736 
737 	/* check for packet len and warn */
738 	if (unlikely(pkt_sz != accum_sz))
739 		dev_dbg(netcp->ndev_dev, "mismatch in packet size(%d) & sum of fragments(%d)\n",
740 			pkt_sz, accum_sz);
741 
742 	/* Newer version of the Ethernet switch can trim the Ethernet FCS
743 	 * from the packet and is indicated in hw_cap. So trim it only for
744 	 * older h/w
745 	 */
746 	if (!(netcp->hw_cap & ETH_SW_CAN_REMOVE_ETH_FCS))
747 		__pskb_trim(skb, skb->len - ETH_FCS_LEN);
748 
749 	/* Call each of the RX hooks */
750 	p_info.skb = skb;
751 	skb->dev = netcp->ndev;
752 	p_info.rxtstamp_complete = false;
753 	get_desc_info(&tmp, &p_info.eflags, desc);
754 	p_info.epib = desc->epib;
755 	p_info.psdata = (u32 __force *)desc->psdata;
756 	p_info.eflags = ((p_info.eflags >> KNAV_DMA_DESC_EFLAGS_SHIFT) &
757 			 KNAV_DMA_DESC_EFLAGS_MASK);
758 	list_for_each_entry(rx_hook, &netcp->rxhook_list_head, list) {
759 		int ret;
760 
761 		ret = rx_hook->hook_rtn(rx_hook->order, rx_hook->hook_data,
762 					&p_info);
763 		if (unlikely(ret)) {
764 			dev_err(netcp->ndev_dev, "RX hook %d failed: %d\n",
765 				rx_hook->order, ret);
766 			/* Free the primary descriptor */
767 			rx_stats->rx_dropped++;
768 			knav_pool_desc_put(netcp->rx_pool, desc);
769 			dev_kfree_skb(skb);
770 			return 0;
771 		}
772 	}
773 	/* Free the primary descriptor */
774 	knav_pool_desc_put(netcp->rx_pool, desc);
775 
776 	u64_stats_update_begin(&rx_stats->syncp_rx);
777 	rx_stats->rx_packets++;
778 	rx_stats->rx_bytes += skb->len;
779 	u64_stats_update_end(&rx_stats->syncp_rx);
780 
781 	/* push skb up the stack */
782 	skb->protocol = eth_type_trans(skb, netcp->ndev);
783 	netif_receive_skb(skb);
784 	return 0;
785 
786 free_desc:
787 	netcp_free_rx_desc_chain(netcp, desc);
788 	rx_stats->rx_errors++;
789 	return 0;
790 }
791 
792 static int netcp_process_rx_packets(struct netcp_intf *netcp,
793 				    unsigned int budget)
794 {
795 	int i;
796 
797 	for (i = 0; (i < budget) && !netcp_process_one_rx_packet(netcp); i++)
798 		;
799 	return i;
800 }
801 
802 /* Release descriptors and attached buffers from Rx FDQ */
803 static void netcp_free_rx_buf(struct netcp_intf *netcp, int fdq)
804 {
805 	struct knav_dma_desc *desc;
806 	unsigned int buf_len, dma_sz;
807 	dma_addr_t dma;
808 	void *buf_ptr;
809 
810 	/* Allocate descriptor */
811 	while ((dma = knav_queue_pop(netcp->rx_fdq[fdq], &dma_sz))) {
812 		desc = knav_pool_desc_unmap(netcp->rx_pool, dma, dma_sz);
813 		if (unlikely(!desc)) {
814 			dev_err(netcp->ndev_dev, "failed to unmap Rx desc\n");
815 			continue;
816 		}
817 
818 		get_org_pkt_info(&dma, &buf_len, desc);
819 		/* warning!!!! We are retrieving the virtual ptr in the sw_data
820 		 * field as a 32bit value. Will not work on 64bit machines
821 		 */
822 		buf_ptr = (void *)GET_SW_DATA0(desc);
823 
824 		if (unlikely(!dma)) {
825 			dev_err(netcp->ndev_dev, "NULL orig_buff in desc\n");
826 			knav_pool_desc_put(netcp->rx_pool, desc);
827 			continue;
828 		}
829 
830 		if (unlikely(!buf_ptr)) {
831 			dev_err(netcp->ndev_dev, "NULL bufptr in desc\n");
832 			knav_pool_desc_put(netcp->rx_pool, desc);
833 			continue;
834 		}
835 
836 		if (fdq == 0) {
837 			dma_unmap_single(netcp->dev, dma, buf_len,
838 					 DMA_FROM_DEVICE);
839 			netcp_frag_free((buf_len <= PAGE_SIZE), buf_ptr);
840 		} else {
841 			dma_unmap_page(netcp->dev, dma, buf_len,
842 				       DMA_FROM_DEVICE);
843 			__free_page(buf_ptr);
844 		}
845 
846 		knav_pool_desc_put(netcp->rx_pool, desc);
847 	}
848 }
849 
850 static void netcp_rxpool_free(struct netcp_intf *netcp)
851 {
852 	int i;
853 
854 	for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN &&
855 	     !IS_ERR_OR_NULL(netcp->rx_fdq[i]); i++)
856 		netcp_free_rx_buf(netcp, i);
857 
858 	if (knav_pool_count(netcp->rx_pool) != netcp->rx_pool_size)
859 		dev_err(netcp->ndev_dev, "Lost Rx (%d) descriptors\n",
860 			netcp->rx_pool_size - knav_pool_count(netcp->rx_pool));
861 
862 	knav_pool_destroy(netcp->rx_pool);
863 	netcp->rx_pool = NULL;
864 }
865 
866 static int netcp_allocate_rx_buf(struct netcp_intf *netcp, int fdq)
867 {
868 	struct knav_dma_desc *hwdesc;
869 	unsigned int buf_len, dma_sz;
870 	u32 desc_info, pkt_info;
871 	struct page *page;
872 	dma_addr_t dma;
873 	void *bufptr;
874 	u32 sw_data[2];
875 
876 	/* Allocate descriptor */
877 	hwdesc = knav_pool_desc_get(netcp->rx_pool);
878 	if (IS_ERR_OR_NULL(hwdesc)) {
879 		dev_dbg(netcp->ndev_dev, "out of rx pool desc\n");
880 		return -ENOMEM;
881 	}
882 
883 	if (likely(fdq == 0)) {
884 		unsigned int primary_buf_len;
885 		/* Allocate a primary receive queue entry */
886 		buf_len = NETCP_PACKET_SIZE + NETCP_SOP_OFFSET;
887 		primary_buf_len = SKB_DATA_ALIGN(buf_len) +
888 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
889 
890 		bufptr = netdev_alloc_frag(primary_buf_len);
891 		sw_data[1] = primary_buf_len;
892 
893 		if (unlikely(!bufptr)) {
894 			dev_warn_ratelimited(netcp->ndev_dev,
895 					     "Primary RX buffer alloc failed\n");
896 			goto fail;
897 		}
898 		dma = dma_map_single(netcp->dev, bufptr, buf_len,
899 				     DMA_TO_DEVICE);
900 		if (unlikely(dma_mapping_error(netcp->dev, dma)))
901 			goto fail;
902 
903 		/* warning!!!! We are saving the virtual ptr in the sw_data
904 		 * field as a 32bit value. Will not work on 64bit machines
905 		 */
906 		sw_data[0] = (u32)bufptr;
907 	} else {
908 		/* Allocate a secondary receive queue entry */
909 		page = alloc_page(GFP_ATOMIC | GFP_DMA | __GFP_COLD);
910 		if (unlikely(!page)) {
911 			dev_warn_ratelimited(netcp->ndev_dev, "Secondary page alloc failed\n");
912 			goto fail;
913 		}
914 		buf_len = PAGE_SIZE;
915 		dma = dma_map_page(netcp->dev, page, 0, buf_len, DMA_TO_DEVICE);
916 		/* warning!!!! We are saving the virtual ptr in the sw_data
917 		 * field as a 32bit value. Will not work on 64bit machines
918 		 */
919 		sw_data[0] = (u32)page;
920 		sw_data[1] = 0;
921 	}
922 
923 	desc_info =  KNAV_DMA_DESC_PS_INFO_IN_DESC;
924 	desc_info |= buf_len & KNAV_DMA_DESC_PKT_LEN_MASK;
925 	pkt_info =  KNAV_DMA_DESC_HAS_EPIB;
926 	pkt_info |= KNAV_DMA_NUM_PS_WORDS << KNAV_DMA_DESC_PSLEN_SHIFT;
927 	pkt_info |= (netcp->rx_queue_id & KNAV_DMA_DESC_RETQ_MASK) <<
928 		    KNAV_DMA_DESC_RETQ_SHIFT;
929 	set_org_pkt_info(dma, buf_len, hwdesc);
930 	SET_SW_DATA0(sw_data[0], hwdesc);
931 	SET_SW_DATA1(sw_data[1], hwdesc);
932 	set_desc_info(desc_info, pkt_info, hwdesc);
933 
934 	/* Push to FDQs */
935 	knav_pool_desc_map(netcp->rx_pool, hwdesc, sizeof(*hwdesc), &dma,
936 			   &dma_sz);
937 	knav_queue_push(netcp->rx_fdq[fdq], dma, sizeof(*hwdesc), 0);
938 	return 0;
939 
940 fail:
941 	knav_pool_desc_put(netcp->rx_pool, hwdesc);
942 	return -ENOMEM;
943 }
944 
945 /* Refill Rx FDQ with descriptors & attached buffers */
946 static void netcp_rxpool_refill(struct netcp_intf *netcp)
947 {
948 	u32 fdq_deficit[KNAV_DMA_FDQ_PER_CHAN] = {0};
949 	int i, ret = 0;
950 
951 	/* Calculate the FDQ deficit and refill */
952 	for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN && netcp->rx_fdq[i]; i++) {
953 		fdq_deficit[i] = netcp->rx_queue_depths[i] -
954 				 knav_queue_get_count(netcp->rx_fdq[i]);
955 
956 		while (fdq_deficit[i]-- && !ret)
957 			ret = netcp_allocate_rx_buf(netcp, i);
958 	} /* end for fdqs */
959 }
960 
961 /* NAPI poll */
962 static int netcp_rx_poll(struct napi_struct *napi, int budget)
963 {
964 	struct netcp_intf *netcp = container_of(napi, struct netcp_intf,
965 						rx_napi);
966 	unsigned int packets;
967 
968 	packets = netcp_process_rx_packets(netcp, budget);
969 
970 	netcp_rxpool_refill(netcp);
971 	if (packets < budget) {
972 		napi_complete_done(&netcp->rx_napi, packets);
973 		knav_queue_enable_notify(netcp->rx_queue);
974 	}
975 
976 	return packets;
977 }
978 
979 static void netcp_rx_notify(void *arg)
980 {
981 	struct netcp_intf *netcp = arg;
982 
983 	knav_queue_disable_notify(netcp->rx_queue);
984 	napi_schedule(&netcp->rx_napi);
985 }
986 
987 static void netcp_free_tx_desc_chain(struct netcp_intf *netcp,
988 				     struct knav_dma_desc *desc,
989 				     unsigned int desc_sz)
990 {
991 	struct knav_dma_desc *ndesc = desc;
992 	dma_addr_t dma_desc, dma_buf;
993 	unsigned int buf_len;
994 
995 	while (ndesc) {
996 		get_pkt_info(&dma_buf, &buf_len, &dma_desc, ndesc);
997 
998 		if (dma_buf && buf_len)
999 			dma_unmap_single(netcp->dev, dma_buf, buf_len,
1000 					 DMA_TO_DEVICE);
1001 		else
1002 			dev_warn(netcp->ndev_dev, "bad Tx desc buf(%pad), len(%d)\n",
1003 				 &dma_buf, buf_len);
1004 
1005 		knav_pool_desc_put(netcp->tx_pool, ndesc);
1006 		ndesc = NULL;
1007 		if (dma_desc) {
1008 			ndesc = knav_pool_desc_unmap(netcp->tx_pool, dma_desc,
1009 						     desc_sz);
1010 			if (!ndesc)
1011 				dev_err(netcp->ndev_dev, "failed to unmap Tx desc\n");
1012 		}
1013 	}
1014 }
1015 
1016 static int netcp_process_tx_compl_packets(struct netcp_intf *netcp,
1017 					  unsigned int budget)
1018 {
1019 	struct netcp_stats *tx_stats = &netcp->stats;
1020 	struct knav_dma_desc *desc;
1021 	struct netcp_tx_cb *tx_cb;
1022 	struct sk_buff *skb;
1023 	unsigned int dma_sz;
1024 	dma_addr_t dma;
1025 	int pkts = 0;
1026 
1027 	while (budget--) {
1028 		dma = knav_queue_pop(netcp->tx_compl_q, &dma_sz);
1029 		if (!dma)
1030 			break;
1031 		desc = knav_pool_desc_unmap(netcp->tx_pool, dma, dma_sz);
1032 		if (unlikely(!desc)) {
1033 			dev_err(netcp->ndev_dev, "failed to unmap Tx desc\n");
1034 			tx_stats->tx_errors++;
1035 			continue;
1036 		}
1037 
1038 		/* warning!!!! We are retrieving the virtual ptr in the sw_data
1039 		 * field as a 32bit value. Will not work on 64bit machines
1040 		 */
1041 		skb = (struct sk_buff *)GET_SW_DATA0(desc);
1042 		netcp_free_tx_desc_chain(netcp, desc, dma_sz);
1043 		if (!skb) {
1044 			dev_err(netcp->ndev_dev, "No skb in Tx desc\n");
1045 			tx_stats->tx_errors++;
1046 			continue;
1047 		}
1048 
1049 		tx_cb = (struct netcp_tx_cb *)skb->cb;
1050 		if (tx_cb->txtstamp)
1051 			tx_cb->txtstamp(tx_cb->ts_context, skb);
1052 
1053 		if (netif_subqueue_stopped(netcp->ndev, skb) &&
1054 		    netif_running(netcp->ndev) &&
1055 		    (knav_pool_count(netcp->tx_pool) >
1056 		    netcp->tx_resume_threshold)) {
1057 			u16 subqueue = skb_get_queue_mapping(skb);
1058 
1059 			netif_wake_subqueue(netcp->ndev, subqueue);
1060 		}
1061 
1062 		u64_stats_update_begin(&tx_stats->syncp_tx);
1063 		tx_stats->tx_packets++;
1064 		tx_stats->tx_bytes += skb->len;
1065 		u64_stats_update_end(&tx_stats->syncp_tx);
1066 		dev_kfree_skb(skb);
1067 		pkts++;
1068 	}
1069 	return pkts;
1070 }
1071 
1072 static int netcp_tx_poll(struct napi_struct *napi, int budget)
1073 {
1074 	int packets;
1075 	struct netcp_intf *netcp = container_of(napi, struct netcp_intf,
1076 						tx_napi);
1077 
1078 	packets = netcp_process_tx_compl_packets(netcp, budget);
1079 	if (packets < budget) {
1080 		napi_complete(&netcp->tx_napi);
1081 		knav_queue_enable_notify(netcp->tx_compl_q);
1082 	}
1083 
1084 	return packets;
1085 }
1086 
1087 static void netcp_tx_notify(void *arg)
1088 {
1089 	struct netcp_intf *netcp = arg;
1090 
1091 	knav_queue_disable_notify(netcp->tx_compl_q);
1092 	napi_schedule(&netcp->tx_napi);
1093 }
1094 
1095 static struct knav_dma_desc*
1096 netcp_tx_map_skb(struct sk_buff *skb, struct netcp_intf *netcp)
1097 {
1098 	struct knav_dma_desc *desc, *ndesc, *pdesc;
1099 	unsigned int pkt_len = skb_headlen(skb);
1100 	struct device *dev = netcp->dev;
1101 	dma_addr_t dma_addr;
1102 	unsigned int dma_sz;
1103 	int i;
1104 
1105 	/* Map the linear buffer */
1106 	dma_addr = dma_map_single(dev, skb->data, pkt_len, DMA_TO_DEVICE);
1107 	if (unlikely(dma_mapping_error(dev, dma_addr))) {
1108 		dev_err(netcp->ndev_dev, "Failed to map skb buffer\n");
1109 		return NULL;
1110 	}
1111 
1112 	desc = knav_pool_desc_get(netcp->tx_pool);
1113 	if (IS_ERR_OR_NULL(desc)) {
1114 		dev_err(netcp->ndev_dev, "out of TX desc\n");
1115 		dma_unmap_single(dev, dma_addr, pkt_len, DMA_TO_DEVICE);
1116 		return NULL;
1117 	}
1118 
1119 	set_pkt_info(dma_addr, pkt_len, 0, desc);
1120 	if (skb_is_nonlinear(skb)) {
1121 		prefetchw(skb_shinfo(skb));
1122 	} else {
1123 		desc->next_desc = 0;
1124 		goto upd_pkt_len;
1125 	}
1126 
1127 	pdesc = desc;
1128 
1129 	/* Handle the case where skb is fragmented in pages */
1130 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1131 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1132 		struct page *page = skb_frag_page(frag);
1133 		u32 page_offset = frag->page_offset;
1134 		u32 buf_len = skb_frag_size(frag);
1135 		dma_addr_t desc_dma;
1136 		u32 desc_dma_32;
1137 		u32 pkt_info;
1138 
1139 		dma_addr = dma_map_page(dev, page, page_offset, buf_len,
1140 					DMA_TO_DEVICE);
1141 		if (unlikely(!dma_addr)) {
1142 			dev_err(netcp->ndev_dev, "Failed to map skb page\n");
1143 			goto free_descs;
1144 		}
1145 
1146 		ndesc = knav_pool_desc_get(netcp->tx_pool);
1147 		if (IS_ERR_OR_NULL(ndesc)) {
1148 			dev_err(netcp->ndev_dev, "out of TX desc for frags\n");
1149 			dma_unmap_page(dev, dma_addr, buf_len, DMA_TO_DEVICE);
1150 			goto free_descs;
1151 		}
1152 
1153 		desc_dma = knav_pool_desc_virt_to_dma(netcp->tx_pool, ndesc);
1154 		pkt_info =
1155 			(netcp->tx_compl_qid & KNAV_DMA_DESC_RETQ_MASK) <<
1156 				KNAV_DMA_DESC_RETQ_SHIFT;
1157 		set_pkt_info(dma_addr, buf_len, 0, ndesc);
1158 		desc_dma_32 = (u32)desc_dma;
1159 		set_words(&desc_dma_32, 1, &pdesc->next_desc);
1160 		pkt_len += buf_len;
1161 		if (pdesc != desc)
1162 			knav_pool_desc_map(netcp->tx_pool, pdesc,
1163 					   sizeof(*pdesc), &desc_dma, &dma_sz);
1164 		pdesc = ndesc;
1165 	}
1166 	if (pdesc != desc)
1167 		knav_pool_desc_map(netcp->tx_pool, pdesc, sizeof(*pdesc),
1168 				   &dma_addr, &dma_sz);
1169 
1170 	/* frag list based linkage is not supported for now. */
1171 	if (skb_shinfo(skb)->frag_list) {
1172 		dev_err_ratelimited(netcp->ndev_dev, "NETIF_F_FRAGLIST not supported\n");
1173 		goto free_descs;
1174 	}
1175 
1176 upd_pkt_len:
1177 	WARN_ON(pkt_len != skb->len);
1178 
1179 	pkt_len &= KNAV_DMA_DESC_PKT_LEN_MASK;
1180 	set_words(&pkt_len, 1, &desc->desc_info);
1181 	return desc;
1182 
1183 free_descs:
1184 	netcp_free_tx_desc_chain(netcp, desc, sizeof(*desc));
1185 	return NULL;
1186 }
1187 
1188 static int netcp_tx_submit_skb(struct netcp_intf *netcp,
1189 			       struct sk_buff *skb,
1190 			       struct knav_dma_desc *desc)
1191 {
1192 	struct netcp_tx_pipe *tx_pipe = NULL;
1193 	struct netcp_hook_list *tx_hook;
1194 	struct netcp_packet p_info;
1195 	struct netcp_tx_cb *tx_cb;
1196 	unsigned int dma_sz;
1197 	dma_addr_t dma;
1198 	u32 tmp = 0;
1199 	int ret = 0;
1200 
1201 	p_info.netcp = netcp;
1202 	p_info.skb = skb;
1203 	p_info.tx_pipe = NULL;
1204 	p_info.psdata_len = 0;
1205 	p_info.ts_context = NULL;
1206 	p_info.txtstamp = NULL;
1207 	p_info.epib = desc->epib;
1208 	p_info.psdata = (u32 __force *)desc->psdata;
1209 	memset(p_info.epib, 0, KNAV_DMA_NUM_EPIB_WORDS * sizeof(__le32));
1210 
1211 	/* Find out where to inject the packet for transmission */
1212 	list_for_each_entry(tx_hook, &netcp->txhook_list_head, list) {
1213 		ret = tx_hook->hook_rtn(tx_hook->order, tx_hook->hook_data,
1214 					&p_info);
1215 		if (unlikely(ret != 0)) {
1216 			dev_err(netcp->ndev_dev, "TX hook %d rejected the packet with reason(%d)\n",
1217 				tx_hook->order, ret);
1218 			ret = (ret < 0) ? ret : NETDEV_TX_OK;
1219 			goto out;
1220 		}
1221 	}
1222 
1223 	/* Make sure some TX hook claimed the packet */
1224 	tx_pipe = p_info.tx_pipe;
1225 	if (!tx_pipe) {
1226 		dev_err(netcp->ndev_dev, "No TX hook claimed the packet!\n");
1227 		ret = -ENXIO;
1228 		goto out;
1229 	}
1230 
1231 	tx_cb = (struct netcp_tx_cb *)skb->cb;
1232 	tx_cb->ts_context = p_info.ts_context;
1233 	tx_cb->txtstamp = p_info.txtstamp;
1234 
1235 	/* update descriptor */
1236 	if (p_info.psdata_len) {
1237 		/* psdata points to both native-endian and device-endian data */
1238 		__le32 *psdata = (void __force *)p_info.psdata;
1239 
1240 		set_words((u32 *)psdata +
1241 			  (KNAV_DMA_NUM_PS_WORDS - p_info.psdata_len),
1242 			  p_info.psdata_len, psdata);
1243 		tmp |= (p_info.psdata_len & KNAV_DMA_DESC_PSLEN_MASK) <<
1244 			KNAV_DMA_DESC_PSLEN_SHIFT;
1245 	}
1246 
1247 	tmp |= KNAV_DMA_DESC_HAS_EPIB |
1248 		((netcp->tx_compl_qid & KNAV_DMA_DESC_RETQ_MASK) <<
1249 		KNAV_DMA_DESC_RETQ_SHIFT);
1250 
1251 	if (!(tx_pipe->flags & SWITCH_TO_PORT_IN_TAGINFO)) {
1252 		tmp |= ((tx_pipe->switch_to_port & KNAV_DMA_DESC_PSFLAG_MASK) <<
1253 			KNAV_DMA_DESC_PSFLAG_SHIFT);
1254 	}
1255 
1256 	set_words(&tmp, 1, &desc->packet_info);
1257 	/* warning!!!! We are saving the virtual ptr in the sw_data
1258 	 * field as a 32bit value. Will not work on 64bit machines
1259 	 */
1260 	SET_SW_DATA0((u32)skb, desc);
1261 
1262 	if (tx_pipe->flags & SWITCH_TO_PORT_IN_TAGINFO) {
1263 		tmp = tx_pipe->switch_to_port;
1264 		set_words(&tmp, 1, &desc->tag_info);
1265 	}
1266 
1267 	/* submit packet descriptor */
1268 	ret = knav_pool_desc_map(netcp->tx_pool, desc, sizeof(*desc), &dma,
1269 				 &dma_sz);
1270 	if (unlikely(ret)) {
1271 		dev_err(netcp->ndev_dev, "%s() failed to map desc\n", __func__);
1272 		ret = -ENOMEM;
1273 		goto out;
1274 	}
1275 	skb_tx_timestamp(skb);
1276 	knav_queue_push(tx_pipe->dma_queue, dma, dma_sz, 0);
1277 
1278 out:
1279 	return ret;
1280 }
1281 
1282 /* Submit the packet */
1283 static int netcp_ndo_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1284 {
1285 	struct netcp_intf *netcp = netdev_priv(ndev);
1286 	struct netcp_stats *tx_stats = &netcp->stats;
1287 	int subqueue = skb_get_queue_mapping(skb);
1288 	struct knav_dma_desc *desc;
1289 	int desc_count, ret = 0;
1290 
1291 	if (unlikely(skb->len <= 0)) {
1292 		dev_kfree_skb(skb);
1293 		return NETDEV_TX_OK;
1294 	}
1295 
1296 	if (unlikely(skb->len < NETCP_MIN_PACKET_SIZE)) {
1297 		ret = skb_padto(skb, NETCP_MIN_PACKET_SIZE);
1298 		if (ret < 0) {
1299 			/* If we get here, the skb has already been dropped */
1300 			dev_warn(netcp->ndev_dev, "padding failed (%d), packet dropped\n",
1301 				 ret);
1302 			tx_stats->tx_dropped++;
1303 			return ret;
1304 		}
1305 		skb->len = NETCP_MIN_PACKET_SIZE;
1306 	}
1307 
1308 	desc = netcp_tx_map_skb(skb, netcp);
1309 	if (unlikely(!desc)) {
1310 		netif_stop_subqueue(ndev, subqueue);
1311 		ret = -ENOBUFS;
1312 		goto drop;
1313 	}
1314 
1315 	ret = netcp_tx_submit_skb(netcp, skb, desc);
1316 	if (ret)
1317 		goto drop;
1318 
1319 	/* Check Tx pool count & stop subqueue if needed */
1320 	desc_count = knav_pool_count(netcp->tx_pool);
1321 	if (desc_count < netcp->tx_pause_threshold) {
1322 		dev_dbg(netcp->ndev_dev, "pausing tx, count(%d)\n", desc_count);
1323 		netif_stop_subqueue(ndev, subqueue);
1324 	}
1325 	return NETDEV_TX_OK;
1326 
1327 drop:
1328 	tx_stats->tx_dropped++;
1329 	if (desc)
1330 		netcp_free_tx_desc_chain(netcp, desc, sizeof(*desc));
1331 	dev_kfree_skb(skb);
1332 	return ret;
1333 }
1334 
1335 int netcp_txpipe_close(struct netcp_tx_pipe *tx_pipe)
1336 {
1337 	if (tx_pipe->dma_channel) {
1338 		knav_dma_close_channel(tx_pipe->dma_channel);
1339 		tx_pipe->dma_channel = NULL;
1340 	}
1341 	return 0;
1342 }
1343 EXPORT_SYMBOL_GPL(netcp_txpipe_close);
1344 
1345 int netcp_txpipe_open(struct netcp_tx_pipe *tx_pipe)
1346 {
1347 	struct device *dev = tx_pipe->netcp_device->device;
1348 	struct knav_dma_cfg config;
1349 	int ret = 0;
1350 	u8 name[16];
1351 
1352 	memset(&config, 0, sizeof(config));
1353 	config.direction = DMA_MEM_TO_DEV;
1354 	config.u.tx.filt_einfo = false;
1355 	config.u.tx.filt_pswords = false;
1356 	config.u.tx.priority = DMA_PRIO_MED_L;
1357 
1358 	tx_pipe->dma_channel = knav_dma_open_channel(dev,
1359 				tx_pipe->dma_chan_name, &config);
1360 	if (IS_ERR_OR_NULL(tx_pipe->dma_channel)) {
1361 		dev_err(dev, "failed opening tx chan(%s)\n",
1362 			tx_pipe->dma_chan_name);
1363 		goto err;
1364 	}
1365 
1366 	snprintf(name, sizeof(name), "tx-pipe-%s", dev_name(dev));
1367 	tx_pipe->dma_queue = knav_queue_open(name, tx_pipe->dma_queue_id,
1368 					     KNAV_QUEUE_SHARED);
1369 	if (IS_ERR(tx_pipe->dma_queue)) {
1370 		dev_err(dev, "Could not open DMA queue for channel \"%s\": %d\n",
1371 			name, ret);
1372 		ret = PTR_ERR(tx_pipe->dma_queue);
1373 		goto err;
1374 	}
1375 
1376 	dev_dbg(dev, "opened tx pipe %s\n", name);
1377 	return 0;
1378 
1379 err:
1380 	if (!IS_ERR_OR_NULL(tx_pipe->dma_channel))
1381 		knav_dma_close_channel(tx_pipe->dma_channel);
1382 	tx_pipe->dma_channel = NULL;
1383 	return ret;
1384 }
1385 EXPORT_SYMBOL_GPL(netcp_txpipe_open);
1386 
1387 int netcp_txpipe_init(struct netcp_tx_pipe *tx_pipe,
1388 		      struct netcp_device *netcp_device,
1389 		      const char *dma_chan_name, unsigned int dma_queue_id)
1390 {
1391 	memset(tx_pipe, 0, sizeof(*tx_pipe));
1392 	tx_pipe->netcp_device = netcp_device;
1393 	tx_pipe->dma_chan_name = dma_chan_name;
1394 	tx_pipe->dma_queue_id = dma_queue_id;
1395 	return 0;
1396 }
1397 EXPORT_SYMBOL_GPL(netcp_txpipe_init);
1398 
1399 static struct netcp_addr *netcp_addr_find(struct netcp_intf *netcp,
1400 					  const u8 *addr,
1401 					  enum netcp_addr_type type)
1402 {
1403 	struct netcp_addr *naddr;
1404 
1405 	list_for_each_entry(naddr, &netcp->addr_list, node) {
1406 		if (naddr->type != type)
1407 			continue;
1408 		if (addr && memcmp(addr, naddr->addr, ETH_ALEN))
1409 			continue;
1410 		return naddr;
1411 	}
1412 
1413 	return NULL;
1414 }
1415 
1416 static struct netcp_addr *netcp_addr_add(struct netcp_intf *netcp,
1417 					 const u8 *addr,
1418 					 enum netcp_addr_type type)
1419 {
1420 	struct netcp_addr *naddr;
1421 
1422 	naddr = devm_kmalloc(netcp->dev, sizeof(*naddr), GFP_ATOMIC);
1423 	if (!naddr)
1424 		return NULL;
1425 
1426 	naddr->type = type;
1427 	naddr->flags = 0;
1428 	naddr->netcp = netcp;
1429 	if (addr)
1430 		ether_addr_copy(naddr->addr, addr);
1431 	else
1432 		eth_zero_addr(naddr->addr);
1433 	list_add_tail(&naddr->node, &netcp->addr_list);
1434 
1435 	return naddr;
1436 }
1437 
1438 static void netcp_addr_del(struct netcp_intf *netcp, struct netcp_addr *naddr)
1439 {
1440 	list_del(&naddr->node);
1441 	devm_kfree(netcp->dev, naddr);
1442 }
1443 
1444 static void netcp_addr_clear_mark(struct netcp_intf *netcp)
1445 {
1446 	struct netcp_addr *naddr;
1447 
1448 	list_for_each_entry(naddr, &netcp->addr_list, node)
1449 		naddr->flags = 0;
1450 }
1451 
1452 static void netcp_addr_add_mark(struct netcp_intf *netcp, const u8 *addr,
1453 				enum netcp_addr_type type)
1454 {
1455 	struct netcp_addr *naddr;
1456 
1457 	naddr = netcp_addr_find(netcp, addr, type);
1458 	if (naddr) {
1459 		naddr->flags |= ADDR_VALID;
1460 		return;
1461 	}
1462 
1463 	naddr = netcp_addr_add(netcp, addr, type);
1464 	if (!WARN_ON(!naddr))
1465 		naddr->flags |= ADDR_NEW;
1466 }
1467 
1468 static void netcp_addr_sweep_del(struct netcp_intf *netcp)
1469 {
1470 	struct netcp_addr *naddr, *tmp;
1471 	struct netcp_intf_modpriv *priv;
1472 	struct netcp_module *module;
1473 	int error;
1474 
1475 	list_for_each_entry_safe(naddr, tmp, &netcp->addr_list, node) {
1476 		if (naddr->flags & (ADDR_VALID | ADDR_NEW))
1477 			continue;
1478 		dev_dbg(netcp->ndev_dev, "deleting address %pM, type %x\n",
1479 			naddr->addr, naddr->type);
1480 		for_each_module(netcp, priv) {
1481 			module = priv->netcp_module;
1482 			if (!module->del_addr)
1483 				continue;
1484 			error = module->del_addr(priv->module_priv,
1485 						 naddr);
1486 			WARN_ON(error);
1487 		}
1488 		netcp_addr_del(netcp, naddr);
1489 	}
1490 }
1491 
1492 static void netcp_addr_sweep_add(struct netcp_intf *netcp)
1493 {
1494 	struct netcp_addr *naddr, *tmp;
1495 	struct netcp_intf_modpriv *priv;
1496 	struct netcp_module *module;
1497 	int error;
1498 
1499 	list_for_each_entry_safe(naddr, tmp, &netcp->addr_list, node) {
1500 		if (!(naddr->flags & ADDR_NEW))
1501 			continue;
1502 		dev_dbg(netcp->ndev_dev, "adding address %pM, type %x\n",
1503 			naddr->addr, naddr->type);
1504 
1505 		for_each_module(netcp, priv) {
1506 			module = priv->netcp_module;
1507 			if (!module->add_addr)
1508 				continue;
1509 			error = module->add_addr(priv->module_priv, naddr);
1510 			WARN_ON(error);
1511 		}
1512 	}
1513 }
1514 
1515 static void netcp_set_rx_mode(struct net_device *ndev)
1516 {
1517 	struct netcp_intf *netcp = netdev_priv(ndev);
1518 	struct netdev_hw_addr *ndev_addr;
1519 	bool promisc;
1520 
1521 	promisc = (ndev->flags & IFF_PROMISC ||
1522 		   ndev->flags & IFF_ALLMULTI ||
1523 		   netdev_mc_count(ndev) > NETCP_MAX_MCAST_ADDR);
1524 
1525 	spin_lock(&netcp->lock);
1526 	/* first clear all marks */
1527 	netcp_addr_clear_mark(netcp);
1528 
1529 	/* next add new entries, mark existing ones */
1530 	netcp_addr_add_mark(netcp, ndev->broadcast, ADDR_BCAST);
1531 	for_each_dev_addr(ndev, ndev_addr)
1532 		netcp_addr_add_mark(netcp, ndev_addr->addr, ADDR_DEV);
1533 	netdev_for_each_uc_addr(ndev_addr, ndev)
1534 		netcp_addr_add_mark(netcp, ndev_addr->addr, ADDR_UCAST);
1535 	netdev_for_each_mc_addr(ndev_addr, ndev)
1536 		netcp_addr_add_mark(netcp, ndev_addr->addr, ADDR_MCAST);
1537 
1538 	if (promisc)
1539 		netcp_addr_add_mark(netcp, NULL, ADDR_ANY);
1540 
1541 	/* finally sweep and callout into modules */
1542 	netcp_addr_sweep_del(netcp);
1543 	netcp_addr_sweep_add(netcp);
1544 	spin_unlock(&netcp->lock);
1545 }
1546 
1547 static void netcp_free_navigator_resources(struct netcp_intf *netcp)
1548 {
1549 	int i;
1550 
1551 	if (netcp->rx_channel) {
1552 		knav_dma_close_channel(netcp->rx_channel);
1553 		netcp->rx_channel = NULL;
1554 	}
1555 
1556 	if (!IS_ERR_OR_NULL(netcp->rx_pool))
1557 		netcp_rxpool_free(netcp);
1558 
1559 	if (!IS_ERR_OR_NULL(netcp->rx_queue)) {
1560 		knav_queue_close(netcp->rx_queue);
1561 		netcp->rx_queue = NULL;
1562 	}
1563 
1564 	for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN &&
1565 	     !IS_ERR_OR_NULL(netcp->rx_fdq[i]) ; ++i) {
1566 		knav_queue_close(netcp->rx_fdq[i]);
1567 		netcp->rx_fdq[i] = NULL;
1568 	}
1569 
1570 	if (!IS_ERR_OR_NULL(netcp->tx_compl_q)) {
1571 		knav_queue_close(netcp->tx_compl_q);
1572 		netcp->tx_compl_q = NULL;
1573 	}
1574 
1575 	if (!IS_ERR_OR_NULL(netcp->tx_pool)) {
1576 		knav_pool_destroy(netcp->tx_pool);
1577 		netcp->tx_pool = NULL;
1578 	}
1579 }
1580 
1581 static int netcp_setup_navigator_resources(struct net_device *ndev)
1582 {
1583 	struct netcp_intf *netcp = netdev_priv(ndev);
1584 	struct knav_queue_notify_config notify_cfg;
1585 	struct knav_dma_cfg config;
1586 	u32 last_fdq = 0;
1587 	u8 name[16];
1588 	int ret;
1589 	int i;
1590 
1591 	/* Create Rx/Tx descriptor pools */
1592 	snprintf(name, sizeof(name), "rx-pool-%s", ndev->name);
1593 	netcp->rx_pool = knav_pool_create(name, netcp->rx_pool_size,
1594 						netcp->rx_pool_region_id);
1595 	if (IS_ERR_OR_NULL(netcp->rx_pool)) {
1596 		dev_err(netcp->ndev_dev, "Couldn't create rx pool\n");
1597 		ret = PTR_ERR(netcp->rx_pool);
1598 		goto fail;
1599 	}
1600 
1601 	snprintf(name, sizeof(name), "tx-pool-%s", ndev->name);
1602 	netcp->tx_pool = knav_pool_create(name, netcp->tx_pool_size,
1603 						netcp->tx_pool_region_id);
1604 	if (IS_ERR_OR_NULL(netcp->tx_pool)) {
1605 		dev_err(netcp->ndev_dev, "Couldn't create tx pool\n");
1606 		ret = PTR_ERR(netcp->tx_pool);
1607 		goto fail;
1608 	}
1609 
1610 	/* open Tx completion queue */
1611 	snprintf(name, sizeof(name), "tx-compl-%s", ndev->name);
1612 	netcp->tx_compl_q = knav_queue_open(name, netcp->tx_compl_qid, 0);
1613 	if (IS_ERR(netcp->tx_compl_q)) {
1614 		ret = PTR_ERR(netcp->tx_compl_q);
1615 		goto fail;
1616 	}
1617 	netcp->tx_compl_qid = knav_queue_get_id(netcp->tx_compl_q);
1618 
1619 	/* Set notification for Tx completion */
1620 	notify_cfg.fn = netcp_tx_notify;
1621 	notify_cfg.fn_arg = netcp;
1622 	ret = knav_queue_device_control(netcp->tx_compl_q,
1623 					KNAV_QUEUE_SET_NOTIFIER,
1624 					(unsigned long)&notify_cfg);
1625 	if (ret)
1626 		goto fail;
1627 
1628 	knav_queue_disable_notify(netcp->tx_compl_q);
1629 
1630 	/* open Rx completion queue */
1631 	snprintf(name, sizeof(name), "rx-compl-%s", ndev->name);
1632 	netcp->rx_queue = knav_queue_open(name, netcp->rx_queue_id, 0);
1633 	if (IS_ERR(netcp->rx_queue)) {
1634 		ret = PTR_ERR(netcp->rx_queue);
1635 		goto fail;
1636 	}
1637 	netcp->rx_queue_id = knav_queue_get_id(netcp->rx_queue);
1638 
1639 	/* Set notification for Rx completion */
1640 	notify_cfg.fn = netcp_rx_notify;
1641 	notify_cfg.fn_arg = netcp;
1642 	ret = knav_queue_device_control(netcp->rx_queue,
1643 					KNAV_QUEUE_SET_NOTIFIER,
1644 					(unsigned long)&notify_cfg);
1645 	if (ret)
1646 		goto fail;
1647 
1648 	knav_queue_disable_notify(netcp->rx_queue);
1649 
1650 	/* open Rx FDQs */
1651 	for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN && netcp->rx_queue_depths[i];
1652 	     ++i) {
1653 		snprintf(name, sizeof(name), "rx-fdq-%s-%d", ndev->name, i);
1654 		netcp->rx_fdq[i] = knav_queue_open(name, KNAV_QUEUE_GP, 0);
1655 		if (IS_ERR(netcp->rx_fdq[i])) {
1656 			ret = PTR_ERR(netcp->rx_fdq[i]);
1657 			goto fail;
1658 		}
1659 	}
1660 
1661 	memset(&config, 0, sizeof(config));
1662 	config.direction		= DMA_DEV_TO_MEM;
1663 	config.u.rx.einfo_present	= true;
1664 	config.u.rx.psinfo_present	= true;
1665 	config.u.rx.err_mode		= DMA_DROP;
1666 	config.u.rx.desc_type		= DMA_DESC_HOST;
1667 	config.u.rx.psinfo_at_sop	= false;
1668 	config.u.rx.sop_offset		= NETCP_SOP_OFFSET;
1669 	config.u.rx.dst_q		= netcp->rx_queue_id;
1670 	config.u.rx.thresh		= DMA_THRESH_NONE;
1671 
1672 	for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN; ++i) {
1673 		if (netcp->rx_fdq[i])
1674 			last_fdq = knav_queue_get_id(netcp->rx_fdq[i]);
1675 		config.u.rx.fdq[i] = last_fdq;
1676 	}
1677 
1678 	netcp->rx_channel = knav_dma_open_channel(netcp->netcp_device->device,
1679 					netcp->dma_chan_name, &config);
1680 	if (IS_ERR_OR_NULL(netcp->rx_channel)) {
1681 		dev_err(netcp->ndev_dev, "failed opening rx chan(%s\n",
1682 			netcp->dma_chan_name);
1683 		goto fail;
1684 	}
1685 
1686 	dev_dbg(netcp->ndev_dev, "opened RX channel: %p\n", netcp->rx_channel);
1687 	return 0;
1688 
1689 fail:
1690 	netcp_free_navigator_resources(netcp);
1691 	return ret;
1692 }
1693 
1694 /* Open the device */
1695 static int netcp_ndo_open(struct net_device *ndev)
1696 {
1697 	struct netcp_intf *netcp = netdev_priv(ndev);
1698 	struct netcp_intf_modpriv *intf_modpriv;
1699 	struct netcp_module *module;
1700 	int ret;
1701 
1702 	netif_carrier_off(ndev);
1703 	ret = netcp_setup_navigator_resources(ndev);
1704 	if (ret) {
1705 		dev_err(netcp->ndev_dev, "Failed to setup navigator resources\n");
1706 		goto fail;
1707 	}
1708 
1709 	for_each_module(netcp, intf_modpriv) {
1710 		module = intf_modpriv->netcp_module;
1711 		if (module->open) {
1712 			ret = module->open(intf_modpriv->module_priv, ndev);
1713 			if (ret != 0) {
1714 				dev_err(netcp->ndev_dev, "module open failed\n");
1715 				goto fail_open;
1716 			}
1717 		}
1718 	}
1719 
1720 	napi_enable(&netcp->rx_napi);
1721 	napi_enable(&netcp->tx_napi);
1722 	knav_queue_enable_notify(netcp->tx_compl_q);
1723 	knav_queue_enable_notify(netcp->rx_queue);
1724 	netcp_rxpool_refill(netcp);
1725 	netif_tx_wake_all_queues(ndev);
1726 	dev_dbg(netcp->ndev_dev, "netcp device %s opened\n", ndev->name);
1727 	return 0;
1728 
1729 fail_open:
1730 	for_each_module(netcp, intf_modpriv) {
1731 		module = intf_modpriv->netcp_module;
1732 		if (module->close)
1733 			module->close(intf_modpriv->module_priv, ndev);
1734 	}
1735 
1736 fail:
1737 	netcp_free_navigator_resources(netcp);
1738 	return ret;
1739 }
1740 
1741 /* Close the device */
1742 static int netcp_ndo_stop(struct net_device *ndev)
1743 {
1744 	struct netcp_intf *netcp = netdev_priv(ndev);
1745 	struct netcp_intf_modpriv *intf_modpriv;
1746 	struct netcp_module *module;
1747 	int err = 0;
1748 
1749 	netif_tx_stop_all_queues(ndev);
1750 	netif_carrier_off(ndev);
1751 	netcp_addr_clear_mark(netcp);
1752 	netcp_addr_sweep_del(netcp);
1753 	knav_queue_disable_notify(netcp->rx_queue);
1754 	knav_queue_disable_notify(netcp->tx_compl_q);
1755 	napi_disable(&netcp->rx_napi);
1756 	napi_disable(&netcp->tx_napi);
1757 
1758 	for_each_module(netcp, intf_modpriv) {
1759 		module = intf_modpriv->netcp_module;
1760 		if (module->close) {
1761 			err = module->close(intf_modpriv->module_priv, ndev);
1762 			if (err != 0)
1763 				dev_err(netcp->ndev_dev, "Close failed\n");
1764 		}
1765 	}
1766 
1767 	/* Recycle Rx descriptors from completion queue */
1768 	netcp_empty_rx_queue(netcp);
1769 
1770 	/* Recycle Tx descriptors from completion queue */
1771 	netcp_process_tx_compl_packets(netcp, netcp->tx_pool_size);
1772 
1773 	if (knav_pool_count(netcp->tx_pool) != netcp->tx_pool_size)
1774 		dev_err(netcp->ndev_dev, "Lost (%d) Tx descs\n",
1775 			netcp->tx_pool_size - knav_pool_count(netcp->tx_pool));
1776 
1777 	netcp_free_navigator_resources(netcp);
1778 	dev_dbg(netcp->ndev_dev, "netcp device %s stopped\n", ndev->name);
1779 	return 0;
1780 }
1781 
1782 static int netcp_ndo_ioctl(struct net_device *ndev,
1783 			   struct ifreq *req, int cmd)
1784 {
1785 	struct netcp_intf *netcp = netdev_priv(ndev);
1786 	struct netcp_intf_modpriv *intf_modpriv;
1787 	struct netcp_module *module;
1788 	int ret = -1, err = -EOPNOTSUPP;
1789 
1790 	if (!netif_running(ndev))
1791 		return -EINVAL;
1792 
1793 	for_each_module(netcp, intf_modpriv) {
1794 		module = intf_modpriv->netcp_module;
1795 		if (!module->ioctl)
1796 			continue;
1797 
1798 		err = module->ioctl(intf_modpriv->module_priv, req, cmd);
1799 		if ((err < 0) && (err != -EOPNOTSUPP)) {
1800 			ret = err;
1801 			goto out;
1802 		}
1803 		if (err == 0)
1804 			ret = err;
1805 	}
1806 
1807 out:
1808 	return (ret == 0) ? 0 : err;
1809 }
1810 
1811 static void netcp_ndo_tx_timeout(struct net_device *ndev)
1812 {
1813 	struct netcp_intf *netcp = netdev_priv(ndev);
1814 	unsigned int descs = knav_pool_count(netcp->tx_pool);
1815 
1816 	dev_err(netcp->ndev_dev, "transmit timed out tx descs(%d)\n", descs);
1817 	netcp_process_tx_compl_packets(netcp, netcp->tx_pool_size);
1818 	netif_trans_update(ndev);
1819 	netif_tx_wake_all_queues(ndev);
1820 }
1821 
1822 static int netcp_rx_add_vid(struct net_device *ndev, __be16 proto, u16 vid)
1823 {
1824 	struct netcp_intf *netcp = netdev_priv(ndev);
1825 	struct netcp_intf_modpriv *intf_modpriv;
1826 	struct netcp_module *module;
1827 	unsigned long flags;
1828 	int err = 0;
1829 
1830 	dev_dbg(netcp->ndev_dev, "adding rx vlan id: %d\n", vid);
1831 
1832 	spin_lock_irqsave(&netcp->lock, flags);
1833 	for_each_module(netcp, intf_modpriv) {
1834 		module = intf_modpriv->netcp_module;
1835 		if ((module->add_vid) && (vid != 0)) {
1836 			err = module->add_vid(intf_modpriv->module_priv, vid);
1837 			if (err != 0) {
1838 				dev_err(netcp->ndev_dev, "Could not add vlan id = %d\n",
1839 					vid);
1840 				break;
1841 			}
1842 		}
1843 	}
1844 	spin_unlock_irqrestore(&netcp->lock, flags);
1845 
1846 	return err;
1847 }
1848 
1849 static int netcp_rx_kill_vid(struct net_device *ndev, __be16 proto, u16 vid)
1850 {
1851 	struct netcp_intf *netcp = netdev_priv(ndev);
1852 	struct netcp_intf_modpriv *intf_modpriv;
1853 	struct netcp_module *module;
1854 	unsigned long flags;
1855 	int err = 0;
1856 
1857 	dev_dbg(netcp->ndev_dev, "removing rx vlan id: %d\n", vid);
1858 
1859 	spin_lock_irqsave(&netcp->lock, flags);
1860 	for_each_module(netcp, intf_modpriv) {
1861 		module = intf_modpriv->netcp_module;
1862 		if (module->del_vid) {
1863 			err = module->del_vid(intf_modpriv->module_priv, vid);
1864 			if (err != 0) {
1865 				dev_err(netcp->ndev_dev, "Could not delete vlan id = %d\n",
1866 					vid);
1867 				break;
1868 			}
1869 		}
1870 	}
1871 	spin_unlock_irqrestore(&netcp->lock, flags);
1872 	return err;
1873 }
1874 
1875 static u16 netcp_select_queue(struct net_device *dev, struct sk_buff *skb,
1876 			      void *accel_priv,
1877 			      select_queue_fallback_t fallback)
1878 {
1879 	return 0;
1880 }
1881 
1882 static int netcp_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
1883 			  struct tc_to_netdev *tc)
1884 {
1885 	int i;
1886 
1887 	/* setup tc must be called under rtnl lock */
1888 	ASSERT_RTNL();
1889 
1890 	if (tc->type != TC_SETUP_MQPRIO)
1891 		return -EINVAL;
1892 
1893 	/* Sanity-check the number of traffic classes requested */
1894 	if ((dev->real_num_tx_queues <= 1) ||
1895 	    (dev->real_num_tx_queues < tc->tc))
1896 		return -EINVAL;
1897 
1898 	/* Configure traffic class to queue mappings */
1899 	if (tc->tc) {
1900 		netdev_set_num_tc(dev, tc->tc);
1901 		for (i = 0; i < tc->tc; i++)
1902 			netdev_set_tc_queue(dev, i, 1, i);
1903 	} else {
1904 		netdev_reset_tc(dev);
1905 	}
1906 
1907 	return 0;
1908 }
1909 
1910 static void
1911 netcp_get_stats(struct net_device *ndev, struct rtnl_link_stats64 *stats)
1912 {
1913 	struct netcp_intf *netcp = netdev_priv(ndev);
1914 	struct netcp_stats *p = &netcp->stats;
1915 	u64 rxpackets, rxbytes, txpackets, txbytes;
1916 	unsigned int start;
1917 
1918 	do {
1919 		start = u64_stats_fetch_begin_irq(&p->syncp_rx);
1920 		rxpackets       = p->rx_packets;
1921 		rxbytes         = p->rx_bytes;
1922 	} while (u64_stats_fetch_retry_irq(&p->syncp_rx, start));
1923 
1924 	do {
1925 		start = u64_stats_fetch_begin_irq(&p->syncp_tx);
1926 		txpackets       = p->tx_packets;
1927 		txbytes         = p->tx_bytes;
1928 	} while (u64_stats_fetch_retry_irq(&p->syncp_tx, start));
1929 
1930 	stats->rx_packets = rxpackets;
1931 	stats->rx_bytes = rxbytes;
1932 	stats->tx_packets = txpackets;
1933 	stats->tx_bytes = txbytes;
1934 
1935 	/* The following are stored as 32 bit */
1936 	stats->rx_errors = p->rx_errors;
1937 	stats->rx_dropped = p->rx_dropped;
1938 	stats->tx_dropped = p->tx_dropped;
1939 }
1940 
1941 static const struct net_device_ops netcp_netdev_ops = {
1942 	.ndo_open		= netcp_ndo_open,
1943 	.ndo_stop		= netcp_ndo_stop,
1944 	.ndo_start_xmit		= netcp_ndo_start_xmit,
1945 	.ndo_set_rx_mode	= netcp_set_rx_mode,
1946 	.ndo_do_ioctl           = netcp_ndo_ioctl,
1947 	.ndo_get_stats64        = netcp_get_stats,
1948 	.ndo_set_mac_address	= eth_mac_addr,
1949 	.ndo_validate_addr	= eth_validate_addr,
1950 	.ndo_vlan_rx_add_vid	= netcp_rx_add_vid,
1951 	.ndo_vlan_rx_kill_vid	= netcp_rx_kill_vid,
1952 	.ndo_tx_timeout		= netcp_ndo_tx_timeout,
1953 	.ndo_select_queue	= netcp_select_queue,
1954 	.ndo_setup_tc		= netcp_setup_tc,
1955 };
1956 
1957 static int netcp_create_interface(struct netcp_device *netcp_device,
1958 				  struct device_node *node_interface)
1959 {
1960 	struct device *dev = netcp_device->device;
1961 	struct device_node *node = dev->of_node;
1962 	struct netcp_intf *netcp;
1963 	struct net_device *ndev;
1964 	resource_size_t size;
1965 	struct resource res;
1966 	void __iomem *efuse = NULL;
1967 	u32 efuse_mac = 0;
1968 	const void *mac_addr;
1969 	u8 efuse_mac_addr[6];
1970 	u32 temp[2];
1971 	int ret = 0;
1972 
1973 	ndev = alloc_etherdev_mqs(sizeof(*netcp), 1, 1);
1974 	if (!ndev) {
1975 		dev_err(dev, "Error allocating netdev\n");
1976 		return -ENOMEM;
1977 	}
1978 
1979 	ndev->features |= NETIF_F_SG;
1980 	ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1981 	ndev->hw_features = ndev->features;
1982 	ndev->vlan_features |=  NETIF_F_SG;
1983 
1984 	/* MTU range: 68 - 9486 */
1985 	ndev->min_mtu = ETH_MIN_MTU;
1986 	ndev->max_mtu = NETCP_MAX_FRAME_SIZE - (ETH_HLEN + ETH_FCS_LEN);
1987 
1988 	netcp = netdev_priv(ndev);
1989 	spin_lock_init(&netcp->lock);
1990 	INIT_LIST_HEAD(&netcp->module_head);
1991 	INIT_LIST_HEAD(&netcp->txhook_list_head);
1992 	INIT_LIST_HEAD(&netcp->rxhook_list_head);
1993 	INIT_LIST_HEAD(&netcp->addr_list);
1994 	u64_stats_init(&netcp->stats.syncp_rx);
1995 	u64_stats_init(&netcp->stats.syncp_tx);
1996 	netcp->netcp_device = netcp_device;
1997 	netcp->dev = netcp_device->device;
1998 	netcp->ndev = ndev;
1999 	netcp->ndev_dev  = &ndev->dev;
2000 	netcp->msg_enable = netif_msg_init(netcp_debug_level, NETCP_DEBUG);
2001 	netcp->tx_pause_threshold = MAX_SKB_FRAGS;
2002 	netcp->tx_resume_threshold = netcp->tx_pause_threshold;
2003 	netcp->node_interface = node_interface;
2004 
2005 	ret = of_property_read_u32(node_interface, "efuse-mac", &efuse_mac);
2006 	if (efuse_mac) {
2007 		if (of_address_to_resource(node, NETCP_EFUSE_REG_INDEX, &res)) {
2008 			dev_err(dev, "could not find efuse-mac reg resource\n");
2009 			ret = -ENODEV;
2010 			goto quit;
2011 		}
2012 		size = resource_size(&res);
2013 
2014 		if (!devm_request_mem_region(dev, res.start, size,
2015 					     dev_name(dev))) {
2016 			dev_err(dev, "could not reserve resource\n");
2017 			ret = -ENOMEM;
2018 			goto quit;
2019 		}
2020 
2021 		efuse = devm_ioremap_nocache(dev, res.start, size);
2022 		if (!efuse) {
2023 			dev_err(dev, "could not map resource\n");
2024 			devm_release_mem_region(dev, res.start, size);
2025 			ret = -ENOMEM;
2026 			goto quit;
2027 		}
2028 
2029 		emac_arch_get_mac_addr(efuse_mac_addr, efuse, efuse_mac);
2030 		if (is_valid_ether_addr(efuse_mac_addr))
2031 			ether_addr_copy(ndev->dev_addr, efuse_mac_addr);
2032 		else
2033 			random_ether_addr(ndev->dev_addr);
2034 
2035 		devm_iounmap(dev, efuse);
2036 		devm_release_mem_region(dev, res.start, size);
2037 	} else {
2038 		mac_addr = of_get_mac_address(node_interface);
2039 		if (mac_addr)
2040 			ether_addr_copy(ndev->dev_addr, mac_addr);
2041 		else
2042 			random_ether_addr(ndev->dev_addr);
2043 	}
2044 
2045 	ret = of_property_read_string(node_interface, "rx-channel",
2046 				      &netcp->dma_chan_name);
2047 	if (ret < 0) {
2048 		dev_err(dev, "missing \"rx-channel\" parameter\n");
2049 		ret = -ENODEV;
2050 		goto quit;
2051 	}
2052 
2053 	ret = of_property_read_u32(node_interface, "rx-queue",
2054 				   &netcp->rx_queue_id);
2055 	if (ret < 0) {
2056 		dev_warn(dev, "missing \"rx-queue\" parameter\n");
2057 		netcp->rx_queue_id = KNAV_QUEUE_QPEND;
2058 	}
2059 
2060 	ret = of_property_read_u32_array(node_interface, "rx-queue-depth",
2061 					 netcp->rx_queue_depths,
2062 					 KNAV_DMA_FDQ_PER_CHAN);
2063 	if (ret < 0) {
2064 		dev_err(dev, "missing \"rx-queue-depth\" parameter\n");
2065 		netcp->rx_queue_depths[0] = 128;
2066 	}
2067 
2068 	ret = of_property_read_u32_array(node_interface, "rx-pool", temp, 2);
2069 	if (ret < 0) {
2070 		dev_err(dev, "missing \"rx-pool\" parameter\n");
2071 		ret = -ENODEV;
2072 		goto quit;
2073 	}
2074 	netcp->rx_pool_size = temp[0];
2075 	netcp->rx_pool_region_id = temp[1];
2076 
2077 	ret = of_property_read_u32_array(node_interface, "tx-pool", temp, 2);
2078 	if (ret < 0) {
2079 		dev_err(dev, "missing \"tx-pool\" parameter\n");
2080 		ret = -ENODEV;
2081 		goto quit;
2082 	}
2083 	netcp->tx_pool_size = temp[0];
2084 	netcp->tx_pool_region_id = temp[1];
2085 
2086 	if (netcp->tx_pool_size < MAX_SKB_FRAGS) {
2087 		dev_err(dev, "tx-pool size too small, must be atleast(%ld)\n",
2088 			MAX_SKB_FRAGS);
2089 		ret = -ENODEV;
2090 		goto quit;
2091 	}
2092 
2093 	ret = of_property_read_u32(node_interface, "tx-completion-queue",
2094 				   &netcp->tx_compl_qid);
2095 	if (ret < 0) {
2096 		dev_warn(dev, "missing \"tx-completion-queue\" parameter\n");
2097 		netcp->tx_compl_qid = KNAV_QUEUE_QPEND;
2098 	}
2099 
2100 	/* NAPI register */
2101 	netif_napi_add(ndev, &netcp->rx_napi, netcp_rx_poll, NETCP_NAPI_WEIGHT);
2102 	netif_tx_napi_add(ndev, &netcp->tx_napi, netcp_tx_poll, NETCP_NAPI_WEIGHT);
2103 
2104 	/* Register the network device */
2105 	ndev->dev_id		= 0;
2106 	ndev->watchdog_timeo	= NETCP_TX_TIMEOUT;
2107 	ndev->netdev_ops	= &netcp_netdev_ops;
2108 	SET_NETDEV_DEV(ndev, dev);
2109 
2110 	list_add_tail(&netcp->interface_list, &netcp_device->interface_head);
2111 	return 0;
2112 
2113 quit:
2114 	free_netdev(ndev);
2115 	return ret;
2116 }
2117 
2118 static void netcp_delete_interface(struct netcp_device *netcp_device,
2119 				   struct net_device *ndev)
2120 {
2121 	struct netcp_intf_modpriv *intf_modpriv, *tmp;
2122 	struct netcp_intf *netcp = netdev_priv(ndev);
2123 	struct netcp_module *module;
2124 
2125 	dev_dbg(netcp_device->device, "Removing interface \"%s\"\n",
2126 		ndev->name);
2127 
2128 	/* Notify each of the modules that the interface is going away */
2129 	list_for_each_entry_safe(intf_modpriv, tmp, &netcp->module_head,
2130 				 intf_list) {
2131 		module = intf_modpriv->netcp_module;
2132 		dev_dbg(netcp_device->device, "Releasing module \"%s\"\n",
2133 			module->name);
2134 		if (module->release)
2135 			module->release(intf_modpriv->module_priv);
2136 		list_del(&intf_modpriv->intf_list);
2137 	}
2138 	WARN(!list_empty(&netcp->module_head), "%s interface module list is not empty!\n",
2139 	     ndev->name);
2140 
2141 	list_del(&netcp->interface_list);
2142 
2143 	of_node_put(netcp->node_interface);
2144 	unregister_netdev(ndev);
2145 	netif_napi_del(&netcp->rx_napi);
2146 	free_netdev(ndev);
2147 }
2148 
2149 static int netcp_probe(struct platform_device *pdev)
2150 {
2151 	struct device_node *node = pdev->dev.of_node;
2152 	struct netcp_intf *netcp_intf, *netcp_tmp;
2153 	struct device_node *child, *interfaces;
2154 	struct netcp_device *netcp_device;
2155 	struct device *dev = &pdev->dev;
2156 	int ret;
2157 
2158 	if (!node) {
2159 		dev_err(dev, "could not find device info\n");
2160 		return -ENODEV;
2161 	}
2162 
2163 	/* Allocate a new NETCP device instance */
2164 	netcp_device = devm_kzalloc(dev, sizeof(*netcp_device), GFP_KERNEL);
2165 	if (!netcp_device)
2166 		return -ENOMEM;
2167 
2168 	pm_runtime_enable(&pdev->dev);
2169 	ret = pm_runtime_get_sync(&pdev->dev);
2170 	if (ret < 0) {
2171 		dev_err(dev, "Failed to enable NETCP power-domain\n");
2172 		pm_runtime_disable(&pdev->dev);
2173 		return ret;
2174 	}
2175 
2176 	/* Initialize the NETCP device instance */
2177 	INIT_LIST_HEAD(&netcp_device->interface_head);
2178 	INIT_LIST_HEAD(&netcp_device->modpriv_head);
2179 	netcp_device->device = dev;
2180 	platform_set_drvdata(pdev, netcp_device);
2181 
2182 	/* create interfaces */
2183 	interfaces = of_get_child_by_name(node, "netcp-interfaces");
2184 	if (!interfaces) {
2185 		dev_err(dev, "could not find netcp-interfaces node\n");
2186 		ret = -ENODEV;
2187 		goto probe_quit;
2188 	}
2189 
2190 	for_each_available_child_of_node(interfaces, child) {
2191 		ret = netcp_create_interface(netcp_device, child);
2192 		if (ret) {
2193 			dev_err(dev, "could not create interface(%s)\n",
2194 				child->name);
2195 			goto probe_quit_interface;
2196 		}
2197 	}
2198 
2199 	of_node_put(interfaces);
2200 
2201 	/* Add the device instance to the list */
2202 	list_add_tail(&netcp_device->device_list, &netcp_devices);
2203 
2204 	return 0;
2205 
2206 probe_quit_interface:
2207 	list_for_each_entry_safe(netcp_intf, netcp_tmp,
2208 				 &netcp_device->interface_head,
2209 				 interface_list) {
2210 		netcp_delete_interface(netcp_device, netcp_intf->ndev);
2211 	}
2212 
2213 	of_node_put(interfaces);
2214 
2215 probe_quit:
2216 	pm_runtime_put_sync(&pdev->dev);
2217 	pm_runtime_disable(&pdev->dev);
2218 	platform_set_drvdata(pdev, NULL);
2219 	return ret;
2220 }
2221 
2222 static int netcp_remove(struct platform_device *pdev)
2223 {
2224 	struct netcp_device *netcp_device = platform_get_drvdata(pdev);
2225 	struct netcp_intf *netcp_intf, *netcp_tmp;
2226 	struct netcp_inst_modpriv *inst_modpriv, *tmp;
2227 	struct netcp_module *module;
2228 
2229 	list_for_each_entry_safe(inst_modpriv, tmp, &netcp_device->modpriv_head,
2230 				 inst_list) {
2231 		module = inst_modpriv->netcp_module;
2232 		dev_dbg(&pdev->dev, "Removing module \"%s\"\n", module->name);
2233 		module->remove(netcp_device, inst_modpriv->module_priv);
2234 		list_del(&inst_modpriv->inst_list);
2235 	}
2236 
2237 	/* now that all modules are removed, clean up the interfaces */
2238 	list_for_each_entry_safe(netcp_intf, netcp_tmp,
2239 				 &netcp_device->interface_head,
2240 				 interface_list) {
2241 		netcp_delete_interface(netcp_device, netcp_intf->ndev);
2242 	}
2243 
2244 	WARN(!list_empty(&netcp_device->interface_head),
2245 	     "%s interface list not empty!\n", pdev->name);
2246 
2247 	pm_runtime_put_sync(&pdev->dev);
2248 	pm_runtime_disable(&pdev->dev);
2249 	platform_set_drvdata(pdev, NULL);
2250 	return 0;
2251 }
2252 
2253 static const struct of_device_id of_match[] = {
2254 	{ .compatible = "ti,netcp-1.0", },
2255 	{},
2256 };
2257 MODULE_DEVICE_TABLE(of, of_match);
2258 
2259 static struct platform_driver netcp_driver = {
2260 	.driver = {
2261 		.name		= "netcp-1.0",
2262 		.of_match_table	= of_match,
2263 	},
2264 	.probe = netcp_probe,
2265 	.remove = netcp_remove,
2266 };
2267 module_platform_driver(netcp_driver);
2268 
2269 MODULE_LICENSE("GPL v2");
2270 MODULE_DESCRIPTION("TI NETCP driver for Keystone SOCs");
2271 MODULE_AUTHOR("Sandeep Nair <sandeep_n@ti.com");
2272