ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / iseries_veth.c
1 /* File veth.c created by Kyle A. Lucke on Mon Aug  7 2000. */
2 /*
3  * IBM eServer iSeries Virtual Ethernet Device Driver
4  * Copyright (C) 2001 Kyle A. Lucke (klucke@us.ibm.com), IBM Corp.
5  * Substantially cleaned up by:
6  * Copyright (C) 2003 David Gibson <dwg@au1.ibm.com>, IBM Corporation.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21  * USA
22  *
23  *
24  * This module implements the virtual ethernet device for iSeries LPAR
25  * Linux.  It uses hypervisor message passing to implement an
26  * ethernet-like network device communicating between partitions on
27  * the iSeries.
28  *
29  * The iSeries LPAR hypervisor currently allows for up to 16 different
30  * virtual ethernets.  These are all dynamically configurable on
31  * OS/400 partitions, but dynamic configuration is not supported under
32  * Linux yet.  An ethXX network device will be created for each
33  * virtual ethernet this partition is connected to.
34  *
35  * - This driver is responsible for routing packets to and from other
36  *   partitions.  The MAC addresses used by the virtual ethernets
37  *   contains meaning and must not be modified.
38  *
39  * - Having 2 virtual ethernets to the same remote partition DOES NOT
40  *   double the available bandwidth.  The 2 devices will share the
41  *   available hypervisor bandwidth.
42  *
43  * - If you send a packet to your own mac address, it will just be
44  *   dropped, you won't get it on the receive side.
45  *
46  * - Multicast is implemented by sending the frame frame to every
47  *   other partition.  It is the responsibility of the receiving
48  *   partition to filter the addresses desired.
49  *
50  * Tunable parameters:
51  *
52  * VETH_NUMBUFFERS: This compile time option defaults to 120.  It
53  * controls how much memory Linux will allocate per remote partition
54  * it is communicating with.  It can be thought of as the maximum
55  * number of packets outstanding to a remote partition at a time.
56  */
57
58 #include <linux/config.h>
59 #include <linux/module.h>
60 #include <linux/version.h>
61 #include <linux/types.h>
62 #include <linux/errno.h>
63 #include <linux/ioport.h>
64 #include <linux/kernel.h>
65 #include <linux/netdevice.h>
66 #include <linux/etherdevice.h>
67 #include <linux/skbuff.h>
68 #include <linux/init.h>
69 #include <linux/delay.h>
70 #include <linux/mm.h>
71 #include <linux/ethtool.h>
72 #include <asm/iSeries/mf.h>
73 #include <asm/iSeries/iSeries_pci.h>
74 #include <asm/uaccess.h>
75
76 #include <asm/iSeries/HvLpConfig.h>
77 #include <asm/iSeries/HvTypes.h>
78 #include <asm/iSeries/HvLpEvent.h>
79 #include <asm/iommu.h>
80 #include <asm/vio.h>
81
82 #include "iseries_veth.h"
83
84 extern struct vio_dev *iSeries_veth_dev;
85
86 MODULE_AUTHOR("Kyle Lucke <klucke@us.ibm.com>");
87 MODULE_DESCRIPTION("iSeries Virtual ethernet driver");
88 MODULE_LICENSE("GPL");
89
90 #define VETH_NUMBUFFERS         (120)
91 #define VETH_ACKTIMEOUT         (1000000) /* microseconds */
92 #define VETH_MAX_MCAST          (12)
93
94 #define VETH_MAX_MTU            (9000)
95
96 #if VETH_NUMBUFFERS < 10
97 #define ACK_THRESHOLD           (1)
98 #elif VETH_NUMBUFFERS < 20
99 #define ACK_THRESHOLD           (4)
100 #elif VETH_NUMBUFFERS < 40
101 #define ACK_THRESHOLD           (10)
102 #else
103 #define ACK_THRESHOLD           (20)
104 #endif
105
106 #define VETH_STATE_SHUTDOWN     (0x0001)
107 #define VETH_STATE_OPEN         (0x0002)
108 #define VETH_STATE_RESET        (0x0004)
109 #define VETH_STATE_SENTMON      (0x0008)
110 #define VETH_STATE_SENTCAPS     (0x0010)
111 #define VETH_STATE_GOTCAPACK    (0x0020)
112 #define VETH_STATE_GOTCAPS      (0x0040)
113 #define VETH_STATE_SENTCAPACK   (0x0080)
114 #define VETH_STATE_READY        (0x0100)
115
116 struct veth_msg {
117         struct veth_msg *next;
118         struct VethFramesData data;
119         int token;
120         unsigned long in_use;
121         struct sk_buff *skb;
122 };
123
124 struct veth_lpar_connection {
125         HvLpIndex remote_lp;
126         struct work_struct statemachine_wq;
127         struct veth_msg *msgs;
128         int num_events;
129         struct VethCapData local_caps;
130
131         struct timer_list ack_timer;
132
133         spinlock_t lock;
134         unsigned long state;
135         HvLpInstanceId src_inst;
136         HvLpInstanceId dst_inst;
137         struct VethLpEvent cap_event, cap_ack_event;
138         u16 pending_acks[VETH_MAX_ACKS_PER_MSG];
139         u32 num_pending_acks;
140
141         int num_ack_events;
142         struct VethCapData remote_caps;
143         u32 ack_timeout;
144
145         spinlock_t msg_stack_lock;
146         struct veth_msg *msg_stack_head;
147 };
148
149 struct veth_port {
150         struct net_device_stats stats;
151         u64 mac_addr;
152         HvLpIndexMap lpar_map;
153
154         spinlock_t pending_gate;
155         struct sk_buff *pending_skb;
156         HvLpIndexMap pending_lpmask;
157
158         rwlock_t mcast_gate;
159         int promiscuous;
160         int all_mcast;
161         int num_mcast;
162         u64 mcast_addr[VETH_MAX_MCAST];
163 };
164
165 static HvLpIndex this_lp;
166 static struct veth_lpar_connection *veth_cnx[HVMAXARCHITECTEDLPS]; /* = 0 */
167 static struct net_device *veth_dev[HVMAXARCHITECTEDVIRTUALLANS]; /* = 0 */
168
169 static int veth_start_xmit(struct sk_buff *skb, struct net_device *dev);
170 static void veth_recycle_msg(struct veth_lpar_connection *, struct veth_msg *);
171 static void veth_flush_pending(struct veth_lpar_connection *cnx);
172 static void veth_receive(struct veth_lpar_connection *, struct VethLpEvent *);
173 static void veth_timed_ack(unsigned long connectionPtr);
174
175 /*
176  * Utility functions
177  */
178
179 #define veth_printk(prio, fmt, args...) \
180         printk(prio "%s: " fmt, __FILE__, ## args)
181
182 #define veth_error(fmt, args...) \
183         printk(KERN_ERR "(%s:%3.3d) ERROR: " fmt, __FILE__, __LINE__ , ## args)
184
185 static inline void veth_stack_push(struct veth_lpar_connection *cnx,
186                                    struct veth_msg *msg)
187 {
188         unsigned long flags;
189
190         spin_lock_irqsave(&cnx->msg_stack_lock, flags);
191         msg->next = cnx->msg_stack_head;
192         cnx->msg_stack_head = msg;
193         spin_unlock_irqrestore(&cnx->msg_stack_lock, flags);
194 }
195
196 static inline struct veth_msg *veth_stack_pop(struct veth_lpar_connection *cnx)
197 {
198         unsigned long flags;
199         struct veth_msg *msg;
200
201         spin_lock_irqsave(&cnx->msg_stack_lock, flags);
202         msg = cnx->msg_stack_head;
203         if (msg)
204                 cnx->msg_stack_head = cnx->msg_stack_head->next;
205         spin_unlock_irqrestore(&cnx->msg_stack_lock, flags);
206         return msg;
207 }
208
209 static inline HvLpEvent_Rc
210 veth_signalevent(struct veth_lpar_connection *cnx, u16 subtype,
211                  HvLpEvent_AckInd ackind, HvLpEvent_AckType acktype,
212                  u64 token,
213                  u64 data1, u64 data2, u64 data3, u64 data4, u64 data5)
214 {
215         return HvCallEvent_signalLpEventFast(cnx->remote_lp,
216                                              HvLpEvent_Type_VirtualLan,
217                                              subtype, ackind, acktype,
218                                              cnx->src_inst,
219                                              cnx->dst_inst,
220                                              token, data1, data2, data3,
221                                              data4, data5);
222 }
223
224 static inline HvLpEvent_Rc veth_signaldata(struct veth_lpar_connection *cnx,
225                                            u16 subtype, u64 token, void *data)
226 {
227         u64 *p = (u64 *) data;
228
229         return veth_signalevent(cnx, subtype, HvLpEvent_AckInd_NoAck,
230                                 HvLpEvent_AckType_ImmediateAck,
231                                 token, p[0], p[1], p[2], p[3], p[4]);
232 }
233
234 struct veth_allocation {
235         struct completion c;
236         int num;
237 };
238
239 static void veth_complete_allocation(void *parm, int number)
240 {
241         struct veth_allocation *vc = (struct veth_allocation *)parm;
242
243         vc->num = number;
244         complete(&vc->c);
245 }
246
247 static int veth_allocate_events(HvLpIndex rlp, int number)
248 {
249         struct veth_allocation vc = { COMPLETION_INITIALIZER(vc.c), 0 };
250
251         mf_allocateLpEvents(rlp, HvLpEvent_Type_VirtualLan,
252                             sizeof(struct VethLpEvent), number,
253                             &veth_complete_allocation, &vc);
254         wait_for_completion(&vc.c);
255
256         return vc.num;
257 }
258
259 /*
260  * LPAR connection code
261  */
262
263 static inline void veth_kick_statemachine(struct veth_lpar_connection *cnx)
264 {
265         schedule_work(&cnx->statemachine_wq);
266 }
267
268 static void veth_take_cap(struct veth_lpar_connection *cnx,
269                           struct VethLpEvent *event)
270 {
271         unsigned long flags;
272
273         spin_lock_irqsave(&cnx->lock, flags);
274         /* Receiving caps may mean the other end has just come up, so
275          * we need to reload the instance ID of the far end */
276         cnx->dst_inst =
277                 HvCallEvent_getTargetLpInstanceId(cnx->remote_lp,
278                                                   HvLpEvent_Type_VirtualLan);
279
280         if (cnx->state & VETH_STATE_GOTCAPS) {
281                 veth_error("Received a second capabilities from lpar %d\n",
282                            cnx->remote_lp);
283                 event->base_event.xRc = HvLpEvent_Rc_BufferNotAvailable;
284                 HvCallEvent_ackLpEvent((struct HvLpEvent *) event);
285         } else {
286                 memcpy(&cnx->cap_event, event, sizeof(cnx->cap_event));
287                 cnx->state |= VETH_STATE_GOTCAPS;
288                 veth_kick_statemachine(cnx);
289         }
290         spin_unlock_irqrestore(&cnx->lock, flags);
291 }
292
293 static void veth_take_cap_ack(struct veth_lpar_connection *cnx,
294                               struct VethLpEvent *event)
295 {
296         unsigned long flags;
297
298         spin_lock_irqsave(&cnx->lock, flags);
299         if (cnx->state & VETH_STATE_GOTCAPACK) {
300                 veth_error("Received a second capabilities ack from lpar %d\n",
301                            cnx->remote_lp);
302         } else {
303                 memcpy(&cnx->cap_ack_event, event,
304                        sizeof(&cnx->cap_ack_event));
305                 cnx->state |= VETH_STATE_GOTCAPACK;
306                 veth_kick_statemachine(cnx);
307         }
308         spin_unlock_irqrestore(&cnx->lock, flags);
309 }
310
311 static void veth_take_monitor_ack(struct veth_lpar_connection *cnx,
312                                   struct VethLpEvent *event)
313 {
314         unsigned long flags;
315
316         spin_lock_irqsave(&cnx->lock, flags);
317         veth_printk(KERN_DEBUG, "Monitor ack returned for lpar %d\n",
318                     cnx->remote_lp);
319         cnx->state |= VETH_STATE_RESET;
320         veth_kick_statemachine(cnx);
321         spin_unlock_irqrestore(&cnx->lock, flags);
322 }
323
324 static void veth_handle_ack(struct VethLpEvent *event)
325 {
326         HvLpIndex rlp = event->base_event.xTargetLp;
327         struct veth_lpar_connection *cnx = veth_cnx[rlp];
328
329         BUG_ON(! cnx);
330
331         switch (event->base_event.xSubtype) {
332         case VethEventTypeCap:
333                 veth_take_cap_ack(cnx, event);
334                 break;
335         case VethEventTypeMonitor:
336                 veth_take_monitor_ack(cnx, event);
337                 break;
338         default:
339                 veth_error("Unknown ack type %d from lpar %d\n",
340                            event->base_event.xSubtype, rlp);
341         };
342 }
343
344 static void veth_handle_int(struct VethLpEvent *event)
345 {
346         HvLpIndex rlp = event->base_event.xSourceLp;
347         struct veth_lpar_connection *cnx = veth_cnx[rlp];
348         unsigned long flags;
349         int i;
350
351         BUG_ON(! cnx);
352
353         switch (event->base_event.xSubtype) {
354         case VethEventTypeCap:
355                 veth_take_cap(cnx, event);
356                 break;
357         case VethEventTypeMonitor:
358                 /* do nothing... this'll hang out here til we're dead,
359                  * and the hypervisor will return it for us. */
360                 break;
361         case VethEventTypeFramesAck:
362                 spin_lock_irqsave(&cnx->lock, flags);
363                 for (i = 0; i < VETH_MAX_ACKS_PER_MSG; ++i) {
364                         u16 msgnum = event->u.frames_ack_data.token[i];
365
366                         if (msgnum < VETH_NUMBUFFERS)
367                                 veth_recycle_msg(cnx, cnx->msgs + msgnum);
368                 }
369                 spin_unlock_irqrestore(&cnx->lock, flags);
370                 veth_flush_pending(cnx);
371                 break;
372         case VethEventTypeFrames:
373                 veth_receive(cnx, event);
374                 break;
375         default:
376                 veth_error("Unknown interrupt type %d from lpar %d\n",
377                            event->base_event.xSubtype, rlp);
378         };
379 }
380
381 static void veth_handle_event(struct HvLpEvent *event, struct pt_regs *regs)
382 {
383         struct VethLpEvent *veth_event = (struct VethLpEvent *)event;
384
385         if (event->xFlags.xFunction == HvLpEvent_Function_Ack)
386                 veth_handle_ack(veth_event);
387         else if (event->xFlags.xFunction == HvLpEvent_Function_Int)
388                 veth_handle_int(veth_event);
389 }
390
391 static int veth_process_caps(struct veth_lpar_connection *cnx)
392 {
393         struct VethCapData *remote_caps = &cnx->remote_caps;
394         int num_acks_needed;
395
396         /* Convert timer to jiffies */
397         cnx->ack_timeout = remote_caps->ack_timeout * HZ / 1000000;
398
399         if ( (remote_caps->num_buffers == 0)
400              || (remote_caps->ack_threshold > VETH_MAX_ACKS_PER_MSG)
401              || (remote_caps->ack_threshold == 0)
402              || (cnx->ack_timeout == 0) ) {
403                 veth_error("Received incompatible capabilities from lpar %d\n",
404                            cnx->remote_lp);
405                 return HvLpEvent_Rc_InvalidSubtypeData;
406         }
407
408         num_acks_needed = (remote_caps->num_buffers
409                            / remote_caps->ack_threshold) + 1;
410
411         /* FIXME: locking on num_ack_events? */
412         if (cnx->num_ack_events < num_acks_needed) {
413                 int num;
414
415                 num = veth_allocate_events(cnx->remote_lp,
416                                            num_acks_needed-cnx->num_ack_events);
417                 if (num > 0)
418                         cnx->num_ack_events += num;
419
420                 if (cnx->num_ack_events < num_acks_needed) {
421                         veth_error("Couldn't allocate enough ack events for lpar %d\n",
422                                    cnx->remote_lp);
423
424                         return HvLpEvent_Rc_BufferNotAvailable;
425                 }
426         }
427
428
429         return HvLpEvent_Rc_Good;
430 }
431
432 /* FIXME: The gotos here are a bit dubious */
433 static void veth_statemachine(void *p)
434 {
435         struct veth_lpar_connection *cnx = (struct veth_lpar_connection *)p;
436         int rlp = cnx->remote_lp;
437         int rc;
438
439         spin_lock_irq(&cnx->lock);
440
441  restart:
442         if (cnx->state & VETH_STATE_RESET) {
443                 int i;
444
445                 del_timer(&cnx->ack_timer);
446
447                 if (cnx->state & VETH_STATE_OPEN)
448                         HvCallEvent_closeLpEventPath(cnx->remote_lp,
449                                                      HvLpEvent_Type_VirtualLan);
450
451                 /* reset ack data */
452                 memset(&cnx->pending_acks, 0xff, sizeof (cnx->pending_acks));
453                 cnx->num_pending_acks = 0;
454
455                 cnx->state &= ~(VETH_STATE_RESET | VETH_STATE_SENTMON
456                                 | VETH_STATE_OPEN | VETH_STATE_SENTCAPS
457                                 | VETH_STATE_GOTCAPACK | VETH_STATE_GOTCAPS
458                                 | VETH_STATE_SENTCAPACK | VETH_STATE_READY);
459
460                 /* Clean up any leftover messages */
461                 if (cnx->msgs)
462                         for (i = 0; i < VETH_NUMBUFFERS; ++i)
463                                 veth_recycle_msg(cnx, cnx->msgs + i);
464         }
465
466         if (cnx->state & VETH_STATE_SHUTDOWN)
467                 /* It's all over, do nothing */
468                 goto out;
469
470         if ( !(cnx->state & VETH_STATE_OPEN) ) {
471                 if (! cnx->msgs || (cnx->num_events < (2 + VETH_NUMBUFFERS)) )
472                         goto cant_cope;
473
474                 HvCallEvent_openLpEventPath(rlp, HvLpEvent_Type_VirtualLan);
475                 cnx->src_inst =
476                         HvCallEvent_getSourceLpInstanceId(rlp,
477                                                           HvLpEvent_Type_VirtualLan);
478                 cnx->dst_inst =
479                         HvCallEvent_getTargetLpInstanceId(rlp,
480                                                           HvLpEvent_Type_VirtualLan);
481                 cnx->state |= VETH_STATE_OPEN;
482         }
483
484         if ( (cnx->state & VETH_STATE_OPEN)
485              && !(cnx->state & VETH_STATE_SENTMON) ) {
486                 rc = veth_signalevent(cnx, VethEventTypeMonitor,
487                                       HvLpEvent_AckInd_DoAck,
488                                       HvLpEvent_AckType_DeferredAck,
489                                       0, 0, 0, 0, 0, 0);
490
491                 if (rc == HvLpEvent_Rc_Good) {
492                         cnx->state |= VETH_STATE_SENTMON;
493                 } else {
494                         if ( (rc != HvLpEvent_Rc_PartitionDead)
495                              && (rc != HvLpEvent_Rc_PathClosed) )
496                                 veth_error("Error sending monitor to "
497                                            "lpar %d, rc=%x\n",
498                                            rlp, (int) rc);
499
500                         /* Oh well, hope we get a cap from the other
501                          * end and do better when that kicks us */
502                         goto out;
503                 }
504         }
505
506         if ( (cnx->state & VETH_STATE_OPEN)
507              && !(cnx->state & VETH_STATE_SENTCAPS)) {
508                 u64 *rawcap = (u64 *)&cnx->local_caps;
509
510                 rc = veth_signalevent(cnx, VethEventTypeCap,
511                                       HvLpEvent_AckInd_DoAck,
512                                       HvLpEvent_AckType_ImmediateAck,
513                                       0, rawcap[0], rawcap[1], rawcap[2],
514                                       rawcap[3], rawcap[4]);
515
516                 if (rc == HvLpEvent_Rc_Good) {
517                         cnx->state |= VETH_STATE_SENTCAPS;
518                 } else {
519                         if ( (rc != HvLpEvent_Rc_PartitionDead)
520                              && (rc != HvLpEvent_Rc_PathClosed) )
521                                 veth_error("Error sending caps to "
522                                            "lpar %d, rc=%x\n",
523                                            rlp, (int) rc);
524                         /* Oh well, hope we get a cap from the other
525                          * end and do better when that kicks us */
526                         goto out;
527                 }
528         }
529
530         if ((cnx->state & VETH_STATE_GOTCAPS)
531             && !(cnx->state & VETH_STATE_SENTCAPACK)) {
532                 struct VethCapData *remote_caps = &cnx->remote_caps;
533
534                 memcpy(remote_caps, &cnx->cap_event.u.caps_data,
535                        sizeof(*remote_caps));
536
537                 spin_unlock_irq(&cnx->lock);
538                 rc = veth_process_caps(cnx);
539                 spin_lock_irq(&cnx->lock);
540
541                 /* We dropped the lock, so recheck for anything which
542                  * might mess us up */
543                 if (cnx->state & (VETH_STATE_RESET|VETH_STATE_SHUTDOWN))
544                         goto restart;
545
546                 cnx->cap_event.base_event.xRc = rc;
547                 HvCallEvent_ackLpEvent((struct HvLpEvent *)&cnx->cap_event);
548                 if (rc == HvLpEvent_Rc_Good)
549                         cnx->state |= VETH_STATE_SENTCAPACK;
550                 else
551                         goto cant_cope;
552         }
553
554         if ((cnx->state & VETH_STATE_GOTCAPACK)
555             && (cnx->state & VETH_STATE_GOTCAPS)
556             && !(cnx->state & VETH_STATE_READY)) {
557                 if (cnx->cap_ack_event.base_event.xRc == HvLpEvent_Rc_Good) {
558                         /* Start the ACK timer */
559                         cnx->ack_timer.expires = jiffies + cnx->ack_timeout;
560                         add_timer(&cnx->ack_timer);
561                         cnx->state |= VETH_STATE_READY;
562                 } else {
563                         veth_printk(KERN_ERR, "Caps rejected (rc=%d) by "
564                                     "lpar %d\n",
565                                     cnx->cap_ack_event.base_event.xRc,
566                                     rlp);
567                         goto cant_cope;
568                 }
569         }
570
571  out:
572         spin_unlock_irq(&cnx->lock);
573         return;
574
575  cant_cope:
576         /* FIXME: we get here if something happens we really can't
577          * cope with.  The link will never work once we get here, and
578          * all we can do is not lock the rest of the system up */
579         veth_error("Badness on connection to lpar %d (state=%04lx) "
580                    " - shutting down\n", rlp, cnx->state);
581         cnx->state |= VETH_STATE_SHUTDOWN;
582         spin_unlock_irq(&cnx->lock);
583 }
584
585 static int veth_init_connection(u8 rlp)
586 {
587         struct veth_lpar_connection *cnx;
588         struct veth_msg *msgs;
589         int i;
590
591         if ( (rlp == this_lp)
592              || ! HvLpConfig_doLpsCommunicateOnVirtualLan(this_lp, rlp) )
593                 return 0;
594
595         cnx = kmalloc(sizeof(*cnx), GFP_KERNEL);
596         if (! cnx)
597                 return -ENOMEM;
598         memset(cnx, 0, sizeof(*cnx));
599
600         cnx->remote_lp = rlp;
601         spin_lock_init(&cnx->lock);
602         INIT_WORK(&cnx->statemachine_wq, veth_statemachine, cnx);
603         init_timer(&cnx->ack_timer);
604         cnx->ack_timer.function = veth_timed_ack;
605         cnx->ack_timer.data = (unsigned long) cnx;
606         memset(&cnx->pending_acks, 0xff, sizeof (cnx->pending_acks));
607
608         veth_cnx[rlp] = cnx;
609
610         msgs = kmalloc(VETH_NUMBUFFERS * sizeof(struct veth_msg), GFP_KERNEL);
611         if (! msgs) {
612                 veth_error("Can't allocate buffers for lpar %d\n", rlp);
613                 return -ENOMEM;
614         }
615
616         cnx->msgs = msgs;
617         memset(msgs, 0, VETH_NUMBUFFERS * sizeof(struct veth_msg));
618         spin_lock_init(&cnx->msg_stack_lock);
619
620         for (i = 0; i < VETH_NUMBUFFERS; i++) {
621                 msgs[i].token = i;
622                 veth_stack_push(cnx, msgs + i);
623         }
624
625         cnx->num_events = veth_allocate_events(rlp, 2 + VETH_NUMBUFFERS);
626
627         if (cnx->num_events < (2 + VETH_NUMBUFFERS)) {
628                 veth_error("Can't allocate events for lpar %d, only got %d\n",
629                            rlp, cnx->num_events);
630                 return -ENOMEM;
631         }
632
633         cnx->local_caps.num_buffers = VETH_NUMBUFFERS;
634         cnx->local_caps.ack_threshold = ACK_THRESHOLD;
635         cnx->local_caps.ack_timeout = VETH_ACKTIMEOUT;
636
637         return 0;
638 }
639
640 static void veth_destroy_connection(u8 rlp)
641 {
642         struct veth_lpar_connection *cnx = veth_cnx[rlp];
643
644         if (! cnx)
645                 return;
646
647         spin_lock_irq(&cnx->lock);
648         cnx->state |= VETH_STATE_RESET | VETH_STATE_SHUTDOWN;
649         veth_kick_statemachine(cnx);
650         spin_unlock_irq(&cnx->lock);
651
652         flush_scheduled_work();
653
654         /* FIXME: not sure if this is necessary - will already have
655          * been deleted by the state machine, just want to make sure
656          * its not running any more */
657         del_timer_sync(&cnx->ack_timer);
658
659         if (cnx->num_events > 0)
660                 mf_deallocateLpEvents(cnx->remote_lp,
661                                       HvLpEvent_Type_VirtualLan,
662                                       cnx->num_events,
663                                       NULL, NULL);
664         if (cnx->num_ack_events > 0)
665                 mf_deallocateLpEvents(cnx->remote_lp,
666                                       HvLpEvent_Type_VirtualLan,
667                                       cnx->num_ack_events,
668                                       NULL, NULL);
669
670         if (cnx->msgs)
671                 kfree(cnx->msgs);
672 }
673
674 /*
675  * net_device code
676  */
677
678 static int veth_open(struct net_device *dev)
679 {
680         struct veth_port *port = (struct veth_port *) dev->priv;
681
682         memset(&port->stats, 0, sizeof (port->stats));
683         netif_start_queue(dev);
684         return 0;
685 }
686
687 static int veth_close(struct net_device *dev)
688 {
689         netif_stop_queue(dev);
690         return 0;
691 }
692
693 static struct net_device_stats *veth_get_stats(struct net_device *dev)
694 {
695         struct veth_port *port = (struct veth_port *) dev->priv;
696
697         return &port->stats;
698 }
699
700 static int veth_change_mtu(struct net_device *dev, int new_mtu)
701 {
702         if ((new_mtu < 68) || (new_mtu > VETH_MAX_MTU))
703                 return -EINVAL;
704         dev->mtu = new_mtu;
705         return 0;
706 }
707
708 static void veth_set_multicast_list(struct net_device *dev)
709 {
710         struct veth_port *port = (struct veth_port *) dev->priv;
711         unsigned long flags;
712
713         write_lock_irqsave(&port->mcast_gate, flags);
714
715         if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
716                 printk(KERN_INFO "%s: Promiscuous mode enabled.\n",
717                        dev->name);
718                 port->promiscuous = 1;
719         } else if ( (dev->flags & IFF_ALLMULTI)
720                     || (dev->mc_count > VETH_MAX_MCAST) ) {
721                 port->all_mcast = 1;
722         } else {
723                 struct dev_mc_list *dmi = dev->mc_list;
724                 int i;
725
726                 /* Update table */
727                 port->num_mcast = 0;
728
729                 for (i = 0; i < dev->mc_count; i++) {
730                         u8 *addr = dmi->dmi_addr;
731                         u64 xaddr = 0;
732
733                         if (addr[0] & 0x01) {/* multicast address? */
734                                 memcpy(&xaddr, addr, ETH_ALEN);
735                                 port->mcast_addr[port->num_mcast] = xaddr;
736                                 port->num_mcast++;
737                         }
738                         dmi = dmi->next;
739                 }
740         }
741
742         write_unlock_irqrestore(&port->mcast_gate, flags);
743 }
744
745 static int veth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
746 {
747 #ifdef SIOCETHTOOL
748         struct ethtool_cmd ecmd;
749
750         if (cmd != SIOCETHTOOL)
751                 return -EOPNOTSUPP;
752         if (copy_from_user(&ecmd, ifr->ifr_data, sizeof (ecmd)))
753                 return -EFAULT;
754         switch (ecmd.cmd) {
755         case ETHTOOL_GSET:
756                 ecmd.supported = (SUPPORTED_1000baseT_Full
757                                   | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
758                 ecmd.advertising = (SUPPORTED_1000baseT_Full
759                                     | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
760
761                 ecmd.port = PORT_FIBRE;
762                 ecmd.transceiver = XCVR_INTERNAL;
763                 ecmd.phy_address = 0;
764                 ecmd.speed = SPEED_1000;
765                 ecmd.duplex = DUPLEX_FULL;
766                 ecmd.autoneg = AUTONEG_ENABLE;
767                 ecmd.maxtxpkt = 120;
768                 ecmd.maxrxpkt = 120;
769                 if (copy_to_user(ifr->ifr_data, &ecmd, sizeof(ecmd)))
770                         return -EFAULT;
771                 return 0;
772
773         case ETHTOOL_GDRVINFO:{
774                         struct ethtool_drvinfo info = { ETHTOOL_GDRVINFO };
775                         strncpy(info.driver, "veth", sizeof(info.driver) - 1);
776                         info.driver[sizeof(info.driver) - 1] = '\0';
777                         strncpy(info.version, "1.0", sizeof(info.version) - 1);
778                         if (copy_to_user(ifr->ifr_data, &info, sizeof(info)))
779                                 return -EFAULT;
780                         return 0;
781                 }
782                 /* get link status */
783         case ETHTOOL_GLINK:{
784                         struct ethtool_value edata = { ETHTOOL_GLINK };
785                         edata.data = 1;
786                         if (copy_to_user(ifr->ifr_data, &edata, sizeof(edata)))
787                                 return -EFAULT;
788                         return 0;
789                 }
790
791         default:
792                 break;
793         }
794
795 #endif
796         return -EOPNOTSUPP;
797 }
798
799 struct net_device * __init veth_probe_one(int vlan)
800 {
801         struct net_device *dev;
802         struct veth_port *port;
803         int i, rc;
804
805         dev = alloc_etherdev(sizeof (struct veth_port));
806         if (! dev) {
807                 veth_error("Unable to allocate net_device structure!\n");
808                 return NULL;
809         }
810
811         port = (struct veth_port *) dev->priv;
812
813         spin_lock_init(&port->pending_gate);
814         rwlock_init(&port->mcast_gate);
815
816         for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
817                 HvLpVirtualLanIndexMap map;
818
819                 if (i == this_lp)
820                         continue;
821                 map = HvLpConfig_getVirtualLanIndexMapForLp(i);
822                 if (map & (0x8000 >> vlan))
823                         port->lpar_map |= (1 << i);
824         }
825
826         dev->dev_addr[0] = 0x02;
827         dev->dev_addr[1] = 0x01;
828         dev->dev_addr[2] = 0xff;
829         dev->dev_addr[3] = vlan;
830         dev->dev_addr[4] = 0xff;
831         dev->dev_addr[5] = this_lp;
832
833         dev->mtu = VETH_MAX_MTU;
834
835         memcpy(&port->mac_addr, dev->dev_addr, 6);
836
837         dev->open = veth_open;
838         dev->hard_start_xmit = veth_start_xmit;
839         dev->stop = veth_close;
840         dev->get_stats = veth_get_stats;
841         dev->change_mtu = veth_change_mtu;
842         dev->set_mac_address = NULL;
843         dev->set_multicast_list = veth_set_multicast_list;
844         dev->do_ioctl = veth_ioctl;
845
846         rc = register_netdev(dev);
847         if (rc != 0) {
848                 veth_printk(KERN_ERR,
849                             "Failed to register ethernet device for vlan %d\n",
850                             vlan);
851                 free_netdev(dev);
852                 return NULL;
853         }
854
855         veth_printk(KERN_DEBUG, "%s attached to iSeries vlan %d (lpar_map=0x%04x)\n",
856                     dev->name, vlan, port->lpar_map);
857
858         return dev;
859 }
860
861 /*
862  * Tx path
863  */
864
865 static int veth_transmit_to_one(struct sk_buff *skb, HvLpIndex rlp,
866                                 struct net_device *dev)
867 {
868         struct veth_lpar_connection *cnx = veth_cnx[rlp];
869         struct veth_port *port = (struct veth_port *) dev->priv;
870         HvLpEvent_Rc rc;
871         u32 dma_address, dma_length;
872         struct veth_msg *msg = NULL;
873         int err = 0;
874         unsigned long flags;
875
876         if (! cnx) {
877                 port->stats.tx_errors++;
878                 dev_kfree_skb(skb);
879                 return 0;
880         }
881
882         spin_lock_irqsave(&cnx->lock, flags);
883
884         if (! cnx->state & VETH_STATE_READY)
885                 goto drop;
886
887         if ((skb->len - 14) > VETH_MAX_MTU)
888                 goto drop;
889
890         msg = veth_stack_pop(cnx);
891
892         if (! msg) {
893                 err = 1;
894                 goto drop;
895         }
896
897         dma_length = skb->len;
898         dma_address = vio_map_single(iSeries_veth_dev, skb->data,
899                                      dma_length, DMA_TO_DEVICE);
900
901         if (dma_mapping_error(dma_address))
902                 goto recycle_and_drop;
903
904         /* Is it really necessary to check the length and address
905          * fields of the first entry here? */
906         msg->skb = skb;
907         msg->data.addr[0] = dma_address;
908         msg->data.len[0] = dma_length;
909         msg->data.eofmask = 1 << VETH_EOF_SHIFT;
910         set_bit(0, &(msg->in_use));
911         rc = veth_signaldata(cnx, VethEventTypeFrames, msg->token, &msg->data);
912
913         if (rc != HvLpEvent_Rc_Good)
914                 goto recycle_and_drop;
915
916         spin_unlock_irqrestore(&cnx->lock, flags);
917         return 0;
918
919  recycle_and_drop:
920         msg->skb = NULL;
921         /* need to set in use to make veth_recycle_msg in case this
922          * was a mapping failure */
923         set_bit(0, &msg->in_use);
924         veth_recycle_msg(cnx, msg);
925  drop:
926         port->stats.tx_errors++;
927         dev_kfree_skb(skb);
928         spin_unlock_irqrestore(&cnx->lock, flags);
929         return err;
930 }
931
932 static HvLpIndexMap veth_transmit_to_many(struct sk_buff *skb,
933                                           HvLpIndexMap lpmask,
934                                           struct net_device *dev)
935 {
936         struct veth_port *port = (struct veth_port *) dev->priv;
937         int i;
938         int rc;
939
940         for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
941                 struct sk_buff *clone;
942
943                 if (! lpmask & (1<<i))
944                         continue;
945
946                 clone = skb_clone(skb, GFP_ATOMIC);
947                 if (! clone) {
948                         veth_error("%s: skb_clone failed %p\n",
949                                    dev->name, skb);
950                         continue;
951                 }
952
953                 rc = veth_transmit_to_one(clone, i, dev);
954                 if (! rc)
955                         lpmask &= ~(1<<i);
956         }
957
958         if (! lpmask) {
959                 port->stats.tx_packets++;
960                 port->stats.tx_bytes += skb->len;
961         }
962
963         return lpmask;
964 }
965
966 static int veth_start_xmit(struct sk_buff *skb, struct net_device *dev)
967 {
968         unsigned char *frame = skb->data;
969         struct veth_port *port = (struct veth_port *) dev->priv;
970         unsigned long flags;
971         HvLpIndexMap lpmask;
972
973         if (! (frame[0] & 0x01)) {
974                 /* unicast packet */
975                 HvLpIndex rlp = frame[5];
976
977                 if ( ! ((1 << rlp) & port->lpar_map) ) {
978                         dev_kfree_skb(skb);
979                         return 0;
980                 }
981
982                 lpmask = 1 << rlp;
983         } else {
984                 lpmask = port->lpar_map;
985         }
986
987         lpmask = veth_transmit_to_many(skb, lpmask, dev);
988
989         if (! lpmask) {
990                 dev_kfree_skb(skb);
991         } else {
992                 spin_lock_irqsave(&port->pending_gate, flags);
993                 if (port->pending_skb) {
994                         veth_error("%s: Tx while skb was pending!\n",
995                                    dev->name);
996                         dev_kfree_skb(skb);
997                         return 1;
998                 }
999
1000                 port->pending_skb = skb;
1001                 port->pending_lpmask = lpmask;
1002                 netif_stop_queue(dev);
1003
1004                 spin_unlock_irqrestore(&port->pending_gate, flags);
1005         }
1006
1007         return 0;
1008 }
1009
1010 static void veth_recycle_msg(struct veth_lpar_connection *cnx,
1011                              struct veth_msg *msg)
1012 {
1013         u32 dma_address, dma_length;
1014
1015         if (test_and_clear_bit(0, &msg->in_use)) {
1016                 dma_address = msg->data.addr[0];
1017                 dma_length = msg->data.len[0];
1018
1019                 vio_unmap_single(iSeries_veth_dev, dma_address, dma_length,
1020                                  DMA_TO_DEVICE);
1021
1022                 if (msg->skb) {
1023                         dev_kfree_skb_any(msg->skb);
1024                         msg->skb = NULL;
1025                 }
1026
1027                 memset(&msg->data, 0, sizeof(msg->data));
1028                 veth_stack_push(cnx, msg);
1029         } else
1030                 if (cnx->state & VETH_STATE_OPEN)
1031                         veth_error("Bogus frames ack from lpar %d (#%d)\n",
1032                                    cnx->remote_lp, msg->token);
1033 }
1034
1035 static void veth_flush_pending(struct veth_lpar_connection *cnx)
1036 {
1037         int i;
1038         for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++) {
1039                 struct net_device *dev = veth_dev[i];
1040                 struct veth_port *port;
1041                 unsigned long flags;
1042
1043                 if (! dev)
1044                         continue;
1045
1046                 port = (struct veth_port *)dev->priv;
1047
1048                 if (! (port->lpar_map & (1<<cnx->remote_lp)))
1049                         continue;
1050
1051                 spin_lock_irqsave(&port->pending_gate, flags);
1052                 if (port->pending_skb) {
1053                         port->pending_lpmask =
1054                                 veth_transmit_to_many(port->pending_skb,
1055                                                       port->pending_lpmask,
1056                                                       dev);
1057                         if (! port->pending_lpmask) {
1058                                 dev_kfree_skb_any(port->pending_skb);
1059                                 port->pending_skb = NULL;
1060                                 netif_start_queue(dev);
1061                         }
1062                 }
1063                 spin_unlock_irqrestore(&port->pending_gate, flags);
1064         }
1065 }
1066
1067 /*
1068  * Rx path
1069  */
1070
1071 static inline int veth_frame_wanted(struct veth_port *port, u64 mac_addr)
1072 {
1073         int wanted = 0;
1074         int i;
1075         unsigned long flags;
1076
1077         if ( (mac_addr == port->mac_addr) || (mac_addr == 0xffffffffffff0000) )
1078                 return 1;
1079
1080         if (! (((char *) &mac_addr)[0] & 0x01))
1081                 return 0;
1082
1083         read_lock_irqsave(&port->mcast_gate, flags);
1084
1085         if (port->promiscuous || port->all_mcast) {
1086                 wanted = 1;
1087                 goto out;
1088         }
1089
1090         for (i = 0; i < port->num_mcast; ++i) {
1091                 if (port->mcast_addr[i] == mac_addr) {
1092                         wanted = 1;
1093                         break;
1094                 }
1095         }
1096
1097  out:
1098         read_unlock_irqrestore(&port->mcast_gate, flags);
1099
1100         return wanted;
1101 }
1102
1103 struct dma_chunk {
1104         u64 addr;
1105         u64 size;
1106 };
1107
1108 #define VETH_MAX_PAGES_PER_FRAME ( (VETH_MAX_MTU+PAGE_SIZE-2)/PAGE_SIZE + 1 )
1109
1110 static inline void veth_build_dma_list(struct dma_chunk *list,
1111                                        unsigned char *p, unsigned long length)
1112 {
1113         unsigned long done;
1114         int i = 1;
1115
1116         /* FIXME: skbs are continguous in real addresses.  Do we
1117          * really need to break it into PAGE_SIZE chunks, or can we do
1118          * it just at the granularity of iSeries real->absolute
1119          * mapping?  Indeed, given the way the allocator works, can we
1120          * count on them being absolutely contiguous? */
1121         list[0].addr = ISERIES_HV_ADDR(p);
1122         list[0].size = min(length,
1123                            PAGE_SIZE - ((unsigned long)p & ~PAGE_MASK));
1124
1125         done = list[0].size;
1126         while (done < length) {
1127                 list[i].addr = ISERIES_HV_ADDR(p + done);
1128                 list[i].size = min(length-done, PAGE_SIZE);
1129                 done += list[i].size;
1130                 i++;
1131         }
1132 }
1133
1134 static void veth_flush_acks(struct veth_lpar_connection *cnx)
1135 {
1136         HvLpEvent_Rc rc;
1137
1138         rc = veth_signaldata(cnx, VethEventTypeFramesAck,
1139                              0, &cnx->pending_acks);
1140
1141         if (rc != HvLpEvent_Rc_Good)
1142                 veth_error("Error 0x%x acking frames from lpar %d!\n",
1143                            (unsigned)rc, cnx->remote_lp);
1144
1145         cnx->num_pending_acks = 0;
1146         memset(&cnx->pending_acks, 0xff, sizeof(cnx->pending_acks));
1147 }
1148
1149 static void veth_receive(struct veth_lpar_connection *cnx,
1150                          struct VethLpEvent *event)
1151 {
1152         struct VethFramesData *senddata = &event->u.frames_data;
1153         int startchunk = 0;
1154         int nchunks;
1155         unsigned long flags;
1156         HvLpDma_Rc rc;
1157
1158         do {
1159                 u16 length = 0;
1160                 struct sk_buff *skb;
1161                 struct dma_chunk local_list[VETH_MAX_PAGES_PER_FRAME];
1162                 struct dma_chunk remote_list[VETH_MAX_FRAMES_PER_MSG];
1163                 u64 dest;
1164                 HvLpVirtualLanIndex vlan;
1165                 struct net_device *dev;
1166                 struct veth_port *port;
1167
1168                 /* FIXME: do we need this? */
1169                 memset(local_list, 0, sizeof(local_list));
1170                 memset(remote_list, 0, sizeof(VETH_MAX_FRAMES_PER_MSG));
1171
1172                 /* a 0 address marks the end of the valid entries */
1173                 if (senddata->addr[startchunk] == 0)
1174                         break;
1175
1176                 /* make sure that we have at least 1 EOF entry in the
1177                  * remaining entries */
1178                 if (! (senddata->eofmask >> (startchunk + VETH_EOF_SHIFT))) {
1179                         veth_error("missing EOF frag in event "
1180                                    "eofmask=0x%x startchunk=%d\n",
1181                                    (unsigned) senddata->eofmask, startchunk);
1182                         break;
1183                 }
1184
1185                 /* build list of chunks in this frame */
1186                 nchunks = 0;
1187                 do {
1188                         remote_list[nchunks].addr =
1189                                 (u64) senddata->addr[startchunk+nchunks] << 32;
1190                         remote_list[nchunks].size =
1191                                 senddata->len[startchunk+nchunks];
1192                         length += remote_list[nchunks].size;
1193                 } while (! (senddata->eofmask &
1194                             (1 << (VETH_EOF_SHIFT + startchunk + nchunks++))));
1195
1196                 /* length == total length of all chunks */
1197                 /* nchunks == # of chunks in this frame */
1198
1199                 if ((length - ETH_HLEN) > VETH_MAX_MTU) {
1200                         veth_error("Received oversize frame from lpar %d "
1201                                    "(length=%d)\n", cnx->remote_lp, length);
1202                         continue;
1203                 }
1204
1205                 skb = alloc_skb(length, GFP_ATOMIC);
1206                 if (!skb)
1207                         continue;
1208
1209                 veth_build_dma_list(local_list, skb->data, length);
1210
1211                 rc = HvCallEvent_dmaBufList(HvLpEvent_Type_VirtualLan,
1212                                             event->base_event.xSourceLp,
1213                                             HvLpDma_Direction_RemoteToLocal,
1214                                             cnx->src_inst,
1215                                             cnx->dst_inst,
1216                                             HvLpDma_AddressType_RealAddress,
1217                                             HvLpDma_AddressType_TceIndex,
1218                                             ISERIES_HV_ADDR(&local_list),
1219                                             ISERIES_HV_ADDR(&remote_list),
1220                                             length);
1221                 if (rc != HvLpDma_Rc_Good) {
1222                         dev_kfree_skb_irq(skb);
1223                         continue;
1224                 }
1225
1226                 vlan = skb->data[9];
1227                 dev = veth_dev[vlan];
1228                 if (! dev)
1229                         /* Some earlier versions of the driver sent
1230                            broadcasts down all connections, even to
1231                            lpars that weren't on the relevant vlan.
1232                            So ignore packets belonging to a vlan we're
1233                            not on. */
1234                         continue;
1235
1236                 port = (struct veth_port *)dev->priv;
1237                 dest = *((u64 *) skb->data) & 0xFFFFFFFFFFFF0000;
1238
1239                 if ((vlan > HVMAXARCHITECTEDVIRTUALLANS) || !port) {
1240                         dev_kfree_skb_irq(skb);
1241                         continue;
1242                 }
1243                 if (! veth_frame_wanted(port, dest)) {
1244                         dev_kfree_skb_irq(skb);
1245                         continue;
1246                 }
1247
1248                 skb_put(skb, length);
1249                 skb->dev = dev;
1250                 skb->protocol = eth_type_trans(skb, dev);
1251                 skb->ip_summed = CHECKSUM_NONE;
1252                 netif_rx(skb);  /* send it up */
1253                 port->stats.rx_packets++;
1254                 port->stats.rx_bytes += length;
1255         } while (startchunk += nchunks, startchunk < VETH_MAX_FRAMES_PER_MSG);
1256
1257         /* Ack it */
1258         spin_lock_irqsave(&cnx->lock, flags);
1259         BUG_ON(cnx->num_pending_acks > VETH_MAX_ACKS_PER_MSG);
1260
1261         cnx->pending_acks[cnx->num_pending_acks++] =
1262                 event->base_event.xCorrelationToken;
1263
1264         if ( (cnx->num_pending_acks >= cnx->remote_caps.ack_threshold)
1265              || (cnx->num_pending_acks >= VETH_MAX_ACKS_PER_MSG) )
1266                 veth_flush_acks(cnx);
1267
1268         spin_unlock_irqrestore(&cnx->lock, flags);
1269 }
1270
1271 static void veth_timed_ack(unsigned long ptr)
1272 {
1273         struct veth_lpar_connection *cnx = (struct veth_lpar_connection *) ptr;
1274         unsigned long flags;
1275
1276         /* Ack all the events */
1277         spin_lock_irqsave(&cnx->lock, flags);
1278         if (cnx->num_pending_acks > 0)
1279                 veth_flush_acks(cnx);
1280
1281         /* Reschedule the timer */
1282         cnx->ack_timer.expires = jiffies + cnx->ack_timeout;
1283         add_timer(&cnx->ack_timer);
1284         spin_unlock_irqrestore(&cnx->lock, flags);
1285 }
1286
1287 /*
1288  * Module initialization/cleanup
1289  */
1290
1291 void __exit veth_module_cleanup(void)
1292 {
1293         int i;
1294
1295         for (i = 0; i < HVMAXARCHITECTEDLPS; ++i)
1296                 veth_destroy_connection(i);
1297
1298         HvLpEvent_unregisterHandler(HvLpEvent_Type_VirtualLan);
1299
1300         for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; ++i) {
1301                 struct net_device *dev = veth_dev[i];
1302
1303                 if (! dev)
1304                         continue;
1305
1306                 veth_dev[i] = NULL;
1307                 unregister_netdev(dev);
1308                 free_netdev(dev);
1309         }
1310 }
1311 module_exit(veth_module_cleanup);
1312
1313 int __init veth_module_init(void)
1314 {
1315         HvLpIndexMap vlan_map = HvLpConfig_getVirtualLanIndexMap();
1316         int i;
1317         int rc;
1318
1319         this_lp = HvLpConfig_getLpIndex_outline();
1320
1321         for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
1322                 rc = veth_init_connection(i);
1323                 if (rc != 0) {
1324                         veth_module_cleanup();
1325                         return rc;
1326                 }
1327         }
1328
1329         for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; ++i) {
1330                 struct net_device *dev;
1331
1332                 if (! (vlan_map & (0x8000 >> i)))
1333                         continue;
1334
1335                 dev = veth_probe_one(i);
1336
1337                 if (! dev) {
1338                         veth_module_cleanup();
1339                         return rc;
1340                 }
1341
1342                 veth_dev[i] = dev;
1343         }
1344
1345         HvLpEvent_registerHandler(HvLpEvent_Type_VirtualLan,
1346                                   &veth_handle_event);
1347
1348         /* Start the state machine on each connection, to commence
1349          * link negotiation */
1350         for (i = 0; i < HVMAXARCHITECTEDLPS; i++)
1351                 if (veth_cnx[i])
1352                         veth_kick_statemachine(veth_cnx[i]);
1353
1354         return 0;
1355 }
1356 module_init(veth_module_init);