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