mwl8k.c 74 KB

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