base.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975
  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. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
  18. * redistribution must be conditioned upon including a substantially
  19. * similar Disclaimer requirement for further binary redistribution.
  20. * 3. Neither the names of the above-listed copyright holders nor the names
  21. * of any contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * Alternatively, this software may be distributed under the terms of the
  25. * GNU General Public License ("GPL") version 2 as published by the Free
  26. * Software Foundation.
  27. *
  28. * NO WARRANTY
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
  32. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  33. * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
  34. * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  35. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  36. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  37. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  38. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  39. * THE POSSIBILITY OF SUCH DAMAGES.
  40. *
  41. */
  42. #include <linux/version.h>
  43. #include <linux/module.h>
  44. #include <linux/delay.h>
  45. #include <linux/if.h>
  46. #include <linux/netdevice.h>
  47. #include <linux/cache.h>
  48. #include <linux/pci.h>
  49. #include <linux/ethtool.h>
  50. #include <linux/uaccess.h>
  51. #include <net/ieee80211_radiotap.h>
  52. #include <asm/unaligned.h>
  53. #include "base.h"
  54. #include "reg.h"
  55. #include "debug.h"
  56. /* unaligned little endian access */
  57. #define LE_READ_2(_p) (le16_to_cpu(get_unaligned((__le16 *)(_p))))
  58. #define LE_READ_4(_p) (le32_to_cpu(get_unaligned((__le32 *)(_p))))
  59. enum {
  60. ATH_LED_TX,
  61. ATH_LED_RX,
  62. };
  63. static int ath5k_calinterval = 10; /* Calibrate PHY every 10 secs (TODO: Fixme) */
  64. /******************\
  65. * Internal defines *
  66. \******************/
  67. /* Module info */
  68. MODULE_AUTHOR("Jiri Slaby");
  69. MODULE_AUTHOR("Nick Kossifidis");
  70. MODULE_DESCRIPTION("Support for 5xxx series of Atheros 802.11 wireless LAN cards.");
  71. MODULE_SUPPORTED_DEVICE("Atheros 5xxx WLAN cards");
  72. MODULE_LICENSE("Dual BSD/GPL");
  73. MODULE_VERSION("0.1.1 (EXPERIMENTAL)");
  74. /* Known PCI ids */
  75. static struct pci_device_id ath5k_pci_id_table[] __devinitdata = {
  76. { PCI_VDEVICE(ATHEROS, 0x0207), .driver_data = AR5K_AR5210 }, /* 5210 early */
  77. { PCI_VDEVICE(ATHEROS, 0x0007), .driver_data = AR5K_AR5210 }, /* 5210 */
  78. { PCI_VDEVICE(ATHEROS, 0x0011), .driver_data = AR5K_AR5211 }, /* 5311 - this is on AHB bus !*/
  79. { PCI_VDEVICE(ATHEROS, 0x0012), .driver_data = AR5K_AR5211 }, /* 5211 */
  80. { PCI_VDEVICE(ATHEROS, 0x0013), .driver_data = AR5K_AR5212 }, /* 5212 */
  81. { PCI_VDEVICE(3COM_2, 0x0013), .driver_data = AR5K_AR5212 }, /* 3com 5212 */
  82. { PCI_VDEVICE(3COM, 0x0013), .driver_data = AR5K_AR5212 }, /* 3com 3CRDAG675 5212 */
  83. { PCI_VDEVICE(ATHEROS, 0x1014), .driver_data = AR5K_AR5212 }, /* IBM minipci 5212 */
  84. { PCI_VDEVICE(ATHEROS, 0x0014), .driver_data = AR5K_AR5212 }, /* 5212 combatible */
  85. { PCI_VDEVICE(ATHEROS, 0x0015), .driver_data = AR5K_AR5212 }, /* 5212 combatible */
  86. { PCI_VDEVICE(ATHEROS, 0x0016), .driver_data = AR5K_AR5212 }, /* 5212 combatible */
  87. { PCI_VDEVICE(ATHEROS, 0x0017), .driver_data = AR5K_AR5212 }, /* 5212 combatible */
  88. { PCI_VDEVICE(ATHEROS, 0x0018), .driver_data = AR5K_AR5212 }, /* 5212 combatible */
  89. { PCI_VDEVICE(ATHEROS, 0x0019), .driver_data = AR5K_AR5212 }, /* 5212 combatible */
  90. { PCI_VDEVICE(ATHEROS, 0x001a), .driver_data = AR5K_AR5212 }, /* 2413 Griffin-lite */
  91. { PCI_VDEVICE(ATHEROS, 0x001b), .driver_data = AR5K_AR5212 }, /* 5413 Eagle */
  92. { PCI_VDEVICE(ATHEROS, 0x001c), .driver_data = AR5K_AR5212 }, /* 5424 Condor (PCI-E)*/
  93. { PCI_VDEVICE(ATHEROS, 0x0023), .driver_data = AR5K_AR5212 }, /* 5416 */
  94. { PCI_VDEVICE(ATHEROS, 0x0024), .driver_data = AR5K_AR5212 }, /* 5418 */
  95. { 0 }
  96. };
  97. MODULE_DEVICE_TABLE(pci, ath5k_pci_id_table);
  98. /* Known SREVs */
  99. static struct ath5k_srev_name srev_names[] = {
  100. { "5210", AR5K_VERSION_VER, AR5K_SREV_VER_AR5210 },
  101. { "5311", AR5K_VERSION_VER, AR5K_SREV_VER_AR5311 },
  102. { "5311A", AR5K_VERSION_VER, AR5K_SREV_VER_AR5311A },
  103. { "5311B", AR5K_VERSION_VER, AR5K_SREV_VER_AR5311B },
  104. { "5211", AR5K_VERSION_VER, AR5K_SREV_VER_AR5211 },
  105. { "5212", AR5K_VERSION_VER, AR5K_SREV_VER_AR5212 },
  106. { "5213", AR5K_VERSION_VER, AR5K_SREV_VER_AR5213 },
  107. { "5213A", AR5K_VERSION_VER, AR5K_SREV_VER_AR5213A },
  108. { "2424", AR5K_VERSION_VER, AR5K_SREV_VER_AR2424 },
  109. { "5424", AR5K_VERSION_VER, AR5K_SREV_VER_AR5424 },
  110. { "5413", AR5K_VERSION_VER, AR5K_SREV_VER_AR5413 },
  111. { "5414", AR5K_VERSION_VER, AR5K_SREV_VER_AR5414 },
  112. { "5416", AR5K_VERSION_VER, AR5K_SREV_VER_AR5416 },
  113. { "5418", AR5K_VERSION_VER, AR5K_SREV_VER_AR5418 },
  114. { "xxxxx", AR5K_VERSION_VER, AR5K_SREV_UNKNOWN },
  115. { "5110", AR5K_VERSION_RAD, AR5K_SREV_RAD_5110 },
  116. { "5111", AR5K_VERSION_RAD, AR5K_SREV_RAD_5111 },
  117. { "2111", AR5K_VERSION_RAD, AR5K_SREV_RAD_2111 },
  118. { "5112", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112 },
  119. { "5112A", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112A },
  120. { "2112", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112 },
  121. { "2112A", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112A },
  122. { "SChip", AR5K_VERSION_RAD, AR5K_SREV_RAD_SC1 },
  123. { "SChip", AR5K_VERSION_RAD, AR5K_SREV_RAD_SC2 },
  124. { "5133", AR5K_VERSION_RAD, AR5K_SREV_RAD_5133 },
  125. { "xxxxx", AR5K_VERSION_RAD, AR5K_SREV_UNKNOWN },
  126. };
  127. /*
  128. * Prototypes - PCI stack related functions
  129. */
  130. static int __devinit ath5k_pci_probe(struct pci_dev *pdev,
  131. const struct pci_device_id *id);
  132. static void __devexit ath5k_pci_remove(struct pci_dev *pdev);
  133. #ifdef CONFIG_PM
  134. static int ath5k_pci_suspend(struct pci_dev *pdev,
  135. pm_message_t state);
  136. static int ath5k_pci_resume(struct pci_dev *pdev);
  137. #else
  138. #define ath5k_pci_suspend NULL
  139. #define ath5k_pci_resume NULL
  140. #endif /* CONFIG_PM */
  141. static struct pci_driver ath5k_pci_driver = {
  142. .name = "ath5k_pci",
  143. .id_table = ath5k_pci_id_table,
  144. .probe = ath5k_pci_probe,
  145. .remove = __devexit_p(ath5k_pci_remove),
  146. .suspend = ath5k_pci_suspend,
  147. .resume = ath5k_pci_resume,
  148. };
  149. /*
  150. * Prototypes - MAC 802.11 stack related functions
  151. */
  152. static int ath5k_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
  153. struct ieee80211_tx_control *ctl);
  154. static int ath5k_reset(struct ieee80211_hw *hw);
  155. static int ath5k_start(struct ieee80211_hw *hw);
  156. static void ath5k_stop(struct ieee80211_hw *hw);
  157. static int ath5k_add_interface(struct ieee80211_hw *hw,
  158. struct ieee80211_if_init_conf *conf);
  159. static void ath5k_remove_interface(struct ieee80211_hw *hw,
  160. struct ieee80211_if_init_conf *conf);
  161. static int ath5k_config(struct ieee80211_hw *hw,
  162. struct ieee80211_conf *conf);
  163. static int ath5k_config_interface(struct ieee80211_hw *hw,
  164. struct ieee80211_vif *vif,
  165. struct ieee80211_if_conf *conf);
  166. static void ath5k_configure_filter(struct ieee80211_hw *hw,
  167. unsigned int changed_flags,
  168. unsigned int *new_flags,
  169. int mc_count, struct dev_mc_list *mclist);
  170. static int ath5k_set_key(struct ieee80211_hw *hw,
  171. enum set_key_cmd cmd,
  172. const u8 *local_addr, const u8 *addr,
  173. struct ieee80211_key_conf *key);
  174. static int ath5k_get_stats(struct ieee80211_hw *hw,
  175. struct ieee80211_low_level_stats *stats);
  176. static int ath5k_get_tx_stats(struct ieee80211_hw *hw,
  177. struct ieee80211_tx_queue_stats *stats);
  178. static u64 ath5k_get_tsf(struct ieee80211_hw *hw);
  179. static void ath5k_reset_tsf(struct ieee80211_hw *hw);
  180. static int ath5k_beacon_update(struct ieee80211_hw *hw,
  181. struct sk_buff *skb,
  182. struct ieee80211_tx_control *ctl);
  183. static struct ieee80211_ops ath5k_hw_ops = {
  184. .tx = ath5k_tx,
  185. .start = ath5k_start,
  186. .stop = ath5k_stop,
  187. .add_interface = ath5k_add_interface,
  188. .remove_interface = ath5k_remove_interface,
  189. .config = ath5k_config,
  190. .config_interface = ath5k_config_interface,
  191. .configure_filter = ath5k_configure_filter,
  192. .set_key = ath5k_set_key,
  193. .get_stats = ath5k_get_stats,
  194. .conf_tx = NULL,
  195. .get_tx_stats = ath5k_get_tx_stats,
  196. .get_tsf = ath5k_get_tsf,
  197. .reset_tsf = ath5k_reset_tsf,
  198. .beacon_update = ath5k_beacon_update,
  199. };
  200. /*
  201. * Prototypes - Internal functions
  202. */
  203. /* Attach detach */
  204. static int ath5k_attach(struct pci_dev *pdev,
  205. struct ieee80211_hw *hw);
  206. static void ath5k_detach(struct pci_dev *pdev,
  207. struct ieee80211_hw *hw);
  208. /* Channel/mode setup */
  209. static inline short ath5k_ieee2mhz(short chan);
  210. static unsigned int ath5k_copy_rates(struct ieee80211_rate *rates,
  211. const struct ath5k_rate_table *rt,
  212. unsigned int max);
  213. static unsigned int ath5k_copy_channels(struct ath5k_hw *ah,
  214. struct ieee80211_channel *channels,
  215. unsigned int mode,
  216. unsigned int max);
  217. static int ath5k_getchannels(struct ieee80211_hw *hw);
  218. static int ath5k_chan_set(struct ath5k_softc *sc,
  219. struct ieee80211_channel *chan);
  220. static void ath5k_setcurmode(struct ath5k_softc *sc,
  221. unsigned int mode);
  222. static void ath5k_mode_setup(struct ath5k_softc *sc);
  223. /* Descriptor setup */
  224. static int ath5k_desc_alloc(struct ath5k_softc *sc,
  225. struct pci_dev *pdev);
  226. static void ath5k_desc_free(struct ath5k_softc *sc,
  227. struct pci_dev *pdev);
  228. /* Buffers setup */
  229. static int ath5k_rxbuf_setup(struct ath5k_softc *sc,
  230. struct ath5k_buf *bf);
  231. static int ath5k_txbuf_setup(struct ath5k_softc *sc,
  232. struct ath5k_buf *bf,
  233. struct ieee80211_tx_control *ctl);
  234. static inline void ath5k_txbuf_free(struct ath5k_softc *sc,
  235. struct ath5k_buf *bf)
  236. {
  237. BUG_ON(!bf);
  238. if (!bf->skb)
  239. return;
  240. pci_unmap_single(sc->pdev, bf->skbaddr, bf->skb->len,
  241. PCI_DMA_TODEVICE);
  242. dev_kfree_skb(bf->skb);
  243. bf->skb = NULL;
  244. }
  245. /* Queues setup */
  246. static struct ath5k_txq *ath5k_txq_setup(struct ath5k_softc *sc,
  247. int qtype, int subtype);
  248. static int ath5k_beaconq_setup(struct ath5k_hw *ah);
  249. static int ath5k_beaconq_config(struct ath5k_softc *sc);
  250. static void ath5k_txq_drainq(struct ath5k_softc *sc,
  251. struct ath5k_txq *txq);
  252. static void ath5k_txq_cleanup(struct ath5k_softc *sc);
  253. static void ath5k_txq_release(struct ath5k_softc *sc);
  254. /* Rx handling */
  255. static int ath5k_rx_start(struct ath5k_softc *sc);
  256. static void ath5k_rx_stop(struct ath5k_softc *sc);
  257. static unsigned int ath5k_rx_decrypted(struct ath5k_softc *sc,
  258. struct ath5k_desc *ds,
  259. struct sk_buff *skb);
  260. static void ath5k_tasklet_rx(unsigned long data);
  261. /* Tx handling */
  262. static void ath5k_tx_processq(struct ath5k_softc *sc,
  263. struct ath5k_txq *txq);
  264. static void ath5k_tasklet_tx(unsigned long data);
  265. /* Beacon handling */
  266. static int ath5k_beacon_setup(struct ath5k_softc *sc,
  267. struct ath5k_buf *bf,
  268. struct ieee80211_tx_control *ctl);
  269. static void ath5k_beacon_send(struct ath5k_softc *sc);
  270. static void ath5k_beacon_config(struct ath5k_softc *sc);
  271. static void ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf);
  272. static inline u64 ath5k_extend_tsf(struct ath5k_hw *ah, u32 rstamp)
  273. {
  274. u64 tsf = ath5k_hw_get_tsf64(ah);
  275. if ((tsf & 0x7fff) < rstamp)
  276. tsf -= 0x8000;
  277. return (tsf & ~0x7fff) | rstamp;
  278. }
  279. /* Interrupt handling */
  280. static int ath5k_init(struct ath5k_softc *sc);
  281. static int ath5k_stop_locked(struct ath5k_softc *sc);
  282. static int ath5k_stop_hw(struct ath5k_softc *sc);
  283. static irqreturn_t ath5k_intr(int irq, void *dev_id);
  284. static void ath5k_tasklet_reset(unsigned long data);
  285. static void ath5k_calibrate(unsigned long data);
  286. /* LED functions */
  287. static void ath5k_led_off(unsigned long data);
  288. static void ath5k_led_blink(struct ath5k_softc *sc,
  289. unsigned int on,
  290. unsigned int off);
  291. static void ath5k_led_event(struct ath5k_softc *sc,
  292. int event);
  293. /*
  294. * Module init/exit functions
  295. */
  296. static int __init
  297. init_ath5k_pci(void)
  298. {
  299. int ret;
  300. ath5k_debug_init();
  301. ret = pci_register_driver(&ath5k_pci_driver);
  302. if (ret) {
  303. printk(KERN_ERR "ath5k_pci: can't register pci driver\n");
  304. return ret;
  305. }
  306. return 0;
  307. }
  308. static void __exit
  309. exit_ath5k_pci(void)
  310. {
  311. pci_unregister_driver(&ath5k_pci_driver);
  312. ath5k_debug_finish();
  313. }
  314. module_init(init_ath5k_pci);
  315. module_exit(exit_ath5k_pci);
  316. /********************\
  317. * PCI Initialization *
  318. \********************/
  319. static const char *
  320. ath5k_chip_name(enum ath5k_srev_type type, u_int16_t val)
  321. {
  322. const char *name = "xxxxx";
  323. unsigned int i;
  324. for (i = 0; i < ARRAY_SIZE(srev_names); i++) {
  325. if (srev_names[i].sr_type != type)
  326. continue;
  327. if ((val & 0xff) < srev_names[i + 1].sr_val) {
  328. name = srev_names[i].sr_name;
  329. break;
  330. }
  331. }
  332. return name;
  333. }
  334. static int __devinit
  335. ath5k_pci_probe(struct pci_dev *pdev,
  336. const struct pci_device_id *id)
  337. {
  338. void __iomem *mem;
  339. struct ath5k_softc *sc;
  340. struct ieee80211_hw *hw;
  341. int ret;
  342. u8 csz;
  343. ret = pci_enable_device(pdev);
  344. if (ret) {
  345. dev_err(&pdev->dev, "can't enable device\n");
  346. goto err;
  347. }
  348. /* XXX 32-bit addressing only */
  349. ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  350. if (ret) {
  351. dev_err(&pdev->dev, "32-bit DMA not available\n");
  352. goto err_dis;
  353. }
  354. /*
  355. * Cache line size is used to size and align various
  356. * structures used to communicate with the hardware.
  357. */
  358. pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
  359. if (csz == 0) {
  360. /*
  361. * Linux 2.4.18 (at least) writes the cache line size
  362. * register as a 16-bit wide register which is wrong.
  363. * We must have this setup properly for rx buffer
  364. * DMA to work so force a reasonable value here if it
  365. * comes up zero.
  366. */
  367. csz = L1_CACHE_BYTES / sizeof(u32);
  368. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
  369. }
  370. /*
  371. * The default setting of latency timer yields poor results,
  372. * set it to the value used by other systems. It may be worth
  373. * tweaking this setting more.
  374. */
  375. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
  376. /* Enable bus mastering */
  377. pci_set_master(pdev);
  378. /*
  379. * Disable the RETRY_TIMEOUT register (0x41) to keep
  380. * PCI Tx retries from interfering with C3 CPU state.
  381. */
  382. pci_write_config_byte(pdev, 0x41, 0);
  383. ret = pci_request_region(pdev, 0, "ath5k");
  384. if (ret) {
  385. dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
  386. goto err_dis;
  387. }
  388. mem = pci_iomap(pdev, 0, 0);
  389. if (!mem) {
  390. dev_err(&pdev->dev, "cannot remap PCI memory region\n") ;
  391. ret = -EIO;
  392. goto err_reg;
  393. }
  394. /*
  395. * Allocate hw (mac80211 main struct)
  396. * and hw->priv (driver private data)
  397. */
  398. hw = ieee80211_alloc_hw(sizeof(*sc), &ath5k_hw_ops);
  399. if (hw == NULL) {
  400. dev_err(&pdev->dev, "cannot allocate ieee80211_hw\n");
  401. ret = -ENOMEM;
  402. goto err_map;
  403. }
  404. dev_info(&pdev->dev, "registered as '%s'\n", wiphy_name(hw->wiphy));
  405. /* Initialize driver private data */
  406. SET_IEEE80211_DEV(hw, &pdev->dev);
  407. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS;
  408. hw->extra_tx_headroom = 2;
  409. hw->channel_change_time = 5000;
  410. /* these names are misleading */
  411. hw->max_rssi = -110; /* signal in dBm */
  412. hw->max_noise = -110; /* noise in dBm */
  413. hw->max_signal = 100; /* we will provide a percentage based on rssi */
  414. sc = hw->priv;
  415. sc->hw = hw;
  416. sc->pdev = pdev;
  417. ath5k_debug_init_device(sc);
  418. /*
  419. * Mark the device as detached to avoid processing
  420. * interrupts until setup is complete.
  421. */
  422. __set_bit(ATH_STAT_INVALID, sc->status);
  423. sc->iobase = mem; /* So we can unmap it on detach */
  424. sc->cachelsz = csz * sizeof(u32); /* convert to bytes */
  425. sc->opmode = IEEE80211_IF_TYPE_STA;
  426. mutex_init(&sc->lock);
  427. spin_lock_init(&sc->rxbuflock);
  428. spin_lock_init(&sc->txbuflock);
  429. /* Set private data */
  430. pci_set_drvdata(pdev, hw);
  431. /* Enable msi for devices that support it */
  432. pci_enable_msi(pdev);
  433. /* Setup interrupt handler */
  434. ret = request_irq(pdev->irq, ath5k_intr, IRQF_SHARED, "ath", sc);
  435. if (ret) {
  436. ATH5K_ERR(sc, "request_irq failed\n");
  437. goto err_free;
  438. }
  439. /* Initialize device */
  440. sc->ah = ath5k_hw_attach(sc, id->driver_data);
  441. if (IS_ERR(sc->ah)) {
  442. ret = PTR_ERR(sc->ah);
  443. goto err_irq;
  444. }
  445. /* Finish private driver data initialization */
  446. ret = ath5k_attach(pdev, hw);
  447. if (ret)
  448. goto err_ah;
  449. ATH5K_INFO(sc, "Atheros AR%s chip found (MAC: 0x%x, PHY: 0x%x)\n",
  450. ath5k_chip_name(AR5K_VERSION_VER,sc->ah->ah_mac_srev),
  451. sc->ah->ah_mac_srev,
  452. sc->ah->ah_phy_revision);
  453. if(!sc->ah->ah_single_chip){
  454. /* Single chip radio (!RF5111) */
  455. if(sc->ah->ah_radio_5ghz_revision && !sc->ah->ah_radio_2ghz_revision) {
  456. /* No 5GHz support -> report 2GHz radio */
  457. if(!test_bit(MODE_IEEE80211A, sc->ah->ah_capabilities.cap_mode)){
  458. ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
  459. ath5k_chip_name(AR5K_VERSION_RAD,sc->ah->ah_radio_5ghz_revision),
  460. sc->ah->ah_radio_5ghz_revision);
  461. /* No 2GHz support (5110 and some 5Ghz only cards) -> report 5Ghz radio */
  462. } else if(!test_bit(MODE_IEEE80211B, sc->ah->ah_capabilities.cap_mode)){
  463. ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
  464. ath5k_chip_name(AR5K_VERSION_RAD,sc->ah->ah_radio_5ghz_revision),
  465. sc->ah->ah_radio_5ghz_revision);
  466. /* Multiband radio */
  467. } else {
  468. ATH5K_INFO(sc, "RF%s multiband radio found"
  469. " (0x%x)\n",
  470. ath5k_chip_name(AR5K_VERSION_RAD,sc->ah->ah_radio_5ghz_revision),
  471. sc->ah->ah_radio_5ghz_revision);
  472. }
  473. }
  474. /* Multi chip radio (RF5111 - RF2111) -> report both 2GHz/5GHz radios */
  475. else if(sc->ah->ah_radio_5ghz_revision && sc->ah->ah_radio_2ghz_revision){
  476. ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
  477. ath5k_chip_name(AR5K_VERSION_RAD,sc->ah->ah_radio_5ghz_revision),
  478. sc->ah->ah_radio_5ghz_revision);
  479. ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
  480. ath5k_chip_name(AR5K_VERSION_RAD,sc->ah->ah_radio_2ghz_revision),
  481. sc->ah->ah_radio_2ghz_revision);
  482. }
  483. }
  484. /* ready to process interrupts */
  485. __clear_bit(ATH_STAT_INVALID, sc->status);
  486. return 0;
  487. err_ah:
  488. ath5k_hw_detach(sc->ah);
  489. err_irq:
  490. free_irq(pdev->irq, sc);
  491. err_free:
  492. pci_disable_msi(pdev);
  493. ieee80211_free_hw(hw);
  494. err_map:
  495. pci_iounmap(pdev, mem);
  496. err_reg:
  497. pci_release_region(pdev, 0);
  498. err_dis:
  499. pci_disable_device(pdev);
  500. err:
  501. return ret;
  502. }
  503. static void __devexit
  504. ath5k_pci_remove(struct pci_dev *pdev)
  505. {
  506. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  507. struct ath5k_softc *sc = hw->priv;
  508. ath5k_debug_finish_device(sc);
  509. ath5k_detach(pdev, hw);
  510. ath5k_hw_detach(sc->ah);
  511. free_irq(pdev->irq, sc);
  512. pci_disable_msi(pdev);
  513. pci_iounmap(pdev, sc->iobase);
  514. pci_release_region(pdev, 0);
  515. pci_disable_device(pdev);
  516. ieee80211_free_hw(hw);
  517. }
  518. #ifdef CONFIG_PM
  519. static int
  520. ath5k_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  521. {
  522. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  523. struct ath5k_softc *sc = hw->priv;
  524. if (test_bit(ATH_STAT_LEDSOFT, sc->status))
  525. ath5k_hw_set_gpio(sc->ah, sc->led_pin, 1);
  526. ath5k_stop_hw(sc);
  527. pci_save_state(pdev);
  528. pci_disable_device(pdev);
  529. pci_set_power_state(pdev, PCI_D3hot);
  530. return 0;
  531. }
  532. static int
  533. ath5k_pci_resume(struct pci_dev *pdev)
  534. {
  535. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  536. struct ath5k_softc *sc = hw->priv;
  537. struct ath5k_hw *ah = sc->ah;
  538. int i, err;
  539. err = pci_set_power_state(pdev, PCI_D0);
  540. if (err)
  541. return err;
  542. err = pci_enable_device(pdev);
  543. if (err)
  544. return err;
  545. pci_restore_state(pdev);
  546. /*
  547. * Suspend/Resume resets the PCI configuration space, so we have to
  548. * re-disable the RETRY_TIMEOUT register (0x41) to keep
  549. * PCI Tx retries from interfering with C3 CPU state
  550. */
  551. pci_write_config_byte(pdev, 0x41, 0);
  552. ath5k_init(sc);
  553. if (test_bit(ATH_STAT_LEDSOFT, sc->status)) {
  554. ath5k_hw_set_gpio_output(ah, sc->led_pin);
  555. ath5k_hw_set_gpio(ah, sc->led_pin, 0);
  556. }
  557. /*
  558. * Reset the key cache since some parts do not
  559. * reset the contents on initial power up or resume.
  560. *
  561. * FIXME: This may need to be revisited when mac80211 becomes
  562. * aware of suspend/resume.
  563. */
  564. for (i = 0; i < AR5K_KEYTABLE_SIZE; i++)
  565. ath5k_hw_reset_key(ah, i);
  566. return 0;
  567. }
  568. #endif /* CONFIG_PM */
  569. /***********************\
  570. * Driver Initialization *
  571. \***********************/
  572. static int
  573. ath5k_attach(struct pci_dev *pdev, struct ieee80211_hw *hw)
  574. {
  575. struct ath5k_softc *sc = hw->priv;
  576. struct ath5k_hw *ah = sc->ah;
  577. u8 mac[ETH_ALEN];
  578. unsigned int i;
  579. int ret;
  580. ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "devid 0x%x\n", pdev->device);
  581. /*
  582. * Check if the MAC has multi-rate retry support.
  583. * We do this by trying to setup a fake extended
  584. * descriptor. MAC's that don't have support will
  585. * return false w/o doing anything. MAC's that do
  586. * support it will return true w/o doing anything.
  587. */
  588. if (ah->ah_setup_xtx_desc(ah, NULL, 0, 0, 0, 0, 0, 0))
  589. __set_bit(ATH_STAT_MRRETRY, sc->status);
  590. /*
  591. * Reset the key cache since some parts do not
  592. * reset the contents on initial power up.
  593. */
  594. for (i = 0; i < AR5K_KEYTABLE_SIZE; i++)
  595. ath5k_hw_reset_key(ah, i);
  596. /*
  597. * Collect the channel list. The 802.11 layer
  598. * is resposible for filtering this list based
  599. * on settings like the phy mode and regulatory
  600. * domain restrictions.
  601. */
  602. ret = ath5k_getchannels(hw);
  603. if (ret) {
  604. ATH5K_ERR(sc, "can't get channels\n");
  605. goto err;
  606. }
  607. /* NB: setup here so ath5k_rate_update is happy */
  608. if (test_bit(MODE_IEEE80211A, ah->ah_modes))
  609. ath5k_setcurmode(sc, MODE_IEEE80211A);
  610. else
  611. ath5k_setcurmode(sc, MODE_IEEE80211B);
  612. /*
  613. * Allocate tx+rx descriptors and populate the lists.
  614. */
  615. ret = ath5k_desc_alloc(sc, pdev);
  616. if (ret) {
  617. ATH5K_ERR(sc, "can't allocate descriptors\n");
  618. goto err;
  619. }
  620. /*
  621. * Allocate hardware transmit queues: one queue for
  622. * beacon frames and one data queue for each QoS
  623. * priority. Note that hw functions handle reseting
  624. * these queues at the needed time.
  625. */
  626. ret = ath5k_beaconq_setup(ah);
  627. if (ret < 0) {
  628. ATH5K_ERR(sc, "can't setup a beacon xmit queue\n");
  629. goto err_desc;
  630. }
  631. sc->bhalq = ret;
  632. sc->txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BK);
  633. if (IS_ERR(sc->txq)) {
  634. ATH5K_ERR(sc, "can't setup xmit queue\n");
  635. ret = PTR_ERR(sc->txq);
  636. goto err_bhal;
  637. }
  638. tasklet_init(&sc->rxtq, ath5k_tasklet_rx, (unsigned long)sc);
  639. tasklet_init(&sc->txtq, ath5k_tasklet_tx, (unsigned long)sc);
  640. tasklet_init(&sc->restq, ath5k_tasklet_reset, (unsigned long)sc);
  641. setup_timer(&sc->calib_tim, ath5k_calibrate, (unsigned long)sc);
  642. setup_timer(&sc->led_tim, ath5k_led_off, (unsigned long)sc);
  643. sc->led_on = 0; /* low true */
  644. /*
  645. * Auto-enable soft led processing for IBM cards and for
  646. * 5211 minipci cards.
  647. */
  648. if (pdev->device == PCI_DEVICE_ID_ATHEROS_AR5212_IBM ||
  649. pdev->device == PCI_DEVICE_ID_ATHEROS_AR5211) {
  650. __set_bit(ATH_STAT_LEDSOFT, sc->status);
  651. sc->led_pin = 0;
  652. }
  653. /* Enable softled on PIN1 on HP Compaq nc6xx, nc4000 & nx5000 laptops */
  654. if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ) {
  655. __set_bit(ATH_STAT_LEDSOFT, sc->status);
  656. sc->led_pin = 0;
  657. }
  658. if (test_bit(ATH_STAT_LEDSOFT, sc->status)) {
  659. ath5k_hw_set_gpio_output(ah, sc->led_pin);
  660. ath5k_hw_set_gpio(ah, sc->led_pin, !sc->led_on);
  661. }
  662. ath5k_hw_get_lladdr(ah, mac);
  663. SET_IEEE80211_PERM_ADDR(hw, mac);
  664. /* All MAC address bits matter for ACKs */
  665. memset(sc->bssidmask, 0xff, ETH_ALEN);
  666. ath5k_hw_set_bssid_mask(sc->ah, sc->bssidmask);
  667. ret = ieee80211_register_hw(hw);
  668. if (ret) {
  669. ATH5K_ERR(sc, "can't register ieee80211 hw\n");
  670. goto err_queues;
  671. }
  672. return 0;
  673. err_queues:
  674. ath5k_txq_release(sc);
  675. err_bhal:
  676. ath5k_hw_release_tx_queue(ah, sc->bhalq);
  677. err_desc:
  678. ath5k_desc_free(sc, pdev);
  679. err:
  680. return ret;
  681. }
  682. static void
  683. ath5k_detach(struct pci_dev *pdev, struct ieee80211_hw *hw)
  684. {
  685. struct ath5k_softc *sc = hw->priv;
  686. /*
  687. * NB: the order of these is important:
  688. * o call the 802.11 layer before detaching ath5k_hw to
  689. * insure callbacks into the driver to delete global
  690. * key cache entries can be handled
  691. * o reclaim the tx queue data structures after calling
  692. * the 802.11 layer as we'll get called back to reclaim
  693. * node state and potentially want to use them
  694. * o to cleanup the tx queues the hal is called, so detach
  695. * it last
  696. * XXX: ??? detach ath5k_hw ???
  697. * Other than that, it's straightforward...
  698. */
  699. ieee80211_unregister_hw(hw);
  700. ath5k_desc_free(sc, pdev);
  701. ath5k_txq_release(sc);
  702. ath5k_hw_release_tx_queue(sc->ah, sc->bhalq);
  703. /*
  704. * NB: can't reclaim these until after ieee80211_ifdetach
  705. * returns because we'll get called back to reclaim node
  706. * state and potentially want to use them.
  707. */
  708. }
  709. /********************\
  710. * Channel/mode setup *
  711. \********************/
  712. /*
  713. * Convert IEEE channel number to MHz frequency.
  714. */
  715. static inline short
  716. ath5k_ieee2mhz(short chan)
  717. {
  718. if (chan <= 14 || chan >= 27)
  719. return ieee80211chan2mhz(chan);
  720. else
  721. return 2212 + chan * 20;
  722. }
  723. static unsigned int
  724. ath5k_copy_rates(struct ieee80211_rate *rates,
  725. const struct ath5k_rate_table *rt,
  726. unsigned int max)
  727. {
  728. unsigned int i, count;
  729. if (rt == NULL)
  730. return 0;
  731. for (i = 0, count = 0; i < rt->rate_count && max > 0; i++) {
  732. if (!rt->rates[i].valid)
  733. continue;
  734. rates->rate = rt->rates[i].rate_kbps / 100;
  735. rates->val = rt->rates[i].rate_code;
  736. rates->flags = rt->rates[i].modulation;
  737. rates++;
  738. count++;
  739. max--;
  740. }
  741. return count;
  742. }
  743. static unsigned int
  744. ath5k_copy_channels(struct ath5k_hw *ah,
  745. struct ieee80211_channel *channels,
  746. unsigned int mode,
  747. unsigned int max)
  748. {
  749. static const struct { unsigned int mode, mask, chan; } map[] = {
  750. [MODE_IEEE80211A] = { CHANNEL_OFDM, CHANNEL_OFDM | CHANNEL_TURBO, CHANNEL_A },
  751. [MODE_ATHEROS_TURBO] = { CHANNEL_OFDM|CHANNEL_TURBO, CHANNEL_OFDM | CHANNEL_TURBO, CHANNEL_T },
  752. [MODE_IEEE80211B] = { CHANNEL_CCK, CHANNEL_CCK, CHANNEL_B },
  753. [MODE_IEEE80211G] = { CHANNEL_OFDM, CHANNEL_OFDM, CHANNEL_G },
  754. [MODE_ATHEROS_TURBOG] = { CHANNEL_OFDM | CHANNEL_TURBO, CHANNEL_OFDM | CHANNEL_TURBO, CHANNEL_TG },
  755. };
  756. static const struct ath5k_regchannel chans_2ghz[] =
  757. IEEE80211_CHANNELS_2GHZ;
  758. static const struct ath5k_regchannel chans_5ghz[] =
  759. IEEE80211_CHANNELS_5GHZ;
  760. const struct ath5k_regchannel *chans;
  761. enum ath5k_regdom dmn;
  762. unsigned int i, count, size, chfreq, all, f, ch;
  763. if (!test_bit(mode, ah->ah_modes))
  764. return 0;
  765. all = ah->ah_regdomain == DMN_DEFAULT || CHAN_DEBUG == 1;
  766. switch (mode) {
  767. case MODE_IEEE80211A:
  768. case MODE_ATHEROS_TURBO:
  769. /* 1..220, but 2GHz frequencies are filtered by check_channel */
  770. size = all ? 220 : ARRAY_SIZE(chans_5ghz);
  771. chans = chans_5ghz;
  772. dmn = ath5k_regdom2flag(ah->ah_regdomain,
  773. IEEE80211_CHANNELS_5GHZ_MIN);
  774. chfreq = CHANNEL_5GHZ;
  775. break;
  776. case MODE_IEEE80211B:
  777. case MODE_IEEE80211G:
  778. case MODE_ATHEROS_TURBOG:
  779. size = all ? 26 : ARRAY_SIZE(chans_2ghz);
  780. chans = chans_2ghz;
  781. dmn = ath5k_regdom2flag(ah->ah_regdomain,
  782. IEEE80211_CHANNELS_2GHZ_MIN);
  783. chfreq = CHANNEL_2GHZ;
  784. break;
  785. default:
  786. ATH5K_WARN(ah->ah_sc, "bad mode, not copying channels\n");
  787. return 0;
  788. }
  789. for (i = 0, count = 0; i < size && max > 0; i++) {
  790. ch = all ? i + 1 : chans[i].chan;
  791. f = ath5k_ieee2mhz(ch);
  792. /* Check if channel is supported by the chipset */
  793. if (!ath5k_channel_ok(ah, f, chfreq))
  794. continue;
  795. /* Match regulation domain */
  796. if (!all && !(IEEE80211_DMN(chans[i].domain) &
  797. IEEE80211_DMN(dmn)))
  798. continue;
  799. if (!all && (chans[i].mode & map[mode].mask) != map[mode].mode)
  800. continue;
  801. /* Write channel and increment counter */
  802. channels->chan = ch;
  803. channels->freq = f;
  804. channels->val = map[mode].chan;
  805. channels++;
  806. count++;
  807. max--;
  808. }
  809. return count;
  810. }
  811. /* Only tries to register modes our EEPROM says it can support */
  812. #define REGISTER_MODE(m) do { \
  813. ret = ath5k_register_mode(hw, m); \
  814. if (ret) \
  815. return ret; \
  816. } while (0) \
  817. static inline int
  818. ath5k_register_mode(struct ieee80211_hw *hw, u8 m)
  819. {
  820. struct ath5k_softc *sc = hw->priv;
  821. struct ieee80211_hw_mode *modes = sc->modes;
  822. unsigned int i;
  823. int ret;
  824. if (!test_bit(m, sc->ah->ah_capabilities.cap_mode))
  825. return 0;
  826. for (i = 0; i < NUM_DRIVER_MODES; i++) {
  827. if (modes[i].mode != m || !modes[i].num_channels)
  828. continue;
  829. ret = ieee80211_register_hwmode(hw, &modes[i]);
  830. if (ret) {
  831. ATH5K_ERR(sc, "can't register hwmode %u\n", m);
  832. return ret;
  833. }
  834. return 0;
  835. }
  836. BUG();
  837. }
  838. static int
  839. ath5k_getchannels(struct ieee80211_hw *hw)
  840. {
  841. struct ath5k_softc *sc = hw->priv;
  842. struct ath5k_hw *ah = sc->ah;
  843. struct ieee80211_hw_mode *modes = sc->modes;
  844. unsigned int i, max_r, max_c;
  845. int ret;
  846. BUILD_BUG_ON(ARRAY_SIZE(sc->modes) < 3);
  847. /* The order here does not matter */
  848. modes[0].mode = MODE_IEEE80211G;
  849. modes[1].mode = MODE_IEEE80211B;
  850. modes[2].mode = MODE_IEEE80211A;
  851. max_r = ARRAY_SIZE(sc->rates);
  852. max_c = ARRAY_SIZE(sc->channels);
  853. for (i = 0; i < NUM_DRIVER_MODES; i++) {
  854. struct ieee80211_hw_mode *mode = &modes[i];
  855. const struct ath5k_rate_table *hw_rates;
  856. if (i == 0) {
  857. modes[0].rates = sc->rates;
  858. modes->channels = sc->channels;
  859. } else {
  860. struct ieee80211_hw_mode *prev_mode = &modes[i-1];
  861. int prev_num_r = prev_mode->num_rates;
  862. int prev_num_c = prev_mode->num_channels;
  863. mode->rates = &prev_mode->rates[prev_num_r];
  864. mode->channels = &prev_mode->channels[prev_num_c];
  865. }
  866. hw_rates = ath5k_hw_get_rate_table(ah, mode->mode);
  867. mode->num_rates = ath5k_copy_rates(mode->rates, hw_rates,
  868. max_r);
  869. mode->num_channels = ath5k_copy_channels(ah, mode->channels,
  870. mode->mode, max_c);
  871. max_r -= mode->num_rates;
  872. max_c -= mode->num_channels;
  873. }
  874. /* We try to register all modes this driver supports. We don't bother
  875. * with MODE_IEEE80211B for AR5212 as MODE_IEEE80211G already accounts
  876. * for that as per mac80211. Then, REGISTER_MODE() will will actually
  877. * check the eeprom reading for more reliable capability information.
  878. * Order matters here as per mac80211's latest preference. This will
  879. * all hopefullly soon go away. */
  880. REGISTER_MODE(MODE_IEEE80211G);
  881. if (ah->ah_version != AR5K_AR5212)
  882. REGISTER_MODE(MODE_IEEE80211B);
  883. REGISTER_MODE(MODE_IEEE80211A);
  884. ath5k_debug_dump_modes(sc, modes);
  885. return ret;
  886. }
  887. /*
  888. * Set/change channels. If the channel is really being changed,
  889. * it's done by reseting the chip. To accomplish this we must
  890. * first cleanup any pending DMA, then restart stuff after a la
  891. * ath5k_init.
  892. */
  893. static int
  894. ath5k_chan_set(struct ath5k_softc *sc, struct ieee80211_channel *chan)
  895. {
  896. struct ath5k_hw *ah = sc->ah;
  897. int ret;
  898. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "%u (%u MHz) -> %u (%u MHz)\n",
  899. sc->curchan->chan, sc->curchan->freq,
  900. chan->chan, chan->freq);
  901. if (chan->freq != sc->curchan->freq || chan->val != sc->curchan->val) {
  902. /*
  903. * To switch channels clear any pending DMA operations;
  904. * wait long enough for the RX fifo to drain, reset the
  905. * hardware at the new frequency, and then re-enable
  906. * the relevant bits of the h/w.
  907. */
  908. ath5k_hw_set_intr(ah, 0); /* disable interrupts */
  909. ath5k_txq_cleanup(sc); /* clear pending tx frames */
  910. ath5k_rx_stop(sc); /* turn off frame recv */
  911. ret = ath5k_hw_reset(ah, sc->opmode, chan, true);
  912. if (ret) {
  913. ATH5K_ERR(sc, "%s: unable to reset channel %u "
  914. "(%u Mhz)\n", __func__, chan->chan, chan->freq);
  915. return ret;
  916. }
  917. sc->curchan = chan;
  918. ath5k_hw_set_txpower_limit(sc->ah, 0);
  919. /*
  920. * Re-enable rx framework.
  921. */
  922. ret = ath5k_rx_start(sc);
  923. if (ret) {
  924. ATH5K_ERR(sc, "%s: unable to restart recv logic\n",
  925. __func__);
  926. return ret;
  927. }
  928. /*
  929. * Change channels and update the h/w rate map
  930. * if we're switching; e.g. 11a to 11b/g.
  931. *
  932. * XXX needed?
  933. */
  934. /* ath5k_chan_change(sc, chan); */
  935. ath5k_beacon_config(sc);
  936. /*
  937. * Re-enable interrupts.
  938. */
  939. ath5k_hw_set_intr(ah, sc->imask);
  940. }
  941. return 0;
  942. }
  943. static void
  944. ath5k_setcurmode(struct ath5k_softc *sc, unsigned int mode)
  945. {
  946. if (unlikely(test_bit(ATH_STAT_LEDSOFT, sc->status))) {
  947. /* from Atheros NDIS driver, w/ permission */
  948. static const struct {
  949. u16 rate; /* tx/rx 802.11 rate */
  950. u16 timeOn; /* LED on time (ms) */
  951. u16 timeOff; /* LED off time (ms) */
  952. } blinkrates[] = {
  953. { 108, 40, 10 },
  954. { 96, 44, 11 },
  955. { 72, 50, 13 },
  956. { 48, 57, 14 },
  957. { 36, 67, 16 },
  958. { 24, 80, 20 },
  959. { 22, 100, 25 },
  960. { 18, 133, 34 },
  961. { 12, 160, 40 },
  962. { 10, 200, 50 },
  963. { 6, 240, 58 },
  964. { 4, 267, 66 },
  965. { 2, 400, 100 },
  966. { 0, 500, 130 }
  967. };
  968. const struct ath5k_rate_table *rt =
  969. ath5k_hw_get_rate_table(sc->ah, mode);
  970. unsigned int i, j;
  971. BUG_ON(rt == NULL);
  972. memset(sc->hwmap, 0, sizeof(sc->hwmap));
  973. for (i = 0; i < 32; i++) {
  974. u8 ix = rt->rate_code_to_index[i];
  975. if (ix == 0xff) {
  976. sc->hwmap[i].ledon = msecs_to_jiffies(500);
  977. sc->hwmap[i].ledoff = msecs_to_jiffies(130);
  978. continue;
  979. }
  980. sc->hwmap[i].txflags = IEEE80211_RADIOTAP_F_DATAPAD;
  981. if (SHPREAMBLE_FLAG(ix) || rt->rates[ix].modulation ==
  982. IEEE80211_RATE_OFDM)
  983. sc->hwmap[i].txflags |=
  984. IEEE80211_RADIOTAP_F_SHORTPRE;
  985. /* receive frames include FCS */
  986. sc->hwmap[i].rxflags = sc->hwmap[i].txflags |
  987. IEEE80211_RADIOTAP_F_FCS;
  988. /* setup blink rate table to avoid per-packet lookup */
  989. for (j = 0; j < ARRAY_SIZE(blinkrates) - 1; j++)
  990. if (blinkrates[j].rate == /* XXX why 7f? */
  991. (rt->rates[ix].dot11_rate&0x7f))
  992. break;
  993. sc->hwmap[i].ledon = msecs_to_jiffies(blinkrates[j].
  994. timeOn);
  995. sc->hwmap[i].ledoff = msecs_to_jiffies(blinkrates[j].
  996. timeOff);
  997. }
  998. }
  999. sc->curmode = mode;
  1000. }
  1001. static void
  1002. ath5k_mode_setup(struct ath5k_softc *sc)
  1003. {
  1004. struct ath5k_hw *ah = sc->ah;
  1005. u32 rfilt;
  1006. /* configure rx filter */
  1007. rfilt = sc->filter_flags;
  1008. ath5k_hw_set_rx_filter(ah, rfilt);
  1009. if (ath5k_hw_hasbssidmask(ah))
  1010. ath5k_hw_set_bssid_mask(ah, sc->bssidmask);
  1011. /* configure operational mode */
  1012. ath5k_hw_set_opmode(ah);
  1013. ath5k_hw_set_mcast_filter(ah, 0, 0);
  1014. ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "RX filter 0x%x\n", rfilt);
  1015. }
  1016. /***************\
  1017. * Buffers setup *
  1018. \***************/
  1019. static int
  1020. ath5k_rxbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
  1021. {
  1022. struct ath5k_hw *ah = sc->ah;
  1023. struct sk_buff *skb = bf->skb;
  1024. struct ath5k_desc *ds;
  1025. if (likely(skb == NULL)) {
  1026. unsigned int off;
  1027. /*
  1028. * Allocate buffer with headroom_needed space for the
  1029. * fake physical layer header at the start.
  1030. */
  1031. skb = dev_alloc_skb(sc->rxbufsize + sc->cachelsz - 1);
  1032. if (unlikely(skb == NULL)) {
  1033. ATH5K_ERR(sc, "can't alloc skbuff of size %u\n",
  1034. sc->rxbufsize + sc->cachelsz - 1);
  1035. return -ENOMEM;
  1036. }
  1037. /*
  1038. * Cache-line-align. This is important (for the
  1039. * 5210 at least) as not doing so causes bogus data
  1040. * in rx'd frames.
  1041. */
  1042. off = ((unsigned long)skb->data) % sc->cachelsz;
  1043. if (off != 0)
  1044. skb_reserve(skb, sc->cachelsz - off);
  1045. bf->skb = skb;
  1046. bf->skbaddr = pci_map_single(sc->pdev,
  1047. skb->data, sc->rxbufsize, PCI_DMA_FROMDEVICE);
  1048. if (unlikely(pci_dma_mapping_error(bf->skbaddr))) {
  1049. ATH5K_ERR(sc, "%s: DMA mapping failed\n", __func__);
  1050. dev_kfree_skb(skb);
  1051. bf->skb = NULL;
  1052. return -ENOMEM;
  1053. }
  1054. }
  1055. /*
  1056. * Setup descriptors. For receive we always terminate
  1057. * the descriptor list with a self-linked entry so we'll
  1058. * not get overrun under high load (as can happen with a
  1059. * 5212 when ANI processing enables PHY error frames).
  1060. *
  1061. * To insure the last descriptor is self-linked we create
  1062. * each descriptor as self-linked and add it to the end. As
  1063. * each additional descriptor is added the previous self-linked
  1064. * entry is ``fixed'' naturally. This should be safe even
  1065. * if DMA is happening. When processing RX interrupts we
  1066. * never remove/process the last, self-linked, entry on the
  1067. * descriptor list. This insures the hardware always has
  1068. * someplace to write a new frame.
  1069. */
  1070. ds = bf->desc;
  1071. ds->ds_link = bf->daddr; /* link to self */
  1072. ds->ds_data = bf->skbaddr;
  1073. ath5k_hw_setup_rx_desc(ah, ds,
  1074. skb_tailroom(skb), /* buffer size */
  1075. 0);
  1076. if (sc->rxlink != NULL)
  1077. *sc->rxlink = bf->daddr;
  1078. sc->rxlink = &ds->ds_link;
  1079. return 0;
  1080. }
  1081. static int
  1082. ath5k_txbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf,
  1083. struct ieee80211_tx_control *ctl)
  1084. {
  1085. struct ath5k_hw *ah = sc->ah;
  1086. struct ath5k_txq *txq = sc->txq;
  1087. struct ath5k_desc *ds = bf->desc;
  1088. struct sk_buff *skb = bf->skb;
  1089. unsigned int pktlen, flags, keyidx = AR5K_TXKEYIX_INVALID;
  1090. int ret;
  1091. flags = AR5K_TXDESC_INTREQ | AR5K_TXDESC_CLRDMASK;
  1092. bf->ctl = *ctl;
  1093. /* XXX endianness */
  1094. bf->skbaddr = pci_map_single(sc->pdev, skb->data, skb->len,
  1095. PCI_DMA_TODEVICE);
  1096. if (ctl->flags & IEEE80211_TXCTL_NO_ACK)
  1097. flags |= AR5K_TXDESC_NOACK;
  1098. pktlen = skb->len;
  1099. if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT)) {
  1100. keyidx = ctl->key_idx;
  1101. pktlen += ctl->icv_len;
  1102. }
  1103. ret = ah->ah_setup_tx_desc(ah, ds, pktlen,
  1104. ieee80211_get_hdrlen_from_skb(skb), AR5K_PKT_TYPE_NORMAL,
  1105. (ctl->power_level * 2), ctl->tx_rate, ctl->retry_limit, keyidx, 0, flags, 0, 0);
  1106. if (ret)
  1107. goto err_unmap;
  1108. ds->ds_link = 0;
  1109. ds->ds_data = bf->skbaddr;
  1110. spin_lock_bh(&txq->lock);
  1111. list_add_tail(&bf->list, &txq->q);
  1112. sc->tx_stats.data[txq->qnum].len++;
  1113. if (txq->link == NULL) /* is this first packet? */
  1114. ath5k_hw_put_tx_buf(ah, txq->qnum, bf->daddr);
  1115. else /* no, so only link it */
  1116. *txq->link = bf->daddr;
  1117. txq->link = &ds->ds_link;
  1118. ath5k_hw_tx_start(ah, txq->qnum);
  1119. spin_unlock_bh(&txq->lock);
  1120. return 0;
  1121. err_unmap:
  1122. pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
  1123. return ret;
  1124. }
  1125. /*******************\
  1126. * Descriptors setup *
  1127. \*******************/
  1128. static int
  1129. ath5k_desc_alloc(struct ath5k_softc *sc, struct pci_dev *pdev)
  1130. {
  1131. struct ath5k_desc *ds;
  1132. struct ath5k_buf *bf;
  1133. dma_addr_t da;
  1134. unsigned int i;
  1135. int ret;
  1136. /* allocate descriptors */
  1137. sc->desc_len = sizeof(struct ath5k_desc) *
  1138. (ATH_TXBUF + ATH_RXBUF + ATH_BCBUF + 1);
  1139. sc->desc = pci_alloc_consistent(pdev, sc->desc_len, &sc->desc_daddr);
  1140. if (sc->desc == NULL) {
  1141. ATH5K_ERR(sc, "can't allocate descriptors\n");
  1142. ret = -ENOMEM;
  1143. goto err;
  1144. }
  1145. ds = sc->desc;
  1146. da = sc->desc_daddr;
  1147. ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "DMA map: %p (%zu) -> %llx\n",
  1148. ds, sc->desc_len, (unsigned long long)sc->desc_daddr);
  1149. bf = kcalloc(1 + ATH_TXBUF + ATH_RXBUF + ATH_BCBUF,
  1150. sizeof(struct ath5k_buf), GFP_KERNEL);
  1151. if (bf == NULL) {
  1152. ATH5K_ERR(sc, "can't allocate bufptr\n");
  1153. ret = -ENOMEM;
  1154. goto err_free;
  1155. }
  1156. sc->bufptr = bf;
  1157. INIT_LIST_HEAD(&sc->rxbuf);
  1158. for (i = 0; i < ATH_RXBUF; i++, bf++, ds++, da += sizeof(*ds)) {
  1159. bf->desc = ds;
  1160. bf->daddr = da;
  1161. list_add_tail(&bf->list, &sc->rxbuf);
  1162. }
  1163. INIT_LIST_HEAD(&sc->txbuf);
  1164. sc->txbuf_len = ATH_TXBUF;
  1165. for (i = 0; i < ATH_TXBUF; i++, bf++, ds++,
  1166. da += sizeof(*ds)) {
  1167. bf->desc = ds;
  1168. bf->daddr = da;
  1169. list_add_tail(&bf->list, &sc->txbuf);
  1170. }
  1171. /* beacon buffer */
  1172. bf->desc = ds;
  1173. bf->daddr = da;
  1174. sc->bbuf = bf;
  1175. return 0;
  1176. err_free:
  1177. pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
  1178. err:
  1179. sc->desc = NULL;
  1180. return ret;
  1181. }
  1182. static void
  1183. ath5k_desc_free(struct ath5k_softc *sc, struct pci_dev *pdev)
  1184. {
  1185. struct ath5k_buf *bf;
  1186. ath5k_txbuf_free(sc, sc->bbuf);
  1187. list_for_each_entry(bf, &sc->txbuf, list)
  1188. ath5k_txbuf_free(sc, bf);
  1189. list_for_each_entry(bf, &sc->rxbuf, list)
  1190. ath5k_txbuf_free(sc, bf);
  1191. /* Free memory associated with all descriptors */
  1192. pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
  1193. kfree(sc->bufptr);
  1194. sc->bufptr = NULL;
  1195. }
  1196. /**************\
  1197. * Queues setup *
  1198. \**************/
  1199. static struct ath5k_txq *
  1200. ath5k_txq_setup(struct ath5k_softc *sc,
  1201. int qtype, int subtype)
  1202. {
  1203. struct ath5k_hw *ah = sc->ah;
  1204. struct ath5k_txq *txq;
  1205. struct ath5k_txq_info qi = {
  1206. .tqi_subtype = subtype,
  1207. .tqi_aifs = AR5K_TXQ_USEDEFAULT,
  1208. .tqi_cw_min = AR5K_TXQ_USEDEFAULT,
  1209. .tqi_cw_max = AR5K_TXQ_USEDEFAULT
  1210. };
  1211. int qnum;
  1212. /*
  1213. * Enable interrupts only for EOL and DESC conditions.
  1214. * We mark tx descriptors to receive a DESC interrupt
  1215. * when a tx queue gets deep; otherwise waiting for the
  1216. * EOL to reap descriptors. Note that this is done to
  1217. * reduce interrupt load and this only defers reaping
  1218. * descriptors, never transmitting frames. Aside from
  1219. * reducing interrupts this also permits more concurrency.
  1220. * The only potential downside is if the tx queue backs
  1221. * up in which case the top half of the kernel may backup
  1222. * due to a lack of tx descriptors.
  1223. */
  1224. qi.tqi_flags = AR5K_TXQ_FLAG_TXEOLINT_ENABLE |
  1225. AR5K_TXQ_FLAG_TXDESCINT_ENABLE;
  1226. qnum = ath5k_hw_setup_tx_queue(ah, qtype, &qi);
  1227. if (qnum < 0) {
  1228. /*
  1229. * NB: don't print a message, this happens
  1230. * normally on parts with too few tx queues
  1231. */
  1232. return ERR_PTR(qnum);
  1233. }
  1234. if (qnum >= ARRAY_SIZE(sc->txqs)) {
  1235. ATH5K_ERR(sc, "hw qnum %u out of range, max %tu!\n",
  1236. qnum, ARRAY_SIZE(sc->txqs));
  1237. ath5k_hw_release_tx_queue(ah, qnum);
  1238. return ERR_PTR(-EINVAL);
  1239. }
  1240. txq = &sc->txqs[qnum];
  1241. if (!txq->setup) {
  1242. txq->qnum = qnum;
  1243. txq->link = NULL;
  1244. INIT_LIST_HEAD(&txq->q);
  1245. spin_lock_init(&txq->lock);
  1246. txq->setup = true;
  1247. }
  1248. return &sc->txqs[qnum];
  1249. }
  1250. static int
  1251. ath5k_beaconq_setup(struct ath5k_hw *ah)
  1252. {
  1253. struct ath5k_txq_info qi = {
  1254. .tqi_aifs = AR5K_TXQ_USEDEFAULT,
  1255. .tqi_cw_min = AR5K_TXQ_USEDEFAULT,
  1256. .tqi_cw_max = AR5K_TXQ_USEDEFAULT,
  1257. /* NB: for dynamic turbo, don't enable any other interrupts */
  1258. .tqi_flags = AR5K_TXQ_FLAG_TXDESCINT_ENABLE
  1259. };
  1260. return ath5k_hw_setup_tx_queue(ah, AR5K_TX_QUEUE_BEACON, &qi);
  1261. }
  1262. static int
  1263. ath5k_beaconq_config(struct ath5k_softc *sc)
  1264. {
  1265. struct ath5k_hw *ah = sc->ah;
  1266. struct ath5k_txq_info qi;
  1267. int ret;
  1268. ret = ath5k_hw_get_tx_queueprops(ah, sc->bhalq, &qi);
  1269. if (ret)
  1270. return ret;
  1271. if (sc->opmode == IEEE80211_IF_TYPE_AP) {
  1272. /*
  1273. * Always burst out beacon and CAB traffic
  1274. * (aifs = cwmin = cwmax = 0)
  1275. */
  1276. qi.tqi_aifs = 0;
  1277. qi.tqi_cw_min = 0;
  1278. qi.tqi_cw_max = 0;
  1279. } else if (sc->opmode == IEEE80211_IF_TYPE_IBSS) {
  1280. /*
  1281. * Adhoc mode; backoff between 0 and (2 * cw_min).
  1282. */
  1283. qi.tqi_aifs = 0;
  1284. qi.tqi_cw_min = 0;
  1285. qi.tqi_cw_max = 2 * ah->ah_cw_min;
  1286. }
  1287. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
  1288. "beacon queueprops tqi_aifs:%d tqi_cw_min:%d tqi_cw_max:%d\n",
  1289. qi.tqi_aifs, qi.tqi_cw_min, qi.tqi_cw_max);
  1290. ret = ath5k_hw_setup_tx_queueprops(ah, sc->bhalq, &qi);
  1291. if (ret) {
  1292. ATH5K_ERR(sc, "%s: unable to update parameters for beacon "
  1293. "hardware queue!\n", __func__);
  1294. return ret;
  1295. }
  1296. return ath5k_hw_reset_tx_queue(ah, sc->bhalq); /* push to h/w */;
  1297. }
  1298. static void
  1299. ath5k_txq_drainq(struct ath5k_softc *sc, struct ath5k_txq *txq)
  1300. {
  1301. struct ath5k_buf *bf, *bf0;
  1302. /*
  1303. * NB: this assumes output has been stopped and
  1304. * we do not need to block ath5k_tx_tasklet
  1305. */
  1306. spin_lock_bh(&txq->lock);
  1307. list_for_each_entry_safe(bf, bf0, &txq->q, list) {
  1308. ath5k_debug_printtxbuf(sc, bf, !sc->ah->ah_proc_tx_desc(sc->ah,
  1309. bf->desc));
  1310. ath5k_txbuf_free(sc, bf);
  1311. spin_lock_bh(&sc->txbuflock);
  1312. sc->tx_stats.data[txq->qnum].len--;
  1313. list_move_tail(&bf->list, &sc->txbuf);
  1314. sc->txbuf_len++;
  1315. spin_unlock_bh(&sc->txbuflock);
  1316. }
  1317. txq->link = NULL;
  1318. spin_unlock_bh(&txq->lock);
  1319. }
  1320. /*
  1321. * Drain the transmit queues and reclaim resources.
  1322. */
  1323. static void
  1324. ath5k_txq_cleanup(struct ath5k_softc *sc)
  1325. {
  1326. struct ath5k_hw *ah = sc->ah;
  1327. unsigned int i;
  1328. /* XXX return value */
  1329. if (likely(!test_bit(ATH_STAT_INVALID, sc->status))) {
  1330. /* don't touch the hardware if marked invalid */
  1331. ath5k_hw_stop_tx_dma(ah, sc->bhalq);
  1332. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "beacon queue %x\n",
  1333. ath5k_hw_get_tx_buf(ah, sc->bhalq));
  1334. for (i = 0; i < ARRAY_SIZE(sc->txqs); i++)
  1335. if (sc->txqs[i].setup) {
  1336. ath5k_hw_stop_tx_dma(ah, sc->txqs[i].qnum);
  1337. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "txq [%u] %x, "
  1338. "link %p\n",
  1339. sc->txqs[i].qnum,
  1340. ath5k_hw_get_tx_buf(ah,
  1341. sc->txqs[i].qnum),
  1342. sc->txqs[i].link);
  1343. }
  1344. }
  1345. ieee80211_start_queues(sc->hw); /* XXX move to callers */
  1346. for (i = 0; i < ARRAY_SIZE(sc->txqs); i++)
  1347. if (sc->txqs[i].setup)
  1348. ath5k_txq_drainq(sc, &sc->txqs[i]);
  1349. }
  1350. static void
  1351. ath5k_txq_release(struct ath5k_softc *sc)
  1352. {
  1353. struct ath5k_txq *txq = sc->txqs;
  1354. unsigned int i;
  1355. for (i = 0; i < ARRAY_SIZE(sc->txqs); i++, txq++)
  1356. if (txq->setup) {
  1357. ath5k_hw_release_tx_queue(sc->ah, txq->qnum);
  1358. txq->setup = false;
  1359. }
  1360. }
  1361. /*************\
  1362. * RX Handling *
  1363. \*************/
  1364. /*
  1365. * Enable the receive h/w following a reset.
  1366. */
  1367. static int
  1368. ath5k_rx_start(struct ath5k_softc *sc)
  1369. {
  1370. struct ath5k_hw *ah = sc->ah;
  1371. struct ath5k_buf *bf;
  1372. int ret;
  1373. sc->rxbufsize = roundup(IEEE80211_MAX_LEN, sc->cachelsz);
  1374. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "cachelsz %u rxbufsize %u\n",
  1375. sc->cachelsz, sc->rxbufsize);
  1376. sc->rxlink = NULL;
  1377. spin_lock_bh(&sc->rxbuflock);
  1378. list_for_each_entry(bf, &sc->rxbuf, list) {
  1379. ret = ath5k_rxbuf_setup(sc, bf);
  1380. if (ret != 0) {
  1381. spin_unlock_bh(&sc->rxbuflock);
  1382. goto err;
  1383. }
  1384. }
  1385. bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
  1386. spin_unlock_bh(&sc->rxbuflock);
  1387. ath5k_hw_put_rx_buf(ah, bf->daddr);
  1388. ath5k_hw_start_rx(ah); /* enable recv descriptors */
  1389. ath5k_mode_setup(sc); /* set filters, etc. */
  1390. ath5k_hw_start_rx_pcu(ah); /* re-enable PCU/DMA engine */
  1391. return 0;
  1392. err:
  1393. return ret;
  1394. }
  1395. /*
  1396. * Disable the receive h/w in preparation for a reset.
  1397. */
  1398. static void
  1399. ath5k_rx_stop(struct ath5k_softc *sc)
  1400. {
  1401. struct ath5k_hw *ah = sc->ah;
  1402. ath5k_hw_stop_pcu_recv(ah); /* disable PCU */
  1403. ath5k_hw_set_rx_filter(ah, 0); /* clear recv filter */
  1404. ath5k_hw_stop_rx_dma(ah); /* disable DMA engine */
  1405. mdelay(3); /* 3ms is long enough for 1 frame */
  1406. ath5k_debug_printrxbuffs(sc, ah);
  1407. sc->rxlink = NULL; /* just in case */
  1408. }
  1409. static unsigned int
  1410. ath5k_rx_decrypted(struct ath5k_softc *sc, struct ath5k_desc *ds,
  1411. struct sk_buff *skb)
  1412. {
  1413. struct ieee80211_hdr *hdr = (void *)skb->data;
  1414. unsigned int keyix, hlen = ieee80211_get_hdrlen_from_skb(skb);
  1415. if (!(ds->ds_rxstat.rs_status & AR5K_RXERR_DECRYPT) &&
  1416. ds->ds_rxstat.rs_keyix != AR5K_RXKEYIX_INVALID)
  1417. return RX_FLAG_DECRYPTED;
  1418. /* Apparently when a default key is used to decrypt the packet
  1419. the hw does not set the index used to decrypt. In such cases
  1420. get the index from the packet. */
  1421. if ((le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_PROTECTED) &&
  1422. !(ds->ds_rxstat.rs_status & AR5K_RXERR_DECRYPT) &&
  1423. skb->len >= hlen + 4) {
  1424. keyix = skb->data[hlen + 3] >> 6;
  1425. if (test_bit(keyix, sc->keymap))
  1426. return RX_FLAG_DECRYPTED;
  1427. }
  1428. return 0;
  1429. }
  1430. static void
  1431. ath5k_check_ibss_hw_merge(struct ath5k_softc *sc, struct sk_buff *skb)
  1432. {
  1433. u32 hw_tu;
  1434. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
  1435. if ((mgmt->frame_control & IEEE80211_FCTL_FTYPE) ==
  1436. IEEE80211_FTYPE_MGMT &&
  1437. (mgmt->frame_control & IEEE80211_FCTL_STYPE) ==
  1438. IEEE80211_STYPE_BEACON &&
  1439. mgmt->u.beacon.capab_info & WLAN_CAPABILITY_IBSS &&
  1440. memcmp(mgmt->bssid, sc->ah->ah_bssid, ETH_ALEN) == 0) {
  1441. /*
  1442. * Received an IBSS beacon with the same BSSID. Hardware might
  1443. * have updated the TSF, check if we need to update timers.
  1444. */
  1445. hw_tu = TSF_TO_TU(ath5k_hw_get_tsf64(sc->ah));
  1446. if (hw_tu >= sc->nexttbtt) {
  1447. ath5k_beacon_update_timers(sc,
  1448. mgmt->u.beacon.timestamp);
  1449. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  1450. "detected HW merge from received beacon\n");
  1451. }
  1452. }
  1453. }
  1454. static void
  1455. ath5k_tasklet_rx(unsigned long data)
  1456. {
  1457. struct ieee80211_rx_status rxs = {};
  1458. struct sk_buff *skb;
  1459. struct ath5k_softc *sc = (void *)data;
  1460. struct ath5k_buf *bf;
  1461. struct ath5k_desc *ds;
  1462. u16 len;
  1463. u8 stat;
  1464. int ret;
  1465. int hdrlen;
  1466. int pad;
  1467. spin_lock(&sc->rxbuflock);
  1468. do {
  1469. if (unlikely(list_empty(&sc->rxbuf))) {
  1470. ATH5K_WARN(sc, "empty rx buf pool\n");
  1471. break;
  1472. }
  1473. bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
  1474. BUG_ON(bf->skb == NULL);
  1475. skb = bf->skb;
  1476. ds = bf->desc;
  1477. /* TODO only one segment */
  1478. pci_dma_sync_single_for_cpu(sc->pdev, sc->desc_daddr,
  1479. sc->desc_len, PCI_DMA_FROMDEVICE);
  1480. if (unlikely(ds->ds_link == bf->daddr)) /* this is the end */
  1481. break;
  1482. ret = sc->ah->ah_proc_rx_desc(sc->ah, ds);
  1483. if (unlikely(ret == -EINPROGRESS))
  1484. break;
  1485. else if (unlikely(ret)) {
  1486. ATH5K_ERR(sc, "error in processing rx descriptor\n");
  1487. spin_unlock(&sc->rxbuflock);
  1488. return;
  1489. }
  1490. if (unlikely(ds->ds_rxstat.rs_more)) {
  1491. ATH5K_WARN(sc, "unsupported jumbo\n");
  1492. goto next;
  1493. }
  1494. stat = ds->ds_rxstat.rs_status;
  1495. if (unlikely(stat)) {
  1496. if (stat & AR5K_RXERR_PHY)
  1497. goto next;
  1498. if (stat & AR5K_RXERR_DECRYPT) {
  1499. /*
  1500. * Decrypt error. If the error occurred
  1501. * because there was no hardware key, then
  1502. * let the frame through so the upper layers
  1503. * can process it. This is necessary for 5210
  1504. * parts which have no way to setup a ``clear''
  1505. * key cache entry.
  1506. *
  1507. * XXX do key cache faulting
  1508. */
  1509. if (ds->ds_rxstat.rs_keyix ==
  1510. AR5K_RXKEYIX_INVALID &&
  1511. !(stat & AR5K_RXERR_CRC))
  1512. goto accept;
  1513. }
  1514. if (stat & AR5K_RXERR_MIC) {
  1515. rxs.flag |= RX_FLAG_MMIC_ERROR;
  1516. goto accept;
  1517. }
  1518. /* let crypto-error packets fall through in MNTR */
  1519. if ((stat & ~(AR5K_RXERR_DECRYPT|AR5K_RXERR_MIC)) ||
  1520. sc->opmode != IEEE80211_IF_TYPE_MNTR)
  1521. goto next;
  1522. }
  1523. accept:
  1524. len = ds->ds_rxstat.rs_datalen;
  1525. pci_dma_sync_single_for_cpu(sc->pdev, bf->skbaddr, len,
  1526. PCI_DMA_FROMDEVICE);
  1527. pci_unmap_single(sc->pdev, bf->skbaddr, sc->rxbufsize,
  1528. PCI_DMA_FROMDEVICE);
  1529. bf->skb = NULL;
  1530. skb_put(skb, len);
  1531. /*
  1532. * the hardware adds a padding to 4 byte boundaries between
  1533. * the header and the payload data if the header length is
  1534. * not multiples of 4 - remove it
  1535. */
  1536. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  1537. if (hdrlen & 3) {
  1538. pad = hdrlen % 4;
  1539. memmove(skb->data + pad, skb->data, hdrlen);
  1540. skb_pull(skb, pad);
  1541. }
  1542. /*
  1543. * always extend the mac timestamp, since this information is
  1544. * also needed for proper IBSS merging.
  1545. *
  1546. * XXX: it might be too late to do it here, since rs_tstamp is
  1547. * 15bit only. that means TSF extension has to be done within
  1548. * 32768usec (about 32ms). it might be necessary to move this to
  1549. * the interrupt handler, like it is done in madwifi.
  1550. */
  1551. rxs.mactime = ath5k_extend_tsf(sc->ah, ds->ds_rxstat.rs_tstamp);
  1552. rxs.flag |= RX_FLAG_TSFT;
  1553. rxs.freq = sc->curchan->freq;
  1554. rxs.channel = sc->curchan->chan;
  1555. rxs.phymode = sc->curmode;
  1556. /*
  1557. * signal quality:
  1558. * the names here are misleading and the usage of these
  1559. * values by iwconfig makes it even worse
  1560. */
  1561. /* noise floor in dBm, from the last noise calibration */
  1562. rxs.noise = sc->ah->ah_noise_floor;
  1563. /* signal level in dBm */
  1564. rxs.ssi = rxs.noise + ds->ds_rxstat.rs_rssi;
  1565. /*
  1566. * "signal" is actually displayed as Link Quality by iwconfig
  1567. * we provide a percentage based on rssi (assuming max rssi 64)
  1568. */
  1569. rxs.signal = ds->ds_rxstat.rs_rssi * 100 / 64;
  1570. rxs.antenna = ds->ds_rxstat.rs_antenna;
  1571. rxs.rate = ds->ds_rxstat.rs_rate;
  1572. rxs.flag |= ath5k_rx_decrypted(sc, ds, skb);
  1573. ath5k_debug_dump_skb(sc, skb, "RX ", 0);
  1574. /* check beacons in IBSS mode */
  1575. if (sc->opmode == IEEE80211_IF_TYPE_IBSS)
  1576. ath5k_check_ibss_hw_merge(sc, skb);
  1577. __ieee80211_rx(sc->hw, skb, &rxs);
  1578. sc->led_rxrate = ds->ds_rxstat.rs_rate;
  1579. ath5k_led_event(sc, ATH_LED_RX);
  1580. next:
  1581. list_move_tail(&bf->list, &sc->rxbuf);
  1582. } while (ath5k_rxbuf_setup(sc, bf) == 0);
  1583. spin_unlock(&sc->rxbuflock);
  1584. }
  1585. /*************\
  1586. * TX Handling *
  1587. \*************/
  1588. static void
  1589. ath5k_tx_processq(struct ath5k_softc *sc, struct ath5k_txq *txq)
  1590. {
  1591. struct ieee80211_tx_status txs = {};
  1592. struct ath5k_buf *bf, *bf0;
  1593. struct ath5k_desc *ds;
  1594. struct sk_buff *skb;
  1595. int ret;
  1596. spin_lock(&txq->lock);
  1597. list_for_each_entry_safe(bf, bf0, &txq->q, list) {
  1598. ds = bf->desc;
  1599. /* TODO only one segment */
  1600. pci_dma_sync_single_for_cpu(sc->pdev, sc->desc_daddr,
  1601. sc->desc_len, PCI_DMA_FROMDEVICE);
  1602. ret = sc->ah->ah_proc_tx_desc(sc->ah, ds);
  1603. if (unlikely(ret == -EINPROGRESS))
  1604. break;
  1605. else if (unlikely(ret)) {
  1606. ATH5K_ERR(sc, "error %d while processing queue %u\n",
  1607. ret, txq->qnum);
  1608. break;
  1609. }
  1610. skb = bf->skb;
  1611. bf->skb = NULL;
  1612. pci_unmap_single(sc->pdev, bf->skbaddr, skb->len,
  1613. PCI_DMA_TODEVICE);
  1614. txs.control = bf->ctl;
  1615. txs.retry_count = ds->ds_txstat.ts_shortretry +
  1616. ds->ds_txstat.ts_longretry / 6;
  1617. if (unlikely(ds->ds_txstat.ts_status)) {
  1618. sc->ll_stats.dot11ACKFailureCount++;
  1619. if (ds->ds_txstat.ts_status & AR5K_TXERR_XRETRY)
  1620. txs.excessive_retries = 1;
  1621. else if (ds->ds_txstat.ts_status & AR5K_TXERR_FILT)
  1622. txs.flags |= IEEE80211_TX_STATUS_TX_FILTERED;
  1623. } else {
  1624. txs.flags |= IEEE80211_TX_STATUS_ACK;
  1625. txs.ack_signal = ds->ds_txstat.ts_rssi;
  1626. }
  1627. ieee80211_tx_status(sc->hw, skb, &txs);
  1628. sc->tx_stats.data[txq->qnum].count++;
  1629. spin_lock(&sc->txbuflock);
  1630. sc->tx_stats.data[txq->qnum].len--;
  1631. list_move_tail(&bf->list, &sc->txbuf);
  1632. sc->txbuf_len++;
  1633. spin_unlock(&sc->txbuflock);
  1634. }
  1635. if (likely(list_empty(&txq->q)))
  1636. txq->link = NULL;
  1637. spin_unlock(&txq->lock);
  1638. if (sc->txbuf_len > ATH_TXBUF / 5)
  1639. ieee80211_wake_queues(sc->hw);
  1640. }
  1641. static void
  1642. ath5k_tasklet_tx(unsigned long data)
  1643. {
  1644. struct ath5k_softc *sc = (void *)data;
  1645. ath5k_tx_processq(sc, sc->txq);
  1646. ath5k_led_event(sc, ATH_LED_TX);
  1647. }
  1648. /*****************\
  1649. * Beacon handling *
  1650. \*****************/
  1651. /*
  1652. * Setup the beacon frame for transmit.
  1653. */
  1654. static int
  1655. ath5k_beacon_setup(struct ath5k_softc *sc, struct ath5k_buf *bf,
  1656. struct ieee80211_tx_control *ctl)
  1657. {
  1658. struct sk_buff *skb = bf->skb;
  1659. struct ath5k_hw *ah = sc->ah;
  1660. struct ath5k_desc *ds;
  1661. int ret, antenna = 0;
  1662. u32 flags;
  1663. bf->skbaddr = pci_map_single(sc->pdev, skb->data, skb->len,
  1664. PCI_DMA_TODEVICE);
  1665. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "skb %p [data %p len %u] "
  1666. "skbaddr %llx\n", skb, skb->data, skb->len,
  1667. (unsigned long long)bf->skbaddr);
  1668. if (pci_dma_mapping_error(bf->skbaddr)) {
  1669. ATH5K_ERR(sc, "beacon DMA mapping failed\n");
  1670. return -EIO;
  1671. }
  1672. ds = bf->desc;
  1673. flags = AR5K_TXDESC_NOACK;
  1674. if (sc->opmode == IEEE80211_IF_TYPE_IBSS && ath5k_hw_hasveol(ah)) {
  1675. ds->ds_link = bf->daddr; /* self-linked */
  1676. flags |= AR5K_TXDESC_VEOL;
  1677. /*
  1678. * Let hardware handle antenna switching if txantenna is not set
  1679. */
  1680. } else {
  1681. ds->ds_link = 0;
  1682. /*
  1683. * Switch antenna every 4 beacons if txantenna is not set
  1684. * XXX assumes two antennas
  1685. */
  1686. if (antenna == 0)
  1687. antenna = sc->bsent & 4 ? 2 : 1;
  1688. }
  1689. ds->ds_data = bf->skbaddr;
  1690. ret = ah->ah_setup_tx_desc(ah, ds, skb->len,
  1691. ieee80211_get_hdrlen_from_skb(skb),
  1692. AR5K_PKT_TYPE_BEACON, (ctl->power_level * 2), ctl->tx_rate, 1,
  1693. AR5K_TXKEYIX_INVALID, antenna, flags, 0, 0);
  1694. if (ret)
  1695. goto err_unmap;
  1696. return 0;
  1697. err_unmap:
  1698. pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
  1699. return ret;
  1700. }
  1701. /*
  1702. * Transmit a beacon frame at SWBA. Dynamic updates to the
  1703. * frame contents are done as needed and the slot time is
  1704. * also adjusted based on current state.
  1705. *
  1706. * this is usually called from interrupt context (ath5k_intr())
  1707. * but also from ath5k_beacon_config() in IBSS mode which in turn
  1708. * can be called from a tasklet and user context
  1709. */
  1710. static void
  1711. ath5k_beacon_send(struct ath5k_softc *sc)
  1712. {
  1713. struct ath5k_buf *bf = sc->bbuf;
  1714. struct ath5k_hw *ah = sc->ah;
  1715. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "in beacon_send\n");
  1716. if (unlikely(bf->skb == NULL || sc->opmode == IEEE80211_IF_TYPE_STA ||
  1717. sc->opmode == IEEE80211_IF_TYPE_MNTR)) {
  1718. ATH5K_WARN(sc, "bf=%p bf_skb=%p\n", bf, bf ? bf->skb : NULL);
  1719. return;
  1720. }
  1721. /*
  1722. * Check if the previous beacon has gone out. If
  1723. * not don't don't try to post another, skip this
  1724. * period and wait for the next. Missed beacons
  1725. * indicate a problem and should not occur. If we
  1726. * miss too many consecutive beacons reset the device.
  1727. */
  1728. if (unlikely(ath5k_hw_num_tx_pending(ah, sc->bhalq) != 0)) {
  1729. sc->bmisscount++;
  1730. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
  1731. "missed %u consecutive beacons\n", sc->bmisscount);
  1732. if (sc->bmisscount > 3) { /* NB: 3 is a guess */
  1733. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
  1734. "stuck beacon time (%u missed)\n",
  1735. sc->bmisscount);
  1736. tasklet_schedule(&sc->restq);
  1737. }
  1738. return;
  1739. }
  1740. if (unlikely(sc->bmisscount != 0)) {
  1741. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
  1742. "resume beacon xmit after %u misses\n",
  1743. sc->bmisscount);
  1744. sc->bmisscount = 0;
  1745. }
  1746. /*
  1747. * Stop any current dma and put the new frame on the queue.
  1748. * This should never fail since we check above that no frames
  1749. * are still pending on the queue.
  1750. */
  1751. if (unlikely(ath5k_hw_stop_tx_dma(ah, sc->bhalq))) {
  1752. ATH5K_WARN(sc, "beacon queue %u didn't stop?\n", sc->bhalq);
  1753. /* NB: hw still stops DMA, so proceed */
  1754. }
  1755. pci_dma_sync_single_for_cpu(sc->pdev, bf->skbaddr, bf->skb->len,
  1756. PCI_DMA_TODEVICE);
  1757. ath5k_hw_put_tx_buf(ah, sc->bhalq, bf->daddr);
  1758. ath5k_hw_tx_start(ah, sc->bhalq);
  1759. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "TXDP[%u] = %llx (%p)\n",
  1760. sc->bhalq, (unsigned long long)bf->daddr, bf->desc);
  1761. sc->bsent++;
  1762. }
  1763. /**
  1764. * ath5k_beacon_update_timers - update beacon timers
  1765. *
  1766. * @sc: struct ath5k_softc pointer we are operating on
  1767. * @bc_tsf: the timestamp of the beacon. 0 to reset the TSF. -1 to perform a
  1768. * beacon timer update based on the current HW TSF.
  1769. *
  1770. * Calculate the next target beacon transmit time (TBTT) based on the timestamp
  1771. * of a received beacon or the current local hardware TSF and write it to the
  1772. * beacon timer registers.
  1773. *
  1774. * This is called in a variety of situations, e.g. when a beacon is received,
  1775. * when a HW merge has been detected, but also when an new IBSS is created or
  1776. * when we otherwise know we have to update the timers, but we keep it in this
  1777. * function to have it all together in one place.
  1778. */
  1779. static void
  1780. ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf)
  1781. {
  1782. struct ath5k_hw *ah = sc->ah;
  1783. u32 nexttbtt, intval, hw_tu, bc_tu;
  1784. u64 hw_tsf;
  1785. intval = sc->bintval & AR5K_BEACON_PERIOD;
  1786. if (WARN_ON(!intval))
  1787. return;
  1788. /* beacon TSF converted to TU */
  1789. bc_tu = TSF_TO_TU(bc_tsf);
  1790. /* current TSF converted to TU */
  1791. hw_tsf = ath5k_hw_get_tsf64(ah);
  1792. hw_tu = TSF_TO_TU(hw_tsf);
  1793. #define FUDGE 3
  1794. /* we use FUDGE to make sure the next TBTT is ahead of the current TU */
  1795. if (bc_tsf == -1) {
  1796. /*
  1797. * no beacons received, called internally.
  1798. * just need to refresh timers based on HW TSF.
  1799. */
  1800. nexttbtt = roundup(hw_tu + FUDGE, intval);
  1801. } else if (bc_tsf == 0) {
  1802. /*
  1803. * no beacon received, probably called by ath5k_reset_tsf().
  1804. * reset TSF to start with 0.
  1805. */
  1806. nexttbtt = intval;
  1807. intval |= AR5K_BEACON_RESET_TSF;
  1808. } else if (bc_tsf > hw_tsf) {
  1809. /*
  1810. * beacon received, SW merge happend but HW TSF not yet updated.
  1811. * not possible to reconfigure timers yet, but next time we
  1812. * receive a beacon with the same BSSID, the hardware will
  1813. * automatically update the TSF and then we need to reconfigure
  1814. * the timers.
  1815. */
  1816. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  1817. "need to wait for HW TSF sync\n");
  1818. return;
  1819. } else {
  1820. /*
  1821. * most important case for beacon synchronization between STA.
  1822. *
  1823. * beacon received and HW TSF has been already updated by HW.
  1824. * update next TBTT based on the TSF of the beacon, but make
  1825. * sure it is ahead of our local TSF timer.
  1826. */
  1827. nexttbtt = bc_tu + roundup(hw_tu + FUDGE - bc_tu, intval);
  1828. }
  1829. #undef FUDGE
  1830. sc->nexttbtt = nexttbtt;
  1831. intval |= AR5K_BEACON_ENA;
  1832. ath5k_hw_init_beacon(ah, nexttbtt, intval);
  1833. /*
  1834. * debugging output last in order to preserve the time critical aspect
  1835. * of this function
  1836. */
  1837. if (bc_tsf == -1)
  1838. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  1839. "reconfigured timers based on HW TSF\n");
  1840. else if (bc_tsf == 0)
  1841. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  1842. "reset HW TSF and timers\n");
  1843. else
  1844. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  1845. "updated timers based on beacon TSF\n");
  1846. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  1847. "bc_tsf %llx hw_tsf %llx bc_tu %u hw_tu %u nexttbtt %u\n",
  1848. bc_tsf, hw_tsf, bc_tu, hw_tu, nexttbtt);
  1849. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "intval %u %s %s\n",
  1850. intval & AR5K_BEACON_PERIOD,
  1851. intval & AR5K_BEACON_ENA ? "AR5K_BEACON_ENA" : "",
  1852. intval & AR5K_BEACON_RESET_TSF ? "AR5K_BEACON_RESET_TSF" : "");
  1853. }
  1854. /**
  1855. * ath5k_beacon_config - Configure the beacon queues and interrupts
  1856. *
  1857. * @sc: struct ath5k_softc pointer we are operating on
  1858. *
  1859. * When operating in station mode we want to receive a BMISS interrupt when we
  1860. * stop seeing beacons from the AP we've associated with so we can look for
  1861. * another AP to associate with.
  1862. *
  1863. * In IBSS mode we use a self-linked tx descriptor if possible. We enable SWBA
  1864. * interrupts to detect HW merges only.
  1865. *
  1866. * AP mode is missing.
  1867. */
  1868. static void
  1869. ath5k_beacon_config(struct ath5k_softc *sc)
  1870. {
  1871. struct ath5k_hw *ah = sc->ah;
  1872. ath5k_hw_set_intr(ah, 0);
  1873. sc->bmisscount = 0;
  1874. if (sc->opmode == IEEE80211_IF_TYPE_STA) {
  1875. sc->imask |= AR5K_INT_BMISS;
  1876. } else if (sc->opmode == IEEE80211_IF_TYPE_IBSS) {
  1877. /*
  1878. * In IBSS mode we use a self-linked tx descriptor and let the
  1879. * hardware send the beacons automatically. We have to load it
  1880. * only once here.
  1881. * We use the SWBA interrupt only to keep track of the beacon
  1882. * timers in order to detect HW merges (automatic TSF updates).
  1883. */
  1884. ath5k_beaconq_config(sc);
  1885. sc->imask |= AR5K_INT_SWBA;
  1886. if (ath5k_hw_hasveol(ah))
  1887. ath5k_beacon_send(sc);
  1888. }
  1889. /* TODO else AP */
  1890. ath5k_hw_set_intr(ah, sc->imask);
  1891. }
  1892. /********************\
  1893. * Interrupt handling *
  1894. \********************/
  1895. static int
  1896. ath5k_init(struct ath5k_softc *sc)
  1897. {
  1898. int ret;
  1899. mutex_lock(&sc->lock);
  1900. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "mode %d\n", sc->opmode);
  1901. /*
  1902. * Stop anything previously setup. This is safe
  1903. * no matter this is the first time through or not.
  1904. */
  1905. ath5k_stop_locked(sc);
  1906. /*
  1907. * The basic interface to setting the hardware in a good
  1908. * state is ``reset''. On return the hardware is known to
  1909. * be powered up and with interrupts disabled. This must
  1910. * be followed by initialization of the appropriate bits
  1911. * and then setup of the interrupt mask.
  1912. */
  1913. sc->curchan = sc->hw->conf.chan;
  1914. ret = ath5k_hw_reset(sc->ah, sc->opmode, sc->curchan, false);
  1915. if (ret) {
  1916. ATH5K_ERR(sc, "unable to reset hardware: %d\n", ret);
  1917. goto done;
  1918. }
  1919. /*
  1920. * This is needed only to setup initial state
  1921. * but it's best done after a reset.
  1922. */
  1923. ath5k_hw_set_txpower_limit(sc->ah, 0);
  1924. /*
  1925. * Setup the hardware after reset: the key cache
  1926. * is filled as needed and the receive engine is
  1927. * set going. Frame transmit is handled entirely
  1928. * in the frame output path; there's nothing to do
  1929. * here except setup the interrupt mask.
  1930. */
  1931. ret = ath5k_rx_start(sc);
  1932. if (ret)
  1933. goto done;
  1934. /*
  1935. * Enable interrupts.
  1936. */
  1937. sc->imask = AR5K_INT_RX | AR5K_INT_TX | AR5K_INT_RXEOL |
  1938. AR5K_INT_RXORN | AR5K_INT_FATAL | AR5K_INT_GLOBAL;
  1939. ath5k_hw_set_intr(sc->ah, sc->imask);
  1940. /* Set ack to be sent at low bit-rates */
  1941. ath5k_hw_set_ack_bitrate_high(sc->ah, false);
  1942. mod_timer(&sc->calib_tim, round_jiffies(jiffies +
  1943. msecs_to_jiffies(ath5k_calinterval * 1000)));
  1944. ret = 0;
  1945. done:
  1946. mutex_unlock(&sc->lock);
  1947. return ret;
  1948. }
  1949. static int
  1950. ath5k_stop_locked(struct ath5k_softc *sc)
  1951. {
  1952. struct ath5k_hw *ah = sc->ah;
  1953. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "invalid %u\n",
  1954. test_bit(ATH_STAT_INVALID, sc->status));
  1955. /*
  1956. * Shutdown the hardware and driver:
  1957. * stop output from above
  1958. * disable interrupts
  1959. * turn off timers
  1960. * turn off the radio
  1961. * clear transmit machinery
  1962. * clear receive machinery
  1963. * drain and release tx queues
  1964. * reclaim beacon resources
  1965. * power down hardware
  1966. *
  1967. * Note that some of this work is not possible if the
  1968. * hardware is gone (invalid).
  1969. */
  1970. ieee80211_stop_queues(sc->hw);
  1971. if (!test_bit(ATH_STAT_INVALID, sc->status)) {
  1972. if (test_bit(ATH_STAT_LEDSOFT, sc->status)) {
  1973. del_timer_sync(&sc->led_tim);
  1974. ath5k_hw_set_gpio(ah, sc->led_pin, !sc->led_on);
  1975. __clear_bit(ATH_STAT_LEDBLINKING, sc->status);
  1976. }
  1977. ath5k_hw_set_intr(ah, 0);
  1978. }
  1979. ath5k_txq_cleanup(sc);
  1980. if (!test_bit(ATH_STAT_INVALID, sc->status)) {
  1981. ath5k_rx_stop(sc);
  1982. ath5k_hw_phy_disable(ah);
  1983. } else
  1984. sc->rxlink = NULL;
  1985. return 0;
  1986. }
  1987. /*
  1988. * Stop the device, grabbing the top-level lock to protect
  1989. * against concurrent entry through ath5k_init (which can happen
  1990. * if another thread does a system call and the thread doing the
  1991. * stop is preempted).
  1992. */
  1993. static int
  1994. ath5k_stop_hw(struct ath5k_softc *sc)
  1995. {
  1996. int ret;
  1997. mutex_lock(&sc->lock);
  1998. ret = ath5k_stop_locked(sc);
  1999. if (ret == 0 && !test_bit(ATH_STAT_INVALID, sc->status)) {
  2000. /*
  2001. * Set the chip in full sleep mode. Note that we are
  2002. * careful to do this only when bringing the interface
  2003. * completely to a stop. When the chip is in this state
  2004. * it must be carefully woken up or references to
  2005. * registers in the PCI clock domain may freeze the bus
  2006. * (and system). This varies by chip and is mostly an
  2007. * issue with newer parts that go to sleep more quickly.
  2008. */
  2009. if (sc->ah->ah_mac_srev >= 0x78) {
  2010. /*
  2011. * XXX
  2012. * don't put newer MAC revisions > 7.8 to sleep because
  2013. * of the above mentioned problems
  2014. */
  2015. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "mac version > 7.8, "
  2016. "not putting device to sleep\n");
  2017. } else {
  2018. ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
  2019. "putting device to full sleep\n");
  2020. ath5k_hw_set_power(sc->ah, AR5K_PM_FULL_SLEEP, true, 0);
  2021. }
  2022. }
  2023. ath5k_txbuf_free(sc, sc->bbuf);
  2024. mutex_unlock(&sc->lock);
  2025. del_timer_sync(&sc->calib_tim);
  2026. return ret;
  2027. }
  2028. static irqreturn_t
  2029. ath5k_intr(int irq, void *dev_id)
  2030. {
  2031. struct ath5k_softc *sc = dev_id;
  2032. struct ath5k_hw *ah = sc->ah;
  2033. enum ath5k_int status;
  2034. unsigned int counter = 1000;
  2035. if (unlikely(test_bit(ATH_STAT_INVALID, sc->status) ||
  2036. !ath5k_hw_is_intr_pending(ah)))
  2037. return IRQ_NONE;
  2038. do {
  2039. /*
  2040. * Figure out the reason(s) for the interrupt. Note
  2041. * that get_isr returns a pseudo-ISR that may include
  2042. * bits we haven't explicitly enabled so we mask the
  2043. * value to insure we only process bits we requested.
  2044. */
  2045. ath5k_hw_get_isr(ah, &status); /* NB: clears IRQ too */
  2046. ATH5K_DBG(sc, ATH5K_DEBUG_INTR, "status 0x%x/0x%x\n",
  2047. status, sc->imask);
  2048. status &= sc->imask; /* discard unasked for bits */
  2049. if (unlikely(status & AR5K_INT_FATAL)) {
  2050. /*
  2051. * Fatal errors are unrecoverable.
  2052. * Typically these are caused by DMA errors.
  2053. */
  2054. tasklet_schedule(&sc->restq);
  2055. } else if (unlikely(status & AR5K_INT_RXORN)) {
  2056. tasklet_schedule(&sc->restq);
  2057. } else {
  2058. if (status & AR5K_INT_SWBA) {
  2059. /*
  2060. * Software beacon alert--time to send a beacon.
  2061. * Handle beacon transmission directly; deferring
  2062. * this is too slow to meet timing constraints
  2063. * under load.
  2064. *
  2065. * In IBSS mode we use this interrupt just to
  2066. * keep track of the next TBTT (target beacon
  2067. * transmission time) in order to detect hardware
  2068. * merges (TSF updates).
  2069. */
  2070. if (sc->opmode == IEEE80211_IF_TYPE_IBSS) {
  2071. /* XXX: only if VEOL suppported */
  2072. u64 tsf = ath5k_hw_get_tsf64(ah);
  2073. sc->nexttbtt += sc->bintval;
  2074. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
  2075. "SWBA nexttbtt: %x hw_tu: %x "
  2076. "TSF: %llx\n",
  2077. sc->nexttbtt,
  2078. TSF_TO_TU(tsf), tsf);
  2079. } else {
  2080. ath5k_beacon_send(sc);
  2081. }
  2082. }
  2083. if (status & AR5K_INT_RXEOL) {
  2084. /*
  2085. * NB: the hardware should re-read the link when
  2086. * RXE bit is written, but it doesn't work at
  2087. * least on older hardware revs.
  2088. */
  2089. sc->rxlink = NULL;
  2090. }
  2091. if (status & AR5K_INT_TXURN) {
  2092. /* bump tx trigger level */
  2093. ath5k_hw_update_tx_triglevel(ah, true);
  2094. }
  2095. if (status & AR5K_INT_RX)
  2096. tasklet_schedule(&sc->rxtq);
  2097. if (status & AR5K_INT_TX)
  2098. tasklet_schedule(&sc->txtq);
  2099. if (status & AR5K_INT_BMISS) {
  2100. }
  2101. if (status & AR5K_INT_MIB) {
  2102. /* TODO */
  2103. }
  2104. }
  2105. } while (ath5k_hw_is_intr_pending(ah) && counter-- > 0);
  2106. if (unlikely(!counter))
  2107. ATH5K_WARN(sc, "too many interrupts, giving up for now\n");
  2108. return IRQ_HANDLED;
  2109. }
  2110. static void
  2111. ath5k_tasklet_reset(unsigned long data)
  2112. {
  2113. struct ath5k_softc *sc = (void *)data;
  2114. ath5k_reset(sc->hw);
  2115. }
  2116. /*
  2117. * Periodically recalibrate the PHY to account
  2118. * for temperature/environment changes.
  2119. */
  2120. static void
  2121. ath5k_calibrate(unsigned long data)
  2122. {
  2123. struct ath5k_softc *sc = (void *)data;
  2124. struct ath5k_hw *ah = sc->ah;
  2125. ATH5K_DBG(sc, ATH5K_DEBUG_CALIBRATE, "channel %u/%x\n",
  2126. sc->curchan->chan, sc->curchan->val);
  2127. if (ath5k_hw_get_rf_gain(ah) == AR5K_RFGAIN_NEED_CHANGE) {
  2128. /*
  2129. * Rfgain is out of bounds, reset the chip
  2130. * to load new gain values.
  2131. */
  2132. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "calibration, resetting\n");
  2133. ath5k_reset(sc->hw);
  2134. }
  2135. if (ath5k_hw_phy_calibrate(ah, sc->curchan))
  2136. ATH5K_ERR(sc, "calibration of channel %u failed\n",
  2137. sc->curchan->chan);
  2138. mod_timer(&sc->calib_tim, round_jiffies(jiffies +
  2139. msecs_to_jiffies(ath5k_calinterval * 1000)));
  2140. }
  2141. /***************\
  2142. * LED functions *
  2143. \***************/
  2144. static void
  2145. ath5k_led_off(unsigned long data)
  2146. {
  2147. struct ath5k_softc *sc = (void *)data;
  2148. if (test_bit(ATH_STAT_LEDENDBLINK, sc->status))
  2149. __clear_bit(ATH_STAT_LEDBLINKING, sc->status);
  2150. else {
  2151. __set_bit(ATH_STAT_LEDENDBLINK, sc->status);
  2152. ath5k_hw_set_gpio(sc->ah, sc->led_pin, !sc->led_on);
  2153. mod_timer(&sc->led_tim, jiffies + sc->led_off);
  2154. }
  2155. }
  2156. /*
  2157. * Blink the LED according to the specified on/off times.
  2158. */
  2159. static void
  2160. ath5k_led_blink(struct ath5k_softc *sc, unsigned int on,
  2161. unsigned int off)
  2162. {
  2163. ATH5K_DBG(sc, ATH5K_DEBUG_LED, "on %u off %u\n", on, off);
  2164. ath5k_hw_set_gpio(sc->ah, sc->led_pin, sc->led_on);
  2165. __set_bit(ATH_STAT_LEDBLINKING, sc->status);
  2166. __clear_bit(ATH_STAT_LEDENDBLINK, sc->status);
  2167. sc->led_off = off;
  2168. mod_timer(&sc->led_tim, jiffies + on);
  2169. }
  2170. static void
  2171. ath5k_led_event(struct ath5k_softc *sc, int event)
  2172. {
  2173. if (likely(!test_bit(ATH_STAT_LEDSOFT, sc->status)))
  2174. return;
  2175. if (unlikely(test_bit(ATH_STAT_LEDBLINKING, sc->status)))
  2176. return; /* don't interrupt active blink */
  2177. switch (event) {
  2178. case ATH_LED_TX:
  2179. ath5k_led_blink(sc, sc->hwmap[sc->led_txrate].ledon,
  2180. sc->hwmap[sc->led_txrate].ledoff);
  2181. break;
  2182. case ATH_LED_RX:
  2183. ath5k_led_blink(sc, sc->hwmap[sc->led_rxrate].ledon,
  2184. sc->hwmap[sc->led_rxrate].ledoff);
  2185. break;
  2186. }
  2187. }
  2188. /********************\
  2189. * Mac80211 functions *
  2190. \********************/
  2191. static int
  2192. ath5k_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
  2193. struct ieee80211_tx_control *ctl)
  2194. {
  2195. struct ath5k_softc *sc = hw->priv;
  2196. struct ath5k_buf *bf;
  2197. unsigned long flags;
  2198. int hdrlen;
  2199. int pad;
  2200. ath5k_debug_dump_skb(sc, skb, "TX ", 1);
  2201. if (sc->opmode == IEEE80211_IF_TYPE_MNTR)
  2202. ATH5K_DBG(sc, ATH5K_DEBUG_XMIT, "tx in monitor (scan?)\n");
  2203. /*
  2204. * the hardware expects the header padded to 4 byte boundaries
  2205. * if this is not the case we add the padding after the header
  2206. */
  2207. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  2208. if (hdrlen & 3) {
  2209. pad = hdrlen % 4;
  2210. if (skb_headroom(skb) < pad) {
  2211. ATH5K_ERR(sc, "tx hdrlen not %%4: %d not enough"
  2212. " headroom to pad %d\n", hdrlen, pad);
  2213. return -1;
  2214. }
  2215. skb_push(skb, pad);
  2216. memmove(skb->data, skb->data+pad, hdrlen);
  2217. }
  2218. sc->led_txrate = ctl->tx_rate;
  2219. spin_lock_irqsave(&sc->txbuflock, flags);
  2220. if (list_empty(&sc->txbuf)) {
  2221. ATH5K_ERR(sc, "no further txbuf available, dropping packet\n");
  2222. spin_unlock_irqrestore(&sc->txbuflock, flags);
  2223. ieee80211_stop_queue(hw, ctl->queue);
  2224. return -1;
  2225. }
  2226. bf = list_first_entry(&sc->txbuf, struct ath5k_buf, list);
  2227. list_del(&bf->list);
  2228. sc->txbuf_len--;
  2229. if (list_empty(&sc->txbuf))
  2230. ieee80211_stop_queues(hw);
  2231. spin_unlock_irqrestore(&sc->txbuflock, flags);
  2232. bf->skb = skb;
  2233. if (ath5k_txbuf_setup(sc, bf, ctl)) {
  2234. bf->skb = NULL;
  2235. spin_lock_irqsave(&sc->txbuflock, flags);
  2236. list_add_tail(&bf->list, &sc->txbuf);
  2237. sc->txbuf_len++;
  2238. spin_unlock_irqrestore(&sc->txbuflock, flags);
  2239. dev_kfree_skb_any(skb);
  2240. return 0;
  2241. }
  2242. return 0;
  2243. }
  2244. static int
  2245. ath5k_reset(struct ieee80211_hw *hw)
  2246. {
  2247. struct ath5k_softc *sc = hw->priv;
  2248. struct ath5k_hw *ah = sc->ah;
  2249. int ret;
  2250. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "resetting\n");
  2251. /*
  2252. * Convert to a hw channel description with the flags
  2253. * constrained to reflect the current operating mode.
  2254. */
  2255. sc->curchan = hw->conf.chan;
  2256. ath5k_hw_set_intr(ah, 0);
  2257. ath5k_txq_cleanup(sc);
  2258. ath5k_rx_stop(sc);
  2259. ret = ath5k_hw_reset(ah, sc->opmode, sc->curchan, true);
  2260. if (unlikely(ret)) {
  2261. ATH5K_ERR(sc, "can't reset hardware (%d)\n", ret);
  2262. goto err;
  2263. }
  2264. ath5k_hw_set_txpower_limit(sc->ah, 0);
  2265. ret = ath5k_rx_start(sc);
  2266. if (unlikely(ret)) {
  2267. ATH5K_ERR(sc, "can't start recv logic\n");
  2268. goto err;
  2269. }
  2270. /*
  2271. * We may be doing a reset in response to an ioctl
  2272. * that changes the channel so update any state that
  2273. * might change as a result.
  2274. *
  2275. * XXX needed?
  2276. */
  2277. /* ath5k_chan_change(sc, c); */
  2278. ath5k_beacon_config(sc);
  2279. /* intrs are started by ath5k_beacon_config */
  2280. ieee80211_wake_queues(hw);
  2281. return 0;
  2282. err:
  2283. return ret;
  2284. }
  2285. static int ath5k_start(struct ieee80211_hw *hw)
  2286. {
  2287. return ath5k_init(hw->priv);
  2288. }
  2289. static void ath5k_stop(struct ieee80211_hw *hw)
  2290. {
  2291. ath5k_stop_hw(hw->priv);
  2292. }
  2293. static int ath5k_add_interface(struct ieee80211_hw *hw,
  2294. struct ieee80211_if_init_conf *conf)
  2295. {
  2296. struct ath5k_softc *sc = hw->priv;
  2297. int ret;
  2298. mutex_lock(&sc->lock);
  2299. if (sc->vif) {
  2300. ret = 0;
  2301. goto end;
  2302. }
  2303. sc->vif = conf->vif;
  2304. switch (conf->type) {
  2305. case IEEE80211_IF_TYPE_STA:
  2306. case IEEE80211_IF_TYPE_IBSS:
  2307. case IEEE80211_IF_TYPE_MNTR:
  2308. sc->opmode = conf->type;
  2309. break;
  2310. default:
  2311. ret = -EOPNOTSUPP;
  2312. goto end;
  2313. }
  2314. ret = 0;
  2315. end:
  2316. mutex_unlock(&sc->lock);
  2317. return ret;
  2318. }
  2319. static void
  2320. ath5k_remove_interface(struct ieee80211_hw *hw,
  2321. struct ieee80211_if_init_conf *conf)
  2322. {
  2323. struct ath5k_softc *sc = hw->priv;
  2324. mutex_lock(&sc->lock);
  2325. if (sc->vif != conf->vif)
  2326. goto end;
  2327. sc->vif = NULL;
  2328. end:
  2329. mutex_unlock(&sc->lock);
  2330. }
  2331. static int
  2332. ath5k_config(struct ieee80211_hw *hw,
  2333. struct ieee80211_conf *conf)
  2334. {
  2335. struct ath5k_softc *sc = hw->priv;
  2336. sc->bintval = conf->beacon_int;
  2337. ath5k_setcurmode(sc, conf->phymode);
  2338. return ath5k_chan_set(sc, conf->chan);
  2339. }
  2340. static int
  2341. ath5k_config_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  2342. struct ieee80211_if_conf *conf)
  2343. {
  2344. struct ath5k_softc *sc = hw->priv;
  2345. struct ath5k_hw *ah = sc->ah;
  2346. int ret;
  2347. /* Set to a reasonable value. Note that this will
  2348. * be set to mac80211's value at ath5k_config(). */
  2349. sc->bintval = 1000;
  2350. mutex_lock(&sc->lock);
  2351. if (sc->vif != vif) {
  2352. ret = -EIO;
  2353. goto unlock;
  2354. }
  2355. if (conf->bssid) {
  2356. /* Cache for later use during resets */
  2357. memcpy(ah->ah_bssid, conf->bssid, ETH_ALEN);
  2358. /* XXX: assoc id is set to 0 for now, mac80211 doesn't have
  2359. * a clean way of letting us retrieve this yet. */
  2360. ath5k_hw_set_associd(ah, ah->ah_bssid, 0);
  2361. }
  2362. mutex_unlock(&sc->lock);
  2363. return ath5k_reset(hw);
  2364. unlock:
  2365. mutex_unlock(&sc->lock);
  2366. return ret;
  2367. }
  2368. #define SUPPORTED_FIF_FLAGS \
  2369. FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | \
  2370. FIF_PLCPFAIL | FIF_CONTROL | FIF_OTHER_BSS | \
  2371. FIF_BCN_PRBRESP_PROMISC
  2372. /*
  2373. * o always accept unicast, broadcast, and multicast traffic
  2374. * o multicast traffic for all BSSIDs will be enabled if mac80211
  2375. * says it should be
  2376. * o maintain current state of phy ofdm or phy cck error reception.
  2377. * If the hardware detects any of these type of errors then
  2378. * ath5k_hw_get_rx_filter() will pass to us the respective
  2379. * hardware filters to be able to receive these type of frames.
  2380. * o probe request frames are accepted only when operating in
  2381. * hostap, adhoc, or monitor modes
  2382. * o enable promiscuous mode according to the interface state
  2383. * o accept beacons:
  2384. * - when operating in adhoc mode so the 802.11 layer creates
  2385. * node table entries for peers,
  2386. * - when operating in station mode for collecting rssi data when
  2387. * the station is otherwise quiet, or
  2388. * - when scanning
  2389. */
  2390. static void ath5k_configure_filter(struct ieee80211_hw *hw,
  2391. unsigned int changed_flags,
  2392. unsigned int *new_flags,
  2393. int mc_count, struct dev_mc_list *mclist)
  2394. {
  2395. struct ath5k_softc *sc = hw->priv;
  2396. struct ath5k_hw *ah = sc->ah;
  2397. u32 mfilt[2], val, rfilt;
  2398. u8 pos;
  2399. int i;
  2400. mfilt[0] = 0;
  2401. mfilt[1] = 0;
  2402. /* Only deal with supported flags */
  2403. changed_flags &= SUPPORTED_FIF_FLAGS;
  2404. *new_flags &= SUPPORTED_FIF_FLAGS;
  2405. /* If HW detects any phy or radar errors, leave those filters on.
  2406. * Also, always enable Unicast, Broadcasts and Multicast
  2407. * XXX: move unicast, bssid broadcasts and multicast to mac80211 */
  2408. rfilt = (ath5k_hw_get_rx_filter(ah) & (AR5K_RX_FILTER_PHYERR)) |
  2409. (AR5K_RX_FILTER_UCAST | AR5K_RX_FILTER_BCAST |
  2410. AR5K_RX_FILTER_MCAST);
  2411. if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
  2412. if (*new_flags & FIF_PROMISC_IN_BSS) {
  2413. rfilt |= AR5K_RX_FILTER_PROM;
  2414. __set_bit(ATH_STAT_PROMISC, sc->status);
  2415. }
  2416. else
  2417. __clear_bit(ATH_STAT_PROMISC, sc->status);
  2418. }
  2419. /* Note, AR5K_RX_FILTER_MCAST is already enabled */
  2420. if (*new_flags & FIF_ALLMULTI) {
  2421. mfilt[0] = ~0;
  2422. mfilt[1] = ~0;
  2423. } else {
  2424. for (i = 0; i < mc_count; i++) {
  2425. if (!mclist)
  2426. break;
  2427. /* calculate XOR of eight 6-bit values */
  2428. val = LE_READ_4(mclist->dmi_addr + 0);
  2429. pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
  2430. val = LE_READ_4(mclist->dmi_addr + 3);
  2431. pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
  2432. pos &= 0x3f;
  2433. mfilt[pos / 32] |= (1 << (pos % 32));
  2434. /* XXX: we might be able to just do this instead,
  2435. * but not sure, needs testing, if we do use this we'd
  2436. * neet to inform below to not reset the mcast */
  2437. /* ath5k_hw_set_mcast_filterindex(ah,
  2438. * mclist->dmi_addr[5]); */
  2439. mclist = mclist->next;
  2440. }
  2441. }
  2442. /* This is the best we can do */
  2443. if (*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL))
  2444. rfilt |= AR5K_RX_FILTER_PHYERR;
  2445. /* FIF_BCN_PRBRESP_PROMISC really means to enable beacons
  2446. * and probes for any BSSID, this needs testing */
  2447. if (*new_flags & FIF_BCN_PRBRESP_PROMISC)
  2448. rfilt |= AR5K_RX_FILTER_BEACON | AR5K_RX_FILTER_PROBEREQ;
  2449. /* FIF_CONTROL doc says that if FIF_PROMISC_IN_BSS is not
  2450. * set we should only pass on control frames for this
  2451. * station. This needs testing. I believe right now this
  2452. * enables *all* control frames, which is OK.. but
  2453. * but we should see if we can improve on granularity */
  2454. if (*new_flags & FIF_CONTROL)
  2455. rfilt |= AR5K_RX_FILTER_CONTROL;
  2456. /* Additional settings per mode -- this is per ath5k */
  2457. /* XXX move these to mac80211, and add a beacon IFF flag to mac80211 */
  2458. if (sc->opmode == IEEE80211_IF_TYPE_MNTR)
  2459. rfilt |= AR5K_RX_FILTER_CONTROL | AR5K_RX_FILTER_BEACON |
  2460. AR5K_RX_FILTER_PROBEREQ | AR5K_RX_FILTER_PROM;
  2461. if (sc->opmode != IEEE80211_IF_TYPE_STA)
  2462. rfilt |= AR5K_RX_FILTER_PROBEREQ;
  2463. if (sc->opmode != IEEE80211_IF_TYPE_AP &&
  2464. test_bit(ATH_STAT_PROMISC, sc->status))
  2465. rfilt |= AR5K_RX_FILTER_PROM;
  2466. if (sc->opmode == IEEE80211_IF_TYPE_STA ||
  2467. sc->opmode == IEEE80211_IF_TYPE_IBSS) {
  2468. rfilt |= AR5K_RX_FILTER_BEACON;
  2469. }
  2470. /* Set filters */
  2471. ath5k_hw_set_rx_filter(ah,rfilt);
  2472. /* Set multicast bits */
  2473. ath5k_hw_set_mcast_filter(ah, mfilt[0], mfilt[1]);
  2474. /* Set the cached hw filter flags, this will alter actually
  2475. * be set in HW */
  2476. sc->filter_flags = rfilt;
  2477. }
  2478. static int
  2479. ath5k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  2480. const u8 *local_addr, const u8 *addr,
  2481. struct ieee80211_key_conf *key)
  2482. {
  2483. struct ath5k_softc *sc = hw->priv;
  2484. int ret = 0;
  2485. switch(key->alg) {
  2486. case ALG_WEP:
  2487. break;
  2488. case ALG_TKIP:
  2489. case ALG_CCMP:
  2490. return -EOPNOTSUPP;
  2491. default:
  2492. WARN_ON(1);
  2493. return -EINVAL;
  2494. }
  2495. mutex_lock(&sc->lock);
  2496. switch (cmd) {
  2497. case SET_KEY:
  2498. ret = ath5k_hw_set_key(sc->ah, key->keyidx, key, addr);
  2499. if (ret) {
  2500. ATH5K_ERR(sc, "can't set the key\n");
  2501. goto unlock;
  2502. }
  2503. __set_bit(key->keyidx, sc->keymap);
  2504. key->hw_key_idx = key->keyidx;
  2505. break;
  2506. case DISABLE_KEY:
  2507. ath5k_hw_reset_key(sc->ah, key->keyidx);
  2508. __clear_bit(key->keyidx, sc->keymap);
  2509. break;
  2510. default:
  2511. ret = -EINVAL;
  2512. goto unlock;
  2513. }
  2514. unlock:
  2515. mutex_unlock(&sc->lock);
  2516. return ret;
  2517. }
  2518. static int
  2519. ath5k_get_stats(struct ieee80211_hw *hw,
  2520. struct ieee80211_low_level_stats *stats)
  2521. {
  2522. struct ath5k_softc *sc = hw->priv;
  2523. memcpy(stats, &sc->ll_stats, sizeof(sc->ll_stats));
  2524. return 0;
  2525. }
  2526. static int
  2527. ath5k_get_tx_stats(struct ieee80211_hw *hw,
  2528. struct ieee80211_tx_queue_stats *stats)
  2529. {
  2530. struct ath5k_softc *sc = hw->priv;
  2531. memcpy(stats, &sc->tx_stats, sizeof(sc->tx_stats));
  2532. return 0;
  2533. }
  2534. static u64
  2535. ath5k_get_tsf(struct ieee80211_hw *hw)
  2536. {
  2537. struct ath5k_softc *sc = hw->priv;
  2538. return ath5k_hw_get_tsf64(sc->ah);
  2539. }
  2540. static void
  2541. ath5k_reset_tsf(struct ieee80211_hw *hw)
  2542. {
  2543. struct ath5k_softc *sc = hw->priv;
  2544. /*
  2545. * in IBSS mode we need to update the beacon timers too.
  2546. * this will also reset the TSF if we call it with 0
  2547. */
  2548. if (sc->opmode == IEEE80211_IF_TYPE_IBSS)
  2549. ath5k_beacon_update_timers(sc, 0);
  2550. else
  2551. ath5k_hw_reset_tsf(sc->ah);
  2552. }
  2553. static int
  2554. ath5k_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
  2555. struct ieee80211_tx_control *ctl)
  2556. {
  2557. struct ath5k_softc *sc = hw->priv;
  2558. int ret;
  2559. ath5k_debug_dump_skb(sc, skb, "BC ", 1);
  2560. mutex_lock(&sc->lock);
  2561. if (sc->opmode != IEEE80211_IF_TYPE_IBSS) {
  2562. ret = -EIO;
  2563. goto end;
  2564. }
  2565. ath5k_txbuf_free(sc, sc->bbuf);
  2566. sc->bbuf->skb = skb;
  2567. ret = ath5k_beacon_setup(sc, sc->bbuf, ctl);
  2568. if (ret)
  2569. sc->bbuf->skb = NULL;
  2570. else
  2571. ath5k_beacon_config(sc);
  2572. end:
  2573. mutex_unlock(&sc->lock);
  2574. return ret;
  2575. }