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