base.c 79 KB

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