xref: /titanic_50/usr/src/uts/sun4u/opl/io/mc-opl.c (revision c77a61a72b5ecdc507d6cf104142edd371a16c84)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * All Rights Reserved, Copyright (c) FUJITSU LIMITED 2006
27  */
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <sys/types.h>
32 #include <sys/sysmacros.h>
33 #include <sys/conf.h>
34 #include <sys/modctl.h>
35 #include <sys/stat.h>
36 #include <sys/async.h>
37 #include <sys/machcpuvar.h>
38 #include <sys/machsystm.h>
39 #include <sys/promif.h>
40 #include <sys/ksynch.h>
41 #include <sys/ddi.h>
42 #include <sys/sunddi.h>
43 #include <sys/ddifm.h>
44 #include <sys/fm/protocol.h>
45 #include <sys/fm/util.h>
46 #include <sys/kmem.h>
47 #include <sys/fm/io/opl_mc_fm.h>
48 #include <sys/memlist.h>
49 #include <sys/param.h>
50 #include <sys/disp.h>
51 #include <vm/page.h>
52 #include <sys/mc-opl.h>
53 #include <sys/opl.h>
54 #include <sys/opl_dimm.h>
55 #include <sys/scfd/scfostoescf.h>
56 #include <sys/cpu_module.h>
57 #include <vm/seg_kmem.h>
58 #include <sys/vmem.h>
59 #include <vm/hat_sfmmu.h>
60 #include <sys/vmsystm.h>
61 #include <sys/membar.h>
62 
63 /*
64  * Function prototypes
65  */
66 static int mc_open(dev_t *, int, int, cred_t *);
67 static int mc_close(dev_t, int, int, cred_t *);
68 static int mc_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
69 static int mc_attach(dev_info_t *, ddi_attach_cmd_t);
70 static int mc_detach(dev_info_t *, ddi_detach_cmd_t);
71 
72 static int mc_poll_init(void);
73 static void mc_poll_fini(void);
74 static int mc_board_add(mc_opl_t *mcp);
75 static int mc_board_del(mc_opl_t *mcp);
76 static int mc_suspend(mc_opl_t *mcp, uint32_t flag);
77 static int mc_resume(mc_opl_t *mcp, uint32_t flag);
78 int opl_mc_suspend(void);
79 int opl_mc_resume(void);
80 
81 static void insert_mcp(mc_opl_t *mcp);
82 static void delete_mcp(mc_opl_t *mcp);
83 
84 static int pa_to_maddr(mc_opl_t *mcp, uint64_t pa, mc_addr_t *maddr);
85 
86 static int mc_rangecheck_pa(mc_opl_t *mcp, uint64_t pa);
87 
88 int mc_get_mem_unum(int, uint64_t, char *, int, int *);
89 int mc_get_mem_addr(char *unum, char *sid, uint64_t offset, uint64_t *paddr);
90 int mc_get_mem_offset(uint64_t paddr, uint64_t *offp);
91 int mc_get_mem_sid(char *unum, char *buf, int buflen, int *lenp);
92 int mc_get_mem_sid_dimm(mc_opl_t *mcp, char *dname, char *buf,
93     int buflen, int *lenp);
94 mc_dimm_info_t *mc_get_dimm_list(mc_opl_t *mcp);
95 mc_dimm_info_t *mc_prepare_dimmlist(board_dimm_info_t *bd_dimmp);
96 int mc_set_mem_sid(mc_opl_t *mcp, char *buf, int buflen, int lsb, int bank,
97     uint32_t mf_type, uint32_t d_slot);
98 static void mc_free_dimm_list(mc_dimm_info_t *d);
99 static void mc_get_mlist(mc_opl_t *);
100 static void mc_polling(void);
101 static int mc_opl_get_physical_board(int);
102 
103 #ifdef	DEBUG
104 static int mc_ioctl_debug(dev_t, int, intptr_t, int, cred_t *, int *);
105 void mc_dump_dimm(char *buf, int dnamesz, int serialsz, int partnumsz);
106 void mc_dump_dimm_info(board_dimm_info_t *bd_dimmp);
107 #endif
108 
109 #pragma weak opl_get_physical_board
110 extern int opl_get_physical_board(int);
111 extern int plat_max_boards(void);
112 
113 /*
114  * Configuration data structures
115  */
116 static struct cb_ops mc_cb_ops = {
117 	mc_open,			/* open */
118 	mc_close,			/* close */
119 	nulldev,			/* strategy */
120 	nulldev,			/* print */
121 	nodev,				/* dump */
122 	nulldev,			/* read */
123 	nulldev,			/* write */
124 	mc_ioctl,			/* ioctl */
125 	nodev,				/* devmap */
126 	nodev,				/* mmap */
127 	nodev,				/* segmap */
128 	nochpoll,			/* poll */
129 	ddi_prop_op,			/* cb_prop_op */
130 	0,				/* streamtab */
131 	D_MP | D_NEW | D_HOTPLUG,	/* Driver compatibility flag */
132 	CB_REV,				/* rev */
133 	nodev,				/* cb_aread */
134 	nodev				/* cb_awrite */
135 };
136 
137 static struct dev_ops mc_ops = {
138 	DEVO_REV,			/* rev */
139 	0,				/* refcnt  */
140 	ddi_getinfo_1to1,		/* getinfo */
141 	nulldev,			/* identify */
142 	nulldev,			/* probe */
143 	mc_attach,			/* attach */
144 	mc_detach,			/* detach */
145 	nulldev,			/* reset */
146 	&mc_cb_ops,			/* cb_ops */
147 	(struct bus_ops *)0,		/* bus_ops */
148 	nulldev				/* power */
149 };
150 
151 /*
152  * Driver globals
153  */
154 
155 static enum {
156 	MODEL_FF1 = 0,
157 	MODEL_FF2 = 1,
158 	MODEL_DC = 2
159 } plat_model = MODEL_DC;	/* The default behaviour is DC */
160 
161 static struct plat_model_names {
162 	const char *unit_name;
163 	const char *mem_name;
164 } model_names[] = {
165 	{ "MBU_A", "MEMB" },
166 	{ "MBU_B", "MEMB" },
167 	{ "CMU", "" }
168 };
169 
170 /*
171  * The DIMM Names for DC platform.
172  * The index into this table is made up of (bank, dslot),
173  * Where dslot occupies bits 0-1 and bank occupies 2-4.
174  */
175 static char *mc_dc_dimm_unum_table[OPL_MAX_DIMMS] = {
176 	/* --------CMUnn----------- */
177 	/* --CS0-----|--CS1------ */
178 	/* -H-|--L-- | -H- | -L-- */
179 	"03A", "02A", "03B", "02B", /* Bank 0 (MAC 0 bank 0) */
180 	"13A", "12A", "13B", "12B", /* Bank 1 (MAC 0 bank 1) */
181 	"23A", "22A", "23B", "22B", /* Bank 2 (MAC 1 bank 0) */
182 	"33A", "32A", "33B", "32B", /* Bank 3 (MAC 1 bank 1) */
183 	"01A", "00A", "01B", "00B", /* Bank 4 (MAC 2 bank 0) */
184 	"11A", "10A", "11B", "10B", /* Bank 5 (MAC 2 bank 1) */
185 	"21A", "20A", "21B", "20B", /* Bank 6 (MAC 3 bank 0) */
186 	"31A", "30A", "31B", "30B"  /* Bank 7 (MAC 3 bank 1) */
187 };
188 
189 /*
190  * The DIMM Names for FF1/FF2 platforms.
191  * The index into this table is made up of (board, bank, dslot),
192  * Where dslot occupies bits 0-1, bank occupies 2-4 and
193  * board occupies the bit 5.
194  */
195 static char *mc_ff_dimm_unum_table[2 * OPL_MAX_DIMMS] = {
196 	/* --------CMU0---------- */
197 	/* --CS0-----|--CS1------ */
198 	/* -H-|--L-- | -H- | -L-- */
199 	"03A", "02A", "03B", "02B", /* Bank 0 (MAC 0 bank 0) */
200 	"01A", "00A", "01B", "00B", /* Bank 1 (MAC 0 bank 1) */
201 	"13A", "12A", "13B", "12B", /* Bank 2 (MAC 1 bank 0) */
202 	"11A", "10A", "11B", "10B", /* Bank 3 (MAC 1 bank 1) */
203 	"23A", "22A", "23B", "22B", /* Bank 4 (MAC 2 bank 0) */
204 	"21A", "20A", "21B", "20B", /* Bank 5 (MAC 2 bank 1) */
205 	"33A", "32A", "33B", "32B", /* Bank 6 (MAC 3 bank 0) */
206 	"31A", "30A", "31B", "30B", /* Bank 7 (MAC 3 bank 1) */
207 	/* --------CMU1---------- */
208 	/* --CS0-----|--CS1------ */
209 	/* -H-|--L-- | -H- | -L-- */
210 	"43A", "42A", "43B", "42B", /* Bank 0 (MAC 0 bank 0) */
211 	"41A", "40A", "41B", "40B", /* Bank 1 (MAC 0 bank 1) */
212 	"53A", "52A", "53B", "52B", /* Bank 2 (MAC 1 bank 0) */
213 	"51A", "50A", "51B", "50B", /* Bank 3 (MAC 1 bank 1) */
214 	"63A", "62A", "63B", "62B", /* Bank 4 (MAC 2 bank 0) */
215 	"61A", "60A", "61B", "60B", /* Bank 5 (MAC 2 bank 1) */
216 	"73A", "72A", "73B", "72B", /* Bank 6 (MAC 3 bank 0) */
217 	"71A", "70A", "71B", "70B"  /* Bank 7 (MAC 3 bank 1) */
218 };
219 
220 #define	BD_BK_SLOT_TO_INDEX(bd, bk, s)			\
221 	(((bd & 0x01) << 5) | ((bk & 0x07) << 2) | (s & 0x03))
222 
223 #define	INDEX_TO_BANK(i)			(((i) & 0x1C) >> 2)
224 #define	INDEX_TO_SLOT(i)			((i) & 0x03)
225 
226 #define	SLOT_TO_CS(slot)	((slot & 0x3) >> 1)
227 
228 /* Isolation unit size is 64 MB */
229 #define	MC_ISOLATION_BSIZE	(64 * 1024 * 1024)
230 
231 #define	MC_MAX_SPEEDS 7
232 
233 typedef struct {
234 	uint32_t mc_speeds;
235 	uint32_t mc_period;
236 } mc_scan_speed_t;
237 
238 #define	MC_CNTL_SPEED_SHIFT 26
239 
240 /*
241  * In mirror mode, we normalized the bank idx to "even" since
242  * the HW treats them as one unit w.r.t programming.
243  * This bank index will be the "effective" bank index.
244  * All mirrored bank state info on mc_period, mc_speedup_period
245  * will be stored in the even bank structure to avoid code duplication.
246  */
247 #define	MIRROR_IDX(bankidx)	(bankidx & ~1)
248 
249 static mc_scan_speed_t	mc_scan_speeds[MC_MAX_SPEEDS] = {
250 	{0x6 << MC_CNTL_SPEED_SHIFT, 0},
251 	{0x5 << MC_CNTL_SPEED_SHIFT, 32},
252 	{0x4 << MC_CNTL_SPEED_SHIFT, 64},
253 	{0x3 << MC_CNTL_SPEED_SHIFT, 128},
254 	{0x2 << MC_CNTL_SPEED_SHIFT, 256},
255 	{0x1 << MC_CNTL_SPEED_SHIFT, 512},
256 	{0x0 << MC_CNTL_SPEED_SHIFT, 1024}
257 };
258 
259 static uint32_t	mc_max_speed = (0x6 << 26);
260 
261 int mc_isolation_bsize = MC_ISOLATION_BSIZE;
262 int mc_patrol_interval_sec = MC_PATROL_INTERVAL_SEC;
263 int mc_max_scf_retry = 16;
264 int mc_max_scf_logs = 64;
265 int mc_max_errlog_processed = BANKNUM_PER_SB*2;
266 int mc_scan_period = 12 * 60 * 60;	/* 12 hours period */
267 int mc_max_rewrite_loop = 100;
268 int mc_rewrite_delay = 10;
269 /*
270  * it takes SCF about 300 m.s. to process a requst.  We can bail out
271  * if it is busy.  It does not pay to wait for it too long.
272  */
273 int mc_max_scf_loop = 2;
274 int mc_scf_delay = 100;
275 int mc_pce_dropped = 0;
276 int mc_poll_priority = MINCLSYSPRI;
277 
278 
279 /*
280  * Mutex hierarchy in mc-opl
281  * If both mcmutex and mc_lock must be held,
282  * mcmutex must be acquired first, and then mc_lock.
283  */
284 
285 static kmutex_t mcmutex;
286 mc_opl_t *mc_instances[OPL_MAX_BOARDS];
287 
288 static kmutex_t mc_polling_lock;
289 static kcondvar_t mc_polling_cv;
290 static kcondvar_t mc_poll_exit_cv;
291 static int mc_poll_cmd = 0;
292 static int mc_pollthr_running = 0;
293 int mc_timeout_period = 0; /* this is in m.s. */
294 void *mc_statep;
295 
296 #ifdef	DEBUG
297 int oplmc_debug = 0;
298 #endif
299 
300 static int mc_debug_show_all = 0;
301 
302 extern struct mod_ops mod_driverops;
303 
304 static struct modldrv modldrv = {
305 	&mod_driverops,			/* module type, this one is a driver */
306 	"OPL Memory-controller %I%",	/* module name */
307 	&mc_ops,			/* driver ops */
308 };
309 
310 static struct modlinkage modlinkage = {
311 	MODREV_1,		/* rev */
312 	(void *)&modldrv,
313 	NULL
314 };
315 
316 #pragma weak opl_get_mem_unum
317 #pragma weak opl_get_mem_sid
318 #pragma weak opl_get_mem_offset
319 #pragma weak opl_get_mem_addr
320 
321 extern int (*opl_get_mem_unum)(int, uint64_t, char *, int, int *);
322 extern int (*opl_get_mem_sid)(char *unum, char *buf, int buflen, int *lenp);
323 extern int (*opl_get_mem_offset)(uint64_t paddr, uint64_t *offp);
324 extern int (*opl_get_mem_addr)(char *unum, char *sid, uint64_t offset,
325     uint64_t *paddr);
326 
327 
328 /*
329  * pseudo-mc node portid format
330  *
331  *		[10]   = 0
332  *		[9]    = 1
333  *		[8]    = LSB_ID[4] = 0
334  *		[7:4]  = LSB_ID[3:0]
335  *		[3:0]  = 0
336  *
337  */
338 
339 /*
340  * These are the module initialization routines.
341  */
342 int
343 _init(void)
344 {
345 	int	error;
346 	int	plen;
347 	char	model[20];
348 	pnode_t	node;
349 
350 
351 	if ((error = ddi_soft_state_init(&mc_statep,
352 	    sizeof (mc_opl_t), 1)) != 0)
353 		return (error);
354 
355 	if ((error = mc_poll_init()) != 0) {
356 		ddi_soft_state_fini(&mc_statep);
357 		return (error);
358 	}
359 
360 	mutex_init(&mcmutex, NULL, MUTEX_DRIVER, NULL);
361 	if (&opl_get_mem_unum)
362 		opl_get_mem_unum = mc_get_mem_unum;
363 	if (&opl_get_mem_sid)
364 		opl_get_mem_sid = mc_get_mem_sid;
365 	if (&opl_get_mem_offset)
366 		opl_get_mem_offset = mc_get_mem_offset;
367 	if (&opl_get_mem_addr)
368 		opl_get_mem_addr = mc_get_mem_addr;
369 
370 	node = prom_rootnode();
371 	plen = prom_getproplen(node, "model");
372 
373 	if (plen > 0 && plen < sizeof (model)) {
374 		(void) prom_getprop(node, "model", model);
375 		model[plen] = '\0';
376 		if (strcmp(model, "FF1") == 0)
377 			plat_model = MODEL_FF1;
378 		else if (strcmp(model, "FF2") == 0)
379 			plat_model = MODEL_FF2;
380 		else if (strncmp(model, "DC", 2) == 0)
381 			plat_model = MODEL_DC;
382 	}
383 
384 	error =  mod_install(&modlinkage);
385 	if (error != 0) {
386 		if (&opl_get_mem_unum)
387 			opl_get_mem_unum = NULL;
388 		if (&opl_get_mem_sid)
389 			opl_get_mem_sid = NULL;
390 		if (&opl_get_mem_offset)
391 			opl_get_mem_offset = NULL;
392 		if (&opl_get_mem_addr)
393 			opl_get_mem_addr = NULL;
394 		mutex_destroy(&mcmutex);
395 		mc_poll_fini();
396 		ddi_soft_state_fini(&mc_statep);
397 	}
398 	return (error);
399 }
400 
401 int
402 _fini(void)
403 {
404 	int error;
405 
406 	if ((error = mod_remove(&modlinkage)) != 0)
407 		return (error);
408 
409 	if (&opl_get_mem_unum)
410 		opl_get_mem_unum = NULL;
411 	if (&opl_get_mem_sid)
412 		opl_get_mem_sid = NULL;
413 	if (&opl_get_mem_offset)
414 		opl_get_mem_offset = NULL;
415 	if (&opl_get_mem_addr)
416 		opl_get_mem_addr = NULL;
417 
418 	mutex_destroy(&mcmutex);
419 	mc_poll_fini();
420 	ddi_soft_state_fini(&mc_statep);
421 
422 	return (0);
423 }
424 
425 int
426 _info(struct modinfo *modinfop)
427 {
428 	return (mod_info(&modlinkage, modinfop));
429 }
430 
431 static void
432 mc_polling_thread()
433 {
434 	mutex_enter(&mc_polling_lock);
435 	mc_pollthr_running = 1;
436 	while (!(mc_poll_cmd & MC_POLL_EXIT)) {
437 		mc_polling();
438 		cv_timedwait(&mc_polling_cv, &mc_polling_lock,
439 		    ddi_get_lbolt() + mc_timeout_period);
440 	}
441 	mc_pollthr_running = 0;
442 
443 	/*
444 	 * signal if any one is waiting for this thread to exit.
445 	 */
446 	cv_signal(&mc_poll_exit_cv);
447 	mutex_exit(&mc_polling_lock);
448 	thread_exit();
449 	/* NOTREACHED */
450 }
451 
452 static int
453 mc_poll_init()
454 {
455 	mutex_init(&mc_polling_lock, NULL, MUTEX_DRIVER, NULL);
456 	cv_init(&mc_polling_cv, NULL, CV_DRIVER, NULL);
457 	cv_init(&mc_poll_exit_cv, NULL, CV_DRIVER, NULL);
458 	return (0);
459 }
460 
461 static void
462 mc_poll_fini()
463 {
464 	mutex_enter(&mc_polling_lock);
465 	if (mc_pollthr_running) {
466 		mc_poll_cmd = MC_POLL_EXIT;
467 		cv_signal(&mc_polling_cv);
468 		while (mc_pollthr_running) {
469 			cv_wait(&mc_poll_exit_cv, &mc_polling_lock);
470 		}
471 	}
472 	mutex_exit(&mc_polling_lock);
473 	mutex_destroy(&mc_polling_lock);
474 	cv_destroy(&mc_polling_cv);
475 	cv_destroy(&mc_poll_exit_cv);
476 }
477 
478 static int
479 mc_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
480 {
481 	mc_opl_t *mcp;
482 	int instance;
483 	int rv;
484 
485 	/* get the instance of this devi */
486 	instance = ddi_get_instance(devi);
487 
488 	switch (cmd) {
489 	case DDI_ATTACH:
490 		break;
491 	case DDI_RESUME:
492 		mcp = ddi_get_soft_state(mc_statep, instance);
493 		rv = mc_resume(mcp, MC_DRIVER_SUSPENDED);
494 		return (rv);
495 	default:
496 		return (DDI_FAILURE);
497 	}
498 
499 	if (ddi_soft_state_zalloc(mc_statep, instance) != DDI_SUCCESS)
500 		return (DDI_FAILURE);
501 
502 	if ((mcp = ddi_get_soft_state(mc_statep, instance)) == NULL) {
503 		goto bad;
504 	}
505 
506 	if (mc_timeout_period == 0) {
507 		mc_patrol_interval_sec = (int)ddi_getprop(DDI_DEV_T_ANY, devi,
508 			DDI_PROP_DONTPASS, "mc-timeout-interval-sec",
509 			mc_patrol_interval_sec);
510 		mc_timeout_period = drv_usectohz(
511 			1000000 * mc_patrol_interval_sec / OPL_MAX_BOARDS);
512 	}
513 
514 	/* set informations in mc state */
515 	mcp->mc_dip = devi;
516 
517 	if (mc_board_add(mcp))
518 		goto bad;
519 
520 	insert_mcp(mcp);
521 
522 	/*
523 	 * Start the polling thread if it is not running already.
524 	 */
525 	mutex_enter(&mc_polling_lock);
526 	if (!mc_pollthr_running) {
527 		(void) thread_create(NULL, 0, (void (*)())mc_polling_thread,
528 			NULL, 0, &p0, TS_RUN, mc_poll_priority);
529 	}
530 	mutex_exit(&mc_polling_lock);
531 	ddi_report_dev(devi);
532 
533 	return (DDI_SUCCESS);
534 
535 bad:
536 	ddi_soft_state_free(mc_statep, instance);
537 	return (DDI_FAILURE);
538 }
539 
540 /* ARGSUSED */
541 static int
542 mc_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
543 {
544 	int rv;
545 	int instance;
546 	mc_opl_t *mcp;
547 
548 	/* get the instance of this devi */
549 	instance = ddi_get_instance(devi);
550 	if ((mcp = ddi_get_soft_state(mc_statep, instance)) == NULL) {
551 		return (DDI_FAILURE);
552 	}
553 
554 	switch (cmd) {
555 	case DDI_SUSPEND:
556 		rv = mc_suspend(mcp, MC_DRIVER_SUSPENDED);
557 		return (rv);
558 	case DDI_DETACH:
559 		break;
560 	default:
561 		return (DDI_FAILURE);
562 	}
563 
564 	delete_mcp(mcp);
565 	if (mc_board_del(mcp) != DDI_SUCCESS) {
566 		return (DDI_FAILURE);
567 	}
568 
569 	/* free up the soft state */
570 	ddi_soft_state_free(mc_statep, instance);
571 
572 	return (DDI_SUCCESS);
573 }
574 
575 /* ARGSUSED */
576 static int
577 mc_open(dev_t *devp, int flag, int otyp, cred_t *credp)
578 {
579 	return (0);
580 }
581 
582 /* ARGSUSED */
583 static int
584 mc_close(dev_t devp, int flag, int otyp, cred_t *credp)
585 {
586 	return (0);
587 }
588 
589 /* ARGSUSED */
590 static int
591 mc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
592 	int *rvalp)
593 {
594 #ifdef DEBUG
595 	return (mc_ioctl_debug(dev, cmd, arg, mode, credp, rvalp));
596 #else
597 	return (ENXIO);
598 #endif
599 }
600 
601 /*
602  * PA validity check:
603  * This function return 1 if the PA is a valid PA
604  * in the running Solaris instance i.e. in physinstall
605  * Otherwise, return 0.
606  */
607 
608 /* ARGSUSED */
609 static int
610 pa_is_valid(mc_opl_t *mcp, uint64_t addr)
611 {
612 	if (mcp->mlist == NULL)
613 		mc_get_mlist(mcp);
614 
615 	if (mcp->mlist && address_in_memlist(mcp->mlist, addr, 0)) {
616 		return (1);
617 	}
618 	return (0);
619 }
620 
621 /*
622  * mac-pa translation routines.
623  *
624  *    Input: mc driver state, (LSB#, Bank#, DIMM address)
625  *    Output: physical address
626  *
627  *    Valid   - return value:  0
628  *    Invalid - return value: -1
629  */
630 static int
631 mcaddr_to_pa(mc_opl_t *mcp, mc_addr_t *maddr, uint64_t *pa)
632 {
633 	int i;
634 	uint64_t pa_offset = 0;
635 	int cs = (maddr->ma_dimm_addr >> CS_SHIFT) & 1;
636 	int bank = maddr->ma_bank;
637 	mc_addr_t maddr1;
638 	int bank0, bank1;
639 
640 	MC_LOG("mcaddr /LSB%d/B%d/%x\n", maddr->ma_bd, bank,
641 		maddr->ma_dimm_addr);
642 
643 	/* loc validity check */
644 	ASSERT(maddr->ma_bd >= 0 && OPL_BOARD_MAX > maddr->ma_bd);
645 	ASSERT(bank >= 0 && OPL_BANK_MAX > bank);
646 
647 	/* Do translation */
648 	for (i = 0; i < PA_BITS_FOR_MAC; i++) {
649 		int pa_bit = 0;
650 		int mc_bit = mcp->mc_trans_table[cs][i];
651 		if (mc_bit < MC_ADDRESS_BITS) {
652 			pa_bit = (maddr->ma_dimm_addr >> mc_bit) & 1;
653 		} else if (mc_bit == MP_NONE) {
654 			pa_bit = 0;
655 		} else if (mc_bit == MP_BANK_0) {
656 			pa_bit = bank & 1;
657 		} else if (mc_bit == MP_BANK_1) {
658 			pa_bit = (bank >> 1) & 1;
659 		} else if (mc_bit == MP_BANK_2) {
660 			pa_bit = (bank >> 2) & 1;
661 		}
662 		pa_offset |= ((uint64_t)pa_bit) << i;
663 	}
664 	*pa = mcp->mc_start_address + pa_offset;
665 	MC_LOG("pa = %lx\n", *pa);
666 
667 	if (pa_to_maddr(mcp, *pa, &maddr1) == -1) {
668 		cmn_err(CE_WARN, "mcaddr_to_pa: /LSB%d/B%d/%x failed to "
669 		    "convert PA %lx\n", maddr->ma_bd, bank,
670 		    maddr->ma_dimm_addr, *pa);
671 		return (-1);
672 	}
673 
674 	/*
675 	 * In mirror mode, PA is always translated to the even bank.
676 	 */
677 	if (IS_MIRROR(mcp, maddr->ma_bank)) {
678 		bank0 = maddr->ma_bank & ~(1);
679 		bank1 = maddr1.ma_bank & ~(1);
680 	} else {
681 		bank0 = maddr->ma_bank;
682 		bank1 = maddr1.ma_bank;
683 	}
684 	/*
685 	 * there is no need to check ma_bd because it is generated from
686 	 * mcp.  They are the same.
687 	 */
688 	if ((bank0 == bank1) &&
689 		(maddr->ma_dimm_addr == maddr1.ma_dimm_addr)) {
690 		return (0);
691 	} else {
692 		cmn_err(CE_WARN, "Translation error source /LSB%d/B%d/%x, "
693 			"PA %lx, target /LSB%d/B%d/%x\n",
694 			maddr->ma_bd, bank, maddr->ma_dimm_addr,
695 			*pa, maddr1.ma_bd, maddr1.ma_bank,
696 			maddr1.ma_dimm_addr);
697 		return (-1);
698 	}
699 }
700 
701 /*
702  * PA to CS (used by pa_to_maddr).
703  */
704 static int
705 pa_to_cs(mc_opl_t *mcp, uint64_t pa_offset)
706 {
707 	int i;
708 	int cs = 1;
709 
710 	for (i = 0; i < PA_BITS_FOR_MAC; i++) {
711 		/* MAC address bit<29> is arranged on the same PA bit */
712 		/* on both table. So we may use any table. */
713 		if (mcp->mc_trans_table[0][i] == CS_SHIFT) {
714 			cs = (pa_offset >> i) & 1;
715 			break;
716 		}
717 	}
718 	return (cs);
719 }
720 
721 /*
722  * PA to DIMM (used by pa_to_maddr).
723  */
724 /* ARGSUSED */
725 static uint32_t
726 pa_to_dimm(mc_opl_t *mcp, uint64_t pa_offset)
727 {
728 	int i;
729 	int cs = pa_to_cs(mcp, pa_offset);
730 	uint32_t dimm_addr = 0;
731 
732 	for (i = 0; i < PA_BITS_FOR_MAC; i++) {
733 		int pa_bit_value = (pa_offset >> i) & 1;
734 		int mc_bit = mcp->mc_trans_table[cs][i];
735 		if (mc_bit < MC_ADDRESS_BITS) {
736 			dimm_addr |= pa_bit_value << mc_bit;
737 		}
738 	}
739 	dimm_addr |= cs << CS_SHIFT;
740 	return (dimm_addr);
741 }
742 
743 /*
744  * PA to Bank (used by pa_to_maddr).
745  */
746 static int
747 pa_to_bank(mc_opl_t *mcp, uint64_t pa_offset)
748 {
749 	int i;
750 	int cs = pa_to_cs(mcp, pa_offset);
751 	int bankno = mcp->mc_trans_table[cs][INDEX_OF_BANK_SUPPLEMENT_BIT];
752 
753 
754 	for (i = 0; i < PA_BITS_FOR_MAC; i++) {
755 		int pa_bit_value = (pa_offset >> i) & 1;
756 		int mc_bit = mcp->mc_trans_table[cs][i];
757 		switch (mc_bit) {
758 		case MP_BANK_0:
759 			bankno |= pa_bit_value;
760 			break;
761 		case MP_BANK_1:
762 			bankno |= pa_bit_value << 1;
763 			break;
764 		case MP_BANK_2:
765 			bankno |= pa_bit_value << 2;
766 			break;
767 		}
768 	}
769 
770 	return (bankno);
771 }
772 
773 /*
774  * PA to MAC address translation
775  *
776  *   Input: MAC driver state, physicall adress
777  *   Output: LSB#, Bank id, mac address
778  *
779  *    Valid   - return value:  0
780  *    Invalid - return value: -1
781  */
782 
783 int
784 pa_to_maddr(mc_opl_t *mcp, uint64_t pa, mc_addr_t *maddr)
785 {
786 	uint64_t pa_offset;
787 
788 	if (!mc_rangecheck_pa(mcp, pa))
789 		return (-1);
790 
791 	/* Do translation */
792 	pa_offset = pa - mcp->mc_start_address;
793 
794 	maddr->ma_bd = mcp->mc_board_num;
795 	maddr->ma_phys_bd = mcp->mc_phys_board_num;
796 	maddr->ma_bank = pa_to_bank(mcp, pa_offset);
797 	maddr->ma_dimm_addr = pa_to_dimm(mcp, pa_offset);
798 	MC_LOG("pa %lx -> mcaddr /LSB%d/B%d/%x\n",
799 		pa_offset, maddr->ma_bd, maddr->ma_bank, maddr->ma_dimm_addr);
800 	return (0);
801 }
802 
803 /*
804  * UNUM format for DC is "/CMUnn/MEMxyZ", where
805  *	nn = 00..03 for DC1 and 00..07 for DC2 and 00..15 for DC3.
806  *	x = MAC 0..3
807  *	y = 0..3 (slot info).
808  *	Z = 'A' or 'B'
809  *
810  * UNUM format for FF1 is "/MBU_A/MEMBx/MEMyZ", where
811  *	x = 0..3 (MEMB number)
812  *	y = 0..3 (slot info).
813  *	Z = 'A' or 'B'
814  *
815  * UNUM format for FF2 is "/MBU_B/MEMBx/MEMyZ"
816  *	x = 0..7 (MEMB number)
817  *	y = 0..3 (slot info).
818  *	Z = 'A' or 'B'
819  */
820 int
821 mc_set_mem_unum(char *buf, int buflen, int sb, int bank,
822     uint32_t mf_type, uint32_t d_slot)
823 {
824 	char *dimmnm;
825 	char memb_num;
826 	int cs;
827 	int i;
828 	int j;
829 
830 	cs = SLOT_TO_CS(d_slot);
831 
832 	if (plat_model == MODEL_DC) {
833 		if (mf_type == FLT_TYPE_PERMANENT_CE) {
834 			i = BD_BK_SLOT_TO_INDEX(0, bank, d_slot);
835 			dimmnm = mc_dc_dimm_unum_table[i];
836 			snprintf(buf, buflen, "/%s%02d/MEM%s",
837 			    model_names[plat_model].unit_name, sb, dimmnm);
838 		} else {
839 			i = BD_BK_SLOT_TO_INDEX(0, bank, 0);
840 			j = (cs == 0) ?  i : i + 2;
841 			snprintf(buf, buflen, "/%s%02d/MEM%s MEM%s",
842 			    model_names[plat_model].unit_name, sb,
843 			    mc_dc_dimm_unum_table[j],
844 			    mc_dc_dimm_unum_table[j + 1]);
845 		}
846 	} else {
847 		if (mf_type == FLT_TYPE_PERMANENT_CE) {
848 			i = BD_BK_SLOT_TO_INDEX(sb, bank, d_slot);
849 			dimmnm = mc_ff_dimm_unum_table[i];
850 			memb_num = dimmnm[0];
851 			snprintf(buf, buflen, "/%s/%s%c/MEM%s",
852 			    model_names[plat_model].unit_name,
853 			    model_names[plat_model].mem_name,
854 			    memb_num, &dimmnm[1]);
855 		} else {
856 			i = BD_BK_SLOT_TO_INDEX(sb, bank, 0);
857 			j = (cs == 0) ?  i : i + 2;
858 			memb_num = mc_ff_dimm_unum_table[i][0],
859 			snprintf(buf, buflen,
860 			    "/%s/%s%c/MEM%s MEM%s",
861 			    model_names[plat_model].unit_name,
862 			    model_names[plat_model].mem_name, memb_num,
863 			    &mc_ff_dimm_unum_table[j][1],
864 			    &mc_ff_dimm_unum_table[j + 1][1]);
865 		}
866 	}
867 	return (0);
868 }
869 
870 static void
871 mc_ereport_post(mc_aflt_t *mc_aflt)
872 {
873 	char buf[FM_MAX_CLASS];
874 	char device_path[MAXPATHLEN];
875 	char sid[MAXPATHLEN];
876 	nv_alloc_t *nva = NULL;
877 	nvlist_t *ereport, *detector, *resource;
878 	errorq_elem_t *eqep;
879 	int nflts;
880 	mc_flt_stat_t *flt_stat;
881 	int i, n;
882 	int blen = MAXPATHLEN;
883 	char *p, *s = NULL;
884 	uint32_t values[2], synd[2], dslot[2];
885 	uint64_t offset = (uint64_t)-1;
886 	int ret = -1;
887 
888 	if (panicstr) {
889 		eqep = errorq_reserve(ereport_errorq);
890 		if (eqep == NULL)
891 			return;
892 		ereport = errorq_elem_nvl(ereport_errorq, eqep);
893 		nva = errorq_elem_nva(ereport_errorq, eqep);
894 	} else {
895 		ereport = fm_nvlist_create(nva);
896 	}
897 
898 	/*
899 	 * Create the scheme "dev" FMRI.
900 	 */
901 	detector = fm_nvlist_create(nva);
902 	resource = fm_nvlist_create(nva);
903 
904 	nflts = mc_aflt->mflt_nflts;
905 
906 	ASSERT(nflts >= 1 && nflts <= 2);
907 
908 	flt_stat = mc_aflt->mflt_stat[0];
909 	(void) ddi_pathname(mc_aflt->mflt_mcp->mc_dip, device_path);
910 	(void) fm_fmri_dev_set(detector, FM_DEV_SCHEME_VERSION, NULL,
911 	    device_path, NULL);
912 
913 	/*
914 	 * Encode all the common data into the ereport.
915 	 */
916 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s-%s",
917 		MC_OPL_ERROR_CLASS,
918 		mc_aflt->mflt_is_ptrl ? MC_OPL_PTRL_SUBCLASS :
919 		MC_OPL_MI_SUBCLASS,
920 		mc_aflt->mflt_erpt_class);
921 
922 	MC_LOG("mc_ereport_post: ereport %s\n", buf);
923 
924 
925 	fm_ereport_set(ereport, FM_EREPORT_VERSION, buf,
926 		fm_ena_generate(mc_aflt->mflt_id, FM_ENA_FMT1),
927 		detector, NULL);
928 
929 	/*
930 	 * Set payload.
931 	 */
932 	fm_payload_set(ereport, MC_OPL_BOARD, DATA_TYPE_UINT32,
933 		flt_stat->mf_flt_maddr.ma_bd, NULL);
934 
935 	fm_payload_set(ereport, MC_OPL_PA, DATA_TYPE_UINT64,
936 		flt_stat->mf_flt_paddr, NULL);
937 
938 	if (flt_stat->mf_type == FLT_TYPE_PERMANENT_CE) {
939 		fm_payload_set(ereport, MC_OPL_FLT_TYPE,
940 			DATA_TYPE_UINT8, ECC_STICKY, NULL);
941 	}
942 
943 	for (i = 0; i < nflts; i++)
944 		values[i] = mc_aflt->mflt_stat[i]->mf_flt_maddr.ma_bank;
945 
946 	fm_payload_set(ereport, MC_OPL_BANK, DATA_TYPE_UINT32_ARRAY,
947 		nflts, values, NULL);
948 
949 	for (i = 0; i < nflts; i++)
950 		values[i] = mc_aflt->mflt_stat[i]->mf_cntl;
951 
952 	fm_payload_set(ereport, MC_OPL_STATUS, DATA_TYPE_UINT32_ARRAY,
953 		nflts, values, NULL);
954 
955 	for (i = 0; i < nflts; i++)
956 		values[i] = mc_aflt->mflt_stat[i]->mf_err_add;
957 
958 	/* offset is set only for PCE */
959 	if (mc_aflt->mflt_stat[0]->mf_type == FLT_TYPE_PERMANENT_CE) {
960 		offset = values[0];
961 
962 	}
963 	fm_payload_set(ereport, MC_OPL_ERR_ADD, DATA_TYPE_UINT32_ARRAY,
964 		nflts, values, NULL);
965 
966 	for (i = 0; i < nflts; i++)
967 		values[i] = mc_aflt->mflt_stat[i]->mf_err_log;
968 
969 	fm_payload_set(ereport, MC_OPL_ERR_LOG, DATA_TYPE_UINT32_ARRAY,
970 		nflts, values, NULL);
971 
972 	for (i = 0; i < nflts; i++) {
973 		flt_stat = mc_aflt->mflt_stat[i];
974 		if (flt_stat->mf_errlog_valid) {
975 			synd[i] = flt_stat->mf_synd;
976 			dslot[i] = flt_stat->mf_dimm_slot;
977 			values[i] = flt_stat->mf_dram_place;
978 		} else {
979 			synd[i] = 0;
980 			dslot[i] = 0;
981 			values[i] = 0;
982 		}
983 	}
984 
985 	fm_payload_set(ereport, MC_OPL_ERR_SYND,
986 		DATA_TYPE_UINT32_ARRAY, nflts, synd, NULL);
987 
988 	fm_payload_set(ereport, MC_OPL_ERR_DIMMSLOT,
989 		DATA_TYPE_UINT32_ARRAY, nflts, dslot, NULL);
990 
991 	fm_payload_set(ereport, MC_OPL_ERR_DRAM,
992 		DATA_TYPE_UINT32_ARRAY, nflts, values, NULL);
993 
994 	device_path[0] = 0;
995 	p = &device_path[0];
996 	sid[0] = 0;
997 	s = &sid[0];
998 	ret = 0;
999 
1000 	for (i = 0; i < nflts; i++) {
1001 		int bank;
1002 
1003 		flt_stat = mc_aflt->mflt_stat[i];
1004 		bank = flt_stat->mf_flt_maddr.ma_bank;
1005 		ret =  mc_set_mem_unum(p + strlen(p), blen,
1006 			flt_stat->mf_flt_maddr.ma_phys_bd, bank,
1007 			flt_stat->mf_type, flt_stat->mf_dimm_slot);
1008 
1009 		if (ret != 0) {
1010 			cmn_err(CE_WARN,
1011 			    "mc_ereport_post: Failed to determine the unum "
1012 			    "for board=%d bank=%d type=0x%x slot=0x%x",
1013 			    flt_stat->mf_flt_maddr.ma_bd, bank,
1014 			    flt_stat->mf_type, flt_stat->mf_dimm_slot);
1015 			continue;
1016 		}
1017 		n = strlen(device_path);
1018 		blen = MAXPATHLEN - n;
1019 		p = &device_path[n];
1020 		if (i < (nflts - 1)) {
1021 			snprintf(p, blen, " ");
1022 			blen--;
1023 			p++;
1024 		}
1025 
1026 		if (ret == 0) {
1027 			ret = mc_set_mem_sid(mc_aflt->mflt_mcp, s + strlen(s),
1028 			    blen, flt_stat->mf_flt_maddr.ma_phys_bd, bank,
1029 			    flt_stat->mf_type, flt_stat->mf_dimm_slot);
1030 
1031 		}
1032 	}
1033 
1034 	(void) fm_fmri_mem_set(resource, FM_MEM_SCHEME_VERSION,
1035 		NULL, device_path, (ret == 0) ? sid : NULL,
1036 		(ret == 0) ? offset : (uint64_t)-1);
1037 
1038 	fm_payload_set(ereport, MC_OPL_RESOURCE, DATA_TYPE_NVLIST,
1039 		resource, NULL);
1040 
1041 	if (panicstr) {
1042 		errorq_commit(ereport_errorq, eqep, ERRORQ_SYNC);
1043 	} else {
1044 		(void) fm_ereport_post(ereport, EVCH_TRYHARD);
1045 		fm_nvlist_destroy(ereport, FM_NVA_FREE);
1046 		fm_nvlist_destroy(detector, FM_NVA_FREE);
1047 		fm_nvlist_destroy(resource, FM_NVA_FREE);
1048 	}
1049 }
1050 
1051 
1052 static void
1053 mc_err_drain(mc_aflt_t *mc_aflt)
1054 {
1055 	int rv;
1056 	uint64_t pa = (uint64_t)(-1);
1057 	int i;
1058 
1059 	MC_LOG("mc_err_drain: %s\n",
1060 		mc_aflt->mflt_erpt_class);
1061 	/*
1062 	 * we come here only when we have:
1063 	 * In mirror mode: MUE, SUE
1064 	 * In normal mode: UE, Permanent CE
1065 	 */
1066 	for (i = 0; i < mc_aflt->mflt_nflts; i++) {
1067 		rv = mcaddr_to_pa(mc_aflt->mflt_mcp,
1068 			&(mc_aflt->mflt_stat[i]->mf_flt_maddr), &pa);
1069 
1070 		/* Ensure the pa is valid (not in isolated memory block) */
1071 		if (rv == 0 && pa_is_valid(mc_aflt->mflt_mcp, pa))
1072 			mc_aflt->mflt_stat[i]->mf_flt_paddr = pa;
1073 		else
1074 			mc_aflt->mflt_stat[i]->mf_flt_paddr = (uint64_t)-1;
1075 	}
1076 
1077 	MC_LOG("mc_err_drain:pa = %lx\n", pa);
1078 
1079 	switch (page_retire_check(pa, NULL)) {
1080 	case 0:
1081 	case EAGAIN:
1082 		MC_LOG("Page retired or pending\n");
1083 		return;
1084 	case EIO:
1085 		/*
1086 		 * Do page retirement except for the PCE case.
1087 		 * This is taken care by the OPL DE
1088 		 */
1089 		if (mc_aflt->mflt_stat[0]->mf_type != FLT_TYPE_PERMANENT_CE) {
1090 			MC_LOG("offline page at pa %lx error %x\n", pa,
1091 				mc_aflt->mflt_pr);
1092 			(void) page_retire(pa, mc_aflt->mflt_pr);
1093 		}
1094 		break;
1095 	case EINVAL:
1096 	default:
1097 		/*
1098 		 * Some memory do not have page structure so
1099 		 * we keep going in case of EINVAL.
1100 		 */
1101 		break;
1102 	}
1103 
1104 	for (i = 0; i < mc_aflt->mflt_nflts; i++) {
1105 		mc_aflt_t mc_aflt0;
1106 		if (mc_aflt->mflt_stat[i]->mf_flt_paddr != (uint64_t)-1) {
1107 			mc_aflt0 = *mc_aflt;
1108 			mc_aflt0.mflt_nflts = 1;
1109 			mc_aflt0.mflt_stat[0] = mc_aflt->mflt_stat[i];
1110 			mc_ereport_post(&mc_aflt0);
1111 		}
1112 	}
1113 }
1114 
1115 /*
1116  * The restart address is actually defined in unit of PA[37:6]
1117  * the mac patrol will convert that to dimm offset.  If the
1118  * address is not in the bank, it will continue to search for
1119  * the next PA that is within the bank.
1120  *
1121  * Also the mac patrol scans the dimms based on PA, not
1122  * dimm offset.
1123  */
1124 static int
1125 restart_patrol(mc_opl_t *mcp, int bank, mc_rsaddr_info_t *rsaddr_info)
1126 {
1127 	uint64_t pa;
1128 	int rv;
1129 
1130 	if (rsaddr_info == NULL || (rsaddr_info->mi_valid == 0)) {
1131 		MAC_PTRL_START(mcp, bank);
1132 		return (0);
1133 	}
1134 
1135 	rv = mcaddr_to_pa(mcp, &rsaddr_info->mi_restartaddr, &pa);
1136 	if (rv != 0) {
1137 		MC_LOG("cannot convert mcaddr to pa. use auto restart\n");
1138 		MAC_PTRL_START(mcp, bank);
1139 		return (0);
1140 	}
1141 
1142 	if (!mc_rangecheck_pa(mcp, pa)) {
1143 		/* pa is not on this board, just retry */
1144 		cmn_err(CE_WARN, "restart_patrol: invalid address %lx "
1145 			"on board %d\n", pa, mcp->mc_board_num);
1146 		MAC_PTRL_START(mcp, bank);
1147 		return (0);
1148 	}
1149 
1150 	MC_LOG("restart_patrol: pa = %lx\n", pa);
1151 
1152 	if (!rsaddr_info->mi_injectrestart) {
1153 		/*
1154 		 * For non-error injection restart we need to
1155 		 * determine if the current restart pa/page is
1156 		 * a "good" page. A "good" page is a page that
1157 		 * has not been page retired. If the current
1158 		 * page that contains the pa is "good", we will
1159 		 * do a HW auto restart and let HW patrol continue
1160 		 * where it last stopped. Most desired scenario.
1161 		 *
1162 		 * If the current page is not "good", we will advance
1163 		 * to the next page to find the next "good" page and
1164 		 * restart the patrol from there.
1165 		 */
1166 		int wrapcount = 0;
1167 		uint64_t origpa = pa;
1168 		while (wrapcount < 2) {
1169 		    if (!pa_is_valid(mcp, pa)) {
1170 			/*
1171 			 * Not in physinstall - advance to the
1172 			 * next memory isolation blocksize
1173 			 */
1174 			MC_LOG("Invalid PA\n");
1175 			pa = roundup(pa + 1, mc_isolation_bsize);
1176 		    } else {
1177 			int rv;
1178 			if ((rv = page_retire_check(pa, NULL)) != 0 &&
1179 			    rv != EAGAIN) {
1180 				/*
1181 				 * The page is "good" (not retired), we will
1182 				 * use automatic HW restart algorithm if
1183 				 * this is the original current starting page
1184 				 */
1185 				if (pa == origpa) {
1186 				    MC_LOG("Page has no error. Auto restart\n");
1187 				    MAC_PTRL_START(mcp, bank);
1188 				    return (0);
1189 				} else {
1190 				    /* found a subsequent good page */
1191 				    break;
1192 				}
1193 			}
1194 
1195 			/*
1196 			 * Skip to the next page
1197 			 */
1198 			pa = roundup(pa + 1, PAGESIZE);
1199 			MC_LOG("Skipping bad page to %lx\n", pa);
1200 		    }
1201 
1202 		    /* Check to see if we hit the end of the memory range */
1203 		    if (pa >= (mcp->mc_start_address + mcp->mc_size)) {
1204 			MC_LOG("Wrap around\n");
1205 			pa = mcp->mc_start_address;
1206 			wrapcount++;
1207 		    }
1208 		}
1209 
1210 		if (wrapcount > 1) {
1211 		    MC_LOG("Failed to find a good page. Just restart\n");
1212 		    MAC_PTRL_START(mcp, bank);
1213 		    return (0);
1214 		}
1215 	}
1216 
1217 	/*
1218 	 * We reached here either:
1219 	 * 1. We are doing an error injection restart that specify
1220 	 *    the exact pa/page to restart. OR
1221 	 * 2. We found a subsequent good page different from the
1222 	 *    original restart pa/page.
1223 	 * Restart MAC patrol: PA[37:6]
1224 	 */
1225 	MC_LOG("restart at pa = %lx\n", pa);
1226 	ST_MAC_REG(MAC_RESTART_ADD(mcp, bank), MAC_RESTART_PA(pa));
1227 	MAC_PTRL_START_ADD(mcp, bank);
1228 
1229 	return (0);
1230 }
1231 
1232 /*
1233  * Rewriting is used for two purposes.
1234  *  - to correct the error in memory.
1235  *  - to determine whether the error is permanent or intermittent.
1236  * It's done by writing the address in MAC_BANKm_REWRITE_ADD
1237  * and issuing REW_REQ command in MAC_BANKm_PTRL_CNRL. After that,
1238  * REW_END (and REW_CE/REW_UE if some error detected) is set when
1239  * rewrite operation is done. See 4.7.3 and 4.7.11 in Columbus2 PRM.
1240  *
1241  * Note that rewrite operation doesn't change RAW_UE to Marked UE.
1242  * Therefore, we use it only CE case.
1243  */
1244 static uint32_t
1245 do_rewrite(mc_opl_t *mcp, int bank, uint32_t dimm_addr)
1246 {
1247 	uint32_t cntl;
1248 	int count = 0;
1249 
1250 	/* first wait to make sure PTRL_STATUS is 0 */
1251 	while (count++ < mc_max_rewrite_loop) {
1252 		cntl = LD_MAC_REG(MAC_PTRL_CNTL(mcp, bank));
1253 		if (!(cntl & MAC_CNTL_PTRL_STATUS))
1254 			break;
1255 		drv_usecwait(mc_rewrite_delay);
1256 	}
1257 	if (count >= mc_max_rewrite_loop)
1258 		goto bad;
1259 
1260 	count = 0;
1261 
1262 	ST_MAC_REG(MAC_REWRITE_ADD(mcp, bank), dimm_addr);
1263 	MAC_REW_REQ(mcp, bank);
1264 
1265 	do {
1266 		cntl = LD_MAC_REG(MAC_PTRL_CNTL(mcp, bank));
1267 		if (count++ >= mc_max_rewrite_loop) {
1268 			goto bad;
1269 		} else {
1270 			drv_usecwait(mc_rewrite_delay);
1271 		}
1272 	/*
1273 	 * If there are other MEMORY or PCI activities, this
1274 	 * will be BUSY, else it should be set immediately
1275 	 */
1276 	} while (!(cntl & MAC_CNTL_REW_END));
1277 
1278 	MAC_CLEAR_ERRS(mcp, bank, MAC_CNTL_REW_ERRS);
1279 	return (cntl);
1280 bad:
1281 	/* This is bad.  Just reset the circuit */
1282 	cmn_err(CE_WARN, "mc-opl rewrite timeout on /LSB%d/B%d\n",
1283 		mcp->mc_board_num, bank);
1284 	cntl = MAC_CNTL_REW_END;
1285 	MAC_CMD(mcp, bank, MAC_CNTL_PTRL_RESET);
1286 	MAC_CLEAR_ERRS(mcp, bank, MAC_CNTL_REW_ERRS);
1287 	return (cntl);
1288 }
1289 void
1290 mc_process_scf_log(mc_opl_t *mcp)
1291 {
1292 	int count;
1293 	int n = 0;
1294 	scf_log_t *p;
1295 	int bank;
1296 
1297 	for (bank = 0; bank < BANKNUM_PER_SB; bank++) {
1298 	    while ((p = mcp->mc_scf_log[bank]) != NULL &&
1299 		(n < mc_max_errlog_processed)) {
1300 		ASSERT(bank == p->sl_bank);
1301 		count = 0;
1302 		while ((LD_MAC_REG(MAC_STATIC_ERR_ADD(mcp, p->sl_bank))
1303 			& MAC_STATIC_ERR_VLD)) {
1304 			if (count++ >= (mc_max_scf_loop)) {
1305 				break;
1306 			}
1307 			drv_usecwait(mc_scf_delay);
1308 		}
1309 
1310 		if (count < mc_max_scf_loop) {
1311 			ST_MAC_REG(MAC_STATIC_ERR_LOG(mcp, p->sl_bank),
1312 				p->sl_err_log);
1313 
1314 			ST_MAC_REG(MAC_STATIC_ERR_ADD(mcp, p->sl_bank),
1315 				p->sl_err_add|MAC_STATIC_ERR_VLD);
1316 			mcp->mc_scf_retry[bank] = 0;
1317 		} else {
1318 			/* if we try too many times, just drop the req */
1319 			if (mcp->mc_scf_retry[bank]++ <= mc_max_scf_retry) {
1320 				return;
1321 			} else {
1322 			    if ((++mc_pce_dropped & 0xff) == 0) {
1323 				cmn_err(CE_WARN,
1324 				    "Cannot report Permanent CE to SCF\n");
1325 			    }
1326 			}
1327 		}
1328 		n++;
1329 		mcp->mc_scf_log[bank] = p->sl_next;
1330 		mcp->mc_scf_total[bank]--;
1331 		ASSERT(mcp->mc_scf_total[bank] >= 0);
1332 		kmem_free(p, sizeof (scf_log_t));
1333 	    }
1334 	}
1335 }
1336 void
1337 mc_queue_scf_log(mc_opl_t *mcp, mc_flt_stat_t *flt_stat, int bank)
1338 {
1339 	scf_log_t *p;
1340 
1341 	if (mcp->mc_scf_total[bank] >= mc_max_scf_logs) {
1342 		if ((++mc_pce_dropped & 0xff) == 0) {
1343 		    cmn_err(CE_WARN, "Too many Permanent CE requests.\n");
1344 		}
1345 		return;
1346 	}
1347 	p = kmem_zalloc(sizeof (scf_log_t), KM_SLEEP);
1348 	p->sl_next = 0;
1349 	p->sl_err_add = flt_stat->mf_err_add;
1350 	p->sl_err_log = flt_stat->mf_err_log;
1351 	p->sl_bank = bank;
1352 
1353 	if (mcp->mc_scf_log[bank] == NULL) {
1354 		/*
1355 		 * we rely on mc_scf_log to detect NULL queue.
1356 		 * mc_scf_log_tail is irrelevant is such case.
1357 		 */
1358 		mcp->mc_scf_log_tail[bank] = mcp->mc_scf_log[bank] = p;
1359 	} else {
1360 		mcp->mc_scf_log_tail[bank]->sl_next = p;
1361 		mcp->mc_scf_log_tail[bank] = p;
1362 	}
1363 	mcp->mc_scf_total[bank]++;
1364 }
1365 /*
1366  * This routine determines what kind of CE happens, intermittent
1367  * or permanent as follows. (See 4.7.3 in Columbus2 PRM.)
1368  * - Do rewrite by issuing REW_REQ command to MAC_PTRL_CNTL register.
1369  * - If CE is still detected on the same address even after doing
1370  *   rewrite operation twice, it is determined as permanent error.
1371  * - If error is not detected anymore, it is determined as intermittent
1372  *   error.
1373  * - If UE is detected due to rewrite operation, it should be treated
1374  *   as UE.
1375  */
1376 
1377 /* ARGSUSED */
1378 static void
1379 mc_scrub_ce(mc_opl_t *mcp, int bank, mc_flt_stat_t *flt_stat, int ptrl_error)
1380 {
1381 	uint32_t cntl;
1382 	int i;
1383 
1384 	flt_stat->mf_type = FLT_TYPE_PERMANENT_CE;
1385 	/*
1386 	 * rewrite request 1st time reads and correct error data
1387 	 * and write to DIMM.  2nd rewrite request must be issued
1388 	 * after REW_CE/UE/END is 0.  When the 2nd request is completed,
1389 	 * if REW_CE = 1, then it is permanent CE.
1390 	 */
1391 	for (i = 0; i < 2; i++) {
1392 		cntl = do_rewrite(mcp, bank, flt_stat->mf_err_add);
1393 		/*
1394 		 * If the error becomes UE or CMPE
1395 		 * we return to the caller immediately.
1396 		 */
1397 		if (cntl & MAC_CNTL_REW_UE) {
1398 			if (ptrl_error)
1399 				flt_stat->mf_cntl |= MAC_CNTL_PTRL_UE;
1400 			else
1401 				flt_stat->mf_cntl |= MAC_CNTL_MI_UE;
1402 			flt_stat->mf_type = FLT_TYPE_UE;
1403 			return;
1404 		}
1405 		if (cntl & MAC_CNTL_REW_CMPE) {
1406 			if (ptrl_error)
1407 				flt_stat->mf_cntl |= MAC_CNTL_PTRL_CMPE;
1408 			else
1409 				flt_stat->mf_cntl |= MAC_CNTL_MI_CMPE;
1410 			flt_stat->mf_type = FLT_TYPE_CMPE;
1411 			return;
1412 		}
1413 	}
1414 	if (!(cntl & MAC_CNTL_REW_CE)) {
1415 		flt_stat->mf_type = FLT_TYPE_INTERMITTENT_CE;
1416 	}
1417 
1418 	if (flt_stat->mf_type == FLT_TYPE_PERMANENT_CE) {
1419 		/* report PERMANENT_CE to SP via SCF */
1420 		if (!(flt_stat->mf_err_log & MAC_ERR_LOG_INVALID)) {
1421 			mc_queue_scf_log(mcp, flt_stat, bank);
1422 		}
1423 	}
1424 }
1425 
1426 #define	IS_CMPE(cntl, f)	((cntl) & ((f) ? MAC_CNTL_PTRL_CMPE :\
1427 				MAC_CNTL_MI_CMPE))
1428 #define	IS_UE(cntl, f)	((cntl) & ((f) ? MAC_CNTL_PTRL_UE : MAC_CNTL_MI_UE))
1429 #define	IS_CE(cntl, f)	((cntl) & ((f) ? MAC_CNTL_PTRL_CE : MAC_CNTL_MI_CE))
1430 #define	IS_OK(cntl, f)	(!((cntl) & ((f) ? MAC_CNTL_PTRL_ERRS : \
1431 			MAC_CNTL_MI_ERRS)))
1432 
1433 
1434 static int
1435 IS_CE_ONLY(uint32_t cntl, int ptrl_error)
1436 {
1437 	if (ptrl_error) {
1438 		return ((cntl & MAC_CNTL_PTRL_ERRS) == MAC_CNTL_PTRL_CE);
1439 	} else {
1440 		return ((cntl & MAC_CNTL_MI_ERRS) == MAC_CNTL_MI_CE);
1441 	}
1442 }
1443 
1444 void
1445 mc_write_cntl(mc_opl_t *mcp, int bank, uint32_t value)
1446 {
1447 	int ebank = (IS_MIRROR(mcp, bank)) ? MIRROR_IDX(bank) : bank;
1448 
1449 	if (mcp->mc_speedup_period[ebank] > 0)
1450 		value |= mc_max_speed;
1451 	else
1452 		value |= mcp->mc_speed;
1453 	ST_MAC_REG(MAC_PTRL_CNTL(mcp, bank), value);
1454 }
1455 
1456 static void
1457 mc_read_ptrl_reg(mc_opl_t *mcp, int bank, mc_flt_stat_t *flt_stat)
1458 {
1459 	flt_stat->mf_cntl = LD_MAC_REG(MAC_PTRL_CNTL(mcp, bank)) &
1460 		MAC_CNTL_PTRL_ERRS;
1461 	flt_stat->mf_err_add = LD_MAC_REG(MAC_PTRL_ERR_ADD(mcp, bank));
1462 	flt_stat->mf_err_log = LD_MAC_REG(MAC_PTRL_ERR_LOG(mcp, bank));
1463 	flt_stat->mf_flt_maddr.ma_bd = mcp->mc_board_num;
1464 	flt_stat->mf_flt_maddr.ma_phys_bd = mcp->mc_phys_board_num;
1465 	flt_stat->mf_flt_maddr.ma_bank = bank;
1466 	flt_stat->mf_flt_maddr.ma_dimm_addr = flt_stat->mf_err_add;
1467 }
1468 
1469 static void
1470 mc_read_mi_reg(mc_opl_t *mcp, int bank, mc_flt_stat_t *flt_stat)
1471 {
1472 	uint32_t status, old_status;
1473 
1474 	status = LD_MAC_REG(MAC_PTRL_CNTL(mcp, bank)) &
1475 		MAC_CNTL_MI_ERRS;
1476 	old_status = 0;
1477 
1478 	/* we keep reading until the status is stable */
1479 	while (old_status != status) {
1480 		old_status = status;
1481 		flt_stat->mf_err_add =
1482 			LD_MAC_REG(MAC_MI_ERR_ADD(mcp, bank));
1483 		flt_stat->mf_err_log =
1484 			LD_MAC_REG(MAC_MI_ERR_LOG(mcp, bank));
1485 		status = LD_MAC_REG(MAC_PTRL_CNTL(mcp, bank)) &
1486 			MAC_CNTL_MI_ERRS;
1487 		if (status == old_status) {
1488 			break;
1489 		}
1490 	}
1491 
1492 	flt_stat->mf_cntl = status;
1493 	flt_stat->mf_flt_maddr.ma_bd = mcp->mc_board_num;
1494 	flt_stat->mf_flt_maddr.ma_phys_bd = mcp->mc_phys_board_num;
1495 	flt_stat->mf_flt_maddr.ma_bank = bank;
1496 	flt_stat->mf_flt_maddr.ma_dimm_addr = flt_stat->mf_err_add;
1497 }
1498 
1499 
1500 /*
1501  * Error philosophy for mirror mode:
1502  *
1503  * PTRL (The error address for both banks are same, since ptrl stops if it
1504  * detects error.)
1505  * - Compare error  log CMPE.
1506  *
1507  * - UE-UE           Report MUE.  No rewrite.
1508  *
1509  * - UE-*	     UE-(CE/OK). Rewrite to scrub UE.  Report SUE.
1510  *
1511  * - CE-*            CE-(CE/OK). Scrub to determine if CE is permanent.
1512  *                   If CE is permanent, inform SCF.  Once for each
1513  *		     Dimm.  If CE becomes UE or CMPE, go back to above.
1514  *
1515  *
1516  * MI (The error addresses for each bank are the same or different.)
1517  * - Compare  error  If addresses are the same.  Just CMPE, so log CMPE.
1518  *		     If addresses are different (this could happen
1519  *		     as a result of scrubbing.  Report each separately.
1520  *		     Only report error info on each side.
1521  *
1522  * - UE-UE           Addresses are the same.  Report MUE.
1523  *		     Addresses are different.  Report SUE on each bank.
1524  *		     Rewrite to clear UE.
1525  *
1526  * - UE-*	     UE-(CE/OK)
1527  *		     Rewrite to clear UE.  Report SUE for the bank.
1528  *
1529  * - CE-*            CE-(CE/OK).  Scrub to determine if CE is permanent.
1530  *                   If CE becomes UE or CMPE, go back to above.
1531  *
1532  */
1533 
1534 static int
1535 mc_process_error_mir(mc_opl_t *mcp, mc_aflt_t *mc_aflt, mc_flt_stat_t *flt_stat)
1536 {
1537 	int ptrl_error = mc_aflt->mflt_is_ptrl;
1538 	int i;
1539 	int rv = 0;
1540 
1541 	MC_LOG("process mirror errors cntl[0] = %x, cntl[1] = %x\n",
1542 		flt_stat[0].mf_cntl, flt_stat[1].mf_cntl);
1543 
1544 	if (ptrl_error) {
1545 		if (((flt_stat[0].mf_cntl | flt_stat[1].mf_cntl)
1546 			& MAC_CNTL_PTRL_ERRS) == 0)
1547 			return (0);
1548 	} else {
1549 		if (((flt_stat[0].mf_cntl | flt_stat[1].mf_cntl)
1550 			& MAC_CNTL_MI_ERRS) == 0)
1551 			return (0);
1552 	}
1553 
1554 	/*
1555 	 * First we take care of the case of CE
1556 	 * because they can become UE or CMPE
1557 	 */
1558 	for (i = 0; i < 2; i++) {
1559 		if (IS_CE_ONLY(flt_stat[i].mf_cntl, ptrl_error)) {
1560 			MC_LOG("CE detected on bank %d\n",
1561 				flt_stat[i].mf_flt_maddr.ma_bank);
1562 			mc_scrub_ce(mcp, flt_stat[i].mf_flt_maddr.ma_bank,
1563 				&flt_stat[i], ptrl_error);
1564 			rv = 1;
1565 		}
1566 	}
1567 
1568 	/* The above scrubbing can turn CE into UE or CMPE */
1569 
1570 	/*
1571 	 * Now we distinguish two cases: same address or not
1572 	 * the same address.  It might seem more intuitive to
1573 	 * distinguish PTRL v.s. MI error but it is more
1574 	 * complicated that way.
1575 	 */
1576 
1577 	if (flt_stat[0].mf_err_add == flt_stat[1].mf_err_add) {
1578 
1579 		if (IS_CMPE(flt_stat[0].mf_cntl, ptrl_error) ||
1580 		    IS_CMPE(flt_stat[1].mf_cntl, ptrl_error)) {
1581 			flt_stat[0].mf_type = FLT_TYPE_CMPE;
1582 			flt_stat[1].mf_type = FLT_TYPE_CMPE;
1583 			mc_aflt->mflt_erpt_class = MC_OPL_CMPE;
1584 			mc_aflt->mflt_nflts = 2;
1585 			mc_aflt->mflt_stat[0] = &flt_stat[0];
1586 			mc_aflt->mflt_stat[1] = &flt_stat[1];
1587 			mc_aflt->mflt_pr = PR_UE;
1588 			/*
1589 			 * Compare error is result of MAC internal error, so
1590 			 * simply log it instead of publishing an ereport. SCF
1591 			 * diagnoses all the MAC internal and its i/f error.
1592 			 * mc_err_drain(mc_aflt);
1593 			 */
1594 			MC_LOG("cmpe error detected\n");
1595 			return (1);
1596 		}
1597 
1598 		if (IS_UE(flt_stat[0].mf_cntl, ptrl_error) &&
1599 			IS_UE(flt_stat[1].mf_cntl, ptrl_error)) {
1600 			/* Both side are UE's */
1601 
1602 			MAC_SET_ERRLOG_INFO(&flt_stat[0]);
1603 			MAC_SET_ERRLOG_INFO(&flt_stat[1]);
1604 			MC_LOG("MUE detected\n");
1605 			flt_stat[0].mf_type = FLT_TYPE_MUE;
1606 			flt_stat[1].mf_type = FLT_TYPE_MUE;
1607 			mc_aflt->mflt_erpt_class = MC_OPL_MUE;
1608 			mc_aflt->mflt_nflts = 2;
1609 			mc_aflt->mflt_stat[0] = &flt_stat[0];
1610 			mc_aflt->mflt_stat[1] = &flt_stat[1];
1611 			mc_aflt->mflt_pr = PR_UE;
1612 			mc_err_drain(mc_aflt);
1613 			return (1);
1614 		}
1615 
1616 		/* Now the only case is UE/CE, UE/OK, or don't care */
1617 		for (i = 0; i < 2; i++) {
1618 		    if (IS_UE(flt_stat[i].mf_cntl, ptrl_error)) {
1619 
1620 			/* rewrite can clear the one side UE error */
1621 
1622 			if (IS_OK(flt_stat[i^1].mf_cntl, ptrl_error)) {
1623 				(void) do_rewrite(mcp,
1624 				    flt_stat[i].mf_flt_maddr.ma_bank,
1625 				    flt_stat[i].mf_flt_maddr.ma_dimm_addr);
1626 			}
1627 			flt_stat[i].mf_type = FLT_TYPE_UE;
1628 			MAC_SET_ERRLOG_INFO(&flt_stat[i]);
1629 			mc_aflt->mflt_erpt_class = MC_OPL_SUE;
1630 			mc_aflt->mflt_stat[0] = &flt_stat[i];
1631 			mc_aflt->mflt_nflts = 1;
1632 			mc_aflt->mflt_pr = PR_MCE;
1633 			mc_err_drain(mc_aflt);
1634 			/* Once we hit a UE/CE or UE/OK case, done */
1635 			return (1);
1636 		    }
1637 		}
1638 
1639 	} else {
1640 		/*
1641 		 * addresses are different. That means errors
1642 		 * on the 2 banks are not related at all.
1643 		 */
1644 		for (i = 0; i < 2; i++) {
1645 		    if (IS_CMPE(flt_stat[i].mf_cntl, ptrl_error)) {
1646 			flt_stat[i].mf_type = FLT_TYPE_CMPE;
1647 			mc_aflt->mflt_erpt_class = MC_OPL_CMPE;
1648 			mc_aflt->mflt_nflts = 1;
1649 			mc_aflt->mflt_stat[0] = &flt_stat[i];
1650 			mc_aflt->mflt_pr = PR_UE;
1651 			/*
1652 			 * Compare error is result of MAC internal error, so
1653 			 * simply log it instead of publishing an ereport. SCF
1654 			 * diagnoses all the MAC internal and its i/f error.
1655 			 * mc_err_drain(mc_aflt);
1656 			 */
1657 			MC_LOG("cmpe error detected\n");
1658 			/* no more report on this bank */
1659 			flt_stat[i].mf_cntl = 0;
1660 			rv = 1;
1661 		    }
1662 		}
1663 
1664 		/* rewrite can clear the one side UE error */
1665 
1666 		for (i = 0; i < 2; i++) {
1667 		    if (IS_UE(flt_stat[i].mf_cntl, ptrl_error)) {
1668 			(void) do_rewrite(mcp,
1669 				flt_stat[i].mf_flt_maddr.ma_bank,
1670 				flt_stat[i].mf_flt_maddr.ma_dimm_addr);
1671 			flt_stat[i].mf_type = FLT_TYPE_UE;
1672 			MAC_SET_ERRLOG_INFO(&flt_stat[i]);
1673 			mc_aflt->mflt_erpt_class = MC_OPL_SUE;
1674 			mc_aflt->mflt_stat[0] = &flt_stat[i];
1675 			mc_aflt->mflt_nflts = 1;
1676 			mc_aflt->mflt_pr = PR_MCE;
1677 			mc_err_drain(mc_aflt);
1678 			rv = 1;
1679 		    }
1680 		}
1681 	}
1682 	return (rv);
1683 }
1684 static void
1685 mc_error_handler_mir(mc_opl_t *mcp, int bank, mc_rsaddr_info_t *rsaddr)
1686 {
1687 	mc_aflt_t mc_aflt;
1688 	mc_flt_stat_t flt_stat[2], mi_flt_stat[2];
1689 	int i;
1690 	int mi_valid;
1691 
1692 	ASSERT(rsaddr);
1693 
1694 	bzero(&mc_aflt, sizeof (mc_aflt_t));
1695 	bzero(&flt_stat, 2 * sizeof (mc_flt_stat_t));
1696 	bzero(&mi_flt_stat, 2 * sizeof (mc_flt_stat_t));
1697 
1698 	mc_aflt.mflt_mcp = mcp;
1699 	mc_aflt.mflt_id = gethrtime();
1700 
1701 	/* Now read all the registers into flt_stat */
1702 
1703 	for (i = 0; i < 2; i++) {
1704 		MC_LOG("Reading registers of bank %d\n", bank);
1705 		/* patrol registers */
1706 		mc_read_ptrl_reg(mcp, bank, &flt_stat[i]);
1707 
1708 		/*
1709 		 * In mirror mode, it is possible that only one bank
1710 		 * may report the error. We need to check for it to
1711 		 * ensure we pick the right addr value for patrol restart.
1712 		 * Note that if both banks reported errors, we pick the
1713 		 * 2nd one. Both banks should reported the same error address.
1714 		 */
1715 		if (flt_stat[i].mf_cntl & MAC_CNTL_PTRL_ERRS)
1716 			rsaddr->mi_restartaddr = flt_stat[i].mf_flt_maddr;
1717 
1718 		MC_LOG("ptrl registers cntl %x add %x log %x\n",
1719 			flt_stat[i].mf_cntl,
1720 			flt_stat[i].mf_err_add,
1721 			flt_stat[i].mf_err_log);
1722 
1723 		/* MI registers */
1724 		mc_read_mi_reg(mcp, bank, &mi_flt_stat[i]);
1725 
1726 		MC_LOG("MI registers cntl %x add %x log %x\n",
1727 			mi_flt_stat[i].mf_cntl,
1728 			mi_flt_stat[i].mf_err_add,
1729 			mi_flt_stat[i].mf_err_log);
1730 
1731 		bank = bank^1;
1732 	}
1733 
1734 	/* clear errors once we read all the registers */
1735 	MAC_CLEAR_ERRS(mcp, bank,
1736 		(MAC_CNTL_PTRL_ERRS|MAC_CNTL_MI_ERRS));
1737 
1738 	MAC_CLEAR_ERRS(mcp, bank ^ 1, (MAC_CNTL_PTRL_ERRS|MAC_CNTL_MI_ERRS));
1739 
1740 	/* Process MI errors first */
1741 
1742 	/* if not error mode, cntl1 is 0 */
1743 	if ((mi_flt_stat[0].mf_err_add & MAC_ERR_ADD_INVALID) ||
1744 		(mi_flt_stat[0].mf_err_log & MAC_ERR_LOG_INVALID))
1745 		mi_flt_stat[0].mf_cntl = 0;
1746 
1747 	if ((mi_flt_stat[1].mf_err_add & MAC_ERR_ADD_INVALID) ||
1748 		(mi_flt_stat[1].mf_err_log & MAC_ERR_LOG_INVALID))
1749 		mi_flt_stat[1].mf_cntl = 0;
1750 
1751 	mc_aflt.mflt_is_ptrl = 0;
1752 	mi_valid = mc_process_error_mir(mcp, &mc_aflt, &mi_flt_stat[0]);
1753 
1754 	if ((((flt_stat[0].mf_cntl & MAC_CNTL_PTRL_ERRS) >>
1755 		MAC_CNTL_PTRL_ERR_SHIFT) ==
1756 		((mi_flt_stat[0].mf_cntl & MAC_CNTL_MI_ERRS) >>
1757 		MAC_CNTL_MI_ERR_SHIFT)) &&
1758 		(flt_stat[0].mf_err_add == mi_flt_stat[0].mf_err_add) &&
1759 		(((flt_stat[1].mf_cntl & MAC_CNTL_PTRL_ERRS) >>
1760 		MAC_CNTL_PTRL_ERR_SHIFT) ==
1761 		((mi_flt_stat[1].mf_cntl & MAC_CNTL_MI_ERRS) >>
1762 		MAC_CNTL_MI_ERR_SHIFT)) &&
1763 		(flt_stat[1].mf_err_add == mi_flt_stat[1].mf_err_add)) {
1764 #ifdef DEBUG
1765 		MC_LOG("discarding PTRL error because "
1766 		    "it is the same as MI\n");
1767 #endif
1768 		rsaddr->mi_valid = mi_valid;
1769 		return;
1770 	}
1771 	/* if not error mode, cntl1 is 0 */
1772 	if ((flt_stat[0].mf_err_add & MAC_ERR_ADD_INVALID) ||
1773 		(flt_stat[0].mf_err_log & MAC_ERR_LOG_INVALID))
1774 		flt_stat[0].mf_cntl = 0;
1775 
1776 	if ((flt_stat[1].mf_err_add & MAC_ERR_ADD_INVALID) ||
1777 		(flt_stat[1].mf_err_log & MAC_ERR_LOG_INVALID))
1778 		flt_stat[1].mf_cntl = 0;
1779 
1780 	mc_aflt.mflt_is_ptrl = 1;
1781 	rsaddr->mi_valid = mc_process_error_mir(mcp, &mc_aflt, &flt_stat[0]);
1782 }
1783 static int
1784 mc_process_error(mc_opl_t *mcp, int bank, mc_aflt_t *mc_aflt,
1785 	mc_flt_stat_t *flt_stat)
1786 {
1787 	int ptrl_error = mc_aflt->mflt_is_ptrl;
1788 	int rv = 0;
1789 
1790 	mc_aflt->mflt_erpt_class = NULL;
1791 	if (IS_UE(flt_stat->mf_cntl, ptrl_error)) {
1792 		MC_LOG("UE detected\n");
1793 		flt_stat->mf_type = FLT_TYPE_UE;
1794 		mc_aflt->mflt_erpt_class = MC_OPL_UE;
1795 		mc_aflt->mflt_pr = PR_UE;
1796 		MAC_SET_ERRLOG_INFO(flt_stat);
1797 		rv = 1;
1798 	} else if (IS_CE(flt_stat->mf_cntl, ptrl_error)) {
1799 		MC_LOG("CE detected\n");
1800 		MAC_SET_ERRLOG_INFO(flt_stat);
1801 
1802 		/* Error type can change after scrubbing */
1803 		mc_scrub_ce(mcp, bank, flt_stat, ptrl_error);
1804 
1805 		if (flt_stat->mf_type == FLT_TYPE_PERMANENT_CE) {
1806 			mc_aflt->mflt_erpt_class = MC_OPL_CE;
1807 			mc_aflt->mflt_pr = PR_MCE;
1808 		} else if (flt_stat->mf_type == FLT_TYPE_UE) {
1809 			mc_aflt->mflt_erpt_class = MC_OPL_UE;
1810 			mc_aflt->mflt_pr = PR_UE;
1811 		}
1812 		rv = 1;
1813 	}
1814 	MC_LOG("mc_process_error: fault type %x erpt %s\n",
1815 		flt_stat->mf_type,
1816 		mc_aflt->mflt_erpt_class);
1817 	if (mc_aflt->mflt_erpt_class) {
1818 		mc_aflt->mflt_stat[0] = flt_stat;
1819 		mc_aflt->mflt_nflts = 1;
1820 		mc_err_drain(mc_aflt);
1821 	}
1822 	return (rv);
1823 }
1824 
1825 static void
1826 mc_error_handler(mc_opl_t *mcp, int bank, mc_rsaddr_info_t *rsaddr)
1827 {
1828 	mc_aflt_t mc_aflt;
1829 	mc_flt_stat_t flt_stat, mi_flt_stat;
1830 	int mi_valid;
1831 
1832 	bzero(&mc_aflt, sizeof (mc_aflt_t));
1833 	bzero(&flt_stat, sizeof (mc_flt_stat_t));
1834 	bzero(&mi_flt_stat, sizeof (mc_flt_stat_t));
1835 
1836 	mc_aflt.mflt_mcp = mcp;
1837 	mc_aflt.mflt_id = gethrtime();
1838 
1839 	/* patrol registers */
1840 	mc_read_ptrl_reg(mcp, bank, &flt_stat);
1841 
1842 	ASSERT(rsaddr);
1843 	rsaddr->mi_restartaddr = flt_stat.mf_flt_maddr;
1844 
1845 	MC_LOG("ptrl registers cntl %x add %x log %x\n",
1846 		flt_stat.mf_cntl,
1847 		flt_stat.mf_err_add,
1848 		flt_stat.mf_err_log);
1849 
1850 	/* MI registers */
1851 	mc_read_mi_reg(mcp, bank, &mi_flt_stat);
1852 
1853 
1854 	MC_LOG("MI registers cntl %x add %x log %x\n",
1855 		mi_flt_stat.mf_cntl,
1856 		mi_flt_stat.mf_err_add,
1857 		mi_flt_stat.mf_err_log);
1858 
1859 	/* clear errors once we read all the registers */
1860 	MAC_CLEAR_ERRS(mcp, bank, (MAC_CNTL_PTRL_ERRS|MAC_CNTL_MI_ERRS));
1861 
1862 	mc_aflt.mflt_is_ptrl = 0;
1863 	if ((mi_flt_stat.mf_cntl & MAC_CNTL_MI_ERRS) &&
1864 		((mi_flt_stat.mf_err_add & MAC_ERR_ADD_INVALID) == 0) &&
1865 		((mi_flt_stat.mf_err_log & MAC_ERR_LOG_INVALID) == 0)) {
1866 		mi_valid = mc_process_error(mcp, bank, &mc_aflt, &mi_flt_stat);
1867 	}
1868 
1869 	if ((((flt_stat.mf_cntl & MAC_CNTL_PTRL_ERRS) >>
1870 		MAC_CNTL_PTRL_ERR_SHIFT) ==
1871 		((mi_flt_stat.mf_cntl & MAC_CNTL_MI_ERRS) >>
1872 		MAC_CNTL_MI_ERR_SHIFT)) &&
1873 		(flt_stat.mf_err_add == mi_flt_stat.mf_err_add)) {
1874 #ifdef DEBUG
1875 		MC_LOG("discarding PTRL error because "
1876 		    "it is the same as MI\n");
1877 #endif
1878 		rsaddr->mi_valid = mi_valid;
1879 		return;
1880 	}
1881 
1882 	mc_aflt.mflt_is_ptrl = 1;
1883 	if ((flt_stat.mf_cntl & MAC_CNTL_PTRL_ERRS) &&
1884 		((flt_stat.mf_err_add & MAC_ERR_ADD_INVALID) == 0) &&
1885 		((flt_stat.mf_err_log & MAC_ERR_LOG_INVALID) == 0)) {
1886 		rsaddr->mi_valid = mc_process_error(mcp, bank,
1887 			&mc_aflt, &flt_stat);
1888 	}
1889 }
1890 /*
1891  *	memory patrol error handling algorithm:
1892  *	timeout() is used to do periodic polling
1893  *	This is the flow chart.
1894  *	timeout ->
1895  *	mc_check_errors()
1896  *	    if memory bank is installed, read the status register
1897  *	    if any error bit is set,
1898  *	    -> mc_error_handler()
1899  *		-> read all error registers
1900  *	        -> mc_process_error()
1901  *	            determine error type
1902  *	            rewrite to clear error or scrub to determine CE type
1903  *	            inform SCF on permanent CE
1904  *	        -> mc_err_drain
1905  *	            page offline processing
1906  *	            -> mc_ereport_post()
1907  */
1908 
1909 static void
1910 mc_check_errors_func(mc_opl_t *mcp)
1911 {
1912 	mc_rsaddr_info_t rsaddr_info;
1913 	int i, error_count = 0;
1914 	uint32_t stat, cntl;
1915 	int running;
1916 	int wrapped;
1917 	int ebk;
1918 
1919 	/*
1920 	 * scan errors.
1921 	 */
1922 	if (mcp->mc_status & MC_MEMORYLESS)
1923 		return;
1924 
1925 	for (i = 0; i < BANKNUM_PER_SB; i++) {
1926 		if (mcp->mc_bank[i].mcb_status & BANK_INSTALLED) {
1927 			stat = ldphysio(MAC_PTRL_STAT(mcp, i));
1928 			cntl = ldphysio(MAC_PTRL_CNTL(mcp, i));
1929 			running = cntl & MAC_CNTL_PTRL_START;
1930 			wrapped = cntl & MAC_CNTL_PTRL_ADD_MAX;
1931 
1932 			/* Compute the effective bank idx */
1933 			ebk = (IS_MIRROR(mcp, i)) ? MIRROR_IDX(i) : i;
1934 
1935 			if (mc_debug_show_all || stat) {
1936 				MC_LOG("/LSB%d/B%d stat %x cntl %x\n",
1937 					mcp->mc_board_num, i,
1938 					stat, cntl);
1939 			}
1940 
1941 			/*
1942 			 * Update stats and reset flag if the HW patrol
1943 			 * wrapped around in its scan.
1944 			 */
1945 			if (wrapped) {
1946 				MAC_CLEAR_MAX(mcp, i);
1947 				mcp->mc_period[ebk]++;
1948 				if (IS_MIRROR(mcp, i))
1949 				    MC_LOG("mirror mc period %ld on "
1950 					"/LSB%d/B%d\n", mcp->mc_period[ebk],
1951 					mcp->mc_board_num, i);
1952 				else {
1953 				    MC_LOG("mc period %ld on "
1954 					"/LSB%d/B%d\n", mcp->mc_period[ebk],
1955 					mcp->mc_board_num, i);
1956 				}
1957 			}
1958 
1959 			if (running) {
1960 				/*
1961 				 * Mac patrol HW is still running.
1962 				 * Normally when an error is detected,
1963 				 * the HW patrol will stop so that we
1964 				 * can collect error data for reporting.
1965 				 * Certain errors (MI errors) detected may not
1966 				 * cause the HW patrol to stop which is a
1967 				 * problem since we cannot read error data while
1968 				 * the HW patrol is running. SW is not allowed
1969 				 * to stop the HW patrol while it is running
1970 				 * as it may cause HW inconsistency. This is
1971 				 * described in a HW errata.
1972 				 * In situations where we detected errors
1973 				 * that may not cause the HW patrol to stop.
1974 				 * We speed up the HW patrol scanning in
1975 				 * the hope that it will find the 'real' PTRL
1976 				 * errors associated with the previous errors
1977 				 * causing the HW to finally stop so that we
1978 				 * can do the reporting.
1979 				 */
1980 				/*
1981 				 * Check to see if we did speed up
1982 				 * the HW patrol due to previous errors
1983 				 * detected that did not cause the patrol
1984 				 * to stop. We only do it if HW patrol scan
1985 				 * wrapped (counted as completing a 'period').
1986 				 */
1987 				if (mcp->mc_speedup_period[ebk] > 0) {
1988 				    if (wrapped &&
1989 					(--mcp->mc_speedup_period[ebk] == 0)) {
1990 					/*
1991 					 * We did try to speed up.
1992 					 * The speed up period has expired
1993 					 * and the HW patrol is still running.
1994 					 * The errors must be intermittent.
1995 					 * We have no choice but to ignore
1996 					 * them, reset the scan speed to normal
1997 					 * and clear the MI error bits. For
1998 					 * mirror mode, we need to clear errors
1999 					 * on both banks.
2000 					 */
2001 					MC_LOG("Clearing MI errors\n");
2002 					MAC_CLEAR_ERRS(mcp, i,
2003 					    MAC_CNTL_MI_ERRS);
2004 
2005 					if (IS_MIRROR(mcp, i)) {
2006 					    MC_LOG("Clearing Mirror MI errs\n");
2007 					    MAC_CLEAR_ERRS(mcp, i^1,
2008 						MAC_CNTL_MI_ERRS);
2009 					}
2010 				    }
2011 				} else if (stat & MAC_STAT_MI_ERRS) {
2012 					/*
2013 					 * MI errors detected but we cannot
2014 					 * report them since the HW patrol
2015 					 * is still running.
2016 					 * We will attempt to speed up the
2017 					 * scanning and hopefully the HW
2018 					 * can detect PRTL errors at the same
2019 					 * location that cause the HW patrol
2020 					 * to stop.
2021 					 */
2022 					mcp->mc_speedup_period[ebk] = 2;
2023 					MAC_CMD(mcp, i, 0);
2024 				}
2025 			} else if (stat & (MAC_STAT_PTRL_ERRS |
2026 			    MAC_STAT_MI_ERRS)) {
2027 				/*
2028 				 * HW Patrol has stopped and we found errors.
2029 				 * Proceed to collect and report error info.
2030 				 */
2031 				mcp->mc_speedup_period[ebk] = 0;
2032 				rsaddr_info.mi_valid = 0;
2033 				rsaddr_info.mi_injectrestart = 0;
2034 				if (IS_MIRROR(mcp, i)) {
2035 				    mc_error_handler_mir(mcp, i, &rsaddr_info);
2036 				} else {
2037 				    mc_error_handler(mcp, i, &rsaddr_info);
2038 				}
2039 
2040 				error_count++;
2041 				restart_patrol(mcp, i, &rsaddr_info);
2042 			} else {
2043 				/*
2044 				 * HW patrol scan has apparently stopped
2045 				 * but no errors detected/flagged.
2046 				 * Restart the HW patrol just to be sure.
2047 				 * In mirror mode, the odd bank might have
2048 				 * reported errors that caused the patrol to
2049 				 * stop. We'll defer the restart to the odd
2050 				 * bank in this case.
2051 				 */
2052 				if (!IS_MIRROR(mcp, i) || (i & 0x1))
2053 					restart_patrol(mcp, i, NULL);
2054 			}
2055 		}
2056 	}
2057 	if (error_count > 0)
2058 		mcp->mc_last_error += error_count;
2059 	else
2060 		mcp->mc_last_error = 0;
2061 }
2062 
2063 /*
2064  * mc_polling -- Check errors for only one instance,
2065  * but process errors for all instances to make sure we drain the errors
2066  * faster than they can be accumulated.
2067  *
2068  * Polling on each board should be done only once per each
2069  * mc_patrol_interval_sec.  This is equivalent to setting mc_tick_left
2070  * to OPL_MAX_BOARDS and decrement by 1 on each timeout.
2071  * Once mc_tick_left becomes negative, the board becomes a candidate
2072  * for polling because it has waited for at least
2073  * mc_patrol_interval_sec's long.    If mc_timeout_period is calculated
2074  * differently, this has to be updated accordingly.
2075  */
2076 
2077 static void
2078 mc_polling(void)
2079 {
2080 	int i, scan_error;
2081 	mc_opl_t *mcp;
2082 
2083 
2084 	scan_error = 1;
2085 	for (i = 0; i < OPL_MAX_BOARDS; i++) {
2086 		mutex_enter(&mcmutex);
2087 		if ((mcp = mc_instances[i]) == NULL) {
2088 			mutex_exit(&mcmutex);
2089 			continue;
2090 		}
2091 		mutex_enter(&mcp->mc_lock);
2092 		mutex_exit(&mcmutex);
2093 		if (!(mcp->mc_status & MC_POLL_RUNNING)) {
2094 			mutex_exit(&mcp->mc_lock);
2095 			continue;
2096 		}
2097 		if (scan_error && mcp->mc_tick_left <= 0) {
2098 			mc_check_errors_func((void *)mcp);
2099 			mcp->mc_tick_left = OPL_MAX_BOARDS;
2100 			scan_error = 0;
2101 		} else {
2102 			mcp->mc_tick_left--;
2103 		}
2104 		mc_process_scf_log(mcp);
2105 		mutex_exit(&mcp->mc_lock);
2106 	}
2107 }
2108 
2109 static void
2110 get_ptrl_start_address(mc_opl_t *mcp, int bank, mc_addr_t *maddr)
2111 {
2112 	maddr->ma_bd = mcp->mc_board_num;
2113 	maddr->ma_bank = bank;
2114 	maddr->ma_dimm_addr = 0;
2115 }
2116 
2117 typedef struct mc_mem_range {
2118 	uint64_t	addr;
2119 	uint64_t	size;
2120 } mc_mem_range_t;
2121 
2122 static int
2123 get_base_address(mc_opl_t *mcp)
2124 {
2125 	mc_mem_range_t *mem_range;
2126 	int len;
2127 
2128 	if (ddi_getlongprop(DDI_DEV_T_ANY, mcp->mc_dip, DDI_PROP_DONTPASS,
2129 		"sb-mem-ranges", (caddr_t)&mem_range, &len) != DDI_SUCCESS) {
2130 		return (DDI_FAILURE);
2131 	}
2132 
2133 	mcp->mc_start_address = mem_range->addr;
2134 	mcp->mc_size = mem_range->size;
2135 
2136 	kmem_free(mem_range, len);
2137 	return (DDI_SUCCESS);
2138 }
2139 
2140 struct mc_addr_spec {
2141 	uint32_t bank;
2142 	uint32_t phys_hi;
2143 	uint32_t phys_lo;
2144 };
2145 
2146 #define	REGS_PA(m, i) ((((uint64_t)m[i].phys_hi)<<32) | m[i].phys_lo)
2147 
2148 static char *mc_tbl_name[] = {
2149 	"cs0-mc-pa-trans-table",
2150 	"cs1-mc-pa-trans-table"
2151 };
2152 
2153 /*
2154  * This routine performs a rangecheck for a given PA
2155  * to see if it belongs to the memory range for this board.
2156  * Return 1 if it is valid (within the range) and 0 otherwise
2157  */
2158 static int
2159 mc_rangecheck_pa(mc_opl_t *mcp, uint64_t pa)
2160 {
2161 	if ((pa < mcp->mc_start_address) ||
2162 		(mcp->mc_start_address + mcp->mc_size <= pa))
2163 		return (0);
2164 	else
2165 		return (1);
2166 }
2167 
2168 static void
2169 mc_memlist_delete(struct memlist *mlist)
2170 {
2171 	struct memlist *ml;
2172 
2173 	for (ml = mlist; ml; ml = mlist) {
2174 		mlist = ml->next;
2175 		kmem_free(ml, sizeof (struct memlist));
2176 	}
2177 }
2178 
2179 static struct memlist *
2180 mc_memlist_dup(struct memlist *mlist)
2181 {
2182 	struct memlist *hl = NULL, *tl, **mlp;
2183 
2184 	if (mlist == NULL)
2185 		return (NULL);
2186 
2187 	mlp = &hl;
2188 	tl = *mlp;
2189 	for (; mlist; mlist = mlist->next) {
2190 		*mlp = kmem_alloc(sizeof (struct memlist), KM_SLEEP);
2191 		(*mlp)->address = mlist->address;
2192 		(*mlp)->size = mlist->size;
2193 		(*mlp)->prev = tl;
2194 		tl = *mlp;
2195 		mlp = &((*mlp)->next);
2196 	}
2197 	*mlp = NULL;
2198 
2199 	return (hl);
2200 }
2201 
2202 
2203 static struct memlist *
2204 mc_memlist_del_span(struct memlist *mlist, uint64_t base, uint64_t len)
2205 {
2206 	uint64_t	end;
2207 	struct memlist	*ml, *tl, *nlp;
2208 
2209 	if (mlist == NULL)
2210 		return (NULL);
2211 
2212 	end = base + len;
2213 	if ((end <= mlist->address) || (base == end))
2214 		return (mlist);
2215 
2216 	for (tl = ml = mlist; ml; tl = ml, ml = nlp) {
2217 		uint64_t	mend;
2218 
2219 		nlp = ml->next;
2220 
2221 		if (end <= ml->address)
2222 			break;
2223 
2224 		mend = ml->address + ml->size;
2225 		if (base < mend) {
2226 			if (base <= ml->address) {
2227 				ml->address = end;
2228 				if (end >= mend)
2229 					ml->size = 0ull;
2230 				else
2231 					ml->size = mend - ml->address;
2232 			} else {
2233 				ml->size = base - ml->address;
2234 				if (end < mend) {
2235 					struct memlist	*nl;
2236 					/*
2237 					 * splitting an memlist entry.
2238 					 */
2239 					nl = kmem_alloc(sizeof (struct memlist),
2240 						KM_SLEEP);
2241 					nl->address = end;
2242 					nl->size = mend - nl->address;
2243 					if ((nl->next = nlp) != NULL)
2244 						nlp->prev = nl;
2245 					nl->prev = ml;
2246 					ml->next = nl;
2247 					nlp = nl;
2248 				}
2249 			}
2250 			if (ml->size == 0ull) {
2251 				if (ml == mlist) {
2252 					if ((mlist = nlp) != NULL)
2253 						nlp->prev = NULL;
2254 					kmem_free(ml, sizeof (struct memlist));
2255 					if (mlist == NULL)
2256 						break;
2257 					ml = nlp;
2258 				} else {
2259 					if ((tl->next = nlp) != NULL)
2260 						nlp->prev = tl;
2261 					kmem_free(ml, sizeof (struct memlist));
2262 					ml = tl;
2263 				}
2264 			}
2265 		}
2266 	}
2267 
2268 	return (mlist);
2269 }
2270 
2271 static void
2272 mc_get_mlist(mc_opl_t *mcp)
2273 {
2274 	struct memlist *mlist;
2275 
2276 	memlist_read_lock();
2277 	mlist = mc_memlist_dup(phys_install);
2278 	memlist_read_unlock();
2279 
2280 	if (mlist) {
2281 		mlist = mc_memlist_del_span(mlist, 0ull, mcp->mc_start_address);
2282 	}
2283 
2284 	if (mlist) {
2285 		uint64_t startpa, endpa;
2286 
2287 		startpa = mcp->mc_start_address + mcp->mc_size;
2288 		endpa = ptob(physmax + 1);
2289 		if (endpa > startpa) {
2290 			mlist = mc_memlist_del_span(mlist,
2291 				startpa, endpa - startpa);
2292 		}
2293 	}
2294 
2295 	if (mlist) {
2296 		mcp->mlist = mlist;
2297 	}
2298 }
2299 
2300 int
2301 mc_board_add(mc_opl_t *mcp)
2302 {
2303 	struct mc_addr_spec *macaddr;
2304 	cs_status_t *cs_status;
2305 	int len, len1, i, bk, cc;
2306 	mc_rsaddr_info_t rsaddr;
2307 	uint32_t mirr;
2308 	int nbanks = 0;
2309 	uint64_t nbytes = 0;
2310 
2311 	/*
2312 	 * Get configurations from "pseudo-mc" node which includes:
2313 	 * board# : LSB number
2314 	 * mac-addr : physical base address of MAC registers
2315 	 * csX-mac-pa-trans-table: translation table from DIMM address
2316 	 *			to physical address or vice versa.
2317 	 */
2318 	mcp->mc_board_num = (int)ddi_getprop(DDI_DEV_T_ANY, mcp->mc_dip,
2319 		DDI_PROP_DONTPASS, "board#", -1);
2320 
2321 	if (mcp->mc_board_num == -1) {
2322 		return (DDI_FAILURE);
2323 	}
2324 
2325 	/*
2326 	 * Get start address in this CAB. It can be gotten from
2327 	 * "sb-mem-ranges" property.
2328 	 */
2329 
2330 	if (get_base_address(mcp) == DDI_FAILURE) {
2331 		return (DDI_FAILURE);
2332 	}
2333 	/* get mac-pa trans tables */
2334 	for (i = 0; i < MC_TT_CS; i++) {
2335 		len = MC_TT_ENTRIES;
2336 		cc = ddi_getlongprop_buf(DDI_DEV_T_ANY, mcp->mc_dip,
2337 			DDI_PROP_DONTPASS, mc_tbl_name[i],
2338 			(caddr_t)mcp->mc_trans_table[i], &len);
2339 
2340 		if (cc != DDI_SUCCESS) {
2341 			bzero(mcp->mc_trans_table[i], MC_TT_ENTRIES);
2342 		}
2343 	}
2344 	mcp->mlist = NULL;
2345 
2346 	mc_get_mlist(mcp);
2347 
2348 	/* initialize bank informations */
2349 	cc = ddi_getlongprop(DDI_DEV_T_ANY, mcp->mc_dip, DDI_PROP_DONTPASS,
2350 		"mc-addr", (caddr_t)&macaddr, &len);
2351 	if (cc != DDI_SUCCESS) {
2352 		cmn_err(CE_WARN, "Cannot get mc-addr. err=%d\n", cc);
2353 		return (DDI_FAILURE);
2354 	}
2355 
2356 	cc = ddi_getlongprop(DDI_DEV_T_ANY, mcp->mc_dip, DDI_PROP_DONTPASS,
2357 		"cs-status", (caddr_t)&cs_status, &len1);
2358 
2359 	if (cc != DDI_SUCCESS) {
2360 		if (len > 0)
2361 			kmem_free(macaddr, len);
2362 		cmn_err(CE_WARN, "Cannot get cs-status. err=%d\n", cc);
2363 		return (DDI_FAILURE);
2364 	}
2365 	/* get the physical board number for a given logical board number */
2366 	mcp->mc_phys_board_num = mc_opl_get_physical_board(mcp->mc_board_num);
2367 
2368 	if (mcp->mc_phys_board_num < 0) {
2369 		if (len > 0)
2370 			kmem_free(macaddr, len);
2371 		cmn_err(CE_WARN, "Unable to obtain the physical board number");
2372 		return (DDI_FAILURE);
2373 	}
2374 
2375 	mutex_init(&mcp->mc_lock, NULL, MUTEX_DRIVER, NULL);
2376 
2377 	for (i = 0; i < len1 / sizeof (cs_status_t); i++) {
2378 		nbytes += ((uint64_t)cs_status[i].cs_avail_hi << 32) |
2379 			((uint64_t)cs_status[i].cs_avail_low);
2380 	}
2381 	if (len1 > 0)
2382 		kmem_free(cs_status, len1);
2383 	nbanks = len / sizeof (struct mc_addr_spec);
2384 
2385 	if (nbanks > 0)
2386 		nbytes /= nbanks;
2387 	else {
2388 		/* No need to free macaddr because len must be 0 */
2389 		mcp->mc_status |= MC_MEMORYLESS;
2390 		return (DDI_SUCCESS);
2391 	}
2392 
2393 	for (i = 0; i < BANKNUM_PER_SB; i++) {
2394 		mcp->mc_scf_retry[i] = 0;
2395 		mcp->mc_period[i] = 0;
2396 		mcp->mc_speedup_period[i] = 0;
2397 	}
2398 
2399 	/*
2400 	 * Get the memory size here. Let it be B (bytes).
2401 	 * Let T be the time in u.s. to scan 64 bytes.
2402 	 * If we want to complete 1 round of scanning in P seconds.
2403 	 *
2404 	 *	B * T * 10^(-6)	= P
2405 	 *	---------------
2406 	 *		64
2407 	 *
2408 	 *	T = P * 64 * 10^6
2409 	 *	    -------------
2410 	 *		B
2411 	 *
2412 	 *	  = P * 64 * 10^6
2413 	 *	    -------------
2414 	 *		B
2415 	 *
2416 	 *	The timing bits are set in PTRL_CNTL[28:26] where
2417 	 *
2418 	 *	0	- 1 m.s
2419 	 *	1	- 512 u.s.
2420 	 *	10	- 256 u.s.
2421 	 *	11	- 128 u.s.
2422 	 *	100	- 64 u.s.
2423 	 *	101	- 32 u.s.
2424 	 *	110	- 0 u.s.
2425 	 *	111	- reserved.
2426 	 *
2427 	 *
2428 	 *	a[0] = 110, a[1] = 101, ... a[6] = 0
2429 	 *
2430 	 *	cs-status property is int x 7
2431 	 *	0 - cs#
2432 	 *	1 - cs-status
2433 	 *	2 - cs-avail.hi
2434 	 *	3 - cs-avail.lo
2435 	 *	4 - dimm-capa.hi
2436 	 *	5 - dimm-capa.lo
2437 	 *	6 - #of dimms
2438 	 */
2439 
2440 	if (nbytes > 0) {
2441 		int i;
2442 		uint64_t ms;
2443 		ms = ((uint64_t)mc_scan_period * 64 * 1000000)/nbytes;
2444 		mcp->mc_speed = mc_scan_speeds[MC_MAX_SPEEDS - 1].mc_speeds;
2445 		for (i = 0; i < MC_MAX_SPEEDS - 1; i++) {
2446 			if (ms < mc_scan_speeds[i + 1].mc_period) {
2447 				mcp->mc_speed = mc_scan_speeds[i].mc_speeds;
2448 				break;
2449 			}
2450 		}
2451 	} else
2452 		mcp->mc_speed = 0;
2453 
2454 
2455 	for (i = 0; i < len / sizeof (struct mc_addr_spec); i++) {
2456 		struct mc_bank *bankp;
2457 		uint32_t reg;
2458 
2459 		/*
2460 		 * setup bank
2461 		 */
2462 		bk = macaddr[i].bank;
2463 		bankp = &(mcp->mc_bank[bk]);
2464 		bankp->mcb_status = BANK_INSTALLED;
2465 		bankp->mcb_reg_base = REGS_PA(macaddr, i);
2466 
2467 		reg = LD_MAC_REG(MAC_PTRL_CNTL(mcp, bk));
2468 		bankp->mcb_ptrl_cntl = (reg & MAC_CNTL_PTRL_PRESERVE_BITS);
2469 
2470 		/*
2471 		 * check if mirror mode
2472 		 */
2473 		mirr = LD_MAC_REG(MAC_MIRR(mcp, bk));
2474 
2475 		if (mirr & MAC_MIRR_MIRROR_MODE) {
2476 			MC_LOG("Mirror -> /LSB%d/B%d\n",
2477 				mcp->mc_board_num, bk);
2478 			bankp->mcb_status |= BANK_MIRROR_MODE;
2479 			/*
2480 			 * The following bit is only used for
2481 			 * error injection.  We should clear it
2482 			 */
2483 			if (mirr & MAC_MIRR_BANK_EXCLUSIVE)
2484 				ST_MAC_REG(MAC_MIRR(mcp, bk),
2485 					0);
2486 		}
2487 
2488 		/*
2489 		 * restart if not mirror mode or the other bank
2490 		 * of the mirror is not running
2491 		 */
2492 		if (!(mirr & MAC_MIRR_MIRROR_MODE) ||
2493 			!(mcp->mc_bank[bk^1].mcb_status &
2494 			BANK_PTRL_RUNNING)) {
2495 			MC_LOG("Starting up /LSB%d/B%d\n",
2496 				mcp->mc_board_num, bk);
2497 			get_ptrl_start_address(mcp, bk, &rsaddr.mi_restartaddr);
2498 			rsaddr.mi_valid = 0;
2499 			rsaddr.mi_injectrestart = 0;
2500 			restart_patrol(mcp, bk, &rsaddr);
2501 		} else {
2502 			MC_LOG("Not starting up /LSB%d/B%d\n",
2503 				mcp->mc_board_num, bk);
2504 		}
2505 		bankp->mcb_status |= BANK_PTRL_RUNNING;
2506 	}
2507 	if (len > 0)
2508 		kmem_free(macaddr, len);
2509 
2510 	mcp->mc_dimm_list = mc_get_dimm_list(mcp);
2511 
2512 	/*
2513 	 * set interval in HZ.
2514 	 */
2515 	mcp->mc_last_error = 0;
2516 
2517 	/* restart memory patrol checking */
2518 	mcp->mc_status |= MC_POLL_RUNNING;
2519 
2520 	return (DDI_SUCCESS);
2521 }
2522 
2523 int
2524 mc_board_del(mc_opl_t *mcp)
2525 {
2526 	int i;
2527 	scf_log_t *p;
2528 
2529 	/*
2530 	 * cleanup mac state
2531 	 */
2532 	mutex_enter(&mcp->mc_lock);
2533 	if (mcp->mc_status & MC_MEMORYLESS) {
2534 		mutex_exit(&mcp->mc_lock);
2535 		mutex_destroy(&mcp->mc_lock);
2536 		return (DDI_SUCCESS);
2537 	}
2538 	for (i = 0; i < BANKNUM_PER_SB; i++) {
2539 		if (mcp->mc_bank[i].mcb_status & BANK_INSTALLED) {
2540 			mcp->mc_bank[i].mcb_status &= ~BANK_INSTALLED;
2541 		}
2542 	}
2543 
2544 	/* stop memory patrol checking */
2545 	mcp->mc_status &= ~MC_POLL_RUNNING;
2546 
2547 	/* just throw away all the scf logs */
2548 	for (i = 0; i < BANKNUM_PER_SB; i++) {
2549 	    while ((p = mcp->mc_scf_log[i]) != NULL) {
2550 		mcp->mc_scf_log[i] = p->sl_next;
2551 		mcp->mc_scf_total[i]--;
2552 		kmem_free(p, sizeof (scf_log_t));
2553 	    }
2554 	}
2555 
2556 	if (mcp->mlist)
2557 		mc_memlist_delete(mcp->mlist);
2558 
2559 	if (mcp->mc_dimm_list)
2560 		mc_free_dimm_list(mcp->mc_dimm_list);
2561 
2562 	mutex_exit(&mcp->mc_lock);
2563 
2564 	mutex_destroy(&mcp->mc_lock);
2565 	return (DDI_SUCCESS);
2566 }
2567 
2568 int
2569 mc_suspend(mc_opl_t *mcp, uint32_t flag)
2570 {
2571 	/* stop memory patrol checking */
2572 	mutex_enter(&mcp->mc_lock);
2573 	if (mcp->mc_status & MC_MEMORYLESS) {
2574 		mutex_exit(&mcp->mc_lock);
2575 		return (DDI_SUCCESS);
2576 	}
2577 
2578 	mcp->mc_status &= ~MC_POLL_RUNNING;
2579 
2580 	mcp->mc_status |= flag;
2581 	mutex_exit(&mcp->mc_lock);
2582 
2583 	return (DDI_SUCCESS);
2584 }
2585 
2586 /* caller must clear the SUSPEND bits or this will do nothing */
2587 
2588 int
2589 mc_resume(mc_opl_t *mcp, uint32_t flag)
2590 {
2591 	int i;
2592 	uint64_t basepa;
2593 
2594 	mutex_enter(&mcp->mc_lock);
2595 	if (mcp->mc_status & MC_MEMORYLESS) {
2596 		mutex_exit(&mcp->mc_lock);
2597 		return (DDI_SUCCESS);
2598 	}
2599 	basepa = mcp->mc_start_address;
2600 	if (get_base_address(mcp) == DDI_FAILURE) {
2601 		mutex_exit(&mcp->mc_lock);
2602 		return (DDI_FAILURE);
2603 	}
2604 
2605 	if (basepa != mcp->mc_start_address) {
2606 		if (mcp->mlist)
2607 			mc_memlist_delete(mcp->mlist);
2608 		mcp->mlist = NULL;
2609 		mc_get_mlist(mcp);
2610 	}
2611 
2612 	mcp->mc_status &= ~flag;
2613 
2614 	if (mcp->mc_status & (MC_SOFT_SUSPENDED | MC_DRIVER_SUSPENDED)) {
2615 		mutex_exit(&mcp->mc_lock);
2616 		return (DDI_SUCCESS);
2617 	}
2618 
2619 	if (!(mcp->mc_status & MC_POLL_RUNNING)) {
2620 		/* restart memory patrol checking */
2621 		mcp->mc_status |= MC_POLL_RUNNING;
2622 		for (i = 0; i < BANKNUM_PER_SB; i++) {
2623 			if (mcp->mc_bank[i].mcb_status & BANK_INSTALLED) {
2624 				restart_patrol(mcp, i, NULL);
2625 			}
2626 		}
2627 	}
2628 	mutex_exit(&mcp->mc_lock);
2629 
2630 	return (DDI_SUCCESS);
2631 }
2632 
2633 static mc_opl_t *
2634 mc_pa_to_mcp(uint64_t pa)
2635 {
2636 	mc_opl_t *mcp;
2637 	int i;
2638 
2639 	ASSERT(MUTEX_HELD(&mcmutex));
2640 	for (i = 0; i < OPL_MAX_BOARDS; i++) {
2641 		if ((mcp = mc_instances[i]) == NULL)
2642 			continue;
2643 		/* if mac patrol is suspended, we cannot rely on it */
2644 		if (!(mcp->mc_status & MC_POLL_RUNNING) ||
2645 			(mcp->mc_status & MC_SOFT_SUSPENDED))
2646 			continue;
2647 		if (mc_rangecheck_pa(mcp, pa)) {
2648 			return (mcp);
2649 		}
2650 	}
2651 	return (NULL);
2652 }
2653 
2654 /*
2655  * Get Physical Board number from Logical one.
2656  */
2657 static int
2658 mc_opl_get_physical_board(int sb)
2659 {
2660 	if (&opl_get_physical_board) {
2661 		return (opl_get_physical_board(sb));
2662 	}
2663 
2664 	cmn_err(CE_NOTE, "!opl_get_physical_board() not loaded\n");
2665 	return (-1);
2666 }
2667 
2668 /* ARGSUSED */
2669 int
2670 mc_get_mem_unum(int synd_code, uint64_t flt_addr, char *buf, int buflen,
2671 	int *lenp)
2672 {
2673 	int i;
2674 	int j;
2675 	int sb;
2676 	int bank;
2677 	int cs;
2678 	mc_opl_t *mcp;
2679 	char memb_num;
2680 
2681 	mutex_enter(&mcmutex);
2682 
2683 	if (((mcp = mc_pa_to_mcp(flt_addr)) == NULL) ||
2684 		(!pa_is_valid(mcp, flt_addr))) {
2685 		mutex_exit(&mcmutex);
2686 		if (snprintf(buf, buflen, "UNKNOWN") >= buflen) {
2687 			return (ENOSPC);
2688 		} else {
2689 			if (lenp)
2690 				*lenp = strlen(buf);
2691 		}
2692 		return (0);
2693 	}
2694 
2695 	bank = pa_to_bank(mcp, flt_addr - mcp->mc_start_address);
2696 	sb = mcp->mc_phys_board_num;
2697 	cs = pa_to_cs(mcp, flt_addr - mcp->mc_start_address);
2698 
2699 	if (sb == -1) {
2700 		mutex_exit(&mcmutex);
2701 		return (ENXIO);
2702 	}
2703 
2704 	if (plat_model == MODEL_DC) {
2705 		i = BD_BK_SLOT_TO_INDEX(0, bank, 0);
2706 		j = (cs == 0) ? i : i + 2;
2707 		snprintf(buf, buflen, "/%s%02d/MEM%s MEM%s",
2708 		    model_names[plat_model].unit_name, sb,
2709 		    mc_dc_dimm_unum_table[j],
2710 		    mc_dc_dimm_unum_table[j + 1]);
2711 	} else {
2712 		i = BD_BK_SLOT_TO_INDEX(sb, bank, 0);
2713 		j = (cs == 0) ? i : i + 2;
2714 		memb_num = mc_ff_dimm_unum_table[i][0];
2715 		snprintf(buf, buflen, "/%s/%s%c/MEM%s MEM%s",
2716 		    model_names[plat_model].unit_name,
2717 		    model_names[plat_model].mem_name, memb_num,
2718 		    &mc_ff_dimm_unum_table[j][1],
2719 		    &mc_ff_dimm_unum_table[j + 1][1]);
2720 	}
2721 	if (lenp) {
2722 		*lenp = strlen(buf);
2723 	}
2724 	mutex_exit(&mcmutex);
2725 	return (0);
2726 }
2727 
2728 int
2729 opl_mc_suspend(void)
2730 {
2731 	mc_opl_t *mcp;
2732 	int i;
2733 
2734 	mutex_enter(&mcmutex);
2735 	for (i = 0; i < OPL_MAX_BOARDS; i++) {
2736 		if ((mcp = mc_instances[i]) == NULL)
2737 			continue;
2738 		mc_suspend(mcp, MC_SOFT_SUSPENDED);
2739 	}
2740 	mutex_exit(&mcmutex);
2741 
2742 	return (0);
2743 }
2744 
2745 int
2746 opl_mc_resume(void)
2747 {
2748 	mc_opl_t *mcp;
2749 	int i;
2750 
2751 	mutex_enter(&mcmutex);
2752 	for (i = 0; i < OPL_MAX_BOARDS; i++) {
2753 		if ((mcp = mc_instances[i]) == NULL)
2754 			continue;
2755 		mc_resume(mcp, MC_SOFT_SUSPENDED);
2756 	}
2757 	mutex_exit(&mcmutex);
2758 
2759 	return (0);
2760 }
2761 static void
2762 insert_mcp(mc_opl_t *mcp)
2763 {
2764 	mutex_enter(&mcmutex);
2765 	if (mc_instances[mcp->mc_board_num] != NULL) {
2766 		MC_LOG("mc-opl instance for board# %d already exists\n",
2767 			mcp->mc_board_num);
2768 	}
2769 	mc_instances[mcp->mc_board_num] = mcp;
2770 	mutex_exit(&mcmutex);
2771 }
2772 
2773 static void
2774 delete_mcp(mc_opl_t *mcp)
2775 {
2776 	mutex_enter(&mcmutex);
2777 	mc_instances[mcp->mc_board_num] = 0;
2778 	mutex_exit(&mcmutex);
2779 }
2780 
2781 /* Error injection interface */
2782 
2783 static void
2784 mc_lock_va(uint64_t pa, caddr_t new_va)
2785 {
2786 	tte_t tte;
2787 
2788 	vtag_flushpage(new_va, (uint64_t)ksfmmup);
2789 	sfmmu_memtte(&tte, pa >> PAGESHIFT,
2790 		PROC_DATA|HAT_NOSYNC, TTE8K);
2791 	tte.tte_intlo |= TTE_LCK_INT;
2792 	sfmmu_dtlb_ld_kva(new_va, &tte);
2793 }
2794 
2795 static void
2796 mc_unlock_va(caddr_t va)
2797 {
2798 	vtag_flushpage(va, (uint64_t)ksfmmup);
2799 }
2800 
2801 /* ARGSUSED */
2802 int
2803 mc_inject_error(int error_type, uint64_t pa, uint32_t flags)
2804 {
2805 	mc_opl_t *mcp;
2806 	int bank;
2807 	uint32_t dimm_addr;
2808 	uint32_t cntl;
2809 	mc_rsaddr_info_t rsaddr;
2810 	uint32_t data, stat;
2811 	int both_sides = 0;
2812 	uint64_t pa0;
2813 	int extra_injection_needed = 0;
2814 	extern void cpu_flush_ecache(void);
2815 
2816 	MC_LOG("HW mc_inject_error(%x, %lx, %x)\n", error_type, pa, flags);
2817 
2818 	mutex_enter(&mcmutex);
2819 	if ((mcp = mc_pa_to_mcp(pa)) == NULL) {
2820 		mutex_exit(&mcmutex);
2821 		MC_LOG("mc_inject_error: invalid pa\n");
2822 		return (ENOTSUP);
2823 	}
2824 
2825 	mutex_enter(&mcp->mc_lock);
2826 	mutex_exit(&mcmutex);
2827 
2828 	if (mcp->mc_status & (MC_SOFT_SUSPENDED | MC_DRIVER_SUSPENDED)) {
2829 		mutex_exit(&mcp->mc_lock);
2830 		MC_LOG("mc-opl has been suspended.  No error injection.\n");
2831 		return (EBUSY);
2832 	}
2833 
2834 	/* convert pa to offset within the board */
2835 	MC_LOG("pa %lx, offset %lx\n", pa, pa - mcp->mc_start_address);
2836 
2837 	if (!pa_is_valid(mcp, pa)) {
2838 		mutex_exit(&mcp->mc_lock);
2839 		return (EINVAL);
2840 	}
2841 
2842 	pa0 = pa - mcp->mc_start_address;
2843 
2844 	bank = pa_to_bank(mcp, pa0);
2845 
2846 	if (flags & MC_INJECT_FLAG_OTHER)
2847 		bank = bank ^ 1;
2848 
2849 	if (MC_INJECT_MIRROR(error_type) && !IS_MIRROR(mcp, bank)) {
2850 		mutex_exit(&mcp->mc_lock);
2851 		MC_LOG("Not mirror mode\n");
2852 		return (EINVAL);
2853 	}
2854 
2855 	dimm_addr = pa_to_dimm(mcp, pa0);
2856 
2857 	MC_LOG("injecting error to /LSB%d/B%d/%x\n",
2858 		mcp->mc_board_num, bank, dimm_addr);
2859 
2860 
2861 	switch (error_type) {
2862 	case MC_INJECT_INTERMITTENT_MCE:
2863 	case MC_INJECT_PERMANENT_MCE:
2864 	case MC_INJECT_MUE:
2865 		both_sides = 1;
2866 	}
2867 
2868 	if (flags & MC_INJECT_FLAG_RESET)
2869 		ST_MAC_REG(MAC_EG_CNTL(mcp, bank), 0);
2870 
2871 	ST_MAC_REG(MAC_EG_ADD(mcp, bank), dimm_addr & MAC_EG_ADD_MASK);
2872 
2873 	if (both_sides) {
2874 		ST_MAC_REG(MAC_EG_CNTL(mcp, bank^1), 0);
2875 		ST_MAC_REG(MAC_EG_ADD(mcp, bank^1),
2876 			dimm_addr & MAC_EG_ADD_MASK);
2877 	}
2878 
2879 	switch (error_type) {
2880 	case MC_INJECT_SUE:
2881 		extra_injection_needed = 1;
2882 		/*FALLTHROUGH*/
2883 	case MC_INJECT_UE:
2884 	case MC_INJECT_MUE:
2885 		if (flags & MC_INJECT_FLAG_PATH) {
2886 			cntl = MAC_EG_ADD_FIX
2887 				|MAC_EG_FORCE_READ00|MAC_EG_FORCE_READ16
2888 				|MAC_EG_RDERR_ONCE;
2889 		} else {
2890 			cntl = MAC_EG_ADD_FIX|MAC_EG_FORCE_DERR00
2891 				|MAC_EG_FORCE_DERR16|MAC_EG_DERR_ONCE;
2892 		}
2893 		flags |= MC_INJECT_FLAG_ST;
2894 		break;
2895 	case MC_INJECT_INTERMITTENT_CE:
2896 	case MC_INJECT_INTERMITTENT_MCE:
2897 		if (flags & MC_INJECT_FLAG_PATH) {
2898 			cntl = MAC_EG_ADD_FIX
2899 				|MAC_EG_FORCE_READ00
2900 				|MAC_EG_RDERR_ONCE;
2901 		} else {
2902 			cntl = MAC_EG_ADD_FIX
2903 				|MAC_EG_FORCE_DERR16
2904 				|MAC_EG_DERR_ONCE;
2905 		}
2906 		extra_injection_needed = 1;
2907 		flags |= MC_INJECT_FLAG_ST;
2908 		break;
2909 	case MC_INJECT_PERMANENT_CE:
2910 	case MC_INJECT_PERMANENT_MCE:
2911 		if (flags & MC_INJECT_FLAG_PATH) {
2912 			cntl = MAC_EG_ADD_FIX
2913 				|MAC_EG_FORCE_READ00
2914 				|MAC_EG_RDERR_ALWAYS;
2915 		} else {
2916 			cntl = MAC_EG_ADD_FIX
2917 				|MAC_EG_FORCE_DERR16
2918 				|MAC_EG_DERR_ALWAYS;
2919 		}
2920 		flags |= MC_INJECT_FLAG_ST;
2921 		break;
2922 	case MC_INJECT_CMPE:
2923 		data = 0xabcdefab;
2924 		stphys(pa, data);
2925 		cpu_flush_ecache();
2926 		MC_LOG("CMPE: writing data %x to %lx\n", data, pa);
2927 		ST_MAC_REG(MAC_MIRR(mcp, bank), MAC_MIRR_BANK_EXCLUSIVE);
2928 		stphys(pa, data ^ 0xffffffff);
2929 		membar_sync();
2930 		cpu_flush_ecache();
2931 		ST_MAC_REG(MAC_MIRR(mcp, bank), 0);
2932 		MC_LOG("CMPE: write new data %xto %lx\n", data, pa);
2933 		cntl = 0;
2934 		break;
2935 	case MC_INJECT_NOP:
2936 		cntl = 0;
2937 		break;
2938 	default:
2939 		MC_LOG("mc_inject_error: invalid option\n");
2940 		cntl = 0;
2941 	}
2942 
2943 	if (cntl) {
2944 		ST_MAC_REG(MAC_EG_CNTL(mcp, bank), cntl & MAC_EG_SETUP_MASK);
2945 		ST_MAC_REG(MAC_EG_CNTL(mcp, bank), cntl);
2946 
2947 		if (both_sides) {
2948 			ST_MAC_REG(MAC_EG_CNTL(mcp, bank^1), cntl &
2949 				MAC_EG_SETUP_MASK);
2950 			ST_MAC_REG(MAC_EG_CNTL(mcp, bank^1), cntl);
2951 		}
2952 	}
2953 
2954 	/*
2955 	 * For all injection cases except compare error, we
2956 	 * must write to the PA to trigger the error.
2957 	 */
2958 
2959 	if (flags & MC_INJECT_FLAG_ST) {
2960 		data = 0xf0e0d0c0;
2961 		MC_LOG("Writing %x to %lx\n", data, pa);
2962 		stphys(pa, data);
2963 		cpu_flush_ecache();
2964 	}
2965 
2966 
2967 	if (flags & MC_INJECT_FLAG_LD) {
2968 		if (flags & MC_INJECT_FLAG_PREFETCH) {
2969 			/*
2970 			 * Use strong prefetch operation to
2971 			 * inject MI errors.
2972 			 */
2973 			page_t *pp;
2974 			extern void mc_prefetch(caddr_t);
2975 
2976 			MC_LOG("prefetch\n");
2977 
2978 			pp = page_numtopp_nolock(pa >> PAGESHIFT);
2979 			if (pp != NULL) {
2980 				caddr_t	va, va1;
2981 
2982 				va = ppmapin(pp, PROT_READ|PROT_WRITE,
2983 					(caddr_t)-1);
2984 				kpreempt_disable();
2985 				mc_lock_va((uint64_t)pa, va);
2986 				va1 = va + (pa & (PAGESIZE - 1));
2987 				mc_prefetch(va1);
2988 				mc_unlock_va(va);
2989 				kpreempt_enable();
2990 				ppmapout(va);
2991 
2992 				/*
2993 				 * For MI errors, we need one extra
2994 				 * injection for HW patrol to stop.
2995 				 */
2996 				extra_injection_needed = 1;
2997 			} else {
2998 				cmn_err(CE_WARN, "Cannot find page structure"
2999 					" for PA %lx\n", pa);
3000 			}
3001 		} else {
3002 			MC_LOG("Reading from %lx\n", pa);
3003 			data = ldphys(pa);
3004 			MC_LOG("data = %x\n", data);
3005 		}
3006 
3007 		if (extra_injection_needed) {
3008 			/*
3009 			 * These are the injection cases where the
3010 			 * requested injected errors will not cause the HW
3011 			 * patrol to stop. For these cases, we need to inject
3012 			 * an extra 'real' PTRL error to force the
3013 			 * HW patrol to stop so that we can report the
3014 			 * errors injected. Note that we cannot read
3015 			 * and report error status while the HW patrol
3016 			 * is running.
3017 			 */
3018 			ST_MAC_REG(MAC_EG_CNTL(mcp, bank),
3019 				cntl & MAC_EG_SETUP_MASK);
3020 			ST_MAC_REG(MAC_EG_CNTL(mcp, bank), cntl);
3021 
3022 			if (both_sides) {
3023 			    ST_MAC_REG(MAC_EG_CNTL(mcp, bank^1), cntl &
3024 				MAC_EG_SETUP_MASK);
3025 			    ST_MAC_REG(MAC_EG_CNTL(mcp, bank^1), cntl);
3026 			}
3027 			data = 0xf0e0d0c0;
3028 			MC_LOG("Writing %x to %lx\n", data, pa);
3029 			stphys(pa, data);
3030 			cpu_flush_ecache();
3031 		}
3032 	}
3033 
3034 	if (flags & MC_INJECT_FLAG_RESTART) {
3035 		MC_LOG("Restart patrol\n");
3036 		rsaddr.mi_restartaddr.ma_bd = mcp->mc_board_num;
3037 		rsaddr.mi_restartaddr.ma_bank = bank;
3038 		rsaddr.mi_restartaddr.ma_dimm_addr = dimm_addr;
3039 		rsaddr.mi_valid = 1;
3040 		rsaddr.mi_injectrestart = 1;
3041 		restart_patrol(mcp, bank, &rsaddr);
3042 	}
3043 
3044 	if (flags & MC_INJECT_FLAG_POLL) {
3045 		int running;
3046 		int ebank = (IS_MIRROR(mcp, bank)) ? MIRROR_IDX(bank) : bank;
3047 
3048 		MC_LOG("Poll patrol error\n");
3049 		stat = LD_MAC_REG(MAC_PTRL_STAT(mcp, bank));
3050 		cntl = LD_MAC_REG(MAC_PTRL_CNTL(mcp, bank));
3051 		running = cntl & MAC_CNTL_PTRL_START;
3052 
3053 		if (!running &&
3054 		    (stat & (MAC_STAT_PTRL_ERRS|MAC_STAT_MI_ERRS))) {
3055 			/*
3056 			 * HW patrol stopped and we have errors to
3057 			 * report. Do it.
3058 			 */
3059 			mcp->mc_speedup_period[ebank] = 0;
3060 			rsaddr.mi_valid = 0;
3061 			rsaddr.mi_injectrestart = 0;
3062 			if (IS_MIRROR(mcp, bank)) {
3063 				mc_error_handler_mir(mcp, bank, &rsaddr);
3064 			} else {
3065 				mc_error_handler(mcp, bank, &rsaddr);
3066 			}
3067 
3068 			restart_patrol(mcp, bank, &rsaddr);
3069 		} else {
3070 			/*
3071 			 * We are expecting to report injected
3072 			 * errors but the HW patrol is still running.
3073 			 * Speed up the scanning
3074 			 */
3075 			mcp->mc_speedup_period[ebank] = 2;
3076 			MAC_CMD(mcp, bank, 0);
3077 			restart_patrol(mcp, bank, NULL);
3078 		}
3079 	}
3080 
3081 	mutex_exit(&mcp->mc_lock);
3082 	return (0);
3083 }
3084 
3085 void
3086 mc_stphysio(uint64_t pa, uint32_t data)
3087 {
3088 	MC_LOG("0x%x -> pa(%lx)\n", data, pa);
3089 	stphysio(pa, data);
3090 
3091 	/* force the above write to be processed by mac patrol */
3092 	data = ldphysio(pa);
3093 	MC_LOG("pa(%lx) = 0x%x\n", pa, data);
3094 }
3095 
3096 uint32_t
3097 mc_ldphysio(uint64_t pa)
3098 {
3099 	uint32_t rv;
3100 
3101 	rv = ldphysio(pa);
3102 	MC_LOG("pa(%lx) = 0x%x\n", pa, rv);
3103 	return (rv);
3104 }
3105 
3106 #define	isdigit(ch)	((ch) >= '0' && (ch) <= '9')
3107 
3108 /*
3109  * parse_unum_memory -- extract the board number and the DIMM name from
3110  * the unum.
3111  *
3112  * Return 0 for success and non-zero for a failure.
3113  */
3114 int
3115 parse_unum_memory(char *unum, int *board, char *dname)
3116 {
3117 	char *c;
3118 	char x, y, z;
3119 
3120 	if ((c = strstr(unum, "CMU")) != NULL) {
3121 		/* DC Model */
3122 		c += 3;
3123 		*board = (uint8_t)stoi(&c);
3124 		if ((c = strstr(c, "MEM")) == NULL) {
3125 			return (1);
3126 		}
3127 		c += 3;
3128 		if (strlen(c) < 3) {
3129 			return (2);
3130 		}
3131 		if ((!isdigit(c[0])) || (!(isdigit(c[1]))) ||
3132 		    ((c[2] != 'A') && (c[2] != 'B'))) {
3133 			return (3);
3134 		}
3135 		x = c[0];
3136 		y = c[1];
3137 		z = c[2];
3138 	} else if ((c = strstr(unum, "MBU_")) != NULL) {
3139 		/*  FF1/FF2 Model */
3140 		c += 4;
3141 		if ((c[0] != 'A') && (c[0] != 'B')) {
3142 			return (4);
3143 		}
3144 		if ((c = strstr(c, "MEMB")) == NULL) {
3145 			return (5);
3146 		}
3147 		c += 4;
3148 
3149 		x = c[0];
3150 		*board =  ((uint8_t)stoi(&c)) / 4;
3151 		if ((c = strstr(c, "MEM")) == NULL) {
3152 			return (6);
3153 		}
3154 		c += 3;
3155 		if (strlen(c) < 2) {
3156 			return (7);
3157 		}
3158 		if ((!isdigit(c[0])) || ((c[1] != 'A') && (c[1] != 'B'))) {
3159 			return (8);
3160 		}
3161 		y = c[0];
3162 		z = c[1];
3163 	} else {
3164 		return (9);
3165 	}
3166 	if (*board < 0) {
3167 		return (10);
3168 	}
3169 	dname[0] = x;
3170 	dname[1] = y;
3171 	dname[2] = z;
3172 	dname[3] = '\0';
3173 	return (0);
3174 }
3175 
3176 /*
3177  * mc_get_mem_sid_dimm -- Get the serial-ID for a given board and
3178  * the DIMM name.
3179  */
3180 int
3181 mc_get_mem_sid_dimm(mc_opl_t *mcp, char *dname, char *buf,
3182     int buflen, int *lenp)
3183 {
3184 	int		ret = ENODEV;
3185 	mc_dimm_info_t	*d = NULL;
3186 
3187 	if ((d = mcp->mc_dimm_list) == NULL)
3188 		return (ENOTSUP);
3189 
3190 	for (; d != NULL; d = d->md_next) {
3191 		if (strcmp(d->md_dimmname, dname) == 0) {
3192 			break;
3193 		}
3194 	}
3195 	if (d != NULL) {
3196 		*lenp = strlen(d->md_serial) + strlen(d->md_partnum);
3197 		if (buflen <=  *lenp) {
3198 			cmn_err(CE_WARN, "mc_get_mem_sid_dimm: "
3199 			    "buflen is smaller than %d\n", *lenp);
3200 			ret = ENOSPC;
3201 		} else {
3202 			snprintf(buf, buflen, "%s:%s",
3203 			    d->md_serial, d->md_partnum);
3204 			ret = 0;
3205 		}
3206 	}
3207 	MC_LOG("mc_get_mem_sid_dimm: Ret=%d Name=%s Serial-ID=%s\n",
3208 	    ret, dname, (ret == 0) ? buf : "");
3209 	return (ret);
3210 }
3211 
3212 int
3213 mc_set_mem_sid(mc_opl_t *mcp, char *buf, int buflen, int sb,
3214     int bank, uint32_t mf_type, uint32_t d_slot)
3215 {
3216 	int	lenp = buflen;
3217 	int	id;
3218 	int	ret;
3219 	char	*dimmnm;
3220 
3221 	if (mf_type == FLT_TYPE_PERMANENT_CE) {
3222 		if (plat_model == MODEL_DC) {
3223 			id = BD_BK_SLOT_TO_INDEX(0, bank, d_slot);
3224 			dimmnm = mc_dc_dimm_unum_table[id];
3225 		} else {
3226 			id = BD_BK_SLOT_TO_INDEX(sb, bank, d_slot);
3227 			dimmnm = mc_ff_dimm_unum_table[id];
3228 		}
3229 		if ((ret = mc_get_mem_sid_dimm(mcp, dimmnm, buf, buflen,
3230 		    &lenp)) != 0) {
3231 			return (ret);
3232 		}
3233 	} else {
3234 		return (1);
3235 	}
3236 
3237 	return (0);
3238 }
3239 
3240 /*
3241  * mc_get_mem_sid -- get the DIMM serial-ID corresponding to the unum.
3242  */
3243 int
3244 mc_get_mem_sid(char *unum, char *buf, int buflen, int *lenp)
3245 {
3246 	int	i;
3247 	int	ret = ENODEV;
3248 	int	board;
3249 	char	dname[MCOPL_MAX_DIMMNAME + 1];
3250 	mc_opl_t *mcp;
3251 
3252 	MC_LOG("mc_get_mem_sid: unum=%s buflen=%d\n", unum, buflen);
3253 	if ((ret = parse_unum_memory(unum, &board, dname)) != 0) {
3254 		MC_LOG("mc_get_mem_sid: unum(%s) parsing failed ret=%d\n",
3255 		    unum, ret);
3256 		return (EINVAL);
3257 	}
3258 
3259 	if (board < 0) {
3260 		MC_LOG("mc_get_mem_sid: Invalid board=%d dimm=%s\n",
3261 		    board, dname);
3262 		return (EINVAL);
3263 	}
3264 
3265 	mutex_enter(&mcmutex);
3266 	/*
3267 	 * return ENOENT if we can not find the matching board.
3268 	 */
3269 	ret = ENOENT;
3270 	for (i = 0; i < OPL_MAX_BOARDS; i++) {
3271 		if ((mcp = mc_instances[i]) == NULL)
3272 			continue;
3273 		mutex_enter(&mcp->mc_lock);
3274 		if (mcp->mc_phys_board_num != board) {
3275 			mutex_exit(&mcp->mc_lock);
3276 			continue;
3277 		}
3278 		ret = mc_get_mem_sid_dimm(mcp, dname, buf, buflen, lenp);
3279 		if (ret == 0) {
3280 			mutex_exit(&mcp->mc_lock);
3281 			break;
3282 		}
3283 		mutex_exit(&mcp->mc_lock);
3284 	}
3285 	mutex_exit(&mcmutex);
3286 	return (ret);
3287 }
3288 
3289 /*
3290  * mc_get_mem_offset -- get the offset in a DIMM for a given physical address.
3291  */
3292 int
3293 mc_get_mem_offset(uint64_t paddr, uint64_t *offp)
3294 {
3295 	int		i;
3296 	int		ret = ENODEV;
3297 	mc_addr_t	maddr;
3298 	mc_opl_t	*mcp;
3299 
3300 	mutex_enter(&mcmutex);
3301 	for (i = 0; ((i < OPL_MAX_BOARDS) && (ret != 0)); i++) {
3302 		if ((mcp = mc_instances[i]) == NULL)
3303 			continue;
3304 		mutex_enter(&mcp->mc_lock);
3305 		if (!pa_is_valid(mcp, paddr)) {
3306 			mutex_exit(&mcp->mc_lock);
3307 			continue;
3308 		}
3309 		if (pa_to_maddr(mcp, paddr, &maddr) == 0) {
3310 			*offp = maddr.ma_dimm_addr;
3311 			ret = 0;
3312 		}
3313 		mutex_exit(&mcp->mc_lock);
3314 	}
3315 	mutex_exit(&mcmutex);
3316 	MC_LOG("mc_get_mem_offset: Ret=%d paddr=0x%lx offset=0x%lx\n",
3317 	    ret, paddr, *offp);
3318 	return (ret);
3319 }
3320 
3321 /*
3322  * dname_to_bankslot - Get the bank and slot number from the DIMM name.
3323  */
3324 int
3325 dname_to_bankslot(char *dname, int *bank, int *slot)
3326 {
3327 	int i;
3328 	int tsz;
3329 	char **tbl;
3330 
3331 	if (plat_model == MODEL_DC) { /* DC */
3332 		tbl = mc_dc_dimm_unum_table;
3333 		tsz = OPL_MAX_DIMMS;
3334 	} else {
3335 		tbl = mc_ff_dimm_unum_table;
3336 		tsz = 2 * OPL_MAX_DIMMS;
3337 	}
3338 
3339 	for (i = 0; i < tsz; i++) {
3340 		if (strcmp(dname,  tbl[i]) == 0) {
3341 			break;
3342 		}
3343 	}
3344 	if (i == tsz) {
3345 		return (1);
3346 	}
3347 	*bank = INDEX_TO_BANK(i);
3348 	*slot = INDEX_TO_SLOT(i);
3349 	return (0);
3350 }
3351 
3352 /*
3353  * mc_get_mem_addr -- get the physical address of a DIMM corresponding
3354  * to the unum and sid.
3355  */
3356 int
3357 mc_get_mem_addr(char *unum, char *sid, uint64_t offset, uint64_t *paddr)
3358 {
3359 	int	board;
3360 	int	bank;
3361 	int	slot;
3362 	int	i;
3363 	int	ret = ENODEV;
3364 	char	dname[MCOPL_MAX_DIMMNAME + 1];
3365 	mc_addr_t maddr;
3366 	mc_opl_t *mcp;
3367 
3368 	MC_LOG("mc_get_mem_addr: unum=%s sid=%s offset=0x%lx\n",
3369 	    unum, sid, offset);
3370 	if (parse_unum_memory(unum, &board, dname) != 0) {
3371 		MC_LOG("mc_get_mem_sid: unum(%s) parsing failed ret=%d\n",
3372 		    unum, ret);
3373 		return (EINVAL);
3374 	}
3375 
3376 	if (board < 0) {
3377 		MC_LOG("mc_get_mem_addr: Invalid board=%d dimm=%s\n",
3378 		    board, dname);
3379 		return (EINVAL);
3380 	}
3381 
3382 	mutex_enter(&mcmutex);
3383 	for (i = 0; i < OPL_MAX_BOARDS; i++) {
3384 		if ((mcp = mc_instances[i]) == NULL)
3385 			continue;
3386 		mutex_enter(&mcp->mc_lock);
3387 		if (mcp->mc_phys_board_num != board) {
3388 			mutex_exit(&mcp->mc_lock);
3389 			continue;
3390 		}
3391 
3392 		ret = dname_to_bankslot(dname, &bank, &slot);
3393 		MC_LOG("mc_get_mem_addr: bank=%d slot=%d\n", bank, slot);
3394 		if (ret != 0) {
3395 			MC_LOG("mc_get_mem_addr: dname_to_bankslot failed\n");
3396 			ret = ENODEV;
3397 		} else {
3398 			maddr.ma_bd = mcp->mc_board_num;
3399 			maddr.ma_bank =  bank;
3400 			maddr.ma_dimm_addr = offset;
3401 			ret = mcaddr_to_pa(mcp, &maddr, paddr);
3402 			if (ret != 0) {
3403 				MC_LOG("mc_get_mem_addr: "
3404 				    "mcaddr_to_pa failed\n");
3405 				ret = ENODEV;
3406 			}
3407 			mutex_exit(&mcp->mc_lock);
3408 			break;
3409 		}
3410 		mutex_exit(&mcp->mc_lock);
3411 	}
3412 	mutex_exit(&mcmutex);
3413 	MC_LOG("mc_get_mem_addr: Ret=%d, Paddr=0x%lx\n", ret, *paddr);
3414 	return (ret);
3415 }
3416 
3417 static void
3418 mc_free_dimm_list(mc_dimm_info_t *d)
3419 {
3420 	mc_dimm_info_t *next;
3421 
3422 	while (d != NULL) {
3423 		next = d->md_next;
3424 		kmem_free(d, sizeof (mc_dimm_info_t));
3425 		d = next;
3426 	}
3427 }
3428 
3429 /*
3430  * mc_get_dimm_list -- get the list of dimms with serial-id info
3431  * from the SP.
3432  */
3433 mc_dimm_info_t *
3434 mc_get_dimm_list(mc_opl_t *mcp)
3435 {
3436 	uint32_t	bufsz;
3437 	uint32_t	maxbufsz;
3438 	int		ret;
3439 	int		sexp;
3440 	board_dimm_info_t *bd_dimmp;
3441 	mc_dimm_info_t	*dimm_list = NULL;
3442 
3443 	maxbufsz = bufsz = sizeof (board_dimm_info_t) +
3444 	    ((MCOPL_MAX_DIMMNAME +  MCOPL_MAX_SERIAL +
3445 	    MCOPL_MAX_PARTNUM) * OPL_MAX_DIMMS);
3446 
3447 	bd_dimmp = (board_dimm_info_t *)kmem_alloc(bufsz, KM_SLEEP);
3448 	ret = scf_get_dimminfo(mcp->mc_board_num, (void *)bd_dimmp, &bufsz);
3449 
3450 	MC_LOG("mc_get_dimm_list:  scf_service_getinfo returned=%d\n", ret);
3451 	if (ret == 0) {
3452 		sexp = sizeof (board_dimm_info_t) +
3453 		    ((bd_dimmp->bd_dnamesz +  bd_dimmp->bd_serialsz +
3454 		    bd_dimmp->bd_partnumsz) * bd_dimmp->bd_numdimms);
3455 
3456 		if ((bd_dimmp->bd_version == OPL_DIMM_INFO_VERSION) &&
3457 		    (bd_dimmp->bd_dnamesz <= MCOPL_MAX_DIMMNAME) &&
3458 		    (bd_dimmp->bd_serialsz <= MCOPL_MAX_SERIAL) &&
3459 		    (bd_dimmp->bd_partnumsz <= MCOPL_MAX_PARTNUM) &&
3460 		    (sexp <= bufsz)) {
3461 
3462 #ifdef DEBUG
3463 			if (oplmc_debug)
3464 				mc_dump_dimm_info(bd_dimmp);
3465 #endif
3466 			dimm_list = mc_prepare_dimmlist(bd_dimmp);
3467 
3468 		} else {
3469 			cmn_err(CE_WARN, "DIMM info version mismatch\n");
3470 		}
3471 	}
3472 	kmem_free(bd_dimmp, maxbufsz);
3473 	MC_LOG("mc_get_dimm_list: dimmlist=0x%p\n", dimm_list);
3474 	return (dimm_list);
3475 }
3476 
3477 /*
3478  * mc_prepare_dimmlist - Prepare the dimm list from the information
3479  * received from the SP.
3480  */
3481 mc_dimm_info_t *
3482 mc_prepare_dimmlist(board_dimm_info_t *bd_dimmp)
3483 {
3484 	char	*dimm_name;
3485 	char	*serial;
3486 	char	*part;
3487 	int	dimm;
3488 	int	dnamesz = bd_dimmp->bd_dnamesz;
3489 	int	sersz = bd_dimmp->bd_serialsz;
3490 	int	partsz = bd_dimmp->bd_partnumsz;
3491 	mc_dimm_info_t	*dimm_list = NULL;
3492 	mc_dimm_info_t	*d;
3493 
3494 	dimm_name = (char *)(bd_dimmp + 1);
3495 	for (dimm = 0; dimm < bd_dimmp->bd_numdimms; dimm++) {
3496 
3497 		d = (mc_dimm_info_t *)kmem_alloc(sizeof (mc_dimm_info_t),
3498 		    KM_SLEEP);
3499 		snprintf(d->md_dimmname, dnamesz + 1, "%s", dimm_name);
3500 		serial = dimm_name + dnamesz;
3501 		snprintf(d->md_serial, sersz + 1, "%s", serial);
3502 		part = serial + sersz;
3503 		snprintf(d->md_partnum, partsz + 1, "%s", part);
3504 
3505 		d->md_next = dimm_list;
3506 		dimm_list = d;
3507 		dimm_name = part + partsz;
3508 	}
3509 	return (dimm_list);
3510 }
3511 
3512 #ifdef DEBUG
3513 void
3514 mc_dump_dimm(char *buf, int dnamesz, int serialsz, int partnumsz)
3515 {
3516 	char dname[MCOPL_MAX_DIMMNAME + 1];
3517 	char serial[MCOPL_MAX_SERIAL + 1];
3518 	char part[ MCOPL_MAX_PARTNUM + 1];
3519 	char *b;
3520 
3521 	b = buf;
3522 	snprintf(dname, dnamesz + 1, "%s", b);
3523 	b += dnamesz;
3524 	snprintf(serial, serialsz + 1, "%s", b);
3525 	b += serialsz;
3526 	snprintf(part, partnumsz + 1, "%s", b);
3527 	printf("DIMM=%s  Serial=%s PartNum=%s\n", dname, serial, part);
3528 }
3529 
3530 void
3531 mc_dump_dimm_info(board_dimm_info_t *bd_dimmp)
3532 {
3533 	int	dimm;
3534 	int	dnamesz = bd_dimmp->bd_dnamesz;
3535 	int	sersz = bd_dimmp->bd_serialsz;
3536 	int	partsz = bd_dimmp->bd_partnumsz;
3537 	char	*buf;
3538 
3539 	printf("Version=%d Board=%02d DIMMs=%d NameSize=%d "
3540 	    "SerialSize=%d PartnumSize=%d\n", bd_dimmp->bd_version,
3541 	    bd_dimmp->bd_boardnum, bd_dimmp->bd_numdimms, bd_dimmp->bd_dnamesz,
3542 	    bd_dimmp->bd_serialsz, bd_dimmp->bd_partnumsz);
3543 	printf("======================================================\n");
3544 
3545 	buf = (char *)(bd_dimmp + 1);
3546 	for (dimm = 0; dimm < bd_dimmp->bd_numdimms; dimm++) {
3547 		mc_dump_dimm(buf, dnamesz, sersz, partsz);
3548 		buf += dnamesz + sersz + partsz;
3549 	}
3550 	printf("======================================================\n");
3551 }
3552 
3553 
3554 /* ARGSUSED */
3555 static int
3556 mc_ioctl_debug(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
3557 	int *rvalp)
3558 {
3559 	caddr_t	buf;
3560 	uint64_t pa;
3561 	int rv = 0;
3562 	int i;
3563 	uint32_t flags;
3564 	static uint32_t offset = 0;
3565 
3566 
3567 	flags = (cmd >> 4) & 0xfffffff;
3568 
3569 	cmd &= 0xf;
3570 
3571 	MC_LOG("mc_ioctl(cmd = %x, flags = %x)\n", cmd, flags);
3572 
3573 	if (arg != NULL) {
3574 		if (ddi_copyin((const void *)arg, (void *)&pa,
3575 			sizeof (uint64_t), 0) < 0) {
3576 			rv = EFAULT;
3577 			return (rv);
3578 		}
3579 		buf = NULL;
3580 	} else {
3581 		buf = (caddr_t)kmem_alloc(PAGESIZE, KM_SLEEP);
3582 
3583 		pa = va_to_pa(buf);
3584 		pa += offset;
3585 
3586 		offset += 64;
3587 		if (offset >= PAGESIZE)
3588 			offset = 0;
3589 	}
3590 
3591 	switch (cmd) {
3592 	case MCI_CE:
3593 		mc_inject_error(MC_INJECT_INTERMITTENT_CE, pa,
3594 			flags);
3595 		break;
3596 	case MCI_PERM_CE:
3597 		mc_inject_error(MC_INJECT_PERMANENT_CE, pa,
3598 			flags);
3599 		break;
3600 	case MCI_UE:
3601 		mc_inject_error(MC_INJECT_UE, pa,
3602 			flags);
3603 		break;
3604 	case MCI_M_CE:
3605 		mc_inject_error(MC_INJECT_INTERMITTENT_MCE, pa,
3606 			flags);
3607 		break;
3608 	case MCI_M_PCE:
3609 		mc_inject_error(MC_INJECT_PERMANENT_MCE, pa,
3610 			flags);
3611 		break;
3612 	case MCI_M_UE:
3613 		mc_inject_error(MC_INJECT_MUE, pa,
3614 			flags);
3615 		break;
3616 	case MCI_CMP:
3617 		mc_inject_error(MC_INJECT_CMPE, pa,
3618 			flags);
3619 		break;
3620 	case MCI_NOP:
3621 		mc_inject_error(MC_INJECT_NOP, pa, flags);
3622 		break;
3623 	case MCI_SHOW_ALL:
3624 		mc_debug_show_all = 1;
3625 		break;
3626 	case MCI_SHOW_NONE:
3627 		mc_debug_show_all = 0;
3628 		break;
3629 	case MCI_ALLOC:
3630 		/*
3631 		 * just allocate some kernel memory and never free it
3632 		 * 512 MB seems to be the maximum size supported.
3633 		 */
3634 		cmn_err(CE_NOTE, "Allocating kmem %d MB\n", flags * 512);
3635 		for (i = 0; i < flags; i++) {
3636 			buf = kmem_alloc(512 * 1024 * 1024, KM_SLEEP);
3637 			cmn_err(CE_NOTE, "kmem buf %llx PA %llx\n",
3638 				(u_longlong_t)buf, (u_longlong_t)va_to_pa(buf));
3639 		}
3640 		break;
3641 	case MCI_SUSPEND:
3642 		(void) opl_mc_suspend();
3643 		break;
3644 	case MCI_RESUME:
3645 		(void) opl_mc_resume();
3646 		break;
3647 	default:
3648 		rv = ENXIO;
3649 	}
3650 	return (rv);
3651 }
3652 
3653 #endif /* DEBUG */
3654