1
2
3
4
5
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
27#include "hpsim_ssc.h"
28
29#define SIMETH_RECV_MAX 10
30
31
32
33
34
35
36
37
38#define SIMETH_FRAME_SIZE ETH_FRAME_LEN
39
40
41#define NETWORK_INTR 8
42
43struct simeth_local {
44 struct net_device_stats stats;
45 int simfd;
46};
47
48static int simeth_probe1(void);
49static int simeth_open(struct net_device *dev);
50static int simeth_close(struct net_device *dev);
51static int simeth_tx(struct sk_buff *skb, struct net_device *dev);
52static int simeth_rx(struct net_device *dev);
53static struct net_device_stats *simeth_get_stats(struct net_device *dev);
54static irqreturn_t simeth_interrupt(int irq, void *dev_id);
55static void set_multicast_list(struct net_device *dev);
56static int simeth_device_event(struct notifier_block *this,unsigned long event, void *ptr);
57
58static char *simeth_version="0.3";
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75static char *simeth_device="eth0";
76
77
78
79static volatile unsigned int card_count;
80static int simeth_debug;
81
82
83
84
85static struct notifier_block simeth_dev_notifier = {
86 simeth_device_event,
87 NULL
88};
89
90
91
92
93
94
95
96static int __init
97simeth_setup(char *str)
98{
99 simeth_device = str;
100 return 1;
101}
102
103__setup("simeth=", simeth_setup);
104
105
106
107
108
109
110int __init
111simeth_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
124static inline int
125netdev_probe(char *name, unsigned char *ether)
126{
127 return ia64_ssc(__pa(name), __pa(ether), 0,0, SSC_NETDEV_PROBE);
128}
129
130
131static inline int
132netdev_connect(int irq)
133{
134
135
136
137
138 ia64_ssc_connect_irq(NETWORK_INTR, irq);
139 return 0;
140}
141
142static inline int
143netdev_attach(int fd, int irq, unsigned int ipaddr)
144{
145
146 return ia64_ssc(fd, ipaddr, 0,0, SSC_NETDEV_ATTACH);
147}
148
149
150static inline int
151netdev_detach(int fd)
152{
153
154
155 return ia64_ssc(fd, 0,0,0, SSC_NETDEV_DETACH);
156}
157
158static inline int
159netdev_send(int fd, unsigned char *buf, unsigned int len)
160{
161 return ia64_ssc(fd, __pa(buf), len, 0, SSC_NETDEV_SEND);
162}
163
164static inline int
165netdev_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
172
173
174
175
176
177
178
179
180
181static int
182simeth_probe1(void)
183{
184 unsigned char mac_addr[ETH_ALEN];
185 struct simeth_local *local;
186 struct net_device *dev;
187 int fd, i, err, rc;
188
189
190
191
192
193 if (test_and_set_bit(0, &card_count))
194 return -ENODEV;
195
196
197
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;
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;
217
218 err = register_netdev(dev);
219 if (err) {
220 free_netdev(dev);
221 return err;
222 }
223
224 if ((rc = assign_irq_vector(AUTO_ASSIGN)) < 0)
225 panic("%s: out of interrupt vectors!\n", __func__);
226 dev->irq = rc;
227
228
229
230
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
246
247static int
248simeth_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
261static __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
269
270
271
272
273
274
275
276
277
278
279
280
281static int
282simeth_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 (dev_net(dev) != &init_net)
298 return NOTIFY_DONE;
299
300 if ( event != NETDEV_UP && event != NETDEV_DOWN ) return NOTIFY_DONE;
301
302
303
304
305
306
307
308 if ( !dev_is_ethdev(dev) ) return NOTIFY_DONE;
309
310 if ((in_dev=dev->ip_ptr) != NULL) {
311 for (ifap=&in_dev->ifa_list; (ifa=*ifap) != NULL; ifap=&ifa->ifa_next)
312 if (strcmp(dev->name, ifa->ifa_label) == 0) break;
313 }
314 if ( ifa == NULL ) {
315 printk(KERN_ERR "simeth_open: can't find device %s's ifa\n", dev->name);
316 return NOTIFY_DONE;
317 }
318
319 printk(KERN_INFO "simeth_device_event: %s ipaddr=0x%x\n",
320 dev->name, ntohl(ifa->ifa_local));
321
322
323
324
325
326
327
328 local = dev->priv;
329
330 r = event == NETDEV_UP ?
331 netdev_attach(local->simfd, dev->irq, ntohl(ifa->ifa_local)):
332 netdev_detach(local->simfd);
333
334 printk(KERN_INFO "simeth: netdev_attach/detach: event=%s ->%d\n",
335 event == NETDEV_UP ? "attach":"detach", r);
336
337 return NOTIFY_DONE;
338}
339
340static int
341simeth_close(struct net_device *dev)
342{
343 netif_stop_queue(dev);
344
345 free_irq(dev->irq, dev);
346
347 return 0;
348}
349
350
351
352
353static void
354frame_print(unsigned char *from, unsigned char *frame, int len)
355{
356 int i;
357
358 printk("%s: (%d) %02x", from, len, frame[0] & 0xff);
359 for(i=1; i < 6; i++ ) {
360 printk(":%02x", frame[i] &0xff);
361 }
362 printk(" %2x", frame[6] &0xff);
363 for(i=7; i < 12; i++ ) {
364 printk(":%02x", frame[i] &0xff);
365 }
366 printk(" [%02x%02x]\n", frame[12], frame[13]);
367
368 for(i=14; i < len; i++ ) {
369 printk("%02x ", frame[i] &0xff);
370 if ( (i%10)==0) printk("\n");
371 }
372 printk("\n");
373}
374
375
376
377
378
379
380static int
381simeth_tx(struct sk_buff *skb, struct net_device *dev)
382{
383 struct simeth_local *local = dev->priv;
384
385#if 0
386
387 unsigned int length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
388
389#else
390
391
392
393 unsigned int length = skb->len;
394#endif
395
396 local->stats.tx_bytes += skb->len;
397 local->stats.tx_packets++;
398
399
400 if (simeth_debug > 5) frame_print("simeth_tx", skb->data, length);
401
402 netdev_send(local->simfd, skb->data, length);
403
404
405
406
407
408
409 dev_kfree_skb(skb);
410 return 0;
411}
412
413static inline struct sk_buff *
414make_new_skb(struct net_device *dev)
415{
416 struct sk_buff *nskb;
417
418
419
420
421
422 nskb = dev_alloc_skb(SIMETH_FRAME_SIZE + 2);
423 if ( nskb == NULL ) {
424 printk(KERN_NOTICE "%s: memory squeeze. dropping packet.\n", dev->name);
425 return NULL;
426 }
427
428 skb_reserve(nskb, 2);
429
430 skb_put(nskb,SIMETH_FRAME_SIZE);
431
432 return nskb;
433}
434
435
436
437
438static int
439simeth_rx(struct net_device *dev)
440{
441 struct simeth_local *local;
442 struct sk_buff *skb;
443 int len;
444 int rcv_count = SIMETH_RECV_MAX;
445
446 local = dev->priv;
447
448
449
450
451
452
453 do {
454 if ( (skb=make_new_skb(dev)) == NULL ) {
455 printk(KERN_NOTICE "%s: memory squeeze. dropping packet.\n", dev->name);
456 local->stats.rx_dropped++;
457 return 0;
458 }
459
460
461
462 len = netdev_read(local->simfd, skb->data, SIMETH_FRAME_SIZE);
463 if ( len == 0 ) {
464 if ( simeth_debug > 0 ) printk(KERN_WARNING "%s: count=%d netdev_read=0\n",
465 dev->name, SIMETH_RECV_MAX-rcv_count);
466 break;
467 }
468#if 0
469
470
471
472
473 skb_copy_to_linear_data(skb, frame, len);
474#endif
475 skb->protocol = eth_type_trans(skb, dev);
476
477 if ( simeth_debug > 6 ) frame_print("simeth_rx", skb->data, len);
478
479
480
481
482 netif_rx(skb);
483
484 local->stats.rx_packets++;
485 local->stats.rx_bytes += len;
486
487 } while ( --rcv_count );
488
489 return len;
490}
491
492
493
494
495static irqreturn_t
496simeth_interrupt(int irq, void *dev_id)
497{
498 struct net_device *dev = dev_id;
499
500
501
502
503 while (simeth_rx(dev));
504 return IRQ_HANDLED;
505}
506
507static struct net_device_stats *
508simeth_get_stats(struct net_device *dev)
509{
510 struct simeth_local *local = dev->priv;
511
512 return &local->stats;
513}
514
515
516static void
517set_multicast_list(struct net_device *dev)
518{
519 printk(KERN_WARNING "%s: set_multicast_list called\n", dev->name);
520}
521
522__initcall(simeth_probe);
523