mwl8k.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120
  1. /*
  2. * drivers/net/wireless/mwl8k.c
  3. * Driver for Marvell TOPDOG 802.11 Wireless cards
  4. *
  5. * Copyright (C) 2008-2009 Marvell Semiconductor Inc.
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/list.h>
  16. #include <linux/pci.h>
  17. #include <linux/delay.h>
  18. #include <linux/completion.h>
  19. #include <linux/etherdevice.h>
  20. #include <net/mac80211.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/firmware.h>
  23. #include <linux/workqueue.h>
  24. #define MWL8K_DESC "Marvell TOPDOG(R) 802.11 Wireless Network Driver"
  25. #define MWL8K_NAME KBUILD_MODNAME
  26. #define MWL8K_VERSION "0.10"
  27. MODULE_DESCRIPTION(MWL8K_DESC);
  28. MODULE_VERSION(MWL8K_VERSION);
  29. MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
  30. MODULE_LICENSE("GPL");
  31. static DEFINE_PCI_DEVICE_TABLE(mwl8k_table) = {
  32. { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = 8687, },
  33. { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = 8687, },
  34. { }
  35. };
  36. MODULE_DEVICE_TABLE(pci, mwl8k_table);
  37. /* Register definitions */
  38. #define MWL8K_HIU_GEN_PTR 0x00000c10
  39. #define MWL8K_MODE_STA 0x0000005a
  40. #define MWL8K_MODE_AP 0x000000a5
  41. #define MWL8K_HIU_INT_CODE 0x00000c14
  42. #define MWL8K_FWSTA_READY 0xf0f1f2f4
  43. #define MWL8K_FWAP_READY 0xf1f2f4a5
  44. #define MWL8K_INT_CODE_CMD_FINISHED 0x00000005
  45. #define MWL8K_HIU_SCRATCH 0x00000c40
  46. /* Host->device communications */
  47. #define MWL8K_HIU_H2A_INTERRUPT_EVENTS 0x00000c18
  48. #define MWL8K_HIU_H2A_INTERRUPT_STATUS 0x00000c1c
  49. #define MWL8K_HIU_H2A_INTERRUPT_MASK 0x00000c20
  50. #define MWL8K_HIU_H2A_INTERRUPT_CLEAR_SEL 0x00000c24
  51. #define MWL8K_HIU_H2A_INTERRUPT_STATUS_MASK 0x00000c28
  52. #define MWL8K_H2A_INT_DUMMY (1 << 20)
  53. #define MWL8K_H2A_INT_RESET (1 << 15)
  54. #define MWL8K_H2A_INT_DOORBELL (1 << 1)
  55. #define MWL8K_H2A_INT_PPA_READY (1 << 0)
  56. /* Device->host communications */
  57. #define MWL8K_HIU_A2H_INTERRUPT_EVENTS 0x00000c2c
  58. #define MWL8K_HIU_A2H_INTERRUPT_STATUS 0x00000c30
  59. #define MWL8K_HIU_A2H_INTERRUPT_MASK 0x00000c34
  60. #define MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL 0x00000c38
  61. #define MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK 0x00000c3c
  62. #define MWL8K_A2H_INT_DUMMY (1 << 20)
  63. #define MWL8K_A2H_INT_CHNL_SWITCHED (1 << 11)
  64. #define MWL8K_A2H_INT_QUEUE_EMPTY (1 << 10)
  65. #define MWL8K_A2H_INT_RADAR_DETECT (1 << 7)
  66. #define MWL8K_A2H_INT_RADIO_ON (1 << 6)
  67. #define MWL8K_A2H_INT_RADIO_OFF (1 << 5)
  68. #define MWL8K_A2H_INT_MAC_EVENT (1 << 3)
  69. #define MWL8K_A2H_INT_OPC_DONE (1 << 2)
  70. #define MWL8K_A2H_INT_RX_READY (1 << 1)
  71. #define MWL8K_A2H_INT_TX_DONE (1 << 0)
  72. #define MWL8K_A2H_EVENTS (MWL8K_A2H_INT_DUMMY | \
  73. MWL8K_A2H_INT_CHNL_SWITCHED | \
  74. MWL8K_A2H_INT_QUEUE_EMPTY | \
  75. MWL8K_A2H_INT_RADAR_DETECT | \
  76. MWL8K_A2H_INT_RADIO_ON | \
  77. MWL8K_A2H_INT_RADIO_OFF | \
  78. MWL8K_A2H_INT_MAC_EVENT | \
  79. MWL8K_A2H_INT_OPC_DONE | \
  80. MWL8K_A2H_INT_RX_READY | \
  81. MWL8K_A2H_INT_TX_DONE)
  82. /* WME stream classes */
  83. #define WME_AC_BE 0 /* best effort */
  84. #define WME_AC_BK 1 /* background */
  85. #define WME_AC_VI 2 /* video */
  86. #define WME_AC_VO 3 /* voice */
  87. #define MWL8K_RX_QUEUES 1
  88. #define MWL8K_TX_QUEUES 4
  89. struct mwl8k_rx_queue {
  90. int rx_desc_count;
  91. /* hw receives here */
  92. int rx_head;
  93. /* refill descs here */
  94. int rx_tail;
  95. struct mwl8k_rx_desc *rx_desc_area;
  96. dma_addr_t rx_desc_dma;
  97. struct sk_buff **rx_skb;
  98. };
  99. struct mwl8k_tx_queue {
  100. /* hw transmits here */
  101. int tx_head;
  102. /* sw appends here */
  103. int tx_tail;
  104. struct ieee80211_tx_queue_stats tx_stats;
  105. struct mwl8k_tx_desc *tx_desc_area;
  106. dma_addr_t tx_desc_dma;
  107. struct sk_buff **tx_skb;
  108. };
  109. /* Pointers to the firmware data and meta information about it. */
  110. struct mwl8k_firmware {
  111. /* Microcode */
  112. struct firmware *ucode;
  113. /* Boot helper code */
  114. struct firmware *helper;
  115. };
  116. struct mwl8k_priv {
  117. void __iomem *regs;
  118. struct ieee80211_hw *hw;
  119. struct pci_dev *pdev;
  120. u8 name[16];
  121. /* firmware files and meta data */
  122. struct mwl8k_firmware fw;
  123. u32 part_num;
  124. /* firmware access */
  125. struct mutex fw_mutex;
  126. struct task_struct *fw_mutex_owner;
  127. int fw_mutex_depth;
  128. struct completion *tx_wait;
  129. struct completion *hostcmd_wait;
  130. /* lock held over TX and TX reap */
  131. spinlock_t tx_lock;
  132. struct ieee80211_vif *vif;
  133. struct ieee80211_channel *current_channel;
  134. /* power management status cookie from firmware */
  135. u32 *cookie;
  136. dma_addr_t cookie_dma;
  137. u16 num_mcaddrs;
  138. u8 hw_rev;
  139. u32 fw_rev;
  140. /*
  141. * Running count of TX packets in flight, to avoid
  142. * iterating over the transmit rings each time.
  143. */
  144. int pending_tx_pkts;
  145. struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
  146. struct mwl8k_tx_queue txq[MWL8K_TX_QUEUES];
  147. /* PHY parameters */
  148. struct ieee80211_supported_band band;
  149. struct ieee80211_channel channels[14];
  150. struct ieee80211_rate rates[12];
  151. bool radio_on;
  152. bool radio_short_preamble;
  153. bool wmm_enabled;
  154. /* XXX need to convert this to handle multiple interfaces */
  155. bool capture_beacon;
  156. u8 capture_bssid[ETH_ALEN];
  157. struct sk_buff *beacon_skb;
  158. /*
  159. * This FJ worker has to be global as it is scheduled from the
  160. * RX handler. At this point we don't know which interface it
  161. * belongs to until the list of bssids waiting to complete join
  162. * is checked.
  163. */
  164. struct work_struct finalize_join_worker;
  165. /* Tasklet to reclaim TX descriptors and buffers after tx */
  166. struct tasklet_struct tx_reclaim_task;
  167. /* Work thread to serialize configuration requests */
  168. struct workqueue_struct *config_wq;
  169. };
  170. /* Per interface specific private data */
  171. struct mwl8k_vif {
  172. /* backpointer to parent config block */
  173. struct mwl8k_priv *priv;
  174. /* BSS config of AP or IBSS from mac80211*/
  175. struct ieee80211_bss_conf bss_info;
  176. /* BSSID of AP or IBSS */
  177. u8 bssid[ETH_ALEN];
  178. u8 mac_addr[ETH_ALEN];
  179. /*
  180. * Subset of supported legacy rates.
  181. * Intersection of AP and STA supported rates.
  182. */
  183. struct ieee80211_rate legacy_rates[12];
  184. /* number of supported legacy rates */
  185. u8 legacy_nrates;
  186. /* Index into station database.Returned by update_sta_db call */
  187. u8 peer_id;
  188. /* Non AMPDU sequence number assigned by driver */
  189. u16 seqno;
  190. };
  191. #define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
  192. static const struct ieee80211_channel mwl8k_channels[] = {
  193. { .center_freq = 2412, .hw_value = 1, },
  194. { .center_freq = 2417, .hw_value = 2, },
  195. { .center_freq = 2422, .hw_value = 3, },
  196. { .center_freq = 2427, .hw_value = 4, },
  197. { .center_freq = 2432, .hw_value = 5, },
  198. { .center_freq = 2437, .hw_value = 6, },
  199. { .center_freq = 2442, .hw_value = 7, },
  200. { .center_freq = 2447, .hw_value = 8, },
  201. { .center_freq = 2452, .hw_value = 9, },
  202. { .center_freq = 2457, .hw_value = 10, },
  203. { .center_freq = 2462, .hw_value = 11, },
  204. };
  205. static const struct ieee80211_rate mwl8k_rates[] = {
  206. { .bitrate = 10, .hw_value = 2, },
  207. { .bitrate = 20, .hw_value = 4, },
  208. { .bitrate = 55, .hw_value = 11, },
  209. { .bitrate = 60, .hw_value = 12, },
  210. { .bitrate = 90, .hw_value = 18, },
  211. { .bitrate = 110, .hw_value = 22, },
  212. { .bitrate = 120, .hw_value = 24, },
  213. { .bitrate = 180, .hw_value = 36, },
  214. { .bitrate = 240, .hw_value = 48, },
  215. { .bitrate = 360, .hw_value = 72, },
  216. { .bitrate = 480, .hw_value = 96, },
  217. { .bitrate = 540, .hw_value = 108, },
  218. };
  219. /* Set or get info from Firmware */
  220. #define MWL8K_CMD_SET 0x0001
  221. #define MWL8K_CMD_GET 0x0000
  222. /* Firmware command codes */
  223. #define MWL8K_CMD_CODE_DNLD 0x0001
  224. #define MWL8K_CMD_GET_HW_SPEC 0x0003
  225. #define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
  226. #define MWL8K_CMD_GET_STAT 0x0014
  227. #define MWL8K_CMD_RADIO_CONTROL 0x001c
  228. #define MWL8K_CMD_RF_TX_POWER 0x001e
  229. #define MWL8K_CMD_SET_PRE_SCAN 0x0107
  230. #define MWL8K_CMD_SET_POST_SCAN 0x0108
  231. #define MWL8K_CMD_SET_RF_CHANNEL 0x010a
  232. #define MWL8K_CMD_SET_AID 0x010d
  233. #define MWL8K_CMD_SET_RATE 0x0110
  234. #define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
  235. #define MWL8K_CMD_RTS_THRESHOLD 0x0113
  236. #define MWL8K_CMD_SET_SLOT 0x0114
  237. #define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
  238. #define MWL8K_CMD_SET_WMM_MODE 0x0123
  239. #define MWL8K_CMD_MIMO_CONFIG 0x0125
  240. #define MWL8K_CMD_USE_FIXED_RATE 0x0126
  241. #define MWL8K_CMD_ENABLE_SNIFFER 0x0150
  242. #define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
  243. #define MWL8K_CMD_UPDATE_STADB 0x1123
  244. static const char *mwl8k_cmd_name(u16 cmd, char *buf, int bufsize)
  245. {
  246. #define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
  247. snprintf(buf, bufsize, "%s", #x);\
  248. return buf;\
  249. } while (0)
  250. switch (cmd & ~0x8000) {
  251. MWL8K_CMDNAME(CODE_DNLD);
  252. MWL8K_CMDNAME(GET_HW_SPEC);
  253. MWL8K_CMDNAME(MAC_MULTICAST_ADR);
  254. MWL8K_CMDNAME(GET_STAT);
  255. MWL8K_CMDNAME(RADIO_CONTROL);
  256. MWL8K_CMDNAME(RF_TX_POWER);
  257. MWL8K_CMDNAME(SET_PRE_SCAN);
  258. MWL8K_CMDNAME(SET_POST_SCAN);
  259. MWL8K_CMDNAME(SET_RF_CHANNEL);
  260. MWL8K_CMDNAME(SET_AID);
  261. MWL8K_CMDNAME(SET_RATE);
  262. MWL8K_CMDNAME(SET_FINALIZE_JOIN);
  263. MWL8K_CMDNAME(RTS_THRESHOLD);
  264. MWL8K_CMDNAME(SET_SLOT);
  265. MWL8K_CMDNAME(SET_EDCA_PARAMS);
  266. MWL8K_CMDNAME(SET_WMM_MODE);
  267. MWL8K_CMDNAME(MIMO_CONFIG);
  268. MWL8K_CMDNAME(USE_FIXED_RATE);
  269. MWL8K_CMDNAME(ENABLE_SNIFFER);
  270. MWL8K_CMDNAME(SET_RATEADAPT_MODE);
  271. MWL8K_CMDNAME(UPDATE_STADB);
  272. default:
  273. snprintf(buf, bufsize, "0x%x", cmd);
  274. }
  275. #undef MWL8K_CMDNAME
  276. return buf;
  277. }
  278. /* Hardware and firmware reset */
  279. static void mwl8k_hw_reset(struct mwl8k_priv *priv)
  280. {
  281. iowrite32(MWL8K_H2A_INT_RESET,
  282. priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  283. iowrite32(MWL8K_H2A_INT_RESET,
  284. priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  285. msleep(20);
  286. }
  287. /* Release fw image */
  288. static void mwl8k_release_fw(struct firmware **fw)
  289. {
  290. if (*fw == NULL)
  291. return;
  292. release_firmware(*fw);
  293. *fw = NULL;
  294. }
  295. static void mwl8k_release_firmware(struct mwl8k_priv *priv)
  296. {
  297. mwl8k_release_fw(&priv->fw.ucode);
  298. mwl8k_release_fw(&priv->fw.helper);
  299. }
  300. /* Request fw image */
  301. static int mwl8k_request_fw(struct mwl8k_priv *priv,
  302. const char *fname, struct firmware **fw)
  303. {
  304. /* release current image */
  305. if (*fw != NULL)
  306. mwl8k_release_fw(fw);
  307. return request_firmware((const struct firmware **)fw,
  308. fname, &priv->pdev->dev);
  309. }
  310. static int mwl8k_request_firmware(struct mwl8k_priv *priv, u32 part_num)
  311. {
  312. u8 filename[64];
  313. int rc;
  314. priv->part_num = part_num;
  315. snprintf(filename, sizeof(filename),
  316. "mwl8k/helper_%u.fw", priv->part_num);
  317. rc = mwl8k_request_fw(priv, filename, &priv->fw.helper);
  318. if (rc) {
  319. printk(KERN_ERR
  320. "%s Error requesting helper firmware file %s\n",
  321. pci_name(priv->pdev), filename);
  322. return rc;
  323. }
  324. snprintf(filename, sizeof(filename),
  325. "mwl8k/fmimage_%u.fw", priv->part_num);
  326. rc = mwl8k_request_fw(priv, filename, &priv->fw.ucode);
  327. if (rc) {
  328. printk(KERN_ERR "%s Error requesting firmware file %s\n",
  329. pci_name(priv->pdev), filename);
  330. mwl8k_release_fw(&priv->fw.helper);
  331. return rc;
  332. }
  333. return 0;
  334. }
  335. struct mwl8k_cmd_pkt {
  336. __le16 code;
  337. __le16 length;
  338. __le16 seq_num;
  339. __le16 result;
  340. char payload[0];
  341. } __attribute__((packed));
  342. /*
  343. * Firmware loading.
  344. */
  345. static int
  346. mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
  347. {
  348. void __iomem *regs = priv->regs;
  349. dma_addr_t dma_addr;
  350. int loops;
  351. dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
  352. if (pci_dma_mapping_error(priv->pdev, dma_addr))
  353. return -ENOMEM;
  354. iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
  355. iowrite32(0, regs + MWL8K_HIU_INT_CODE);
  356. iowrite32(MWL8K_H2A_INT_DOORBELL,
  357. regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  358. iowrite32(MWL8K_H2A_INT_DUMMY,
  359. regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  360. loops = 1000;
  361. do {
  362. u32 int_code;
  363. int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
  364. if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
  365. iowrite32(0, regs + MWL8K_HIU_INT_CODE);
  366. break;
  367. }
  368. udelay(1);
  369. } while (--loops);
  370. pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
  371. return loops ? 0 : -ETIMEDOUT;
  372. }
  373. static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
  374. const u8 *data, size_t length)
  375. {
  376. struct mwl8k_cmd_pkt *cmd;
  377. int done;
  378. int rc = 0;
  379. cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
  380. if (cmd == NULL)
  381. return -ENOMEM;
  382. cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
  383. cmd->seq_num = 0;
  384. cmd->result = 0;
  385. done = 0;
  386. while (length) {
  387. int block_size = length > 256 ? 256 : length;
  388. memcpy(cmd->payload, data + done, block_size);
  389. cmd->length = cpu_to_le16(block_size);
  390. rc = mwl8k_send_fw_load_cmd(priv, cmd,
  391. sizeof(*cmd) + block_size);
  392. if (rc)
  393. break;
  394. done += block_size;
  395. length -= block_size;
  396. }
  397. if (!rc) {
  398. cmd->length = 0;
  399. rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
  400. }
  401. kfree(cmd);
  402. return rc;
  403. }
  404. static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
  405. const u8 *data, size_t length)
  406. {
  407. unsigned char *buffer;
  408. int may_continue, rc = 0;
  409. u32 done, prev_block_size;
  410. buffer = kmalloc(1024, GFP_KERNEL);
  411. if (buffer == NULL)
  412. return -ENOMEM;
  413. done = 0;
  414. prev_block_size = 0;
  415. may_continue = 1000;
  416. while (may_continue > 0) {
  417. u32 block_size;
  418. block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
  419. if (block_size & 1) {
  420. block_size &= ~1;
  421. may_continue--;
  422. } else {
  423. done += prev_block_size;
  424. length -= prev_block_size;
  425. }
  426. if (block_size > 1024 || block_size > length) {
  427. rc = -EOVERFLOW;
  428. break;
  429. }
  430. if (length == 0) {
  431. rc = 0;
  432. break;
  433. }
  434. if (block_size == 0) {
  435. rc = -EPROTO;
  436. may_continue--;
  437. udelay(1);
  438. continue;
  439. }
  440. prev_block_size = block_size;
  441. memcpy(buffer, data + done, block_size);
  442. rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
  443. if (rc)
  444. break;
  445. }
  446. if (!rc && length != 0)
  447. rc = -EREMOTEIO;
  448. kfree(buffer);
  449. return rc;
  450. }
  451. static int mwl8k_load_firmware(struct mwl8k_priv *priv)
  452. {
  453. int loops, rc;
  454. const u8 *ucode = priv->fw.ucode->data;
  455. size_t ucode_len = priv->fw.ucode->size;
  456. const u8 *helper = priv->fw.helper->data;
  457. size_t helper_len = priv->fw.helper->size;
  458. if (!memcmp(ucode, "\x01\x00\x00\x00", 4)) {
  459. rc = mwl8k_load_fw_image(priv, helper, helper_len);
  460. if (rc) {
  461. printk(KERN_ERR "%s: unable to load firmware "
  462. "helper image\n", pci_name(priv->pdev));
  463. return rc;
  464. }
  465. msleep(1);
  466. rc = mwl8k_feed_fw_image(priv, ucode, ucode_len);
  467. } else {
  468. rc = mwl8k_load_fw_image(priv, ucode, ucode_len);
  469. }
  470. if (rc) {
  471. printk(KERN_ERR "%s: unable to load firmware data\n",
  472. pci_name(priv->pdev));
  473. return rc;
  474. }
  475. iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
  476. msleep(1);
  477. loops = 200000;
  478. do {
  479. if (ioread32(priv->regs + MWL8K_HIU_INT_CODE)
  480. == MWL8K_FWSTA_READY)
  481. break;
  482. udelay(1);
  483. } while (--loops);
  484. return loops ? 0 : -ETIMEDOUT;
  485. }
  486. /*
  487. * Defines shared between transmission and reception.
  488. */
  489. /* HT control fields for firmware */
  490. struct ewc_ht_info {
  491. __le16 control1;
  492. __le16 control2;
  493. __le16 control3;
  494. } __attribute__((packed));
  495. /* Firmware Station database operations */
  496. #define MWL8K_STA_DB_ADD_ENTRY 0
  497. #define MWL8K_STA_DB_MODIFY_ENTRY 1
  498. #define MWL8K_STA_DB_DEL_ENTRY 2
  499. #define MWL8K_STA_DB_FLUSH 3
  500. /* Peer Entry flags - used to define the type of the peer node */
  501. #define MWL8K_PEER_TYPE_ACCESSPOINT 2
  502. #define MWL8K_IEEE_LEGACY_DATA_RATES 12
  503. #define MWL8K_MCS_BITMAP_SIZE 16
  504. struct peer_capability_info {
  505. /* Peer type - AP vs. STA. */
  506. __u8 peer_type;
  507. /* Basic 802.11 capabilities from assoc resp. */
  508. __le16 basic_caps;
  509. /* Set if peer supports 802.11n high throughput (HT). */
  510. __u8 ht_support;
  511. /* Valid if HT is supported. */
  512. __le16 ht_caps;
  513. __u8 extended_ht_caps;
  514. struct ewc_ht_info ewc_info;
  515. /* Legacy rate table. Intersection of our rates and peer rates. */
  516. __u8 legacy_rates[MWL8K_IEEE_LEGACY_DATA_RATES];
  517. /* HT rate table. Intersection of our rates and peer rates. */
  518. __u8 ht_rates[MWL8K_MCS_BITMAP_SIZE];
  519. __u8 pad[16];
  520. /* If set, interoperability mode, no proprietary extensions. */
  521. __u8 interop;
  522. __u8 pad2;
  523. __u8 station_id;
  524. __le16 amsdu_enabled;
  525. } __attribute__((packed));
  526. /* Inline functions to manipulate QoS field in data descriptor. */
  527. static inline u16 mwl8k_qos_setbit_eosp(u16 qos)
  528. {
  529. u16 val_mask = 1 << 4;
  530. /* End of Service Period Bit 4 */
  531. return qos | val_mask;
  532. }
  533. static inline u16 mwl8k_qos_setbit_ack(u16 qos, u8 ack_policy)
  534. {
  535. u16 val_mask = 0x3;
  536. u8 shift = 5;
  537. u16 qos_mask = ~(val_mask << shift);
  538. /* Ack Policy Bit 5-6 */
  539. return (qos & qos_mask) | ((ack_policy & val_mask) << shift);
  540. }
  541. static inline u16 mwl8k_qos_setbit_amsdu(u16 qos)
  542. {
  543. u16 val_mask = 1 << 7;
  544. /* AMSDU present Bit 7 */
  545. return qos | val_mask;
  546. }
  547. static inline u16 mwl8k_qos_setbit_qlen(u16 qos, u8 len)
  548. {
  549. u16 val_mask = 0xff;
  550. u8 shift = 8;
  551. u16 qos_mask = ~(val_mask << shift);
  552. /* Queue Length Bits 8-15 */
  553. return (qos & qos_mask) | ((len & val_mask) << shift);
  554. }
  555. /* DMA header used by firmware and hardware. */
  556. struct mwl8k_dma_data {
  557. __le16 fwlen;
  558. struct ieee80211_hdr wh;
  559. } __attribute__((packed));
  560. /* Routines to add/remove DMA header from skb. */
  561. static inline void mwl8k_remove_dma_header(struct sk_buff *skb)
  562. {
  563. struct mwl8k_dma_data *tr = (struct mwl8k_dma_data *)skb->data;
  564. void *dst, *src = &tr->wh;
  565. int hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
  566. u16 space = sizeof(struct mwl8k_dma_data) - hdrlen;
  567. dst = (void *)tr + space;
  568. if (dst != src) {
  569. memmove(dst, src, hdrlen);
  570. skb_pull(skb, space);
  571. }
  572. }
  573. static inline void mwl8k_add_dma_header(struct sk_buff *skb)
  574. {
  575. struct ieee80211_hdr *wh;
  576. u32 hdrlen, pktlen;
  577. struct mwl8k_dma_data *tr;
  578. wh = (struct ieee80211_hdr *)skb->data;
  579. hdrlen = ieee80211_hdrlen(wh->frame_control);
  580. pktlen = skb->len;
  581. /*
  582. * Copy up/down the 802.11 header; the firmware requires
  583. * we present a 2-byte payload length followed by a
  584. * 4-address header (w/o QoS), followed (optionally) by
  585. * any WEP/ExtIV header (but only filled in for CCMP).
  586. */
  587. if (hdrlen != sizeof(struct mwl8k_dma_data))
  588. skb_push(skb, sizeof(struct mwl8k_dma_data) - hdrlen);
  589. tr = (struct mwl8k_dma_data *)skb->data;
  590. if (wh != &tr->wh)
  591. memmove(&tr->wh, wh, hdrlen);
  592. /* Clear addr4 */
  593. memset(tr->wh.addr4, 0, ETH_ALEN);
  594. /*
  595. * Firmware length is the length of the fully formed "802.11
  596. * payload". That is, everything except for the 802.11 header.
  597. * This includes all crypto material including the MIC.
  598. */
  599. tr->fwlen = cpu_to_le16(pktlen - hdrlen);
  600. }
  601. /*
  602. * Packet reception.
  603. */
  604. #define MWL8K_RX_CTRL_OWNED_BY_HOST 0x02
  605. struct mwl8k_rx_desc {
  606. __le16 pkt_len;
  607. __u8 link_quality;
  608. __u8 noise_level;
  609. __le32 pkt_phys_addr;
  610. __le32 next_rx_desc_phys_addr;
  611. __le16 qos_control;
  612. __le16 rate_info;
  613. __le32 pad0[4];
  614. __u8 rssi;
  615. __u8 channel;
  616. __le16 pad1;
  617. __u8 rx_ctrl;
  618. __u8 rx_status;
  619. __u8 pad2[2];
  620. } __attribute__((packed));
  621. #define MWL8K_RX_DESCS 256
  622. #define MWL8K_RX_MAXSZ 3800
  623. static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
  624. {
  625. struct mwl8k_priv *priv = hw->priv;
  626. struct mwl8k_rx_queue *rxq = priv->rxq + index;
  627. int size;
  628. int i;
  629. rxq->rx_desc_count = 0;
  630. rxq->rx_head = 0;
  631. rxq->rx_tail = 0;
  632. size = MWL8K_RX_DESCS * sizeof(struct mwl8k_rx_desc);
  633. rxq->rx_desc_area =
  634. pci_alloc_consistent(priv->pdev, size, &rxq->rx_desc_dma);
  635. if (rxq->rx_desc_area == NULL) {
  636. printk(KERN_ERR "%s: failed to alloc RX descriptors\n",
  637. priv->name);
  638. return -ENOMEM;
  639. }
  640. memset(rxq->rx_desc_area, 0, size);
  641. rxq->rx_skb = kmalloc(MWL8K_RX_DESCS *
  642. sizeof(*rxq->rx_skb), GFP_KERNEL);
  643. if (rxq->rx_skb == NULL) {
  644. printk(KERN_ERR "%s: failed to alloc RX skbuff list\n",
  645. priv->name);
  646. pci_free_consistent(priv->pdev, size,
  647. rxq->rx_desc_area, rxq->rx_desc_dma);
  648. return -ENOMEM;
  649. }
  650. memset(rxq->rx_skb, 0, MWL8K_RX_DESCS * sizeof(*rxq->rx_skb));
  651. for (i = 0; i < MWL8K_RX_DESCS; i++) {
  652. struct mwl8k_rx_desc *rx_desc;
  653. int nexti;
  654. rx_desc = rxq->rx_desc_area + i;
  655. nexti = (i + 1) % MWL8K_RX_DESCS;
  656. rx_desc->next_rx_desc_phys_addr =
  657. cpu_to_le32(rxq->rx_desc_dma
  658. + nexti * sizeof(*rx_desc));
  659. rx_desc->rx_ctrl = MWL8K_RX_CTRL_OWNED_BY_HOST;
  660. }
  661. return 0;
  662. }
  663. static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
  664. {
  665. struct mwl8k_priv *priv = hw->priv;
  666. struct mwl8k_rx_queue *rxq = priv->rxq + index;
  667. int refilled;
  668. refilled = 0;
  669. while (rxq->rx_desc_count < MWL8K_RX_DESCS && limit--) {
  670. struct sk_buff *skb;
  671. int rx;
  672. skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
  673. if (skb == NULL)
  674. break;
  675. rxq->rx_desc_count++;
  676. rx = rxq->rx_tail;
  677. rxq->rx_tail = (rx + 1) % MWL8K_RX_DESCS;
  678. rxq->rx_desc_area[rx].pkt_phys_addr =
  679. cpu_to_le32(pci_map_single(priv->pdev, skb->data,
  680. MWL8K_RX_MAXSZ, DMA_FROM_DEVICE));
  681. rxq->rx_desc_area[rx].pkt_len = cpu_to_le16(MWL8K_RX_MAXSZ);
  682. rxq->rx_skb[rx] = skb;
  683. wmb();
  684. rxq->rx_desc_area[rx].rx_ctrl = 0;
  685. refilled++;
  686. }
  687. return refilled;
  688. }
  689. /* Must be called only when the card's reception is completely halted */
  690. static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
  691. {
  692. struct mwl8k_priv *priv = hw->priv;
  693. struct mwl8k_rx_queue *rxq = priv->rxq + index;
  694. int i;
  695. for (i = 0; i < MWL8K_RX_DESCS; i++) {
  696. if (rxq->rx_skb[i] != NULL) {
  697. unsigned long addr;
  698. addr = le32_to_cpu(rxq->rx_desc_area[i].pkt_phys_addr);
  699. pci_unmap_single(priv->pdev, addr, MWL8K_RX_MAXSZ,
  700. PCI_DMA_FROMDEVICE);
  701. kfree_skb(rxq->rx_skb[i]);
  702. rxq->rx_skb[i] = NULL;
  703. }
  704. }
  705. kfree(rxq->rx_skb);
  706. rxq->rx_skb = NULL;
  707. pci_free_consistent(priv->pdev,
  708. MWL8K_RX_DESCS * sizeof(struct mwl8k_rx_desc),
  709. rxq->rx_desc_area, rxq->rx_desc_dma);
  710. rxq->rx_desc_area = NULL;
  711. }
  712. /*
  713. * Scan a list of BSSIDs to process for finalize join.
  714. * Allows for extension to process multiple BSSIDs.
  715. */
  716. static inline int
  717. mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
  718. {
  719. return priv->capture_beacon &&
  720. ieee80211_is_beacon(wh->frame_control) &&
  721. !compare_ether_addr(wh->addr3, priv->capture_bssid);
  722. }
  723. static inline void mwl8k_save_beacon(struct mwl8k_priv *priv,
  724. struct sk_buff *skb)
  725. {
  726. priv->capture_beacon = false;
  727. memset(priv->capture_bssid, 0, ETH_ALEN);
  728. /*
  729. * Use GFP_ATOMIC as rxq_process is called from
  730. * the primary interrupt handler, memory allocation call
  731. * must not sleep.
  732. */
  733. priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
  734. if (priv->beacon_skb != NULL)
  735. queue_work(priv->config_wq,
  736. &priv->finalize_join_worker);
  737. }
  738. static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
  739. {
  740. struct mwl8k_priv *priv = hw->priv;
  741. struct mwl8k_rx_queue *rxq = priv->rxq + index;
  742. int processed;
  743. processed = 0;
  744. while (rxq->rx_desc_count && limit--) {
  745. struct mwl8k_rx_desc *rx_desc;
  746. struct sk_buff *skb;
  747. struct ieee80211_rx_status status;
  748. unsigned long addr;
  749. struct ieee80211_hdr *wh;
  750. rx_desc = rxq->rx_desc_area + rxq->rx_head;
  751. if (!(rx_desc->rx_ctrl & MWL8K_RX_CTRL_OWNED_BY_HOST))
  752. break;
  753. rmb();
  754. skb = rxq->rx_skb[rxq->rx_head];
  755. if (skb == NULL)
  756. break;
  757. rxq->rx_skb[rxq->rx_head] = NULL;
  758. rxq->rx_head = (rxq->rx_head + 1) % MWL8K_RX_DESCS;
  759. rxq->rx_desc_count--;
  760. addr = le32_to_cpu(rx_desc->pkt_phys_addr);
  761. pci_unmap_single(priv->pdev, addr,
  762. MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
  763. skb_put(skb, le16_to_cpu(rx_desc->pkt_len));
  764. mwl8k_remove_dma_header(skb);
  765. wh = (struct ieee80211_hdr *)skb->data;
  766. /*
  767. * Check for pending join operation. save a copy of
  768. * the beacon and schedule a tasklet to send finalize
  769. * join command to the firmware.
  770. */
  771. if (mwl8k_capture_bssid(priv, wh))
  772. mwl8k_save_beacon(priv, skb);
  773. memset(&status, 0, sizeof(status));
  774. status.mactime = 0;
  775. status.signal = -rx_desc->rssi;
  776. status.noise = -rx_desc->noise_level;
  777. status.qual = rx_desc->link_quality;
  778. status.antenna = 1;
  779. status.rate_idx = 1;
  780. status.flag = 0;
  781. status.band = IEEE80211_BAND_2GHZ;
  782. status.freq = ieee80211_channel_to_frequency(rx_desc->channel);
  783. memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
  784. ieee80211_rx_irqsafe(hw, skb);
  785. processed++;
  786. }
  787. return processed;
  788. }
  789. /*
  790. * Packet transmission.
  791. */
  792. /* Transmit queue assignment. */
  793. enum {
  794. MWL8K_WME_AC_BK = 0, /* background access */
  795. MWL8K_WME_AC_BE = 1, /* best effort access */
  796. MWL8K_WME_AC_VI = 2, /* video access */
  797. MWL8K_WME_AC_VO = 3, /* voice access */
  798. };
  799. /* Transmit packet ACK policy */
  800. #define MWL8K_TXD_ACK_POLICY_NORMAL 0
  801. #define MWL8K_TXD_ACK_POLICY_BLOCKACK 3
  802. #define GET_TXQ(_ac) (\
  803. ((_ac) == WME_AC_VO) ? MWL8K_WME_AC_VO : \
  804. ((_ac) == WME_AC_VI) ? MWL8K_WME_AC_VI : \
  805. ((_ac) == WME_AC_BK) ? MWL8K_WME_AC_BK : \
  806. MWL8K_WME_AC_BE)
  807. #define MWL8K_TXD_STATUS_OK 0x00000001
  808. #define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
  809. #define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
  810. #define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
  811. #define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
  812. struct mwl8k_tx_desc {
  813. __le32 status;
  814. __u8 data_rate;
  815. __u8 tx_priority;
  816. __le16 qos_control;
  817. __le32 pkt_phys_addr;
  818. __le16 pkt_len;
  819. __u8 dest_MAC_addr[ETH_ALEN];
  820. __le32 next_tx_desc_phys_addr;
  821. __le32 reserved;
  822. __le16 rate_info;
  823. __u8 peer_id;
  824. __u8 tx_frag_cnt;
  825. } __attribute__((packed));
  826. #define MWL8K_TX_DESCS 128
  827. static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
  828. {
  829. struct mwl8k_priv *priv = hw->priv;
  830. struct mwl8k_tx_queue *txq = priv->txq + index;
  831. int size;
  832. int i;
  833. memset(&txq->tx_stats, 0, sizeof(struct ieee80211_tx_queue_stats));
  834. txq->tx_stats.limit = MWL8K_TX_DESCS;
  835. txq->tx_head = 0;
  836. txq->tx_tail = 0;
  837. size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
  838. txq->tx_desc_area =
  839. pci_alloc_consistent(priv->pdev, size, &txq->tx_desc_dma);
  840. if (txq->tx_desc_area == NULL) {
  841. printk(KERN_ERR "%s: failed to alloc TX descriptors\n",
  842. priv->name);
  843. return -ENOMEM;
  844. }
  845. memset(txq->tx_desc_area, 0, size);
  846. txq->tx_skb = kmalloc(MWL8K_TX_DESCS * sizeof(*txq->tx_skb),
  847. GFP_KERNEL);
  848. if (txq->tx_skb == NULL) {
  849. printk(KERN_ERR "%s: failed to alloc TX skbuff list\n",
  850. priv->name);
  851. pci_free_consistent(priv->pdev, size,
  852. txq->tx_desc_area, txq->tx_desc_dma);
  853. return -ENOMEM;
  854. }
  855. memset(txq->tx_skb, 0, MWL8K_TX_DESCS * sizeof(*txq->tx_skb));
  856. for (i = 0; i < MWL8K_TX_DESCS; i++) {
  857. struct mwl8k_tx_desc *tx_desc;
  858. int nexti;
  859. tx_desc = txq->tx_desc_area + i;
  860. nexti = (i + 1) % MWL8K_TX_DESCS;
  861. tx_desc->status = 0;
  862. tx_desc->next_tx_desc_phys_addr =
  863. cpu_to_le32(txq->tx_desc_dma +
  864. nexti * sizeof(*tx_desc));
  865. }
  866. return 0;
  867. }
  868. static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
  869. {
  870. iowrite32(MWL8K_H2A_INT_PPA_READY,
  871. priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  872. iowrite32(MWL8K_H2A_INT_DUMMY,
  873. priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  874. ioread32(priv->regs + MWL8K_HIU_INT_CODE);
  875. }
  876. static inline int mwl8k_txq_busy(struct mwl8k_priv *priv)
  877. {
  878. return priv->pending_tx_pkts;
  879. }
  880. struct mwl8k_txq_info {
  881. u32 fw_owned;
  882. u32 drv_owned;
  883. u32 unused;
  884. u32 len;
  885. u32 head;
  886. u32 tail;
  887. };
  888. static int mwl8k_scan_tx_ring(struct mwl8k_priv *priv,
  889. struct mwl8k_txq_info *txinfo)
  890. {
  891. int count, desc, status;
  892. struct mwl8k_tx_queue *txq;
  893. struct mwl8k_tx_desc *tx_desc;
  894. int ndescs = 0;
  895. memset(txinfo, 0, MWL8K_TX_QUEUES * sizeof(struct mwl8k_txq_info));
  896. spin_lock_bh(&priv->tx_lock);
  897. for (count = 0; count < MWL8K_TX_QUEUES; count++) {
  898. txq = priv->txq + count;
  899. txinfo[count].len = txq->tx_stats.len;
  900. txinfo[count].head = txq->tx_head;
  901. txinfo[count].tail = txq->tx_tail;
  902. for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
  903. tx_desc = txq->tx_desc_area + desc;
  904. status = le32_to_cpu(tx_desc->status);
  905. if (status & MWL8K_TXD_STATUS_FW_OWNED)
  906. txinfo[count].fw_owned++;
  907. else
  908. txinfo[count].drv_owned++;
  909. if (tx_desc->pkt_len == 0)
  910. txinfo[count].unused++;
  911. }
  912. }
  913. spin_unlock_bh(&priv->tx_lock);
  914. return ndescs;
  915. }
  916. /*
  917. * Must be called with hw->fw_mutex held and tx queues stopped.
  918. */
  919. static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
  920. {
  921. struct mwl8k_priv *priv = hw->priv;
  922. DECLARE_COMPLETION_ONSTACK(cmd_wait);
  923. u32 count;
  924. unsigned long timeout;
  925. might_sleep();
  926. spin_lock_bh(&priv->tx_lock);
  927. count = mwl8k_txq_busy(priv);
  928. if (count) {
  929. priv->tx_wait = &cmd_wait;
  930. if (priv->radio_on)
  931. mwl8k_tx_start(priv);
  932. }
  933. spin_unlock_bh(&priv->tx_lock);
  934. if (count) {
  935. struct mwl8k_txq_info txinfo[MWL8K_TX_QUEUES];
  936. int index;
  937. int newcount;
  938. timeout = wait_for_completion_timeout(&cmd_wait,
  939. msecs_to_jiffies(5000));
  940. if (timeout)
  941. return 0;
  942. spin_lock_bh(&priv->tx_lock);
  943. priv->tx_wait = NULL;
  944. newcount = mwl8k_txq_busy(priv);
  945. spin_unlock_bh(&priv->tx_lock);
  946. printk(KERN_ERR "%s(%u) TIMEDOUT:5000ms Pend:%u-->%u\n",
  947. __func__, __LINE__, count, newcount);
  948. mwl8k_scan_tx_ring(priv, txinfo);
  949. for (index = 0; index < MWL8K_TX_QUEUES; index++)
  950. printk(KERN_ERR
  951. "TXQ:%u L:%u H:%u T:%u FW:%u DRV:%u U:%u\n",
  952. index,
  953. txinfo[index].len,
  954. txinfo[index].head,
  955. txinfo[index].tail,
  956. txinfo[index].fw_owned,
  957. txinfo[index].drv_owned,
  958. txinfo[index].unused);
  959. return -ETIMEDOUT;
  960. }
  961. return 0;
  962. }
  963. #define MWL8K_TXD_SUCCESS(status) \
  964. ((status) & (MWL8K_TXD_STATUS_OK | \
  965. MWL8K_TXD_STATUS_OK_RETRY | \
  966. MWL8K_TXD_STATUS_OK_MORE_RETRY))
  967. static void mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int force)
  968. {
  969. struct mwl8k_priv *priv = hw->priv;
  970. struct mwl8k_tx_queue *txq = priv->txq + index;
  971. int wake = 0;
  972. while (txq->tx_stats.len > 0) {
  973. int tx;
  974. struct mwl8k_tx_desc *tx_desc;
  975. unsigned long addr;
  976. int size;
  977. struct sk_buff *skb;
  978. struct ieee80211_tx_info *info;
  979. u32 status;
  980. tx = txq->tx_head;
  981. tx_desc = txq->tx_desc_area + tx;
  982. status = le32_to_cpu(tx_desc->status);
  983. if (status & MWL8K_TXD_STATUS_FW_OWNED) {
  984. if (!force)
  985. break;
  986. tx_desc->status &=
  987. ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
  988. }
  989. txq->tx_head = (tx + 1) % MWL8K_TX_DESCS;
  990. BUG_ON(txq->tx_stats.len == 0);
  991. txq->tx_stats.len--;
  992. priv->pending_tx_pkts--;
  993. addr = le32_to_cpu(tx_desc->pkt_phys_addr);
  994. size = le16_to_cpu(tx_desc->pkt_len);
  995. skb = txq->tx_skb[tx];
  996. txq->tx_skb[tx] = NULL;
  997. BUG_ON(skb == NULL);
  998. pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
  999. mwl8k_remove_dma_header(skb);
  1000. /* Mark descriptor as unused */
  1001. tx_desc->pkt_phys_addr = 0;
  1002. tx_desc->pkt_len = 0;
  1003. info = IEEE80211_SKB_CB(skb);
  1004. ieee80211_tx_info_clear_status(info);
  1005. if (MWL8K_TXD_SUCCESS(status))
  1006. info->flags |= IEEE80211_TX_STAT_ACK;
  1007. ieee80211_tx_status_irqsafe(hw, skb);
  1008. wake = 1;
  1009. }
  1010. if (wake && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
  1011. ieee80211_wake_queue(hw, index);
  1012. }
  1013. /* must be called only when the card's transmit is completely halted */
  1014. static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
  1015. {
  1016. struct mwl8k_priv *priv = hw->priv;
  1017. struct mwl8k_tx_queue *txq = priv->txq + index;
  1018. mwl8k_txq_reclaim(hw, index, 1);
  1019. kfree(txq->tx_skb);
  1020. txq->tx_skb = NULL;
  1021. pci_free_consistent(priv->pdev,
  1022. MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
  1023. txq->tx_desc_area, txq->tx_desc_dma);
  1024. txq->tx_desc_area = NULL;
  1025. }
  1026. static int
  1027. mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
  1028. {
  1029. struct mwl8k_priv *priv = hw->priv;
  1030. struct ieee80211_tx_info *tx_info;
  1031. struct mwl8k_vif *mwl8k_vif;
  1032. struct ieee80211_hdr *wh;
  1033. struct mwl8k_tx_queue *txq;
  1034. struct mwl8k_tx_desc *tx;
  1035. dma_addr_t dma;
  1036. u32 txstatus;
  1037. u8 txdatarate;
  1038. u16 qos;
  1039. wh = (struct ieee80211_hdr *)skb->data;
  1040. if (ieee80211_is_data_qos(wh->frame_control))
  1041. qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
  1042. else
  1043. qos = 0;
  1044. mwl8k_add_dma_header(skb);
  1045. wh = &((struct mwl8k_dma_data *)skb->data)->wh;
  1046. tx_info = IEEE80211_SKB_CB(skb);
  1047. mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
  1048. if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  1049. u16 seqno = mwl8k_vif->seqno;
  1050. wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  1051. wh->seq_ctrl |= cpu_to_le16(seqno << 4);
  1052. mwl8k_vif->seqno = seqno++ % 4096;
  1053. }
  1054. /* Setup firmware control bit fields for each frame type. */
  1055. txstatus = 0;
  1056. txdatarate = 0;
  1057. if (ieee80211_is_mgmt(wh->frame_control) ||
  1058. ieee80211_is_ctl(wh->frame_control)) {
  1059. txdatarate = 0;
  1060. qos = mwl8k_qos_setbit_eosp(qos);
  1061. /* Set Queue size to unspecified */
  1062. qos = mwl8k_qos_setbit_qlen(qos, 0xff);
  1063. } else if (ieee80211_is_data(wh->frame_control)) {
  1064. txdatarate = 1;
  1065. if (is_multicast_ether_addr(wh->addr1))
  1066. txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
  1067. /* Send pkt in an aggregate if AMPDU frame. */
  1068. if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
  1069. qos = mwl8k_qos_setbit_ack(qos,
  1070. MWL8K_TXD_ACK_POLICY_BLOCKACK);
  1071. else
  1072. qos = mwl8k_qos_setbit_ack(qos,
  1073. MWL8K_TXD_ACK_POLICY_NORMAL);
  1074. if (qos & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
  1075. qos = mwl8k_qos_setbit_amsdu(qos);
  1076. }
  1077. dma = pci_map_single(priv->pdev, skb->data,
  1078. skb->len, PCI_DMA_TODEVICE);
  1079. if (pci_dma_mapping_error(priv->pdev, dma)) {
  1080. printk(KERN_DEBUG "%s: failed to dma map skb, "
  1081. "dropping TX frame.\n", priv->name);
  1082. dev_kfree_skb(skb);
  1083. return NETDEV_TX_OK;
  1084. }
  1085. spin_lock_bh(&priv->tx_lock);
  1086. txq = priv->txq + index;
  1087. BUG_ON(txq->tx_skb[txq->tx_tail] != NULL);
  1088. txq->tx_skb[txq->tx_tail] = skb;
  1089. tx = txq->tx_desc_area + txq->tx_tail;
  1090. tx->data_rate = txdatarate;
  1091. tx->tx_priority = index;
  1092. tx->qos_control = cpu_to_le16(qos);
  1093. tx->pkt_phys_addr = cpu_to_le32(dma);
  1094. tx->pkt_len = cpu_to_le16(skb->len);
  1095. tx->rate_info = 0;
  1096. tx->peer_id = mwl8k_vif->peer_id;
  1097. wmb();
  1098. tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
  1099. txq->tx_stats.count++;
  1100. txq->tx_stats.len++;
  1101. priv->pending_tx_pkts++;
  1102. txq->tx_tail++;
  1103. if (txq->tx_tail == MWL8K_TX_DESCS)
  1104. txq->tx_tail = 0;
  1105. if (txq->tx_head == txq->tx_tail)
  1106. ieee80211_stop_queue(hw, index);
  1107. mwl8k_tx_start(priv);
  1108. spin_unlock_bh(&priv->tx_lock);
  1109. return NETDEV_TX_OK;
  1110. }
  1111. /*
  1112. * Firmware access.
  1113. *
  1114. * We have the following requirements for issuing firmware commands:
  1115. * - Some commands require that the packet transmit path is idle when
  1116. * the command is issued. (For simplicity, we'll just quiesce the
  1117. * transmit path for every command.)
  1118. * - There are certain sequences of commands that need to be issued to
  1119. * the hardware sequentially, with no other intervening commands.
  1120. *
  1121. * This leads to an implementation of a "firmware lock" as a mutex that
  1122. * can be taken recursively, and which is taken by both the low-level
  1123. * command submission function (mwl8k_post_cmd) as well as any users of
  1124. * that function that require issuing of an atomic sequence of commands,
  1125. * and quiesces the transmit path whenever it's taken.
  1126. */
  1127. static int mwl8k_fw_lock(struct ieee80211_hw *hw)
  1128. {
  1129. struct mwl8k_priv *priv = hw->priv;
  1130. if (priv->fw_mutex_owner != current) {
  1131. int rc;
  1132. mutex_lock(&priv->fw_mutex);
  1133. ieee80211_stop_queues(hw);
  1134. rc = mwl8k_tx_wait_empty(hw);
  1135. if (rc) {
  1136. ieee80211_wake_queues(hw);
  1137. mutex_unlock(&priv->fw_mutex);
  1138. return rc;
  1139. }
  1140. priv->fw_mutex_owner = current;
  1141. }
  1142. priv->fw_mutex_depth++;
  1143. return 0;
  1144. }
  1145. static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
  1146. {
  1147. struct mwl8k_priv *priv = hw->priv;
  1148. if (!--priv->fw_mutex_depth) {
  1149. ieee80211_wake_queues(hw);
  1150. priv->fw_mutex_owner = NULL;
  1151. mutex_unlock(&priv->fw_mutex);
  1152. }
  1153. }
  1154. /*
  1155. * Command processing.
  1156. */
  1157. /* Timeout firmware commands after 2000ms */
  1158. #define MWL8K_CMD_TIMEOUT_MS 2000
  1159. static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
  1160. {
  1161. DECLARE_COMPLETION_ONSTACK(cmd_wait);
  1162. struct mwl8k_priv *priv = hw->priv;
  1163. void __iomem *regs = priv->regs;
  1164. dma_addr_t dma_addr;
  1165. unsigned int dma_size;
  1166. int rc;
  1167. unsigned long timeout = 0;
  1168. u8 buf[32];
  1169. cmd->result = 0xFFFF;
  1170. dma_size = le16_to_cpu(cmd->length);
  1171. dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
  1172. PCI_DMA_BIDIRECTIONAL);
  1173. if (pci_dma_mapping_error(priv->pdev, dma_addr))
  1174. return -ENOMEM;
  1175. rc = mwl8k_fw_lock(hw);
  1176. if (rc) {
  1177. pci_unmap_single(priv->pdev, dma_addr, dma_size,
  1178. PCI_DMA_BIDIRECTIONAL);
  1179. return rc;
  1180. }
  1181. priv->hostcmd_wait = &cmd_wait;
  1182. iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
  1183. iowrite32(MWL8K_H2A_INT_DOORBELL,
  1184. regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  1185. iowrite32(MWL8K_H2A_INT_DUMMY,
  1186. regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  1187. timeout = wait_for_completion_timeout(&cmd_wait,
  1188. msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
  1189. priv->hostcmd_wait = NULL;
  1190. mwl8k_fw_unlock(hw);
  1191. pci_unmap_single(priv->pdev, dma_addr, dma_size,
  1192. PCI_DMA_BIDIRECTIONAL);
  1193. if (!timeout) {
  1194. printk(KERN_ERR "%s: Command %s timeout after %u ms\n",
  1195. priv->name,
  1196. mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
  1197. MWL8K_CMD_TIMEOUT_MS);
  1198. rc = -ETIMEDOUT;
  1199. } else {
  1200. rc = cmd->result ? -EINVAL : 0;
  1201. if (rc)
  1202. printk(KERN_ERR "%s: Command %s error 0x%x\n",
  1203. priv->name,
  1204. mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
  1205. le16_to_cpu(cmd->result));
  1206. }
  1207. return rc;
  1208. }
  1209. /*
  1210. * GET_HW_SPEC.
  1211. */
  1212. struct mwl8k_cmd_get_hw_spec {
  1213. struct mwl8k_cmd_pkt header;
  1214. __u8 hw_rev;
  1215. __u8 host_interface;
  1216. __le16 num_mcaddrs;
  1217. __u8 perm_addr[ETH_ALEN];
  1218. __le16 region_code;
  1219. __le32 fw_rev;
  1220. __le32 ps_cookie;
  1221. __le32 caps;
  1222. __u8 mcs_bitmap[16];
  1223. __le32 rx_queue_ptr;
  1224. __le32 num_tx_queues;
  1225. __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
  1226. __le32 caps2;
  1227. __le32 num_tx_desc_per_queue;
  1228. __le32 total_rx_desc;
  1229. } __attribute__((packed));
  1230. static int mwl8k_cmd_get_hw_spec(struct ieee80211_hw *hw)
  1231. {
  1232. struct mwl8k_priv *priv = hw->priv;
  1233. struct mwl8k_cmd_get_hw_spec *cmd;
  1234. int rc;
  1235. int i;
  1236. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1237. if (cmd == NULL)
  1238. return -ENOMEM;
  1239. cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
  1240. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  1241. memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
  1242. cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
  1243. cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rx_desc_dma);
  1244. cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
  1245. for (i = 0; i < MWL8K_TX_QUEUES; i++)
  1246. cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].tx_desc_dma);
  1247. cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
  1248. cmd->total_rx_desc = cpu_to_le32(MWL8K_RX_DESCS);
  1249. rc = mwl8k_post_cmd(hw, &cmd->header);
  1250. if (!rc) {
  1251. SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
  1252. priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
  1253. priv->fw_rev = le32_to_cpu(cmd->fw_rev);
  1254. priv->hw_rev = cmd->hw_rev;
  1255. }
  1256. kfree(cmd);
  1257. return rc;
  1258. }
  1259. /*
  1260. * CMD_MAC_MULTICAST_ADR.
  1261. */
  1262. struct mwl8k_cmd_mac_multicast_adr {
  1263. struct mwl8k_cmd_pkt header;
  1264. __le16 action;
  1265. __le16 numaddr;
  1266. __u8 addr[0][ETH_ALEN];
  1267. };
  1268. #define MWL8K_ENABLE_RX_MULTICAST 0x000F
  1269. static struct mwl8k_cmd_pkt *
  1270. __mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw,
  1271. int mc_count, struct dev_addr_list *mclist)
  1272. {
  1273. struct mwl8k_priv *priv = hw->priv;
  1274. struct mwl8k_cmd_mac_multicast_adr *cmd;
  1275. int size;
  1276. int i;
  1277. if (mc_count > priv->num_mcaddrs)
  1278. mc_count = priv->num_mcaddrs;
  1279. size = sizeof(*cmd) + mc_count * ETH_ALEN;
  1280. cmd = kzalloc(size, GFP_ATOMIC);
  1281. if (cmd == NULL)
  1282. return NULL;
  1283. cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
  1284. cmd->header.length = cpu_to_le16(size);
  1285. cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
  1286. cmd->numaddr = cpu_to_le16(mc_count);
  1287. for (i = 0; i < mc_count && mclist; i++) {
  1288. if (mclist->da_addrlen != ETH_ALEN) {
  1289. kfree(cmd);
  1290. return NULL;
  1291. }
  1292. memcpy(cmd->addr[i], mclist->da_addr, ETH_ALEN);
  1293. mclist = mclist->next;
  1294. }
  1295. return &cmd->header;
  1296. }
  1297. /*
  1298. * CMD_802_11_GET_STAT.
  1299. */
  1300. struct mwl8k_cmd_802_11_get_stat {
  1301. struct mwl8k_cmd_pkt header;
  1302. __le16 action;
  1303. __le32 stats[64];
  1304. } __attribute__((packed));
  1305. #define MWL8K_STAT_ACK_FAILURE 9
  1306. #define MWL8K_STAT_RTS_FAILURE 12
  1307. #define MWL8K_STAT_FCS_ERROR 24
  1308. #define MWL8K_STAT_RTS_SUCCESS 11
  1309. static int mwl8k_cmd_802_11_get_stat(struct ieee80211_hw *hw,
  1310. struct ieee80211_low_level_stats *stats)
  1311. {
  1312. struct mwl8k_cmd_802_11_get_stat *cmd;
  1313. int rc;
  1314. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1315. if (cmd == NULL)
  1316. return -ENOMEM;
  1317. cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
  1318. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  1319. cmd->action = cpu_to_le16(MWL8K_CMD_GET);
  1320. rc = mwl8k_post_cmd(hw, &cmd->header);
  1321. if (!rc) {
  1322. stats->dot11ACKFailureCount =
  1323. le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
  1324. stats->dot11RTSFailureCount =
  1325. le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
  1326. stats->dot11FCSErrorCount =
  1327. le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
  1328. stats->dot11RTSSuccessCount =
  1329. le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
  1330. }
  1331. kfree(cmd);
  1332. return rc;
  1333. }
  1334. /*
  1335. * CMD_802_11_RADIO_CONTROL.
  1336. */
  1337. struct mwl8k_cmd_802_11_radio_control {
  1338. struct mwl8k_cmd_pkt header;
  1339. __le16 action;
  1340. __le16 control;
  1341. __le16 radio_on;
  1342. } __attribute__((packed));
  1343. static int
  1344. mwl8k_cmd_802_11_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
  1345. {
  1346. struct mwl8k_priv *priv = hw->priv;
  1347. struct mwl8k_cmd_802_11_radio_control *cmd;
  1348. int rc;
  1349. if (enable == priv->radio_on && !force)
  1350. return 0;
  1351. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1352. if (cmd == NULL)
  1353. return -ENOMEM;
  1354. cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
  1355. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  1356. cmd->action = cpu_to_le16(MWL8K_CMD_SET);
  1357. cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
  1358. cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
  1359. rc = mwl8k_post_cmd(hw, &cmd->header);
  1360. kfree(cmd);
  1361. if (!rc)
  1362. priv->radio_on = enable;
  1363. return rc;
  1364. }
  1365. static int mwl8k_cmd_802_11_radio_disable(struct ieee80211_hw *hw)
  1366. {
  1367. return mwl8k_cmd_802_11_radio_control(hw, 0, 0);
  1368. }
  1369. static int mwl8k_cmd_802_11_radio_enable(struct ieee80211_hw *hw)
  1370. {
  1371. return mwl8k_cmd_802_11_radio_control(hw, 1, 0);
  1372. }
  1373. static int
  1374. mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
  1375. {
  1376. struct mwl8k_priv *priv;
  1377. if (hw == NULL || hw->priv == NULL)
  1378. return -EINVAL;
  1379. priv = hw->priv;
  1380. priv->radio_short_preamble = short_preamble;
  1381. return mwl8k_cmd_802_11_radio_control(hw, 1, 1);
  1382. }
  1383. /*
  1384. * CMD_802_11_RF_TX_POWER.
  1385. */
  1386. #define MWL8K_TX_POWER_LEVEL_TOTAL 8
  1387. struct mwl8k_cmd_802_11_rf_tx_power {
  1388. struct mwl8k_cmd_pkt header;
  1389. __le16 action;
  1390. __le16 support_level;
  1391. __le16 current_level;
  1392. __le16 reserved;
  1393. __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
  1394. } __attribute__((packed));
  1395. static int mwl8k_cmd_802_11_rf_tx_power(struct ieee80211_hw *hw, int dBm)
  1396. {
  1397. struct mwl8k_cmd_802_11_rf_tx_power *cmd;
  1398. int rc;
  1399. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1400. if (cmd == NULL)
  1401. return -ENOMEM;
  1402. cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
  1403. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  1404. cmd->action = cpu_to_le16(MWL8K_CMD_SET);
  1405. cmd->support_level = cpu_to_le16(dBm);
  1406. rc = mwl8k_post_cmd(hw, &cmd->header);
  1407. kfree(cmd);
  1408. return rc;
  1409. }
  1410. /*
  1411. * CMD_SET_PRE_SCAN.
  1412. */
  1413. struct mwl8k_cmd_set_pre_scan {
  1414. struct mwl8k_cmd_pkt header;
  1415. } __attribute__((packed));
  1416. static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
  1417. {
  1418. struct mwl8k_cmd_set_pre_scan *cmd;
  1419. int rc;
  1420. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1421. if (cmd == NULL)
  1422. return -ENOMEM;
  1423. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
  1424. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  1425. rc = mwl8k_post_cmd(hw, &cmd->header);
  1426. kfree(cmd);
  1427. return rc;
  1428. }
  1429. /*
  1430. * CMD_SET_POST_SCAN.
  1431. */
  1432. struct mwl8k_cmd_set_post_scan {
  1433. struct mwl8k_cmd_pkt header;
  1434. __le32 isibss;
  1435. __u8 bssid[ETH_ALEN];
  1436. } __attribute__((packed));
  1437. static int
  1438. mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, __u8 *mac)
  1439. {
  1440. struct mwl8k_cmd_set_post_scan *cmd;
  1441. int rc;
  1442. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1443. if (cmd == NULL)
  1444. return -ENOMEM;
  1445. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
  1446. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  1447. cmd->isibss = 0;
  1448. memcpy(cmd->bssid, mac, ETH_ALEN);
  1449. rc = mwl8k_post_cmd(hw, &cmd->header);
  1450. kfree(cmd);
  1451. return rc;
  1452. }
  1453. /*
  1454. * CMD_SET_RF_CHANNEL.
  1455. */
  1456. struct mwl8k_cmd_set_rf_channel {
  1457. struct mwl8k_cmd_pkt header;
  1458. __le16 action;
  1459. __u8 current_channel;
  1460. __le32 channel_flags;
  1461. } __attribute__((packed));
  1462. static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
  1463. struct ieee80211_channel *channel)
  1464. {
  1465. struct mwl8k_cmd_set_rf_channel *cmd;
  1466. int rc;
  1467. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1468. if (cmd == NULL)
  1469. return -ENOMEM;
  1470. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
  1471. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  1472. cmd->action = cpu_to_le16(MWL8K_CMD_SET);
  1473. cmd->current_channel = channel->hw_value;
  1474. if (channel->band == IEEE80211_BAND_2GHZ)
  1475. cmd->channel_flags = cpu_to_le32(0x00000081);
  1476. else
  1477. cmd->channel_flags = cpu_to_le32(0x00000000);
  1478. rc = mwl8k_post_cmd(hw, &cmd->header);
  1479. kfree(cmd);
  1480. return rc;
  1481. }
  1482. /*
  1483. * CMD_SET_SLOT.
  1484. */
  1485. struct mwl8k_cmd_set_slot {
  1486. struct mwl8k_cmd_pkt header;
  1487. __le16 action;
  1488. __u8 short_slot;
  1489. } __attribute__((packed));
  1490. static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
  1491. {
  1492. struct mwl8k_cmd_set_slot *cmd;
  1493. int rc;
  1494. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1495. if (cmd == NULL)
  1496. return -ENOMEM;
  1497. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
  1498. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  1499. cmd->action = cpu_to_le16(MWL8K_CMD_SET);
  1500. cmd->short_slot = short_slot_time;
  1501. rc = mwl8k_post_cmd(hw, &cmd->header);
  1502. kfree(cmd);
  1503. return rc;
  1504. }
  1505. /*
  1506. * CMD_MIMO_CONFIG.
  1507. */
  1508. struct mwl8k_cmd_mimo_config {
  1509. struct mwl8k_cmd_pkt header;
  1510. __le32 action;
  1511. __u8 rx_antenna_map;
  1512. __u8 tx_antenna_map;
  1513. } __attribute__((packed));
  1514. static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
  1515. {
  1516. struct mwl8k_cmd_mimo_config *cmd;
  1517. int rc;
  1518. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1519. if (cmd == NULL)
  1520. return -ENOMEM;
  1521. cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
  1522. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  1523. cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
  1524. cmd->rx_antenna_map = rx;
  1525. cmd->tx_antenna_map = tx;
  1526. rc = mwl8k_post_cmd(hw, &cmd->header);
  1527. kfree(cmd);
  1528. return rc;
  1529. }
  1530. /*
  1531. * CMD_ENABLE_SNIFFER.
  1532. */
  1533. struct mwl8k_cmd_enable_sniffer {
  1534. struct mwl8k_cmd_pkt header;
  1535. __le32 action;
  1536. } __attribute__((packed));
  1537. static int mwl8k_enable_sniffer(struct ieee80211_hw *hw, bool enable)
  1538. {
  1539. struct mwl8k_cmd_enable_sniffer *cmd;
  1540. int rc;
  1541. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1542. if (cmd == NULL)
  1543. return -ENOMEM;
  1544. cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
  1545. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  1546. cmd->action = cpu_to_le32(!!enable);
  1547. rc = mwl8k_post_cmd(hw, &cmd->header);
  1548. kfree(cmd);
  1549. return rc;
  1550. }
  1551. /*
  1552. * CMD_SET_RATEADAPT_MODE.
  1553. */
  1554. struct mwl8k_cmd_set_rate_adapt_mode {
  1555. struct mwl8k_cmd_pkt header;
  1556. __le16 action;
  1557. __le16 mode;
  1558. } __attribute__((packed));
  1559. static int mwl8k_cmd_setrateadaptmode(struct ieee80211_hw *hw, __u16 mode)
  1560. {
  1561. struct mwl8k_cmd_set_rate_adapt_mode *cmd;
  1562. int rc;
  1563. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1564. if (cmd == NULL)
  1565. return -ENOMEM;
  1566. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
  1567. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  1568. cmd->action = cpu_to_le16(MWL8K_CMD_SET);
  1569. cmd->mode = cpu_to_le16(mode);
  1570. rc = mwl8k_post_cmd(hw, &cmd->header);
  1571. kfree(cmd);
  1572. return rc;
  1573. }
  1574. /*
  1575. * CMD_SET_WMM_MODE.
  1576. */
  1577. struct mwl8k_cmd_set_wmm {
  1578. struct mwl8k_cmd_pkt header;
  1579. __le16 action;
  1580. } __attribute__((packed));
  1581. static int mwl8k_set_wmm(struct ieee80211_hw *hw, bool enable)
  1582. {
  1583. struct mwl8k_priv *priv = hw->priv;
  1584. struct mwl8k_cmd_set_wmm *cmd;
  1585. int rc;
  1586. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1587. if (cmd == NULL)
  1588. return -ENOMEM;
  1589. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
  1590. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  1591. cmd->action = cpu_to_le16(!!enable);
  1592. rc = mwl8k_post_cmd(hw, &cmd->header);
  1593. kfree(cmd);
  1594. if (!rc)
  1595. priv->wmm_enabled = enable;
  1596. return rc;
  1597. }
  1598. /*
  1599. * CMD_SET_RTS_THRESHOLD.
  1600. */
  1601. struct mwl8k_cmd_rts_threshold {
  1602. struct mwl8k_cmd_pkt header;
  1603. __le16 action;
  1604. __le16 threshold;
  1605. } __attribute__((packed));
  1606. static int mwl8k_rts_threshold(struct ieee80211_hw *hw,
  1607. u16 action, u16 threshold)
  1608. {
  1609. struct mwl8k_cmd_rts_threshold *cmd;
  1610. int rc;
  1611. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1612. if (cmd == NULL)
  1613. return -ENOMEM;
  1614. cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
  1615. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  1616. cmd->action = cpu_to_le16(action);
  1617. cmd->threshold = cpu_to_le16(threshold);
  1618. rc = mwl8k_post_cmd(hw, &cmd->header);
  1619. kfree(cmd);
  1620. return rc;
  1621. }
  1622. /*
  1623. * CMD_SET_EDCA_PARAMS.
  1624. */
  1625. struct mwl8k_cmd_set_edca_params {
  1626. struct mwl8k_cmd_pkt header;
  1627. /* See MWL8K_SET_EDCA_XXX below */
  1628. __le16 action;
  1629. /* TX opportunity in units of 32 us */
  1630. __le16 txop;
  1631. /* Log exponent of max contention period: 0...15*/
  1632. __u8 log_cw_max;
  1633. /* Log exponent of min contention period: 0...15 */
  1634. __u8 log_cw_min;
  1635. /* Adaptive interframe spacing in units of 32us */
  1636. __u8 aifs;
  1637. /* TX queue to configure */
  1638. __u8 txq;
  1639. } __attribute__((packed));
  1640. #define MWL8K_SET_EDCA_CW 0x01
  1641. #define MWL8K_SET_EDCA_TXOP 0x02
  1642. #define MWL8K_SET_EDCA_AIFS 0x04
  1643. #define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
  1644. MWL8K_SET_EDCA_TXOP | \
  1645. MWL8K_SET_EDCA_AIFS)
  1646. static int
  1647. mwl8k_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
  1648. __u16 cw_min, __u16 cw_max,
  1649. __u8 aifs, __u16 txop)
  1650. {
  1651. struct mwl8k_cmd_set_edca_params *cmd;
  1652. int rc;
  1653. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1654. if (cmd == NULL)
  1655. return -ENOMEM;
  1656. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
  1657. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  1658. cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
  1659. cmd->txop = cpu_to_le16(txop);
  1660. cmd->log_cw_max = (u8)ilog2(cw_max + 1);
  1661. cmd->log_cw_min = (u8)ilog2(cw_min + 1);
  1662. cmd->aifs = aifs;
  1663. cmd->txq = qnum;
  1664. rc = mwl8k_post_cmd(hw, &cmd->header);
  1665. kfree(cmd);
  1666. return rc;
  1667. }
  1668. /*
  1669. * CMD_FINALIZE_JOIN.
  1670. */
  1671. /* FJ beacon buffer size is compiled into the firmware. */
  1672. #define MWL8K_FJ_BEACON_MAXLEN 128
  1673. struct mwl8k_cmd_finalize_join {
  1674. struct mwl8k_cmd_pkt header;
  1675. __le32 sleep_interval; /* Number of beacon periods to sleep */
  1676. __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
  1677. } __attribute__((packed));
  1678. static int mwl8k_finalize_join(struct ieee80211_hw *hw, void *frame,
  1679. __u16 framelen, __u16 dtim)
  1680. {
  1681. struct mwl8k_cmd_finalize_join *cmd;
  1682. struct ieee80211_mgmt *payload = frame;
  1683. u16 hdrlen;
  1684. u32 payload_len;
  1685. int rc;
  1686. if (frame == NULL)
  1687. return -EINVAL;
  1688. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1689. if (cmd == NULL)
  1690. return -ENOMEM;
  1691. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
  1692. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  1693. cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
  1694. hdrlen = ieee80211_hdrlen(payload->frame_control);
  1695. payload_len = framelen > hdrlen ? framelen - hdrlen : 0;
  1696. /* XXX TBD Might just have to abort and return an error */
  1697. if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
  1698. printk(KERN_ERR "%s(): WARNING: Incomplete beacon "
  1699. "sent to firmware. Sz=%u MAX=%u\n", __func__,
  1700. payload_len, MWL8K_FJ_BEACON_MAXLEN);
  1701. if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
  1702. payload_len = MWL8K_FJ_BEACON_MAXLEN;
  1703. if (payload && payload_len)
  1704. memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
  1705. rc = mwl8k_post_cmd(hw, &cmd->header);
  1706. kfree(cmd);
  1707. return rc;
  1708. }
  1709. /*
  1710. * CMD_UPDATE_STADB.
  1711. */
  1712. struct mwl8k_cmd_update_sta_db {
  1713. struct mwl8k_cmd_pkt header;
  1714. /* See STADB_ACTION_TYPE */
  1715. __le32 action;
  1716. /* Peer MAC address */
  1717. __u8 peer_addr[ETH_ALEN];
  1718. __le32 reserved;
  1719. /* Peer info - valid during add/update. */
  1720. struct peer_capability_info peer_info;
  1721. } __attribute__((packed));
  1722. static int mwl8k_cmd_update_sta_db(struct ieee80211_hw *hw,
  1723. struct ieee80211_vif *vif, __u32 action)
  1724. {
  1725. struct mwl8k_vif *mv_vif = MWL8K_VIF(vif);
  1726. struct ieee80211_bss_conf *info = &mv_vif->bss_info;
  1727. struct mwl8k_cmd_update_sta_db *cmd;
  1728. struct peer_capability_info *peer_info;
  1729. struct ieee80211_rate *bitrates = mv_vif->legacy_rates;
  1730. int rc;
  1731. __u8 count, *rates;
  1732. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1733. if (cmd == NULL)
  1734. return -ENOMEM;
  1735. cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
  1736. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  1737. cmd->action = cpu_to_le32(action);
  1738. peer_info = &cmd->peer_info;
  1739. memcpy(cmd->peer_addr, mv_vif->bssid, ETH_ALEN);
  1740. switch (action) {
  1741. case MWL8K_STA_DB_ADD_ENTRY:
  1742. case MWL8K_STA_DB_MODIFY_ENTRY:
  1743. /* Build peer_info block */
  1744. peer_info->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
  1745. peer_info->basic_caps = cpu_to_le16(info->assoc_capability);
  1746. peer_info->interop = 1;
  1747. peer_info->amsdu_enabled = 0;
  1748. rates = peer_info->legacy_rates;
  1749. for (count = 0; count < mv_vif->legacy_nrates; count++)
  1750. rates[count] = bitrates[count].hw_value;
  1751. rc = mwl8k_post_cmd(hw, &cmd->header);
  1752. if (rc == 0)
  1753. mv_vif->peer_id = peer_info->station_id;
  1754. break;
  1755. case MWL8K_STA_DB_DEL_ENTRY:
  1756. case MWL8K_STA_DB_FLUSH:
  1757. default:
  1758. rc = mwl8k_post_cmd(hw, &cmd->header);
  1759. if (rc == 0)
  1760. mv_vif->peer_id = 0;
  1761. break;
  1762. }
  1763. kfree(cmd);
  1764. return rc;
  1765. }
  1766. /*
  1767. * CMD_SET_AID.
  1768. */
  1769. #define MWL8K_RATE_INDEX_MAX_ARRAY 14
  1770. #define MWL8K_FRAME_PROT_DISABLED 0x00
  1771. #define MWL8K_FRAME_PROT_11G 0x07
  1772. #define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
  1773. #define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
  1774. struct mwl8k_cmd_update_set_aid {
  1775. struct mwl8k_cmd_pkt header;
  1776. __le16 aid;
  1777. /* AP's MAC address (BSSID) */
  1778. __u8 bssid[ETH_ALEN];
  1779. __le16 protection_mode;
  1780. __u8 supp_rates[MWL8K_RATE_INDEX_MAX_ARRAY];
  1781. } __attribute__((packed));
  1782. static int mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
  1783. struct ieee80211_vif *vif)
  1784. {
  1785. struct mwl8k_vif *mv_vif = MWL8K_VIF(vif);
  1786. struct ieee80211_bss_conf *info = &mv_vif->bss_info;
  1787. struct mwl8k_cmd_update_set_aid *cmd;
  1788. struct ieee80211_rate *bitrates = mv_vif->legacy_rates;
  1789. int count;
  1790. u16 prot_mode;
  1791. int rc;
  1792. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1793. if (cmd == NULL)
  1794. return -ENOMEM;
  1795. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
  1796. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  1797. cmd->aid = cpu_to_le16(info->aid);
  1798. memcpy(cmd->bssid, mv_vif->bssid, ETH_ALEN);
  1799. if (info->use_cts_prot) {
  1800. prot_mode = MWL8K_FRAME_PROT_11G;
  1801. } else {
  1802. switch (info->ht_operation_mode &
  1803. IEEE80211_HT_OP_MODE_PROTECTION) {
  1804. case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
  1805. prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
  1806. break;
  1807. case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
  1808. prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
  1809. break;
  1810. default:
  1811. prot_mode = MWL8K_FRAME_PROT_DISABLED;
  1812. break;
  1813. }
  1814. }
  1815. cmd->protection_mode = cpu_to_le16(prot_mode);
  1816. for (count = 0; count < mv_vif->legacy_nrates; count++)
  1817. cmd->supp_rates[count] = bitrates[count].hw_value;
  1818. rc = mwl8k_post_cmd(hw, &cmd->header);
  1819. kfree(cmd);
  1820. return rc;
  1821. }
  1822. /*
  1823. * CMD_SET_RATE.
  1824. */
  1825. struct mwl8k_cmd_update_rateset {
  1826. struct mwl8k_cmd_pkt header;
  1827. __u8 legacy_rates[MWL8K_RATE_INDEX_MAX_ARRAY];
  1828. /* Bitmap for supported MCS codes. */
  1829. __u8 mcs_set[MWL8K_IEEE_LEGACY_DATA_RATES];
  1830. __u8 reserved[MWL8K_IEEE_LEGACY_DATA_RATES];
  1831. } __attribute__((packed));
  1832. static int mwl8k_update_rateset(struct ieee80211_hw *hw,
  1833. struct ieee80211_vif *vif)
  1834. {
  1835. struct mwl8k_vif *mv_vif = MWL8K_VIF(vif);
  1836. struct mwl8k_cmd_update_rateset *cmd;
  1837. struct ieee80211_rate *bitrates = mv_vif->legacy_rates;
  1838. int count;
  1839. int rc;
  1840. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1841. if (cmd == NULL)
  1842. return -ENOMEM;
  1843. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
  1844. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  1845. for (count = 0; count < mv_vif->legacy_nrates; count++)
  1846. cmd->legacy_rates[count] = bitrates[count].hw_value;
  1847. rc = mwl8k_post_cmd(hw, &cmd->header);
  1848. kfree(cmd);
  1849. return rc;
  1850. }
  1851. /*
  1852. * CMD_USE_FIXED_RATE.
  1853. */
  1854. #define MWL8K_RATE_TABLE_SIZE 8
  1855. #define MWL8K_UCAST_RATE 0
  1856. #define MWL8K_USE_AUTO_RATE 0x0002
  1857. struct mwl8k_rate_entry {
  1858. /* Set to 1 if HT rate, 0 if legacy. */
  1859. __le32 is_ht_rate;
  1860. /* Set to 1 to use retry_count field. */
  1861. __le32 enable_retry;
  1862. /* Specified legacy rate or MCS. */
  1863. __le32 rate;
  1864. /* Number of allowed retries. */
  1865. __le32 retry_count;
  1866. } __attribute__((packed));
  1867. struct mwl8k_rate_table {
  1868. /* 1 to allow specified rate and below */
  1869. __le32 allow_rate_drop;
  1870. __le32 num_rates;
  1871. struct mwl8k_rate_entry rate_entry[MWL8K_RATE_TABLE_SIZE];
  1872. } __attribute__((packed));
  1873. struct mwl8k_cmd_use_fixed_rate {
  1874. struct mwl8k_cmd_pkt header;
  1875. __le32 action;
  1876. struct mwl8k_rate_table rate_table;
  1877. /* Unicast, Broadcast or Multicast */
  1878. __le32 rate_type;
  1879. __le32 reserved1;
  1880. __le32 reserved2;
  1881. } __attribute__((packed));
  1882. static int mwl8k_cmd_use_fixed_rate(struct ieee80211_hw *hw,
  1883. u32 action, u32 rate_type, struct mwl8k_rate_table *rate_table)
  1884. {
  1885. struct mwl8k_cmd_use_fixed_rate *cmd;
  1886. int count;
  1887. int rc;
  1888. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1889. if (cmd == NULL)
  1890. return -ENOMEM;
  1891. cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
  1892. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  1893. cmd->action = cpu_to_le32(action);
  1894. cmd->rate_type = cpu_to_le32(rate_type);
  1895. if (rate_table != NULL) {
  1896. /* Copy over each field manually so
  1897. * that bitflipping can be done
  1898. */
  1899. cmd->rate_table.allow_rate_drop =
  1900. cpu_to_le32(rate_table->allow_rate_drop);
  1901. cmd->rate_table.num_rates =
  1902. cpu_to_le32(rate_table->num_rates);
  1903. for (count = 0; count < rate_table->num_rates; count++) {
  1904. struct mwl8k_rate_entry *dst =
  1905. &cmd->rate_table.rate_entry[count];
  1906. struct mwl8k_rate_entry *src =
  1907. &rate_table->rate_entry[count];
  1908. dst->is_ht_rate = cpu_to_le32(src->is_ht_rate);
  1909. dst->enable_retry = cpu_to_le32(src->enable_retry);
  1910. dst->rate = cpu_to_le32(src->rate);
  1911. dst->retry_count = cpu_to_le32(src->retry_count);
  1912. }
  1913. }
  1914. rc = mwl8k_post_cmd(hw, &cmd->header);
  1915. kfree(cmd);
  1916. return rc;
  1917. }
  1918. /*
  1919. * Interrupt handling.
  1920. */
  1921. static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
  1922. {
  1923. struct ieee80211_hw *hw = dev_id;
  1924. struct mwl8k_priv *priv = hw->priv;
  1925. u32 status;
  1926. status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
  1927. iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
  1928. if (!status)
  1929. return IRQ_NONE;
  1930. if (status & MWL8K_A2H_INT_TX_DONE)
  1931. tasklet_schedule(&priv->tx_reclaim_task);
  1932. if (status & MWL8K_A2H_INT_RX_READY) {
  1933. while (rxq_process(hw, 0, 1))
  1934. rxq_refill(hw, 0, 1);
  1935. }
  1936. if (status & MWL8K_A2H_INT_OPC_DONE) {
  1937. if (priv->hostcmd_wait != NULL)
  1938. complete(priv->hostcmd_wait);
  1939. }
  1940. if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
  1941. if (!mutex_is_locked(&priv->fw_mutex) &&
  1942. priv->radio_on && mwl8k_txq_busy(priv))
  1943. mwl8k_tx_start(priv);
  1944. }
  1945. return IRQ_HANDLED;
  1946. }
  1947. /*
  1948. * Core driver operations.
  1949. */
  1950. static int mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  1951. {
  1952. struct mwl8k_priv *priv = hw->priv;
  1953. int index = skb_get_queue_mapping(skb);
  1954. int rc;
  1955. if (priv->current_channel == NULL) {
  1956. printk(KERN_DEBUG "%s: dropped TX frame since radio "
  1957. "disabled\n", priv->name);
  1958. dev_kfree_skb(skb);
  1959. return NETDEV_TX_OK;
  1960. }
  1961. rc = mwl8k_txq_xmit(hw, index, skb);
  1962. return rc;
  1963. }
  1964. static int mwl8k_start(struct ieee80211_hw *hw)
  1965. {
  1966. struct mwl8k_priv *priv = hw->priv;
  1967. int rc;
  1968. rc = request_irq(priv->pdev->irq, &mwl8k_interrupt,
  1969. IRQF_SHARED, MWL8K_NAME, hw);
  1970. if (rc) {
  1971. printk(KERN_ERR "%s: failed to register IRQ handler\n",
  1972. priv->name);
  1973. return -EIO;
  1974. }
  1975. /* Enable tx reclaim tasklet */
  1976. tasklet_enable(&priv->tx_reclaim_task);
  1977. /* Enable interrupts */
  1978. iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  1979. rc = mwl8k_fw_lock(hw);
  1980. if (!rc) {
  1981. rc = mwl8k_cmd_802_11_radio_enable(hw);
  1982. if (!rc)
  1983. rc = mwl8k_cmd_set_pre_scan(hw);
  1984. if (!rc)
  1985. rc = mwl8k_cmd_set_post_scan(hw,
  1986. "\x00\x00\x00\x00\x00\x00");
  1987. if (!rc)
  1988. rc = mwl8k_cmd_setrateadaptmode(hw, 0);
  1989. if (!rc)
  1990. rc = mwl8k_set_wmm(hw, 0);
  1991. if (!rc)
  1992. rc = mwl8k_enable_sniffer(hw, 0);
  1993. mwl8k_fw_unlock(hw);
  1994. }
  1995. if (rc) {
  1996. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  1997. free_irq(priv->pdev->irq, hw);
  1998. tasklet_disable(&priv->tx_reclaim_task);
  1999. }
  2000. return rc;
  2001. }
  2002. static void mwl8k_stop(struct ieee80211_hw *hw)
  2003. {
  2004. struct mwl8k_priv *priv = hw->priv;
  2005. int i;
  2006. mwl8k_cmd_802_11_radio_disable(hw);
  2007. ieee80211_stop_queues(hw);
  2008. /* Disable interrupts */
  2009. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  2010. free_irq(priv->pdev->irq, hw);
  2011. /* Stop finalize join worker */
  2012. cancel_work_sync(&priv->finalize_join_worker);
  2013. if (priv->beacon_skb != NULL)
  2014. dev_kfree_skb(priv->beacon_skb);
  2015. /* Stop tx reclaim tasklet */
  2016. tasklet_disable(&priv->tx_reclaim_task);
  2017. /* Stop config thread */
  2018. flush_workqueue(priv->config_wq);
  2019. /* Return all skbs to mac80211 */
  2020. for (i = 0; i < MWL8K_TX_QUEUES; i++)
  2021. mwl8k_txq_reclaim(hw, i, 1);
  2022. }
  2023. static int mwl8k_add_interface(struct ieee80211_hw *hw,
  2024. struct ieee80211_if_init_conf *conf)
  2025. {
  2026. struct mwl8k_priv *priv = hw->priv;
  2027. struct mwl8k_vif *mwl8k_vif;
  2028. /*
  2029. * We only support one active interface at a time.
  2030. */
  2031. if (priv->vif != NULL)
  2032. return -EBUSY;
  2033. /*
  2034. * We only support managed interfaces for now.
  2035. */
  2036. if (conf->type != NL80211_IFTYPE_STATION)
  2037. return -EINVAL;
  2038. /* Clean out driver private area */
  2039. mwl8k_vif = MWL8K_VIF(conf->vif);
  2040. memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
  2041. /* Save the mac address */
  2042. memcpy(mwl8k_vif->mac_addr, conf->mac_addr, ETH_ALEN);
  2043. /* Back pointer to parent config block */
  2044. mwl8k_vif->priv = priv;
  2045. /* Setup initial PHY parameters */
  2046. memcpy(mwl8k_vif->legacy_rates,
  2047. priv->rates, sizeof(mwl8k_vif->legacy_rates));
  2048. mwl8k_vif->legacy_nrates = ARRAY_SIZE(priv->rates);
  2049. /* Set Initial sequence number to zero */
  2050. mwl8k_vif->seqno = 0;
  2051. priv->vif = conf->vif;
  2052. priv->current_channel = NULL;
  2053. return 0;
  2054. }
  2055. static void mwl8k_remove_interface(struct ieee80211_hw *hw,
  2056. struct ieee80211_if_init_conf *conf)
  2057. {
  2058. struct mwl8k_priv *priv = hw->priv;
  2059. if (priv->vif == NULL)
  2060. return;
  2061. priv->vif = NULL;
  2062. }
  2063. static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
  2064. {
  2065. struct ieee80211_conf *conf = &hw->conf;
  2066. struct mwl8k_priv *priv = hw->priv;
  2067. int rc;
  2068. if (conf->flags & IEEE80211_CONF_IDLE) {
  2069. mwl8k_cmd_802_11_radio_disable(hw);
  2070. priv->current_channel = NULL;
  2071. return 0;
  2072. }
  2073. rc = mwl8k_fw_lock(hw);
  2074. if (rc)
  2075. return rc;
  2076. rc = mwl8k_cmd_802_11_radio_enable(hw);
  2077. if (rc)
  2078. goto out;
  2079. rc = mwl8k_cmd_set_rf_channel(hw, conf->channel);
  2080. if (rc)
  2081. goto out;
  2082. priv->current_channel = conf->channel;
  2083. if (conf->power_level > 18)
  2084. conf->power_level = 18;
  2085. rc = mwl8k_cmd_802_11_rf_tx_power(hw, conf->power_level);
  2086. if (rc)
  2087. goto out;
  2088. if (mwl8k_cmd_mimo_config(hw, 0x7, 0x7))
  2089. rc = -EINVAL;
  2090. out:
  2091. mwl8k_fw_unlock(hw);
  2092. return rc;
  2093. }
  2094. static void mwl8k_bss_info_changed(struct ieee80211_hw *hw,
  2095. struct ieee80211_vif *vif,
  2096. struct ieee80211_bss_conf *info,
  2097. u32 changed)
  2098. {
  2099. struct mwl8k_priv *priv = hw->priv;
  2100. struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
  2101. int rc;
  2102. if (changed & BSS_CHANGED_BSSID)
  2103. memcpy(mwl8k_vif->bssid, info->bssid, ETH_ALEN);
  2104. if ((changed & BSS_CHANGED_ASSOC) == 0)
  2105. return;
  2106. priv->capture_beacon = false;
  2107. rc = mwl8k_fw_lock(hw);
  2108. if (rc)
  2109. return;
  2110. if (info->assoc) {
  2111. memcpy(&mwl8k_vif->bss_info, info,
  2112. sizeof(struct ieee80211_bss_conf));
  2113. /* Install rates */
  2114. rc = mwl8k_update_rateset(hw, vif);
  2115. if (rc)
  2116. goto out;
  2117. /* Turn on rate adaptation */
  2118. rc = mwl8k_cmd_use_fixed_rate(hw, MWL8K_USE_AUTO_RATE,
  2119. MWL8K_UCAST_RATE, NULL);
  2120. if (rc)
  2121. goto out;
  2122. /* Set radio preamble */
  2123. rc = mwl8k_set_radio_preamble(hw, info->use_short_preamble);
  2124. if (rc)
  2125. goto out;
  2126. /* Set slot time */
  2127. rc = mwl8k_cmd_set_slot(hw, info->use_short_slot);
  2128. if (rc)
  2129. goto out;
  2130. /* Update peer rate info */
  2131. rc = mwl8k_cmd_update_sta_db(hw, vif,
  2132. MWL8K_STA_DB_MODIFY_ENTRY);
  2133. if (rc)
  2134. goto out;
  2135. /* Set AID */
  2136. rc = mwl8k_cmd_set_aid(hw, vif);
  2137. if (rc)
  2138. goto out;
  2139. /*
  2140. * Finalize the join. Tell rx handler to process
  2141. * next beacon from our BSSID.
  2142. */
  2143. memcpy(priv->capture_bssid, mwl8k_vif->bssid, ETH_ALEN);
  2144. priv->capture_beacon = true;
  2145. } else {
  2146. rc = mwl8k_cmd_update_sta_db(hw, vif, MWL8K_STA_DB_DEL_ENTRY);
  2147. memset(&mwl8k_vif->bss_info, 0,
  2148. sizeof(struct ieee80211_bss_conf));
  2149. memset(mwl8k_vif->bssid, 0, ETH_ALEN);
  2150. }
  2151. out:
  2152. mwl8k_fw_unlock(hw);
  2153. }
  2154. static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
  2155. int mc_count, struct dev_addr_list *mclist)
  2156. {
  2157. struct mwl8k_cmd_pkt *cmd;
  2158. cmd = __mwl8k_cmd_mac_multicast_adr(hw, mc_count, mclist);
  2159. return (unsigned long)cmd;
  2160. }
  2161. static void mwl8k_configure_filter(struct ieee80211_hw *hw,
  2162. unsigned int changed_flags,
  2163. unsigned int *total_flags,
  2164. u64 multicast)
  2165. {
  2166. struct mwl8k_priv *priv = hw->priv;
  2167. struct mwl8k_cmd_pkt *multicast_adr_cmd;
  2168. /* Clear unsupported feature flags */
  2169. *total_flags &= FIF_BCN_PRBRESP_PROMISC;
  2170. if (mwl8k_fw_lock(hw))
  2171. return;
  2172. if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
  2173. if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
  2174. mwl8k_cmd_set_pre_scan(hw);
  2175. else {
  2176. u8 *bssid;
  2177. bssid = "\x00\x00\x00\x00\x00\x00";
  2178. if (priv->vif != NULL)
  2179. bssid = MWL8K_VIF(priv->vif)->bssid;
  2180. mwl8k_cmd_set_post_scan(hw, bssid);
  2181. }
  2182. }
  2183. multicast_adr_cmd = (void *)(unsigned long)multicast;
  2184. if (multicast_adr_cmd != NULL) {
  2185. mwl8k_post_cmd(hw, multicast_adr_cmd);
  2186. kfree(multicast_adr_cmd);
  2187. }
  2188. mwl8k_fw_unlock(hw);
  2189. }
  2190. static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  2191. {
  2192. return mwl8k_rts_threshold(hw, MWL8K_CMD_SET, value);
  2193. }
  2194. static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
  2195. const struct ieee80211_tx_queue_params *params)
  2196. {
  2197. struct mwl8k_priv *priv = hw->priv;
  2198. int rc;
  2199. rc = mwl8k_fw_lock(hw);
  2200. if (!rc) {
  2201. if (!priv->wmm_enabled)
  2202. rc = mwl8k_set_wmm(hw, 1);
  2203. if (!rc)
  2204. rc = mwl8k_set_edca_params(hw, queue,
  2205. params->cw_min,
  2206. params->cw_max,
  2207. params->aifs,
  2208. params->txop);
  2209. mwl8k_fw_unlock(hw);
  2210. }
  2211. return rc;
  2212. }
  2213. static int mwl8k_get_tx_stats(struct ieee80211_hw *hw,
  2214. struct ieee80211_tx_queue_stats *stats)
  2215. {
  2216. struct mwl8k_priv *priv = hw->priv;
  2217. struct mwl8k_tx_queue *txq;
  2218. int index;
  2219. spin_lock_bh(&priv->tx_lock);
  2220. for (index = 0; index < MWL8K_TX_QUEUES; index++) {
  2221. txq = priv->txq + index;
  2222. memcpy(&stats[index], &txq->tx_stats,
  2223. sizeof(struct ieee80211_tx_queue_stats));
  2224. }
  2225. spin_unlock_bh(&priv->tx_lock);
  2226. return 0;
  2227. }
  2228. static int mwl8k_get_stats(struct ieee80211_hw *hw,
  2229. struct ieee80211_low_level_stats *stats)
  2230. {
  2231. return mwl8k_cmd_802_11_get_stat(hw, stats);
  2232. }
  2233. static const struct ieee80211_ops mwl8k_ops = {
  2234. .tx = mwl8k_tx,
  2235. .start = mwl8k_start,
  2236. .stop = mwl8k_stop,
  2237. .add_interface = mwl8k_add_interface,
  2238. .remove_interface = mwl8k_remove_interface,
  2239. .config = mwl8k_config,
  2240. .bss_info_changed = mwl8k_bss_info_changed,
  2241. .prepare_multicast = mwl8k_prepare_multicast,
  2242. .configure_filter = mwl8k_configure_filter,
  2243. .set_rts_threshold = mwl8k_set_rts_threshold,
  2244. .conf_tx = mwl8k_conf_tx,
  2245. .get_tx_stats = mwl8k_get_tx_stats,
  2246. .get_stats = mwl8k_get_stats,
  2247. };
  2248. static void mwl8k_tx_reclaim_handler(unsigned long data)
  2249. {
  2250. int i;
  2251. struct ieee80211_hw *hw = (struct ieee80211_hw *) data;
  2252. struct mwl8k_priv *priv = hw->priv;
  2253. spin_lock_bh(&priv->tx_lock);
  2254. for (i = 0; i < MWL8K_TX_QUEUES; i++)
  2255. mwl8k_txq_reclaim(hw, i, 0);
  2256. if (priv->tx_wait != NULL && mwl8k_txq_busy(priv) == 0) {
  2257. complete(priv->tx_wait);
  2258. priv->tx_wait = NULL;
  2259. }
  2260. spin_unlock_bh(&priv->tx_lock);
  2261. }
  2262. static void mwl8k_finalize_join_worker(struct work_struct *work)
  2263. {
  2264. struct mwl8k_priv *priv =
  2265. container_of(work, struct mwl8k_priv, finalize_join_worker);
  2266. struct sk_buff *skb = priv->beacon_skb;
  2267. u8 dtim = MWL8K_VIF(priv->vif)->bss_info.dtim_period;
  2268. mwl8k_finalize_join(priv->hw, skb->data, skb->len, dtim);
  2269. dev_kfree_skb(skb);
  2270. priv->beacon_skb = NULL;
  2271. }
  2272. static int __devinit mwl8k_probe(struct pci_dev *pdev,
  2273. const struct pci_device_id *id)
  2274. {
  2275. static int printed_version = 0;
  2276. struct ieee80211_hw *hw;
  2277. struct mwl8k_priv *priv;
  2278. int rc;
  2279. int i;
  2280. if (!printed_version) {
  2281. printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
  2282. printed_version = 1;
  2283. }
  2284. rc = pci_enable_device(pdev);
  2285. if (rc) {
  2286. printk(KERN_ERR "%s: Cannot enable new PCI device\n",
  2287. MWL8K_NAME);
  2288. return rc;
  2289. }
  2290. rc = pci_request_regions(pdev, MWL8K_NAME);
  2291. if (rc) {
  2292. printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
  2293. MWL8K_NAME);
  2294. return rc;
  2295. }
  2296. pci_set_master(pdev);
  2297. hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
  2298. if (hw == NULL) {
  2299. printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
  2300. rc = -ENOMEM;
  2301. goto err_free_reg;
  2302. }
  2303. priv = hw->priv;
  2304. priv->hw = hw;
  2305. priv->pdev = pdev;
  2306. priv->wmm_enabled = false;
  2307. priv->pending_tx_pkts = 0;
  2308. strncpy(priv->name, MWL8K_NAME, sizeof(priv->name));
  2309. SET_IEEE80211_DEV(hw, &pdev->dev);
  2310. pci_set_drvdata(pdev, hw);
  2311. priv->regs = pci_iomap(pdev, 1, 0x10000);
  2312. if (priv->regs == NULL) {
  2313. printk(KERN_ERR "%s: Cannot map device memory\n", priv->name);
  2314. goto err_iounmap;
  2315. }
  2316. memcpy(priv->channels, mwl8k_channels, sizeof(mwl8k_channels));
  2317. priv->band.band = IEEE80211_BAND_2GHZ;
  2318. priv->band.channels = priv->channels;
  2319. priv->band.n_channels = ARRAY_SIZE(mwl8k_channels);
  2320. priv->band.bitrates = priv->rates;
  2321. priv->band.n_bitrates = ARRAY_SIZE(mwl8k_rates);
  2322. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  2323. BUILD_BUG_ON(sizeof(priv->rates) != sizeof(mwl8k_rates));
  2324. memcpy(priv->rates, mwl8k_rates, sizeof(mwl8k_rates));
  2325. /*
  2326. * Extra headroom is the size of the required DMA header
  2327. * minus the size of the smallest 802.11 frame (CTS frame).
  2328. */
  2329. hw->extra_tx_headroom =
  2330. sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
  2331. hw->channel_change_time = 10;
  2332. hw->queues = MWL8K_TX_QUEUES;
  2333. hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  2334. /* Set rssi and noise values to dBm */
  2335. hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_NOISE_DBM;
  2336. hw->vif_data_size = sizeof(struct mwl8k_vif);
  2337. priv->vif = NULL;
  2338. /* Set default radio state and preamble */
  2339. priv->radio_on = 0;
  2340. priv->radio_short_preamble = 0;
  2341. /* Finalize join worker */
  2342. INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
  2343. /* TX reclaim tasklet */
  2344. tasklet_init(&priv->tx_reclaim_task,
  2345. mwl8k_tx_reclaim_handler, (unsigned long)hw);
  2346. tasklet_disable(&priv->tx_reclaim_task);
  2347. /* Config workthread */
  2348. priv->config_wq = create_singlethread_workqueue("mwl8k_config");
  2349. if (priv->config_wq == NULL)
  2350. goto err_iounmap;
  2351. /* Power management cookie */
  2352. priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
  2353. if (priv->cookie == NULL)
  2354. goto err_iounmap;
  2355. rc = mwl8k_rxq_init(hw, 0);
  2356. if (rc)
  2357. goto err_iounmap;
  2358. rxq_refill(hw, 0, INT_MAX);
  2359. mutex_init(&priv->fw_mutex);
  2360. priv->fw_mutex_owner = NULL;
  2361. priv->fw_mutex_depth = 0;
  2362. priv->tx_wait = NULL;
  2363. priv->hostcmd_wait = NULL;
  2364. spin_lock_init(&priv->tx_lock);
  2365. for (i = 0; i < MWL8K_TX_QUEUES; i++) {
  2366. rc = mwl8k_txq_init(hw, i);
  2367. if (rc)
  2368. goto err_free_queues;
  2369. }
  2370. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
  2371. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  2372. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
  2373. iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
  2374. rc = request_irq(priv->pdev->irq, &mwl8k_interrupt,
  2375. IRQF_SHARED, MWL8K_NAME, hw);
  2376. if (rc) {
  2377. printk(KERN_ERR "%s: failed to register IRQ handler\n",
  2378. priv->name);
  2379. goto err_free_queues;
  2380. }
  2381. /* Reset firmware and hardware */
  2382. mwl8k_hw_reset(priv);
  2383. /* Ask userland hotplug daemon for the device firmware */
  2384. rc = mwl8k_request_firmware(priv, (u32)id->driver_data);
  2385. if (rc) {
  2386. printk(KERN_ERR "%s: Firmware files not found\n", priv->name);
  2387. goto err_free_irq;
  2388. }
  2389. /* Load firmware into hardware */
  2390. rc = mwl8k_load_firmware(priv);
  2391. if (rc) {
  2392. printk(KERN_ERR "%s: Cannot start firmware\n", priv->name);
  2393. goto err_stop_firmware;
  2394. }
  2395. /* Reclaim memory once firmware is successfully loaded */
  2396. mwl8k_release_firmware(priv);
  2397. /*
  2398. * Temporarily enable interrupts. Initial firmware host
  2399. * commands use interrupts and avoids polling. Disable
  2400. * interrupts when done.
  2401. */
  2402. iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  2403. /* Get config data, mac addrs etc */
  2404. rc = mwl8k_cmd_get_hw_spec(hw);
  2405. if (rc) {
  2406. printk(KERN_ERR "%s: Cannot initialise firmware\n", priv->name);
  2407. goto err_stop_firmware;
  2408. }
  2409. /* Turn radio off */
  2410. rc = mwl8k_cmd_802_11_radio_disable(hw);
  2411. if (rc) {
  2412. printk(KERN_ERR "%s: Cannot disable\n", priv->name);
  2413. goto err_stop_firmware;
  2414. }
  2415. /* Disable interrupts */
  2416. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  2417. free_irq(priv->pdev->irq, hw);
  2418. rc = ieee80211_register_hw(hw);
  2419. if (rc) {
  2420. printk(KERN_ERR "%s: Cannot register device\n", priv->name);
  2421. goto err_stop_firmware;
  2422. }
  2423. printk(KERN_INFO "%s: 88w%u v%d, %pM, firmware version %u.%u.%u.%u\n",
  2424. wiphy_name(hw->wiphy), priv->part_num, priv->hw_rev,
  2425. hw->wiphy->perm_addr,
  2426. (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
  2427. (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
  2428. return 0;
  2429. err_stop_firmware:
  2430. mwl8k_hw_reset(priv);
  2431. mwl8k_release_firmware(priv);
  2432. err_free_irq:
  2433. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  2434. free_irq(priv->pdev->irq, hw);
  2435. err_free_queues:
  2436. for (i = 0; i < MWL8K_TX_QUEUES; i++)
  2437. mwl8k_txq_deinit(hw, i);
  2438. mwl8k_rxq_deinit(hw, 0);
  2439. err_iounmap:
  2440. if (priv->cookie != NULL)
  2441. pci_free_consistent(priv->pdev, 4,
  2442. priv->cookie, priv->cookie_dma);
  2443. if (priv->regs != NULL)
  2444. pci_iounmap(pdev, priv->regs);
  2445. if (priv->config_wq != NULL)
  2446. destroy_workqueue(priv->config_wq);
  2447. pci_set_drvdata(pdev, NULL);
  2448. ieee80211_free_hw(hw);
  2449. err_free_reg:
  2450. pci_release_regions(pdev);
  2451. pci_disable_device(pdev);
  2452. return rc;
  2453. }
  2454. static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
  2455. {
  2456. printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
  2457. }
  2458. static void __devexit mwl8k_remove(struct pci_dev *pdev)
  2459. {
  2460. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  2461. struct mwl8k_priv *priv;
  2462. int i;
  2463. if (hw == NULL)
  2464. return;
  2465. priv = hw->priv;
  2466. ieee80211_stop_queues(hw);
  2467. ieee80211_unregister_hw(hw);
  2468. /* Remove tx reclaim tasklet */
  2469. tasklet_kill(&priv->tx_reclaim_task);
  2470. /* Stop config thread */
  2471. destroy_workqueue(priv->config_wq);
  2472. /* Stop hardware */
  2473. mwl8k_hw_reset(priv);
  2474. /* Return all skbs to mac80211 */
  2475. for (i = 0; i < MWL8K_TX_QUEUES; i++)
  2476. mwl8k_txq_reclaim(hw, i, 1);
  2477. for (i = 0; i < MWL8K_TX_QUEUES; i++)
  2478. mwl8k_txq_deinit(hw, i);
  2479. mwl8k_rxq_deinit(hw, 0);
  2480. pci_free_consistent(priv->pdev, 4,
  2481. priv->cookie, priv->cookie_dma);
  2482. pci_iounmap(pdev, priv->regs);
  2483. pci_set_drvdata(pdev, NULL);
  2484. ieee80211_free_hw(hw);
  2485. pci_release_regions(pdev);
  2486. pci_disable_device(pdev);
  2487. }
  2488. static struct pci_driver mwl8k_driver = {
  2489. .name = MWL8K_NAME,
  2490. .id_table = mwl8k_table,
  2491. .probe = mwl8k_probe,
  2492. .remove = __devexit_p(mwl8k_remove),
  2493. .shutdown = __devexit_p(mwl8k_shutdown),
  2494. };
  2495. static int __init mwl8k_init(void)
  2496. {
  2497. return pci_register_driver(&mwl8k_driver);
  2498. }
  2499. static void __exit mwl8k_exit(void)
  2500. {
  2501. pci_unregister_driver(&mwl8k_driver);
  2502. }
  2503. module_init(mwl8k_init);
  2504. module_exit(mwl8k_exit);