1221167Sgnn/*-
2221167Sgnn * Copyright(c) 2002-2011 Exar Corp.
3221167Sgnn * All rights reserved.
4221167Sgnn *
5221167Sgnn * Redistribution and use in source and binary forms, with or without
6221167Sgnn * modification are permitted provided the following conditions are met:
7221167Sgnn *
8221167Sgnn *    1. Redistributions of source code must retain the above copyright notice,
9221167Sgnn *       this list of conditions and the following disclaimer.
10221167Sgnn *
11221167Sgnn *    2. Redistributions in binary form must reproduce the above copyright
12221167Sgnn *       notice, this list of conditions and the following disclaimer in the
13221167Sgnn *       documentation and/or other materials provided with the distribution.
14221167Sgnn *
15221167Sgnn *    3. Neither the name of the Exar Corporation nor the names of its
16221167Sgnn *       contributors may be used to endorse or promote products derived from
17221167Sgnn *       this software without specific prior written permission.
18221167Sgnn *
19221167Sgnn * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20221167Sgnn * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21221167Sgnn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22221167Sgnn * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23221167Sgnn * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24221167Sgnn * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25221167Sgnn * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26221167Sgnn * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27221167Sgnn * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28221167Sgnn * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29221167Sgnn * POSSIBILITY OF SUCH DAMAGE.
30221167Sgnn */
31221167Sgnn/*$FreeBSD$*/
32221167Sgnn
33221167Sgnn#include <dev/vxge/vxgehal/vxgehal.h>
34221167Sgnn
35221167Sgnnstatic __hal_driver_t g_driver;
36221167Sgnn__hal_driver_t *g_vxge_hal_driver;
37221167Sgnn
38221167Sgnn#if defined(VXGE_OS_MEMORY_CHECK)
39221167Sgnnvxge_os_malloc_t g_malloc_arr[VXGE_OS_MALLOC_CNT_MAX];
40221167Sgnnu32 g_malloc_cnt;
41221167Sgnn
42221167Sgnn#endif
43221167Sgnn
44221167Sgnn/*
45221167Sgnn * Runtime tracing support
46221167Sgnn */
47221167Sgnnu32 g_debug_level;
48221167Sgnn
49221167Sgnn/*
50221167Sgnn * vxge_hal_driver_initialize - Initialize HAL.
51221167Sgnn * @config: HAL configuration, see vxge_hal_driver_config_t {}.
52221167Sgnn * @uld_callbacks: Upper-layer driver callbacks, e.g. link-up.
53221167Sgnn *
54221167Sgnn * HAL initialization entry point. Not to confuse with device initialization
55221167Sgnn * (note that HAL "contains" zero or more X3100 devices).
56221167Sgnn *
57221167Sgnn * Returns: VXGE_HAL_OK - success;
58221167Sgnn * VXGE_HAL_ERR_BAD_DRIVER_CONFIG - Driver configuration params invalid.
59221167Sgnn *
60221167Sgnn * See also: vxge_hal_device_initialize(), vxge_hal_status_e {},
61221167Sgnn * vxge_hal_uld_cbs_t {}.
62221167Sgnn */
63221167Sgnnvxge_hal_status_e
64221167Sgnnvxge_hal_driver_initialize(
65221167Sgnn    vxge_hal_driver_config_t *config,
66221167Sgnn    vxge_hal_uld_cbs_t *uld_callbacks)
67221167Sgnn{
68221167Sgnn	vxge_hal_status_e status;
69221167Sgnn	g_vxge_hal_driver = &g_driver;
70221167Sgnn
71221167Sgnn	if ((status = vxge_hal_driver_config_check(config)) != VXGE_HAL_OK)
72221167Sgnn		return (status);
73221167Sgnn
74221167Sgnn	vxge_os_memzero(g_vxge_hal_driver, sizeof(__hal_driver_t));
75221167Sgnn
76221167Sgnn	/* apply config */
77221167Sgnn	vxge_os_memcpy(&g_vxge_hal_driver->config, config,
78221167Sgnn	    sizeof(vxge_hal_driver_config_t));
79221167Sgnn
80221167Sgnn	/* apply ULD callbacks */
81221167Sgnn	vxge_os_memcpy(&g_vxge_hal_driver->uld_callbacks, uld_callbacks,
82221167Sgnn	    sizeof(vxge_hal_uld_cbs_t));
83221167Sgnn
84221167Sgnn	vxge_hal_driver_debug_set(config->level);
85221167Sgnn
86221167Sgnn	g_vxge_hal_driver->is_initialized = 1;
87221167Sgnn
88221167Sgnn	return (VXGE_HAL_OK);
89221167Sgnn}
90221167Sgnn
91221167Sgnn/*
92221167Sgnn * vxge_hal_driver_terminate - Terminate HAL.
93221167Sgnn *
94221167Sgnn * HAL termination entry point.
95221167Sgnn *
96221167Sgnn * See also: vxge_hal_device_terminate().
97221167Sgnn */
98221167Sgnnvoid
99221167Sgnnvxge_hal_driver_terminate(void)
100221167Sgnn{
101221167Sgnn	g_vxge_hal_driver->is_initialized = 0;
102221167Sgnn
103221167Sgnn	g_vxge_hal_driver = NULL;
104221167Sgnn
105221167Sgnn#if defined(VXGE_OS_MEMORY_CHECK)
106221167Sgnn	if (TRUE) {
107221167Sgnn		u32 i, leaks = 0;
108221167Sgnn
109221167Sgnn		vxge_os_printf("OSPAL: max g_malloc_cnt %d\n", g_malloc_cnt);
110221167Sgnn		for (i = 0; i < g_malloc_cnt; i++) {
111221167Sgnn			if (g_malloc_arr[i].ptr != NULL) {
112221167Sgnn				vxge_os_printf("OSPAL: memory leak detected at "
113221167Sgnn				    "%s:%lu:"VXGE_OS_LLXFMT":%lu\n",
114221167Sgnn				    g_malloc_arr[i].file,
115221167Sgnn				    g_malloc_arr[i].line,
116221167Sgnn				    (u64) (ptr_t) g_malloc_arr[i].ptr,
117221167Sgnn				    g_malloc_arr[i].size);
118221167Sgnn				leaks++;
119221167Sgnn			}
120221167Sgnn		}
121221167Sgnn		if (leaks) {
122221167Sgnn			vxge_os_printf("OSPAL: %d memory leaks detected\n",
123221167Sgnn			    leaks);
124221167Sgnn		} else {
125221167Sgnn			vxge_os_println("OSPAL: no memory leaks detected\n");
126221167Sgnn		}
127221167Sgnn	}
128221167Sgnn#endif
129221167Sgnn}
130221167Sgnn
131221167Sgnn/*
132221167Sgnn * vxge_hal_driver_debug_set - Set the debug module, level and timestamp
133221167Sgnn * @level: Debug level as defined in enum vxge_debug_level_e
134221167Sgnn *
135221167Sgnn * This routine is used to dynamically change the debug output
136221167Sgnn */
137221167Sgnnvoid
138221167Sgnnvxge_hal_driver_debug_set(
139221167Sgnn    vxge_debug_level_e level)
140221167Sgnn{
141221167Sgnn	g_vxge_hal_driver->debug_level = level;
142221167Sgnn	g_debug_level = 0;
143221167Sgnn
144221167Sgnn	switch (level) {
145221167Sgnn		/* FALLTHRU */
146221167Sgnn
147221167Sgnn	case VXGE_TRACE:
148221167Sgnn		g_debug_level |= VXGE_TRACE;
149221167Sgnn		/* FALLTHRU */
150221167Sgnn
151221167Sgnn	case VXGE_INFO:
152221167Sgnn		g_debug_level |= VXGE_INFO;
153221167Sgnn		/* FALLTHRU */
154221167Sgnn
155221167Sgnn	case VXGE_ERR:
156221167Sgnn		g_debug_level |= VXGE_ERR;
157221167Sgnn		/* FALLTHRU */
158221167Sgnn
159221167Sgnn	default:
160221167Sgnn		break;
161221167Sgnn	}
162221167Sgnn}
163221167Sgnn
164221167Sgnn/*
165221167Sgnn * vxge_hal_driver_debug_get - Get the debug level
166221167Sgnn *
167221167Sgnn * This routine returns the current debug level set
168221167Sgnn */
169221167Sgnnu32
170221167Sgnnvxge_hal_driver_debug_get(void)
171221167Sgnn{
172221167Sgnn	return (g_vxge_hal_driver->debug_level);
173221167Sgnn}
174