Home | History | Annotate | Download | only in io
      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 2008 Sun Microsystems, Inc.  All rights reserved.
     23  * Use is subject to license terms.
     24  */
     25 
     26 
     27 /*
     28  * bscv.c - multi-threaded lom driver for the Stiletto platform.
     29  */
     30 
     31 /*
     32  * Included files.
     33  */
     34 
     35 #include <sys/note.h>
     36 #include <sys/types.h>
     37 #include <sys/param.h>
     38 #include <sys/uio.h>
     39 #include <sys/open.h>
     40 #include <sys/cred.h>
     41 #include <sys/stream.h>
     42 #include <sys/systm.h>
     43 #include <sys/conf.h>
     44 #include <sys/reboot.h>
     45 #include <sys/modctl.h>
     46 #include <sys/mkdev.h>
     47 #include <sys/errno.h>
     48 #include <sys/debug.h>
     49 #include <sys/kmem.h>
     50 #include <sys/consdev.h>
     51 #include <sys/file.h>
     52 #include <sys/stat.h>
     53 #include <sys/disp.h>
     54 #include <sys/ddi.h>
     55 #include <sys/sunddi.h>
     56 #include <sys/stream.h>
     57 #include <sys/strlog.h>
     58 #include <sys/log.h>
     59 #include <sys/utsname.h>
     60 #include <sys/callb.h>
     61 #include <sys/sysevent.h>
     62 #include <sys/nvpair.h>
     63 #include <sys/sysevent/eventdefs.h>
     64 #include <sys/sysevent/domain.h>
     65 #include <sys/sysevent/env.h>
     66 #include <sys/sysevent/dr.h>
     67 
     68 #include <sys/lom_io.h>
     69 #include <sys/bscbus.h>
     70 #include <sys/bscv_impl.h>
     71 
     72 /*
     73  * Variables defined here and visible internally only
     74  */
     75 
     76 static void *bscv_statep = NULL;
     77 
     78 /*
     79  * Forward declarations
     80  */
     81 
     82 static int bscv_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
     83 static int bscv_attach(dev_info_t *, ddi_attach_cmd_t);
     84 static int bscv_detach(dev_info_t *, ddi_detach_cmd_t);
     85 static int bscv_reset(dev_info_t *, ddi_reset_cmd_t);
     86 static int bscv_quiesce(dev_info_t *);
     87 static int bscv_map_regs(bscv_soft_state_t *);
     88 static void bscv_unmap_regs(bscv_soft_state_t *);
     89 static void bscv_map_chan_logical_physical(bscv_soft_state_t *);
     90 
     91 static int bscv_open(dev_t *, int, int, cred_t *);
     92 static int bscv_close(dev_t, int, int, cred_t *);
     93 static void bscv_full_stop(bscv_soft_state_t *);
     94 
     95 static void bscv_enter(bscv_soft_state_t *);
     96 static void bscv_exit(bscv_soft_state_t *);
     97 #ifdef DEBUG
     98 static int bscv_held(bscv_soft_state_t *);
     99 #endif /* DEBUG */
    100 
    101 static void bscv_put8(bscv_soft_state_t *, int, bscv_addr_t, uint8_t);
    102 static void bscv_put16(bscv_soft_state_t *, int, bscv_addr_t, uint16_t);
    103 static void bscv_put32(bscv_soft_state_t *, int, bscv_addr_t, uint32_t);
    104 static uint8_t bscv_get8(bscv_soft_state_t *, int, bscv_addr_t);
    105 static uint16_t bscv_get16(bscv_soft_state_t *, int, bscv_addr_t);
    106 static uint32_t bscv_get32(bscv_soft_state_t *, int, bscv_addr_t);
    107 static void bscv_setclear8(bscv_soft_state_t *, int,
    108 	bscv_addr_t, uint8_t, uint8_t);
    109 static void bscv_setclear8_volatile(bscv_soft_state_t *, int,
    110 	bscv_addr_t, uint8_t, uint8_t);
    111 static void bscv_rep_rw8(bscv_soft_state_t *, int,
    112 	uint8_t *, bscv_addr_t, size_t, uint_t, boolean_t);
    113 static uint8_t bscv_get8_cached(bscv_soft_state_t *, bscv_addr_t);
    114 
    115 static uint8_t bscv_get8_locked(bscv_soft_state_t *, int, bscv_addr_t, int *);
    116 static void bscv_rep_get8_locked(bscv_soft_state_t *, int,
    117 	uint8_t *, bscv_addr_t, size_t, uint_t, int *);
    118 
    119 static boolean_t bscv_faulty(bscv_soft_state_t *);
    120 static void bscv_clear_fault(bscv_soft_state_t *);
    121 static void bscv_set_fault(bscv_soft_state_t *);
    122 static boolean_t bscv_session_error(bscv_soft_state_t *);
    123 static int bscv_retcode(bscv_soft_state_t *);
    124 static int bscv_should_retry(bscv_soft_state_t *);
    125 static void bscv_locked_result(bscv_soft_state_t *, int *);
    126 
    127 static void bscv_put8_once(bscv_soft_state_t *, int, bscv_addr_t, uint8_t);
    128 static uint8_t bscv_get8_once(bscv_soft_state_t *, int, bscv_addr_t);
    129 static uint32_t bscv_probe(bscv_soft_state_t *, int, uint32_t *);
    130 static void bscv_resync_comms(bscv_soft_state_t *, int);
    131 
    132 static boolean_t bscv_window_setup(bscv_soft_state_t *);
    133 static int bscv_eerw(bscv_soft_state_t *, uint32_t, uint8_t *,
    134     unsigned, boolean_t);
    135 
    136 static int bscv_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
    137 static int bscv_ioc_dogstate(bscv_soft_state_t *, intptr_t, int);
    138 static int bscv_ioc_psustate(bscv_soft_state_t *, intptr_t, int);
    139 static int bscv_ioc_fanstate(bscv_soft_state_t *, intptr_t, int);
    140 static int bscv_ioc_fledstate(bscv_soft_state_t *, intptr_t, int);
    141 static int bscv_ioc_ledstate(bscv_soft_state_t *, intptr_t, int);
    142 static int bscv_ioc_info(bscv_soft_state_t *, intptr_t, int);
    143 static int bscv_ioc_mread(bscv_soft_state_t *, intptr_t, int);
    144 static int bscv_ioc_volts(bscv_soft_state_t *, intptr_t, int);
    145 static int bscv_ioc_stats(bscv_soft_state_t *, intptr_t, int);
    146 static int bscv_ioc_temp(bscv_soft_state_t *, intptr_t, int);
    147 static int bscv_ioc_cons(bscv_soft_state_t *, intptr_t, int);
    148 static int bscv_ioc_eventlog2(bscv_soft_state_t *, intptr_t, int);
    149 static int bscv_ioc_info2(bscv_soft_state_t *, intptr_t, int);
    150 static int bscv_ioc_test(bscv_soft_state_t *, intptr_t, int);
    151 static int bscv_ioc_mprog2(bscv_soft_state_t *, intptr_t, int);
    152 static int bscv_ioc_mread2(bscv_soft_state_t *, intptr_t, int);
    153 
    154 static void bscv_event_daemon(void *);
    155 static void bscv_start_event_daemon(bscv_soft_state_t *);
    156 static int bscv_stop_event_daemon(bscv_soft_state_t *);
    157 static int bscv_pause_event_daemon(bscv_soft_state_t *);
    158 static void bscv_resume_event_daemon(bscv_soft_state_t *);
    159 static void bscv_event_process(bscv_soft_state_t *ssp, boolean_t);
    160 static int bscv_event_validate(bscv_soft_state_t *, uint32_t, uint8_t);
    161 static void bscv_event_process_one(bscv_soft_state_t *, lom_event_t *);
    162 static void bscv_build_eventstring(bscv_soft_state_t *,
    163     lom_event_t *, char *, char *);
    164 static int bscv_level_of_event(lom_event_t *);
    165 static void bscv_status(bscv_soft_state_t *, uint8_t, uint8_t);
    166 char *bscv_get_label(char [][MAX_LOM2_NAME_STR], int, int);
    167 static void bscv_generic_sysevent(bscv_soft_state_t *, char *, char *, char *,
    168     char *, int32_t, char *);
    169 static void bscv_sysevent(bscv_soft_state_t *, lom_event_t *);
    170 
    171 static int bscv_prog(bscv_soft_state_t *, intptr_t, int);
    172 static int bscv_prog_image(bscv_soft_state_t *, boolean_t,
    173     uint8_t *, int, uint32_t);
    174 static int bscv_prog_receive_image(bscv_soft_state_t *, lom_prog_t *,
    175     uint8_t *, int);
    176 static void bscv_leave_programming_mode(bscv_soft_state_t *, boolean_t);
    177 static int bscv_prog_stop_lom(bscv_soft_state_t *);
    178 static int bscv_prog_start_lom(bscv_soft_state_t *);
    179 
    180 static int bscv_attach_common(bscv_soft_state_t *);
    181 static int bscv_cleanup(bscv_soft_state_t *);
    182 static void bscv_setup_capability(bscv_soft_state_t *);
    183 static int bscv_probe_check(bscv_soft_state_t *);
    184 static void bscv_setup_hostname(bscv_soft_state_t *);
    185 static void bscv_read_hostname(bscv_soft_state_t *, char *);
    186 static void bscv_write_hostname(bscv_soft_state_t *, char *, uint8_t);
    187 static void bscv_setup_static_info(bscv_soft_state_t *);
    188 static uint8_t bscv_read_env_name(bscv_soft_state_t *, uint8_t,
    189     uint8_t, uint8_t, char [][MAX_LOM2_NAME_STR], int);
    190 static void bscv_setup_events(bscv_soft_state_t *);
    191 
    192 static void bscv_trace(bscv_soft_state_t *, char, const char *,
    193     const char *, ...);
    194 
    195 #ifdef __sparc
    196 static void bscv_idi_init();
    197 static void bscv_idi_fini();
    198 static void bscv_idi_new_instance(dev_info_t *dip);
    199 static void bscv_idi_clear_err();
    200 void bscv_idi_set(struct bscv_idi_info info);
    201 static boolean_t bscv_idi_err();
    202 static boolean_t bscv_nodename_set(struct bscv_idi_info info);
    203 static boolean_t bscv_sig_set(struct bscv_idi_info info);
    204 static boolean_t bscv_wdog_pat(struct bscv_idi_info info);
    205 static boolean_t bscv_wdog_cfg(struct bscv_idi_info info);
    206 static void bscv_write_sig(bscv_soft_state_t *ssp, bscv_sig_t s);
    207 #endif /* __sparc */
    208 
    209 static void bscv_setup_watchdog(bscv_soft_state_t *ssp);
    210 static void bscv_write_wdog_cfg(bscv_soft_state_t *,
    211     uint_t, boolean_t, uint8_t);
    212 
    213 #if defined(__i386) || defined(__amd64)
    214 static void bscv_inform_bsc(bscv_soft_state_t *, uint32_t);
    215 static void bscv_watchdog_pat_request(void *);
    216 static void bscv_watchdog_cfg_request(bscv_soft_state_t *, uint8_t);
    217 static uint_t bscv_set_watchdog_timer(bscv_soft_state_t *, uint_t);
    218 static void bscv_clear_watchdog_timer(bscv_soft_state_t *);
    219 
    220 static boolean_t bscv_panic_callback(void *, int);
    221 static void bscv_watchdog_cyclic_add(bscv_soft_state_t *);
    222 static void bscv_watchdog_cyclic_remove(bscv_soft_state_t *);
    223 
    224 static uint8_t	wdog_reset_on_timeout = 1;
    225 
    226 #define	WDOG_ON			1
    227 #define	WDOG_OFF		0
    228 #define	CLK_WATCHDOG_DEFAULT	10		/* 10 seconds */
    229 #define	WATCHDOG_PAT_INTERVAL	1000000000	/* 1 second */
    230 
    231 static int	bscv_watchdog_enable;
    232 static int	bscv_watchdog_available;
    233 static int	watchdog_activated;
    234 static uint_t	bscv_watchdog_timeout_seconds;
    235 #endif /* __i386 || __amd64 */
    236 
    237 #ifdef __sparc
    238 struct bscv_idi_callout bscv_idi_callout_table[] = {
    239 	{BSCV_IDI_NODENAME,	&bscv_nodename_set	},
    240 	{BSCV_IDI_SIG,		&bscv_sig_set		},
    241 	{BSCV_IDI_WDOG_PAT,	&bscv_wdog_pat		},
    242 	{BSCV_IDI_WDOG_CFG,	&bscv_wdog_cfg		},
    243 	{BSCV_IDI_NULL,		NULL			}
    244 };
    245 
    246 static struct bscv_idi_callout_mgr bscv_idi_mgr;
    247 #endif /* __sparc */
    248 
    249 /*
    250  * Local Definitions
    251  */
    252 #define	STATUS_READ_LIMIT	8   /* Read up to 8 status changes at a time */
    253 #define	MYNAME			"bscv"
    254 #define	BSCV_INST_TO_MINOR(i)	(i)
    255 #define	BSCV_MINOR_TO_INST(m)	(m)
    256 #define	ddi_driver_major(dip)	ddi_name_to_major(ddi_binding_name(dip))
    257 
    258 /*
    259  * Strings for daemon event reporting
    260  */
    261 
    262 static char *eventSubsysStrings[] =
    263 {	"",				/* 00 */
    264 	"Alarm ",			/* 01 */
    265 	"temperature sensor ",		/* 02 */
    266 	"overheat sensor ",		/* 03 */
    267 	"Fan ",				/* 04 */
    268 	"supply rail ",			/* 05 */
    269 	"circuit breaker ",		/* 06 */
    270 	"PSU ",				/* 07 */
    271 	"user ",			/* 08 */
    272 	"phonehome ",			/* 09; unutilized */
    273 	"LOM ",				/* 0a */
    274 	"host ",			/* 0b */
    275 	"event log ",			/* 0c */
    276 	"",				/* 0d; EVENT_SUBSYS_EXTRA unutilized */
    277 	"LED ",				/* 0e */
    278 };
    279 
    280 static char *eventTypeStrings[] =
    281 {
    282 	"[null event]",			/* 00 */
    283 	"ON",				/* 01 */
    284 	"OFF",				/* 02 */
    285 	"state change",			/* 03 */
    286 	"power on",			/* 04 */
    287 	"power off",			/* 05 */
    288 	"powered off unexpectedly",	/* 06 */
    289 	"reset unexpectedly",		/* 07 */
    290 	"booted",			/* 08 */
    291 	"watchdog enabled",		/* 09 */
    292 	"watchdog disabled",		/* 0a */
    293 	"watchdog triggered",		/* 0b */
    294 	"failed",			/* 0c */
    295 	"recovered",			/* 0d */
    296 	"reset",			/* 0e */
    297 	"XIR reset",			/* 0f */
    298 	"console selected",		/* 10 */
    299 	"time reference",		/* 11 */
    300 	"script failure",		/* 12 */
    301 	"modem access failure",		/* 13 */
    302 	"modem dialing failure",	/* 14 */
    303 	"bad checksum",			/* 15 */
    304 	"added",			/* 16 */
    305 	"removed",			/* 17 */
    306 	"changed",			/* 18 */
    307 	"login",			/* 19 */
    308 	"password changed",		/* 1a */
    309 	"login failed",			/* 1b */
    310 	"logout",			/* 1c */
    311 	"flash download",		/* 1d */
    312 	"data lost",			/* 1e */
    313 	"device busy",			/* 1f */
    314 	"fault led state",		/* 20 */
    315 	"overheat",			/* 21 */
    316 	"severe overheat",		/* 22 */
    317 	"no overheat",			/* 23 */
    318 	"SCC",				/* 24 */
    319 	"device inaccessible",		/* 25 */
    320 	"Hostname change",		/* 26 */
    321 	"CPU signature timeout",	/* 27 */
    322 	"Bootmode change",		/* 28 */
    323 	"Watchdog change policy",	/* 29 */
    324 	"Watchdog change timeout",	/* 2a */
    325 };
    326 
    327 /*
    328  * These store to mapping between the logical service, e.g. chan_prog for
    329  * programming, and the actual Xbus channel which carries that traffic.
    330  * Any services can be shared on the same channel apart from chan_wdogpat.
    331  */
    332 static int chan_general;	/* General Traffic */
    333 static int chan_wdogpat;	/* Watchdog Patting */
    334 static int chan_cpusig;		/* CPU signatures */
    335 static int chan_eeprom;		/* EEPROM I/O */
    336 static int chan_prog;		/* Programming */
    337 
    338 /*
    339  * cb_ops structure defining the driver entry points
    340  */
    341 
    342 static struct cb_ops bscv_cb_ops = {
    343 	bscv_open,	/* open */
    344 	bscv_close,	/* close */
    345 	nodev,		/* strategy */
    346 	nodev,		/* print */
    347 	nodev,		/* dump */
    348 	nodev,		/* read */
    349 	nodev,		/* write */
    350 	bscv_ioctl,	/* ioctl */
    351 	nodev,		/* devmap */
    352 	nodev,		/* mmap */
    353 	nodev,		/* segmap */
    354 	nochpoll,	/* poll */
    355 	ddi_prop_op,	/* prop op */
    356 	NULL,		/* ! STREAMS */
    357 	D_NEW | D_MP	/* MT/MP Safe */
    358 };
    359 
    360 /*
    361  * dev_ops structure defining autoconfiguration driver autoconfiguration
    362  * routines
    363  */
    364 
    365 static struct dev_ops bscv_dev_ops = {
    366 	DEVO_REV,		/* devo_rev */
    367 	0,			/* devo_refcnt */
    368 	bscv_getinfo,		/* devo_getinfo */
    369 	nulldev,		/* devo_identify */
    370 	nulldev,		/* devo_probe */
    371 	bscv_attach,		/* devo_attach */
    372 	bscv_detach,		/* devo_detach */
    373 	bscv_reset,		/* devo_reset */
    374 	&bscv_cb_ops,		/* devo_cb_ops */
    375 	(struct bus_ops *)0,	/* devo_bus_ops */
    376 	NULL,			/* devo_power */
    377 	bscv_quiesce,		/* devo_quiesce */
    378 };
    379 
    380 /*
    381  * module configuration section
    382  */
    383 
    384 #ifdef DEBUG
    385 #define	BSCV_VERSION_STRING "bscv driver - Debug"
    386 #else /* DEBUG */
    387 #define	BSCV_VERSION_STRING "bscv driver"
    388 #endif /* DEBUG */
    389 
    390 static struct modldrv modldrv = {
    391 	&mod_driverops,
    392 	BSCV_VERSION_STRING,
    393 	&bscv_dev_ops,
    394 };
    395 
    396 static struct modlinkage modlinkage = {
    397 	MODREV_1,
    398 	&modldrv,
    399 	NULL
    400 };
    401 
    402 /*
    403  * kernel accessible routines. These routines are necessarily global so the
    404  * driver can be loaded, and unloaded successfully
    405  */
    406 
    407 /*
    408  * function	- _init
    409  * description	- initializes the driver state structure and installs the
    410  *		  driver module into the kernel
    411  * inputs	- none
    412  * outputs	- success or failure of module installation
    413  */
    414 
    415 int
    416 _init(void)
    417 {
    418 	register int e;
    419 
    420 	if ((e = ddi_soft_state_init(&bscv_statep,
    421 	    sizeof (bscv_soft_state_t), 1)) != 0) {
    422 		return (e);
    423 	}
    424 
    425 	if ((e = mod_install(&modlinkage)) != 0) {
    426 		ddi_soft_state_fini(&bscv_statep);
    427 	}
    428 
    429 #ifdef __sparc
    430 	if (e == 0) bscv_idi_init();
    431 #endif /* __sparc */
    432 	return (e);
    433 }
    434 
    435 /*
    436  * function	- _info
    437  * description	- provide information about a kernel loaded module
    438  * inputs	- module infomation
    439  * outputs	- success or failure of information request
    440  */
    441 
    442 int
    443 _info(struct modinfo *modinfop)
    444 {
    445 	return (mod_info(&modlinkage, modinfop));
    446 }
    447 
    448 /*
    449  * function	- _fini
    450  * description	- removes a module from the kernel and frees the driver soft
    451  *		  state memory
    452  * inputs	- none
    453  * outputs	- success or failure of module removal
    454  */
    455 
    456 int
    457 _fini(void)
    458 {
    459 	register int e;
    460 
    461 	if ((e = mod_remove(&modlinkage)) != 0) {
    462 		return (e);
    463 	}
    464 
    465 #ifdef __sparc
    466 	bscv_idi_fini();
    467 #endif /* __sparc */
    468 	ddi_soft_state_fini(&bscv_statep);
    469 
    470 	return (e);
    471 }
    472 
    473 /*
    474  * function	- bscv_getinfo
    475  * description	- routine used to provide information on the driver
    476  * inputs	- device information structure, command, command arg, storage
    477  *		  area for the result
    478  * outputs	- DDI_SUCCESS or DDI_FAILURE
    479  */
    480 
    481 /*ARGSUSED*/
    482 static int
    483 bscv_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
    484 {
    485 	bscv_soft_state_t *ssp;
    486 	dev_t	dev = (dev_t)arg;
    487 	int	instance;
    488 	int	error;
    489 
    490 	instance = DEVICETOINSTANCE(dev);
    491 
    492 	switch (cmd) {
    493 	case DDI_INFO_DEVT2INSTANCE:
    494 		*result = (void *)(uintptr_t)instance;
    495 		error = DDI_SUCCESS;
    496 		break;
    497 
    498 	case DDI_INFO_DEVT2DEVINFO:
    499 		ssp = ddi_get_soft_state(bscv_statep, instance);
    500 		if (ssp == NULL)
    501 			return (DDI_FAILURE);
    502 		*result = (void *) ssp->dip;
    503 		error = DDI_SUCCESS;
    504 		break;
    505 
    506 	default:
    507 		error = DDI_FAILURE;
    508 		break;
    509 	}
    510 
    511 	return (error);
    512 }
    513 
    514 #ifdef __sparc
    515 void
    516 bscv_idi_init()
    517 {
    518 	bscv_idi_mgr.valid_inst = (uint32_t)~0;    /* No valid instances */
    519 	bscv_idi_mgr.tbl = bscv_idi_callout_table;
    520 	bscv_idi_mgr.errs = 0;
    521 
    522 	/*
    523 	 * Now that all fields are initialized, set the magic flag.  This is
    524 	 * a kind of integrity check for the data structure.
    525 	 */
    526 	bscv_idi_mgr.magic = BSCV_IDI_CALLOUT_MAGIC;
    527 }
    528 
    529 static void
    530 bscv_idi_clear_err()
    531 {
    532 	ASSERT(bscv_idi_mgr.magic == BSCV_IDI_CALLOUT_MAGIC);
    533 
    534 	bscv_idi_mgr.errs = 0;
    535 }
    536 
    537 /*
    538  * function	- bscv_idi_err
    539  * description	- error messaging service which throttles the number of error
    540  *		  messages to avoid overflowing storage
    541  * inputs	- none
    542  * returns	- boolean to indicate whether a message should be reported
    543  * side-effects	- updates the error number counter
    544  */
    545 static boolean_t
    546 bscv_idi_err()
    547 {
    548 	ASSERT(bscv_idi_mgr.magic == BSCV_IDI_CALLOUT_MAGIC);
    549 
    550 	bscv_idi_mgr.errs++;
    551 
    552 	if (bscv_idi_mgr.errs++ < BSCV_IDI_ERR_MSG_THRESHOLD)
    553 		return (B_TRUE);
    554 
    555 	return (B_FALSE);
    556 }
    557 
    558 void
    559 bscv_idi_new_instance(dev_info_t *dip)
    560 {
    561 	ASSERT(bscv_idi_mgr.magic == BSCV_IDI_CALLOUT_MAGIC);
    562 
    563 	/*
    564 	 * We don't care how many instances we have, or their value, so long
    565 	 * as we have at least one valid value.  This is so service routines
    566 	 * can get any required locks via a soft state pointer.
    567 	 */
    568 	if (bscv_idi_mgr.valid_inst == (uint32_t)~0) {
    569 		bscv_idi_mgr.valid_inst = ddi_get_instance(dip);
    570 	}
    571 }
    572 
    573 void
    574 bscv_idi_fini()
    575 {
    576 	bscv_idi_mgr.valid_inst = (uint32_t)~0;    /* No valid instances */
    577 	bscv_idi_mgr.tbl = NULL;
    578 }
    579 #endif /* __sparc */
    580 
    581 /*
    582  * function	- bscv_attach
    583  * description	- this routine is responsible for setting aside memory for the
    584  *		  driver data structures, initialising the mutexes and creating
    585  *		  the device minor nodes. Additionally, this routine calls the
    586  *		  the callback routine.
    587  * inputs	- device information structure, DDI_ATTACH command
    588  * outputs	- DDI_SUCCESS or DDI_FAILURE
    589  */
    590 
    591 int
    592 bscv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
    593 {
    594 	bscv_soft_state_t *ssp;
    595 	int	instance;
    596 
    597 	switch (cmd) {
    598 	case DDI_ATTACH:
    599 
    600 		instance = ddi_get_instance(dip);
    601 
    602 		if (ddi_soft_state_zalloc(bscv_statep, instance) !=
    603 		    DDI_SUCCESS) {
    604 			return (DDI_FAILURE);
    605 		}
    606 
    607 
    608 		ssp = ddi_get_soft_state(bscv_statep, instance);
    609 
    610 		ssp->progress = 0;
    611 
    612 		ssp->dip = dip;
    613 		ssp->instance = instance;
    614 		ssp->event_waiting = B_FALSE;
    615 		ssp->status_change = B_FALSE;
    616 		ssp->nodename_change = B_FALSE;
    617 		ssp->cap0 = 0;
    618 		ssp->cap1 = 0;
    619 		ssp->cap2 = 0;
    620 		ssp->prog_mode_only = B_FALSE;
    621 		ssp->programming = B_FALSE;
    622 		ssp->cssp_prog = B_FALSE;
    623 		ssp->task_flags = 0;
    624 		ssp->debug = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
    625 		    DDI_PROP_DONTPASS, "debug", 0);
    626 		ssp->majornum = ddi_driver_major(dip);
    627 		ssp->minornum = BSCV_INST_TO_MINOR(instance);
    628 #if defined(__i386) || defined(__amd64)
    629 		ssp->last_nodename[0] = '\0';
    630 #endif /* __i386 || __amd64 */
    631 
    632 		/*
    633 		 * initialise the mutexes
    634 		 */
    635 
    636 		mutex_init(&ssp->cmd_mutex, NULL, MUTEX_DRIVER, NULL);
    637 
    638 		mutex_init(&ssp->task_mu, NULL, MUTEX_DRIVER, NULL);
    639 		cv_init(&ssp->task_cv, NULL, CV_DRIVER, NULL);
    640 		cv_init(&ssp->task_evnt_cv, NULL, CV_DRIVER, NULL);
    641 		mutex_init(&ssp->prog_mu, NULL, MUTEX_DRIVER, NULL);
    642 		ssp->progress |= BSCV_LOCKS;
    643 
    644 		bscv_trace(ssp, 'A', "bscv_attach",
    645 		    "bscv_attach: mutexes and condition vars initialised");
    646 
    647 		/* Map in physical communication channels */
    648 
    649 		if (bscv_map_regs(ssp) != DDI_SUCCESS) {
    650 			(void) bscv_cleanup(ssp);
    651 			return (DDI_FAILURE);
    652 		}
    653 		ssp->progress |= BSCV_MAPPED_REGS;
    654 
    655 		/* Associate logical channels to physical channels */
    656 
    657 		bscv_map_chan_logical_physical(ssp);
    658 
    659 		bscv_enter(ssp);
    660 
    661 		bscv_leave_programming_mode(ssp, B_FALSE);
    662 
    663 		if (bscv_attach_common(ssp) == DDI_FAILURE) {
    664 			bscv_exit(ssp);
    665 			(void) bscv_cleanup(ssp);
    666 			return (DDI_FAILURE);
    667 		}
    668 
    669 #ifdef __sparc
    670 		/*
    671 		 * At this point the inter-driver-interface is made available.
    672 		 * The IDI uses the event thread service which
    673 		 * bscv_attach_common() sets up.
    674 		 */
    675 		bscv_idi_new_instance(dip);
    676 #endif /* __sparc */
    677 
    678 		bscv_exit(ssp);
    679 
    680 		/*
    681 		 * now create the minor nodes
    682 		 */
    683 		if (ddi_create_minor_node(ssp->dip, "lom", S_IFCHR,
    684 		    BSCV_INST_TO_MINOR(instance),
    685 		    DDI_PSEUDO, 0) != DDI_SUCCESS) {
    686 			(void) bscv_cleanup(ssp);
    687 			return (DDI_FAILURE);
    688 		}
    689 		bscv_trace(ssp, 'A', "bscv_attach",
    690 		    "bscv_attach: device minor nodes created");
    691 		ssp->progress |= BSCV_NODES;
    692 
    693 		if (!ssp->prog_mode_only)
    694 			bscv_start_event_daemon(ssp);
    695 
    696 #if defined(__i386) || defined(__amd64)
    697 		bscv_watchdog_enable = 1;
    698 		bscv_watchdog_available = 1;
    699 		watchdog_activated = 0;
    700 		bscv_watchdog_timeout_seconds = CLK_WATCHDOG_DEFAULT;
    701 
    702 		if (bscv_watchdog_enable && (boothowto & RB_DEBUG)) {
    703 			bscv_watchdog_available = 0;
    704 			cmn_err(CE_WARN, "bscv: kernel debugger "
    705 			    "detected: hardware watchdog disabled");
    706 		}
    707 
    708 		/*
    709 		 * Before we enable the watchdog - register the panic
    710 		 * callback so that we get called to stop the watchdog
    711 		 * in the case of a panic.
    712 		 */
    713 		ssp->callb_id = callb_add(bscv_panic_callback,
    714 		    (void *)ssp, CB_CL_PANIC, "");
    715 
    716 		if (bscv_watchdog_available) {
    717 			(void) bscv_set_watchdog_timer(ssp,
    718 			    CLK_WATCHDOG_DEFAULT);
    719 			bscv_enter(ssp);
    720 			bscv_setup_watchdog(ssp);  /* starts cyclic callback */
    721 			bscv_exit(ssp);
    722 		}
    723 #endif /* __i386 || __amd64 */
    724 		ddi_report_dev(dip);
    725 		return (DDI_SUCCESS);
    726 	default:
    727 		return (DDI_FAILURE);
    728 	}
    729 }
    730 
    731 /*
    732  * function	- bscv_detach
    733  * description	- routine that prepares a module to be unloaded. It undoes all
    734  *		  the work done by the bscv_attach)() routine. This is
    735  *		  facilitated by the use of the progress indicator
    736  * inputs	- device information structure, DDI_DETACH command
    737  * outputs	- DDI_SUCCESS or DDI_FAILURE
    738  */
    739 
    740 /*ARGSUSED*/
    741 static int
    742 bscv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
    743 {
    744 	return (DDI_FAILURE);
    745 }
    746 
    747 /*
    748  * function	- bscv_reset
    749  * description	- routine called when system is being stopped - used to disable
    750  *		  the watchdog.
    751  * inputs	- device information structure, DDI_RESET command
    752  * outputs	- DDI_SUCCESS or DDI_FAILURE
    753  */
    754 static int
    755 bscv_reset(dev_info_t *dip, ddi_reset_cmd_t cmd)
    756 {
    757 	bscv_soft_state_t *ssp;
    758 	int	instance;
    759 
    760 	switch (cmd) {
    761 	case DDI_RESET_FORCE:
    762 
    763 		instance = ddi_get_instance(dip);
    764 		ssp = ddi_get_soft_state(bscv_statep, instance);
    765 		if (ssp == NULL) {
    766 			return (DDI_FAILURE);
    767 		}
    768 		bscv_full_stop(ssp);
    769 		return (DDI_SUCCESS);
    770 
    771 	default:
    772 		return (DDI_FAILURE);
    773 	}
    774 }
    775 
    776 /*
    777  * quiesce(9E) entry point.
    778  *
    779  * This function is called when the system is single-threaded at high
    780  * PIL with preemption disabled. Therefore, this function must not be
    781  * blocked.
    782  *
    783  * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
    784  * DDI_FAILURE indicates an error condition and should almost never happen.
    785  */
    786 static int
    787 bscv_quiesce(dev_info_t *dip)
    788 {
    789 	bscv_soft_state_t *ssp;
    790 	int	instance;
    791 
    792 
    793 	instance = ddi_get_instance(dip);
    794 	ssp = ddi_get_soft_state(bscv_statep, instance);
    795 	if (ssp == NULL) {
    796 		return (DDI_FAILURE);
    797 	}
    798 	bscv_full_stop(ssp);
    799 	return (DDI_SUCCESS);
    800 }
    801 
    802 /*
    803  * cb_ops routines
    804  */
    805 
    806 /*
    807  * function	- bscv_open
    808  * description	- routine to provide association between user fd and device
    809  *		  minor number. This routine is necessarily simple since a
    810  *		  read/write interface is not provided. Additionally, the
    811  *		  driver does not enforce exclusive access (FEXCL) or
    812  *		  non-blocking during an open (FNDELAY). Deferred attach is
    813  *		  supported.
    814  * inputs	- device number, flag specifying open type, device type,
    815  *		  permissions
    816  * outputs	- success or failure of operation
    817  */
    818 
    819 /*ARGSUSED*/
    820 static int
    821 bscv_open(dev_t *devp, int flag, int otype, cred_t *cred)
    822 {
    823 	bscv_soft_state_t *ssp;
    824 	int instance;
    825 
    826 	instance = DEVICETOINSTANCE(*devp);
    827 	ssp = ddi_get_soft_state(bscv_statep, instance);
    828 	if (ssp == NULL) {
    829 		return (ENXIO);	/* not attached yet */
    830 	}
    831 	bscv_trace(ssp, 'O', "bscv_open", "instance 0x%x", instance);
    832 
    833 	if (otype != OTYP_CHR) {
    834 		return (EINVAL);
    835 	}
    836 
    837 	return (0);
    838 }
    839 
    840 /*
    841  * function	- bscv_close
    842  * description	- routine to perform the final close on the device. As per the
    843  *		  open routine, neither FEXCL or FNDELAY accesses are enforced
    844  *		  by the driver.
    845  * inputs	- device number,flag specifying open type, device type,
    846  *		  permissions
    847  * outputs	- success or failure of operation
    848  */
    849 
    850 /*ARGSUSED1*/
    851 static int
    852 bscv_close(dev_t dev, int flag, int otype, cred_t *cred)
    853 {
    854 	bscv_soft_state_t *ssp;
    855 	int instance;
    856 
    857 	instance = DEVICETOINSTANCE(dev);
    858 	ssp = ddi_get_soft_state(bscv_statep, instance);
    859 	if (ssp == NULL) {
    860 		return (ENXIO);
    861 	}
    862 	bscv_trace(ssp, 'O', "bscv_close", "instance 0x%x", instance);
    863 
    864 	return (0);
    865 }
    866 
    867 static int
    868 bscv_map_regs(bscv_soft_state_t *ssp)
    869 {
    870 	int i;
    871 	int retval;
    872 	int *props;
    873 	unsigned int nelements;
    874 
    875 	ASSERT(ssp);
    876 
    877 	ssp->nchannels = 0;
    878 
    879 	/*
    880 	 * Work out how many channels are available by looking at the number
    881 	 * of elements of the regs property array.
    882 	 */
    883 	retval = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, ssp->dip,
    884 	    DDI_PROP_DONTPASS, "reg", &props, &nelements);
    885 
    886 	/* We don't need props anymore.  Free memory if it was allocated */
    887 	if (retval == DDI_PROP_SUCCESS)
    888 		ddi_prop_free(props);
    889 
    890 	/* Check for sanity of nelements */
    891 	if (retval != DDI_PROP_SUCCESS) {
    892 		bscv_trace(ssp, 'A', "bscv_map_regs", "lookup reg returned"
    893 		    " 0x%x", retval);
    894 		goto cleanup_exit;
    895 	} else if (nelements % LOMBUS_REGSPEC_SIZE != 0) {
    896 		bscv_trace(ssp, 'A', "bscv_map_regs", "nelements %d not"
    897 		    " a multiple of %d", nelements, LOMBUS_REGSPEC_SIZE);
    898 		goto cleanup_exit;
    899 	} else if (nelements > BSCV_MAXCHANNELS * LOMBUS_REGSPEC_SIZE) {
    900 		bscv_trace(ssp, 'A', "bscv_map_regs", "nelements %d too large"
    901 		    ", probably a misconfiguration", nelements);
    902 		goto cleanup_exit;
    903 	} else if (nelements < BSCV_MINCHANNELS * LOMBUS_REGSPEC_SIZE) {
    904 		bscv_trace(ssp, 'A', "bscv_map_regs", "nelements %d too small"
    905 		    ", need to have at least a general and a wdog channel",
    906 		    nelements);
    907 		goto cleanup_exit;
    908 	}
    909 
    910 	ssp->nchannels = nelements / LOMBUS_REGSPEC_SIZE;
    911 
    912 	ssp->attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
    913 	ssp->attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
    914 	ssp->attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
    915 
    916 	for (i = 0; i < ssp->nchannels; i++) {
    917 		retval = ddi_regs_map_setup(ssp->dip, i,
    918 		    (caddr_t *)&ssp->channel[i].regs,
    919 		    0, 0, &ssp->attr, &ssp->channel[i].handle);
    920 		if (retval != DDI_SUCCESS) {
    921 			bscv_trace(ssp, 'A', "bscv_map_regs", "map failure"
    922 			    " 0x%x on space %d", retval, i);
    923 
    924 			/* Rewind all current mappings - avoiding failed one */
    925 			i--;
    926 			for (; i >= 0; i--) {
    927 				ddi_regs_map_free(&ssp->channel[i].handle);
    928 			}
    929 
    930 			goto cleanup_exit;
    931 		}
    932 	}
    933 
    934 	return (DDI_SUCCESS);
    935 
    936 cleanup_exit:
    937 	/*
    938 	 * It is important to set nchannels to 0 even if, say, only one of
    939 	 * the two required handles was mapped.  If we cannot achieve our
    940 	 * minimum config its not safe to do any IO; this keeps our failure
    941 	 * mode handling simpler.
    942 	 */
    943 	ssp->nchannels = 0;
    944 	return (DDI_FAILURE);
    945 }
    946 
    947 static void
    948 bscv_unmap_regs(bscv_soft_state_t *ssp)
    949 {
    950 	int i;
    951 
    952 	ASSERT(ssp);
    953 
    954 	for (i = 0; i < ssp->nchannels; i++) {
    955 		ddi_regs_map_free(&ssp->channel[i].handle);
    956 	}
    957 }
    958 
    959 /*
    960  * Map logical services onto physical XBus channels.
    961  */
    962 static void
    963 bscv_map_chan_logical_physical(bscv_soft_state_t *ssp)
    964 {
    965 	ASSERT(ssp);
    966 
    967 	/*
    968 	 * We can assert that there will always be at least two channels,
    969 	 * to allow watchdog pats to be segregated from all other traffic.
    970 	 */
    971 	chan_general = 0;
    972 	chan_wdogpat = 1;
    973 
    974 	/*
    975 	 * By default move all other services onto the generic channel unless
    976 	 * the hardware supports additional channels.
    977 	 */
    978 
    979 	chan_cpusig = chan_eeprom = chan_prog = chan_general;
    980 
    981 	if (ssp->nchannels > 2)
    982 		chan_cpusig = 2;
    983 	if (ssp->nchannels > 3)
    984 		chan_eeprom = 3;
    985 	if (ssp->nchannels > 4)
    986 		chan_prog = 4;
    987 }
    988 
    989 
    990 /*
    991  * function	- bscv_full_stop
    992  * description	- gracefully shut the lom down during panic or reboot.
    993  *		  Disables the watchdog, setup up serial event reporting
    994  *		  and stops the event daemon running.
    995  * inputs	- soft state pointer
    996  * outputs	- none
    997  */
    998 void
    999 bscv_full_stop(bscv_soft_state_t *ssp)
   1000 {
   1001 	uint8_t bits2set = 0;
   1002 	uint8_t bits2clear = 0;
   1003 
   1004 	bscv_trace(ssp, 'W', "bscv_full_stop",
   1005 	    "turning off watchdog");
   1006 
   1007 	if (!ddi_in_panic()) {
   1008 		/* Stop the event daemon if we are not panicking. */
   1009 		(void) bscv_pause_event_daemon(ssp);
   1010 	}
   1011 
   1012 	bscv_enter(ssp);
   1013 
   1014 #if defined(__i386) || defined(__amd64)
   1015 	if (ddi_in_panic()) {
   1016 		bscv_inform_bsc(ssp, BSC_INFORM_PANIC);
   1017 	} else {
   1018 		bscv_inform_bsc(ssp, BSC_INFORM_OFFLINE);
   1019 	}
   1020 #endif /* __i386 || __amd64 */
   1021 
   1022 	/* set serial event reporting */
   1023 	switch (ssp->serial_reporting) {
   1024 	case LOM_SER_EVENTS_ON:
   1025 	case LOM_SER_EVENTS_DEF:
   1026 		/* Make sure serial event reporting is on */
   1027 		bits2clear = EBUS_ALARM_NOEVENTS;
   1028 		break;
   1029 	case LOM_SER_EVENTS_OFF:
   1030 		/* Make sure serial event reporting is on */
   1031 		bits2set = EBUS_ALARM_NOEVENTS;
   1032 		break;
   1033 	default:
   1034 		break;
   1035 	}
   1036 	bscv_setclear8_volatile(ssp, chan_general,
   1037 	    EBUS_IDX_ALARM, bits2set, bits2clear);
   1038 
   1039 	bscv_exit(ssp);
   1040 }
   1041 
   1042 /*
   1043  * LOM I/O routines.
   1044  *
   1045  * locking
   1046  *
   1047  * Two sets of routines are provided:
   1048  *	normal - must be called after acquiring an appropriate lock.
   1049  *	locked - perform all the locking required and return any error
   1050  *		 code in the supplied 'res' argument. If there is no
   1051  *		 error 'res' is not changed.
   1052  * The locked routines are designed for use in ioctl commands where
   1053  * only a single operation needs to be performed and the overhead of
   1054  * locking and result checking adds significantly to code complexity.
   1055  *
   1056  * locking primitives
   1057  *
   1058  * bscv_enter() - acquires an I/O lock for the calling thread.
   1059  * bscv_exit() - releases an I/O lock acquired by bscv_enter().
   1060  * bscv_held() - used to assert ownership of an I/O lock.
   1061  *
   1062  * normal I/O routines
   1063  *
   1064  * Note bscv_{put|get}{16|32} routines are big-endian. This assumes that
   1065  * the firmware works that way too.
   1066  *
   1067  * bscv_put8(), bscv_put16, bscv_put32 - write values to the LOM
   1068  *		and handle any retries if necessary.
   1069  *		16 and 32 bit values are big-endian.
   1070  * bscv_get8(), bscv_get16, bscv_get32 - read values from the LOM
   1071  *		and handle any retries if necessary.
   1072  *		16 and 32 bit values are big-endian.
   1073  * bscv_setclear8() - set or clear the specified bits in the register
   1074  *		at the supplied address.
   1075  * bscv_setclear8_volatile() - set or clear the specified bits in the
   1076  *		register at the supplied address. If the lom reports
   1077  *		that the registers has changed since the last read
   1078  *		re-read and apply the set or clear to the new bits.
   1079  * bscv_get8_cached() - Return a cached register value (addr < 0x80).
   1080  *		Does not access the hardware. A read of the hardware
   1081  *		automatically updates this cache.
   1082  *
   1083  * locked I/O routines
   1084  *
   1085  * bscv_get8_locked(), bscv_rep_get8_locked().
   1086  *
   1087  * Call the indicated function from above, but wrapping it with
   1088  * bscv_enter()/bscv_exit().
   1089  *
   1090  *
   1091  * Fault management
   1092  *
   1093  * LOM communications fault are grouped into three categories:
   1094  * 1) Faulty - the LOM is not responding and no attempt to communicate
   1095  *		with it should be made.
   1096  * 2) Transient fault - something which might recover after a retry
   1097  *		but which doesn't affect our ability to perform other
   1098  *		commands.
   1099  * 3) Command error - an inappropriate command was executed. A retry
   1100  *		will not fix it but the command failed.
   1101  *
   1102  * The current implementation of the bscv driver is not very good at
   1103  * noticing command errors due to the structure of the original code
   1104  * that it is based on. It is possible to extend the driver to do this
   1105  * and would probably involve having a concept of a "session error"
   1106  * which is less severe than a fault but means that a sequence of
   1107  * commands had some fault which cannot be recovered.
   1108  *
   1109  *
   1110  * faults
   1111  *
   1112  * bscv_faulty() - returns B_TRUE if the LOM (communications) have been
   1113  *		declared faulty.
   1114  * bscv_clear_fault() - marks the LOM as not faulty.
   1115  * bscv_set_fault() - marks the LOM as being faulty.
   1116  *
   1117  * bscv_clear_fault and bscv_set_fault should generally not be called
   1118  * directly.
   1119  *
   1120  * command errors/transient faults
   1121  *
   1122  * bscv_retcode() - returns the actual error code of the last operation.
   1123  * bscv_should_retry() - determines if last operation may suceed if
   1124  *		retried.
   1125  * bscv_locked_result() - Set the result of a locked register access.
   1126  *
   1127  * low level I/O primitives
   1128  *
   1129  * These are generally not called directly. These perform a single
   1130  * access to the LOM device. They do not handle retries.
   1131  *
   1132  * bscv_put8_once()
   1133  * bscv_get8_once()
   1134  * bscv_probe() - perform a probe (NOP) operation to check out lom comms.
   1135  * bscv_resync_comms() - resynchronise communications after a transient fault.
   1136  */
   1137 
   1138 static void
   1139