base.c 79 KB

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