blob: ab9fb218d1117f256c0d0417467413f0ede0b80e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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
Michael Ellerman61a3c692005-09-01 11:28:57 +100082#undef DEBUG
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#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;
Michael Ellermanb08bd5c2005-09-01 11:29:06 +1000120 int in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 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
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct veth_msg *msg_stack_head;
147};
148
149struct veth_port {
150 struct device *dev;
151 struct net_device_stats stats;
152 u64 mac_addr;
153 HvLpIndexMap lpar_map;
154
155 spinlock_t pending_gate;
156 struct sk_buff *pending_skb;
157 HvLpIndexMap pending_lpmask;
158
159 rwlock_t mcast_gate;
160 int promiscuous;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 int num_mcast;
162 u64 mcast_addr[VETH_MAX_MCAST];
163};
164
165static HvLpIndex this_lp;
166static struct veth_lpar_connection *veth_cnx[HVMAXARCHITECTEDLPS]; /* = 0 */
167static struct net_device *veth_dev[HVMAXARCHITECTEDVIRTUALLANS]; /* = 0 */
168
169static int veth_start_xmit(struct sk_buff *skb, struct net_device *dev);
170static void veth_recycle_msg(struct veth_lpar_connection *, struct veth_msg *);
171static void veth_flush_pending(struct veth_lpar_connection *cnx);
172static void veth_receive(struct veth_lpar_connection *, struct VethLpEvent *);
173static void veth_timed_ack(unsigned long connectionPtr);
174
175/*
176 * Utility functions
177 */
178
Michael Ellerman61a3c692005-09-01 11:28:57 +1000179#define veth_info(fmt, args...) \
180 printk(KERN_INFO "iseries_veth: " fmt, ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182#define veth_error(fmt, args...) \
Michael Ellerman61a3c692005-09-01 11:28:57 +1000183 printk(KERN_ERR "iseries_veth: Error: " fmt, ## args)
184
185#ifdef DEBUG
186#define veth_debug(fmt, args...) \
187 printk(KERN_DEBUG "iseries_veth: " fmt, ## args)
188#else
189#define veth_debug(fmt, args...) do {} while (0)
190#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Michael Ellermand7893dd2005-09-01 11:29:05 +1000192/* You must hold the connection's lock when you call this function. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static inline void veth_stack_push(struct veth_lpar_connection *cnx,
194 struct veth_msg *msg)
195{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 msg->next = cnx->msg_stack_head;
197 cnx->msg_stack_head = msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Michael Ellermand7893dd2005-09-01 11:29:05 +1000200/* You must hold the connection's lock when you call this function. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static inline struct veth_msg *veth_stack_pop(struct veth_lpar_connection *cnx)
202{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 struct veth_msg *msg;
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 msg = cnx->msg_stack_head;
206 if (msg)
207 cnx->msg_stack_head = cnx->msg_stack_head->next;
Michael Ellermand7893dd2005-09-01 11:29:05 +1000208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return msg;
210}
211
212static inline HvLpEvent_Rc
213veth_signalevent(struct veth_lpar_connection *cnx, u16 subtype,
214 HvLpEvent_AckInd ackind, HvLpEvent_AckType acktype,
215 u64 token,
216 u64 data1, u64 data2, u64 data3, u64 data4, u64 data5)
217{
218 return HvCallEvent_signalLpEventFast(cnx->remote_lp,
219 HvLpEvent_Type_VirtualLan,
220 subtype, ackind, acktype,
221 cnx->src_inst,
222 cnx->dst_inst,
223 token, data1, data2, data3,
224 data4, data5);
225}
226
227static inline HvLpEvent_Rc veth_signaldata(struct veth_lpar_connection *cnx,
228 u16 subtype, u64 token, void *data)
229{
230 u64 *p = (u64 *) data;
231
232 return veth_signalevent(cnx, subtype, HvLpEvent_AckInd_NoAck,
233 HvLpEvent_AckType_ImmediateAck,
234 token, p[0], p[1], p[2], p[3], p[4]);
235}
236
237struct veth_allocation {
238 struct completion c;
239 int num;
240};
241
242static void veth_complete_allocation(void *parm, int number)
243{
244 struct veth_allocation *vc = (struct veth_allocation *)parm;
245
246 vc->num = number;
247 complete(&vc->c);
248}
249
250static int veth_allocate_events(HvLpIndex rlp, int number)
251{
252 struct veth_allocation vc = { COMPLETION_INITIALIZER(vc.c), 0 };
253
254 mf_allocate_lp_events(rlp, HvLpEvent_Type_VirtualLan,
255 sizeof(struct VethLpEvent), number,
256 &veth_complete_allocation, &vc);
257 wait_for_completion(&vc.c);
258
259 return vc.num;
260}
261
262/*
263 * LPAR connection code
264 */
265
266static inline void veth_kick_statemachine(struct veth_lpar_connection *cnx)
267{
268 schedule_work(&cnx->statemachine_wq);
269}
270
271static void veth_take_cap(struct veth_lpar_connection *cnx,
272 struct VethLpEvent *event)
273{
274 unsigned long flags;
275
276 spin_lock_irqsave(&cnx->lock, flags);
277 /* Receiving caps may mean the other end has just come up, so
278 * we need to reload the instance ID of the far end */
279 cnx->dst_inst =
280 HvCallEvent_getTargetLpInstanceId(cnx->remote_lp,
281 HvLpEvent_Type_VirtualLan);
282
283 if (cnx->state & VETH_STATE_GOTCAPS) {
Michael Ellerman61a3c692005-09-01 11:28:57 +1000284 veth_error("Received a second capabilities from LPAR %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 cnx->remote_lp);
286 event->base_event.xRc = HvLpEvent_Rc_BufferNotAvailable;
287 HvCallEvent_ackLpEvent((struct HvLpEvent *) event);
288 } else {
289 memcpy(&cnx->cap_event, event, sizeof(cnx->cap_event));
290 cnx->state |= VETH_STATE_GOTCAPS;
291 veth_kick_statemachine(cnx);
292 }
293 spin_unlock_irqrestore(&cnx->lock, flags);
294}
295
296static void veth_take_cap_ack(struct veth_lpar_connection *cnx,
297 struct VethLpEvent *event)
298{
299 unsigned long flags;
300
301 spin_lock_irqsave(&cnx->lock, flags);
302 if (cnx->state & VETH_STATE_GOTCAPACK) {
Michael Ellerman61a3c692005-09-01 11:28:57 +1000303 veth_error("Received a second capabilities ack from LPAR %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 cnx->remote_lp);
305 } else {
306 memcpy(&cnx->cap_ack_event, event,
307 sizeof(&cnx->cap_ack_event));
308 cnx->state |= VETH_STATE_GOTCAPACK;
309 veth_kick_statemachine(cnx);
310 }
311 spin_unlock_irqrestore(&cnx->lock, flags);
312}
313
314static void veth_take_monitor_ack(struct veth_lpar_connection *cnx,
315 struct VethLpEvent *event)
316{
317 unsigned long flags;
318
319 spin_lock_irqsave(&cnx->lock, flags);
Michael Ellerman61a3c692005-09-01 11:28:57 +1000320 veth_debug("cnx %d: lost connection.\n", cnx->remote_lp);
Michael Ellerman58c59002005-09-01 11:29:00 +1000321
322 /* Avoid kicking the statemachine once we're shutdown.
323 * It's unnecessary and it could break veth_stop_connection(). */
324
325 if (! (cnx->state & VETH_STATE_SHUTDOWN)) {
326 cnx->state |= VETH_STATE_RESET;
327 veth_kick_statemachine(cnx);
328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 spin_unlock_irqrestore(&cnx->lock, flags);
330}
331
332static 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:
Michael Ellerman61a3c692005-09-01 11:28:57 +1000347 veth_error("Unknown ack type %d from LPAR %d.\n",
348 event->base_event.xSubtype, rlp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 };
350}
351
352static 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:
Michael Ellerman61a3c692005-09-01 11:28:57 +1000384 veth_error("Unknown interrupt type %d from LPAR %d.\n",
385 event->base_event.xSubtype, rlp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 };
387}
388
389static 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
399static 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) ) {
Michael Ellerman61a3c692005-09-01 11:28:57 +1000411 veth_error("Received incompatible capabilities from LPAR %d.\n",
412 cnx->remote_lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 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) {
Michael Ellerman61a3c692005-09-01 11:28:57 +1000429 veth_error("Couldn't allocate enough ack events "
430 "for LPAR %d.\n", cnx->remote_lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
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 */
441static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (cnx->state & VETH_STATE_OPEN)
454 HvCallEvent_closeLpEventPath(cnx->remote_lp,
455 HvLpEvent_Type_VirtualLan);
456
Michael Ellermanabfda472005-09-01 11:28:59 +1000457 /*
458 * Reset ack data. This prevents the ack_timer actually
459 * doing anything, even if it runs one more time when
460 * we drop the lock below.
461 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 memset(&cnx->pending_acks, 0xff, sizeof (cnx->pending_acks));
463 cnx->num_pending_acks = 0;
464
465 cnx->state &= ~(VETH_STATE_RESET | VETH_STATE_SENTMON
466 | VETH_STATE_OPEN | VETH_STATE_SENTCAPS
467 | VETH_STATE_GOTCAPACK | VETH_STATE_GOTCAPS
468 | VETH_STATE_SENTCAPACK | VETH_STATE_READY);
469
470 /* Clean up any leftover messages */
471 if (cnx->msgs)
472 for (i = 0; i < VETH_NUMBUFFERS; ++i)
473 veth_recycle_msg(cnx, cnx->msgs + i);
Michael Ellermanabfda472005-09-01 11:28:59 +1000474
475 /* Drop the lock so we can do stuff that might sleep or
476 * take other locks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 spin_unlock_irq(&cnx->lock);
Michael Ellermanabfda472005-09-01 11:28:59 +1000478
479 del_timer_sync(&cnx->ack_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 veth_flush_pending(cnx);
Michael Ellermanabfda472005-09-01 11:28:59 +1000481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 spin_lock_irq(&cnx->lock);
Michael Ellermanabfda472005-09-01 11:28:59 +1000483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (cnx->state & VETH_STATE_RESET)
485 goto restart;
Michael Ellerman58c59002005-09-01 11:29:00 +1000486
487 /* Hack, wait for the other end to reset itself. */
488 if (! (cnx->state & VETH_STATE_SHUTDOWN)) {
489 schedule_delayed_work(&cnx->statemachine_wq, 5 * HZ);
490 goto out;
491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493
494 if (cnx->state & VETH_STATE_SHUTDOWN)
495 /* It's all over, do nothing */
496 goto out;
497
498 if ( !(cnx->state & VETH_STATE_OPEN) ) {
499 if (! cnx->msgs || (cnx->num_events < (2 + VETH_NUMBUFFERS)) )
500 goto cant_cope;
501
502 HvCallEvent_openLpEventPath(rlp, HvLpEvent_Type_VirtualLan);
503 cnx->src_inst =
504 HvCallEvent_getSourceLpInstanceId(rlp,
505 HvLpEvent_Type_VirtualLan);
506 cnx->dst_inst =
507 HvCallEvent_getTargetLpInstanceId(rlp,
508 HvLpEvent_Type_VirtualLan);
509 cnx->state |= VETH_STATE_OPEN;
510 }
511
512 if ( (cnx->state & VETH_STATE_OPEN)
513 && !(cnx->state & VETH_STATE_SENTMON) ) {
514 rc = veth_signalevent(cnx, VethEventTypeMonitor,
515 HvLpEvent_AckInd_DoAck,
516 HvLpEvent_AckType_DeferredAck,
517 0, 0, 0, 0, 0, 0);
518
519 if (rc == HvLpEvent_Rc_Good) {
520 cnx->state |= VETH_STATE_SENTMON;
521 } else {
522 if ( (rc != HvLpEvent_Rc_PartitionDead)
523 && (rc != HvLpEvent_Rc_PathClosed) )
Michael Ellerman61a3c692005-09-01 11:28:57 +1000524 veth_error("Error sending monitor to LPAR %d, "
525 "rc = %d\n", rlp, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 /* Oh well, hope we get a cap from the other
528 * end and do better when that kicks us */
529 goto out;
530 }
531 }
532
533 if ( (cnx->state & VETH_STATE_OPEN)
534 && !(cnx->state & VETH_STATE_SENTCAPS)) {
535 u64 *rawcap = (u64 *)&cnx->local_caps;
536
537 rc = veth_signalevent(cnx, VethEventTypeCap,
538 HvLpEvent_AckInd_DoAck,
539 HvLpEvent_AckType_ImmediateAck,
540 0, rawcap[0], rawcap[1], rawcap[2],
541 rawcap[3], rawcap[4]);
542
543 if (rc == HvLpEvent_Rc_Good) {
544 cnx->state |= VETH_STATE_SENTCAPS;
545 } else {
546 if ( (rc != HvLpEvent_Rc_PartitionDead)
547 && (rc != HvLpEvent_Rc_PathClosed) )
Michael Ellerman61a3c692005-09-01 11:28:57 +1000548 veth_error("Error sending caps to LPAR %d, "
549 "rc = %d\n", rlp, rc);
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /* Oh well, hope we get a cap from the other
552 * end and do better when that kicks us */
553 goto out;
554 }
555 }
556
557 if ((cnx->state & VETH_STATE_GOTCAPS)
558 && !(cnx->state & VETH_STATE_SENTCAPACK)) {
559 struct VethCapData *remote_caps = &cnx->remote_caps;
560
561 memcpy(remote_caps, &cnx->cap_event.u.caps_data,
562 sizeof(*remote_caps));
563
564 spin_unlock_irq(&cnx->lock);
565 rc = veth_process_caps(cnx);
566 spin_lock_irq(&cnx->lock);
567
568 /* We dropped the lock, so recheck for anything which
569 * might mess us up */
570 if (cnx->state & (VETH_STATE_RESET|VETH_STATE_SHUTDOWN))
571 goto restart;
572
573 cnx->cap_event.base_event.xRc = rc;
574 HvCallEvent_ackLpEvent((struct HvLpEvent *)&cnx->cap_event);
575 if (rc == HvLpEvent_Rc_Good)
576 cnx->state |= VETH_STATE_SENTCAPACK;
577 else
578 goto cant_cope;
579 }
580
581 if ((cnx->state & VETH_STATE_GOTCAPACK)
582 && (cnx->state & VETH_STATE_GOTCAPS)
583 && !(cnx->state & VETH_STATE_READY)) {
584 if (cnx->cap_ack_event.base_event.xRc == HvLpEvent_Rc_Good) {
585 /* Start the ACK timer */
586 cnx->ack_timer.expires = jiffies + cnx->ack_timeout;
587 add_timer(&cnx->ack_timer);
588 cnx->state |= VETH_STATE_READY;
589 } else {
Michael Ellerman61a3c692005-09-01 11:28:57 +1000590 veth_error("Caps rejected by LPAR %d, rc = %d\n",
591 rlp, cnx->cap_ack_event.base_event.xRc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 goto cant_cope;
593 }
594 }
595
596 out:
597 spin_unlock_irq(&cnx->lock);
598 return;
599
600 cant_cope:
601 /* FIXME: we get here if something happens we really can't
602 * cope with. The link will never work once we get here, and
603 * all we can do is not lock the rest of the system up */
Michael Ellerman61a3c692005-09-01 11:28:57 +1000604 veth_error("Unrecoverable error on connection to LPAR %d, shutting down"
605 " (state = 0x%04lx)\n", rlp, cnx->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 cnx->state |= VETH_STATE_SHUTDOWN;
607 spin_unlock_irq(&cnx->lock);
608}
609
610static int veth_init_connection(u8 rlp)
611{
612 struct veth_lpar_connection *cnx;
613 struct veth_msg *msgs;
614 int i;
615
616 if ( (rlp == this_lp)
617 || ! HvLpConfig_doLpsCommunicateOnVirtualLan(this_lp, rlp) )
618 return 0;
619
620 cnx = kmalloc(sizeof(*cnx), GFP_KERNEL);
621 if (! cnx)
622 return -ENOMEM;
623 memset(cnx, 0, sizeof(*cnx));
624
625 cnx->remote_lp = rlp;
626 spin_lock_init(&cnx->lock);
627 INIT_WORK(&cnx->statemachine_wq, veth_statemachine, cnx);
628 init_timer(&cnx->ack_timer);
629 cnx->ack_timer.function = veth_timed_ack;
630 cnx->ack_timer.data = (unsigned long) cnx;
631 memset(&cnx->pending_acks, 0xff, sizeof (cnx->pending_acks));
632
633 veth_cnx[rlp] = cnx;
634
635 msgs = kmalloc(VETH_NUMBUFFERS * sizeof(struct veth_msg), GFP_KERNEL);
636 if (! msgs) {
Michael Ellerman61a3c692005-09-01 11:28:57 +1000637 veth_error("Can't allocate buffers for LPAR %d.\n", rlp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return -ENOMEM;
639 }
640
641 cnx->msgs = msgs;
642 memset(msgs, 0, VETH_NUMBUFFERS * sizeof(struct veth_msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 for (i = 0; i < VETH_NUMBUFFERS; i++) {
645 msgs[i].token = i;
646 veth_stack_push(cnx, msgs + i);
647 }
648
649 cnx->num_events = veth_allocate_events(rlp, 2 + VETH_NUMBUFFERS);
650
651 if (cnx->num_events < (2 + VETH_NUMBUFFERS)) {
Michael Ellerman61a3c692005-09-01 11:28:57 +1000652 veth_error("Can't allocate enough events for LPAR %d.\n", rlp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return -ENOMEM;
654 }
655
656 cnx->local_caps.num_buffers = VETH_NUMBUFFERS;
657 cnx->local_caps.ack_threshold = ACK_THRESHOLD;
658 cnx->local_caps.ack_timeout = VETH_ACKTIMEOUT;
659
660 return 0;
661}
662
663static void veth_stop_connection(u8 rlp)
664{
665 struct veth_lpar_connection *cnx = veth_cnx[rlp];
666
667 if (! cnx)
668 return;
669
670 spin_lock_irq(&cnx->lock);
671 cnx->state |= VETH_STATE_RESET | VETH_STATE_SHUTDOWN;
672 veth_kick_statemachine(cnx);
673 spin_unlock_irq(&cnx->lock);
674
Michael Ellerman58c59002005-09-01 11:29:00 +1000675 /* There's a slim chance the reset code has just queued the
676 * statemachine to run in five seconds. If so we need to cancel
677 * that and requeue the work to run now. */
678 if (cancel_delayed_work(&cnx->statemachine_wq)) {
679 spin_lock_irq(&cnx->lock);
680 veth_kick_statemachine(cnx);
681 spin_unlock_irq(&cnx->lock);
682 }
683
Michael Ellermanabfda472005-09-01 11:28:59 +1000684 /* Wait for the state machine to run. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 flush_scheduled_work();
Michael Ellermanec60bee2005-09-01 11:29:08 +1000686}
687
688static void veth_destroy_connection(u8 rlp)
689{
690 struct veth_lpar_connection *cnx = veth_cnx[rlp];
691
692 if (! cnx)
693 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (cnx->num_events > 0)
696 mf_deallocate_lp_events(cnx->remote_lp,
697 HvLpEvent_Type_VirtualLan,
698 cnx->num_events,
699 NULL, NULL);
700 if (cnx->num_ack_events > 0)
701 mf_deallocate_lp_events(cnx->remote_lp,
702 HvLpEvent_Type_VirtualLan,
703 cnx->num_ack_events,
704 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 kfree(cnx->msgs);
707 kfree(cnx);
708 veth_cnx[rlp] = NULL;
709}
710
711/*
712 * net_device code
713 */
714
715static int veth_open(struct net_device *dev)
716{
717 struct veth_port *port = (struct veth_port *) dev->priv;
718
719 memset(&port->stats, 0, sizeof (port->stats));
720 netif_start_queue(dev);
721 return 0;
722}
723
724static int veth_close(struct net_device *dev)
725{
726 netif_stop_queue(dev);
727 return 0;
728}
729
730static struct net_device_stats *veth_get_stats(struct net_device *dev)
731{
732 struct veth_port *port = (struct veth_port *) dev->priv;
733
734 return &port->stats;
735}
736
737static int veth_change_mtu(struct net_device *dev, int new_mtu)
738{
739 if ((new_mtu < 68) || (new_mtu > VETH_MAX_MTU))
740 return -EINVAL;
741 dev->mtu = new_mtu;
742 return 0;
743}
744
745static void veth_set_multicast_list(struct net_device *dev)
746{
747 struct veth_port *port = (struct veth_port *) dev->priv;
748 unsigned long flags;
749
750 write_lock_irqsave(&port->mcast_gate, flags);
751
Michael Ellerman2a5391a2005-09-01 11:29:02 +1000752 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
753 (dev->mc_count > VETH_MAX_MCAST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 port->promiscuous = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 } else {
756 struct dev_mc_list *dmi = dev->mc_list;
757 int i;
758
Michael Ellerman2a5391a2005-09-01 11:29:02 +1000759 port->promiscuous = 0;
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /* Update table */
762 port->num_mcast = 0;
763
764 for (i = 0; i < dev->mc_count; i++) {
765 u8 *addr = dmi->dmi_addr;
766 u64 xaddr = 0;
767
768 if (addr[0] & 0x01) {/* multicast address? */
769 memcpy(&xaddr, addr, ETH_ALEN);
770 port->mcast_addr[port->num_mcast] = xaddr;
771 port->num_mcast++;
772 }
773 dmi = dmi->next;
774 }
775 }
776
777 write_unlock_irqrestore(&port->mcast_gate, flags);
778}
779
780static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
781{
782 strncpy(info->driver, "veth", sizeof(info->driver) - 1);
783 info->driver[sizeof(info->driver) - 1] = '\0';
784 strncpy(info->version, "1.0", sizeof(info->version) - 1);
785}
786
787static int veth_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
788{
789 ecmd->supported = (SUPPORTED_1000baseT_Full
790 | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
791 ecmd->advertising = (SUPPORTED_1000baseT_Full
792 | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
793 ecmd->port = PORT_FIBRE;
794 ecmd->transceiver = XCVR_INTERNAL;
795 ecmd->phy_address = 0;
796 ecmd->speed = SPEED_1000;
797 ecmd->duplex = DUPLEX_FULL;
798 ecmd->autoneg = AUTONEG_ENABLE;
799 ecmd->maxtxpkt = 120;
800 ecmd->maxrxpkt = 120;
801 return 0;
802}
803
804static u32 veth_get_link(struct net_device *dev)
805{
806 return 1;
807}
808
809static struct ethtool_ops ops = {
810 .get_drvinfo = veth_get_drvinfo,
811 .get_settings = veth_get_settings,
812 .get_link = veth_get_link,
813};
814
815static void veth_tx_timeout(struct net_device *dev)
816{
817 struct veth_port *port = (struct veth_port *)dev->priv;
818 struct net_device_stats *stats = &port->stats;
819 unsigned long flags;
820 int i;
821
822 stats->tx_errors++;
823
824 spin_lock_irqsave(&port->pending_gate, flags);
825
Michael Ellerman243cd552005-06-09 12:36:33 -0700826 if (!port->pending_lpmask) {
827 spin_unlock_irqrestore(&port->pending_gate, flags);
828 return;
829 }
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 printk(KERN_WARNING "%s: Tx timeout! Resetting lp connections: %08x\n",
832 dev->name, port->pending_lpmask);
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
835 struct veth_lpar_connection *cnx = veth_cnx[i];
836
837 if (! (port->pending_lpmask & (1<<i)))
838 continue;
839
840 /* If we're pending on it, we must be connected to it,
841 * so we should certainly have a structure for it. */
842 BUG_ON(! cnx);
843
844 /* Theoretically we could be kicking a connection
845 * which doesn't deserve it, but in practice if we've
846 * had a Tx timeout, the pending_lpmask will have
847 * exactly one bit set - the connection causing the
848 * problem. */
849 spin_lock(&cnx->lock);
850 cnx->state |= VETH_STATE_RESET;
851 veth_kick_statemachine(cnx);
852 spin_unlock(&cnx->lock);
853 }
854
855 spin_unlock_irqrestore(&port->pending_gate, flags);
856}
857
858static struct net_device * __init veth_probe_one(int vlan, struct device *vdev)
859{
860 struct net_device *dev;
861 struct veth_port *port;
862 int i, rc;
863
864 dev = alloc_etherdev(sizeof (struct veth_port));
865 if (! dev) {
866 veth_error("Unable to allocate net_device structure!\n");
867 return NULL;
868 }
869
870 port = (struct veth_port *) dev->priv;
871
872 spin_lock_init(&port->pending_gate);
873 rwlock_init(&port->mcast_gate);
874
875 for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
876 HvLpVirtualLanIndexMap map;
877
878 if (i == this_lp)
879 continue;
880 map = HvLpConfig_getVirtualLanIndexMapForLp(i);
881 if (map & (0x8000 >> vlan))
882 port->lpar_map |= (1 << i);
883 }
884 port->dev = vdev;
885
886 dev->dev_addr[0] = 0x02;
887 dev->dev_addr[1] = 0x01;
888 dev->dev_addr[2] = 0xff;
889 dev->dev_addr[3] = vlan;
890 dev->dev_addr[4] = 0xff;
891 dev->dev_addr[5] = this_lp;
892
893 dev->mtu = VETH_MAX_MTU;
894
895 memcpy(&port->mac_addr, dev->dev_addr, 6);
896
897 dev->open = veth_open;
898 dev->hard_start_xmit = veth_start_xmit;
899 dev->stop = veth_close;
900 dev->get_stats = veth_get_stats;
901 dev->change_mtu = veth_change_mtu;
902 dev->set_mac_address = NULL;
903 dev->set_multicast_list = veth_set_multicast_list;
904 SET_ETHTOOL_OPS(dev, &ops);
905
906 dev->watchdog_timeo = 2 * (VETH_ACKTIMEOUT * HZ / 1000000);
907 dev->tx_timeout = veth_tx_timeout;
908
909 SET_NETDEV_DEV(dev, vdev);
910
911 rc = register_netdev(dev);
912 if (rc != 0) {
Michael Ellerman61a3c692005-09-01 11:28:57 +1000913 veth_error("Failed registering net device for vlan%d.\n", vlan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 free_netdev(dev);
915 return NULL;
916 }
917
Michael Ellerman61a3c692005-09-01 11:28:57 +1000918 veth_info("%s attached to iSeries vlan %d (LPAR map = 0x%.4X)\n",
919 dev->name, vlan, port->lpar_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 return dev;
922}
923
924/*
925 * Tx path
926 */
927
928static int veth_transmit_to_one(struct sk_buff *skb, HvLpIndex rlp,
929 struct net_device *dev)
930{
931 struct veth_lpar_connection *cnx = veth_cnx[rlp];
932 struct veth_port *port = (struct veth_port *) dev->priv;
933 HvLpEvent_Rc rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 struct veth_msg *msg = NULL;
935 int err = 0;
936 unsigned long flags;
937
938 if (! cnx) {
939 port->stats.tx_errors++;
940 dev_kfree_skb(skb);
941 return 0;
942 }
943
944 spin_lock_irqsave(&cnx->lock, flags);
945
Michael Ellermanf27eff12005-05-12 17:47:27 +1000946 if (! (cnx->state & VETH_STATE_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 goto drop;
948
949 if ((skb->len - 14) > VETH_MAX_MTU)
950 goto drop;
951
952 msg = veth_stack_pop(cnx);
953
954 if (! msg) {
955 err = 1;
956 goto drop;
957 }
958
Michael Ellermanb08bd5c2005-09-01 11:29:06 +1000959 msg->in_use = 1;
960
Michael Ellermancbf90742005-09-01 11:29:07 +1000961 msg->data.addr[0] = dma_map_single(port->dev, skb->data,
962 skb->len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Michael Ellermancbf90742005-09-01 11:29:07 +1000964 if (dma_mapping_error(msg->data.addr[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 goto recycle_and_drop;
966
967 /* Is it really necessary to check the length and address
968 * fields of the first entry here? */
969 msg->skb = skb;
970 msg->dev = port->dev;
Michael Ellermancbf90742005-09-01 11:29:07 +1000971 msg->data.len[0] = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 msg->data.eofmask = 1 << VETH_EOF_SHIFT;
Michael Ellermancbf90742005-09-01 11:29:07 +1000973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 rc = veth_signaldata(cnx, VethEventTypeFrames, msg->token, &msg->data);
975
976 if (rc != HvLpEvent_Rc_Good)
977 goto recycle_and_drop;
978
979 spin_unlock_irqrestore(&cnx->lock, flags);
980 return 0;
981
982 recycle_and_drop:
Michael Ellermanb08bd5c2005-09-01 11:29:06 +1000983 /* we free the skb below, so tell veth_recycle_msg() not to. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 msg->skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 veth_recycle_msg(cnx, msg);
986 drop:
987 port->stats.tx_errors++;
988 dev_kfree_skb(skb);
989 spin_unlock_irqrestore(&cnx->lock, flags);
990 return err;
991}
992
993static HvLpIndexMap veth_transmit_to_many(struct sk_buff *skb,
994 HvLpIndexMap lpmask,
995 struct net_device *dev)
996{
997 struct veth_port *port = (struct veth_port *) dev->priv;
998 int i;
999 int rc;
1000
1001 for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
1002 if ((lpmask & (1 << i)) == 0)
1003 continue;
1004
1005 rc = veth_transmit_to_one(skb_get(skb), i, dev);
1006 if (! rc)
1007 lpmask &= ~(1<<i);
1008 }
1009
1010 if (! lpmask) {
1011 port->stats.tx_packets++;
1012 port->stats.tx_bytes += skb->len;
1013 }
1014
1015 return lpmask;
1016}
1017
1018static int veth_start_xmit(struct sk_buff *skb, struct net_device *dev)
1019{
1020 unsigned char *frame = skb->data;
1021 struct veth_port *port = (struct veth_port *) dev->priv;
1022 unsigned long flags;
1023 HvLpIndexMap lpmask;
1024
1025 if (! (frame[0] & 0x01)) {
1026 /* unicast packet */
1027 HvLpIndex rlp = frame[5];
1028
1029 if ( ! ((1 << rlp) & port->lpar_map) ) {
1030 dev_kfree_skb(skb);
1031 return 0;
1032 }
1033
1034 lpmask = 1 << rlp;
1035 } else {
1036 lpmask = port->lpar_map;
1037 }
1038
1039 spin_lock_irqsave(&port->pending_gate, flags);
1040
1041 lpmask = veth_transmit_to_many(skb, lpmask, dev);
1042
Michael Ellermaneb235ae2005-05-12 17:53:18 +10001043 dev->trans_start = jiffies;
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (! lpmask) {
1046 dev_kfree_skb(skb);
1047 } else {
1048 if (port->pending_skb) {
Michael Ellerman61a3c692005-09-01 11:28:57 +10001049 veth_error("%s: TX while skb was pending!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 dev->name);
1051 dev_kfree_skb(skb);
1052 spin_unlock_irqrestore(&port->pending_gate, flags);
1053 return 1;
1054 }
1055
1056 port->pending_skb = skb;
1057 port->pending_lpmask = lpmask;
1058 netif_stop_queue(dev);
1059 }
1060
1061 spin_unlock_irqrestore(&port->pending_gate, flags);
1062
1063 return 0;
1064}
1065
Michael Ellermanb08bd5c2005-09-01 11:29:06 +10001066/* You must hold the connection's lock when you call this function. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067static void veth_recycle_msg(struct veth_lpar_connection *cnx,
1068 struct veth_msg *msg)
1069{
1070 u32 dma_address, dma_length;
1071
Michael Ellermanb08bd5c2005-09-01 11:29:06 +10001072 if (msg->in_use) {
1073 msg->in_use = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 dma_address = msg->data.addr[0];
1075 dma_length = msg->data.len[0];
1076
Michael Ellermancbf90742005-09-01 11:29:07 +10001077 if (!dma_mapping_error(dma_address))
1078 dma_unmap_single(msg->dev, dma_address, dma_length,
1079 DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 if (msg->skb) {
1082 dev_kfree_skb_any(msg->skb);
1083 msg->skb = NULL;
1084 }
1085
1086 memset(&msg->data, 0, sizeof(msg->data));
1087 veth_stack_push(cnx, msg);
Michael Ellerman61a3c692005-09-01 11:28:57 +10001088 } else if (cnx->state & VETH_STATE_OPEN) {
1089 veth_error("Non-pending frame (# %d) acked by LPAR %d.\n",
1090 cnx->remote_lp, msg->token);
1091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
1093
1094static void veth_flush_pending(struct veth_lpar_connection *cnx)
1095{
1096 int i;
1097 for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++) {
1098 struct net_device *dev = veth_dev[i];
1099 struct veth_port *port;
1100 unsigned long flags;
1101
1102 if (! dev)
1103 continue;
1104
1105 port = (struct veth_port *)dev->priv;
1106
1107 if (! (port->lpar_map & (1<<cnx->remote_lp)))
1108 continue;
1109
1110 spin_lock_irqsave(&port->pending_gate, flags);
1111 if (port->pending_skb) {
1112 port->pending_lpmask =
1113 veth_transmit_to_many(port->pending_skb,
1114 port->pending_lpmask,
1115 dev);
1116 if (! port->pending_lpmask) {
1117 dev_kfree_skb_any(port->pending_skb);
1118 port->pending_skb = NULL;
1119 netif_wake_queue(dev);
1120 }
1121 }
1122 spin_unlock_irqrestore(&port->pending_gate, flags);
1123 }
1124}
1125
1126/*
1127 * Rx path
1128 */
1129
1130static inline int veth_frame_wanted(struct veth_port *port, u64 mac_addr)
1131{
1132 int wanted = 0;
1133 int i;
1134 unsigned long flags;
1135
1136 if ( (mac_addr == port->mac_addr) || (mac_addr == 0xffffffffffff0000) )
1137 return 1;
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 read_lock_irqsave(&port->mcast_gate, flags);
1140
Michael Ellerman2a5391a2005-09-01 11:29:02 +10001141 if (port->promiscuous) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 wanted = 1;
1143 goto out;
1144 }
1145
1146 for (i = 0; i < port->num_mcast; ++i) {
1147 if (port->mcast_addr[i] == mac_addr) {
1148 wanted = 1;
1149 break;
1150 }
1151 }
1152
1153 out:
1154 read_unlock_irqrestore(&port->mcast_gate, flags);
1155
1156 return wanted;
1157}
1158
1159struct dma_chunk {
1160 u64 addr;
1161 u64 size;
1162};
1163
1164#define VETH_MAX_PAGES_PER_FRAME ( (VETH_MAX_MTU+PAGE_SIZE-2)/PAGE_SIZE + 1 )
1165
1166static inline void veth_build_dma_list(struct dma_chunk *list,
1167 unsigned char *p, unsigned long length)
1168{
1169 unsigned long done;
1170 int i = 1;
1171
1172 /* FIXME: skbs are continguous in real addresses. Do we
1173 * really need to break it into PAGE_SIZE chunks, or can we do
1174 * it just at the granularity of iSeries real->absolute
1175 * mapping? Indeed, given the way the allocator works, can we
1176 * count on them being absolutely contiguous? */
1177 list[0].addr = ISERIES_HV_ADDR(p);
1178 list[0].size = min(length,
1179 PAGE_SIZE - ((unsigned long)p & ~PAGE_MASK));
1180
1181 done = list[0].size;
1182 while (done < length) {
1183 list[i].addr = ISERIES_HV_ADDR(p + done);
1184 list[i].size = min(length-done, PAGE_SIZE);
1185 done += list[i].size;
1186 i++;
1187 }
1188}
1189
1190static void veth_flush_acks(struct veth_lpar_connection *cnx)
1191{
1192 HvLpEvent_Rc rc;
1193
1194 rc = veth_signaldata(cnx, VethEventTypeFramesAck,
1195 0, &cnx->pending_acks);
1196
1197 if (rc != HvLpEvent_Rc_Good)
Michael Ellerman61a3c692005-09-01 11:28:57 +10001198 veth_error("Failed acking frames from LPAR %d, rc = %d\n",
1199 cnx->remote_lp, (int)rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 cnx->num_pending_acks = 0;
1202 memset(&cnx->pending_acks, 0xff, sizeof(cnx->pending_acks));
1203}
1204
1205static void veth_receive(struct veth_lpar_connection *cnx,
1206 struct VethLpEvent *event)
1207{
1208 struct VethFramesData *senddata = &event->u.frames_data;
1209 int startchunk = 0;
1210 int nchunks;
1211 unsigned long flags;
1212 HvLpDma_Rc rc;
1213
1214 do {
1215 u16 length = 0;
1216 struct sk_buff *skb;
1217 struct dma_chunk local_list[VETH_MAX_PAGES_PER_FRAME];
1218 struct dma_chunk remote_list[VETH_MAX_FRAMES_PER_MSG];
1219 u64 dest;
1220 HvLpVirtualLanIndex vlan;
1221 struct net_device *dev;
1222 struct veth_port *port;
1223
1224 /* FIXME: do we need this? */
1225 memset(local_list, 0, sizeof(local_list));
1226 memset(remote_list, 0, sizeof(VETH_MAX_FRAMES_PER_MSG));
1227
1228 /* a 0 address marks the end of the valid entries */
1229 if (senddata->addr[startchunk] == 0)
1230 break;
1231
1232 /* make sure that we have at least 1 EOF entry in the
1233 * remaining entries */
1234 if (! (senddata->eofmask >> (startchunk + VETH_EOF_SHIFT))) {
Michael Ellerman61a3c692005-09-01 11:28:57 +10001235 veth_error("Missing EOF fragment in event "
1236 "eofmask = 0x%x startchunk = %d\n",
1237 (unsigned)senddata->eofmask,
1238 startchunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 break;
1240 }
1241
1242 /* build list of chunks in this frame */
1243 nchunks = 0;
1244 do {
1245 remote_list[nchunks].addr =
1246 (u64) senddata->addr[startchunk+nchunks] << 32;
1247 remote_list[nchunks].size =
1248 senddata->len[startchunk+nchunks];
1249 length += remote_list[nchunks].size;
1250 } while (! (senddata->eofmask &
1251 (1 << (VETH_EOF_SHIFT + startchunk + nchunks++))));
1252
1253 /* length == total length of all chunks */
1254 /* nchunks == # of chunks in this frame */
1255
1256 if ((length - ETH_HLEN) > VETH_MAX_MTU) {
Michael Ellerman61a3c692005-09-01 11:28:57 +10001257 veth_error("Received oversize frame from LPAR %d "
1258 "(length = %d)\n",
1259 cnx->remote_lp, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 continue;
1261 }
1262
1263 skb = alloc_skb(length, GFP_ATOMIC);
1264 if (!skb)
1265 continue;
1266
1267 veth_build_dma_list(local_list, skb->data, length);
1268
1269 rc = HvCallEvent_dmaBufList(HvLpEvent_Type_VirtualLan,
1270 event->base_event.xSourceLp,
1271 HvLpDma_Direction_RemoteToLocal,
1272 cnx->src_inst,
1273 cnx->dst_inst,
1274 HvLpDma_AddressType_RealAddress,
1275 HvLpDma_AddressType_TceIndex,
1276 ISERIES_HV_ADDR(&local_list),
1277 ISERIES_HV_ADDR(&remote_list),
1278 length);
1279 if (rc != HvLpDma_Rc_Good) {
1280 dev_kfree_skb_irq(skb);
1281 continue;
1282 }
1283
1284 vlan = skb->data[9];
1285 dev = veth_dev[vlan];
Michael Ellerman41664c02005-05-12 17:55:08 +10001286 if (! dev) {
1287 /*
1288 * Some earlier versions of the driver sent
1289 * broadcasts down all connections, even to lpars
1290 * that weren't on the relevant vlan. So ignore
1291 * packets belonging to a vlan we're not on.
1292 * We can also be here if we receive packets while
1293 * the driver is going down, because then dev is NULL.
1294 */
1295 dev_kfree_skb_irq(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 continue;
Michael Ellerman41664c02005-05-12 17:55:08 +10001297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 port = (struct veth_port *)dev->priv;
1300 dest = *((u64 *) skb->data) & 0xFFFFFFFFFFFF0000;
1301
1302 if ((vlan > HVMAXARCHITECTEDVIRTUALLANS) || !port) {
1303 dev_kfree_skb_irq(skb);
1304 continue;
1305 }
1306 if (! veth_frame_wanted(port, dest)) {
1307 dev_kfree_skb_irq(skb);
1308 continue;
1309 }
1310
1311 skb_put(skb, length);
1312 skb->dev = dev;
1313 skb->protocol = eth_type_trans(skb, dev);
1314 skb->ip_summed = CHECKSUM_NONE;
1315 netif_rx(skb); /* send it up */
1316 port->stats.rx_packets++;
1317 port->stats.rx_bytes += length;
1318 } while (startchunk += nchunks, startchunk < VETH_MAX_FRAMES_PER_MSG);
1319
1320 /* Ack it */
1321 spin_lock_irqsave(&cnx->lock, flags);
1322 BUG_ON(cnx->num_pending_acks > VETH_MAX_ACKS_PER_MSG);
1323
1324 cnx->pending_acks[cnx->num_pending_acks++] =
1325 event->base_event.xCorrelationToken;
1326
1327 if ( (cnx->num_pending_acks >= cnx->remote_caps.ack_threshold)
1328 || (cnx->num_pending_acks >= VETH_MAX_ACKS_PER_MSG) )
1329 veth_flush_acks(cnx);
1330
1331 spin_unlock_irqrestore(&cnx->lock, flags);
1332}
1333
1334static void veth_timed_ack(unsigned long ptr)
1335{
1336 struct veth_lpar_connection *cnx = (struct veth_lpar_connection *) ptr;
1337 unsigned long flags;
1338
1339 /* Ack all the events */
1340 spin_lock_irqsave(&cnx->lock, flags);
1341 if (cnx->num_pending_acks > 0)
1342 veth_flush_acks(cnx);
1343
1344 /* Reschedule the timer */
1345 cnx->ack_timer.expires = jiffies + cnx->ack_timeout;
1346 add_timer(&cnx->ack_timer);
1347 spin_unlock_irqrestore(&cnx->lock, flags);
1348}
1349
1350static int veth_remove(struct vio_dev *vdev)
1351{
1352 int i = vdev->unit_address;
1353 struct net_device *dev;
1354
1355 dev = veth_dev[i];
1356 if (dev != NULL) {
1357 veth_dev[i] = NULL;
1358 unregister_netdev(dev);
1359 free_netdev(dev);
1360 }
1361 return 0;
1362}
1363
1364static int veth_probe(struct vio_dev *vdev, const struct vio_device_id *id)
1365{
1366 int i = vdev->unit_address;
1367 struct net_device *dev;
1368
1369 dev = veth_probe_one(i, &vdev->dev);
1370 if (dev == NULL) {
1371 veth_remove(vdev);
1372 return 1;
1373 }
1374 veth_dev[i] = dev;
1375
1376 /* Start the state machine on each connection, to commence
1377 * link negotiation */
1378 for (i = 0; i < HVMAXARCHITECTEDLPS; i++)
1379 if (veth_cnx[i])
1380 veth_kick_statemachine(veth_cnx[i]);
1381
1382 return 0;
1383}
1384
1385/**
1386 * veth_device_table: Used by vio.c to match devices that we
1387 * support.
1388 */
1389static struct vio_device_id veth_device_table[] __devinitdata = {
1390 { "vlan", "" },
Stephen Rothwellfb120da2005-08-17 16:42:59 +10001391 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392};
1393MODULE_DEVICE_TABLE(vio, veth_device_table);
1394
1395static struct vio_driver veth_driver = {
1396 .name = "iseries_veth",
1397 .id_table = veth_device_table,
1398 .probe = veth_probe,
1399 .remove = veth_remove
1400};
1401
1402/*
1403 * Module initialization/cleanup
1404 */
1405
1406void __exit veth_module_cleanup(void)
1407{
1408 int i;
1409
Michael Ellermanb2e08522005-05-12 18:09:45 +10001410 /* Stop the queues first to stop any new packets being sent. */
1411 for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++)
1412 if (veth_dev[i])
1413 netif_stop_queue(veth_dev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Michael Ellermanb2e08522005-05-12 18:09:45 +10001415 /* Stop the connections before we unregister the driver. This
1416 * ensures there's no skbs lying around holding the device open. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 for (i = 0; i < HVMAXARCHITECTEDLPS; ++i)
1418 veth_stop_connection(i);
1419
1420 HvLpEvent_unregisterHandler(HvLpEvent_Type_VirtualLan);
1421
1422 /* Hypervisor callbacks may have scheduled more work while we
Michael Ellermanb2e08522005-05-12 18:09:45 +10001423 * were stoping connections. Now that we've disconnected from
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 * the hypervisor make sure everything's finished. */
1425 flush_scheduled_work();
1426
Michael Ellermanb2e08522005-05-12 18:09:45 +10001427 vio_unregister_driver(&veth_driver);
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 for (i = 0; i < HVMAXARCHITECTEDLPS; ++i)
1430 veth_destroy_connection(i);
1431
1432}
1433module_exit(veth_module_cleanup);
1434
1435int __init veth_module_init(void)
1436{
1437 int i;
1438 int rc;
1439
1440 this_lp = HvLpConfig_getLpIndex_outline();
1441
1442 for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
1443 rc = veth_init_connection(i);
Michael Ellermanec60bee2005-09-01 11:29:08 +10001444 if (rc != 0)
1445 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
1447
1448 HvLpEvent_registerHandler(HvLpEvent_Type_VirtualLan,
1449 &veth_handle_event);
1450
Michael Ellermanec60bee2005-09-01 11:29:08 +10001451 rc = vio_register_driver(&veth_driver);
1452 if (rc != 0)
1453 goto error;
1454
1455 return 0;
1456
1457error:
1458 for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
1459 veth_destroy_connection(i);
1460 }
1461
1462 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
1464module_init(veth_module_init);