base.c 74 KB

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