xref: /freebsd/sys/dev/ath/if_ath_alq.c (revision 2a2441c9fad4cd37a5423a58a60908cf35250a64)
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/pcpu.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/alq.h>
46 
47 #include <dev/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 	printf("%s (%s): detached\n", __func__, alq->sc_alq_devname);
72 	snprintf(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_tidyup(struct if_ath_alq *alq)
81 {
82 
83 	if_ath_alq_stop(alq);
84 	printf("%s (%s): detached\n", __func__, alq->sc_alq_devname);
85 	bzero(alq, sizeof(*alq));
86 }
87 
88 int
89 if_ath_alq_start(struct if_ath_alq *alq)
90 {
91 	int error;
92 
93 	if (alq->sc_alq_isactive)
94 		return (0);
95 
96 	/*
97 	 * Create a variable-length ALQ.
98 	 */
99 	error = alq_open(&alq->sc_alq_alq, alq->sc_alq_filename,
100 	    curthread->td_ucred, ALQ_DEFAULT_CMODE,
101 	    alq->sc_alq_qsize, 0);
102 
103 	if (error != 0) {
104 		printf("%s (%s): failed, err=%d\n", __func__,
105 		    alq->sc_alq_devname, error);
106 	} else {
107 		printf("%s (%s): opened\n", __func__, alq->sc_alq_devname);
108 		alq->sc_alq_isactive = 1;
109 	}
110 	return (error);
111 }
112 
113 int
114 if_ath_alq_stop(struct if_ath_alq *alq)
115 {
116 
117 	if (alq->sc_alq_isactive == 0)
118 		return (0);
119 
120 	printf("%s (%s): closed\n", __func__, alq->sc_alq_devname);
121 
122 	alq->sc_alq_isactive = 0;
123 	alq_close(alq->sc_alq_alq);
124 	alq->sc_alq_alq = NULL;
125 
126 	return (0);
127 }
128 
129 /*
130  * Post a debug message to the ALQ.
131  *
132  * "len" is the size of the buf payload in bytes.
133  */
134 void
135 if_ath_alq_post(struct if_ath_alq *alq, uint16_t op, uint16_t len,
136     const char *buf)
137 {
138 	struct if_ath_alq_hdr *ap;
139 	struct ale *ale;
140 
141 	if (! if_ath_alq_checkdebug(alq, op))
142 		return;
143 
144 	/*
145 	 * Enforce some semblence of sanity on 'len'.
146 	 * Although strictly speaking, any length is possible -
147 	 * just be conservative so things don't get out of hand.
148 	 */
149 	if (len > ATH_ALQ_PAYLOAD_LEN)
150 		len = ATH_ALQ_PAYLOAD_LEN;
151 
152 	ale = if_ath_alq_get(alq, len + sizeof(struct if_ath_alq_hdr));
153 
154 	if (ale == NULL)
155 		return;
156 
157 	ap = (struct if_ath_alq_hdr *) ale->ae_data;
158 	ap->threadid = (uint64_t) curthread->td_tid;
159 	ap->tstamp = (uint32_t) ticks;
160 	ap->op = op;
161 	ap->len = len;
162 
163 	/*
164 	 * Copy the payload _after_ the header field.
165 	 */
166 	memcpy(((char *) ap) + sizeof(struct if_ath_alq_hdr),
167 	    buf,
168 	    ap->len);
169 
170 	alq_post(alq->sc_alq_alq, ale);
171 }
172 #endif	/* ATH_DEBUG */
173