1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
82struct 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
89struct 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
96struct 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
103struct netcp_tx_cb {
104 void *ts_context;
105 void (*txtstamp)(void *context, struct sk_buff *skb);
106};
107
108static LIST_HEAD(netcp_devices);
109static LIST_HEAD(netcp_modules);
110static DEFINE_MUTEX(netcp_modules_lock);
111
112static int netcp_debug_level = -1;
113module_param(netcp_debug_level, int, 0);
114MODULE_PARM_DESC(netcp_debug_level, "Netcp debug level (NETIF_MSG bits) (0=none,...,16=all)");
115
116
117static 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
125static 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
132static u32 get_sw_data(int index, struct knav_dma_desc *desc)
133{
134
135 return desc->sw_data[index];
136}
137
138
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
144static 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
151static 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
159static 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
167static 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
174static void set_sw_data(int index, u32 data, struct knav_dma_desc *desc)
175{
176
177 desc->sw_data[index] = data;
178}
179
180
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
186static 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
193static 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
202static 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
228static 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
240static 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
250static 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
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
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
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
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
350 list_for_each_entry(netcp_intf,
351 &netcp_device->interface_head,
352 interface_list) {
353
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
363int 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
397fail:
398 mutex_unlock(&netcp_modules_lock);
399 netcp_unregister_module(module);
400 return ret;
401}
402EXPORT_SYMBOL_GPL(netcp_register_module);
403
404static 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
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
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
442void 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
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}
463EXPORT_SYMBOL_GPL(netcp_unregister_module);
464
465void *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}
475EXPORT_SYMBOL_GPL(netcp_module_get_intf_data);
476
477
478struct netcp_hook_list {
479 struct list_head list;
480 netcp_hook_rtn *hook_rtn;
481 void *hook_data;
482 int order;
483};
484
485int 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}
510EXPORT_SYMBOL_GPL(netcp_register_txhook);
511
512int 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}
532EXPORT_SYMBOL_GPL(netcp_unregister_txhook);
533
534int 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}
559EXPORT_SYMBOL_GPL(netcp_register_rxhook);
560
561int 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}
582EXPORT_SYMBOL_GPL(netcp_unregister_rxhook);
583
584static 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
592static 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
611
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
620
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
630static 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
654static 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
679
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
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
701 skb_reserve(skb, NETCP_SOP_OFFSET);
702 __skb_put(skb, buf_len);
703
704
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
716
717
718 page = (struct page *)GET_SW_DATA0(ndesc);
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
734 knav_pool_desc_put(netcp->rx_pool, ndesc);
735 }
736
737
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
743
744
745
746 if (!(netcp->hw_cap & ETH_SW_CAN_REMOVE_ETH_FCS))
747 __pskb_trim(skb, skb->len - ETH_FCS_LEN);
748
749
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
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
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
782 skb->protocol = eth_type_trans(skb, netcp->ndev);
783 netif_receive_skb(skb);
784 return 0;
785
786free_desc:
787 netcp_free_rx_desc_chain(netcp, desc);
788 rx_stats->rx_errors++;
789 return 0;
790}
791
792static 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
803static 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
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
820
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
850static 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
866static 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
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
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
904
905
906 sw_data[0] = (u32)bufptr;
907 } else {
908
909 page = alloc_page(GFP_ATOMIC | GFP_DMA);
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
917
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
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
940fail:
941 knav_pool_desc_put(netcp->rx_pool, hwdesc);
942 return -ENOMEM;
943}
944
945
946static 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
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 }
959}
960
961
962static 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
979static 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
987static 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
1016static 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
1039
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
1072static 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
1087static 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
1095static struct knav_dma_desc*
1096netcp_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
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
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
1138 dma_addr = dma_map_page(dev, page, page_offset, buf_len,
1139 DMA_TO_DEVICE);
1140 if (unlikely(!dma_addr)) {
1141 dev_err(netcp->ndev_dev, "Failed to map skb page\n");
1142 goto free_descs;
1143 }
1144
1145 ndesc = knav_pool_desc_get(netcp->tx_pool);
1146 if (IS_ERR_OR_NULL(ndesc)) {
1147 dev_err(netcp->ndev_dev, "out of TX desc for frags\n");
1148 dma_unmap_page(dev, dma_addr, buf_len, DMA_TO_DEVICE);
1149 goto free_descs;
1150 }
1151
1152 desc_dma = knav_pool_desc_virt_to_dma(netcp->tx_pool, ndesc);
1153 set_pkt_info(dma_addr, buf_len, 0, ndesc);
1154 desc_dma_32 = (u32)desc_dma;
1155 set_words(&desc_dma_32, 1, &pdesc->next_desc);
1156 pkt_len += buf_len;
1157 if (pdesc != desc)
1158 knav_pool_desc_map(netcp->tx_pool, pdesc,
1159 sizeof(*pdesc), &desc_dma, &dma_sz);
1160 pdesc = ndesc;
1161 }
1162 if (pdesc != desc)
1163 knav_pool_desc_map(netcp->tx_pool, pdesc, sizeof(*pdesc),
1164 &dma_addr, &dma_sz);
1165
1166
1167 if (skb_shinfo(skb)->frag_list) {
1168 dev_err_ratelimited(netcp->ndev_dev, "NETIF_F_FRAGLIST not supported\n");
1169 goto free_descs;
1170 }
1171
1172upd_pkt_len:
1173 WARN_ON(pkt_len != skb->len);
1174
1175 pkt_len &= KNAV_DMA_DESC_PKT_LEN_MASK;
1176 set_words(&pkt_len, 1, &desc->desc_info);
1177 return desc;
1178
1179free_descs:
1180 netcp_free_tx_desc_chain(netcp, desc, sizeof(*desc));
1181 return NULL;
1182}
1183
1184static int netcp_tx_submit_skb(struct netcp_intf *netcp,
1185 struct sk_buff *skb,
1186 struct knav_dma_desc *desc)
1187{
1188 struct netcp_tx_pipe *tx_pipe = NULL;
1189 struct netcp_hook_list *tx_hook;
1190 struct netcp_packet p_info;
1191 struct netcp_tx_cb *tx_cb;
1192 unsigned int dma_sz;
1193 dma_addr_t dma;
1194 u32 tmp = 0;
1195 int ret = 0;
1196
1197 p_info.netcp = netcp;
1198 p_info.skb = skb;
1199 p_info.tx_pipe = NULL;
1200 p_info.psdata_len = 0;
1201 p_info.ts_context = NULL;
1202 p_info.txtstamp = NULL;
1203 p_info.epib = desc->epib;
1204 p_info.psdata = (u32 __force *)desc->psdata;
1205 memset(p_info.epib, 0, KNAV_DMA_NUM_EPIB_WORDS * sizeof(__le32));
1206
1207
1208 list_for_each_entry(tx_hook, &netcp->txhook_list_head, list) {
1209 ret = tx_hook->hook_rtn(tx_hook->order, tx_hook->hook_data,
1210 &p_info);
1211 if (unlikely(ret != 0)) {
1212 dev_err(netcp->ndev_dev, "TX hook %d rejected the packet with reason(%d)\n",
1213 tx_hook->order, ret);
1214 ret = (ret < 0) ? ret : NETDEV_TX_OK;
1215 goto out;
1216 }
1217 }
1218
1219
1220 tx_pipe = p_info.tx_pipe;
1221 if (!tx_pipe) {
1222 dev_err(netcp->ndev_dev, "No TX hook claimed the packet!\n");
1223 ret = -ENXIO;
1224 goto out;
1225 }
1226
1227 tx_cb = (struct netcp_tx_cb *)skb->cb;
1228 tx_cb->ts_context = p_info.ts_context;
1229 tx_cb->txtstamp = p_info.txtstamp;
1230
1231
1232 if (p_info.psdata_len) {
1233
1234 __le32 *psdata = (void __force *)p_info.psdata;
1235
1236 set_words((u32 *)psdata +
1237 (KNAV_DMA_NUM_PS_WORDS - p_info.psdata_len),
1238 p_info.psdata_len, psdata);
1239 tmp |= (p_info.psdata_len & KNAV_DMA_DESC_PSLEN_MASK) <<
1240 KNAV_DMA_DESC_PSLEN_SHIFT;
1241 }
1242
1243 tmp |= KNAV_DMA_DESC_HAS_EPIB |
1244 ((netcp->tx_compl_qid & KNAV_DMA_DESC_RETQ_MASK) <<
1245 KNAV_DMA_DESC_RETQ_SHIFT);
1246
1247 if (!(tx_pipe->flags & SWITCH_TO_PORT_IN_TAGINFO)) {
1248 tmp |= ((tx_pipe->switch_to_port & KNAV_DMA_DESC_PSFLAG_MASK) <<
1249 KNAV_DMA_DESC_PSFLAG_SHIFT);
1250 }
1251
1252 set_words(&tmp, 1, &desc->packet_info);
1253
1254
1255
1256 SET_SW_DATA0((u32)skb, desc);
1257
1258 if (tx_pipe->flags & SWITCH_TO_PORT_IN_TAGINFO) {
1259 tmp = tx_pipe->switch_to_port;
1260 set_words(&tmp, 1, &desc->tag_info);
1261 }
1262
1263
1264 ret = knav_pool_desc_map(netcp->tx_pool, desc, sizeof(*desc), &dma,
1265 &dma_sz);
1266 if (unlikely(ret)) {
1267 dev_err(netcp->ndev_dev, "%s() failed to map desc\n", __func__);
1268 ret = -ENOMEM;
1269 goto out;
1270 }
1271 skb_tx_timestamp(skb);
1272 knav_queue_push(tx_pipe->dma_queue, dma, dma_sz, 0);
1273
1274out:
1275 return ret;
1276}
1277
1278
1279static int netcp_ndo_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1280{
1281 struct netcp_intf *netcp = netdev_priv(ndev);
1282 struct netcp_stats *tx_stats = &netcp->stats;
1283 int subqueue = skb_get_queue_mapping(skb);
1284 struct knav_dma_desc *desc;
1285 int desc_count, ret = 0;
1286
1287 if (unlikely(skb->len <= 0)) {
1288 dev_kfree_skb(skb);
1289 return NETDEV_TX_OK;
1290 }
1291
1292 if (unlikely(skb->len < NETCP_MIN_PACKET_SIZE)) {
1293 ret = skb_padto(skb, NETCP_MIN_PACKET_SIZE);
1294 if (ret < 0) {
1295
1296 dev_warn(netcp->ndev_dev, "padding failed (%d), packet dropped\n",
1297 ret);
1298 tx_stats->tx_dropped++;
1299 return ret;
1300 }
1301 skb->len = NETCP_MIN_PACKET_SIZE;
1302 }
1303
1304 desc = netcp_tx_map_skb(skb, netcp);
1305 if (unlikely(!desc)) {
1306 netif_stop_subqueue(ndev, subqueue);
1307 ret = -ENOBUFS;
1308 goto drop;
1309 }
1310
1311 ret = netcp_tx_submit_skb(netcp, skb, desc);
1312 if (ret)
1313 goto drop;
1314
1315
1316 desc_count = knav_pool_count(netcp->tx_pool);
1317 if (desc_count < netcp->tx_pause_threshold) {
1318 dev_dbg(netcp->ndev_dev, "pausing tx, count(%d)\n", desc_count);
1319 netif_stop_subqueue(ndev, subqueue);
1320 }
1321 return NETDEV_TX_OK;
1322
1323drop:
1324 tx_stats->tx_dropped++;
1325 if (desc)
1326 netcp_free_tx_desc_chain(netcp, desc, sizeof(*desc));
1327 dev_kfree_skb(skb);
1328 return ret;
1329}
1330
1331int netcp_txpipe_close(struct netcp_tx_pipe *tx_pipe)
1332{
1333 if (tx_pipe->dma_channel) {
1334 knav_dma_close_channel(tx_pipe->dma_channel);
1335 tx_pipe->dma_channel = NULL;
1336 }
1337 return 0;
1338}
1339EXPORT_SYMBOL_GPL(netcp_txpipe_close);
1340
1341int netcp_txpipe_open(struct netcp_tx_pipe *tx_pipe)
1342{
1343 struct device *dev = tx_pipe->netcp_device->device;
1344 struct knav_dma_cfg config;
1345 int ret = 0;
1346 u8 name[16];
1347
1348 memset(&config, 0, sizeof(config));
1349 config.direction = DMA_MEM_TO_DEV;
1350 config.u.tx.filt_einfo = false;
1351 config.u.tx.filt_pswords = false;
1352 config.u.tx.priority = DMA_PRIO_MED_L;
1353
1354 tx_pipe->dma_channel = knav_dma_open_channel(dev,
1355 tx_pipe->dma_chan_name, &config);
1356 if (IS_ERR(tx_pipe->dma_channel)) {
1357 dev_err(dev, "failed opening tx chan(%s)\n",
1358 tx_pipe->dma_chan_name);
1359 ret = PTR_ERR(tx_pipe->dma_channel);
1360 goto err;
1361 }
1362
1363 snprintf(name, sizeof(name), "tx-pipe-%s", dev_name(dev));
1364 tx_pipe->dma_queue = knav_queue_open(name, tx_pipe->dma_queue_id,
1365 KNAV_QUEUE_SHARED);
1366 if (IS_ERR(tx_pipe->dma_queue)) {
1367 dev_err(dev, "Could not open DMA queue for channel \"%s\": %d\n",
1368 name, ret);
1369 ret = PTR_ERR(tx_pipe->dma_queue);
1370 goto err;
1371 }
1372
1373 dev_dbg(dev, "opened tx pipe %s\n", name);
1374 return 0;
1375
1376err:
1377 if (!IS_ERR_OR_NULL(tx_pipe->dma_channel))
1378 knav_dma_close_channel(tx_pipe->dma_channel);
1379 tx_pipe->dma_channel = NULL;
1380 return ret;
1381}
1382EXPORT_SYMBOL_GPL(netcp_txpipe_open);
1383
1384int netcp_txpipe_init(struct netcp_tx_pipe *tx_pipe,
1385 struct netcp_device *netcp_device,
1386 const char *dma_chan_name, unsigned int dma_queue_id)
1387{
1388 memset(tx_pipe, 0, sizeof(*tx_pipe));
1389 tx_pipe->netcp_device = netcp_device;
1390 tx_pipe->dma_chan_name = dma_chan_name;
1391 tx_pipe->dma_queue_id = dma_queue_id;
1392 return 0;
1393}
1394EXPORT_SYMBOL_GPL(netcp_txpipe_init);
1395
1396static struct netcp_addr *netcp_addr_find(struct netcp_intf *netcp,
1397 const u8 *addr,
1398 enum netcp_addr_type type)
1399{
1400 struct netcp_addr *naddr;
1401
1402 list_for_each_entry(naddr, &netcp->addr_list, node) {
1403 if (naddr->type != type)
1404 continue;
1405 if (addr && memcmp(addr, naddr->addr, ETH_ALEN))
1406 continue;
1407 return naddr;
1408 }
1409
1410 return NULL;
1411}
1412
1413static struct netcp_addr *netcp_addr_add(struct netcp_intf *netcp,
1414 const u8 *addr,
1415 enum netcp_addr_type type)
1416{
1417 struct netcp_addr *naddr;
1418
1419 naddr = devm_kmalloc(netcp->dev, sizeof(*naddr), GFP_ATOMIC);
1420 if (!naddr)
1421 return NULL;
1422
1423 naddr->type = type;
1424 naddr->flags = 0;
1425 naddr->netcp = netcp;
1426 if (addr)
1427 ether_addr_copy(naddr->addr, addr);
1428 else
1429 eth_zero_addr(naddr->addr);
1430 list_add_tail(&naddr->node, &netcp->addr_list);
1431
1432 return naddr;
1433}
1434
1435static void netcp_addr_del(struct netcp_intf *netcp, struct netcp_addr *naddr)
1436{
1437 list_del(&naddr->node);
1438 devm_kfree(netcp->dev, naddr);
1439}
1440
1441static void netcp_addr_clear_mark(struct netcp_intf *netcp)
1442{
1443 struct netcp_addr *naddr;
1444
1445 list_for_each_entry(naddr, &netcp->addr_list, node)
1446 naddr->flags = 0;
1447}
1448
1449static void netcp_addr_add_mark(struct netcp_intf *netcp, const u8 *addr,
1450 enum netcp_addr_type type)
1451{
1452 struct netcp_addr *naddr;
1453
1454 naddr = netcp_addr_find(netcp, addr, type);
1455 if (naddr) {
1456 naddr->flags |= ADDR_VALID;
1457 return;
1458 }
1459
1460 naddr = netcp_addr_add(netcp, addr, type);
1461 if (!WARN_ON(!naddr))
1462 naddr->flags |= ADDR_NEW;
1463}
1464
1465static void netcp_addr_sweep_del(struct netcp_intf *netcp)
1466{
1467 struct netcp_addr *naddr, *tmp;
1468 struct netcp_intf_modpriv *priv;
1469 struct netcp_module *module;
1470 int error;
1471
1472 list_for_each_entry_safe(naddr, tmp, &netcp->addr_list, node) {
1473 if (naddr->flags & (ADDR_VALID | ADDR_NEW))
1474 continue;
1475 dev_dbg(netcp->ndev_dev, "deleting address %pM, type %x\n",
1476 naddr->addr, naddr->type);
1477 for_each_module(netcp, priv) {
1478 module = priv->netcp_module;
1479 if (!module->del_addr)
1480 continue;
1481 error = module->del_addr(priv->module_priv,
1482 naddr);
1483 WARN_ON(error);
1484 }
1485 netcp_addr_del(netcp, naddr);
1486 }
1487}
1488
1489static void netcp_addr_sweep_add(struct netcp_intf *netcp)
1490{
1491 struct netcp_addr *naddr, *tmp;
1492 struct netcp_intf_modpriv *priv;
1493 struct netcp_module *module;
1494 int error;
1495
1496 list_for_each_entry_safe(naddr, tmp, &netcp->addr_list, node) {
1497 if (!(naddr->flags & ADDR_NEW))
1498 continue;
1499 dev_dbg(netcp->ndev_dev, "adding address %pM, type %x\n",
1500 naddr->addr, naddr->type);
1501
1502 for_each_module(netcp, priv) {
1503 module = priv->netcp_module;
1504 if (!module->add_addr)
1505 continue;
1506 error = module->add_addr(priv->module_priv, naddr);
1507 WARN_ON(error);
1508 }
1509 }
1510}
1511
1512static int netcp_set_promiscuous(struct netcp_intf *netcp, bool promisc)
1513{
1514 struct netcp_intf_modpriv *priv;
1515 struct netcp_module *module;
1516 int error;
1517
1518 for_each_module(netcp, priv) {
1519 module = priv->netcp_module;
1520 if (!module->set_rx_mode)
1521 continue;
1522
1523 error = module->set_rx_mode(priv->module_priv, promisc);
1524 if (error)
1525 return error;
1526 }
1527 return 0;
1528}
1529
1530static void netcp_set_rx_mode(struct net_device *ndev)
1531{
1532 struct netcp_intf *netcp = netdev_priv(ndev);
1533 struct netdev_hw_addr *ndev_addr;
1534 bool promisc;
1535
1536 promisc = (ndev->flags & IFF_PROMISC ||
1537 ndev->flags & IFF_ALLMULTI ||
1538 netdev_mc_count(ndev) > NETCP_MAX_MCAST_ADDR);
1539
1540 spin_lock(&netcp->lock);
1541
1542 netcp_addr_clear_mark(netcp);
1543
1544
1545 netcp_addr_add_mark(netcp, ndev->broadcast, ADDR_BCAST);
1546 for_each_dev_addr(ndev, ndev_addr)
1547 netcp_addr_add_mark(netcp, ndev_addr->addr, ADDR_DEV);
1548 netdev_for_each_uc_addr(ndev_addr, ndev)
1549 netcp_addr_add_mark(netcp, ndev_addr->addr, ADDR_UCAST);
1550 netdev_for_each_mc_addr(ndev_addr, ndev)
1551 netcp_addr_add_mark(netcp, ndev_addr->addr, ADDR_MCAST);
1552
1553 if (promisc)
1554 netcp_addr_add_mark(netcp, NULL, ADDR_ANY);
1555
1556
1557 netcp_addr_sweep_del(netcp);
1558 netcp_addr_sweep_add(netcp);
1559 netcp_set_promiscuous(netcp, promisc);
1560 spin_unlock(&netcp->lock);
1561}
1562
1563static void netcp_free_navigator_resources(struct netcp_intf *netcp)
1564{
1565 int i;
1566
1567 if (netcp->rx_channel) {
1568 knav_dma_close_channel(netcp->rx_channel);
1569 netcp->rx_channel = NULL;
1570 }
1571
1572 if (!IS_ERR_OR_NULL(netcp->rx_pool))
1573 netcp_rxpool_free(netcp);
1574
1575 if (!IS_ERR_OR_NULL(netcp->rx_queue)) {
1576 knav_queue_close(netcp->rx_queue);
1577 netcp->rx_queue = NULL;
1578 }
1579
1580 for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN &&
1581 !IS_ERR_OR_NULL(netcp->rx_fdq[i]) ; ++i) {
1582 knav_queue_close(netcp->rx_fdq[i]);
1583 netcp->rx_fdq[i] = NULL;
1584 }
1585
1586 if (!IS_ERR_OR_NULL(netcp->tx_compl_q)) {
1587 knav_queue_close(netcp->tx_compl_q);
1588 netcp->tx_compl_q = NULL;
1589 }
1590
1591 if (!IS_ERR_OR_NULL(netcp->tx_pool)) {
1592 knav_pool_destroy(netcp->tx_pool);
1593 netcp->tx_pool = NULL;
1594 }
1595}
1596
1597static int netcp_setup_navigator_resources(struct net_device *ndev)
1598{
1599 struct netcp_intf *netcp = netdev_priv(ndev);
1600 struct knav_queue_notify_config notify_cfg;
1601 struct knav_dma_cfg config;
1602 u32 last_fdq = 0;
1603 u8 name[16];
1604 int ret;
1605 int i;
1606
1607
1608 snprintf(name, sizeof(name), "rx-pool-%s", ndev->name);
1609 netcp->rx_pool = knav_pool_create(name, netcp->rx_pool_size,
1610 netcp->rx_pool_region_id);
1611 if (IS_ERR_OR_NULL(netcp->rx_pool)) {
1612 dev_err(netcp->ndev_dev, "Couldn't create rx pool\n");
1613 ret = PTR_ERR(netcp->rx_pool);
1614 goto fail;
1615 }
1616
1617 snprintf(name, sizeof(name), "tx-pool-%s", ndev->name);
1618 netcp->tx_pool = knav_pool_create(name, netcp->tx_pool_size,
1619 netcp->tx_pool_region_id);
1620 if (IS_ERR_OR_NULL(netcp->tx_pool)) {
1621 dev_err(netcp->ndev_dev, "Couldn't create tx pool\n");
1622 ret = PTR_ERR(netcp->tx_pool);
1623 goto fail;
1624 }
1625
1626
1627 snprintf(name, sizeof(name), "tx-compl-%s", ndev->name);
1628 netcp->tx_compl_q = knav_queue_open(name, netcp->tx_compl_qid, 0);
1629 if (IS_ERR(netcp->tx_compl_q)) {
1630 ret = PTR_ERR(netcp->tx_compl_q);
1631 goto fail;
1632 }
1633 netcp->tx_compl_qid = knav_queue_get_id(netcp->tx_compl_q);
1634
1635
1636 notify_cfg.fn = netcp_tx_notify;
1637 notify_cfg.fn_arg = netcp;
1638 ret = knav_queue_device_control(netcp->tx_compl_q,
1639 KNAV_QUEUE_SET_NOTIFIER,
1640 (unsigned long)¬ify_cfg);
1641 if (ret)
1642 goto fail;
1643
1644 knav_queue_disable_notify(netcp->tx_compl_q);
1645
1646
1647 snprintf(name, sizeof(name), "rx-compl-%s", ndev->name);
1648 netcp->rx_queue = knav_queue_open(name, netcp->rx_queue_id, 0);
1649 if (IS_ERR(netcp->rx_queue)) {
1650 ret = PTR_ERR(netcp->rx_queue);
1651 goto fail;
1652 }
1653 netcp->rx_queue_id = knav_queue_get_id(netcp->rx_queue);
1654
1655
1656 notify_cfg.fn = netcp_rx_notify;
1657 notify_cfg.fn_arg = netcp;
1658 ret = knav_queue_device_control(netcp->rx_queue,
1659 KNAV_QUEUE_SET_NOTIFIER,
1660 (unsigned long)¬ify_cfg);
1661 if (ret)
1662 goto fail;
1663
1664 knav_queue_disable_notify(netcp->rx_queue);
1665
1666
1667 for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN && netcp->rx_queue_depths[i];
1668 ++i) {
1669 snprintf(name, sizeof(name), "rx-fdq-%s-%d", ndev->name, i);
1670 netcp->rx_fdq[i] = knav_queue_open(name, KNAV_QUEUE_GP, 0);
1671 if (IS_ERR(netcp->rx_fdq[i])) {
1672 ret = PTR_ERR(netcp->rx_fdq[i]);
1673 goto fail;
1674 }
1675 }
1676
1677 memset(&config, 0, sizeof(config));
1678 config.direction = DMA_DEV_TO_MEM;
1679 config.u.rx.einfo_present = true;
1680 config.u.rx.psinfo_present = true;
1681 config.u.rx.err_mode = DMA_DROP;
1682 config.u.rx.desc_type = DMA_DESC_HOST;
1683 config.u.rx.psinfo_at_sop = false;
1684 config.u.rx.sop_offset = NETCP_SOP_OFFSET;
1685 config.u.rx.dst_q = netcp->rx_queue_id;
1686 config.u.rx.thresh = DMA_THRESH_NONE;
1687
1688 for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN; ++i) {
1689 if (netcp->rx_fdq[i])
1690 last_fdq = knav_queue_get_id(netcp->rx_fdq[i]);
1691 config.u.rx.fdq[i] = last_fdq;
1692 }
1693
1694 netcp->rx_channel = knav_dma_open_channel(netcp->netcp_device->device,
1695 netcp->dma_chan_name, &config);
1696 if (IS_ERR(netcp->rx_channel)) {
1697 dev_err(netcp->ndev_dev, "failed opening rx chan(%s\n",
1698 netcp->dma_chan_name);
1699 ret = PTR_ERR(netcp->rx_channel);
1700 goto fail;
1701 }
1702
1703 dev_dbg(netcp->ndev_dev, "opened RX channel: %p\n", netcp->rx_channel);
1704 return 0;
1705
1706fail:
1707 netcp_free_navigator_resources(netcp);
1708 return ret;
1709}
1710
1711
1712static int netcp_ndo_open(struct net_device *ndev)
1713{
1714 struct netcp_intf *netcp = netdev_priv(ndev);
1715 struct netcp_intf_modpriv *intf_modpriv;
1716 struct netcp_module *module;
1717 int ret;
1718
1719 netif_carrier_off(ndev);
1720 ret = netcp_setup_navigator_resources(ndev);
1721 if (ret) {
1722 dev_err(netcp->ndev_dev, "Failed to setup navigator resources\n");
1723 goto fail;
1724 }
1725
1726 for_each_module(netcp, intf_modpriv) {
1727 module = intf_modpriv->netcp_module;
1728 if (module->open) {
1729 ret = module->open(intf_modpriv->module_priv, ndev);
1730 if (ret != 0) {
1731 dev_err(netcp->ndev_dev, "module open failed\n");
1732 goto fail_open;
1733 }
1734 }
1735 }
1736
1737 napi_enable(&netcp->rx_napi);
1738 napi_enable(&netcp->tx_napi);
1739 knav_queue_enable_notify(netcp->tx_compl_q);
1740 knav_queue_enable_notify(netcp->rx_queue);
1741 netcp_rxpool_refill(netcp);
1742 netif_tx_wake_all_queues(ndev);
1743 dev_dbg(netcp->ndev_dev, "netcp device %s opened\n", ndev->name);
1744 return 0;
1745
1746fail_open:
1747 for_each_module(netcp, intf_modpriv) {
1748 module = intf_modpriv->netcp_module;
1749 if (module->close)
1750 module->close(intf_modpriv->module_priv, ndev);
1751 }
1752
1753fail:
1754 netcp_free_navigator_resources(netcp);
1755 return ret;
1756}
1757
1758
1759static int netcp_ndo_stop(struct net_device *ndev)
1760{
1761 struct netcp_intf *netcp = netdev_priv(ndev);
1762 struct netcp_intf_modpriv *intf_modpriv;
1763 struct netcp_module *module;
1764 int err = 0;
1765
1766 netif_tx_stop_all_queues(ndev);
1767 netif_carrier_off(ndev);
1768 netcp_addr_clear_mark(netcp);
1769 netcp_addr_sweep_del(netcp);
1770 knav_queue_disable_notify(netcp->rx_queue);
1771 knav_queue_disable_notify(netcp->tx_compl_q);
1772 napi_disable(&netcp->rx_napi);
1773 napi_disable(&netcp->tx_napi);
1774
1775 for_each_module(netcp, intf_modpriv) {
1776 module = intf_modpriv->netcp_module;
1777 if (module->close) {
1778 err = module->close(intf_modpriv->module_priv, ndev);
1779 if (err != 0)
1780 dev_err(netcp->ndev_dev, "Close failed\n");
1781 }
1782 }
1783
1784
1785 netcp_empty_rx_queue(netcp);
1786
1787
1788 netcp_process_tx_compl_packets(netcp, netcp->tx_pool_size);
1789
1790 if (knav_pool_count(netcp->tx_pool) != netcp->tx_pool_size)
1791 dev_err(netcp->ndev_dev, "Lost (%d) Tx descs\n",
1792 netcp->tx_pool_size - knav_pool_count(netcp->tx_pool));
1793
1794 netcp_free_navigator_resources(netcp);
1795 dev_dbg(netcp->ndev_dev, "netcp device %s stopped\n", ndev->name);
1796 return 0;
1797}
1798
1799static int netcp_ndo_ioctl(struct net_device *ndev,
1800 struct ifreq *req, int cmd)
1801{
1802 struct netcp_intf *netcp = netdev_priv(ndev);
1803 struct netcp_intf_modpriv *intf_modpriv;
1804 struct netcp_module *module;
1805 int ret = -1, err = -EOPNOTSUPP;
1806
1807 if (!netif_running(ndev))
1808 return -EINVAL;
1809
1810 for_each_module(netcp, intf_modpriv) {
1811 module = intf_modpriv->netcp_module;
1812 if (!module->ioctl)
1813 continue;
1814
1815 err = module->ioctl(intf_modpriv->module_priv, req, cmd);
1816 if ((err < 0) && (err != -EOPNOTSUPP)) {
1817 ret = err;
1818 goto out;
1819 }
1820 if (err == 0)
1821 ret = err;
1822 }
1823
1824out:
1825 return (ret == 0) ? 0 : err;
1826}
1827
1828static void netcp_ndo_tx_timeout(struct net_device *ndev, unsigned int txqueue)
1829{
1830 struct netcp_intf *netcp = netdev_priv(ndev);
1831 unsigned int descs = knav_pool_count(netcp->tx_pool);
1832
1833 dev_err(netcp->ndev_dev, "transmit timed out tx descs(%d)\n", descs);
1834 netcp_process_tx_compl_packets(netcp, netcp->tx_pool_size);
1835 netif_trans_update(ndev);
1836 netif_tx_wake_all_queues(ndev);
1837}
1838
1839static int netcp_rx_add_vid(struct net_device *ndev, __be16 proto, u16 vid)
1840{
1841 struct netcp_intf *netcp = netdev_priv(ndev);
1842 struct netcp_intf_modpriv *intf_modpriv;
1843 struct netcp_module *module;
1844 unsigned long flags;
1845 int err = 0;
1846
1847 dev_dbg(netcp->ndev_dev, "adding rx vlan id: %d\n", vid);
1848
1849 spin_lock_irqsave(&netcp->lock, flags);
1850 for_each_module(netcp, intf_modpriv) {
1851 module = intf_modpriv->netcp_module;
1852 if ((module->add_vid) && (vid != 0)) {
1853 err = module->add_vid(intf_modpriv->module_priv, vid);
1854 if (err != 0) {
1855 dev_err(netcp->ndev_dev, "Could not add vlan id = %d\n",
1856 vid);
1857 break;
1858 }
1859 }
1860 }
1861 spin_unlock_irqrestore(&netcp->lock, flags);
1862
1863 return err;
1864}
1865
1866static int netcp_rx_kill_vid(struct net_device *ndev, __be16 proto, u16 vid)
1867{
1868 struct netcp_intf *netcp = netdev_priv(ndev);
1869 struct netcp_intf_modpriv *intf_modpriv;
1870 struct netcp_module *module;
1871 unsigned long flags;
1872 int err = 0;
1873
1874 dev_dbg(netcp->ndev_dev, "removing rx vlan id: %d\n", vid);
1875
1876 spin_lock_irqsave(&netcp->lock, flags);
1877 for_each_module(netcp, intf_modpriv) {
1878 module = intf_modpriv->netcp_module;
1879 if (module->del_vid) {
1880 err = module->del_vid(intf_modpriv->module_priv, vid);
1881 if (err != 0) {
1882 dev_err(netcp->ndev_dev, "Could not delete vlan id = %d\n",
1883 vid);
1884 break;
1885 }
1886 }
1887 }
1888 spin_unlock_irqrestore(&netcp->lock, flags);
1889 return err;
1890}
1891
1892static int netcp_setup_tc(struct net_device *dev, enum tc_setup_type type,
1893 void *type_data)
1894{
1895 struct tc_mqprio_qopt *mqprio = type_data;
1896 u8 num_tc;
1897 int i;
1898
1899
1900 ASSERT_RTNL();
1901
1902 if (type != TC_SETUP_QDISC_MQPRIO)
1903 return -EOPNOTSUPP;
1904
1905 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
1906 num_tc = mqprio->num_tc;
1907
1908
1909 if ((dev->real_num_tx_queues <= 1) ||
1910 (dev->real_num_tx_queues < num_tc))
1911 return -EINVAL;
1912
1913
1914 if (num_tc) {
1915 netdev_set_num_tc(dev, num_tc);
1916 for (i = 0; i < num_tc; i++)
1917 netdev_set_tc_queue(dev, i, 1, i);
1918 } else {
1919 netdev_reset_tc(dev);
1920 }
1921
1922 return 0;
1923}
1924
1925static void
1926netcp_get_stats(struct net_device *ndev, struct rtnl_link_stats64 *stats)
1927{
1928 struct netcp_intf *netcp = netdev_priv(ndev);
1929 struct netcp_stats *p = &netcp->stats;
1930 u64 rxpackets, rxbytes, txpackets, txbytes;
1931 unsigned int start;
1932
1933 do {
1934 start = u64_stats_fetch_begin_irq(&p->syncp_rx);
1935 rxpackets = p->rx_packets;
1936 rxbytes = p->rx_bytes;
1937 } while (u64_stats_fetch_retry_irq(&p->syncp_rx, start));
1938
1939 do {
1940 start = u64_stats_fetch_begin_irq(&p->syncp_tx);
1941 txpackets = p->tx_packets;
1942 txbytes = p->tx_bytes;
1943 } while (u64_stats_fetch_retry_irq(&p->syncp_tx, start));
1944
1945 stats->rx_packets = rxpackets;
1946 stats->rx_bytes = rxbytes;
1947 stats->tx_packets = txpackets;
1948 stats->tx_bytes = txbytes;
1949
1950
1951 stats->rx_errors = p->rx_errors;
1952 stats->rx_dropped = p->rx_dropped;
1953 stats->tx_dropped = p->tx_dropped;
1954}
1955
1956static const struct net_device_ops netcp_netdev_ops = {
1957 .ndo_open = netcp_ndo_open,
1958 .ndo_stop = netcp_ndo_stop,
1959 .ndo_start_xmit = netcp_ndo_start_xmit,
1960 .ndo_set_rx_mode = netcp_set_rx_mode,
1961 .ndo_do_ioctl = netcp_ndo_ioctl,
1962 .ndo_get_stats64 = netcp_get_stats,
1963 .ndo_set_mac_address = eth_mac_addr,
1964 .ndo_validate_addr = eth_validate_addr,
1965 .ndo_vlan_rx_add_vid = netcp_rx_add_vid,
1966 .ndo_vlan_rx_kill_vid = netcp_rx_kill_vid,
1967 .ndo_tx_timeout = netcp_ndo_tx_timeout,
1968 .ndo_select_queue = dev_pick_tx_zero,
1969 .ndo_setup_tc = netcp_setup_tc,
1970};
1971
1972static int netcp_create_interface(struct netcp_device *netcp_device,
1973 struct device_node *node_interface)
1974{
1975 struct device *dev = netcp_device->device;
1976 struct device_node *node = dev->of_node;
1977 struct netcp_intf *netcp;
1978 struct net_device *ndev;
1979 resource_size_t size;
1980 struct resource res;
1981 void __iomem *efuse = NULL;
1982 u32 efuse_mac = 0;
1983 const void *mac_addr;
1984 u8 efuse_mac_addr[6];
1985 u32 temp[2];
1986 int ret = 0;
1987
1988 ndev = alloc_etherdev_mqs(sizeof(*netcp), 1, 1);
1989 if (!ndev) {
1990 dev_err(dev, "Error allocating netdev\n");
1991 return -ENOMEM;
1992 }
1993
1994 ndev->features |= NETIF_F_SG;
1995 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1996 ndev->hw_features = ndev->features;
1997 ndev->vlan_features |= NETIF_F_SG;
1998
1999
2000 ndev->min_mtu = ETH_MIN_MTU;
2001 ndev->max_mtu = NETCP_MAX_FRAME_SIZE - (ETH_HLEN + ETH_FCS_LEN);
2002
2003 netcp = netdev_priv(ndev);
2004 spin_lock_init(&netcp->lock);
2005 INIT_LIST_HEAD(&netcp->module_head);
2006 INIT_LIST_HEAD(&netcp->txhook_list_head);
2007 INIT_LIST_HEAD(&netcp->rxhook_list_head);
2008 INIT_LIST_HEAD(&netcp->addr_list);
2009 u64_stats_init(&netcp->stats.syncp_rx);
2010 u64_stats_init(&netcp->stats.syncp_tx);
2011 netcp->netcp_device = netcp_device;
2012 netcp->dev = netcp_device->device;
2013 netcp->ndev = ndev;
2014 netcp->ndev_dev = &ndev->dev;
2015 netcp->msg_enable = netif_msg_init(netcp_debug_level, NETCP_DEBUG);
2016 netcp->tx_pause_threshold = MAX_SKB_FRAGS;
2017 netcp->tx_resume_threshold = netcp->tx_pause_threshold;
2018 netcp->node_interface = node_interface;
2019
2020 ret = of_property_read_u32(node_interface, "efuse-mac", &efuse_mac);
2021 if (efuse_mac) {
2022 if (of_address_to_resource(node, NETCP_EFUSE_REG_INDEX, &res)) {
2023 dev_err(dev, "could not find efuse-mac reg resource\n");
2024 ret = -ENODEV;
2025 goto quit;
2026 }
2027 size = resource_size(&res);
2028
2029 if (!devm_request_mem_region(dev, res.start, size,
2030 dev_name(dev))) {
2031 dev_err(dev, "could not reserve resource\n");
2032 ret = -ENOMEM;
2033 goto quit;
2034 }
2035
2036 efuse = devm_ioremap_nocache(dev, res.start, size);
2037 if (!efuse) {
2038 dev_err(dev, "could not map resource\n");
2039 devm_release_mem_region(dev, res.start, size);
2040 ret = -ENOMEM;
2041 goto quit;
2042 }
2043
2044 emac_arch_get_mac_addr(efuse_mac_addr, efuse, efuse_mac);
2045 if (is_valid_ether_addr(efuse_mac_addr))
2046 ether_addr_copy(ndev->dev_addr, efuse_mac_addr);
2047 else
2048 random_ether_addr(ndev->dev_addr);
2049
2050 devm_iounmap(dev, efuse);
2051 devm_release_mem_region(dev, res.start, size);
2052 } else {
2053 mac_addr = of_get_mac_address(node_interface);
2054 if (mac_addr)
2055 ether_addr_copy(ndev->dev_addr, mac_addr);
2056 else
2057 random_ether_addr(ndev->dev_addr);
2058 }
2059
2060 ret = of_property_read_string(node_interface, "rx-channel",
2061 &netcp->dma_chan_name);
2062 if (ret < 0) {
2063 dev_err(dev, "missing \"rx-channel\" parameter\n");
2064 ret = -ENODEV;
2065 goto quit;
2066 }
2067
2068 ret = of_property_read_u32(node_interface, "rx-queue",
2069 &netcp->rx_queue_id);
2070 if (ret < 0) {
2071 dev_warn(dev, "missing \"rx-queue\" parameter\n");
2072 netcp->rx_queue_id = KNAV_QUEUE_QPEND;
2073 }
2074
2075 ret = of_property_read_u32_array(node_interface, "rx-queue-depth",
2076 netcp->rx_queue_depths,
2077 KNAV_DMA_FDQ_PER_CHAN);
2078 if (ret < 0) {
2079 dev_err(dev, "missing \"rx-queue-depth\" parameter\n");
2080 netcp->rx_queue_depths[0] = 128;
2081 }
2082
2083 ret = of_property_read_u32_array(node_interface, "rx-pool", temp, 2);
2084 if (ret < 0) {
2085 dev_err(dev, "missing \"rx-pool\" parameter\n");
2086 ret = -ENODEV;
2087 goto quit;
2088 }
2089 netcp->rx_pool_size = temp[0];
2090 netcp->rx_pool_region_id = temp[1];
2091
2092 ret = of_property_read_u32_array(node_interface, "tx-pool", temp, 2);
2093 if (ret < 0) {
2094 dev_err(dev, "missing \"tx-pool\" parameter\n");
2095 ret = -ENODEV;
2096 goto quit;
2097 }
2098 netcp->tx_pool_size = temp[0];
2099 netcp->tx_pool_region_id = temp[1];
2100
2101 if (netcp->tx_pool_size < MAX_SKB_FRAGS) {
2102 dev_err(dev, "tx-pool size too small, must be atleast(%ld)\n",
2103 MAX_SKB_FRAGS);
2104 ret = -ENODEV;
2105 goto quit;
2106 }
2107
2108 ret = of_property_read_u32(node_interface, "tx-completion-queue",
2109 &netcp->tx_compl_qid);
2110 if (ret < 0) {
2111 dev_warn(dev, "missing \"tx-completion-queue\" parameter\n");
2112 netcp->tx_compl_qid = KNAV_QUEUE_QPEND;
2113 }
2114
2115
2116 netif_napi_add(ndev, &netcp->rx_napi, netcp_rx_poll, NETCP_NAPI_WEIGHT);
2117 netif_tx_napi_add(ndev, &netcp->tx_napi, netcp_tx_poll, NETCP_NAPI_WEIGHT);
2118
2119
2120 ndev->dev_id = 0;
2121 ndev->watchdog_timeo = NETCP_TX_TIMEOUT;
2122 ndev->netdev_ops = &netcp_netdev_ops;
2123 SET_NETDEV_DEV(ndev, dev);
2124
2125 list_add_tail(&netcp->interface_list, &netcp_device->interface_head);
2126 return 0;
2127
2128quit:
2129 free_netdev(ndev);
2130 return ret;
2131}
2132
2133static void netcp_delete_interface(struct netcp_device *netcp_device,
2134 struct net_device *ndev)
2135{
2136 struct netcp_intf_modpriv *intf_modpriv, *tmp;
2137 struct netcp_intf *netcp = netdev_priv(ndev);
2138 struct netcp_module *module;
2139
2140 dev_dbg(netcp_device->device, "Removing interface \"%s\"\n",
2141 ndev->name);
2142
2143
2144 list_for_each_entry_safe(intf_modpriv, tmp, &netcp->module_head,
2145 intf_list) {
2146 module = intf_modpriv->netcp_module;
2147 dev_dbg(netcp_device->device, "Releasing module \"%s\"\n",
2148 module->name);
2149 if (module->release)
2150 module->release(intf_modpriv->module_priv);
2151 list_del(&intf_modpriv->intf_list);
2152 }
2153 WARN(!list_empty(&netcp->module_head), "%s interface module list is not empty!\n",
2154 ndev->name);
2155
2156 list_del(&netcp->interface_list);
2157
2158 of_node_put(netcp->node_interface);
2159 unregister_netdev(ndev);
2160 free_netdev(ndev);
2161}
2162
2163static int netcp_probe(struct platform_device *pdev)
2164{
2165 struct device_node *node = pdev->dev.of_node;
2166 struct netcp_intf *netcp_intf, *netcp_tmp;
2167 struct device_node *child, *interfaces;
2168 struct netcp_device *netcp_device;
2169 struct device *dev = &pdev->dev;
2170 struct netcp_module *module;
2171 int ret;
2172
2173 if (!knav_dma_device_ready() ||
2174 !knav_qmss_device_ready())
2175 return -EPROBE_DEFER;
2176
2177 if (!node) {
2178 dev_err(dev, "could not find device info\n");
2179 return -ENODEV;
2180 }
2181
2182
2183 netcp_device = devm_kzalloc(dev, sizeof(*netcp_device), GFP_KERNEL);
2184 if (!netcp_device)
2185 return -ENOMEM;
2186
2187 pm_runtime_enable(&pdev->dev);
2188 ret = pm_runtime_get_sync(&pdev->dev);
2189 if (ret < 0) {
2190 dev_err(dev, "Failed to enable NETCP power-domain\n");
2191 pm_runtime_disable(&pdev->dev);
2192 return ret;
2193 }
2194
2195
2196 INIT_LIST_HEAD(&netcp_device->interface_head);
2197 INIT_LIST_HEAD(&netcp_device->modpriv_head);
2198 netcp_device->device = dev;
2199 platform_set_drvdata(pdev, netcp_device);
2200
2201
2202 interfaces = of_get_child_by_name(node, "netcp-interfaces");
2203 if (!interfaces) {
2204 dev_err(dev, "could not find netcp-interfaces node\n");
2205 ret = -ENODEV;
2206 goto probe_quit;
2207 }
2208
2209 for_each_available_child_of_node(interfaces, child) {
2210 ret = netcp_create_interface(netcp_device, child);
2211 if (ret) {
2212 dev_err(dev, "could not create interface(%s)\n",
2213 child->name);
2214 goto probe_quit_interface;
2215 }
2216 }
2217
2218 of_node_put(interfaces);
2219
2220
2221 list_add_tail(&netcp_device->device_list, &netcp_devices);
2222
2223
2224 mutex_lock(&netcp_modules_lock);
2225 for_each_netcp_module(module) {
2226 ret = netcp_module_probe(netcp_device, module);
2227 if (ret < 0)
2228 dev_err(dev, "module(%s) probe failed\n", module->name);
2229 }
2230 mutex_unlock(&netcp_modules_lock);
2231 return 0;
2232
2233probe_quit_interface:
2234 list_for_each_entry_safe(netcp_intf, netcp_tmp,
2235 &netcp_device->interface_head,
2236 interface_list) {
2237 netcp_delete_interface(netcp_device, netcp_intf->ndev);
2238 }
2239
2240 of_node_put(interfaces);
2241
2242probe_quit:
2243 pm_runtime_put_sync(&pdev->dev);
2244 pm_runtime_disable(&pdev->dev);
2245 platform_set_drvdata(pdev, NULL);
2246 return ret;
2247}
2248
2249static int netcp_remove(struct platform_device *pdev)
2250{
2251 struct netcp_device *netcp_device = platform_get_drvdata(pdev);
2252 struct netcp_intf *netcp_intf, *netcp_tmp;
2253 struct netcp_inst_modpriv *inst_modpriv, *tmp;
2254 struct netcp_module *module;
2255
2256 list_for_each_entry_safe(inst_modpriv, tmp, &netcp_device->modpriv_head,
2257 inst_list) {
2258 module = inst_modpriv->netcp_module;
2259 dev_dbg(&pdev->dev, "Removing module \"%s\"\n", module->name);
2260 module->remove(netcp_device, inst_modpriv->module_priv);
2261 list_del(&inst_modpriv->inst_list);
2262 }
2263
2264
2265 list_for_each_entry_safe(netcp_intf, netcp_tmp,
2266 &netcp_device->interface_head,
2267 interface_list) {
2268 netcp_delete_interface(netcp_device, netcp_intf->ndev);
2269 }
2270
2271 WARN(!list_empty(&netcp_device->interface_head),
2272 "%s interface list not empty!\n", pdev->name);
2273
2274 pm_runtime_put_sync(&pdev->dev);
2275 pm_runtime_disable(&pdev->dev);
2276 platform_set_drvdata(pdev, NULL);
2277 return 0;
2278}
2279
2280static const struct of_device_id of_match[] = {
2281 { .compatible = "ti,netcp-1.0", },
2282 {},
2283};
2284MODULE_DEVICE_TABLE(of, of_match);
2285
2286static struct platform_driver netcp_driver = {
2287 .driver = {
2288 .name = "netcp-1.0",
2289 .of_match_table = of_match,
2290 },
2291 .probe = netcp_probe,
2292 .remove = netcp_remove,
2293};
2294module_platform_driver(netcp_driver);
2295
2296MODULE_LICENSE("GPL v2");
2297MODULE_DESCRIPTION("TI NETCP driver for Keystone SOCs");
2298MODULE_AUTHOR("Sandeep Nair <sandeep_n@ti.com");
2299