aboutsummaryrefslogtreecommitdiffstats
path: root/arch/ia64/sn/io/ifconfig_net.c
blob: c7b2b27edcccdd3ec544645f45e3a7f787382847 (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
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
/* $Id: ifconfig_net.c,v 1.1 2002/02/28 17:31:25 marcelo Exp $
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 *  ifconfig_net - SGI's Persistent Network Device names.
 *
 * Copyright (C) 1992-1997, 2000-2002 Silicon Graphics, Inc.  All rights reserved.
 */

#include <linux/types.h>
#include <linux/config.h>
#include <linux/slab.h>
#include <linux/ctype.h>
#include <linux/module.h>
#include <linux/init.h>

#include <linux/pci.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>

#include <asm/sn/sgi.h>
#include <linux/devfs_fs.h>
#include <linux/devfs_fs_kernel.h>
#include <asm/io.h>
#include <asm/sn/iograph.h>
#include <asm/sn/invent.h>
#include <asm/sn/hcl.h>
#include <asm/sn/labelcl.h>
#include <asm/sn/ifconfig_net.h>

#define SGI_IFCONFIG_NET "SGI-PERSISTENT NETWORK DEVICE NAME DRIVER"
#define SGI_IFCONFIG_NET_VERSION "1.0"

/*
 * Some Global definitions.
 */
devfs_handle_t ifconfig_net_handle = NULL;
unsigned long ifconfig_net_debug = 0;

/*
 * ifconfig_net_open - Opens the special device node "/devhw/.ifconfig_net".
 */
static int ifconfig_net_open(struct inode * inode, struct file * filp)
{
	if (ifconfig_net_debug) {
        	printk("ifconfig_net_open called.\n");
	}

        return(0);

}

/*
 * ifconfig_net_close - Closes the special device node "/devhw/.ifconfig_net".
 */
static int ifconfig_net_close(struct inode * inode, struct file * filp)
{

	if (ifconfig_net_debug) {
        	printk("ifconfig_net_close called.\n");
	}

        return(0);
}

/*
 * assign_ifname - Assign the next available interface name from the persistent list.
 */
void
assign_ifname(struct net_device *dev,
		  struct ifname_num *ifname_num)

{

	/*
	 * Handle eth devices.
	 */
        if ( (memcmp(dev->name, "eth", 3) == 0) ) {
		if (ifname_num->next_eth != -1) {
			/*
			 * Assign it the next available eth interface number. 
			 */
			memset(dev->name, 0, strlen(dev->name));
			sprintf(dev->name, "eth%d", (int)ifname_num->next_eth);
			ifname_num->next_eth++;
		} 

                return;
        }

	/*
	 * Handle fddi devices.
	 */
	if ( (memcmp(dev->name, "fddi", 4) == 0) ) {
		if (ifname_num->next_fddi != -1) {
			/*
			 * Assign it the next available fddi interface number.
			 */
			memset(dev->name, 0, strlen(dev->name));
			sprintf(dev->name, "fddi%d", (int)ifname_num->next_fddi);
			ifname_num->next_fddi++;
		}

		return;
	}

	/*
	 * Handle hip devices.
	 */
	if ( (memcmp(dev->name, "hip", 3) == 0) ) {
		if (ifname_num->next_hip != -1) {
			/*
			 * Assign it the next available hip interface number.
			 */
			memset(dev->name, 0, strlen(dev->name));
			sprintf(dev->name, "hip%d", (int)ifname_num->next_hip);
			ifname_num->next_hip++;
		}

		return;
	}

	/*
	 * Handle tr devices.
	 */
	if ( (memcmp(dev->name, "tr", 2) == 0) ) {
		if (ifname_num->next_tr != -1) {
			/*
			 * Assign it the next available tr interface number.
			 */
			memset(dev->name, 0, strlen(dev->name));
			sprintf(dev->name, "tr%d", (int)ifname_num->next_tr);
			ifname_num->next_tr++;
		}

		return;
	}

	/*
	 * Handle fc devices.
	 */
	if ( (memcmp(dev->name, "fc", 2) == 0) ) {
		if (ifname_num->next_fc != -1) {
			/*
			 * Assign it the next available fc interface number.
			 */
			memset(dev->name, 0, strlen(dev->name));
			sprintf(dev->name, "fc%d", (int)ifname_num->next_fc);
			ifname_num->next_fc++;
		}

		return;
	}
}

/*
 * find_persistent_ifname: Returns the entry that was seen in previous boot.
 */
struct ifname_MAC *
find_persistent_ifname(struct net_device *dev,
	struct ifname_MAC *ifname_MAC)

{

	while (ifname_MAC->addr_len) {
		if (memcmp(dev->dev_addr, ifname_MAC->dev_addr, dev->addr_len) == 0)
			return(ifname_MAC);

		ifname_MAC++;
	}

	return(NULL);
}

/*
 * ifconfig_net_ioctl: ifconfig_net driver ioctl interface.
 */
static int ifconfig_net_ioctl(struct inode * inode, struct file * file,
        unsigned int cmd, unsigned long arg)
{

	extern struct net_device *__dev_get_by_name(const char *);
#ifdef CONFIG_NET
	struct net_device *dev;
	struct ifname_MAC *found;
	char temp[64];
#endif
	struct ifname_MAC *ifname_MAC;
	struct ifname_MAC *new_devices, *temp_new_devices;
	struct ifname_num *ifname_num;
	unsigned long size;


	if (ifconfig_net_debug) {
		printk("HCL: hcl_ioctl called.\n");
	}

	/*
	 * Read in the header and see how big of a buffer we really need to 
	 * allocate.
	 */
	ifname_num = (struct ifname_num *) kmalloc(sizeof(struct ifname_num), 
			GFP_KERNEL);
	copy_from_user( ifname_num, (char *) arg, sizeof(struct ifname_num));
	size = ifname_num->size;
	kfree(ifname_num);
	ifname_num = (struct ifname_num *) kmalloc(size, GFP_KERNEL);
	ifname_MAC = (struct ifname_MAC *) ((char *)ifname_num + (sizeof(struct ifname_num)) );

	copy_from_user( ifname_num, (char *) arg, size);
	new_devices =  kmalloc(size - sizeof(struct ifname_num), GFP_KERNEL);
	temp_new_devices = new_devices;

	memset(new_devices, 0, size - sizeof(struct ifname_num));

#ifdef CONFIG_NET
	/*
	 * Go through the net device entries and make them persistent!
	 */
	for (dev = dev_base; dev != NULL; dev = dev->next) {
		/*
		 * Skip NULL entries or "lo"
		 */
		if ( (dev->addr_len == 0) || ( !strncmp(dev->name, "lo", strlen(dev->name))) ){
			continue;
		}

		/*
		 * See if we have a persistent interface name for this device.
		 */
		found = NULL;
		found = find_persistent_ifname(dev, ifname_MAC);
		if (found) {
			strcpy(dev->name, found->name);
		} else {
			/* Never seen this before .. */
			assign_ifname(dev, ifname_num);

			/* 
			 * Save the information for the next boot.
			 */
 			sprintf(temp,"%s %02x:%02x:%02x:%02x:%02x:%02x\n", dev->name,
				dev->dev_addr[0],  dev->dev_addr[1],  dev->dev_addr[2],
				dev->dev_addr[3],  dev->dev_addr[4],  dev->dev_addr[5]);
			strcpy(temp_new_devices->name, dev->name);
			temp_new_devices->addr_len = dev->addr_len;
			memcpy(temp_new_devices->dev_addr, dev->dev_addr, dev->addr_len);
			temp_new_devices++;
		}
		
	}
#endif

	/*
	 * Copy back to the User Buffer area any new devices encountered.
	 */
	copy_to_user((char *)arg + (sizeof(struct ifname_num)), new_devices, 
			size - sizeof(struct ifname_num));

	return(0);

}

struct file_operations ifconfig_net_fops = {
	ioctl:ifconfig_net_ioctl,	/* ioctl */
	open:ifconfig_net_open,		/* open */
	release:ifconfig_net_close	/* release */
};


/*
 * init_ifconfig_net() - Boot time initialization.  Ensure that it is called 
 *	after devfs has been initialized.
 *
 */
#ifdef MODULE
int init_module (void)
#else
int __init init_ifconfig_net(void)
#endif
{
	ifconfig_net_handle = NULL;
	ifconfig_net_handle = hwgraph_register(hwgraph_root, ".ifconfig_net",
			0, DEVFS_FL_AUTO_DEVNUM,
			0, 0,
			S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, 0, 0,
			&ifconfig_net_fops, NULL);

	if (ifconfig_net_handle == NULL) {
		panic("Unable to create SGI PERSISTENT NETWORK DEVICE Name Driver.\n");
	}

	return(0);

}