xref: /freebsd/sys/dev/smartpqi/smartpqi_misc.c (revision 19261079)
1 /*-
2  * Copyright 2016-2021 Microchip Technology, Inc. and/or its subsidiaries.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 /* $FreeBSD$ */
27 
28 #include "smartpqi_includes.h"
29 
30 /*
31  * Populate hostwellness time variables in bcd format from FreeBSD format
32  */
33 void
34 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
59 os_wellness_periodic(void *data)
60 {
61 	struct pqisrc_softstate *softs = (struct pqisrc_softstate *)data;
62 	int ret = 0;
63 
64 	/* update time to FW */
65 	if (!pqisrc_ctrl_offline(softs)){
66 		if( (ret = pqisrc_write_current_time_to_host_wellness(softs)) != 0 )
67 			DBG_ERR("Failed to update time to FW in periodic ret = %d\n", ret);
68 	}
69 
70 	/* reschedule ourselves */
71 	callout_reset(&softs->os_specific.wellness_periodic,
72 			PQI_HOST_WELLNESS_TIMEOUT_SEC * hz, os_wellness_periodic, softs);
73 }
74 
75 /*
76  * Routine used to stop the heart-beat timer
77  */
78 void
79 os_stop_heartbeat_timer(pqisrc_softstate_t *softs)
80 {
81 	DBG_FUNC("IN\n");
82 
83 	/* Kill the heart beat event */
84 	callout_stop(&softs->os_specific.heartbeat_timeout_id);
85 
86 	DBG_FUNC("OUT\n");
87 }
88 
89 /*
90  * Routine used to start the heart-beat timer
91  */
92 void
93 os_start_heartbeat_timer(void *data)
94 {
95 	struct pqisrc_softstate *softs = (struct pqisrc_softstate *)data;
96 	DBG_FUNC("IN\n");
97 
98 	pqisrc_heartbeat_timer_handler(softs);
99 	if (!pqisrc_ctrl_offline(softs)) {
100 		callout_reset(&softs->os_specific.heartbeat_timeout_id,
101 				PQI_HEARTBEAT_TIMEOUT_SEC * hz,
102 				os_start_heartbeat_timer, softs);
103 	}
104 
105        DBG_FUNC("OUT\n");
106 }
107 
108 /*
109  * Mutex initialization function
110  */
111 int
112 os_init_spinlock(struct pqisrc_softstate *softs, struct mtx *lock,
113 			char *lockname)
114 {
115 	mtx_init(lock, lockname, NULL, MTX_SPIN);
116 	return 0;
117 }
118 
119 /*
120  * Mutex uninitialization function
121  */
122 void
123 os_uninit_spinlock(struct mtx *lock)
124 {
125 	mtx_destroy(lock);
126 	return;
127 }
128 
129 /*
130  * Semaphore initialization function
131  */
132 int
133 os_create_semaphore(const char *name, int value, struct sema *sema)
134 {
135 	sema_init(sema, value, name);
136 	return PQI_STATUS_SUCCESS;
137 }
138 
139 /*
140  * Semaphore uninitialization function
141  */
142 int
143 os_destroy_semaphore(struct sema *sema)
144 {
145 	sema_destroy(sema);
146 	return PQI_STATUS_SUCCESS;
147 }
148 
149 /*
150  * Semaphore grab function
151  */
152 void inline
153 os_sema_lock(struct sema *sema)
154 {
155 	sema_post(sema);
156 }
157 
158 /*
159  * Semaphore release function
160  */
161 void inline
162 os_sema_unlock(struct sema *sema)
163 {
164 	sema_wait(sema);
165 }
166 
167 /*
168  * string copy wrapper function
169  */
170 int
171 os_strlcpy(char *dst, char *src, int size)
172 {
173 	return strlcpy(dst, src, size);
174 }
175 
176 int
177 bsd_status_to_pqi_status(int bsd_status)
178 {
179 	if (bsd_status == BSD_SUCCESS)
180 		return PQI_STATUS_SUCCESS;
181 	else
182 		return PQI_STATUS_FAILURE;
183 }
184