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