simeth.c 12 KB

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