forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiseries_veth.c
1735 lines (1407 loc) · 43.5 KB
/
iseries_veth.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* File veth.c created by Kyle A. Lucke on Mon Aug 7 2000. */
/*
* IBM eServer iSeries Virtual Ethernet Device Driver
* Copyright (C) 2001 Kyle A. Lucke ([email protected]), IBM Corp.
* Substantially cleaned up by:
* Copyright (C) 2003 David Gibson <[email protected]>, IBM Corporation.
* Copyright (C) 2004-2005 Michael Ellerman, IBM Corporation.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
*
* This module implements the virtual ethernet device for iSeries LPAR
* Linux. It uses hypervisor message passing to implement an
* ethernet-like network device communicating between partitions on
* the iSeries.
*
* The iSeries LPAR hypervisor currently allows for up to 16 different
* virtual ethernets. These are all dynamically configurable on
* OS/400 partitions, but dynamic configuration is not supported under
* Linux yet. An ethXX network device will be created for each
* virtual ethernet this partition is connected to.
*
* - This driver is responsible for routing packets to and from other
* partitions. The MAC addresses used by the virtual ethernets
* contains meaning and must not be modified.
*
* - Having 2 virtual ethernets to the same remote partition DOES NOT
* double the available bandwidth. The 2 devices will share the
* available hypervisor bandwidth.
*
* - If you send a packet to your own mac address, it will just be
* dropped, you won't get it on the receive side.
*
* - Multicast is implemented by sending the frame frame to every
* other partition. It is the responsibility of the receiving
* partition to filter the addresses desired.
*
* Tunable parameters:
*
* VETH_NUMBUFFERS: This compile time option defaults to 120. It
* controls how much memory Linux will allocate per remote partition
* it is communicating with. It can be thought of as the maximum
* number of packets outstanding to a remote partition at a time.
*/
#include <linux/config.h>
#include <linux/module.h>
#include <linux/version.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/ioport.h>
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/mm.h>
#include <linux/ethtool.h>
#include <asm/abs_addr.h>
#include <asm/iseries/mf.h>
#include <asm/uaccess.h>
#include <asm/iseries/hv_lp_config.h>
#include <asm/iseries/hv_types.h>
#include <asm/iseries/hv_lp_event.h>
#include <asm/iommu.h>
#include <asm/vio.h>
#undef DEBUG
MODULE_AUTHOR("Kyle Lucke <[email protected]>");
MODULE_DESCRIPTION("iSeries Virtual ethernet driver");
MODULE_LICENSE("GPL");
#define VETH_EVENT_CAP (0)
#define VETH_EVENT_FRAMES (1)
#define VETH_EVENT_MONITOR (2)
#define VETH_EVENT_FRAMES_ACK (3)
#define VETH_MAX_ACKS_PER_MSG (20)
#define VETH_MAX_FRAMES_PER_MSG (6)
struct veth_frames_data {
u32 addr[VETH_MAX_FRAMES_PER_MSG];
u16 len[VETH_MAX_FRAMES_PER_MSG];
u32 eofmask;
};
#define VETH_EOF_SHIFT (32-VETH_MAX_FRAMES_PER_MSG)
struct veth_frames_ack_data {
u16 token[VETH_MAX_ACKS_PER_MSG];
};
struct veth_cap_data {
u8 caps_version;
u8 rsvd1;
u16 num_buffers;
u16 ack_threshold;
u16 rsvd2;
u32 ack_timeout;
u32 rsvd3;
u64 rsvd4[3];
};
struct veth_lpevent {
struct HvLpEvent base_event;
union {
struct veth_cap_data caps_data;
struct veth_frames_data frames_data;
struct veth_frames_ack_data frames_ack_data;
} u;
};
#define DRV_NAME "iseries_veth"
#define DRV_VERSION "2.0"
#define VETH_NUMBUFFERS (120)
#define VETH_ACKTIMEOUT (1000000) /* microseconds */
#define VETH_MAX_MCAST (12)
#define VETH_MAX_MTU (9000)
#if VETH_NUMBUFFERS < 10
#define ACK_THRESHOLD (1)
#elif VETH_NUMBUFFERS < 20
#define ACK_THRESHOLD (4)
#elif VETH_NUMBUFFERS < 40
#define ACK_THRESHOLD (10)
#else
#define ACK_THRESHOLD (20)
#endif
#define VETH_STATE_SHUTDOWN (0x0001)
#define VETH_STATE_OPEN (0x0002)
#define VETH_STATE_RESET (0x0004)
#define VETH_STATE_SENTMON (0x0008)
#define VETH_STATE_SENTCAPS (0x0010)
#define VETH_STATE_GOTCAPACK (0x0020)
#define VETH_STATE_GOTCAPS (0x0040)
#define VETH_STATE_SENTCAPACK (0x0080)
#define VETH_STATE_READY (0x0100)
struct veth_msg {
struct veth_msg *next;
struct veth_frames_data data;
int token;
int in_use;
struct sk_buff *skb;
struct device *dev;
};
struct veth_lpar_connection {
HvLpIndex remote_lp;
struct work_struct statemachine_wq;
struct veth_msg *msgs;
int num_events;
struct veth_cap_data local_caps;
struct kobject kobject;
struct timer_list ack_timer;
struct timer_list reset_timer;
unsigned int reset_timeout;
unsigned long last_contact;
int outstanding_tx;
spinlock_t lock;
unsigned long state;
HvLpInstanceId src_inst;
HvLpInstanceId dst_inst;
struct veth_lpevent cap_event, cap_ack_event;
u16 pending_acks[VETH_MAX_ACKS_PER_MSG];
u32 num_pending_acks;
int num_ack_events;
struct veth_cap_data remote_caps;
u32 ack_timeout;
struct veth_msg *msg_stack_head;
};
struct veth_port {
struct device *dev;
struct net_device_stats stats;
u64 mac_addr;
HvLpIndexMap lpar_map;
/* queue_lock protects the stopped_map and dev's queue. */
spinlock_t queue_lock;
HvLpIndexMap stopped_map;
/* mcast_gate protects promiscuous, num_mcast & mcast_addr. */
rwlock_t mcast_gate;
int promiscuous;
int num_mcast;
u64 mcast_addr[VETH_MAX_MCAST];
struct kobject kobject;
};
static HvLpIndex this_lp;
static struct veth_lpar_connection *veth_cnx[HVMAXARCHITECTEDLPS]; /* = 0 */
static struct net_device *veth_dev[HVMAXARCHITECTEDVIRTUALLANS]; /* = 0 */
static int veth_start_xmit(struct sk_buff *skb, struct net_device *dev);
static void veth_recycle_msg(struct veth_lpar_connection *, struct veth_msg *);
static void veth_wake_queues(struct veth_lpar_connection *cnx);
static void veth_stop_queues(struct veth_lpar_connection *cnx);
static void veth_receive(struct veth_lpar_connection *, struct veth_lpevent *);
static void veth_release_connection(struct kobject *kobject);
static void veth_timed_ack(unsigned long ptr);
static void veth_timed_reset(unsigned long ptr);
/*
* Utility functions
*/
#define veth_info(fmt, args...) \
printk(KERN_INFO DRV_NAME ": " fmt, ## args)
#define veth_error(fmt, args...) \
printk(KERN_ERR DRV_NAME ": Error: " fmt, ## args)
#ifdef DEBUG
#define veth_debug(fmt, args...) \
printk(KERN_DEBUG DRV_NAME ": " fmt, ## args)
#else
#define veth_debug(fmt, args...) do {} while (0)
#endif
/* You must hold the connection's lock when you call this function. */
static inline void veth_stack_push(struct veth_lpar_connection *cnx,
struct veth_msg *msg)
{
msg->next = cnx->msg_stack_head;
cnx->msg_stack_head = msg;
}
/* You must hold the connection's lock when you call this function. */
static inline struct veth_msg *veth_stack_pop(struct veth_lpar_connection *cnx)
{
struct veth_msg *msg;
msg = cnx->msg_stack_head;
if (msg)
cnx->msg_stack_head = cnx->msg_stack_head->next;
return msg;
}
/* You must hold the connection's lock when you call this function. */
static inline int veth_stack_is_empty(struct veth_lpar_connection *cnx)
{
return cnx->msg_stack_head == NULL;
}
static inline HvLpEvent_Rc
veth_signalevent(struct veth_lpar_connection *cnx, u16 subtype,
HvLpEvent_AckInd ackind, HvLpEvent_AckType acktype,
u64 token,
u64 data1, u64 data2, u64 data3, u64 data4, u64 data5)
{
return HvCallEvent_signalLpEventFast(cnx->remote_lp,
HvLpEvent_Type_VirtualLan,
subtype, ackind, acktype,
cnx->src_inst,
cnx->dst_inst,
token, data1, data2, data3,
data4, data5);
}
static inline HvLpEvent_Rc veth_signaldata(struct veth_lpar_connection *cnx,
u16 subtype, u64 token, void *data)
{
u64 *p = (u64 *) data;
return veth_signalevent(cnx, subtype, HvLpEvent_AckInd_NoAck,
HvLpEvent_AckType_ImmediateAck,
token, p[0], p[1], p[2], p[3], p[4]);
}
struct veth_allocation {
struct completion c;
int num;
};
static void veth_complete_allocation(void *parm, int number)
{
struct veth_allocation *vc = (struct veth_allocation *)parm;
vc->num = number;
complete(&vc->c);
}
static int veth_allocate_events(HvLpIndex rlp, int number)
{
struct veth_allocation vc = { COMPLETION_INITIALIZER(vc.c), 0 };
mf_allocate_lp_events(rlp, HvLpEvent_Type_VirtualLan,
sizeof(struct veth_lpevent), number,
&veth_complete_allocation, &vc);
wait_for_completion(&vc.c);
return vc.num;
}
/*
* sysfs support
*/
struct veth_cnx_attribute {
struct attribute attr;
ssize_t (*show)(struct veth_lpar_connection *, char *buf);
ssize_t (*store)(struct veth_lpar_connection *, const char *buf);
};
static ssize_t veth_cnx_attribute_show(struct kobject *kobj,
struct attribute *attr, char *buf)
{
struct veth_cnx_attribute *cnx_attr;
struct veth_lpar_connection *cnx;
cnx_attr = container_of(attr, struct veth_cnx_attribute, attr);
cnx = container_of(kobj, struct veth_lpar_connection, kobject);
if (!cnx_attr->show)
return -EIO;
return cnx_attr->show(cnx, buf);
}
#define CUSTOM_CNX_ATTR(_name, _format, _expression) \
static ssize_t _name##_show(struct veth_lpar_connection *cnx, char *buf)\
{ \
return sprintf(buf, _format, _expression); \
} \
struct veth_cnx_attribute veth_cnx_attr_##_name = __ATTR_RO(_name)
#define SIMPLE_CNX_ATTR(_name) \
CUSTOM_CNX_ATTR(_name, "%lu\n", (unsigned long)cnx->_name)
SIMPLE_CNX_ATTR(outstanding_tx);
SIMPLE_CNX_ATTR(remote_lp);
SIMPLE_CNX_ATTR(num_events);
SIMPLE_CNX_ATTR(src_inst);
SIMPLE_CNX_ATTR(dst_inst);
SIMPLE_CNX_ATTR(num_pending_acks);
SIMPLE_CNX_ATTR(num_ack_events);
CUSTOM_CNX_ATTR(ack_timeout, "%d\n", jiffies_to_msecs(cnx->ack_timeout));
CUSTOM_CNX_ATTR(reset_timeout, "%d\n", jiffies_to_msecs(cnx->reset_timeout));
CUSTOM_CNX_ATTR(state, "0x%.4lX\n", cnx->state);
CUSTOM_CNX_ATTR(last_contact, "%d\n", cnx->last_contact ?
jiffies_to_msecs(jiffies - cnx->last_contact) : 0);
#define GET_CNX_ATTR(_name) (&veth_cnx_attr_##_name.attr)
static struct attribute *veth_cnx_default_attrs[] = {
GET_CNX_ATTR(outstanding_tx),
GET_CNX_ATTR(remote_lp),
GET_CNX_ATTR(num_events),
GET_CNX_ATTR(reset_timeout),
GET_CNX_ATTR(last_contact),
GET_CNX_ATTR(state),
GET_CNX_ATTR(src_inst),
GET_CNX_ATTR(dst_inst),
GET_CNX_ATTR(num_pending_acks),
GET_CNX_ATTR(num_ack_events),
GET_CNX_ATTR(ack_timeout),
NULL
};
static struct sysfs_ops veth_cnx_sysfs_ops = {
.show = veth_cnx_attribute_show
};
static struct kobj_type veth_lpar_connection_ktype = {
.release = veth_release_connection,
.sysfs_ops = &veth_cnx_sysfs_ops,
.default_attrs = veth_cnx_default_attrs
};
struct veth_port_attribute {
struct attribute attr;
ssize_t (*show)(struct veth_port *, char *buf);
ssize_t (*store)(struct veth_port *, const char *buf);
};
static ssize_t veth_port_attribute_show(struct kobject *kobj,
struct attribute *attr, char *buf)
{
struct veth_port_attribute *port_attr;
struct veth_port *port;
port_attr = container_of(attr, struct veth_port_attribute, attr);
port = container_of(kobj, struct veth_port, kobject);
if (!port_attr->show)
return -EIO;
return port_attr->show(port, buf);
}
#define CUSTOM_PORT_ATTR(_name, _format, _expression) \
static ssize_t _name##_show(struct veth_port *port, char *buf) \
{ \
return sprintf(buf, _format, _expression); \
} \
struct veth_port_attribute veth_port_attr_##_name = __ATTR_RO(_name)
#define SIMPLE_PORT_ATTR(_name) \
CUSTOM_PORT_ATTR(_name, "%lu\n", (unsigned long)port->_name)
SIMPLE_PORT_ATTR(promiscuous);
SIMPLE_PORT_ATTR(num_mcast);
CUSTOM_PORT_ATTR(lpar_map, "0x%X\n", port->lpar_map);
CUSTOM_PORT_ATTR(stopped_map, "0x%X\n", port->stopped_map);
CUSTOM_PORT_ATTR(mac_addr, "0x%lX\n", port->mac_addr);
#define GET_PORT_ATTR(_name) (&veth_port_attr_##_name.attr)
static struct attribute *veth_port_default_attrs[] = {
GET_PORT_ATTR(mac_addr),
GET_PORT_ATTR(lpar_map),
GET_PORT_ATTR(stopped_map),
GET_PORT_ATTR(promiscuous),
GET_PORT_ATTR(num_mcast),
NULL
};
static struct sysfs_ops veth_port_sysfs_ops = {
.show = veth_port_attribute_show
};
static struct kobj_type veth_port_ktype = {
.sysfs_ops = &veth_port_sysfs_ops,
.default_attrs = veth_port_default_attrs
};
/*
* LPAR connection code
*/
static inline void veth_kick_statemachine(struct veth_lpar_connection *cnx)
{
schedule_work(&cnx->statemachine_wq);
}
static void veth_take_cap(struct veth_lpar_connection *cnx,
struct veth_lpevent *event)
{
unsigned long flags;
spin_lock_irqsave(&cnx->lock, flags);
/* Receiving caps may mean the other end has just come up, so
* we need to reload the instance ID of the far end */
cnx->dst_inst =
HvCallEvent_getTargetLpInstanceId(cnx->remote_lp,
HvLpEvent_Type_VirtualLan);
if (cnx->state & VETH_STATE_GOTCAPS) {
veth_error("Received a second capabilities from LPAR %d.\n",
cnx->remote_lp);
event->base_event.xRc = HvLpEvent_Rc_BufferNotAvailable;
HvCallEvent_ackLpEvent((struct HvLpEvent *) event);
} else {
memcpy(&cnx->cap_event, event, sizeof(cnx->cap_event));
cnx->state |= VETH_STATE_GOTCAPS;
veth_kick_statemachine(cnx);
}
spin_unlock_irqrestore(&cnx->lock, flags);
}
static void veth_take_cap_ack(struct veth_lpar_connection *cnx,
struct veth_lpevent *event)
{
unsigned long flags;
spin_lock_irqsave(&cnx->lock, flags);
if (cnx->state & VETH_STATE_GOTCAPACK) {
veth_error("Received a second capabilities ack from LPAR %d.\n",
cnx->remote_lp);
} else {
memcpy(&cnx->cap_ack_event, event,
sizeof(&cnx->cap_ack_event));
cnx->state |= VETH_STATE_GOTCAPACK;
veth_kick_statemachine(cnx);
}
spin_unlock_irqrestore(&cnx->lock, flags);
}
static void veth_take_monitor_ack(struct veth_lpar_connection *cnx,
struct veth_lpevent *event)
{
unsigned long flags;
spin_lock_irqsave(&cnx->lock, flags);
veth_debug("cnx %d: lost connection.\n", cnx->remote_lp);
/* Avoid kicking the statemachine once we're shutdown.
* It's unnecessary and it could break veth_stop_connection(). */
if (! (cnx->state & VETH_STATE_SHUTDOWN)) {
cnx->state |= VETH_STATE_RESET;
veth_kick_statemachine(cnx);
}
spin_unlock_irqrestore(&cnx->lock, flags);
}
static void veth_handle_ack(struct veth_lpevent *event)
{
HvLpIndex rlp = event->base_event.xTargetLp;
struct veth_lpar_connection *cnx = veth_cnx[rlp];
BUG_ON(! cnx);
switch (event->base_event.xSubtype) {
case VETH_EVENT_CAP:
veth_take_cap_ack(cnx, event);
break;
case VETH_EVENT_MONITOR:
veth_take_monitor_ack(cnx, event);
break;
default:
veth_error("Unknown ack type %d from LPAR %d.\n",
event->base_event.xSubtype, rlp);
};
}
static void veth_handle_int(struct veth_lpevent *event)
{
HvLpIndex rlp = event->base_event.xSourceLp;
struct veth_lpar_connection *cnx = veth_cnx[rlp];
unsigned long flags;
int i, acked = 0;
BUG_ON(! cnx);
switch (event->base_event.xSubtype) {
case VETH_EVENT_CAP:
veth_take_cap(cnx, event);
break;
case VETH_EVENT_MONITOR:
/* do nothing... this'll hang out here til we're dead,
* and the hypervisor will return it for us. */
break;
case VETH_EVENT_FRAMES_ACK:
spin_lock_irqsave(&cnx->lock, flags);
for (i = 0; i < VETH_MAX_ACKS_PER_MSG; ++i) {
u16 msgnum = event->u.frames_ack_data.token[i];
if (msgnum < VETH_NUMBUFFERS) {
veth_recycle_msg(cnx, cnx->msgs + msgnum);
cnx->outstanding_tx--;
acked++;
}
}
if (acked > 0) {
cnx->last_contact = jiffies;
veth_wake_queues(cnx);
}
spin_unlock_irqrestore(&cnx->lock, flags);
break;
case VETH_EVENT_FRAMES:
veth_receive(cnx, event);
break;
default:
veth_error("Unknown interrupt type %d from LPAR %d.\n",
event->base_event.xSubtype, rlp);
};
}
static void veth_handle_event(struct HvLpEvent *event, struct pt_regs *regs)
{
struct veth_lpevent *veth_event = (struct veth_lpevent *)event;
if (event->xFlags.xFunction == HvLpEvent_Function_Ack)
veth_handle_ack(veth_event);
else if (event->xFlags.xFunction == HvLpEvent_Function_Int)
veth_handle_int(veth_event);
}
static int veth_process_caps(struct veth_lpar_connection *cnx)
{
struct veth_cap_data *remote_caps = &cnx->remote_caps;
int num_acks_needed;
/* Convert timer to jiffies */
cnx->ack_timeout = remote_caps->ack_timeout * HZ / 1000000;
if ( (remote_caps->num_buffers == 0)
|| (remote_caps->ack_threshold > VETH_MAX_ACKS_PER_MSG)
|| (remote_caps->ack_threshold == 0)
|| (cnx->ack_timeout == 0) ) {
veth_error("Received incompatible capabilities from LPAR %d.\n",
cnx->remote_lp);
return HvLpEvent_Rc_InvalidSubtypeData;
}
num_acks_needed = (remote_caps->num_buffers
/ remote_caps->ack_threshold) + 1;
/* FIXME: locking on num_ack_events? */
if (cnx->num_ack_events < num_acks_needed) {
int num;
num = veth_allocate_events(cnx->remote_lp,
num_acks_needed-cnx->num_ack_events);
if (num > 0)
cnx->num_ack_events += num;
if (cnx->num_ack_events < num_acks_needed) {
veth_error("Couldn't allocate enough ack events "
"for LPAR %d.\n", cnx->remote_lp);
return HvLpEvent_Rc_BufferNotAvailable;
}
}
return HvLpEvent_Rc_Good;
}
/* FIXME: The gotos here are a bit dubious */
static void veth_statemachine(void *p)
{
struct veth_lpar_connection *cnx = (struct veth_lpar_connection *)p;
int rlp = cnx->remote_lp;
int rc;
spin_lock_irq(&cnx->lock);
restart:
if (cnx->state & VETH_STATE_RESET) {
if (cnx->state & VETH_STATE_OPEN)
HvCallEvent_closeLpEventPath(cnx->remote_lp,
HvLpEvent_Type_VirtualLan);
/*
* Reset ack data. This prevents the ack_timer actually
* doing anything, even if it runs one more time when
* we drop the lock below.
*/
memset(&cnx->pending_acks, 0xff, sizeof (cnx->pending_acks));
cnx->num_pending_acks = 0;
cnx->state &= ~(VETH_STATE_RESET | VETH_STATE_SENTMON
| VETH_STATE_OPEN | VETH_STATE_SENTCAPS
| VETH_STATE_GOTCAPACK | VETH_STATE_GOTCAPS
| VETH_STATE_SENTCAPACK | VETH_STATE_READY);
/* Clean up any leftover messages */
if (cnx->msgs) {
int i;
for (i = 0; i < VETH_NUMBUFFERS; ++i)
veth_recycle_msg(cnx, cnx->msgs + i);
}
cnx->outstanding_tx = 0;
veth_wake_queues(cnx);
/* Drop the lock so we can do stuff that might sleep or
* take other locks. */
spin_unlock_irq(&cnx->lock);
del_timer_sync(&cnx->ack_timer);
del_timer_sync(&cnx->reset_timer);
spin_lock_irq(&cnx->lock);
if (cnx->state & VETH_STATE_RESET)
goto restart;
/* Hack, wait for the other end to reset itself. */
if (! (cnx->state & VETH_STATE_SHUTDOWN)) {
schedule_delayed_work(&cnx->statemachine_wq, 5 * HZ);
goto out;
}
}
if (cnx->state & VETH_STATE_SHUTDOWN)
/* It's all over, do nothing */
goto out;
if ( !(cnx->state & VETH_STATE_OPEN) ) {
if (! cnx->msgs || (cnx->num_events < (2 + VETH_NUMBUFFERS)) )
goto cant_cope;
HvCallEvent_openLpEventPath(rlp, HvLpEvent_Type_VirtualLan);
cnx->src_inst =
HvCallEvent_getSourceLpInstanceId(rlp,
HvLpEvent_Type_VirtualLan);
cnx->dst_inst =
HvCallEvent_getTargetLpInstanceId(rlp,
HvLpEvent_Type_VirtualLan);
cnx->state |= VETH_STATE_OPEN;
}
if ( (cnx->state & VETH_STATE_OPEN)
&& !(cnx->state & VETH_STATE_SENTMON) ) {
rc = veth_signalevent(cnx, VETH_EVENT_MONITOR,
HvLpEvent_AckInd_DoAck,
HvLpEvent_AckType_DeferredAck,
0, 0, 0, 0, 0, 0);
if (rc == HvLpEvent_Rc_Good) {
cnx->state |= VETH_STATE_SENTMON;
} else {
if ( (rc != HvLpEvent_Rc_PartitionDead)
&& (rc != HvLpEvent_Rc_PathClosed) )
veth_error("Error sending monitor to LPAR %d, "
"rc = %d\n", rlp, rc);
/* Oh well, hope we get a cap from the other
* end and do better when that kicks us */
goto out;
}
}
if ( (cnx->state & VETH_STATE_OPEN)
&& !(cnx->state & VETH_STATE_SENTCAPS)) {
u64 *rawcap = (u64 *)&cnx->local_caps;
rc = veth_signalevent(cnx, VETH_EVENT_CAP,
HvLpEvent_AckInd_DoAck,
HvLpEvent_AckType_ImmediateAck,
0, rawcap[0], rawcap[1], rawcap[2],
rawcap[3], rawcap[4]);
if (rc == HvLpEvent_Rc_Good) {
cnx->state |= VETH_STATE_SENTCAPS;
} else {
if ( (rc != HvLpEvent_Rc_PartitionDead)
&& (rc != HvLpEvent_Rc_PathClosed) )
veth_error("Error sending caps to LPAR %d, "
"rc = %d\n", rlp, rc);
/* Oh well, hope we get a cap from the other
* end and do better when that kicks us */
goto out;
}
}
if ((cnx->state & VETH_STATE_GOTCAPS)
&& !(cnx->state & VETH_STATE_SENTCAPACK)) {
struct veth_cap_data *remote_caps = &cnx->remote_caps;
memcpy(remote_caps, &cnx->cap_event.u.caps_data,
sizeof(*remote_caps));
spin_unlock_irq(&cnx->lock);
rc = veth_process_caps(cnx);
spin_lock_irq(&cnx->lock);
/* We dropped the lock, so recheck for anything which
* might mess us up */
if (cnx->state & (VETH_STATE_RESET|VETH_STATE_SHUTDOWN))
goto restart;
cnx->cap_event.base_event.xRc = rc;
HvCallEvent_ackLpEvent((struct HvLpEvent *)&cnx->cap_event);
if (rc == HvLpEvent_Rc_Good)
cnx->state |= VETH_STATE_SENTCAPACK;
else
goto cant_cope;
}
if ((cnx->state & VETH_STATE_GOTCAPACK)
&& (cnx->state & VETH_STATE_GOTCAPS)
&& !(cnx->state & VETH_STATE_READY)) {
if (cnx->cap_ack_event.base_event.xRc == HvLpEvent_Rc_Good) {
/* Start the ACK timer */
cnx->ack_timer.expires = jiffies + cnx->ack_timeout;
add_timer(&cnx->ack_timer);
cnx->state |= VETH_STATE_READY;
} else {
veth_error("Caps rejected by LPAR %d, rc = %d\n",
rlp, cnx->cap_ack_event.base_event.xRc);
goto cant_cope;
}
}
out:
spin_unlock_irq(&cnx->lock);
return;
cant_cope:
/* FIXME: we get here if something happens we really can't
* cope with. The link will never work once we get here, and
* all we can do is not lock the rest of the system up */
veth_error("Unrecoverable error on connection to LPAR %d, shutting down"
" (state = 0x%04lx)\n", rlp, cnx->state);
cnx->state |= VETH_STATE_SHUTDOWN;
spin_unlock_irq(&cnx->lock);
}
static int veth_init_connection(u8 rlp)
{
struct veth_lpar_connection *cnx;
struct veth_msg *msgs;
int i, rc;
if ( (rlp == this_lp)
|| ! HvLpConfig_doLpsCommunicateOnVirtualLan(this_lp, rlp) )
return 0;
cnx = kmalloc(sizeof(*cnx), GFP_KERNEL);
if (! cnx)
return -ENOMEM;
memset(cnx, 0, sizeof(*cnx));
cnx->remote_lp = rlp;
spin_lock_init(&cnx->lock);
INIT_WORK(&cnx->statemachine_wq, veth_statemachine, cnx);
init_timer(&cnx->ack_timer);
cnx->ack_timer.function = veth_timed_ack;
cnx->ack_timer.data = (unsigned long) cnx;
init_timer(&cnx->reset_timer);
cnx->reset_timer.function = veth_timed_reset;
cnx->reset_timer.data = (unsigned long) cnx;
cnx->reset_timeout = 5 * HZ * (VETH_ACKTIMEOUT / 1000000);
memset(&cnx->pending_acks, 0xff, sizeof (cnx->pending_acks));
veth_cnx[rlp] = cnx;
/* This gets us 1 reference, which is held on behalf of the driver
* infrastructure. It's released at module unload. */
kobject_init(&cnx->kobject);
cnx->kobject.ktype = &veth_lpar_connection_ktype;
rc = kobject_set_name(&cnx->kobject, "cnx%.2d", rlp);
if (rc != 0)
return rc;
msgs = kmalloc(VETH_NUMBUFFERS * sizeof(struct veth_msg), GFP_KERNEL);
if (! msgs) {
veth_error("Can't allocate buffers for LPAR %d.\n", rlp);
return -ENOMEM;
}
cnx->msgs = msgs;
memset(msgs, 0, VETH_NUMBUFFERS * sizeof(struct veth_msg));
for (i = 0; i < VETH_NUMBUFFERS; i++) {
msgs[i].token = i;
veth_stack_push(cnx, msgs + i);
}
cnx->num_events = veth_allocate_events(rlp, 2 + VETH_NUMBUFFERS);
if (cnx->num_events < (2 + VETH_NUMBUFFERS)) {
veth_error("Can't allocate enough events for LPAR %d.\n", rlp);
return -ENOMEM;
}
cnx->local_caps.num_buffers = VETH_NUMBUFFERS;
cnx->local_caps.ack_threshold = ACK_THRESHOLD;
cnx->local_caps.ack_timeout = VETH_ACKTIMEOUT;
return 0;
}
static void veth_stop_connection(struct veth_lpar_connection *cnx)
{
if (!cnx)
return;
spin_lock_irq(&cnx->lock);
cnx->state |= VETH_STATE_RESET | VETH_STATE_SHUTDOWN;
veth_kick_statemachine(cnx);
spin_unlock_irq(&cnx->lock);
/* There's a slim chance the reset code has just queued the
* statemachine to run in five seconds. If so we need to cancel
* that and requeue the work to run now. */
if (cancel_delayed_work(&cnx->statemachine_wq)) {
spin_lock_irq(&cnx->lock);
veth_kick_statemachine(cnx);
spin_unlock_irq(&cnx->lock);
}
/* Wait for the state machine to run. */
flush_scheduled_work();
}
static void veth_destroy_connection(struct veth_lpar_connection *cnx)
{
if (!cnx)
return;
if (cnx->num_events > 0)
mf_deallocate_lp_events(cnx->remote_lp,
HvLpEvent_Type_VirtualLan,
cnx->num_events,
NULL, NULL);
if (cnx->num_ack_events > 0)
mf_deallocate_lp_events(cnx->remote_lp,
HvLpEvent_Type_VirtualLan,
cnx->num_ack_events,
NULL, NULL);
kfree(cnx->msgs);
veth_cnx[cnx->remote_lp] = NULL;
kfree(cnx);
}
static void veth_release_connection(struct kobject *kobj)
{
struct veth_lpar_connection *cnx;
cnx = container_of(kobj, struct veth_lpar_connection, kobject);
veth_stop_connection(cnx);
veth_destroy_connection(cnx);
}
/*
* net_device code
*/
static int veth_open(struct net_device *dev)
{
struct veth_port *port = (struct veth_port *) dev->priv;
memset(&port->stats, 0, sizeof (port->stats));
netif_start_queue(dev);
return 0;
}
static int veth_close(struct net_device *dev)
{
netif_stop_queue(dev);
return 0;
}
static struct net_device_stats *veth_get_stats(struct net_device *dev)
{
struct veth_port *port = (struct veth_port *) dev->priv;
return &port->stats;
}
static int veth_change_mtu(struct net_device *dev, int new_mtu)
{
if ((new_mtu < 68) || (new_mtu > VETH_MAX_MTU))
return -EINVAL;
dev->mtu = new_mtu;
return 0;
}
static void veth_set_multicast_list(struct net_device *dev)
{
struct veth_port *port = (struct veth_port *) dev->priv;
unsigned long flags;
write_lock_irqsave(&port->mcast_gate, flags);
if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
(dev->mc_count > VETH_MAX_MCAST)) {
port->promiscuous = 1;
} else {
struct dev_mc_list *dmi = dev->mc_list;
int i;
port->promiscuous = 0;
/* Update table */
port->num_mcast = 0;
for (i = 0; i < dev->mc_count; i++) {
u8 *addr = dmi->dmi_addr;
u64 xaddr = 0;
if (addr[0] & 0x01) {/* multicast address? */
memcpy(&xaddr, addr, ETH_ALEN);
port->mcast_addr[port->num_mcast] = xaddr;
port->num_mcast++;
}
dmi = dmi->next;
}
}
write_unlock_irqrestore(&port->mcast_gate, flags);