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