mwl8k.c 77 KB

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