Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / drivers / net / ath / ath9k / ath9k_init.c
1 /*
2  * Copyright (c) 2008-2011 Atheros Communications Inc.
3  *
4  * Modified for iPXE by Scott K Logan <logans@cottsay.net> July 2011
5  * Original from Linux kernel 3.0.1
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19
20 FILE_LICENCE ( BSD2 );
21
22 #include <ipxe/malloc.h>
23 #include <ipxe/pci_io.h>
24 #include <ipxe/pci.h>
25
26 #include "ath9k.h"
27
28 int is_ath9k_unloaded;
29 /* We use the hw_value as an index into our private channel structure */
30
31 #define CHAN2G(_freq, _idx)  { \
32         .band = NET80211_BAND_2GHZ, \
33         .center_freq = (_freq), \
34         .hw_value = (_idx), \
35         .maxpower = 20, \
36 }
37
38 #define CHAN5G(_freq, _idx) { \
39         .band = NET80211_BAND_5GHZ, \
40         .center_freq = (_freq), \
41         .hw_value = (_idx), \
42         .maxpower = 20, \
43 }
44
45 /* Some 2 GHz radios are actually tunable on 2312-2732
46  * on 5 MHz steps, we support the channels which we know
47  * we have calibration data for all cards though to make
48  * this static */
49 static const struct net80211_channel ath9k_2ghz_chantable[] = {
50         CHAN2G(2412, 0), /* Channel 1 */
51         CHAN2G(2417, 1), /* Channel 2 */
52         CHAN2G(2422, 2), /* Channel 3 */
53         CHAN2G(2427, 3), /* Channel 4 */
54         CHAN2G(2432, 4), /* Channel 5 */
55         CHAN2G(2437, 5), /* Channel 6 */
56         CHAN2G(2442, 6), /* Channel 7 */
57         CHAN2G(2447, 7), /* Channel 8 */
58         CHAN2G(2452, 8), /* Channel 9 */
59         CHAN2G(2457, 9), /* Channel 10 */
60         CHAN2G(2462, 10), /* Channel 11 */
61         CHAN2G(2467, 11), /* Channel 12 */
62         CHAN2G(2472, 12), /* Channel 13 */
63         CHAN2G(2484, 13), /* Channel 14 */
64 };
65
66 /* Some 5 GHz radios are actually tunable on XXXX-YYYY
67  * on 5 MHz steps, we support the channels which we know
68  * we have calibration data for all cards though to make
69  * this static */
70 static const struct net80211_channel ath9k_5ghz_chantable[] = {
71         /* _We_ call this UNII 1 */
72         CHAN5G(5180, 14), /* Channel 36 */
73         CHAN5G(5200, 15), /* Channel 40 */
74         CHAN5G(5220, 16), /* Channel 44 */
75         CHAN5G(5240, 17), /* Channel 48 */
76         /* _We_ call this UNII 2 */
77         CHAN5G(5260, 18), /* Channel 52 */
78         CHAN5G(5280, 19), /* Channel 56 */
79         CHAN5G(5300, 20), /* Channel 60 */
80         CHAN5G(5320, 21), /* Channel 64 */
81         /* _We_ call this "Middle band" */
82         CHAN5G(5500, 22), /* Channel 100 */
83         CHAN5G(5520, 23), /* Channel 104 */
84         CHAN5G(5540, 24), /* Channel 108 */
85         CHAN5G(5560, 25), /* Channel 112 */
86         CHAN5G(5580, 26), /* Channel 116 */
87         CHAN5G(5600, 27), /* Channel 120 */
88         CHAN5G(5620, 28), /* Channel 124 */
89         CHAN5G(5640, 29), /* Channel 128 */
90         CHAN5G(5660, 30), /* Channel 132 */
91         CHAN5G(5680, 31), /* Channel 136 */
92         CHAN5G(5700, 32), /* Channel 140 */
93         /* _We_ call this UNII 3 */
94         CHAN5G(5745, 33), /* Channel 149 */
95         CHAN5G(5765, 34), /* Channel 153 */
96         CHAN5G(5785, 35), /* Channel 157 */
97         CHAN5G(5805, 36), /* Channel 161 */
98         CHAN5G(5825, 37), /* Channel 165 */
99 };
100
101 /* Atheros hardware rate code addition for short premble */
102 #define SHPCHECK(__hw_rate, __flags) \
103         ((__flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) ? (__hw_rate | 0x04 ) : 0)
104
105 #define RATE(_bitrate, _hw_rate, _flags) {              \
106         .bitrate        = (_bitrate),                   \
107         .flags          = (_flags),                     \
108         .hw_value       = (_hw_rate),                   \
109         .hw_value_short = (SHPCHECK(_hw_rate, _flags))  \
110 }
111
112 static struct ath9k_legacy_rate ath9k_legacy_rates[] = {
113         RATE(10, 0x1b, 0),
114         RATE(20, 0x1a, IEEE80211_TX_RC_USE_SHORT_PREAMBLE),
115         RATE(55, 0x19, IEEE80211_TX_RC_USE_SHORT_PREAMBLE),
116         RATE(110, 0x18, IEEE80211_TX_RC_USE_SHORT_PREAMBLE),
117         RATE(60, 0x0b, 0),
118         RATE(90, 0x0f, 0),
119         RATE(120, 0x0a, 0),
120         RATE(180, 0x0e, 0),
121         RATE(240, 0x09, 0),
122         RATE(360, 0x0d, 0),
123         RATE(480, 0x08, 0),
124         RATE(540, 0x0c, 0),
125 };
126
127 static void ath9k_deinit_softc(struct ath_softc *sc);
128
129 /*
130  * Read and write, they both share the same lock. We do this to serialize
131  * reads and writes on Atheros 802.11n PCI devices only. This is required
132  * as the FIFO on these devices can only accept sanely 2 requests.
133  */
134
135 static void ath9k_iowrite32(void *hw_priv, u32 val, u32 reg_offset)
136 {
137         struct ath_hw *ah = (struct ath_hw *) hw_priv;
138         struct ath_common *common = ath9k_hw_common(ah);
139         struct ath_softc *sc = (struct ath_softc *) common->priv;
140
141         writel(val, sc->mem + reg_offset);
142 }
143
144 static unsigned int ath9k_ioread32(void *hw_priv, u32 reg_offset)
145 {
146         struct ath_hw *ah = (struct ath_hw *) hw_priv;
147         struct ath_common *common = ath9k_hw_common(ah);
148         struct ath_softc *sc = (struct ath_softc *) common->priv;
149         u32 val;
150
151         val = readl(sc->mem + reg_offset);
152         return val;
153 }
154
155 static unsigned int ath9k_reg_rmw(void *hw_priv, u32 reg_offset, u32 set, u32 clr)
156 {
157         struct ath_hw *ah = (struct ath_hw *) hw_priv;
158         struct ath_common *common = ath9k_hw_common(ah);
159         struct ath_softc *sc = (struct ath_softc *) common->priv;
160         u32 val;
161
162         val = readl(sc->mem + reg_offset);
163         val &= ~clr;
164         val |= set;
165         writel(val, sc->mem + reg_offset);
166
167         return val;
168 }
169
170 /**************************/
171 /*     Initialization     */
172 /**************************/
173
174 /*
175  *  This function will allocate both the DMA descriptor structure, and the
176  *  buffers it contains.  These are used to contain the descriptors used
177  *  by the system.
178 */
179 int ath_descdma_setup(struct ath_softc *sc, struct ath_descdma *dd,
180                       struct list_head *head, const char *name,
181                       int nbuf, int ndesc, int is_tx)
182 {
183 #define DS2PHYS(_dd, _ds)                                               \
184         ((_dd)->dd_desc_paddr + ((char *)(_ds) - (char *)(_dd)->dd_desc))
185 #define ATH_DESC_4KB_BOUND_CHECK(_daddr) ((((_daddr) & 0xFFF) > 0xF9F) ? 1 : 0)
186         u8 *ds;
187         struct ath_buf *bf;
188         int i, bsize, error, desc_len;
189
190         DBG2("ath9k: %s DMA: %d buffers %d desc/buf\n",
191                 name, nbuf, ndesc);
192
193         INIT_LIST_HEAD(head);
194
195         if (is_tx)
196                 desc_len = sc->sc_ah->caps.tx_desc_len;
197         else
198                 desc_len = sizeof(struct ath_desc);
199
200         /* ath_desc must be a multiple of DWORDs */
201         if ((desc_len % 4) != 0) {
202                 DBG("ath9k: ath_desc not DWORD aligned\n");
203                 error = -ENOMEM;
204                 goto fail;
205         }
206
207         dd->dd_desc_len = desc_len * nbuf * ndesc;
208
209         /*
210          * Need additional DMA memory because we can't use
211          * descriptors that cross the 4K page boundary.
212          * However, iPXE only utilizes 16 buffers, which
213          * will never make up more than half of one page,
214          * so we will only ever skip 1 descriptor, if that.
215          */
216         if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_4KB_SPLITTRANS)) {
217                 u32 ndesc_skipped = 1;
218                 u32 dma_len;
219
220                 dma_len = ndesc_skipped * desc_len;
221                 dd->dd_desc_len += dma_len;
222         }
223
224         /* allocate descriptors */
225         dd->dd_desc = malloc_dma(dd->dd_desc_len, 16);
226         if (dd->dd_desc == NULL) {
227                 error = -ENOMEM;
228                 goto fail;
229         }
230         dd->dd_desc_paddr = virt_to_bus(dd->dd_desc);
231         ds = (u8 *) dd->dd_desc;
232         DBG2("ath9k: %s DMA map: %p (%d) -> %llx (%d)\n",
233                 name, ds, (u32) dd->dd_desc_len,
234                 ito64(dd->dd_desc_paddr), /*XXX*/(u32) dd->dd_desc_len);
235
236         /* allocate buffers */
237         bsize = sizeof(struct ath_buf) * nbuf;
238         bf = zalloc(bsize);
239         if (bf == NULL) {
240                 error = -ENOMEM;
241                 goto fail2;
242         }
243         dd->dd_bufptr = bf;
244
245         for (i = 0; i < nbuf; i++, bf++, ds += (desc_len * ndesc)) {
246                 bf->bf_desc = ds;
247                 bf->bf_daddr = DS2PHYS(dd, ds);
248
249                 if (!(sc->sc_ah->caps.hw_caps &
250                       ATH9K_HW_CAP_4KB_SPLITTRANS)) {
251                         /*
252                          * Skip descriptor addresses which can cause 4KB
253                          * boundary crossing (addr + length) with a 32 dword
254                          * descriptor fetch.
255                          */
256                         while (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr)) {
257                                 ds += (desc_len * ndesc);
258                                 bf->bf_desc = ds;
259                                 bf->bf_daddr = DS2PHYS(dd, ds);
260                         }
261                 }
262                 list_add_tail(&bf->list, head);
263         }
264         return 0;
265 fail2:
266         free_dma(dd->dd_desc, dd->dd_desc_len);
267 fail:
268         memset(dd, 0, sizeof(*dd));
269         return error;
270 #undef ATH_DESC_4KB_BOUND_CHECK
271 #undef DS2PHYS
272 }
273
274 void ath9k_init_crypto(struct ath_softc *sc)
275 {
276         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
277         unsigned int i = 0;
278
279         /* Get the hardware key cache size. */
280         common->keymax = AR_KEYTABLE_SIZE;
281
282         /*
283          * Reset the key cache since some parts do not
284          * reset the contents on initial power up.
285          */
286         for (i = 0; i < common->keymax; i++)
287                 ath_hw_keyreset(common, (u16) i);
288
289         /*
290          * Check whether the separate key cache entries
291          * are required to handle both tx+rx MIC keys.
292          * With split mic keys the number of stations is limited
293          * to 27 otherwise 59.
294          */
295         if (sc->sc_ah->misc_mode & AR_PCU_MIC_NEW_LOC_ENA)
296                 common->crypt_caps |= ATH_CRYPT_CAP_MIC_COMBINED;
297 }
298
299 static int ath9k_init_queues(struct ath_softc *sc)
300 {
301         int i = 0;
302
303         for (i = 0; i < WME_NUM_AC; i++) {
304                 sc->tx.txq_map[i] = ath_txq_setup(sc, ATH9K_TX_QUEUE_DATA, i);
305                 sc->tx.txq_map[i]->mac80211_qnum = i;
306         }
307         return 0;
308 }
309
310 static int ath9k_init_channels_rates(struct ath_softc *sc)
311 {
312         unsigned int i;
313
314         memcpy(&sc->rates, ath9k_legacy_rates, sizeof(ath9k_legacy_rates));
315
316         if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ) {
317                 memcpy(&sc->hwinfo->channels[sc->hwinfo->nr_channels], ath9k_2ghz_chantable, sizeof(ath9k_2ghz_chantable));
318
319                 sc->hwinfo->nr_channels += ARRAY_SIZE(ath9k_2ghz_chantable);
320
321                 for (i = 0; i < ARRAY_SIZE(ath9k_legacy_rates); i++)
322                         sc->hwinfo->rates[NET80211_BAND_2GHZ][i] = ath9k_legacy_rates[i].bitrate;
323                 sc->hwinfo->nr_rates[NET80211_BAND_2GHZ] = ARRAY_SIZE(ath9k_legacy_rates);
324         }
325
326         if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ) {
327                 memcpy(&sc->hwinfo->channels[sc->hwinfo->nr_channels], ath9k_5ghz_chantable, sizeof(ath9k_5ghz_chantable));
328
329                 sc->hwinfo->nr_channels += ARRAY_SIZE(ath9k_5ghz_chantable);
330
331                 for (i = 4; i < ARRAY_SIZE(ath9k_legacy_rates); i++)
332                         sc->hwinfo->rates[NET80211_BAND_5GHZ][i - 4] = ath9k_legacy_rates[i].bitrate;
333                 sc->hwinfo->nr_rates[NET80211_BAND_5GHZ] = ARRAY_SIZE(ath9k_legacy_rates) - 4;
334         }
335         return 0;
336 }
337
338 static void ath9k_init_misc(struct ath_softc *sc)
339 {
340         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
341
342         common->ani.timer = 0;
343
344         sc->config.txpowlimit = ATH_TXPOWER_MAX;
345
346         common->tx_chainmask = sc->sc_ah->caps.tx_chainmask;
347         common->rx_chainmask = sc->sc_ah->caps.rx_chainmask;
348
349         ath9k_hw_set_diversity(sc->sc_ah, 1);
350         sc->rx.defant = ath9k_hw_getdefantenna(sc->sc_ah);
351
352         memcpy(common->bssidmask, ath_bcast_mac, ETH_ALEN);
353 }
354
355 static int ath9k_init_softc(u16 devid, struct ath_softc *sc, u16 subsysid,
356                             const struct ath_bus_ops *bus_ops)
357 {
358         struct ath_hw *ah = NULL;
359         struct ath_common *common;
360         int ret = 0, i;
361         int csz = 0;
362
363         ah = zalloc(sizeof(struct ath_hw));
364         if (!ah)
365                 return -ENOMEM;
366
367         ah->dev = sc->dev;
368         ah->hw_version.devid = devid;
369         ah->hw_version.subsysid = subsysid;
370         ah->reg_ops.read = ath9k_ioread32;
371         ah->reg_ops.write = ath9k_iowrite32;
372         ah->reg_ops.rmw = ath9k_reg_rmw;
373         sc->sc_ah = ah;
374
375         sc->hwinfo = zalloc(sizeof(*sc->hwinfo));
376         if (!sc->hwinfo) {
377                 DBG("ath9k: cannot allocate 802.11 hardware info structure\n");
378                 return -ENOMEM;
379         }
380
381         ah->ah_flags |= AH_USE_EEPROM;
382         sc->sc_ah->led_pin = -1;
383
384         common = ath9k_hw_common(ah);
385         common->ops = &ah->reg_ops;
386         common->bus_ops = bus_ops;
387         common->ah = ah;
388         common->dev = sc->dev;
389         common->priv = sc;
390
391         sc->intr_tq = ath9k_tasklet;
392
393         /*
394          * Cache line size is used to size and align various
395          * structures used to communicate with the hardware.
396          */
397         ath_read_cachesize(common, &csz);
398         common->cachelsz = csz << 2; /* convert to bytes */
399
400         /* Initializes the hardware for all supported chipsets */
401         ret = ath9k_hw_init(ah);
402         if (ret)
403                 goto err_hw;
404
405         memcpy(sc->hwinfo->hwaddr, common->macaddr, ETH_ALEN);
406
407         ret = ath9k_init_queues(sc);
408         if (ret)
409                 goto err_queues;
410
411         ret = ath9k_init_channels_rates(sc);
412         if (ret)
413                 goto err_btcoex;
414
415         ath9k_init_crypto(sc);
416         ath9k_init_misc(sc);
417
418         return 0;
419
420 err_btcoex:
421         for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
422                 if (ATH_TXQ_SETUP(sc, i))
423                         ath_tx_cleanupq(sc, &sc->tx.txq[i]);
424 err_queues:
425         ath9k_hw_deinit(ah);
426 err_hw:
427         free(sc->hwinfo);
428         sc->hwinfo = NULL;
429
430         free(ah);
431         sc->sc_ah = NULL;
432
433         return ret;
434 }
435
436 static void ath9k_init_band_txpower(struct ath_softc *sc, int band)
437 {
438         struct net80211_channel *chan;
439         struct ath_hw *ah = sc->sc_ah;
440         struct ath_regulatory *reg = ath9k_hw_regulatory(ah);
441         int i;
442
443         for (i = 0; i < sc->hwinfo->nr_channels; i++) {
444                 chan = &sc->hwinfo->channels[i];
445                 if(chan->band != band)
446                         continue;
447                 ah->curchan = &ah->channels[chan->hw_value];
448                 ath9k_hw_set_txpowerlimit(ah, MAX_RATE_POWER, 1);
449                 chan->maxpower = reg->max_power_level / 2;
450         }
451 }
452
453 static void ath9k_init_txpower_limits(struct ath_softc *sc)
454 {
455         struct ath_hw *ah = sc->sc_ah;
456         struct ath9k_channel *curchan = ah->curchan;
457
458         if (ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ)
459                 ath9k_init_band_txpower(sc, NET80211_BAND_2GHZ);
460         if (ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ)
461                 ath9k_init_band_txpower(sc, NET80211_BAND_5GHZ);
462
463         ah->curchan = curchan;
464 }
465
466 void ath9k_set_hw_capab(struct ath_softc *sc, struct net80211_device *dev __unused)
467 {
468         sc->hwinfo->flags = NET80211_HW_RX_HAS_FCS;
469         sc->hwinfo->signal_type = NET80211_SIGNAL_DB;
470         sc->hwinfo->signal_max = 40; /* 35dB should give perfect 54Mbps */
471         sc->hwinfo->channel_change_time = 5000;
472
473         if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ)
474         {
475                 sc->hwinfo->bands |= NET80211_BAND_BIT_2GHZ;
476                 sc->hwinfo->modes |= NET80211_MODE_B | NET80211_MODE_G;
477         }
478         if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ)
479         {
480                 sc->hwinfo->bands |= NET80211_BAND_BIT_5GHZ;
481                 sc->hwinfo->modes |= NET80211_MODE_A;
482         }
483 }
484
485 int ath9k_init_device(u16 devid, struct ath_softc *sc, u16 subsysid,
486                     const struct ath_bus_ops *bus_ops)
487 {
488         struct net80211_device *dev = sc->dev;
489         /*struct ath_common *common;
490         struct ath_hw *ah;*/
491         int error = 0;
492         /*struct ath_regulatory *reg;*/
493
494         /* Bring up device */
495         error = ath9k_init_softc(devid, sc, subsysid, bus_ops);
496         if (error != 0)
497                 goto error_init;
498
499         /*ah = sc->sc_ah;
500         common = ath9k_hw_common(ah);*/
501         ath9k_set_hw_capab(sc, dev);
502         /* TODO Cottsay: reg */
503         /* Initialize regulatory */
504         /*error = ath_regd_init(&common->regulatory, sc->dev->wiphy,
505                               ath9k_reg_notifier);
506         if (error)
507                 goto error_regd;
508
509         reg = &common->regulatory;*/
510
511         /* Setup TX DMA */
512         error = ath_tx_init(sc, ATH_TXBUF);
513         if (error != 0)
514                 goto error_tx;
515
516         /* Setup RX DMA */
517         error = ath_rx_init(sc, ATH_RXBUF);
518         if (error != 0)
519                 goto error_rx;
520
521         ath9k_init_txpower_limits(sc);
522
523         /* Register with mac80211 */
524         error = net80211_register(dev, &ath9k_ops, sc->hwinfo);
525         if (error)
526                 goto error_register;
527
528         /* TODO Cottsay: reg */
529         /* Handle world regulatory */
530         /*if (!ath_is_world_regd(reg)) {
531                 error = regulatory_hint(hw->wiphy, reg->alpha2);
532                 if (error)
533                         goto error_world;
534         }*/
535
536         sc->hw_pll_work = ath_hw_pll_work;
537         sc->last_rssi = ATH_RSSI_DUMMY_MARKER;
538
539         /* TODO Cottsay: rfkill */
540         /*ath_start_rfkill_poll(sc);*/
541
542         return 0;
543
544 //error_world:
545 //      net80211_unregister(dev);
546 error_register:
547         ath_rx_cleanup(sc);
548 error_rx:
549         ath_tx_cleanup(sc);
550 error_tx:
551         ath9k_deinit_softc(sc);
552 error_init:
553         return error;
554 }
555
556 /*****************************/
557 /*     De-Initialization     */
558 /*****************************/
559
560 static void ath9k_deinit_softc(struct ath_softc *sc)
561 {
562         int i = 0;
563
564         for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
565                 if (ATH_TXQ_SETUP(sc, i))
566                         ath_tx_cleanupq(sc, &sc->tx.txq[i]);
567
568         ath9k_hw_deinit(sc->sc_ah);
569
570         free(sc->hwinfo);
571         sc->hwinfo = NULL;
572         free(sc->sc_ah);
573         sc->sc_ah = NULL;
574 }
575
576 void ath9k_deinit_device(struct ath_softc *sc)
577 {
578         struct net80211_device *dev = sc->dev;
579
580         net80211_unregister(dev);
581         ath_rx_cleanup(sc);
582         ath_tx_cleanup(sc);
583         ath9k_deinit_softc(sc);
584 }
585
586 void ath_descdma_cleanup(struct ath_softc *sc __unused,
587                          struct ath_descdma *dd,
588                          struct list_head *head)
589 {
590         free_dma(dd->dd_desc, dd->dd_desc_len);
591
592         INIT_LIST_HEAD(head);
593         free(dd->dd_bufptr);
594         memset(dd, 0, sizeof(*dd));
595 }