xref: /freebsd/sys/dev/ath/if_ath_alq.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012 Adrian Chadd
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer,
12  *    without modification.
13  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15  *    redistribution must be conditioned upon including a substantially
16  *    similar Disclaimer requirement for further binary redistribution.
17  *
18  * NO WARRANTY
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24  * OR 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
27  * IN 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
29  * THE POSSIBILITY OF SUCH DAMAGES.
30  */
31 #include "opt_ah.h"
32 #include "opt_ath.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/sysctl.h>
39 #include <sys/bus.h>
40 #include <sys/malloc.h>
41 #include <sys/proc.h>
42 #include <sys/pcpu.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/alq.h>
46 #include <sys/endian.h>
47 #include <sys/time.h>
48 
49 #include <dev/ath/if_ath_alq.h>
50 
51 #ifdef	ATH_DEBUG_ALQ
52 static struct ale *
53 if_ath_alq_get(struct if_ath_alq *alq, int len)
54 {
55 	struct ale *ale;
56 
57 	if (alq->sc_alq_isactive == 0)
58 		return (NULL);
59 
60 	ale = alq_getn(alq->sc_alq_alq, len, ALQ_NOWAIT);
61 	if (! ale)
62 		alq->sc_alq_numlost++;
63 	return (ale);
64 }
65 
66 void
67 if_ath_alq_init(struct if_ath_alq *alq, const char *devname)
68 {
69 
70 	bzero(alq, sizeof(*alq));
71 
72 	strncpy(alq->sc_alq_devname, devname, ATH_ALQ_DEVNAME_LEN);
73 	printf("%s (%s): attached\n", __func__, alq->sc_alq_devname);
74 	snprintf(alq->sc_alq_filename, ATH_ALQ_FILENAME_LEN,
75 	    "/tmp/ath_%s_alq.log", alq->sc_alq_devname);
76 
77 	/* XXX too conservative, right? */
78 	alq->sc_alq_qsize = (64*1024);
79 }
80 
81 void
82 if_ath_alq_setcfg(struct if_ath_alq *alq, uint32_t macVer,
83     uint32_t macRev, uint32_t phyRev, uint32_t halMagic)
84 {
85 
86 	/* Store these in network order */
87 	alq->sc_alq_cfg.sc_mac_version = htobe32(macVer);
88 	alq->sc_alq_cfg.sc_mac_revision = htobe32(macRev);
89 	alq->sc_alq_cfg.sc_phy_rev = htobe32(phyRev);
90 	alq->sc_alq_cfg.sc_hal_magic = htobe32(halMagic);
91 }
92 
93 void
94 if_ath_alq_tidyup(struct if_ath_alq *alq)
95 {
96 
97 	if_ath_alq_stop(alq);
98 	printf("%s (%s): detached\n", __func__, alq->sc_alq_devname);
99 	bzero(alq, sizeof(*alq));
100 }
101 
102 int
103 if_ath_alq_start(struct if_ath_alq *alq)
104 {
105 	int error;
106 
107 	if (alq->sc_alq_isactive)
108 		return (0);
109 
110 	/*
111 	 * Create a variable-length ALQ.
112 	 */
113 	error = alq_open(&alq->sc_alq_alq, alq->sc_alq_filename,
114 	    curthread->td_ucred, ALQ_DEFAULT_CMODE,
115 	    alq->sc_alq_qsize, 0);
116 
117 	if (error != 0) {
118 		printf("%s (%s): failed, err=%d\n", __func__,
119 		    alq->sc_alq_devname, error);
120 	} else {
121 		printf("%s (%s): opened\n", __func__, alq->sc_alq_devname);
122 		alq->sc_alq_isactive = 1;
123 		if_ath_alq_post(alq, ATH_ALQ_INIT_STATE,
124 		    sizeof (struct if_ath_alq_init_state),
125 		    (char *) &alq->sc_alq_cfg);
126 	}
127 	return (error);
128 }
129 
130 int
131 if_ath_alq_stop(struct if_ath_alq *alq)
132 {
133 
134 	if (alq->sc_alq_isactive == 0)
135 		return (0);
136 
137 	printf("%s (%s): closed\n", __func__, alq->sc_alq_devname);
138 
139 	alq->sc_alq_isactive = 0;
140 	alq_close(alq->sc_alq_alq);
141 	alq->sc_alq_alq = NULL;
142 
143 	return (0);
144 }
145 
146 /*
147  * Post a debug message to the ALQ.
148  *
149  * "len" is the size of the buf payload in bytes.
150  */
151 void
152 if_ath_alq_post(struct if_ath_alq *alq, uint16_t op, uint16_t len,
153     const char *buf)
154 {
155 	struct if_ath_alq_hdr *ap;
156 	struct ale *ale;
157 	struct timeval tv;
158 
159 	if (! if_ath_alq_checkdebug(alq, op))
160 		return;
161 
162 	microtime(&tv);
163 
164 	/*
165 	 * Enforce some semblence of sanity on 'len'.
166 	 * Although strictly speaking, any length is possible -
167 	 * just be conservative so things don't get out of hand.
168 	 */
169 	if (len > ATH_ALQ_PAYLOAD_LEN)
170 		len = ATH_ALQ_PAYLOAD_LEN;
171 
172 	ale = if_ath_alq_get(alq, len + sizeof(struct if_ath_alq_hdr));
173 
174 	if (ale == NULL)
175 		return;
176 
177 	ap = (struct if_ath_alq_hdr *) ale->ae_data;
178 	ap->threadid = htobe64((uint64_t) curthread->td_tid);
179 	ap->tstamp_sec = htobe32((uint32_t) tv.tv_sec);
180 	ap->tstamp_usec = htobe32((uint32_t) tv.tv_usec);
181 	ap->op = htobe16(op);
182 	ap->len = htobe16(len);
183 
184 	/*
185 	 * Copy the payload _after_ the header field.
186 	 */
187 	if (buf != NULL) {
188 		memcpy(((char *) ap) + sizeof(struct if_ath_alq_hdr),
189 		    buf,
190 		    len);
191 	}
192 
193 	alq_post(alq->sc_alq_alq, ale);
194 }
195 #endif	/* ATH_DEBUG */
196