xref: /freebsd-13-stable/sys/dev/malo/if_malohal.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2007 Marvell Semiconductor, Inc.
5  * Copyright (c) 2007 Sam Leffler, Errno Consulting
6  * Copyright (c) 2008 Weongyo Jeong <weongyo@freebsd.org>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer,
14  *    without modification.
15  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
17  *    redistribution must be conditioned upon including a substantially
18  *    similar Disclaimer requirement for further binary redistribution.
19  *
20  * NO WARRANTY
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
24  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
25  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
26  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGES.
32  */
33 
34 #include <sys/cdefs.h>
35 #ifdef __FreeBSD__
36 #endif
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/endian.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/firmware.h>
44 #include <sys/socket.h>
45 
46 #include <machine/bus.h>
47 #include <sys/bus.h>
48 
49 #include <net/if.h>
50 #include <net/if_var.h>
51 #include <net/if_dl.h>
52 #include <net/if_media.h>
53 #include <net/ethernet.h>
54 
55 #include <net80211/ieee80211_var.h>
56 
57 #include <dev/malo/if_malo.h>
58 
59 #define MALO_WAITOK				1
60 #define MALO_NOWAIT				0
61 
62 #define	_CMD_SETUP(pCmd, _type, _cmd) do {				\
63 	pCmd = (_type *)&mh->mh_cmdbuf[0];				\
64 	memset(pCmd, 0, sizeof(_type));					\
65 	pCmd->cmdhdr.cmd = htole16(_cmd);				\
66 	pCmd->cmdhdr.length = htole16(sizeof(_type));			\
67 } while (0)
68 
69 static __inline uint32_t
malo_hal_read4(struct malo_hal * mh,bus_size_t off)70 malo_hal_read4(struct malo_hal *mh, bus_size_t off)
71 {
72 	return bus_space_read_4(mh->mh_iot, mh->mh_ioh, off);
73 }
74 
75 static __inline void
malo_hal_write4(struct malo_hal * mh,bus_size_t off,uint32_t val)76 malo_hal_write4(struct malo_hal *mh, bus_size_t off, uint32_t val)
77 {
78 	bus_space_write_4(mh->mh_iot, mh->mh_ioh, off, val);
79 }
80 
81 static void
malo_hal_load_cb(void * arg,bus_dma_segment_t * segs,int nsegs,int error)82 malo_hal_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
83 {
84 	bus_addr_t *paddr = (bus_addr_t*) arg;
85 
86 	KASSERT(error == 0, ("error %u on bus_dma callback", error));
87 	*paddr = segs->ds_addr;
88 }
89 
90 /*
91  * Setup for communication with the device.  We allocate
92  * a command buffer and map it for bus dma use.  The pci
93  * device id is used to identify whether the device has
94  * SRAM on it (in which case f/w download must include a
95  * memory controller reset).  All bus i/o operations happen
96  * in BAR 1; the driver passes in the tag and handle we need.
97  */
98 struct malo_hal *
malo_hal_attach(device_t dev,uint16_t devid,bus_space_handle_t ioh,bus_space_tag_t iot,bus_dma_tag_t tag)99 malo_hal_attach(device_t dev, uint16_t devid,
100     bus_space_handle_t ioh, bus_space_tag_t iot, bus_dma_tag_t tag)
101 {
102 	int error;
103 	struct malo_hal *mh;
104 
105 	mh = malloc(sizeof(struct malo_hal), M_DEVBUF, M_NOWAIT | M_ZERO);
106 	if (mh == NULL)
107 		return NULL;
108 
109 	mh->mh_dev = dev;
110 	mh->mh_ioh = ioh;
111 	mh->mh_iot = iot;
112 
113 	snprintf(mh->mh_mtxname, sizeof(mh->mh_mtxname),
114 	    "%s_hal", device_get_nameunit(dev));
115 	mtx_init(&mh->mh_mtx, mh->mh_mtxname, NULL, MTX_DEF);
116 
117 	/*
118 	 * Allocate the command buffer and map into the address
119 	 * space of the h/w.  We request "coherent" memory which
120 	 * will be uncached on some architectures.
121 	 */
122 	error = bus_dma_tag_create(tag,		/* parent */
123 		       PAGE_SIZE, 0,		/* alignment, bounds */
124 		       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
125 		       BUS_SPACE_MAXADDR,	/* highaddr */
126 		       NULL, NULL,		/* filter, filterarg */
127 		       MALO_CMDBUF_SIZE,	/* maxsize */
128 		       1,			/* nsegments */
129 		       MALO_CMDBUF_SIZE,	/* maxsegsize */
130 		       BUS_DMA_ALLOCNOW,	/* flags */
131 		       NULL,			/* lockfunc */
132 		       NULL,			/* lockarg */
133 		       &mh->mh_dmat);
134 	if (error != 0) {
135 		device_printf(dev, "unable to allocate memory for cmd tag, "
136 			"error %u\n", error);
137 		goto fail;
138 	}
139 
140 	/* allocate descriptors */
141 	error = bus_dmamem_alloc(mh->mh_dmat, (void**) &mh->mh_cmdbuf,
142 				 BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
143 				 &mh->mh_dmamap);
144 	if (error != 0) {
145 		device_printf(dev, "unable to allocate memory for cmd buffer, "
146 			"error %u\n", error);
147 		goto fail;
148 	}
149 
150 	error = bus_dmamap_load(mh->mh_dmat, mh->mh_dmamap,
151 				mh->mh_cmdbuf, MALO_CMDBUF_SIZE,
152 				malo_hal_load_cb, &mh->mh_cmdaddr,
153 				BUS_DMA_NOWAIT);
154 	if (error != 0) {
155 		device_printf(dev, "unable to load cmd buffer, error %u\n",
156 			error);
157 		goto fail;
158 	}
159 
160 	return (mh);
161 
162 fail:
163 	if (mh->mh_cmdbuf != NULL)
164 		bus_dmamem_free(mh->mh_dmat, mh->mh_cmdbuf,
165 		    mh->mh_dmamap);
166 	if (mh->mh_dmat)
167 		bus_dma_tag_destroy(mh->mh_dmat);
168 	free(mh, M_DEVBUF);
169 
170 	return (NULL);
171 }
172 
173 /*
174  * Low level firmware cmd block handshake support.
175  */
176 
177 static void
malo_hal_send_cmd(struct malo_hal * mh)178 malo_hal_send_cmd(struct malo_hal *mh)
179 {
180 	uint32_t dummy;
181 
182 	bus_dmamap_sync(mh->mh_dmat, mh->mh_dmamap,
183 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
184 
185 	malo_hal_write4(mh, MALO_REG_GEN_PTR, mh->mh_cmdaddr);
186 	dummy = malo_hal_read4(mh, MALO_REG_INT_CODE);
187 
188 	malo_hal_write4(mh, MALO_REG_H2A_INTERRUPT_EVENTS,
189 	    MALO_H2ARIC_BIT_DOOR_BELL);
190 }
191 
192 static int
malo_hal_waitforcmd(struct malo_hal * mh,uint16_t cmd)193 malo_hal_waitforcmd(struct malo_hal *mh, uint16_t cmd)
194 {
195 #define MAX_WAIT_FW_COMPLETE_ITERATIONS 10000
196 	int i;
197 
198 	for (i = 0; i < MAX_WAIT_FW_COMPLETE_ITERATIONS; i++) {
199 		if (mh->mh_cmdbuf[0] == le16toh(cmd))
200 			return 1;
201 
202 		DELAY(1 * 1000);
203 	}
204 
205 	return 0;
206 #undef MAX_WAIT_FW_COMPLETE_ITERATIONS
207 }
208 
209 static int
malo_hal_execute_cmd(struct malo_hal * mh,unsigned short cmd)210 malo_hal_execute_cmd(struct malo_hal *mh, unsigned short cmd)
211 {
212 	MALO_HAL_LOCK_ASSERT(mh);
213 
214 	if ((mh->mh_flags & MHF_FWHANG) &&
215 	    (mh->mh_debug & MALO_HAL_DEBUG_IGNHANG) == 0) {
216 		device_printf(mh->mh_dev, "firmware hung, skipping cmd 0x%x\n",
217 			cmd);
218 		return ENXIO;
219 	}
220 
221 	if (malo_hal_read4(mh, MALO_REG_INT_CODE) == 0xffffffff) {
222 		device_printf(mh->mh_dev, "%s: device not present!\n",
223 		    __func__);
224 		return EIO;
225 	}
226 
227 	malo_hal_send_cmd(mh);
228 	if (!malo_hal_waitforcmd(mh, cmd | 0x8000)) {
229 		device_printf(mh->mh_dev,
230 		    "timeout waiting for f/w cmd 0x%x\n", cmd);
231 		mh->mh_flags |= MHF_FWHANG;
232 		return ETIMEDOUT;
233 	}
234 
235 	bus_dmamap_sync(mh->mh_dmat, mh->mh_dmamap,
236 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
237 
238 	return 0;
239 }
240 
241 static int
malo_hal_get_cal_table(struct malo_hal * mh,uint8_t annex,uint8_t index)242 malo_hal_get_cal_table(struct malo_hal *mh, uint8_t annex, uint8_t index)
243 {
244 	struct malo_cmd_caltable *cmd;
245 	int ret;
246 
247 	MALO_HAL_LOCK_ASSERT(mh);
248 
249 	_CMD_SETUP(cmd, struct malo_cmd_caltable, MALO_HOSTCMD_GET_CALTABLE);
250 	cmd->annex = annex;
251 	cmd->index = index;
252 
253 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_GET_CALTABLE);
254 	if (ret == 0 && cmd->caltbl[0] != annex && annex != 0 && annex != 255)
255 		ret = EIO;
256 	return ret;
257 }
258 
259 static int
malo_hal_get_pwrcal_table(struct malo_hal * mh,struct malo_hal_caldata * cal)260 malo_hal_get_pwrcal_table(struct malo_hal *mh, struct malo_hal_caldata *cal)
261 {
262 	const uint8_t *data;
263 	int len;
264 
265 	MALO_HAL_LOCK(mh);
266 	/* NB: we hold the lock so it's ok to use cmdbuf */
267 	data = ((const struct malo_cmd_caltable *) mh->mh_cmdbuf)->caltbl;
268 	if (malo_hal_get_cal_table(mh, 33, 0) == 0) {
269 		len = (data[2] | (data[3] << 8)) - 12;
270 		/* XXX validate len */
271 		memcpy(cal->pt_ratetable_20m, &data[12], len);
272 	}
273 	mh->mh_flags |= MHF_CALDATA;
274 	MALO_HAL_UNLOCK(mh);
275 
276 	return 0;
277 }
278 
279 /*
280  * Reset internal state after a firmware download.
281  */
282 static int
malo_hal_resetstate(struct malo_hal * mh)283 malo_hal_resetstate(struct malo_hal *mh)
284 {
285 	/*
286 	 * Fetch cal data for later use.
287 	 * XXX may want to fetch other stuff too.
288 	 */
289 	if ((mh->mh_flags & MHF_CALDATA) == 0)
290 		malo_hal_get_pwrcal_table(mh, &mh->mh_caldata);
291 	return 0;
292 }
293 
294 static void
malo_hal_fw_reset(struct malo_hal * mh)295 malo_hal_fw_reset(struct malo_hal *mh)
296 {
297 
298 	if (malo_hal_read4(mh,  MALO_REG_INT_CODE) == 0xffffffff) {
299 		device_printf(mh->mh_dev, "%s: device not present!\n",
300 		    __func__);
301 		return;
302 	}
303 
304 	malo_hal_write4(mh, MALO_REG_H2A_INTERRUPT_EVENTS, MALO_ISR_RESET);
305 	mh->mh_flags &= ~MHF_FWHANG;
306 }
307 
308 static void
malo_hal_trigger_pcicmd(struct malo_hal * mh)309 malo_hal_trigger_pcicmd(struct malo_hal *mh)
310 {
311 	uint32_t dummy;
312 
313 	bus_dmamap_sync(mh->mh_dmat, mh->mh_dmamap, BUS_DMASYNC_PREWRITE);
314 
315 	malo_hal_write4(mh, MALO_REG_GEN_PTR, mh->mh_cmdaddr);
316 	dummy = malo_hal_read4(mh, MALO_REG_INT_CODE);
317 
318 	malo_hal_write4(mh, MALO_REG_INT_CODE, 0x00);
319 	dummy = malo_hal_read4(mh, MALO_REG_INT_CODE);
320 
321 	malo_hal_write4(mh, MALO_REG_H2A_INTERRUPT_EVENTS,
322 	    MALO_H2ARIC_BIT_DOOR_BELL);
323 	dummy = malo_hal_read4(mh, MALO_REG_INT_CODE);
324 }
325 
326 static int
malo_hal_waitfor(struct malo_hal * mh,uint32_t val)327 malo_hal_waitfor(struct malo_hal *mh, uint32_t val)
328 {
329 	int i;
330 
331 	for (i = 0; i < MALO_FW_MAX_NUM_CHECKS; i++) {
332 		DELAY(MALO_FW_CHECK_USECS);
333 		if (malo_hal_read4(mh, MALO_REG_INT_CODE) == val)
334 			return 0;
335 	}
336 
337 	return -1;
338 }
339 
340 /*
341  * Firmware block xmit when talking to the boot-rom.
342  */
343 static int
malo_hal_send_helper(struct malo_hal * mh,int bsize,const void * data,size_t dsize,int waitfor)344 malo_hal_send_helper(struct malo_hal *mh, int bsize,
345     const void *data, size_t dsize, int waitfor)
346 {
347 	mh->mh_cmdbuf[0] = htole16(MALO_HOSTCMD_CODE_DNLD);
348 	mh->mh_cmdbuf[1] = htole16(bsize);
349 	memcpy(&mh->mh_cmdbuf[4], data , dsize);
350 
351 	malo_hal_trigger_pcicmd(mh);
352 
353 	if (waitfor == MALO_NOWAIT)
354 		goto pass;
355 
356 	/* XXX 2000 vs 200 */
357 	if (malo_hal_waitfor(mh, MALO_INT_CODE_CMD_FINISHED) != 0) {
358 		device_printf(mh->mh_dev,
359 		    "%s: timeout waiting for CMD_FINISHED, INT_CODE 0x%x\n",
360 		    __func__, malo_hal_read4(mh, MALO_REG_INT_CODE));
361 
362 		return ETIMEDOUT;
363 	}
364 
365 pass:
366 	malo_hal_write4(mh, MALO_REG_INT_CODE, 0);
367 
368 	return (0);
369 }
370 
371 static int
malo_hal_fwload_helper(struct malo_hal * mh,char * helper)372 malo_hal_fwload_helper(struct malo_hal *mh, char *helper)
373 {
374 	const struct firmware *fw;
375 	int error;
376 
377 	fw = firmware_get(helper);
378 	if (fw == NULL) {
379 		device_printf(mh->mh_dev, "could not read microcode %s!\n",
380 		    helper);
381 		return (EIO);
382 	}
383 
384 	device_printf(mh->mh_dev, "load %s firmware image (%zu bytes)\n",
385 	    helper, fw->datasize);
386 
387 	error = malo_hal_send_helper(mh, fw->datasize, fw->data, fw->datasize,
388 		MALO_WAITOK);
389 	if (error != 0)
390 		goto fail;
391 
392 	/* tell the card we're done and... */
393 	error = malo_hal_send_helper(mh, 0, NULL, 0, MALO_NOWAIT);
394 
395 fail:
396 	firmware_put(fw, FIRMWARE_UNLOAD);
397 
398 	return (error);
399 }
400 
401 /*
402  * Firmware block xmit when talking to the 1st-stage loader.
403  */
404 static int
malo_hal_send_main(struct malo_hal * mh,const void * data,size_t dsize,uint16_t seqnum,int waitfor)405 malo_hal_send_main(struct malo_hal *mh, const void *data, size_t dsize,
406     uint16_t seqnum, int waitfor)
407 {
408 	mh->mh_cmdbuf[0] = htole16(MALO_HOSTCMD_CODE_DNLD);
409 	mh->mh_cmdbuf[1] = htole16(dsize);
410 	mh->mh_cmdbuf[2] = htole16(seqnum);
411 	mh->mh_cmdbuf[3] = 0;
412 	memcpy(&mh->mh_cmdbuf[4], data, dsize);
413 
414 	malo_hal_trigger_pcicmd(mh);
415 
416 	if (waitfor == MALO_NOWAIT)
417 		goto pass;
418 
419 	if (malo_hal_waitfor(mh, MALO_INT_CODE_CMD_FINISHED) != 0) {
420 		device_printf(mh->mh_dev,
421 		    "%s: timeout waiting for CMD_FINISHED, INT_CODE 0x%x\n",
422 		    __func__, malo_hal_read4(mh, MALO_REG_INT_CODE));
423 
424 		return ETIMEDOUT;
425 	}
426 
427 pass:
428 	malo_hal_write4(mh, MALO_REG_INT_CODE, 0);
429 
430 	return 0;
431 }
432 
433 static int
malo_hal_fwload_main(struct malo_hal * mh,char * firmware)434 malo_hal_fwload_main(struct malo_hal *mh, char *firmware)
435 {
436 	const struct firmware *fw;
437 	const uint8_t *fp;
438 	int error;
439 	size_t count;
440 	uint16_t seqnum;
441 	uint32_t blocksize;
442 
443 	error = 0;
444 
445 	fw = firmware_get(firmware);
446 	if (fw == NULL) {
447 		device_printf(mh->mh_dev, "could not read firmware %s!\n",
448 		    firmware);
449 		return (EIO);
450 	}
451 
452 	device_printf(mh->mh_dev, "load %s firmware image (%zu bytes)\n",
453 	    firmware, fw->datasize);
454 
455 	seqnum = 1;
456 	for (count = 0; count < fw->datasize; count += blocksize) {
457 		blocksize = MIN(256, fw->datasize - count);
458 		fp = (const uint8_t *)fw->data + count;
459 
460 		error = malo_hal_send_main(mh, fp, blocksize, seqnum++,
461 		    MALO_NOWAIT);
462 		if (error != 0)
463 			goto fail;
464 		DELAY(500);
465 	}
466 
467 	/*
468 	 * send a command with size 0 to tell that the firmware has been
469 	 * uploaded
470 	 */
471 	error = malo_hal_send_main(mh, NULL, 0, seqnum++, MALO_NOWAIT);
472 	DELAY(100);
473 
474 fail:
475 	firmware_put(fw, FIRMWARE_UNLOAD);
476 
477 	return (error);
478 }
479 
480 int
malo_hal_fwload(struct malo_hal * mh,char * helper,char * firmware)481 malo_hal_fwload(struct malo_hal *mh, char *helper, char *firmware)
482 {
483 	int error, i;
484 	uint32_t fwreadysig, opmode;
485 
486 	/*
487 	 * NB: now malo(4) supports only STA mode.  It will be better if it
488 	 * supports AP mode.
489 	 */
490 	fwreadysig = MALO_HOSTCMD_STA_FWRDY_SIGNATURE;
491 	opmode = MALO_HOSTCMD_STA_MODE;
492 
493 	malo_hal_fw_reset(mh);
494 
495 	malo_hal_write4(mh, MALO_REG_A2H_INTERRUPT_CLEAR_SEL,
496 	    MALO_A2HRIC_BIT_MASK);
497 	malo_hal_write4(mh, MALO_REG_A2H_INTERRUPT_CAUSE, 0x00);
498 	malo_hal_write4(mh, MALO_REG_A2H_INTERRUPT_MASK, 0x00);
499 	malo_hal_write4(mh, MALO_REG_A2H_INTERRUPT_STATUS_MASK,
500 	    MALO_A2HRIC_BIT_MASK);
501 
502 	error = malo_hal_fwload_helper(mh, helper);
503 	if (error != 0) {
504 		device_printf(mh->mh_dev, "failed to load bootrom loader.\n");
505 		goto fail;
506 	}
507 
508 	DELAY(200 * MALO_FW_CHECK_USECS);
509 
510 	error = malo_hal_fwload_main(mh, firmware);
511 	if (error != 0) {
512 		device_printf(mh->mh_dev, "failed to load firmware.\n");
513 		goto fail;
514 	}
515 
516 	/*
517 	 * Wait for firmware to startup; we monitor the INT_CODE register
518 	 * waiting for a signature to written back indicating it's ready to go.
519 	 */
520 	mh->mh_cmdbuf[1] = 0;
521 
522 	if (opmode != MALO_HOSTCMD_STA_MODE)
523 		malo_hal_trigger_pcicmd(mh);
524 
525 	for (i = 0; i < MALO_FW_MAX_NUM_CHECKS; i++) {
526 		malo_hal_write4(mh, MALO_REG_GEN_PTR, opmode);
527 		DELAY(MALO_FW_CHECK_USECS);
528 		if (malo_hal_read4(mh, MALO_REG_INT_CODE) == fwreadysig) {
529 			malo_hal_write4(mh, MALO_REG_INT_CODE, 0x00);
530 			return malo_hal_resetstate(mh);
531 		}
532 	}
533 
534 	return ETIMEDOUT;
535 fail:
536 	malo_hal_fw_reset(mh);
537 
538 	return (error);
539 }
540 
541 /*
542  * Return "hw specs".  Note this must be the first cmd MUST be done after
543  * a firmware download or the f/w will lockup.
544  */
545 int
malo_hal_gethwspecs(struct malo_hal * mh,struct malo_hal_hwspec * hw)546 malo_hal_gethwspecs(struct malo_hal *mh, struct malo_hal_hwspec *hw)
547 {
548 	struct malo_cmd_get_hwspec *cmd;
549 	int ret;
550 
551 	MALO_HAL_LOCK(mh);
552 
553 	_CMD_SETUP(cmd, struct malo_cmd_get_hwspec, MALO_HOSTCMD_GET_HW_SPEC);
554 	memset(&cmd->permaddr[0], 0xff, IEEE80211_ADDR_LEN);
555 	cmd->ul_fw_awakecookie = htole32((unsigned int)mh->mh_cmdaddr + 2048);
556 
557 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_GET_HW_SPEC);
558 	if (ret == 0) {
559 		IEEE80211_ADDR_COPY(hw->macaddr, cmd->permaddr);
560 		hw->wcbbase[0] = le32toh(cmd->wcbbase0) & 0x0000ffff;
561 		hw->wcbbase[1] = le32toh(cmd->wcbbase1) & 0x0000ffff;
562 		hw->wcbbase[2] = le32toh(cmd->wcbbase2) & 0x0000ffff;
563 		hw->wcbbase[3] = le32toh(cmd->wcbbase3) & 0x0000ffff;
564 		hw->rxdesc_read = le32toh(cmd->rxpdrd_ptr)& 0x0000ffff;
565 		hw->rxdesc_write = le32toh(cmd->rxpdwr_ptr)& 0x0000ffff;
566 		hw->regioncode = le16toh(cmd->regioncode) & 0x00ff;
567 		hw->fw_releasenum = le32toh(cmd->fw_releasenum);
568 		hw->maxnum_wcb = le16toh(cmd->num_wcb);
569 		hw->maxnum_mcaddr = le16toh(cmd->num_mcastaddr);
570 		hw->num_antenna = le16toh(cmd->num_antenna);
571 		hw->hwversion = cmd->version;
572 		hw->hostinterface = cmd->hostif;
573 	}
574 
575 	MALO_HAL_UNLOCK(mh);
576 
577 	return ret;
578 }
579 
580 void
malo_hal_detach(struct malo_hal * mh)581 malo_hal_detach(struct malo_hal *mh)
582 {
583 
584 	bus_dmamem_free(mh->mh_dmat, mh->mh_cmdbuf, mh->mh_dmamap);
585 	bus_dma_tag_destroy(mh->mh_dmat);
586 	mtx_destroy(&mh->mh_mtx);
587 	free(mh, M_DEVBUF);
588 }
589 
590 /*
591  * Configure antenna use.  Takes effect immediately.
592  *
593  * XXX tx antenna setting ignored
594  * XXX rx antenna setting should always be 3 (for now)
595  */
596 int
malo_hal_setantenna(struct malo_hal * mh,enum malo_hal_antenna dirset,int ant)597 malo_hal_setantenna(struct malo_hal *mh, enum malo_hal_antenna dirset, int ant)
598 {
599 	struct malo_cmd_rf_antenna *cmd;
600 	int ret;
601 
602 	if (!(dirset == MHA_ANTENNATYPE_RX || dirset == MHA_ANTENNATYPE_TX))
603 		return EINVAL;
604 
605 	MALO_HAL_LOCK(mh);
606 
607 	_CMD_SETUP(cmd, struct malo_cmd_rf_antenna,
608 	    MALO_HOSTCMD_802_11_RF_ANTENNA);
609 	cmd->action = htole16(dirset);
610 	if (ant == 0) {			/* default to all/both antennae */
611 		/* XXX never reach now.  */
612 		ant = 3;
613 	}
614 	cmd->mode = htole16(ant);
615 
616 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_802_11_RF_ANTENNA);
617 
618 	MALO_HAL_UNLOCK(mh);
619 
620 	return ret;
621 }
622 
623 /*
624  * Configure radio.  Takes effect immediately.
625  *
626  * XXX preamble installed after set fixed rate cmd
627  */
628 int
malo_hal_setradio(struct malo_hal * mh,int onoff,enum malo_hal_preamble preamble)629 malo_hal_setradio(struct malo_hal *mh, int onoff,
630     enum malo_hal_preamble preamble)
631 {
632 	struct malo_cmd_radio_control *cmd;
633 	int ret;
634 
635 	MALO_HAL_LOCK(mh);
636 
637 	_CMD_SETUP(cmd, struct malo_cmd_radio_control,
638 	    MALO_HOSTCMD_802_11_RADIO_CONTROL);
639 	cmd->action = htole16(MALO_HOSTCMD_ACT_GEN_SET);
640 	if (onoff == 0)
641 		cmd->control = 0;
642 	else
643 		cmd->control = htole16(preamble);
644 	cmd->radio_on = htole16(onoff);
645 
646 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_802_11_RADIO_CONTROL);
647 
648 	MALO_HAL_UNLOCK(mh);
649 
650 	return ret;
651 }
652 
653 /*
654  * Set the interrupt mask.
655  */
656 void
malo_hal_intrset(struct malo_hal * mh,uint32_t mask)657 malo_hal_intrset(struct malo_hal *mh, uint32_t mask)
658 {
659 
660 	malo_hal_write4(mh, MALO_REG_A2H_INTERRUPT_MASK, 0);
661 	(void)malo_hal_read4(mh, MALO_REG_INT_CODE);
662 
663 	mh->mh_imask = mask;
664 	malo_hal_write4(mh, MALO_REG_A2H_INTERRUPT_MASK, mask);
665 	(void)malo_hal_read4(mh, MALO_REG_INT_CODE);
666 }
667 
668 int
malo_hal_setchannel(struct malo_hal * mh,const struct malo_hal_channel * chan)669 malo_hal_setchannel(struct malo_hal *mh, const struct malo_hal_channel *chan)
670 {
671 	struct malo_cmd_fw_set_rf_channel *cmd;
672 	int ret;
673 
674 	MALO_HAL_LOCK(mh);
675 
676 	_CMD_SETUP(cmd, struct malo_cmd_fw_set_rf_channel,
677 	    MALO_HOSTCMD_SET_RF_CHANNEL);
678 	cmd->action = htole16(MALO_HOSTCMD_ACT_GEN_SET);
679 	cmd->cur_channel = chan->channel;
680 
681 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_SET_RF_CHANNEL);
682 
683 	MALO_HAL_UNLOCK(mh);
684 
685 	return ret;
686 }
687 
688 int
malo_hal_settxpower(struct malo_hal * mh,const struct malo_hal_channel * c)689 malo_hal_settxpower(struct malo_hal *mh, const struct malo_hal_channel *c)
690 {
691 	struct malo_cmd_rf_tx_power *cmd;
692 	const struct malo_hal_caldata *cal = &mh->mh_caldata;
693 	uint8_t chan = c->channel;
694 	uint16_t pow;
695 	int i, idx, ret;
696 
697 	MALO_HAL_LOCK(mh);
698 
699 	_CMD_SETUP(cmd, struct malo_cmd_rf_tx_power,
700 	    MALO_HOSTCMD_802_11_RF_TX_POWER);
701 	cmd->action = htole16(MALO_HOSTCMD_ACT_GEN_SET_LIST);
702 	for (i = 0; i < 4; i++) {
703 		idx = (chan - 1) * 4 + i;
704 		pow = cal->pt_ratetable_20m[idx];
705 		cmd->power_levellist[i] = htole16(pow);
706 	}
707 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_802_11_RF_TX_POWER);
708 
709 	MALO_HAL_UNLOCK(mh);
710 
711 	return ret;
712 }
713 
714 int
malo_hal_setpromisc(struct malo_hal * mh,int enable)715 malo_hal_setpromisc(struct malo_hal *mh, int enable)
716 {
717 	/* XXX need host cmd */
718 	return 0;
719 }
720 
721 int
malo_hal_setassocid(struct malo_hal * mh,const uint8_t bssid[IEEE80211_ADDR_LEN],uint16_t associd)722 malo_hal_setassocid(struct malo_hal *mh,
723     const uint8_t bssid[IEEE80211_ADDR_LEN], uint16_t associd)
724 {
725 	struct malo_cmd_fw_set_aid *cmd;
726 	int ret;
727 
728 	MALO_HAL_LOCK(mh);
729 
730 	_CMD_SETUP(cmd, struct malo_cmd_fw_set_aid,
731 	    MALO_HOSTCMD_SET_AID);
732 	cmd->cmdhdr.seqnum = 1;
733 	cmd->associd = htole16(associd);
734 	IEEE80211_ADDR_COPY(&cmd->macaddr[0], bssid);
735 
736 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_SET_AID);
737 	MALO_HAL_UNLOCK(mh);
738 	return ret;
739 }
740 
741 /*
742  * Kick the firmware to tell it there are new tx descriptors
743  * for processing.  The driver says what h/w q has work in
744  * case the f/w ever gets smarter.
745  */
746 void
malo_hal_txstart(struct malo_hal * mh,int qnum)747 malo_hal_txstart(struct malo_hal *mh, int qnum)
748 {
749 	bus_space_write_4(mh->mh_iot, mh->mh_ioh,
750 	    MALO_REG_H2A_INTERRUPT_EVENTS, MALO_H2ARIC_BIT_PPA_READY);
751 	(void) bus_space_read_4(mh->mh_iot, mh->mh_ioh, MALO_REG_INT_CODE);
752 }
753 
754 /*
755  * Return the current ISR setting and clear the cause.
756  */
757 void
malo_hal_getisr(struct malo_hal * mh,uint32_t * status)758 malo_hal_getisr(struct malo_hal *mh, uint32_t *status)
759 {
760 	uint32_t cause;
761 
762 	cause = bus_space_read_4(mh->mh_iot, mh->mh_ioh,
763 	    MALO_REG_A2H_INTERRUPT_CAUSE);
764 	if (cause == 0xffffffff) {	/* card removed */
765 		cause = 0;
766 	} else if (cause != 0) {
767 		/* clear cause bits */
768 		bus_space_write_4(mh->mh_iot, mh->mh_ioh,
769 		    MALO_REG_A2H_INTERRUPT_CAUSE, cause &~ mh->mh_imask);
770 		(void) bus_space_read_4(mh->mh_iot, mh->mh_ioh,
771 		    MALO_REG_INT_CODE);
772 		cause &= mh->mh_imask;
773 	}
774 
775 	*status = cause;
776 }
777 
778 /*
779  * Callback from the driver on a cmd done interrupt.  Nothing to do right
780  * now as we spin waiting for cmd completion.
781  */
782 void
malo_hal_cmddone(struct malo_hal * mh)783 malo_hal_cmddone(struct malo_hal *mh)
784 {
785 	/* NB : do nothing.  */
786 }
787 
788 int
malo_hal_prescan(struct malo_hal * mh)789 malo_hal_prescan(struct malo_hal *mh)
790 {
791 	struct malo_cmd_prescan *cmd;
792 	int ret;
793 
794 	MALO_HAL_LOCK(mh);
795 
796 	_CMD_SETUP(cmd, struct malo_cmd_prescan, MALO_HOSTCMD_SET_PRE_SCAN);
797 	cmd->cmdhdr.seqnum = 1;
798 
799 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_SET_PRE_SCAN);
800 
801 	MALO_HAL_UNLOCK(mh);
802 
803 	return ret;
804 }
805 
806 int
malo_hal_postscan(struct malo_hal * mh,uint8_t * macaddr,uint8_t ibsson)807 malo_hal_postscan(struct malo_hal *mh, uint8_t *macaddr, uint8_t ibsson)
808 {
809 	struct malo_cmd_postscan *cmd;
810 	int ret;
811 
812 	MALO_HAL_LOCK(mh);
813 
814 	_CMD_SETUP(cmd, struct malo_cmd_postscan, MALO_HOSTCMD_SET_POST_SCAN);
815 	cmd->cmdhdr.seqnum = 1;
816 	cmd->isibss = htole32(ibsson);
817 	IEEE80211_ADDR_COPY(&cmd->bssid[0], macaddr);
818 
819 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_SET_POST_SCAN);
820 
821 	MALO_HAL_UNLOCK(mh);
822 
823 	return ret;
824 }
825 
826 int
malo_hal_set_slot(struct malo_hal * mh,int is_short)827 malo_hal_set_slot(struct malo_hal *mh, int is_short)
828 {
829 	int ret;
830 	struct malo_cmd_fw_setslot *cmd;
831 
832 	MALO_HAL_LOCK(mh);
833 
834 	_CMD_SETUP(cmd, struct malo_cmd_fw_setslot, MALO_HOSTCMD_SET_SLOT);
835 	cmd->action = htole16(MALO_HOSTCMD_ACT_GEN_SET);
836 	cmd->slot = (is_short == 1 ? 1 : 0);
837 
838 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_SET_SLOT);
839 
840 	MALO_HAL_UNLOCK(mh);
841 
842 	return ret;
843 }
844 
845 int
malo_hal_set_rate(struct malo_hal * mh,uint16_t curmode,uint8_t rate)846 malo_hal_set_rate(struct malo_hal *mh, uint16_t curmode, uint8_t rate)
847 {
848 	int i, ret;
849 	struct malo_cmd_set_rate *cmd;
850 
851 	MALO_HAL_LOCK(mh);
852 
853 	_CMD_SETUP(cmd, struct malo_cmd_set_rate, MALO_HOSTCMD_SET_RATE);
854 	cmd->aprates[0] = 2;
855 	cmd->aprates[1] = 4;
856 	cmd->aprates[2] = 11;
857 	cmd->aprates[3] = 22;
858 	if (curmode == IEEE80211_MODE_11G) {
859 		cmd->aprates[4] = 0;		/* XXX reserved?  */
860 		cmd->aprates[5] = 12;
861 		cmd->aprates[6] = 18;
862 		cmd->aprates[7] = 24;
863 		cmd->aprates[8] = 36;
864 		cmd->aprates[9] = 48;
865 		cmd->aprates[10] = 72;
866 		cmd->aprates[11] = 96;
867 		cmd->aprates[12] = 108;
868 	}
869 
870 	if (rate != 0) {
871 		/* fixed rate */
872 		for (i = 0; i < 13; i++) {
873 			if (cmd->aprates[i] == rate) {
874 				cmd->rateindex = i;
875 				cmd->dataratetype = 1;
876 				break;
877 			}
878 		}
879 	}
880 
881 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_SET_RATE);
882 
883 	MALO_HAL_UNLOCK(mh);
884 
885 	return ret;
886 }
887 
888 int
malo_hal_setmcast(struct malo_hal * mh,int nmc,const uint8_t macs[])889 malo_hal_setmcast(struct malo_hal *mh, int nmc, const uint8_t macs[])
890 {
891 	struct malo_cmd_mcast *cmd;
892 	int ret;
893 
894 	if (nmc > MALO_HAL_MCAST_MAX)
895 		return EINVAL;
896 
897 	MALO_HAL_LOCK(mh);
898 
899 	_CMD_SETUP(cmd, struct malo_cmd_mcast, MALO_HOSTCMD_MAC_MULTICAST_ADR);
900 	memcpy(cmd->maclist, macs, nmc * IEEE80211_ADDR_LEN);
901 	cmd->numaddr = htole16(nmc);
902 	cmd->action = htole16(0xffff);
903 
904 	ret = malo_hal_execute_cmd(mh, MALO_HOSTCMD_MAC_MULTICAST_ADR);
905 
906 	MALO_HAL_UNLOCK(mh);
907 
908 	return ret;
909 }
910