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