aboutsummaryrefslogtreecommitdiff
path: root/clients/net-snk/kernel/modules.c
blob: e06bb9fd49715b42f74fe7a5b7c22c98c1f93295 (plain)
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
/******************************************************************************
 * Copyright (c) 2004, 2011 IBM Corporation
 * All rights reserved.
 * This program and the accompanying materials
 * are made available under the terms of the BSD License
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * Contributors:
 *     IBM Corporation - initial implementation
 *****************************************************************************/

#include <netdriver_int.h>
#include <kernel.h>
#include <of.h>
#include <rtas.h> 
#include <libelf.h>
#include <cpu.h> /* flush_cache */
#include <unistd.h> /* open, close, read, write */
#include <stdio.h>
#include <pci.h>
#include "modules.h"

extern snk_module_t of_module;

extern char __client_start[];


typedef snk_module_t *(*module_init_t) (snk_kernel_t *, pci_config_t *);

typedef struct {
	const char *name;
	int        type;
} mod_descriptor_t;

static const mod_descriptor_t modules[] = {
	{ "net_e1000",  MOD_TYPE_NETWORK },
	{ "net_bcm",    MOD_TYPE_NETWORK },
	{ "net_veth",   MOD_TYPE_NETWORK },
	{ "net_virtio", MOD_TYPE_NETWORK },
	{ NULL,         0                }
};

snk_module_t *snk_modules[MODULES_MAX];

extern snk_kernel_t snk_kernel_interface;

/* Load module and call init code.
   Init code will check, if module is responsible for device.
   Returns -1, if not responsible for device, 0 otherwise.
*/

static int
load_module(const char *name)
{
	int len, i;
	void *addr;
	void *link_addr;
	module_init_t module_init;

	// Load modules right after the SNK...
	// FIXME: hard-coded offset!
	link_addr = (void*)__client_start + 0x800000;

	// snk_kernel_interface.print("Loading Module '%s'\n", name);

	/* find module in module list and lookup link address */
	for(i=0; modules[i].name; ++i) {
		if(strcmp(modules[i].name, name) == 0)
			break;
	}
	if( modules[i].name == 0 ) {
		/* module not in list */
		return -1;
	}

	/* check if link address is used already */
	for(i=1; i<MODULES_MAX; ++i) {
		if(snk_modules[i] /* && snk_modules[i]->link_addr == link_addr*/) {
			// busy, can't load modules
			return -2;
		}
	}

	/* find empty position in array of loaded modules */
	for(i=0; i<MODULES_MAX; ++i) {
		if(snk_modules[i] == 0) {
			break;
		}
	}
	if(i == MODULES_MAX) {
		// no space avaliable!
		return -3;
	}

	/* Read module from FLASH */
	len = romfs_lookup(name, &addr);
	if (len <= 0) {
		/* file not found */
		return -4;
	}

	/* Copy image from flash to RAM */
	if (elf_load_file_to_addr(addr, link_addr, (void*)&module_init,
				  NULL, flush_cache) != 2) {
		snk_kernel_interface.print("ELF loading failed!\n");
		return -5;
	}

	snk_modules[i] = module_init(&snk_kernel_interface, &snk_kernel_interface.pci_conf);
	if(snk_modules[i] == 0) {
		/* no device found that can be managed by this module */
		return -5;
	}

	if(snk_modules[i]->type == MOD_TYPE_NETWORK) {
		/* Get mac address from device tree */
		get_mac(snk_modules[i]->mac_addr);
	}

	return i;
}

void
modules_init(void)
{
	int i;

	snk_kernel_interface.io_read  = read_io;
	snk_kernel_interface.io_write = write_io;

	snk_modules[0] = &of_module;

	/* Setup Module List */
	for(i=1; i<MODULES_MAX; ++i) {
		snk_modules[i] = 0;
	}

	/* Load all modules */
	for(i=0; modules[i].name; ++i) {
		load_module(modules[i].name);
	}
}

void
modules_term(void)
{
	int i;

	/* remove all modules */
	for(i=0; i<MODULES_MAX; ++i) {
		if(snk_modules[i] && snk_modules[i]->running != 0) {
			snk_modules[i]->term();
		}
		snk_modules[i] = 0;
	}
}

snk_module_t *
get_module_by_type(int type) {
	int i;

	for(i=0; i<MODULES_MAX; ++i) {
		if(snk_modules[i] && snk_modules[i]->type == type) {
			return snk_modules[i];
		}
	}
	return 0;
}

/**
 * insmod_by_type - Load first module of given type
 *
 * @param type  Type of module that we want to load
 * @return      module descriptor on success
 *              NULL              if not successful
 */
snk_module_t *
insmod_by_type(int type) {
	int i, j;
	for(i = 0; modules[i].name; ++i) {
		if(modules[i].type != type)
			continue;
		j = load_module(modules[i].name);
		if(j >= 0)
			return snk_modules[j];
	}
	return 0;
}

/**
 * rmmod_by_type - Remove all module of given type
 *
 * @param type  Type of module that we want to load
 */
void
rmmod_by_type(int type) {
	int i;

	for (i = 0; i < MODULES_MAX; ++i) {
		if (snk_modules[i] && snk_modules[i]->type == type) {
			if (snk_modules[i]->running)
				snk_modules[i]->term();
			snk_modules[i] = 0;
		}
	}
}