simeth.c 12 KB

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