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, MERCHANTABILITY
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 #if defined(__DragonFly__)
49 /* empty */
50 #else
51 #include <sys/alq.h>
52 #endif
53 #include <sys/endian.h>
54 #include <sys/time.h>
55
56 #include <dev/netif/ath/ath/if_ath_alq.h>
57
58 #ifdef ATH_DEBUG_ALQ
59 static struct ale *
if_ath_alq_get(struct if_ath_alq * alq,int len)60 if_ath_alq_get(struct if_ath_alq *alq, int len)
61 {
62 struct ale *ale;
63
64 if (alq->sc_alq_isactive == 0)
65 return (NULL);
66
67 ale = alq_getn(alq->sc_alq_alq, len, ALQ_NOWAIT);
68 if (! ale)
69 alq->sc_alq_numlost++;
70 return (ale);
71 }
72
73 void
if_ath_alq_init(struct if_ath_alq * alq,const char * devname)74 if_ath_alq_init(struct if_ath_alq *alq, const char *devname)
75 {
76
77 bzero(alq, sizeof(*alq));
78
79 strncpy(alq->sc_alq_devname, devname, ATH_ALQ_DEVNAME_LEN);
80 kprintf("%s (%s): attached\n", __func__, alq->sc_alq_devname);
81 ksnprintf(alq->sc_alq_filename, ATH_ALQ_FILENAME_LEN,
82 "/tmp/ath_%s_alq.log", alq->sc_alq_devname);
83
84 /* XXX too conservative, right? */
85 alq->sc_alq_qsize = (64*1024);
86 }
87
88 void
if_ath_alq_setcfg(struct if_ath_alq * alq,uint32_t macVer,uint32_t macRev,uint32_t phyRev,uint32_t halMagic)89 if_ath_alq_setcfg(struct if_ath_alq *alq, uint32_t macVer,
90 uint32_t macRev, uint32_t phyRev, uint32_t halMagic)
91 {
92
93 /* Store these in network order */
94 alq->sc_alq_cfg.sc_mac_version = htobe32(macVer);
95 alq->sc_alq_cfg.sc_mac_revision = htobe32(macRev);
96 alq->sc_alq_cfg.sc_phy_rev = htobe32(phyRev);
97 alq->sc_alq_cfg.sc_hal_magic = htobe32(halMagic);
98 }
99
100 void
if_ath_alq_tidyup(struct if_ath_alq * alq)101 if_ath_alq_tidyup(struct if_ath_alq *alq)
102 {
103
104 if_ath_alq_stop(alq);
105 kprintf("%s (%s): detached\n", __func__, alq->sc_alq_devname);
106 bzero(alq, sizeof(*alq));
107 }
108
109 int
if_ath_alq_start(struct if_ath_alq * alq)110 if_ath_alq_start(struct if_ath_alq *alq)
111 {
112 int error;
113
114 if (alq->sc_alq_isactive)
115 return (0);
116
117 /*
118 * Create a variable-length ALQ.
119 */
120 error = alq_open(&alq->sc_alq_alq, alq->sc_alq_filename,
121 curthread->td_ucred, ALQ_DEFAULT_CMODE,
122 alq->sc_alq_qsize, 0);
123
124 if (error != 0) {
125 kprintf("%s (%s): failed, err=%d\n", __func__,
126 alq->sc_alq_devname, error);
127 } else {
128 kprintf("%s (%s): opened\n", __func__, alq->sc_alq_devname);
129 alq->sc_alq_isactive = 1;
130 if_ath_alq_post(alq, ATH_ALQ_INIT_STATE,
131 sizeof (struct if_ath_alq_init_state),
132 (char *) &alq->sc_alq_cfg);
133 }
134 return (error);
135 }
136
137 int
if_ath_alq_stop(struct if_ath_alq * alq)138 if_ath_alq_stop(struct if_ath_alq *alq)
139 {
140
141 if (alq->sc_alq_isactive == 0)
142 return (0);
143
144 kprintf("%s (%s): closed\n", __func__, alq->sc_alq_devname);
145
146 alq->sc_alq_isactive = 0;
147 alq_close(alq->sc_alq_alq);
148 alq->sc_alq_alq = NULL;
149
150 return (0);
151 }
152
153 /*
154 * Post a debug message to the ALQ.
155 *
156 * "len" is the size of the buf payload in bytes.
157 */
158 void
if_ath_alq_post(struct if_ath_alq * alq,uint16_t op,uint16_t len,const char * buf)159 if_ath_alq_post(struct if_ath_alq *alq, uint16_t op, uint16_t len,
160 const char *buf)
161 {
162 struct if_ath_alq_hdr *ap;
163 struct ale *ale;
164 struct timeval tv;
165
166 if (! if_ath_alq_checkdebug(alq, op))
167 return;
168
169 microtime(&tv);
170
171 /*
172 * Enforce some semblence of sanity on 'len'.
173 * Although strictly speaking, any length is possible -
174 * just be conservative so things don't get out of hand.
175 */
176 if (len > ATH_ALQ_PAYLOAD_LEN)
177 len = ATH_ALQ_PAYLOAD_LEN;
178
179 ale = if_ath_alq_get(alq, len + sizeof(struct if_ath_alq_hdr));
180
181 if (ale == NULL)
182 return;
183
184 ap = (struct if_ath_alq_hdr *) ale->ae_data;
185 ap->threadid = htobe64((uint64_t) curthread->td_tid);
186 ap->tstamp_sec = htobe32((uint32_t) tv.tv_sec);
187 ap->tstamp_usec = htobe32((uint32_t) tv.tv_usec);
188 ap->op = htobe16(op);
189 ap->len = htobe16(len);
190
191 /*
192 * Copy the payload _after_ the header field.
193 */
194 if (buf != NULL) {
195 memcpy(((char *) ap) + sizeof(struct if_ath_alq_hdr),
196 buf,
197 len);
198 }
199
200 alq_post(alq->sc_alq_alq, ale);
201 }
202 #endif /* ATH_DEBUG */
203