mwl8k.c 77 KB

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