[PATCH] iseries_veth: Remove studly caps from iseries_veth.c
[linux-2.6.git] / drivers / net / iseries_veth.c
1 /* File veth.c created by Kyle A. Lucke on Mon Aug  7 2000. */
2 /*
3  * IBM eServer iSeries Virtual Ethernet Device Driver
4  * Copyright (C) 2001 Kyle A. Lucke (klucke@us.ibm.com), IBM Corp.
5  * Substantially cleaned up by:
6  * Copyright (C) 2003 David Gibson <dwg@au1.ibm.com>, IBM Corporation.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21  * USA
22  *
23  *
24  * This module implements the virtual ethernet device for iSeries LPAR
25  * Linux.  It uses hypervisor message passing to implement an
26  * ethernet-like network device communicating between partitions on
27  * the iSeries.
28  *
29  * The iSeries LPAR hypervisor currently allows for up to 16 different
30  * virtual ethernets.  These are all dynamically configurable on
31  * OS/400 partitions, but dynamic configuration is not supported under
32  * Linux yet.  An ethXX network device will be created for each
33  * virtual ethernet this partition is connected to.
34  *
35  * - This driver is responsible for routing packets to and from other
36  *   partitions.  The MAC addresses used by the virtual ethernets
37  *   contains meaning and must not be modified.
38  *
39  * - Having 2 virtual ethernets to the same remote partition DOES NOT
40  *   double the available bandwidth.  The 2 devices will share the
41  *   available hypervisor bandwidth.
42  *
43  * - If you send a packet to your own mac address, it will just be
44  *   dropped, you won't get it on the receive side.
45  *
46  * - Multicast is implemented by sending the frame frame to every
47  *   other partition.  It is the responsibility of the receiving
48  *   partition to filter the addresses desired.
49  *
50  * Tunable parameters:
51  *
52  * VETH_NUMBUFFERS: This compile time option defaults to 120.  It
53  * controls how much memory Linux will allocate per remote partition
54  * it is communicating with.  It can be thought of as the maximum
55  * number of packets outstanding to a remote partition at a time.
56  */
57
58 #include <linux/config.h>
59 #include <linux/module.h>
60 #include <linux/version.h>
61 #include <linux/types.h>
62 #include <linux/errno.h>
63 #include <linux/ioport.h>
64 #include <linux/kernel.h>
65 #include <linux/netdevice.h>
66 #include <linux/etherdevice.h>
67 #include <linux/skbuff.h>
68 #include <linux/init.h>
69 #include <linux/delay.h>
70 #include <linux/mm.h>
71 #include <linux/ethtool.h>
72 #include <asm/iSeries/mf.h>
73 #include <asm/iSeries/iSeries_pci.h>
74 #include <asm/uaccess.h>
75
76 #include <asm/iSeries/HvLpConfig.h>
77 #include <asm/iSeries/HvTypes.h>
78 #include <asm/iSeries/HvLpEvent.h>
79 #include <asm/iommu.h>
80 #include <asm/vio.h>
81
82 #undef DEBUG
83
84 MODULE_AUTHOR("Kyle Lucke <klucke@us.ibm.com>");
85 MODULE_DESCRIPTION("iSeries Virtual ethernet driver");
86 MODULE_LICENSE("GPL");
87
88 #define VETH_EVENT_CAP  (0)
89 #define VETH_EVENT_FRAMES       (1)
90 #define VETH_EVENT_MONITOR      (2)
91 #define VETH_EVENT_FRAMES_ACK   (3)
92
93 #define VETH_MAX_ACKS_PER_MSG   (20)
94 #define VETH_MAX_FRAMES_PER_MSG (6)
95
96 struct veth_frames_data {
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
103 struct veth_frames_ack_data {
104         u16 token[VETH_MAX_ACKS_PER_MSG];
105 };
106
107 struct veth_cap_data {
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
118 struct veth_lpevent {
119         struct HvLpEvent base_event;
120         union {
121                 struct veth_cap_data caps_data;
122                 struct veth_frames_data frames_data;
123                 struct veth_frames_ack_data frames_ack_data;
124         } u;
125
126 };
127
128 #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
154 struct veth_msg {
155         struct veth_msg *next;
156         struct veth_frames_data data;
157         int token;
158         int in_use;
159         struct sk_buff *skb;
160         struct device *dev;
161 };
162
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 veth_cap_data local_caps;
169
170         struct kobject kobject;
171         struct timer_list ack_timer;
172
173         struct timer_list reset_timer;
174         unsigned int reset_timeout;
175         unsigned long last_contact;
176         int outstanding_tx;
177
178         spinlock_t lock;
179         unsigned long state;
180         HvLpInstanceId src_inst;
181         HvLpInstanceId dst_inst;
182         struct veth_lpevent 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 veth_cap_data remote_caps;
188         u32 ack_timeout;
189
190         struct veth_msg *msg_stack_head;
191 };
192
193 struct veth_port {
194         struct device *dev;
195         struct net_device_stats stats;
196         u64 mac_addr;
197         HvLpIndexMap lpar_map;
198
199         /* queue_lock protects the stopped_map and dev's queue. */
200         spinlock_t queue_lock;
201         HvLpIndexMap stopped_map;
202
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];
208
209         struct kobject kobject;
210 };
211
212 static HvLpIndex this_lp;
213 static struct veth_lpar_connection *veth_cnx[HVMAXARCHITECTEDLPS]; /* = 0 */
214 static struct net_device *veth_dev[HVMAXARCHITECTEDVIRTUALLANS]; /* = 0 */
215
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 veth_lpevent *);
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);
224
225 /*
226  * Utility functions
227  */
228
229 #define veth_info(fmt, args...) \
230         printk(KERN_INFO "iseries_veth: " fmt, ## args)
231
232 #define veth_error(fmt, args...) \
233         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
241
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)
245 {
246         msg->next = cnx->msg_stack_head;
247         cnx->msg_stack_head = msg;
248 }
249
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)
252 {
253         struct veth_msg *msg;
254
255         msg = cnx->msg_stack_head;
256         if (msg)
257                 cnx->msg_stack_head = cnx->msg_stack_head->next;
258
259         return msg;
260 }
261
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)
264 {
265         return cnx->msg_stack_head == NULL;
266 }
267
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)
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
283 static 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
293 struct veth_allocation {
294         struct completion c;
295         int num;
296 };
297
298 static 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
306 static 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 veth_lpevent), number,
312                             &veth_complete_allocation, &vc);
313         wait_for_completion(&vc.c);
314
315         return vc.num;
316 }
317
318 /*
319  * sysfs support
320  */
321
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);
326 };
327
328 static 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)                    \
344 static ssize_t _name##_show(struct veth_lpar_connection *cnx, char *buf)\
345 {                                                                       \
346         return sprintf(buf, _format, _expression);                      \
347 }                                                                       \
348 struct 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
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);
365
366 #define GET_CNX_ATTR(_name)     (&veth_cnx_attr_##_name.attr)
367
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
381 };
382
383 static struct sysfs_ops veth_cnx_sysfs_ops = {
384                 .show = veth_cnx_attribute_show
385 };
386
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
391 };
392
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);
397 };
398
399 static 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)                   \
415 static ssize_t _name##_show(struct veth_port *port, char *buf)          \
416 {                                                                       \
417         return sprintf(buf, _format, _expression);                      \
418 }                                                                       \
419 struct 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
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);
429
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
438 };
439
440 static struct sysfs_ops veth_port_sysfs_ops = {
441         .show = veth_port_attribute_show
442 };
443
444 static struct kobj_type veth_port_ktype = {
445         .sysfs_ops      = &veth_port_sysfs_ops,
446         .default_attrs  = veth_port_default_attrs
447 };
448
449 /*
450  * LPAR connection code
451  */
452
453 static inline void veth_kick_statemachine(struct veth_lpar_connection *cnx)
454 {
455         schedule_work(&cnx->statemachine_wq);
456 }
457
458 static void veth_take_cap(struct veth_lpar_connection *cnx,
459                           struct veth_lpevent *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) {
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);
479         }
480         spin_unlock_irqrestore(&cnx->lock, flags);
481 }
482
483 static void veth_take_cap_ack(struct veth_lpar_connection *cnx,
484                               struct veth_lpevent *event)
485 {
486         unsigned long flags;
487
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);
497         }
498         spin_unlock_irqrestore(&cnx->lock, flags);
499 }
500
501 static void veth_take_monitor_ack(struct veth_lpar_connection *cnx,
502                                   struct veth_lpevent *event)
503 {
504         unsigned long flags;
505
506         spin_lock_irqsave(&cnx->lock, flags);
507         veth_debug("cnx %d: lost connection.\n", cnx->remote_lp);
508
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         }
516         spin_unlock_irqrestore(&cnx->lock, flags);
517 }
518
519 static void veth_handle_ack(struct veth_lpevent *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 VETH_EVENT_CAP:
528                 veth_take_cap_ack(cnx, event);
529                 break;
530         case VETH_EVENT_MONITOR:
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);
536         };
537 }
538
539 static void veth_handle_int(struct veth_lpevent *event)
540 {
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;
545
546         BUG_ON(! cnx);
547
548         switch (event->base_event.xSubtype) {
549         case VETH_EVENT_CAP:
550                 veth_take_cap(cnx, event);
551                 break;
552         case VETH_EVENT_MONITOR:
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 VETH_EVENT_FRAMES_ACK:
557                 spin_lock_irqsave(&cnx->lock, flags);
558
559                 for (i = 0; i < VETH_MAX_ACKS_PER_MSG; ++i) {
560                         u16 msgnum = event->u.frames_ack_data.token[i];
561
562                         if (msgnum < VETH_NUMBUFFERS) {
563                                 veth_recycle_msg(cnx, cnx->msgs + msgnum);
564                                 cnx->outstanding_tx--;
565                                 acked++;
566                         }
567                 }
568
569                 if (acked > 0) {
570                         cnx->last_contact = jiffies;
571                         veth_wake_queues(cnx);
572                 }
573
574                 spin_unlock_irqrestore(&cnx->lock, flags);
575                 break;
576         case VETH_EVENT_FRAMES:
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);
582         };
583 }
584
585 static void veth_handle_event(struct HvLpEvent *event, struct pt_regs *regs)
586 {
587         struct veth_lpevent *veth_event = (struct veth_lpevent *)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
595 static int veth_process_caps(struct veth_lpar_connection *cnx)
596 {
597         struct veth_cap_data *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) ) {
607                 veth_error("Received incompatible capabilities from LPAR %d.\n",
608                                 cnx->remote_lp);
609                 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) {
625                         veth_error("Couldn't allocate enough ack events "
626                                         "for LPAR %d.\n", cnx->remote_lp);
627
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 */
637 static 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) {
647                 if (cnx->state & VETH_STATE_OPEN)
648                         HvCallEvent_closeLpEventPath(cnx->remote_lp,
649                                                      HvLpEvent_Type_VirtualLan);
650
651                 /*
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                  */
656                 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 */
665                 if (cnx->msgs) {
666                         int i;
667                         for (i = 0; i < VETH_NUMBUFFERS; ++i)
668                                 veth_recycle_msg(cnx, cnx->msgs + i);
669                 }
670
671                 cnx->outstanding_tx = 0;
672                 veth_wake_queues(cnx);
673
674                 /* Drop the lock so we can do stuff that might sleep or
675                  * take other locks. */
676                 spin_unlock_irq(&cnx->lock);
677
678                 del_timer_sync(&cnx->ack_timer);
679                 del_timer_sync(&cnx->reset_timer);
680
681                 spin_lock_irq(&cnx->lock);
682
683                 if (cnx->state & VETH_STATE_RESET)
684                         goto restart;
685
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                 }
691         }
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, VETH_EVENT_MONITOR,
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) )
723                                 veth_error("Error sending monitor to LPAR %d, "
724                                                 "rc = %d\n", rlp, rc);
725
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, VETH_EVENT_CAP,
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) )
747                                 veth_error("Error sending caps to LPAR %d, "
748                                                 "rc = %d\n", rlp, rc);
749
750                         /* 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 veth_cap_data *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 {
789                         veth_error("Caps rejected by LPAR %d, rc = %d\n",
790                                         rlp, cnx->cap_ack_event.base_event.xRc);
791                         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 */
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);
807 }
808
809 static int veth_init_connection(u8 rlp)
810 {
811         struct veth_lpar_connection *cnx;
812         struct veth_msg *msgs;
813         int i, rc;
814
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);
827
828         init_timer(&cnx->ack_timer);
829         cnx->ack_timer.function = veth_timed_ack;
830         cnx->ack_timer.data = (unsigned long) cnx;
831
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
837         memset(&cnx->pending_acks, 0xff, sizeof (cnx->pending_acks));
838
839         veth_cnx[rlp] = cnx;
840
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;
848
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;
853         }
854
855         cnx->msgs = msgs;
856         memset(msgs, 0, VETH_NUMBUFFERS * sizeof(struct veth_msg));
857
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)) {
866                 veth_error("Can't allocate enough events for LPAR %d.\n", rlp);
867                 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
877 static void veth_stop_connection(struct veth_lpar_connection *cnx)
878 {
879         if (!cnx)
880                 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
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);
894         }
895
896         /* Wait for the state machine to run. */
897         flush_scheduled_work();
898 }
899
900 static void veth_destroy_connection(struct veth_lpar_connection *cnx)
901 {
902         if (!cnx)
903                 return;
904
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);
915
916         kfree(cnx->msgs);
917         veth_cnx[cnx->remote_lp] = NULL;
918         kfree(cnx);
919 }
920
921 static 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);
927 }
928
929 /*
930  * net_device code
931  */
932
933 static 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
942 static int veth_close(struct net_device *dev)
943 {
944         netif_stop_queue(dev);
945         return 0;
946 }
947
948 static 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
955 static 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
963 static 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
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;
976
977                 port->promiscuous = 0;
978
979                 /* 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
998 static 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
1005 static 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
1022 static u32 veth_get_link(struct net_device *dev)
1023 {
1024         return 1;
1025 }
1026
1027 static struct ethtool_ops ops = {
1028         .get_drvinfo = veth_get_drvinfo,
1029         .get_settings = veth_get_settings,
1030         .get_link = veth_get_link,
1031 };
1032
1033 static 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
1047         spin_lock_init(&port->queue_lock);
1048         rwlock_init(&port->mcast_gate);
1049         port->stopped_map = 0;
1050
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
1082         SET_NETDEV_DEV(dev, vdev);
1083
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;
1089         }
1090
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);
1097
1098         veth_info("%s attached to iSeries vlan %d (LPAR map = 0x%.4X)\n",
1099                         dev->name, vlan, port->lpar_map);
1100
1101         return dev;
1102 }
1103
1104 /*
1105  * Tx path
1106  */
1107
1108 static 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;
1114         struct veth_msg *msg = NULL;
1115         unsigned long flags;
1116
1117         if (! cnx)
1118                 return 0;
1119
1120         spin_lock_irqsave(&cnx->lock, flags);
1121
1122         if (! (cnx->state & VETH_STATE_READY))
1123                 goto no_error;
1124
1125         if ((skb->len - ETH_HLEN) > VETH_MAX_MTU)
1126                 goto drop;
1127
1128         msg = veth_stack_pop(cnx);
1129         if (! msg)
1130                 goto drop;
1131
1132         msg->in_use = 1;
1133         msg->skb = skb_get(skb);
1134
1135         msg->data.addr[0] = dma_map_single(port->dev, skb->data,
1136                                 skb->len, DMA_TO_DEVICE);
1137
1138         if (dma_mapping_error(msg->data.addr[0]))
1139                 goto recycle_and_drop;
1140
1141         msg->dev = port->dev;
1142         msg->data.len[0] = skb->len;
1143         msg->data.eofmask = 1 << VETH_EOF_SHIFT;
1144
1145         rc = veth_signaldata(cnx, VETH_EVENT_FRAMES, msg->token, &msg->data);
1146
1147         if (rc != HvLpEvent_Rc_Good)
1148                 goto recycle_and_drop;
1149
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);
1153
1154         cnx->last_contact = jiffies;
1155         cnx->outstanding_tx++;
1156
1157         if (veth_stack_is_empty(cnx))
1158                 veth_stop_queues(cnx);
1159
1160  no_error:
1161         spin_unlock_irqrestore(&cnx->lock, flags);
1162         return 0;
1163
1164  recycle_and_drop:
1165         veth_recycle_msg(cnx, msg);
1166  drop:
1167         spin_unlock_irqrestore(&cnx->lock, flags);
1168         return 1;
1169 }
1170
1171 static void veth_transmit_to_many(struct sk_buff *skb,
1172                                           HvLpIndexMap lpmask,
1173                                           struct net_device *dev)
1174 {
1175         struct veth_port *port = (struct veth_port *) dev->priv;
1176         int i, success, error;
1177
1178         success = error = 0;
1179
1180         for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
1181                 if ((lpmask & (1 << i)) == 0)
1182                         continue;
1183
1184                 if (veth_transmit_to_one(skb, i, dev))
1185                         error = 1;
1186                 else
1187                         success = 1;
1188         }
1189
1190         if (error)
1191                 port->stats.tx_errors++;
1192
1193         if (success) {
1194                 port->stats.tx_packets++;
1195                 port->stats.tx_bytes += skb->len;
1196         }
1197 }
1198
1199 static 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;
1203         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
1219         veth_transmit_to_many(skb, lpmask, dev);
1220
1221         dev_kfree_skb(skb);
1222
1223         return 0;
1224 }
1225
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)
1229 {
1230         u32 dma_address, dma_length;
1231
1232         if (msg->in_use) {
1233                 msg->in_use = 0;
1234                 dma_address = msg->data.addr[0];
1235                 dma_length = msg->data.len[0];
1236
1237                 if (!dma_mapping_error(dma_address))
1238                         dma_unmap_single(msg->dev, dma_address, dma_length,
1239                                         DMA_TO_DEVICE);
1240
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);
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);
1251         }
1252 }
1253
1254 static void veth_wake_queues(struct veth_lpar_connection *cnx)
1255 {
1256         int i;
1257
1258         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
1271                 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);
1279                 }
1280                 spin_unlock_irqrestore(&port->queue_lock, flags);
1281         }
1282 }
1283
1284 static 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);
1310         }
1311 }
1312
1313 static 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
1344 /*
1345  * Rx path
1346  */
1347
1348 static 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
1357         read_lock_irqsave(&port->mcast_gate, flags);
1358
1359         if (port->promiscuous) {
1360                 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
1377 struct 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
1384 static 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
1408 static void veth_flush_acks(struct veth_lpar_connection *cnx)
1409 {
1410         HvLpEvent_Rc rc;
1411
1412         rc = veth_signaldata(cnx, VETH_EVENT_FRAMES_ACK,
1413                              0, &cnx->pending_acks);
1414
1415         if (rc != HvLpEvent_Rc_Good)
1416                 veth_error("Failed acking frames from LPAR %d, rc = %d\n",
1417                                 cnx->remote_lp, (int)rc);
1418
1419         cnx->num_pending_acks = 0;
1420         memset(&cnx->pending_acks, 0xff, sizeof(cnx->pending_acks));
1421 }
1422
1423 static void veth_receive(struct veth_lpar_connection *cnx,
1424                          struct veth_lpevent *event)
1425 {
1426         struct veth_frames_data *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))) {
1453                         veth_error("Missing EOF fragment in event "
1454                                         "eofmask = 0x%x startchunk = %d\n",
1455                                         (unsigned)senddata->eofmask,
1456                                         startchunk);
1457                         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) {
1475                         veth_error("Received oversize frame from LPAR %d "
1476                                         "(length = %d)\n",
1477                                         cnx->remote_lp, length);
1478                         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];
1504                 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);
1514                         continue;
1515                 }
1516
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
1552 static 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
1568 static int veth_remove(struct vio_dev *vdev)
1569 {
1570         struct veth_lpar_connection *cnx;
1571         struct net_device *dev;
1572         struct veth_port *port;
1573         int i;
1574
1575         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                 }
1589         }
1590
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);
1596
1597         return 0;
1598 }
1599
1600 static 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;
1604         struct veth_port *port;
1605
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
1613         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         }
1630
1631         return 0;
1632 }
1633
1634 /**
1635  * veth_device_table: Used by vio.c to match devices that we
1636  * support.
1637  */
1638 static struct vio_device_id veth_device_table[] __devinitdata = {
1639         { "vlan", "" },
1640         { "", "" }
1641 };
1642 MODULE_DEVICE_TABLE(vio, veth_device_table);
1643
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
1649 };
1650
1651 /*
1652  * Module initialization/cleanup
1653  */
1654
1655 void __exit veth_module_cleanup(void)
1656 {
1657         int i;
1658         struct veth_lpar_connection *cnx;
1659
1660         /* Disconnect our "irq" to stop events coming from the Hypervisor. */
1661         HvLpEvent_unregisterHandler(HvLpEvent_Type_VirtualLan);
1662
1663         /* Make sure any work queued from Hypervisor callbacks is finished. */
1664         flush_scheduled_work();
1665
1666         for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
1667                 cnx = veth_cnx[i];
1668
1669                 if (!cnx)
1670                         continue;
1671
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);
1676         }
1677
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);
1681 }
1682 module_exit(veth_module_cleanup);
1683
1684 int __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);
1693                 if (rc != 0)
1694                         goto error;
1695         }
1696
1697         HvLpEvent_registerHandler(HvLpEvent_Type_VirtualLan,
1698                                   &veth_handle_event);
1699
1700         rc = vio_register_driver(&veth_driver);
1701         if (rc != 0)
1702                 goto error;
1703
1704         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
1717         return 0;
1718
1719 error:
1720         for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
1721                 veth_destroy_connection(veth_cnx[i]);
1722         }
1723
1724         return rc;
1725 }
1726 module_init(veth_module_init);