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