2 * Keystone NetCP Core driver
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>
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.
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.
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>
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
41 #define NETCP_EFUSE_REG_INDEX 0
43 #define NETCP_MOD_PROBE_SKIPPED 1
44 #define NETCP_MOD_PROBE_FAILED 2
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 | \
55 #define NETCP_EFUSE_ADDR_SWAP 2
57 #define knav_queue_get_id(q) knav_queue_device_control(q, \
58 KNAV_QUEUE_GET_ID, (unsigned long)NULL)
60 #define knav_queue_enable_notify(q) knav_queue_device_control(q, \
61 KNAV_QUEUE_ENABLE_NOTIFY, \
64 #define knav_queue_disable_notify(q) knav_queue_device_control(q, \
65 KNAV_QUEUE_DISABLE_NOTIFY, \
68 #define knav_queue_get_count(q) knav_queue_device_control(q, \
69 KNAV_QUEUE_GET_COUNT, (unsigned long)NULL)
71 #define for_each_netcp_module(module) \
72 list_for_each_entry(module, &netcp_modules, module_list)
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)
78 #define for_each_module(netcp, intf_modpriv) \
79 list_for_each_entry(intf_modpriv, &netcp->module_head, intf_list)
81 /* Module management structures */
83 struct list_head device_list;
84 struct list_head interface_head;
85 struct list_head modpriv_head;
86 struct device *device;
89 struct netcp_inst_modpriv {
90 struct netcp_device *netcp_device;
91 struct netcp_module *netcp_module;
92 struct list_head inst_list;
96 struct netcp_intf_modpriv {
97 struct netcp_intf *netcp_priv;
98 struct netcp_module *netcp_module;
99 struct list_head intf_list;
103 static LIST_HEAD(netcp_devices);
104 static LIST_HEAD(netcp_modules);
105 static DEFINE_MUTEX(netcp_modules_lock);
107 static int netcp_debug_level = -1;
108 module_param(netcp_debug_level, int, 0);
109 MODULE_PARM_DESC(netcp_debug_level, "Netcp debug level (NETIF_MSG bits) (0=none,...,16=all)");
111 /* Helper functions - Get/Set */
112 static void get_pkt_info(u32 *buff, u32 *buff_len, u32 *ndesc,
113 struct knav_dma_desc *desc)
115 *buff_len = desc->buff_len;
117 *ndesc = desc->next_desc;
120 static void get_pad_info(u32 *pad0, u32 *pad1, struct knav_dma_desc *desc)
122 *pad0 = desc->pad[0];
123 *pad1 = desc->pad[1];
126 static void get_org_pkt_info(u32 *buff, u32 *buff_len,
127 struct knav_dma_desc *desc)
129 *buff = desc->orig_buff;
130 *buff_len = desc->orig_len;
133 static void get_words(u32 *words, int num_words, u32 *desc)
137 for (i = 0; i < num_words; i++)
141 static void set_pkt_info(u32 buff, u32 buff_len, u32 ndesc,
142 struct knav_dma_desc *desc)
144 desc->buff_len = buff_len;
146 desc->next_desc = ndesc;
149 static void set_desc_info(u32 desc_info, u32 pkt_info,
150 struct knav_dma_desc *desc)
152 desc->desc_info = desc_info;
153 desc->packet_info = pkt_info;
156 static void set_pad_info(u32 pad0, u32 pad1, struct knav_dma_desc *desc)
162 static void set_org_pkt_info(u32 buff, u32 buff_len,
163 struct knav_dma_desc *desc)
165 desc->orig_buff = buff;
166 desc->orig_len = buff_len;
169 static void set_words(u32 *words, int num_words, u32 *desc)
173 for (i = 0; i < num_words; i++)
177 /* Read the e-fuse value as 32 bit values to be endian independent */
178 static int emac_arch_get_mac_addr(char *x, void __iomem *efuse_mac, u32 swap)
180 unsigned int addr0, addr1;
182 addr1 = readl(efuse_mac + 4);
183 addr0 = readl(efuse_mac);
186 case NETCP_EFUSE_ADDR_SWAP:
188 addr1 = readl(efuse_mac);
194 x[0] = (addr1 & 0x0000ff00) >> 8;
195 x[1] = addr1 & 0x000000ff;
196 x[2] = (addr0 & 0xff000000) >> 24;
197 x[3] = (addr0 & 0x00ff0000) >> 16;
198 x[4] = (addr0 & 0x0000ff00) >> 8;
199 x[5] = addr0 & 0x000000ff;
204 static const char *netcp_node_name(struct device_node *node)
208 if (of_property_read_string(node, "label", &name) < 0)
215 /* Module management routines */
216 static int netcp_register_interface(struct netcp_intf *netcp)
220 ret = register_netdev(netcp->ndev);
222 netcp->netdev_registered = true;
226 static int netcp_module_probe(struct netcp_device *netcp_device,
227 struct netcp_module *module)
229 struct device *dev = netcp_device->device;
230 struct device_node *devices, *interface, *node = dev->of_node;
231 struct device_node *child;
232 struct netcp_inst_modpriv *inst_modpriv;
233 struct netcp_intf *netcp_intf;
234 struct netcp_module *tmp;
235 bool primary_module_registered = false;
238 /* Find this module in the sub-tree for this device */
239 devices = of_get_child_by_name(node, "netcp-devices");
241 dev_err(dev, "could not find netcp-devices node\n");
242 return NETCP_MOD_PROBE_SKIPPED;
245 for_each_available_child_of_node(devices, child) {
246 const char *name = netcp_node_name(child);
248 if (!strcasecmp(module->name, name))
252 of_node_put(devices);
253 /* If module not used for this device, skip it */
255 dev_warn(dev, "module(%s) not used for device\n", module->name);
256 return NETCP_MOD_PROBE_SKIPPED;
259 inst_modpriv = devm_kzalloc(dev, sizeof(*inst_modpriv), GFP_KERNEL);
265 inst_modpriv->netcp_device = netcp_device;
266 inst_modpriv->netcp_module = module;
267 list_add_tail(&inst_modpriv->inst_list, &netcp_device->modpriv_head);
269 ret = module->probe(netcp_device, dev, child,
270 &inst_modpriv->module_priv);
273 dev_err(dev, "Probe of module(%s) failed with %d\n",
275 list_del(&inst_modpriv->inst_list);
276 devm_kfree(dev, inst_modpriv);
277 return NETCP_MOD_PROBE_FAILED;
280 /* Attach modules only if the primary module is probed */
281 for_each_netcp_module(tmp) {
283 primary_module_registered = true;
286 if (!primary_module_registered)
289 /* Attach module to interfaces */
290 list_for_each_entry(netcp_intf, &netcp_device->interface_head,
292 struct netcp_intf_modpriv *intf_modpriv;
294 intf_modpriv = devm_kzalloc(dev, sizeof(*intf_modpriv),
299 interface = of_parse_phandle(netcp_intf->node_interface,
303 devm_kfree(dev, intf_modpriv);
307 intf_modpriv->netcp_priv = netcp_intf;
308 intf_modpriv->netcp_module = module;
309 list_add_tail(&intf_modpriv->intf_list,
310 &netcp_intf->module_head);
312 ret = module->attach(inst_modpriv->module_priv,
313 netcp_intf->ndev, interface,
314 &intf_modpriv->module_priv);
315 of_node_put(interface);
317 dev_dbg(dev, "Attach of module %s declined with %d\n",
319 list_del(&intf_modpriv->intf_list);
320 devm_kfree(dev, intf_modpriv);
325 /* Now register the interface with netdev */
326 list_for_each_entry(netcp_intf,
327 &netcp_device->interface_head,
329 /* If interface not registered then register now */
330 if (!netcp_intf->netdev_registered) {
331 ret = netcp_register_interface(netcp_intf);
339 int netcp_register_module(struct netcp_module *module)
341 struct netcp_device *netcp_device;
342 struct netcp_module *tmp;
346 WARN(1, "error registering netcp module: no name\n");
350 if (!module->probe) {
351 WARN(1, "error registering netcp module: no probe\n");
355 mutex_lock(&netcp_modules_lock);
357 for_each_netcp_module(tmp) {
358 if (!strcasecmp(tmp->name, module->name)) {
359 mutex_unlock(&netcp_modules_lock);
363 list_add_tail(&module->module_list, &netcp_modules);
365 list_for_each_entry(netcp_device, &netcp_devices, device_list) {
366 ret = netcp_module_probe(netcp_device, module);
370 mutex_unlock(&netcp_modules_lock);
374 mutex_unlock(&netcp_modules_lock);
375 netcp_unregister_module(module);
378 EXPORT_SYMBOL_GPL(netcp_register_module);
380 static void netcp_release_module(struct netcp_device *netcp_device,
381 struct netcp_module *module)
383 struct netcp_inst_modpriv *inst_modpriv, *inst_tmp;
384 struct netcp_intf *netcp_intf, *netcp_tmp;
385 struct device *dev = netcp_device->device;
387 /* Release the module from each interface */
388 list_for_each_entry_safe(netcp_intf, netcp_tmp,
389 &netcp_device->interface_head,
391 struct netcp_intf_modpriv *intf_modpriv, *intf_tmp;
393 list_for_each_entry_safe(intf_modpriv, intf_tmp,
394 &netcp_intf->module_head,
396 if (intf_modpriv->netcp_module == module) {
397 module->release(intf_modpriv->module_priv);
398 list_del(&intf_modpriv->intf_list);
399 devm_kfree(dev, intf_modpriv);
405 /* Remove the module from each instance */
406 list_for_each_entry_safe(inst_modpriv, inst_tmp,
407 &netcp_device->modpriv_head, inst_list) {
408 if (inst_modpriv->netcp_module == module) {
409 module->remove(netcp_device,
410 inst_modpriv->module_priv);
411 list_del(&inst_modpriv->inst_list);
412 devm_kfree(dev, inst_modpriv);
418 void netcp_unregister_module(struct netcp_module *module)
420 struct netcp_device *netcp_device;
421 struct netcp_module *module_tmp;
423 mutex_lock(&netcp_modules_lock);
425 list_for_each_entry(netcp_device, &netcp_devices, device_list) {
426 netcp_release_module(netcp_device, module);
429 /* Remove the module from the module list */
430 for_each_netcp_module(module_tmp) {
431 if (module == module_tmp) {
432 list_del(&module->module_list);
437 mutex_unlock(&netcp_modules_lock);
439 EXPORT_SYMBOL_GPL(netcp_unregister_module);
441 void *netcp_module_get_intf_data(struct netcp_module *module,
442 struct netcp_intf *intf)
444 struct netcp_intf_modpriv *intf_modpriv;
446 list_for_each_entry(intf_modpriv, &intf->module_head, intf_list)
447 if (intf_modpriv->netcp_module == module)
448 return intf_modpriv->module_priv;
451 EXPORT_SYMBOL_GPL(netcp_module_get_intf_data);
453 /* Module TX and RX Hook management */
454 struct netcp_hook_list {
455 struct list_head list;
456 netcp_hook_rtn *hook_rtn;
461 int netcp_register_txhook(struct netcp_intf *netcp_priv, int order,
462 netcp_hook_rtn *hook_rtn, void *hook_data)
464 struct netcp_hook_list *entry;
465 struct netcp_hook_list *next;
468 entry = devm_kzalloc(netcp_priv->dev, sizeof(*entry), GFP_KERNEL);
472 entry->hook_rtn = hook_rtn;
473 entry->hook_data = hook_data;
474 entry->order = order;
476 spin_lock_irqsave(&netcp_priv->lock, flags);
477 list_for_each_entry(next, &netcp_priv->txhook_list_head, list) {
478 if (next->order > order)
481 __list_add(&entry->list, next->list.prev, &next->list);
482 spin_unlock_irqrestore(&netcp_priv->lock, flags);
486 EXPORT_SYMBOL_GPL(netcp_register_txhook);
488 int netcp_unregister_txhook(struct netcp_intf *netcp_priv, int order,
489 netcp_hook_rtn *hook_rtn, void *hook_data)
491 struct netcp_hook_list *next, *n;
494 spin_lock_irqsave(&netcp_priv->lock, flags);
495 list_for_each_entry_safe(next, n, &netcp_priv->txhook_list_head, list) {
496 if ((next->order == order) &&
497 (next->hook_rtn == hook_rtn) &&
498 (next->hook_data == hook_data)) {
499 list_del(&next->list);
500 spin_unlock_irqrestore(&netcp_priv->lock, flags);
501 devm_kfree(netcp_priv->dev, next);
505 spin_unlock_irqrestore(&netcp_priv->lock, flags);
508 EXPORT_SYMBOL_GPL(netcp_unregister_txhook);
510 int netcp_register_rxhook(struct netcp_intf *netcp_priv, int order,
511 netcp_hook_rtn *hook_rtn, void *hook_data)
513 struct netcp_hook_list *entry;
514 struct netcp_hook_list *next;
517 entry = devm_kzalloc(netcp_priv->dev, sizeof(*entry), GFP_KERNEL);
521 entry->hook_rtn = hook_rtn;
522 entry->hook_data = hook_data;
523 entry->order = order;
525 spin_lock_irqsave(&netcp_priv->lock, flags);
526 list_for_each_entry(next, &netcp_priv->rxhook_list_head, list) {
527 if (next->order > order)
530 __list_add(&entry->list, next->list.prev, &next->list);
531 spin_unlock_irqrestore(&netcp_priv->lock, flags);
536 int netcp_unregister_rxhook(struct netcp_intf *netcp_priv, int order,
537 netcp_hook_rtn *hook_rtn, void *hook_data)
539 struct netcp_hook_list *next, *n;
542 spin_lock_irqsave(&netcp_priv->lock, flags);
543 list_for_each_entry_safe(next, n, &netcp_priv->rxhook_list_head, list) {
544 if ((next->order == order) &&
545 (next->hook_rtn == hook_rtn) &&
546 (next->hook_data == hook_data)) {
547 list_del(&next->list);
548 spin_unlock_irqrestore(&netcp_priv->lock, flags);
549 devm_kfree(netcp_priv->dev, next);
553 spin_unlock_irqrestore(&netcp_priv->lock, flags);
558 static void netcp_frag_free(bool is_frag, void *ptr)
566 static void netcp_free_rx_desc_chain(struct netcp_intf *netcp,
567 struct knav_dma_desc *desc)
569 struct knav_dma_desc *ndesc;
570 dma_addr_t dma_desc, dma_buf;
571 unsigned int buf_len, dma_sz = sizeof(*ndesc);
575 get_words(&dma_desc, 1, &desc->next_desc);
578 ndesc = knav_pool_desc_unmap(netcp->rx_pool, dma_desc, dma_sz);
579 if (unlikely(!ndesc)) {
580 dev_err(netcp->ndev_dev, "failed to unmap Rx desc\n");
583 get_pkt_info(&dma_buf, &tmp, &dma_desc, ndesc);
584 get_pad_info((u32 *)&buf_ptr, &tmp, ndesc);
585 dma_unmap_page(netcp->dev, dma_buf, PAGE_SIZE, DMA_FROM_DEVICE);
586 __free_page(buf_ptr);
587 knav_pool_desc_put(netcp->rx_pool, desc);
590 get_pad_info((u32 *)&buf_ptr, &buf_len, desc);
592 netcp_frag_free(buf_len <= PAGE_SIZE, buf_ptr);
593 knav_pool_desc_put(netcp->rx_pool, desc);
596 static void netcp_empty_rx_queue(struct netcp_intf *netcp)
598 struct knav_dma_desc *desc;
603 dma = knav_queue_pop(netcp->rx_queue, &dma_sz);
607 desc = knav_pool_desc_unmap(netcp->rx_pool, dma, dma_sz);
608 if (unlikely(!desc)) {
609 dev_err(netcp->ndev_dev, "%s: failed to unmap Rx desc\n",
611 netcp->ndev->stats.rx_errors++;
614 netcp_free_rx_desc_chain(netcp, desc);
615 netcp->ndev->stats.rx_dropped++;
619 static int netcp_process_one_rx_packet(struct netcp_intf *netcp)
621 unsigned int dma_sz, buf_len, org_buf_len;
622 struct knav_dma_desc *desc, *ndesc;
623 unsigned int pkt_sz = 0, accum_sz;
624 struct netcp_hook_list *rx_hook;
625 dma_addr_t dma_desc, dma_buff;
626 struct netcp_packet p_info;
631 dma_desc = knav_queue_pop(netcp->rx_queue, &dma_sz);
635 desc = knav_pool_desc_unmap(netcp->rx_pool, dma_desc, dma_sz);
636 if (unlikely(!desc)) {
637 dev_err(netcp->ndev_dev, "failed to unmap Rx desc\n");
641 get_pkt_info(&dma_buff, &buf_len, &dma_desc, desc);
642 get_pad_info((u32 *)&org_buf_ptr, &org_buf_len, desc);
644 if (unlikely(!org_buf_ptr)) {
645 dev_err(netcp->ndev_dev, "NULL bufptr in desc\n");
649 pkt_sz &= KNAV_DMA_DESC_PKT_LEN_MASK;
651 dma_unmap_single(netcp->dev, dma_buff, buf_len, DMA_FROM_DEVICE);
653 /* Build a new sk_buff for the primary buffer */
654 skb = build_skb(org_buf_ptr, org_buf_len);
655 if (unlikely(!skb)) {
656 dev_err(netcp->ndev_dev, "build_skb() failed\n");
660 /* update data, tail and len */
661 skb_reserve(skb, NETCP_SOP_OFFSET);
662 __skb_put(skb, buf_len);
664 /* Fill in the page fragment list */
668 ndesc = knav_pool_desc_unmap(netcp->rx_pool, dma_desc, dma_sz);
669 if (unlikely(!ndesc)) {
670 dev_err(netcp->ndev_dev, "failed to unmap Rx desc\n");
674 get_pkt_info(&dma_buff, &buf_len, &dma_desc, ndesc);
675 get_pad_info((u32 *)&page, &tmp, ndesc);
677 if (likely(dma_buff && buf_len && page)) {
678 dma_unmap_page(netcp->dev, dma_buff, PAGE_SIZE,
681 dev_err(netcp->ndev_dev, "Bad Rx desc dma_buff(%p), len(%d), page(%p)\n",
682 (void *)dma_buff, buf_len, page);
686 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
687 offset_in_page(dma_buff), buf_len, PAGE_SIZE);
690 /* Free the descriptor */
691 knav_pool_desc_put(netcp->rx_pool, ndesc);
694 /* Free the primary descriptor */
695 knav_pool_desc_put(netcp->rx_pool, desc);
697 /* check for packet len and warn */
698 if (unlikely(pkt_sz != accum_sz))
699 dev_dbg(netcp->ndev_dev, "mismatch in packet size(%d) & sum of fragments(%d)\n",
702 /* Remove ethernet FCS from the packet */
703 __pskb_trim(skb, skb->len - ETH_FCS_LEN);
705 /* Call each of the RX hooks */
707 p_info.rxtstamp_complete = false;
708 list_for_each_entry(rx_hook, &netcp->rxhook_list_head, list) {
711 ret = rx_hook->hook_rtn(rx_hook->order, rx_hook->hook_data,
714 dev_err(netcp->ndev_dev, "RX hook %d failed: %d\n",
715 rx_hook->order, ret);
716 netcp->ndev->stats.rx_errors++;
722 netcp->ndev->stats.rx_packets++;
723 netcp->ndev->stats.rx_bytes += skb->len;
725 /* push skb up the stack */
726 skb->protocol = eth_type_trans(skb, netcp->ndev);
727 netif_receive_skb(skb);
731 netcp_free_rx_desc_chain(netcp, desc);
732 netcp->ndev->stats.rx_errors++;
736 static int netcp_process_rx_packets(struct netcp_intf *netcp,
741 for (i = 0; (i < budget) && !netcp_process_one_rx_packet(netcp); i++)
746 /* Release descriptors and attached buffers from Rx FDQ */
747 static void netcp_free_rx_buf(struct netcp_intf *netcp, int fdq)
749 struct knav_dma_desc *desc;
750 unsigned int buf_len, dma_sz;
755 /* Allocate descriptor */
756 while ((dma = knav_queue_pop(netcp->rx_fdq[fdq], &dma_sz))) {
757 desc = knav_pool_desc_unmap(netcp->rx_pool, dma, dma_sz);
758 if (unlikely(!desc)) {
759 dev_err(netcp->ndev_dev, "failed to unmap Rx desc\n");
763 get_org_pkt_info(&dma, &buf_len, desc);
764 get_pad_info((u32 *)&buf_ptr, &tmp, desc);
766 if (unlikely(!dma)) {
767 dev_err(netcp->ndev_dev, "NULL orig_buff in desc\n");
768 knav_pool_desc_put(netcp->rx_pool, desc);
772 if (unlikely(!buf_ptr)) {
773 dev_err(netcp->ndev_dev, "NULL bufptr in desc\n");
774 knav_pool_desc_put(netcp->rx_pool, desc);
779 dma_unmap_single(netcp->dev, dma, buf_len,
781 netcp_frag_free((buf_len <= PAGE_SIZE), buf_ptr);
783 dma_unmap_page(netcp->dev, dma, buf_len,
785 __free_page(buf_ptr);
788 knav_pool_desc_put(netcp->rx_pool, desc);
792 static void netcp_rxpool_free(struct netcp_intf *netcp)
796 for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN &&
797 !IS_ERR_OR_NULL(netcp->rx_fdq[i]); i++)
798 netcp_free_rx_buf(netcp, i);
800 if (knav_pool_count(netcp->rx_pool) != netcp->rx_pool_size)
801 dev_err(netcp->ndev_dev, "Lost Rx (%d) descriptors\n",
802 netcp->rx_pool_size - knav_pool_count(netcp->rx_pool));
804 knav_pool_destroy(netcp->rx_pool);
805 netcp->rx_pool = NULL;
808 static int netcp_allocate_rx_buf(struct netcp_intf *netcp, int fdq)
810 struct knav_dma_desc *hwdesc;
811 unsigned int buf_len, dma_sz;
812 u32 desc_info, pkt_info;
818 /* Allocate descriptor */
819 hwdesc = knav_pool_desc_get(netcp->rx_pool);
820 if (IS_ERR_OR_NULL(hwdesc)) {
821 dev_dbg(netcp->ndev_dev, "out of rx pool desc\n");
825 if (likely(fdq == 0)) {
826 unsigned int primary_buf_len;
827 /* Allocate a primary receive queue entry */
828 buf_len = NETCP_PACKET_SIZE + NETCP_SOP_OFFSET;
829 primary_buf_len = SKB_DATA_ALIGN(buf_len) +
830 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
832 bufptr = netdev_alloc_frag(primary_buf_len);
833 pad[1] = primary_buf_len;
835 if (unlikely(!bufptr)) {
836 dev_warn_ratelimited(netcp->ndev_dev,
837 "Primary RX buffer alloc failed\n");
840 dma = dma_map_single(netcp->dev, bufptr, buf_len,
842 if (unlikely(dma_mapping_error(netcp->dev, dma)))
845 pad[0] = (u32)bufptr;
848 /* Allocate a secondary receive queue entry */
849 page = alloc_page(GFP_ATOMIC | GFP_DMA | __GFP_COLD);
850 if (unlikely(!page)) {
851 dev_warn_ratelimited(netcp->ndev_dev, "Secondary page alloc failed\n");
855 dma = dma_map_page(netcp->dev, page, 0, buf_len, DMA_TO_DEVICE);
860 desc_info = KNAV_DMA_DESC_PS_INFO_IN_DESC;
861 desc_info |= buf_len & KNAV_DMA_DESC_PKT_LEN_MASK;
862 pkt_info = KNAV_DMA_DESC_HAS_EPIB;
863 pkt_info |= KNAV_DMA_NUM_PS_WORDS << KNAV_DMA_DESC_PSLEN_SHIFT;
864 pkt_info |= (netcp->rx_queue_id & KNAV_DMA_DESC_RETQ_MASK) <<
865 KNAV_DMA_DESC_RETQ_SHIFT;
866 set_org_pkt_info(dma, buf_len, hwdesc);
867 set_pad_info(pad[0], pad[1], hwdesc);
868 set_desc_info(desc_info, pkt_info, hwdesc);
871 knav_pool_desc_map(netcp->rx_pool, hwdesc, sizeof(*hwdesc), &dma,
873 knav_queue_push(netcp->rx_fdq[fdq], dma, sizeof(*hwdesc), 0);
877 knav_pool_desc_put(netcp->rx_pool, hwdesc);
881 /* Refill Rx FDQ with descriptors & attached buffers */
882 static void netcp_rxpool_refill(struct netcp_intf *netcp)
884 u32 fdq_deficit[KNAV_DMA_FDQ_PER_CHAN] = {0};
887 /* Calculate the FDQ deficit and refill */
888 for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN && netcp->rx_fdq[i]; i++) {
889 fdq_deficit[i] = netcp->rx_queue_depths[i] -
890 knav_queue_get_count(netcp->rx_fdq[i]);
892 while (fdq_deficit[i]-- && !ret)
893 ret = netcp_allocate_rx_buf(netcp, i);
898 static int netcp_rx_poll(struct napi_struct *napi, int budget)
900 struct netcp_intf *netcp = container_of(napi, struct netcp_intf,
902 unsigned int packets;
904 packets = netcp_process_rx_packets(netcp, budget);
906 netcp_rxpool_refill(netcp);
907 if (packets < budget) {
908 napi_complete(&netcp->rx_napi);
909 knav_queue_enable_notify(netcp->rx_queue);
915 static void netcp_rx_notify(void *arg)
917 struct netcp_intf *netcp = arg;
919 knav_queue_disable_notify(netcp->rx_queue);
920 napi_schedule(&netcp->rx_napi);
923 static void netcp_free_tx_desc_chain(struct netcp_intf *netcp,
924 struct knav_dma_desc *desc,
925 unsigned int desc_sz)
927 struct knav_dma_desc *ndesc = desc;
928 dma_addr_t dma_desc, dma_buf;
929 unsigned int buf_len;
932 get_pkt_info(&dma_buf, &buf_len, &dma_desc, ndesc);
934 if (dma_buf && buf_len)
935 dma_unmap_single(netcp->dev, dma_buf, buf_len,
938 dev_warn(netcp->ndev_dev, "bad Tx desc buf(%p), len(%d)\n",
939 (void *)dma_buf, buf_len);
941 knav_pool_desc_put(netcp->tx_pool, ndesc);
944 ndesc = knav_pool_desc_unmap(netcp->tx_pool, dma_desc,
947 dev_err(netcp->ndev_dev, "failed to unmap Tx desc\n");
952 static int netcp_process_tx_compl_packets(struct netcp_intf *netcp,
955 struct knav_dma_desc *desc;
963 dma = knav_queue_pop(netcp->tx_compl_q, &dma_sz);
966 desc = knav_pool_desc_unmap(netcp->tx_pool, dma, dma_sz);
967 if (unlikely(!desc)) {
968 dev_err(netcp->ndev_dev, "failed to unmap Tx desc\n");
969 netcp->ndev->stats.tx_errors++;
973 get_pad_info((u32 *)&skb, &tmp, desc);
974 netcp_free_tx_desc_chain(netcp, desc, dma_sz);
976 dev_err(netcp->ndev_dev, "No skb in Tx desc\n");
977 netcp->ndev->stats.tx_errors++;
981 if (netif_subqueue_stopped(netcp->ndev, skb) &&
982 netif_running(netcp->ndev) &&
983 (knav_pool_count(netcp->tx_pool) >
984 netcp->tx_resume_threshold)) {
985 u16 subqueue = skb_get_queue_mapping(skb);
987 netif_wake_subqueue(netcp->ndev, subqueue);
990 netcp->ndev->stats.tx_packets++;
991 netcp->ndev->stats.tx_bytes += skb->len;
998 static int netcp_tx_poll(struct napi_struct *napi, int budget)
1001 struct netcp_intf *netcp = container_of(napi, struct netcp_intf,
1004 packets = netcp_process_tx_compl_packets(netcp, budget);
1005 if (packets < budget) {
1006 napi_complete(&netcp->tx_napi);
1007 knav_queue_enable_notify(netcp->tx_compl_q);
1013 static void netcp_tx_notify(void *arg)
1015 struct netcp_intf *netcp = arg;
1017 knav_queue_disable_notify(netcp->tx_compl_q);
1018 napi_schedule(&netcp->tx_napi);
1021 static struct knav_dma_desc*
1022 netcp_tx_map_skb(struct sk_buff *skb, struct netcp_intf *netcp)
1024 struct knav_dma_desc *desc, *ndesc, *pdesc;
1025 unsigned int pkt_len = skb_headlen(skb);
1026 struct device *dev = netcp->dev;
1027 dma_addr_t dma_addr;
1028 unsigned int dma_sz;
1031 /* Map the linear buffer */
1032 dma_addr = dma_map_single(dev, skb->data, pkt_len, DMA_TO_DEVICE);
1033 if (unlikely(dma_mapping_error(dev, dma_addr))) {
1034 dev_err(netcp->ndev_dev, "Failed to map skb buffer\n");
1038 desc = knav_pool_desc_get(netcp->tx_pool);
1039 if (IS_ERR_OR_NULL(desc)) {
1040 dev_err(netcp->ndev_dev, "out of TX desc\n");
1041 dma_unmap_single(dev, dma_addr, pkt_len, DMA_TO_DEVICE);
1045 set_pkt_info(dma_addr, pkt_len, 0, desc);
1046 if (skb_is_nonlinear(skb)) {
1047 prefetchw(skb_shinfo(skb));
1049 desc->next_desc = 0;
1055 /* Handle the case where skb is fragmented in pages */
1056 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1057 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1058 struct page *page = skb_frag_page(frag);
1059 u32 page_offset = frag->page_offset;
1060 u32 buf_len = skb_frag_size(frag);
1061 dma_addr_t desc_dma;
1064 dma_addr = dma_map_page(dev, page, page_offset, buf_len,
1066 if (unlikely(!dma_addr)) {
1067 dev_err(netcp->ndev_dev, "Failed to map skb page\n");
1071 ndesc = knav_pool_desc_get(netcp->tx_pool);
1072 if (IS_ERR_OR_NULL(ndesc)) {
1073 dev_err(netcp->ndev_dev, "out of TX desc for frags\n");
1074 dma_unmap_page(dev, dma_addr, buf_len, DMA_TO_DEVICE);
1078 desc_dma = knav_pool_desc_virt_to_dma(netcp->tx_pool,
1081 (netcp->tx_compl_qid & KNAV_DMA_DESC_RETQ_MASK) <<
1082 KNAV_DMA_DESC_RETQ_SHIFT;
1083 set_pkt_info(dma_addr, buf_len, 0, ndesc);
1084 set_words(&desc_dma, 1, &pdesc->next_desc);
1087 knav_pool_desc_map(netcp->tx_pool, pdesc,
1088 sizeof(*pdesc), &desc_dma, &dma_sz);
1092 knav_pool_desc_map(netcp->tx_pool, pdesc, sizeof(*pdesc),
1093 &dma_addr, &dma_sz);
1095 /* frag list based linkage is not supported for now. */
1096 if (skb_shinfo(skb)->frag_list) {
1097 dev_err_ratelimited(netcp->ndev_dev, "NETIF_F_FRAGLIST not supported\n");
1102 WARN_ON(pkt_len != skb->len);
1104 pkt_len &= KNAV_DMA_DESC_PKT_LEN_MASK;
1105 set_words(&pkt_len, 1, &desc->desc_info);
1109 netcp_free_tx_desc_chain(netcp, desc, sizeof(*desc));
1113 static int netcp_tx_submit_skb(struct netcp_intf *netcp,
1114 struct sk_buff *skb,
1115 struct knav_dma_desc *desc)
1117 struct netcp_tx_pipe *tx_pipe = NULL;
1118 struct netcp_hook_list *tx_hook;
1119 struct netcp_packet p_info;
1120 unsigned int dma_sz;
1125 p_info.netcp = netcp;
1127 p_info.tx_pipe = NULL;
1128 p_info.psdata_len = 0;
1129 p_info.ts_context = NULL;
1130 p_info.txtstamp_complete = NULL;
1131 p_info.epib = desc->epib;
1132 p_info.psdata = desc->psdata;
1133 memset(p_info.epib, 0, KNAV_DMA_NUM_EPIB_WORDS * sizeof(u32));
1135 /* Find out where to inject the packet for transmission */
1136 list_for_each_entry(tx_hook, &netcp->txhook_list_head, list) {
1137 ret = tx_hook->hook_rtn(tx_hook->order, tx_hook->hook_data,
1139 if (unlikely(ret != 0)) {
1140 dev_err(netcp->ndev_dev, "TX hook %d rejected the packet with reason(%d)\n",
1141 tx_hook->order, ret);
1142 ret = (ret < 0) ? ret : NETDEV_TX_OK;
1147 /* Make sure some TX hook claimed the packet */
1148 tx_pipe = p_info.tx_pipe;
1150 dev_err(netcp->ndev_dev, "No TX hook claimed the packet!\n");
1155 /* update descriptor */
1156 if (p_info.psdata_len) {
1157 u32 *psdata = p_info.psdata;
1159 memmove(p_info.psdata, p_info.psdata + p_info.psdata_len,
1161 set_words(psdata, p_info.psdata_len, psdata);
1162 tmp |= (p_info.psdata_len & KNAV_DMA_DESC_PSLEN_MASK) <<
1163 KNAV_DMA_DESC_PSLEN_SHIFT;
1166 tmp |= KNAV_DMA_DESC_HAS_EPIB |
1167 ((netcp->tx_compl_qid & KNAV_DMA_DESC_RETQ_MASK) <<
1168 KNAV_DMA_DESC_RETQ_SHIFT);
1170 if (!(tx_pipe->flags & SWITCH_TO_PORT_IN_TAGINFO)) {
1171 tmp |= ((tx_pipe->switch_to_port & KNAV_DMA_DESC_PSFLAG_MASK) <<
1172 KNAV_DMA_DESC_PSFLAG_SHIFT);
1175 set_words(&tmp, 1, &desc->packet_info);
1176 set_words((u32 *)&skb, 1, &desc->pad[0]);
1178 if (tx_pipe->flags & SWITCH_TO_PORT_IN_TAGINFO) {
1179 tmp = tx_pipe->switch_to_port;
1180 set_words((u32 *)&tmp, 1, &desc->tag_info);
1183 /* submit packet descriptor */
1184 ret = knav_pool_desc_map(netcp->tx_pool, desc, sizeof(*desc), &dma,
1186 if (unlikely(ret)) {
1187 dev_err(netcp->ndev_dev, "%s() failed to map desc\n", __func__);
1191 skb_tx_timestamp(skb);
1192 knav_queue_push(tx_pipe->dma_queue, dma, dma_sz, 0);
1198 /* Submit the packet */
1199 static int netcp_ndo_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1201 struct netcp_intf *netcp = netdev_priv(ndev);
1202 int subqueue = skb_get_queue_mapping(skb);
1203 struct knav_dma_desc *desc;
1204 int desc_count, ret = 0;
1206 if (unlikely(skb->len <= 0)) {
1208 return NETDEV_TX_OK;
1211 if (unlikely(skb->len < NETCP_MIN_PACKET_SIZE)) {
1212 ret = skb_padto(skb, NETCP_MIN_PACKET_SIZE);
1214 /* If we get here, the skb has already been dropped */
1215 dev_warn(netcp->ndev_dev, "padding failed (%d), packet dropped\n",
1217 ndev->stats.tx_dropped++;
1220 skb->len = NETCP_MIN_PACKET_SIZE;
1223 desc = netcp_tx_map_skb(skb, netcp);
1224 if (unlikely(!desc)) {
1225 netif_stop_subqueue(ndev, subqueue);
1230 ret = netcp_tx_submit_skb(netcp, skb, desc);
1234 ndev->trans_start = jiffies;
1236 /* Check Tx pool count & stop subqueue if needed */
1237 desc_count = knav_pool_count(netcp->tx_pool);
1238 if (desc_count < netcp->tx_pause_threshold) {
1239 dev_dbg(netcp->ndev_dev, "pausing tx, count(%d)\n", desc_count);
1240 netif_stop_subqueue(ndev, subqueue);
1242 return NETDEV_TX_OK;
1245 ndev->stats.tx_dropped++;
1247 netcp_free_tx_desc_chain(netcp, desc, sizeof(*desc));
1252 int netcp_txpipe_close(struct netcp_tx_pipe *tx_pipe)
1254 if (tx_pipe->dma_channel) {
1255 knav_dma_close_channel(tx_pipe->dma_channel);
1256 tx_pipe->dma_channel = NULL;
1260 EXPORT_SYMBOL_GPL(netcp_txpipe_close);
1262 int netcp_txpipe_open(struct netcp_tx_pipe *tx_pipe)
1264 struct device *dev = tx_pipe->netcp_device->device;
1265 struct knav_dma_cfg config;
1269 memset(&config, 0, sizeof(config));
1270 config.direction = DMA_MEM_TO_DEV;
1271 config.u.tx.filt_einfo = false;
1272 config.u.tx.filt_pswords = false;
1273 config.u.tx.priority = DMA_PRIO_MED_L;
1275 tx_pipe->dma_channel = knav_dma_open_channel(dev,
1276 tx_pipe->dma_chan_name, &config);
1277 if (IS_ERR_OR_NULL(tx_pipe->dma_channel)) {
1278 dev_err(dev, "failed opening tx chan(%s)\n",
1279 tx_pipe->dma_chan_name);
1283 snprintf(name, sizeof(name), "tx-pipe-%s", dev_name(dev));
1284 tx_pipe->dma_queue = knav_queue_open(name, tx_pipe->dma_queue_id,
1286 if (IS_ERR(tx_pipe->dma_queue)) {
1287 ret = PTR_ERR(tx_pipe->dma_queue);
1288 dev_err(dev, "Could not open DMA queue for channel \"%s\": %d\n",
1293 dev_dbg(dev, "opened tx pipe %s\n", name);
1297 if (!IS_ERR_OR_NULL(tx_pipe->dma_channel))
1298 knav_dma_close_channel(tx_pipe->dma_channel);
1299 tx_pipe->dma_channel = NULL;
1302 EXPORT_SYMBOL_GPL(netcp_txpipe_open);
1304 int netcp_txpipe_init(struct netcp_tx_pipe *tx_pipe,
1305 struct netcp_device *netcp_device,
1306 const char *dma_chan_name, unsigned int dma_queue_id)
1308 memset(tx_pipe, 0, sizeof(*tx_pipe));
1309 tx_pipe->netcp_device = netcp_device;
1310 tx_pipe->dma_chan_name = dma_chan_name;
1311 tx_pipe->dma_queue_id = dma_queue_id;
1314 EXPORT_SYMBOL_GPL(netcp_txpipe_init);
1316 static struct netcp_addr *netcp_addr_find(struct netcp_intf *netcp,
1318 enum netcp_addr_type type)
1320 struct netcp_addr *naddr;
1322 list_for_each_entry(naddr, &netcp->addr_list, node) {
1323 if (naddr->type != type)
1325 if (addr && memcmp(addr, naddr->addr, ETH_ALEN))
1333 static struct netcp_addr *netcp_addr_add(struct netcp_intf *netcp,
1335 enum netcp_addr_type type)
1337 struct netcp_addr *naddr;
1339 naddr = devm_kmalloc(netcp->dev, sizeof(*naddr), GFP_ATOMIC);
1345 naddr->netcp = netcp;
1347 ether_addr_copy(naddr->addr, addr);
1349 eth_zero_addr(naddr->addr);
1350 list_add_tail(&naddr->node, &netcp->addr_list);
1355 static void netcp_addr_del(struct netcp_intf *netcp, struct netcp_addr *naddr)
1357 list_del(&naddr->node);
1358 devm_kfree(netcp->dev, naddr);
1361 static void netcp_addr_clear_mark(struct netcp_intf *netcp)
1363 struct netcp_addr *naddr;
1365 list_for_each_entry(naddr, &netcp->addr_list, node)
1369 static void netcp_addr_add_mark(struct netcp_intf *netcp, const u8 *addr,
1370 enum netcp_addr_type type)
1372 struct netcp_addr *naddr;
1374 naddr = netcp_addr_find(netcp, addr, type);
1376 naddr->flags |= ADDR_VALID;
1380 naddr = netcp_addr_add(netcp, addr, type);
1381 if (!WARN_ON(!naddr))
1382 naddr->flags |= ADDR_NEW;
1385 static void netcp_addr_sweep_del(struct netcp_intf *netcp)
1387 struct netcp_addr *naddr, *tmp;
1388 struct netcp_intf_modpriv *priv;
1389 struct netcp_module *module;
1392 list_for_each_entry_safe(naddr, tmp, &netcp->addr_list, node) {
1393 if (naddr->flags & (ADDR_VALID | ADDR_NEW))
1395 dev_dbg(netcp->ndev_dev, "deleting address %pM, type %x\n",
1396 naddr->addr, naddr->type);
1397 for_each_module(netcp, priv) {
1398 module = priv->netcp_module;
1399 if (!module->del_addr)
1401 error = module->del_addr(priv->module_priv,
1405 netcp_addr_del(netcp, naddr);
1409 static void netcp_addr_sweep_add(struct netcp_intf *netcp)
1411 struct netcp_addr *naddr, *tmp;
1412 struct netcp_intf_modpriv *priv;
1413 struct netcp_module *module;
1416 list_for_each_entry_safe(naddr, tmp, &netcp->addr_list, node) {
1417 if (!(naddr->flags & ADDR_NEW))
1419 dev_dbg(netcp->ndev_dev, "adding address %pM, type %x\n",
1420 naddr->addr, naddr->type);
1422 for_each_module(netcp, priv) {
1423 module = priv->netcp_module;
1424 if (!module->add_addr)
1426 error = module->add_addr(priv->module_priv, naddr);
1432 static void netcp_set_rx_mode(struct net_device *ndev)
1434 struct netcp_intf *netcp = netdev_priv(ndev);
1435 struct netdev_hw_addr *ndev_addr;
1438 promisc = (ndev->flags & IFF_PROMISC ||
1439 ndev->flags & IFF_ALLMULTI ||
1440 netdev_mc_count(ndev) > NETCP_MAX_MCAST_ADDR);
1442 spin_lock(&netcp->lock);
1443 /* first clear all marks */
1444 netcp_addr_clear_mark(netcp);
1446 /* next add new entries, mark existing ones */
1447 netcp_addr_add_mark(netcp, ndev->broadcast, ADDR_BCAST);
1448 for_each_dev_addr(ndev, ndev_addr)
1449 netcp_addr_add_mark(netcp, ndev_addr->addr, ADDR_DEV);
1450 netdev_for_each_uc_addr(ndev_addr, ndev)
1451 netcp_addr_add_mark(netcp, ndev_addr->addr, ADDR_UCAST);
1452 netdev_for_each_mc_addr(ndev_addr, ndev)
1453 netcp_addr_add_mark(netcp, ndev_addr->addr, ADDR_MCAST);
1456 netcp_addr_add_mark(netcp, NULL, ADDR_ANY);
1458 /* finally sweep and callout into modules */
1459 netcp_addr_sweep_del(netcp);
1460 netcp_addr_sweep_add(netcp);
1461 spin_unlock(&netcp->lock);
1464 static void netcp_free_navigator_resources(struct netcp_intf *netcp)
1468 if (netcp->rx_channel) {
1469 knav_dma_close_channel(netcp->rx_channel);
1470 netcp->rx_channel = NULL;
1473 if (!IS_ERR_OR_NULL(netcp->rx_pool))
1474 netcp_rxpool_free(netcp);
1476 if (!IS_ERR_OR_NULL(netcp->rx_queue)) {
1477 knav_queue_close(netcp->rx_queue);
1478 netcp->rx_queue = NULL;
1481 for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN &&
1482 !IS_ERR_OR_NULL(netcp->rx_fdq[i]) ; ++i) {
1483 knav_queue_close(netcp->rx_fdq[i]);
1484 netcp->rx_fdq[i] = NULL;
1487 if (!IS_ERR_OR_NULL(netcp->tx_compl_q)) {
1488 knav_queue_close(netcp->tx_compl_q);
1489 netcp->tx_compl_q = NULL;
1492 if (!IS_ERR_OR_NULL(netcp->tx_pool)) {
1493 knav_pool_destroy(netcp->tx_pool);
1494 netcp->tx_pool = NULL;
1498 static int netcp_setup_navigator_resources(struct net_device *ndev)
1500 struct netcp_intf *netcp = netdev_priv(ndev);
1501 struct knav_queue_notify_config notify_cfg;
1502 struct knav_dma_cfg config;
1508 /* Create Rx/Tx descriptor pools */
1509 snprintf(name, sizeof(name), "rx-pool-%s", ndev->name);
1510 netcp->rx_pool = knav_pool_create(name, netcp->rx_pool_size,
1511 netcp->rx_pool_region_id);
1512 if (IS_ERR_OR_NULL(netcp->rx_pool)) {
1513 dev_err(netcp->ndev_dev, "Couldn't create rx pool\n");
1514 ret = PTR_ERR(netcp->rx_pool);
1518 snprintf(name, sizeof(name), "tx-pool-%s", ndev->name);
1519 netcp->tx_pool = knav_pool_create(name, netcp->tx_pool_size,
1520 netcp->tx_pool_region_id);
1521 if (IS_ERR_OR_NULL(netcp->tx_pool)) {
1522 dev_err(netcp->ndev_dev, "Couldn't create tx pool\n");
1523 ret = PTR_ERR(netcp->tx_pool);
1527 /* open Tx completion queue */
1528 snprintf(name, sizeof(name), "tx-compl-%s", ndev->name);
1529 netcp->tx_compl_q = knav_queue_open(name, netcp->tx_compl_qid, 0);
1530 if (IS_ERR_OR_NULL(netcp->tx_compl_q)) {
1531 ret = PTR_ERR(netcp->tx_compl_q);
1534 netcp->tx_compl_qid = knav_queue_get_id(netcp->tx_compl_q);
1536 /* Set notification for Tx completion */
1537 notify_cfg.fn = netcp_tx_notify;
1538 notify_cfg.fn_arg = netcp;
1539 ret = knav_queue_device_control(netcp->tx_compl_q,
1540 KNAV_QUEUE_SET_NOTIFIER,
1541 (unsigned long)¬ify_cfg);
1545 knav_queue_disable_notify(netcp->tx_compl_q);
1547 /* open Rx completion queue */
1548 snprintf(name, sizeof(name), "rx-compl-%s", ndev->name);
1549 netcp->rx_queue = knav_queue_open(name, netcp->rx_queue_id, 0);
1550 if (IS_ERR_OR_NULL(netcp->rx_queue)) {
1551 ret = PTR_ERR(netcp->rx_queue);
1554 netcp->rx_queue_id = knav_queue_get_id(netcp->rx_queue);
1556 /* Set notification for Rx completion */
1557 notify_cfg.fn = netcp_rx_notify;
1558 notify_cfg.fn_arg = netcp;
1559 ret = knav_queue_device_control(netcp->rx_queue,
1560 KNAV_QUEUE_SET_NOTIFIER,
1561 (unsigned long)¬ify_cfg);
1565 knav_queue_disable_notify(netcp->rx_queue);
1568 for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN && netcp->rx_queue_depths[i];
1570 snprintf(name, sizeof(name), "rx-fdq-%s-%d", ndev->name, i);
1571 netcp->rx_fdq[i] = knav_queue_open(name, KNAV_QUEUE_GP, 0);
1572 if (IS_ERR_OR_NULL(netcp->rx_fdq[i])) {
1573 ret = PTR_ERR(netcp->rx_fdq[i]);
1578 memset(&config, 0, sizeof(config));
1579 config.direction = DMA_DEV_TO_MEM;
1580 config.u.rx.einfo_present = true;
1581 config.u.rx.psinfo_present = true;
1582 config.u.rx.err_mode = DMA_DROP;
1583 config.u.rx.desc_type = DMA_DESC_HOST;
1584 config.u.rx.psinfo_at_sop = false;
1585 config.u.rx.sop_offset = NETCP_SOP_OFFSET;
1586 config.u.rx.dst_q = netcp->rx_queue_id;
1587 config.u.rx.thresh = DMA_THRESH_NONE;
1589 for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN; ++i) {
1590 if (netcp->rx_fdq[i])
1591 last_fdq = knav_queue_get_id(netcp->rx_fdq[i]);
1592 config.u.rx.fdq[i] = last_fdq;
1595 netcp->rx_channel = knav_dma_open_channel(netcp->netcp_device->device,
1596 netcp->dma_chan_name, &config);
1597 if (IS_ERR_OR_NULL(netcp->rx_channel)) {
1598 dev_err(netcp->ndev_dev, "failed opening rx chan(%s\n",
1599 netcp->dma_chan_name);
1603 dev_dbg(netcp->ndev_dev, "opened RX channel: %p\n", netcp->rx_channel);
1607 netcp_free_navigator_resources(netcp);
1611 /* Open the device */
1612 static int netcp_ndo_open(struct net_device *ndev)
1614 struct netcp_intf *netcp = netdev_priv(ndev);
1615 struct netcp_intf_modpriv *intf_modpriv;
1616 struct netcp_module *module;
1619 netif_carrier_off(ndev);
1620 ret = netcp_setup_navigator_resources(ndev);
1622 dev_err(netcp->ndev_dev, "Failed to setup navigator resources\n");
1626 for_each_module(netcp, intf_modpriv) {
1627 module = intf_modpriv->netcp_module;
1629 ret = module->open(intf_modpriv->module_priv, ndev);
1631 dev_err(netcp->ndev_dev, "module open failed\n");
1637 napi_enable(&netcp->rx_napi);
1638 napi_enable(&netcp->tx_napi);
1639 knav_queue_enable_notify(netcp->tx_compl_q);
1640 knav_queue_enable_notify(netcp->rx_queue);
1641 netcp_rxpool_refill(netcp);
1642 netif_tx_wake_all_queues(ndev);
1643 dev_dbg(netcp->ndev_dev, "netcp device %s opened\n", ndev->name);
1647 for_each_module(netcp, intf_modpriv) {
1648 module = intf_modpriv->netcp_module;
1650 module->close(intf_modpriv->module_priv, ndev);
1654 netcp_free_navigator_resources(netcp);
1658 /* Close the device */
1659 static int netcp_ndo_stop(struct net_device *ndev)
1661 struct netcp_intf *netcp = netdev_priv(ndev);
1662 struct netcp_intf_modpriv *intf_modpriv;
1663 struct netcp_module *module;
1666 netif_tx_stop_all_queues(ndev);
1667 netif_carrier_off(ndev);
1668 netcp_addr_clear_mark(netcp);
1669 netcp_addr_sweep_del(netcp);
1670 knav_queue_disable_notify(netcp->rx_queue);
1671 knav_queue_disable_notify(netcp->tx_compl_q);
1672 napi_disable(&netcp->rx_napi);
1673 napi_disable(&netcp->tx_napi);
1675 for_each_module(netcp, intf_modpriv) {
1676 module = intf_modpriv->netcp_module;
1677 if (module->close) {
1678 err = module->close(intf_modpriv->module_priv, ndev);
1680 dev_err(netcp->ndev_dev, "Close failed\n");
1684 /* Recycle Rx descriptors from completion queue */
1685 netcp_empty_rx_queue(netcp);
1687 /* Recycle Tx descriptors from completion queue */
1688 netcp_process_tx_compl_packets(netcp, netcp->tx_pool_size);
1690 if (knav_pool_count(netcp->tx_pool) != netcp->tx_pool_size)
1691 dev_err(netcp->ndev_dev, "Lost (%d) Tx descs\n",
1692 netcp->tx_pool_size - knav_pool_count(netcp->tx_pool));
1694 netcp_free_navigator_resources(netcp);
1695 dev_dbg(netcp->ndev_dev, "netcp device %s stopped\n", ndev->name);
1699 static int netcp_ndo_ioctl(struct net_device *ndev,
1700 struct ifreq *req, int cmd)
1702 struct netcp_intf *netcp = netdev_priv(ndev);
1703 struct netcp_intf_modpriv *intf_modpriv;
1704 struct netcp_module *module;
1705 int ret = -1, err = -EOPNOTSUPP;
1707 if (!netif_running(ndev))
1710 for_each_module(netcp, intf_modpriv) {
1711 module = intf_modpriv->netcp_module;
1715 err = module->ioctl(intf_modpriv->module_priv, req, cmd);
1716 if ((err < 0) && (err != -EOPNOTSUPP)) {
1725 return (ret == 0) ? 0 : err;
1728 static int netcp_ndo_change_mtu(struct net_device *ndev, int new_mtu)
1730 struct netcp_intf *netcp = netdev_priv(ndev);
1732 /* MTU < 68 is an error for IPv4 traffic */
1733 if ((new_mtu < 68) ||
1734 (new_mtu > (NETCP_MAX_FRAME_SIZE - ETH_HLEN - ETH_FCS_LEN))) {
1735 dev_err(netcp->ndev_dev, "Invalid mtu size = %d\n", new_mtu);
1739 ndev->mtu = new_mtu;
1743 static void netcp_ndo_tx_timeout(struct net_device *ndev)
1745 struct netcp_intf *netcp = netdev_priv(ndev);
1746 unsigned int descs = knav_pool_count(netcp->tx_pool);
1748 dev_err(netcp->ndev_dev, "transmit timed out tx descs(%d)\n", descs);
1749 netcp_process_tx_compl_packets(netcp, netcp->tx_pool_size);
1750 ndev->trans_start = jiffies;
1751 netif_tx_wake_all_queues(ndev);
1754 static int netcp_rx_add_vid(struct net_device *ndev, __be16 proto, u16 vid)
1756 struct netcp_intf *netcp = netdev_priv(ndev);
1757 struct netcp_intf_modpriv *intf_modpriv;
1758 struct netcp_module *module;
1759 unsigned long flags;
1762 dev_dbg(netcp->ndev_dev, "adding rx vlan id: %d\n", vid);
1764 spin_lock_irqsave(&netcp->lock, flags);
1765 for_each_module(netcp, intf_modpriv) {
1766 module = intf_modpriv->netcp_module;
1767 if ((module->add_vid) && (vid != 0)) {
1768 err = module->add_vid(intf_modpriv->module_priv, vid);
1770 dev_err(netcp->ndev_dev, "Could not add vlan id = %d\n",
1776 spin_unlock_irqrestore(&netcp->lock, flags);
1781 static int netcp_rx_kill_vid(struct net_device *ndev, __be16 proto, u16 vid)
1783 struct netcp_intf *netcp = netdev_priv(ndev);
1784 struct netcp_intf_modpriv *intf_modpriv;
1785 struct netcp_module *module;
1786 unsigned long flags;
1789 dev_dbg(netcp->ndev_dev, "removing rx vlan id: %d\n", vid);
1791 spin_lock_irqsave(&netcp->lock, flags);
1792 for_each_module(netcp, intf_modpriv) {
1793 module = intf_modpriv->netcp_module;
1794 if (module->del_vid) {
1795 err = module->del_vid(intf_modpriv->module_priv, vid);
1797 dev_err(netcp->ndev_dev, "Could not delete vlan id = %d\n",
1803 spin_unlock_irqrestore(&netcp->lock, flags);
1807 static u16 netcp_select_queue(struct net_device *dev, struct sk_buff *skb,
1809 select_queue_fallback_t fallback)
1814 static int netcp_setup_tc(struct net_device *dev, u8 num_tc)
1818 /* setup tc must be called under rtnl lock */
1821 /* Sanity-check the number of traffic classes requested */
1822 if ((dev->real_num_tx_queues <= 1) ||
1823 (dev->real_num_tx_queues < num_tc))
1826 /* Configure traffic class to queue mappings */
1828 netdev_set_num_tc(dev, num_tc);
1829 for (i = 0; i < num_tc; i++)
1830 netdev_set_tc_queue(dev, i, 1, i);
1832 netdev_reset_tc(dev);
1838 static const struct net_device_ops netcp_netdev_ops = {
1839 .ndo_open = netcp_ndo_open,
1840 .ndo_stop = netcp_ndo_stop,
1841 .ndo_start_xmit = netcp_ndo_start_xmit,
1842 .ndo_set_rx_mode = netcp_set_rx_mode,
1843 .ndo_do_ioctl = netcp_ndo_ioctl,
1844 .ndo_change_mtu = netcp_ndo_change_mtu,
1845 .ndo_set_mac_address = eth_mac_addr,
1846 .ndo_validate_addr = eth_validate_addr,
1847 .ndo_vlan_rx_add_vid = netcp_rx_add_vid,
1848 .ndo_vlan_rx_kill_vid = netcp_rx_kill_vid,
1849 .ndo_tx_timeout = netcp_ndo_tx_timeout,
1850 .ndo_select_queue = netcp_select_queue,
1851 .ndo_setup_tc = netcp_setup_tc,
1854 static int netcp_create_interface(struct netcp_device *netcp_device,
1855 struct device_node *node_interface)
1857 struct device *dev = netcp_device->device;
1858 struct device_node *node = dev->of_node;
1859 struct netcp_intf *netcp;
1860 struct net_device *ndev;
1861 resource_size_t size;
1862 struct resource res;
1863 void __iomem *efuse = NULL;
1865 const void *mac_addr;
1866 u8 efuse_mac_addr[6];
1870 ndev = alloc_etherdev_mqs(sizeof(*netcp), 1, 1);
1872 dev_err(dev, "Error allocating netdev\n");
1876 ndev->features |= NETIF_F_SG;
1877 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1878 ndev->hw_features = ndev->features;
1879 ndev->vlan_features |= NETIF_F_SG;
1881 netcp = netdev_priv(ndev);
1882 spin_lock_init(&netcp->lock);
1883 INIT_LIST_HEAD(&netcp->module_head);
1884 INIT_LIST_HEAD(&netcp->txhook_list_head);
1885 INIT_LIST_HEAD(&netcp->rxhook_list_head);
1886 INIT_LIST_HEAD(&netcp->addr_list);
1887 netcp->netcp_device = netcp_device;
1888 netcp->dev = netcp_device->device;
1890 netcp->ndev_dev = &ndev->dev;
1891 netcp->msg_enable = netif_msg_init(netcp_debug_level, NETCP_DEBUG);
1892 netcp->tx_pause_threshold = MAX_SKB_FRAGS;
1893 netcp->tx_resume_threshold = netcp->tx_pause_threshold;
1894 netcp->node_interface = node_interface;
1896 ret = of_property_read_u32(node_interface, "efuse-mac", &efuse_mac);
1898 if (of_address_to_resource(node, NETCP_EFUSE_REG_INDEX, &res)) {
1899 dev_err(dev, "could not find efuse-mac reg resource\n");
1903 size = resource_size(&res);
1905 if (!devm_request_mem_region(dev, res.start, size,
1907 dev_err(dev, "could not reserve resource\n");
1912 efuse = devm_ioremap_nocache(dev, res.start, size);
1914 dev_err(dev, "could not map resource\n");
1915 devm_release_mem_region(dev, res.start, size);
1920 emac_arch_get_mac_addr(efuse_mac_addr, efuse, efuse_mac);
1921 if (is_valid_ether_addr(efuse_mac_addr))
1922 ether_addr_copy(ndev->dev_addr, efuse_mac_addr);
1924 random_ether_addr(ndev->dev_addr);
1926 devm_iounmap(dev, efuse);
1927 devm_release_mem_region(dev, res.start, size);
1929 mac_addr = of_get_mac_address(node_interface);
1931 ether_addr_copy(ndev->dev_addr, mac_addr);
1933 random_ether_addr(ndev->dev_addr);
1936 ret = of_property_read_string(node_interface, "rx-channel",
1937 &netcp->dma_chan_name);
1939 dev_err(dev, "missing \"rx-channel\" parameter\n");
1944 ret = of_property_read_u32(node_interface, "rx-queue",
1945 &netcp->rx_queue_id);
1947 dev_warn(dev, "missing \"rx-queue\" parameter\n");
1948 netcp->rx_queue_id = KNAV_QUEUE_QPEND;
1951 ret = of_property_read_u32_array(node_interface, "rx-queue-depth",
1952 netcp->rx_queue_depths,
1953 KNAV_DMA_FDQ_PER_CHAN);
1955 dev_err(dev, "missing \"rx-queue-depth\" parameter\n");
1956 netcp->rx_queue_depths[0] = 128;
1959 ret = of_property_read_u32_array(node_interface, "rx-pool", temp, 2);
1961 dev_err(dev, "missing \"rx-pool\" parameter\n");
1965 netcp->rx_pool_size = temp[0];
1966 netcp->rx_pool_region_id = temp[1];
1968 ret = of_property_read_u32_array(node_interface, "tx-pool", temp, 2);
1970 dev_err(dev, "missing \"tx-pool\" parameter\n");
1974 netcp->tx_pool_size = temp[0];
1975 netcp->tx_pool_region_id = temp[1];
1977 if (netcp->tx_pool_size < MAX_SKB_FRAGS) {
1978 dev_err(dev, "tx-pool size too small, must be atleast(%ld)\n",
1984 ret = of_property_read_u32(node_interface, "tx-completion-queue",
1985 &netcp->tx_compl_qid);
1987 dev_warn(dev, "missing \"tx-completion-queue\" parameter\n");
1988 netcp->tx_compl_qid = KNAV_QUEUE_QPEND;
1992 netif_napi_add(ndev, &netcp->rx_napi, netcp_rx_poll, NETCP_NAPI_WEIGHT);
1993 netif_napi_add(ndev, &netcp->tx_napi, netcp_tx_poll, NETCP_NAPI_WEIGHT);
1995 /* Register the network device */
1997 ndev->watchdog_timeo = NETCP_TX_TIMEOUT;
1998 ndev->netdev_ops = &netcp_netdev_ops;
1999 SET_NETDEV_DEV(ndev, dev);
2001 list_add_tail(&netcp->interface_list, &netcp_device->interface_head);
2009 static void netcp_delete_interface(struct netcp_device *netcp_device,
2010 struct net_device *ndev)
2012 struct netcp_intf_modpriv *intf_modpriv, *tmp;
2013 struct netcp_intf *netcp = netdev_priv(ndev);
2014 struct netcp_module *module;
2016 dev_dbg(netcp_device->device, "Removing interface \"%s\"\n",
2019 /* Notify each of the modules that the interface is going away */
2020 list_for_each_entry_safe(intf_modpriv, tmp, &netcp->module_head,
2022 module = intf_modpriv->netcp_module;
2023 dev_dbg(netcp_device->device, "Releasing module \"%s\"\n",
2025 if (module->release)
2026 module->release(intf_modpriv->module_priv);
2027 list_del(&intf_modpriv->intf_list);
2028 kfree(intf_modpriv);
2030 WARN(!list_empty(&netcp->module_head), "%s interface module list is not empty!\n",
2033 list_del(&netcp->interface_list);
2035 of_node_put(netcp->node_interface);
2036 unregister_netdev(ndev);
2037 netif_napi_del(&netcp->rx_napi);
2041 static int netcp_probe(struct platform_device *pdev)
2043 struct device_node *node = pdev->dev.of_node;
2044 struct netcp_intf *netcp_intf, *netcp_tmp;
2045 struct device_node *child, *interfaces;
2046 struct netcp_device *netcp_device;
2047 struct device *dev = &pdev->dev;
2051 dev_err(dev, "could not find device info\n");
2055 /* Allocate a new NETCP device instance */
2056 netcp_device = devm_kzalloc(dev, sizeof(*netcp_device), GFP_KERNEL);
2060 pm_runtime_enable(&pdev->dev);
2061 ret = pm_runtime_get_sync(&pdev->dev);
2063 dev_err(dev, "Failed to enable NETCP power-domain\n");
2064 pm_runtime_disable(&pdev->dev);
2068 /* Initialize the NETCP device instance */
2069 INIT_LIST_HEAD(&netcp_device->interface_head);
2070 INIT_LIST_HEAD(&netcp_device->modpriv_head);
2071 netcp_device->device = dev;
2072 platform_set_drvdata(pdev, netcp_device);
2074 /* create interfaces */
2075 interfaces = of_get_child_by_name(node, "netcp-interfaces");
2077 dev_err(dev, "could not find netcp-interfaces node\n");
2082 for_each_available_child_of_node(interfaces, child) {
2083 ret = netcp_create_interface(netcp_device, child);
2085 dev_err(dev, "could not create interface(%s)\n",
2087 goto probe_quit_interface;
2091 /* Add the device instance to the list */
2092 list_add_tail(&netcp_device->device_list, &netcp_devices);
2096 probe_quit_interface:
2097 list_for_each_entry_safe(netcp_intf, netcp_tmp,
2098 &netcp_device->interface_head,
2100 netcp_delete_interface(netcp_device, netcp_intf->ndev);
2104 pm_runtime_put_sync(&pdev->dev);
2105 pm_runtime_disable(&pdev->dev);
2106 platform_set_drvdata(pdev, NULL);
2110 static int netcp_remove(struct platform_device *pdev)
2112 struct netcp_device *netcp_device = platform_get_drvdata(pdev);
2113 struct netcp_intf *netcp_intf, *netcp_tmp;
2114 struct netcp_inst_modpriv *inst_modpriv, *tmp;
2115 struct netcp_module *module;
2117 list_for_each_entry_safe(inst_modpriv, tmp, &netcp_device->modpriv_head,
2119 module = inst_modpriv->netcp_module;
2120 dev_dbg(&pdev->dev, "Removing module \"%s\"\n", module->name);
2121 module->remove(netcp_device, inst_modpriv->module_priv);
2122 list_del(&inst_modpriv->inst_list);
2123 kfree(inst_modpriv);
2126 /* now that all modules are removed, clean up the interfaces */
2127 list_for_each_entry_safe(netcp_intf, netcp_tmp,
2128 &netcp_device->interface_head,
2130 netcp_delete_interface(netcp_device, netcp_intf->ndev);
2133 WARN(!list_empty(&netcp_device->interface_head),
2134 "%s interface list not empty!\n", pdev->name);
2136 pm_runtime_put_sync(&pdev->dev);
2137 pm_runtime_disable(&pdev->dev);
2138 platform_set_drvdata(pdev, NULL);
2142 static const struct of_device_id of_match[] = {
2143 { .compatible = "ti,netcp-1.0", },
2146 MODULE_DEVICE_TABLE(of, of_match);
2148 static struct platform_driver netcp_driver = {
2150 .name = "netcp-1.0",
2151 .of_match_table = of_match,
2153 .probe = netcp_probe,
2154 .remove = netcp_remove,
2156 module_platform_driver(netcp_driver);
2158 MODULE_LICENSE("GPL v2");
2159 MODULE_DESCRIPTION("TI NETCP driver for Keystone SOCs");
2160 MODULE_AUTHOR("Sandeep Nair <sandeep_n@ti.com");