You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ath5k.c 43KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698
  1. /*
  2. * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
  3. * Copyright (c) 2004-2005 Atheros Communications, Inc.
  4. * Copyright (c) 2006 Devicescape Software, Inc.
  5. * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
  6. * Copyright (c) 2007 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
  7. *
  8. * Modified for iPXE, July 2009, by Joshua Oreman <oremanj@rwcr.net>
  9. * Original from Linux kernel 2.6.30.
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer,
  18. * without modification.
  19. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  20. * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
  21. * redistribution must be conditioned upon including a substantially
  22. * similar Disclaimer requirement for further binary redistribution.
  23. * 3. Neither the names of the above-listed copyright holders nor the names
  24. * of any contributors may be used to endorse or promote products derived
  25. * from this software without specific prior written permission.
  26. *
  27. * Alternatively, this software may be distributed under the terms of the
  28. * GNU General Public License ("GPL") version 2 as published by the Free
  29. * Software Foundation.
  30. *
  31. * NO WARRANTY
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  33. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  34. * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
  35. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  36. * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
  37. * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  38. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  39. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  40. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  42. * THE POSSIBILITY OF SUCH DAMAGES.
  43. *
  44. */
  45. FILE_LICENCE ( BSD3 );
  46. #include <stdlib.h>
  47. #include <ipxe/malloc.h>
  48. #include <ipxe/timer.h>
  49. #include <ipxe/netdevice.h>
  50. #include <ipxe/pci.h>
  51. #include <ipxe/pci_io.h>
  52. #include "base.h"
  53. #include "reg.h"
  54. #define ATH5K_CALIB_INTERVAL 10 /* Calibrate PHY every 10 seconds */
  55. #define ATH5K_RETRIES 4 /* Number of times to retry packet sends */
  56. #define ATH5K_DESC_ALIGN 16 /* Alignment for TX/RX descriptors */
  57. /******************\
  58. * Internal defines *
  59. \******************/
  60. /* Known PCI ids */
  61. static struct pci_device_id ath5k_nics[] = {
  62. PCI_ROM(0x168c, 0x0207, "ath5210e", "Atheros 5210 early", AR5K_AR5210),
  63. PCI_ROM(0x168c, 0x0007, "ath5210", "Atheros 5210", AR5K_AR5210),
  64. PCI_ROM(0x168c, 0x0011, "ath5311", "Atheros 5311 (AHB)", AR5K_AR5211),
  65. PCI_ROM(0x168c, 0x0012, "ath5211", "Atheros 5211", AR5K_AR5211),
  66. PCI_ROM(0x168c, 0x0013, "ath5212", "Atheros 5212", AR5K_AR5212),
  67. PCI_ROM(0xa727, 0x0013, "ath5212c","3com Ath 5212", AR5K_AR5212),
  68. PCI_ROM(0x10b7, 0x0013, "rdag675", "3com 3CRDAG675", AR5K_AR5212),
  69. PCI_ROM(0x168c, 0x1014, "ath5212m", "Ath 5212 miniPCI", AR5K_AR5212),
  70. PCI_ROM(0x168c, 0x0014, "ath5212x14", "Atheros 5212 x14", AR5K_AR5212),
  71. PCI_ROM(0x168c, 0x0015, "ath5212x15", "Atheros 5212 x15", AR5K_AR5212),
  72. PCI_ROM(0x168c, 0x0016, "ath5212x16", "Atheros 5212 x16", AR5K_AR5212),
  73. PCI_ROM(0x168c, 0x0017, "ath5212x17", "Atheros 5212 x17", AR5K_AR5212),
  74. PCI_ROM(0x168c, 0x0018, "ath5212x18", "Atheros 5212 x18", AR5K_AR5212),
  75. PCI_ROM(0x168c, 0x0019, "ath5212x19", "Atheros 5212 x19", AR5K_AR5212),
  76. PCI_ROM(0x168c, 0x001a, "ath2413", "Atheros 2413 Griffin", AR5K_AR5212),
  77. PCI_ROM(0x168c, 0x001b, "ath5413", "Atheros 5413 Eagle", AR5K_AR5212),
  78. PCI_ROM(0x168c, 0x001c, "ath5212e", "Atheros 5212 PCI-E", AR5K_AR5212),
  79. PCI_ROM(0x168c, 0x001d, "ath2417", "Atheros 2417 Nala", AR5K_AR5212),
  80. };
  81. /* Known SREVs */
  82. static const struct ath5k_srev_name srev_names[] = {
  83. { "5210", AR5K_VERSION_MAC, AR5K_SREV_AR5210 },
  84. { "5311", AR5K_VERSION_MAC, AR5K_SREV_AR5311 },
  85. { "5311A", AR5K_VERSION_MAC, AR5K_SREV_AR5311A },
  86. { "5311B", AR5K_VERSION_MAC, AR5K_SREV_AR5311B },
  87. { "5211", AR5K_VERSION_MAC, AR5K_SREV_AR5211 },
  88. { "5212", AR5K_VERSION_MAC, AR5K_SREV_AR5212 },
  89. { "5213", AR5K_VERSION_MAC, AR5K_SREV_AR5213 },
  90. { "5213A", AR5K_VERSION_MAC, AR5K_SREV_AR5213A },
  91. { "2413", AR5K_VERSION_MAC, AR5K_SREV_AR2413 },
  92. { "2414", AR5K_VERSION_MAC, AR5K_SREV_AR2414 },
  93. { "5424", AR5K_VERSION_MAC, AR5K_SREV_AR5424 },
  94. { "5413", AR5K_VERSION_MAC, AR5K_SREV_AR5413 },
  95. { "5414", AR5K_VERSION_MAC, AR5K_SREV_AR5414 },
  96. { "2415", AR5K_VERSION_MAC, AR5K_SREV_AR2415 },
  97. { "5416", AR5K_VERSION_MAC, AR5K_SREV_AR5416 },
  98. { "5418", AR5K_VERSION_MAC, AR5K_SREV_AR5418 },
  99. { "2425", AR5K_VERSION_MAC, AR5K_SREV_AR2425 },
  100. { "2417", AR5K_VERSION_MAC, AR5K_SREV_AR2417 },
  101. { "xxxxx", AR5K_VERSION_MAC, AR5K_SREV_UNKNOWN },
  102. { "5110", AR5K_VERSION_RAD, AR5K_SREV_RAD_5110 },
  103. { "5111", AR5K_VERSION_RAD, AR5K_SREV_RAD_5111 },
  104. { "5111A", AR5K_VERSION_RAD, AR5K_SREV_RAD_5111A },
  105. { "2111", AR5K_VERSION_RAD, AR5K_SREV_RAD_2111 },
  106. { "5112", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112 },
  107. { "5112A", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112A },
  108. { "5112B", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112B },
  109. { "2112", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112 },
  110. { "2112A", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112A },
  111. { "2112B", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112B },
  112. { "2413", AR5K_VERSION_RAD, AR5K_SREV_RAD_2413 },
  113. { "5413", AR5K_VERSION_RAD, AR5K_SREV_RAD_5413 },
  114. { "2316", AR5K_VERSION_RAD, AR5K_SREV_RAD_2316 },
  115. { "2317", AR5K_VERSION_RAD, AR5K_SREV_RAD_2317 },
  116. { "5424", AR5K_VERSION_RAD, AR5K_SREV_RAD_5424 },
  117. { "5133", AR5K_VERSION_RAD, AR5K_SREV_RAD_5133 },
  118. { "xxxxx", AR5K_VERSION_RAD, AR5K_SREV_UNKNOWN },
  119. };
  120. #define ATH5K_SPMBL_NO 1
  121. #define ATH5K_SPMBL_YES 2
  122. #define ATH5K_SPMBL_BOTH 3
  123. static const struct {
  124. u16 bitrate;
  125. u8 short_pmbl;
  126. u8 hw_code;
  127. } ath5k_rates[] = {
  128. { 10, ATH5K_SPMBL_BOTH, ATH5K_RATE_CODE_1M },
  129. { 20, ATH5K_SPMBL_NO, ATH5K_RATE_CODE_2M },
  130. { 55, ATH5K_SPMBL_NO, ATH5K_RATE_CODE_5_5M },
  131. { 110, ATH5K_SPMBL_NO, ATH5K_RATE_CODE_11M },
  132. { 60, ATH5K_SPMBL_BOTH, ATH5K_RATE_CODE_6M },
  133. { 90, ATH5K_SPMBL_BOTH, ATH5K_RATE_CODE_9M },
  134. { 120, ATH5K_SPMBL_BOTH, ATH5K_RATE_CODE_12M },
  135. { 180, ATH5K_SPMBL_BOTH, ATH5K_RATE_CODE_18M },
  136. { 240, ATH5K_SPMBL_BOTH, ATH5K_RATE_CODE_24M },
  137. { 360, ATH5K_SPMBL_BOTH, ATH5K_RATE_CODE_36M },
  138. { 480, ATH5K_SPMBL_BOTH, ATH5K_RATE_CODE_48M },
  139. { 540, ATH5K_SPMBL_BOTH, ATH5K_RATE_CODE_54M },
  140. { 20, ATH5K_SPMBL_YES, ATH5K_RATE_CODE_2M | AR5K_SET_SHORT_PREAMBLE },
  141. { 55, ATH5K_SPMBL_YES, ATH5K_RATE_CODE_5_5M | AR5K_SET_SHORT_PREAMBLE },
  142. { 110, ATH5K_SPMBL_YES, ATH5K_RATE_CODE_11M | AR5K_SET_SHORT_PREAMBLE },
  143. { 0, 0, 0 },
  144. };
  145. #define ATH5K_NR_RATES 15
  146. /*
  147. * Prototypes - PCI stack related functions
  148. */
  149. static int ath5k_probe(struct pci_device *pdev);
  150. static void ath5k_remove(struct pci_device *pdev);
  151. struct pci_driver ath5k_pci_driver __pci_driver = {
  152. .ids = ath5k_nics,
  153. .id_count = sizeof(ath5k_nics) / sizeof(ath5k_nics[0]),
  154. .probe = ath5k_probe,
  155. .remove = ath5k_remove,
  156. };
  157. /*
  158. * Prototypes - MAC 802.11 stack related functions
  159. */
  160. static int ath5k_tx(struct net80211_device *dev, struct io_buffer *skb);
  161. static int ath5k_reset(struct ath5k_softc *sc, struct net80211_channel *chan);
  162. static int ath5k_reset_wake(struct ath5k_softc *sc);
  163. static int ath5k_start(struct net80211_device *dev);
  164. static void ath5k_stop(struct net80211_device *dev);
  165. static int ath5k_config(struct net80211_device *dev, int changed);
  166. static void ath5k_poll(struct net80211_device *dev);
  167. static void ath5k_irq(struct net80211_device *dev, int enable);
  168. static struct net80211_device_operations ath5k_ops = {
  169. .open = ath5k_start,
  170. .close = ath5k_stop,
  171. .transmit = ath5k_tx,
  172. .poll = ath5k_poll,
  173. .irq = ath5k_irq,
  174. .config = ath5k_config,
  175. };
  176. /*
  177. * Prototypes - Internal functions
  178. */
  179. /* Attach detach */
  180. static int ath5k_attach(struct net80211_device *dev);
  181. static void ath5k_detach(struct net80211_device *dev);
  182. /* Channel/mode setup */
  183. static unsigned int ath5k_copy_channels(struct ath5k_hw *ah,
  184. struct net80211_channel *channels,
  185. unsigned int mode,
  186. unsigned int max);
  187. static int ath5k_setup_bands(struct net80211_device *dev);
  188. static int ath5k_chan_set(struct ath5k_softc *sc,
  189. struct net80211_channel *chan);
  190. static void ath5k_setcurmode(struct ath5k_softc *sc,
  191. unsigned int mode);
  192. static void ath5k_mode_setup(struct ath5k_softc *sc);
  193. /* Descriptor setup */
  194. static int ath5k_desc_alloc(struct ath5k_softc *sc);
  195. static void ath5k_desc_free(struct ath5k_softc *sc);
  196. /* Buffers setup */
  197. static int ath5k_rxbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf);
  198. static int ath5k_txbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf);
  199. static inline void ath5k_txbuf_free(struct ath5k_softc *sc,
  200. struct ath5k_buf *bf)
  201. {
  202. if (!bf->iob)
  203. return;
  204. net80211_tx_complete(sc->dev, bf->iob, 0, ECANCELED);
  205. bf->iob = NULL;
  206. }
  207. static inline void ath5k_rxbuf_free(struct ath5k_softc *sc __unused,
  208. struct ath5k_buf *bf)
  209. {
  210. free_iob(bf->iob);
  211. bf->iob = NULL;
  212. }
  213. /* Queues setup */
  214. static int ath5k_txq_setup(struct ath5k_softc *sc,
  215. int qtype, int subtype);
  216. static void ath5k_txq_drainq(struct ath5k_softc *sc,
  217. struct ath5k_txq *txq);
  218. static void ath5k_txq_cleanup(struct ath5k_softc *sc);
  219. static void ath5k_txq_release(struct ath5k_softc *sc);
  220. /* Rx handling */
  221. static int ath5k_rx_start(struct ath5k_softc *sc);
  222. static void ath5k_rx_stop(struct ath5k_softc *sc);
  223. /* Tx handling */
  224. static void ath5k_tx_processq(struct ath5k_softc *sc,
  225. struct ath5k_txq *txq);
  226. /* Interrupt handling */
  227. static int ath5k_init(struct ath5k_softc *sc);
  228. static int ath5k_stop_hw(struct ath5k_softc *sc);
  229. static void ath5k_calibrate(struct ath5k_softc *sc);
  230. /* Filter */
  231. static void ath5k_configure_filter(struct ath5k_softc *sc);
  232. /********************\
  233. * PCI Initialization *
  234. \********************/
  235. #if DBGLVL_MAX
  236. static const char *
  237. ath5k_chip_name(enum ath5k_srev_type type, u16 val)
  238. {
  239. const char *name = "xxxxx";
  240. unsigned int i;
  241. for (i = 0; i < ARRAY_SIZE(srev_names); i++) {
  242. if (srev_names[i].sr_type != type)
  243. continue;
  244. if ((val & 0xf0) == srev_names[i].sr_val)
  245. name = srev_names[i].sr_name;
  246. if ((val & 0xff) == srev_names[i].sr_val) {
  247. name = srev_names[i].sr_name;
  248. break;
  249. }
  250. }
  251. return name;
  252. }
  253. #endif
  254. static int ath5k_probe(struct pci_device *pdev)
  255. {
  256. void *mem;
  257. struct ath5k_softc *sc;
  258. struct net80211_device *dev;
  259. int ret;
  260. u8 csz;
  261. adjust_pci_device(pdev);
  262. /*
  263. * Cache line size is used to size and align various
  264. * structures used to communicate with the hardware.
  265. */
  266. pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
  267. if (csz == 0) {
  268. /*
  269. * We must have this setup properly for rx buffer
  270. * DMA to work so force a reasonable value here if it
  271. * comes up zero.
  272. */
  273. csz = 16;
  274. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
  275. }
  276. /*
  277. * The default setting of latency timer yields poor results,
  278. * set it to the value used by other systems. It may be worth
  279. * tweaking this setting more.
  280. */
  281. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
  282. /*
  283. * Disable the RETRY_TIMEOUT register (0x41) to keep
  284. * PCI Tx retries from interfering with C3 CPU state.
  285. */
  286. pci_write_config_byte(pdev, 0x41, 0);
  287. mem = ioremap(pdev->membase, 0x10000);
  288. if (!mem) {
  289. DBG("ath5k: cannot remap PCI memory region\n");
  290. ret = -EIO;
  291. goto err;
  292. }
  293. /*
  294. * Allocate dev (net80211 main struct)
  295. * and dev->priv (driver private data)
  296. */
  297. dev = net80211_alloc(sizeof(*sc));
  298. if (!dev) {
  299. DBG("ath5k: cannot allocate 802.11 device\n");
  300. ret = -ENOMEM;
  301. goto err_map;
  302. }
  303. /* Initialize driver private data */
  304. sc = dev->priv;
  305. sc->dev = dev;
  306. sc->pdev = pdev;
  307. sc->hwinfo = zalloc(sizeof(*sc->hwinfo));
  308. if (!sc->hwinfo) {
  309. DBG("ath5k: cannot allocate 802.11 hardware info structure\n");
  310. ret = -ENOMEM;
  311. goto err_free;
  312. }
  313. sc->hwinfo->flags = NET80211_HW_RX_HAS_FCS;
  314. sc->hwinfo->signal_type = NET80211_SIGNAL_DB;
  315. sc->hwinfo->signal_max = 40; /* 35dB should give perfect 54Mbps */
  316. sc->hwinfo->channel_change_time = 5000;
  317. /* Avoid working with the device until setup is complete */
  318. sc->status |= ATH_STAT_INVALID;
  319. sc->iobase = mem;
  320. sc->cachelsz = csz * 4; /* convert to bytes */
  321. DBG("ath5k: register base at %p (%08lx)\n", sc->iobase, pdev->membase);
  322. DBG("ath5k: cache line size %d\n", sc->cachelsz);
  323. /* Set private data */
  324. pci_set_drvdata(pdev, dev);
  325. dev->netdev->dev = (struct device *)pdev;
  326. /* Initialize device */
  327. ret = ath5k_hw_attach(sc, pdev->id->driver_data, &sc->ah);
  328. if (ret)
  329. goto err_free_hwinfo;
  330. /* Finish private driver data initialization */
  331. ret = ath5k_attach(dev);
  332. if (ret)
  333. goto err_ah;
  334. #if DBGLVL_MAX
  335. DBG("Atheros AR%s chip found (MAC: 0x%x, PHY: 0x%x)\n",
  336. ath5k_chip_name(AR5K_VERSION_MAC, sc->ah->ah_mac_srev),
  337. sc->ah->ah_mac_srev, sc->ah->ah_phy_revision);
  338. if (!sc->ah->ah_single_chip) {
  339. /* Single chip radio (!RF5111) */
  340. if (sc->ah->ah_radio_5ghz_revision &&
  341. !sc->ah->ah_radio_2ghz_revision) {
  342. /* No 5GHz support -> report 2GHz radio */
  343. if (!(sc->ah->ah_capabilities.cap_mode & AR5K_MODE_BIT_11A)) {
  344. DBG("RF%s 2GHz radio found (0x%x)\n",
  345. ath5k_chip_name(AR5K_VERSION_RAD,
  346. sc->ah->ah_radio_5ghz_revision),
  347. sc->ah->ah_radio_5ghz_revision);
  348. /* No 2GHz support (5110 and some
  349. * 5Ghz only cards) -> report 5Ghz radio */
  350. } else if (!(sc->ah->ah_capabilities.cap_mode & AR5K_MODE_BIT_11B)) {
  351. DBG("RF%s 5GHz radio found (0x%x)\n",
  352. ath5k_chip_name(AR5K_VERSION_RAD,
  353. sc->ah->ah_radio_5ghz_revision),
  354. sc->ah->ah_radio_5ghz_revision);
  355. /* Multiband radio */
  356. } else {
  357. DBG("RF%s multiband radio found (0x%x)\n",
  358. ath5k_chip_name(AR5K_VERSION_RAD,
  359. sc->ah->ah_radio_5ghz_revision),
  360. sc->ah->ah_radio_5ghz_revision);
  361. }
  362. }
  363. /* Multi chip radio (RF5111 - RF2111) ->
  364. * report both 2GHz/5GHz radios */
  365. else if (sc->ah->ah_radio_5ghz_revision &&
  366. sc->ah->ah_radio_2ghz_revision) {
  367. DBG("RF%s 5GHz radio found (0x%x)\n",
  368. ath5k_chip_name(AR5K_VERSION_RAD,
  369. sc->ah->ah_radio_5ghz_revision),
  370. sc->ah->ah_radio_5ghz_revision);
  371. DBG("RF%s 2GHz radio found (0x%x)\n",
  372. ath5k_chip_name(AR5K_VERSION_RAD,
  373. sc->ah->ah_radio_2ghz_revision),
  374. sc->ah->ah_radio_2ghz_revision);
  375. }
  376. }
  377. #endif
  378. /* Ready to go */
  379. sc->status &= ~ATH_STAT_INVALID;
  380. return 0;
  381. err_ah:
  382. ath5k_hw_detach(sc->ah);
  383. err_free_hwinfo:
  384. free(sc->hwinfo);
  385. err_free:
  386. net80211_free(dev);
  387. err_map:
  388. iounmap(mem);
  389. err:
  390. return ret;
  391. }
  392. static void ath5k_remove(struct pci_device *pdev)
  393. {
  394. struct net80211_device *dev = pci_get_drvdata(pdev);
  395. struct ath5k_softc *sc = dev->priv;
  396. ath5k_detach(dev);
  397. ath5k_hw_detach(sc->ah);
  398. iounmap(sc->iobase);
  399. free(sc->hwinfo);
  400. net80211_free(dev);
  401. }
  402. /***********************\
  403. * Driver Initialization *
  404. \***********************/
  405. static int
  406. ath5k_attach(struct net80211_device *dev)
  407. {
  408. struct ath5k_softc *sc = dev->priv;
  409. struct ath5k_hw *ah = sc->ah;
  410. int ret;
  411. /*
  412. * Collect the channel list. The 802.11 layer
  413. * is resposible for filtering this list based
  414. * on settings like the phy mode and regulatory
  415. * domain restrictions.
  416. */
  417. ret = ath5k_setup_bands(dev);
  418. if (ret) {
  419. DBG("ath5k: can't get channels\n");
  420. goto err;
  421. }
  422. /* NB: setup here so ath5k_rate_update is happy */
  423. if (ah->ah_modes & AR5K_MODE_BIT_11A)
  424. ath5k_setcurmode(sc, AR5K_MODE_11A);
  425. else
  426. ath5k_setcurmode(sc, AR5K_MODE_11B);
  427. /*
  428. * Allocate tx+rx descriptors and populate the lists.
  429. */
  430. ret = ath5k_desc_alloc(sc);
  431. if (ret) {
  432. DBG("ath5k: can't allocate descriptors\n");
  433. goto err;
  434. }
  435. /*
  436. * Allocate hardware transmit queues. Note that hw functions
  437. * handle reseting these queues at the needed time.
  438. */
  439. ret = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BE);
  440. if (ret) {
  441. DBG("ath5k: can't setup xmit queue\n");
  442. goto err_desc;
  443. }
  444. sc->last_calib_ticks = currticks();
  445. ret = ath5k_eeprom_read_mac(ah, sc->hwinfo->hwaddr);
  446. if (ret) {
  447. DBG("ath5k: unable to read address from EEPROM: 0x%04x\n",
  448. sc->pdev->device);
  449. goto err_queues;
  450. }
  451. memset(sc->bssidmask, 0xff, ETH_ALEN);
  452. ath5k_hw_set_bssid_mask(sc->ah, sc->bssidmask);
  453. ret = net80211_register(sc->dev, &ath5k_ops, sc->hwinfo);
  454. if (ret) {
  455. DBG("ath5k: can't register ieee80211 hw\n");
  456. goto err_queues;
  457. }
  458. return 0;
  459. err_queues:
  460. ath5k_txq_release(sc);
  461. err_desc:
  462. ath5k_desc_free(sc);
  463. err:
  464. return ret;
  465. }
  466. static void
  467. ath5k_detach(struct net80211_device *dev)
  468. {
  469. struct ath5k_softc *sc = dev->priv;
  470. net80211_unregister(dev);
  471. ath5k_desc_free(sc);
  472. ath5k_txq_release(sc);
  473. }
  474. /********************\
  475. * Channel/mode setup *
  476. \********************/
  477. /*
  478. * Convert IEEE channel number to MHz frequency.
  479. */
  480. static inline short
  481. ath5k_ieee2mhz(short chan)
  482. {
  483. if (chan < 14)
  484. return 2407 + 5 * chan;
  485. if (chan == 14)
  486. return 2484;
  487. if (chan < 27)
  488. return 2212 + 20 * chan;
  489. return 5000 + 5 * chan;
  490. }
  491. static unsigned int
  492. ath5k_copy_channels(struct ath5k_hw *ah,
  493. struct net80211_channel *channels,
  494. unsigned int mode, unsigned int max)
  495. {
  496. unsigned int i, count, size, chfreq, freq, ch;
  497. if (!(ah->ah_modes & (1 << mode)))
  498. return 0;
  499. switch (mode) {
  500. case AR5K_MODE_11A:
  501. case AR5K_MODE_11A_TURBO:
  502. /* 1..220, but 2GHz frequencies are filtered by check_channel */
  503. size = 220;
  504. chfreq = CHANNEL_5GHZ;
  505. break;
  506. case AR5K_MODE_11B:
  507. case AR5K_MODE_11G:
  508. case AR5K_MODE_11G_TURBO:
  509. size = 26;
  510. chfreq = CHANNEL_2GHZ;
  511. break;
  512. default:
  513. return 0;
  514. }
  515. for (i = 0, count = 0; i < size && max > 0; i++) {
  516. ch = i + 1 ;
  517. freq = ath5k_ieee2mhz(ch);
  518. /* Check if channel is supported by the chipset */
  519. if (!ath5k_channel_ok(ah, freq, chfreq))
  520. continue;
  521. /* Write channel info and increment counter */
  522. channels[count].center_freq = freq;
  523. channels[count].maxpower = 0; /* use regulatory */
  524. channels[count].band = (chfreq == CHANNEL_2GHZ) ?
  525. NET80211_BAND_2GHZ : NET80211_BAND_5GHZ;
  526. switch (mode) {
  527. case AR5K_MODE_11A:
  528. case AR5K_MODE_11G:
  529. channels[count].hw_value = chfreq | CHANNEL_OFDM;
  530. break;
  531. case AR5K_MODE_11A_TURBO:
  532. case AR5K_MODE_11G_TURBO:
  533. channels[count].hw_value = chfreq |
  534. CHANNEL_OFDM | CHANNEL_TURBO;
  535. break;
  536. case AR5K_MODE_11B:
  537. channels[count].hw_value = CHANNEL_B;
  538. }
  539. count++;
  540. max--;
  541. }
  542. return count;
  543. }
  544. static int
  545. ath5k_setup_bands(struct net80211_device *dev)
  546. {
  547. struct ath5k_softc *sc = dev->priv;
  548. struct ath5k_hw *ah = sc->ah;
  549. int max_c, count_c = 0;
  550. int i;
  551. int band;
  552. max_c = sizeof(sc->hwinfo->channels) / sizeof(sc->hwinfo->channels[0]);
  553. /* 2GHz band */
  554. if (sc->ah->ah_capabilities.cap_mode & AR5K_MODE_BIT_11G) {
  555. /* G mode */
  556. band = NET80211_BAND_2GHZ;
  557. sc->hwinfo->bands = NET80211_BAND_BIT_2GHZ;
  558. sc->hwinfo->modes = (NET80211_MODE_G | NET80211_MODE_B);
  559. for (i = 0; i < 12; i++)
  560. sc->hwinfo->rates[band][i] = ath5k_rates[i].bitrate;
  561. sc->hwinfo->nr_rates[band] = 12;
  562. sc->hwinfo->nr_channels =
  563. ath5k_copy_channels(ah, sc->hwinfo->channels,
  564. AR5K_MODE_11G, max_c);
  565. count_c = sc->hwinfo->nr_channels;
  566. max_c -= count_c;
  567. } else if (sc->ah->ah_capabilities.cap_mode & AR5K_MODE_BIT_11B) {
  568. /* B mode */
  569. band = NET80211_BAND_2GHZ;
  570. sc->hwinfo->bands = NET80211_BAND_BIT_2GHZ;
  571. sc->hwinfo->modes = NET80211_MODE_B;
  572. for (i = 0; i < 4; i++)
  573. sc->hwinfo->rates[band][i] = ath5k_rates[i].bitrate;
  574. sc->hwinfo->nr_rates[band] = 4;
  575. sc->hwinfo->nr_channels =
  576. ath5k_copy_channels(ah, sc->hwinfo->channels,
  577. AR5K_MODE_11B, max_c);
  578. count_c = sc->hwinfo->nr_channels;
  579. max_c -= count_c;
  580. }
  581. /* 5GHz band, A mode */
  582. if (sc->ah->ah_capabilities.cap_mode & AR5K_MODE_BIT_11A) {
  583. band = NET80211_BAND_5GHZ;
  584. sc->hwinfo->bands |= NET80211_BAND_BIT_5GHZ;
  585. sc->hwinfo->modes |= NET80211_MODE_A;
  586. for (i = 0; i < 8; i++)
  587. sc->hwinfo->rates[band][i] = ath5k_rates[i+4].bitrate;
  588. sc->hwinfo->nr_rates[band] = 8;
  589. sc->hwinfo->nr_channels =
  590. ath5k_copy_channels(ah, sc->hwinfo->channels,
  591. AR5K_MODE_11B, max_c);
  592. count_c = sc->hwinfo->nr_channels;
  593. max_c -= count_c;
  594. }
  595. return 0;
  596. }
  597. /*
  598. * Set/change channels. If the channel is really being changed,
  599. * it's done by reseting the chip. To accomplish this we must
  600. * first cleanup any pending DMA, then restart stuff after a la
  601. * ath5k_init.
  602. */
  603. static int
  604. ath5k_chan_set(struct ath5k_softc *sc, struct net80211_channel *chan)
  605. {
  606. if (chan->center_freq != sc->curchan->center_freq ||
  607. chan->hw_value != sc->curchan->hw_value) {
  608. /*
  609. * To switch channels clear any pending DMA operations;
  610. * wait long enough for the RX fifo to drain, reset the
  611. * hardware at the new frequency, and then re-enable
  612. * the relevant bits of the h/w.
  613. */
  614. DBG2("ath5k: resetting for channel change (%d -> %d MHz)\n",
  615. sc->curchan->center_freq, chan->center_freq);
  616. return ath5k_reset(sc, chan);
  617. }
  618. return 0;
  619. }
  620. static void
  621. ath5k_setcurmode(struct ath5k_softc *sc, unsigned int mode)
  622. {
  623. sc->curmode = mode;
  624. if (mode == AR5K_MODE_11A) {
  625. sc->curband = NET80211_BAND_5GHZ;
  626. } else {
  627. sc->curband = NET80211_BAND_2GHZ;
  628. }
  629. }
  630. static void
  631. ath5k_mode_setup(struct ath5k_softc *sc)
  632. {
  633. struct ath5k_hw *ah = sc->ah;
  634. u32 rfilt;
  635. /* configure rx filter */
  636. rfilt = sc->filter_flags;
  637. ath5k_hw_set_rx_filter(ah, rfilt);
  638. if (ath5k_hw_hasbssidmask(ah))
  639. ath5k_hw_set_bssid_mask(ah, sc->bssidmask);
  640. /* configure operational mode */
  641. ath5k_hw_set_opmode(ah);
  642. ath5k_hw_set_mcast_filter(ah, 0, 0);
  643. }
  644. static inline int
  645. ath5k_hw_rix_to_bitrate(int hw_rix)
  646. {
  647. int i;
  648. for (i = 0; i < ATH5K_NR_RATES; i++) {
  649. if (ath5k_rates[i].hw_code == hw_rix)
  650. return ath5k_rates[i].bitrate;
  651. }
  652. DBG("ath5k: invalid rix %02x\n", hw_rix);
  653. return 10; /* use lowest rate */
  654. }
  655. int ath5k_bitrate_to_hw_rix(int bitrate)
  656. {
  657. int i;
  658. for (i = 0; i < ATH5K_NR_RATES; i++) {
  659. if (ath5k_rates[i].bitrate == bitrate)
  660. return ath5k_rates[i].hw_code;
  661. }
  662. DBG("ath5k: invalid bitrate %d\n", bitrate);
  663. return ATH5K_RATE_CODE_1M; /* use lowest rate */
  664. }
  665. /***************\
  666. * Buffers setup *
  667. \***************/
  668. static struct io_buffer *
  669. ath5k_rx_iob_alloc(struct ath5k_softc *sc, u32 *iob_addr)
  670. {
  671. struct io_buffer *iob;
  672. unsigned int off;
  673. /*
  674. * Allocate buffer with headroom_needed space for the
  675. * fake physical layer header at the start.
  676. */
  677. iob = alloc_iob(sc->rxbufsize + sc->cachelsz - 1);
  678. if (!iob) {
  679. DBG("ath5k: can't alloc iobuf of size %d\n",
  680. sc->rxbufsize + sc->cachelsz - 1);
  681. return NULL;
  682. }
  683. *iob_addr = virt_to_bus(iob->data);
  684. /*
  685. * Cache-line-align. This is important (for the
  686. * 5210 at least) as not doing so causes bogus data
  687. * in rx'd frames.
  688. */
  689. off = *iob_addr % sc->cachelsz;
  690. if (off != 0) {
  691. iob_reserve(iob, sc->cachelsz - off);
  692. *iob_addr += sc->cachelsz - off;
  693. }
  694. return iob;
  695. }
  696. static int
  697. ath5k_rxbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
  698. {
  699. struct ath5k_hw *ah = sc->ah;
  700. struct io_buffer *iob = bf->iob;
  701. struct ath5k_desc *ds;
  702. if (!iob) {
  703. iob = ath5k_rx_iob_alloc(sc, &bf->iobaddr);
  704. if (!iob)
  705. return -ENOMEM;
  706. bf->iob = iob;
  707. }
  708. /*
  709. * Setup descriptors. For receive we always terminate
  710. * the descriptor list with a self-linked entry so we'll
  711. * not get overrun under high load (as can happen with a
  712. * 5212 when ANI processing enables PHY error frames).
  713. *
  714. * To insure the last descriptor is self-linked we create
  715. * each descriptor as self-linked and add it to the end. As
  716. * each additional descriptor is added the previous self-linked
  717. * entry is ``fixed'' naturally. This should be safe even
  718. * if DMA is happening. When processing RX interrupts we
  719. * never remove/process the last, self-linked, entry on the
  720. * descriptor list. This insures the hardware always has
  721. * someplace to write a new frame.
  722. */
  723. ds = bf->desc;
  724. ds->ds_link = bf->daddr; /* link to self */
  725. ds->ds_data = bf->iobaddr;
  726. if (ah->ah_setup_rx_desc(ah, ds,
  727. iob_tailroom(iob), /* buffer size */
  728. 0) != 0) {
  729. DBG("ath5k: error setting up RX descriptor for %zd bytes\n", iob_tailroom(iob));
  730. return -EINVAL;
  731. }
  732. if (sc->rxlink != NULL)
  733. *sc->rxlink = bf->daddr;
  734. sc->rxlink = &ds->ds_link;
  735. return 0;
  736. }
  737. static int
  738. ath5k_txbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
  739. {
  740. struct ath5k_hw *ah = sc->ah;
  741. struct ath5k_txq *txq = &sc->txq;
  742. struct ath5k_desc *ds = bf->desc;
  743. struct io_buffer *iob = bf->iob;
  744. unsigned int pktlen, flags;
  745. int ret;
  746. u16 duration = 0;
  747. u16 cts_rate = 0;
  748. flags = AR5K_TXDESC_INTREQ | AR5K_TXDESC_CLRDMASK;
  749. bf->iobaddr = virt_to_bus(iob->data);
  750. pktlen = iob_len(iob);
  751. /* FIXME: If we are in g mode and rate is a CCK rate
  752. * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
  753. * from tx power (value is in dB units already) */
  754. if (sc->dev->phy_flags & NET80211_PHY_USE_PROTECTION) {
  755. struct net80211_device *dev = sc->dev;
  756. flags |= AR5K_TXDESC_CTSENA;
  757. cts_rate = sc->hw_rtscts_rate;
  758. duration = net80211_cts_duration(dev, pktlen);
  759. }
  760. ret = ah->ah_setup_tx_desc(ah, ds, pktlen,
  761. IEEE80211_TYP_FRAME_HEADER_LEN,
  762. AR5K_PKT_TYPE_NORMAL, sc->power_level * 2,
  763. sc->hw_rate, ATH5K_RETRIES,
  764. AR5K_TXKEYIX_INVALID, 0, flags,
  765. cts_rate, duration);
  766. if (ret)
  767. return ret;
  768. ds->ds_link = 0;
  769. ds->ds_data = bf->iobaddr;
  770. list_add_tail(&bf->list, &txq->q);
  771. if (txq->link == NULL) /* is this first packet? */
  772. ath5k_hw_set_txdp(ah, txq->qnum, bf->daddr);
  773. else /* no, so only link it */
  774. *txq->link = bf->daddr;
  775. txq->link = &ds->ds_link;
  776. ath5k_hw_start_tx_dma(ah, txq->qnum);
  777. mb();
  778. return 0;
  779. }
  780. /*******************\
  781. * Descriptors setup *
  782. \*******************/
  783. static int
  784. ath5k_desc_alloc(struct ath5k_softc *sc)
  785. {
  786. struct ath5k_desc *ds;
  787. struct ath5k_buf *bf;
  788. u32 da;
  789. unsigned int i;
  790. int ret;
  791. /* allocate descriptors */
  792. sc->desc_len = sizeof(struct ath5k_desc) * (ATH_TXBUF + ATH_RXBUF + 1);
  793. sc->desc = malloc_dma(sc->desc_len, ATH5K_DESC_ALIGN);
  794. if (sc->desc == NULL) {
  795. DBG("ath5k: can't allocate descriptors\n");
  796. ret = -ENOMEM;
  797. goto err;
  798. }
  799. memset(sc->desc, 0, sc->desc_len);
  800. sc->desc_daddr = virt_to_bus(sc->desc);
  801. ds = sc->desc;
  802. da = sc->desc_daddr;
  803. bf = calloc(ATH_TXBUF + ATH_RXBUF + 1, sizeof(struct ath5k_buf));
  804. if (bf == NULL) {
  805. DBG("ath5k: can't allocate buffer pointers\n");
  806. ret = -ENOMEM;
  807. goto err_free;
  808. }
  809. sc->bufptr = bf;
  810. INIT_LIST_HEAD(&sc->rxbuf);
  811. for (i = 0; i < ATH_RXBUF; i++, bf++, ds++, da += sizeof(*ds)) {
  812. bf->desc = ds;
  813. bf->daddr = da;
  814. list_add_tail(&bf->list, &sc->rxbuf);
  815. }
  816. INIT_LIST_HEAD(&sc->txbuf);
  817. sc->txbuf_len = ATH_TXBUF;
  818. for (i = 0; i < ATH_TXBUF; i++, bf++, ds++, da += sizeof(*ds)) {
  819. bf->desc = ds;
  820. bf->daddr = da;
  821. list_add_tail(&bf->list, &sc->txbuf);
  822. }
  823. return 0;
  824. err_free:
  825. free_dma(sc->desc, sc->desc_len);
  826. err:
  827. sc->desc = NULL;
  828. return ret;
  829. }
  830. static void
  831. ath5k_desc_free(struct ath5k_softc *sc)
  832. {
  833. struct ath5k_buf *bf;
  834. list_for_each_entry(bf, &sc->txbuf, list)
  835. ath5k_txbuf_free(sc, bf);
  836. list_for_each_entry(bf, &sc->rxbuf, list)
  837. ath5k_rxbuf_free(sc, bf);
  838. /* Free memory associated with all descriptors */
  839. free_dma(sc->desc, sc->desc_len);
  840. free(sc->bufptr);
  841. sc->bufptr = NULL;
  842. }
  843. /**************\
  844. * Queues setup *
  845. \**************/
  846. static int
  847. ath5k_txq_setup(struct ath5k_softc *sc, int qtype, int subtype)
  848. {
  849. struct ath5k_hw *ah = sc->ah;
  850. struct ath5k_txq *txq;
  851. struct ath5k_txq_info qi = {
  852. .tqi_subtype = subtype,
  853. .tqi_aifs = AR5K_TXQ_USEDEFAULT,
  854. .tqi_cw_min = AR5K_TXQ_USEDEFAULT,
  855. .tqi_cw_max = AR5K_TXQ_USEDEFAULT
  856. };
  857. int qnum;
  858. /*
  859. * Enable interrupts only for EOL and DESC conditions.
  860. * We mark tx descriptors to receive a DESC interrupt
  861. * when a tx queue gets deep; otherwise waiting for the
  862. * EOL to reap descriptors. Note that this is done to
  863. * reduce interrupt load and this only defers reaping
  864. * descriptors, never transmitting frames. Aside from
  865. * reducing interrupts this also permits more concurrency.
  866. * The only potential downside is if the tx queue backs
  867. * up in which case the top half of the kernel may backup
  868. * due to a lack of tx descriptors.
  869. */
  870. qi.tqi_flags = AR5K_TXQ_FLAG_TXEOLINT_ENABLE |
  871. AR5K_TXQ_FLAG_TXDESCINT_ENABLE;
  872. qnum = ath5k_hw_setup_tx_queue(ah, qtype, &qi);
  873. if (qnum < 0) {
  874. DBG("ath5k: can't set up a TX queue\n");
  875. return -EIO;
  876. }
  877. txq = &sc->txq;
  878. if (!txq->setup) {
  879. txq->qnum = qnum;
  880. txq->link = NULL;
  881. INIT_LIST_HEAD(&txq->q);
  882. txq->setup = 1;
  883. }
  884. return 0;
  885. }
  886. static void
  887. ath5k_txq_drainq(struct ath5k_softc *sc, struct ath5k_txq *txq)
  888. {
  889. struct ath5k_buf *bf, *bf0;
  890. list_for_each_entry_safe(bf, bf0, &txq->q, list) {
  891. ath5k_txbuf_free(sc, bf);
  892. list_del(&bf->list);
  893. list_add_tail(&bf->list, &sc->txbuf);
  894. sc->txbuf_len++;
  895. }
  896. txq->link = NULL;
  897. }
  898. /*
  899. * Drain the transmit queues and reclaim resources.
  900. */
  901. static void
  902. ath5k_txq_cleanup(struct ath5k_softc *sc)
  903. {
  904. struct ath5k_hw *ah = sc->ah;
  905. if (!(sc->status & ATH_STAT_INVALID)) {
  906. /* don't touch the hardware if marked invalid */
  907. if (sc->txq.setup) {
  908. ath5k_hw_stop_tx_dma(ah, sc->txq.qnum);
  909. DBG("ath5k: txq [%d] %x, link %p\n",
  910. sc->txq.qnum,
  911. ath5k_hw_get_txdp(ah, sc->txq.qnum),
  912. sc->txq.link);
  913. }
  914. }
  915. if (sc->txq.setup)
  916. ath5k_txq_drainq(sc, &sc->txq);
  917. }
  918. static void
  919. ath5k_txq_release(struct ath5k_softc *sc)
  920. {
  921. if (sc->txq.setup) {
  922. ath5k_hw_release_tx_queue(sc->ah);
  923. sc->txq.setup = 0;
  924. }
  925. }
  926. /*************\
  927. * RX Handling *
  928. \*************/
  929. /*
  930. * Enable the receive h/w following a reset.
  931. */
  932. static int
  933. ath5k_rx_start(struct ath5k_softc *sc)
  934. {
  935. struct ath5k_hw *ah = sc->ah;
  936. struct ath5k_buf *bf;
  937. int ret;
  938. sc->rxbufsize = IEEE80211_MAX_LEN;
  939. if (sc->rxbufsize % sc->cachelsz != 0)
  940. sc->rxbufsize += sc->cachelsz - (sc->rxbufsize % sc->cachelsz);
  941. sc->rxlink = NULL;
  942. list_for_each_entry(bf, &sc->rxbuf, list) {
  943. ret = ath5k_rxbuf_setup(sc, bf);
  944. if (ret != 0)
  945. return ret;
  946. }
  947. bf = list_entry(sc->rxbuf.next, struct ath5k_buf, list);
  948. ath5k_hw_set_rxdp(ah, bf->daddr);
  949. ath5k_hw_start_rx_dma(ah); /* enable recv descriptors */
  950. ath5k_mode_setup(sc); /* set filters, etc. */
  951. ath5k_hw_start_rx_pcu(ah); /* re-enable PCU/DMA engine */
  952. return 0;
  953. }
  954. /*
  955. * Disable the receive h/w in preparation for a reset.
  956. */
  957. static void
  958. ath5k_rx_stop(struct ath5k_softc *sc)
  959. {
  960. struct ath5k_hw *ah = sc->ah;
  961. ath5k_hw_stop_rx_pcu(ah); /* disable PCU */
  962. ath5k_hw_set_rx_filter(ah, 0); /* clear recv filter */
  963. ath5k_hw_stop_rx_dma(ah); /* disable DMA engine */
  964. sc->rxlink = NULL; /* just in case */
  965. }
  966. static void
  967. ath5k_handle_rx(struct ath5k_softc *sc)
  968. {
  969. struct ath5k_rx_status rs;
  970. struct io_buffer *iob, *next_iob;
  971. u32 next_iob_addr;
  972. struct ath5k_buf *bf, *bf_last;
  973. struct ath5k_desc *ds;
  974. int ret;
  975. memset(&rs, 0, sizeof(rs));
  976. if (list_empty(&sc->rxbuf)) {
  977. DBG("ath5k: empty rx buf pool\n");
  978. return;
  979. }
  980. bf_last = list_entry(sc->rxbuf.prev, struct ath5k_buf, list);
  981. do {
  982. bf = list_entry(sc->rxbuf.next, struct ath5k_buf, list);
  983. assert(bf->iob != NULL);
  984. iob = bf->iob;
  985. ds = bf->desc;
  986. /*
  987. * last buffer must not be freed to ensure proper hardware
  988. * function. When the hardware finishes also a packet next to
  989. * it, we are sure, it doesn't use it anymore and we can go on.
  990. */
  991. if (bf_last == bf)
  992. bf->flags |= 1;
  993. if (bf->flags) {
  994. struct ath5k_buf *bf_next = list_entry(bf->list.next,
  995. struct ath5k_buf, list);
  996. ret = sc->ah->ah_proc_rx_desc(sc->ah, bf_next->desc,
  997. &rs);
  998. if (ret)
  999. break;
  1000. bf->flags &= ~1;
  1001. /* skip the overwritten one (even status is martian) */
  1002. goto next;
  1003. }
  1004. ret = sc->ah->ah_proc_rx_desc(sc->ah, ds, &rs);
  1005. if (ret) {
  1006. if (ret != -EINPROGRESS) {
  1007. DBG("ath5k: error in processing rx desc: %s\n",
  1008. strerror(ret));
  1009. net80211_rx_err(sc->dev, NULL, -ret);
  1010. } else {
  1011. /* normal return, reached end of
  1012. available descriptors */
  1013. }
  1014. return;
  1015. }
  1016. if (rs.rs_more) {
  1017. DBG("ath5k: unsupported fragmented rx\n");
  1018. goto next;
  1019. }
  1020. if (rs.rs_status) {
  1021. if (rs.rs_status & AR5K_RXERR_PHY) {
  1022. /* These are uncommon, and may indicate a real problem. */
  1023. net80211_rx_err(sc->dev, NULL, EIO);
  1024. goto next;
  1025. }
  1026. if (rs.rs_status & AR5K_RXERR_CRC) {
  1027. /* These occur *all the time*. */
  1028. goto next;
  1029. }
  1030. if (rs.rs_status & AR5K_RXERR_DECRYPT) {
  1031. /*
  1032. * Decrypt error. If the error occurred
  1033. * because there was no hardware key, then
  1034. * let the frame through so the upper layers
  1035. * can process it. This is necessary for 5210
  1036. * parts which have no way to setup a ``clear''
  1037. * key cache entry.
  1038. *
  1039. * XXX do key cache faulting
  1040. */
  1041. if (rs.rs_keyix == AR5K_RXKEYIX_INVALID &&
  1042. !(rs.rs_status & AR5K_RXERR_CRC))
  1043. goto accept;
  1044. }
  1045. /* any other error, unhandled */
  1046. DBG("ath5k: packet rx status %x\n", rs.rs_status);
  1047. goto next;
  1048. }
  1049. accept:
  1050. next_iob = ath5k_rx_iob_alloc(sc, &next_iob_addr);
  1051. /*
  1052. * If we can't replace bf->iob with a new iob under memory
  1053. * pressure, just skip this packet
  1054. */
  1055. if (!next_iob) {
  1056. DBG("ath5k: dropping packet under memory pressure\n");
  1057. goto next;
  1058. }
  1059. iob_put(iob, rs.rs_datalen);
  1060. /* The MAC header is padded to have 32-bit boundary if the
  1061. * packet payload is non-zero. However, iPXE only
  1062. * supports standard 802.11 packets with 24-byte
  1063. * header, so no padding correction should be needed.
  1064. */
  1065. DBG2("ath5k: rx %d bytes, signal %d\n", rs.rs_datalen,
  1066. rs.rs_rssi);
  1067. net80211_rx(sc->dev, iob, rs.rs_rssi,
  1068. ath5k_hw_rix_to_bitrate(rs.rs_rate));
  1069. bf->iob = next_iob;
  1070. bf->iobaddr = next_iob_addr;
  1071. next:
  1072. list_del(&bf->list);
  1073. list_add_tail(&bf->list, &sc->rxbuf);
  1074. } while (ath5k_rxbuf_setup(sc, bf) == 0);
  1075. }
  1076. /*************\
  1077. * TX Handling *
  1078. \*************/
  1079. static void
  1080. ath5k_tx_processq(struct ath5k_softc *sc, struct ath5k_txq *txq)
  1081. {
  1082. struct ath5k_tx_status ts;
  1083. struct ath5k_buf *bf, *bf0;
  1084. struct ath5k_desc *ds;
  1085. struct io_buffer *iob;
  1086. int ret;
  1087. memset(&ts, 0, sizeof(ts));
  1088. list_for_each_entry_safe(bf, bf0, &txq->q, list) {
  1089. ds = bf->desc;
  1090. ret = sc->ah->ah_proc_tx_desc(sc->ah, ds, &ts);
  1091. if (ret) {
  1092. if (ret != -EINPROGRESS) {
  1093. DBG("ath5k: error in processing tx desc: %s\n",
  1094. strerror(ret));
  1095. } else {
  1096. /* normal return, reached end of tx completions */
  1097. }
  1098. break;
  1099. }
  1100. iob = bf->iob;
  1101. bf->iob = NULL;
  1102. DBG2("ath5k: tx %zd bytes complete, %d retries\n",
  1103. iob_len(iob), ts.ts_retry[0]);
  1104. net80211_tx_complete(sc->dev, iob, ts.ts_retry[0],
  1105. ts.ts_status ? EIO : 0);
  1106. list_del(&bf->list);
  1107. list_add_tail(&bf->list, &sc->txbuf);
  1108. sc->txbuf_len++;
  1109. }
  1110. if (list_empty(&txq->q))
  1111. txq->link = NULL;
  1112. }
  1113. static void
  1114. ath5k_handle_tx(struct ath5k_softc *sc)
  1115. {
  1116. ath5k_tx_processq(sc, &sc->txq);
  1117. }
  1118. /********************\
  1119. * Interrupt handling *
  1120. \********************/
  1121. static void
  1122. ath5k_irq(struct net80211_device *dev, int enable)
  1123. {
  1124. struct ath5k_softc *sc = dev->priv;
  1125. struct ath5k_hw *ah = sc->ah;
  1126. sc->irq_ena = enable;
  1127. ah->ah_ier = enable ? AR5K_IER_ENABLE : AR5K_IER_DISABLE;
  1128. ath5k_hw_reg_write(ah, ah->ah_ier, AR5K_IER);
  1129. ath5k_hw_set_imr(ah, sc->imask);
  1130. }
  1131. static int
  1132. ath5k_init(struct ath5k_softc *sc)
  1133. {
  1134. struct ath5k_hw *ah = sc->ah;
  1135. int ret, i;
  1136. /*
  1137. * Stop anything previously setup. This is safe
  1138. * no matter this is the first time through or not.
  1139. */
  1140. ath5k_stop_hw(sc);
  1141. /*
  1142. * The basic interface to setting the hardware in a good
  1143. * state is ``reset''. On return the hardware is known to
  1144. * be powered up and with interrupts disabled. This must
  1145. * be followed by initialization of the appropriate bits
  1146. * and then setup of the interrupt mask.
  1147. */
  1148. sc->curchan = sc->dev->channels + sc->dev->channel;
  1149. sc->curband = sc->curchan->band;
  1150. sc->imask = AR5K_INT_RXOK | AR5K_INT_RXERR | AR5K_INT_RXEOL |
  1151. AR5K_INT_RXORN | AR5K_INT_TXDESC | AR5K_INT_TXEOL |
  1152. AR5K_INT_FATAL | AR5K_INT_GLOBAL;
  1153. ret = ath5k_reset(sc, NULL);
  1154. if (ret)
  1155. goto done;
  1156. ath5k_rfkill_hw_start(ah);
  1157. /*
  1158. * Reset the key cache since some parts do not reset the
  1159. * contents on initial power up or resume from suspend.
  1160. */
  1161. for (i = 0; i < AR5K_KEYTABLE_SIZE; i++)
  1162. ath5k_hw_reset_key(ah, i);
  1163. /* Set ack to be sent at low bit-rates */
  1164. ath5k_hw_set_ack_bitrate_high(ah, 0);
  1165. ret = 0;
  1166. done:
  1167. mb();
  1168. return ret;
  1169. }
  1170. static int
  1171. ath5k_stop_hw(struct ath5k_softc *sc)
  1172. {
  1173. struct ath5k_hw *ah = sc->ah;
  1174. /*
  1175. * Shutdown the hardware and driver:
  1176. * stop output from above
  1177. * disable interrupts
  1178. * turn off timers
  1179. * turn off the radio
  1180. * clear transmit machinery
  1181. * clear receive machinery
  1182. * drain and release tx queues
  1183. * reclaim beacon resources
  1184. * power down hardware
  1185. *
  1186. * Note that some of this work is not possible if the
  1187. * hardware is gone (invalid).
  1188. */
  1189. if (!(sc->status & ATH_STAT_INVALID)) {
  1190. ath5k_hw_set_imr(ah, 0);
  1191. }
  1192. ath5k_txq_cleanup(sc);
  1193. if (!(sc->status & ATH_STAT_INVALID)) {
  1194. ath5k_rx_stop(sc);
  1195. ath5k_hw_phy_disable(ah);
  1196. } else
  1197. sc->rxlink = NULL;
  1198. ath5k_rfkill_hw_stop(sc->ah);
  1199. return 0;
  1200. }
  1201. static void
  1202. ath5k_poll(struct net80211_device *dev)
  1203. {
  1204. struct ath5k_softc *sc = dev->priv;
  1205. struct ath5k_hw *ah = sc->ah;
  1206. enum ath5k_int status;
  1207. unsigned int counter = 1000;
  1208. if (currticks() - sc->last_calib_ticks >
  1209. ATH5K_CALIB_INTERVAL * ticks_per_sec()) {
  1210. ath5k_calibrate(sc);
  1211. sc->last_calib_ticks = currticks();
  1212. }
  1213. if ((sc->status & ATH_STAT_INVALID) ||
  1214. (sc->irq_ena && !ath5k_hw_is_intr_pending(ah)))
  1215. return;
  1216. do {
  1217. ath5k_hw_get_isr(ah, &status); /* NB: clears IRQ too */
  1218. DBGP("ath5k: status %#x/%#x\n", status, sc->imask);
  1219. if (status & AR5K_INT_FATAL) {
  1220. /*
  1221. * Fatal errors are unrecoverable.
  1222. * Typically these are caused by DMA errors.
  1223. */
  1224. DBG("ath5k: fatal error, resetting\n");
  1225. ath5k_reset_wake(sc);
  1226. } else if (status & AR5K_INT_RXORN) {
  1227. DBG("ath5k: rx overrun, resetting\n");
  1228. ath5k_reset_wake(sc);
  1229. } else {
  1230. if (status & AR5K_INT_RXEOL) {
  1231. /*
  1232. * NB: the hardware should re-read the link when
  1233. * RXE bit is written, but it doesn't work at
  1234. * least on older hardware revs.
  1235. */
  1236. DBG("ath5k: rx EOL\n");
  1237. sc->rxlink = NULL;
  1238. }
  1239. if (status & AR5K_INT_TXURN) {
  1240. /* bump tx trigger level */
  1241. DBG("ath5k: tx underrun\n");
  1242. ath5k_hw_update_tx_triglevel(ah, 1);
  1243. }
  1244. if (status & (AR5K_INT_RXOK | AR5K_INT_RXERR))
  1245. ath5k_handle_rx(sc);
  1246. if (status & (AR5K_INT_TXOK | AR5K_INT_TXDESC
  1247. | AR5K_INT_TXERR | AR5K_INT_TXEOL))
  1248. ath5k_handle_tx(sc);
  1249. }
  1250. } while (ath5k_hw_is_intr_pending(ah) && counter-- > 0);
  1251. if (!counter)
  1252. DBG("ath5k: too many interrupts, giving up for now\n");
  1253. }
  1254. /*
  1255. * Periodically recalibrate the PHY to account
  1256. * for temperature/environment changes.
  1257. */
  1258. static void
  1259. ath5k_calibrate(struct ath5k_softc *sc)
  1260. {
  1261. struct ath5k_hw *ah = sc->ah;
  1262. if (ath5k_hw_gainf_calibrate(ah) == AR5K_RFGAIN_NEED_CHANGE) {
  1263. /*
  1264. * Rfgain is out of bounds, reset the chip
  1265. * to load new gain values.
  1266. */
  1267. DBG("ath5k: resetting for calibration\n");
  1268. ath5k_reset_wake(sc);
  1269. }
  1270. if (ath5k_hw_phy_calibrate(ah, sc->curchan))
  1271. DBG("ath5k: calibration of channel %d failed\n",
  1272. sc->curchan->channel_nr);
  1273. }
  1274. /********************\
  1275. * Net80211 functions *
  1276. \********************/
  1277. static int
  1278. ath5k_tx(struct net80211_device *dev, struct io_buffer *iob)
  1279. {
  1280. struct ath5k_softc *sc = dev->priv;
  1281. struct ath5k_buf *bf;
  1282. int rc;
  1283. /*
  1284. * The hardware expects the header padded to 4 byte boundaries.
  1285. * iPXE only ever sends 24-byte headers, so no action necessary.
  1286. */
  1287. if (list_empty(&sc->txbuf)) {
  1288. DBG("ath5k: dropping packet because no tx bufs available\n");
  1289. return -ENOBUFS;
  1290. }
  1291. bf = list_entry(sc->txbuf.next, struct ath5k_buf, list);
  1292. list_del(&bf->list);
  1293. sc->txbuf_len--;
  1294. bf->iob = iob;
  1295. if ((rc = ath5k_txbuf_setup(sc, bf)) != 0) {
  1296. bf->iob = NULL;
  1297. list_add_tail(&bf->list, &sc->txbuf);
  1298. sc->txbuf_len++;
  1299. return rc;
  1300. }
  1301. return 0;
  1302. }
  1303. /*
  1304. * Reset the hardware. If chan is not NULL, then also pause rx/tx
  1305. * and change to the given channel.
  1306. */
  1307. static int
  1308. ath5k_reset(struct ath5k_softc *sc, struct net80211_channel *chan)
  1309. {
  1310. struct ath5k_hw *ah = sc->ah;
  1311. int ret;
  1312. if (chan) {
  1313. ath5k_hw_set_imr(ah, 0);
  1314. ath5k_txq_cleanup(sc);
  1315. ath5k_rx_stop(sc);
  1316. sc->curchan = chan;
  1317. sc->curband = chan->band;
  1318. }
  1319. ret = ath5k_hw_reset(ah, sc->curchan, 1);
  1320. if (ret) {
  1321. DBG("ath5k: can't reset hardware: %s\n", strerror(ret));
  1322. return ret;
  1323. }
  1324. ret = ath5k_rx_start(sc);
  1325. if (ret) {
  1326. DBG("ath5k: can't start rx logic: %s\n", strerror(ret));
  1327. return ret;
  1328. }
  1329. /*
  1330. * Change channels and update the h/w rate map if we're switching;
  1331. * e.g. 11a to 11b/g.
  1332. *
  1333. * We may be doing a reset in response to an ioctl that changes the
  1334. * channel so update any state that might change as a result.
  1335. *
  1336. * XXX needed?
  1337. */
  1338. /* ath5k_chan_change(sc, c); */
  1339. /* Reenable interrupts if necessary */
  1340. ath5k_irq(sc->dev, sc->irq_ena);
  1341. return 0;
  1342. }
  1343. static int ath5k_reset_wake(struct ath5k_softc *sc)
  1344. {
  1345. return ath5k_reset(sc, sc->curchan);
  1346. }
  1347. static int ath5k_start(struct net80211_device *dev)
  1348. {
  1349. struct ath5k_softc *sc = dev->priv;
  1350. int ret;
  1351. if ((ret = ath5k_init(sc)) != 0)
  1352. return ret;
  1353. sc->assoc = 0;
  1354. ath5k_configure_filter(sc);
  1355. ath5k_hw_set_lladdr(sc->ah, dev->netdev->ll_addr);
  1356. return 0;
  1357. }
  1358. static void ath5k_stop(struct net80211_device *dev)
  1359. {
  1360. struct ath5k_softc *sc = dev->priv;
  1361. u8 mac[ETH_ALEN] = {};
  1362. ath5k_hw_set_lladdr(sc->ah, mac);
  1363. ath5k_stop_hw(sc);
  1364. }
  1365. static int
  1366. ath5k_config(struct net80211_device *dev, int changed)
  1367. {
  1368. struct ath5k_softc *sc = dev->priv;
  1369. struct ath5k_hw *ah = sc->ah;
  1370. struct net80211_channel *chan = &dev->channels[dev->channel];
  1371. int ret;
  1372. if (changed & NET80211_CFG_CHANNEL) {
  1373. sc->power_level = chan->maxpower;
  1374. if ((ret = ath5k_chan_set(sc, chan)) != 0)
  1375. return ret;
  1376. }
  1377. if ((changed & NET80211_CFG_RATE) ||
  1378. (changed & NET80211_CFG_PHY_PARAMS)) {
  1379. int spmbl = ATH5K_SPMBL_NO;
  1380. u16 rate = dev->rates[dev->rate];
  1381. u16 slowrate = dev->rates[dev->rtscts_rate];
  1382. int i;
  1383. if (dev->phy_flags & NET80211_PHY_USE_SHORT_PREAMBLE)
  1384. spmbl = ATH5K_SPMBL_YES;
  1385. for (i = 0; i < ATH5K_NR_RATES; i++) {
  1386. if (ath5k_rates[i].bitrate == rate &&
  1387. (ath5k_rates[i].short_pmbl & spmbl))
  1388. sc->hw_rate = ath5k_rates[i].hw_code;
  1389. if (ath5k_rates[i].bitrate == slowrate &&
  1390. (ath5k_rates[i].short_pmbl & spmbl))
  1391. sc->hw_rtscts_rate = ath5k_rates[i].hw_code;
  1392. }
  1393. }
  1394. if (changed & NET80211_CFG_ASSOC) {
  1395. sc->assoc = !!(dev->state & NET80211_ASSOCIATED);
  1396. if (sc->assoc) {
  1397. memcpy(ah->ah_bssid, dev->bssid, ETH_ALEN);
  1398. } else {
  1399. memset(ah->ah_bssid, 0xff, ETH_ALEN);
  1400. }
  1401. ath5k_hw_set_associd(ah, ah->ah_bssid, 0);
  1402. }
  1403. return 0;
  1404. }
  1405. /*
  1406. * o always accept unicast, broadcast, and multicast traffic
  1407. * o multicast traffic for all BSSIDs will be enabled if mac80211
  1408. * says it should be
  1409. * o maintain current state of phy ofdm or phy cck error reception.
  1410. * If the hardware detects any of these type of errors then
  1411. * ath5k_hw_get_rx_filter() will pass to us the respective
  1412. * hardware filters to be able to receive these type of frames.
  1413. * o probe request frames are accepted only when operating in
  1414. * hostap, adhoc, or monitor modes
  1415. * o enable promiscuous mode according to the interface state
  1416. * o accept beacons:
  1417. * - when operating in adhoc mode so the 802.11 layer creates
  1418. * node table entries for peers,
  1419. * - when operating in station mode for collecting rssi data when
  1420. * the station is otherwise quiet, or
  1421. * - when scanning
  1422. */
  1423. static void ath5k_configure_filter(struct ath5k_softc *sc)
  1424. {
  1425. struct ath5k_hw *ah = sc->ah;
  1426. u32 mfilt[2], rfilt;
  1427. /* Enable all multicast */
  1428. mfilt[0] = ~0;
  1429. mfilt[1] = ~0;
  1430. /* Enable data frames and beacons */
  1431. rfilt = (AR5K_RX_FILTER_UCAST | AR5K_RX_FILTER_BCAST |
  1432. AR5K_RX_FILTER_MCAST | AR5K_RX_FILTER_BEACON);
  1433. /* Set filters */
  1434. ath5k_hw_set_rx_filter(ah, rfilt);
  1435. /* Set multicast bits */
  1436. ath5k_hw_set_mcast_filter(ah, mfilt[0], mfilt[1]);
  1437. /* Set the cached hw filter flags, this will alter actually
  1438. * be set in HW */
  1439. sc->filter_flags = rfilt;
  1440. }