1/*-
2 * Copyright(c) 2002-2011 Exar Corp.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification are permitted provided the following conditions are met:
7 *
8 *    1. Redistributions of source code must retain the above copyright notice,
9 *       this list of conditions and the following disclaimer.
10 *
11 *    2. Redistributions in binary form must reproduce the above copyright
12 *       notice, this list of conditions and the following disclaimer in the
13 *       documentation and/or other materials provided with the distribution.
14 *
15 *    3. Neither the name of the Exar Corporation nor the names of its
16 *       contributors may be used to endorse or promote products derived from
17 *       this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31/*$FreeBSD$*/
32
33#include <dev/vxge/vxgehal/vxgehal.h>
34
35static __hal_driver_t g_driver;
36__hal_driver_t *g_vxge_hal_driver;
37
38#if defined(VXGE_OS_MEMORY_CHECK)
39vxge_os_malloc_t g_malloc_arr[VXGE_OS_MALLOC_CNT_MAX];
40u32 g_malloc_cnt;
41
42#endif
43
44/*
45 * Runtime tracing support
46 */
47u32 g_debug_level;
48
49/*
50 * vxge_hal_driver_initialize - Initialize HAL.
51 * @config: HAL configuration, see vxge_hal_driver_config_t {}.
52 * @uld_callbacks: Upper-layer driver callbacks, e.g. link-up.
53 *
54 * HAL initialization entry point. Not to confuse with device initialization
55 * (note that HAL "contains" zero or more X3100 devices).
56 *
57 * Returns: VXGE_HAL_OK - success;
58 * VXGE_HAL_ERR_BAD_DRIVER_CONFIG - Driver configuration params invalid.
59 *
60 * See also: vxge_hal_device_initialize(), vxge_hal_status_e {},
61 * vxge_hal_uld_cbs_t {}.
62 */
63vxge_hal_status_e
64vxge_hal_driver_initialize(
65    vxge_hal_driver_config_t *config,
66    vxge_hal_uld_cbs_t *uld_callbacks)
67{
68	vxge_hal_status_e status;
69	g_vxge_hal_driver = &g_driver;
70
71	if ((status = vxge_hal_driver_config_check(config)) != VXGE_HAL_OK)
72		return (status);
73
74	vxge_os_memzero(g_vxge_hal_driver, sizeof(__hal_driver_t));
75
76	/* apply config */
77	vxge_os_memcpy(&g_vxge_hal_driver->config, config,
78	    sizeof(vxge_hal_driver_config_t));
79
80	/* apply ULD callbacks */
81	vxge_os_memcpy(&g_vxge_hal_driver->uld_callbacks, uld_callbacks,
82	    sizeof(vxge_hal_uld_cbs_t));
83
84	vxge_hal_driver_debug_set(config->level);
85
86	g_vxge_hal_driver->is_initialized = 1;
87
88	return (VXGE_HAL_OK);
89}
90
91/*
92 * vxge_hal_driver_terminate - Terminate HAL.
93 *
94 * HAL termination entry point.
95 *
96 * See also: vxge_hal_device_terminate().
97 */
98void
99vxge_hal_driver_terminate(void)
100{
101	g_vxge_hal_driver->is_initialized = 0;
102
103	g_vxge_hal_driver = NULL;
104
105#if defined(VXGE_OS_MEMORY_CHECK)
106	if (TRUE) {
107		u32 i, leaks = 0;
108
109		vxge_os_printf("OSPAL: max g_malloc_cnt %d\n", g_malloc_cnt);
110		for (i = 0; i < g_malloc_cnt; i++) {
111			if (g_malloc_arr[i].ptr != NULL) {
112				vxge_os_printf("OSPAL: memory leak detected at "
113				    "%s:%lu:"VXGE_OS_LLXFMT":%lu\n",
114				    g_malloc_arr[i].file,
115				    g_malloc_arr[i].line,
116				    (u64) (ptr_t) g_malloc_arr[i].ptr,
117				    g_malloc_arr[i].size);
118				leaks++;
119			}
120		}
121		if (leaks) {
122			vxge_os_printf("OSPAL: %d memory leaks detected\n",
123			    leaks);
124		} else {
125			vxge_os_println("OSPAL: no memory leaks detected\n");
126		}
127	}
128#endif
129}
130
131/*
132 * vxge_hal_driver_debug_set - Set the debug module, level and timestamp
133 * @level: Debug level as defined in enum vxge_debug_level_e
134 *
135 * This routine is used to dynamically change the debug output
136 */
137void
138vxge_hal_driver_debug_set(
139    vxge_debug_level_e level)
140{
141	g_vxge_hal_driver->debug_level = level;
142	g_debug_level = 0;
143
144	switch (level) {
145		/* FALLTHRU */
146
147	case VXGE_TRACE:
148		g_debug_level |= VXGE_TRACE;
149		/* FALLTHRU */
150
151	case VXGE_INFO:
152		g_debug_level |= VXGE_INFO;
153		/* FALLTHRU */
154
155	case VXGE_ERR:
156		g_debug_level |= VXGE_ERR;
157		/* FALLTHRU */
158
159	default:
160		break;
161	}
162}
163
164/*
165 * vxge_hal_driver_debug_get - Get the debug level
166 *
167 * This routine returns the current debug level set
168 */
169u32
170vxge_hal_driver_debug_get(void)
171{
172	return (g_vxge_hal_driver->debug_level);
173}
174