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