upgrade to linux 2.6.10-1.12_FC2
[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 MODULE_AUTHOR("Kyle Lucke <klucke@us.ibm.com>");
85 MODULE_DESCRIPTION("iSeries Virtual ethernet driver");
86 MODULE_LICENSE("GPL");
87
88 #define VETH_NUMBUFFERS         (120)
89 #define VETH_ACKTIMEOUT         (1000000) /* microseconds */
90 #define VETH_MAX_MCAST          (12)
91
92 #define VETH_MAX_MTU            (9000)
93
94 #if VETH_NUMBUFFERS < 10
95 #define ACK_THRESHOLD           (1)
96 #elif VETH_NUMBUFFERS < 20
97 #define ACK_THRESHOLD           (4)
98 #elif VETH_NUMBUFFERS < 40
99 #define ACK_THRESHOLD           (10)
100 #else
101 #define ACK_THRESHOLD           (20)
102 #endif
103
104 #define VETH_STATE_SHUTDOWN     (0x0001)
105 #define VETH_STATE_OPEN         (0x0002)
106 #define VETH_STATE_RESET        (0x0004)
107 #define VETH_STATE_SENTMON      (0x0008)
108 #define VETH_STATE_SENTCAPS     (0x0010)
109 #define VETH_STATE_GOTCAPACK    (0x0020)
110 #define VETH_STATE_GOTCAPS      (0x0040)
111 #define VETH_STATE_SENTCAPACK   (0x0080)
112 #define VETH_STATE_READY        (0x0100)
113
114 struct veth_msg {
115         struct veth_msg *next;
116         struct VethFramesData data;
117         int token;
118         unsigned long in_use;
119         struct sk_buff *skb;
120         struct device *dev;
121 };
122
123 struct veth_lpar_connection {
124         HvLpIndex remote_lp;
125         struct work_struct statemachine_wq;
126         struct veth_msg *msgs;
127         int num_events;
128         struct VethCapData local_caps;
129
130         struct timer_list ack_timer;
131
132         spinlock_t lock;
133         unsigned long state;
134         HvLpInstanceId src_inst;
135         HvLpInstanceId dst_inst;
136         struct VethLpEvent cap_event, cap_ack_event;
137         u16 pending_acks[VETH_MAX_ACKS_PER_MSG];
138         u32 num_pending_acks;
139
140         int num_ack_events;
141         struct VethCapData remote_caps;
142         u32 ack_timeout;
143
144         spinlock_t msg_stack_lock;
145         struct veth_msg *msg_stack_head;
146 };
147
148 struct veth_port {
149         struct device *dev;
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_allocate_lp_events(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                 spin_unlock_irq(&cnx->lock);
465                 veth_flush_pending(cnx);
466                 spin_lock_irq(&cnx->lock);
467                 if (cnx->state & VETH_STATE_RESET)
468                         goto restart;
469         }
470
471         if (cnx->state & VETH_STATE_SHUTDOWN)
472                 /* It's all over, do nothing */
473                 goto out;
474
475         if ( !(cnx->state & VETH_STATE_OPEN) ) {
476                 if (! cnx->msgs || (cnx->num_events < (2 + VETH_NUMBUFFERS)) )
477                         goto cant_cope;
478
479                 HvCallEvent_openLpEventPath(rlp, HvLpEvent_Type_VirtualLan);
480                 cnx->src_inst =
481                         HvCallEvent_getSourceLpInstanceId(rlp,
482                                                           HvLpEvent_Type_VirtualLan);
483                 cnx->dst_inst =
484                         HvCallEvent_getTargetLpInstanceId(rlp,
485                                                           HvLpEvent_Type_VirtualLan);
486                 cnx->state |= VETH_STATE_OPEN;
487         }
488
489         if ( (cnx->state & VETH_STATE_OPEN)
490              && !(cnx->state & VETH_STATE_SENTMON) ) {
491                 rc = veth_signalevent(cnx, VethEventTypeMonitor,
492                                       HvLpEvent_AckInd_DoAck,
493                                       HvLpEvent_AckType_DeferredAck,
494                                       0, 0, 0, 0, 0, 0);
495
496                 if (rc == HvLpEvent_Rc_Good) {
497                         cnx->state |= VETH_STATE_SENTMON;
498                 } else {
499                         if ( (rc != HvLpEvent_Rc_PartitionDead)
500                              && (rc != HvLpEvent_Rc_PathClosed) )
501                                 veth_error("Error sending monitor to "
502                                            "lpar %d, rc=%x\n",
503                                            rlp, (int) rc);
504
505                         /* Oh well, hope we get a cap from the other
506                          * end and do better when that kicks us */
507                         goto out;
508                 }
509         }
510
511         if ( (cnx->state & VETH_STATE_OPEN)
512              && !(cnx->state & VETH_STATE_SENTCAPS)) {
513                 u64 *rawcap = (u64 *)&cnx->local_caps;
514
515                 rc = veth_signalevent(cnx, VethEventTypeCap,
516                                       HvLpEvent_AckInd_DoAck,
517                                       HvLpEvent_AckType_ImmediateAck,
518                                       0, rawcap[0], rawcap[1], rawcap[2],
519                                       rawcap[3], rawcap[4]);
520
521                 if (rc == HvLpEvent_Rc_Good) {
522                         cnx->state |= VETH_STATE_SENTCAPS;
523                 } else {
524                         if ( (rc != HvLpEvent_Rc_PartitionDead)
525                              && (rc != HvLpEvent_Rc_PathClosed) )
526                                 veth_error("Error sending caps to "
527                                            "lpar %d, rc=%x\n",
528                                            rlp, (int) rc);
529                         /* Oh well, hope we get a cap from the other
530                          * end and do better when that kicks us */
531                         goto out;
532                 }
533         }
534
535         if ((cnx->state & VETH_STATE_GOTCAPS)
536             && !(cnx->state & VETH_STATE_SENTCAPACK)) {
537                 struct VethCapData *remote_caps = &cnx->remote_caps;
538
539                 memcpy(remote_caps, &cnx->cap_event.u.caps_data,
540                        sizeof(*remote_caps));
541
542                 spin_unlock_irq(&cnx->lock);
543                 rc = veth_process_caps(cnx);
544                 spin_lock_irq(&cnx->lock);
545
546                 /* We dropped the lock, so recheck for anything which
547                  * might mess us up */
548                 if (cnx->state & (VETH_STATE_RESET|VETH_STATE_SHUTDOWN))
549                         goto restart;
550
551                 cnx->cap_event.base_event.xRc = rc;
552                 HvCallEvent_ackLpEvent((struct HvLpEvent *)&cnx->cap_event);
553                 if (rc == HvLpEvent_Rc_Good)
554                         cnx->state |= VETH_STATE_SENTCAPACK;
555                 else
556                         goto cant_cope;
557         }
558
559         if ((cnx->state & VETH_STATE_GOTCAPACK)
560             && (cnx->state & VETH_STATE_GOTCAPS)
561             && !(cnx->state & VETH_STATE_READY)) {
562                 if (cnx->cap_ack_event.base_event.xRc == HvLpEvent_Rc_Good) {
563                         /* Start the ACK timer */
564                         cnx->ack_timer.expires = jiffies + cnx->ack_timeout;
565                         add_timer(&cnx->ack_timer);
566                         cnx->state |= VETH_STATE_READY;
567                 } else {
568                         veth_printk(KERN_ERR, "Caps rejected (rc=%d) by "
569                                     "lpar %d\n",
570                                     cnx->cap_ack_event.base_event.xRc,
571                                     rlp);
572                         goto cant_cope;
573                 }
574         }
575
576  out:
577         spin_unlock_irq(&cnx->lock);
578         return;
579
580  cant_cope:
581         /* FIXME: we get here if something happens we really can't
582          * cope with.  The link will never work once we get here, and
583          * all we can do is not lock the rest of the system up */
584         veth_error("Badness on connection to lpar %d (state=%04lx) "
585                    " - shutting down\n", rlp, cnx->state);
586         cnx->state |= VETH_STATE_SHUTDOWN;
587         spin_unlock_irq(&cnx->lock);
588 }
589
590 static int veth_init_connection(u8 rlp)
591 {
592         struct veth_lpar_connection *cnx;
593         struct veth_msg *msgs;
594         int i;
595
596         if ( (rlp == this_lp)
597              || ! HvLpConfig_doLpsCommunicateOnVirtualLan(this_lp, rlp) )
598                 return 0;
599
600         cnx = kmalloc(sizeof(*cnx), GFP_KERNEL);
601         if (! cnx)
602                 return -ENOMEM;
603         memset(cnx, 0, sizeof(*cnx));
604
605         cnx->remote_lp = rlp;
606         spin_lock_init(&cnx->lock);
607         INIT_WORK(&cnx->statemachine_wq, veth_statemachine, cnx);
608         init_timer(&cnx->ack_timer);
609         cnx->ack_timer.function = veth_timed_ack;
610         cnx->ack_timer.data = (unsigned long) cnx;
611         memset(&cnx->pending_acks, 0xff, sizeof (cnx->pending_acks));
612
613         veth_cnx[rlp] = cnx;
614
615         msgs = kmalloc(VETH_NUMBUFFERS * sizeof(struct veth_msg), GFP_KERNEL);
616         if (! msgs) {
617                 veth_error("Can't allocate buffers for lpar %d\n", rlp);
618                 return -ENOMEM;
619         }
620
621         cnx->msgs = msgs;
622         memset(msgs, 0, VETH_NUMBUFFERS * sizeof(struct veth_msg));
623         spin_lock_init(&cnx->msg_stack_lock);
624
625         for (i = 0; i < VETH_NUMBUFFERS; i++) {
626                 msgs[i].token = i;
627                 veth_stack_push(cnx, msgs + i);
628         }
629
630         cnx->num_events = veth_allocate_events(rlp, 2 + VETH_NUMBUFFERS);
631
632         if (cnx->num_events < (2 + VETH_NUMBUFFERS)) {
633                 veth_error("Can't allocate events for lpar %d, only got %d\n",
634                            rlp, cnx->num_events);
635                 return -ENOMEM;
636         }
637
638         cnx->local_caps.num_buffers = VETH_NUMBUFFERS;
639         cnx->local_caps.ack_threshold = ACK_THRESHOLD;
640         cnx->local_caps.ack_timeout = VETH_ACKTIMEOUT;
641
642         return 0;
643 }
644
645 static void veth_destroy_connection(u8 rlp)
646 {
647         struct veth_lpar_connection *cnx = veth_cnx[rlp];
648
649         if (! cnx)
650                 return;
651
652         spin_lock_irq(&cnx->lock);
653         cnx->state |= VETH_STATE_RESET | VETH_STATE_SHUTDOWN;
654         veth_kick_statemachine(cnx);
655         spin_unlock_irq(&cnx->lock);
656
657         flush_scheduled_work();
658
659         /* FIXME: not sure if this is necessary - will already have
660          * been deleted by the state machine, just want to make sure
661          * its not running any more */
662         del_timer_sync(&cnx->ack_timer);
663
664         if (cnx->num_events > 0)
665                 mf_deallocate_lp_events(cnx->remote_lp,
666                                       HvLpEvent_Type_VirtualLan,
667                                       cnx->num_events,
668                                       NULL, NULL);
669         if (cnx->num_ack_events > 0)
670                 mf_deallocate_lp_events(cnx->remote_lp,
671                                       HvLpEvent_Type_VirtualLan,
672                                       cnx->num_ack_events,
673                                       NULL, NULL);
674
675         if (cnx->msgs)
676                 kfree(cnx->msgs);
677 }
678
679 /*
680  * net_device code
681  */
682
683 static int veth_open(struct net_device *dev)
684 {
685         struct veth_port *port = (struct veth_port *) dev->priv;
686
687         memset(&port->stats, 0, sizeof (port->stats));
688         netif_start_queue(dev);
689         return 0;
690 }
691
692 static int veth_close(struct net_device *dev)
693 {
694         netif_stop_queue(dev);
695         return 0;
696 }
697
698 static struct net_device_stats *veth_get_stats(struct net_device *dev)
699 {
700         struct veth_port *port = (struct veth_port *) dev->priv;
701
702         return &port->stats;
703 }
704
705 static int veth_change_mtu(struct net_device *dev, int new_mtu)
706 {
707         if ((new_mtu < 68) || (new_mtu > VETH_MAX_MTU))
708                 return -EINVAL;
709         dev->mtu = new_mtu;
710         return 0;
711 }
712
713 static void veth_set_multicast_list(struct net_device *dev)
714 {
715         struct veth_port *port = (struct veth_port *) dev->priv;
716         unsigned long flags;
717
718         write_lock_irqsave(&port->mcast_gate, flags);
719
720         if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
721                 printk(KERN_INFO "%s: Promiscuous mode enabled.\n",
722                        dev->name);
723                 port->promiscuous = 1;
724         } else if ( (dev->flags & IFF_ALLMULTI)
725                     || (dev->mc_count > VETH_MAX_MCAST) ) {
726                 port->all_mcast = 1;
727         } else {
728                 struct dev_mc_list *dmi = dev->mc_list;
729                 int i;
730
731                 /* Update table */
732                 port->num_mcast = 0;
733
734                 for (i = 0; i < dev->mc_count; i++) {
735                         u8 *addr = dmi->dmi_addr;
736                         u64 xaddr = 0;
737
738                         if (addr[0] & 0x01) {/* multicast address? */
739                                 memcpy(&xaddr, addr, ETH_ALEN);
740                                 port->mcast_addr[port->num_mcast] = xaddr;
741                                 port->num_mcast++;
742                         }
743                         dmi = dmi->next;
744                 }
745         }
746
747         write_unlock_irqrestore(&port->mcast_gate, flags);
748 }
749
750 static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
751 {
752         strncpy(info->driver, "veth", sizeof(info->driver) - 1);
753         info->driver[sizeof(info->driver) - 1] = '\0';
754         strncpy(info->version, "1.0", sizeof(info->version) - 1);
755 }
756
757 static int veth_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
758 {
759         ecmd->supported = (SUPPORTED_1000baseT_Full
760                           | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
761         ecmd->advertising = (SUPPORTED_1000baseT_Full
762                             | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
763         ecmd->port = PORT_FIBRE;
764         ecmd->transceiver = XCVR_INTERNAL;
765         ecmd->phy_address = 0;
766         ecmd->speed = SPEED_1000;
767         ecmd->duplex = DUPLEX_FULL;
768         ecmd->autoneg = AUTONEG_ENABLE;
769         ecmd->maxtxpkt = 120;
770         ecmd->maxrxpkt = 120;
771         return 0;
772 }
773
774 static u32 veth_get_link(struct net_device *dev)
775 {
776         return 1;
777 }
778
779 static struct ethtool_ops ops = {
780         .get_drvinfo = veth_get_drvinfo,
781         .get_settings = veth_get_settings,
782         .get_link = veth_get_link,
783 };
784
785 static void veth_tx_timeout(struct net_device *dev)
786 {
787         struct veth_port *port = (struct veth_port *)dev->priv;
788         struct net_device_stats *stats = &port->stats;
789         unsigned long flags;
790         int i;
791
792         stats->tx_errors++;
793
794         spin_lock_irqsave(&port->pending_gate, flags);
795
796         printk(KERN_WARNING "%s: Tx timeout!  Resetting lp connections: %08x\n",
797                dev->name, port->pending_lpmask);
798
799         /* If we've timed out the queue must be stopped, which should
800          * only ever happen when there is a pending packet. */
801         WARN_ON(! port->pending_lpmask);
802
803         for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
804                 struct veth_lpar_connection *cnx = veth_cnx[i];
805
806                 if (! (port->pending_lpmask & (1<<i)))
807                         continue;
808
809                 /* If we're pending on it, we must be connected to it,
810                  * so we should certainly have a structure for it. */
811                 BUG_ON(! cnx);
812
813                 /* Theoretically we could be kicking a connection
814                  * which doesn't deserve it, but in practice if we've
815                  * had a Tx timeout, the pending_lpmask will have
816                  * exactly one bit set - the connection causing the
817                  * problem. */
818                 spin_lock(&cnx->lock);
819                 cnx->state |= VETH_STATE_RESET;
820                 veth_kick_statemachine(cnx);
821                 spin_unlock(&cnx->lock);
822         }
823
824         spin_unlock_irqrestore(&port->pending_gate, flags);
825 }
826
827 static struct net_device * __init veth_probe_one(int vlan, struct device *vdev)
828 {
829         struct net_device *dev;
830         struct veth_port *port;
831         int i, rc;
832
833         dev = alloc_etherdev(sizeof (struct veth_port));
834         if (! dev) {
835                 veth_error("Unable to allocate net_device structure!\n");
836                 return NULL;
837         }
838
839         port = (struct veth_port *) dev->priv;
840
841         spin_lock_init(&port->pending_gate);
842         rwlock_init(&port->mcast_gate);
843
844         for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
845                 HvLpVirtualLanIndexMap map;
846
847                 if (i == this_lp)
848                         continue;
849                 map = HvLpConfig_getVirtualLanIndexMapForLp(i);
850                 if (map & (0x8000 >> vlan))
851                         port->lpar_map |= (1 << i);
852         }
853         port->dev = vdev;
854
855         dev->dev_addr[0] = 0x02;
856         dev->dev_addr[1] = 0x01;
857         dev->dev_addr[2] = 0xff;
858         dev->dev_addr[3] = vlan;
859         dev->dev_addr[4] = 0xff;
860         dev->dev_addr[5] = this_lp;
861
862         dev->mtu = VETH_MAX_MTU;
863
864         memcpy(&port->mac_addr, dev->dev_addr, 6);
865
866         dev->open = veth_open;
867         dev->hard_start_xmit = veth_start_xmit;
868         dev->stop = veth_close;
869         dev->get_stats = veth_get_stats;
870         dev->change_mtu = veth_change_mtu;
871         dev->set_mac_address = NULL;
872         dev->set_multicast_list = veth_set_multicast_list;
873         SET_ETHTOOL_OPS(dev, &ops);
874
875         dev->watchdog_timeo = 2 * (VETH_ACKTIMEOUT * HZ / 1000000);
876         dev->tx_timeout = veth_tx_timeout;
877
878         SET_NETDEV_DEV(dev, vdev);
879
880         rc = register_netdev(dev);
881         if (rc != 0) {
882                 veth_printk(KERN_ERR,
883                             "Failed to register ethernet device for vlan %d\n",
884                             vlan);
885                 free_netdev(dev);
886                 return NULL;
887         }
888
889         veth_printk(KERN_DEBUG, "%s attached to iSeries vlan %d (lpar_map=0x%04x)\n",
890                     dev->name, vlan, port->lpar_map);
891
892         return dev;
893 }
894
895 /*
896  * Tx path
897  */
898
899 static int veth_transmit_to_one(struct sk_buff *skb, HvLpIndex rlp,
900                                 struct net_device *dev)
901 {
902         struct veth_lpar_connection *cnx = veth_cnx[rlp];
903         struct veth_port *port = (struct veth_port *) dev->priv;
904         HvLpEvent_Rc rc;
905         u32 dma_address, dma_length;
906         struct veth_msg *msg = NULL;
907         int err = 0;
908         unsigned long flags;
909
910         if (! cnx) {
911                 port->stats.tx_errors++;
912                 dev_kfree_skb(skb);
913                 return 0;
914         }
915
916         spin_lock_irqsave(&cnx->lock, flags);
917
918         if (! cnx->state & VETH_STATE_READY)
919                 goto drop;
920
921         if ((skb->len - 14) > VETH_MAX_MTU)
922                 goto drop;
923
924         msg = veth_stack_pop(cnx);
925
926         if (! msg) {
927                 err = 1;
928                 goto drop;
929         }
930
931         dma_length = skb->len;
932         dma_address = dma_map_single(port->dev, skb->data,
933                                      dma_length, DMA_TO_DEVICE);
934
935         if (dma_mapping_error(dma_address))
936                 goto recycle_and_drop;
937
938         /* Is it really necessary to check the length and address
939          * fields of the first entry here? */
940         msg->skb = skb;
941         msg->dev = port->dev;
942         msg->data.addr[0] = dma_address;
943         msg->data.len[0] = dma_length;
944         msg->data.eofmask = 1 << VETH_EOF_SHIFT;
945         set_bit(0, &(msg->in_use));
946         rc = veth_signaldata(cnx, VethEventTypeFrames, msg->token, &msg->data);
947
948         if (rc != HvLpEvent_Rc_Good)
949                 goto recycle_and_drop;
950
951         spin_unlock_irqrestore(&cnx->lock, flags);
952         return 0;
953
954  recycle_and_drop:
955         msg->skb = NULL;
956         /* need to set in use to make veth_recycle_msg in case this
957          * was a mapping failure */
958         set_bit(0, &msg->in_use);
959         veth_recycle_msg(cnx, msg);
960  drop:
961         port->stats.tx_errors++;
962         dev_kfree_skb(skb);
963         spin_unlock_irqrestore(&cnx->lock, flags);
964         return err;
965 }
966
967 static HvLpIndexMap veth_transmit_to_many(struct sk_buff *skb,
968                                           HvLpIndexMap lpmask,
969                                           struct net_device *dev)
970 {
971         struct veth_port *port = (struct veth_port *) dev->priv;
972         int i;
973         int rc;
974
975         for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
976                 if ((lpmask & (1 << i)) == 0)
977                         continue;
978
979                 rc = veth_transmit_to_one(skb_get(skb), i, dev);
980                 if (! rc)
981                         lpmask &= ~(1<<i);
982         }
983
984         if (! lpmask) {
985                 port->stats.tx_packets++;
986                 port->stats.tx_bytes += skb->len;
987         }
988
989         return lpmask;
990 }
991
992 static int veth_start_xmit(struct sk_buff *skb, struct net_device *dev)
993 {
994         unsigned char *frame = skb->data;
995         struct veth_port *port = (struct veth_port *) dev->priv;
996         unsigned long flags;
997         HvLpIndexMap lpmask;
998
999         if (! (frame[0] & 0x01)) {
1000                 /* unicast packet */
1001                 HvLpIndex rlp = frame[5];
1002
1003                 if ( ! ((1 << rlp) & port->lpar_map) ) {
1004                         dev_kfree_skb(skb);
1005                         return 0;
1006                 }
1007
1008                 lpmask = 1 << rlp;
1009         } else {
1010                 lpmask = port->lpar_map;
1011         }
1012
1013         spin_lock_irqsave(&port->pending_gate, flags);
1014
1015         lpmask = veth_transmit_to_many(skb, lpmask, dev);
1016
1017         if (! lpmask) {
1018                 dev_kfree_skb(skb);
1019         } else {
1020                 if (port->pending_skb) {
1021                         veth_error("%s: Tx while skb was pending!\n",
1022                                    dev->name);
1023                         dev_kfree_skb(skb);
1024                         spin_unlock_irqrestore(&port->pending_gate, flags);
1025                         return 1;
1026                 }
1027
1028                 port->pending_skb = skb;
1029                 port->pending_lpmask = lpmask;
1030                 netif_stop_queue(dev);
1031         }
1032
1033         spin_unlock_irqrestore(&port->pending_gate, flags);
1034
1035         return 0;
1036 }
1037
1038 static void veth_recycle_msg(struct veth_lpar_connection *cnx,
1039                              struct veth_msg *msg)
1040 {
1041         u32 dma_address, dma_length;
1042
1043         if (test_and_clear_bit(0, &msg->in_use)) {
1044                 dma_address = msg->data.addr[0];
1045                 dma_length = msg->data.len[0];
1046
1047                 dma_unmap_single(msg->dev, dma_address, dma_length,
1048                                  DMA_TO_DEVICE);
1049
1050                 if (msg->skb) {
1051                         dev_kfree_skb_any(msg->skb);
1052                         msg->skb = NULL;
1053                 }
1054
1055                 memset(&msg->data, 0, sizeof(msg->data));
1056                 veth_stack_push(cnx, msg);
1057         } else
1058                 if (cnx->state & VETH_STATE_OPEN)
1059                         veth_error("Bogus frames ack from lpar %d (#%d)\n",
1060                                    cnx->remote_lp, msg->token);
1061 }
1062
1063 static void veth_flush_pending(struct veth_lpar_connection *cnx)
1064 {
1065         int i;
1066         for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++) {
1067                 struct net_device *dev = veth_dev[i];
1068                 struct veth_port *port;
1069                 unsigned long flags;
1070
1071                 if (! dev)
1072                         continue;
1073
1074                 port = (struct veth_port *)dev->priv;
1075
1076                 if (! (port->lpar_map & (1<<cnx->remote_lp)))
1077                         continue;
1078
1079                 spin_lock_irqsave(&port->pending_gate, flags);
1080                 if (port->pending_skb) {
1081                         port->pending_lpmask =
1082                                 veth_transmit_to_many(port->pending_skb,
1083                                                       port->pending_lpmask,
1084                                                       dev);
1085                         if (! port->pending_lpmask) {
1086                                 dev_kfree_skb_any(port->pending_skb);
1087                                 port->pending_skb = NULL;
1088                                 netif_wake_queue(dev);
1089                         }
1090                 }
1091                 spin_unlock_irqrestore(&port->pending_gate, flags);
1092         }
1093 }
1094
1095 /*
1096  * Rx path
1097  */
1098
1099 static inline int veth_frame_wanted(struct veth_port *port, u64 mac_addr)
1100 {
1101         int wanted = 0;
1102         int i;
1103         unsigned long flags;
1104
1105         if ( (mac_addr == port->mac_addr) || (mac_addr == 0xffffffffffff0000) )
1106                 return 1;
1107
1108         if (! (((char *) &mac_addr)[0] & 0x01))
1109                 return 0;
1110
1111         read_lock_irqsave(&port->mcast_gate, flags);
1112
1113         if (port->promiscuous || port->all_mcast) {
1114                 wanted = 1;
1115                 goto out;
1116         }
1117
1118         for (i = 0; i < port->num_mcast; ++i) {
1119                 if (port->mcast_addr[i] == mac_addr) {
1120                         wanted = 1;
1121                         break;
1122                 }
1123         }
1124
1125  out:
1126         read_unlock_irqrestore(&port->mcast_gate, flags);
1127
1128         return wanted;
1129 }
1130
1131 struct dma_chunk {
1132         u64 addr;
1133         u64 size;
1134 };
1135
1136 #define VETH_MAX_PAGES_PER_FRAME ( (VETH_MAX_MTU+PAGE_SIZE-2)/PAGE_SIZE + 1 )
1137
1138 static inline void veth_build_dma_list(struct dma_chunk *list,
1139                                        unsigned char *p, unsigned long length)
1140 {
1141         unsigned long done;
1142         int i = 1;
1143
1144         /* FIXME: skbs are continguous in real addresses.  Do we
1145          * really need to break it into PAGE_SIZE chunks, or can we do
1146          * it just at the granularity of iSeries real->absolute
1147          * mapping?  Indeed, given the way the allocator works, can we
1148          * count on them being absolutely contiguous? */
1149         list[0].addr = ISERIES_HV_ADDR(p);
1150         list[0].size = min(length,
1151                            PAGE_SIZE - ((unsigned long)p & ~PAGE_MASK));
1152
1153         done = list[0].size;
1154         while (done < length) {
1155                 list[i].addr = ISERIES_HV_ADDR(p + done);
1156                 list[i].size = min(length-done, PAGE_SIZE);
1157                 done += list[i].size;
1158                 i++;
1159         }
1160 }
1161
1162 static void veth_flush_acks(struct veth_lpar_connection *cnx)
1163 {
1164         HvLpEvent_Rc rc;
1165
1166         rc = veth_signaldata(cnx, VethEventTypeFramesAck,
1167                              0, &cnx->pending_acks);
1168
1169         if (rc != HvLpEvent_Rc_Good)
1170                 veth_error("Error 0x%x acking frames from lpar %d!\n",
1171                            (unsigned)rc, cnx->remote_lp);
1172
1173         cnx->num_pending_acks = 0;
1174         memset(&cnx->pending_acks, 0xff, sizeof(cnx->pending_acks));
1175 }
1176
1177 static void veth_receive(struct veth_lpar_connection *cnx,
1178                          struct VethLpEvent *event)
1179 {
1180         struct VethFramesData *senddata = &event->u.frames_data;
1181         int startchunk = 0;
1182         int nchunks;
1183         unsigned long flags;
1184         HvLpDma_Rc rc;
1185
1186         do {
1187                 u16 length = 0;
1188                 struct sk_buff *skb;
1189                 struct dma_chunk local_list[VETH_MAX_PAGES_PER_FRAME];
1190                 struct dma_chunk remote_list[VETH_MAX_FRAMES_PER_MSG];
1191                 u64 dest;
1192                 HvLpVirtualLanIndex vlan;
1193                 struct net_device *dev;
1194                 struct veth_port *port;
1195
1196                 /* FIXME: do we need this? */
1197                 memset(local_list, 0, sizeof(local_list));
1198                 memset(remote_list, 0, sizeof(VETH_MAX_FRAMES_PER_MSG));
1199
1200                 /* a 0 address marks the end of the valid entries */
1201                 if (senddata->addr[startchunk] == 0)
1202                         break;
1203
1204                 /* make sure that we have at least 1 EOF entry in the
1205                  * remaining entries */
1206                 if (! (senddata->eofmask >> (startchunk + VETH_EOF_SHIFT))) {
1207                         veth_error("missing EOF frag in event "
1208                                    "eofmask=0x%x startchunk=%d\n",
1209                                    (unsigned) senddata->eofmask, startchunk);
1210                         break;
1211                 }
1212
1213                 /* build list of chunks in this frame */
1214                 nchunks = 0;
1215                 do {
1216                         remote_list[nchunks].addr =
1217                                 (u64) senddata->addr[startchunk+nchunks] << 32;
1218                         remote_list[nchunks].size =
1219                                 senddata->len[startchunk+nchunks];
1220                         length += remote_list[nchunks].size;
1221                 } while (! (senddata->eofmask &
1222                             (1 << (VETH_EOF_SHIFT + startchunk + nchunks++))));
1223
1224                 /* length == total length of all chunks */
1225                 /* nchunks == # of chunks in this frame */
1226
1227                 if ((length - ETH_HLEN) > VETH_MAX_MTU) {
1228                         veth_error("Received oversize frame from lpar %d "
1229                                    "(length=%d)\n", cnx->remote_lp, length);
1230                         continue;
1231                 }
1232
1233                 skb = alloc_skb(length, GFP_ATOMIC);
1234                 if (!skb)
1235                         continue;
1236
1237                 veth_build_dma_list(local_list, skb->data, length);
1238
1239                 rc = HvCallEvent_dmaBufList(HvLpEvent_Type_VirtualLan,
1240                                             event->base_event.xSourceLp,
1241                                             HvLpDma_Direction_RemoteToLocal,
1242                                             cnx->src_inst,
1243                                             cnx->dst_inst,
1244                                             HvLpDma_AddressType_RealAddress,
1245                                             HvLpDma_AddressType_TceIndex,
1246                                             ISERIES_HV_ADDR(&local_list),
1247                                             ISERIES_HV_ADDR(&remote_list),
1248                                             length);
1249                 if (rc != HvLpDma_Rc_Good) {
1250                         dev_kfree_skb_irq(skb);
1251                         continue;
1252                 }
1253
1254                 vlan = skb->data[9];
1255                 dev = veth_dev[vlan];
1256                 if (! dev)
1257                         /* Some earlier versions of the driver sent
1258                            broadcasts down all connections, even to
1259                            lpars that weren't on the relevant vlan.
1260                            So ignore packets belonging to a vlan we're
1261                            not on. */
1262                         continue;
1263
1264                 port = (struct veth_port *)dev->priv;
1265                 dest = *((u64 *) skb->data) & 0xFFFFFFFFFFFF0000;
1266
1267                 if ((vlan > HVMAXARCHITECTEDVIRTUALLANS) || !port) {
1268                         dev_kfree_skb_irq(skb);
1269                         continue;
1270                 }
1271                 if (! veth_frame_wanted(port, dest)) {
1272                         dev_kfree_skb_irq(skb);
1273                         continue;
1274                 }
1275
1276                 skb_put(skb, length);
1277                 skb->dev = dev;
1278                 skb->protocol = eth_type_trans(skb, dev);
1279                 skb->ip_summed = CHECKSUM_NONE;
1280                 netif_rx(skb);  /* send it up */
1281                 port->stats.rx_packets++;
1282                 port->stats.rx_bytes += length;
1283         } while (startchunk += nchunks, startchunk < VETH_MAX_FRAMES_PER_MSG);
1284
1285         /* Ack it */
1286         spin_lock_irqsave(&cnx->lock, flags);
1287         BUG_ON(cnx->num_pending_acks > VETH_MAX_ACKS_PER_MSG);
1288
1289         cnx->pending_acks[cnx->num_pending_acks++] =
1290                 event->base_event.xCorrelationToken;
1291
1292         if ( (cnx->num_pending_acks >= cnx->remote_caps.ack_threshold)
1293              || (cnx->num_pending_acks >= VETH_MAX_ACKS_PER_MSG) )
1294                 veth_flush_acks(cnx);
1295
1296         spin_unlock_irqrestore(&cnx->lock, flags);
1297 }
1298
1299 static void veth_timed_ack(unsigned long ptr)
1300 {
1301         struct veth_lpar_connection *cnx = (struct veth_lpar_connection *) ptr;
1302         unsigned long flags;
1303
1304         /* Ack all the events */
1305         spin_lock_irqsave(&cnx->lock, flags);
1306         if (cnx->num_pending_acks > 0)
1307                 veth_flush_acks(cnx);
1308
1309         /* Reschedule the timer */
1310         cnx->ack_timer.expires = jiffies + cnx->ack_timeout;
1311         add_timer(&cnx->ack_timer);
1312         spin_unlock_irqrestore(&cnx->lock, flags);
1313 }
1314
1315 static int veth_remove(struct vio_dev *vdev)
1316 {
1317         int i = vdev->unit_address;
1318         struct net_device *dev;
1319
1320         dev = veth_dev[i];
1321         if (dev != NULL) {
1322                 veth_dev[i] = NULL;
1323                 unregister_netdev(dev);
1324                 free_netdev(dev);
1325         }
1326         return 0;
1327 }
1328
1329 static int veth_probe(struct vio_dev *vdev, const struct vio_device_id *id)
1330 {
1331         int i = vdev->unit_address;
1332         struct net_device *dev;
1333
1334         dev = veth_probe_one(i, &vdev->dev);
1335         if (dev == NULL) {
1336                 veth_remove(vdev);
1337                 return 1;
1338         }
1339         veth_dev[i] = dev;
1340
1341         /* Start the state machine on each connection, to commence
1342          * link negotiation */
1343         for (i = 0; i < HVMAXARCHITECTEDLPS; i++)
1344                 if (veth_cnx[i])
1345                         veth_kick_statemachine(veth_cnx[i]);
1346
1347         return 0;
1348 }
1349
1350 /**
1351  * veth_device_table: Used by vio.c to match devices that we
1352  * support.
1353  */
1354 static struct vio_device_id veth_device_table[] __devinitdata = {
1355         { "vlan", "" },
1356         { NULL, NULL }
1357 };
1358 MODULE_DEVICE_TABLE(vio, veth_device_table);
1359
1360 static struct vio_driver veth_driver = {
1361         .name = "iseries_veth",
1362         .id_table = veth_device_table,
1363         .probe = veth_probe,
1364         .remove = veth_remove
1365 };
1366
1367 /*
1368  * Module initialization/cleanup
1369  */
1370
1371 void __exit veth_module_cleanup(void)
1372 {
1373         int i;
1374
1375         vio_unregister_driver(&veth_driver);
1376
1377         for (i = 0; i < HVMAXARCHITECTEDLPS; ++i)
1378                 veth_destroy_connection(i);
1379
1380         HvLpEvent_unregisterHandler(HvLpEvent_Type_VirtualLan);
1381 }
1382 module_exit(veth_module_cleanup);
1383
1384 int __init veth_module_init(void)
1385 {
1386         int i;
1387         int rc;
1388
1389         this_lp = HvLpConfig_getLpIndex_outline();
1390
1391         for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
1392                 rc = veth_init_connection(i);
1393                 if (rc != 0) {
1394                         veth_module_cleanup();
1395                         return rc;
1396                 }
1397         }
1398
1399         HvLpEvent_registerHandler(HvLpEvent_Type_VirtualLan,
1400                                   &veth_handle_event);
1401
1402         return vio_register_driver(&veth_driver);
1403 }
1404 module_init(veth_module_init);