1 /* File veth.c created by Kyle A. Lucke on Mon Aug 7 2000. */
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.
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.
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.
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
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
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.
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.
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.
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.
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.
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.
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>
71 #include <linux/ethtool.h>
72 #include <asm/iSeries/mf.h>
73 #include <asm/iSeries/iSeries_pci.h>
74 #include <asm/uaccess.h>
76 #include <asm/iSeries/HvLpConfig.h>
77 #include <asm/iSeries/HvTypes.h>
78 #include <asm/iSeries/HvLpEvent.h>
79 #include <asm/iommu.h>
84 #include "iseries_veth.h"
86 MODULE_AUTHOR("Kyle Lucke <klucke@us.ibm.com>");
87 MODULE_DESCRIPTION("iSeries Virtual ethernet driver");
88 MODULE_LICENSE("GPL");
90 #define VETH_NUMBUFFERS (120)
91 #define VETH_ACKTIMEOUT (1000000) /* microseconds */
92 #define VETH_MAX_MCAST (12)
94 #define VETH_MAX_MTU (9000)
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)
103 #define ACK_THRESHOLD (20)
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)
117 struct veth_msg *next;
118 struct VethFramesData data;
125 struct veth_lpar_connection {
127 struct work_struct statemachine_wq;
128 struct veth_msg *msgs;
130 struct VethCapData local_caps;
132 struct kobject kobject;
133 struct timer_list ack_timer;
135 struct timer_list reset_timer;
136 unsigned int reset_timeout;
137 unsigned long last_contact;
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;
149 struct VethCapData remote_caps;
152 struct veth_msg *msg_stack_head;
157 struct net_device_stats stats;
159 HvLpIndexMap lpar_map;
161 spinlock_t pending_gate;
162 struct sk_buff *pending_skb;
163 HvLpIndexMap pending_lpmask;
168 u64 mcast_addr[VETH_MAX_MCAST];
171 static HvLpIndex this_lp;
172 static struct veth_lpar_connection *veth_cnx[HVMAXARCHITECTEDLPS]; /* = 0 */
173 static struct net_device *veth_dev[HVMAXARCHITECTEDVIRTUALLANS]; /* = 0 */
175 static int veth_start_xmit(struct sk_buff *skb, struct net_device *dev);
176 static void veth_recycle_msg(struct veth_lpar_connection *, struct veth_msg *);
177 static void veth_flush_pending(struct veth_lpar_connection *cnx);
178 static void veth_receive(struct veth_lpar_connection *, struct VethLpEvent *);
179 static void veth_release_connection(struct kobject *kobject);
180 static void veth_timed_ack(unsigned long ptr);
181 static void veth_timed_reset(unsigned long ptr);
183 static struct kobj_type veth_lpar_connection_ktype = {
184 .release = veth_release_connection
191 #define veth_info(fmt, args...) \
192 printk(KERN_INFO "iseries_veth: " fmt, ## args)
194 #define veth_error(fmt, args...) \
195 printk(KERN_ERR "iseries_veth: Error: " fmt, ## args)
198 #define veth_debug(fmt, args...) \
199 printk(KERN_DEBUG "iseries_veth: " fmt, ## args)
201 #define veth_debug(fmt, args...) do {} while (0)
204 /* You must hold the connection's lock when you call this function. */
205 static inline void veth_stack_push(struct veth_lpar_connection *cnx,
206 struct veth_msg *msg)
208 msg->next = cnx->msg_stack_head;
209 cnx->msg_stack_head = msg;
212 /* You must hold the connection's lock when you call this function. */
213 static inline struct veth_msg *veth_stack_pop(struct veth_lpar_connection *cnx)
215 struct veth_msg *msg;
217 msg = cnx->msg_stack_head;
219 cnx->msg_stack_head = cnx->msg_stack_head->next;
224 static inline HvLpEvent_Rc
225 veth_signalevent(struct veth_lpar_connection *cnx, u16 subtype,
226 HvLpEvent_AckInd ackind, HvLpEvent_AckType acktype,
228 u64 data1, u64 data2, u64 data3, u64 data4, u64 data5)
230 return HvCallEvent_signalLpEventFast(cnx->remote_lp,
231 HvLpEvent_Type_VirtualLan,
232 subtype, ackind, acktype,
235 token, data1, data2, data3,
239 static inline HvLpEvent_Rc veth_signaldata(struct veth_lpar_connection *cnx,
240 u16 subtype, u64 token, void *data)
242 u64 *p = (u64 *) data;
244 return veth_signalevent(cnx, subtype, HvLpEvent_AckInd_NoAck,
245 HvLpEvent_AckType_ImmediateAck,
246 token, p[0], p[1], p[2], p[3], p[4]);
249 struct veth_allocation {
254 static void veth_complete_allocation(void *parm, int number)
256 struct veth_allocation *vc = (struct veth_allocation *)parm;
262 static int veth_allocate_events(HvLpIndex rlp, int number)
264 struct veth_allocation vc = { COMPLETION_INITIALIZER(vc.c), 0 };
266 mf_allocate_lp_events(rlp, HvLpEvent_Type_VirtualLan,
267 sizeof(struct VethLpEvent), number,
268 &veth_complete_allocation, &vc);
269 wait_for_completion(&vc.c);
275 * LPAR connection code
278 static inline void veth_kick_statemachine(struct veth_lpar_connection *cnx)
280 schedule_work(&cnx->statemachine_wq);
283 static void veth_take_cap(struct veth_lpar_connection *cnx,
284 struct VethLpEvent *event)
288 spin_lock_irqsave(&cnx->lock, flags);
289 /* Receiving caps may mean the other end has just come up, so
290 * we need to reload the instance ID of the far end */
292 HvCallEvent_getTargetLpInstanceId(cnx->remote_lp,
293 HvLpEvent_Type_VirtualLan);
295 if (cnx->state & VETH_STATE_GOTCAPS) {
296 veth_error("Received a second capabilities from LPAR %d.\n",
298 event->base_event.xRc = HvLpEvent_Rc_BufferNotAvailable;
299 HvCallEvent_ackLpEvent((struct HvLpEvent *) event);
301 memcpy(&cnx->cap_event, event, sizeof(cnx->cap_event));
302 cnx->state |= VETH_STATE_GOTCAPS;
303 veth_kick_statemachine(cnx);
305 spin_unlock_irqrestore(&cnx->lock, flags);
308 static void veth_take_cap_ack(struct veth_lpar_connection *cnx,
309 struct VethLpEvent *event)
313 spin_lock_irqsave(&cnx->lock, flags);
314 if (cnx->state & VETH_STATE_GOTCAPACK) {
315 veth_error("Received a second capabilities ack from LPAR %d.\n",
318 memcpy(&cnx->cap_ack_event, event,
319 sizeof(&cnx->cap_ack_event));
320 cnx->state |= VETH_STATE_GOTCAPACK;
321 veth_kick_statemachine(cnx);
323 spin_unlock_irqrestore(&cnx->lock, flags);
326 static void veth_take_monitor_ack(struct veth_lpar_connection *cnx,
327 struct VethLpEvent *event)
331 spin_lock_irqsave(&cnx->lock, flags);
332 veth_debug("cnx %d: lost connection.\n", cnx->remote_lp);
334 /* Avoid kicking the statemachine once we're shutdown.
335 * It's unnecessary and it could break veth_stop_connection(). */
337 if (! (cnx->state & VETH_STATE_SHUTDOWN)) {
338 cnx->state |= VETH_STATE_RESET;
339 veth_kick_statemachine(cnx);
341 spin_unlock_irqrestore(&cnx->lock, flags);
344 static void veth_handle_ack(struct VethLpEvent *event)
346 HvLpIndex rlp = event->base_event.xTargetLp;
347 struct veth_lpar_connection *cnx = veth_cnx[rlp];
351 switch (event->base_event.xSubtype) {
352 case VethEventTypeCap:
353 veth_take_cap_ack(cnx, event);
355 case VethEventTypeMonitor:
356 veth_take_monitor_ack(cnx, event);
359 veth_error("Unknown ack type %d from LPAR %d.\n",
360 event->base_event.xSubtype, rlp);
364 static void veth_handle_int(struct VethLpEvent *event)
366 HvLpIndex rlp = event->base_event.xSourceLp;
367 struct veth_lpar_connection *cnx = veth_cnx[rlp];
373 switch (event->base_event.xSubtype) {
374 case VethEventTypeCap:
375 veth_take_cap(cnx, event);
377 case VethEventTypeMonitor:
378 /* do nothing... this'll hang out here til we're dead,
379 * and the hypervisor will return it for us. */
381 case VethEventTypeFramesAck:
382 spin_lock_irqsave(&cnx->lock, flags);
384 for (i = 0; i < VETH_MAX_ACKS_PER_MSG; ++i) {
385 u16 msgnum = event->u.frames_ack_data.token[i];
387 if (msgnum < VETH_NUMBUFFERS) {
388 veth_recycle_msg(cnx, cnx->msgs + msgnum);
389 cnx->outstanding_tx--;
395 cnx->last_contact = jiffies;
397 spin_unlock_irqrestore(&cnx->lock, flags);
399 veth_flush_pending(cnx);
401 case VethEventTypeFrames:
402 veth_receive(cnx, event);
405 veth_error("Unknown interrupt type %d from LPAR %d.\n",
406 event->base_event.xSubtype, rlp);
410 static void veth_handle_event(struct HvLpEvent *event, struct pt_regs *regs)
412 struct VethLpEvent *veth_event = (struct VethLpEvent *)event;
414 if (event->xFlags.xFunction == HvLpEvent_Function_Ack)
415 veth_handle_ack(veth_event);
416 else if (event->xFlags.xFunction == HvLpEvent_Function_Int)
417 veth_handle_int(veth_event);
420 static int veth_process_caps(struct veth_lpar_connection *cnx)
422 struct VethCapData *remote_caps = &cnx->remote_caps;
425 /* Convert timer to jiffies */
426 cnx->ack_timeout = remote_caps->ack_timeout * HZ / 1000000;
428 if ( (remote_caps->num_buffers == 0)
429 || (remote_caps->ack_threshold > VETH_MAX_ACKS_PER_MSG)
430 || (remote_caps->ack_threshold == 0)
431 || (cnx->ack_timeout == 0) ) {
432 veth_error("Received incompatible capabilities from LPAR %d.\n",
434 return HvLpEvent_Rc_InvalidSubtypeData;
437 num_acks_needed = (remote_caps->num_buffers
438 / remote_caps->ack_threshold) + 1;
440 /* FIXME: locking on num_ack_events? */
441 if (cnx->num_ack_events < num_acks_needed) {
444 num = veth_allocate_events(cnx->remote_lp,
445 num_acks_needed-cnx->num_ack_events);
447 cnx->num_ack_events += num;
449 if (cnx->num_ack_events < num_acks_needed) {
450 veth_error("Couldn't allocate enough ack events "
451 "for LPAR %d.\n", cnx->remote_lp);
453 return HvLpEvent_Rc_BufferNotAvailable;
458 return HvLpEvent_Rc_Good;
461 /* FIXME: The gotos here are a bit dubious */
462 static void veth_statemachine(void *p)
464 struct veth_lpar_connection *cnx = (struct veth_lpar_connection *)p;
465 int rlp = cnx->remote_lp;
468 spin_lock_irq(&cnx->lock);
471 if (cnx->state & VETH_STATE_RESET) {
472 if (cnx->state & VETH_STATE_OPEN)
473 HvCallEvent_closeLpEventPath(cnx->remote_lp,
474 HvLpEvent_Type_VirtualLan);
477 * Reset ack data. This prevents the ack_timer actually
478 * doing anything, even if it runs one more time when
479 * we drop the lock below.
481 memset(&cnx->pending_acks, 0xff, sizeof (cnx->pending_acks));
482 cnx->num_pending_acks = 0;
484 cnx->state &= ~(VETH_STATE_RESET | VETH_STATE_SENTMON
485 | VETH_STATE_OPEN | VETH_STATE_SENTCAPS
486 | VETH_STATE_GOTCAPACK | VETH_STATE_GOTCAPS
487 | VETH_STATE_SENTCAPACK | VETH_STATE_READY);
489 /* Clean up any leftover messages */
492 for (i = 0; i < VETH_NUMBUFFERS; ++i)
493 veth_recycle_msg(cnx, cnx->msgs + i);
495 cnx->outstanding_tx = 0;
497 /* Drop the lock so we can do stuff that might sleep or
498 * take other locks. */
499 spin_unlock_irq(&cnx->lock);
501 del_timer_sync(&cnx->ack_timer);
502 del_timer_sync(&cnx->reset_timer);
504 veth_flush_pending(cnx);
506 spin_lock_irq(&cnx->lock);
508 if (cnx->state & VETH_STATE_RESET)
511 /* Hack, wait for the other end to reset itself. */
512 if (! (cnx->state & VETH_STATE_SHUTDOWN)) {
513 schedule_delayed_work(&cnx->statemachine_wq, 5 * HZ);
518 if (cnx->state & VETH_STATE_SHUTDOWN)
519 /* It's all over, do nothing */
522 if ( !(cnx->state & VETH_STATE_OPEN) ) {
523 if (! cnx->msgs || (cnx->num_events < (2 + VETH_NUMBUFFERS)) )
526 HvCallEvent_openLpEventPath(rlp, HvLpEvent_Type_VirtualLan);
528 HvCallEvent_getSourceLpInstanceId(rlp,
529 HvLpEvent_Type_VirtualLan);
531 HvCallEvent_getTargetLpInstanceId(rlp,
532 HvLpEvent_Type_VirtualLan);
533 cnx->state |= VETH_STATE_OPEN;
536 if ( (cnx->state & VETH_STATE_OPEN)
537 && !(cnx->state & VETH_STATE_SENTMON) ) {
538 rc = veth_signalevent(cnx, VethEventTypeMonitor,
539 HvLpEvent_AckInd_DoAck,
540 HvLpEvent_AckType_DeferredAck,
543 if (rc == HvLpEvent_Rc_Good) {
544 cnx->state |= VETH_STATE_SENTMON;
546 if ( (rc != HvLpEvent_Rc_PartitionDead)
547 && (rc != HvLpEvent_Rc_PathClosed) )
548 veth_error("Error sending monitor to LPAR %d, "
549 "rc = %d\n", rlp, rc);
551 /* Oh well, hope we get a cap from the other
552 * end and do better when that kicks us */
557 if ( (cnx->state & VETH_STATE_OPEN)
558 && !(cnx->state & VETH_STATE_SENTCAPS)) {
559 u64 *rawcap = (u64 *)&cnx->local_caps;
561 rc = veth_signalevent(cnx, VethEventTypeCap,
562 HvLpEvent_AckInd_DoAck,
563 HvLpEvent_AckType_ImmediateAck,
564 0, rawcap[0], rawcap[1], rawcap[2],
565 rawcap[3], rawcap[4]);
567 if (rc == HvLpEvent_Rc_Good) {
568 cnx->state |= VETH_STATE_SENTCAPS;
570 if ( (rc != HvLpEvent_Rc_PartitionDead)
571 && (rc != HvLpEvent_Rc_PathClosed) )
572 veth_error("Error sending caps to LPAR %d, "
573 "rc = %d\n", rlp, rc);
575 /* Oh well, hope we get a cap from the other
576 * end and do better when that kicks us */
581 if ((cnx->state & VETH_STATE_GOTCAPS)
582 && !(cnx->state & VETH_STATE_SENTCAPACK)) {
583 struct VethCapData *remote_caps = &cnx->remote_caps;
585 memcpy(remote_caps, &cnx->cap_event.u.caps_data,
586 sizeof(*remote_caps));
588 spin_unlock_irq(&cnx->lock);
589 rc = veth_process_caps(cnx);
590 spin_lock_irq(&cnx->lock);
592 /* We dropped the lock, so recheck for anything which
593 * might mess us up */
594 if (cnx->state & (VETH_STATE_RESET|VETH_STATE_SHUTDOWN))
597 cnx->cap_event.base_event.xRc = rc;
598 HvCallEvent_ackLpEvent((struct HvLpEvent *)&cnx->cap_event);
599 if (rc == HvLpEvent_Rc_Good)
600 cnx->state |= VETH_STATE_SENTCAPACK;
605 if ((cnx->state & VETH_STATE_GOTCAPACK)
606 && (cnx->state & VETH_STATE_GOTCAPS)
607 && !(cnx->state & VETH_STATE_READY)) {
608 if (cnx->cap_ack_event.base_event.xRc == HvLpEvent_Rc_Good) {
609 /* Start the ACK timer */
610 cnx->ack_timer.expires = jiffies + cnx->ack_timeout;
611 add_timer(&cnx->ack_timer);
612 cnx->state |= VETH_STATE_READY;
614 veth_error("Caps rejected by LPAR %d, rc = %d\n",
615 rlp, cnx->cap_ack_event.base_event.xRc);
621 spin_unlock_irq(&cnx->lock);
625 /* FIXME: we get here if something happens we really can't
626 * cope with. The link will never work once we get here, and
627 * all we can do is not lock the rest of the system up */
628 veth_error("Unrecoverable error on connection to LPAR %d, shutting down"
629 " (state = 0x%04lx)\n", rlp, cnx->state);
630 cnx->state |= VETH_STATE_SHUTDOWN;
631 spin_unlock_irq(&cnx->lock);
634 static int veth_init_connection(u8 rlp)
636 struct veth_lpar_connection *cnx;
637 struct veth_msg *msgs;
640 if ( (rlp == this_lp)
641 || ! HvLpConfig_doLpsCommunicateOnVirtualLan(this_lp, rlp) )
644 cnx = kmalloc(sizeof(*cnx), GFP_KERNEL);
647 memset(cnx, 0, sizeof(*cnx));
649 cnx->remote_lp = rlp;
650 spin_lock_init(&cnx->lock);
651 INIT_WORK(&cnx->statemachine_wq, veth_statemachine, cnx);
653 init_timer(&cnx->ack_timer);
654 cnx->ack_timer.function = veth_timed_ack;
655 cnx->ack_timer.data = (unsigned long) cnx;
657 init_timer(&cnx->reset_timer);
658 cnx->reset_timer.function = veth_timed_reset;
659 cnx->reset_timer.data = (unsigned long) cnx;
660 cnx->reset_timeout = 5 * HZ * (VETH_ACKTIMEOUT / 1000000);
662 memset(&cnx->pending_acks, 0xff, sizeof (cnx->pending_acks));
666 /* This gets us 1 reference, which is held on behalf of the driver
667 * infrastructure. It's released at module unload. */
668 kobject_init(&cnx->kobject);
669 cnx->kobject.ktype = &veth_lpar_connection_ktype;
670 rc = kobject_set_name(&cnx->kobject, "cnx%.2d", rlp);
674 msgs = kmalloc(VETH_NUMBUFFERS * sizeof(struct veth_msg), GFP_KERNEL);
676 veth_error("Can't allocate buffers for LPAR %d.\n", rlp);
681 memset(msgs, 0, VETH_NUMBUFFERS * sizeof(struct veth_msg));
683 for (i = 0; i < VETH_NUMBUFFERS; i++) {
685 veth_stack_push(cnx, msgs + i);
688 cnx->num_events = veth_allocate_events(rlp, 2 + VETH_NUMBUFFERS);
690 if (cnx->num_events < (2 + VETH_NUMBUFFERS)) {
691 veth_error("Can't allocate enough events for LPAR %d.\n", rlp);
695 cnx->local_caps.num_buffers = VETH_NUMBUFFERS;
696 cnx->local_caps.ack_threshold = ACK_THRESHOLD;
697 cnx->local_caps.ack_timeout = VETH_ACKTIMEOUT;
702 static void veth_stop_connection(struct veth_lpar_connection *cnx)
707 spin_lock_irq(&cnx->lock);
708 cnx->state |= VETH_STATE_RESET | VETH_STATE_SHUTDOWN;
709 veth_kick_statemachine(cnx);
710 spin_unlock_irq(&cnx->lock);
712 /* There's a slim chance the reset code has just queued the
713 * statemachine to run in five seconds. If so we need to cancel
714 * that and requeue the work to run now. */
715 if (cancel_delayed_work(&cnx->statemachine_wq)) {
716 spin_lock_irq(&cnx->lock);
717 veth_kick_statemachine(cnx);
718 spin_unlock_irq(&cnx->lock);
721 /* Wait for the state machine to run. */
722 flush_scheduled_work();
725 static void veth_destroy_connection(struct veth_lpar_connection *cnx)
730 if (cnx->num_events > 0)
731 mf_deallocate_lp_events(cnx->remote_lp,
732 HvLpEvent_Type_VirtualLan,
735 if (cnx->num_ack_events > 0)
736 mf_deallocate_lp_events(cnx->remote_lp,
737 HvLpEvent_Type_VirtualLan,
742 veth_cnx[cnx->remote_lp] = NULL;
746 static void veth_release_connection(struct kobject *kobj)
748 struct veth_lpar_connection *cnx;
749 cnx = container_of(kobj, struct veth_lpar_connection, kobject);
750 veth_stop_connection(cnx);
751 veth_destroy_connection(cnx);
758 static int veth_open(struct net_device *dev)
760 struct veth_port *port = (struct veth_port *) dev->priv;
762 memset(&port->stats, 0, sizeof (port->stats));
763 netif_start_queue(dev);
767 static int veth_close(struct net_device *dev)
769 netif_stop_queue(dev);
773 static struct net_device_stats *veth_get_stats(struct net_device *dev)
775 struct veth_port *port = (struct veth_port *) dev->priv;
780 static int veth_change_mtu(struct net_device *dev, int new_mtu)
782 if ((new_mtu < 68) || (new_mtu > VETH_MAX_MTU))
788 static void veth_set_multicast_list(struct net_device *dev)
790 struct veth_port *port = (struct veth_port *) dev->priv;
793 write_lock_irqsave(&port->mcast_gate, flags);
795 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
796 (dev->mc_count > VETH_MAX_MCAST)) {
797 port->promiscuous = 1;
799 struct dev_mc_list *dmi = dev->mc_list;
802 port->promiscuous = 0;
807 for (i = 0; i < dev->mc_count; i++) {
808 u8 *addr = dmi->dmi_addr;
811 if (addr[0] & 0x01) {/* multicast address? */
812 memcpy(&xaddr, addr, ETH_ALEN);
813 port->mcast_addr[port->num_mcast] = xaddr;
820 write_unlock_irqrestore(&port->mcast_gate, flags);
823 static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
825 strncpy(info->driver, "veth", sizeof(info->driver) - 1);
826 info->driver[sizeof(info->driver) - 1] = '\0';
827 strncpy(info->version, "1.0", sizeof(info->version) - 1);
830 static int veth_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
832 ecmd->supported = (SUPPORTED_1000baseT_Full
833 | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
834 ecmd->advertising = (SUPPORTED_1000baseT_Full
835 | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
836 ecmd->port = PORT_FIBRE;
837 ecmd->transceiver = XCVR_INTERNAL;
838 ecmd->phy_address = 0;
839 ecmd->speed = SPEED_1000;
840 ecmd->duplex = DUPLEX_FULL;
841 ecmd->autoneg = AUTONEG_ENABLE;
842 ecmd->maxtxpkt = 120;
843 ecmd->maxrxpkt = 120;
847 static u32 veth_get_link(struct net_device *dev)
852 static struct ethtool_ops ops = {
853 .get_drvinfo = veth_get_drvinfo,
854 .get_settings = veth_get_settings,
855 .get_link = veth_get_link,
858 static struct net_device * __init veth_probe_one(int vlan, struct device *vdev)
860 struct net_device *dev;
861 struct veth_port *port;
864 dev = alloc_etherdev(sizeof (struct veth_port));
866 veth_error("Unable to allocate net_device structure!\n");
870 port = (struct veth_port *) dev->priv;
872 spin_lock_init(&port->pending_gate);
873 rwlock_init(&port->mcast_gate);
875 for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
876 HvLpVirtualLanIndexMap map;
880 map = HvLpConfig_getVirtualLanIndexMapForLp(i);
881 if (map & (0x8000 >> vlan))
882 port->lpar_map |= (1 << i);
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;
893 dev->mtu = VETH_MAX_MTU;
895 memcpy(&port->mac_addr, dev->dev_addr, 6);
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);
906 SET_NETDEV_DEV(dev, vdev);
908 rc = register_netdev(dev);
910 veth_error("Failed registering net device for vlan%d.\n", vlan);
915 veth_info("%s attached to iSeries vlan %d (LPAR map = 0x%.4X)\n",
916 dev->name, vlan, port->lpar_map);
925 static int veth_transmit_to_one(struct sk_buff *skb, HvLpIndex rlp,
926 struct net_device *dev)
928 struct veth_lpar_connection *cnx = veth_cnx[rlp];
929 struct veth_port *port = (struct veth_port *) dev->priv;
931 struct veth_msg *msg = NULL;
936 port->stats.tx_errors++;
941 spin_lock_irqsave(&cnx->lock, flags);
943 if (! (cnx->state & VETH_STATE_READY))
946 if ((skb->len - 14) > VETH_MAX_MTU)
949 msg = veth_stack_pop(cnx);
958 msg->data.addr[0] = dma_map_single(port->dev, skb->data,
959 skb->len, DMA_TO_DEVICE);
961 if (dma_mapping_error(msg->data.addr[0]))
962 goto recycle_and_drop;
964 /* Is it really necessary to check the length and address
965 * fields of the first entry here? */
967 msg->dev = port->dev;
968 msg->data.len[0] = skb->len;
969 msg->data.eofmask = 1 << VETH_EOF_SHIFT;
971 rc = veth_signaldata(cnx, VethEventTypeFrames, msg->token, &msg->data);
973 if (rc != HvLpEvent_Rc_Good)
974 goto recycle_and_drop;
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);
980 cnx->last_contact = jiffies;
981 cnx->outstanding_tx++;
983 spin_unlock_irqrestore(&cnx->lock, flags);
987 /* we free the skb below, so tell veth_recycle_msg() not to. */
989 veth_recycle_msg(cnx, msg);
991 port->stats.tx_errors++;
993 spin_unlock_irqrestore(&cnx->lock, flags);
997 static HvLpIndexMap veth_transmit_to_many(struct sk_buff *skb,
999 struct net_device *dev)
1001 struct veth_port *port = (struct veth_port *) dev->priv;
1005 for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
1006 if ((lpmask & (1 << i)) == 0)
1009 rc = veth_transmit_to_one(skb_get(skb), i, dev);
1015 port->stats.tx_packets++;
1016 port->stats.tx_bytes += skb->len;
1022 static int veth_start_xmit(struct sk_buff *skb, struct net_device *dev)
1024 unsigned char *frame = skb->data;
1025 struct veth_port *port = (struct veth_port *) dev->priv;
1026 unsigned long flags;
1027 HvLpIndexMap lpmask;
1029 if (! (frame[0] & 0x01)) {
1030 /* unicast packet */
1031 HvLpIndex rlp = frame[5];
1033 if ( ! ((1 << rlp) & port->lpar_map) ) {
1040 lpmask = port->lpar_map;
1043 spin_lock_irqsave(&port->pending_gate, flags);
1045 lpmask = veth_transmit_to_many(skb, lpmask, dev);
1050 if (port->pending_skb) {
1051 veth_error("%s: TX while skb was pending!\n",
1054 spin_unlock_irqrestore(&port->pending_gate, flags);
1058 port->pending_skb = skb;
1059 port->pending_lpmask = lpmask;
1060 netif_stop_queue(dev);
1063 spin_unlock_irqrestore(&port->pending_gate, flags);
1068 /* You must hold the connection's lock when you call this function. */
1069 static void veth_recycle_msg(struct veth_lpar_connection *cnx,
1070 struct veth_msg *msg)
1072 u32 dma_address, dma_length;
1076 dma_address = msg->data.addr[0];
1077 dma_length = msg->data.len[0];
1079 if (!dma_mapping_error(dma_address))
1080 dma_unmap_single(msg->dev, dma_address, dma_length,
1084 dev_kfree_skb_any(msg->skb);
1088 memset(&msg->data, 0, sizeof(msg->data));
1089 veth_stack_push(cnx, msg);
1090 } else if (cnx->state & VETH_STATE_OPEN) {
1091 veth_error("Non-pending frame (# %d) acked by LPAR %d.\n",
1092 cnx->remote_lp, msg->token);
1096 static void veth_flush_pending(struct veth_lpar_connection *cnx)
1099 for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++) {
1100 struct net_device *dev = veth_dev[i];
1101 struct veth_port *port;
1102 unsigned long flags;
1107 port = (struct veth_port *)dev->priv;
1109 if (! (port->lpar_map & (1<<cnx->remote_lp)))
1112 spin_lock_irqsave(&port->pending_gate, flags);
1113 if (port->pending_skb) {
1114 port->pending_lpmask =
1115 veth_transmit_to_many(port->pending_skb,
1116 port->pending_lpmask,
1118 if (! port->pending_lpmask) {
1119 dev_kfree_skb_any(port->pending_skb);
1120 port->pending_skb = NULL;
1121 netif_wake_queue(dev);
1124 spin_unlock_irqrestore(&port->pending_gate, flags);
1128 static void veth_timed_reset(unsigned long ptr)
1130 struct veth_lpar_connection *cnx = (struct veth_lpar_connection *)ptr;
1131 unsigned long trigger_time, flags;
1133 /* FIXME is it possible this fires after veth_stop_connection()?
1134 * That would reschedule the statemachine for 5 seconds and probably
1135 * execute it after the module's been unloaded. Hmm. */
1137 spin_lock_irqsave(&cnx->lock, flags);
1139 if (cnx->outstanding_tx > 0) {
1140 trigger_time = cnx->last_contact + cnx->reset_timeout;
1142 if (trigger_time < jiffies) {
1143 cnx->state |= VETH_STATE_RESET;
1144 veth_kick_statemachine(cnx);
1145 veth_error("%d packets not acked by LPAR %d within %d "
1146 "seconds, resetting.\n",
1147 cnx->outstanding_tx, cnx->remote_lp,
1148 cnx->reset_timeout / HZ);
1150 /* Reschedule the timer */
1151 trigger_time = jiffies + cnx->reset_timeout;
1152 mod_timer(&cnx->reset_timer, trigger_time);
1156 spin_unlock_irqrestore(&cnx->lock, flags);
1163 static inline int veth_frame_wanted(struct veth_port *port, u64 mac_addr)
1167 unsigned long flags;
1169 if ( (mac_addr == port->mac_addr) || (mac_addr == 0xffffffffffff0000) )
1172 read_lock_irqsave(&port->mcast_gate, flags);
1174 if (port->promiscuous) {
1179 for (i = 0; i < port->num_mcast; ++i) {
1180 if (port->mcast_addr[i] == mac_addr) {
1187 read_unlock_irqrestore(&port->mcast_gate, flags);
1197 #define VETH_MAX_PAGES_PER_FRAME ( (VETH_MAX_MTU+PAGE_SIZE-2)/PAGE_SIZE + 1 )
1199 static inline void veth_build_dma_list(struct dma_chunk *list,
1200 unsigned char *p, unsigned long length)
1205 /* FIXME: skbs are continguous in real addresses. Do we
1206 * really need to break it into PAGE_SIZE chunks, or can we do
1207 * it just at the granularity of iSeries real->absolute
1208 * mapping? Indeed, given the way the allocator works, can we
1209 * count on them being absolutely contiguous? */
1210 list[0].addr = ISERIES_HV_ADDR(p);
1211 list[0].size = min(length,
1212 PAGE_SIZE - ((unsigned long)p & ~PAGE_MASK));
1214 done = list[0].size;
1215 while (done < length) {
1216 list[i].addr = ISERIES_HV_ADDR(p + done);
1217 list[i].size = min(length-done, PAGE_SIZE);
1218 done += list[i].size;
1223 static void veth_flush_acks(struct veth_lpar_connection *cnx)
1227 rc = veth_signaldata(cnx, VethEventTypeFramesAck,
1228 0, &cnx->pending_acks);
1230 if (rc != HvLpEvent_Rc_Good)
1231 veth_error("Failed acking frames from LPAR %d, rc = %d\n",
1232 cnx->remote_lp, (int)rc);
1234 cnx->num_pending_acks = 0;
1235 memset(&cnx->pending_acks, 0xff, sizeof(cnx->pending_acks));
1238 static void veth_receive(struct veth_lpar_connection *cnx,
1239 struct VethLpEvent *event)
1241 struct VethFramesData *senddata = &event->u.frames_data;
1244 unsigned long flags;
1249 struct sk_buff *skb;
1250 struct dma_chunk local_list[VETH_MAX_PAGES_PER_FRAME];
1251 struct dma_chunk remote_list[VETH_MAX_FRAMES_PER_MSG];
1253 HvLpVirtualLanIndex vlan;
1254 struct net_device *dev;
1255 struct veth_port *port;
1257 /* FIXME: do we need this? */
1258 memset(local_list, 0, sizeof(local_list));
1259 memset(remote_list, 0, sizeof(VETH_MAX_FRAMES_PER_MSG));
1261 /* a 0 address marks the end of the valid entries */
1262 if (senddata->addr[startchunk] == 0)
1265 /* make sure that we have at least 1 EOF entry in the
1266 * remaining entries */
1267 if (! (senddata->eofmask >> (startchunk + VETH_EOF_SHIFT))) {
1268 veth_error("Missing EOF fragment in event "
1269 "eofmask = 0x%x startchunk = %d\n",
1270 (unsigned)senddata->eofmask,
1275 /* build list of chunks in this frame */
1278 remote_list[nchunks].addr =
1279 (u64) senddata->addr[startchunk+nchunks] << 32;
1280 remote_list[nchunks].size =
1281 senddata->len[startchunk+nchunks];
1282 length += remote_list[nchunks].size;
1283 } while (! (senddata->eofmask &
1284 (1 << (VETH_EOF_SHIFT + startchunk + nchunks++))));
1286 /* length == total length of all chunks */
1287 /* nchunks == # of chunks in this frame */
1289 if ((length - ETH_HLEN) > VETH_MAX_MTU) {
1290 veth_error("Received oversize frame from LPAR %d "
1292 cnx->remote_lp, length);
1296 skb = alloc_skb(length, GFP_ATOMIC);
1300 veth_build_dma_list(local_list, skb->data, length);
1302 rc = HvCallEvent_dmaBufList(HvLpEvent_Type_VirtualLan,
1303 event->base_event.xSourceLp,
1304 HvLpDma_Direction_RemoteToLocal,
1307 HvLpDma_AddressType_RealAddress,
1308 HvLpDma_AddressType_TceIndex,
1309 ISERIES_HV_ADDR(&local_list),
1310 ISERIES_HV_ADDR(&remote_list),
1312 if (rc != HvLpDma_Rc_Good) {
1313 dev_kfree_skb_irq(skb);
1317 vlan = skb->data[9];
1318 dev = veth_dev[vlan];
1321 * Some earlier versions of the driver sent
1322 * broadcasts down all connections, even to lpars
1323 * that weren't on the relevant vlan. So ignore
1324 * packets belonging to a vlan we're not on.
1325 * We can also be here if we receive packets while
1326 * the driver is going down, because then dev is NULL.
1328 dev_kfree_skb_irq(skb);
1332 port = (struct veth_port *)dev->priv;
1333 dest = *((u64 *) skb->data) & 0xFFFFFFFFFFFF0000;
1335 if ((vlan > HVMAXARCHITECTEDVIRTUALLANS) || !port) {
1336 dev_kfree_skb_irq(skb);
1339 if (! veth_frame_wanted(port, dest)) {
1340 dev_kfree_skb_irq(skb);
1344 skb_put(skb, length);
1346 skb->protocol = eth_type_trans(skb, dev);
1347 skb->ip_summed = CHECKSUM_NONE;
1348 netif_rx(skb); /* send it up */
1349 port->stats.rx_packets++;
1350 port->stats.rx_bytes += length;
1351 } while (startchunk += nchunks, startchunk < VETH_MAX_FRAMES_PER_MSG);
1354 spin_lock_irqsave(&cnx->lock, flags);
1355 BUG_ON(cnx->num_pending_acks > VETH_MAX_ACKS_PER_MSG);
1357 cnx->pending_acks[cnx->num_pending_acks++] =
1358 event->base_event.xCorrelationToken;
1360 if ( (cnx->num_pending_acks >= cnx->remote_caps.ack_threshold)
1361 || (cnx->num_pending_acks >= VETH_MAX_ACKS_PER_MSG) )
1362 veth_flush_acks(cnx);
1364 spin_unlock_irqrestore(&cnx->lock, flags);
1367 static void veth_timed_ack(unsigned long ptr)
1369 struct veth_lpar_connection *cnx = (struct veth_lpar_connection *) ptr;
1370 unsigned long flags;
1372 /* Ack all the events */
1373 spin_lock_irqsave(&cnx->lock, flags);
1374 if (cnx->num_pending_acks > 0)
1375 veth_flush_acks(cnx);
1377 /* Reschedule the timer */
1378 cnx->ack_timer.expires = jiffies + cnx->ack_timeout;
1379 add_timer(&cnx->ack_timer);
1380 spin_unlock_irqrestore(&cnx->lock, flags);
1383 static int veth_remove(struct vio_dev *vdev)
1385 struct veth_lpar_connection *cnx;
1386 struct net_device *dev;
1387 struct veth_port *port;
1390 dev = veth_dev[vdev->unit_address];
1395 port = netdev_priv(dev);
1397 for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
1400 if (cnx && (port->lpar_map & (1 << i))) {
1401 /* Drop our reference to connections on our VLAN */
1402 kobject_put(&cnx->kobject);
1406 veth_dev[vdev->unit_address] = NULL;
1407 unregister_netdev(dev);
1413 static int veth_probe(struct vio_dev *vdev, const struct vio_device_id *id)
1415 int i = vdev->unit_address;
1416 struct net_device *dev;
1417 struct veth_port *port;
1419 dev = veth_probe_one(i, &vdev->dev);
1426 port = (struct veth_port*)netdev_priv(dev);
1428 /* Start the state machine on each connection on this vlan. If we're
1429 * the first dev to do so this will commence link negotiation */
1430 for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
1431 struct veth_lpar_connection *cnx;
1433 if (! (port->lpar_map & (1 << i)))
1440 kobject_get(&cnx->kobject);
1441 veth_kick_statemachine(cnx);
1448 * veth_device_table: Used by vio.c to match devices that we
1451 static struct vio_device_id veth_device_table[] __devinitdata = {
1455 MODULE_DEVICE_TABLE(vio, veth_device_table);
1457 static struct vio_driver veth_driver = {
1458 .name = "iseries_veth",
1459 .id_table = veth_device_table,
1460 .probe = veth_probe,
1461 .remove = veth_remove
1465 * Module initialization/cleanup
1468 void __exit veth_module_cleanup(void)
1471 struct veth_lpar_connection *cnx;
1473 /* Disconnect our "irq" to stop events coming from the Hypervisor. */
1474 HvLpEvent_unregisterHandler(HvLpEvent_Type_VirtualLan);
1476 /* Make sure any work queued from Hypervisor callbacks is finished. */
1477 flush_scheduled_work();
1479 for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
1485 /* Drop the driver's reference to the connection */
1486 kobject_put(&cnx->kobject);
1489 /* Unregister the driver, which will close all the netdevs and stop
1490 * the connections when they're no longer referenced. */
1491 vio_unregister_driver(&veth_driver);
1493 module_exit(veth_module_cleanup);
1495 int __init veth_module_init(void)
1500 this_lp = HvLpConfig_getLpIndex_outline();
1502 for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
1503 rc = veth_init_connection(i);
1508 HvLpEvent_registerHandler(HvLpEvent_Type_VirtualLan,
1509 &veth_handle_event);
1511 rc = vio_register_driver(&veth_driver);
1518 for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
1519 veth_destroy_connection(veth_cnx[i]);
1524 module_init(veth_module_init);