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