module.c 6.44 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6
/* $Id$
 *
 * 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.
 *
David Mosberger's avatar
David Mosberger committed
7
 * Copyright (C) 1992 - 1997, 2000-2002 Silicon Graphics, Inc. All rights reserved.
Linus Torvalds's avatar
Linus Torvalds committed
8 9 10 11 12
 */

#include <linux/types.h>
#include <linux/slab.h>
#include <asm/sn/sgi.h>
David Mosberger's avatar
David Mosberger committed
13 14
#include <asm/sn/sn_sal.h>
#include <asm/sn/io.h>
Linus Torvalds's avatar
Linus Torvalds committed
15 16 17 18 19 20 21 22 23 24 25
#include <asm/sn/invent.h>
#include <asm/sn/hcl.h>
#include <asm/sn/labelcl.h>
#include <asm/sn/xtalk/xbow.h>
#include <asm/sn/pci/bridge.h>
#include <asm/sn/klconfig.h>
#include <asm/sn/sn1/hubdev.h>
#include <asm/sn/module.h>
#include <asm/sn/pci/pcibr.h>
#include <asm/sn/xtalk/xswitch.h>
#include <asm/sn/nodepda.h>
Linus Torvalds's avatar
Linus Torvalds committed
26
#include <asm/sn/sn_cpuid.h>
Linus Torvalds's avatar
Linus Torvalds committed
27 28


David Mosberger's avatar
David Mosberger committed
29
/* #define LDEBUG	1 */
Linus Torvalds's avatar
Linus Torvalds committed
30

Linus Torvalds's avatar
Linus Torvalds committed
31 32
#ifdef LDEBUG
#define DPRINTF		printk
Linus Torvalds's avatar
Linus Torvalds committed
33
#define printf		printk
Linus Torvalds's avatar
Linus Torvalds committed
34 35 36
#else
#define DPRINTF(x...)
#endif
Linus Torvalds's avatar
Linus Torvalds committed
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

module_t	       *modules[MODULE_MAX];
int			nummodules;

#define SN00_SERIAL_FUDGE	0x3b1af409d513c2
#define SN0_SERIAL_FUDGE	0x6e

void
encode_int_serial(uint64_t src,uint64_t *dest)
{
    uint64_t val;
    int i;

    val = src + SN00_SERIAL_FUDGE;


    for (i = 0; i < sizeof(long long); i++) {
	((char*)dest)[i] =
	    ((char*)&val)[sizeof(long long)/2 +
			 ((i%2) ? ((i/2 * -1) - 1) : (i/2))];
    }
}


void
decode_int_serial(uint64_t src, uint64_t *dest)
{
    uint64_t val;
    int i;

    for (i = 0; i < sizeof(long long); i++) {
	((char*)&val)[sizeof(long long)/2 +
		     ((i%2) ? ((i/2 * -1) - 1) : (i/2))] =
	    ((char*)&src)[i];
    }

    *dest = val - SN00_SERIAL_FUDGE;
}


void
encode_str_serial(const char *src, char *dest)
{
    int i;

    for (i = 0; i < MAX_SERIAL_NUM_SIZE; i++) {

	dest[i] = src[MAX_SERIAL_NUM_SIZE/2 +
		     ((i%2) ? ((i/2 * -1) - 1) : (i/2))] +
	    SN0_SERIAL_FUDGE;
    }
}

void
decode_str_serial(const char *src, char *dest)
{
    int i;

    for (i = 0; i < MAX_SERIAL_NUM_SIZE; i++) {
	dest[MAX_SERIAL_NUM_SIZE/2 +
	    ((i%2) ? ((i/2 * -1) - 1) : (i/2))] = src[i] -
	    SN0_SERIAL_FUDGE;
    }
}


module_t *module_lookup(moduleid_t id)
{
    int			i;

    for (i = 0; i < nummodules; i++)
	if (modules[i]->id == id) {
	    DPRINTF("module_lookup: found m=0x%p\n", modules[i]);
	    return modules[i];
	}

    return NULL;
}

/*
 * module_add_node
 *
 *   The first time a new module number is seen, a module structure is
 *   inserted into the module list in order sorted by module number
 *   and the structure is initialized.
 *
 *   The node number is added to the list of nodes in the module.
 */

module_t *module_add_node(moduleid_t id, cnodeid_t n)
{
    module_t	       *m;
    int			i;
Linus Torvalds's avatar
Linus Torvalds committed
130
    char		buffer[16];
Linus Torvalds's avatar
Linus Torvalds committed
131

Linus Torvalds's avatar
Linus Torvalds committed
132 133 134 135 136
#ifdef __ia64
    memset(buffer, 0, 16);
    format_module_id(buffer, id, MODULE_FORMAT_BRIEF);
    DPRINTF("module_add_node: id=%s node=%d\n", buffer, n);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
137 138

    if ((m = module_lookup(id)) == 0) {
Linus Torvalds's avatar
Linus Torvalds committed
139
#ifdef LATER
Linus Torvalds's avatar
Linus Torvalds committed
140 141 142 143 144 145 146 147 148 149
	m = kmem_zalloc_node(sizeof (module_t), KM_NOSLEEP, n);
#else
	m = kmalloc(sizeof (module_t), GFP_KERNEL);
	memset(m, 0 , sizeof(module_t));
#endif
	ASSERT_ALWAYS(m);

	m->id = id;
	spin_lock_init(&m->lock);

Linus Torvalds's avatar
Linus Torvalds committed
150
	mutex_init_locked(&m->thdcnt);
Linus Torvalds's avatar
Linus Torvalds committed
151

Linus Torvalds's avatar
Linus Torvalds committed
152
// set_elsc(&m->elsc);
Linus Torvalds's avatar
Linus Torvalds committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166
	elsc_init(&m->elsc, COMPACT_TO_NASID_NODEID(n));
	spin_lock_init(&m->elsclock);

	/* Insert in sorted order by module number */

	for (i = nummodules; i > 0 && modules[i - 1]->id > id; i--)
	    modules[i] = modules[i - 1];

	modules[i] = m;
	nummodules++;
    }

    m->nodes[m->nodecnt++] = n;

Linus Torvalds's avatar
Linus Torvalds committed
167
    DPRINTF("module_add_node: module %s now has %d nodes\n", buffer, m->nodecnt);
Linus Torvalds's avatar
Linus Torvalds committed
168 169 170 171 172 173 174 175

    return m;
}

int module_probe_snum(module_t *m, nasid_t nasid)
{
    lboard_t	       *board;
    klmod_serial_num_t *comp;
Linus Torvalds's avatar
Linus Torvalds committed
176
    char * bcopy(const char * src, char * dest, int count);
David Mosberger's avatar
David Mosberger committed
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
    char serial_number[16];

    /*
     * record brick serial number
     */
    board = find_lboard((lboard_t *) KL_CONFIG_INFO(nasid), KLTYPE_SNIA);

    if (! board || KL_CONFIG_DUPLICATE_BOARD(board))
    {
#if	LDEBUG
	printf ("module_probe_snum: no IP35 board found!\n");
#endif
	return 0;
    }

    board_serial_number_get( board, serial_number );
    if( serial_number[0] != '\0' ) {
	encode_str_serial( serial_number, m->snum.snum_str );
	m->snum_valid = 1;
    }
#if	LDEBUG
    else {
	printf("module_probe_snum: brick serial number is null!\n");
    }
    printf("module_probe_snum: brick serial number == %s\n", serial_number);
#endif /* DEBUG */
Linus Torvalds's avatar
Linus Torvalds committed
203 204

    board = find_lboard((lboard_t *) KL_CONFIG_INFO(nasid),
David Mosberger's avatar
David Mosberger committed
205
			KLTYPE_IOBRICK_XBOW);
Linus Torvalds's avatar
Linus Torvalds committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

    if (! board || KL_CONFIG_DUPLICATE_BOARD(board))
	return 0;

    comp = GET_SNUM_COMP(board);

    if (comp) {
#if LDEBUG
	    int i;

	    printf("********found module with id %x and string", m->id);

	    for (i = 0; i < MAX_SERIAL_NUM_SIZE; i++)
		printf(" %x ", comp->snum.snum_str[i]);

	    printf("\n");	/* Fudged string is not ASCII */
#endif

	    if (comp->snum.snum_str[0] != '\0') {
		bcopy(comp->snum.snum_str,
David Mosberger's avatar
David Mosberger committed
226
		      m->sys_snum,
Linus Torvalds's avatar
Linus Torvalds committed
227
		      MAX_SERIAL_NUM_SIZE);
David Mosberger's avatar
David Mosberger committed
228
		m->sys_snum_valid = 1;
Linus Torvalds's avatar
Linus Torvalds committed
229 230 231
	    }
    }

David Mosberger's avatar
David Mosberger committed
232
    if (m->sys_snum_valid)
Linus Torvalds's avatar
Linus Torvalds committed
233 234
	return 1;
    else {
Linus Torvalds's avatar
Linus Torvalds committed
235
	DPRINTF("Invalid serial number for module %d, "
Linus Torvalds's avatar
Linus Torvalds committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
		"possible missing or invalid NIC.", m->id);
	return 0;
    }
}

void
io_module_init(void)
{
    cnodeid_t		node;
    lboard_t	       *board;
    nasid_t		nasid;
    int			nserial;
    module_t	       *m;

    DPRINTF("*******module_init\n");

    nserial = 0;

    for (node = 0; node < numnodes; node++) {
	nasid = COMPACT_TO_NASID_NODEID(node);

David Mosberger's avatar
David Mosberger committed
257
	board = find_lboard((lboard_t *) KL_CONFIG_INFO(nasid), KLTYPE_SNIA);
Linus Torvalds's avatar
Linus Torvalds committed
258 259 260 261 262 263 264 265 266 267 268 269
	ASSERT(board);

	m = module_add_node(board->brd_module, node);

	if (! m->snum_valid && module_probe_snum(m, nasid))
	    nserial++;
    }

    DPRINTF("********found total of %d serial numbers in the system\n",
	    nserial);

    if (nserial == 0)
David Mosberger's avatar
David Mosberger committed
270
	printk(KERN_WARNING  "io_module_init: No serial number found.\n");
Linus Torvalds's avatar
Linus Torvalds committed
271 272 273 274
}

elsc_t *get_elsc(void)
{
Linus Torvalds's avatar
Linus Torvalds committed
275
	return &NODEPDA(cpuid_to_cnodeid(smp_processor_id()))->module->elsc;
Linus Torvalds's avatar
Linus Torvalds committed
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
}

int
get_kmod_info(cmoduleid_t cmod, module_info_t *mod_info)
{
    int i;

    if (cmod < 0 || cmod >= nummodules)
	return EINVAL;

    if (! modules[cmod]->snum_valid)
	return ENXIO;

    mod_info->mod_num = modules[cmod]->id;
    {
	char temp[MAX_SERIAL_NUM_SIZE];

	decode_str_serial(modules[cmod]->snum.snum_str, temp);

	/* if this is an invalid serial number return an error */
	if (temp[0] != 'K')
	    return ENXIO;

	mod_info->serial_num = 0;

	for (i = 0; i < MAX_SERIAL_NUM_SIZE && temp[i] != '\0'; i++) {
	    mod_info->serial_num <<= 4;
	    mod_info->serial_num |= (temp[i] & 0xf);

	    mod_info->serial_str[i] = temp[i];
	}

	mod_info->serial_str[i] = '\0';
    }

    return 0;
}