[PATCH] iseries_veth: Incorporate iseries_veth.h in iseries_veth.c
[wandboard.git] / drivers / net / iseries_veth.c
blob2e54c85bc099a2634772ed980b49df9755ec3b52
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.
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
21 * USA
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.
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.
50 * Tunable parameters:
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>
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>
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>
82 #undef DEBUG
84 MODULE_AUTHOR("Kyle Lucke <klucke@us.ibm.com>");
85 MODULE_DESCRIPTION("iSeries Virtual ethernet driver");
86 MODULE_LICENSE("GPL");
88 #define VethEventTypeCap (0)
89 #define VethEventTypeFrames (1)
90 #define VethEventTypeMonitor (2)
91 #define VethEventTypeFramesAck (3)
93 #define VETH_MAX_ACKS_PER_MSG (20)
94 #define VETH_MAX_FRAMES_PER_MSG (6)
96 struct VethFramesData {
97 u32 addr[VETH_MAX_FRAMES_PER_MSG];
98 u16 len[VETH_MAX_FRAMES_PER_MSG];
99 u32 eofmask;
101 #define VETH_EOF_SHIFT (32-VETH_MAX_FRAMES_PER_MSG)
103 struct VethFramesAckData {
104 u16 token[VETH_MAX_ACKS_PER_MSG];
107 struct VethCapData {
108 u8 caps_version;
109 u8 rsvd1;
110 u16 num_buffers;
111 u16 ack_threshold;
112 u16 rsvd2;
113 u32 ack_timeout;
114 u32 rsvd3;
115 u64 rsvd4[3];
118 struct VethLpEvent {
119 struct HvLpEvent base_event;
120 union {
121 struct VethCapData caps_data;
122 struct VethFramesData frames_data;
123 struct VethFramesAckData frames_ack_data;
124 } u;
128 #define VETH_NUMBUFFERS (120)
129 #define VETH_ACKTIMEOUT (1000000) /* microseconds */
130 #define VETH_MAX_MCAST (12)
132 #define VETH_MAX_MTU (9000)
134 #if VETH_NUMBUFFERS < 10
135 #define ACK_THRESHOLD (1)
136 #elif VETH_NUMBUFFERS < 20
137 #define ACK_THRESHOLD (4)
138 #elif VETH_NUMBUFFERS < 40
139 #define ACK_THRESHOLD (10)
140 #else
141 #define ACK_THRESHOLD (20)
142 #endif
144 #define VETH_STATE_SHUTDOWN (0x0001)
145 #define VETH_STATE_OPEN (0x0002)
146 #define VETH_STATE_RESET (0x0004)
147 #define VETH_STATE_SENTMON (0x0008)
148 #define VETH_STATE_SENTCAPS (0x0010)
149 #define VETH_STATE_GOTCAPACK (0x0020)
150 #define VETH_STATE_GOTCAPS (0x0040)
151 #define VETH_STATE_SENTCAPACK (0x0080)
152 #define VETH_STATE_READY (0x0100)
154 struct veth_msg {
155 struct veth_msg *next;
156 struct VethFramesData data;
157 int token;
158 int in_use;
159 struct sk_buff *skb;
160 struct device *dev;
163 struct veth_lpar_connection {
164 HvLpIndex remote_lp;
165 struct work_struct statemachine_wq;
166 struct veth_msg *msgs;
167 int num_events;
168 struct VethCapData local_caps;
170 struct kobject kobject;
171 struct timer_list ack_timer;
173 struct timer_list reset_timer;
174 unsigned int reset_timeout;
175 unsigned long last_contact;
176 int outstanding_tx;
178 spinlock_t lock;
179 unsigned long state;
180 HvLpInstanceId src_inst;
181 HvLpInstanceId dst_inst;
182 struct VethLpEvent cap_event, cap_ack_event;
183 u16 pending_acks[VETH_MAX_ACKS_PER_MSG];
184 u32 num_pending_acks;
186 int num_ack_events;
187 struct VethCapData remote_caps;
188 u32 ack_timeout;
190 struct veth_msg *msg_stack_head;
193 struct veth_port {
194 struct device *dev;
195 struct net_device_stats stats;
196 u64 mac_addr;
197 HvLpIndexMap lpar_map;
199 /* queue_lock protects the stopped_map and dev's queue. */
200 spinlock_t queue_lock;
201 HvLpIndexMap stopped_map;
203 /* mcast_gate protects promiscuous, num_mcast & mcast_addr. */
204 rwlock_t mcast_gate;
205 int promiscuous;
206 int num_mcast;
207 u64 mcast_addr[VETH_MAX_MCAST];
209 struct kobject kobject;
212 static HvLpIndex this_lp;
213 static struct veth_lpar_connection *veth_cnx[HVMAXARCHITECTEDLPS]; /* = 0 */
214 static struct net_device *veth_dev[HVMAXARCHITECTEDVIRTUALLANS]; /* = 0 */
216 static int veth_start_xmit(struct sk_buff *skb, struct net_device *dev);
217 static void veth_recycle_msg(struct veth_lpar_connection *, struct veth_msg *);
218 static void veth_wake_queues(struct veth_lpar_connection *cnx);
219 static void veth_stop_queues(struct veth_lpar_connection *cnx);
220 static void veth_receive(struct veth_lpar_connection *, struct VethLpEvent *);
221 static void veth_release_connection(struct kobject *kobject);
222 static void veth_timed_ack(unsigned long ptr);
223 static void veth_timed_reset(unsigned long ptr);
226 * Utility functions
229 #define veth_info(fmt, args...) \
230 printk(KERN_INFO "iseries_veth: " fmt, ## args)
232 #define veth_error(fmt, args...) \
233 printk(KERN_ERR "iseries_veth: Error: " fmt, ## args)
235 #ifdef DEBUG
236 #define veth_debug(fmt, args...) \
237 printk(KERN_DEBUG "iseries_veth: " fmt, ## args)
238 #else
239 #define veth_debug(fmt, args...) do {} while (0)
240 #endif
242 /* You must hold the connection's lock when you call this function. */
243 static inline void veth_stack_push(struct veth_lpar_connection *cnx,
244 struct veth_msg *msg)
246 msg->next = cnx->msg_stack_head;
247 cnx->msg_stack_head = msg;
250 /* You must hold the connection's lock when you call this function. */
251 static inline struct veth_msg *veth_stack_pop(struct veth_lpar_connection *cnx)
253 struct veth_msg *msg;
255 msg = cnx->msg_stack_head;
256 if (msg)
257 cnx->msg_stack_head = cnx->msg_stack_head->next;
259 return msg;
262 /* You must hold the connection's lock when you call this function. */
263 static inline int veth_stack_is_empty(struct veth_lpar_connection *cnx)
265 return cnx->msg_stack_head == NULL;
268 static inline HvLpEvent_Rc
269 veth_signalevent(struct veth_lpar_connection *cnx, u16 subtype,
270 HvLpEvent_AckInd ackind, HvLpEvent_AckType acktype,
271 u64 token,
272 u64 data1, u64 data2, u64 data3, u64 data4, u64 data5)
274 return HvCallEvent_signalLpEventFast(cnx->remote_lp,
275 HvLpEvent_Type_VirtualLan,
276 subtype, ackind, acktype,
277 cnx->src_inst,
278 cnx->dst_inst,
279 token, data1, data2, data3,
280 data4, data5);
283 static inline HvLpEvent_Rc veth_signaldata(struct veth_lpar_connection *cnx,
284 u16 subtype, u64 token, void *data)
286 u64 *p = (u64 *) data;
288 return veth_signalevent(cnx, subtype, HvLpEvent_AckInd_NoAck,
289 HvLpEvent_AckType_ImmediateAck,
290 token, p[0], p[1], p[2], p[3], p[4]);
293 struct veth_allocation {
294 struct completion c;
295 int num;
298 static void veth_complete_allocation(void *parm, int number)
300 struct veth_allocation *vc = (struct veth_allocation *)parm;
302 vc->num = number;
303 complete(&vc->c);
306 static int veth_allocate_events(HvLpIndex rlp, int number)
308 struct veth_allocation vc = { COMPLETION_INITIALIZER(vc.c), 0 };
310 mf_allocate_lp_events(rlp, HvLpEvent_Type_VirtualLan,
311 sizeof(struct VethLpEvent), number,
312 &veth_complete_allocation, &vc);
313 wait_for_completion(&vc.c);
315 return vc.num;
319 * sysfs support
322 struct veth_cnx_attribute {
323 struct attribute attr;
324 ssize_t (*show)(struct veth_lpar_connection *, char *buf);
325 ssize_t (*store)(struct veth_lpar_connection *, const char *buf);
328 static ssize_t veth_cnx_attribute_show(struct kobject *kobj,
329 struct attribute *attr, char *buf)
331 struct veth_cnx_attribute *cnx_attr;
332 struct veth_lpar_connection *cnx;
334 cnx_attr = container_of(attr, struct veth_cnx_attribute, attr);
335 cnx = container_of(kobj, struct veth_lpar_connection, kobject);
337 if (!cnx_attr->show)
338 return -EIO;
340 return cnx_attr->show(cnx, buf);
343 #define CUSTOM_CNX_ATTR(_name, _format, _expression) \
344 static ssize_t _name##_show(struct veth_lpar_connection *cnx, char *buf)\
346 return sprintf(buf, _format, _expression); \
348 struct veth_cnx_attribute veth_cnx_attr_##_name = __ATTR_RO(_name)
350 #define SIMPLE_CNX_ATTR(_name) \
351 CUSTOM_CNX_ATTR(_name, "%lu\n", (unsigned long)cnx->_name)
353 SIMPLE_CNX_ATTR(outstanding_tx);
354 SIMPLE_CNX_ATTR(remote_lp);
355 SIMPLE_CNX_ATTR(num_events);
356 SIMPLE_CNX_ATTR(src_inst);
357 SIMPLE_CNX_ATTR(dst_inst);
358 SIMPLE_CNX_ATTR(num_pending_acks);
359 SIMPLE_CNX_ATTR(num_ack_events);
360 CUSTOM_CNX_ATTR(ack_timeout, "%d\n", jiffies_to_msecs(cnx->ack_timeout));
361 CUSTOM_CNX_ATTR(reset_timeout, "%d\n", jiffies_to_msecs(cnx->reset_timeout));
362 CUSTOM_CNX_ATTR(state, "0x%.4lX\n", cnx->state);
363 CUSTOM_CNX_ATTR(last_contact, "%d\n", cnx->last_contact ?
364 jiffies_to_msecs(jiffies - cnx->last_contact) : 0);
366 #define GET_CNX_ATTR(_name) (&veth_cnx_attr_##_name.attr)
368 static struct attribute *veth_cnx_default_attrs[] = {
369 GET_CNX_ATTR(outstanding_tx),
370 GET_CNX_ATTR(remote_lp),
371 GET_CNX_ATTR(num_events),
372 GET_CNX_ATTR(reset_timeout),
373 GET_CNX_ATTR(last_contact),
374 GET_CNX_ATTR(state),
375 GET_CNX_ATTR(src_inst),
376 GET_CNX_ATTR(dst_inst),
377 GET_CNX_ATTR(num_pending_acks),
378 GET_CNX_ATTR(num_ack_events),
379 GET_CNX_ATTR(ack_timeout),
380 NULL
383 static struct sysfs_ops veth_cnx_sysfs_ops = {
384 .show = veth_cnx_attribute_show
387 static struct kobj_type veth_lpar_connection_ktype = {
388 .release = veth_release_connection,
389 .sysfs_ops = &veth_cnx_sysfs_ops,
390 .default_attrs = veth_cnx_default_attrs
393 struct veth_port_attribute {
394 struct attribute attr;
395 ssize_t (*show)(struct veth_port *, char *buf);
396 ssize_t (*store)(struct veth_port *, const char *buf);
399 static ssize_t veth_port_attribute_show(struct kobject *kobj,
400 struct attribute *attr, char *buf)
402 struct veth_port_attribute *port_attr;
403 struct veth_port *port;
405 port_attr = container_of(attr, struct veth_port_attribute, attr);
406 port = container_of(kobj, struct veth_port, kobject);
408 if (!port_attr->show)
409 return -EIO;
411 return port_attr->show(port, buf);
414 #define CUSTOM_PORT_ATTR(_name, _format, _expression) \
415 static ssize_t _name##_show(struct veth_port *port, char *buf) \
417 return sprintf(buf, _format, _expression); \
419 struct veth_port_attribute veth_port_attr_##_name = __ATTR_RO(_name)
421 #define SIMPLE_PORT_ATTR(_name) \
422 CUSTOM_PORT_ATTR(_name, "%lu\n", (unsigned long)port->_name)
424 SIMPLE_PORT_ATTR(promiscuous);
425 SIMPLE_PORT_ATTR(num_mcast);
426 CUSTOM_PORT_ATTR(lpar_map, "0x%X\n", port->lpar_map);
427 CUSTOM_PORT_ATTR(stopped_map, "0x%X\n", port->stopped_map);
428 CUSTOM_PORT_ATTR(mac_addr, "0x%lX\n", port->mac_addr);
430 #define GET_PORT_ATTR(_name) (&veth_port_attr_##_name.attr)
431 static struct attribute *veth_port_default_attrs[] = {
432 GET_PORT_ATTR(mac_addr),
433 GET_PORT_ATTR(lpar_map),
434 GET_PORT_ATTR(stopped_map),
435 GET_PORT_ATTR(promiscuous),
436 GET_PORT_ATTR(num_mcast),
437 NULL
440 static struct sysfs_ops veth_port_sysfs_ops = {
441 .show = veth_port_attribute_show
444 static struct kobj_type veth_port_ktype = {
445 .sysfs_ops = &veth_port_sysfs_ops,
446 .default_attrs = veth_port_default_attrs
450 * LPAR connection code
453 static inline void veth_kick_statemachine(struct veth_lpar_connection *cnx)
455 schedule_work(&cnx->statemachine_wq);
458 static void veth_take_cap(struct veth_lpar_connection *cnx,
459 struct VethLpEvent *event)
461 unsigned long flags;
463 spin_lock_irqsave(&cnx->lock, flags);
464 /* Receiving caps may mean the other end has just come up, so
465 * we need to reload the instance ID of the far end */
466 cnx->dst_inst =
467 HvCallEvent_getTargetLpInstanceId(cnx->remote_lp,
468 HvLpEvent_Type_VirtualLan);
470 if (cnx->state & VETH_STATE_GOTCAPS) {
471 veth_error("Received a second capabilities from LPAR %d.\n",
472 cnx->remote_lp);
473 event->base_event.xRc = HvLpEvent_Rc_BufferNotAvailable;
474 HvCallEvent_ackLpEvent((struct HvLpEvent *) event);
475 } else {
476 memcpy(&cnx->cap_event, event, sizeof(cnx->cap_event));
477 cnx->state |= VETH_STATE_GOTCAPS;
478 veth_kick_statemachine(cnx);
480 spin_unlock_irqrestore(&cnx->lock, flags);
483 static void veth_take_cap_ack(struct veth_lpar_connection *cnx,
484 struct VethLpEvent *event)
486 unsigned long flags;
488 spin_lock_irqsave(&cnx->lock, flags);
489 if (cnx->state & VETH_STATE_GOTCAPACK) {
490 veth_error("Received a second capabilities ack from LPAR %d.\n",
491 cnx->remote_lp);
492 } else {
493 memcpy(&cnx->cap_ack_event, event,
494 sizeof(&cnx->cap_ack_event));
495 cnx->state |= VETH_STATE_GOTCAPACK;
496 veth_kick_statemachine(cnx);
498 spin_unlock_irqrestore(&cnx->lock, flags);
501 static void veth_take_monitor_ack(struct veth_lpar_connection *cnx,
502 struct VethLpEvent *event)
504 unsigned long flags;
506 spin_lock_irqsave(&cnx->lock, flags);
507 veth_debug("cnx %d: lost connection.\n", cnx->remote_lp);
509 /* Avoid kicking the statemachine once we're shutdown.
510 * It's unnecessary and it could break veth_stop_connection(). */
512 if (! (cnx->state & VETH_STATE_SHUTDOWN)) {
513 cnx->state |= VETH_STATE_RESET;
514 veth_kick_statemachine(cnx);
516 spin_unlock_irqrestore(&cnx->lock, flags);
519 static void veth_handle_ack(struct VethLpEvent *event)
521 HvLpIndex rlp = event->base_event.xTargetLp;
522 struct veth_lpar_connection *cnx = veth_cnx[rlp];
524 BUG_ON(! cnx);
526 switch (event->base_event.xSubtype) {
527 case VethEventTypeCap:
528 veth_take_cap_ack(cnx, event);
529 break;
530 case VethEventTypeMonitor:
531 veth_take_monitor_ack(cnx, event);
532 break;
533 default:
534 veth_error("Unknown ack type %d from LPAR %d.\n",
535 event->base_event.xSubtype, rlp);
539 static void veth_handle_int(struct VethLpEvent *event)
541 HvLpIndex rlp = event->base_event.xSourceLp;
542 struct veth_lpar_connection *cnx = veth_cnx[rlp];
543 unsigned long flags;
544 int i, acked = 0;
546 BUG_ON(! cnx);
548 switch (event->base_event.xSubtype) {
549 case VethEventTypeCap:
550 veth_take_cap(cnx, event);
551 break;
552 case VethEventTypeMonitor:
553 /* do nothing... this'll hang out here til we're dead,
554 * and the hypervisor will return it for us. */
555 break;
556 case VethEventTypeFramesAck:
557 spin_lock_irqsave(&cnx->lock, flags);
559 for (i = 0; i < VETH_MAX_ACKS_PER_MSG; ++i) {
560 u16 msgnum = event->u.frames_ack_data.token[i];
562 if (msgnum < VETH_NUMBUFFERS) {
563 veth_recycle_msg(cnx, cnx->msgs + msgnum);
564 cnx->outstanding_tx--;
565 acked++;
569 if (acked > 0) {
570 cnx->last_contact = jiffies;
571 veth_wake_queues(cnx);
574 spin_unlock_irqrestore(&cnx->lock, flags);
575 break;
576 case VethEventTypeFrames:
577 veth_receive(cnx, event);
578 break;
579 default:
580 veth_error("Unknown interrupt type %d from LPAR %d.\n",
581 event->base_event.xSubtype, rlp);
585 static void veth_handle_event(struct HvLpEvent *event, struct pt_regs *regs)
587 struct VethLpEvent *veth_event = (struct VethLpEvent *)event;
589 if (event->xFlags.xFunction == HvLpEvent_Function_Ack)
590 veth_handle_ack(veth_event);
591 else if (event->xFlags.xFunction == HvLpEvent_Function_Int)
592 veth_handle_int(veth_event);
595 static int veth_process_caps(struct veth_lpar_connection *cnx)
597 struct VethCapData *remote_caps = &cnx->remote_caps;
598 int num_acks_needed;
600 /* Convert timer to jiffies */
601 cnx->ack_timeout = remote_caps->ack_timeout * HZ / 1000000;
603 if ( (remote_caps->num_buffers == 0)
604 || (remote_caps->ack_threshold > VETH_MAX_ACKS_PER_MSG)
605 || (remote_caps->ack_threshold == 0)
606 || (cnx->ack_timeout == 0) ) {
607 veth_error("Received incompatible capabilities from LPAR %d.\n",
608 cnx->remote_lp);
609 return HvLpEvent_Rc_InvalidSubtypeData;
612 num_acks_needed = (remote_caps->num_buffers
613 / remote_caps->ack_threshold) + 1;
615 /* FIXME: locking on num_ack_events? */
616 if (cnx->num_ack_events < num_acks_needed) {
617 int num;
619 num = veth_allocate_events(cnx->remote_lp,
620 num_acks_needed-cnx->num_ack_events);
621 if (num > 0)
622 cnx->num_ack_events += num;
624 if (cnx->num_ack_events < num_acks_needed) {
625 veth_error("Couldn't allocate enough ack events "
626 "for LPAR %d.\n", cnx->remote_lp);
628 return HvLpEvent_Rc_BufferNotAvailable;
633 return HvLpEvent_Rc_Good;
636 /* FIXME: The gotos here are a bit dubious */
637 static void veth_statemachine(void *p)
639 struct veth_lpar_connection *cnx = (struct veth_lpar_connection *)p;
640 int rlp = cnx->remote_lp;
641 int rc;
643 spin_lock_irq(&cnx->lock);
645 restart:
646 if (cnx->state & VETH_STATE_RESET) {
647 if (cnx->state & VETH_STATE_OPEN)
648 HvCallEvent_closeLpEventPath(cnx->remote_lp,
649 HvLpEvent_Type_VirtualLan);
652 * Reset ack data. This prevents the ack_timer actually
653 * doing anything, even if it runs one more time when
654 * we drop the lock below.
656 memset(&cnx->pending_acks, 0xff, sizeof (cnx->pending_acks));
657 cnx->num_pending_acks = 0;
659 cnx->state &= ~(VETH_STATE_RESET | VETH_STATE_SENTMON
660 | VETH_STATE_OPEN | VETH_STATE_SENTCAPS
661 | VETH_STATE_GOTCAPACK | VETH_STATE_GOTCAPS
662 | VETH_STATE_SENTCAPACK | VETH_STATE_READY);
664 /* Clean up any leftover messages */
665 if (cnx->msgs) {
666 int i;
667 for (i = 0; i < VETH_NUMBUFFERS; ++i)
668 veth_recycle_msg(cnx, cnx->msgs + i);
671 cnx->outstanding_tx = 0;
672 veth_wake_queues(cnx);
674 /* Drop the lock so we can do stuff that might sleep or
675 * take other locks. */
676 spin_unlock_irq(&cnx->lock);
678 del_timer_sync(&cnx->ack_timer);
679 del_timer_sync(&cnx->reset_timer);
681 spin_lock_irq(&cnx->lock);
683 if (cnx->state & VETH_STATE_RESET)
684 goto restart;
686 /* Hack, wait for the other end to reset itself. */
687 if (! (cnx->state & VETH_STATE_SHUTDOWN)) {
688 schedule_delayed_work(&cnx->statemachine_wq, 5 * HZ);
689 goto out;
693 if (cnx->state & VETH_STATE_SHUTDOWN)
694 /* It's all over, do nothing */
695 goto out;
697 if ( !(cnx->state & VETH_STATE_OPEN) ) {
698 if (! cnx->msgs || (cnx->num_events < (2 + VETH_NUMBUFFERS)) )
699 goto cant_cope;
701 HvCallEvent_openLpEventPath(rlp, HvLpEvent_Type_VirtualLan);
702 cnx->src_inst =
703 HvCallEvent_getSourceLpInstanceId(rlp,
704 HvLpEvent_Type_VirtualLan);
705 cnx->dst_inst =
706 HvCallEvent_getTargetLpInstanceId(rlp,
707 HvLpEvent_Type_VirtualLan);
708 cnx->state |= VETH_STATE_OPEN;
711 if ( (cnx->state & VETH_STATE_OPEN)
712 && !(cnx->state & VETH_STATE_SENTMON) ) {
713 rc = veth_signalevent(cnx, VethEventTypeMonitor,
714 HvLpEvent_AckInd_DoAck,
715 HvLpEvent_AckType_DeferredAck,
716 0, 0, 0, 0, 0, 0);
718 if (rc == HvLpEvent_Rc_Good) {
719 cnx->state |= VETH_STATE_SENTMON;
720 } else {
721 if ( (rc != HvLpEvent_Rc_PartitionDead)
722 && (rc != HvLpEvent_Rc_PathClosed) )
723 veth_error("Error sending monitor to LPAR %d, "
724 "rc = %d\n", rlp, rc);
726 /* Oh well, hope we get a cap from the other
727 * end and do better when that kicks us */
728 goto out;
732 if ( (cnx->state & VETH_STATE_OPEN)
733 && !(cnx->state & VETH_STATE_SENTCAPS)) {
734 u64 *rawcap = (u64 *)&cnx->local_caps;
736 rc = veth_signalevent(cnx, VethEventTypeCap,
737 HvLpEvent_AckInd_DoAck,
738 HvLpEvent_AckType_ImmediateAck,
739 0, rawcap[0], rawcap[1], rawcap[2],
740 rawcap[3], rawcap[4]);
742 if (rc == HvLpEvent_Rc_Good) {
743 cnx->state |= VETH_STATE_SENTCAPS;
744 } else {
745 if ( (rc != HvLpEvent_Rc_PartitionDead)
746 && (rc != HvLpEvent_Rc_PathClosed) )
747 veth_error("Error sending caps to LPAR %d, "
748 "rc = %d\n", rlp, rc);
750 /* Oh well, hope we get a cap from the other
751 * end and do better when that kicks us */
752 goto out;
756 if ((cnx->state & VETH_STATE_GOTCAPS)
757 && !(cnx->state & VETH_STATE_SENTCAPACK)) {
758 struct VethCapData *remote_caps = &cnx->remote_caps;
760 memcpy(remote_caps, &cnx->cap_event.u.caps_data,
761 sizeof(*remote_caps));
763 spin_unlock_irq(&cnx->lock);
764 rc = veth_process_caps(cnx);
765 spin_lock_irq(&cnx->lock);
767 /* We dropped the lock, so recheck for anything which
768 * might mess us up */
769 if (cnx->state & (VETH_STATE_RESET|VETH_STATE_SHUTDOWN))
770 goto restart;
772 cnx->cap_event.base_event.xRc = rc;
773 HvCallEvent_ackLpEvent((struct HvLpEvent *)&cnx->cap_event);
774 if (rc == HvLpEvent_Rc_Good)
775 cnx->state |= VETH_STATE_SENTCAPACK;
776 else
777 goto cant_cope;
780 if ((cnx->state & VETH_STATE_GOTCAPACK)
781 && (cnx->state & VETH_STATE_GOTCAPS)
782 && !(cnx->state & VETH_STATE_READY)) {
783 if (cnx->cap_ack_event.base_event.xRc == HvLpEvent_Rc_Good) {
784 /* Start the ACK timer */
785 cnx->ack_timer.expires = jiffies + cnx->ack_timeout;
786 add_timer(&cnx->ack_timer);
787 cnx->state |= VETH_STATE_READY;
788 } else {
789 veth_error("Caps rejected by LPAR %d, rc = %d\n",
790 rlp, cnx->cap_ack_event.base_event.xRc);
791 goto cant_cope;
795 out:
796 spin_unlock_irq(&cnx->lock);
797 return;
799 cant_cope:
800 /* FIXME: we get here if something happens we really can't
801 * cope with. The link will never work once we get here, and
802 * all we can do is not lock the rest of the system up */
803 veth_error("Unrecoverable error on connection to LPAR %d, shutting down"
804 " (state = 0x%04lx)\n", rlp, cnx->state);
805 cnx->state |= VETH_STATE_SHUTDOWN;
806 spin_unlock_irq(&cnx->lock);
809 static int veth_init_connection(u8 rlp)
811 struct veth_lpar_connection *cnx;
812 struct veth_msg *msgs;
813 int i, rc;
815 if ( (rlp == this_lp)
816 || ! HvLpConfig_doLpsCommunicateOnVirtualLan(this_lp, rlp) )
817 return 0;
819 cnx = kmalloc(sizeof(*cnx), GFP_KERNEL);
820 if (! cnx)
821 return -ENOMEM;
822 memset(cnx, 0, sizeof(*cnx));
824 cnx->remote_lp = rlp;
825 spin_lock_init(&cnx->lock);
826 INIT_WORK(&cnx->statemachine_wq, veth_statemachine, cnx);
828 init_timer(&cnx->ack_timer);
829 cnx->ack_timer.function = veth_timed_ack;
830 cnx->ack_timer.data = (unsigned long) cnx;
832 init_timer(&cnx->reset_timer);
833 cnx->reset_timer.function = veth_timed_reset;
834 cnx->reset_timer.data = (unsigned long) cnx;
835 cnx->reset_timeout = 5 * HZ * (VETH_ACKTIMEOUT / 1000000);
837 memset(&cnx->pending_acks, 0xff, sizeof (cnx->pending_acks));
839 veth_cnx[rlp] = cnx;
841 /* This gets us 1 reference, which is held on behalf of the driver
842 * infrastructure. It's released at module unload. */
843 kobject_init(&cnx->kobject);
844 cnx->kobject.ktype = &veth_lpar_connection_ktype;
845 rc = kobject_set_name(&cnx->kobject, "cnx%.2d", rlp);
846 if (rc != 0)
847 return rc;
849 msgs = kmalloc(VETH_NUMBUFFERS * sizeof(struct veth_msg), GFP_KERNEL);
850 if (! msgs) {
851 veth_error("Can't allocate buffers for LPAR %d.\n", rlp);
852 return -ENOMEM;
855 cnx->msgs = msgs;
856 memset(msgs, 0, VETH_NUMBUFFERS * sizeof(struct veth_msg));
858 for (i = 0; i < VETH_NUMBUFFERS; i++) {
859 msgs[i].token = i;
860 veth_stack_push(cnx, msgs + i);
863 cnx->num_events = veth_allocate_events(rlp, 2 + VETH_NUMBUFFERS);
865 if (cnx->num_events < (2 + VETH_NUMBUFFERS)) {
866 veth_error("Can't allocate enough events for LPAR %d.\n", rlp);
867 return -ENOMEM;
870 cnx->local_caps.num_buffers = VETH_NUMBUFFERS;
871 cnx->local_caps.ack_threshold = ACK_THRESHOLD;
872 cnx->local_caps.ack_timeout = VETH_ACKTIMEOUT;
874 return 0;
877 static void veth_stop_connection(struct veth_lpar_connection *cnx)
879 if (!cnx)
880 return;
882 spin_lock_irq(&cnx->lock);
883 cnx->state |= VETH_STATE_RESET | VETH_STATE_SHUTDOWN;
884 veth_kick_statemachine(cnx);
885 spin_unlock_irq(&cnx->lock);
887 /* There's a slim chance the reset code has just queued the
888 * statemachine to run in five seconds. If so we need to cancel
889 * that and requeue the work to run now. */
890 if (cancel_delayed_work(&cnx->statemachine_wq)) {
891 spin_lock_irq(&cnx->lock);
892 veth_kick_statemachine(cnx);
893 spin_unlock_irq(&cnx->lock);
896 /* Wait for the state machine to run. */
897 flush_scheduled_work();
900 static void veth_destroy_connection(struct veth_lpar_connection *cnx)
902 if (!cnx)
903 return;
905 if (cnx->num_events > 0)
906 mf_deallocate_lp_events(cnx->remote_lp,
907 HvLpEvent_Type_VirtualLan,
908 cnx->num_events,
909 NULL, NULL);
910 if (cnx->num_ack_events > 0)
911 mf_deallocate_lp_events(cnx->remote_lp,
912 HvLpEvent_Type_VirtualLan,
913 cnx->num_ack_events,
914 NULL, NULL);
916 kfree(cnx->msgs);
917 veth_cnx[cnx->remote_lp] = NULL;
918 kfree(cnx);
921 static void veth_release_connection(struct kobject *kobj)
923 struct veth_lpar_connection *cnx;
924 cnx = container_of(kobj, struct veth_lpar_connection, kobject);
925 veth_stop_connection(cnx);
926 veth_destroy_connection(cnx);
930 * net_device code
933 static int veth_open(struct net_device *dev)
935 struct veth_port *port = (struct veth_port *) dev->priv;
937 memset(&port->stats, 0, sizeof (port->stats));
938 netif_start_queue(dev);
939 return 0;
942 static int veth_close(struct net_device *dev)
944 netif_stop_queue(dev);
945 return 0;
948 static struct net_device_stats *veth_get_stats(struct net_device *dev)
950 struct veth_port *port = (struct veth_port *) dev->priv;
952 return &port->stats;
955 static int veth_change_mtu(struct net_device *dev, int new_mtu)
957 if ((new_mtu < 68) || (new_mtu > VETH_MAX_MTU))
958 return -EINVAL;
959 dev->mtu = new_mtu;
960 return 0;
963 static void veth_set_multicast_list(struct net_device *dev)
965 struct veth_port *port = (struct veth_port *) dev->priv;
966 unsigned long flags;
968 write_lock_irqsave(&port->mcast_gate, flags);
970 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
971 (dev->mc_count > VETH_MAX_MCAST)) {
972 port->promiscuous = 1;
973 } else {
974 struct dev_mc_list *dmi = dev->mc_list;
975 int i;
977 port->promiscuous = 0;
979 /* Update table */
980 port->num_mcast = 0;
982 for (i = 0; i < dev->mc_count; i++) {
983 u8 *addr = dmi->dmi_addr;
984 u64 xaddr = 0;
986 if (addr[0] & 0x01) {/* multicast address? */
987 memcpy(&xaddr, addr, ETH_ALEN);
988 port->mcast_addr[port->num_mcast] = xaddr;
989 port->num_mcast++;
991 dmi = dmi->next;
995 write_unlock_irqrestore(&port->mcast_gate, flags);
998 static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1000 strncpy(info->driver, "veth", sizeof(info->driver) - 1);
1001 info->driver[sizeof(info->driver) - 1] = '\0';
1002 strncpy(info->version, "1.0", sizeof(info->version) - 1);
1005 static int veth_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1007 ecmd->supported = (SUPPORTED_1000baseT_Full
1008 | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
1009 ecmd->advertising = (SUPPORTED_1000baseT_Full
1010 | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
1011 ecmd->port = PORT_FIBRE;
1012 ecmd->transceiver = XCVR_INTERNAL;
1013 ecmd->phy_address = 0;
1014 ecmd->speed = SPEED_1000;
1015 ecmd->duplex = DUPLEX_FULL;
1016 ecmd->autoneg = AUTONEG_ENABLE;
1017 ecmd->maxtxpkt = 120;
1018 ecmd->maxrxpkt = 120;
1019 return 0;
1022 static u32 veth_get_link(struct net_device *dev)
1024 return 1;
1027 static struct ethtool_ops ops = {
1028 .get_drvinfo = veth_get_drvinfo,
1029 .get_settings = veth_get_settings,
1030 .get_link = veth_get_link,
1033 static struct net_device * __init veth_probe_one(int vlan, struct device *vdev)
1035 struct net_device *dev;
1036 struct veth_port *port;
1037 int i, rc;
1039 dev = alloc_etherdev(sizeof (struct veth_port));
1040 if (! dev) {
1041 veth_error("Unable to allocate net_device structure!\n");
1042 return NULL;
1045 port = (struct veth_port *) dev->priv;
1047 spin_lock_init(&port->queue_lock);
1048 rwlock_init(&port->mcast_gate);
1049 port->stopped_map = 0;
1051 for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
1052 HvLpVirtualLanIndexMap map;
1054 if (i == this_lp)
1055 continue;
1056 map = HvLpConfig_getVirtualLanIndexMapForLp(i);
1057 if (map & (0x8000 >> vlan))
1058 port->lpar_map |= (1 << i);
1060 port->dev = vdev;
1062 dev->dev_addr[0] = 0x02;
1063 dev->dev_addr[1] = 0x01;
1064 dev->dev_addr[2] = 0xff;
1065 dev->dev_addr[3] = vlan;
1066 dev->dev_addr[4] = 0xff;
1067 dev->dev_addr[5] = this_lp;
1069 dev->mtu = VETH_MAX_MTU;
1071 memcpy(&port->mac_addr, dev->dev_addr, 6);
1073 dev->open = veth_open;
1074 dev->hard_start_xmit = veth_start_xmit;
1075 dev->stop = veth_close;
1076 dev->get_stats = veth_get_stats;
1077 dev->change_mtu = veth_change_mtu;
1078 dev->set_mac_address = NULL;
1079 dev->set_multicast_list = veth_set_multicast_list;
1080 SET_ETHTOOL_OPS(dev, &ops);
1082 SET_NETDEV_DEV(dev, vdev);
1084 rc = register_netdev(dev);
1085 if (rc != 0) {
1086 veth_error("Failed registering net device for vlan%d.\n", vlan);
1087 free_netdev(dev);
1088 return NULL;
1091 kobject_init(&port->kobject);
1092 port->kobject.parent = &dev->class_dev.kobj;
1093 port->kobject.ktype = &veth_port_ktype;
1094 kobject_set_name(&port->kobject, "veth_port");
1095 if (0 != kobject_add(&port->kobject))
1096 veth_error("Failed adding port for %s to sysfs.\n", dev->name);
1098 veth_info("%s attached to iSeries vlan %d (LPAR map = 0x%.4X)\n",
1099 dev->name, vlan, port->lpar_map);
1101 return dev;
1105 * Tx path
1108 static int veth_transmit_to_one(struct sk_buff *skb, HvLpIndex rlp,
1109 struct net_device *dev)
1111 struct veth_lpar_connection *cnx = veth_cnx[rlp];
1112 struct veth_port *port = (struct veth_port *) dev->priv;
1113 HvLpEvent_Rc rc;
1114 struct veth_msg *msg = NULL;
1115 unsigned long flags;
1117 if (! cnx)
1118 return 0;
1120 spin_lock_irqsave(&cnx->lock, flags);
1122 if (! (cnx->state & VETH_STATE_READY))
1123 goto no_error;
1125 if ((skb->len - ETH_HLEN) > VETH_MAX_MTU)
1126 goto drop;
1128 msg = veth_stack_pop(cnx);
1129 if (! msg)
1130 goto drop;
1132 msg->in_use = 1;
1133 msg->skb = skb_get(skb);
1135 msg->data.addr[0] = dma_map_single(port->dev, skb->data,
1136 skb->len, DMA_TO_DEVICE);
1138 if (dma_mapping_error(msg->data.addr[0]))
1139 goto recycle_and_drop;
1141 msg->dev = port->dev;
1142 msg->data.len[0] = skb->len;
1143 msg->data.eofmask = 1 << VETH_EOF_SHIFT;
1145 rc = veth_signaldata(cnx, VethEventTypeFrames, msg->token, &msg->data);
1147 if (rc != HvLpEvent_Rc_Good)
1148 goto recycle_and_drop;
1150 /* If the timer's not already running, start it now. */
1151 if (0 == cnx->outstanding_tx)
1152 mod_timer(&cnx->reset_timer, jiffies + cnx->reset_timeout);
1154 cnx->last_contact = jiffies;
1155 cnx->outstanding_tx++;
1157 if (veth_stack_is_empty(cnx))
1158 veth_stop_queues(cnx);
1160 no_error:
1161 spin_unlock_irqrestore(&cnx->lock, flags);
1162 return 0;
1164 recycle_and_drop:
1165 veth_recycle_msg(cnx, msg);
1166 drop:
1167 spin_unlock_irqrestore(&cnx->lock, flags);
1168 return 1;
1171 static void veth_transmit_to_many(struct sk_buff *skb,
1172 HvLpIndexMap lpmask,
1173 struct net_device *dev)
1175 struct veth_port *port = (struct veth_port *) dev->priv;
1176 int i, success, error;
1178 success = error = 0;
1180 for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
1181 if ((lpmask & (1 << i)) == 0)
1182 continue;
1184 if (veth_transmit_to_one(skb, i, dev))
1185 error = 1;
1186 else
1187 success = 1;
1190 if (error)
1191 port->stats.tx_errors++;
1193 if (success) {
1194 port->stats.tx_packets++;
1195 port->stats.tx_bytes += skb->len;
1199 static int veth_start_xmit(struct sk_buff *skb, struct net_device *dev)
1201 unsigned char *frame = skb->data;
1202 struct veth_port *port = (struct veth_port *) dev->priv;
1203 HvLpIndexMap lpmask;
1205 if (! (frame[0] & 0x01)) {
1206 /* unicast packet */
1207 HvLpIndex rlp = frame[5];
1209 if ( ! ((1 << rlp) & port->lpar_map) ) {
1210 dev_kfree_skb(skb);
1211 return 0;
1214 lpmask = 1 << rlp;
1215 } else {
1216 lpmask = port->lpar_map;
1219 veth_transmit_to_many(skb, lpmask, dev);
1221 dev_kfree_skb(skb);
1223 return 0;
1226 /* You must hold the connection's lock when you call this function. */
1227 static void veth_recycle_msg(struct veth_lpar_connection *cnx,
1228 struct veth_msg *msg)
1230 u32 dma_address, dma_length;
1232 if (msg->in_use) {
1233 msg->in_use = 0;
1234 dma_address = msg->data.addr[0];
1235 dma_length = msg->data.len[0];
1237 if (!dma_mapping_error(dma_address))
1238 dma_unmap_single(msg->dev, dma_address, dma_length,
1239 DMA_TO_DEVICE);
1241 if (msg->skb) {
1242 dev_kfree_skb_any(msg->skb);
1243 msg->skb = NULL;
1246 memset(&msg->data, 0, sizeof(msg->data));
1247 veth_stack_push(cnx, msg);
1248 } else if (cnx->state & VETH_STATE_OPEN) {
1249 veth_error("Non-pending frame (# %d) acked by LPAR %d.\n",
1250 cnx->remote_lp, msg->token);
1254 static void veth_wake_queues(struct veth_lpar_connection *cnx)
1256 int i;
1258 for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++) {
1259 struct net_device *dev = veth_dev[i];
1260 struct veth_port *port;
1261 unsigned long flags;
1263 if (! dev)
1264 continue;
1266 port = (struct veth_port *)dev->priv;
1268 if (! (port->lpar_map & (1<<cnx->remote_lp)))
1269 continue;
1271 spin_lock_irqsave(&port->queue_lock, flags);
1273 port->stopped_map &= ~(1 << cnx->remote_lp);
1275 if (0 == port->stopped_map && netif_queue_stopped(dev)) {
1276 veth_debug("cnx %d: woke queue for %s.\n",
1277 cnx->remote_lp, dev->name);
1278 netif_wake_queue(dev);
1280 spin_unlock_irqrestore(&port->queue_lock, flags);
1284 static void veth_stop_queues(struct veth_lpar_connection *cnx)
1286 int i;
1288 for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++) {
1289 struct net_device *dev = veth_dev[i];
1290 struct veth_port *port;
1292 if (! dev)
1293 continue;
1295 port = (struct veth_port *)dev->priv;
1297 /* If this cnx is not on the vlan for this port, continue */
1298 if (! (port->lpar_map & (1 << cnx->remote_lp)))
1299 continue;
1301 spin_lock(&port->queue_lock);
1303 netif_stop_queue(dev);
1304 port->stopped_map |= (1 << cnx->remote_lp);
1306 veth_debug("cnx %d: stopped queue for %s, map = 0x%x.\n",
1307 cnx->remote_lp, dev->name, port->stopped_map);
1309 spin_unlock(&port->queue_lock);
1313 static void veth_timed_reset(unsigned long ptr)
1315 struct veth_lpar_connection *cnx = (struct veth_lpar_connection *)ptr;
1316 unsigned long trigger_time, flags;
1318 /* FIXME is it possible this fires after veth_stop_connection()?
1319 * That would reschedule the statemachine for 5 seconds and probably
1320 * execute it after the module's been unloaded. Hmm. */
1322 spin_lock_irqsave(&cnx->lock, flags);
1324 if (cnx->outstanding_tx > 0) {
1325 trigger_time = cnx->last_contact + cnx->reset_timeout;
1327 if (trigger_time < jiffies) {
1328 cnx->state |= VETH_STATE_RESET;
1329 veth_kick_statemachine(cnx);
1330 veth_error("%d packets not acked by LPAR %d within %d "
1331 "seconds, resetting.\n",
1332 cnx->outstanding_tx, cnx->remote_lp,
1333 cnx->reset_timeout / HZ);
1334 } else {
1335 /* Reschedule the timer */
1336 trigger_time = jiffies + cnx->reset_timeout;
1337 mod_timer(&cnx->reset_timer, trigger_time);
1341 spin_unlock_irqrestore(&cnx->lock, flags);
1345 * Rx path
1348 static inline int veth_frame_wanted(struct veth_port *port, u64 mac_addr)
1350 int wanted = 0;
1351 int i;
1352 unsigned long flags;
1354 if ( (mac_addr == port->mac_addr) || (mac_addr == 0xffffffffffff0000) )
1355 return 1;
1357 read_lock_irqsave(&port->mcast_gate, flags);
1359 if (port->promiscuous) {
1360 wanted = 1;
1361 goto out;
1364 for (i = 0; i < port->num_mcast; ++i) {
1365 if (port->mcast_addr[i] == mac_addr) {
1366 wanted = 1;
1367 break;
1371 out:
1372 read_unlock_irqrestore(&port->mcast_gate, flags);
1374 return wanted;
1377 struct dma_chunk {
1378 u64 addr;
1379 u64 size;
1382 #define VETH_MAX_PAGES_PER_FRAME ( (VETH_MAX_MTU+PAGE_SIZE-2)/PAGE_SIZE + 1 )
1384 static inline void veth_build_dma_list(struct dma_chunk *list,
1385 unsigned char *p, unsigned long length)
1387 unsigned long done;
1388 int i = 1;
1390 /* FIXME: skbs are continguous in real addresses. Do we
1391 * really need to break it into PAGE_SIZE chunks, or can we do
1392 * it just at the granularity of iSeries real->absolute
1393 * mapping? Indeed, given the way the allocator works, can we
1394 * count on them being absolutely contiguous? */
1395 list[0].addr = ISERIES_HV_ADDR(p);
1396 list[0].size = min(length,
1397 PAGE_SIZE - ((unsigned long)p & ~PAGE_MASK));
1399 done = list[0].size;
1400 while (done < length) {
1401 list[i].addr = ISERIES_HV_ADDR(p + done);
1402 list[i].size = min(length-done, PAGE_SIZE);
1403 done += list[i].size;
1404 i++;
1408 static void veth_flush_acks(struct veth_lpar_connection *cnx)
1410 HvLpEvent_Rc rc;
1412 rc = veth_signaldata(cnx, VethEventTypeFramesAck,
1413 0, &cnx->pending_acks);
1415 if (rc != HvLpEvent_Rc_Good)
1416 veth_error("Failed acking frames from LPAR %d, rc = %d\n",
1417 cnx->remote_lp, (int)rc);
1419 cnx->num_pending_acks = 0;
1420 memset(&cnx->pending_acks, 0xff, sizeof(cnx->pending_acks));
1423 static void veth_receive(struct veth_lpar_connection *cnx,
1424 struct VethLpEvent *event)
1426 struct VethFramesData *senddata = &event->u.frames_data;
1427 int startchunk = 0;
1428 int nchunks;
1429 unsigned long flags;
1430 HvLpDma_Rc rc;
1432 do {
1433 u16 length = 0;
1434 struct sk_buff *skb;
1435 struct dma_chunk local_list[VETH_MAX_PAGES_PER_FRAME];
1436 struct dma_chunk remote_list[VETH_MAX_FRAMES_PER_MSG];
1437 u64 dest;
1438 HvLpVirtualLanIndex vlan;
1439 struct net_device *dev;
1440 struct veth_port *port;
1442 /* FIXME: do we need this? */
1443 memset(local_list, 0, sizeof(local_list));
1444 memset(remote_list, 0, sizeof(VETH_MAX_FRAMES_PER_MSG));
1446 /* a 0 address marks the end of the valid entries */
1447 if (senddata->addr[startchunk] == 0)
1448 break;
1450 /* make sure that we have at least 1 EOF entry in the
1451 * remaining entries */
1452 if (! (senddata->eofmask >> (startchunk + VETH_EOF_SHIFT))) {
1453 veth_error("Missing EOF fragment in event "
1454 "eofmask = 0x%x startchunk = %d\n",
1455 (unsigned)senddata->eofmask,
1456 startchunk);
1457 break;
1460 /* build list of chunks in this frame */
1461 nchunks = 0;
1462 do {
1463 remote_list[nchunks].addr =
1464 (u64) senddata->addr[startchunk+nchunks] << 32;
1465 remote_list[nchunks].size =
1466 senddata->len[startchunk+nchunks];
1467 length += remote_list[nchunks].size;
1468 } while (! (senddata->eofmask &
1469 (1 << (VETH_EOF_SHIFT + startchunk + nchunks++))));
1471 /* length == total length of all chunks */
1472 /* nchunks == # of chunks in this frame */
1474 if ((length - ETH_HLEN) > VETH_MAX_MTU) {
1475 veth_error("Received oversize frame from LPAR %d "
1476 "(length = %d)\n",
1477 cnx->remote_lp, length);
1478 continue;
1481 skb = alloc_skb(length, GFP_ATOMIC);
1482 if (!skb)
1483 continue;
1485 veth_build_dma_list(local_list, skb->data, length);
1487 rc = HvCallEvent_dmaBufList(HvLpEvent_Type_VirtualLan,
1488 event->base_event.xSourceLp,
1489 HvLpDma_Direction_RemoteToLocal,
1490 cnx->src_inst,
1491 cnx->dst_inst,
1492 HvLpDma_AddressType_RealAddress,
1493 HvLpDma_AddressType_TceIndex,
1494 ISERIES_HV_ADDR(&local_list),
1495 ISERIES_HV_ADDR(&remote_list),
1496 length);
1497 if (rc != HvLpDma_Rc_Good) {
1498 dev_kfree_skb_irq(skb);
1499 continue;
1502 vlan = skb->data[9];
1503 dev = veth_dev[vlan];
1504 if (! dev) {
1506 * Some earlier versions of the driver sent
1507 * broadcasts down all connections, even to lpars
1508 * that weren't on the relevant vlan. So ignore
1509 * packets belonging to a vlan we're not on.
1510 * We can also be here if we receive packets while
1511 * the driver is going down, because then dev is NULL.
1513 dev_kfree_skb_irq(skb);
1514 continue;
1517 port = (struct veth_port *)dev->priv;
1518 dest = *((u64 *) skb->data) & 0xFFFFFFFFFFFF0000;
1520 if ((vlan > HVMAXARCHITECTEDVIRTUALLANS) || !port) {
1521 dev_kfree_skb_irq(skb);
1522 continue;
1524 if (! veth_frame_wanted(port, dest)) {
1525 dev_kfree_skb_irq(skb);
1526 continue;
1529 skb_put(skb, length);
1530 skb->dev = dev;
1531 skb->protocol = eth_type_trans(skb, dev);
1532 skb->ip_summed = CHECKSUM_NONE;
1533 netif_rx(skb); /* send it up */
1534 port->stats.rx_packets++;
1535 port->stats.rx_bytes += length;
1536 } while (startchunk += nchunks, startchunk < VETH_MAX_FRAMES_PER_MSG);
1538 /* Ack it */
1539 spin_lock_irqsave(&cnx->lock, flags);
1540 BUG_ON(cnx->num_pending_acks > VETH_MAX_ACKS_PER_MSG);
1542 cnx->pending_acks[cnx->num_pending_acks++] =
1543 event->base_event.xCorrelationToken;
1545 if ( (cnx->num_pending_acks >= cnx->remote_caps.ack_threshold)
1546 || (cnx->num_pending_acks >= VETH_MAX_ACKS_PER_MSG) )
1547 veth_flush_acks(cnx);
1549 spin_unlock_irqrestore(&cnx->lock, flags);
1552 static void veth_timed_ack(unsigned long ptr)
1554 struct veth_lpar_connection *cnx = (struct veth_lpar_connection *) ptr;
1555 unsigned long flags;
1557 /* Ack all the events */
1558 spin_lock_irqsave(&cnx->lock, flags);
1559 if (cnx->num_pending_acks > 0)
1560 veth_flush_acks(cnx);
1562 /* Reschedule the timer */
1563 cnx->ack_timer.expires = jiffies + cnx->ack_timeout;
1564 add_timer(&cnx->ack_timer);
1565 spin_unlock_irqrestore(&cnx->lock, flags);
1568 static int veth_remove(struct vio_dev *vdev)
1570 struct veth_lpar_connection *cnx;
1571 struct net_device *dev;
1572 struct veth_port *port;
1573 int i;
1575 dev = veth_dev[vdev->unit_address];
1577 if (! dev)
1578 return 0;
1580 port = netdev_priv(dev);
1582 for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
1583 cnx = veth_cnx[i];
1585 if (cnx && (port->lpar_map & (1 << i))) {
1586 /* Drop our reference to connections on our VLAN */
1587 kobject_put(&cnx->kobject);
1591 veth_dev[vdev->unit_address] = NULL;
1592 kobject_del(&port->kobject);
1593 kobject_put(&port->kobject);
1594 unregister_netdev(dev);
1595 free_netdev(dev);
1597 return 0;
1600 static int veth_probe(struct vio_dev *vdev, const struct vio_device_id *id)
1602 int i = vdev->unit_address;
1603 struct net_device *dev;
1604 struct veth_port *port;
1606 dev = veth_probe_one(i, &vdev->dev);
1607 if (dev == NULL) {
1608 veth_remove(vdev);
1609 return 1;
1611 veth_dev[i] = dev;
1613 port = (struct veth_port*)netdev_priv(dev);
1615 /* Start the state machine on each connection on this vlan. If we're
1616 * the first dev to do so this will commence link negotiation */
1617 for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
1618 struct veth_lpar_connection *cnx;
1620 if (! (port->lpar_map & (1 << i)))
1621 continue;
1623 cnx = veth_cnx[i];
1624 if (!cnx)
1625 continue;
1627 kobject_get(&cnx->kobject);
1628 veth_kick_statemachine(cnx);
1631 return 0;
1635 * veth_device_table: Used by vio.c to match devices that we
1636 * support.
1638 static struct vio_device_id veth_device_table[] __devinitdata = {
1639 { "vlan", "" },
1640 { "", "" }
1642 MODULE_DEVICE_TABLE(vio, veth_device_table);
1644 static struct vio_driver veth_driver = {
1645 .name = "iseries_veth",
1646 .id_table = veth_device_table,
1647 .probe = veth_probe,
1648 .remove = veth_remove
1652 * Module initialization/cleanup
1655 void __exit veth_module_cleanup(void)
1657 int i;
1658 struct veth_lpar_connection *cnx;
1660 /* Disconnect our "irq" to stop events coming from the Hypervisor. */
1661 HvLpEvent_unregisterHandler(HvLpEvent_Type_VirtualLan);
1663 /* Make sure any work queued from Hypervisor callbacks is finished. */
1664 flush_scheduled_work();
1666 for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
1667 cnx = veth_cnx[i];
1669 if (!cnx)
1670 continue;
1672 /* Remove the connection from sysfs */
1673 kobject_del(&cnx->kobject);
1674 /* Drop the driver's reference to the connection */
1675 kobject_put(&cnx->kobject);
1678 /* Unregister the driver, which will close all the netdevs and stop
1679 * the connections when they're no longer referenced. */
1680 vio_unregister_driver(&veth_driver);
1682 module_exit(veth_module_cleanup);
1684 int __init veth_module_init(void)
1686 int i;
1687 int rc;
1689 this_lp = HvLpConfig_getLpIndex_outline();
1691 for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
1692 rc = veth_init_connection(i);
1693 if (rc != 0)
1694 goto error;
1697 HvLpEvent_registerHandler(HvLpEvent_Type_VirtualLan,
1698 &veth_handle_event);
1700 rc = vio_register_driver(&veth_driver);
1701 if (rc != 0)
1702 goto error;
1704 for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
1705 struct kobject *kobj;
1707 if (!veth_cnx[i])
1708 continue;
1710 kobj = &veth_cnx[i]->kobject;
1711 kobj->parent = &veth_driver.driver.kobj;
1712 /* If the add failes, complain but otherwise continue */
1713 if (0 != kobject_add(kobj))
1714 veth_error("cnx %d: Failed adding to sysfs.\n", i);
1717 return 0;
1719 error:
1720 for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
1721 veth_destroy_connection(veth_cnx[i]);
1724 return rc;
1726 module_init(veth_module_init);