1/* 2 * 3 * arch/xtensa/platform-iss/network.c 4 * 5 * Platform specific initialization. 6 * 7 * Authors: Chris Zankel <chris@zankel.net> 8 * Based on work form the UML team. 9 * 10 * Copyright 2005 Tensilica Inc. 11 * 12 * This program is free software; you can redistribute it and/or modify it 13 * under the terms of the GNU General Public License as published by the 14 * Free Software Foundation; either version 2 of the License, or (at your 15 * option) any later version. 16 * 17 */ 18 19#include <linux/list.h> 20#include <linux/irq.h> 21#include <linux/spinlock.h> 22#include <linux/slab.h> 23#include <linux/timer.h> 24#include <linux/if_ether.h> 25#include <linux/inetdevice.h> 26#include <linux/init.h> 27#include <linux/if_tun.h> 28#include <linux/etherdevice.h> 29#include <linux/interrupt.h> 30#include <linux/ioctl.h> 31#include <linux/bootmem.h> 32#include <linux/ethtool.h> 33#include <linux/rtnetlink.h> 34#include <linux/timer.h> 35#include <linux/platform_device.h> 36 37#include <asm/platform/simcall.h> 38 39#define DRIVER_NAME "iss-netdev" 40#define ETH_MAX_PACKET 1500 41#define ETH_HEADER_OTHER 14 42#define ISS_NET_TIMER_VALUE (2 * HZ) 43 44 45static DEFINE_SPINLOCK(opened_lock); 46static LIST_HEAD(opened); 47 48static DEFINE_SPINLOCK(devices_lock); 49static LIST_HEAD(devices); 50 51/* ------------------------------------------------------------------------- */ 52 53/* We currently only support the TUNTAP transport protocol. */ 54 55#define TRANSPORT_TUNTAP_NAME "tuntap" 56#define TRANSPORT_TUNTAP_MTU ETH_MAX_PACKET 57 58struct tuntap_info { 59 char dev_name[IFNAMSIZ]; 60 int fixed_config; 61 unsigned char gw[ETH_ALEN]; 62 int fd; 63}; 64 65/* ------------------------------------------------------------------------- */ 66 67 68/* This structure contains out private information for the driver. */ 69 70struct iss_net_private { 71 72 struct list_head device_list; 73 struct list_head opened_list; 74 75 spinlock_t lock; 76 struct net_device *dev; 77 struct platform_device pdev; 78 struct timer_list tl; 79 struct net_device_stats stats; 80 81 struct timer_list timer; 82 unsigned int timer_val; 83 84 int index; 85 int mtu; 86 87 unsigned char mac[ETH_ALEN]; 88 int have_mac; 89 90 struct { 91 union { 92 struct tuntap_info tuntap; 93 } info; 94 95 int (*open)(struct iss_net_private *lp); 96 void (*close)(struct iss_net_private *lp); 97 int (*read)(struct iss_net_private *lp, struct sk_buff **skb); 98 int (*write)(struct iss_net_private *lp, struct sk_buff **skb); 99 unsigned short (*protocol)(struct sk_buff *skb); 100 int (*poll)(struct iss_net_private *lp); 101 } tp; 102 103}; 104 105/* ======================= ISS SIMCALL INTERFACE =========================== */ 106 107/* Note: __simc must _not_ be declared inline! */ 108 109static int errno; 110 111static int __simc (int a, int b, int c, int d, int e, int f) 112{ 113 int ret; 114 __asm__ __volatile__ ("simcall\n" 115 "mov %0, a2\n" 116 "mov %1, a3\n" : "=a" (ret), "=a" (errno) 117 : : "a2", "a3"); 118 return ret; 119} 120 121static int inline simc_open(char *file, int flags, int mode) 122{ 123 return __simc(SYS_open, (int) file, flags, mode, 0, 0); 124} 125 126static int inline simc_close(int fd) 127{ 128 return __simc(SYS_close, fd, 0, 0, 0, 0); 129} 130 131static int inline simc_ioctl(int fd, int request, void *arg) 132{ 133 return __simc(SYS_ioctl, fd, request, (int) arg, 0, 0); 134} 135 136static int inline simc_read(int fd, void *buf, size_t count) 137{ 138 return __simc(SYS_read, fd, (int) buf, count, 0, 0); 139} 140 141static int inline simc_write(int fd, void *buf, size_t count) 142{ 143 return __simc(SYS_write, fd, (int) buf, count, 0, 0); 144} 145 146static int inline simc_poll(int fd) 147{ 148 struct timeval tv = { .tv_sec = 0, .tv_usec = 0 }; 149 150 return __simc(SYS_select_one, fd, XTISS_SELECT_ONE_READ, (int)&tv,0,0); 151} 152 153/* ================================ HELPERS ================================ */ 154 155 156static char *split_if_spec(char *str, ...) 157{ 158 char **arg, *end; 159 va_list ap; 160 161 va_start(ap, str); 162 while ((arg = va_arg(ap, char**)) != NULL) { 163 if (*str == '\0') 164 return NULL; 165 end = strchr(str, ','); 166 if (end != str) 167 *arg = str; 168 if (end == NULL) 169 return NULL; 170 *end ++ = '\0'; 171 str = end; 172 } 173 va_end(ap); 174 return str; 175} 176 177 178 179/* Return the IP address as a string for a given device. */ 180 181static void dev_ip_addr(void *d, char *buf, char *bin_buf) 182{ 183 struct net_device *dev = d; 184 struct in_device *ip = dev->ip_ptr; 185 struct in_ifaddr *in; 186 __be32 addr; 187 188 if ((ip == NULL) || ((in = ip->ifa_list) == NULL)) { 189 printk(KERN_WARNING "Device not assigned an IP address!\n"); 190 return; 191 } 192 193 addr = in->ifa_address; 194 sprintf(buf, "%d.%d.%d.%d", addr & 0xff, (addr >> 8) & 0xff, 195 (addr >> 16) & 0xff, addr >> 24); 196 197 if (bin_buf) { 198 bin_buf[0] = addr & 0xff; 199 bin_buf[1] = (addr >> 8) & 0xff; 200 bin_buf[2] = (addr >> 16) & 0xff; 201 bin_buf[3] = addr >> 24; 202 } 203} 204 205/* Set Ethernet address of the specified device. */ 206 207static void inline set_ether_mac(void *d, unsigned char *addr) 208{ 209 struct net_device *dev = d; 210 memcpy(dev->dev_addr, addr, ETH_ALEN); 211} 212 213 214/* ======================= TUNTAP TRANSPORT INTERFACE ====================== */ 215 216static int tuntap_open(struct iss_net_private *lp) 217{ 218 struct ifreq ifr; 219 char *dev_name = lp->tp.info.tuntap.dev_name; 220 int err = -EINVAL; 221 int fd; 222 223 /* We currently only support a fixed configuration. */ 224 225 if (!lp->tp.info.tuntap.fixed_config) 226 return -EINVAL; 227 228 if ((fd = simc_open("/dev/net/tun", 02, 0)) < 0) { /* O_RDWR */ 229 printk("Failed to open /dev/net/tun, returned %d " 230 "(errno = %d)\n", fd, errno); 231 return fd; 232 } 233 234 memset(&ifr, 0, sizeof ifr); 235 ifr.ifr_flags = IFF_TAP | IFF_NO_PI; 236 strlcpy(ifr.ifr_name, dev_name, sizeof ifr.ifr_name); 237 238 if ((err = simc_ioctl(fd, TUNSETIFF, (void*) &ifr)) < 0) { 239 printk("Failed to set interface, returned %d " 240 "(errno = %d)\n", err, errno); 241 simc_close(fd); 242 return err; 243 } 244 245 lp->tp.info.tuntap.fd = fd; 246 return err; 247} 248 249static void tuntap_close(struct iss_net_private *lp) 250{ 251 simc_close(lp->tp.info.tuntap.fd); 252 lp->tp.info.tuntap.fd = -1; 253} 254 255static int tuntap_read (struct iss_net_private *lp, struct sk_buff **skb) 256{ 257 258 return simc_read(lp->tp.info.tuntap.fd, 259 (*skb)->data, (*skb)->dev->mtu + ETH_HEADER_OTHER); 260} 261 262static int tuntap_write (struct iss_net_private *lp, struct sk_buff **skb) 263{ 264 return simc_write(lp->tp.info.tuntap.fd, (*skb)->data, (*skb)->len); 265} 266 267unsigned short tuntap_protocol(struct sk_buff *skb) 268{ 269 return eth_type_trans(skb, skb->dev); 270} 271 272static int tuntap_poll(struct iss_net_private *lp) 273{ 274 return simc_poll(lp->tp.info.tuntap.fd); 275} 276 277/* 278 * Currently only a device name is supported. 279 * ethX=tuntap[,[mac address][,[device name]]] 280 */ 281 282static int tuntap_probe(struct iss_net_private *lp, int index, char *init) 283{ 284 const int len = strlen(TRANSPORT_TUNTAP_NAME); 285 char *dev_name = NULL, *mac_str = NULL, *rem = NULL; 286 287 /* Transport should be 'tuntap': ethX=tuntap,mac,dev_name */ 288 289 if (strncmp(init, TRANSPORT_TUNTAP_NAME, len)) 290 return 0; 291 292 if (*(init += strlen(TRANSPORT_TUNTAP_NAME)) == ',') { 293 if ((rem=split_if_spec(init+1, &mac_str, &dev_name)) != NULL) { 294 printk("Extra garbage on specification : '%s'\n", rem); 295 return 0; 296 } 297 } else if (*init != '\0') { 298 printk("Invalid argument: %s. Skipping device!\n", init); 299 return 0; 300 } 301 302 if (dev_name) { 303 strncpy(lp->tp.info.tuntap.dev_name, dev_name, 304 sizeof lp->tp.info.tuntap.dev_name); 305 lp->tp.info.tuntap.fixed_config = 1; 306 } else 307 strcpy(lp->tp.info.tuntap.dev_name, TRANSPORT_TUNTAP_NAME); 308 309 310 lp->mtu = TRANSPORT_TUNTAP_MTU; 311 312 //lp->info.tuntap.gate_addr = gate_addr; 313 314 lp->tp.info.tuntap.fd = -1; 315 316 lp->tp.open = tuntap_open; 317 lp->tp.close = tuntap_close; 318 lp->tp.read = tuntap_read; 319 lp->tp.write = tuntap_write; 320 lp->tp.protocol = tuntap_protocol; 321 lp->tp.poll = tuntap_poll; 322 323 printk("TUN/TAP backend - "); 324 printk("\n"); 325 326 return 1; 327} 328 329/* ================================ ISS NET ================================ */ 330 331static int iss_net_rx(struct net_device *dev) 332{ 333 struct iss_net_private *lp = dev->priv; 334 int pkt_len; 335 struct sk_buff *skb; 336 337 /* Check if there is any new data. */ 338 339 if (lp->tp.poll(lp) == 0) 340 return 0; 341 342 /* Try to allocate memory, if it fails, try again next round. */ 343 344 if ((skb = dev_alloc_skb(dev->mtu + 2 + ETH_HEADER_OTHER)) == NULL) { 345 lp->stats.rx_dropped++; 346 return 0; 347 } 348 349 skb_reserve(skb, 2); 350 351 /* Setup skb */ 352 353 skb->dev = dev; 354 skb_reset_mac_header(skb); 355 pkt_len = lp->tp.read(lp, &skb); 356 skb_put(skb, pkt_len); 357 358 if (pkt_len > 0) { 359 skb_trim(skb, pkt_len); 360 skb->protocol = lp->tp.protocol(skb); 361 // netif_rx(skb); 362 netif_rx_ni(skb); 363 364 lp->stats.rx_bytes += skb->len; 365 lp->stats.rx_packets++; 366 return pkt_len; 367 } 368 kfree_skb(skb); 369 return pkt_len; 370} 371 372static int iss_net_poll(void) 373{ 374 struct list_head *ele; 375 int err, ret = 0; 376 377 spin_lock(&opened_lock); 378 379 list_for_each(ele, &opened) { 380 struct iss_net_private *lp; 381 382 lp = list_entry(ele, struct iss_net_private, opened_list); 383 384 if (!netif_running(lp->dev)) 385 break; 386 387 spin_lock(&lp->lock); 388 389 while ((err = iss_net_rx(lp->dev)) > 0) 390 ret++; 391 392 spin_unlock(&lp->lock); 393 394 if (err < 0) { 395 printk(KERN_ERR "Device '%s' read returned %d, " 396 "shutting it down\n", lp->dev->name, err); 397 dev_close(lp->dev); 398 } else { 399 } 400 } 401 402 spin_unlock(&opened_lock); 403 return ret; 404} 405 406 407static void iss_net_timer(unsigned long priv) 408{ 409 struct iss_net_private* lp = (struct iss_net_private*) priv; 410 411 spin_lock(&lp->lock); 412 413 iss_net_poll(); 414 415 mod_timer(&lp->timer, jiffies + lp->timer_val); 416 417 spin_unlock(&lp->lock); 418} 419 420 421static int iss_net_open(struct net_device *dev) 422{ 423 struct iss_net_private *lp = dev->priv; 424 char addr[sizeof "255.255.255.255\0"]; 425 int err; 426 427 spin_lock(&lp->lock); 428 429 if ((err = lp->tp.open(lp)) < 0) 430 goto out; 431 432 if (!lp->have_mac) { 433 dev_ip_addr(dev, addr, &lp->mac[2]); 434 set_ether_mac(dev, lp->mac); 435 } 436 437 netif_start_queue(dev); 438 439 /* clear buffer - it can happen that the host side of the interface 440 * is full when we get here. In this case, new data is never queued, 441 * SIGIOs never arrive, and the net never works. 442 */ 443 while ((err = iss_net_rx(dev)) > 0) 444 ; 445 446 spin_lock(&opened_lock); 447 list_add(&lp->opened_list, &opened); 448 spin_unlock(&opened_lock); 449 450 init_timer(&lp->timer); 451 lp->timer_val = ISS_NET_TIMER_VALUE; 452 lp->timer.data = (unsigned long) lp; 453 lp->timer.function = iss_net_timer; 454 mod_timer(&lp->timer, jiffies + lp->timer_val); 455 456out: 457 spin_unlock(&lp->lock); 458 return err; 459} 460 461static int iss_net_close(struct net_device *dev) 462{ 463 struct iss_net_private *lp = dev->priv; 464printk("iss_net_close!\n"); 465 netif_stop_queue(dev); 466 spin_lock(&lp->lock); 467 468 spin_lock(&opened_lock); 469 list_del(&opened); 470 spin_unlock(&opened_lock); 471 472 del_timer_sync(&lp->timer); 473 474 lp->tp.close(lp); 475 476 spin_unlock(&lp->lock); 477 return 0; 478} 479 480static int iss_net_start_xmit(struct sk_buff *skb, struct net_device *dev) 481{ 482 struct iss_net_private *lp = dev->priv; 483 unsigned long flags; 484 int len; 485 486 netif_stop_queue(dev); 487 spin_lock_irqsave(&lp->lock, flags); 488 489 len = lp->tp.write(lp, &skb); 490 491 if (len == skb->len) { 492 lp->stats.tx_packets++; 493 lp->stats.tx_bytes += skb->len; 494 dev->trans_start = jiffies; 495 netif_start_queue(dev); 496 497 /* this is normally done in the interrupt when tx finishes */ 498 netif_wake_queue(dev); 499 500 } else if (len == 0) { 501 netif_start_queue(dev); 502 lp->stats.tx_dropped++; 503 504 } else { 505 netif_start_queue(dev); 506 printk(KERN_ERR "iss_net_start_xmit: failed(%d)\n", len); 507 } 508 509 spin_unlock_irqrestore(&lp->lock, flags); 510 511 dev_kfree_skb(skb); 512 return 0; 513} 514 515 516static struct net_device_stats *iss_net_get_stats(struct net_device *dev) 517{ 518 struct iss_net_private *lp = dev->priv; 519 return &lp->stats; 520} 521 522static void iss_net_set_multicast_list(struct net_device *dev) 523{ 524} 525 526static void iss_net_tx_timeout(struct net_device *dev) 527{ 528} 529 530static int iss_net_set_mac(struct net_device *dev, void *addr) 531{ 532 533 return 0; 534} 535 536static int iss_net_change_mtu(struct net_device *dev, int new_mtu) 537{ 538 return -EINVAL; 539} 540 541void iss_net_user_timer_expire(unsigned long _conn) 542{ 543} 544 545 546static struct platform_driver iss_net_driver = { 547 .driver = { 548 .name = DRIVER_NAME, 549 }, 550}; 551 552static int driver_registered; 553 554static int iss_net_configure(int index, char *init) 555{ 556 struct net_device *dev; 557 struct iss_net_private *lp; 558 int err; 559 560 if ((dev = alloc_etherdev(sizeof *lp)) == NULL) { 561 printk(KERN_ERR "eth_configure: failed to allocate device\n"); 562 return 1; 563 } 564 565 /* Initialize private element. */ 566 567 lp = dev->priv; 568 *lp = ((struct iss_net_private) { 569 .device_list = LIST_HEAD_INIT(lp->device_list), 570 .opened_list = LIST_HEAD_INIT(lp->opened_list), 571 .lock = SPIN_LOCK_UNLOCKED, 572 .dev = dev, 573 .index = index, 574 //.fd = -1, 575 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0 }, 576 .have_mac = 0, 577 }); 578 579 /* 580 * Try all transport protocols. 581 * Note: more protocols can be added by adding '&& !X_init(lp, eth)'. 582 */ 583 584 if (!tuntap_probe(lp, index, init)) { 585 printk("Invalid arguments. Skipping device!\n"); 586 goto errout; 587 } 588 589 printk(KERN_INFO "Netdevice %d ", index); 590 if (lp->have_mac) 591 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ", 592 lp->mac[0], lp->mac[1], 593 lp->mac[2], lp->mac[3], 594 lp->mac[4], lp->mac[5]); 595 printk(": "); 596 597 /* sysfs register */ 598 599 if (!driver_registered) { 600 platform_driver_register(&iss_net_driver); 601 driver_registered = 1; 602 } 603 604 spin_lock(&devices_lock); 605 list_add(&lp->device_list, &devices); 606 spin_unlock(&devices_lock); 607 608 lp->pdev.id = index; 609 lp->pdev.name = DRIVER_NAME; 610 platform_device_register(&lp->pdev); 611 SET_NETDEV_DEV(dev,&lp->pdev.dev); 612 613 /* 614 * If this name ends up conflicting with an existing registered 615 * netdevice, that is OK, register_netdev{,ice}() will notice this 616 * and fail. 617 */ 618 snprintf(dev->name, sizeof dev->name, "eth%d", index); 619 620 dev->mtu = lp->mtu; 621 dev->open = iss_net_open; 622 dev->hard_start_xmit = iss_net_start_xmit; 623 dev->stop = iss_net_close; 624 dev->get_stats = iss_net_get_stats; 625 dev->set_multicast_list = iss_net_set_multicast_list; 626 dev->tx_timeout = iss_net_tx_timeout; 627 dev->set_mac_address = iss_net_set_mac; 628 dev->change_mtu = iss_net_change_mtu; 629 dev->watchdog_timeo = (HZ >> 1); 630 dev->irq = -1; 631 632 rtnl_lock(); 633 err = register_netdevice(dev); 634 rtnl_unlock(); 635 636 if (err) { 637 printk("Error registering net device!\n"); 638 free_netdev(dev); 639 return 1; 640 } 641 642 init_timer(&lp->tl); 643 lp->tl.function = iss_net_user_timer_expire; 644 645 return 0; 646 647errout: 648 return -EIO; 649 650} 651 652/* ------------------------------------------------------------------------- */ 653 654/* Filled in during early boot */ 655 656struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line); 657 658struct iss_net_init { 659 struct list_head list; 660 char *init; /* init string */ 661 int index; 662}; 663 664/* 665 * Parse the command line and look for 'ethX=...' fields, and register all 666 * those fields. They will be later initialized in iss_net_init. 667 */ 668 669#define ERR KERN_ERR "iss_net_setup: " 670 671static int iss_net_setup(char *str) 672{ 673 struct iss_net_private *device = NULL; 674 struct iss_net_init *new; 675 struct list_head *ele; 676 char *end; 677 int n; 678 679 n = simple_strtoul(str, &end, 0); 680 if (end == str) { 681 printk(ERR "Failed to parse '%s'\n", str); 682 return 1; 683 } 684 if (n < 0) { 685 printk(ERR "Device %d is negative\n", n); 686 return 1; 687 } 688 if (*(str = end) != '=') { 689 printk(ERR "Expected '=' after device number\n"); 690 return 1; 691 } 692 693 spin_lock(&devices_lock); 694 695 list_for_each(ele, &devices) { 696 device = list_entry(ele, struct iss_net_private, device_list); 697 if (device->index == n) 698 break; 699 } 700 701 spin_unlock(&devices_lock); 702 703 if (device && device->index == n) { 704 printk(ERR "Device %d already configured\n", n); 705 return 1; 706 } 707 708 if ((new = alloc_bootmem(sizeof new)) == NULL) { 709 printk("Alloc_bootmem failed\n"); 710 return 1; 711 } 712 713 INIT_LIST_HEAD(&new->list); 714 new->index = n; 715 new->init = str + 1; 716 717 list_add_tail(&new->list, ð_cmd_line); 718 return 1; 719} 720 721#undef ERR 722 723__setup("eth", iss_net_setup); 724 725/* 726 * Initialize all ISS Ethernet devices previously registered in iss_net_setup. 727 */ 728 729static int iss_net_init(void) 730{ 731 struct list_head *ele, *next; 732 733 /* Walk through all Ethernet devices specified in the command line. */ 734 735 list_for_each_safe(ele, next, ð_cmd_line) { 736 struct iss_net_init *eth; 737 eth = list_entry(ele, struct iss_net_init, list); 738 iss_net_configure(eth->index, eth->init); 739 } 740 741 return 1; 742} 743 744module_init(iss_net_init); 745