mwl8k.c 76 KB

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