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

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