xref: /freebsd/sys/dev/smartpqi/smartpqi_misc.c (revision eac7052fdebb90caf2f653e06187bdbca837b9c7)
1 /*-
2  * Copyright (c) 2018 Microsemi Corporation.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /* $FreeBSD$ */
28 
29 #include "smartpqi_includes.h"
30 
31 /*
32  * Populate hostwell time variables in bcd format from FreeBSD format
33  */
34 void os_get_time(struct bmic_host_wellness_time *host_wellness_time)
35 {
36 	struct timespec ts;
37 	struct clocktime ct;
38 
39 	getnanotime(&ts);
40 	clock_ts_to_ct(&ts, &ct);
41 
42 	/* Fill the time In BCD Format */
43 	host_wellness_time->hour= (uint8_t)bin2bcd(ct.hour);
44 	host_wellness_time->min = (uint8_t)bin2bcd(ct.min);
45 	host_wellness_time->sec= (uint8_t)bin2bcd(ct.sec);
46 	host_wellness_time->reserved = 0;
47 	host_wellness_time->month = (uint8_t)bin2bcd(ct.mon);
48 	host_wellness_time->day = (uint8_t)bin2bcd(ct.day);
49 	host_wellness_time->century = (uint8_t)bin2bcd(ct.year / 100);
50 	host_wellness_time->year = (uint8_t)bin2bcd(ct.year % 100);
51 
52 }
53 
54 /*
55  * Update host time to f/w every 24 hours in a periodic timer.
56  */
57 
58 void os_wellness_periodic(void *data)
59 {
60 	struct pqisrc_softstate *softs = (struct pqisrc_softstate *)data;
61 	int ret = 0;
62 
63 	/* update time to FW */
64 	if (!pqisrc_ctrl_offline(softs)){
65 		if( (ret = pqisrc_write_current_time_to_host_wellness(softs)) != 0 )
66 			DBG_ERR("Failed to update time to FW in periodic ret = %d\n", ret);
67 	}
68 
69 	/* reschedule ourselves */
70 	callout_schedule(&softs->os_specific.wellness_periodic,
71 	    OS_HOST_WELLNESS_TIMEOUT * hz);
72 }
73 
74 /*
75  * Routine used to stop the heart-beat timer
76  */
77 void os_stop_heartbeat_timer(pqisrc_softstate_t *softs)
78 {
79 	DBG_FUNC("IN\n");
80 
81 	/* Kill the heart beat event */
82 	callout_stop(&softs->os_specific.heartbeat_timeout_id);
83 
84 	DBG_FUNC("OUT\n");
85 }
86 
87 /*
88  * Routine used to start the heart-beat timer
89  */
90 void os_start_heartbeat_timer(void *data)
91 {
92 	struct pqisrc_softstate *softs = (struct pqisrc_softstate *)data;
93 	DBG_FUNC("IN\n");
94 
95 	pqisrc_heartbeat_timer_handler(softs);
96 	if (!pqisrc_ctrl_offline(softs)) {
97 		callout_reset(&softs->os_specific.heartbeat_timeout_id,
98 			      OS_FW_HEARTBEAT_TIMER_INTERVAL * hz,
99 			      os_start_heartbeat_timer, softs);
100 	}
101 
102        DBG_FUNC("OUT\n");
103 }
104 
105 /*
106  * Mutex initialization function
107  */
108 int os_init_spinlock(struct pqisrc_softstate *softs, struct mtx *lock,
109 			char *lockname)
110 {
111     mtx_init(lock, lockname, NULL, MTX_SPIN);
112     return 0;
113 
114 }
115 
116 /*
117  * Mutex uninitialization function
118  */
119 void os_uninit_spinlock(struct mtx *lock)
120 {
121     mtx_destroy(lock);
122     return;
123 
124 }
125 
126 /*
127  * Semaphore initialization function
128  */
129 int os_create_semaphore(const char *name, int value, struct sema *sema)
130 {
131     sema_init(sema, value, name);
132     return PQI_STATUS_SUCCESS;
133 
134 }
135 
136 /*
137  * Semaphore uninitialization function
138  */
139 int os_destroy_semaphore(struct sema *sema)
140 {
141     sema_destroy(sema);
142     return PQI_STATUS_SUCCESS;
143 
144 }
145 
146 /*
147  * Semaphore grab function
148  */
149 void inline os_sema_lock(struct sema *sema)
150 {
151 	sema_post(sema);
152 }
153 
154 /*
155  * Semaphore release function
156  */
157 void inline os_sema_unlock(struct sema *sema)
158 {
159 	sema_wait(sema);
160 }
161 
162 /*
163  * string copy wrapper function
164  */
165 int os_strlcpy(char *dst, char *src, int size)
166 {
167 	return strlcpy(dst, src, size);
168 }
169