Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Simulated Ethernet Driver |
| 3 | * |
| 4 | * Copyright (C) 1999-2001, 2003 Hewlett-Packard Co |
| 5 | * Stephane Eranian <eranian@hpl.hp.com> |
| 6 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/kernel.h> |
| 8 | #include <linux/sched.h> |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/in.h> |
| 11 | #include <linux/string.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/netdevice.h> |
| 16 | #include <linux/etherdevice.h> |
| 17 | #include <linux/inetdevice.h> |
| 18 | #include <linux/if_ether.h> |
| 19 | #include <linux/if_arp.h> |
| 20 | #include <linux/skbuff.h> |
| 21 | #include <linux/notifier.h> |
| 22 | #include <linux/bitops.h> |
| 23 | #include <asm/system.h> |
| 24 | #include <asm/irq.h> |
Peter Chubb | 7b3166d | 2007-08-21 13:57:01 +1000 | [diff] [blame^] | 25 | #include <asm/hpsim.h> |
| 26 | |
| 27 | #include "hpsim_ssc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | #define SIMETH_RECV_MAX 10 |
| 30 | |
| 31 | /* |
| 32 | * Maximum possible received frame for Ethernet. |
| 33 | * We preallocate an sk_buff of that size to avoid costly |
| 34 | * memcpy for temporary buffer into sk_buff. We do basically |
| 35 | * what's done in other drivers, like eepro with a ring. |
| 36 | * The difference is, of course, that we don't have real DMA !!! |
| 37 | */ |
| 38 | #define SIMETH_FRAME_SIZE ETH_FRAME_LEN |
| 39 | |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #define NETWORK_INTR 8 |
| 42 | |
| 43 | struct simeth_local { |
| 44 | struct net_device_stats stats; |
| 45 | int simfd; /* descriptor in the simulator */ |
| 46 | }; |
| 47 | |
| 48 | static int simeth_probe1(void); |
| 49 | static int simeth_open(struct net_device *dev); |
| 50 | static int simeth_close(struct net_device *dev); |
| 51 | static int simeth_tx(struct sk_buff *skb, struct net_device *dev); |
| 52 | static int simeth_rx(struct net_device *dev); |
| 53 | static struct net_device_stats *simeth_get_stats(struct net_device *dev); |
Al Viro | 5dcded1 | 2006-10-08 14:59:19 +0100 | [diff] [blame] | 54 | static irqreturn_t simeth_interrupt(int irq, void *dev_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | static void set_multicast_list(struct net_device *dev); |
| 56 | static int simeth_device_event(struct notifier_block *this,unsigned long event, void *ptr); |
| 57 | |
| 58 | static char *simeth_version="0.3"; |
| 59 | |
| 60 | /* |
| 61 | * This variable is used to establish a mapping between the Linux/ia64 kernel |
| 62 | * and the host linux kernel. |
| 63 | * |
| 64 | * As of today, we support only one card, even though most of the code |
| 65 | * is ready for many more. The mapping is then: |
| 66 | * linux/ia64 -> linux/x86 |
| 67 | * eth0 -> eth1 |
| 68 | * |
| 69 | * In the future, we some string operations, we could easily support up |
| 70 | * to 10 cards (0-9). |
| 71 | * |
| 72 | * The default mapping can be changed on the kernel command line by |
| 73 | * specifying simeth=ethX (or whatever string you want). |
| 74 | */ |
| 75 | static char *simeth_device="eth0"; /* default host interface to use */ |
| 76 | |
| 77 | |
| 78 | |
| 79 | static volatile unsigned int card_count; /* how many cards "found" so far */ |
| 80 | static int simeth_debug; /* set to 1 to get debug information */ |
| 81 | |
| 82 | /* |
| 83 | * Used to catch IFF_UP & IFF_DOWN events |
| 84 | */ |
| 85 | static struct notifier_block simeth_dev_notifier = { |
| 86 | simeth_device_event, |
Al Viro | cfa7fd7 | 2006-10-10 22:46:17 +0100 | [diff] [blame] | 87 | NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | |
| 91 | /* |
| 92 | * Function used when using a kernel command line option. |
| 93 | * |
| 94 | * Format: simeth=interface_name (like eth0) |
| 95 | */ |
| 96 | static int __init |
| 97 | simeth_setup(char *str) |
| 98 | { |
| 99 | simeth_device = str; |
| 100 | return 1; |
| 101 | } |
| 102 | |
| 103 | __setup("simeth=", simeth_setup); |
| 104 | |
| 105 | /* |
| 106 | * Function used to probe for simeth devices when not installed |
| 107 | * as a loadable module |
| 108 | */ |
| 109 | |
| 110 | int __init |
| 111 | simeth_probe (void) |
| 112 | { |
| 113 | int r; |
| 114 | |
| 115 | printk(KERN_INFO "simeth: v%s\n", simeth_version); |
| 116 | |
| 117 | r = simeth_probe1(); |
| 118 | |
| 119 | if (r == 0) register_netdevice_notifier(&simeth_dev_notifier); |
| 120 | |
| 121 | return r; |
| 122 | } |
| 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | static inline int |
| 125 | netdev_probe(char *name, unsigned char *ether) |
| 126 | { |
| 127 | return ia64_ssc(__pa(name), __pa(ether), 0,0, SSC_NETDEV_PROBE); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | static inline int |
| 132 | netdev_connect(int irq) |
| 133 | { |
| 134 | /* XXX Fix me |
| 135 | * this does not support multiple cards |
| 136 | * also no return value |
| 137 | */ |
| 138 | ia64_ssc_connect_irq(NETWORK_INTR, irq); |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static inline int |
| 143 | netdev_attach(int fd, int irq, unsigned int ipaddr) |
| 144 | { |
| 145 | /* this puts the host interface in the right mode (start interrupting) */ |
| 146 | return ia64_ssc(fd, ipaddr, 0,0, SSC_NETDEV_ATTACH); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | static inline int |
| 151 | netdev_detach(int fd) |
| 152 | { |
| 153 | /* |
| 154 | * inactivate the host interface (don't interrupt anymore) */ |
| 155 | return ia64_ssc(fd, 0,0,0, SSC_NETDEV_DETACH); |
| 156 | } |
| 157 | |
| 158 | static inline int |
| 159 | netdev_send(int fd, unsigned char *buf, unsigned int len) |
| 160 | { |
| 161 | return ia64_ssc(fd, __pa(buf), len, 0, SSC_NETDEV_SEND); |
| 162 | } |
| 163 | |
| 164 | static inline int |
| 165 | netdev_read(int fd, unsigned char *buf, unsigned int len) |
| 166 | { |
| 167 | return ia64_ssc(fd, __pa(buf), len, 0, SSC_NETDEV_RECV); |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Function shared with module code, so cannot be in init section |
| 172 | * |
| 173 | * So far this function "detects" only one card (test_&_set) but could |
| 174 | * be extended easily. |
| 175 | * |
| 176 | * Return: |
| 177 | * - -ENODEV is no device found |
| 178 | * - -ENOMEM is no more memory |
| 179 | * - 0 otherwise |
| 180 | */ |
| 181 | static int |
| 182 | simeth_probe1(void) |
| 183 | { |
| 184 | unsigned char mac_addr[ETH_ALEN]; |
| 185 | struct simeth_local *local; |
| 186 | struct net_device *dev; |
Kenji Kaneshige | 3b5cc09 | 2005-07-10 21:49:00 -0700 | [diff] [blame] | 187 | int fd, i, err, rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
| 189 | /* |
| 190 | * XXX Fix me |
| 191 | * let's support just one card for now |
| 192 | */ |
| 193 | if (test_and_set_bit(0, &card_count)) |
| 194 | return -ENODEV; |
| 195 | |
| 196 | /* |
| 197 | * check with the simulator for the device |
| 198 | */ |
| 199 | fd = netdev_probe(simeth_device, mac_addr); |
| 200 | if (fd == -1) |
| 201 | return -ENODEV; |
| 202 | |
| 203 | dev = alloc_etherdev(sizeof(struct simeth_local)); |
| 204 | if (!dev) |
| 205 | return -ENOMEM; |
| 206 | |
| 207 | memcpy(dev->dev_addr, mac_addr, sizeof(mac_addr)); |
| 208 | |
| 209 | local = dev->priv; |
| 210 | local->simfd = fd; /* keep track of underlying file descriptor */ |
| 211 | |
| 212 | dev->open = simeth_open; |
| 213 | dev->stop = simeth_close; |
| 214 | dev->hard_start_xmit = simeth_tx; |
| 215 | dev->get_stats = simeth_get_stats; |
| 216 | dev->set_multicast_list = set_multicast_list; /* no yet used */ |
| 217 | |
| 218 | err = register_netdev(dev); |
| 219 | if (err) { |
| 220 | free_netdev(dev); |
| 221 | return err; |
| 222 | } |
| 223 | |
Kenji Kaneshige | 3b5cc09 | 2005-07-10 21:49:00 -0700 | [diff] [blame] | 224 | if ((rc = assign_irq_vector(AUTO_ASSIGN)) < 0) |
| 225 | panic("%s: out of interrupt vectors!\n", __FUNCTION__); |
| 226 | dev->irq = rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
| 228 | /* |
| 229 | * attach the interrupt in the simulator, this does enable interrupts |
| 230 | * until a netdev_attach() is called |
| 231 | */ |
| 232 | netdev_connect(dev->irq); |
| 233 | |
| 234 | printk(KERN_INFO "%s: hosteth=%s simfd=%d, HwAddr", |
| 235 | dev->name, simeth_device, local->simfd); |
| 236 | for(i = 0; i < ETH_ALEN; i++) { |
| 237 | printk(" %2.2x", dev->dev_addr[i]); |
| 238 | } |
| 239 | printk(", IRQ %d\n", dev->irq); |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * actually binds the device to an interrupt vector |
| 246 | */ |
| 247 | static int |
| 248 | simeth_open(struct net_device *dev) |
| 249 | { |
| 250 | if (request_irq(dev->irq, simeth_interrupt, 0, "simeth", dev)) { |
| 251 | printk(KERN_WARNING "simeth: unable to get IRQ %d.\n", dev->irq); |
| 252 | return -EAGAIN; |
| 253 | } |
| 254 | |
| 255 | netif_start_queue(dev); |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | /* copied from lapbether.c */ |
| 261 | static __inline__ int dev_is_ethdev(struct net_device *dev) |
| 262 | { |
| 263 | return ( dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5)); |
| 264 | } |
| 265 | |
| 266 | |
| 267 | /* |
| 268 | * Handler for IFF_UP or IFF_DOWN |
| 269 | * |
| 270 | * The reason for that is that we don't want to be interrupted when the |
| 271 | * interface is down. There is no way to unconnect in the simualtor. Instead |
| 272 | * we use this function to shutdown packet processing in the frame filter |
| 273 | * in the simulator. Thus no interrupts are generated |
| 274 | * |
| 275 | * |
| 276 | * That's also the place where we pass the IP address of this device to the |
| 277 | * simulator so that that we can start filtering packets for it |
| 278 | * |
| 279 | * There may be a better way of doing this, but I don't know which yet. |
| 280 | */ |
| 281 | static int |
| 282 | simeth_device_event(struct notifier_block *this,unsigned long event, void *ptr) |
| 283 | { |
| 284 | struct net_device *dev = ptr; |
| 285 | struct simeth_local *local; |
| 286 | struct in_device *in_dev; |
| 287 | struct in_ifaddr **ifap = NULL; |
| 288 | struct in_ifaddr *ifa = NULL; |
| 289 | int r; |
| 290 | |
| 291 | |
| 292 | if ( ! dev ) { |
| 293 | printk(KERN_WARNING "simeth_device_event dev=0\n"); |
| 294 | return NOTIFY_DONE; |
| 295 | } |
| 296 | |
| 297 | if ( event != NETDEV_UP && event != NETDEV_DOWN ) return NOTIFY_DONE; |
| 298 | |
| 299 | /* |
| 300 | * Check whether or not it's for an ethernet device |
| 301 | * |
| 302 | * XXX Fixme: This works only as long as we support one |
| 303 | * type of ethernet device. |
| 304 | */ |
| 305 | if ( !dev_is_ethdev(dev) ) return NOTIFY_DONE; |
| 306 | |
| 307 | if ((in_dev=dev->ip_ptr) != NULL) { |
| 308 | for (ifap=&in_dev->ifa_list; (ifa=*ifap) != NULL; ifap=&ifa->ifa_next) |
| 309 | if (strcmp(dev->name, ifa->ifa_label) == 0) break; |
| 310 | } |
| 311 | if ( ifa == NULL ) { |
| 312 | printk(KERN_ERR "simeth_open: can't find device %s's ifa\n", dev->name); |
| 313 | return NOTIFY_DONE; |
| 314 | } |
| 315 | |
| 316 | printk(KERN_INFO "simeth_device_event: %s ipaddr=0x%x\n", |
Al Viro | a144ea4 | 2006-09-28 18:00:55 -0700 | [diff] [blame] | 317 | dev->name, ntohl(ifa->ifa_local)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
| 319 | /* |
| 320 | * XXX Fix me |
| 321 | * if the device was up, and we're simply reconfiguring it, not sure |
| 322 | * we get DOWN then UP. |
| 323 | */ |
| 324 | |
| 325 | local = dev->priv; |
| 326 | /* now do it for real */ |
| 327 | r = event == NETDEV_UP ? |
Al Viro | a144ea4 | 2006-09-28 18:00:55 -0700 | [diff] [blame] | 328 | netdev_attach(local->simfd, dev->irq, ntohl(ifa->ifa_local)): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | netdev_detach(local->simfd); |
| 330 | |
| 331 | printk(KERN_INFO "simeth: netdev_attach/detach: event=%s ->%d\n", |
| 332 | event == NETDEV_UP ? "attach":"detach", r); |
| 333 | |
| 334 | return NOTIFY_DONE; |
| 335 | } |
| 336 | |
| 337 | static int |
| 338 | simeth_close(struct net_device *dev) |
| 339 | { |
| 340 | netif_stop_queue(dev); |
| 341 | |
| 342 | free_irq(dev->irq, dev); |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * Only used for debug |
| 349 | */ |
| 350 | static void |
| 351 | frame_print(unsigned char *from, unsigned char *frame, int len) |
| 352 | { |
| 353 | int i; |
| 354 | |
| 355 | printk("%s: (%d) %02x", from, len, frame[0] & 0xff); |
| 356 | for(i=1; i < 6; i++ ) { |
| 357 | printk(":%02x", frame[i] &0xff); |
| 358 | } |
| 359 | printk(" %2x", frame[6] &0xff); |
| 360 | for(i=7; i < 12; i++ ) { |
| 361 | printk(":%02x", frame[i] &0xff); |
| 362 | } |
| 363 | printk(" [%02x%02x]\n", frame[12], frame[13]); |
| 364 | |
| 365 | for(i=14; i < len; i++ ) { |
| 366 | printk("%02x ", frame[i] &0xff); |
| 367 | if ( (i%10)==0) printk("\n"); |
| 368 | } |
| 369 | printk("\n"); |
| 370 | } |
| 371 | |
| 372 | |
| 373 | /* |
| 374 | * Function used to transmit of frame, very last one on the path before |
| 375 | * going to the simulator. |
| 376 | */ |
| 377 | static int |
| 378 | simeth_tx(struct sk_buff *skb, struct net_device *dev) |
| 379 | { |
| 380 | struct simeth_local *local = dev->priv; |
| 381 | |
| 382 | #if 0 |
| 383 | /* ensure we have at least ETH_ZLEN bytes (min frame size) */ |
| 384 | unsigned int length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN; |
| 385 | /* Where do the extra padding bytes comes from inthe skbuff ? */ |
| 386 | #else |
| 387 | /* the real driver in the host system is going to take care of that |
| 388 | * or maybe it's the NIC itself. |
| 389 | */ |
| 390 | unsigned int length = skb->len; |
| 391 | #endif |
| 392 | |
| 393 | local->stats.tx_bytes += skb->len; |
| 394 | local->stats.tx_packets++; |
| 395 | |
| 396 | |
| 397 | if (simeth_debug > 5) frame_print("simeth_tx", skb->data, length); |
| 398 | |
| 399 | netdev_send(local->simfd, skb->data, length); |
| 400 | |
| 401 | /* |
| 402 | * we are synchronous on write, so we don't simulate a |
| 403 | * trasnmit complete interrupt, thus we don't need to arm a tx |
| 404 | */ |
| 405 | |
| 406 | dev_kfree_skb(skb); |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static inline struct sk_buff * |
| 411 | make_new_skb(struct net_device *dev) |
| 412 | { |
| 413 | struct sk_buff *nskb; |
| 414 | |
| 415 | /* |
| 416 | * The +2 is used to make sure that the IP header is nicely |
| 417 | * aligned (on 4byte boundary I assume 14+2=16) |
| 418 | */ |
| 419 | nskb = dev_alloc_skb(SIMETH_FRAME_SIZE + 2); |
| 420 | if ( nskb == NULL ) { |
| 421 | printk(KERN_NOTICE "%s: memory squeeze. dropping packet.\n", dev->name); |
| 422 | return NULL; |
| 423 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
| 425 | skb_reserve(nskb, 2); /* Align IP on 16 byte boundaries */ |
| 426 | |
| 427 | skb_put(nskb,SIMETH_FRAME_SIZE); |
| 428 | |
| 429 | return nskb; |
| 430 | } |
| 431 | |
| 432 | /* |
| 433 | * called from interrupt handler to process a received frame |
| 434 | */ |
| 435 | static int |
| 436 | simeth_rx(struct net_device *dev) |
| 437 | { |
| 438 | struct simeth_local *local; |
| 439 | struct sk_buff *skb; |
| 440 | int len; |
| 441 | int rcv_count = SIMETH_RECV_MAX; |
| 442 | |
| 443 | local = dev->priv; |
| 444 | /* |
| 445 | * the loop concept has been borrowed from other drivers |
| 446 | * looks to me like it's a throttling thing to avoid pushing to many |
| 447 | * packets at one time into the stack. Making sure we can process them |
| 448 | * upstream and make forward progress overall |
| 449 | */ |
| 450 | do { |
| 451 | if ( (skb=make_new_skb(dev)) == NULL ) { |
| 452 | printk(KERN_NOTICE "%s: memory squeeze. dropping packet.\n", dev->name); |
| 453 | local->stats.rx_dropped++; |
| 454 | return 0; |
| 455 | } |
| 456 | /* |
| 457 | * Read only one frame at a time |
| 458 | */ |
| 459 | len = netdev_read(local->simfd, skb->data, SIMETH_FRAME_SIZE); |
| 460 | if ( len == 0 ) { |
| 461 | if ( simeth_debug > 0 ) printk(KERN_WARNING "%s: count=%d netdev_read=0\n", |
| 462 | dev->name, SIMETH_RECV_MAX-rcv_count); |
| 463 | break; |
| 464 | } |
| 465 | #if 0 |
| 466 | /* |
| 467 | * XXX Fix me |
| 468 | * Should really do a csum+copy here |
| 469 | */ |
Arnaldo Carvalho de Melo | 27d7ff4 | 2007-03-31 11:55:19 -0300 | [diff] [blame] | 470 | skb_copy_to_linear_data(skb, frame, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | #endif |
| 472 | skb->protocol = eth_type_trans(skb, dev); |
| 473 | |
| 474 | if ( simeth_debug > 6 ) frame_print("simeth_rx", skb->data, len); |
| 475 | |
| 476 | /* |
| 477 | * push the packet up & trigger software interrupt |
| 478 | */ |
| 479 | netif_rx(skb); |
| 480 | |
| 481 | local->stats.rx_packets++; |
| 482 | local->stats.rx_bytes += len; |
| 483 | |
| 484 | } while ( --rcv_count ); |
| 485 | |
| 486 | return len; /* 0 = nothing left to read, otherwise, we can try again */ |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * Interrupt handler (Yes, we can do it too !!!) |
| 491 | */ |
| 492 | static irqreturn_t |
Al Viro | 5dcded1 | 2006-10-08 14:59:19 +0100 | [diff] [blame] | 493 | simeth_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | { |
| 495 | struct net_device *dev = dev_id; |
| 496 | |
| 497 | if ( dev == NULL ) { |
| 498 | printk(KERN_WARNING "simeth: irq %d for unknown device\n", irq); |
| 499 | return IRQ_NONE; |
| 500 | } |
| 501 | |
| 502 | /* |
| 503 | * very simple loop because we get interrupts only when receiving |
| 504 | */ |
| 505 | while (simeth_rx(dev)); |
| 506 | return IRQ_HANDLED; |
| 507 | } |
| 508 | |
| 509 | static struct net_device_stats * |
| 510 | simeth_get_stats(struct net_device *dev) |
| 511 | { |
| 512 | struct simeth_local *local = dev->priv; |
| 513 | |
| 514 | return &local->stats; |
| 515 | } |
| 516 | |
| 517 | /* fake multicast ability */ |
| 518 | static void |
| 519 | set_multicast_list(struct net_device *dev) |
| 520 | { |
| 521 | printk(KERN_WARNING "%s: set_multicast_list called\n", dev->name); |
| 522 | } |
| 523 | |
| 524 | __initcall(simeth_probe); |