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

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