simeth.c 12 KB

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