[PATCH] iseries_veth: Fix broken promiscuous handling
[linux-2.6] / 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 #undef DEBUG
83
84 #include "iseries_veth.h"
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         struct device *dev;
123 };
124
125 struct veth_lpar_connection {
126         HvLpIndex remote_lp;
127         struct work_struct statemachine_wq;
128         struct veth_msg *msgs;
129         int num_events;
130         struct VethCapData local_caps;
131
132         struct timer_list ack_timer;
133
134         spinlock_t lock;
135         unsigned long state;
136         HvLpInstanceId src_inst;
137         HvLpInstanceId dst_inst;
138         struct VethLpEvent cap_event, cap_ack_event;
139         u16 pending_acks[VETH_MAX_ACKS_PER_MSG];
140         u32 num_pending_acks;
141
142         int num_ack_events;
143         struct VethCapData remote_caps;
144         u32 ack_timeout;
145
146         spinlock_t msg_stack_lock;
147         struct veth_msg *msg_stack_head;
148 };
149
150 struct veth_port {
151         struct device *dev;
152         struct net_device_stats stats;
153         u64 mac_addr;
154         HvLpIndexMap lpar_map;
155
156         spinlock_t pending_gate;
157         struct sk_buff *pending_skb;
158         HvLpIndexMap pending_lpmask;
159
160         rwlock_t mcast_gate;
161         int promiscuous;
162         int num_mcast;
163         u64 mcast_addr[VETH_MAX_MCAST];
164 };
165
166 static HvLpIndex this_lp;
167 static struct veth_lpar_connection *veth_cnx[HVMAXARCHITECTEDLPS]; /* = 0 */
168 static struct net_device *veth_dev[HVMAXARCHITECTEDVIRTUALLANS]; /* = 0 */
169
170 static int veth_start_xmit(struct sk_buff *skb, struct net_device *dev);
171 static void veth_recycle_msg(struct veth_lpar_connection *, struct veth_msg *);
172 static void veth_flush_pending(struct veth_lpar_connection *cnx);
173 static void veth_receive(struct veth_lpar_connection *, struct VethLpEvent *);
174 static void veth_timed_ack(unsigned long connectionPtr);
175
176 /*
177  * Utility functions
178  */
179
180 #define veth_info(fmt, args...) \
181         printk(KERN_INFO "iseries_veth: " fmt, ## args)
182
183 #define veth_error(fmt, args...) \
184         printk(KERN_ERR "iseries_veth: Error: " fmt, ## args)
185
186 #ifdef DEBUG
187 #define veth_debug(fmt, args...) \
188         printk(KERN_DEBUG "iseries_veth: " fmt, ## args)
189 #else
190 #define veth_debug(fmt, args...) do {} while (0)
191 #endif
192
193 static inline void veth_stack_push(struct veth_lpar_connection *cnx,
194                                    struct veth_msg *msg)
195 {
196         unsigned long flags;
197
198         spin_lock_irqsave(&cnx->msg_stack_lock, flags);
199         msg->next = cnx->msg_stack_head;
200         cnx->msg_stack_head = msg;
201         spin_unlock_irqrestore(&cnx->msg_stack_lock, flags);
202 }
203
204 static inline struct veth_msg *veth_stack_pop(struct veth_lpar_connection *cnx)
205 {
206         unsigned long flags;
207         struct veth_msg *msg;
208
209         spin_lock_irqsave(&cnx->msg_stack_lock, flags);
210         msg = cnx->msg_stack_head;
211         if (msg)
212                 cnx->msg_stack_head = cnx->msg_stack_head->next;
213         spin_unlock_irqrestore(&cnx->msg_stack_lock, flags);
214         return msg;
215 }
216
217 static inline HvLpEvent_Rc
218 veth_signalevent(struct veth_lpar_connection *cnx, u16 subtype,
219                  HvLpEvent_AckInd ackind, HvLpEvent_AckType acktype,
220                  u64 token,
221                  u64 data1, u64 data2, u64 data3, u64 data4, u64 data5)
222 {
223         return HvCallEvent_signalLpEventFast(cnx->remote_lp,
224                                              HvLpEvent_Type_VirtualLan,
225                                              subtype, ackind, acktype,
226                                              cnx->src_inst,
227                                              cnx->dst_inst,
228                                              token, data1, data2, data3,
229                                              data4, data5);
230 }
231
232 static inline HvLpEvent_Rc veth_signaldata(struct veth_lpar_connection *cnx,
233                                            u16 subtype, u64 token, void *data)
234 {
235         u64 *p = (u64 *) data;
236
237         return veth_signalevent(cnx, subtype, HvLpEvent_AckInd_NoAck,
238                                 HvLpEvent_AckType_ImmediateAck,
239                                 token, p[0], p[1], p[2], p[3], p[4]);
240 }
241
242 struct veth_allocation {
243         struct completion c;
244         int num;
245 };
246
247 static void veth_complete_allocation(void *parm, int number)
248 {
249         struct veth_allocation *vc = (struct veth_allocation *)parm;
250
251         vc->num = number;
252         complete(&vc->c);
253 }
254
255 static int veth_allocate_events(HvLpIndex rlp, int number)
256 {
257         struct veth_allocation vc = { COMPLETION_INITIALIZER(vc.c), 0 };
258
259         mf_allocate_lp_events(rlp, HvLpEvent_Type_VirtualLan,
260                             sizeof(struct VethLpEvent), number,
261                             &veth_complete_allocation, &vc);
262         wait_for_completion(&vc.c);
263
264         return vc.num;
265 }
266
267 /*
268  * LPAR connection code
269  */
270
271 static inline void veth_kick_statemachine(struct veth_lpar_connection *cnx)
272 {
273         schedule_work(&cnx->statemachine_wq);
274 }
275
276 static void veth_take_cap(struct veth_lpar_connection *cnx,
277                           struct VethLpEvent *event)
278 {
279         unsigned long flags;
280
281         spin_lock_irqsave(&cnx->lock, flags);
282         /* Receiving caps may mean the other end has just come up, so
283          * we need to reload the instance ID of the far end */
284         cnx->dst_inst =
285                 HvCallEvent_getTargetLpInstanceId(cnx->remote_lp,
286                                                   HvLpEvent_Type_VirtualLan);
287
288         if (cnx->state & VETH_STATE_GOTCAPS) {
289                 veth_error("Received a second capabilities from LPAR %d.\n",
290                            cnx->remote_lp);
291                 event->base_event.xRc = HvLpEvent_Rc_BufferNotAvailable;
292                 HvCallEvent_ackLpEvent((struct HvLpEvent *) event);
293         } else {
294                 memcpy(&cnx->cap_event, event, sizeof(cnx->cap_event));
295                 cnx->state |= VETH_STATE_GOTCAPS;
296                 veth_kick_statemachine(cnx);
297         }
298         spin_unlock_irqrestore(&cnx->lock, flags);
299 }
300
301 static void veth_take_cap_ack(struct veth_lpar_connection *cnx,
302                               struct VethLpEvent *event)
303 {
304         unsigned long flags;
305
306         spin_lock_irqsave(&cnx->lock, flags);
307         if (cnx->state & VETH_STATE_GOTCAPACK) {
308                 veth_error("Received a second capabilities ack from LPAR %d.\n",
309                            cnx->remote_lp);
310         } else {
311                 memcpy(&cnx->cap_ack_event, event,
312                        sizeof(&cnx->cap_ack_event));
313                 cnx->state |= VETH_STATE_GOTCAPACK;
314                 veth_kick_statemachine(cnx);
315         }
316         spin_unlock_irqrestore(&cnx->lock, flags);
317 }
318
319 static void veth_take_monitor_ack(struct veth_lpar_connection *cnx,
320                                   struct VethLpEvent *event)
321 {
322         unsigned long flags;
323
324         spin_lock_irqsave(&cnx->lock, flags);
325         veth_debug("cnx %d: lost connection.\n", cnx->remote_lp);
326
327         /* Avoid kicking the statemachine once we're shutdown.
328          * It's unnecessary and it could break veth_stop_connection(). */
329
330         if (! (cnx->state & VETH_STATE_SHUTDOWN)) {
331                 cnx->state |= VETH_STATE_RESET;
332                 veth_kick_statemachine(cnx);
333         }
334         spin_unlock_irqrestore(&cnx->lock, flags);
335 }
336
337 static void veth_handle_ack(struct VethLpEvent *event)
338 {
339         HvLpIndex rlp = event->base_event.xTargetLp;
340         struct veth_lpar_connection *cnx = veth_cnx[rlp];
341
342         BUG_ON(! cnx);
343
344         switch (event->base_event.xSubtype) {
345         case VethEventTypeCap:
346                 veth_take_cap_ack(cnx, event);
347                 break;
348         case VethEventTypeMonitor:
349                 veth_take_monitor_ack(cnx, event);
350                 break;
351         default:
352                 veth_error("Unknown ack type %d from LPAR %d.\n",
353                                 event->base_event.xSubtype, rlp);
354         };
355 }
356
357 static void veth_handle_int(struct VethLpEvent *event)
358 {
359         HvLpIndex rlp = event->base_event.xSourceLp;
360         struct veth_lpar_connection *cnx = veth_cnx[rlp];
361         unsigned long flags;
362         int i;
363
364         BUG_ON(! cnx);
365
366         switch (event->base_event.xSubtype) {
367         case VethEventTypeCap:
368                 veth_take_cap(cnx, event);
369                 break;
370         case VethEventTypeMonitor:
371                 /* do nothing... this'll hang out here til we're dead,
372                  * and the hypervisor will return it for us. */
373                 break;
374         case VethEventTypeFramesAck:
375                 spin_lock_irqsave(&cnx->lock, flags);
376                 for (i = 0; i < VETH_MAX_ACKS_PER_MSG; ++i) {
377                         u16 msgnum = event->u.frames_ack_data.token[i];
378
379                         if (msgnum < VETH_NUMBUFFERS)
380                                 veth_recycle_msg(cnx, cnx->msgs + msgnum);
381                 }
382                 spin_unlock_irqrestore(&cnx->lock, flags);
383                 veth_flush_pending(cnx);
384                 break;
385         case VethEventTypeFrames:
386                 veth_receive(cnx, event);
387                 break;
388         default:
389                 veth_error("Unknown interrupt type %d from LPAR %d.\n",
390                                 event->base_event.xSubtype, rlp);
391         };
392 }
393
394 static void veth_handle_event(struct HvLpEvent *event, struct pt_regs *regs)
395 {
396         struct VethLpEvent *veth_event = (struct VethLpEvent *)event;
397
398         if (event->xFlags.xFunction == HvLpEvent_Function_Ack)
399                 veth_handle_ack(veth_event);
400         else if (event->xFlags.xFunction == HvLpEvent_Function_Int)
401                 veth_handle_int(veth_event);
402 }
403
404 static int veth_process_caps(struct veth_lpar_connection *cnx)
405 {
406         struct VethCapData *remote_caps = &cnx->remote_caps;
407         int num_acks_needed;
408
409         /* Convert timer to jiffies */
410         cnx->ack_timeout = remote_caps->ack_timeout * HZ / 1000000;
411
412         if ( (remote_caps->num_buffers == 0)
413              || (remote_caps->ack_threshold > VETH_MAX_ACKS_PER_MSG)
414              || (remote_caps->ack_threshold == 0)
415              || (cnx->ack_timeout == 0) ) {
416                 veth_error("Received incompatible capabilities from LPAR %d.\n",
417                                 cnx->remote_lp);
418                 return HvLpEvent_Rc_InvalidSubtypeData;
419         }
420
421         num_acks_needed = (remote_caps->num_buffers
422                            / remote_caps->ack_threshold) + 1;
423
424         /* FIXME: locking on num_ack_events? */
425         if (cnx->num_ack_events < num_acks_needed) {
426                 int num;
427
428                 num = veth_allocate_events(cnx->remote_lp,
429                                            num_acks_needed-cnx->num_ack_events);
430                 if (num > 0)
431                         cnx->num_ack_events += num;
432
433                 if (cnx->num_ack_events < num_acks_needed) {
434                         veth_error("Couldn't allocate enough ack events "
435                                         "for LPAR %d.\n", cnx->remote_lp);
436
437                         return HvLpEvent_Rc_BufferNotAvailable;
438                 }
439         }
440
441
442         return HvLpEvent_Rc_Good;
443 }
444
445 /* FIXME: The gotos here are a bit dubious */
446 static void veth_statemachine(void *p)
447 {
448         struct veth_lpar_connection *cnx = (struct veth_lpar_connection *)p;
449         int rlp = cnx->remote_lp;
450         int rc;
451
452         spin_lock_irq(&cnx->lock);
453
454  restart:
455         if (cnx->state & VETH_STATE_RESET) {
456                 int i;
457
458                 if (cnx->state & VETH_STATE_OPEN)
459                         HvCallEvent_closeLpEventPath(cnx->remote_lp,
460                                                      HvLpEvent_Type_VirtualLan);
461
462                 /*
463                  * Reset ack data. This prevents the ack_timer actually
464                  * doing anything, even if it runs one more time when
465                  * we drop the lock below.
466                  */
467                 memset(&cnx->pending_acks, 0xff, sizeof (cnx->pending_acks));
468                 cnx->num_pending_acks = 0;
469
470                 cnx->state &= ~(VETH_STATE_RESET | VETH_STATE_SENTMON
471                                 | VETH_STATE_OPEN | VETH_STATE_SENTCAPS
472                                 | VETH_STATE_GOTCAPACK | VETH_STATE_GOTCAPS
473                                 | VETH_STATE_SENTCAPACK | VETH_STATE_READY);
474
475                 /* Clean up any leftover messages */
476                 if (cnx->msgs)
477                         for (i = 0; i < VETH_NUMBUFFERS; ++i)
478                                 veth_recycle_msg(cnx, cnx->msgs + i);
479
480                 /* Drop the lock so we can do stuff that might sleep or
481                  * take other locks. */
482                 spin_unlock_irq(&cnx->lock);
483
484                 del_timer_sync(&cnx->ack_timer);
485                 veth_flush_pending(cnx);
486
487                 spin_lock_irq(&cnx->lock);
488
489                 if (cnx->state & VETH_STATE_RESET)
490                         goto restart;
491
492                 /* Hack, wait for the other end to reset itself. */
493                 if (! (cnx->state & VETH_STATE_SHUTDOWN)) {
494                         schedule_delayed_work(&cnx->statemachine_wq, 5 * HZ);
495                         goto out;
496                 }
497         }
498
499         if (cnx->state & VETH_STATE_SHUTDOWN)
500                 /* It's all over, do nothing */
501                 goto out;
502
503         if ( !(cnx->state & VETH_STATE_OPEN) ) {
504                 if (! cnx->msgs || (cnx->num_events < (2 + VETH_NUMBUFFERS)) )
505                         goto cant_cope;
506
507                 HvCallEvent_openLpEventPath(rlp, HvLpEvent_Type_VirtualLan);
508                 cnx->src_inst =
509                         HvCallEvent_getSourceLpInstanceId(rlp,
510                                                           HvLpEvent_Type_VirtualLan);
511                 cnx->dst_inst =
512                         HvCallEvent_getTargetLpInstanceId(rlp,
513                                                           HvLpEvent_Type_VirtualLan);
514                 cnx->state |= VETH_STATE_OPEN;
515         }
516
517         if ( (cnx->state & VETH_STATE_OPEN)
518              && !(cnx->state & VETH_STATE_SENTMON) ) {
519                 rc = veth_signalevent(cnx, VethEventTypeMonitor,
520                                       HvLpEvent_AckInd_DoAck,
521                                       HvLpEvent_AckType_DeferredAck,
522                                       0, 0, 0, 0, 0, 0);
523
524                 if (rc == HvLpEvent_Rc_Good) {
525                         cnx->state |= VETH_STATE_SENTMON;
526                 } else {
527                         if ( (rc != HvLpEvent_Rc_PartitionDead)
528                              && (rc != HvLpEvent_Rc_PathClosed) )
529                                 veth_error("Error sending monitor to LPAR %d, "
530                                                 "rc = %d\n", rlp, rc);
531
532                         /* Oh well, hope we get a cap from the other
533                          * end and do better when that kicks us */
534                         goto out;
535                 }
536         }
537
538         if ( (cnx->state & VETH_STATE_OPEN)
539              && !(cnx->state & VETH_STATE_SENTCAPS)) {
540                 u64 *rawcap = (u64 *)&cnx->local_caps;
541
542                 rc = veth_signalevent(cnx, VethEventTypeCap,
543                                       HvLpEvent_AckInd_DoAck,
544                                       HvLpEvent_AckType_ImmediateAck,
545                                       0, rawcap[0], rawcap[1], rawcap[2],
546                                       rawcap[3], rawcap[4]);
547
548                 if (rc == HvLpEvent_Rc_Good) {
549                         cnx->state |= VETH_STATE_SENTCAPS;
550                 } else {
551                         if ( (rc != HvLpEvent_Rc_PartitionDead)
552                              && (rc != HvLpEvent_Rc_PathClosed) )
553                                 veth_error("Error sending caps to LPAR %d, "
554                                                 "rc = %d\n", rlp, rc);
555
556                         /* Oh well, hope we get a cap from the other
557                          * end and do better when that kicks us */
558                         goto out;
559                 }
560         }
561
562         if ((cnx->state & VETH_STATE_GOTCAPS)
563             && !(cnx->state & VETH_STATE_SENTCAPACK)) {
564                 struct VethCapData *remote_caps = &cnx->remote_caps;
565
566                 memcpy(remote_caps, &cnx->cap_event.u.caps_data,
567                        sizeof(*remote_caps));
568
569                 spin_unlock_irq(&cnx->lock);
570                 rc = veth_process_caps(cnx);
571                 spin_lock_irq(&cnx->lock);
572
573                 /* We dropped the lock, so recheck for anything which
574                  * might mess us up */
575                 if (cnx->state & (VETH_STATE_RESET|VETH_STATE_SHUTDOWN))
576                         goto restart;
577
578                 cnx->cap_event.base_event.xRc = rc;
579                 HvCallEvent_ackLpEvent((struct HvLpEvent *)&cnx->cap_event);
580                 if (rc == HvLpEvent_Rc_Good)
581                         cnx->state |= VETH_STATE_SENTCAPACK;
582                 else
583                         goto cant_cope;
584         }
585
586         if ((cnx->state & VETH_STATE_GOTCAPACK)
587             && (cnx->state & VETH_STATE_GOTCAPS)
588             && !(cnx->state & VETH_STATE_READY)) {
589                 if (cnx->cap_ack_event.base_event.xRc == HvLpEvent_Rc_Good) {
590                         /* Start the ACK timer */
591                         cnx->ack_timer.expires = jiffies + cnx->ack_timeout;
592                         add_timer(&cnx->ack_timer);
593                         cnx->state |= VETH_STATE_READY;
594                 } else {
595                         veth_error("Caps rejected by LPAR %d, rc = %d\n",
596                                         rlp, cnx->cap_ack_event.base_event.xRc);
597                         goto cant_cope;
598                 }
599         }
600
601  out:
602         spin_unlock_irq(&cnx->lock);
603         return;
604
605  cant_cope:
606         /* FIXME: we get here if something happens we really can't
607          * cope with.  The link will never work once we get here, and
608          * all we can do is not lock the rest of the system up */
609         veth_error("Unrecoverable error on connection to LPAR %d, shutting down"
610                         " (state = 0x%04lx)\n", rlp, cnx->state);
611         cnx->state |= VETH_STATE_SHUTDOWN;
612         spin_unlock_irq(&cnx->lock);
613 }
614
615 static int veth_init_connection(u8 rlp)
616 {
617         struct veth_lpar_connection *cnx;
618         struct veth_msg *msgs;
619         int i;
620
621         if ( (rlp == this_lp)
622              || ! HvLpConfig_doLpsCommunicateOnVirtualLan(this_lp, rlp) )
623                 return 0;
624
625         cnx = kmalloc(sizeof(*cnx), GFP_KERNEL);
626         if (! cnx)
627                 return -ENOMEM;
628         memset(cnx, 0, sizeof(*cnx));
629
630         cnx->remote_lp = rlp;
631         spin_lock_init(&cnx->lock);
632         INIT_WORK(&cnx->statemachine_wq, veth_statemachine, cnx);
633         init_timer(&cnx->ack_timer);
634         cnx->ack_timer.function = veth_timed_ack;
635         cnx->ack_timer.data = (unsigned long) cnx;
636         memset(&cnx->pending_acks, 0xff, sizeof (cnx->pending_acks));
637
638         veth_cnx[rlp] = cnx;
639
640         msgs = kmalloc(VETH_NUMBUFFERS * sizeof(struct veth_msg), GFP_KERNEL);
641         if (! msgs) {
642                 veth_error("Can't allocate buffers for LPAR %d.\n", rlp);
643                 return -ENOMEM;
644         }
645
646         cnx->msgs = msgs;
647         memset(msgs, 0, VETH_NUMBUFFERS * sizeof(struct veth_msg));
648         spin_lock_init(&cnx->msg_stack_lock);
649
650         for (i = 0; i < VETH_NUMBUFFERS; i++) {
651                 msgs[i].token = i;
652                 veth_stack_push(cnx, msgs + i);
653         }
654
655         cnx->num_events = veth_allocate_events(rlp, 2 + VETH_NUMBUFFERS);
656
657         if (cnx->num_events < (2 + VETH_NUMBUFFERS)) {
658                 veth_error("Can't allocate enough events for LPAR %d.\n", rlp);
659                 return -ENOMEM;
660         }
661
662         cnx->local_caps.num_buffers = VETH_NUMBUFFERS;
663         cnx->local_caps.ack_threshold = ACK_THRESHOLD;
664         cnx->local_caps.ack_timeout = VETH_ACKTIMEOUT;
665
666         return 0;
667 }
668
669 static void veth_stop_connection(u8 rlp)
670 {
671         struct veth_lpar_connection *cnx = veth_cnx[rlp];
672
673         if (! cnx)
674                 return;
675
676         spin_lock_irq(&cnx->lock);
677         cnx->state |= VETH_STATE_RESET | VETH_STATE_SHUTDOWN;
678         veth_kick_statemachine(cnx);
679         spin_unlock_irq(&cnx->lock);
680
681         /* There's a slim chance the reset code has just queued the
682          * statemachine to run in five seconds. If so we need to cancel
683          * that and requeue the work to run now. */
684         if (cancel_delayed_work(&cnx->statemachine_wq)) {
685                 spin_lock_irq(&cnx->lock);
686                 veth_kick_statemachine(cnx);
687                 spin_unlock_irq(&cnx->lock);
688         }
689
690         /* Wait for the state machine to run. */
691         flush_scheduled_work();
692
693         if (cnx->num_events > 0)
694                 mf_deallocate_lp_events(cnx->remote_lp,
695                                       HvLpEvent_Type_VirtualLan,
696                                       cnx->num_events,
697                                       NULL, NULL);
698         if (cnx->num_ack_events > 0)
699                 mf_deallocate_lp_events(cnx->remote_lp,
700                                       HvLpEvent_Type_VirtualLan,
701                                       cnx->num_ack_events,
702                                       NULL, NULL);
703 }
704
705 static void veth_destroy_connection(u8 rlp)
706 {
707         struct veth_lpar_connection *cnx = veth_cnx[rlp];
708
709         if (! cnx)
710                 return;
711
712         kfree(cnx->msgs);
713         kfree(cnx);
714         veth_cnx[rlp] = NULL;
715 }
716
717 /*
718  * net_device code
719  */
720
721 static int veth_open(struct net_device *dev)
722 {
723         struct veth_port *port = (struct veth_port *) dev->priv;
724
725         memset(&port->stats, 0, sizeof (port->stats));
726         netif_start_queue(dev);
727         return 0;
728 }
729
730 static int veth_close(struct net_device *dev)
731 {
732         netif_stop_queue(dev);
733         return 0;
734 }
735
736 static struct net_device_stats *veth_get_stats(struct net_device *dev)
737 {
738         struct veth_port *port = (struct veth_port *) dev->priv;
739
740         return &port->stats;
741 }
742
743 static int veth_change_mtu(struct net_device *dev, int new_mtu)
744 {
745         if ((new_mtu < 68) || (new_mtu > VETH_MAX_MTU))
746                 return -EINVAL;
747         dev->mtu = new_mtu;
748         return 0;
749 }
750
751 static void veth_set_multicast_list(struct net_device *dev)
752 {
753         struct veth_port *port = (struct veth_port *) dev->priv;
754         unsigned long flags;
755
756         write_lock_irqsave(&port->mcast_gate, flags);
757
758         if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
759                         (dev->mc_count > VETH_MAX_MCAST)) {
760                 port->promiscuous = 1;
761         } else {
762                 struct dev_mc_list *dmi = dev->mc_list;
763                 int i;
764
765                 port->promiscuous = 0;
766
767                 /* Update table */
768                 port->num_mcast = 0;
769
770                 for (i = 0; i < dev->mc_count; i++) {
771                         u8 *addr = dmi->dmi_addr;
772                         u64 xaddr = 0;
773
774                         if (addr[0] & 0x01) {/* multicast address? */
775                                 memcpy(&xaddr, addr, ETH_ALEN);
776                                 port->mcast_addr[port->num_mcast] = xaddr;
777                                 port->num_mcast++;
778                         }
779                         dmi = dmi->next;
780                 }
781         }
782
783         write_unlock_irqrestore(&port->mcast_gate, flags);
784 }
785
786 static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
787 {
788         strncpy(info->driver, "veth", sizeof(info->driver) - 1);
789         info->driver[sizeof(info->driver) - 1] = '\0';
790         strncpy(info->version, "1.0", sizeof(info->version) - 1);
791 }
792
793 static int veth_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
794 {
795         ecmd->supported = (SUPPORTED_1000baseT_Full
796                           | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
797         ecmd->advertising = (SUPPORTED_1000baseT_Full
798                             | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
799         ecmd->port = PORT_FIBRE;
800         ecmd->transceiver = XCVR_INTERNAL;
801         ecmd->phy_address = 0;
802         ecmd->speed = SPEED_1000;
803         ecmd->duplex = DUPLEX_FULL;
804         ecmd->autoneg = AUTONEG_ENABLE;
805         ecmd->maxtxpkt = 120;
806         ecmd->maxrxpkt = 120;
807         return 0;
808 }
809
810 static u32 veth_get_link(struct net_device *dev)
811 {
812         return 1;
813 }
814
815 static struct ethtool_ops ops = {
816         .get_drvinfo = veth_get_drvinfo,
817         .get_settings = veth_get_settings,
818         .get_link = veth_get_link,
819 };
820
821 static void veth_tx_timeout(struct net_device *dev)
822 {
823         struct veth_port *port = (struct veth_port *)dev->priv;
824         struct net_device_stats *stats = &port->stats;
825         unsigned long flags;
826         int i;
827
828         stats->tx_errors++;
829
830         spin_lock_irqsave(&port->pending_gate, flags);
831
832         if (!port->pending_lpmask) {
833                 spin_unlock_irqrestore(&port->pending_gate, flags);
834                 return;
835         }
836
837         printk(KERN_WARNING "%s: Tx timeout!  Resetting lp connections: %08x\n",
838                dev->name, port->pending_lpmask);
839
840         for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
841                 struct veth_lpar_connection *cnx = veth_cnx[i];
842
843                 if (! (port->pending_lpmask & (1<<i)))
844                         continue;
845
846                 /* If we're pending on it, we must be connected to it,
847                  * so we should certainly have a structure for it. */
848                 BUG_ON(! cnx);
849
850                 /* Theoretically we could be kicking a connection
851                  * which doesn't deserve it, but in practice if we've
852                  * had a Tx timeout, the pending_lpmask will have
853                  * exactly one bit set - the connection causing the
854                  * problem. */
855                 spin_lock(&cnx->lock);
856                 cnx->state |= VETH_STATE_RESET;
857                 veth_kick_statemachine(cnx);
858                 spin_unlock(&cnx->lock);
859         }
860
861         spin_unlock_irqrestore(&port->pending_gate, flags);
862 }
863
864 static struct net_device * __init veth_probe_one(int vlan, struct device *vdev)
865 {
866         struct net_device *dev;
867         struct veth_port *port;
868         int i, rc;
869
870         dev = alloc_etherdev(sizeof (struct veth_port));
871         if (! dev) {
872                 veth_error("Unable to allocate net_device structure!\n");
873                 return NULL;
874         }
875
876         port = (struct veth_port *) dev->priv;
877
878         spin_lock_init(&port->pending_gate);
879         rwlock_init(&port->mcast_gate);
880
881         for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
882                 HvLpVirtualLanIndexMap map;
883
884                 if (i == this_lp)
885                         continue;
886                 map = HvLpConfig_getVirtualLanIndexMapForLp(i);
887                 if (map & (0x8000 >> vlan))
888                         port->lpar_map |= (1 << i);
889         }
890         port->dev = vdev;
891
892         dev->dev_addr[0] = 0x02;
893         dev->dev_addr[1] = 0x01;
894         dev->dev_addr[2] = 0xff;
895         dev->dev_addr[3] = vlan;
896         dev->dev_addr[4] = 0xff;
897         dev->dev_addr[5] = this_lp;
898
899         dev->mtu = VETH_MAX_MTU;
900
901         memcpy(&port->mac_addr, dev->dev_addr, 6);
902
903         dev->open = veth_open;
904         dev->hard_start_xmit = veth_start_xmit;
905         dev->stop = veth_close;
906         dev->get_stats = veth_get_stats;
907         dev->change_mtu = veth_change_mtu;
908         dev->set_mac_address = NULL;
909         dev->set_multicast_list = veth_set_multicast_list;
910         SET_ETHTOOL_OPS(dev, &ops);
911
912         dev->watchdog_timeo = 2 * (VETH_ACKTIMEOUT * HZ / 1000000);
913         dev->tx_timeout = veth_tx_timeout;
914
915         SET_NETDEV_DEV(dev, vdev);
916
917         rc = register_netdev(dev);
918         if (rc != 0) {
919                 veth_error("Failed registering net device for vlan%d.\n", vlan);
920                 free_netdev(dev);
921                 return NULL;
922         }
923
924         veth_info("%s attached to iSeries vlan %d (LPAR map = 0x%.4X)\n",
925                         dev->name, vlan, port->lpar_map);
926
927         return dev;
928 }
929
930 /*
931  * Tx path
932  */
933
934 static int veth_transmit_to_one(struct sk_buff *skb, HvLpIndex rlp,
935                                 struct net_device *dev)
936 {
937         struct veth_lpar_connection *cnx = veth_cnx[rlp];
938         struct veth_port *port = (struct veth_port *) dev->priv;
939         HvLpEvent_Rc rc;
940         u32 dma_address, dma_length;
941         struct veth_msg *msg = NULL;
942         int err = 0;
943         unsigned long flags;
944
945         if (! cnx) {
946                 port->stats.tx_errors++;
947                 dev_kfree_skb(skb);
948                 return 0;
949         }
950
951         spin_lock_irqsave(&cnx->lock, flags);
952
953         if (! (cnx->state & VETH_STATE_READY))
954                 goto drop;
955
956         if ((skb->len - 14) > VETH_MAX_MTU)
957                 goto drop;
958
959         msg = veth_stack_pop(cnx);
960
961         if (! msg) {
962                 err = 1;
963                 goto drop;
964         }
965
966         dma_length = skb->len;
967         dma_address = dma_map_single(port->dev, skb->data,
968                                      dma_length, DMA_TO_DEVICE);
969
970         if (dma_mapping_error(dma_address))
971                 goto recycle_and_drop;
972
973         /* Is it really necessary to check the length and address
974          * fields of the first entry here? */
975         msg->skb = skb;
976         msg->dev = port->dev;
977         msg->data.addr[0] = dma_address;
978         msg->data.len[0] = dma_length;
979         msg->data.eofmask = 1 << VETH_EOF_SHIFT;
980         set_bit(0, &(msg->in_use));
981         rc = veth_signaldata(cnx, VethEventTypeFrames, msg->token, &msg->data);
982
983         if (rc != HvLpEvent_Rc_Good)
984                 goto recycle_and_drop;
985
986         spin_unlock_irqrestore(&cnx->lock, flags);
987         return 0;
988
989  recycle_and_drop:
990         msg->skb = NULL;
991         /* need to set in use to make veth_recycle_msg in case this
992          * was a mapping failure */
993         set_bit(0, &msg->in_use);
994         veth_recycle_msg(cnx, msg);
995  drop:
996         port->stats.tx_errors++;
997         dev_kfree_skb(skb);
998         spin_unlock_irqrestore(&cnx->lock, flags);
999         return err;
1000 }
1001
1002 static HvLpIndexMap veth_transmit_to_many(struct sk_buff *skb,
1003                                           HvLpIndexMap lpmask,
1004                                           struct net_device *dev)
1005 {
1006         struct veth_port *port = (struct veth_port *) dev->priv;
1007         int i;
1008         int rc;
1009
1010         for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
1011                 if ((lpmask & (1 << i)) == 0)
1012                         continue;
1013
1014                 rc = veth_transmit_to_one(skb_get(skb), i, dev);
1015                 if (! rc)
1016                         lpmask &= ~(1<<i);
1017         }
1018
1019         if (! lpmask) {
1020                 port->stats.tx_packets++;
1021                 port->stats.tx_bytes += skb->len;
1022         }
1023
1024         return lpmask;
1025 }
1026
1027 static int veth_start_xmit(struct sk_buff *skb, struct net_device *dev)
1028 {
1029         unsigned char *frame = skb->data;
1030         struct veth_port *port = (struct veth_port *) dev->priv;
1031         unsigned long flags;
1032         HvLpIndexMap lpmask;
1033
1034         if (! (frame[0] & 0x01)) {
1035                 /* unicast packet */
1036                 HvLpIndex rlp = frame[5];
1037
1038                 if ( ! ((1 << rlp) & port->lpar_map) ) {
1039                         dev_kfree_skb(skb);
1040                         return 0;
1041                 }
1042
1043                 lpmask = 1 << rlp;
1044         } else {
1045                 lpmask = port->lpar_map;
1046         }
1047
1048         spin_lock_irqsave(&port->pending_gate, flags);
1049
1050         lpmask = veth_transmit_to_many(skb, lpmask, dev);
1051
1052         dev->trans_start = jiffies;
1053
1054         if (! lpmask) {
1055                 dev_kfree_skb(skb);
1056         } else {
1057                 if (port->pending_skb) {
1058                         veth_error("%s: TX while skb was pending!\n",
1059                                    dev->name);
1060                         dev_kfree_skb(skb);
1061                         spin_unlock_irqrestore(&port->pending_gate, flags);
1062                         return 1;
1063                 }
1064
1065                 port->pending_skb = skb;
1066                 port->pending_lpmask = lpmask;
1067                 netif_stop_queue(dev);
1068         }
1069
1070         spin_unlock_irqrestore(&port->pending_gate, flags);
1071
1072         return 0;
1073 }
1074
1075 static void veth_recycle_msg(struct veth_lpar_connection *cnx,
1076                              struct veth_msg *msg)
1077 {
1078         u32 dma_address, dma_length;
1079
1080         if (test_and_clear_bit(0, &msg->in_use)) {
1081                 dma_address = msg->data.addr[0];
1082                 dma_length = msg->data.len[0];
1083
1084                 dma_unmap_single(msg->dev, dma_address, dma_length,
1085                                  DMA_TO_DEVICE);
1086
1087                 if (msg->skb) {
1088                         dev_kfree_skb_any(msg->skb);
1089                         msg->skb = NULL;
1090                 }
1091
1092                 memset(&msg->data, 0, sizeof(msg->data));
1093                 veth_stack_push(cnx, msg);
1094         } else if (cnx->state & VETH_STATE_OPEN) {
1095                 veth_error("Non-pending frame (# %d) acked by LPAR %d.\n",
1096                                 cnx->remote_lp, msg->token);
1097         }
1098 }
1099
1100 static void veth_flush_pending(struct veth_lpar_connection *cnx)
1101 {
1102         int i;
1103         for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++) {
1104                 struct net_device *dev = veth_dev[i];
1105                 struct veth_port *port;
1106                 unsigned long flags;
1107
1108                 if (! dev)
1109                         continue;
1110
1111                 port = (struct veth_port *)dev->priv;
1112
1113                 if (! (port->lpar_map & (1<<cnx->remote_lp)))
1114                         continue;
1115
1116                 spin_lock_irqsave(&port->pending_gate, flags);
1117                 if (port->pending_skb) {
1118                         port->pending_lpmask =
1119                                 veth_transmit_to_many(port->pending_skb,
1120                                                       port->pending_lpmask,
1121                                                       dev);
1122                         if (! port->pending_lpmask) {
1123                                 dev_kfree_skb_any(port->pending_skb);
1124                                 port->pending_skb = NULL;
1125                                 netif_wake_queue(dev);
1126                         }
1127                 }
1128                 spin_unlock_irqrestore(&port->pending_gate, flags);
1129         }
1130 }
1131
1132 /*
1133  * Rx path
1134  */
1135
1136 static inline int veth_frame_wanted(struct veth_port *port, u64 mac_addr)
1137 {
1138         int wanted = 0;
1139         int i;
1140         unsigned long flags;
1141
1142         if ( (mac_addr == port->mac_addr) || (mac_addr == 0xffffffffffff0000) )
1143                 return 1;
1144
1145         read_lock_irqsave(&port->mcast_gate, flags);
1146
1147         if (port->promiscuous) {
1148                 wanted = 1;
1149                 goto out;
1150         }
1151
1152         for (i = 0; i < port->num_mcast; ++i) {
1153                 if (port->mcast_addr[i] == mac_addr) {
1154                         wanted = 1;
1155                         break;
1156                 }
1157         }
1158
1159  out:
1160         read_unlock_irqrestore(&port->mcast_gate, flags);
1161
1162         return wanted;
1163 }
1164
1165 struct dma_chunk {
1166         u64 addr;
1167         u64 size;
1168 };
1169
1170 #define VETH_MAX_PAGES_PER_FRAME ( (VETH_MAX_MTU+PAGE_SIZE-2)/PAGE_SIZE + 1 )
1171
1172 static inline void veth_build_dma_list(struct dma_chunk *list,
1173                                        unsigned char *p, unsigned long length)
1174 {
1175         unsigned long done;
1176         int i = 1;
1177
1178         /* FIXME: skbs are continguous in real addresses.  Do we
1179          * really need to break it into PAGE_SIZE chunks, or can we do
1180          * it just at the granularity of iSeries real->absolute
1181          * mapping?  Indeed, given the way the allocator works, can we
1182          * count on them being absolutely contiguous? */
1183         list[0].addr = ISERIES_HV_ADDR(p);
1184         list[0].size = min(length,
1185                            PAGE_SIZE - ((unsigned long)p & ~PAGE_MASK));
1186
1187         done = list[0].size;
1188         while (done < length) {
1189                 list[i].addr = ISERIES_HV_ADDR(p + done);
1190                 list[i].size = min(length-done, PAGE_SIZE);
1191                 done += list[i].size;
1192                 i++;
1193         }
1194 }
1195
1196 static void veth_flush_acks(struct veth_lpar_connection *cnx)
1197 {
1198         HvLpEvent_Rc rc;
1199
1200         rc = veth_signaldata(cnx, VethEventTypeFramesAck,
1201                              0, &cnx->pending_acks);
1202
1203         if (rc != HvLpEvent_Rc_Good)
1204                 veth_error("Failed acking frames from LPAR %d, rc = %d\n",
1205                                 cnx->remote_lp, (int)rc);
1206
1207         cnx->num_pending_acks = 0;
1208         memset(&cnx->pending_acks, 0xff, sizeof(cnx->pending_acks));
1209 }
1210
1211 static void veth_receive(struct veth_lpar_connection *cnx,
1212                          struct VethLpEvent *event)
1213 {
1214         struct VethFramesData *senddata = &event->u.frames_data;
1215         int startchunk = 0;
1216         int nchunks;
1217         unsigned long flags;
1218         HvLpDma_Rc rc;
1219
1220         do {
1221                 u16 length = 0;
1222                 struct sk_buff *skb;
1223                 struct dma_chunk local_list[VETH_MAX_PAGES_PER_FRAME];
1224                 struct dma_chunk remote_list[VETH_MAX_FRAMES_PER_MSG];
1225                 u64 dest;
1226                 HvLpVirtualLanIndex vlan;
1227                 struct net_device *dev;
1228                 struct veth_port *port;
1229
1230                 /* FIXME: do we need this? */
1231                 memset(local_list, 0, sizeof(local_list));
1232                 memset(remote_list, 0, sizeof(VETH_MAX_FRAMES_PER_MSG));
1233
1234                 /* a 0 address marks the end of the valid entries */
1235                 if (senddata->addr[startchunk] == 0)
1236                         break;
1237
1238                 /* make sure that we have at least 1 EOF entry in the
1239                  * remaining entries */
1240                 if (! (senddata->eofmask >> (startchunk + VETH_EOF_SHIFT))) {
1241                         veth_error("Missing EOF fragment in event "
1242                                         "eofmask = 0x%x startchunk = %d\n",
1243                                         (unsigned)senddata->eofmask,
1244                                         startchunk);
1245                         break;
1246                 }
1247
1248                 /* build list of chunks in this frame */
1249                 nchunks = 0;
1250                 do {
1251                         remote_list[nchunks].addr =
1252                                 (u64) senddata->addr[startchunk+nchunks] << 32;
1253                         remote_list[nchunks].size =
1254                                 senddata->len[startchunk+nchunks];
1255                         length += remote_list[nchunks].size;
1256                 } while (! (senddata->eofmask &
1257                             (1 << (VETH_EOF_SHIFT + startchunk + nchunks++))));
1258
1259                 /* length == total length of all chunks */
1260                 /* nchunks == # of chunks in this frame */
1261
1262                 if ((length - ETH_HLEN) > VETH_MAX_MTU) {
1263                         veth_error("Received oversize frame from LPAR %d "
1264                                         "(length = %d)\n",
1265                                         cnx->remote_lp, length);
1266                         continue;
1267                 }
1268
1269                 skb = alloc_skb(length, GFP_ATOMIC);
1270                 if (!skb)
1271                         continue;
1272
1273                 veth_build_dma_list(local_list, skb->data, length);
1274
1275                 rc = HvCallEvent_dmaBufList(HvLpEvent_Type_VirtualLan,
1276                                             event->base_event.xSourceLp,
1277                                             HvLpDma_Direction_RemoteToLocal,
1278                                             cnx->src_inst,
1279                                             cnx->dst_inst,
1280                                             HvLpDma_AddressType_RealAddress,
1281                                             HvLpDma_AddressType_TceIndex,
1282                                             ISERIES_HV_ADDR(&local_list),
1283                                             ISERIES_HV_ADDR(&remote_list),
1284                                             length);
1285                 if (rc != HvLpDma_Rc_Good) {
1286                         dev_kfree_skb_irq(skb);
1287                         continue;
1288                 }
1289
1290                 vlan = skb->data[9];
1291                 dev = veth_dev[vlan];
1292                 if (! dev) {
1293                         /*
1294                          * Some earlier versions of the driver sent
1295                          * broadcasts down all connections, even to lpars
1296                          * that weren't on the relevant vlan. So ignore
1297                          * packets belonging to a vlan we're not on.
1298                          * We can also be here if we receive packets while
1299                          * the driver is going down, because then dev is NULL.
1300                          */
1301                         dev_kfree_skb_irq(skb);
1302                         continue;
1303                 }
1304
1305                 port = (struct veth_port *)dev->priv;
1306                 dest = *((u64 *) skb->data) & 0xFFFFFFFFFFFF0000;
1307
1308                 if ((vlan > HVMAXARCHITECTEDVIRTUALLANS) || !port) {
1309                         dev_kfree_skb_irq(skb);
1310                         continue;
1311                 }
1312                 if (! veth_frame_wanted(port, dest)) {
1313                         dev_kfree_skb_irq(skb);
1314                         continue;
1315                 }
1316
1317                 skb_put(skb, length);
1318                 skb->dev = dev;
1319                 skb->protocol = eth_type_trans(skb, dev);
1320                 skb->ip_summed = CHECKSUM_NONE;
1321                 netif_rx(skb);  /* send it up */
1322                 port->stats.rx_packets++;
1323                 port->stats.rx_bytes += length;
1324         } while (startchunk += nchunks, startchunk < VETH_MAX_FRAMES_PER_MSG);
1325
1326         /* Ack it */
1327         spin_lock_irqsave(&cnx->lock, flags);
1328         BUG_ON(cnx->num_pending_acks > VETH_MAX_ACKS_PER_MSG);
1329
1330         cnx->pending_acks[cnx->num_pending_acks++] =
1331                 event->base_event.xCorrelationToken;
1332
1333         if ( (cnx->num_pending_acks >= cnx->remote_caps.ack_threshold)
1334              || (cnx->num_pending_acks >= VETH_MAX_ACKS_PER_MSG) )
1335                 veth_flush_acks(cnx);
1336
1337         spin_unlock_irqrestore(&cnx->lock, flags);
1338 }
1339
1340 static void veth_timed_ack(unsigned long ptr)
1341 {
1342         struct veth_lpar_connection *cnx = (struct veth_lpar_connection *) ptr;
1343         unsigned long flags;
1344
1345         /* Ack all the events */
1346         spin_lock_irqsave(&cnx->lock, flags);
1347         if (cnx->num_pending_acks > 0)
1348                 veth_flush_acks(cnx);
1349
1350         /* Reschedule the timer */
1351         cnx->ack_timer.expires = jiffies + cnx->ack_timeout;
1352         add_timer(&cnx->ack_timer);
1353         spin_unlock_irqrestore(&cnx->lock, flags);
1354 }
1355
1356 static int veth_remove(struct vio_dev *vdev)
1357 {
1358         int i = vdev->unit_address;
1359         struct net_device *dev;
1360
1361         dev = veth_dev[i];
1362         if (dev != NULL) {
1363                 veth_dev[i] = NULL;
1364                 unregister_netdev(dev);
1365                 free_netdev(dev);
1366         }
1367         return 0;
1368 }
1369
1370 static int veth_probe(struct vio_dev *vdev, const struct vio_device_id *id)
1371 {
1372         int i = vdev->unit_address;
1373         struct net_device *dev;
1374
1375         dev = veth_probe_one(i, &vdev->dev);
1376         if (dev == NULL) {
1377                 veth_remove(vdev);
1378                 return 1;
1379         }
1380         veth_dev[i] = dev;
1381
1382         /* Start the state machine on each connection, to commence
1383          * link negotiation */
1384         for (i = 0; i < HVMAXARCHITECTEDLPS; i++)
1385                 if (veth_cnx[i])
1386                         veth_kick_statemachine(veth_cnx[i]);
1387
1388         return 0;
1389 }
1390
1391 /**
1392  * veth_device_table: Used by vio.c to match devices that we
1393  * support.
1394  */
1395 static struct vio_device_id veth_device_table[] __devinitdata = {
1396         { "vlan", "" },
1397         { "", "" }
1398 };
1399 MODULE_DEVICE_TABLE(vio, veth_device_table);
1400
1401 static struct vio_driver veth_driver = {
1402         .name = "iseries_veth",
1403         .id_table = veth_device_table,
1404         .probe = veth_probe,
1405         .remove = veth_remove
1406 };
1407
1408 /*
1409  * Module initialization/cleanup
1410  */
1411
1412 void __exit veth_module_cleanup(void)
1413 {
1414         int i;
1415
1416         /* Stop the queues first to stop any new packets being sent. */
1417         for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++)
1418                 if (veth_dev[i])
1419                         netif_stop_queue(veth_dev[i]);
1420
1421         /* Stop the connections before we unregister the driver. This
1422          * ensures there's no skbs lying around holding the device open. */
1423         for (i = 0; i < HVMAXARCHITECTEDLPS; ++i)
1424                 veth_stop_connection(i);
1425
1426         HvLpEvent_unregisterHandler(HvLpEvent_Type_VirtualLan);
1427
1428         /* Hypervisor callbacks may have scheduled more work while we
1429          * were stoping connections. Now that we've disconnected from
1430          * the hypervisor make sure everything's finished. */
1431         flush_scheduled_work();
1432
1433         vio_unregister_driver(&veth_driver);
1434
1435         for (i = 0; i < HVMAXARCHITECTEDLPS; ++i)
1436                 veth_destroy_connection(i);
1437
1438 }
1439 module_exit(veth_module_cleanup);
1440
1441 int __init veth_module_init(void)
1442 {
1443         int i;
1444         int rc;
1445
1446         this_lp = HvLpConfig_getLpIndex_outline();
1447
1448         for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
1449                 rc = veth_init_connection(i);
1450                 if (rc != 0) {
1451                         veth_module_cleanup();
1452                         return rc;
1453                 }
1454         }
1455
1456         HvLpEvent_registerHandler(HvLpEvent_Type_VirtualLan,
1457                                   &veth_handle_event);
1458
1459         return vio_register_driver(&veth_driver);
1460 }
1461 module_init(veth_module_init);