base.c 81 KB

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