base.c 85 KB

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