1/* 2 * Keystone NetCP Core driver 3 * 4 * Copyright (C) 2014 Texas Instruments Incorporated 5 * Authors: Sandeep Nair <sandeep_n@ti.com> 6 * Sandeep Paulraj <s-paulraj@ti.com> 7 * Cyril Chemparathy <cyril@ti.com> 8 * Santosh Shilimkar <santosh.shilimkar@ti.com> 9 * Murali Karicheri <m-karicheri2@ti.com> 10 * Wingman Kwok <w-kwok2@ti.com> 11 * 12 * This program is free software; you can redistribute it and/or 13 * modify it under the terms of the GNU General Public License as 14 * published by the Free Software Foundation version 2. 15 * 16 * This program is distributed "as is" WITHOUT ANY WARRANTY of any 17 * kind, whether express or implied; without even the implied warranty 18 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU General Public License for more details. 20 */ 21 22#include <linux/io.h> 23#include <linux/module.h> 24#include <linux/of_net.h> 25#include <linux/of_address.h> 26#include <linux/if_vlan.h> 27#include <linux/pm_runtime.h> 28#include <linux/platform_device.h> 29#include <linux/soc/ti/knav_qmss.h> 30#include <linux/soc/ti/knav_dma.h> 31 32#include "netcp.h" 33 34#define NETCP_SOP_OFFSET (NET_IP_ALIGN + NET_SKB_PAD) 35#define NETCP_NAPI_WEIGHT 64 36#define NETCP_TX_TIMEOUT (5 * HZ) 37#define NETCP_PACKET_SIZE (ETH_FRAME_LEN + ETH_FCS_LEN) 38#define NETCP_MIN_PACKET_SIZE ETH_ZLEN 39#define NETCP_MAX_MCAST_ADDR 16 40 41#define NETCP_EFUSE_REG_INDEX 0 42 43#define NETCP_MOD_PROBE_SKIPPED 1 44#define NETCP_MOD_PROBE_FAILED 2 45 46#define NETCP_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \ 47 NETIF_MSG_DRV | NETIF_MSG_LINK | \ 48 NETIF_MSG_IFUP | NETIF_MSG_INTR | \ 49 NETIF_MSG_PROBE | NETIF_MSG_TIMER | \ 50 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | \ 51 NETIF_MSG_TX_ERR | NETIF_MSG_TX_DONE | \ 52 NETIF_MSG_PKTDATA | NETIF_MSG_TX_QUEUED | \ 53 NETIF_MSG_RX_STATUS) 54 55#define NETCP_EFUSE_ADDR_SWAP 2 56 57#define knav_queue_get_id(q) knav_queue_device_control(q, \ 58 KNAV_QUEUE_GET_ID, (unsigned long)NULL) 59 60#define knav_queue_enable_notify(q) knav_queue_device_control(q, \ 61 KNAV_QUEUE_ENABLE_NOTIFY, \ 62 (unsigned long)NULL) 63 64#define knav_queue_disable_notify(q) knav_queue_device_control(q, \ 65 KNAV_QUEUE_DISABLE_NOTIFY, \ 66 (unsigned long)NULL) 67 68#define knav_queue_get_count(q) knav_queue_device_control(q, \ 69 KNAV_QUEUE_GET_COUNT, (unsigned long)NULL) 70 71#define for_each_netcp_module(module) \ 72 list_for_each_entry(module, &netcp_modules, module_list) 73 74#define for_each_netcp_device_module(netcp_device, inst_modpriv) \ 75 list_for_each_entry(inst_modpriv, \ 76 &((netcp_device)->modpriv_head), inst_list) 77 78#define for_each_module(netcp, intf_modpriv) \ 79 list_for_each_entry(intf_modpriv, &netcp->module_head, intf_list) 80 81/* Module management structures */ 82struct netcp_device { 83 struct list_head device_list; 84 struct list_head interface_head; 85 struct list_head modpriv_head; 86 struct device *device; 87}; 88 89struct netcp_inst_modpriv { 90 struct netcp_device *netcp_device; 91 struct netcp_module *netcp_module; 92 struct list_head inst_list; 93 void *module_priv; 94}; 95 96struct netcp_intf_modpriv { 97 struct netcp_intf *netcp_priv; 98 struct netcp_module *netcp_module; 99 struct list_head intf_list; 100 void *module_priv; 101}; 102 103static LIST_HEAD(netcp_devices); 104static LIST_HEAD(netcp_modules); 105static DEFINE_MUTEX(netcp_modules_lock); 106 107static int netcp_debug_level = -1; 108module_param(netcp_debug_level, int, 0); 109MODULE_PARM_DESC(netcp_debug_level, "Netcp debug level (NETIF_MSG bits) (0=none,...,16=all)"); 110 111/* Helper functions - Get/Set */ 112static void get_pkt_info(u32 *buff, u32 *buff_len, u32 *ndesc, 113 struct knav_dma_desc *desc) 114{ 115 *buff_len = desc->buff_len; 116 *buff = desc->buff; 117 *ndesc = desc->next_desc; 118} 119 120static void get_pad_info(u32 *pad0, u32 *pad1, struct knav_dma_desc *desc) 121{ 122 *pad0 = desc->pad[0]; 123 *pad1 = desc->pad[1]; 124} 125 126static void get_org_pkt_info(u32 *buff, u32 *buff_len, 127 struct knav_dma_desc *desc) 128{ 129 *buff = desc->orig_buff; 130 *buff_len = desc->orig_len; 131} 132 133static void get_words(u32 *words, int num_words, u32 *desc) 134{ 135 int i; 136 137 for (i = 0; i < num_words; i++) 138 words[i] = desc[i]; 139} 140 141static void set_pkt_info(u32 buff, u32 buff_len, u32 ndesc, 142 struct knav_dma_desc *desc) 143{ 144 desc->buff_len = buff_len; 145 desc->buff = buff; 146 desc->next_desc = ndesc; 147} 148 149static void set_desc_info(u32 desc_info, u32 pkt_info, 150 struct knav_dma_desc *desc) 151{ 152 desc->desc_info = desc_info; 153 desc->packet_info = pkt_info; 154} 155 156static void set_pad_info(u32 pad0, u32 pad1, struct knav_dma_desc *desc) 157{ 158 desc->pad[0] = pad0; 159 desc->pad[1] = pad1; 160} 161 162static void set_org_pkt_info(u32 buff, u32 buff_len, 163 struct knav_dma_desc *desc) 164{ 165 desc->orig_buff = buff; 166 desc->orig_len = buff_len; 167} 168 169static void set_words(u32 *words, int num_words, u32 *desc) 170{ 171 int i; 172 173 for (i = 0; i < num_words; i++) 174 desc[i] = words[i]; 175} 176 177/* Read the e-fuse value as 32 bit values to be endian independent */ 178static int emac_arch_get_mac_addr(char *x, void __iomem *efuse_mac, u32 swap) 179{ 180 unsigned int addr0, addr1; 181 182 addr1 = readl(efuse_mac + 4); 183 addr0 = readl(efuse_mac); 184 185 switch (swap) { 186 case NETCP_EFUSE_ADDR_SWAP: 187 addr0 = addr1; 188 addr1 = readl(efuse_mac); 189 break; 190 default: 191 break; 192 } 193 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; 200 201 return 0; 202} 203 204static const char *netcp_node_name(struct device_node *node) 205{ 206 const char *name; 207 208 if (of_property_read_string(node, "label", &name) < 0) 209 name = node->name; 210 if (!name) 211 name = "unknown"; 212 return name; 213} 214 215/* Module management routines */ 216static int netcp_register_interface(struct netcp_intf *netcp) 217{ 218 int ret; 219 220 ret = register_netdev(netcp->ndev); 221 if (!ret) 222 netcp->netdev_registered = true; 223 return ret; 224} 225 226static int netcp_module_probe(struct netcp_device *netcp_device, 227 struct netcp_module *module) 228{ 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; 236 int ret; 237 238 /* Find this module in the sub-tree for this device */ 239 devices = of_get_child_by_name(node, "netcp-devices"); 240 if (!devices) { 241 dev_err(dev, "could not find netcp-devices node\n"); 242 return NETCP_MOD_PROBE_SKIPPED; 243 } 244 245 for_each_available_child_of_node(devices, child) { 246 const char *name = netcp_node_name(child); 247 248 if (!strcasecmp(module->name, name)) 249 break; 250 } 251 252 of_node_put(devices); 253 /* If module not used for this device, skip it */ 254 if (!child) { 255 dev_warn(dev, "module(%s) not used for device\n", module->name); 256 return NETCP_MOD_PROBE_SKIPPED; 257 } 258 259 inst_modpriv = devm_kzalloc(dev, sizeof(*inst_modpriv), GFP_KERNEL); 260 if (!inst_modpriv) { 261 of_node_put(child); 262 return -ENOMEM; 263 } 264 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); 268 269 ret = module->probe(netcp_device, dev, child, 270 &inst_modpriv->module_priv); 271 of_node_put(child); 272 if (ret) { 273 dev_err(dev, "Probe of module(%s) failed with %d\n", 274 module->name, ret); 275 list_del(&inst_modpriv->inst_list); 276 devm_kfree(dev, inst_modpriv); 277 return NETCP_MOD_PROBE_FAILED; 278 } 279 280 /* Attach modules only if the primary module is probed */ 281 for_each_netcp_module(tmp) { 282 if (tmp->primary) 283 primary_module_registered = true; 284 } 285 286 if (!primary_module_registered) 287 return 0; 288 289 /* Attach module to interfaces */ 290 list_for_each_entry(netcp_intf, &netcp_device->interface_head, 291 interface_list) { 292 struct netcp_intf_modpriv *intf_modpriv; 293 294 intf_modpriv = devm_kzalloc(dev, sizeof(*intf_modpriv), 295 GFP_KERNEL); 296 if (!intf_modpriv) 297 return -ENOMEM; 298 299 interface = of_parse_phandle(netcp_intf->node_interface, 300 module->name, 0); 301 302 if (!interface) { 303 devm_kfree(dev, intf_modpriv); 304 continue; 305 } 306 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); 311 312 ret = module->attach(inst_modpriv->module_priv, 313 netcp_intf->ndev, interface, 314 &intf_modpriv->module_priv); 315 of_node_put(interface); 316 if (ret) { 317 dev_dbg(dev, "Attach of module %s declined with %d\n", 318 module->name, ret); 319 list_del(&intf_modpriv->intf_list); 320 devm_kfree(dev, intf_modpriv); 321 continue; 322 } 323 } 324 325 /* Now register the interface with netdev */ 326 list_for_each_entry(netcp_intf, 327 &netcp_device->interface_head, 328 interface_list) { 329 /* If interface not registered then register now */ 330 if (!netcp_intf->netdev_registered) { 331 ret = netcp_register_interface(netcp_intf); 332 if (ret) 333 return -ENODEV; 334 } 335 } 336 return 0; 337} 338 339int netcp_register_module(struct netcp_module *module) 340{ 341 struct netcp_device *netcp_device; 342 struct netcp_module *tmp; 343 int ret; 344 345 if (!module->name) { 346 WARN(1, "error registering netcp module: no name\n"); 347 return -EINVAL; 348 } 349 350 if (!module->probe) { 351 WARN(1, "error registering netcp module: no probe\n"); 352 return -EINVAL; 353 } 354 355 mutex_lock(&netcp_modules_lock); 356 357 for_each_netcp_module(tmp) { 358 if (!strcasecmp(tmp->name, module->name)) { 359 mutex_unlock(&netcp_modules_lock); 360 return -EEXIST; 361 } 362 } 363 list_add_tail(&module->module_list, &netcp_modules); 364 365 list_for_each_entry(netcp_device, &netcp_devices, device_list) { 366 ret = netcp_module_probe(netcp_device, module); 367 if (ret < 0) 368 goto fail; 369 } 370 mutex_unlock(&netcp_modules_lock); 371 return 0; 372 373fail: 374 mutex_unlock(&netcp_modules_lock); 375 netcp_unregister_module(module); 376 return ret; 377} 378EXPORT_SYMBOL_GPL(netcp_register_module); 379 380static void netcp_release_module(struct netcp_device *netcp_device, 381 struct netcp_module *module) 382{ 383 struct netcp_inst_modpriv *inst_modpriv, *inst_tmp; 384 struct netcp_intf *netcp_intf, *netcp_tmp; 385 struct device *dev = netcp_device->device; 386 387 /* Release the module from each interface */ 388 list_for_each_entry_safe(netcp_intf, netcp_tmp, 389 &netcp_device->interface_head, 390 interface_list) { 391 struct netcp_intf_modpriv *intf_modpriv, *intf_tmp; 392 393 list_for_each_entry_safe(intf_modpriv, intf_tmp, 394 &netcp_intf->module_head, 395 intf_list) { 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); 400 break; 401 } 402 } 403 } 404 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); 413 break; 414 } 415 } 416} 417 418void netcp_unregister_module(struct netcp_module *module) 419{ 420 struct netcp_device *netcp_device; 421 struct netcp_module *module_tmp; 422 423 mutex_lock(&netcp_modules_lock); 424 425 list_for_each_entry(netcp_device, &netcp_devices, device_list) { 426 netcp_release_module(netcp_device, module); 427 } 428 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); 433 break; 434 } 435 } 436 437 mutex_unlock(&netcp_modules_lock); 438} 439EXPORT_SYMBOL_GPL(netcp_unregister_module); 440 441void *netcp_module_get_intf_data(struct netcp_module *module, 442 struct netcp_intf *intf) 443{ 444 struct netcp_intf_modpriv *intf_modpriv; 445 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; 449 return NULL; 450} 451EXPORT_SYMBOL_GPL(netcp_module_get_intf_data); 452 453/* Module TX and RX Hook management */ 454struct netcp_hook_list { 455 struct list_head list; 456 netcp_hook_rtn *hook_rtn; 457 void *hook_data; 458 int order; 459}; 460 461int netcp_register_txhook(struct netcp_intf *netcp_priv, int order, 462 netcp_hook_rtn *hook_rtn, void *hook_data) 463{ 464 struct netcp_hook_list *entry; 465 struct netcp_hook_list *next; 466 unsigned long flags; 467 468 entry = devm_kzalloc(netcp_priv->dev, sizeof(*entry), GFP_KERNEL); 469 if (!entry) 470 return -ENOMEM; 471 472 entry->hook_rtn = hook_rtn; 473 entry->hook_data = hook_data; 474 entry->order = order; 475 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) 479 break; 480 } 481 __list_add(&entry->list, next->list.prev, &next->list); 482 spin_unlock_irqrestore(&netcp_priv->lock, flags); 483 484 return 0; 485} 486EXPORT_SYMBOL_GPL(netcp_register_txhook); 487 488int netcp_unregister_txhook(struct netcp_intf *netcp_priv, int order, 489 netcp_hook_rtn *hook_rtn, void *hook_data) 490{ 491 struct netcp_hook_list *next, *n; 492 unsigned long flags; 493 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); 502 return 0; 503 } 504 } 505 spin_unlock_irqrestore(&netcp_priv->lock, flags); 506 return -ENOENT; 507} 508EXPORT_SYMBOL_GPL(netcp_unregister_txhook); 509 510int netcp_register_rxhook(struct netcp_intf *netcp_priv, int order, 511 netcp_hook_rtn *hook_rtn, void *hook_data) 512{ 513 struct netcp_hook_list *entry; 514 struct netcp_hook_list *next; 515 unsigned long flags; 516 517 entry = devm_kzalloc(netcp_priv->dev, sizeof(*entry), GFP_KERNEL); 518 if (!entry) 519 return -ENOMEM; 520 521 entry->hook_rtn = hook_rtn; 522 entry->hook_data = hook_data; 523 entry->order = order; 524 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) 528 break; 529 } 530 __list_add(&entry->list, next->list.prev, &next->list); 531 spin_unlock_irqrestore(&netcp_priv->lock, flags); 532 533 return 0; 534} 535 536int netcp_unregister_rxhook(struct netcp_intf *netcp_priv, int order, 537 netcp_hook_rtn *hook_rtn, void *hook_data) 538{ 539 struct netcp_hook_list *next, *n; 540 unsigned long flags; 541 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); 550 return 0; 551 } 552 } 553 spin_unlock_irqrestore(&netcp_priv->lock, flags); 554 555 return -ENOENT; 556} 557 558static void netcp_frag_free(bool is_frag, void *ptr) 559{ 560 if (is_frag) 561 skb_free_frag(ptr); 562 else 563 kfree(ptr); 564} 565 566static void netcp_free_rx_desc_chain(struct netcp_intf *netcp, 567 struct knav_dma_desc *desc) 568{ 569 struct knav_dma_desc *ndesc; 570 dma_addr_t dma_desc, dma_buf; 571 unsigned int buf_len, dma_sz = sizeof(*ndesc); 572 void *buf_ptr; 573 u32 tmp; 574 575 get_words(&dma_desc, 1, &desc->next_desc); 576 577 while (dma_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"); 581 break; 582 } 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); 588 } 589 590 get_pad_info((u32 *)&buf_ptr, &buf_len, desc); 591 if (buf_ptr) 592 netcp_frag_free(buf_len <= PAGE_SIZE, buf_ptr); 593 knav_pool_desc_put(netcp->rx_pool, desc); 594} 595 596static void netcp_empty_rx_queue(struct netcp_intf *netcp) 597{ 598 struct knav_dma_desc *desc; 599 unsigned int dma_sz; 600 dma_addr_t dma; 601 602 for (; ;) { 603 dma = knav_queue_pop(netcp->rx_queue, &dma_sz); 604 if (!dma) 605 break; 606 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", 610 __func__); 611 netcp->ndev->stats.rx_errors++; 612 continue; 613 } 614 netcp_free_rx_desc_chain(netcp, desc); 615 netcp->ndev->stats.rx_dropped++; 616 } 617} 618 619static int netcp_process_one_rx_packet(struct netcp_intf *netcp) 620{ 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; 627 struct sk_buff *skb; 628 void *org_buf_ptr; 629 u32 tmp; 630 631 dma_desc = knav_queue_pop(netcp->rx_queue, &dma_sz); 632 if (!dma_desc) 633 return -1; 634 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"); 638 return 0; 639 } 640 641 get_pkt_info(&dma_buff, &buf_len, &dma_desc, desc); 642 get_pad_info((u32 *)&org_buf_ptr, &org_buf_len, desc); 643 644 if (unlikely(!org_buf_ptr)) { 645 dev_err(netcp->ndev_dev, "NULL bufptr in desc\n"); 646 goto free_desc; 647 } 648 649 pkt_sz &= KNAV_DMA_DESC_PKT_LEN_MASK; 650 accum_sz = buf_len; 651 dma_unmap_single(netcp->dev, dma_buff, buf_len, DMA_FROM_DEVICE); 652 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"); 657 goto free_desc; 658 } 659 660 /* update data, tail and len */ 661 skb_reserve(skb, NETCP_SOP_OFFSET); 662 __skb_put(skb, buf_len); 663 664 /* Fill in the page fragment list */ 665 while (dma_desc) { 666 struct page *page; 667 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"); 671 goto free_desc; 672 } 673 674 get_pkt_info(&dma_buff, &buf_len, &dma_desc, ndesc); 675 get_pad_info((u32 *)&page, &tmp, ndesc); 676 677 if (likely(dma_buff && buf_len && page)) { 678 dma_unmap_page(netcp->dev, dma_buff, PAGE_SIZE, 679 DMA_FROM_DEVICE); 680 } else { 681 dev_err(netcp->ndev_dev, "Bad Rx desc dma_buff(%p), len(%d), page(%p)\n", 682 (void *)dma_buff, buf_len, page); 683 goto free_desc; 684 } 685 686 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, 687 offset_in_page(dma_buff), buf_len, PAGE_SIZE); 688 accum_sz += buf_len; 689 690 /* Free the descriptor */ 691 knav_pool_desc_put(netcp->rx_pool, ndesc); 692 } 693 694 /* Free the primary descriptor */ 695 knav_pool_desc_put(netcp->rx_pool, desc); 696 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", 700 pkt_sz, accum_sz); 701 702 /* Remove ethernet FCS from the packet */ 703 __pskb_trim(skb, skb->len - ETH_FCS_LEN); 704 705 /* Call each of the RX hooks */ 706 p_info.skb = skb; 707 p_info.rxtstamp_complete = false; 708 list_for_each_entry(rx_hook, &netcp->rxhook_list_head, list) { 709 int ret; 710 711 ret = rx_hook->hook_rtn(rx_hook->order, rx_hook->hook_data, 712 &p_info); 713 if (unlikely(ret)) { 714 dev_err(netcp->ndev_dev, "RX hook %d failed: %d\n", 715 rx_hook->order, ret); 716 netcp->ndev->stats.rx_errors++; 717 dev_kfree_skb(skb); 718 return 0; 719 } 720 } 721 722 netcp->ndev->stats.rx_packets++; 723 netcp->ndev->stats.rx_bytes += skb->len; 724 725 /* push skb up the stack */ 726 skb->protocol = eth_type_trans(skb, netcp->ndev); 727 netif_receive_skb(skb); 728 return 0; 729 730free_desc: 731 netcp_free_rx_desc_chain(netcp, desc); 732 netcp->ndev->stats.rx_errors++; 733 return 0; 734} 735 736static int netcp_process_rx_packets(struct netcp_intf *netcp, 737 unsigned int budget) 738{ 739 int i; 740 741 for (i = 0; (i < budget) && !netcp_process_one_rx_packet(netcp); i++) 742 ; 743 return i; 744} 745 746/* Release descriptors and attached buffers from Rx FDQ */ 747static void netcp_free_rx_buf(struct netcp_intf *netcp, int fdq) 748{ 749 struct knav_dma_desc *desc; 750 unsigned int buf_len, dma_sz; 751 dma_addr_t dma; 752 void *buf_ptr; 753 u32 tmp; 754 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"); 760 continue; 761 } 762 763 get_org_pkt_info(&dma, &buf_len, desc); 764 get_pad_info((u32 *)&buf_ptr, &tmp, desc); 765 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); 769 continue; 770 } 771 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); 775 continue; 776 } 777 778 if (fdq == 0) { 779 dma_unmap_single(netcp->dev, dma, buf_len, 780 DMA_FROM_DEVICE); 781 netcp_frag_free((buf_len <= PAGE_SIZE), buf_ptr); 782 } else { 783 dma_unmap_page(netcp->dev, dma, buf_len, 784 DMA_FROM_DEVICE); 785 __free_page(buf_ptr); 786 } 787 788 knav_pool_desc_put(netcp->rx_pool, desc); 789 } 790} 791 792static void netcp_rxpool_free(struct netcp_intf *netcp) 793{ 794 int i; 795 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); 799 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)); 803 804 knav_pool_destroy(netcp->rx_pool); 805 netcp->rx_pool = NULL; 806} 807 808static int netcp_allocate_rx_buf(struct netcp_intf *netcp, int fdq) 809{ 810 struct knav_dma_desc *hwdesc; 811 unsigned int buf_len, dma_sz; 812 u32 desc_info, pkt_info; 813 struct page *page; 814 dma_addr_t dma; 815 void *bufptr; 816 u32 pad[2]; 817 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"); 822 return -ENOMEM; 823 } 824 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)); 831 832 bufptr = netdev_alloc_frag(primary_buf_len); 833 pad[1] = primary_buf_len; 834 835 if (unlikely(!bufptr)) { 836 dev_warn_ratelimited(netcp->ndev_dev, 837 "Primary RX buffer alloc failed\n"); 838 goto fail; 839 } 840 dma = dma_map_single(netcp->dev, bufptr, buf_len, 841 DMA_TO_DEVICE); 842 if (unlikely(dma_mapping_error(netcp->dev, dma))) 843 goto fail; 844 845 pad[0] = (u32)bufptr; 846 847 } else { 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"); 852 goto fail; 853 } 854 buf_len = PAGE_SIZE; 855 dma = dma_map_page(netcp->dev, page, 0, buf_len, DMA_TO_DEVICE); 856 pad[0] = (u32)page; 857 pad[1] = 0; 858 } 859 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); 869 870 /* Push to FDQs */ 871 knav_pool_desc_map(netcp->rx_pool, hwdesc, sizeof(*hwdesc), &dma, 872 &dma_sz); 873 knav_queue_push(netcp->rx_fdq[fdq], dma, sizeof(*hwdesc), 0); 874 return 0; 875 876fail: 877 knav_pool_desc_put(netcp->rx_pool, hwdesc); 878 return -ENOMEM; 879} 880 881/* Refill Rx FDQ with descriptors & attached buffers */ 882static void netcp_rxpool_refill(struct netcp_intf *netcp) 883{ 884 u32 fdq_deficit[KNAV_DMA_FDQ_PER_CHAN] = {0}; 885 int i, ret = 0; 886 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]); 891 892 while (fdq_deficit[i]-- && !ret) 893 ret = netcp_allocate_rx_buf(netcp, i); 894 } /* end for fdqs */ 895} 896 897/* NAPI poll */ 898static int netcp_rx_poll(struct napi_struct *napi, int budget) 899{ 900 struct netcp_intf *netcp = container_of(napi, struct netcp_intf, 901 rx_napi); 902 unsigned int packets; 903 904 packets = netcp_process_rx_packets(netcp, budget); 905 906 netcp_rxpool_refill(netcp); 907 if (packets < budget) { 908 napi_complete(&netcp->rx_napi); 909 knav_queue_enable_notify(netcp->rx_queue); 910 } 911 912 return packets; 913} 914 915static void netcp_rx_notify(void *arg) 916{ 917 struct netcp_intf *netcp = arg; 918 919 knav_queue_disable_notify(netcp->rx_queue); 920 napi_schedule(&netcp->rx_napi); 921} 922 923static void netcp_free_tx_desc_chain(struct netcp_intf *netcp, 924 struct knav_dma_desc *desc, 925 unsigned int desc_sz) 926{ 927 struct knav_dma_desc *ndesc = desc; 928 dma_addr_t dma_desc, dma_buf; 929 unsigned int buf_len; 930 931 while (ndesc) { 932 get_pkt_info(&dma_buf, &buf_len, &dma_desc, ndesc); 933 934 if (dma_buf && buf_len) 935 dma_unmap_single(netcp->dev, dma_buf, buf_len, 936 DMA_TO_DEVICE); 937 else 938 dev_warn(netcp->ndev_dev, "bad Tx desc buf(%p), len(%d)\n", 939 (void *)dma_buf, buf_len); 940 941 knav_pool_desc_put(netcp->tx_pool, ndesc); 942 ndesc = NULL; 943 if (dma_desc) { 944 ndesc = knav_pool_desc_unmap(netcp->tx_pool, dma_desc, 945 desc_sz); 946 if (!ndesc) 947 dev_err(netcp->ndev_dev, "failed to unmap Tx desc\n"); 948 } 949 } 950} 951 952static int netcp_process_tx_compl_packets(struct netcp_intf *netcp, 953 unsigned int budget) 954{ 955 struct knav_dma_desc *desc; 956 struct sk_buff *skb; 957 unsigned int dma_sz; 958 dma_addr_t dma; 959 int pkts = 0; 960 u32 tmp; 961 962 while (budget--) { 963 dma = knav_queue_pop(netcp->tx_compl_q, &dma_sz); 964 if (!dma) 965 break; 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++; 970 continue; 971 } 972 973 get_pad_info((u32 *)&skb, &tmp, desc); 974 netcp_free_tx_desc_chain(netcp, desc, dma_sz); 975 if (!skb) { 976 dev_err(netcp->ndev_dev, "No skb in Tx desc\n"); 977 netcp->ndev->stats.tx_errors++; 978 continue; 979 } 980 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); 986 987 netif_wake_subqueue(netcp->ndev, subqueue); 988 } 989 990 netcp->ndev->stats.tx_packets++; 991 netcp->ndev->stats.tx_bytes += skb->len; 992 dev_kfree_skb(skb); 993 pkts++; 994 } 995 return pkts; 996} 997 998static int netcp_tx_poll(struct napi_struct *napi, int budget) 999{ 1000 int packets; 1001 struct netcp_intf *netcp = container_of(napi, struct netcp_intf, 1002 tx_napi); 1003 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); 1008 } 1009 1010 return packets; 1011} 1012 1013static void netcp_tx_notify(void *arg) 1014{ 1015 struct netcp_intf *netcp = arg; 1016 1017 knav_queue_disable_notify(netcp->tx_compl_q); 1018 napi_schedule(&netcp->tx_napi); 1019} 1020 1021static struct knav_dma_desc* 1022netcp_tx_map_skb(struct sk_buff *skb, struct netcp_intf *netcp) 1023{ 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; 1029 int i; 1030 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"); 1035 return NULL; 1036 } 1037 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); 1042 return NULL; 1043 } 1044 1045 set_pkt_info(dma_addr, pkt_len, 0, desc); 1046 if (skb_is_nonlinear(skb)) { 1047 prefetchw(skb_shinfo(skb)); 1048 } else { 1049 desc->next_desc = 0; 1050 goto upd_pkt_len; 1051 } 1052 1053 pdesc = desc; 1054 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; 1062 u32 pkt_info; 1063 1064 dma_addr = dma_map_page(dev, page, page_offset, buf_len, 1065 DMA_TO_DEVICE); 1066 if (unlikely(!dma_addr)) { 1067 dev_err(netcp->ndev_dev, "Failed to map skb page\n"); 1068 goto free_descs; 1069 } 1070 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); 1075 goto free_descs; 1076 } 1077 1078 desc_dma = knav_pool_desc_virt_to_dma(netcp->tx_pool, 1079 (void *)ndesc); 1080 pkt_info = 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); 1085 pkt_len += buf_len; 1086 if (pdesc != desc) 1087 knav_pool_desc_map(netcp->tx_pool, pdesc, 1088 sizeof(*pdesc), &desc_dma, &dma_sz); 1089 pdesc = ndesc; 1090 } 1091 if (pdesc != desc) 1092 knav_pool_desc_map(netcp->tx_pool, pdesc, sizeof(*pdesc), 1093 &dma_addr, &dma_sz); 1094 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"); 1098 goto free_descs; 1099 } 1100 1101upd_pkt_len: 1102 WARN_ON(pkt_len != skb->len); 1103 1104 pkt_len &= KNAV_DMA_DESC_PKT_LEN_MASK; 1105 set_words(&pkt_len, 1, &desc->desc_info); 1106 return desc; 1107 1108free_descs: 1109 netcp_free_tx_desc_chain(netcp, desc, sizeof(*desc)); 1110 return NULL; 1111} 1112 1113static int netcp_tx_submit_skb(struct netcp_intf *netcp, 1114 struct sk_buff *skb, 1115 struct knav_dma_desc *desc) 1116{ 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; 1121 dma_addr_t dma; 1122 u32 tmp = 0; 1123 int ret = 0; 1124 1125 p_info.netcp = netcp; 1126 p_info.skb = skb; 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)); 1134 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, 1138 &p_info); 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; 1143 goto out; 1144 } 1145 } 1146 1147 /* Make sure some TX hook claimed the packet */ 1148 tx_pipe = p_info.tx_pipe; 1149 if (!tx_pipe) { 1150 dev_err(netcp->ndev_dev, "No TX hook claimed the packet!\n"); 1151 ret = -ENXIO; 1152 goto out; 1153 } 1154 1155 /* update descriptor */ 1156 if (p_info.psdata_len) { 1157 u32 *psdata = p_info.psdata; 1158 1159 memmove(p_info.psdata, p_info.psdata + p_info.psdata_len, 1160 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; 1164 } 1165 1166 tmp |= KNAV_DMA_DESC_HAS_EPIB | 1167 ((netcp->tx_compl_qid & KNAV_DMA_DESC_RETQ_MASK) << 1168 KNAV_DMA_DESC_RETQ_SHIFT); 1169 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); 1173 } 1174 1175 set_words(&tmp, 1, &desc->packet_info); 1176 set_words((u32 *)&skb, 1, &desc->pad[0]); 1177 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); 1181 } 1182 1183 /* submit packet descriptor */ 1184 ret = knav_pool_desc_map(netcp->tx_pool, desc, sizeof(*desc), &dma, 1185 &dma_sz); 1186 if (unlikely(ret)) { 1187 dev_err(netcp->ndev_dev, "%s() failed to map desc\n", __func__); 1188 ret = -ENOMEM; 1189 goto out; 1190 } 1191 skb_tx_timestamp(skb); 1192 knav_queue_push(tx_pipe->dma_queue, dma, dma_sz, 0); 1193 1194out: 1195 return ret; 1196} 1197 1198/* Submit the packet */ 1199static int netcp_ndo_start_xmit(struct sk_buff *skb, struct net_device *ndev) 1200{ 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; 1205 1206 if (unlikely(skb->len <= 0)) { 1207 dev_kfree_skb(skb); 1208 return NETDEV_TX_OK; 1209 } 1210 1211 if (unlikely(skb->len < NETCP_MIN_PACKET_SIZE)) { 1212 ret = skb_padto(skb, NETCP_MIN_PACKET_SIZE); 1213 if (ret < 0) { 1214 /* If we get here, the skb has already been dropped */ 1215 dev_warn(netcp->ndev_dev, "padding failed (%d), packet dropped\n", 1216 ret); 1217 ndev->stats.tx_dropped++; 1218 return ret; 1219 } 1220 skb->len = NETCP_MIN_PACKET_SIZE; 1221 } 1222 1223 desc = netcp_tx_map_skb(skb, netcp); 1224 if (unlikely(!desc)) { 1225 netif_stop_subqueue(ndev, subqueue); 1226 ret = -ENOBUFS; 1227 goto drop; 1228 } 1229 1230 ret = netcp_tx_submit_skb(netcp, skb, desc); 1231 if (ret) 1232 goto drop; 1233 1234 ndev->trans_start = jiffies; 1235 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); 1241 } 1242 return NETDEV_TX_OK; 1243 1244drop: 1245 ndev->stats.tx_dropped++; 1246 if (desc) 1247 netcp_free_tx_desc_chain(netcp, desc, sizeof(*desc)); 1248 dev_kfree_skb(skb); 1249 return ret; 1250} 1251 1252int netcp_txpipe_close(struct netcp_tx_pipe *tx_pipe) 1253{ 1254 if (tx_pipe->dma_channel) { 1255 knav_dma_close_channel(tx_pipe->dma_channel); 1256 tx_pipe->dma_channel = NULL; 1257 } 1258 return 0; 1259} 1260EXPORT_SYMBOL_GPL(netcp_txpipe_close); 1261 1262int netcp_txpipe_open(struct netcp_tx_pipe *tx_pipe) 1263{ 1264 struct device *dev = tx_pipe->netcp_device->device; 1265 struct knav_dma_cfg config; 1266 int ret = 0; 1267 u8 name[16]; 1268 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; 1274 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); 1280 goto err; 1281 } 1282 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, 1285 KNAV_QUEUE_SHARED); 1286 if (IS_ERR(tx_pipe->dma_queue)) { 1287 dev_err(dev, "Could not open DMA queue for channel \"%s\": %d\n", 1288 name, ret); 1289 ret = PTR_ERR(tx_pipe->dma_queue); 1290 goto err; 1291 } 1292 1293 dev_dbg(dev, "opened tx pipe %s\n", name); 1294 return 0; 1295 1296err: 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; 1300 return ret; 1301} 1302EXPORT_SYMBOL_GPL(netcp_txpipe_open); 1303 1304int 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) 1307{ 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; 1312 return 0; 1313} 1314EXPORT_SYMBOL_GPL(netcp_txpipe_init); 1315 1316static struct netcp_addr *netcp_addr_find(struct netcp_intf *netcp, 1317 const u8 *addr, 1318 enum netcp_addr_type type) 1319{ 1320 struct netcp_addr *naddr; 1321 1322 list_for_each_entry(naddr, &netcp->addr_list, node) { 1323 if (naddr->type != type) 1324 continue; 1325 if (addr && memcmp(addr, naddr->addr, ETH_ALEN)) 1326 continue; 1327 return naddr; 1328 } 1329 1330 return NULL; 1331} 1332 1333static struct netcp_addr *netcp_addr_add(struct netcp_intf *netcp, 1334 const u8 *addr, 1335 enum netcp_addr_type type) 1336{ 1337 struct netcp_addr *naddr; 1338 1339 naddr = devm_kmalloc(netcp->dev, sizeof(*naddr), GFP_ATOMIC); 1340 if (!naddr) 1341 return NULL; 1342 1343 naddr->type = type; 1344 naddr->flags = 0; 1345 naddr->netcp = netcp; 1346 if (addr) 1347 ether_addr_copy(naddr->addr, addr); 1348 else 1349 eth_zero_addr(naddr->addr); 1350 list_add_tail(&naddr->node, &netcp->addr_list); 1351 1352 return naddr; 1353} 1354 1355static void netcp_addr_del(struct netcp_intf *netcp, struct netcp_addr *naddr) 1356{ 1357 list_del(&naddr->node); 1358 devm_kfree(netcp->dev, naddr); 1359} 1360 1361static void netcp_addr_clear_mark(struct netcp_intf *netcp) 1362{ 1363 struct netcp_addr *naddr; 1364 1365 list_for_each_entry(naddr, &netcp->addr_list, node) 1366 naddr->flags = 0; 1367} 1368 1369static void netcp_addr_add_mark(struct netcp_intf *netcp, const u8 *addr, 1370 enum netcp_addr_type type) 1371{ 1372 struct netcp_addr *naddr; 1373 1374 naddr = netcp_addr_find(netcp, addr, type); 1375 if (naddr) { 1376 naddr->flags |= ADDR_VALID; 1377 return; 1378 } 1379 1380 naddr = netcp_addr_add(netcp, addr, type); 1381 if (!WARN_ON(!naddr)) 1382 naddr->flags |= ADDR_NEW; 1383} 1384 1385static void netcp_addr_sweep_del(struct netcp_intf *netcp) 1386{ 1387 struct netcp_addr *naddr, *tmp; 1388 struct netcp_intf_modpriv *priv; 1389 struct netcp_module *module; 1390 int error; 1391 1392 list_for_each_entry_safe(naddr, tmp, &netcp->addr_list, node) { 1393 if (naddr->flags & (ADDR_VALID | ADDR_NEW)) 1394 continue; 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) 1400 continue; 1401 error = module->del_addr(priv->module_priv, 1402 naddr); 1403 WARN_ON(error); 1404 } 1405 netcp_addr_del(netcp, naddr); 1406 } 1407} 1408 1409static void netcp_addr_sweep_add(struct netcp_intf *netcp) 1410{ 1411 struct netcp_addr *naddr, *tmp; 1412 struct netcp_intf_modpriv *priv; 1413 struct netcp_module *module; 1414 int error; 1415 1416 list_for_each_entry_safe(naddr, tmp, &netcp->addr_list, node) { 1417 if (!(naddr->flags & ADDR_NEW)) 1418 continue; 1419 dev_dbg(netcp->ndev_dev, "adding address %pM, type %x\n", 1420 naddr->addr, naddr->type); 1421 1422 for_each_module(netcp, priv) { 1423 module = priv->netcp_module; 1424 if (!module->add_addr) 1425 continue; 1426 error = module->add_addr(priv->module_priv, naddr); 1427 WARN_ON(error); 1428 } 1429 } 1430} 1431 1432static void netcp_set_rx_mode(struct net_device *ndev) 1433{ 1434 struct netcp_intf *netcp = netdev_priv(ndev); 1435 struct netdev_hw_addr *ndev_addr; 1436 bool promisc; 1437 1438 promisc = (ndev->flags & IFF_PROMISC || 1439 ndev->flags & IFF_ALLMULTI || 1440 netdev_mc_count(ndev) > NETCP_MAX_MCAST_ADDR); 1441 1442 spin_lock(&netcp->lock); 1443 /* first clear all marks */ 1444 netcp_addr_clear_mark(netcp); 1445 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); 1454 1455 if (promisc) 1456 netcp_addr_add_mark(netcp, NULL, ADDR_ANY); 1457 1458 /* finally sweep and callout into modules */ 1459 netcp_addr_sweep_del(netcp); 1460 netcp_addr_sweep_add(netcp); 1461 spin_unlock(&netcp->lock); 1462} 1463 1464static void netcp_free_navigator_resources(struct netcp_intf *netcp) 1465{ 1466 int i; 1467 1468 if (netcp->rx_channel) { 1469 knav_dma_close_channel(netcp->rx_channel); 1470 netcp->rx_channel = NULL; 1471 } 1472 1473 if (!IS_ERR_OR_NULL(netcp->rx_pool)) 1474 netcp_rxpool_free(netcp); 1475 1476 if (!IS_ERR_OR_NULL(netcp->rx_queue)) { 1477 knav_queue_close(netcp->rx_queue); 1478 netcp->rx_queue = NULL; 1479 } 1480 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; 1485 } 1486 1487 if (!IS_ERR_OR_NULL(netcp->tx_compl_q)) { 1488 knav_queue_close(netcp->tx_compl_q); 1489 netcp->tx_compl_q = NULL; 1490 } 1491 1492 if (!IS_ERR_OR_NULL(netcp->tx_pool)) { 1493 knav_pool_destroy(netcp->tx_pool); 1494 netcp->tx_pool = NULL; 1495 } 1496} 1497 1498static int netcp_setup_navigator_resources(struct net_device *ndev) 1499{ 1500 struct netcp_intf *netcp = netdev_priv(ndev); 1501 struct knav_queue_notify_config notify_cfg; 1502 struct knav_dma_cfg config; 1503 u32 last_fdq = 0; 1504 u8 name[16]; 1505 int ret; 1506 int i; 1507 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); 1515 goto fail; 1516 } 1517 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); 1524 goto fail; 1525 } 1526 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); 1532 goto fail; 1533 } 1534 netcp->tx_compl_qid = knav_queue_get_id(netcp->tx_compl_q); 1535 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); 1542 if (ret) 1543 goto fail; 1544 1545 knav_queue_disable_notify(netcp->tx_compl_q); 1546 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); 1552 goto fail; 1553 } 1554 netcp->rx_queue_id = knav_queue_get_id(netcp->rx_queue); 1555 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); 1562 if (ret) 1563 goto fail; 1564 1565 knav_queue_disable_notify(netcp->rx_queue); 1566 1567 /* open Rx FDQs */ 1568 for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN && netcp->rx_queue_depths[i]; 1569 ++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]); 1574 goto fail; 1575 } 1576 } 1577 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; 1588 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; 1593 } 1594 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); 1600 goto fail; 1601 } 1602 1603 dev_dbg(netcp->ndev_dev, "opened RX channel: %p\n", netcp->rx_channel); 1604 return 0; 1605 1606fail: 1607 netcp_free_navigator_resources(netcp); 1608 return ret; 1609} 1610 1611/* Open the device */ 1612static int netcp_ndo_open(struct net_device *ndev) 1613{ 1614 struct netcp_intf *netcp = netdev_priv(ndev); 1615 struct netcp_intf_modpriv *intf_modpriv; 1616 struct netcp_module *module; 1617 int ret; 1618 1619 netif_carrier_off(ndev); 1620 ret = netcp_setup_navigator_resources(ndev); 1621 if (ret) { 1622 dev_err(netcp->ndev_dev, "Failed to setup navigator resources\n"); 1623 goto fail; 1624 } 1625 1626 for_each_module(netcp, intf_modpriv) { 1627 module = intf_modpriv->netcp_module; 1628 if (module->open) { 1629 ret = module->open(intf_modpriv->module_priv, ndev); 1630 if (ret != 0) { 1631 dev_err(netcp->ndev_dev, "module open failed\n"); 1632 goto fail_open; 1633 } 1634 } 1635 } 1636 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); 1644 return 0; 1645 1646fail_open: 1647 for_each_module(netcp, intf_modpriv) { 1648 module = intf_modpriv->netcp_module; 1649 if (module->close) 1650 module->close(intf_modpriv->module_priv, ndev); 1651 } 1652 1653fail: 1654 netcp_free_navigator_resources(netcp); 1655 return ret; 1656} 1657 1658/* Close the device */ 1659static int netcp_ndo_stop(struct net_device *ndev) 1660{ 1661 struct netcp_intf *netcp = netdev_priv(ndev); 1662 struct netcp_intf_modpriv *intf_modpriv; 1663 struct netcp_module *module; 1664 int err = 0; 1665 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); 1674 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); 1679 if (err != 0) 1680 dev_err(netcp->ndev_dev, "Close failed\n"); 1681 } 1682 } 1683 1684 /* Recycle Rx descriptors from completion queue */ 1685 netcp_empty_rx_queue(netcp); 1686 1687 /* Recycle Tx descriptors from completion queue */ 1688 netcp_process_tx_compl_packets(netcp, netcp->tx_pool_size); 1689 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)); 1693 1694 netcp_free_navigator_resources(netcp); 1695 dev_dbg(netcp->ndev_dev, "netcp device %s stopped\n", ndev->name); 1696 return 0; 1697} 1698 1699static int netcp_ndo_ioctl(struct net_device *ndev, 1700 struct ifreq *req, int cmd) 1701{ 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; 1706 1707 if (!netif_running(ndev)) 1708 return -EINVAL; 1709 1710 for_each_module(netcp, intf_modpriv) { 1711 module = intf_modpriv->netcp_module; 1712 if (!module->ioctl) 1713 continue; 1714 1715 err = module->ioctl(intf_modpriv->module_priv, req, cmd); 1716 if ((err < 0) && (err != -EOPNOTSUPP)) { 1717 ret = err; 1718 goto out; 1719 } 1720 if (err == 0) 1721 ret = err; 1722 } 1723 1724out: 1725 return (ret == 0) ? 0 : err; 1726} 1727 1728static int netcp_ndo_change_mtu(struct net_device *ndev, int new_mtu) 1729{ 1730 struct netcp_intf *netcp = netdev_priv(ndev); 1731 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); 1736 return -EINVAL; 1737 } 1738 1739 ndev->mtu = new_mtu; 1740 return 0; 1741} 1742 1743static void netcp_ndo_tx_timeout(struct net_device *ndev) 1744{ 1745 struct netcp_intf *netcp = netdev_priv(ndev); 1746 unsigned int descs = knav_pool_count(netcp->tx_pool); 1747 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); 1752} 1753 1754static int netcp_rx_add_vid(struct net_device *ndev, __be16 proto, u16 vid) 1755{ 1756 struct netcp_intf *netcp = netdev_priv(ndev); 1757 struct netcp_intf_modpriv *intf_modpriv; 1758 struct netcp_module *module; 1759 unsigned long flags; 1760 int err = 0; 1761 1762 dev_dbg(netcp->ndev_dev, "adding rx vlan id: %d\n", vid); 1763 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); 1769 if (err != 0) { 1770 dev_err(netcp->ndev_dev, "Could not add vlan id = %d\n", 1771 vid); 1772 break; 1773 } 1774 } 1775 } 1776 spin_unlock_irqrestore(&netcp->lock, flags); 1777 1778 return err; 1779} 1780 1781static int netcp_rx_kill_vid(struct net_device *ndev, __be16 proto, u16 vid) 1782{ 1783 struct netcp_intf *netcp = netdev_priv(ndev); 1784 struct netcp_intf_modpriv *intf_modpriv; 1785 struct netcp_module *module; 1786 unsigned long flags; 1787 int err = 0; 1788 1789 dev_dbg(netcp->ndev_dev, "removing rx vlan id: %d\n", vid); 1790 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); 1796 if (err != 0) { 1797 dev_err(netcp->ndev_dev, "Could not delete vlan id = %d\n", 1798 vid); 1799 break; 1800 } 1801 } 1802 } 1803 spin_unlock_irqrestore(&netcp->lock, flags); 1804 return err; 1805} 1806 1807static u16 netcp_select_queue(struct net_device *dev, struct sk_buff *skb, 1808 void *accel_priv, 1809 select_queue_fallback_t fallback) 1810{ 1811 return 0; 1812} 1813 1814static int netcp_setup_tc(struct net_device *dev, u8 num_tc) 1815{ 1816 int i; 1817 1818 /* setup tc must be called under rtnl lock */ 1819 ASSERT_RTNL(); 1820 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)) 1824 return -EINVAL; 1825 1826 /* Configure traffic class to queue mappings */ 1827 if (num_tc) { 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); 1831 } else { 1832 netdev_reset_tc(dev); 1833 } 1834 1835 return 0; 1836} 1837 1838static 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, 1852}; 1853 1854static int netcp_create_interface(struct netcp_device *netcp_device, 1855 struct device_node *node_interface) 1856{ 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; 1864 u32 efuse_mac = 0; 1865 const void *mac_addr; 1866 u8 efuse_mac_addr[6]; 1867 u32 temp[2]; 1868 int ret = 0; 1869 1870 ndev = alloc_etherdev_mqs(sizeof(*netcp), 1, 1); 1871 if (!ndev) { 1872 dev_err(dev, "Error allocating netdev\n"); 1873 return -ENOMEM; 1874 } 1875 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; 1880 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; 1889 netcp->ndev = ndev; 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; 1895 1896 ret = of_property_read_u32(node_interface, "efuse-mac", &efuse_mac); 1897 if (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"); 1900 ret = -ENODEV; 1901 goto quit; 1902 } 1903 size = resource_size(&res); 1904 1905 if (!devm_request_mem_region(dev, res.start, size, 1906 dev_name(dev))) { 1907 dev_err(dev, "could not reserve resource\n"); 1908 ret = -ENOMEM; 1909 goto quit; 1910 } 1911 1912 efuse = devm_ioremap_nocache(dev, res.start, size); 1913 if (!efuse) { 1914 dev_err(dev, "could not map resource\n"); 1915 devm_release_mem_region(dev, res.start, size); 1916 ret = -ENOMEM; 1917 goto quit; 1918 } 1919 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); 1923 else 1924 random_ether_addr(ndev->dev_addr); 1925 1926 devm_iounmap(dev, efuse); 1927 devm_release_mem_region(dev, res.start, size); 1928 } else { 1929 mac_addr = of_get_mac_address(node_interface); 1930 if (mac_addr) 1931 ether_addr_copy(ndev->dev_addr, mac_addr); 1932 else 1933 random_ether_addr(ndev->dev_addr); 1934 } 1935 1936 ret = of_property_read_string(node_interface, "rx-channel", 1937 &netcp->dma_chan_name); 1938 if (ret < 0) { 1939 dev_err(dev, "missing \"rx-channel\" parameter\n"); 1940 ret = -ENODEV; 1941 goto quit; 1942 } 1943 1944 ret = of_property_read_u32(node_interface, "rx-queue", 1945 &netcp->rx_queue_id); 1946 if (ret < 0) { 1947 dev_warn(dev, "missing \"rx-queue\" parameter\n"); 1948 netcp->rx_queue_id = KNAV_QUEUE_QPEND; 1949 } 1950 1951 ret = of_property_read_u32_array(node_interface, "rx-queue-depth", 1952 netcp->rx_queue_depths, 1953 KNAV_DMA_FDQ_PER_CHAN); 1954 if (ret < 0) { 1955 dev_err(dev, "missing \"rx-queue-depth\" parameter\n"); 1956 netcp->rx_queue_depths[0] = 128; 1957 } 1958 1959 ret = of_property_read_u32_array(node_interface, "rx-pool", temp, 2); 1960 if (ret < 0) { 1961 dev_err(dev, "missing \"rx-pool\" parameter\n"); 1962 ret = -ENODEV; 1963 goto quit; 1964 } 1965 netcp->rx_pool_size = temp[0]; 1966 netcp->rx_pool_region_id = temp[1]; 1967 1968 ret = of_property_read_u32_array(node_interface, "tx-pool", temp, 2); 1969 if (ret < 0) { 1970 dev_err(dev, "missing \"tx-pool\" parameter\n"); 1971 ret = -ENODEV; 1972 goto quit; 1973 } 1974 netcp->tx_pool_size = temp[0]; 1975 netcp->tx_pool_region_id = temp[1]; 1976 1977 if (netcp->tx_pool_size < MAX_SKB_FRAGS) { 1978 dev_err(dev, "tx-pool size too small, must be atleast(%ld)\n", 1979 MAX_SKB_FRAGS); 1980 ret = -ENODEV; 1981 goto quit; 1982 } 1983 1984 ret = of_property_read_u32(node_interface, "tx-completion-queue", 1985 &netcp->tx_compl_qid); 1986 if (ret < 0) { 1987 dev_warn(dev, "missing \"tx-completion-queue\" parameter\n"); 1988 netcp->tx_compl_qid = KNAV_QUEUE_QPEND; 1989 } 1990 1991 /* NAPI register */ 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); 1994 1995 /* Register the network device */ 1996 ndev->dev_id = 0; 1997 ndev->watchdog_timeo = NETCP_TX_TIMEOUT; 1998 ndev->netdev_ops = &netcp_netdev_ops; 1999 SET_NETDEV_DEV(ndev, dev); 2000 2001 list_add_tail(&netcp->interface_list, &netcp_device->interface_head); 2002 return 0; 2003 2004quit: 2005 free_netdev(ndev); 2006 return ret; 2007} 2008 2009static void netcp_delete_interface(struct netcp_device *netcp_device, 2010 struct net_device *ndev) 2011{ 2012 struct netcp_intf_modpriv *intf_modpriv, *tmp; 2013 struct netcp_intf *netcp = netdev_priv(ndev); 2014 struct netcp_module *module; 2015 2016 dev_dbg(netcp_device->device, "Removing interface \"%s\"\n", 2017 ndev->name); 2018 2019 /* Notify each of the modules that the interface is going away */ 2020 list_for_each_entry_safe(intf_modpriv, tmp, &netcp->module_head, 2021 intf_list) { 2022 module = intf_modpriv->netcp_module; 2023 dev_dbg(netcp_device->device, "Releasing module \"%s\"\n", 2024 module->name); 2025 if (module->release) 2026 module->release(intf_modpriv->module_priv); 2027 list_del(&intf_modpriv->intf_list); 2028 kfree(intf_modpriv); 2029 } 2030 WARN(!list_empty(&netcp->module_head), "%s interface module list is not empty!\n", 2031 ndev->name); 2032 2033 list_del(&netcp->interface_list); 2034 2035 of_node_put(netcp->node_interface); 2036 unregister_netdev(ndev); 2037 netif_napi_del(&netcp->rx_napi); 2038 free_netdev(ndev); 2039} 2040 2041static int netcp_probe(struct platform_device *pdev) 2042{ 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; 2048 int ret; 2049 2050 if (!node) { 2051 dev_err(dev, "could not find device info\n"); 2052 return -ENODEV; 2053 } 2054 2055 /* Allocate a new NETCP device instance */ 2056 netcp_device = devm_kzalloc(dev, sizeof(*netcp_device), GFP_KERNEL); 2057 if (!netcp_device) 2058 return -ENOMEM; 2059 2060 pm_runtime_enable(&pdev->dev); 2061 ret = pm_runtime_get_sync(&pdev->dev); 2062 if (ret < 0) { 2063 dev_err(dev, "Failed to enable NETCP power-domain\n"); 2064 pm_runtime_disable(&pdev->dev); 2065 return ret; 2066 } 2067 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); 2073 2074 /* create interfaces */ 2075 interfaces = of_get_child_by_name(node, "netcp-interfaces"); 2076 if (!interfaces) { 2077 dev_err(dev, "could not find netcp-interfaces node\n"); 2078 ret = -ENODEV; 2079 goto probe_quit; 2080 } 2081 2082 for_each_available_child_of_node(interfaces, child) { 2083 ret = netcp_create_interface(netcp_device, child); 2084 if (ret) { 2085 dev_err(dev, "could not create interface(%s)\n", 2086 child->name); 2087 goto probe_quit_interface; 2088 } 2089 } 2090 2091 /* Add the device instance to the list */ 2092 list_add_tail(&netcp_device->device_list, &netcp_devices); 2093 2094 return 0; 2095 2096probe_quit_interface: 2097 list_for_each_entry_safe(netcp_intf, netcp_tmp, 2098 &netcp_device->interface_head, 2099 interface_list) { 2100 netcp_delete_interface(netcp_device, netcp_intf->ndev); 2101 } 2102 2103probe_quit: 2104 pm_runtime_put_sync(&pdev->dev); 2105 pm_runtime_disable(&pdev->dev); 2106 platform_set_drvdata(pdev, NULL); 2107 return ret; 2108} 2109 2110static int netcp_remove(struct platform_device *pdev) 2111{ 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; 2116 2117 list_for_each_entry_safe(inst_modpriv, tmp, &netcp_device->modpriv_head, 2118 inst_list) { 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); 2124 } 2125 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, 2129 interface_list) { 2130 netcp_delete_interface(netcp_device, netcp_intf->ndev); 2131 } 2132 2133 WARN(!list_empty(&netcp_device->interface_head), 2134 "%s interface list not empty!\n", pdev->name); 2135 2136 pm_runtime_put_sync(&pdev->dev); 2137 pm_runtime_disable(&pdev->dev); 2138 platform_set_drvdata(pdev, NULL); 2139 return 0; 2140} 2141 2142static const struct of_device_id of_match[] = { 2143 { .compatible = "ti,netcp-1.0", }, 2144 {}, 2145}; 2146MODULE_DEVICE_TABLE(of, of_match); 2147 2148static struct platform_driver netcp_driver = { 2149 .driver = { 2150 .name = "netcp-1.0", 2151 .of_match_table = of_match, 2152 }, 2153 .probe = netcp_probe, 2154 .remove = netcp_remove, 2155}; 2156module_platform_driver(netcp_driver); 2157 2158MODULE_LICENSE("GPL v2"); 2159MODULE_DESCRIPTION("TI NETCP driver for Keystone SOCs"); 2160MODULE_AUTHOR("Sandeep Nair <sandeep_n@ti.com"); 2161