base.c 82 KB

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