xref: /dragonfly/sys/dev/netif/ath/ath/if_ath_alq.c (revision 277350a0)
1 /*-
2  * Copyright (c) 2012 Adrian Chadd
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  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  *
29  * $FreeBSD$
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 #if defined(__DragonFly__)
43 /* empty */
44 #else
45 #include <sys/pcpu.h>
46 #endif
47 #include <sys/lock.h>
48 #include <sys/mutex.h>
49 #if defined(__DragonFly__)
50 /* empty */
51 #else
52 #include <sys/alq.h>
53 #endif
54 #include <sys/endian.h>
55 #include <sys/time.h>
56 
57 #include <dev/netif/ath/ath/if_ath_alq.h>
58 
59 #ifdef	ATH_DEBUG_ALQ
60 static struct ale *
61 if_ath_alq_get(struct if_ath_alq *alq, int len)
62 {
63 	struct ale *ale;
64 
65 	if (alq->sc_alq_isactive == 0)
66 		return (NULL);
67 
68 	ale = alq_getn(alq->sc_alq_alq, len, ALQ_NOWAIT);
69 	if (! ale)
70 		alq->sc_alq_numlost++;
71 	return (ale);
72 }
73 
74 void
75 if_ath_alq_init(struct if_ath_alq *alq, const char *devname)
76 {
77 
78 	bzero(alq, sizeof(*alq));
79 
80 	strncpy(alq->sc_alq_devname, devname, ATH_ALQ_DEVNAME_LEN);
81 	kprintf("%s (%s): attached\n", __func__, alq->sc_alq_devname);
82 	ksnprintf(alq->sc_alq_filename, ATH_ALQ_FILENAME_LEN,
83 	    "/tmp/ath_%s_alq.log", alq->sc_alq_devname);
84 
85 	/* XXX too conservative, right? */
86 	alq->sc_alq_qsize = (64*1024);
87 }
88 
89 void
90 if_ath_alq_setcfg(struct if_ath_alq *alq, uint32_t macVer,
91     uint32_t macRev, uint32_t phyRev, uint32_t halMagic)
92 {
93 
94 	/* Store these in network order */
95 	alq->sc_alq_cfg.sc_mac_version = htobe32(macVer);
96 	alq->sc_alq_cfg.sc_mac_revision = htobe32(macRev);
97 	alq->sc_alq_cfg.sc_phy_rev = htobe32(phyRev);
98 	alq->sc_alq_cfg.sc_hal_magic = htobe32(halMagic);
99 }
100 
101 void
102 if_ath_alq_tidyup(struct if_ath_alq *alq)
103 {
104 
105 	if_ath_alq_stop(alq);
106 	kprintf("%s (%s): detached\n", __func__, alq->sc_alq_devname);
107 	bzero(alq, sizeof(*alq));
108 }
109 
110 int
111 if_ath_alq_start(struct if_ath_alq *alq)
112 {
113 	int error;
114 
115 	if (alq->sc_alq_isactive)
116 		return (0);
117 
118 	/*
119 	 * Create a variable-length ALQ.
120 	 */
121 	error = alq_open(&alq->sc_alq_alq, alq->sc_alq_filename,
122 	    curthread->td_ucred, ALQ_DEFAULT_CMODE,
123 	    alq->sc_alq_qsize, 0);
124 
125 	if (error != 0) {
126 		kprintf("%s (%s): failed, err=%d\n", __func__,
127 		    alq->sc_alq_devname, error);
128 	} else {
129 		kprintf("%s (%s): opened\n", __func__, alq->sc_alq_devname);
130 		alq->sc_alq_isactive = 1;
131 		if_ath_alq_post(alq, ATH_ALQ_INIT_STATE,
132 		    sizeof (struct if_ath_alq_init_state),
133 		    (char *) &alq->sc_alq_cfg);
134 	}
135 	return (error);
136 }
137 
138 int
139 if_ath_alq_stop(struct if_ath_alq *alq)
140 {
141 
142 	if (alq->sc_alq_isactive == 0)
143 		return (0);
144 
145 	kprintf("%s (%s): closed\n", __func__, alq->sc_alq_devname);
146 
147 	alq->sc_alq_isactive = 0;
148 	alq_close(alq->sc_alq_alq);
149 	alq->sc_alq_alq = NULL;
150 
151 	return (0);
152 }
153 
154 /*
155  * Post a debug message to the ALQ.
156  *
157  * "len" is the size of the buf payload in bytes.
158  */
159 void
160 if_ath_alq_post(struct if_ath_alq *alq, uint16_t op, uint16_t len,
161     const char *buf)
162 {
163 	struct if_ath_alq_hdr *ap;
164 	struct ale *ale;
165 	struct timeval tv;
166 
167 	if (! if_ath_alq_checkdebug(alq, op))
168 		return;
169 
170 	microtime(&tv);
171 
172 	/*
173 	 * Enforce some semblence of sanity on 'len'.
174 	 * Although strictly speaking, any length is possible -
175 	 * just be conservative so things don't get out of hand.
176 	 */
177 	if (len > ATH_ALQ_PAYLOAD_LEN)
178 		len = ATH_ALQ_PAYLOAD_LEN;
179 
180 	ale = if_ath_alq_get(alq, len + sizeof(struct if_ath_alq_hdr));
181 
182 	if (ale == NULL)
183 		return;
184 
185 	ap = (struct if_ath_alq_hdr *) ale->ae_data;
186 	ap->threadid = htobe64((uint64_t) curthread->td_tid);
187 	ap->tstamp_sec = htobe32((uint32_t) tv.tv_sec);
188 	ap->tstamp_usec = htobe32((uint32_t) tv.tv_usec);
189 	ap->op = htobe16(op);
190 	ap->len = htobe16(len);
191 
192 	/*
193 	 * Copy the payload _after_ the header field.
194 	 */
195 	if (buf != NULL) {
196 		memcpy(((char *) ap) + sizeof(struct if_ath_alq_hdr),
197 		    buf,
198 		    len);
199 	}
200 
201 	alq_post(alq->sc_alq_alq, ale);
202 }
203 #endif	/* ATH_DEBUG */
204