blob: 2e54c85bc099a2634772ed980b49df9755ec3b52 [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 -070084MODULE_AUTHOR("Kyle Lucke <klucke@us.ibm.com>");
85MODULE_DESCRIPTION("iSeries Virtual ethernet driver");
86MODULE_LICENSE("GPL");
87
Michael Ellerman642d1a42005-09-01 11:29:25 +100088#define VethEventTypeCap (0)
89#define VethEventTypeFrames (1)
90#define VethEventTypeMonitor (2)
91#define VethEventTypeFramesAck (3)
92
93#define VETH_MAX_ACKS_PER_MSG (20)
94#define VETH_MAX_FRAMES_PER_MSG (6)
95
96struct VethFramesData {
97 u32 addr[VETH_MAX_FRAMES_PER_MSG];
98 u16 len[VETH_MAX_FRAMES_PER_MSG];
99 u32 eofmask;
100};
101#define VETH_EOF_SHIFT (32-VETH_MAX_FRAMES_PER_MSG)
102
103struct VethFramesAckData {
104 u16 token[VETH_MAX_ACKS_PER_MSG];
105};
106
107struct 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];
116};
117
118struct 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;
125
126};
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#define VETH_NUMBUFFERS (120)
129#define VETH_ACKTIMEOUT (1000000) /* microseconds */
130#define VETH_MAX_MCAST (12)
131
132#define VETH_MAX_MTU (9000)
133
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
143
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)
153
154struct veth_msg {
155 struct veth_msg *next;
156 struct VethFramesData data;
157 int token;
Michael Ellermanb08bd5c2005-09-01 11:29:06 +1000158 int in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 struct sk_buff *skb;
160 struct device *dev;
161};
162
163struct 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;
169
Michael Ellermanf0c129c2005-09-01 11:29:09 +1000170 struct kobject kobject;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 struct timer_list ack_timer;
172
Michael Ellerman24562ff2005-09-01 11:29:17 +1000173 struct timer_list reset_timer;
174 unsigned int reset_timeout;
175 unsigned long last_contact;
176 int outstanding_tx;
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 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;
185
186 int num_ack_events;
187 struct VethCapData remote_caps;
188 u32 ack_timeout;
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 struct veth_msg *msg_stack_head;
191};
192
193struct veth_port {
194 struct device *dev;
195 struct net_device_stats stats;
196 u64 mac_addr;
197 HvLpIndexMap lpar_map;
198
Michael Ellermane0808492005-09-01 11:29:18 +1000199 /* queue_lock protects the stopped_map and dev's queue. */
200 spinlock_t queue_lock;
201 HvLpIndexMap stopped_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Michael Ellermane0808492005-09-01 11:29:18 +1000203 /* mcast_gate protects promiscuous, num_mcast & mcast_addr. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 rwlock_t mcast_gate;
205 int promiscuous;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 int num_mcast;
207 u64 mcast_addr[VETH_MAX_MCAST];
Michael Ellerman07a5c172005-09-01 11:29:21 +1000208
209 struct kobject kobject;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210};
211
212static HvLpIndex this_lp;
213static struct veth_lpar_connection *veth_cnx[HVMAXARCHITECTEDLPS]; /* = 0 */
214static struct net_device *veth_dev[HVMAXARCHITECTEDVIRTUALLANS]; /* = 0 */
215
216static int veth_start_xmit(struct sk_buff *skb, struct net_device *dev);
217static void veth_recycle_msg(struct veth_lpar_connection *, struct veth_msg *);
Michael Ellermane0808492005-09-01 11:29:18 +1000218static void veth_wake_queues(struct veth_lpar_connection *cnx);
219static void veth_stop_queues(struct veth_lpar_connection *cnx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220static void veth_receive(struct veth_lpar_connection *, struct VethLpEvent *);
Michael Ellermanf0c129c2005-09-01 11:29:09 +1000221static void veth_release_connection(struct kobject *kobject);
Michael Ellerman24562ff2005-09-01 11:29:17 +1000222static void veth_timed_ack(unsigned long ptr);
223static void veth_timed_reset(unsigned long ptr);
Michael Ellermanf0c129c2005-09-01 11:29:09 +1000224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225/*
226 * Utility functions
227 */
228
Michael Ellerman61a3c692005-09-01 11:28:57 +1000229#define veth_info(fmt, args...) \
230 printk(KERN_INFO "iseries_veth: " fmt, ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232#define veth_error(fmt, args...) \
Michael Ellerman61a3c692005-09-01 11:28:57 +1000233 printk(KERN_ERR "iseries_veth: Error: " fmt, ## args)
234
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Michael Ellermand7893dd2005-09-01 11:29:05 +1000242/* You must hold the connection's lock when you call this function. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243static inline void veth_stack_push(struct veth_lpar_connection *cnx,
244 struct veth_msg *msg)
245{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 msg->next = cnx->msg_stack_head;
247 cnx->msg_stack_head = msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
Michael Ellermand7893dd2005-09-01 11:29:05 +1000250/* You must hold the connection's lock when you call this function. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251static inline struct veth_msg *veth_stack_pop(struct veth_lpar_connection *cnx)
252{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 struct veth_msg *msg;
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 msg = cnx->msg_stack_head;
256 if (msg)
257 cnx->msg_stack_head = cnx->msg_stack_head->next;
Michael Ellermand7893dd2005-09-01 11:29:05 +1000258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return msg;
260}
261
Michael Ellermane0808492005-09-01 11:29:18 +1000262/* You must hold the connection's lock when you call this function. */
263static inline int veth_stack_is_empty(struct veth_lpar_connection *cnx)
264{
265 return cnx->msg_stack_head == NULL;
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268static inline HvLpEvent_Rc
269veth_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)
273{
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);
281}
282
283static inline HvLpEvent_Rc veth_signaldata(struct veth_lpar_connection *cnx,
284 u16 subtype, u64 token, void *data)
285{
286 u64 *p = (u64 *) data;
287
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]);
291}
292
293struct veth_allocation {
294 struct completion c;
295 int num;
296};
297
298static void veth_complete_allocation(void *parm, int number)
299{
300 struct veth_allocation *vc = (struct veth_allocation *)parm;
301
302 vc->num = number;
303 complete(&vc->c);
304}
305
306static int veth_allocate_events(HvLpIndex rlp, int number)
307{
308 struct veth_allocation vc = { COMPLETION_INITIALIZER(vc.c), 0 };
309
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);
314
315 return vc.num;
316}
317
318/*
Michael Ellerman76812d8122005-09-01 11:29:20 +1000319 * sysfs support
320 */
321
322struct 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);
326};
327
328static ssize_t veth_cnx_attribute_show(struct kobject *kobj,
329 struct attribute *attr, char *buf)
330{
331 struct veth_cnx_attribute *cnx_attr;
332 struct veth_lpar_connection *cnx;
333
334 cnx_attr = container_of(attr, struct veth_cnx_attribute, attr);
335 cnx = container_of(kobj, struct veth_lpar_connection, kobject);
336
337 if (!cnx_attr->show)
338 return -EIO;
339
340 return cnx_attr->show(cnx, buf);
341}
342
343#define CUSTOM_CNX_ATTR(_name, _format, _expression) \
344static ssize_t _name##_show(struct veth_lpar_connection *cnx, char *buf)\
345{ \
346 return sprintf(buf, _format, _expression); \
347} \
348struct veth_cnx_attribute veth_cnx_attr_##_name = __ATTR_RO(_name)
349
350#define SIMPLE_CNX_ATTR(_name) \
351 CUSTOM_CNX_ATTR(_name, "%lu\n", (unsigned long)cnx->_name)
352
353SIMPLE_CNX_ATTR(outstanding_tx);
354SIMPLE_CNX_ATTR(remote_lp);
355SIMPLE_CNX_ATTR(num_events);
356SIMPLE_CNX_ATTR(src_inst);
357SIMPLE_CNX_ATTR(dst_inst);
358SIMPLE_CNX_ATTR(num_pending_acks);
359SIMPLE_CNX_ATTR(num_ack_events);
360CUSTOM_CNX_ATTR(ack_timeout, "%d\n", jiffies_to_msecs(cnx->ack_timeout));
361CUSTOM_CNX_ATTR(reset_timeout, "%d\n", jiffies_to_msecs(cnx->reset_timeout));
362CUSTOM_CNX_ATTR(state, "0x%.4lX\n", cnx->state);
363CUSTOM_CNX_ATTR(last_contact, "%d\n", cnx->last_contact ?
364 jiffies_to_msecs(jiffies - cnx->last_contact) : 0);
365
366#define GET_CNX_ATTR(_name) (&veth_cnx_attr_##_name.attr)
367
368static 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
381};
382
383static struct sysfs_ops veth_cnx_sysfs_ops = {
384 .show = veth_cnx_attribute_show
385};
386
387static 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
391};
392
Michael Ellerman07a5c172005-09-01 11:29:21 +1000393struct 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);
397};
398
399static ssize_t veth_port_attribute_show(struct kobject *kobj,
400 struct attribute *attr, char *buf)
401{
402 struct veth_port_attribute *port_attr;
403 struct veth_port *port;
404
405 port_attr = container_of(attr, struct veth_port_attribute, attr);
406 port = container_of(kobj, struct veth_port, kobject);
407
408 if (!port_attr->show)
409 return -EIO;
410
411 return port_attr->show(port, buf);
412}
413
414#define CUSTOM_PORT_ATTR(_name, _format, _expression) \
415static ssize_t _name##_show(struct veth_port *port, char *buf) \
416{ \
417 return sprintf(buf, _format, _expression); \
418} \
419struct veth_port_attribute veth_port_attr_##_name = __ATTR_RO(_name)
420
421#define SIMPLE_PORT_ATTR(_name) \
422 CUSTOM_PORT_ATTR(_name, "%lu\n", (unsigned long)port->_name)
423
424SIMPLE_PORT_ATTR(promiscuous);
425SIMPLE_PORT_ATTR(num_mcast);
426CUSTOM_PORT_ATTR(lpar_map, "0x%X\n", port->lpar_map);
427CUSTOM_PORT_ATTR(stopped_map, "0x%X\n", port->stopped_map);
428CUSTOM_PORT_ATTR(mac_addr, "0x%lX\n", port->mac_addr);
429
430#define GET_PORT_ATTR(_name) (&veth_port_attr_##_name.attr)
431static 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
438};
439
440static struct sysfs_ops veth_port_sysfs_ops = {
441 .show = veth_port_attribute_show
442};
443
444static struct kobj_type veth_port_ktype = {
445 .sysfs_ops = &veth_port_sysfs_ops,
446 .default_attrs = veth_port_default_attrs
447};
448
Michael Ellerman76812d8122005-09-01 11:29:20 +1000449/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 * LPAR connection code
451 */
452
453static inline void veth_kick_statemachine(struct veth_lpar_connection *cnx)
454{
455 schedule_work(&cnx->statemachine_wq);
456}
457
458static void veth_take_cap(struct veth_lpar_connection *cnx,
459 struct VethLpEvent *event)
460{
461 unsigned long flags;
462
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);
469
470 if (cnx->state & VETH_STATE_GOTCAPS) {
Michael Ellerman61a3c692005-09-01 11:28:57 +1000471 veth_error("Received a second capabilities from LPAR %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 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);
479 }
480 spin_unlock_irqrestore(&cnx->lock, flags);
481}
482
483static void veth_take_cap_ack(struct veth_lpar_connection *cnx,
484 struct VethLpEvent *event)
485{
486 unsigned long flags;
487
488 spin_lock_irqsave(&cnx->lock, flags);
489 if (cnx->state & VETH_STATE_GOTCAPACK) {
Michael Ellerman61a3c692005-09-01 11:28:57 +1000490 veth_error("Received a second capabilities ack from LPAR %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 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);
497 }
498 spin_unlock_irqrestore(&cnx->lock, flags);
499}
500
501static void veth_take_monitor_ack(struct veth_lpar_connection *cnx,
502 struct VethLpEvent *event)
503{
504 unsigned long flags;
505
506 spin_lock_irqsave(&cnx->lock, flags);
Michael Ellerman61a3c692005-09-01 11:28:57 +1000507 veth_debug("cnx %d: lost connection.\n", cnx->remote_lp);
Michael Ellerman58c59002005-09-01 11:29:00 +1000508
509 /* Avoid kicking the statemachine once we're shutdown.
510 * It's unnecessary and it could break veth_stop_connection(). */
511
512 if (! (cnx->state & VETH_STATE_SHUTDOWN)) {
513 cnx->state |= VETH_STATE_RESET;
514 veth_kick_statemachine(cnx);
515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 spin_unlock_irqrestore(&cnx->lock, flags);
517}
518
519static void veth_handle_ack(struct VethLpEvent *event)
520{
521 HvLpIndex rlp = event->base_event.xTargetLp;
522 struct veth_lpar_connection *cnx = veth_cnx[rlp];
523
524 BUG_ON(! cnx);
525
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:
Michael Ellerman61a3c692005-09-01 11:28:57 +1000534 veth_error("Unknown ack type %d from LPAR %d.\n",
535 event->base_event.xSubtype, rlp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 };
537}
538
539static void veth_handle_int(struct VethLpEvent *event)
540{
541 HvLpIndex rlp = event->base_event.xSourceLp;
542 struct veth_lpar_connection *cnx = veth_cnx[rlp];
543 unsigned long flags;
Michael Ellerman24562ff2005-09-01 11:29:17 +1000544 int i, acked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 BUG_ON(! cnx);
547
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);
Michael Ellerman24562ff2005-09-01 11:29:17 +1000558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 for (i = 0; i < VETH_MAX_ACKS_PER_MSG; ++i) {
560 u16 msgnum = event->u.frames_ack_data.token[i];
561
Michael Ellerman24562ff2005-09-01 11:29:17 +1000562 if (msgnum < VETH_NUMBUFFERS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 veth_recycle_msg(cnx, cnx->msgs + msgnum);
Michael Ellerman24562ff2005-09-01 11:29:17 +1000564 cnx->outstanding_tx--;
565 acked++;
566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
Michael Ellerman24562ff2005-09-01 11:29:17 +1000568
Michael Ellermane0808492005-09-01 11:29:18 +1000569 if (acked > 0) {
Michael Ellerman24562ff2005-09-01 11:29:17 +1000570 cnx->last_contact = jiffies;
Michael Ellermane0808492005-09-01 11:29:18 +1000571 veth_wake_queues(cnx);
572 }
Michael Ellerman24562ff2005-09-01 11:29:17 +1000573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 spin_unlock_irqrestore(&cnx->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 break;
576 case VethEventTypeFrames:
577 veth_receive(cnx, event);
578 break;
579 default:
Michael Ellerman61a3c692005-09-01 11:28:57 +1000580 veth_error("Unknown interrupt type %d from LPAR %d.\n",
581 event->base_event.xSubtype, rlp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 };
583}
584
585static void veth_handle_event(struct HvLpEvent *event, struct pt_regs *regs)
586{
587 struct VethLpEvent *veth_event = (struct VethLpEvent *)event;
588
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);
593}
594
595static int veth_process_caps(struct veth_lpar_connection *cnx)
596{
597 struct VethCapData *remote_caps = &cnx->remote_caps;
598 int num_acks_needed;
599
600 /* Convert timer to jiffies */
601 cnx->ack_timeout = remote_caps->ack_timeout * HZ / 1000000;
602
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) ) {
Michael Ellerman61a3c692005-09-01 11:28:57 +1000607 veth_error("Received incompatible capabilities from LPAR %d.\n",
608 cnx->remote_lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return HvLpEvent_Rc_InvalidSubtypeData;
610 }
611
612 num_acks_needed = (remote_caps->num_buffers
613 / remote_caps->ack_threshold) + 1;
614
615 /* FIXME: locking on num_ack_events? */
616 if (cnx->num_ack_events < num_acks_needed) {
617 int num;
618
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;
623
624 if (cnx->num_ack_events < num_acks_needed) {
Michael Ellerman61a3c692005-09-01 11:28:57 +1000625 veth_error("Couldn't allocate enough ack events "
626 "for LPAR %d.\n", cnx->remote_lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 return HvLpEvent_Rc_BufferNotAvailable;
629 }
630 }
631
632
633 return HvLpEvent_Rc_Good;
634}
635
636/* FIXME: The gotos here are a bit dubious */
637static void veth_statemachine(void *p)
638{
639 struct veth_lpar_connection *cnx = (struct veth_lpar_connection *)p;
640 int rlp = cnx->remote_lp;
641 int rc;
642
643 spin_lock_irq(&cnx->lock);
644
645 restart:
646 if (cnx->state & VETH_STATE_RESET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (cnx->state & VETH_STATE_OPEN)
648 HvCallEvent_closeLpEventPath(cnx->remote_lp,
649 HvLpEvent_Type_VirtualLan);
650
Michael Ellermanabfda472005-09-01 11:28:59 +1000651 /*
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.
655 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 memset(&cnx->pending_acks, 0xff, sizeof (cnx->pending_acks));
657 cnx->num_pending_acks = 0;
658
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);
663
664 /* Clean up any leftover messages */
Michael Ellerman24562ff2005-09-01 11:29:17 +1000665 if (cnx->msgs) {
666 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 for (i = 0; i < VETH_NUMBUFFERS; ++i)
668 veth_recycle_msg(cnx, cnx->msgs + i);
Michael Ellerman24562ff2005-09-01 11:29:17 +1000669 }
Michael Ellermane0808492005-09-01 11:29:18 +1000670
Michael Ellerman24562ff2005-09-01 11:29:17 +1000671 cnx->outstanding_tx = 0;
Michael Ellermane0808492005-09-01 11:29:18 +1000672 veth_wake_queues(cnx);
Michael Ellermanabfda472005-09-01 11:28:59 +1000673
674 /* Drop the lock so we can do stuff that might sleep or
675 * take other locks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 spin_unlock_irq(&cnx->lock);
Michael Ellermanabfda472005-09-01 11:28:59 +1000677
678 del_timer_sync(&cnx->ack_timer);
Michael Ellerman24562ff2005-09-01 11:29:17 +1000679 del_timer_sync(&cnx->reset_timer);
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 spin_lock_irq(&cnx->lock);
Michael Ellermanabfda472005-09-01 11:28:59 +1000682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (cnx->state & VETH_STATE_RESET)
684 goto restart;
Michael Ellerman58c59002005-09-01 11:29:00 +1000685
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;
690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692
693 if (cnx->state & VETH_STATE_SHUTDOWN)
694 /* It's all over, do nothing */
695 goto out;
696
697 if ( !(cnx->state & VETH_STATE_OPEN) ) {
698 if (! cnx->msgs || (cnx->num_events < (2 + VETH_NUMBUFFERS)) )
699 goto cant_cope;
700
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;
709 }
710
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);
717
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) )
Michael Ellerman61a3c692005-09-01 11:28:57 +1000723 veth_error("Error sending monitor to LPAR %d, "
724 "rc = %d\n", rlp, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 /* Oh well, hope we get a cap from the other
727 * end and do better when that kicks us */
728 goto out;
729 }
730 }
731
732 if ( (cnx->state & VETH_STATE_OPEN)
733 && !(cnx->state & VETH_STATE_SENTCAPS)) {
734 u64 *rawcap = (u64 *)&cnx->local_caps;
735
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]);
741
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) )
Michael Ellerman61a3c692005-09-01 11:28:57 +1000747 veth_error("Error sending caps to LPAR %d, "
748 "rc = %d\n", rlp, rc);
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 /* Oh well, hope we get a cap from the other
751 * end and do better when that kicks us */
752 goto out;
753 }
754 }
755
756 if ((cnx->state & VETH_STATE_GOTCAPS)
757 && !(cnx->state & VETH_STATE_SENTCAPACK)) {
758 struct VethCapData *remote_caps = &cnx->remote_caps;
759
760 memcpy(remote_caps, &cnx->cap_event.u.caps_data,
761 sizeof(*remote_caps));
762
763 spin_unlock_irq(&cnx->lock);
764 rc = veth_process_caps(cnx);
765 spin_lock_irq(&cnx->lock);
766
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;
771
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;
778 }
779
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 {
Michael Ellerman61a3c692005-09-01 11:28:57 +1000789 veth_error("Caps rejected by LPAR %d, rc = %d\n",
790 rlp, cnx->cap_ack_event.base_event.xRc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 goto cant_cope;
792 }
793 }
794
795 out:
796 spin_unlock_irq(&cnx->lock);
797 return;
798
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 */
Michael Ellerman61a3c692005-09-01 11:28:57 +1000803 veth_error("Unrecoverable error on connection to LPAR %d, shutting down"
804 " (state = 0x%04lx)\n", rlp, cnx->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 cnx->state |= VETH_STATE_SHUTDOWN;
806 spin_unlock_irq(&cnx->lock);
807}
808
809static int veth_init_connection(u8 rlp)
810{
811 struct veth_lpar_connection *cnx;
812 struct veth_msg *msgs;
Michael Ellermanf0c129c2005-09-01 11:29:09 +1000813 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 if ( (rlp == this_lp)
816 || ! HvLpConfig_doLpsCommunicateOnVirtualLan(this_lp, rlp) )
817 return 0;
818
819 cnx = kmalloc(sizeof(*cnx), GFP_KERNEL);
820 if (! cnx)
821 return -ENOMEM;
822 memset(cnx, 0, sizeof(*cnx));
823
824 cnx->remote_lp = rlp;
825 spin_lock_init(&cnx->lock);
826 INIT_WORK(&cnx->statemachine_wq, veth_statemachine, cnx);
Michael Ellerman24562ff2005-09-01 11:29:17 +1000827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 init_timer(&cnx->ack_timer);
829 cnx->ack_timer.function = veth_timed_ack;
830 cnx->ack_timer.data = (unsigned long) cnx;
Michael Ellerman24562ff2005-09-01 11:29:17 +1000831
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);
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 memset(&cnx->pending_acks, 0xff, sizeof (cnx->pending_acks));
838
839 veth_cnx[rlp] = cnx;
840
Michael Ellermanf0c129c2005-09-01 11:29:09 +1000841 /* 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;
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 msgs = kmalloc(VETH_NUMBUFFERS * sizeof(struct veth_msg), GFP_KERNEL);
850 if (! msgs) {
Michael Ellerman61a3c692005-09-01 11:28:57 +1000851 veth_error("Can't allocate buffers for LPAR %d.\n", rlp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return -ENOMEM;
853 }
854
855 cnx->msgs = msgs;
856 memset(msgs, 0, VETH_NUMBUFFERS * sizeof(struct veth_msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 for (i = 0; i < VETH_NUMBUFFERS; i++) {
859 msgs[i].token = i;
860 veth_stack_push(cnx, msgs + i);
861 }
862
863 cnx->num_events = veth_allocate_events(rlp, 2 + VETH_NUMBUFFERS);
864
865 if (cnx->num_events < (2 + VETH_NUMBUFFERS)) {
Michael Ellerman61a3c692005-09-01 11:28:57 +1000866 veth_error("Can't allocate enough events for LPAR %d.\n", rlp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return -ENOMEM;
868 }
869
870 cnx->local_caps.num_buffers = VETH_NUMBUFFERS;
871 cnx->local_caps.ack_threshold = ACK_THRESHOLD;
872 cnx->local_caps.ack_timeout = VETH_ACKTIMEOUT;
873
874 return 0;
875}
876
Michael Ellermanf0c129c2005-09-01 11:29:09 +1000877static void veth_stop_connection(struct veth_lpar_connection *cnx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Michael Ellermanf0c129c2005-09-01 11:29:09 +1000879 if (!cnx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return;
881
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);
886
Michael Ellerman58c59002005-09-01 11:29:00 +1000887 /* 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);
894 }
895
Michael Ellermanabfda472005-09-01 11:28:59 +1000896 /* Wait for the state machine to run. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 flush_scheduled_work();
Michael Ellermanec60bee2005-09-01 11:29:08 +1000898}
899
Michael Ellermanf0c129c2005-09-01 11:29:09 +1000900static void veth_destroy_connection(struct veth_lpar_connection *cnx)
Michael Ellermanec60bee2005-09-01 11:29:08 +1000901{
Michael Ellermanf0c129c2005-09-01 11:29:09 +1000902 if (!cnx)
Michael Ellermanec60bee2005-09-01 11:29:08 +1000903 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 kfree(cnx->msgs);
Michael Ellermanf0c129c2005-09-01 11:29:09 +1000917 veth_cnx[cnx->remote_lp] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 kfree(cnx);
Michael Ellermanf0c129c2005-09-01 11:29:09 +1000919}
920
921static void veth_release_connection(struct kobject *kobj)
922{
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
929/*
930 * net_device code
931 */
932
933static int veth_open(struct net_device *dev)
934{
935 struct veth_port *port = (struct veth_port *) dev->priv;
936
937 memset(&port->stats, 0, sizeof (port->stats));
938 netif_start_queue(dev);
939 return 0;
940}
941
942static int veth_close(struct net_device *dev)
943{
944 netif_stop_queue(dev);
945 return 0;
946}
947
948static struct net_device_stats *veth_get_stats(struct net_device *dev)
949{
950 struct veth_port *port = (struct veth_port *) dev->priv;
951
952 return &port->stats;
953}
954
955static int veth_change_mtu(struct net_device *dev, int new_mtu)
956{
957 if ((new_mtu < 68) || (new_mtu > VETH_MAX_MTU))
958 return -EINVAL;
959 dev->mtu = new_mtu;
960 return 0;
961}
962
963static void veth_set_multicast_list(struct net_device *dev)
964{
965 struct veth_port *port = (struct veth_port *) dev->priv;
966 unsigned long flags;
967
968 write_lock_irqsave(&port->mcast_gate, flags);
969
Michael Ellerman2a5391a2005-09-01 11:29:02 +1000970 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
971 (dev->mc_count > VETH_MAX_MCAST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 port->promiscuous = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 } else {
974 struct dev_mc_list *dmi = dev->mc_list;
975 int i;
976
Michael Ellerman2a5391a2005-09-01 11:29:02 +1000977 port->promiscuous = 0;
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /* Update table */
980 port->num_mcast = 0;
981
982 for (i = 0; i < dev->mc_count; i++) {
983 u8 *addr = dmi->dmi_addr;
984 u64 xaddr = 0;
985
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++;
990 }
991 dmi = dmi->next;
992 }
993 }
994
995 write_unlock_irqrestore(&port->mcast_gate, flags);
996}
997
998static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
999{
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);
1003}
1004
1005static int veth_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1006{
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;
1020}
1021
1022static u32 veth_get_link(struct net_device *dev)
1023{
1024 return 1;
1025}
1026
1027static struct ethtool_ops ops = {
1028 .get_drvinfo = veth_get_drvinfo,
1029 .get_settings = veth_get_settings,
1030 .get_link = veth_get_link,
1031};
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033static struct net_device * __init veth_probe_one(int vlan, struct device *vdev)
1034{
1035 struct net_device *dev;
1036 struct veth_port *port;
1037 int i, rc;
1038
1039 dev = alloc_etherdev(sizeof (struct veth_port));
1040 if (! dev) {
1041 veth_error("Unable to allocate net_device structure!\n");
1042 return NULL;
1043 }
1044
1045 port = (struct veth_port *) dev->priv;
1046
Michael Ellermane0808492005-09-01 11:29:18 +10001047 spin_lock_init(&port->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 rwlock_init(&port->mcast_gate);
Michael Ellermane0808492005-09-01 11:29:18 +10001049 port->stopped_map = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
1052 HvLpVirtualLanIndexMap map;
1053
1054 if (i == this_lp)
1055 continue;
1056 map = HvLpConfig_getVirtualLanIndexMapForLp(i);
1057 if (map & (0x8000 >> vlan))
1058 port->lpar_map |= (1 << i);
1059 }
1060 port->dev = vdev;
1061
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;
1068
1069 dev->mtu = VETH_MAX_MTU;
1070
1071 memcpy(&port->mac_addr, dev->dev_addr, 6);
1072
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);
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 SET_NETDEV_DEV(dev, vdev);
1083
1084 rc = register_netdev(dev);
1085 if (rc != 0) {
Michael Ellerman61a3c692005-09-01 11:28:57 +10001086 veth_error("Failed registering net device for vlan%d.\n", vlan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 free_netdev(dev);
1088 return NULL;
1089 }
1090
Michael Ellerman07a5c172005-09-01 11:29:21 +10001091 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);
1097
Michael Ellerman61a3c692005-09-01 11:28:57 +10001098 veth_info("%s attached to iSeries vlan %d (LPAR map = 0x%.4X)\n",
1099 dev->name, vlan, port->lpar_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 return dev;
1102}
1103
1104/*
1105 * Tx path
1106 */
1107
1108static int veth_transmit_to_one(struct sk_buff *skb, HvLpIndex rlp,
1109 struct net_device *dev)
1110{
1111 struct veth_lpar_connection *cnx = veth_cnx[rlp];
1112 struct veth_port *port = (struct veth_port *) dev->priv;
1113 HvLpEvent_Rc rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 struct veth_msg *msg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 unsigned long flags;
1116
Michael Ellermandb5e8712005-09-01 11:29:19 +10001117 if (! cnx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 spin_lock_irqsave(&cnx->lock, flags);
1121
Michael Ellermanf27eff12005-05-12 17:47:27 +10001122 if (! (cnx->state & VETH_STATE_READY))
Michael Ellermandb5e8712005-09-01 11:29:19 +10001123 goto no_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Michael Ellermandb5e8712005-09-01 11:29:19 +10001125 if ((skb->len - ETH_HLEN) > VETH_MAX_MTU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 goto drop;
1127
1128 msg = veth_stack_pop(cnx);
Michael Ellermandb5e8712005-09-01 11:29:19 +10001129 if (! msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Michael Ellermanb08bd5c2005-09-01 11:29:06 +10001132 msg->in_use = 1;
Michael Ellermandb5e8712005-09-01 11:29:19 +10001133 msg->skb = skb_get(skb);
Michael Ellermanb08bd5c2005-09-01 11:29:06 +10001134
Michael Ellermancbf90742005-09-01 11:29:07 +10001135 msg->data.addr[0] = dma_map_single(port->dev, skb->data,
1136 skb->len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Michael Ellermancbf90742005-09-01 11:29:07 +10001138 if (dma_mapping_error(msg->data.addr[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 goto recycle_and_drop;
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 msg->dev = port->dev;
Michael Ellermancbf90742005-09-01 11:29:07 +10001142 msg->data.len[0] = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 msg->data.eofmask = 1 << VETH_EOF_SHIFT;
Michael Ellermancbf90742005-09-01 11:29:07 +10001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 rc = veth_signaldata(cnx, VethEventTypeFrames, msg->token, &msg->data);
1146
1147 if (rc != HvLpEvent_Rc_Good)
1148 goto recycle_and_drop;
1149
Michael Ellerman24562ff2005-09-01 11:29:17 +10001150 /* 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);
1153
1154 cnx->last_contact = jiffies;
1155 cnx->outstanding_tx++;
1156
Michael Ellermane0808492005-09-01 11:29:18 +10001157 if (veth_stack_is_empty(cnx))
1158 veth_stop_queues(cnx);
1159
Michael Ellermandb5e8712005-09-01 11:29:19 +10001160 no_error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 spin_unlock_irqrestore(&cnx->lock, flags);
1162 return 0;
1163
1164 recycle_and_drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 veth_recycle_msg(cnx, msg);
1166 drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 spin_unlock_irqrestore(&cnx->lock, flags);
Michael Ellermandb5e8712005-09-01 11:29:19 +10001168 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
Michael Ellermandb5e8712005-09-01 11:29:19 +10001171static void veth_transmit_to_many(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 HvLpIndexMap lpmask,
1173 struct net_device *dev)
1174{
1175 struct veth_port *port = (struct veth_port *) dev->priv;
Michael Ellermandb5e8712005-09-01 11:29:19 +10001176 int i, success, error;
1177
1178 success = error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
1181 if ((lpmask & (1 << i)) == 0)
1182 continue;
1183
Michael Ellermandb5e8712005-09-01 11:29:19 +10001184 if (veth_transmit_to_one(skb, i, dev))
1185 error = 1;
1186 else
1187 success = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189
Michael Ellermandb5e8712005-09-01 11:29:19 +10001190 if (error)
1191 port->stats.tx_errors++;
1192
1193 if (success) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 port->stats.tx_packets++;
1195 port->stats.tx_bytes += skb->len;
1196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
1199static int veth_start_xmit(struct sk_buff *skb, struct net_device *dev)
1200{
1201 unsigned char *frame = skb->data;
1202 struct veth_port *port = (struct veth_port *) dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 HvLpIndexMap lpmask;
1204
1205 if (! (frame[0] & 0x01)) {
1206 /* unicast packet */
1207 HvLpIndex rlp = frame[5];
1208
1209 if ( ! ((1 << rlp) & port->lpar_map) ) {
1210 dev_kfree_skb(skb);
1211 return 0;
1212 }
1213
1214 lpmask = 1 << rlp;
1215 } else {
1216 lpmask = port->lpar_map;
1217 }
1218
Michael Ellermane0808492005-09-01 11:29:18 +10001219 veth_transmit_to_many(skb, lpmask, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Michael Ellermane0808492005-09-01 11:29:18 +10001221 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 return 0;
1224}
1225
Michael Ellermanb08bd5c2005-09-01 11:29:06 +10001226/* You must hold the connection's lock when you call this function. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227static void veth_recycle_msg(struct veth_lpar_connection *cnx,
1228 struct veth_msg *msg)
1229{
1230 u32 dma_address, dma_length;
1231
Michael Ellermanb08bd5c2005-09-01 11:29:06 +10001232 if (msg->in_use) {
1233 msg->in_use = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 dma_address = msg->data.addr[0];
1235 dma_length = msg->data.len[0];
1236
Michael Ellermancbf90742005-09-01 11:29:07 +10001237 if (!dma_mapping_error(dma_address))
1238 dma_unmap_single(msg->dev, dma_address, dma_length,
1239 DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 if (msg->skb) {
1242 dev_kfree_skb_any(msg->skb);
1243 msg->skb = NULL;
1244 }
1245
1246 memset(&msg->data, 0, sizeof(msg->data));
1247 veth_stack_push(cnx, msg);
Michael Ellerman61a3c692005-09-01 11:28:57 +10001248 } 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);
1251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}
1253
Michael Ellermane0808492005-09-01 11:29:18 +10001254static void veth_wake_queues(struct veth_lpar_connection *cnx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
1256 int i;
Michael Ellermane0808492005-09-01 11:29:18 +10001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++) {
1259 struct net_device *dev = veth_dev[i];
1260 struct veth_port *port;
1261 unsigned long flags;
1262
1263 if (! dev)
1264 continue;
1265
1266 port = (struct veth_port *)dev->priv;
1267
1268 if (! (port->lpar_map & (1<<cnx->remote_lp)))
1269 continue;
1270
Michael Ellermane0808492005-09-01 11:29:18 +10001271 spin_lock_irqsave(&port->queue_lock, flags);
1272
1273 port->stopped_map &= ~(1 << cnx->remote_lp);
1274
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
Michael Ellermane0808492005-09-01 11:29:18 +10001280 spin_unlock_irqrestore(&port->queue_lock, flags);
1281 }
1282}
1283
1284static void veth_stop_queues(struct veth_lpar_connection *cnx)
1285{
1286 int i;
1287
1288 for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++) {
1289 struct net_device *dev = veth_dev[i];
1290 struct veth_port *port;
1291
1292 if (! dev)
1293 continue;
1294
1295 port = (struct veth_port *)dev->priv;
1296
1297 /* If this cnx is not on the vlan for this port, continue */
1298 if (! (port->lpar_map & (1 << cnx->remote_lp)))
1299 continue;
1300
1301 spin_lock(&port->queue_lock);
1302
1303 netif_stop_queue(dev);
1304 port->stopped_map |= (1 << cnx->remote_lp);
1305
1306 veth_debug("cnx %d: stopped queue for %s, map = 0x%x.\n",
1307 cnx->remote_lp, dev->name, port->stopped_map);
1308
1309 spin_unlock(&port->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
1311}
1312
Michael Ellerman24562ff2005-09-01 11:29:17 +10001313static void veth_timed_reset(unsigned long ptr)
1314{
1315 struct veth_lpar_connection *cnx = (struct veth_lpar_connection *)ptr;
1316 unsigned long trigger_time, flags;
1317
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. */
1321
1322 spin_lock_irqsave(&cnx->lock, flags);
1323
1324 if (cnx->outstanding_tx > 0) {
1325 trigger_time = cnx->last_contact + cnx->reset_timeout;
1326
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);
1338 }
1339 }
1340
1341 spin_unlock_irqrestore(&cnx->lock, flags);
1342}
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344/*
1345 * Rx path
1346 */
1347
1348static inline int veth_frame_wanted(struct veth_port *port, u64 mac_addr)
1349{
1350 int wanted = 0;
1351 int i;
1352 unsigned long flags;
1353
1354 if ( (mac_addr == port->mac_addr) || (mac_addr == 0xffffffffffff0000) )
1355 return 1;
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 read_lock_irqsave(&port->mcast_gate, flags);
1358
Michael Ellerman2a5391a2005-09-01 11:29:02 +10001359 if (port->promiscuous) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 wanted = 1;
1361 goto out;
1362 }
1363
1364 for (i = 0; i < port->num_mcast; ++i) {
1365 if (port->mcast_addr[i] == mac_addr) {
1366 wanted = 1;
1367 break;
1368 }
1369 }
1370
1371 out:
1372 read_unlock_irqrestore(&port->mcast_gate, flags);
1373
1374 return wanted;
1375}
1376
1377struct dma_chunk {
1378 u64 addr;
1379 u64 size;
1380};
1381
1382#define VETH_MAX_PAGES_PER_FRAME ( (VETH_MAX_MTU+PAGE_SIZE-2)/PAGE_SIZE + 1 )
1383
1384static inline void veth_build_dma_list(struct dma_chunk *list,
1385 unsigned char *p, unsigned long length)
1386{
1387 unsigned long done;
1388 int i = 1;
1389
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));
1398
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++;
1405 }
1406}
1407
1408static void veth_flush_acks(struct veth_lpar_connection *cnx)
1409{
1410 HvLpEvent_Rc rc;
1411
1412 rc = veth_signaldata(cnx, VethEventTypeFramesAck,
1413 0, &cnx->pending_acks);
1414
1415 if (rc != HvLpEvent_Rc_Good)
Michael Ellerman61a3c692005-09-01 11:28:57 +10001416 veth_error("Failed acking frames from LPAR %d, rc = %d\n",
1417 cnx->remote_lp, (int)rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 cnx->num_pending_acks = 0;
1420 memset(&cnx->pending_acks, 0xff, sizeof(cnx->pending_acks));
1421}
1422
1423static void veth_receive(struct veth_lpar_connection *cnx,
1424 struct VethLpEvent *event)
1425{
1426 struct VethFramesData *senddata = &event->u.frames_data;
1427 int startchunk = 0;
1428 int nchunks;
1429 unsigned long flags;
1430 HvLpDma_Rc rc;
1431
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;
1441
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));
1445
1446 /* a 0 address marks the end of the valid entries */
1447 if (senddata->addr[startchunk] == 0)
1448 break;
1449
1450 /* make sure that we have at least 1 EOF entry in the
1451 * remaining entries */
1452 if (! (senddata->eofmask >> (startchunk + VETH_EOF_SHIFT))) {
Michael Ellerman61a3c692005-09-01 11:28:57 +10001453 veth_error("Missing EOF fragment in event "
1454 "eofmask = 0x%x startchunk = %d\n",
1455 (unsigned)senddata->eofmask,
1456 startchunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 break;
1458 }
1459
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++))));
1470
1471 /* length == total length of all chunks */
1472 /* nchunks == # of chunks in this frame */
1473
1474 if ((length - ETH_HLEN) > VETH_MAX_MTU) {
Michael Ellerman61a3c692005-09-01 11:28:57 +10001475 veth_error("Received oversize frame from LPAR %d "
1476 "(length = %d)\n",
1477 cnx->remote_lp, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 continue;
1479 }
1480
1481 skb = alloc_skb(length, GFP_ATOMIC);
1482 if (!skb)
1483 continue;
1484
1485 veth_build_dma_list(local_list, skb->data, length);
1486
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;
1500 }
1501
1502 vlan = skb->data[9];
1503 dev = veth_dev[vlan];
Michael Ellerman41664c02005-05-12 17:55:08 +10001504 if (! dev) {
1505 /*
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.
1512 */
1513 dev_kfree_skb_irq(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 continue;
Michael Ellerman41664c02005-05-12 17:55:08 +10001515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 port = (struct veth_port *)dev->priv;
1518 dest = *((u64 *) skb->data) & 0xFFFFFFFFFFFF0000;
1519
1520 if ((vlan > HVMAXARCHITECTEDVIRTUALLANS) || !port) {
1521 dev_kfree_skb_irq(skb);
1522 continue;
1523 }
1524 if (! veth_frame_wanted(port, dest)) {
1525 dev_kfree_skb_irq(skb);
1526 continue;
1527 }
1528
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);
1537
1538 /* Ack it */
1539 spin_lock_irqsave(&cnx->lock, flags);
1540 BUG_ON(cnx->num_pending_acks > VETH_MAX_ACKS_PER_MSG);
1541
1542 cnx->pending_acks[cnx->num_pending_acks++] =
1543 event->base_event.xCorrelationToken;
1544
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);
1548
1549 spin_unlock_irqrestore(&cnx->lock, flags);
1550}
1551
1552static void veth_timed_ack(unsigned long ptr)
1553{
1554 struct veth_lpar_connection *cnx = (struct veth_lpar_connection *) ptr;
1555 unsigned long flags;
1556
1557 /* Ack all the events */
1558 spin_lock_irqsave(&cnx->lock, flags);
1559 if (cnx->num_pending_acks > 0)
1560 veth_flush_acks(cnx);
1561
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);
1566}
1567
1568static int veth_remove(struct vio_dev *vdev)
1569{
Michael Ellermanf0c129c2005-09-01 11:29:09 +10001570 struct veth_lpar_connection *cnx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 struct net_device *dev;
Michael Ellermanf0c129c2005-09-01 11:29:09 +10001572 struct veth_port *port;
1573 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Michael Ellermanf0c129c2005-09-01 11:29:09 +10001575 dev = veth_dev[vdev->unit_address];
1576
1577 if (! dev)
1578 return 0;
1579
1580 port = netdev_priv(dev);
1581
1582 for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
1583 cnx = veth_cnx[i];
1584
1585 if (cnx && (port->lpar_map & (1 << i))) {
1586 /* Drop our reference to connections on our VLAN */
1587 kobject_put(&cnx->kobject);
1588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
Michael Ellermanf0c129c2005-09-01 11:29:09 +10001590
1591 veth_dev[vdev->unit_address] = NULL;
Michael Ellerman07a5c172005-09-01 11:29:21 +10001592 kobject_del(&port->kobject);
1593 kobject_put(&port->kobject);
Michael Ellermanf0c129c2005-09-01 11:29:09 +10001594 unregister_netdev(dev);
1595 free_netdev(dev);
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 return 0;
1598}
1599
1600static int veth_probe(struct vio_dev *vdev, const struct vio_device_id *id)
1601{
1602 int i = vdev->unit_address;
1603 struct net_device *dev;
Michael Ellermanf0c129c2005-09-01 11:29:09 +10001604 struct veth_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
1606 dev = veth_probe_one(i, &vdev->dev);
1607 if (dev == NULL) {
1608 veth_remove(vdev);
1609 return 1;
1610 }
1611 veth_dev[i] = dev;
1612
Michael Ellermanf0c129c2005-09-01 11:29:09 +10001613 port = (struct veth_port*)netdev_priv(dev);
1614
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;
1619
1620 if (! (port->lpar_map & (1 << i)))
1621 continue;
1622
1623 cnx = veth_cnx[i];
1624 if (!cnx)
1625 continue;
1626
1627 kobject_get(&cnx->kobject);
1628 veth_kick_statemachine(cnx);
1629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 return 0;
1632}
1633
1634/**
1635 * veth_device_table: Used by vio.c to match devices that we
1636 * support.
1637 */
1638static struct vio_device_id veth_device_table[] __devinitdata = {
1639 { "vlan", "" },
Stephen Rothwellfb120da2005-08-17 16:42:59 +10001640 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641};
1642MODULE_DEVICE_TABLE(vio, veth_device_table);
1643
1644static struct vio_driver veth_driver = {
1645 .name = "iseries_veth",
1646 .id_table = veth_device_table,
1647 .probe = veth_probe,
1648 .remove = veth_remove
1649};
1650
1651/*
1652 * Module initialization/cleanup
1653 */
1654
1655void __exit veth_module_cleanup(void)
1656{
1657 int i;
Michael Ellermanf0c129c2005-09-01 11:29:09 +10001658 struct veth_lpar_connection *cnx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Michael Ellermanf0c129c2005-09-01 11:29:09 +10001660 /* Disconnect our "irq" to stop events coming from the Hypervisor. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 HvLpEvent_unregisterHandler(HvLpEvent_Type_VirtualLan);
1662
Michael Ellermanf0c129c2005-09-01 11:29:09 +10001663 /* Make sure any work queued from Hypervisor callbacks is finished. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 flush_scheduled_work();
1665
Michael Ellermanf0c129c2005-09-01 11:29:09 +10001666 for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
1667 cnx = veth_cnx[i];
1668
1669 if (!cnx)
1670 continue;
1671
Michael Ellerman76812d8122005-09-01 11:29:20 +10001672 /* Remove the connection from sysfs */
1673 kobject_del(&cnx->kobject);
Michael Ellermanf0c129c2005-09-01 11:29:09 +10001674 /* Drop the driver's reference to the connection */
1675 kobject_put(&cnx->kobject);
1676 }
1677
1678 /* Unregister the driver, which will close all the netdevs and stop
1679 * the connections when they're no longer referenced. */
Michael Ellermanb2e08522005-05-12 18:09:45 +10001680 vio_unregister_driver(&veth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682module_exit(veth_module_cleanup);
1683
1684int __init veth_module_init(void)
1685{
1686 int i;
1687 int rc;
1688
1689 this_lp = HvLpConfig_getLpIndex_outline();
1690
1691 for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
1692 rc = veth_init_connection(i);
Michael Ellermanec60bee2005-09-01 11:29:08 +10001693 if (rc != 0)
1694 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
1696
1697 HvLpEvent_registerHandler(HvLpEvent_Type_VirtualLan,
1698 &veth_handle_event);
1699
Michael Ellermanec60bee2005-09-01 11:29:08 +10001700 rc = vio_register_driver(&veth_driver);
1701 if (rc != 0)
1702 goto error;
1703
Michael Ellerman76812d8122005-09-01 11:29:20 +10001704 for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
1705 struct kobject *kobj;
1706
1707 if (!veth_cnx[i])
1708 continue;
1709
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);
1715 }
1716
Michael Ellermanec60bee2005-09-01 11:29:08 +10001717 return 0;
1718
1719error:
1720 for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
Michael Ellermanf0c129c2005-09-01 11:29:09 +10001721 veth_destroy_connection(veth_cnx[i]);
Michael Ellermanec60bee2005-09-01 11:29:08 +10001722 }
1723
1724 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726module_init(veth_module_init);