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 */ 79 struct 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 86 struct 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 93 struct 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 100 static LIST_HEAD(netcp_devices); 101 static LIST_HEAD(netcp_modules); 102 static DEFINE_MUTEX(netcp_modules_lock); 103 104 static int netcp_debug_level = -1; 105 module_param(netcp_debug_level, int, 0); 106 MODULE_PARM_DESC(netcp_debug_level, "Netcp debug level (NETIF_MSG bits) (0=none,...,16=all)"); 107 108 /* Helper functions - Get/Set */ 109 static 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 117 static 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 123 static 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 130 static 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 138 static 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 146 static 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 153 static 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 159 static 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 166 static 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 */ 175 static 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 192 static 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 */ 204 static 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 214 static 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 317 int 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 352 fail: 353 mutex_unlock(&netcp_modules_lock); 354 netcp_unregister_module(module); 355 return ret; 356 } 357 EXPORT_SYMBOL_GPL(netcp_register_module); 358 359 static 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 397 void 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 } 418 EXPORT_SYMBOL_GPL(netcp_unregister_module); 419 420 void *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 } 430 EXPORT_SYMBOL_GPL(netcp_module_get_intf_data); 431 432 /* Module TX and RX Hook management */ 433 struct netcp_hook_list { 434 struct list_head list; 435 netcp_hook_rtn *hook_rtn; 436 void *hook_data; 437 int order; 438 }; 439 440 int 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 } 465 EXPORT_SYMBOL_GPL(netcp_register_txhook); 466 467 int 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 } 487 EXPORT_SYMBOL_GPL(netcp_unregister_txhook); 488 489 int 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 515 int 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 537 static 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 545 static 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 575 static 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 598 static 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 710 free_desc: 711 netcp_free_rx_desc_chain(netcp, desc); 712 netcp->ndev->stats.rx_errors++; 713 return 0; 714 } 715 716 static 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 */ 727 static 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 772 static 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 788 static 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 858 fail: 859 knav_pool_desc_put(netcp->rx_pool, hwdesc); 860 } 861 862 /* Refill Rx FDQ with descriptors & attached buffers */ 863 static 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 */ 879 static 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 896 static 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 904 static 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 933 static 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 979 static 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 994 static 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 1002 static struct knav_dma_desc* 1003 netcp_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 1082 upd_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 1089 free_descs: 1090 netcp_free_tx_desc_chain(netcp, desc, sizeof(*desc)); 1091 return NULL; 1092 } 1093 1094 static 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 u32 packet_info = 0; 1102 unsigned int dma_sz; 1103 dma_addr_t dma; 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 packet_info |= 1144 (p_info.psdata_len & KNAV_DMA_DESC_PSLEN_MASK) << 1145 KNAV_DMA_DESC_PSLEN_SHIFT; 1146 } 1147 1148 packet_info |= KNAV_DMA_DESC_HAS_EPIB | 1149 ((netcp->tx_compl_qid & KNAV_DMA_DESC_RETQ_MASK) << 1150 KNAV_DMA_DESC_RETQ_SHIFT) | 1151 ((tx_pipe->dma_psflags & KNAV_DMA_DESC_PSFLAG_MASK) << 1152 KNAV_DMA_DESC_PSFLAG_SHIFT); 1153 1154 set_words(&packet_info, 1, &desc->packet_info); 1155 set_words((u32 *)&skb, 1, &desc->pad[0]); 1156 1157 /* submit packet descriptor */ 1158 ret = knav_pool_desc_map(netcp->tx_pool, desc, sizeof(*desc), &dma, 1159 &dma_sz); 1160 if (unlikely(ret)) { 1161 dev_err(netcp->ndev_dev, "%s() failed to map desc\n", __func__); 1162 ret = -ENOMEM; 1163 goto out; 1164 } 1165 skb_tx_timestamp(skb); 1166 knav_queue_push(tx_pipe->dma_queue, dma, dma_sz, 0); 1167 1168 out: 1169 return ret; 1170 } 1171 1172 /* Submit the packet */ 1173 static int netcp_ndo_start_xmit(struct sk_buff *skb, struct net_device *ndev) 1174 { 1175 struct netcp_intf *netcp = netdev_priv(ndev); 1176 int subqueue = skb_get_queue_mapping(skb); 1177 struct knav_dma_desc *desc; 1178 int desc_count, ret = 0; 1179 1180 if (unlikely(skb->len <= 0)) { 1181 dev_kfree_skb(skb); 1182 return NETDEV_TX_OK; 1183 } 1184 1185 if (unlikely(skb->len < NETCP_MIN_PACKET_SIZE)) { 1186 ret = skb_padto(skb, NETCP_MIN_PACKET_SIZE); 1187 if (ret < 0) { 1188 /* If we get here, the skb has already been dropped */ 1189 dev_warn(netcp->ndev_dev, "padding failed (%d), packet dropped\n", 1190 ret); 1191 ndev->stats.tx_dropped++; 1192 return ret; 1193 } 1194 skb->len = NETCP_MIN_PACKET_SIZE; 1195 } 1196 1197 desc = netcp_tx_map_skb(skb, netcp); 1198 if (unlikely(!desc)) { 1199 netif_stop_subqueue(ndev, subqueue); 1200 ret = -ENOBUFS; 1201 goto drop; 1202 } 1203 1204 ret = netcp_tx_submit_skb(netcp, skb, desc); 1205 if (ret) 1206 goto drop; 1207 1208 ndev->trans_start = jiffies; 1209 1210 /* Check Tx pool count & stop subqueue if needed */ 1211 desc_count = knav_pool_count(netcp->tx_pool); 1212 if (desc_count < netcp->tx_pause_threshold) { 1213 dev_dbg(netcp->ndev_dev, "pausing tx, count(%d)\n", desc_count); 1214 netif_stop_subqueue(ndev, subqueue); 1215 } 1216 return NETDEV_TX_OK; 1217 1218 drop: 1219 ndev->stats.tx_dropped++; 1220 if (desc) 1221 netcp_free_tx_desc_chain(netcp, desc, sizeof(*desc)); 1222 dev_kfree_skb(skb); 1223 return ret; 1224 } 1225 1226 int netcp_txpipe_close(struct netcp_tx_pipe *tx_pipe) 1227 { 1228 if (tx_pipe->dma_channel) { 1229 knav_dma_close_channel(tx_pipe->dma_channel); 1230 tx_pipe->dma_channel = NULL; 1231 } 1232 return 0; 1233 } 1234 EXPORT_SYMBOL_GPL(netcp_txpipe_close); 1235 1236 int netcp_txpipe_open(struct netcp_tx_pipe *tx_pipe) 1237 { 1238 struct device *dev = tx_pipe->netcp_device->device; 1239 struct knav_dma_cfg config; 1240 int ret = 0; 1241 u8 name[16]; 1242 1243 memset(&config, 0, sizeof(config)); 1244 config.direction = DMA_MEM_TO_DEV; 1245 config.u.tx.filt_einfo = false; 1246 config.u.tx.filt_pswords = false; 1247 config.u.tx.priority = DMA_PRIO_MED_L; 1248 1249 tx_pipe->dma_channel = knav_dma_open_channel(dev, 1250 tx_pipe->dma_chan_name, &config); 1251 if (IS_ERR_OR_NULL(tx_pipe->dma_channel)) { 1252 dev_err(dev, "failed opening tx chan(%s)\n", 1253 tx_pipe->dma_chan_name); 1254 goto err; 1255 } 1256 1257 snprintf(name, sizeof(name), "tx-pipe-%s", dev_name(dev)); 1258 tx_pipe->dma_queue = knav_queue_open(name, tx_pipe->dma_queue_id, 1259 KNAV_QUEUE_SHARED); 1260 if (IS_ERR(tx_pipe->dma_queue)) { 1261 dev_err(dev, "Could not open DMA queue for channel \"%s\": %d\n", 1262 name, ret); 1263 ret = PTR_ERR(tx_pipe->dma_queue); 1264 goto err; 1265 } 1266 1267 dev_dbg(dev, "opened tx pipe %s\n", name); 1268 return 0; 1269 1270 err: 1271 if (!IS_ERR_OR_NULL(tx_pipe->dma_channel)) 1272 knav_dma_close_channel(tx_pipe->dma_channel); 1273 tx_pipe->dma_channel = NULL; 1274 return ret; 1275 } 1276 EXPORT_SYMBOL_GPL(netcp_txpipe_open); 1277 1278 int netcp_txpipe_init(struct netcp_tx_pipe *tx_pipe, 1279 struct netcp_device *netcp_device, 1280 const char *dma_chan_name, unsigned int dma_queue_id) 1281 { 1282 memset(tx_pipe, 0, sizeof(*tx_pipe)); 1283 tx_pipe->netcp_device = netcp_device; 1284 tx_pipe->dma_chan_name = dma_chan_name; 1285 tx_pipe->dma_queue_id = dma_queue_id; 1286 return 0; 1287 } 1288 EXPORT_SYMBOL_GPL(netcp_txpipe_init); 1289 1290 static struct netcp_addr *netcp_addr_find(struct netcp_intf *netcp, 1291 const u8 *addr, 1292 enum netcp_addr_type type) 1293 { 1294 struct netcp_addr *naddr; 1295 1296 list_for_each_entry(naddr, &netcp->addr_list, node) { 1297 if (naddr->type != type) 1298 continue; 1299 if (addr && memcmp(addr, naddr->addr, ETH_ALEN)) 1300 continue; 1301 return naddr; 1302 } 1303 1304 return NULL; 1305 } 1306 1307 static struct netcp_addr *netcp_addr_add(struct netcp_intf *netcp, 1308 const u8 *addr, 1309 enum netcp_addr_type type) 1310 { 1311 struct netcp_addr *naddr; 1312 1313 naddr = devm_kmalloc(netcp->dev, sizeof(*naddr), GFP_ATOMIC); 1314 if (!naddr) 1315 return NULL; 1316 1317 naddr->type = type; 1318 naddr->flags = 0; 1319 naddr->netcp = netcp; 1320 if (addr) 1321 ether_addr_copy(naddr->addr, addr); 1322 else 1323 memset(naddr->addr, 0, ETH_ALEN); 1324 list_add_tail(&naddr->node, &netcp->addr_list); 1325 1326 return naddr; 1327 } 1328 1329 static void netcp_addr_del(struct netcp_intf *netcp, struct netcp_addr *naddr) 1330 { 1331 list_del(&naddr->node); 1332 devm_kfree(netcp->dev, naddr); 1333 } 1334 1335 static void netcp_addr_clear_mark(struct netcp_intf *netcp) 1336 { 1337 struct netcp_addr *naddr; 1338 1339 list_for_each_entry(naddr, &netcp->addr_list, node) 1340 naddr->flags = 0; 1341 } 1342 1343 static void netcp_addr_add_mark(struct netcp_intf *netcp, const u8 *addr, 1344 enum netcp_addr_type type) 1345 { 1346 struct netcp_addr *naddr; 1347 1348 naddr = netcp_addr_find(netcp, addr, type); 1349 if (naddr) { 1350 naddr->flags |= ADDR_VALID; 1351 return; 1352 } 1353 1354 naddr = netcp_addr_add(netcp, addr, type); 1355 if (!WARN_ON(!naddr)) 1356 naddr->flags |= ADDR_NEW; 1357 } 1358 1359 static void netcp_addr_sweep_del(struct netcp_intf *netcp) 1360 { 1361 struct netcp_addr *naddr, *tmp; 1362 struct netcp_intf_modpriv *priv; 1363 struct netcp_module *module; 1364 int error; 1365 1366 list_for_each_entry_safe(naddr, tmp, &netcp->addr_list, node) { 1367 if (naddr->flags & (ADDR_VALID | ADDR_NEW)) 1368 continue; 1369 dev_dbg(netcp->ndev_dev, "deleting address %pM, type %x\n", 1370 naddr->addr, naddr->type); 1371 mutex_lock(&netcp_modules_lock); 1372 for_each_module(netcp, priv) { 1373 module = priv->netcp_module; 1374 if (!module->del_addr) 1375 continue; 1376 error = module->del_addr(priv->module_priv, 1377 naddr); 1378 WARN_ON(error); 1379 } 1380 mutex_unlock(&netcp_modules_lock); 1381 netcp_addr_del(netcp, naddr); 1382 } 1383 } 1384 1385 static void netcp_addr_sweep_add(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_NEW)) 1394 continue; 1395 dev_dbg(netcp->ndev_dev, "adding address %pM, type %x\n", 1396 naddr->addr, naddr->type); 1397 mutex_lock(&netcp_modules_lock); 1398 for_each_module(netcp, priv) { 1399 module = priv->netcp_module; 1400 if (!module->add_addr) 1401 continue; 1402 error = module->add_addr(priv->module_priv, naddr); 1403 WARN_ON(error); 1404 } 1405 mutex_unlock(&netcp_modules_lock); 1406 } 1407 } 1408 1409 static void netcp_set_rx_mode(struct net_device *ndev) 1410 { 1411 struct netcp_intf *netcp = netdev_priv(ndev); 1412 struct netdev_hw_addr *ndev_addr; 1413 bool promisc; 1414 1415 promisc = (ndev->flags & IFF_PROMISC || 1416 ndev->flags & IFF_ALLMULTI || 1417 netdev_mc_count(ndev) > NETCP_MAX_MCAST_ADDR); 1418 1419 /* first clear all marks */ 1420 netcp_addr_clear_mark(netcp); 1421 1422 /* next add new entries, mark existing ones */ 1423 netcp_addr_add_mark(netcp, ndev->broadcast, ADDR_BCAST); 1424 for_each_dev_addr(ndev, ndev_addr) 1425 netcp_addr_add_mark(netcp, ndev_addr->addr, ADDR_DEV); 1426 netdev_for_each_uc_addr(ndev_addr, ndev) 1427 netcp_addr_add_mark(netcp, ndev_addr->addr, ADDR_UCAST); 1428 netdev_for_each_mc_addr(ndev_addr, ndev) 1429 netcp_addr_add_mark(netcp, ndev_addr->addr, ADDR_MCAST); 1430 1431 if (promisc) 1432 netcp_addr_add_mark(netcp, NULL, ADDR_ANY); 1433 1434 /* finally sweep and callout into modules */ 1435 netcp_addr_sweep_del(netcp); 1436 netcp_addr_sweep_add(netcp); 1437 } 1438 1439 static void netcp_free_navigator_resources(struct netcp_intf *netcp) 1440 { 1441 int i; 1442 1443 if (netcp->rx_channel) { 1444 knav_dma_close_channel(netcp->rx_channel); 1445 netcp->rx_channel = NULL; 1446 } 1447 1448 if (!IS_ERR_OR_NULL(netcp->rx_pool)) 1449 netcp_rxpool_free(netcp); 1450 1451 if (!IS_ERR_OR_NULL(netcp->rx_queue)) { 1452 knav_queue_close(netcp->rx_queue); 1453 netcp->rx_queue = NULL; 1454 } 1455 1456 for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN && 1457 !IS_ERR_OR_NULL(netcp->rx_fdq[i]) ; ++i) { 1458 knav_queue_close(netcp->rx_fdq[i]); 1459 netcp->rx_fdq[i] = NULL; 1460 } 1461 1462 if (!IS_ERR_OR_NULL(netcp->tx_compl_q)) { 1463 knav_queue_close(netcp->tx_compl_q); 1464 netcp->tx_compl_q = NULL; 1465 } 1466 1467 if (!IS_ERR_OR_NULL(netcp->tx_pool)) { 1468 knav_pool_destroy(netcp->tx_pool); 1469 netcp->tx_pool = NULL; 1470 } 1471 } 1472 1473 static int netcp_setup_navigator_resources(struct net_device *ndev) 1474 { 1475 struct netcp_intf *netcp = netdev_priv(ndev); 1476 struct knav_queue_notify_config notify_cfg; 1477 struct knav_dma_cfg config; 1478 u32 last_fdq = 0; 1479 u8 name[16]; 1480 int ret; 1481 int i; 1482 1483 /* Create Rx/Tx descriptor pools */ 1484 snprintf(name, sizeof(name), "rx-pool-%s", ndev->name); 1485 netcp->rx_pool = knav_pool_create(name, netcp->rx_pool_size, 1486 netcp->rx_pool_region_id); 1487 if (IS_ERR_OR_NULL(netcp->rx_pool)) { 1488 dev_err(netcp->ndev_dev, "Couldn't create rx pool\n"); 1489 ret = PTR_ERR(netcp->rx_pool); 1490 goto fail; 1491 } 1492 1493 snprintf(name, sizeof(name), "tx-pool-%s", ndev->name); 1494 netcp->tx_pool = knav_pool_create(name, netcp->tx_pool_size, 1495 netcp->tx_pool_region_id); 1496 if (IS_ERR_OR_NULL(netcp->tx_pool)) { 1497 dev_err(netcp->ndev_dev, "Couldn't create tx pool\n"); 1498 ret = PTR_ERR(netcp->tx_pool); 1499 goto fail; 1500 } 1501 1502 /* open Tx completion queue */ 1503 snprintf(name, sizeof(name), "tx-compl-%s", ndev->name); 1504 netcp->tx_compl_q = knav_queue_open(name, netcp->tx_compl_qid, 0); 1505 if (IS_ERR_OR_NULL(netcp->tx_compl_q)) { 1506 ret = PTR_ERR(netcp->tx_compl_q); 1507 goto fail; 1508 } 1509 netcp->tx_compl_qid = knav_queue_get_id(netcp->tx_compl_q); 1510 1511 /* Set notification for Tx completion */ 1512 notify_cfg.fn = netcp_tx_notify; 1513 notify_cfg.fn_arg = netcp; 1514 ret = knav_queue_device_control(netcp->tx_compl_q, 1515 KNAV_QUEUE_SET_NOTIFIER, 1516 (unsigned long)¬ify_cfg); 1517 if (ret) 1518 goto fail; 1519 1520 knav_queue_disable_notify(netcp->tx_compl_q); 1521 1522 /* open Rx completion queue */ 1523 snprintf(name, sizeof(name), "rx-compl-%s", ndev->name); 1524 netcp->rx_queue = knav_queue_open(name, netcp->rx_queue_id, 0); 1525 if (IS_ERR_OR_NULL(netcp->rx_queue)) { 1526 ret = PTR_ERR(netcp->rx_queue); 1527 goto fail; 1528 } 1529 netcp->rx_queue_id = knav_queue_get_id(netcp->rx_queue); 1530 1531 /* Set notification for Rx completion */ 1532 notify_cfg.fn = netcp_rx_notify; 1533 notify_cfg.fn_arg = netcp; 1534 ret = knav_queue_device_control(netcp->rx_queue, 1535 KNAV_QUEUE_SET_NOTIFIER, 1536 (unsigned long)¬ify_cfg); 1537 if (ret) 1538 goto fail; 1539 1540 knav_queue_disable_notify(netcp->rx_queue); 1541 1542 /* open Rx FDQs */ 1543 for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN && 1544 netcp->rx_queue_depths[i] && netcp->rx_buffer_sizes[i]; ++i) { 1545 snprintf(name, sizeof(name), "rx-fdq-%s-%d", ndev->name, i); 1546 netcp->rx_fdq[i] = knav_queue_open(name, KNAV_QUEUE_GP, 0); 1547 if (IS_ERR_OR_NULL(netcp->rx_fdq[i])) { 1548 ret = PTR_ERR(netcp->rx_fdq[i]); 1549 goto fail; 1550 } 1551 } 1552 1553 memset(&config, 0, sizeof(config)); 1554 config.direction = DMA_DEV_TO_MEM; 1555 config.u.rx.einfo_present = true; 1556 config.u.rx.psinfo_present = true; 1557 config.u.rx.err_mode = DMA_DROP; 1558 config.u.rx.desc_type = DMA_DESC_HOST; 1559 config.u.rx.psinfo_at_sop = false; 1560 config.u.rx.sop_offset = NETCP_SOP_OFFSET; 1561 config.u.rx.dst_q = netcp->rx_queue_id; 1562 config.u.rx.thresh = DMA_THRESH_NONE; 1563 1564 for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN; ++i) { 1565 if (netcp->rx_fdq[i]) 1566 last_fdq = knav_queue_get_id(netcp->rx_fdq[i]); 1567 config.u.rx.fdq[i] = last_fdq; 1568 } 1569 1570 netcp->rx_channel = knav_dma_open_channel(netcp->netcp_device->device, 1571 netcp->dma_chan_name, &config); 1572 if (IS_ERR_OR_NULL(netcp->rx_channel)) { 1573 dev_err(netcp->ndev_dev, "failed opening rx chan(%s\n", 1574 netcp->dma_chan_name); 1575 goto fail; 1576 } 1577 1578 dev_dbg(netcp->ndev_dev, "opened RX channel: %p\n", netcp->rx_channel); 1579 return 0; 1580 1581 fail: 1582 netcp_free_navigator_resources(netcp); 1583 return ret; 1584 } 1585 1586 /* Open the device */ 1587 static int netcp_ndo_open(struct net_device *ndev) 1588 { 1589 struct netcp_intf *netcp = netdev_priv(ndev); 1590 struct netcp_intf_modpriv *intf_modpriv; 1591 struct netcp_module *module; 1592 int ret; 1593 1594 netif_carrier_off(ndev); 1595 ret = netcp_setup_navigator_resources(ndev); 1596 if (ret) { 1597 dev_err(netcp->ndev_dev, "Failed to setup navigator resources\n"); 1598 goto fail; 1599 } 1600 1601 mutex_lock(&netcp_modules_lock); 1602 for_each_module(netcp, intf_modpriv) { 1603 module = intf_modpriv->netcp_module; 1604 if (module->open) { 1605 ret = module->open(intf_modpriv->module_priv, ndev); 1606 if (ret != 0) { 1607 dev_err(netcp->ndev_dev, "module open failed\n"); 1608 goto fail_open; 1609 } 1610 } 1611 } 1612 mutex_unlock(&netcp_modules_lock); 1613 1614 netcp_rxpool_refill(netcp); 1615 napi_enable(&netcp->rx_napi); 1616 napi_enable(&netcp->tx_napi); 1617 knav_queue_enable_notify(netcp->tx_compl_q); 1618 knav_queue_enable_notify(netcp->rx_queue); 1619 netif_tx_wake_all_queues(ndev); 1620 dev_dbg(netcp->ndev_dev, "netcp device %s opened\n", ndev->name); 1621 return 0; 1622 1623 fail_open: 1624 for_each_module(netcp, intf_modpriv) { 1625 module = intf_modpriv->netcp_module; 1626 if (module->close) 1627 module->close(intf_modpriv->module_priv, ndev); 1628 } 1629 mutex_unlock(&netcp_modules_lock); 1630 1631 fail: 1632 netcp_free_navigator_resources(netcp); 1633 return ret; 1634 } 1635 1636 /* Close the device */ 1637 static int netcp_ndo_stop(struct net_device *ndev) 1638 { 1639 struct netcp_intf *netcp = netdev_priv(ndev); 1640 struct netcp_intf_modpriv *intf_modpriv; 1641 struct netcp_module *module; 1642 int err = 0; 1643 1644 netif_tx_stop_all_queues(ndev); 1645 netif_carrier_off(ndev); 1646 netcp_addr_clear_mark(netcp); 1647 netcp_addr_sweep_del(netcp); 1648 knav_queue_disable_notify(netcp->rx_queue); 1649 knav_queue_disable_notify(netcp->tx_compl_q); 1650 napi_disable(&netcp->rx_napi); 1651 napi_disable(&netcp->tx_napi); 1652 1653 mutex_lock(&netcp_modules_lock); 1654 for_each_module(netcp, intf_modpriv) { 1655 module = intf_modpriv->netcp_module; 1656 if (module->close) { 1657 err = module->close(intf_modpriv->module_priv, ndev); 1658 if (err != 0) 1659 dev_err(netcp->ndev_dev, "Close failed\n"); 1660 } 1661 } 1662 mutex_unlock(&netcp_modules_lock); 1663 1664 /* Recycle Rx descriptors from completion queue */ 1665 netcp_empty_rx_queue(netcp); 1666 1667 /* Recycle Tx descriptors from completion queue */ 1668 netcp_process_tx_compl_packets(netcp, netcp->tx_pool_size); 1669 1670 if (knav_pool_count(netcp->tx_pool) != netcp->tx_pool_size) 1671 dev_err(netcp->ndev_dev, "Lost (%d) Tx descs\n", 1672 netcp->tx_pool_size - knav_pool_count(netcp->tx_pool)); 1673 1674 netcp_free_navigator_resources(netcp); 1675 dev_dbg(netcp->ndev_dev, "netcp device %s stopped\n", ndev->name); 1676 return 0; 1677 } 1678 1679 static int netcp_ndo_ioctl(struct net_device *ndev, 1680 struct ifreq *req, int cmd) 1681 { 1682 struct netcp_intf *netcp = netdev_priv(ndev); 1683 struct netcp_intf_modpriv *intf_modpriv; 1684 struct netcp_module *module; 1685 int ret = -1, err = -EOPNOTSUPP; 1686 1687 if (!netif_running(ndev)) 1688 return -EINVAL; 1689 1690 mutex_lock(&netcp_modules_lock); 1691 for_each_module(netcp, intf_modpriv) { 1692 module = intf_modpriv->netcp_module; 1693 if (!module->ioctl) 1694 continue; 1695 1696 err = module->ioctl(intf_modpriv->module_priv, req, cmd); 1697 if ((err < 0) && (err != -EOPNOTSUPP)) { 1698 ret = err; 1699 goto out; 1700 } 1701 if (err == 0) 1702 ret = err; 1703 } 1704 1705 out: 1706 mutex_unlock(&netcp_modules_lock); 1707 return (ret == 0) ? 0 : err; 1708 } 1709 1710 static int netcp_ndo_change_mtu(struct net_device *ndev, int new_mtu) 1711 { 1712 struct netcp_intf *netcp = netdev_priv(ndev); 1713 1714 /* MTU < 68 is an error for IPv4 traffic */ 1715 if ((new_mtu < 68) || 1716 (new_mtu > (NETCP_MAX_FRAME_SIZE - ETH_HLEN - ETH_FCS_LEN))) { 1717 dev_err(netcp->ndev_dev, "Invalid mtu size = %d\n", new_mtu); 1718 return -EINVAL; 1719 } 1720 1721 ndev->mtu = new_mtu; 1722 return 0; 1723 } 1724 1725 static void netcp_ndo_tx_timeout(struct net_device *ndev) 1726 { 1727 struct netcp_intf *netcp = netdev_priv(ndev); 1728 unsigned int descs = knav_pool_count(netcp->tx_pool); 1729 1730 dev_err(netcp->ndev_dev, "transmit timed out tx descs(%d)\n", descs); 1731 netcp_process_tx_compl_packets(netcp, netcp->tx_pool_size); 1732 ndev->trans_start = jiffies; 1733 netif_tx_wake_all_queues(ndev); 1734 } 1735 1736 static int netcp_rx_add_vid(struct net_device *ndev, __be16 proto, u16 vid) 1737 { 1738 struct netcp_intf *netcp = netdev_priv(ndev); 1739 struct netcp_intf_modpriv *intf_modpriv; 1740 struct netcp_module *module; 1741 int err = 0; 1742 1743 dev_dbg(netcp->ndev_dev, "adding rx vlan id: %d\n", vid); 1744 1745 mutex_lock(&netcp_modules_lock); 1746 for_each_module(netcp, intf_modpriv) { 1747 module = intf_modpriv->netcp_module; 1748 if ((module->add_vid) && (vid != 0)) { 1749 err = module->add_vid(intf_modpriv->module_priv, vid); 1750 if (err != 0) { 1751 dev_err(netcp->ndev_dev, "Could not add vlan id = %d\n", 1752 vid); 1753 break; 1754 } 1755 } 1756 } 1757 mutex_unlock(&netcp_modules_lock); 1758 return err; 1759 } 1760 1761 static int netcp_rx_kill_vid(struct net_device *ndev, __be16 proto, u16 vid) 1762 { 1763 struct netcp_intf *netcp = netdev_priv(ndev); 1764 struct netcp_intf_modpriv *intf_modpriv; 1765 struct netcp_module *module; 1766 int err = 0; 1767 1768 dev_dbg(netcp->ndev_dev, "removing rx vlan id: %d\n", vid); 1769 1770 mutex_lock(&netcp_modules_lock); 1771 for_each_module(netcp, intf_modpriv) { 1772 module = intf_modpriv->netcp_module; 1773 if (module->del_vid) { 1774 err = module->del_vid(intf_modpriv->module_priv, vid); 1775 if (err != 0) { 1776 dev_err(netcp->ndev_dev, "Could not delete vlan id = %d\n", 1777 vid); 1778 break; 1779 } 1780 } 1781 } 1782 mutex_unlock(&netcp_modules_lock); 1783 return err; 1784 } 1785 1786 static u16 netcp_select_queue(struct net_device *dev, struct sk_buff *skb, 1787 void *accel_priv, 1788 select_queue_fallback_t fallback) 1789 { 1790 return 0; 1791 } 1792 1793 static int netcp_setup_tc(struct net_device *dev, u8 num_tc) 1794 { 1795 int i; 1796 1797 /* setup tc must be called under rtnl lock */ 1798 ASSERT_RTNL(); 1799 1800 /* Sanity-check the number of traffic classes requested */ 1801 if ((dev->real_num_tx_queues <= 1) || 1802 (dev->real_num_tx_queues < num_tc)) 1803 return -EINVAL; 1804 1805 /* Configure traffic class to queue mappings */ 1806 if (num_tc) { 1807 netdev_set_num_tc(dev, num_tc); 1808 for (i = 0; i < num_tc; i++) 1809 netdev_set_tc_queue(dev, i, 1, i); 1810 } else { 1811 netdev_reset_tc(dev); 1812 } 1813 1814 return 0; 1815 } 1816 1817 static const struct net_device_ops netcp_netdev_ops = { 1818 .ndo_open = netcp_ndo_open, 1819 .ndo_stop = netcp_ndo_stop, 1820 .ndo_start_xmit = netcp_ndo_start_xmit, 1821 .ndo_set_rx_mode = netcp_set_rx_mode, 1822 .ndo_do_ioctl = netcp_ndo_ioctl, 1823 .ndo_change_mtu = netcp_ndo_change_mtu, 1824 .ndo_set_mac_address = eth_mac_addr, 1825 .ndo_validate_addr = eth_validate_addr, 1826 .ndo_vlan_rx_add_vid = netcp_rx_add_vid, 1827 .ndo_vlan_rx_kill_vid = netcp_rx_kill_vid, 1828 .ndo_tx_timeout = netcp_ndo_tx_timeout, 1829 .ndo_select_queue = netcp_select_queue, 1830 .ndo_setup_tc = netcp_setup_tc, 1831 }; 1832 1833 static int netcp_create_interface(struct netcp_device *netcp_device, 1834 struct device_node *node_interface) 1835 { 1836 struct device *dev = netcp_device->device; 1837 struct device_node *node = dev->of_node; 1838 struct netcp_intf *netcp; 1839 struct net_device *ndev; 1840 resource_size_t size; 1841 struct resource res; 1842 void __iomem *efuse = NULL; 1843 u32 efuse_mac = 0; 1844 const void *mac_addr; 1845 u8 efuse_mac_addr[6]; 1846 u32 temp[2]; 1847 int ret = 0; 1848 1849 ndev = alloc_etherdev_mqs(sizeof(*netcp), 1, 1); 1850 if (!ndev) { 1851 dev_err(dev, "Error allocating netdev\n"); 1852 return -ENOMEM; 1853 } 1854 1855 ndev->features |= NETIF_F_SG; 1856 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 1857 ndev->hw_features = ndev->features; 1858 ndev->vlan_features |= NETIF_F_SG; 1859 1860 netcp = netdev_priv(ndev); 1861 spin_lock_init(&netcp->lock); 1862 INIT_LIST_HEAD(&netcp->module_head); 1863 INIT_LIST_HEAD(&netcp->txhook_list_head); 1864 INIT_LIST_HEAD(&netcp->rxhook_list_head); 1865 INIT_LIST_HEAD(&netcp->addr_list); 1866 netcp->netcp_device = netcp_device; 1867 netcp->dev = netcp_device->device; 1868 netcp->ndev = ndev; 1869 netcp->ndev_dev = &ndev->dev; 1870 netcp->msg_enable = netif_msg_init(netcp_debug_level, NETCP_DEBUG); 1871 netcp->tx_pause_threshold = MAX_SKB_FRAGS; 1872 netcp->tx_resume_threshold = netcp->tx_pause_threshold; 1873 netcp->node_interface = node_interface; 1874 1875 ret = of_property_read_u32(node_interface, "efuse-mac", &efuse_mac); 1876 if (efuse_mac) { 1877 if (of_address_to_resource(node, NETCP_EFUSE_REG_INDEX, &res)) { 1878 dev_err(dev, "could not find efuse-mac reg resource\n"); 1879 ret = -ENODEV; 1880 goto quit; 1881 } 1882 size = resource_size(&res); 1883 1884 if (!devm_request_mem_region(dev, res.start, size, 1885 dev_name(dev))) { 1886 dev_err(dev, "could not reserve resource\n"); 1887 ret = -ENOMEM; 1888 goto quit; 1889 } 1890 1891 efuse = devm_ioremap_nocache(dev, res.start, size); 1892 if (!efuse) { 1893 dev_err(dev, "could not map resource\n"); 1894 devm_release_mem_region(dev, res.start, size); 1895 ret = -ENOMEM; 1896 goto quit; 1897 } 1898 1899 emac_arch_get_mac_addr(efuse_mac_addr, efuse); 1900 if (is_valid_ether_addr(efuse_mac_addr)) 1901 ether_addr_copy(ndev->dev_addr, efuse_mac_addr); 1902 else 1903 random_ether_addr(ndev->dev_addr); 1904 1905 devm_iounmap(dev, efuse); 1906 devm_release_mem_region(dev, res.start, size); 1907 } else { 1908 mac_addr = of_get_mac_address(node_interface); 1909 if (mac_addr) 1910 ether_addr_copy(ndev->dev_addr, mac_addr); 1911 else 1912 random_ether_addr(ndev->dev_addr); 1913 } 1914 1915 ret = of_property_read_string(node_interface, "rx-channel", 1916 &netcp->dma_chan_name); 1917 if (ret < 0) { 1918 dev_err(dev, "missing \"rx-channel\" parameter\n"); 1919 ret = -ENODEV; 1920 goto quit; 1921 } 1922 1923 ret = of_property_read_u32(node_interface, "rx-queue", 1924 &netcp->rx_queue_id); 1925 if (ret < 0) { 1926 dev_warn(dev, "missing \"rx-queue\" parameter\n"); 1927 netcp->rx_queue_id = KNAV_QUEUE_QPEND; 1928 } 1929 1930 ret = of_property_read_u32_array(node_interface, "rx-queue-depth", 1931 netcp->rx_queue_depths, 1932 KNAV_DMA_FDQ_PER_CHAN); 1933 if (ret < 0) { 1934 dev_err(dev, "missing \"rx-queue-depth\" parameter\n"); 1935 netcp->rx_queue_depths[0] = 128; 1936 } 1937 1938 ret = of_property_read_u32_array(node_interface, "rx-buffer-size", 1939 netcp->rx_buffer_sizes, 1940 KNAV_DMA_FDQ_PER_CHAN); 1941 if (ret) { 1942 dev_err(dev, "missing \"rx-buffer-size\" parameter\n"); 1943 netcp->rx_buffer_sizes[0] = 1536; 1944 } 1945 1946 ret = of_property_read_u32_array(node_interface, "rx-pool", temp, 2); 1947 if (ret < 0) { 1948 dev_err(dev, "missing \"rx-pool\" parameter\n"); 1949 ret = -ENODEV; 1950 goto quit; 1951 } 1952 netcp->rx_pool_size = temp[0]; 1953 netcp->rx_pool_region_id = temp[1]; 1954 1955 ret = of_property_read_u32_array(node_interface, "tx-pool", temp, 2); 1956 if (ret < 0) { 1957 dev_err(dev, "missing \"tx-pool\" parameter\n"); 1958 ret = -ENODEV; 1959 goto quit; 1960 } 1961 netcp->tx_pool_size = temp[0]; 1962 netcp->tx_pool_region_id = temp[1]; 1963 1964 if (netcp->tx_pool_size < MAX_SKB_FRAGS) { 1965 dev_err(dev, "tx-pool size too small, must be atleast(%ld)\n", 1966 MAX_SKB_FRAGS); 1967 ret = -ENODEV; 1968 goto quit; 1969 } 1970 1971 ret = of_property_read_u32(node_interface, "tx-completion-queue", 1972 &netcp->tx_compl_qid); 1973 if (ret < 0) { 1974 dev_warn(dev, "missing \"tx-completion-queue\" parameter\n"); 1975 netcp->tx_compl_qid = KNAV_QUEUE_QPEND; 1976 } 1977 1978 /* NAPI register */ 1979 netif_napi_add(ndev, &netcp->rx_napi, netcp_rx_poll, NETCP_NAPI_WEIGHT); 1980 netif_napi_add(ndev, &netcp->tx_napi, netcp_tx_poll, NETCP_NAPI_WEIGHT); 1981 1982 /* Register the network device */ 1983 ndev->dev_id = 0; 1984 ndev->watchdog_timeo = NETCP_TX_TIMEOUT; 1985 ndev->netdev_ops = &netcp_netdev_ops; 1986 SET_NETDEV_DEV(ndev, dev); 1987 1988 list_add_tail(&netcp->interface_list, &netcp_device->interface_head); 1989 return 0; 1990 1991 quit: 1992 free_netdev(ndev); 1993 return ret; 1994 } 1995 1996 static void netcp_delete_interface(struct netcp_device *netcp_device, 1997 struct net_device *ndev) 1998 { 1999 struct netcp_intf_modpriv *intf_modpriv, *tmp; 2000 struct netcp_intf *netcp = netdev_priv(ndev); 2001 struct netcp_module *module; 2002 2003 dev_dbg(netcp_device->device, "Removing interface \"%s\"\n", 2004 ndev->name); 2005 2006 /* Notify each of the modules that the interface is going away */ 2007 list_for_each_entry_safe(intf_modpriv, tmp, &netcp->module_head, 2008 intf_list) { 2009 module = intf_modpriv->netcp_module; 2010 dev_dbg(netcp_device->device, "Releasing module \"%s\"\n", 2011 module->name); 2012 if (module->release) 2013 module->release(intf_modpriv->module_priv); 2014 list_del(&intf_modpriv->intf_list); 2015 kfree(intf_modpriv); 2016 } 2017 WARN(!list_empty(&netcp->module_head), "%s interface module list is not empty!\n", 2018 ndev->name); 2019 2020 list_del(&netcp->interface_list); 2021 2022 of_node_put(netcp->node_interface); 2023 unregister_netdev(ndev); 2024 netif_napi_del(&netcp->rx_napi); 2025 free_netdev(ndev); 2026 } 2027 2028 static int netcp_probe(struct platform_device *pdev) 2029 { 2030 struct device_node *node = pdev->dev.of_node; 2031 struct netcp_intf *netcp_intf, *netcp_tmp; 2032 struct device_node *child, *interfaces; 2033 struct netcp_device *netcp_device; 2034 struct device *dev = &pdev->dev; 2035 struct netcp_module *module; 2036 int ret; 2037 2038 if (!node) { 2039 dev_err(dev, "could not find device info\n"); 2040 return -ENODEV; 2041 } 2042 2043 /* Allocate a new NETCP device instance */ 2044 netcp_device = devm_kzalloc(dev, sizeof(*netcp_device), GFP_KERNEL); 2045 if (!netcp_device) 2046 return -ENOMEM; 2047 2048 pm_runtime_enable(&pdev->dev); 2049 ret = pm_runtime_get_sync(&pdev->dev); 2050 if (ret < 0) { 2051 dev_err(dev, "Failed to enable NETCP power-domain\n"); 2052 pm_runtime_disable(&pdev->dev); 2053 return ret; 2054 } 2055 2056 /* Initialize the NETCP device instance */ 2057 INIT_LIST_HEAD(&netcp_device->interface_head); 2058 INIT_LIST_HEAD(&netcp_device->modpriv_head); 2059 netcp_device->device = dev; 2060 platform_set_drvdata(pdev, netcp_device); 2061 2062 /* create interfaces */ 2063 interfaces = of_get_child_by_name(node, "netcp-interfaces"); 2064 if (!interfaces) { 2065 dev_err(dev, "could not find netcp-interfaces node\n"); 2066 ret = -ENODEV; 2067 goto probe_quit; 2068 } 2069 2070 for_each_available_child_of_node(interfaces, child) { 2071 ret = netcp_create_interface(netcp_device, child); 2072 if (ret) { 2073 dev_err(dev, "could not create interface(%s)\n", 2074 child->name); 2075 goto probe_quit_interface; 2076 } 2077 } 2078 2079 /* Add the device instance to the list */ 2080 list_add_tail(&netcp_device->device_list, &netcp_devices); 2081 2082 /* Probe & attach any modules already registered */ 2083 mutex_lock(&netcp_modules_lock); 2084 for_each_netcp_module(module) { 2085 ret = netcp_module_probe(netcp_device, module); 2086 if (ret < 0) 2087 dev_err(dev, "module(%s) probe failed\n", module->name); 2088 } 2089 mutex_unlock(&netcp_modules_lock); 2090 return 0; 2091 2092 probe_quit_interface: 2093 list_for_each_entry_safe(netcp_intf, netcp_tmp, 2094 &netcp_device->interface_head, 2095 interface_list) { 2096 netcp_delete_interface(netcp_device, netcp_intf->ndev); 2097 } 2098 2099 probe_quit: 2100 pm_runtime_put_sync(&pdev->dev); 2101 pm_runtime_disable(&pdev->dev); 2102 platform_set_drvdata(pdev, NULL); 2103 return ret; 2104 } 2105 2106 static int netcp_remove(struct platform_device *pdev) 2107 { 2108 struct netcp_device *netcp_device = platform_get_drvdata(pdev); 2109 struct netcp_inst_modpriv *inst_modpriv, *tmp; 2110 struct netcp_module *module; 2111 2112 list_for_each_entry_safe(inst_modpriv, tmp, &netcp_device->modpriv_head, 2113 inst_list) { 2114 module = inst_modpriv->netcp_module; 2115 dev_dbg(&pdev->dev, "Removing module \"%s\"\n", module->name); 2116 module->remove(netcp_device, inst_modpriv->module_priv); 2117 list_del(&inst_modpriv->inst_list); 2118 kfree(inst_modpriv); 2119 } 2120 WARN(!list_empty(&netcp_device->interface_head), "%s interface list not empty!\n", 2121 pdev->name); 2122 2123 devm_kfree(&pdev->dev, netcp_device); 2124 pm_runtime_put_sync(&pdev->dev); 2125 pm_runtime_disable(&pdev->dev); 2126 platform_set_drvdata(pdev, NULL); 2127 return 0; 2128 } 2129 2130 static struct of_device_id of_match[] = { 2131 { .compatible = "ti,netcp-1.0", }, 2132 {}, 2133 }; 2134 MODULE_DEVICE_TABLE(of, of_match); 2135 2136 static struct platform_driver netcp_driver = { 2137 .driver = { 2138 .name = "netcp-1.0", 2139 .owner = THIS_MODULE, 2140 .of_match_table = of_match, 2141 }, 2142 .probe = netcp_probe, 2143 .remove = netcp_remove, 2144 }; 2145 module_platform_driver(netcp_driver); 2146 2147 MODULE_LICENSE("GPL v2"); 2148 MODULE_DESCRIPTION("TI NETCP driver for Keystone SOCs"); 2149 MODULE_AUTHOR("Sandeep Nair <sandeep_n@ti.com"); 2150