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