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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694
  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. DBG("ath5k: rx PHY error\n");
  1024. goto next;
  1025. }
  1026. if (rs.rs_status & AR5K_RXERR_CRC) {
  1027. net80211_rx_err(sc->dev, NULL, EIO);
  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, gPXE 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 %d 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. /*
  1157. * Reset the key cache since some parts do not reset the
  1158. * contents on initial power up or resume from suspend.
  1159. */
  1160. for (i = 0; i < AR5K_KEYTABLE_SIZE; i++)
  1161. ath5k_hw_reset_key(ah, i);
  1162. /* Set ack to be sent at low bit-rates */
  1163. ath5k_hw_set_ack_bitrate_high(ah, 0);
  1164. ret = 0;
  1165. done:
  1166. mb();
  1167. return ret;
  1168. }
  1169. static int
  1170. ath5k_stop_hw(struct ath5k_softc *sc)
  1171. {
  1172. struct ath5k_hw *ah = sc->ah;
  1173. /*
  1174. * Shutdown the hardware and driver:
  1175. * stop output from above
  1176. * disable interrupts
  1177. * turn off timers
  1178. * turn off the radio
  1179. * clear transmit machinery
  1180. * clear receive machinery
  1181. * drain and release tx queues
  1182. * reclaim beacon resources
  1183. * power down hardware
  1184. *
  1185. * Note that some of this work is not possible if the
  1186. * hardware is gone (invalid).
  1187. */
  1188. if (!(sc->status & ATH_STAT_INVALID)) {
  1189. ath5k_hw_set_imr(ah, 0);
  1190. }
  1191. ath5k_txq_cleanup(sc);
  1192. if (!(sc->status & ATH_STAT_INVALID)) {
  1193. ath5k_rx_stop(sc);
  1194. ath5k_hw_phy_disable(ah);
  1195. } else
  1196. sc->rxlink = NULL;
  1197. return 0;
  1198. }
  1199. static void
  1200. ath5k_poll(struct net80211_device *dev)
  1201. {
  1202. struct ath5k_softc *sc = dev->priv;
  1203. struct ath5k_hw *ah = sc->ah;
  1204. enum ath5k_int status;
  1205. unsigned int counter = 1000;
  1206. if (currticks() - sc->last_calib_ticks >
  1207. ATH5K_CALIB_INTERVAL * ticks_per_sec()) {
  1208. ath5k_calibrate(sc);
  1209. sc->last_calib_ticks = currticks();
  1210. }
  1211. if ((sc->status & ATH_STAT_INVALID) ||
  1212. (sc->irq_ena && !ath5k_hw_is_intr_pending(ah)))
  1213. return;
  1214. do {
  1215. ath5k_hw_get_isr(ah, &status); /* NB: clears IRQ too */
  1216. DBGP("ath5k: status %#x/%#x\n", status, sc->imask);
  1217. if (status & AR5K_INT_FATAL) {
  1218. /*
  1219. * Fatal errors are unrecoverable.
  1220. * Typically these are caused by DMA errors.
  1221. */
  1222. DBG("ath5k: fatal error, resetting\n");
  1223. ath5k_reset_wake(sc);
  1224. } else if (status & AR5K_INT_RXORN) {
  1225. DBG("ath5k: rx overrun, resetting\n");
  1226. ath5k_reset_wake(sc);
  1227. } else {
  1228. if (status & AR5K_INT_RXEOL) {
  1229. /*
  1230. * NB: the hardware should re-read the link when
  1231. * RXE bit is written, but it doesn't work at
  1232. * least on older hardware revs.
  1233. */
  1234. DBG("ath5k: rx EOL\n");
  1235. sc->rxlink = NULL;
  1236. }
  1237. if (status & AR5K_INT_TXURN) {
  1238. /* bump tx trigger level */
  1239. DBG("ath5k: tx underrun\n");
  1240. ath5k_hw_update_tx_triglevel(ah, 1);
  1241. }
  1242. if (status & (AR5K_INT_RXOK | AR5K_INT_RXERR))
  1243. ath5k_handle_rx(sc);
  1244. if (status & (AR5K_INT_TXOK | AR5K_INT_TXDESC
  1245. | AR5K_INT_TXERR | AR5K_INT_TXEOL))
  1246. ath5k_handle_tx(sc);
  1247. }
  1248. } while (ath5k_hw_is_intr_pending(ah) && counter-- > 0);
  1249. if (!counter)
  1250. DBG("ath5k: too many interrupts, giving up for now\n");
  1251. }
  1252. /*
  1253. * Periodically recalibrate the PHY to account
  1254. * for temperature/environment changes.
  1255. */
  1256. static void
  1257. ath5k_calibrate(struct ath5k_softc *sc)
  1258. {
  1259. struct ath5k_hw *ah = sc->ah;
  1260. if (ath5k_hw_gainf_calibrate(ah) == AR5K_RFGAIN_NEED_CHANGE) {
  1261. /*
  1262. * Rfgain is out of bounds, reset the chip
  1263. * to load new gain values.
  1264. */
  1265. DBG("ath5k: resetting for calibration\n");
  1266. ath5k_reset_wake(sc);
  1267. }
  1268. if (ath5k_hw_phy_calibrate(ah, sc->curchan))
  1269. DBG("ath5k: calibration of channel %d failed\n",
  1270. sc->curchan->channel_nr);
  1271. }
  1272. /********************\
  1273. * Net80211 functions *
  1274. \********************/
  1275. static int
  1276. ath5k_tx(struct net80211_device *dev, struct io_buffer *iob)
  1277. {
  1278. struct ath5k_softc *sc = dev->priv;
  1279. struct ath5k_buf *bf;
  1280. int rc;
  1281. /*
  1282. * The hardware expects the header padded to 4 byte boundaries.
  1283. * gPXE only ever sends 24-byte headers, so no action necessary.
  1284. */
  1285. if (list_empty(&sc->txbuf)) {
  1286. DBG("ath5k: dropping packet because no tx bufs available\n");
  1287. return -ENOBUFS;
  1288. }
  1289. bf = list_entry(sc->txbuf.next, struct ath5k_buf, list);
  1290. list_del(&bf->list);
  1291. sc->txbuf_len--;
  1292. bf->iob = iob;
  1293. if ((rc = ath5k_txbuf_setup(sc, bf)) != 0) {
  1294. bf->iob = NULL;
  1295. list_add_tail(&bf->list, &sc->txbuf);
  1296. sc->txbuf_len++;
  1297. return rc;
  1298. }
  1299. return 0;
  1300. }
  1301. /*
  1302. * Reset the hardware. If chan is not NULL, then also pause rx/tx
  1303. * and change to the given channel.
  1304. */
  1305. static int
  1306. ath5k_reset(struct ath5k_softc *sc, struct net80211_channel *chan)
  1307. {
  1308. struct ath5k_hw *ah = sc->ah;
  1309. int ret;
  1310. if (chan) {
  1311. ath5k_hw_set_imr(ah, 0);
  1312. ath5k_txq_cleanup(sc);
  1313. ath5k_rx_stop(sc);
  1314. sc->curchan = chan;
  1315. sc->curband = chan->band;
  1316. }
  1317. ret = ath5k_hw_reset(ah, sc->curchan, 1);
  1318. if (ret) {
  1319. DBG("ath5k: can't reset hardware: %s\n", strerror(ret));
  1320. return ret;
  1321. }
  1322. ret = ath5k_rx_start(sc);
  1323. if (ret) {
  1324. DBG("ath5k: can't start rx logic: %s\n", strerror(ret));
  1325. return ret;
  1326. }
  1327. /*
  1328. * Change channels and update the h/w rate map if we're switching;
  1329. * e.g. 11a to 11b/g.
  1330. *
  1331. * We may be doing a reset in response to an ioctl that changes the
  1332. * channel so update any state that might change as a result.
  1333. *
  1334. * XXX needed?
  1335. */
  1336. /* ath5k_chan_change(sc, c); */
  1337. /* Reenable interrupts if necessary */
  1338. ath5k_irq(sc->dev, sc->irq_ena);
  1339. return 0;
  1340. }
  1341. static int ath5k_reset_wake(struct ath5k_softc *sc)
  1342. {
  1343. return ath5k_reset(sc, sc->curchan);
  1344. }
  1345. static int ath5k_start(struct net80211_device *dev)
  1346. {
  1347. struct ath5k_softc *sc = dev->priv;
  1348. int ret;
  1349. if ((ret = ath5k_init(sc)) != 0)
  1350. return ret;
  1351. sc->assoc = 0;
  1352. ath5k_configure_filter(sc);
  1353. ath5k_hw_set_lladdr(sc->ah, dev->netdev->ll_addr);
  1354. return 0;
  1355. }
  1356. static void ath5k_stop(struct net80211_device *dev)
  1357. {
  1358. struct ath5k_softc *sc = dev->priv;
  1359. u8 mac[ETH_ALEN] = {};
  1360. ath5k_hw_set_lladdr(sc->ah, mac);
  1361. ath5k_stop_hw(sc);
  1362. }
  1363. static int
  1364. ath5k_config(struct net80211_device *dev, int changed)
  1365. {
  1366. struct ath5k_softc *sc = dev->priv;
  1367. struct ath5k_hw *ah = sc->ah;
  1368. struct net80211_channel *chan = &dev->channels[dev->channel];
  1369. int ret;
  1370. if (changed & NET80211_CFG_CHANNEL) {
  1371. sc->power_level = chan->maxpower;
  1372. if ((ret = ath5k_chan_set(sc, chan)) != 0)
  1373. return ret;
  1374. }
  1375. if ((changed & NET80211_CFG_RATE) ||
  1376. (changed & NET80211_CFG_PHY_PARAMS)) {
  1377. int spmbl = ATH5K_SPMBL_NO;
  1378. u16 rate = dev->rates[dev->rate];
  1379. u16 slowrate = dev->rates[dev->rtscts_rate];
  1380. int i;
  1381. if (dev->phy_flags & NET80211_PHY_USE_SHORT_PREAMBLE)
  1382. spmbl = ATH5K_SPMBL_YES;
  1383. for (i = 0; i < ATH5K_NR_RATES; i++) {
  1384. if (ath5k_rates[i].bitrate == rate &&
  1385. (ath5k_rates[i].short_pmbl & spmbl))
  1386. sc->hw_rate = ath5k_rates[i].hw_code;
  1387. if (ath5k_rates[i].bitrate == slowrate &&
  1388. (ath5k_rates[i].short_pmbl & spmbl))
  1389. sc->hw_rtscts_rate = ath5k_rates[i].hw_code;
  1390. }
  1391. }
  1392. if (changed & NET80211_CFG_ASSOC) {
  1393. sc->assoc = !!(dev->state & NET80211_ASSOCIATED);
  1394. if (sc->assoc) {
  1395. memcpy(ah->ah_bssid, dev->bssid, ETH_ALEN);
  1396. } else {
  1397. memset(ah->ah_bssid, 0xff, ETH_ALEN);
  1398. }
  1399. ath5k_hw_set_associd(ah, ah->ah_bssid, 0);
  1400. }
  1401. return 0;
  1402. }
  1403. /*
  1404. * o always accept unicast, broadcast, and multicast traffic
  1405. * o multicast traffic for all BSSIDs will be enabled if mac80211
  1406. * says it should be
  1407. * o maintain current state of phy ofdm or phy cck error reception.
  1408. * If the hardware detects any of these type of errors then
  1409. * ath5k_hw_get_rx_filter() will pass to us the respective
  1410. * hardware filters to be able to receive these type of frames.
  1411. * o probe request frames are accepted only when operating in
  1412. * hostap, adhoc, or monitor modes
  1413. * o enable promiscuous mode according to the interface state
  1414. * o accept beacons:
  1415. * - when operating in adhoc mode so the 802.11 layer creates
  1416. * node table entries for peers,
  1417. * - when operating in station mode for collecting rssi data when
  1418. * the station is otherwise quiet, or
  1419. * - when scanning
  1420. */
  1421. static void ath5k_configure_filter(struct ath5k_softc *sc)
  1422. {
  1423. struct ath5k_hw *ah = sc->ah;
  1424. u32 mfilt[2], rfilt;
  1425. /* Enable all multicast */
  1426. mfilt[0] = ~0;
  1427. mfilt[1] = ~0;
  1428. /* Enable data frames and beacons */
  1429. rfilt = (AR5K_RX_FILTER_UCAST | AR5K_RX_FILTER_BCAST |
  1430. AR5K_RX_FILTER_MCAST | AR5K_RX_FILTER_BEACON);
  1431. /* Set filters */
  1432. ath5k_hw_set_rx_filter(ah, rfilt);
  1433. /* Set multicast bits */
  1434. ath5k_hw_set_mcast_filter(ah, mfilt[0], mfilt[1]);
  1435. /* Set the cached hw filter flags, this will alter actually
  1436. * be set in HW */
  1437. sc->filter_flags = rfilt;
  1438. }