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 knav_queue_get_id(q) knav_queue_device_control(q, \
56 KNAV_QUEUE_GET_ID, (unsigned long)NULL)
58 #define knav_queue_enable_notify(q) knav_queue_device_control(q, \
59 KNAV_QUEUE_ENABLE_NOTIFY, \
62 #define knav_queue_disable_notify(q) knav_queue_device_control(q, \
63 KNAV_QUEUE_DISABLE_NOTIFY, \
66 #define knav_queue_get_count(q) knav_queue_device_control(q, \
67 KNAV_QUEUE_GET_COUNT, (unsigned long)NULL)
69 #define for_each_netcp_module(module) \
70 list_for_each_entry(module, &netcp_modules, module_list)
72 #define for_each_netcp_device_module(netcp_device, inst_modpriv) \
73 list_for_each_entry(inst_modpriv, \
74 &((netcp_device)->modpriv_head), inst_list)
76 #define for_each_module(netcp, intf_modpriv) \
77 list_for_each_entry(intf_modpriv, &netcp->module_head, intf_list)
79 /* Module management structures */
81 struct list_head device_list;
82 struct list_head interface_head;
83 struct list_head modpriv_head;
84 struct device *device;
87 struct netcp_inst_modpriv {
88 struct netcp_device *netcp_device;
89 struct netcp_module *netcp_module;
90 struct list_head inst_list;
94 struct netcp_intf_modpriv {
95 struct netcp_intf *netcp_priv;
96 struct netcp_module *netcp_module;
97 struct list_head intf_list;
101 static LIST_HEAD(netcp_devices);
102 static LIST_HEAD(netcp_modules);
103 static DEFINE_MUTEX(netcp_modules_lock);
105 static int netcp_debug_level = -1;
106 module_param(netcp_debug_level, int, 0);
107 MODULE_PARM_DESC(netcp_debug_level, "Netcp debug level (NETIF_MSG bits) (0=none,...,16=all)");
109 /* Helper functions - Get/Set */
110 static void get_pkt_info(u32 *buff, u32 *buff_len, u32 *ndesc,
111 struct knav_dma_desc *desc)
113 *buff_len = desc->buff_len;
115 *ndesc = desc->next_desc;
118 static void get_pad_info(u32 *pad0, u32 *pad1, struct knav_dma_desc *desc)
120 *pad0 = desc->pad[0];
121 *pad1 = desc->pad[1];
124 static void get_org_pkt_info(u32 *buff, u32 *buff_len,
125 struct knav_dma_desc *desc)
127 *buff = desc->orig_buff;
128 *buff_len = desc->orig_len;
131 static void get_words(u32 *words, int num_words, u32 *desc)
135 for (i = 0; i < num_words; i++)
139 static void set_pkt_info(u32 buff, u32 buff_len, u32 ndesc,
140 struct knav_dma_desc *desc)
142 desc->buff_len = buff_len;
144 desc->next_desc = ndesc;
147 static void set_desc_info(u32 desc_info, u32 pkt_info,
148 struct knav_dma_desc *desc)
150 desc->desc_info = desc_info;
151 desc->packet_info = pkt_info;
154 static void set_pad_info(u32 pad0, u32 pad1, struct knav_dma_desc *desc)
160 static void set_org_pkt_info(u32 buff, u32 buff_len,
161 struct knav_dma_desc *desc)
163 desc->orig_buff = buff;
164 desc->orig_len = buff_len;
167 static void set_words(u32 *words, int num_words, u32 *desc)
171 for (i = 0; i < num_words; i++)
175 /* Read the e-fuse value as 32 bit values to be endian independent */
176 static int emac_arch_get_mac_addr(char *x, void __iomem *efuse_mac)
178 unsigned int addr0, addr1;
180 addr1 = readl(efuse_mac + 4);
181 addr0 = readl(efuse_mac);
183 x[0] = (addr1 & 0x0000ff00) >> 8;
184 x[1] = addr1 & 0x000000ff;
185 x[2] = (addr0 & 0xff000000) >> 24;
186 x[3] = (addr0 & 0x00ff0000) >> 16;
187 x[4] = (addr0 & 0x0000ff00) >> 8;
188 x[5] = addr0 & 0x000000ff;
193 static const char *netcp_node_name(struct device_node *node)
197 if (of_property_read_string(node, "label", &name) < 0)
204 /* Module management routines */
205 static int netcp_register_interface(struct netcp_intf *netcp)
209 ret = register_netdev(netcp->ndev);
211 netcp->netdev_registered = true;
215 static int netcp_module_probe(struct netcp_device *netcp_device,
216 struct netcp_module *module)
218 struct device *dev = netcp_device->device;
219 struct device_node *devices, *interface, *node = dev->of_node;
220 struct device_node *child;
221 struct netcp_inst_modpriv *inst_modpriv;
222 struct netcp_intf *netcp_intf;
223 struct netcp_module *tmp;
224 bool primary_module_registered = false;
227 /* Find this module in the sub-tree for this device */
228 devices = of_get_child_by_name(node, "netcp-devices");
230 dev_err(dev, "could not find netcp-devices node\n");
231 return NETCP_MOD_PROBE_SKIPPED;
234 for_each_available_child_of_node(devices, child) {
235 const char *name = netcp_node_name(child);
237 if (!strcasecmp(module->name, name))
241 of_node_put(devices);
242 /* If module not used for this device, skip it */
244 dev_warn(dev, "module(%s) not used for device\n", module->name);
245 return NETCP_MOD_PROBE_SKIPPED;
248 inst_modpriv = devm_kzalloc(dev, sizeof(*inst_modpriv), GFP_KERNEL);
254 inst_modpriv->netcp_device = netcp_device;
255 inst_modpriv->netcp_module = module;
256 list_add_tail(&inst_modpriv->inst_list, &netcp_device->modpriv_head);
258 ret = module->probe(netcp_device, dev, child,
259 &inst_modpriv->module_priv);
262 dev_err(dev, "Probe of module(%s) failed with %d\n",
264 list_del(&inst_modpriv->inst_list);
265 devm_kfree(dev, inst_modpriv);
266 return NETCP_MOD_PROBE_FAILED;
269 /* Attach modules only if the primary module is probed */
270 for_each_netcp_module(tmp) {
272 primary_module_registered = true;
275 if (!primary_module_registered)
278 /* Attach module to interfaces */
279 list_for_each_entry(netcp_intf, &netcp_device->interface_head,
281 struct netcp_intf_modpriv *intf_modpriv;
283 /* If interface not registered then register now */
284 if (!netcp_intf->netdev_registered)
285 ret = netcp_register_interface(netcp_intf);
290 intf_modpriv = devm_kzalloc(dev, sizeof(*intf_modpriv),
295 interface = of_parse_phandle(netcp_intf->node_interface,
298 intf_modpriv->netcp_priv = netcp_intf;
299 intf_modpriv->netcp_module = module;
300 list_add_tail(&intf_modpriv->intf_list,
301 &netcp_intf->module_head);
303 ret = module->attach(inst_modpriv->module_priv,
304 netcp_intf->ndev, interface,
305 &intf_modpriv->module_priv);
306 of_node_put(interface);
308 dev_dbg(dev, "Attach of module %s declined with %d\n",
310 list_del(&intf_modpriv->intf_list);
311 devm_kfree(dev, intf_modpriv);
318 int netcp_register_module(struct netcp_module *module)
320 struct netcp_device *netcp_device;
321 struct netcp_module *tmp;
325 WARN(1, "error registering netcp module: no name\n");
329 if (!module->probe) {
330 WARN(1, "error registering netcp module: no probe\n");
334 mutex_lock(&netcp_modules_lock);
336 for_each_netcp_module(tmp) {
337 if (!strcasecmp(tmp->name, module->name)) {
338 mutex_unlock(&netcp_modules_lock);
342 list_add_tail(&module->module_list, &netcp_modules);
344 list_for_each_entry(netcp_device, &netcp_devices, device_list) {
345 ret = netcp_module_probe(netcp_device, module);
350 mutex_unlock(&netcp_modules_lock);
354 mutex_unlock(&netcp_modules_lock);
355 netcp_unregister_module(module);
358 EXPORT_SYMBOL_GPL(netcp_register_module);
360 static void netcp_release_module(struct netcp_device *netcp_device,
361 struct netcp_module *module)
363 struct netcp_inst_modpriv *inst_modpriv, *inst_tmp;
364 struct netcp_intf *netcp_intf, *netcp_tmp;
365 struct device *dev = netcp_device->device;
367 /* Release the module from each interface */
368 list_for_each_entry_safe(netcp_intf, netcp_tmp,
369 &netcp_device->interface_head,
371 struct netcp_intf_modpriv *intf_modpriv, *intf_tmp;
373 list_for_each_entry_safe(intf_modpriv, intf_tmp,
374 &netcp_intf->module_head,
376 if (intf_modpriv->netcp_module == module) {
377 module->release(intf_modpriv->module_priv);
378 list_del(&intf_modpriv->intf_list);
379 devm_kfree(dev, intf_modpriv);
385 /* Remove the module from each instance */
386 list_for_each_entry_safe(inst_modpriv, inst_tmp,
387 &netcp_device->modpriv_head, inst_list) {
388 if (inst_modpriv->netcp_module == module) {
389 module->remove(netcp_device,
390 inst_modpriv->module_priv);
391 list_del(&inst_modpriv->inst_list);
392 devm_kfree(dev, inst_modpriv);
398 void netcp_unregister_module(struct netcp_module *module)
400 struct netcp_device *netcp_device;
401 struct netcp_module *module_tmp;
403 mutex_lock(&netcp_modules_lock);
405 list_for_each_entry(netcp_device, &netcp_devices, device_list) {
406 netcp_release_module(netcp_device, module);
409 /* Remove the module from the module list */
410 for_each_netcp_module(module_tmp) {
411 if (module == module_tmp) {
412 list_del(&module->module_list);
417 mutex_unlock(&netcp_modules_lock);
419 EXPORT_SYMBOL_GPL(netcp_unregister_module);
421 void *netcp_module_get_intf_data(struct netcp_module *module,
422 struct netcp_intf *intf)
424 struct netcp_intf_modpriv *intf_modpriv;
426 list_for_each_entry(intf_modpriv, &intf->module_head, intf_list)
427 if (intf_modpriv->netcp_module == module)
428 return intf_modpriv->module_priv;
431 EXPORT_SYMBOL_GPL(netcp_module_get_intf_data);
433 /* Module TX and RX Hook management */
434 struct netcp_hook_list {
435 struct list_head list;
436 netcp_hook_rtn *hook_rtn;
441 int netcp_register_txhook(struct netcp_intf *netcp_priv, int order,
442 netcp_hook_rtn *hook_rtn, void *hook_data)
444 struct netcp_hook_list *entry;
445 struct netcp_hook_list *next;
448 entry = devm_kzalloc(netcp_priv->dev, sizeof(*entry), GFP_KERNEL);
452 entry->hook_rtn = hook_rtn;
453 entry->hook_data = hook_data;
454 entry->order = order;
456 spin_lock_irqsave(&netcp_priv->lock, flags);
457 list_for_each_entry(next, &netcp_priv->txhook_list_head, list) {
458 if (next->order > order)
461 __list_add(&entry->list, next->list.prev, &next->list);
462 spin_unlock_irqrestore(&netcp_priv->lock, flags);
466 EXPORT_SYMBOL_GPL(netcp_register_txhook);
468 int netcp_unregister_txhook(struct netcp_intf *netcp_priv, int order,
469 netcp_hook_rtn *hook_rtn, void *hook_data)
471 struct netcp_hook_list *next, *n;
474 spin_lock_irqsave(&netcp_priv->lock, flags);
475 list_for_each_entry_safe(next, n, &netcp_priv->txhook_list_head, list) {
476 if ((next->order == order) &&
477 (next->hook_rtn == hook_rtn) &&
478 (next->hook_data == hook_data)) {
479 list_del(&next->list);
480 spin_unlock_irqrestore(&netcp_priv->lock, flags);
481 devm_kfree(netcp_priv->dev, next);
485 spin_unlock_irqrestore(&netcp_priv->lock, flags);
488 EXPORT_SYMBOL_GPL(netcp_unregister_txhook);
490 int netcp_register_rxhook(struct netcp_intf *netcp_priv, int order,
491 netcp_hook_rtn *hook_rtn, void *hook_data)
493 struct netcp_hook_list *entry;
494 struct netcp_hook_list *next;
497 entry = devm_kzalloc(netcp_priv->dev, sizeof(*entry), GFP_KERNEL);
501 entry->hook_rtn = hook_rtn;
502 entry->hook_data = hook_data;
503 entry->order = order;
505 spin_lock_irqsave(&netcp_priv->lock, flags);
506 list_for_each_entry(next, &netcp_priv->rxhook_list_head, list) {
507 if (next->order > order)
510 __list_add(&entry->list, next->list.prev, &next->list);
511 spin_unlock_irqrestore(&netcp_priv->lock, flags);
516 int netcp_unregister_rxhook(struct netcp_intf *netcp_priv, int order,
517 netcp_hook_rtn *hook_rtn, void *hook_data)
519 struct netcp_hook_list *next, *n;
522 spin_lock_irqsave(&netcp_priv->lock, flags);
523 list_for_each_entry_safe(next, n, &netcp_priv->rxhook_list_head, list) {
524 if ((next->order == order) &&
525 (next->hook_rtn == hook_rtn) &&
526 (next->hook_data == hook_data)) {
527 list_del(&next->list);
528 spin_unlock_irqrestore(&netcp_priv->lock, flags);
529 devm_kfree(netcp_priv->dev, next);
533 spin_unlock_irqrestore(&netcp_priv->lock, flags);
538 static void netcp_frag_free(bool is_frag, void *ptr)
546 static void netcp_free_rx_desc_chain(struct netcp_intf *netcp,
547 struct knav_dma_desc *desc)
549 struct knav_dma_desc *ndesc;
550 dma_addr_t dma_desc, dma_buf;
551 unsigned int buf_len, dma_sz = sizeof(*ndesc);
555 get_words(&dma_desc, 1, &desc->next_desc);
558 ndesc = knav_pool_desc_unmap(netcp->rx_pool, dma_desc, dma_sz);
559 if (unlikely(!ndesc)) {
560 dev_err(netcp->ndev_dev, "failed to unmap Rx desc\n");
563 get_pkt_info(&dma_buf, &tmp, &dma_desc, ndesc);
564 get_pad_info((u32 *)&buf_ptr, &tmp, ndesc);
565 dma_unmap_page(netcp->dev, dma_buf, PAGE_SIZE, DMA_FROM_DEVICE);
566 __free_page(buf_ptr);
567 knav_pool_desc_put(netcp->rx_pool, desc);
570 get_pad_info((u32 *)&buf_ptr, &buf_len, desc);
572 netcp_frag_free(buf_len <= PAGE_SIZE, buf_ptr);
573 knav_pool_desc_put(netcp->rx_pool, desc);
576 static void netcp_empty_rx_queue(struct netcp_intf *netcp)
578 struct knav_dma_desc *desc;
583 dma = knav_queue_pop(netcp->rx_queue, &dma_sz);
587 desc = knav_pool_desc_unmap(netcp->rx_pool, dma, dma_sz);
588 if (unlikely(!desc)) {
589 dev_err(netcp->ndev_dev, "%s: failed to unmap Rx desc\n",
591 netcp->ndev->stats.rx_errors++;
594 netcp_free_rx_desc_chain(netcp, desc);
595 netcp->ndev->stats.rx_dropped++;
599 static int netcp_process_one_rx_packet(struct netcp_intf *netcp)
601 unsigned int dma_sz, buf_len, org_buf_len;
602 struct knav_dma_desc *desc, *ndesc;
603 unsigned int pkt_sz = 0, accum_sz;
604 struct netcp_hook_list *rx_hook;
605 dma_addr_t dma_desc, dma_buff;
606 struct netcp_packet p_info;
611 dma_desc = knav_queue_pop(netcp->rx_queue, &dma_sz);
615 desc = knav_pool_desc_unmap(netcp->rx_pool, dma_desc, dma_sz);
616 if (unlikely(!desc)) {
617 dev_err(netcp->ndev_dev, "failed to unmap Rx desc\n");
621 get_pkt_info(&dma_buff, &buf_len, &dma_desc, desc);
622 get_pad_info((u32 *)&org_buf_ptr, &org_buf_len, desc);
624 if (unlikely(!org_buf_ptr)) {
625 dev_err(netcp->ndev_dev, "NULL bufptr in desc\n");
629 pkt_sz &= KNAV_DMA_DESC_PKT_LEN_MASK;
631 dma_unmap_single(netcp->dev, dma_buff, buf_len, DMA_FROM_DEVICE);
633 /* Build a new sk_buff for the primary buffer */
634 skb = build_skb(org_buf_ptr, org_buf_len);
635 if (unlikely(!skb)) {
636 dev_err(netcp->ndev_dev, "build_skb() failed\n");
640 /* update data, tail and len */
641 skb_reserve(skb, NETCP_SOP_OFFSET);
642 __skb_put(skb, buf_len);
644 /* Fill in the page fragment list */
648 ndesc = knav_pool_desc_unmap(netcp->rx_pool, dma_desc, dma_sz);
649 if (unlikely(!ndesc)) {
650 dev_err(netcp->ndev_dev, "failed to unmap Rx desc\n");
654 get_pkt_info(&dma_buff, &buf_len, &dma_desc, ndesc);
655 get_pad_info((u32 *)&page, &tmp, ndesc);
657 if (likely(dma_buff && buf_len && page)) {
658 dma_unmap_page(netcp->dev, dma_buff, PAGE_SIZE,
661 dev_err(netcp->ndev_dev, "Bad Rx desc dma_buff(%p), len(%d), page(%p)\n",
662 (void *)dma_buff, buf_len, page);
666 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
667 offset_in_page(dma_buff), buf_len, PAGE_SIZE);
670 /* Free the descriptor */
671 knav_pool_desc_put(netcp->rx_pool, ndesc);
674 /* Free the primary descriptor */
675 knav_pool_desc_put(netcp->rx_pool, desc);
677 /* check for packet len and warn */
678 if (unlikely(pkt_sz != accum_sz))
679 dev_dbg(netcp->ndev_dev, "mismatch in packet size(%d) & sum of fragments(%d)\n",
682 /* Remove ethernet FCS from the packet */
683 __pskb_trim(skb, skb->len - ETH_FCS_LEN);
685 /* Call each of the RX hooks */
687 p_info.rxtstamp_complete = false;
688 list_for_each_entry(rx_hook, &netcp->rxhook_list_head, list) {
691 ret = rx_hook->hook_rtn(rx_hook->order, rx_hook->hook_data,
694 dev_err(netcp->ndev_dev, "RX hook %d failed: %d\n",
695 rx_hook->order, ret);
696 netcp->ndev->stats.rx_errors++;
702 netcp->ndev->stats.rx_packets++;
703 netcp->ndev->stats.rx_bytes += skb->len;
705 /* push skb up the stack */
706 skb->protocol = eth_type_trans(skb, netcp->ndev);
707 netif_receive_skb(skb);
711 netcp_free_rx_desc_chain(netcp, desc);
712 netcp->ndev->stats.rx_errors++;
716 static int netcp_process_rx_packets(struct netcp_intf *netcp,
721 for (i = 0; (i < budget) && !netcp_process_one_rx_packet(netcp); i++)
726 /* Release descriptors and attached buffers from Rx FDQ */
727 static void netcp_free_rx_buf(struct netcp_intf *netcp, int fdq)
729 struct knav_dma_desc *desc;
730 unsigned int buf_len, dma_sz;
735 /* Allocate descriptor */
736 while ((dma = knav_queue_pop(netcp->rx_fdq[fdq], &dma_sz))) {
737 desc = knav_pool_desc_unmap(netcp->rx_pool, dma, dma_sz);
738 if (unlikely(!desc)) {
739 dev_err(netcp->ndev_dev, "failed to unmap Rx desc\n");
743 get_org_pkt_info(&dma, &buf_len, desc);
744 get_pad_info((u32 *)&buf_ptr, &tmp, desc);
746 if (unlikely(!dma)) {
747 dev_err(netcp->ndev_dev, "NULL orig_buff in desc\n");
748 knav_pool_desc_put(netcp->rx_pool, desc);
752 if (unlikely(!buf_ptr)) {
753 dev_err(netcp->ndev_dev, "NULL bufptr in desc\n");
754 knav_pool_desc_put(netcp->rx_pool, desc);
759 dma_unmap_single(netcp->dev, dma, buf_len,
761 netcp_frag_free((buf_len <= PAGE_SIZE), buf_ptr);
763 dma_unmap_page(netcp->dev, dma, buf_len,
765 __free_page(buf_ptr);
768 knav_pool_desc_put(netcp->rx_pool, desc);
772 static void netcp_rxpool_free(struct netcp_intf *netcp)
776 for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN &&
777 !IS_ERR_OR_NULL(netcp->rx_fdq[i]); i++)
778 netcp_free_rx_buf(netcp, i);
780 if (knav_pool_count(netcp->rx_pool) != netcp->rx_pool_size)
781 dev_err(netcp->ndev_dev, "Lost Rx (%d) descriptors\n",
782 netcp->rx_pool_size - knav_pool_count(netcp->rx_pool));
784 knav_pool_destroy(netcp->rx_pool);
785 netcp->rx_pool = NULL;
788 static void netcp_allocate_rx_buf(struct netcp_intf *netcp, int fdq)
790 struct knav_dma_desc *hwdesc;
791 unsigned int buf_len, dma_sz;
792 u32 desc_info, pkt_info;
798 /* Allocate descriptor */
799 hwdesc = knav_pool_desc_get(netcp->rx_pool);
800 if (IS_ERR_OR_NULL(hwdesc)) {
801 dev_dbg(netcp->ndev_dev, "out of rx pool desc\n");
805 if (likely(fdq == 0)) {
806 unsigned int primary_buf_len;
807 /* Allocate a primary receive queue entry */
808 buf_len = NETCP_PACKET_SIZE + NETCP_SOP_OFFSET;
809 primary_buf_len = SKB_DATA_ALIGN(buf_len) +
810 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
812 bufptr = netdev_alloc_frag(primary_buf_len);
813 pad[1] = primary_buf_len;
815 if (unlikely(!bufptr)) {
816 dev_warn_ratelimited(netcp->ndev_dev,
817 "Primary RX buffer alloc failed\n");
820 dma = dma_map_single(netcp->dev, bufptr, buf_len,
822 if (unlikely(dma_mapping_error(netcp->dev, dma)))
825 pad[0] = (u32)bufptr;
828 /* Allocate a secondary receive queue entry */
829 page = alloc_page(GFP_ATOMIC | GFP_DMA | __GFP_COLD);
830 if (unlikely(!page)) {
831 dev_warn_ratelimited(netcp->ndev_dev, "Secondary page alloc failed\n");
835 dma = dma_map_page(netcp->dev, page, 0, buf_len, DMA_TO_DEVICE);
840 desc_info = KNAV_DMA_DESC_PS_INFO_IN_DESC;
841 desc_info |= buf_len & KNAV_DMA_DESC_PKT_LEN_MASK;
842 pkt_info = KNAV_DMA_DESC_HAS_EPIB;
843 pkt_info |= KNAV_DMA_NUM_PS_WORDS << KNAV_DMA_DESC_PSLEN_SHIFT;
844 pkt_info |= (netcp->rx_queue_id & KNAV_DMA_DESC_RETQ_MASK) <<
845 KNAV_DMA_DESC_RETQ_SHIFT;
846 set_org_pkt_info(dma, buf_len, hwdesc);
847 set_pad_info(pad[0], pad[1], hwdesc);
848 set_desc_info(desc_info, pkt_info, hwdesc);
851 knav_pool_desc_map(netcp->rx_pool, hwdesc, sizeof(*hwdesc), &dma,
853 knav_queue_push(netcp->rx_fdq[fdq], dma, sizeof(*hwdesc), 0);
857 knav_pool_desc_put(netcp->rx_pool, hwdesc);
860 /* Refill Rx FDQ with descriptors & attached buffers */
861 static void netcp_rxpool_refill(struct netcp_intf *netcp)
863 u32 fdq_deficit[KNAV_DMA_FDQ_PER_CHAN] = {0};
866 /* Calculate the FDQ deficit and refill */
867 for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN && netcp->rx_fdq[i]; i++) {
868 fdq_deficit[i] = netcp->rx_queue_depths[i] -
869 knav_queue_get_count(netcp->rx_fdq[i]);
871 while (fdq_deficit[i]--)
872 netcp_allocate_rx_buf(netcp, i);
877 static int netcp_rx_poll(struct napi_struct *napi, int budget)
879 struct netcp_intf *netcp = container_of(napi, struct netcp_intf,
881 unsigned int packets;
883 packets = netcp_process_rx_packets(netcp, budget);
885 if (packets < budget) {
886 napi_complete(&netcp->rx_napi);
887 knav_queue_enable_notify(netcp->rx_queue);
890 netcp_rxpool_refill(netcp);
894 static void netcp_rx_notify(void *arg)
896 struct netcp_intf *netcp = arg;
898 knav_queue_disable_notify(netcp->rx_queue);
899 napi_schedule(&netcp->rx_napi);
902 static void netcp_free_tx_desc_chain(struct netcp_intf *netcp,
903 struct knav_dma_desc *desc,
904 unsigned int desc_sz)
906 struct knav_dma_desc *ndesc = desc;
907 dma_addr_t dma_desc, dma_buf;
908 unsigned int buf_len;
911 get_pkt_info(&dma_buf, &buf_len, &dma_desc, ndesc);
913 if (dma_buf && buf_len)
914 dma_unmap_single(netcp->dev, dma_buf, buf_len,
917 dev_warn(netcp->ndev_dev, "bad Tx desc buf(%p), len(%d)\n",
918 (void *)dma_buf, buf_len);
920 knav_pool_desc_put(netcp->tx_pool, ndesc);
923 ndesc = knav_pool_desc_unmap(netcp->tx_pool, dma_desc,
926 dev_err(netcp->ndev_dev, "failed to unmap Tx desc\n");
931 static int netcp_process_tx_compl_packets(struct netcp_intf *netcp,
934 struct knav_dma_desc *desc;
942 dma = knav_queue_pop(netcp->tx_compl_q, &dma_sz);
945 desc = knav_pool_desc_unmap(netcp->tx_pool, dma, dma_sz);
946 if (unlikely(!desc)) {
947 dev_err(netcp->ndev_dev, "failed to unmap Tx desc\n");
948 netcp->ndev->stats.tx_errors++;
952 get_pad_info((u32 *)&skb, &tmp, desc);
953 netcp_free_tx_desc_chain(netcp, desc, dma_sz);
955 dev_err(netcp->ndev_dev, "No skb in Tx desc\n");
956 netcp->ndev->stats.tx_errors++;
960 if (netif_subqueue_stopped(netcp->ndev, skb) &&
961 netif_running(netcp->ndev) &&
962 (knav_pool_count(netcp->tx_pool) >
963 netcp->tx_resume_threshold)) {
964 u16 subqueue = skb_get_queue_mapping(skb);
966 netif_wake_subqueue(netcp->ndev, subqueue);
969 netcp->ndev->stats.tx_packets++;
970 netcp->ndev->stats.tx_bytes += skb->len;
977 static int netcp_tx_poll(struct napi_struct *napi, int budget)
980 struct netcp_intf *netcp = container_of(napi, struct netcp_intf,
983 packets = netcp_process_tx_compl_packets(netcp, budget);
984 if (packets < budget) {
985 napi_complete(&netcp->tx_napi);
986 knav_queue_enable_notify(netcp->tx_compl_q);
992 static void netcp_tx_notify(void *arg)
994 struct netcp_intf *netcp = arg;
996 knav_queue_disable_notify(netcp->tx_compl_q);
997 napi_schedule(&netcp->tx_napi);
1000 static struct knav_dma_desc*
1001 netcp_tx_map_skb(struct sk_buff *skb, struct netcp_intf *netcp)
1003 struct knav_dma_desc *desc, *ndesc, *pdesc;
1004 unsigned int pkt_len = skb_headlen(skb);
1005 struct device *dev = netcp->dev;
1006 dma_addr_t dma_addr;
1007 unsigned int dma_sz;
1010 /* Map the linear buffer */
1011 dma_addr = dma_map_single(dev, skb->data, pkt_len, DMA_TO_DEVICE);
1012 if (unlikely(dma_mapping_error(dev, dma_addr))) {
1013 dev_err(netcp->ndev_dev, "Failed to map skb buffer\n");
1017 desc = knav_pool_desc_get(netcp->tx_pool);
1018 if (unlikely(IS_ERR_OR_NULL(desc))) {
1019 dev_err(netcp->ndev_dev, "out of TX desc\n");
1020 dma_unmap_single(dev, dma_addr, pkt_len, DMA_TO_DEVICE);
1024 set_pkt_info(dma_addr, pkt_len, 0, desc);
1025 if (skb_is_nonlinear(skb)) {
1026 prefetchw(skb_shinfo(skb));
1028 desc->next_desc = 0;
1034 /* Handle the case where skb is fragmented in pages */
1035 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1036 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1037 struct page *page = skb_frag_page(frag);
1038 u32 page_offset = frag->page_offset;
1039 u32 buf_len = skb_frag_size(frag);
1040 dma_addr_t desc_dma;
1043 dma_addr = dma_map_page(dev, page, page_offset, buf_len,
1045 if (unlikely(!dma_addr)) {
1046 dev_err(netcp->ndev_dev, "Failed to map skb page\n");
1050 ndesc = knav_pool_desc_get(netcp->tx_pool);
1051 if (unlikely(IS_ERR_OR_NULL(ndesc))) {
1052 dev_err(netcp->ndev_dev, "out of TX desc for frags\n");
1053 dma_unmap_page(dev, dma_addr, buf_len, DMA_TO_DEVICE);
1057 desc_dma = knav_pool_desc_virt_to_dma(netcp->tx_pool,
1060 (netcp->tx_compl_qid & KNAV_DMA_DESC_RETQ_MASK) <<
1061 KNAV_DMA_DESC_RETQ_SHIFT;
1062 set_pkt_info(dma_addr, buf_len, 0, ndesc);
1063 set_words(&desc_dma, 1, &pdesc->next_desc);
1066 knav_pool_desc_map(netcp->tx_pool, pdesc,
1067 sizeof(*pdesc), &desc_dma, &dma_sz);
1071 knav_pool_desc_map(netcp->tx_pool, pdesc, sizeof(*pdesc),
1072 &dma_addr, &dma_sz);
1074 /* frag list based linkage is not supported for now. */
1075 if (skb_shinfo(skb)->frag_list) {
1076 dev_err_ratelimited(netcp->ndev_dev, "NETIF_F_FRAGLIST not supported\n");
1081 WARN_ON(pkt_len != skb->len);
1083 pkt_len &= KNAV_DMA_DESC_PKT_LEN_MASK;
1084 set_words(&pkt_len, 1, &desc->desc_info);
1088 netcp_free_tx_desc_chain(netcp, desc, sizeof(*desc));
1092 static int netcp_tx_submit_skb(struct netcp_intf *netcp,
1093 struct sk_buff *skb,
1094 struct knav_dma_desc *desc)
1096 struct netcp_tx_pipe *tx_pipe = NULL;
1097 struct netcp_hook_list *tx_hook;
1098 struct netcp_packet p_info;
1099 unsigned int dma_sz;
1104 p_info.netcp = netcp;
1106 p_info.tx_pipe = NULL;
1107 p_info.psdata_len = 0;
1108 p_info.ts_context = NULL;
1109 p_info.txtstamp_complete = NULL;
1110 p_info.epib = desc->epib;
1111 p_info.psdata = desc->psdata;
1112 memset(p_info.epib, 0, KNAV_DMA_NUM_EPIB_WORDS * sizeof(u32));
1114 /* Find out where to inject the packet for transmission */
1115 list_for_each_entry(tx_hook, &netcp->txhook_list_head, list) {
1116 ret = tx_hook->hook_rtn(tx_hook->order, tx_hook->hook_data,
1118 if (unlikely(ret != 0)) {
1119 dev_err(netcp->ndev_dev, "TX hook %d rejected the packet with reason(%d)\n",
1120 tx_hook->order, ret);
1121 ret = (ret < 0) ? ret : NETDEV_TX_OK;
1126 /* Make sure some TX hook claimed the packet */
1127 tx_pipe = p_info.tx_pipe;
1129 dev_err(netcp->ndev_dev, "No TX hook claimed the packet!\n");
1134 /* update descriptor */
1135 if (p_info.psdata_len) {
1136 u32 *psdata = p_info.psdata;
1138 memmove(p_info.psdata, p_info.psdata + p_info.psdata_len,
1140 set_words(psdata, p_info.psdata_len, psdata);
1141 tmp |= (p_info.psdata_len & KNAV_DMA_DESC_PSLEN_MASK) <<
1142 KNAV_DMA_DESC_PSLEN_SHIFT;
1145 tmp |= KNAV_DMA_DESC_HAS_EPIB |
1146 ((netcp->tx_compl_qid & KNAV_DMA_DESC_RETQ_MASK) <<
1147 KNAV_DMA_DESC_RETQ_SHIFT);
1149 if (!(tx_pipe->flags & SWITCH_TO_PORT_IN_TAGINFO)) {
1150 tmp |= ((tx_pipe->switch_to_port & KNAV_DMA_DESC_PSFLAG_MASK) <<
1151 KNAV_DMA_DESC_PSFLAG_SHIFT);
1154 set_words(&tmp, 1, &desc->packet_info);
1155 set_words((u32 *)&skb, 1, &desc->pad[0]);
1157 if (tx_pipe->flags & SWITCH_TO_PORT_IN_TAGINFO) {
1158 tmp = tx_pipe->switch_to_port;
1159 set_words((u32 *)&tmp, 1, &desc->tag_info);
1162 /* submit packet descriptor */
1163 ret = knav_pool_desc_map(netcp->tx_pool, desc, sizeof(*desc), &dma,
1165 if (unlikely(ret)) {
1166 dev_err(netcp->ndev_dev, "%s() failed to map desc\n", __func__);
1170 skb_tx_timestamp(skb);
1171 knav_queue_push(tx_pipe->dma_queue, dma, dma_sz, 0);
1177 /* Submit the packet */
1178 static int netcp_ndo_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1180 struct netcp_intf *netcp = netdev_priv(ndev);
1181 int subqueue = skb_get_queue_mapping(skb);
1182 struct knav_dma_desc *desc;
1183 int desc_count, ret = 0;
1185 if (unlikely(skb->len <= 0)) {
1187 return NETDEV_TX_OK;
1190 if (unlikely(skb->len < NETCP_MIN_PACKET_SIZE)) {
1191 ret = skb_padto(skb, NETCP_MIN_PACKET_SIZE);
1193 /* If we get here, the skb has already been dropped */
1194 dev_warn(netcp->ndev_dev, "padding failed (%d), packet dropped\n",
1196 ndev->stats.tx_dropped++;
1199 skb->len = NETCP_MIN_PACKET_SIZE;
1202 desc = netcp_tx_map_skb(skb, netcp);
1203 if (unlikely(!desc)) {
1204 netif_stop_subqueue(ndev, subqueue);
1209 ret = netcp_tx_submit_skb(netcp, skb, desc);
1213 ndev->trans_start = jiffies;
1215 /* Check Tx pool count & stop subqueue if needed */
1216 desc_count = knav_pool_count(netcp->tx_pool);
1217 if (desc_count < netcp->tx_pause_threshold) {
1218 dev_dbg(netcp->ndev_dev, "pausing tx, count(%d)\n", desc_count);
1219 netif_stop_subqueue(ndev, subqueue);
1221 return NETDEV_TX_OK;
1224 ndev->stats.tx_dropped++;
1226 netcp_free_tx_desc_chain(netcp, desc, sizeof(*desc));
1231 int netcp_txpipe_close(struct netcp_tx_pipe *tx_pipe)
1233 if (tx_pipe->dma_channel) {
1234 knav_dma_close_channel(tx_pipe->dma_channel);
1235 tx_pipe->dma_channel = NULL;
1239 EXPORT_SYMBOL_GPL(netcp_txpipe_close);
1241 int netcp_txpipe_open(struct netcp_tx_pipe *tx_pipe)
1243 struct device *dev = tx_pipe->netcp_device->device;
1244 struct knav_dma_cfg config;
1248 memset(&config, 0, sizeof(config));
1249 config.direction = DMA_MEM_TO_DEV;
1250 config.u.tx.filt_einfo = false;
1251 config.u.tx.filt_pswords = false;
1252 config.u.tx.priority = DMA_PRIO_MED_L;
1254 tx_pipe->dma_channel = knav_dma_open_channel(dev,
1255 tx_pipe->dma_chan_name, &config);
1256 if (IS_ERR_OR_NULL(tx_pipe->dma_channel)) {
1257 dev_err(dev, "failed opening tx chan(%s)\n",
1258 tx_pipe->dma_chan_name);
1262 snprintf(name, sizeof(name), "tx-pipe-%s", dev_name(dev));
1263 tx_pipe->dma_queue = knav_queue_open(name, tx_pipe->dma_queue_id,
1265 if (IS_ERR(tx_pipe->dma_queue)) {
1266 dev_err(dev, "Could not open DMA queue for channel \"%s\": %d\n",
1268 ret = PTR_ERR(tx_pipe->dma_queue);
1272 dev_dbg(dev, "opened tx pipe %s\n", name);
1276 if (!IS_ERR_OR_NULL(tx_pipe->dma_channel))
1277 knav_dma_close_channel(tx_pipe->dma_channel);
1278 tx_pipe->dma_channel = NULL;
1281 EXPORT_SYMBOL_GPL(netcp_txpipe_open);
1283 int netcp_txpipe_init(struct netcp_tx_pipe *tx_pipe,
1284 struct netcp_device *netcp_device,
1285 const char *dma_chan_name, unsigned int dma_queue_id)
1287 memset(tx_pipe, 0, sizeof(*tx_pipe));
1288 tx_pipe->netcp_device = netcp_device;
1289 tx_pipe->dma_chan_name = dma_chan_name;
1290 tx_pipe->dma_queue_id = dma_queue_id;
1293 EXPORT_SYMBOL_GPL(netcp_txpipe_init);
1295 static struct netcp_addr *netcp_addr_find(struct netcp_intf *netcp,
1297 enum netcp_addr_type type)
1299 struct netcp_addr *naddr;
1301 list_for_each_entry(naddr, &netcp->addr_list, node) {
1302 if (naddr->type != type)
1304 if (addr && memcmp(addr, naddr->addr, ETH_ALEN))
1312 static struct netcp_addr *netcp_addr_add(struct netcp_intf *netcp,
1314 enum netcp_addr_type type)
1316 struct netcp_addr *naddr;
1318 naddr = devm_kmalloc(netcp->dev, sizeof(*naddr), GFP_ATOMIC);
1324 naddr->netcp = netcp;
1326 ether_addr_copy(naddr->addr, addr);
1328 eth_zero_addr(naddr->addr);
1329 list_add_tail(&naddr->node, &netcp->addr_list);
1334 static void netcp_addr_del(struct netcp_intf *netcp, struct netcp_addr *naddr)
1336 list_del(&naddr->node);
1337 devm_kfree(netcp->dev, naddr);
1340 static void netcp_addr_clear_mark(struct netcp_intf *netcp)
1342 struct netcp_addr *naddr;
1344 list_for_each_entry(naddr, &netcp->addr_list, node)
1348 static void netcp_addr_add_mark(struct netcp_intf *netcp, const u8 *addr,
1349 enum netcp_addr_type type)
1351 struct netcp_addr *naddr;
1353 naddr = netcp_addr_find(netcp, addr, type);
1355 naddr->flags |= ADDR_VALID;
1359 naddr = netcp_addr_add(netcp, addr, type);
1360 if (!WARN_ON(!naddr))
1361 naddr->flags |= ADDR_NEW;
1364 static void netcp_addr_sweep_del(struct netcp_intf *netcp)
1366 struct netcp_addr *naddr, *tmp;
1367 struct netcp_intf_modpriv *priv;
1368 struct netcp_module *module;
1371 list_for_each_entry_safe(naddr, tmp, &netcp->addr_list, node) {
1372 if (naddr->flags & (ADDR_VALID | ADDR_NEW))
1374 dev_dbg(netcp->ndev_dev, "deleting address %pM, type %x\n",
1375 naddr->addr, naddr->type);
1376 mutex_lock(&netcp_modules_lock);
1377 for_each_module(netcp, priv) {
1378 module = priv->netcp_module;
1379 if (!module->del_addr)
1381 error = module->del_addr(priv->module_priv,
1385 mutex_unlock(&netcp_modules_lock);
1386 netcp_addr_del(netcp, naddr);
1390 static void netcp_addr_sweep_add(struct netcp_intf *netcp)
1392 struct netcp_addr *naddr, *tmp;
1393 struct netcp_intf_modpriv *priv;
1394 struct netcp_module *module;
1397 list_for_each_entry_safe(naddr, tmp, &netcp->addr_list, node) {
1398 if (!(naddr->flags & ADDR_NEW))
1400 dev_dbg(netcp->ndev_dev, "adding address %pM, type %x\n",
1401 naddr->addr, naddr->type);
1402 mutex_lock(&netcp_modules_lock);
1403 for_each_module(netcp, priv) {
1404 module = priv->netcp_module;
1405 if (!module->add_addr)
1407 error = module->add_addr(priv->module_priv, naddr);
1410 mutex_unlock(&netcp_modules_lock);
1414 static void netcp_set_rx_mode(struct net_device *ndev)
1416 struct netcp_intf *netcp = netdev_priv(ndev);
1417 struct netdev_hw_addr *ndev_addr;
1420 promisc = (ndev->flags & IFF_PROMISC ||
1421 ndev->flags & IFF_ALLMULTI ||
1422 netdev_mc_count(ndev) > NETCP_MAX_MCAST_ADDR);
1424 /* first clear all marks */
1425 netcp_addr_clear_mark(netcp);
1427 /* next add new entries, mark existing ones */
1428 netcp_addr_add_mark(netcp, ndev->broadcast, ADDR_BCAST);
1429 for_each_dev_addr(ndev, ndev_addr)
1430 netcp_addr_add_mark(netcp, ndev_addr->addr, ADDR_DEV);
1431 netdev_for_each_uc_addr(ndev_addr, ndev)
1432 netcp_addr_add_mark(netcp, ndev_addr->addr, ADDR_UCAST);
1433 netdev_for_each_mc_addr(ndev_addr, ndev)
1434 netcp_addr_add_mark(netcp, ndev_addr->addr, ADDR_MCAST);
1437 netcp_addr_add_mark(netcp, NULL, ADDR_ANY);
1439 /* finally sweep and callout into modules */
1440 netcp_addr_sweep_del(netcp);
1441 netcp_addr_sweep_add(netcp);
1444 static void netcp_free_navigator_resources(struct netcp_intf *netcp)
1448 if (netcp->rx_channel) {
1449 knav_dma_close_channel(netcp->rx_channel);
1450 netcp->rx_channel = NULL;
1453 if (!IS_ERR_OR_NULL(netcp->rx_pool))
1454 netcp_rxpool_free(netcp);
1456 if (!IS_ERR_OR_NULL(netcp->rx_queue)) {
1457 knav_queue_close(netcp->rx_queue);
1458 netcp->rx_queue = NULL;
1461 for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN &&
1462 !IS_ERR_OR_NULL(netcp->rx_fdq[i]) ; ++i) {
1463 knav_queue_close(netcp->rx_fdq[i]);
1464 netcp->rx_fdq[i] = NULL;
1467 if (!IS_ERR_OR_NULL(netcp->tx_compl_q)) {
1468 knav_queue_close(netcp->tx_compl_q);
1469 netcp->tx_compl_q = NULL;
1472 if (!IS_ERR_OR_NULL(netcp->tx_pool)) {
1473 knav_pool_destroy(netcp->tx_pool);
1474 netcp->tx_pool = NULL;
1478 static int netcp_setup_navigator_resources(struct net_device *ndev)
1480 struct netcp_intf *netcp = netdev_priv(ndev);
1481 struct knav_queue_notify_config notify_cfg;
1482 struct knav_dma_cfg config;
1488 /* Create Rx/Tx descriptor pools */
1489 snprintf(name, sizeof(name), "rx-pool-%s", ndev->name);
1490 netcp->rx_pool = knav_pool_create(name, netcp->rx_pool_size,
1491 netcp->rx_pool_region_id);
1492 if (IS_ERR_OR_NULL(netcp->rx_pool)) {
1493 dev_err(netcp->ndev_dev, "Couldn't create rx pool\n");
1494 ret = PTR_ERR(netcp->rx_pool);
1498 snprintf(name, sizeof(name), "tx-pool-%s", ndev->name);
1499 netcp->tx_pool = knav_pool_create(name, netcp->tx_pool_size,
1500 netcp->tx_pool_region_id);
1501 if (IS_ERR_OR_NULL(netcp->tx_pool)) {
1502 dev_err(netcp->ndev_dev, "Couldn't create tx pool\n");
1503 ret = PTR_ERR(netcp->tx_pool);
1507 /* open Tx completion queue */
1508 snprintf(name, sizeof(name), "tx-compl-%s", ndev->name);
1509 netcp->tx_compl_q = knav_queue_open(name, netcp->tx_compl_qid, 0);
1510 if (IS_ERR_OR_NULL(netcp->tx_compl_q)) {
1511 ret = PTR_ERR(netcp->tx_compl_q);
1514 netcp->tx_compl_qid = knav_queue_get_id(netcp->tx_compl_q);
1516 /* Set notification for Tx completion */
1517 notify_cfg.fn = netcp_tx_notify;
1518 notify_cfg.fn_arg = netcp;
1519 ret = knav_queue_device_control(netcp->tx_compl_q,
1520 KNAV_QUEUE_SET_NOTIFIER,
1521 (unsigned long)¬ify_cfg);
1525 knav_queue_disable_notify(netcp->tx_compl_q);
1527 /* open Rx completion queue */
1528 snprintf(name, sizeof(name), "rx-compl-%s", ndev->name);
1529 netcp->rx_queue = knav_queue_open(name, netcp->rx_queue_id, 0);
1530 if (IS_ERR_OR_NULL(netcp->rx_queue)) {
1531 ret = PTR_ERR(netcp->rx_queue);
1534 netcp->rx_queue_id = knav_queue_get_id(netcp->rx_queue);
1536 /* Set notification for Rx completion */
1537 notify_cfg.fn = netcp_rx_notify;
1538 notify_cfg.fn_arg = netcp;
1539 ret = knav_queue_device_control(netcp->rx_queue,
1540 KNAV_QUEUE_SET_NOTIFIER,
1541 (unsigned long)¬ify_cfg);
1545 knav_queue_disable_notify(netcp->rx_queue);
1548 for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN && netcp->rx_queue_depths[i];
1550 snprintf(name, sizeof(name), "rx-fdq-%s-%d", ndev->name, i);
1551 netcp->rx_fdq[i] = knav_queue_open(name, KNAV_QUEUE_GP, 0);
1552 if (IS_ERR_OR_NULL(netcp->rx_fdq[i])) {
1553 ret = PTR_ERR(netcp->rx_fdq[i]);
1558 memset(&config, 0, sizeof(config));
1559 config.direction = DMA_DEV_TO_MEM;
1560 config.u.rx.einfo_present = true;
1561 config.u.rx.psinfo_present = true;
1562 config.u.rx.err_mode = DMA_DROP;
1563 config.u.rx.desc_type = DMA_DESC_HOST;
1564 config.u.rx.psinfo_at_sop = false;
1565 config.u.rx.sop_offset = NETCP_SOP_OFFSET;
1566 config.u.rx.dst_q = netcp->rx_queue_id;
1567 config.u.rx.thresh = DMA_THRESH_NONE;
1569 for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN; ++i) {
1570 if (netcp->rx_fdq[i])
1571 last_fdq = knav_queue_get_id(netcp->rx_fdq[i]);
1572 config.u.rx.fdq[i] = last_fdq;
1575 netcp->rx_channel = knav_dma_open_channel(netcp->netcp_device->device,
1576 netcp->dma_chan_name, &config);
1577 if (IS_ERR_OR_NULL(netcp->rx_channel)) {
1578 dev_err(netcp->ndev_dev, "failed opening rx chan(%s\n",
1579 netcp->dma_chan_name);
1583 dev_dbg(netcp->ndev_dev, "opened RX channel: %p\n", netcp->rx_channel);
1587 netcp_free_navigator_resources(netcp);
1591 /* Open the device */
1592 static int netcp_ndo_open(struct net_device *ndev)
1594 struct netcp_intf *netcp = netdev_priv(ndev);
1595 struct netcp_intf_modpriv *intf_modpriv;
1596 struct netcp_module *module;
1599 netif_carrier_off(ndev);
1600 ret = netcp_setup_navigator_resources(ndev);
1602 dev_err(netcp->ndev_dev, "Failed to setup navigator resources\n");
1606 mutex_lock(&netcp_modules_lock);
1607 for_each_module(netcp, intf_modpriv) {
1608 module = intf_modpriv->netcp_module;
1610 ret = module->open(intf_modpriv->module_priv, ndev);
1612 dev_err(netcp->ndev_dev, "module open failed\n");
1617 mutex_unlock(&netcp_modules_lock);
1619 napi_enable(&netcp->rx_napi);
1620 napi_enable(&netcp->tx_napi);
1621 knav_queue_enable_notify(netcp->tx_compl_q);
1622 knav_queue_enable_notify(netcp->rx_queue);
1623 netcp_rxpool_refill(netcp);
1624 netif_tx_wake_all_queues(ndev);
1625 dev_dbg(netcp->ndev_dev, "netcp device %s opened\n", ndev->name);
1629 for_each_module(netcp, intf_modpriv) {
1630 module = intf_modpriv->netcp_module;
1632 module->close(intf_modpriv->module_priv, ndev);
1634 mutex_unlock(&netcp_modules_lock);
1637 netcp_free_navigator_resources(netcp);
1641 /* Close the device */
1642 static int netcp_ndo_stop(struct net_device *ndev)
1644 struct netcp_intf *netcp = netdev_priv(ndev);
1645 struct netcp_intf_modpriv *intf_modpriv;
1646 struct netcp_module *module;
1649 netif_tx_stop_all_queues(ndev);
1650 netif_carrier_off(ndev);
1651 netcp_addr_clear_mark(netcp);
1652 netcp_addr_sweep_del(netcp);
1653 knav_queue_disable_notify(netcp->rx_queue);
1654 knav_queue_disable_notify(netcp->tx_compl_q);
1655 napi_disable(&netcp->rx_napi);
1656 napi_disable(&netcp->tx_napi);
1658 mutex_lock(&netcp_modules_lock);
1659 for_each_module(netcp, intf_modpriv) {
1660 module = intf_modpriv->netcp_module;
1661 if (module->close) {
1662 err = module->close(intf_modpriv->module_priv, ndev);
1664 dev_err(netcp->ndev_dev, "Close failed\n");
1667 mutex_unlock(&netcp_modules_lock);
1669 /* Recycle Rx descriptors from completion queue */
1670 netcp_empty_rx_queue(netcp);
1672 /* Recycle Tx descriptors from completion queue */
1673 netcp_process_tx_compl_packets(netcp, netcp->tx_pool_size);
1675 if (knav_pool_count(netcp->tx_pool) != netcp->tx_pool_size)
1676 dev_err(netcp->ndev_dev, "Lost (%d) Tx descs\n",
1677 netcp->tx_pool_size - knav_pool_count(netcp->tx_pool));
1679 netcp_free_navigator_resources(netcp);
1680 dev_dbg(netcp->ndev_dev, "netcp device %s stopped\n", ndev->name);
1684 static int netcp_ndo_ioctl(struct net_device *ndev,
1685 struct ifreq *req, int cmd)
1687 struct netcp_intf *netcp = netdev_priv(ndev);
1688 struct netcp_intf_modpriv *intf_modpriv;
1689 struct netcp_module *module;
1690 int ret = -1, err = -EOPNOTSUPP;
1692 if (!netif_running(ndev))
1695 mutex_lock(&netcp_modules_lock);
1696 for_each_module(netcp, intf_modpriv) {
1697 module = intf_modpriv->netcp_module;
1701 err = module->ioctl(intf_modpriv->module_priv, req, cmd);
1702 if ((err < 0) && (err != -EOPNOTSUPP)) {
1711 mutex_unlock(&netcp_modules_lock);
1712 return (ret == 0) ? 0 : err;
1715 static int netcp_ndo_change_mtu(struct net_device *ndev, int new_mtu)
1717 struct netcp_intf *netcp = netdev_priv(ndev);
1719 /* MTU < 68 is an error for IPv4 traffic */
1720 if ((new_mtu < 68) ||
1721 (new_mtu > (NETCP_MAX_FRAME_SIZE - ETH_HLEN - ETH_FCS_LEN))) {
1722 dev_err(netcp->ndev_dev, "Invalid mtu size = %d\n", new_mtu);
1726 ndev->mtu = new_mtu;
1730 static void netcp_ndo_tx_timeout(struct net_device *ndev)
1732 struct netcp_intf *netcp = netdev_priv(ndev);
1733 unsigned int descs = knav_pool_count(netcp->tx_pool);
1735 dev_err(netcp->ndev_dev, "transmit timed out tx descs(%d)\n", descs);
1736 netcp_process_tx_compl_packets(netcp, netcp->tx_pool_size);
1737 ndev->trans_start = jiffies;
1738 netif_tx_wake_all_queues(ndev);
1741 static int netcp_rx_add_vid(struct net_device *ndev, __be16 proto, u16 vid)
1743 struct netcp_intf *netcp = netdev_priv(ndev);
1744 struct netcp_intf_modpriv *intf_modpriv;
1745 struct netcp_module *module;
1748 dev_dbg(netcp->ndev_dev, "adding rx vlan id: %d\n", vid);
1750 mutex_lock(&netcp_modules_lock);
1751 for_each_module(netcp, intf_modpriv) {
1752 module = intf_modpriv->netcp_module;
1753 if ((module->add_vid) && (vid != 0)) {
1754 err = module->add_vid(intf_modpriv->module_priv, vid);
1756 dev_err(netcp->ndev_dev, "Could not add vlan id = %d\n",
1762 mutex_unlock(&netcp_modules_lock);
1766 static int netcp_rx_kill_vid(struct net_device *ndev, __be16 proto, u16 vid)
1768 struct netcp_intf *netcp = netdev_priv(ndev);
1769 struct netcp_intf_modpriv *intf_modpriv;
1770 struct netcp_module *module;
1773 dev_dbg(netcp->ndev_dev, "removing rx vlan id: %d\n", vid);
1775 mutex_lock(&netcp_modules_lock);
1776 for_each_module(netcp, intf_modpriv) {
1777 module = intf_modpriv->netcp_module;
1778 if (module->del_vid) {
1779 err = module->del_vid(intf_modpriv->module_priv, vid);
1781 dev_err(netcp->ndev_dev, "Could not delete vlan id = %d\n",
1787 mutex_unlock(&netcp_modules_lock);
1791 static u16 netcp_select_queue(struct net_device *dev, struct sk_buff *skb,
1793 select_queue_fallback_t fallback)
1798 static int netcp_setup_tc(struct net_device *dev, u8 num_tc)
1802 /* setup tc must be called under rtnl lock */
1805 /* Sanity-check the number of traffic classes requested */
1806 if ((dev->real_num_tx_queues <= 1) ||
1807 (dev->real_num_tx_queues < num_tc))
1810 /* Configure traffic class to queue mappings */
1812 netdev_set_num_tc(dev, num_tc);
1813 for (i = 0; i < num_tc; i++)
1814 netdev_set_tc_queue(dev, i, 1, i);
1816 netdev_reset_tc(dev);
1822 static const struct net_device_ops netcp_netdev_ops = {
1823 .ndo_open = netcp_ndo_open,
1824 .ndo_stop = netcp_ndo_stop,
1825 .ndo_start_xmit = netcp_ndo_start_xmit,
1826 .ndo_set_rx_mode = netcp_set_rx_mode,
1827 .ndo_do_ioctl = netcp_ndo_ioctl,
1828 .ndo_change_mtu = netcp_ndo_change_mtu,
1829 .ndo_set_mac_address = eth_mac_addr,
1830 .ndo_validate_addr = eth_validate_addr,
1831 .ndo_vlan_rx_add_vid = netcp_rx_add_vid,
1832 .ndo_vlan_rx_kill_vid = netcp_rx_kill_vid,
1833 .ndo_tx_timeout = netcp_ndo_tx_timeout,
1834 .ndo_select_queue = netcp_select_queue,
1835 .ndo_setup_tc = netcp_setup_tc,
1838 static int netcp_create_interface(struct netcp_device *netcp_device,
1839 struct device_node *node_interface)
1841 struct device *dev = netcp_device->device;
1842 struct device_node *node = dev->of_node;
1843 struct netcp_intf *netcp;
1844 struct net_device *ndev;
1845 resource_size_t size;
1846 struct resource res;
1847 void __iomem *efuse = NULL;
1849 const void *mac_addr;
1850 u8 efuse_mac_addr[6];
1854 ndev = alloc_etherdev_mqs(sizeof(*netcp), 1, 1);
1856 dev_err(dev, "Error allocating netdev\n");
1860 ndev->features |= NETIF_F_SG;
1861 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1862 ndev->hw_features = ndev->features;
1863 ndev->vlan_features |= NETIF_F_SG;
1865 netcp = netdev_priv(ndev);
1866 spin_lock_init(&netcp->lock);
1867 INIT_LIST_HEAD(&netcp->module_head);
1868 INIT_LIST_HEAD(&netcp->txhook_list_head);
1869 INIT_LIST_HEAD(&netcp->rxhook_list_head);
1870 INIT_LIST_HEAD(&netcp->addr_list);
1871 netcp->netcp_device = netcp_device;
1872 netcp->dev = netcp_device->device;
1874 netcp->ndev_dev = &ndev->dev;
1875 netcp->msg_enable = netif_msg_init(netcp_debug_level, NETCP_DEBUG);
1876 netcp->tx_pause_threshold = MAX_SKB_FRAGS;
1877 netcp->tx_resume_threshold = netcp->tx_pause_threshold;
1878 netcp->node_interface = node_interface;
1880 ret = of_property_read_u32(node_interface, "efuse-mac", &efuse_mac);
1882 if (of_address_to_resource(node, NETCP_EFUSE_REG_INDEX, &res)) {
1883 dev_err(dev, "could not find efuse-mac reg resource\n");
1887 size = resource_size(&res);
1889 if (!devm_request_mem_region(dev, res.start, size,
1891 dev_err(dev, "could not reserve resource\n");
1896 efuse = devm_ioremap_nocache(dev, res.start, size);
1898 dev_err(dev, "could not map resource\n");
1899 devm_release_mem_region(dev, res.start, size);
1904 emac_arch_get_mac_addr(efuse_mac_addr, efuse);
1905 if (is_valid_ether_addr(efuse_mac_addr))
1906 ether_addr_copy(ndev->dev_addr, efuse_mac_addr);
1908 random_ether_addr(ndev->dev_addr);
1910 devm_iounmap(dev, efuse);
1911 devm_release_mem_region(dev, res.start, size);
1913 mac_addr = of_get_mac_address(node_interface);
1915 ether_addr_copy(ndev->dev_addr, mac_addr);
1917 random_ether_addr(ndev->dev_addr);
1920 ret = of_property_read_string(node_interface, "rx-channel",
1921 &netcp->dma_chan_name);
1923 dev_err(dev, "missing \"rx-channel\" parameter\n");
1928 ret = of_property_read_u32(node_interface, "rx-queue",
1929 &netcp->rx_queue_id);
1931 dev_warn(dev, "missing \"rx-queue\" parameter\n");
1932 netcp->rx_queue_id = KNAV_QUEUE_QPEND;
1935 ret = of_property_read_u32_array(node_interface, "rx-queue-depth",
1936 netcp->rx_queue_depths,
1937 KNAV_DMA_FDQ_PER_CHAN);
1939 dev_err(dev, "missing \"rx-queue-depth\" parameter\n");
1940 netcp->rx_queue_depths[0] = 128;
1943 ret = of_property_read_u32_array(node_interface, "rx-pool", temp, 2);
1945 dev_err(dev, "missing \"rx-pool\" parameter\n");
1949 netcp->rx_pool_size = temp[0];
1950 netcp->rx_pool_region_id = temp[1];
1952 ret = of_property_read_u32_array(node_interface, "tx-pool", temp, 2);
1954 dev_err(dev, "missing \"tx-pool\" parameter\n");
1958 netcp->tx_pool_size = temp[0];
1959 netcp->tx_pool_region_id = temp[1];
1961 if (netcp->tx_pool_size < MAX_SKB_FRAGS) {
1962 dev_err(dev, "tx-pool size too small, must be atleast(%ld)\n",
1968 ret = of_property_read_u32(node_interface, "tx-completion-queue",
1969 &netcp->tx_compl_qid);
1971 dev_warn(dev, "missing \"tx-completion-queue\" parameter\n");
1972 netcp->tx_compl_qid = KNAV_QUEUE_QPEND;
1976 netif_napi_add(ndev, &netcp->rx_napi, netcp_rx_poll, NETCP_NAPI_WEIGHT);
1977 netif_napi_add(ndev, &netcp->tx_napi, netcp_tx_poll, NETCP_NAPI_WEIGHT);
1979 /* Register the network device */
1981 ndev->watchdog_timeo = NETCP_TX_TIMEOUT;
1982 ndev->netdev_ops = &netcp_netdev_ops;
1983 SET_NETDEV_DEV(ndev, dev);
1985 list_add_tail(&netcp->interface_list, &netcp_device->interface_head);
1993 static void netcp_delete_interface(struct netcp_device *netcp_device,
1994 struct net_device *ndev)
1996 struct netcp_intf_modpriv *intf_modpriv, *tmp;
1997 struct netcp_intf *netcp = netdev_priv(ndev);
1998 struct netcp_module *module;
2000 dev_dbg(netcp_device->device, "Removing interface \"%s\"\n",
2003 /* Notify each of the modules that the interface is going away */
2004 list_for_each_entry_safe(intf_modpriv, tmp, &netcp->module_head,
2006 module = intf_modpriv->netcp_module;
2007 dev_dbg(netcp_device->device, "Releasing module \"%s\"\n",
2009 if (module->release)
2010 module->release(intf_modpriv->module_priv);
2011 list_del(&intf_modpriv->intf_list);
2012 kfree(intf_modpriv);
2014 WARN(!list_empty(&netcp->module_head), "%s interface module list is not empty!\n",
2017 list_del(&netcp->interface_list);
2019 of_node_put(netcp->node_interface);
2020 unregister_netdev(ndev);
2021 netif_napi_del(&netcp->rx_napi);
2025 static int netcp_probe(struct platform_device *pdev)
2027 struct device_node *node = pdev->dev.of_node;
2028 struct netcp_intf *netcp_intf, *netcp_tmp;
2029 struct device_node *child, *interfaces;
2030 struct netcp_device *netcp_device;
2031 struct device *dev = &pdev->dev;
2032 struct netcp_module *module;
2036 dev_err(dev, "could not find device info\n");
2040 /* Allocate a new NETCP device instance */
2041 netcp_device = devm_kzalloc(dev, sizeof(*netcp_device), GFP_KERNEL);
2045 pm_runtime_enable(&pdev->dev);
2046 ret = pm_runtime_get_sync(&pdev->dev);
2048 dev_err(dev, "Failed to enable NETCP power-domain\n");
2049 pm_runtime_disable(&pdev->dev);
2053 /* Initialize the NETCP device instance */
2054 INIT_LIST_HEAD(&netcp_device->interface_head);
2055 INIT_LIST_HEAD(&netcp_device->modpriv_head);
2056 netcp_device->device = dev;
2057 platform_set_drvdata(pdev, netcp_device);
2059 /* create interfaces */
2060 interfaces = of_get_child_by_name(node, "netcp-interfaces");
2062 dev_err(dev, "could not find netcp-interfaces node\n");
2067 for_each_available_child_of_node(interfaces, child) {
2068 ret = netcp_create_interface(netcp_device, child);
2070 dev_err(dev, "could not create interface(%s)\n",
2072 goto probe_quit_interface;
2076 /* Add the device instance to the list */
2077 list_add_tail(&netcp_device->device_list, &netcp_devices);
2079 /* Probe & attach any modules already registered */
2080 mutex_lock(&netcp_modules_lock);
2081 for_each_netcp_module(module) {
2082 ret = netcp_module_probe(netcp_device, module);
2084 dev_err(dev, "module(%s) probe failed\n", module->name);
2086 mutex_unlock(&netcp_modules_lock);
2089 probe_quit_interface:
2090 list_for_each_entry_safe(netcp_intf, netcp_tmp,
2091 &netcp_device->interface_head,
2093 netcp_delete_interface(netcp_device, netcp_intf->ndev);
2097 pm_runtime_put_sync(&pdev->dev);
2098 pm_runtime_disable(&pdev->dev);
2099 platform_set_drvdata(pdev, NULL);
2103 static int netcp_remove(struct platform_device *pdev)
2105 struct netcp_device *netcp_device = platform_get_drvdata(pdev);
2106 struct netcp_intf *netcp_intf, *netcp_tmp;
2107 struct netcp_inst_modpriv *inst_modpriv, *tmp;
2108 struct netcp_module *module;
2110 list_for_each_entry_safe(inst_modpriv, tmp, &netcp_device->modpriv_head,
2112 module = inst_modpriv->netcp_module;
2113 dev_dbg(&pdev->dev, "Removing module \"%s\"\n", module->name);
2114 module->remove(netcp_device, inst_modpriv->module_priv);
2115 list_del(&inst_modpriv->inst_list);
2116 kfree(inst_modpriv);
2119 /* now that all modules are removed, clean up the interfaces */
2120 list_for_each_entry_safe(netcp_intf, netcp_tmp,
2121 &netcp_device->interface_head,
2123 netcp_delete_interface(netcp_device, netcp_intf->ndev);
2126 WARN(!list_empty(&netcp_device->interface_head),
2127 "%s interface list not empty!\n", pdev->name);
2129 pm_runtime_put_sync(&pdev->dev);
2130 pm_runtime_disable(&pdev->dev);
2131 platform_set_drvdata(pdev, NULL);
2135 static const struct of_device_id of_match[] = {
2136 { .compatible = "ti,netcp-1.0", },
2139 MODULE_DEVICE_TABLE(of, of_match);
2141 static struct platform_driver netcp_driver = {
2143 .name = "netcp-1.0",
2144 .owner = THIS_MODULE,
2145 .of_match_table = of_match,
2147 .probe = netcp_probe,
2148 .remove = netcp_remove,
2150 module_platform_driver(netcp_driver);
2152 MODULE_LICENSE("GPL v2");
2153 MODULE_DESCRIPTION("TI NETCP driver for Keystone SOCs");
2154 MODULE_AUTHOR("Sandeep Nair <sandeep_n@ti.com");