mwl8k.c 74 KB

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