aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/iseries_veth.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/net/iseries_veth.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'drivers/net/iseries_veth.c')
-rw-r--r--drivers/net/iseries_veth.c1422
1 files changed, 1422 insertions, 0 deletions
diff --git a/drivers/net/iseries_veth.c b/drivers/net/iseries_veth.c
new file mode 100644
index 000000000000..855f8b2cf13b
--- /dev/null
+++ b/drivers/net/iseries_veth.c
@@ -0,0 +1,1422 @@
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#include "iseries_veth.h"
83
84MODULE_AUTHOR("Kyle Lucke <klucke@us.ibm.com>");
85MODULE_DESCRIPTION("iSeries Virtual ethernet driver");
86MODULE_LICENSE("GPL");
87
88#define VETH_NUMBUFFERS (120)
89#define VETH_ACKTIMEOUT (1000000) /* microseconds */
90#define VETH_MAX_MCAST (12)
91
92#define VETH_MAX_MTU (9000)
93
94#if VETH_NUMBUFFERS < 10
95#define ACK_THRESHOLD (1)
96#elif VETH_NUMBUFFERS < 20
97#define ACK_THRESHOLD (4)
98#elif VETH_NUMBUFFERS < 40
99#define ACK_THRESHOLD (10)
100#else
101#define ACK_THRESHOLD (20)
102#endif
103
104#define VETH_STATE_SHUTDOWN (0x0001)
105#define VETH_STATE_OPEN (0x0002)
106#define VETH_STATE_RESET (0x0004)
107#define VETH_STATE_SENTMON (0x0008)
108#define VETH_STATE_SENTCAPS (0x0010)
109#define VETH_STATE_GOTCAPACK (0x0020)
110#define VETH_STATE_GOTCAPS (0x0040)
111#define VETH_STATE_SENTCAPACK (0x0080)
112#define VETH_STATE_READY (0x0100)
113
114struct veth_msg {
115 struct veth_msg *next;
116 struct VethFramesData data;
117 int token;
118 unsigned long in_use;
119 struct sk_buff *skb;
120 struct device *dev;
121};
122
123struct veth_lpar_connection {
124 HvLpIndex remote_lp;
125 struct work_struct statemachine_wq;
126 struct veth_msg *msgs;
127 int num_events;
128 struct VethCapData local_caps;
129
130 struct timer_list ack_timer;
131
132 spinlock_t lock;
133 unsigned long state;
134 HvLpInstanceId src_inst;
135 HvLpInstanceId dst_inst;
136 struct VethLpEvent cap_event, cap_ack_event;
137 u16 pending_acks[VETH_MAX_ACKS_PER_MSG];
138 u32 num_pending_acks;
139
140 int num_ack_events;
141 struct VethCapData remote_caps;
142 u32 ack_timeout;
143
144 spinlock_t msg_stack_lock;
145 struct veth_msg *msg_stack_head;
146};
147
148struct veth_port {
149 struct device *dev;
150 struct net_device_stats stats;
151 u64 mac_addr;
152 HvLpIndexMap lpar_map;
153
154 spinlock_t pending_gate;
155 struct sk_buff *pending_skb;
156 HvLpIndexMap pending_lpmask;
157
158 rwlock_t mcast_gate;
159 int promiscuous;
160 int all_mcast;
161 int num_mcast;
162 u64 mcast_addr[VETH_MAX_MCAST];
163};
164
165static HvLpIndex this_lp;
166static struct veth_lpar_connection *veth_cnx[HVMAXARCHITECTEDLPS]; /* = 0 */
167static struct net_device *veth_dev[HVMAXARCHITECTEDVIRTUALLANS]; /* = 0 */
168
169static int veth_start_xmit(struct sk_buff *skb, struct net_device *dev);
170static void veth_recycle_msg(struct veth_lpar_connection *, struct veth_msg *);
171static void veth_flush_pending(struct veth_lpar_connection *cnx);
172static void veth_receive(struct veth_lpar_connection *, struct VethLpEvent *);
173static void veth_timed_ack(unsigned long connectionPtr);
174
175/*
176 * Utility functions
177 */
178
179#define veth_printk(prio, fmt, args...) \
180 printk(prio "%s: " fmt, __FILE__, ## args)
181
182#define veth_error(fmt, args...) \
183 printk(KERN_ERR "(%s:%3.3d) ERROR: " fmt, __FILE__, __LINE__ , ## args)
184
185static inline void veth_stack_push(struct veth_lpar_connection *cnx,
186 struct veth_msg *msg)
187{
188 unsigned long flags;
189
190 spin_lock_irqsave(&cnx->msg_stack_lock, flags);
191 msg->next = cnx->msg_stack_head;
192 cnx->msg_stack_head = msg;
193 spin_unlock_irqrestore(&cnx->msg_stack_lock, flags);
194}
195
196static inline struct veth_msg *veth_stack_pop(struct veth_lpar_connection *cnx)
197{
198 unsigned long flags;
199 struct veth_msg *msg;
200
201 spin_lock_irqsave(&cnx->msg_stack_lock, flags);
202 msg = cnx->msg_stack_head;
203 if (msg)
204 cnx->msg_stack_head = cnx->msg_stack_head->next;
205 spin_unlock_irqrestore(&cnx->msg_stack_lock, flags);
206 return msg;
207}
208
209static inline HvLpEvent_Rc
210veth_signalevent(struct veth_lpar_connection *cnx, u16 subtype,
211 HvLpEvent_AckInd ackind, HvLpEvent_AckType acktype,
212 u64 token,
213 u64 data1, u64 data2, u64 data3, u64 data4, u64 data5)
214{
215 return HvCallEvent_signalLpEventFast(cnx->remote_lp,
216 HvLpEvent_Type_VirtualLan,
217 subtype, ackind, acktype,
218 cnx->src_inst,
219 cnx->dst_inst,
220 token, data1, data2, data3,
221 data4, data5);
222}
223
224static inline HvLpEvent_Rc veth_signaldata(struct veth_lpar_connection *cnx,
225 u16 subtype, u64 token, void *data)
226{
227 u64 *p = (u64 *) data;
228
229 return veth_signalevent(cnx, subtype, HvLpEvent_AckInd_NoAck,
230 HvLpEvent_AckType_ImmediateAck,
231 token, p[0], p[1], p[2], p[3], p[4]);
232}
233
234struct veth_allocation {
235 struct completion c;
236 int num;
237};
238
239static void veth_complete_allocation(void *parm, int number)
240{
241 struct veth_allocation *vc = (struct veth_allocation *)parm;
242
243 vc->num = number;
244 complete(&vc->c);
245}
246
247static int veth_allocate_events(HvLpIndex rlp, int number)
248{
249 struct veth_allocation vc = { COMPLETION_INITIALIZER(vc.c), 0 };
250
251 mf_allocate_lp_events(rlp, HvLpEvent_Type_VirtualLan,
252 sizeof(struct VethLpEvent), number,
253 &veth_complete_allocation, &vc);
254 wait_for_completion(&vc.c);
255
256 return vc.num;
257}
258
259/*
260 * LPAR connection code
261 */
262
263static inline void veth_kick_statemachine(struct veth_lpar_connection *cnx)
264{
265 schedule_work(&cnx->statemachine_wq);
266}
267
268static void veth_take_cap(struct veth_lpar_connection *cnx,
269 struct VethLpEvent *event)
270{
271 unsigned long flags;
272
273 spin_lock_irqsave(&cnx->lock, flags);
274 /* Receiving caps may mean the other end has just come up, so
275 * we need to reload the instance ID of the far end */
276 cnx->dst_inst =
277 HvCallEvent_getTargetLpInstanceId(cnx->remote_lp,
278 HvLpEvent_Type_VirtualLan);
279
280 if (cnx->state & VETH_STATE_GOTCAPS) {
281 veth_error("Received a second capabilities from lpar %d\n",
282 cnx->remote_lp);
283 event->base_event.xRc = HvLpEvent_Rc_BufferNotAvailable;
284 HvCallEvent_ackLpEvent((struct HvLpEvent *) event);
285 } else {
286 memcpy(&cnx->cap_event, event, sizeof(cnx->cap_event));
287 cnx->state |= VETH_STATE_GOTCAPS;
288 veth_kick_statemachine(cnx);
289 }
290 spin_unlock_irqrestore(&cnx->lock, flags);
291}
292
293static void veth_take_cap_ack(struct veth_lpar_connection *cnx,
294 struct VethLpEvent *event)
295{
296 unsigned long flags;
297
298 spin_lock_irqsave(&cnx->lock, flags);
299 if (cnx->state & VETH_STATE_GOTCAPACK) {
300 veth_error("Received a second capabilities ack from lpar %d\n",
301 cnx->remote_lp);
302 } else {
303 memcpy(&cnx->cap_ack_event, event,
304 sizeof(&cnx->cap_ack_event));
305 cnx->state |= VETH_STATE_GOTCAPACK;
306 veth_kick_statemachine(cnx);
307 }
308 spin_unlock_irqrestore(&cnx->lock, flags);
309}
310
311static void veth_take_monitor_ack(struct veth_lpar_connection *cnx,
312 struct VethLpEvent *event)
313{
314 unsigned long flags;
315
316 spin_lock_irqsave(&cnx->lock, flags);
317 veth_printk(KERN_DEBUG, "Monitor ack returned for lpar %d\n",
318 cnx->remote_lp);
319 cnx->state |= VETH_STATE_RESET;
320 veth_kick_statemachine(cnx);
321 spin_unlock_irqrestore(&cnx->lock, flags);
322}
323
324static void veth_handle_ack(struct VethLpEvent *event)
325{
326 HvLpIndex rlp = event->base_event.xTargetLp;
327 struct veth_lpar_connection *cnx = veth_cnx[rlp];
328
329 BUG_ON(! cnx);
330
331 switch (event->base_event.xSubtype) {
332 case VethEventTypeCap:
333 veth_take_cap_ack(cnx, event);
334 break;
335 case VethEventTypeMonitor:
336 veth_take_monitor_ack(cnx, event);
337 break;
338 default:
339 veth_error("Unknown ack type %d from lpar %d\n",
340 event->base_event.xSubtype, rlp);
341 };
342}
343
344static void veth_handle_int(struct VethLpEvent *event)
345{
346 HvLpIndex rlp = event->base_event.xSourceLp;
347 struct veth_lpar_connection *cnx = veth_cnx[rlp];
348 unsigned long flags;
349 int i;
350
351 BUG_ON(! cnx);
352
353 switch (event->base_event.xSubtype) {
354 case VethEventTypeCap:
355 veth_take_cap(cnx, event);
356 break;
357 case VethEventTypeMonitor:
358 /* do nothing... this'll hang out here til we're dead,
359 * and the hypervisor will return it for us. */
360 break;
361 case VethEventTypeFramesAck:
362 spin_lock_irqsave(&cnx->lock, flags);
363 for (i = 0; i < VETH_MAX_ACKS_PER_MSG; ++i) {
364 u16 msgnum = event->u.frames_ack_data.token[i];
365
366 if (msgnum < VETH_NUMBUFFERS)
367 veth_recycle_msg(cnx, cnx->msgs + msgnum);
368 }
369 spin_unlock_irqrestore(&cnx->lock, flags);
370 veth_flush_pending(cnx);
371 break;
372 case VethEventTypeFrames:
373 veth_receive(cnx, event);
374 break;
375 default:
376 veth_error("Unknown interrupt type %d from lpar %d\n",
377 event->base_event.xSubtype, rlp);
378 };
379}
380
381static void veth_handle_event(struct HvLpEvent *event, struct pt_regs *regs)
382{
383 struct VethLpEvent *veth_event = (struct VethLpEvent *)event;
384
385 if (event->xFlags.xFunction == HvLpEvent_Function_Ack)
386 veth_handle_ack(veth_event);
387 else if (event->xFlags.xFunction == HvLpEvent_Function_Int)
388 veth_handle_int(veth_event);
389}
390
391static int veth_process_caps(struct veth_lpar_connection *cnx)
392{
393 struct VethCapData *remote_caps = &cnx->remote_caps;
394 int num_acks_needed;
395
396 /* Convert timer to jiffies */
397 cnx->ack_timeout = remote_caps->ack_timeout * HZ / 1000000;
398
399 if ( (remote_caps->num_buffers == 0)
400 || (remote_caps->ack_threshold > VETH_MAX_ACKS_PER_MSG)
401 || (remote_caps->ack_threshold == 0)
402 || (cnx->ack_timeout == 0) ) {
403 veth_error("Received incompatible capabilities from lpar %d\n",
404 cnx->remote_lp);
405 return HvLpEvent_Rc_InvalidSubtypeData;
406 }
407
408 num_acks_needed = (remote_caps->num_buffers
409 / remote_caps->ack_threshold) + 1;
410
411 /* FIXME: locking on num_ack_events? */
412 if (cnx->num_ack_events < num_acks_needed) {
413 int num;
414
415 num = veth_allocate_events(cnx->remote_lp,
416 num_acks_needed-cnx->num_ack_events);
417 if (num > 0)
418 cnx->num_ack_events += num;
419
420 if (cnx->num_ack_events < num_acks_needed) {
421 veth_error("Couldn't allocate enough ack events for lpar %d\n",
422 cnx->remote_lp);
423
424 return HvLpEvent_Rc_BufferNotAvailable;
425 }
426 }
427
428
429 return HvLpEvent_Rc_Good;
430}
431
432/* FIXME: The gotos here are a bit dubious */
433static void veth_statemachine(void *p)
434{
435 struct veth_lpar_connection *cnx = (struct veth_lpar_connection *)p;
436 int rlp = cnx->remote_lp;
437 int rc;
438
439 spin_lock_irq(&cnx->lock);
440
441 restart:
442 if (cnx->state & VETH_STATE_RESET) {
443 int i;
444
445 del_timer(&cnx->ack_timer);
446
447 if (cnx->state & VETH_STATE_OPEN)
448 HvCallEvent_closeLpEventPath(cnx->remote_lp,
449 HvLpEvent_Type_VirtualLan);
450
451 /* reset ack data */
452 memset(&cnx->pending_acks, 0xff, sizeof (cnx->pending_acks));
453 cnx->num_pending_acks = 0;
454
455 cnx->state &= ~(VETH_STATE_RESET | VETH_STATE_SENTMON
456 | VETH_STATE_OPEN | VETH_STATE_SENTCAPS
457 | VETH_STATE_GOTCAPACK | VETH_STATE_GOTCAPS
458 | VETH_STATE_SENTCAPACK | VETH_STATE_READY);
459
460 /* Clean up any leftover messages */
461 if (cnx->msgs)
462 for (i = 0; i < VETH_NUMBUFFERS; ++i)
463 veth_recycle_msg(cnx, cnx->msgs + i);
464 spin_unlock_irq(&cnx->lock);
465 veth_flush_pending(cnx);
466 spin_lock_irq(&cnx->lock);
467 if (cnx->state & VETH_STATE_RESET)
468 goto restart;
469 }
470
471 if (cnx->state & VETH_STATE_SHUTDOWN)
472 /* It's all over, do nothing */
473 goto out;
474
475 if ( !(cnx->state & VETH_STATE_OPEN) ) {
476 if (! cnx->msgs || (cnx->num_events < (2 + VETH_NUMBUFFERS)) )
477 goto cant_cope;
478
479 HvCallEvent_openLpEventPath(rlp, HvLpEvent_Type_VirtualLan);
480 cnx->src_inst =
481 HvCallEvent_getSourceLpInstanceId(rlp,
482 HvLpEvent_Type_VirtualLan);
483 cnx->dst_inst =
484 HvCallEvent_getTargetLpInstanceId(rlp,
485 HvLpEvent_Type_VirtualLan);
486 cnx->state |= VETH_STATE_OPEN;
487 }
488
489 if ( (cnx->state & VETH_STATE_OPEN)
490 && !(cnx->state & VETH_STATE_SENTMON) ) {
491 rc = veth_signalevent(cnx, VethEventTypeMonitor,
492 HvLpEvent_AckInd_DoAck,
493 HvLpEvent_AckType_DeferredAck,
494 0, 0, 0, 0, 0, 0);
495
496 if (rc == HvLpEvent_Rc_Good) {
497 cnx->state |= VETH_STATE_SENTMON;
498 } else {
499 if ( (rc != HvLpEvent_Rc_PartitionDead)
500 && (rc != HvLpEvent_Rc_PathClosed) )
501 veth_error("Error sending monitor to "
502 "lpar %d, rc=%x\n",
503 rlp, (int) rc);
504
505 /* Oh well, hope we get a cap from the other
506 * end and do better when that kicks us */
507 goto out;
508 }
509 }
510
511 if ( (cnx->state & VETH_STATE_OPEN)
512 && !(cnx->state & VETH_STATE_SENTCAPS)) {
513 u64 *rawcap = (u64 *)&cnx->local_caps;
514
515 rc = veth_signalevent(cnx, VethEventTypeCap,
516 HvLpEvent_AckInd_DoAck,
517 HvLpEvent_AckType_ImmediateAck,
518 0, rawcap[0], rawcap[1], rawcap[2],
519 rawcap[3], rawcap[4]);
520
521 if (rc == HvLpEvent_Rc_Good) {
522 cnx->state |= VETH_STATE_SENTCAPS;
523 } else {
524 if ( (rc != HvLpEvent_Rc_PartitionDead)
525 && (rc != HvLpEvent_Rc_PathClosed) )
526 veth_error("Error sending caps to "
527 "lpar %d, rc=%x\n",
528 rlp, (int) rc);
529 /* Oh well, hope we get a cap from the other
530 * end and do better when that kicks us */
531 goto out;
532 }
533 }
534
535 if ((cnx->state & VETH_STATE_GOTCAPS)
536 && !(cnx->state & VETH_STATE_SENTCAPACK)) {
537 struct VethCapData *remote_caps = &cnx->remote_caps;
538
539 memcpy(remote_caps, &cnx->cap_event.u.caps_data,
540 sizeof(*remote_caps));
541
542 spin_unlock_irq(&cnx->lock);
543 rc = veth_process_caps(cnx);
544 spin_lock_irq(&cnx->lock);
545
546 /* We dropped the lock, so recheck for anything which
547 * might mess us up */
548 if (cnx->state & (VETH_STATE_RESET|VETH_STATE_SHUTDOWN))
549 goto restart;
550
551 cnx->cap_event.base_event.xRc = rc;
552 HvCallEvent_ackLpEvent((struct HvLpEvent *)&cnx->cap_event);
553 if (rc == HvLpEvent_Rc_Good)
554 cnx->state |= VETH_STATE_SENTCAPACK;
555 else
556 goto cant_cope;
557 }
558
559 if ((cnx->state & VETH_STATE_GOTCAPACK)
560 && (cnx->state & VETH_STATE_GOTCAPS)
561 && !(cnx->state & VETH_STATE_READY)) {
562 if (cnx->cap_ack_event.base_event.xRc == HvLpEvent_Rc_Good) {
563 /* Start the ACK timer */
564 cnx->ack_timer.expires = jiffies + cnx->ack_timeout;
565 add_timer(&cnx->ack_timer);
566 cnx->state |= VETH_STATE_READY;
567 } else {
568 veth_printk(KERN_ERR, "Caps rejected (rc=%d) by "
569 "lpar %d\n",
570 cnx->cap_ack_event.base_event.xRc,
571 rlp);
572 goto cant_cope;
573 }
574 }
575
576 out:
577 spin_unlock_irq(&cnx->lock);
578 return;
579
580 cant_cope:
581 /* FIXME: we get here if something happens we really can't
582 * cope with. The link will never work once we get here, and
583 * all we can do is not lock the rest of the system up */
584 veth_error("Badness on connection to lpar %d (state=%04lx) "
585 " - shutting down\n", rlp, cnx->state);
586 cnx->state |= VETH_STATE_SHUTDOWN;
587 spin_unlock_irq(&cnx->lock);
588}
589
590static int veth_init_connection(u8 rlp)
591{
592 struct veth_lpar_connection *cnx;
593 struct veth_msg *msgs;
594 int i;
595
596 if ( (rlp == this_lp)
597 || ! HvLpConfig_doLpsCommunicateOnVirtualLan(this_lp, rlp) )
598 return 0;
599
600 cnx = kmalloc(sizeof(*cnx), GFP_KERNEL);
601 if (! cnx)
602 return -ENOMEM;
603 memset(cnx, 0, sizeof(*cnx));
604
605 cnx->remote_lp = rlp;
606 spin_lock_init(&cnx->lock);
607 INIT_WORK(&cnx->statemachine_wq, veth_statemachine, cnx);
608 init_timer(&cnx->ack_timer);
609 cnx->ack_timer.function = veth_timed_ack;
610 cnx->ack_timer.data = (unsigned long) cnx;
611 memset(&cnx->pending_acks, 0xff, sizeof (cnx->pending_acks));
612
613 veth_cnx[rlp] = cnx;
614
615 msgs = kmalloc(VETH_NUMBUFFERS * sizeof(struct veth_msg), GFP_KERNEL);
616 if (! msgs) {
617 veth_error("Can't allocate buffers for lpar %d\n", rlp);
618 return -ENOMEM;
619 }
620
621 cnx->msgs = msgs;
622 memset(msgs, 0, VETH_NUMBUFFERS * sizeof(struct veth_msg));
623 spin_lock_init(&cnx->msg_stack_lock);
624
625 for (i = 0; i < VETH_NUMBUFFERS; i++) {
626 msgs[i].token = i;
627 veth_stack_push(cnx, msgs + i);
628 }
629
630 cnx->num_events = veth_allocate_events(rlp, 2 + VETH_NUMBUFFERS);
631
632 if (cnx->num_events < (2 + VETH_NUMBUFFERS)) {
633 veth_error("Can't allocate events for lpar %d, only got %d\n",
634 rlp, cnx->num_events);
635 return -ENOMEM;
636 }
637
638 cnx->local_caps.num_buffers = VETH_NUMBUFFERS;
639 cnx->local_caps.ack_threshold = ACK_THRESHOLD;
640 cnx->local_caps.ack_timeout = VETH_ACKTIMEOUT;
641
642 return 0;
643}
644
645static void veth_stop_connection(u8 rlp)
646{
647 struct veth_lpar_connection *cnx = veth_cnx[rlp];
648
649 if (! cnx)
650 return;
651
652 spin_lock_irq(&cnx->lock);
653 cnx->state |= VETH_STATE_RESET | VETH_STATE_SHUTDOWN;
654 veth_kick_statemachine(cnx);
655 spin_unlock_irq(&cnx->lock);
656
657 flush_scheduled_work();
658
659 /* FIXME: not sure if this is necessary - will already have
660 * been deleted by the state machine, just want to make sure
661 * its not running any more */
662 del_timer_sync(&cnx->ack_timer);
663
664 if (cnx->num_events > 0)
665 mf_deallocate_lp_events(cnx->remote_lp,
666 HvLpEvent_Type_VirtualLan,
667 cnx->num_events,
668 NULL, NULL);
669 if (cnx->num_ack_events > 0)
670 mf_deallocate_lp_events(cnx->remote_lp,
671 HvLpEvent_Type_VirtualLan,
672 cnx->num_ack_events,
673 NULL, NULL);
674}
675
676static void veth_destroy_connection(u8 rlp)
677{
678 struct veth_lpar_connection *cnx = veth_cnx[rlp];
679
680 if (! cnx)
681 return;
682
683 kfree(cnx->msgs);
684 kfree(cnx);
685 veth_cnx[rlp] = NULL;
686}
687
688/*
689 * net_device code
690 */
691
692static int veth_open(struct net_device *dev)
693{
694 struct veth_port *port = (struct veth_port *) dev->priv;
695
696 memset(&port->stats, 0, sizeof (port->stats));
697 netif_start_queue(dev);
698 return 0;
699}
700
701static int veth_close(struct net_device *dev)
702{
703 netif_stop_queue(dev);
704 return 0;
705}
706
707static struct net_device_stats *veth_get_stats(struct net_device *dev)
708{
709 struct veth_port *port = (struct veth_port *) dev->priv;
710
711 return &port->stats;
712}
713
714static int veth_change_mtu(struct net_device *dev, int new_mtu)
715{
716 if ((new_mtu < 68) || (new_mtu > VETH_MAX_MTU))
717 return -EINVAL;
718 dev->mtu = new_mtu;
719 return 0;
720}
721
722static void veth_set_multicast_list(struct net_device *dev)
723{
724 struct veth_port *port = (struct veth_port *) dev->priv;
725 unsigned long flags;
726
727 write_lock_irqsave(&port->mcast_gate, flags);
728
729 if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
730 printk(KERN_INFO "%s: Promiscuous mode enabled.\n",
731 dev->name);
732 port->promiscuous = 1;
733 } else if ( (dev->flags & IFF_ALLMULTI)
734 || (dev->mc_count > VETH_MAX_MCAST) ) {
735 port->all_mcast = 1;
736 } else {
737 struct dev_mc_list *dmi = dev->mc_list;
738 int i;
739
740 /* Update table */
741 port->num_mcast = 0;
742
743 for (i = 0; i < dev->mc_count; i++) {
744 u8 *addr = dmi->dmi_addr;
745 u64 xaddr = 0;
746
747 if (addr[0] & 0x01) {/* multicast address? */
748 memcpy(&xaddr, addr, ETH_ALEN);
749 port->mcast_addr[port->num_mcast] = xaddr;
750 port->num_mcast++;
751 }
752 dmi = dmi->next;
753 }
754 }
755
756 write_unlock_irqrestore(&port->mcast_gate, flags);
757}
758
759static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
760{
761 strncpy(info->driver, "veth", sizeof(info->driver) - 1);
762 info->driver[sizeof(info->driver) - 1] = '\0';
763 strncpy(info->version, "1.0", sizeof(info->version) - 1);
764}
765
766static int veth_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
767{
768 ecmd->supported = (SUPPORTED_1000baseT_Full
769 | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
770 ecmd->advertising = (SUPPORTED_1000baseT_Full
771 | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
772 ecmd->port = PORT_FIBRE;
773 ecmd->transceiver = XCVR_INTERNAL;
774 ecmd->phy_address = 0;
775 ecmd->speed = SPEED_1000;
776 ecmd->duplex = DUPLEX_FULL;
777 ecmd->autoneg = AUTONEG_ENABLE;
778 ecmd->maxtxpkt = 120;
779 ecmd->maxrxpkt = 120;
780 return 0;
781}
782
783static u32 veth_get_link(struct net_device *dev)
784{
785 return 1;
786}
787
788static struct ethtool_ops ops = {
789 .get_drvinfo = veth_get_drvinfo,
790 .get_settings = veth_get_settings,
791 .get_link = veth_get_link,
792};
793
794static void veth_tx_timeout(struct net_device *dev)
795{
796 struct veth_port *port = (struct veth_port *)dev->priv;
797 struct net_device_stats *stats = &port->stats;
798 unsigned long flags;
799 int i;
800
801 stats->tx_errors++;
802
803 spin_lock_irqsave(&port->pending_gate, flags);
804
805 printk(KERN_WARNING "%s: Tx timeout! Resetting lp connections: %08x\n",
806 dev->name, port->pending_lpmask);
807
808 /* If we've timed out the queue must be stopped, which should
809 * only ever happen when there is a pending packet. */
810 WARN_ON(! port->pending_lpmask);
811
812 for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
813 struct veth_lpar_connection *cnx = veth_cnx[i];
814
815 if (! (port->pending_lpmask & (1<<i)))
816 continue;
817
818 /* If we're pending on it, we must be connected to it,
819 * so we should certainly have a structure for it. */
820 BUG_ON(! cnx);
821
822 /* Theoretically we could be kicking a connection
823 * which doesn't deserve it, but in practice if we've
824 * had a Tx timeout, the pending_lpmask will have
825 * exactly one bit set - the connection causing the
826 * problem. */
827 spin_lock(&cnx->lock);
828 cnx->state |= VETH_STATE_RESET;
829 veth_kick_statemachine(cnx);
830 spin_unlock(&cnx->lock);
831 }
832
833 spin_unlock_irqrestore(&port->pending_gate, flags);
834}
835
836static struct net_device * __init veth_probe_one(int vlan, struct device *vdev)
837{
838 struct net_device *dev;
839 struct veth_port *port;
840 int i, rc;
841
842 dev = alloc_etherdev(sizeof (struct veth_port));
843 if (! dev) {
844 veth_error("Unable to allocate net_device structure!\n");
845 return NULL;
846 }
847
848 port = (struct veth_port *) dev->priv;
849
850 spin_lock_init(&port->pending_gate);
851 rwlock_init(&port->mcast_gate);
852
853 for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
854 HvLpVirtualLanIndexMap map;
855
856 if (i == this_lp)
857 continue;
858 map = HvLpConfig_getVirtualLanIndexMapForLp(i);
859 if (map & (0x8000 >> vlan))
860 port->lpar_map |= (1 << i);
861 }
862 port->dev = vdev;
863
864 dev->dev_addr[0] = 0x02;
865 dev->dev_addr[1] = 0x01;
866 dev->dev_addr[2] = 0xff;
867 dev->dev_addr[3] = vlan;
868 dev->dev_addr[4] = 0xff;
869 dev->dev_addr[5] = this_lp;
870
871 dev->mtu = VETH_MAX_MTU;
872
873 memcpy(&port->mac_addr, dev->dev_addr, 6);
874
875 dev->open = veth_open;
876 dev->hard_start_xmit = veth_start_xmit;
877 dev->stop = veth_close;
878 dev->get_stats = veth_get_stats;
879 dev->change_mtu = veth_change_mtu;
880 dev->set_mac_address = NULL;
881 dev->set_multicast_list = veth_set_multicast_list;
882 SET_ETHTOOL_OPS(dev, &ops);
883
884 dev->watchdog_timeo = 2 * (VETH_ACKTIMEOUT * HZ / 1000000);
885 dev->tx_timeout = veth_tx_timeout;
886
887 SET_NETDEV_DEV(dev, vdev);
888
889 rc = register_netdev(dev);
890 if (rc != 0) {
891 veth_printk(KERN_ERR,
892 "Failed to register ethernet device for vlan %d\n",
893 vlan);
894 free_netdev(dev);
895 return NULL;
896 }
897
898 veth_printk(KERN_DEBUG, "%s attached to iSeries vlan %d (lpar_map=0x%04x)\n",
899 dev->name, vlan, port->lpar_map);
900
901 return dev;
902}
903
904/*
905 * Tx path
906 */
907
908static int veth_transmit_to_one(struct sk_buff *skb, HvLpIndex rlp,
909 struct net_device *dev)
910{
911 struct veth_lpar_connection *cnx = veth_cnx[rlp];
912 struct veth_port *port = (struct veth_port *) dev->priv;
913 HvLpEvent_Rc rc;
914 u32 dma_address, dma_length;
915 struct veth_msg *msg = NULL;
916 int err = 0;
917 unsigned long flags;
918
919 if (! cnx) {
920 port->stats.tx_errors++;
921 dev_kfree_skb(skb);
922 return 0;
923 }
924
925 spin_lock_irqsave(&cnx->lock, flags);
926
927 if (! cnx->state & VETH_STATE_READY)
928 goto drop;
929
930 if ((skb->len - 14) > VETH_MAX_MTU)
931 goto drop;
932
933 msg = veth_stack_pop(cnx);
934
935 if (! msg) {
936 err = 1;
937 goto drop;
938 }
939
940 dma_length = skb->len;
941 dma_address = dma_map_single(port->dev, skb->data,
942 dma_length, DMA_TO_DEVICE);
943
944 if (dma_mapping_error(dma_address))
945 goto recycle_and_drop;
946
947 /* Is it really necessary to check the length and address
948 * fields of the first entry here? */
949 msg->skb = skb;
950 msg->dev = port->dev;
951 msg->data.addr[0] = dma_address;
952 msg->data.len[0] = dma_length;
953 msg->data.eofmask = 1 << VETH_EOF_SHIFT;
954 set_bit(0, &(msg->in_use));
955 rc = veth_signaldata(cnx, VethEventTypeFrames, msg->token, &msg->data);
956
957 if (rc != HvLpEvent_Rc_Good)
958 goto recycle_and_drop;
959
960 spin_unlock_irqrestore(&cnx->lock, flags);
961 return 0;
962
963 recycle_and_drop:
964 msg->skb = NULL;
965 /* need to set in use to make veth_recycle_msg in case this
966 * was a mapping failure */
967 set_bit(0, &msg->in_use);
968 veth_recycle_msg(cnx, msg);
969 drop:
970 port->stats.tx_errors++;
971 dev_kfree_skb(skb);
972 spin_unlock_irqrestore(&cnx->lock, flags);
973 return err;
974}
975
976static HvLpIndexMap veth_transmit_to_many(struct sk_buff *skb,
977 HvLpIndexMap lpmask,
978 struct net_device *dev)
979{
980 struct veth_port *port = (struct veth_port *) dev->priv;
981 int i;
982 int rc;
983
984 for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
985 if ((lpmask & (1 << i)) == 0)
986 continue;
987
988 rc = veth_transmit_to_one(skb_get(skb), i, dev);
989 if (! rc)
990 lpmask &= ~(1<<i);
991 }
992
993 if (! lpmask) {
994 port->stats.tx_packets++;
995 port->stats.tx_bytes += skb->len;
996 }
997
998 return lpmask;
999}
1000
1001static int veth_start_xmit(struct sk_buff *skb, struct net_device *dev)
1002{
1003 unsigned char *frame = skb->data;
1004 struct veth_port *port = (struct veth_port *) dev->priv;
1005 unsigned long flags;
1006 HvLpIndexMap lpmask;
1007
1008 if (! (frame[0] & 0x01)) {
1009 /* unicast packet */
1010 HvLpIndex rlp = frame[5];
1011
1012 if ( ! ((1 << rlp) & port->lpar_map) ) {
1013 dev_kfree_skb(skb);
1014 return 0;
1015 }
1016
1017 lpmask = 1 << rlp;
1018 } else {
1019 lpmask = port->lpar_map;
1020 }
1021
1022 spin_lock_irqsave(&port->pending_gate, flags);
1023
1024 lpmask = veth_transmit_to_many(skb, lpmask, dev);
1025
1026 if (! lpmask) {
1027 dev_kfree_skb(skb);
1028 } else {
1029 if (port->pending_skb) {
1030 veth_error("%s: Tx while skb was pending!\n",
1031 dev->name);
1032 dev_kfree_skb(skb);
1033 spin_unlock_irqrestore(&port->pending_gate, flags);
1034 return 1;
1035 }
1036
1037 port->pending_skb = skb;
1038 port->pending_lpmask = lpmask;
1039 netif_stop_queue(dev);
1040 }
1041
1042 spin_unlock_irqrestore(&port->pending_gate, flags);
1043
1044 return 0;
1045}
1046
1047static void veth_recycle_msg(struct veth_lpar_connection *cnx,
1048 struct veth_msg *msg)
1049{
1050 u32 dma_address, dma_length;
1051
1052 if (test_and_clear_bit(0, &msg->in_use)) {
1053 dma_address = msg->data.addr[0];
1054 dma_length = msg->data.len[0];
1055
1056 dma_unmap_single(msg->dev, dma_address, dma_length,
1057 DMA_TO_DEVICE);
1058
1059 if (msg->skb) {
1060 dev_kfree_skb_any(msg->skb);
1061 msg->skb = NULL;
1062 }
1063
1064 memset(&msg->data, 0, sizeof(msg->data));
1065 veth_stack_push(cnx, msg);
1066 } else
1067 if (cnx->state & VETH_STATE_OPEN)
1068 veth_error("Bogus frames ack from lpar %d (#%d)\n",
1069 cnx->remote_lp, msg->token);
1070}
1071
1072static void veth_flush_pending(struct veth_lpar_connection *cnx)
1073{
1074 int i;
1075 for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++) {
1076 struct net_device *dev = veth_dev[i];
1077 struct veth_port *port;
1078 unsigned long flags;
1079
1080 if (! dev)
1081 continue;
1082
1083 port = (struct veth_port *)dev->priv;
1084
1085 if (! (port->lpar_map & (1<<cnx->remote_lp)))
1086 continue;
1087
1088 spin_lock_irqsave(&port->pending_gate, flags);
1089 if (port->pending_skb) {
1090 port->pending_lpmask =
1091 veth_transmit_to_many(port->pending_skb,
1092 port->pending_lpmask,
1093 dev);
1094 if (! port->pending_lpmask) {
1095 dev_kfree_skb_any(port->pending_skb);
1096 port->pending_skb = NULL;
1097 netif_wake_queue(dev);
1098 }
1099 }
1100 spin_unlock_irqrestore(&port->pending_gate, flags);
1101 }
1102}
1103
1104/*
1105 * Rx path
1106 */
1107
1108static inline int veth_frame_wanted(struct veth_port *port, u64 mac_addr)
1109{
1110 int wanted = 0;
1111 int i;
1112 unsigned long flags;
1113
1114 if ( (mac_addr == port->mac_addr) || (mac_addr == 0xffffffffffff0000) )
1115 return 1;
1116
1117 if (! (((char *) &mac_addr)[0] & 0x01))
1118 return 0;
1119
1120 read_lock_irqsave(&port->mcast_gate, flags);
1121
1122 if (port->promiscuous || port->all_mcast) {
1123 wanted = 1;
1124 goto out;
1125 }
1126
1127 for (i = 0; i < port->num_mcast; ++i) {
1128 if (port->mcast_addr[i] == mac_addr) {
1129 wanted = 1;
1130 break;
1131 }
1132 }
1133
1134 out:
1135 read_unlock_irqrestore(&port->mcast_gate, flags);
1136
1137 return wanted;
1138}
1139
1140struct dma_chunk {
1141 u64 addr;
1142 u64 size;
1143};
1144
1145#define VETH_MAX_PAGES_PER_FRAME ( (VETH_MAX_MTU+PAGE_SIZE-2)/PAGE_SIZE + 1 )
1146
1147static inline void veth_build_dma_list(struct dma_chunk *list,
1148 unsigned char *p, unsigned long length)
1149{
1150 unsigned long done;
1151 int i = 1;
1152
1153 /* FIXME: skbs are continguous in real addresses. Do we
1154 * really need to break it into PAGE_SIZE chunks, or can we do
1155 * it just at the granularity of iSeries real->absolute
1156 * mapping? Indeed, given the way the allocator works, can we
1157 * count on them being absolutely contiguous? */
1158 list[0].addr = ISERIES_HV_ADDR(p);
1159 list[0].size = min(length,
1160 PAGE_SIZE - ((unsigned long)p & ~PAGE_MASK));
1161
1162 done = list[0].size;
1163 while (done < length) {
1164 list[i].addr = ISERIES_HV_ADDR(p + done);
1165 list[i].size = min(length-done, PAGE_SIZE);
1166 done += list[i].size;
1167 i++;
1168 }
1169}
1170
1171static void veth_flush_acks(struct veth_lpar_connection *cnx)
1172{
1173 HvLpEvent_Rc rc;
1174
1175 rc = veth_signaldata(cnx, VethEventTypeFramesAck,
1176 0, &cnx->pending_acks);
1177
1178 if (rc != HvLpEvent_Rc_Good)
1179 veth_error("Error 0x%x acking frames from lpar %d!\n",
1180 (unsigned)rc, cnx->remote_lp);
1181
1182 cnx->num_pending_acks = 0;
1183 memset(&cnx->pending_acks, 0xff, sizeof(cnx->pending_acks));
1184}
1185
1186static void veth_receive(struct veth_lpar_connection *cnx,
1187 struct VethLpEvent *event)
1188{
1189 struct VethFramesData *senddata = &event->u.frames_data;
1190 int startchunk = 0;
1191 int nchunks;
1192 unsigned long flags;
1193 HvLpDma_Rc rc;
1194
1195 do {
1196 u16 length = 0;
1197 struct sk_buff *skb;
1198 struct dma_chunk local_list[VETH_MAX_PAGES_PER_FRAME];
1199 struct dma_chunk remote_list[VETH_MAX_FRAMES_PER_MSG];
1200 u64 dest;
1201 HvLpVirtualLanIndex vlan;
1202 struct net_device *dev;
1203 struct veth_port *port;
1204
1205 /* FIXME: do we need this? */
1206 memset(local_list, 0, sizeof(local_list));
1207 memset(remote_list, 0, sizeof(VETH_MAX_FRAMES_PER_MSG));
1208
1209 /* a 0 address marks the end of the valid entries */
1210 if (senddata->addr[startchunk] == 0)
1211 break;
1212
1213 /* make sure that we have at least 1 EOF entry in the
1214 * remaining entries */
1215 if (! (senddata->eofmask >> (startchunk + VETH_EOF_SHIFT))) {
1216 veth_error("missing EOF frag in event "
1217 "eofmask=0x%x startchunk=%d\n",
1218 (unsigned) senddata->eofmask, startchunk);
1219 break;
1220 }
1221
1222 /* build list of chunks in this frame */
1223 nchunks = 0;
1224 do {
1225 remote_list[nchunks].addr =
1226 (u64) senddata->addr[startchunk+nchunks] << 32;
1227 remote_list[nchunks].size =
1228 senddata->len[startchunk+nchunks];
1229 length += remote_list[nchunks].size;
1230 } while (! (senddata->eofmask &
1231 (1 << (VETH_EOF_SHIFT + startchunk + nchunks++))));
1232
1233 /* length == total length of all chunks */
1234 /* nchunks == # of chunks in this frame */
1235
1236 if ((length - ETH_HLEN) > VETH_MAX_MTU) {
1237 veth_error("Received oversize frame from lpar %d "
1238 "(length=%d)\n", cnx->remote_lp, length);
1239 continue;
1240 }
1241
1242 skb = alloc_skb(length, GFP_ATOMIC);
1243 if (!skb)
1244 continue;
1245
1246 veth_build_dma_list(local_list, skb->data, length);
1247
1248 rc = HvCallEvent_dmaBufList(HvLpEvent_Type_VirtualLan,
1249 event->base_event.xSourceLp,
1250 HvLpDma_Direction_RemoteToLocal,
1251 cnx->src_inst,
1252 cnx->dst_inst,
1253 HvLpDma_AddressType_RealAddress,
1254 HvLpDma_AddressType_TceIndex,
1255 ISERIES_HV_ADDR(&local_list),
1256 ISERIES_HV_ADDR(&remote_list),
1257 length);
1258 if (rc != HvLpDma_Rc_Good) {
1259 dev_kfree_skb_irq(skb);
1260 continue;
1261 }
1262
1263 vlan = skb->data[9];
1264 dev = veth_dev[vlan];
1265 if (! dev)
1266 /* Some earlier versions of the driver sent
1267 broadcasts down all connections, even to
1268 lpars that weren't on the relevant vlan.
1269 So ignore packets belonging to a vlan we're
1270 not on. */
1271 continue;
1272
1273 port = (struct veth_port *)dev->priv;
1274 dest = *((u64 *) skb->data) & 0xFFFFFFFFFFFF0000;
1275
1276 if ((vlan > HVMAXARCHITECTEDVIRTUALLANS) || !port) {
1277 dev_kfree_skb_irq(skb);
1278 continue;
1279 }
1280 if (! veth_frame_wanted(port, dest)) {
1281 dev_kfree_skb_irq(skb);
1282 continue;
1283 }
1284
1285 skb_put(skb, length);
1286 skb->dev = dev;
1287 skb->protocol = eth_type_trans(skb, dev);
1288 skb->ip_summed = CHECKSUM_NONE;
1289 netif_rx(skb); /* send it up */
1290 port->stats.rx_packets++;
1291 port->stats.rx_bytes += length;
1292 } while (startchunk += nchunks, startchunk < VETH_MAX_FRAMES_PER_MSG);
1293
1294 /* Ack it */
1295 spin_lock_irqsave(&cnx->lock, flags);
1296 BUG_ON(cnx->num_pending_acks > VETH_MAX_ACKS_PER_MSG);
1297
1298 cnx->pending_acks[cnx->num_pending_acks++] =
1299 event->base_event.xCorrelationToken;
1300
1301 if ( (cnx->num_pending_acks >= cnx->remote_caps.ack_threshold)
1302 || (cnx->num_pending_acks >= VETH_MAX_ACKS_PER_MSG) )
1303 veth_flush_acks(cnx);
1304
1305 spin_unlock_irqrestore(&cnx->lock, flags);
1306}
1307
1308static void veth_timed_ack(unsigned long ptr)
1309{
1310 struct veth_lpar_connection *cnx = (struct veth_lpar_connection *) ptr;
1311 unsigned long flags;
1312
1313 /* Ack all the events */
1314 spin_lock_irqsave(&cnx->lock, flags);
1315 if (cnx->num_pending_acks > 0)
1316 veth_flush_acks(cnx);
1317
1318 /* Reschedule the timer */
1319 cnx->ack_timer.expires = jiffies + cnx->ack_timeout;
1320 add_timer(&cnx->ack_timer);
1321 spin_unlock_irqrestore(&cnx->lock, flags);
1322}
1323
1324static int veth_remove(struct vio_dev *vdev)
1325{
1326 int i = vdev->unit_address;
1327 struct net_device *dev;
1328
1329 dev = veth_dev[i];
1330 if (dev != NULL) {
1331 veth_dev[i] = NULL;
1332 unregister_netdev(dev);
1333 free_netdev(dev);
1334 }
1335 return 0;
1336}
1337
1338static int veth_probe(struct vio_dev *vdev, const struct vio_device_id *id)
1339{
1340 int i = vdev->unit_address;
1341 struct net_device *dev;
1342
1343 dev = veth_probe_one(i, &vdev->dev);
1344 if (dev == NULL) {
1345 veth_remove(vdev);
1346 return 1;
1347 }
1348 veth_dev[i] = dev;
1349
1350 /* Start the state machine on each connection, to commence
1351 * link negotiation */
1352 for (i = 0; i < HVMAXARCHITECTEDLPS; i++)
1353 if (veth_cnx[i])
1354 veth_kick_statemachine(veth_cnx[i]);
1355
1356 return 0;
1357}
1358
1359/**
1360 * veth_device_table: Used by vio.c to match devices that we
1361 * support.
1362 */
1363static struct vio_device_id veth_device_table[] __devinitdata = {
1364 { "vlan", "" },
1365 { NULL, NULL }
1366};
1367MODULE_DEVICE_TABLE(vio, veth_device_table);
1368
1369static struct vio_driver veth_driver = {
1370 .name = "iseries_veth",
1371 .id_table = veth_device_table,
1372 .probe = veth_probe,
1373 .remove = veth_remove
1374};
1375
1376/*
1377 * Module initialization/cleanup
1378 */
1379
1380void __exit veth_module_cleanup(void)
1381{
1382 int i;
1383
1384 vio_unregister_driver(&veth_driver);
1385
1386 for (i = 0; i < HVMAXARCHITECTEDLPS; ++i)
1387 veth_stop_connection(i);
1388
1389 HvLpEvent_unregisterHandler(HvLpEvent_Type_VirtualLan);
1390
1391 /* Hypervisor callbacks may have scheduled more work while we
1392 * were destroying connections. Now that we've disconnected from
1393 * the hypervisor make sure everything's finished. */
1394 flush_scheduled_work();
1395
1396 for (i = 0; i < HVMAXARCHITECTEDLPS; ++i)
1397 veth_destroy_connection(i);
1398
1399}
1400module_exit(veth_module_cleanup);
1401
1402int __init veth_module_init(void)
1403{
1404 int i;
1405 int rc;
1406
1407 this_lp = HvLpConfig_getLpIndex_outline();
1408
1409 for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
1410 rc = veth_init_connection(i);
1411 if (rc != 0) {
1412 veth_module_cleanup();
1413 return rc;
1414 }
1415 }
1416
1417 HvLpEvent_registerHandler(HvLpEvent_Type_VirtualLan,
1418 &veth_handle_event);
1419
1420 return vio_register_driver(&veth_driver);
1421}
1422module_init(veth_module_init);
"hl opt">, cpumask_t mask) { balance_irq_affinity[irq] = mask; } static unsigned long move(int curr_cpu, cpumask_t allowed_mask, unsigned long now, int direction) { int search_idle = 1; int cpu = curr_cpu; goto inside; do { if (unlikely(cpu == curr_cpu)) search_idle = 0; inside: if (direction == 1) { cpu++; if (cpu >= NR_CPUS) cpu = 0; } else { cpu--; if (cpu == -1) cpu = NR_CPUS-1; } } while (!cpu_online(cpu) || !IRQ_ALLOWED(cpu,allowed_mask) || (search_idle && !IDLE_ENOUGH(cpu,now))); return cpu; } static inline void balance_irq(int cpu, int irq) { unsigned long now = jiffies; cpumask_t allowed_mask; unsigned int new_cpu; if (irqbalance_disabled) return; cpus_and(allowed_mask, cpu_online_map, balance_irq_affinity[irq]); new_cpu = move(cpu, allowed_mask, now, 1); if (cpu != new_cpu) { set_pending_irq(irq, cpumask_of_cpu(new_cpu)); } } static inline void rotate_irqs_among_cpus(unsigned long useful_load_threshold) { int i, j; for_each_online_cpu(i) { for (j = 0; j < NR_IRQS; j++) { if (!irq_desc[j].action) continue; /* Is it a significant load ? */ if (IRQ_DELTA(CPU_TO_PACKAGEINDEX(i),j) < useful_load_threshold) continue; balance_irq(i, j); } } balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL, balanced_irq_interval - BALANCED_IRQ_LESS_DELTA); return; } static void do_irq_balance(void) { int i, j; unsigned long max_cpu_irq = 0, min_cpu_irq = (~0); unsigned long move_this_load = 0; int max_loaded = 0, min_loaded = 0; int load; unsigned long useful_load_threshold = balanced_irq_interval + 10; int selected_irq; int tmp_loaded, first_attempt = 1; unsigned long tmp_cpu_irq; unsigned long imbalance = 0; cpumask_t allowed_mask, target_cpu_mask, tmp; for_each_possible_cpu(i) { int package_index; CPU_IRQ(i) = 0; if (!cpu_online(i)) continue; package_index = CPU_TO_PACKAGEINDEX(i); for (j = 0; j < NR_IRQS; j++) { unsigned long value_now, delta; /* Is this an active IRQ or balancing disabled ? */ if (!irq_desc[j].action || irq_balancing_disabled(j)) continue; if ( package_index == i ) IRQ_DELTA(package_index,j) = 0; /* Determine the total count per processor per IRQ */ value_now = (unsigned long) kstat_cpu(i).irqs[j]; /* Determine the activity per processor per IRQ */ delta = value_now - LAST_CPU_IRQ(i,j); /* Update last_cpu_irq[][] for the next time */ LAST_CPU_IRQ(i,j) = value_now; /* Ignore IRQs whose rate is less than the clock */ if (delta < useful_load_threshold) continue; /* update the load for the processor or package total */ IRQ_DELTA(package_index,j) += delta; /* Keep track of the higher numbered sibling as well */ if (i != package_index) CPU_IRQ(i) += delta; /* * We have sibling A and sibling B in the package * * cpu_irq[A] = load for cpu A + load for cpu B * cpu_irq[B] = load for cpu B */ CPU_IRQ(package_index) += delta; } } /* Find the least loaded processor package */ for_each_online_cpu(i) { if (i != CPU_TO_PACKAGEINDEX(i)) continue; if (min_cpu_irq > CPU_IRQ(i)) { min_cpu_irq = CPU_IRQ(i); min_loaded = i; } } max_cpu_irq = ULONG_MAX; tryanothercpu: /* Look for heaviest loaded processor. * We may come back to get the next heaviest loaded processor. * Skip processors with trivial loads. */ tmp_cpu_irq = 0; tmp_loaded = -1; for_each_online_cpu(i) { if (i != CPU_TO_PACKAGEINDEX(i)) continue; if (max_cpu_irq <= CPU_IRQ(i)) continue; if (tmp_cpu_irq < CPU_IRQ(i)) { tmp_cpu_irq = CPU_IRQ(i); tmp_loaded = i; } } if (tmp_loaded == -1) { /* In the case of small number of heavy interrupt sources, * loading some of the cpus too much. We use Ingo's original * approach to rotate them around. */ if (!first_attempt && imbalance >= useful_load_threshold) { rotate_irqs_among_cpus(useful_load_threshold); return; } goto not_worth_the_effort; } first_attempt = 0; /* heaviest search */ max_cpu_irq = tmp_cpu_irq; /* load */ max_loaded = tmp_loaded; /* processor */ imbalance = (max_cpu_irq - min_cpu_irq) / 2; /* if imbalance is less than approx 10% of max load, then * observe diminishing returns action. - quit */ if (imbalance < (max_cpu_irq >> 3)) goto not_worth_the_effort; tryanotherirq: /* if we select an IRQ to move that can't go where we want, then * see if there is another one to try. */ move_this_load = 0; selected_irq = -1; for (j = 0; j < NR_IRQS; j++) { /* Is this an active IRQ? */ if (!irq_desc[j].action) continue; if (imbalance <= IRQ_DELTA(max_loaded,j)) continue; /* Try to find the IRQ that is closest to the imbalance * without going over. */ if (move_this_load < IRQ_DELTA(max_loaded,j)) { move_this_load = IRQ_DELTA(max_loaded,j); selected_irq = j; } } if (selected_irq == -1) { goto tryanothercpu; } imbalance = move_this_load; /* For physical_balance case, we accumulated both load * values in the one of the siblings cpu_irq[], * to use the same code for physical and logical processors * as much as possible. * * NOTE: the cpu_irq[] array holds the sum of the load for * sibling A and sibling B in the slot for the lowest numbered * sibling (A), _AND_ the load for sibling B in the slot for * the higher numbered sibling. * * We seek the least loaded sibling by making the comparison * (A+B)/2 vs B */ load = CPU_IRQ(min_loaded) >> 1; for_each_cpu_mask(j, per_cpu(cpu_sibling_map, min_loaded)) { if (load > CPU_IRQ(j)) { /* This won't change cpu_sibling_map[min_loaded] */ load = CPU_IRQ(j); min_loaded = j; } } cpus_and(allowed_mask, cpu_online_map, balance_irq_affinity[selected_irq]); target_cpu_mask = cpumask_of_cpu(min_loaded); cpus_and(tmp, target_cpu_mask, allowed_mask); if (!cpus_empty(tmp)) { /* mark for change destination */ set_pending_irq(selected_irq, cpumask_of_cpu(min_loaded)); /* Since we made a change, come back sooner to * check for more variation. */ balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL, balanced_irq_interval - BALANCED_IRQ_LESS_DELTA); return; } goto tryanotherirq; not_worth_the_effort: /* * if we did not find an IRQ to move, then adjust the time interval * upward */ balanced_irq_interval = min((long)MAX_BALANCED_IRQ_INTERVAL, balanced_irq_interval + BALANCED_IRQ_MORE_DELTA); return; } static int balanced_irq(void *unused) { int i; unsigned long prev_balance_time = jiffies; long time_remaining = balanced_irq_interval; /* push everything to CPU 0 to give us a starting point. */ for (i = 0 ; i < NR_IRQS ; i++) { irq_desc[i].pending_mask = cpumask_of_cpu(0); set_pending_irq(i, cpumask_of_cpu(0)); } set_freezable(); for ( ; ; ) { time_remaining = schedule_timeout_interruptible(time_remaining); try_to_freeze(); if (time_after(jiffies, prev_balance_time+balanced_irq_interval)) { preempt_disable(); do_irq_balance(); prev_balance_time = jiffies; time_remaining = balanced_irq_interval; preempt_enable(); } } return 0; } static int __init balanced_irq_init(void) { int i; struct cpuinfo_x86 *c; cpumask_t tmp; cpus_shift_right(tmp, cpu_online_map, 2); c = &boot_cpu_data; /* When not overwritten by the command line ask subarchitecture. */ if (irqbalance_disabled == IRQBALANCE_CHECK_ARCH) irqbalance_disabled = NO_BALANCE_IRQ; if (irqbalance_disabled) return 0; /* disable irqbalance completely if there is only one processor online */ if (num_online_cpus() < 2) { irqbalance_disabled = 1; return 0; } /* * Enable physical balance only if more than 1 physical processor * is present */ if (smp_num_siblings > 1 && !cpus_empty(tmp)) physical_balance = 1; for_each_online_cpu(i) { irq_cpu_data[i].irq_delta = kmalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL); irq_cpu_data[i].last_irq = kmalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL); if (irq_cpu_data[i].irq_delta == NULL || irq_cpu_data[i].last_irq == NULL) { printk(KERN_ERR "balanced_irq_init: out of memory"); goto failed; } memset(irq_cpu_data[i].irq_delta,0,sizeof(unsigned long) * NR_IRQS); memset(irq_cpu_data[i].last_irq,0,sizeof(unsigned long) * NR_IRQS); } printk(KERN_INFO "Starting balanced_irq\n"); if (!IS_ERR(kthread_run(balanced_irq, NULL, "kirqd"))) return 0; printk(KERN_ERR "balanced_irq_init: failed to spawn balanced_irq"); failed: for_each_possible_cpu(i) { kfree(irq_cpu_data[i].irq_delta); irq_cpu_data[i].irq_delta = NULL; kfree(irq_cpu_data[i].last_irq); irq_cpu_data[i].last_irq = NULL; } return 0; } int __devinit irqbalance_disable(char *str) { irqbalance_disabled = 1; return 1; } __setup("noirqbalance", irqbalance_disable); late_initcall(balanced_irq_init); #endif /* CONFIG_IRQBALANCE */ #endif /* CONFIG_SMP */ #ifndef CONFIG_SMP void fastcall send_IPI_self(int vector) { unsigned int cfg; /* * Wait for idle. */ apic_wait_icr_idle(); cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL; /* * Send the IPI. The write to APIC_ICR fires this off. */ apic_write_around(APIC_ICR, cfg); } #endif /* !CONFIG_SMP */ /* * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to * specific CPU-side IRQs. */ #define MAX_PIRQS 8 static int pirq_entries [MAX_PIRQS]; static int pirqs_enabled; int skip_ioapic_setup; static int __init ioapic_pirq_setup(char *str) { int i, max; int ints[MAX_PIRQS+1]; get_options(str, ARRAY_SIZE(ints), ints); for (i = 0; i < MAX_PIRQS; i++) pirq_entries[i] = -1; pirqs_enabled = 1; apic_printk(APIC_VERBOSE, KERN_INFO "PIRQ redirection, working around broken MP-BIOS.\n"); max = MAX_PIRQS; if (ints[0] < MAX_PIRQS) max = ints[0]; for (i = 0; i < max; i++) { apic_printk(APIC_VERBOSE, KERN_DEBUG "... PIRQ%d -> IRQ %d\n", i, ints[i+1]); /* * PIRQs are mapped upside down, usually. */ pirq_entries[MAX_PIRQS-i-1] = ints[i+1]; } return 1; } __setup("pirq=", ioapic_pirq_setup); /* * Find the IRQ entry number of a certain pin. */ static int find_irq_entry(int apic, int pin, int type) { int i; for (i = 0; i < mp_irq_entries; i++) if (mp_irqs[i].mpc_irqtype == type && (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid || mp_irqs[i].mpc_dstapic == MP_APIC_ALL) && mp_irqs[i].mpc_dstirq == pin) return i; return -1; } /* * Find the pin to which IRQ[irq] (ISA) is connected */ static int __init find_isa_irq_pin(int irq, int type) { int i; for (i = 0; i < mp_irq_entries; i++) { int lbus = mp_irqs[i].mpc_srcbus; if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA || mp_bus_id_to_type[lbus] == MP_BUS_EISA || mp_bus_id_to_type[lbus] == MP_BUS_MCA ) && (mp_irqs[i].mpc_irqtype == type) && (mp_irqs[i].mpc_srcbusirq == irq)) return mp_irqs[i].mpc_dstirq; } return -1; } static int __init find_isa_irq_apic(int irq, int type) { int i; for (i = 0; i < mp_irq_entries; i++) { int lbus = mp_irqs[i].mpc_srcbus; if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA || mp_bus_id_to_type[lbus] == MP_BUS_EISA || mp_bus_id_to_type[lbus] == MP_BUS_MCA ) && (mp_irqs[i].mpc_irqtype == type) && (mp_irqs[i].mpc_srcbusirq == irq)) break; } if (i < mp_irq_entries) { int apic; for(apic = 0; apic < nr_ioapics; apic++) { if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic) return apic; } } return -1; } /* * Find a specific PCI IRQ entry. * Not an __init, possibly needed by modules */ static int pin_2_irq(int idx, int apic, int pin); int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin) { int apic, i, best_guess = -1; apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, " "slot:%d, pin:%d.\n", bus, slot, pin); if (mp_bus_id_to_pci_bus[bus] == -1) { printk(KERN_WARNING "PCI BIOS passed nonexistent PCI bus %d!\n", bus); return -1; } for (i = 0; i < mp_irq_entries; i++) { int lbus = mp_irqs[i].mpc_srcbus; for (apic = 0; apic < nr_ioapics; apic++) if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic || mp_irqs[i].mpc_dstapic == MP_APIC_ALL) break; if ((mp_bus_id_to_type[lbus] == MP_BUS_PCI) && !mp_irqs[i].mpc_irqtype && (bus == lbus) && (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) { int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq); if (!(apic || IO_APIC_IRQ(irq))) continue; if (pin == (mp_irqs[i].mpc_srcbusirq & 3)) return irq; /* * Use the first all-but-pin matching entry as a * best-guess fuzzy result for broken mptables. */ if (best_guess < 0) best_guess = irq; } } return best_guess; } EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector); /* * This function currently is only a helper for the i386 smp boot process where * we need to reprogram the ioredtbls to cater for the cpus which have come online * so mask in all cases should simply be TARGET_CPUS */ #ifdef CONFIG_SMP void __init setup_ioapic_dest(void) { int pin, ioapic, irq, irq_entry; if (skip_ioapic_setup == 1) return; for (ioapic = 0; ioapic < nr_ioapics; ioapic++) { for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) { irq_entry = find_irq_entry(ioapic, pin, mp_INT); if (irq_entry == -1) continue; irq = pin_2_irq(irq_entry, ioapic, pin); set_ioapic_affinity_irq(irq, TARGET_CPUS); } } } #endif /* * EISA Edge/Level control register, ELCR */ static int EISA_ELCR(unsigned int irq) { if (irq < 16) { unsigned int port = 0x4d0 + (irq >> 3); return (inb(port) >> (irq & 7)) & 1; } apic_printk(APIC_VERBOSE, KERN_INFO "Broken MPtable reports ISA irq %d\n", irq); return 0; } /* EISA interrupts are always polarity zero and can be edge or level * trigger depending on the ELCR value. If an interrupt is listed as * EISA conforming in the MP table, that means its trigger type must * be read in from the ELCR */ #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq)) #define default_EISA_polarity(idx) (0) /* ISA interrupts are always polarity zero edge triggered, * when listed as conforming in the MP table. */ #define default_ISA_trigger(idx) (0) #define default_ISA_polarity(idx) (0) /* PCI interrupts are always polarity one level triggered, * when listed as conforming in the MP table. */ #define default_PCI_trigger(idx) (1) #define default_PCI_polarity(idx) (1) /* MCA interrupts are always polarity zero level triggered, * when listed as conforming in the MP table. */ #define default_MCA_trigger(idx) (1) #define default_MCA_polarity(idx) (0) static int MPBIOS_polarity(int idx) { int bus = mp_irqs[idx].mpc_srcbus; int polarity; /* * Determine IRQ line polarity (high active or low active): */ switch (mp_irqs[idx].mpc_irqflag & 3) { case 0: /* conforms, ie. bus-type dependent polarity */ { switch (mp_bus_id_to_type[bus]) { case MP_BUS_ISA: /* ISA pin */ { polarity = default_ISA_polarity(idx); break; } case MP_BUS_EISA: /* EISA pin */ { polarity = default_EISA_polarity(idx); break; } case MP_BUS_PCI: /* PCI pin */ { polarity = default_PCI_polarity(idx); break; } case MP_BUS_MCA: /* MCA pin */ { polarity = default_MCA_polarity(idx); break; } default: { printk(KERN_WARNING "broken BIOS!!\n"); polarity = 1; break; } } break; } case 1: /* high active */ { polarity = 0; break; } case 2: /* reserved */ { printk(KERN_WARNING "broken BIOS!!\n"); polarity = 1; break; } case 3: /* low active */ { polarity = 1; break; } default: /* invalid */ { printk(KERN_WARNING "broken BIOS!!\n"); polarity = 1; break; } } return polarity; } static int MPBIOS_trigger(int idx) { int bus = mp_irqs[idx].mpc_srcbus; int trigger; /* * Determine IRQ trigger mode (edge or level sensitive): */ switch ((mp_irqs[idx].mpc_irqflag>>2) & 3) { case 0: /* conforms, ie. bus-type dependent */ { switch (mp_bus_id_to_type[bus]) { case MP_BUS_ISA: /* ISA pin */ { trigger = default_ISA_trigger(idx); break; } case MP_BUS_EISA: /* EISA pin */ { trigger = default_EISA_trigger(idx); break; } case MP_BUS_PCI: /* PCI pin */ { trigger = default_PCI_trigger(idx); break; } case MP_BUS_MCA: /* MCA pin */ { trigger = default_MCA_trigger(idx); break; } default: { printk(KERN_WARNING "broken BIOS!!\n"); trigger = 1; break; } } break; } case 1: /* edge */ { trigger = 0; break; } case 2: /* reserved */ { printk(KERN_WARNING "broken BIOS!!\n"); trigger = 1; break; } case 3: /* level */ { trigger = 1; break; } default: /* invalid */ { printk(KERN_WARNING "broken BIOS!!\n"); trigger = 0; break; } } return trigger; } static inline int irq_polarity(int idx) { return MPBIOS_polarity(idx); } static inline int irq_trigger(int idx) { return MPBIOS_trigger(idx); } static int pin_2_irq(int idx, int apic, int pin) { int irq, i; int bus = mp_irqs[idx].mpc_srcbus; /* * Debugging check, we are in big trouble if this message pops up! */ if (mp_irqs[idx].mpc_dstirq != pin) printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n"); switch (mp_bus_id_to_type[bus]) { case MP_BUS_ISA: /* ISA pin */ case MP_BUS_EISA: case MP_BUS_MCA: { irq = mp_irqs[idx].mpc_srcbusirq; break; } case MP_BUS_PCI: /* PCI pin */ { /* * PCI IRQs are mapped in order */ i = irq = 0; while (i < apic) irq += nr_ioapic_registers[i++]; irq += pin; /* * For MPS mode, so far only needed by ES7000 platform */ if (ioapic_renumber_irq) irq = ioapic_renumber_irq(apic, irq); break; } default: { printk(KERN_ERR "unknown bus type %d.\n",bus); irq = 0; break; } } /* * PCI IRQ command line redirection. Yes, limits are hardcoded. */ if ((pin >= 16) && (pin <= 23)) { if (pirq_entries[pin-16] != -1) { if (!pirq_entries[pin-16]) { apic_printk(APIC_VERBOSE, KERN_DEBUG "disabling PIRQ%d\n", pin-16); } else { irq = pirq_entries[pin-16]; apic_printk(APIC_VERBOSE, KERN_DEBUG "using PIRQ%d -> IRQ %d\n", pin-16, irq); } } } return irq; } static inline int IO_APIC_irq_trigger(int irq) { int apic, idx, pin; for (apic = 0; apic < nr_ioapics; apic++) { for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) { idx = find_irq_entry(apic,pin,mp_INT); if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin))) return irq_trigger(idx); } } /* * nonexistent IRQs are edge default */ return 0; } /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */ static u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = { FIRST_DEVICE_VECTOR , 0 }; static int __assign_irq_vector(int irq) { static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0; int vector, offset; BUG_ON((unsigned)irq >= NR_IRQ_VECTORS); if (irq_vector[irq] > 0) return irq_vector[irq]; vector = current_vector; offset = current_offset; next: vector += 8; if (vector >= FIRST_SYSTEM_VECTOR) { offset = (offset + 1) % 8; vector = FIRST_DEVICE_VECTOR + offset; } if (vector == current_vector) return -ENOSPC; if (test_and_set_bit(vector, used_vectors)) goto next; current_vector = vector; current_offset = offset; irq_vector[irq] = vector; return vector; } static int assign_irq_vector(int irq) { unsigned long flags; int vector; spin_lock_irqsave(&vector_lock, flags); vector = __assign_irq_vector(irq); spin_unlock_irqrestore(&vector_lock, flags); return vector; } static struct irq_chip ioapic_chip; #define IOAPIC_AUTO -1 #define IOAPIC_EDGE 0 #define IOAPIC_LEVEL 1 static void ioapic_register_intr(int irq, int vector, unsigned long trigger) { if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) || trigger == IOAPIC_LEVEL) { irq_desc[irq].status |= IRQ_LEVEL; set_irq_chip_and_handler_name(irq, &ioapic_chip, handle_fasteoi_irq, "fasteoi"); } else { irq_desc[irq].status &= ~IRQ_LEVEL; set_irq_chip_and_handler_name(irq, &ioapic_chip, handle_edge_irq, "edge"); } set_intr_gate(vector, interrupt[irq]); } static void __init setup_IO_APIC_irqs(void) { struct IO_APIC_route_entry entry; int apic, pin, idx, irq, first_notcon = 1, vector; unsigned long flags; apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n"); for (apic = 0; apic < nr_ioapics; apic++) { for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) { /* * add it to the IO-APIC irq-routing table: */ memset(&entry,0,sizeof(entry)); entry.delivery_mode = INT_DELIVERY_MODE; entry.dest_mode = INT_DEST_MODE; entry.mask = 0; /* enable IRQ */ entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS); idx = find_irq_entry(apic,pin,mp_INT); if (idx == -1) { if (first_notcon) { apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin); first_notcon = 0; } else apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin); continue; } if (!first_notcon) { apic_printk(APIC_VERBOSE, " not connected.\n"); first_notcon = 1; } entry.trigger = irq_trigger(idx); entry.polarity = irq_polarity(idx); if (irq_trigger(idx)) { entry.trigger = 1; entry.mask = 1; } irq = pin_2_irq(idx, apic, pin); /* * skip adding the timer int on secondary nodes, which causes * a small but painful rift in the time-space continuum */ if (multi_timer_check(apic, irq)) continue; else add_pin_to_irq(irq, apic, pin); if (!apic && !IO_APIC_IRQ(irq)) continue; if (IO_APIC_IRQ(irq)) { vector = assign_irq_vector(irq); entry.vector = vector; ioapic_register_intr(irq, vector, IOAPIC_AUTO); if (!apic && (irq < 16)) disable_8259A_irq(irq); } spin_lock_irqsave(&ioapic_lock, flags); __ioapic_write_entry(apic, pin, entry); spin_unlock_irqrestore(&ioapic_lock, flags); } } if (!first_notcon) apic_printk(APIC_VERBOSE, " not connected.\n"); } /* * Set up the 8259A-master output pin: */ static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector) { struct IO_APIC_route_entry entry; memset(&entry,0,sizeof(entry)); disable_8259A_irq(0); /* mask LVT0 */ apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT); /* * We use logical delivery to get the timer IRQ * to the first CPU. */ entry.dest_mode = INT_DEST_MODE; entry.mask = 0; /* unmask IRQ now */ entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS); entry.delivery_mode = INT_DELIVERY_MODE; entry.polarity = 0; entry.trigger = 0; entry.vector = vector; /* * The timer IRQ doesn't have to know that behind the * scene we have a 8259A-master in AEOI mode ... */ irq_desc[0].chip = &ioapic_chip; set_irq_handler(0, handle_edge_irq); /* * Add it to the IO-APIC irq-routing table: */ ioapic_write_entry(apic, pin, entry); enable_8259A_irq(0); } void __init print_IO_APIC(void) { int apic, i; union IO_APIC_reg_00 reg_00; union IO_APIC_reg_01 reg_01; union IO_APIC_reg_02 reg_02; union IO_APIC_reg_03 reg_03; unsigned long flags; if (apic_verbosity == APIC_QUIET) return; printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries); for (i = 0; i < nr_ioapics; i++) printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n", mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]); /* * We are a bit conservative about what we expect. We have to * know about every hardware change ASAP. */ printk(KERN_INFO "testing the IO APIC.......................\n"); for (apic = 0; apic < nr_ioapics; apic++) { spin_lock_irqsave(&ioapic_lock, flags); reg_00.raw = io_apic_read(apic, 0); reg_01.raw = io_apic_read(apic, 1); if (reg_01.bits.version >= 0x10) reg_02.raw = io_apic_read(apic, 2); if (reg_01.bits.version >= 0x20) reg_03.raw = io_apic_read(apic, 3); spin_unlock_irqrestore(&ioapic_lock, flags); printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid); printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw); printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID); printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type); printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS); printk(KERN_DEBUG ".... register #01: %08X\n", reg_01.raw); printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries); printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ); printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version); /* * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02, * but the value of reg_02 is read as the previous read register * value, so ignore it if reg_02 == reg_01. */ if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) { printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw); printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration); } /* * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02 * or reg_03, but the value of reg_0[23] is read as the previous read * register value, so ignore it if reg_03 == reg_0[12]. */ if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw && reg_03.raw != reg_01.raw) { printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw); printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT); } printk(KERN_DEBUG ".... IRQ redirection table:\n"); printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol" " Stat Dest Deli Vect: \n"); for (i = 0; i <= reg_01.bits.entries; i++) { struct IO_APIC_route_entry entry; entry = ioapic_read_entry(apic, i); printk(KERN_DEBUG " %02x %03X %02X ", i, entry.dest.logical.logical_dest, entry.dest.physical.physical_dest ); printk("%1d %1d %1d %1d %1d %1d %1d %02X\n", entry.mask, entry.trigger, entry.irr, entry.polarity, entry.delivery_status, entry.dest_mode, entry.delivery_mode, entry.vector ); } } printk(KERN_DEBUG "IRQ to pin mappings:\n"); for (i = 0; i < NR_IRQS; i++) { struct irq_pin_list *entry = irq_2_pin + i; if (entry->pin < 0) continue; printk(KERN_DEBUG "IRQ%d ", i); for (;;) { printk("-> %d:%d", entry->apic, entry->pin); if (!entry->next) break; entry = irq_2_pin + entry->next; } printk("\n"); } printk(KERN_INFO ".................................... done.\n"); return; } #if 0 static void print_APIC_bitfield (int base) { unsigned int v; int i, j; if (apic_verbosity == APIC_QUIET) return; printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG); for (i = 0; i < 8; i++) { v = apic_read(base + i*0x10); for (j = 0; j < 32; j++) { if (v & (1<<j)) printk("1"); else printk("0"); } printk("\n"); } } void /*__init*/ print_local_APIC(void * dummy) { unsigned int v, ver, maxlvt; if (apic_verbosity == APIC_QUIET) return; printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n", smp_processor_id(), hard_smp_processor_id()); v = apic_read(APIC_ID); printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v)); v = apic_read(APIC_LVR); printk(KERN_INFO "... APIC VERSION: %08x\n", v); ver = GET_APIC_VERSION(v); maxlvt = lapic_get_maxlvt(); v = apic_read(APIC_TASKPRI); printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK); if (APIC_INTEGRATED(ver)) { /* !82489DX */ v = apic_read(APIC_ARBPRI); printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v, v & APIC_ARBPRI_MASK); v = apic_read(APIC_PROCPRI); printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v); } v = apic_read(APIC_EOI); printk(KERN_DEBUG "... APIC EOI: %08x\n", v); v = apic_read(APIC_RRR); printk(KERN_DEBUG "... APIC RRR: %08x\n", v); v = apic_read(APIC_LDR); printk(KERN_DEBUG "... APIC LDR: %08x\n", v); v = apic_read(APIC_DFR); printk(KERN_DEBUG "... APIC DFR: %08x\n", v); v = apic_read(APIC_SPIV); printk(KERN_DEBUG "... APIC SPIV: %08x\n", v); printk(KERN_DEBUG "... APIC ISR field:\n"); print_APIC_bitfield(APIC_ISR); printk(KERN_DEBUG "... APIC TMR field:\n"); print_APIC_bitfield(APIC_TMR); printk(KERN_DEBUG "... APIC IRR field:\n"); print_APIC_bitfield(APIC_IRR); if (APIC_INTEGRATED(ver)) { /* !82489DX */ if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */ apic_write(APIC_ESR, 0); v = apic_read(APIC_ESR); printk(KERN_DEBUG "... APIC ESR: %08x\n", v); } v = apic_read(APIC_ICR); printk(KERN_DEBUG "... APIC ICR: %08x\n", v); v = apic_read(APIC_ICR2); printk(KERN_DEBUG "... APIC ICR2: %08x\n", v); v = apic_read(APIC_LVTT); printk(KERN_DEBUG "... APIC LVTT: %08x\n", v); if (maxlvt > 3) { /* PC is LVT#4. */ v = apic_read(APIC_LVTPC); printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v); } v = apic_read(APIC_LVT0); printk(KERN_DEBUG "... APIC LVT0: %08x\n", v); v = apic_read(APIC_LVT1); printk(KERN_DEBUG "... APIC LVT1: %08x\n", v); if (maxlvt > 2) { /* ERR is LVT#3. */ v = apic_read(APIC_LVTERR); printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v); } v = apic_read(APIC_TMICT); printk(KERN_DEBUG "... APIC TMICT: %08x\n", v); v = apic_read(APIC_TMCCT); printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v); v = apic_read(APIC_TDCR); printk(KERN_DEBUG "... APIC TDCR: %08x\n", v); printk("\n"); } void print_all_local_APICs (void) { on_each_cpu(print_local_APIC, NULL, 1, 1); } void /*__init*/ print_PIC(void) { unsigned int v; unsigned long flags; if (apic_verbosity == APIC_QUIET) return; printk(KERN_DEBUG "\nprinting PIC contents\n"); spin_lock_irqsave(&i8259A_lock, flags); v = inb(0xa1) << 8 | inb(0x21); printk(KERN_DEBUG "... PIC IMR: %04x\n", v);