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