main.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743
  1. /*
  2. * Atheros AR9170 driver
  3. *
  4. * mac80211 interaction code
  5. *
  6. * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
  7. * Copyright 2009, Christian Lamparter <chunkeey@web.de>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; see the file COPYING. If not, see
  21. * http://www.gnu.org/licenses/.
  22. *
  23. * This file incorporates work covered by the following copyright and
  24. * permission notice:
  25. * Copyright (c) 2007-2008 Atheros Communications, Inc.
  26. *
  27. * Permission to use, copy, modify, and/or distribute this software for any
  28. * purpose with or without fee is hereby granted, provided that the above
  29. * copyright notice and this permission notice appear in all copies.
  30. *
  31. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  32. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  33. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  34. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  35. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  36. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  37. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  38. */
  39. #include <linux/init.h>
  40. #include <linux/module.h>
  41. #include <linux/etherdevice.h>
  42. #include <net/mac80211.h>
  43. #include "ar9170.h"
  44. #include "hw.h"
  45. #include "cmd.h"
  46. static int modparam_nohwcrypt;
  47. module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
  48. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  49. static int modparam_ht;
  50. module_param_named(ht, modparam_ht, bool, S_IRUGO);
  51. MODULE_PARM_DESC(ht, "enable MPDU aggregation.");
  52. #define RATE(_bitrate, _hw_rate, _txpidx, _flags) { \
  53. .bitrate = (_bitrate), \
  54. .flags = (_flags), \
  55. .hw_value = (_hw_rate) | (_txpidx) << 4, \
  56. }
  57. static struct ieee80211_rate __ar9170_ratetable[] = {
  58. RATE(10, 0, 0, 0),
  59. RATE(20, 1, 1, IEEE80211_RATE_SHORT_PREAMBLE),
  60. RATE(55, 2, 2, IEEE80211_RATE_SHORT_PREAMBLE),
  61. RATE(110, 3, 3, IEEE80211_RATE_SHORT_PREAMBLE),
  62. RATE(60, 0xb, 0, 0),
  63. RATE(90, 0xf, 0, 0),
  64. RATE(120, 0xa, 0, 0),
  65. RATE(180, 0xe, 0, 0),
  66. RATE(240, 0x9, 0, 0),
  67. RATE(360, 0xd, 1, 0),
  68. RATE(480, 0x8, 2, 0),
  69. RATE(540, 0xc, 3, 0),
  70. };
  71. #undef RATE
  72. #define ar9170_g_ratetable (__ar9170_ratetable + 0)
  73. #define ar9170_g_ratetable_size 12
  74. #define ar9170_a_ratetable (__ar9170_ratetable + 4)
  75. #define ar9170_a_ratetable_size 8
  76. /*
  77. * NB: The hw_value is used as an index into the ar9170_phy_freq_params
  78. * array in phy.c so that we don't have to do frequency lookups!
  79. */
  80. #define CHAN(_freq, _idx) { \
  81. .center_freq = (_freq), \
  82. .hw_value = (_idx), \
  83. .max_power = 18, /* XXX */ \
  84. }
  85. static struct ieee80211_channel ar9170_2ghz_chantable[] = {
  86. CHAN(2412, 0),
  87. CHAN(2417, 1),
  88. CHAN(2422, 2),
  89. CHAN(2427, 3),
  90. CHAN(2432, 4),
  91. CHAN(2437, 5),
  92. CHAN(2442, 6),
  93. CHAN(2447, 7),
  94. CHAN(2452, 8),
  95. CHAN(2457, 9),
  96. CHAN(2462, 10),
  97. CHAN(2467, 11),
  98. CHAN(2472, 12),
  99. CHAN(2484, 13),
  100. };
  101. static struct ieee80211_channel ar9170_5ghz_chantable[] = {
  102. CHAN(4920, 14),
  103. CHAN(4940, 15),
  104. CHAN(4960, 16),
  105. CHAN(4980, 17),
  106. CHAN(5040, 18),
  107. CHAN(5060, 19),
  108. CHAN(5080, 20),
  109. CHAN(5180, 21),
  110. CHAN(5200, 22),
  111. CHAN(5220, 23),
  112. CHAN(5240, 24),
  113. CHAN(5260, 25),
  114. CHAN(5280, 26),
  115. CHAN(5300, 27),
  116. CHAN(5320, 28),
  117. CHAN(5500, 29),
  118. CHAN(5520, 30),
  119. CHAN(5540, 31),
  120. CHAN(5560, 32),
  121. CHAN(5580, 33),
  122. CHAN(5600, 34),
  123. CHAN(5620, 35),
  124. CHAN(5640, 36),
  125. CHAN(5660, 37),
  126. CHAN(5680, 38),
  127. CHAN(5700, 39),
  128. CHAN(5745, 40),
  129. CHAN(5765, 41),
  130. CHAN(5785, 42),
  131. CHAN(5805, 43),
  132. CHAN(5825, 44),
  133. CHAN(5170, 45),
  134. CHAN(5190, 46),
  135. CHAN(5210, 47),
  136. CHAN(5230, 48),
  137. };
  138. #undef CHAN
  139. #define AR9170_HT_CAP \
  140. { \
  141. .ht_supported = true, \
  142. .cap = IEEE80211_HT_CAP_MAX_AMSDU | \
  143. IEEE80211_HT_CAP_SUP_WIDTH_20_40 | \
  144. IEEE80211_HT_CAP_SGI_40 | \
  145. IEEE80211_HT_CAP_GRN_FLD | \
  146. IEEE80211_HT_CAP_DSSSCCK40 | \
  147. IEEE80211_HT_CAP_SM_PS, \
  148. .ampdu_factor = 3, \
  149. .ampdu_density = 6, \
  150. .mcs = { \
  151. .rx_mask = { 0xff, 0xff, 0, 0, 0x1, 0, 0, 0, 0, 0, }, \
  152. .rx_highest = cpu_to_le16(300), \
  153. .tx_params = IEEE80211_HT_MCS_TX_DEFINED, \
  154. }, \
  155. }
  156. static struct ieee80211_supported_band ar9170_band_2GHz = {
  157. .channels = ar9170_2ghz_chantable,
  158. .n_channels = ARRAY_SIZE(ar9170_2ghz_chantable),
  159. .bitrates = ar9170_g_ratetable,
  160. .n_bitrates = ar9170_g_ratetable_size,
  161. .ht_cap = AR9170_HT_CAP,
  162. };
  163. static struct ieee80211_supported_band ar9170_band_5GHz = {
  164. .channels = ar9170_5ghz_chantable,
  165. .n_channels = ARRAY_SIZE(ar9170_5ghz_chantable),
  166. .bitrates = ar9170_a_ratetable,
  167. .n_bitrates = ar9170_a_ratetable_size,
  168. .ht_cap = AR9170_HT_CAP,
  169. };
  170. static void ar9170_tx(struct ar9170 *ar);
  171. static bool ar9170_tx_ampdu(struct ar9170 *ar);
  172. static inline u16 ar9170_get_seq_h(struct ieee80211_hdr *hdr)
  173. {
  174. return le16_to_cpu(hdr->seq_ctrl) >> 4;
  175. }
  176. static inline u16 ar9170_get_seq(struct sk_buff *skb)
  177. {
  178. struct ar9170_tx_control *txc = (void *) skb->data;
  179. return ar9170_get_seq_h((void *) txc->frame_data);
  180. }
  181. static inline u16 ar9170_get_tid(struct sk_buff *skb)
  182. {
  183. struct ar9170_tx_control *txc = (void *) skb->data;
  184. struct ieee80211_hdr *hdr = (void *) txc->frame_data;
  185. return (ieee80211_get_qos_ctl(hdr))[0] & IEEE80211_QOS_CTL_TID_MASK;
  186. }
  187. #define GET_NEXT_SEQ(seq) ((seq + 1) & 0x0fff)
  188. #define GET_NEXT_SEQ_FROM_SKB(skb) (GET_NEXT_SEQ(ar9170_get_seq(skb)))
  189. #if (defined AR9170_QUEUE_DEBUG) || (defined AR9170_TXAGG_DEBUG)
  190. static void ar9170_print_txheader(struct ar9170 *ar, struct sk_buff *skb)
  191. {
  192. struct ar9170_tx_control *txc = (void *) skb->data;
  193. struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
  194. struct ar9170_tx_info *arinfo = (void *) txinfo->rate_driver_data;
  195. struct ieee80211_hdr *hdr = (void *) txc->frame_data;
  196. printk(KERN_DEBUG "%s: => FRAME [skb:%p, q:%d, DA:[%pM] flags:%x s:%d "
  197. "mac_ctrl:%04x, phy_ctrl:%08x, timeout:[%d ms]]\n",
  198. wiphy_name(ar->hw->wiphy), skb, skb_get_queue_mapping(skb),
  199. ieee80211_get_DA(hdr), arinfo->flags, ar9170_get_seq_h(hdr),
  200. le16_to_cpu(txc->mac_control), le32_to_cpu(txc->phy_control),
  201. jiffies_to_msecs(arinfo->timeout - jiffies));
  202. }
  203. static void __ar9170_dump_txqueue(struct ar9170 *ar,
  204. struct sk_buff_head *queue)
  205. {
  206. struct sk_buff *skb;
  207. int i = 0;
  208. printk(KERN_DEBUG "---[ cut here ]---\n");
  209. printk(KERN_DEBUG "%s: %d entries in queue.\n",
  210. wiphy_name(ar->hw->wiphy), skb_queue_len(queue));
  211. skb_queue_walk(queue, skb) {
  212. printk(KERN_DEBUG "index:%d => \n", i++);
  213. ar9170_print_txheader(ar, skb);
  214. }
  215. if (i != skb_queue_len(queue))
  216. printk(KERN_DEBUG "WARNING: queue frame counter "
  217. "mismatch %d != %d\n", skb_queue_len(queue), i);
  218. printk(KERN_DEBUG "---[ end ]---\n");
  219. }
  220. #endif /* AR9170_QUEUE_DEBUG || AR9170_TXAGG_DEBUG */
  221. #ifdef AR9170_QUEUE_DEBUG
  222. static void ar9170_dump_txqueue(struct ar9170 *ar,
  223. struct sk_buff_head *queue)
  224. {
  225. unsigned long flags;
  226. spin_lock_irqsave(&queue->lock, flags);
  227. __ar9170_dump_txqueue(ar, queue);
  228. spin_unlock_irqrestore(&queue->lock, flags);
  229. }
  230. #endif /* AR9170_QUEUE_DEBUG */
  231. #ifdef AR9170_QUEUE_STOP_DEBUG
  232. static void __ar9170_dump_txstats(struct ar9170 *ar)
  233. {
  234. int i;
  235. printk(KERN_DEBUG "%s: QoS queue stats\n",
  236. wiphy_name(ar->hw->wiphy));
  237. for (i = 0; i < __AR9170_NUM_TXQ; i++)
  238. printk(KERN_DEBUG "%s: queue:%d limit:%d len:%d waitack:%d "
  239. " stopped:%d\n", wiphy_name(ar->hw->wiphy), i,
  240. ar->tx_stats[i].limit, ar->tx_stats[i].len,
  241. skb_queue_len(&ar->tx_status[i]),
  242. ieee80211_queue_stopped(ar->hw, i));
  243. }
  244. #endif /* AR9170_QUEUE_STOP_DEBUG */
  245. #ifdef AR9170_TXAGG_DEBUG
  246. static void ar9170_dump_tx_status_ampdu(struct ar9170 *ar)
  247. {
  248. unsigned long flags;
  249. spin_lock_irqsave(&ar->tx_status_ampdu.lock, flags);
  250. printk(KERN_DEBUG "%s: A-MPDU tx_status queue => \n",
  251. wiphy_name(ar->hw->wiphy));
  252. __ar9170_dump_txqueue(ar, &ar->tx_status_ampdu);
  253. spin_unlock_irqrestore(&ar->tx_status_ampdu.lock, flags);
  254. }
  255. #endif /* AR9170_TXAGG_DEBUG */
  256. /* caller must guarantee exclusive access for _bin_ queue. */
  257. static void ar9170_recycle_expired(struct ar9170 *ar,
  258. struct sk_buff_head *queue,
  259. struct sk_buff_head *bin)
  260. {
  261. struct sk_buff *skb, *old = NULL;
  262. unsigned long flags;
  263. spin_lock_irqsave(&queue->lock, flags);
  264. while ((skb = skb_peek(queue))) {
  265. struct ieee80211_tx_info *txinfo;
  266. struct ar9170_tx_info *arinfo;
  267. txinfo = IEEE80211_SKB_CB(skb);
  268. arinfo = (void *) txinfo->rate_driver_data;
  269. if (time_is_before_jiffies(arinfo->timeout)) {
  270. #ifdef AR9170_QUEUE_DEBUG
  271. printk(KERN_DEBUG "%s: [%ld > %ld] frame expired => "
  272. "recycle \n", wiphy_name(ar->hw->wiphy),
  273. jiffies, arinfo->timeout);
  274. ar9170_print_txheader(ar, skb);
  275. #endif /* AR9170_QUEUE_DEBUG */
  276. __skb_unlink(skb, queue);
  277. __skb_queue_tail(bin, skb);
  278. } else {
  279. break;
  280. }
  281. if (unlikely(old == skb)) {
  282. /* bail out - queue is shot. */
  283. WARN_ON(1);
  284. break;
  285. }
  286. old = skb;
  287. }
  288. spin_unlock_irqrestore(&queue->lock, flags);
  289. }
  290. static void ar9170_tx_status(struct ar9170 *ar, struct sk_buff *skb,
  291. u16 tx_status)
  292. {
  293. struct ieee80211_tx_info *txinfo;
  294. unsigned int retries = 0;
  295. txinfo = IEEE80211_SKB_CB(skb);
  296. ieee80211_tx_info_clear_status(txinfo);
  297. switch (tx_status) {
  298. case AR9170_TX_STATUS_RETRY:
  299. retries = 2;
  300. case AR9170_TX_STATUS_COMPLETE:
  301. txinfo->flags |= IEEE80211_TX_STAT_ACK;
  302. break;
  303. case AR9170_TX_STATUS_FAILED:
  304. retries = ar->hw->conf.long_frame_max_tx_count;
  305. break;
  306. default:
  307. printk(KERN_ERR "%s: invalid tx_status response (%x).\n",
  308. wiphy_name(ar->hw->wiphy), tx_status);
  309. break;
  310. }
  311. txinfo->status.rates[0].count = retries + 1;
  312. skb_pull(skb, sizeof(struct ar9170_tx_control));
  313. ieee80211_tx_status_irqsafe(ar->hw, skb);
  314. }
  315. static void ar9170_tx_fake_ampdu_status(struct ar9170 *ar)
  316. {
  317. struct sk_buff_head success;
  318. struct sk_buff *skb;
  319. unsigned int i;
  320. unsigned long queue_bitmap = 0;
  321. skb_queue_head_init(&success);
  322. while (skb_queue_len(&ar->tx_status_ampdu) > AR9170_NUM_TX_STATUS)
  323. __skb_queue_tail(&success, skb_dequeue(&ar->tx_status_ampdu));
  324. ar9170_recycle_expired(ar, &ar->tx_status_ampdu, &success);
  325. #ifdef AR9170_TXAGG_DEBUG
  326. printk(KERN_DEBUG "%s: collected %d A-MPDU frames.\n",
  327. wiphy_name(ar->hw->wiphy), skb_queue_len(&success));
  328. __ar9170_dump_txqueue(ar, &success);
  329. #endif /* AR9170_TXAGG_DEBUG */
  330. while ((skb = __skb_dequeue(&success))) {
  331. struct ieee80211_tx_info *txinfo;
  332. queue_bitmap |= BIT(skb_get_queue_mapping(skb));
  333. txinfo = IEEE80211_SKB_CB(skb);
  334. ieee80211_tx_info_clear_status(txinfo);
  335. txinfo->flags |= IEEE80211_TX_STAT_ACK;
  336. txinfo->status.rates[0].count = 1;
  337. skb_pull(skb, sizeof(struct ar9170_tx_control));
  338. ieee80211_tx_status_irqsafe(ar->hw, skb);
  339. }
  340. for_each_bit(i, &queue_bitmap, BITS_PER_BYTE) {
  341. #ifdef AR9170_QUEUE_STOP_DEBUG
  342. printk(KERN_DEBUG "%s: wake queue %d\n",
  343. wiphy_name(ar->hw->wiphy), i);
  344. __ar9170_dump_txstats(ar);
  345. #endif /* AR9170_QUEUE_STOP_DEBUG */
  346. ieee80211_wake_queue(ar->hw, i);
  347. }
  348. if (queue_bitmap)
  349. ar9170_tx(ar);
  350. }
  351. static void ar9170_tx_ampdu_callback(struct ar9170 *ar, struct sk_buff *skb)
  352. {
  353. struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
  354. struct ar9170_tx_info *arinfo = (void *) txinfo->rate_driver_data;
  355. arinfo->timeout = jiffies +
  356. msecs_to_jiffies(AR9170_BA_TIMEOUT);
  357. skb_queue_tail(&ar->tx_status_ampdu, skb);
  358. ar9170_tx_fake_ampdu_status(ar);
  359. ar->tx_ampdu_pending--;
  360. if (!list_empty(&ar->tx_ampdu_list) && !ar->tx_ampdu_pending)
  361. ar9170_tx_ampdu(ar);
  362. }
  363. void ar9170_tx_callback(struct ar9170 *ar, struct sk_buff *skb)
  364. {
  365. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  366. struct ar9170_tx_info *arinfo = (void *) info->rate_driver_data;
  367. unsigned int queue = skb_get_queue_mapping(skb);
  368. unsigned long flags;
  369. spin_lock_irqsave(&ar->tx_stats_lock, flags);
  370. ar->tx_stats[queue].len--;
  371. if (skb_queue_empty(&ar->tx_pending[queue])) {
  372. #ifdef AR9170_QUEUE_STOP_DEBUG
  373. printk(KERN_DEBUG "%s: wake queue %d\n",
  374. wiphy_name(ar->hw->wiphy), queue);
  375. __ar9170_dump_txstats(ar);
  376. #endif /* AR9170_QUEUE_STOP_DEBUG */
  377. ieee80211_wake_queue(ar->hw, queue);
  378. }
  379. spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
  380. if (arinfo->flags & AR9170_TX_FLAG_BLOCK_ACK) {
  381. ar9170_tx_ampdu_callback(ar, skb);
  382. } else if (arinfo->flags & AR9170_TX_FLAG_WAIT_FOR_ACK) {
  383. arinfo->timeout = jiffies +
  384. msecs_to_jiffies(AR9170_TX_TIMEOUT);
  385. skb_queue_tail(&ar->tx_status[queue], skb);
  386. } else if (arinfo->flags & AR9170_TX_FLAG_NO_ACK) {
  387. ar9170_tx_status(ar, skb, AR9170_TX_STATUS_FAILED);
  388. } else {
  389. #ifdef AR9170_QUEUE_DEBUG
  390. printk(KERN_DEBUG "%s: unsupported frame flags!\n",
  391. wiphy_name(ar->hw->wiphy));
  392. ar9170_print_txheader(ar, skb);
  393. #endif /* AR9170_QUEUE_DEBUG */
  394. dev_kfree_skb_any(skb);
  395. }
  396. if (!ar->tx_stats[queue].len &&
  397. !skb_queue_empty(&ar->tx_pending[queue])) {
  398. ar9170_tx(ar);
  399. }
  400. }
  401. static struct sk_buff *ar9170_get_queued_skb(struct ar9170 *ar,
  402. const u8 *mac,
  403. struct sk_buff_head *queue,
  404. const u32 rate)
  405. {
  406. unsigned long flags;
  407. struct sk_buff *skb;
  408. /*
  409. * Unfortunately, the firmware does not tell to which (queued) frame
  410. * this transmission status report belongs to.
  411. *
  412. * So we have to make risky guesses - with the scarce information
  413. * the firmware provided (-> destination MAC, and phy_control) -
  414. * and hope that we picked the right one...
  415. */
  416. spin_lock_irqsave(&queue->lock, flags);
  417. skb_queue_walk(queue, skb) {
  418. struct ar9170_tx_control *txc = (void *) skb->data;
  419. struct ieee80211_hdr *hdr = (void *) txc->frame_data;
  420. u32 r;
  421. if (mac && compare_ether_addr(ieee80211_get_DA(hdr), mac)) {
  422. #ifdef AR9170_QUEUE_DEBUG
  423. printk(KERN_DEBUG "%s: skip frame => DA %pM != %pM\n",
  424. wiphy_name(ar->hw->wiphy), mac,
  425. ieee80211_get_DA(hdr));
  426. ar9170_print_txheader(ar, skb);
  427. #endif /* AR9170_QUEUE_DEBUG */
  428. continue;
  429. }
  430. r = (le32_to_cpu(txc->phy_control) & AR9170_TX_PHY_MCS_MASK) >>
  431. AR9170_TX_PHY_MCS_SHIFT;
  432. if ((rate != AR9170_TX_INVALID_RATE) && (r != rate)) {
  433. #ifdef AR9170_QUEUE_DEBUG
  434. printk(KERN_DEBUG "%s: skip frame => rate %d != %d\n",
  435. wiphy_name(ar->hw->wiphy), rate, r);
  436. ar9170_print_txheader(ar, skb);
  437. #endif /* AR9170_QUEUE_DEBUG */
  438. continue;
  439. }
  440. __skb_unlink(skb, queue);
  441. spin_unlock_irqrestore(&queue->lock, flags);
  442. return skb;
  443. }
  444. #ifdef AR9170_QUEUE_DEBUG
  445. printk(KERN_ERR "%s: ESS:[%pM] does not have any "
  446. "outstanding frames in queue.\n",
  447. wiphy_name(ar->hw->wiphy), mac);
  448. __ar9170_dump_txqueue(ar, queue);
  449. #endif /* AR9170_QUEUE_DEBUG */
  450. spin_unlock_irqrestore(&queue->lock, flags);
  451. return NULL;
  452. }
  453. static void ar9170_handle_block_ack(struct ar9170 *ar, u16 count, u16 r)
  454. {
  455. struct sk_buff *skb;
  456. struct ieee80211_tx_info *txinfo;
  457. while (count) {
  458. skb = ar9170_get_queued_skb(ar, NULL, &ar->tx_status_ampdu, r);
  459. if (!skb)
  460. break;
  461. txinfo = IEEE80211_SKB_CB(skb);
  462. ieee80211_tx_info_clear_status(txinfo);
  463. /* FIXME: maybe more ? */
  464. txinfo->status.rates[0].count = 1;
  465. skb_pull(skb, sizeof(struct ar9170_tx_control));
  466. ieee80211_tx_status_irqsafe(ar->hw, skb);
  467. count--;
  468. }
  469. #ifdef AR9170_TXAGG_DEBUG
  470. if (count) {
  471. printk(KERN_DEBUG "%s: got %d more failed mpdus, but no more "
  472. "suitable frames left in tx_status queue.\n",
  473. wiphy_name(ar->hw->wiphy), count);
  474. ar9170_dump_tx_status_ampdu(ar);
  475. }
  476. #endif /* AR9170_TXAGG_DEBUG */
  477. }
  478. /*
  479. * This worker tries to keeps an maintain tx_status queues.
  480. * So we can guarantee that incoming tx_status reports are
  481. * actually for a pending frame.
  482. */
  483. static void ar9170_tx_janitor(struct work_struct *work)
  484. {
  485. struct ar9170 *ar = container_of(work, struct ar9170,
  486. tx_janitor.work);
  487. struct sk_buff_head waste;
  488. unsigned int i;
  489. bool resched = false;
  490. if (unlikely(!IS_STARTED(ar)))
  491. return ;
  492. skb_queue_head_init(&waste);
  493. for (i = 0; i < __AR9170_NUM_TXQ; i++) {
  494. #ifdef AR9170_QUEUE_DEBUG
  495. printk(KERN_DEBUG "%s: garbage collector scans queue:%d\n",
  496. wiphy_name(ar->hw->wiphy), i);
  497. ar9170_dump_txqueue(ar, &ar->tx_pending[i]);
  498. ar9170_dump_txqueue(ar, &ar->tx_status[i]);
  499. #endif /* AR9170_QUEUE_DEBUG */
  500. ar9170_recycle_expired(ar, &ar->tx_status[i], &waste);
  501. ar9170_recycle_expired(ar, &ar->tx_pending[i], &waste);
  502. skb_queue_purge(&waste);
  503. if (!skb_queue_empty(&ar->tx_status[i]) ||
  504. !skb_queue_empty(&ar->tx_pending[i]))
  505. resched = true;
  506. }
  507. ar9170_tx_fake_ampdu_status(ar);
  508. if (!resched)
  509. return;
  510. ieee80211_queue_delayed_work(ar->hw,
  511. &ar->tx_janitor,
  512. msecs_to_jiffies(AR9170_JANITOR_DELAY));
  513. }
  514. void ar9170_handle_command_response(struct ar9170 *ar, void *buf, u32 len)
  515. {
  516. struct ar9170_cmd_response *cmd = (void *) buf;
  517. if ((cmd->type & 0xc0) != 0xc0) {
  518. ar->callback_cmd(ar, len, buf);
  519. return;
  520. }
  521. /* hardware event handlers */
  522. switch (cmd->type) {
  523. case 0xc1: {
  524. /*
  525. * TX status notification:
  526. * bytes: 0c c1 XX YY M1 M2 M3 M4 M5 M6 R4 R3 R2 R1 S2 S1
  527. *
  528. * XX always 81
  529. * YY always 00
  530. * M1-M6 is the MAC address
  531. * R1-R4 is the transmit rate
  532. * S1-S2 is the transmit status
  533. */
  534. struct sk_buff *skb;
  535. u32 phy = le32_to_cpu(cmd->tx_status.rate);
  536. u32 q = (phy & AR9170_TX_PHY_QOS_MASK) >>
  537. AR9170_TX_PHY_QOS_SHIFT;
  538. #ifdef AR9170_QUEUE_DEBUG
  539. printk(KERN_DEBUG "%s: recv tx_status for %pM, p:%08x, q:%d\n",
  540. wiphy_name(ar->hw->wiphy), cmd->tx_status.dst, phy, q);
  541. #endif /* AR9170_QUEUE_DEBUG */
  542. skb = ar9170_get_queued_skb(ar, cmd->tx_status.dst,
  543. &ar->tx_status[q],
  544. AR9170_TX_INVALID_RATE);
  545. if (unlikely(!skb))
  546. return ;
  547. ar9170_tx_status(ar, skb, le16_to_cpu(cmd->tx_status.status));
  548. break;
  549. }
  550. case 0xc0:
  551. /*
  552. * pre-TBTT event
  553. */
  554. if (ar->vif && ar->vif->type == NL80211_IFTYPE_AP)
  555. ieee80211_queue_work(ar->hw, &ar->beacon_work);
  556. break;
  557. case 0xc2:
  558. /*
  559. * (IBSS) beacon send notification
  560. * bytes: 04 c2 XX YY B4 B3 B2 B1
  561. *
  562. * XX always 80
  563. * YY always 00
  564. * B1-B4 "should" be the number of send out beacons.
  565. */
  566. break;
  567. case 0xc3:
  568. /* End of Atim Window */
  569. break;
  570. case 0xc4:
  571. /* BlockACK bitmap */
  572. break;
  573. case 0xc5:
  574. /* BlockACK events */
  575. ar9170_handle_block_ack(ar,
  576. le16_to_cpu(cmd->ba_fail_cnt.failed),
  577. le16_to_cpu(cmd->ba_fail_cnt.rate));
  578. ar9170_tx_fake_ampdu_status(ar);
  579. break;
  580. case 0xc6:
  581. /* Watchdog Interrupt */
  582. break;
  583. case 0xc9:
  584. /* retransmission issue / SIFS/EIFS collision ?! */
  585. break;
  586. /* firmware debug */
  587. case 0xca:
  588. printk(KERN_DEBUG "ar9170 FW: %.*s\n", len - 4, (char *)buf + 4);
  589. break;
  590. case 0xcb:
  591. len -= 4;
  592. switch (len) {
  593. case 1:
  594. printk(KERN_DEBUG "ar9170 FW: u8: %#.2x\n",
  595. *((char *)buf + 4));
  596. break;
  597. case 2:
  598. printk(KERN_DEBUG "ar9170 FW: u8: %#.4x\n",
  599. le16_to_cpup((__le16 *)((char *)buf + 4)));
  600. break;
  601. case 4:
  602. printk(KERN_DEBUG "ar9170 FW: u8: %#.8x\n",
  603. le32_to_cpup((__le32 *)((char *)buf + 4)));
  604. break;
  605. case 8:
  606. printk(KERN_DEBUG "ar9170 FW: u8: %#.16lx\n",
  607. (unsigned long)le64_to_cpup(
  608. (__le64 *)((char *)buf + 4)));
  609. break;
  610. }
  611. break;
  612. case 0xcc:
  613. print_hex_dump_bytes("ar9170 FW:", DUMP_PREFIX_NONE,
  614. (char *)buf + 4, len - 4);
  615. break;
  616. default:
  617. printk(KERN_INFO "received unhandled event %x\n", cmd->type);
  618. print_hex_dump_bytes("dump:", DUMP_PREFIX_NONE, buf, len);
  619. break;
  620. }
  621. }
  622. static void ar9170_rx_reset_rx_mpdu(struct ar9170 *ar)
  623. {
  624. memset(&ar->rx_mpdu.plcp, 0, sizeof(struct ar9170_rx_head));
  625. ar->rx_mpdu.has_plcp = false;
  626. }
  627. int ar9170_nag_limiter(struct ar9170 *ar)
  628. {
  629. bool print_message;
  630. /*
  631. * we expect all sorts of errors in promiscuous mode.
  632. * don't bother with it, it's OK!
  633. */
  634. if (ar->sniffer_enabled)
  635. return false;
  636. /*
  637. * only go for frequent errors! The hardware tends to
  638. * do some stupid thing once in a while under load, in
  639. * noisy environments or just for fun!
  640. */
  641. if (time_before(jiffies, ar->bad_hw_nagger) && net_ratelimit())
  642. print_message = true;
  643. else
  644. print_message = false;
  645. /* reset threshold for "once in a while" */
  646. ar->bad_hw_nagger = jiffies + HZ / 4;
  647. return print_message;
  648. }
  649. static int ar9170_rx_mac_status(struct ar9170 *ar,
  650. struct ar9170_rx_head *head,
  651. struct ar9170_rx_macstatus *mac,
  652. struct ieee80211_rx_status *status)
  653. {
  654. u8 error, decrypt;
  655. BUILD_BUG_ON(sizeof(struct ar9170_rx_head) != 12);
  656. BUILD_BUG_ON(sizeof(struct ar9170_rx_macstatus) != 4);
  657. error = mac->error;
  658. if (error & AR9170_RX_ERROR_MMIC) {
  659. status->flag |= RX_FLAG_MMIC_ERROR;
  660. error &= ~AR9170_RX_ERROR_MMIC;
  661. }
  662. if (error & AR9170_RX_ERROR_PLCP) {
  663. status->flag |= RX_FLAG_FAILED_PLCP_CRC;
  664. error &= ~AR9170_RX_ERROR_PLCP;
  665. if (!(ar->filter_state & FIF_PLCPFAIL))
  666. return -EINVAL;
  667. }
  668. if (error & AR9170_RX_ERROR_FCS) {
  669. status->flag |= RX_FLAG_FAILED_FCS_CRC;
  670. error &= ~AR9170_RX_ERROR_FCS;
  671. if (!(ar->filter_state & FIF_FCSFAIL))
  672. return -EINVAL;
  673. }
  674. decrypt = ar9170_get_decrypt_type(mac);
  675. if (!(decrypt & AR9170_RX_ENC_SOFTWARE) &&
  676. decrypt != AR9170_ENC_ALG_NONE)
  677. status->flag |= RX_FLAG_DECRYPTED;
  678. /* ignore wrong RA errors */
  679. error &= ~AR9170_RX_ERROR_WRONG_RA;
  680. if (error & AR9170_RX_ERROR_DECRYPT) {
  681. error &= ~AR9170_RX_ERROR_DECRYPT;
  682. /*
  683. * Rx decryption is done in place,
  684. * the original data is lost anyway.
  685. */
  686. return -EINVAL;
  687. }
  688. /* drop any other error frames */
  689. if (unlikely(error)) {
  690. /* TODO: update netdevice's RX dropped/errors statistics */
  691. if (ar9170_nag_limiter(ar))
  692. printk(KERN_DEBUG "%s: received frame with "
  693. "suspicious error code (%#x).\n",
  694. wiphy_name(ar->hw->wiphy), error);
  695. return -EINVAL;
  696. }
  697. status->band = ar->channel->band;
  698. status->freq = ar->channel->center_freq;
  699. switch (mac->status & AR9170_RX_STATUS_MODULATION_MASK) {
  700. case AR9170_RX_STATUS_MODULATION_CCK:
  701. if (mac->status & AR9170_RX_STATUS_SHORT_PREAMBLE)
  702. status->flag |= RX_FLAG_SHORTPRE;
  703. switch (head->plcp[0]) {
  704. case 0x0a:
  705. status->rate_idx = 0;
  706. break;
  707. case 0x14:
  708. status->rate_idx = 1;
  709. break;
  710. case 0x37:
  711. status->rate_idx = 2;
  712. break;
  713. case 0x6e:
  714. status->rate_idx = 3;
  715. break;
  716. default:
  717. if (ar9170_nag_limiter(ar))
  718. printk(KERN_ERR "%s: invalid plcp cck rate "
  719. "(%x).\n", wiphy_name(ar->hw->wiphy),
  720. head->plcp[0]);
  721. return -EINVAL;
  722. }
  723. break;
  724. case AR9170_RX_STATUS_MODULATION_OFDM:
  725. switch (head->plcp[0] & 0xf) {
  726. case 0xb:
  727. status->rate_idx = 0;
  728. break;
  729. case 0xf:
  730. status->rate_idx = 1;
  731. break;
  732. case 0xa:
  733. status->rate_idx = 2;
  734. break;
  735. case 0xe:
  736. status->rate_idx = 3;
  737. break;
  738. case 0x9:
  739. status->rate_idx = 4;
  740. break;
  741. case 0xd:
  742. status->rate_idx = 5;
  743. break;
  744. case 0x8:
  745. status->rate_idx = 6;
  746. break;
  747. case 0xc:
  748. status->rate_idx = 7;
  749. break;
  750. default:
  751. if (ar9170_nag_limiter(ar))
  752. printk(KERN_ERR "%s: invalid plcp ofdm rate "
  753. "(%x).\n", wiphy_name(ar->hw->wiphy),
  754. head->plcp[0]);
  755. return -EINVAL;
  756. }
  757. if (status->band == IEEE80211_BAND_2GHZ)
  758. status->rate_idx += 4;
  759. break;
  760. case AR9170_RX_STATUS_MODULATION_HT:
  761. if (head->plcp[3] & 0x80)
  762. status->flag |= RX_FLAG_40MHZ;
  763. if (head->plcp[6] & 0x80)
  764. status->flag |= RX_FLAG_SHORT_GI;
  765. status->rate_idx = clamp(0, 75, head->plcp[6] & 0x7f);
  766. status->flag |= RX_FLAG_HT;
  767. break;
  768. case AR9170_RX_STATUS_MODULATION_DUPOFDM:
  769. /* XXX */
  770. if (ar9170_nag_limiter(ar))
  771. printk(KERN_ERR "%s: invalid modulation\n",
  772. wiphy_name(ar->hw->wiphy));
  773. return -EINVAL;
  774. }
  775. return 0;
  776. }
  777. static void ar9170_rx_phy_status(struct ar9170 *ar,
  778. struct ar9170_rx_phystatus *phy,
  779. struct ieee80211_rx_status *status)
  780. {
  781. int i;
  782. BUILD_BUG_ON(sizeof(struct ar9170_rx_phystatus) != 20);
  783. for (i = 0; i < 3; i++)
  784. if (phy->rssi[i] != 0x80)
  785. status->antenna |= BIT(i);
  786. /* post-process RSSI */
  787. for (i = 0; i < 7; i++)
  788. if (phy->rssi[i] & 0x80)
  789. phy->rssi[i] = ((phy->rssi[i] & 0x7f) + 1) & 0x7f;
  790. /* TODO: we could do something with phy_errors */
  791. status->signal = ar->noise[0] + phy->rssi_combined;
  792. status->noise = ar->noise[0];
  793. }
  794. static struct sk_buff *ar9170_rx_copy_data(u8 *buf, int len)
  795. {
  796. struct sk_buff *skb;
  797. int reserved = 0;
  798. struct ieee80211_hdr *hdr = (void *) buf;
  799. if (ieee80211_is_data_qos(hdr->frame_control)) {
  800. u8 *qc = ieee80211_get_qos_ctl(hdr);
  801. reserved += NET_IP_ALIGN;
  802. if (*qc & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
  803. reserved += NET_IP_ALIGN;
  804. }
  805. if (ieee80211_has_a4(hdr->frame_control))
  806. reserved += NET_IP_ALIGN;
  807. reserved = 32 + (reserved & NET_IP_ALIGN);
  808. skb = dev_alloc_skb(len + reserved);
  809. if (likely(skb)) {
  810. skb_reserve(skb, reserved);
  811. memcpy(skb_put(skb, len), buf, len);
  812. }
  813. return skb;
  814. }
  815. /*
  816. * If the frame alignment is right (or the kernel has
  817. * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS), and there
  818. * is only a single MPDU in the USB frame, then we could
  819. * submit to mac80211 the SKB directly. However, since
  820. * there may be multiple packets in one SKB in stream
  821. * mode, and we need to observe the proper ordering,
  822. * this is non-trivial.
  823. */
  824. static void ar9170_handle_mpdu(struct ar9170 *ar, u8 *buf, int len)
  825. {
  826. struct ar9170_rx_head *head;
  827. struct ar9170_rx_macstatus *mac;
  828. struct ar9170_rx_phystatus *phy = NULL;
  829. struct ieee80211_rx_status status;
  830. struct sk_buff *skb;
  831. int mpdu_len;
  832. if (unlikely(!IS_STARTED(ar) || len < (sizeof(*mac))))
  833. return ;
  834. /* Received MPDU */
  835. mpdu_len = len - sizeof(*mac);
  836. mac = (void *)(buf + mpdu_len);
  837. if (unlikely(mac->error & AR9170_RX_ERROR_FATAL)) {
  838. /* this frame is too damaged and can't be used - drop it */
  839. return ;
  840. }
  841. switch (mac->status & AR9170_RX_STATUS_MPDU_MASK) {
  842. case AR9170_RX_STATUS_MPDU_FIRST:
  843. /* first mpdu packet has the plcp header */
  844. if (likely(mpdu_len >= sizeof(struct ar9170_rx_head))) {
  845. head = (void *) buf;
  846. memcpy(&ar->rx_mpdu.plcp, (void *) buf,
  847. sizeof(struct ar9170_rx_head));
  848. mpdu_len -= sizeof(struct ar9170_rx_head);
  849. buf += sizeof(struct ar9170_rx_head);
  850. ar->rx_mpdu.has_plcp = true;
  851. } else {
  852. if (ar9170_nag_limiter(ar))
  853. printk(KERN_ERR "%s: plcp info is clipped.\n",
  854. wiphy_name(ar->hw->wiphy));
  855. return ;
  856. }
  857. break;
  858. case AR9170_RX_STATUS_MPDU_LAST:
  859. /* last mpdu has a extra tail with phy status information */
  860. if (likely(mpdu_len >= sizeof(struct ar9170_rx_phystatus))) {
  861. mpdu_len -= sizeof(struct ar9170_rx_phystatus);
  862. phy = (void *)(buf + mpdu_len);
  863. } else {
  864. if (ar9170_nag_limiter(ar))
  865. printk(KERN_ERR "%s: frame tail is clipped.\n",
  866. wiphy_name(ar->hw->wiphy));
  867. return ;
  868. }
  869. case AR9170_RX_STATUS_MPDU_MIDDLE:
  870. /* middle mpdus are just data */
  871. if (unlikely(!ar->rx_mpdu.has_plcp)) {
  872. if (!ar9170_nag_limiter(ar))
  873. return ;
  874. printk(KERN_ERR "%s: rx stream did not start "
  875. "with a first_mpdu frame tag.\n",
  876. wiphy_name(ar->hw->wiphy));
  877. return ;
  878. }
  879. head = &ar->rx_mpdu.plcp;
  880. break;
  881. case AR9170_RX_STATUS_MPDU_SINGLE:
  882. /* single mpdu - has plcp (head) and phy status (tail) */
  883. head = (void *) buf;
  884. mpdu_len -= sizeof(struct ar9170_rx_head);
  885. mpdu_len -= sizeof(struct ar9170_rx_phystatus);
  886. buf += sizeof(struct ar9170_rx_head);
  887. phy = (void *)(buf + mpdu_len);
  888. break;
  889. default:
  890. BUG_ON(1);
  891. break;
  892. }
  893. if (unlikely(mpdu_len < FCS_LEN))
  894. return ;
  895. memset(&status, 0, sizeof(status));
  896. if (unlikely(ar9170_rx_mac_status(ar, head, mac, &status)))
  897. return ;
  898. if (phy)
  899. ar9170_rx_phy_status(ar, phy, &status);
  900. skb = ar9170_rx_copy_data(buf, mpdu_len);
  901. if (likely(skb)) {
  902. memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
  903. ieee80211_rx_irqsafe(ar->hw, skb);
  904. }
  905. }
  906. void ar9170_rx(struct ar9170 *ar, struct sk_buff *skb)
  907. {
  908. unsigned int i, tlen, resplen, wlen = 0, clen = 0;
  909. u8 *tbuf, *respbuf;
  910. tbuf = skb->data;
  911. tlen = skb->len;
  912. while (tlen >= 4) {
  913. clen = tbuf[1] << 8 | tbuf[0];
  914. wlen = ALIGN(clen, 4);
  915. /* check if this is stream has a valid tag.*/
  916. if (tbuf[2] != 0 || tbuf[3] != 0x4e) {
  917. /*
  918. * TODO: handle the highly unlikely event that the
  919. * corrupted stream has the TAG at the right position.
  920. */
  921. /* check if the frame can be repaired. */
  922. if (!ar->rx_failover_missing) {
  923. /* this is no "short read". */
  924. if (ar9170_nag_limiter(ar)) {
  925. printk(KERN_ERR "%s: missing tag!\n",
  926. wiphy_name(ar->hw->wiphy));
  927. goto err_telluser;
  928. } else
  929. goto err_silent;
  930. }
  931. if (ar->rx_failover_missing > tlen) {
  932. if (ar9170_nag_limiter(ar)) {
  933. printk(KERN_ERR "%s: possible multi "
  934. "stream corruption!\n",
  935. wiphy_name(ar->hw->wiphy));
  936. goto err_telluser;
  937. } else
  938. goto err_silent;
  939. }
  940. memcpy(skb_put(ar->rx_failover, tlen), tbuf, tlen);
  941. ar->rx_failover_missing -= tlen;
  942. if (ar->rx_failover_missing <= 0) {
  943. /*
  944. * nested ar9170_rx call!
  945. * termination is guranteed, even when the
  946. * combined frame also have a element with
  947. * a bad tag.
  948. */
  949. ar->rx_failover_missing = 0;
  950. ar9170_rx(ar, ar->rx_failover);
  951. skb_reset_tail_pointer(ar->rx_failover);
  952. skb_trim(ar->rx_failover, 0);
  953. }
  954. return ;
  955. }
  956. /* check if stream is clipped */
  957. if (wlen > tlen - 4) {
  958. if (ar->rx_failover_missing) {
  959. /* TODO: handle double stream corruption. */
  960. if (ar9170_nag_limiter(ar)) {
  961. printk(KERN_ERR "%s: double rx stream "
  962. "corruption!\n",
  963. wiphy_name(ar->hw->wiphy));
  964. goto err_telluser;
  965. } else
  966. goto err_silent;
  967. }
  968. /*
  969. * save incomplete data set.
  970. * the firmware will resend the missing bits when
  971. * the rx - descriptor comes round again.
  972. */
  973. memcpy(skb_put(ar->rx_failover, tlen), tbuf, tlen);
  974. ar->rx_failover_missing = clen - tlen;
  975. return ;
  976. }
  977. resplen = clen;
  978. respbuf = tbuf + 4;
  979. tbuf += wlen + 4;
  980. tlen -= wlen + 4;
  981. i = 0;
  982. /* weird thing, but this is the same in the original driver */
  983. while (resplen > 2 && i < 12 &&
  984. respbuf[0] == 0xff && respbuf[1] == 0xff) {
  985. i += 2;
  986. resplen -= 2;
  987. respbuf += 2;
  988. }
  989. if (resplen < 4)
  990. continue;
  991. /* found the 6 * 0xffff marker? */
  992. if (i == 12)
  993. ar9170_handle_command_response(ar, respbuf, resplen);
  994. else
  995. ar9170_handle_mpdu(ar, respbuf, clen);
  996. }
  997. if (tlen) {
  998. if (net_ratelimit())
  999. printk(KERN_ERR "%s: %d bytes of unprocessed "
  1000. "data left in rx stream!\n",
  1001. wiphy_name(ar->hw->wiphy), tlen);
  1002. goto err_telluser;
  1003. }
  1004. return ;
  1005. err_telluser:
  1006. printk(KERN_ERR "%s: damaged RX stream data [want:%d, "
  1007. "data:%d, rx:%d, pending:%d ]\n",
  1008. wiphy_name(ar->hw->wiphy), clen, wlen, tlen,
  1009. ar->rx_failover_missing);
  1010. if (ar->rx_failover_missing)
  1011. print_hex_dump_bytes("rxbuf:", DUMP_PREFIX_OFFSET,
  1012. ar->rx_failover->data,
  1013. ar->rx_failover->len);
  1014. print_hex_dump_bytes("stream:", DUMP_PREFIX_OFFSET,
  1015. skb->data, skb->len);
  1016. printk(KERN_ERR "%s: please check your hardware and cables, if "
  1017. "you see this message frequently.\n",
  1018. wiphy_name(ar->hw->wiphy));
  1019. err_silent:
  1020. if (ar->rx_failover_missing) {
  1021. skb_reset_tail_pointer(ar->rx_failover);
  1022. skb_trim(ar->rx_failover, 0);
  1023. ar->rx_failover_missing = 0;
  1024. }
  1025. }
  1026. #define AR9170_FILL_QUEUE(queue, ai_fs, cwmin, cwmax, _txop) \
  1027. do { \
  1028. queue.aifs = ai_fs; \
  1029. queue.cw_min = cwmin; \
  1030. queue.cw_max = cwmax; \
  1031. queue.txop = _txop; \
  1032. } while (0)
  1033. static int ar9170_op_start(struct ieee80211_hw *hw)
  1034. {
  1035. struct ar9170 *ar = hw->priv;
  1036. int err, i;
  1037. mutex_lock(&ar->mutex);
  1038. /* reinitialize queues statistics */
  1039. memset(&ar->tx_stats, 0, sizeof(ar->tx_stats));
  1040. for (i = 0; i < __AR9170_NUM_TXQ; i++)
  1041. ar->tx_stats[i].limit = AR9170_TXQ_DEPTH;
  1042. /* reset QoS defaults */
  1043. AR9170_FILL_QUEUE(ar->edcf[0], 3, 15, 1023, 0); /* BEST EFFORT*/
  1044. AR9170_FILL_QUEUE(ar->edcf[1], 7, 15, 1023, 0); /* BACKGROUND */
  1045. AR9170_FILL_QUEUE(ar->edcf[2], 2, 7, 15, 94); /* VIDEO */
  1046. AR9170_FILL_QUEUE(ar->edcf[3], 2, 3, 7, 47); /* VOICE */
  1047. AR9170_FILL_QUEUE(ar->edcf[4], 2, 3, 7, 0); /* SPECIAL */
  1048. /* set sane AMPDU defaults */
  1049. ar->global_ampdu_density = 6;
  1050. ar->global_ampdu_factor = 3;
  1051. ar->bad_hw_nagger = jiffies;
  1052. err = ar->open(ar);
  1053. if (err)
  1054. goto out;
  1055. err = ar9170_init_mac(ar);
  1056. if (err)
  1057. goto out;
  1058. err = ar9170_set_qos(ar);
  1059. if (err)
  1060. goto out;
  1061. err = ar9170_init_phy(ar, IEEE80211_BAND_2GHZ);
  1062. if (err)
  1063. goto out;
  1064. err = ar9170_init_rf(ar);
  1065. if (err)
  1066. goto out;
  1067. /* start DMA */
  1068. err = ar9170_write_reg(ar, 0x1c3d30, 0x100);
  1069. if (err)
  1070. goto out;
  1071. ar->state = AR9170_STARTED;
  1072. out:
  1073. mutex_unlock(&ar->mutex);
  1074. return err;
  1075. }
  1076. static void ar9170_op_stop(struct ieee80211_hw *hw)
  1077. {
  1078. struct ar9170 *ar = hw->priv;
  1079. unsigned int i;
  1080. if (IS_STARTED(ar))
  1081. ar->state = AR9170_IDLE;
  1082. cancel_delayed_work_sync(&ar->tx_janitor);
  1083. #ifdef CONFIG_AR9170_LEDS
  1084. cancel_delayed_work_sync(&ar->led_work);
  1085. #endif
  1086. cancel_work_sync(&ar->beacon_work);
  1087. mutex_lock(&ar->mutex);
  1088. if (IS_ACCEPTING_CMD(ar)) {
  1089. ar9170_set_leds_state(ar, 0);
  1090. /* stop DMA */
  1091. ar9170_write_reg(ar, 0x1c3d30, 0);
  1092. ar->stop(ar);
  1093. }
  1094. for (i = 0; i < __AR9170_NUM_TXQ; i++) {
  1095. skb_queue_purge(&ar->tx_pending[i]);
  1096. skb_queue_purge(&ar->tx_status[i]);
  1097. }
  1098. skb_queue_purge(&ar->tx_status_ampdu);
  1099. mutex_unlock(&ar->mutex);
  1100. }
  1101. static void ar9170_tx_indicate_immba(struct ar9170 *ar, struct sk_buff *skb)
  1102. {
  1103. struct ar9170_tx_control *txc = (void *) skb->data;
  1104. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_IMM_AMPDU);
  1105. }
  1106. static void ar9170_tx_copy_phy(struct ar9170 *ar, struct sk_buff *dst,
  1107. struct sk_buff *src)
  1108. {
  1109. struct ar9170_tx_control *dst_txc, *src_txc;
  1110. struct ieee80211_tx_info *dst_info, *src_info;
  1111. struct ar9170_tx_info *dst_arinfo, *src_arinfo;
  1112. src_txc = (void *) src->data;
  1113. src_info = IEEE80211_SKB_CB(src);
  1114. src_arinfo = (void *) src_info->rate_driver_data;
  1115. dst_txc = (void *) dst->data;
  1116. dst_info = IEEE80211_SKB_CB(dst);
  1117. dst_arinfo = (void *) dst_info->rate_driver_data;
  1118. dst_txc->phy_control = src_txc->phy_control;
  1119. /* same MCS for the whole aggregate */
  1120. memcpy(dst_info->driver_rates, src_info->driver_rates,
  1121. sizeof(dst_info->driver_rates));
  1122. }
  1123. static int ar9170_tx_prepare(struct ar9170 *ar, struct sk_buff *skb)
  1124. {
  1125. struct ieee80211_hdr *hdr;
  1126. struct ar9170_tx_control *txc;
  1127. struct ieee80211_tx_info *info;
  1128. struct ieee80211_tx_rate *txrate;
  1129. struct ar9170_tx_info *arinfo;
  1130. unsigned int queue = skb_get_queue_mapping(skb);
  1131. u16 keytype = 0;
  1132. u16 len, icv = 0;
  1133. BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
  1134. hdr = (void *)skb->data;
  1135. info = IEEE80211_SKB_CB(skb);
  1136. len = skb->len;
  1137. txc = (void *)skb_push(skb, sizeof(*txc));
  1138. if (info->control.hw_key) {
  1139. icv = info->control.hw_key->icv_len;
  1140. switch (info->control.hw_key->alg) {
  1141. case ALG_WEP:
  1142. keytype = AR9170_TX_MAC_ENCR_RC4;
  1143. break;
  1144. case ALG_TKIP:
  1145. keytype = AR9170_TX_MAC_ENCR_RC4;
  1146. break;
  1147. case ALG_CCMP:
  1148. keytype = AR9170_TX_MAC_ENCR_AES;
  1149. break;
  1150. default:
  1151. WARN_ON(1);
  1152. goto err_out;
  1153. }
  1154. }
  1155. /* Length */
  1156. txc->length = cpu_to_le16(len + icv + 4);
  1157. txc->mac_control = cpu_to_le16(AR9170_TX_MAC_HW_DURATION |
  1158. AR9170_TX_MAC_BACKOFF);
  1159. txc->mac_control |= cpu_to_le16(ar9170_qos_hwmap[queue] <<
  1160. AR9170_TX_MAC_QOS_SHIFT);
  1161. txc->mac_control |= cpu_to_le16(keytype);
  1162. txc->phy_control = cpu_to_le32(0);
  1163. if (info->flags & IEEE80211_TX_CTL_NO_ACK)
  1164. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_NO_ACK);
  1165. txrate = &info->control.rates[0];
  1166. if (txrate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  1167. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_PROT_CTS);
  1168. else if (txrate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
  1169. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_PROT_RTS);
  1170. arinfo = (void *)info->rate_driver_data;
  1171. arinfo->timeout = jiffies + msecs_to_jiffies(AR9170_QUEUE_TIMEOUT);
  1172. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
  1173. (is_valid_ether_addr(ieee80211_get_DA(hdr)))) {
  1174. if (info->flags & IEEE80211_TX_CTL_AMPDU) {
  1175. if (unlikely(!info->control.sta))
  1176. goto err_out;
  1177. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_AGGR);
  1178. arinfo->flags = AR9170_TX_FLAG_BLOCK_ACK;
  1179. goto out;
  1180. }
  1181. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_RATE_PROBE);
  1182. /*
  1183. * WARNING:
  1184. * Putting the QoS queue bits into an unexplored territory is
  1185. * certainly not elegant.
  1186. *
  1187. * In my defense: This idea provides a reasonable way to
  1188. * smuggle valuable information to the tx_status callback.
  1189. * Also, the idea behind this bit-abuse came straight from
  1190. * the original driver code.
  1191. */
  1192. txc->phy_control |=
  1193. cpu_to_le32(queue << AR9170_TX_PHY_QOS_SHIFT);
  1194. arinfo->flags = AR9170_TX_FLAG_WAIT_FOR_ACK;
  1195. } else {
  1196. arinfo->flags = AR9170_TX_FLAG_NO_ACK;
  1197. }
  1198. out:
  1199. return 0;
  1200. err_out:
  1201. skb_pull(skb, sizeof(*txc));
  1202. return -EINVAL;
  1203. }
  1204. static void ar9170_tx_prepare_phy(struct ar9170 *ar, struct sk_buff *skb)
  1205. {
  1206. struct ar9170_tx_control *txc;
  1207. struct ieee80211_tx_info *info;
  1208. struct ieee80211_rate *rate = NULL;
  1209. struct ieee80211_tx_rate *txrate;
  1210. u32 power, chains;
  1211. txc = (void *) skb->data;
  1212. info = IEEE80211_SKB_CB(skb);
  1213. txrate = &info->control.rates[0];
  1214. if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
  1215. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_GREENFIELD);
  1216. if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  1217. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_SHORT_PREAMBLE);
  1218. if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  1219. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ);
  1220. /* this works because 40 MHz is 2 and dup is 3 */
  1221. if (txrate->flags & IEEE80211_TX_RC_DUP_DATA)
  1222. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ_DUP);
  1223. if (txrate->flags & IEEE80211_TX_RC_SHORT_GI)
  1224. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_SHORT_GI);
  1225. if (txrate->flags & IEEE80211_TX_RC_MCS) {
  1226. u32 r = txrate->idx;
  1227. u8 *txpower;
  1228. /* heavy clip control */
  1229. txc->phy_control |= cpu_to_le32((r & 0x7) << 7);
  1230. r <<= AR9170_TX_PHY_MCS_SHIFT;
  1231. BUG_ON(r & ~AR9170_TX_PHY_MCS_MASK);
  1232. txc->phy_control |= cpu_to_le32(r & AR9170_TX_PHY_MCS_MASK);
  1233. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_MOD_HT);
  1234. if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
  1235. if (info->band == IEEE80211_BAND_5GHZ)
  1236. txpower = ar->power_5G_ht40;
  1237. else
  1238. txpower = ar->power_2G_ht40;
  1239. } else {
  1240. if (info->band == IEEE80211_BAND_5GHZ)
  1241. txpower = ar->power_5G_ht20;
  1242. else
  1243. txpower = ar->power_2G_ht20;
  1244. }
  1245. power = txpower[(txrate->idx) & 7];
  1246. } else {
  1247. u8 *txpower;
  1248. u32 mod;
  1249. u32 phyrate;
  1250. u8 idx = txrate->idx;
  1251. if (info->band != IEEE80211_BAND_2GHZ) {
  1252. idx += 4;
  1253. txpower = ar->power_5G_leg;
  1254. mod = AR9170_TX_PHY_MOD_OFDM;
  1255. } else {
  1256. if (idx < 4) {
  1257. txpower = ar->power_2G_cck;
  1258. mod = AR9170_TX_PHY_MOD_CCK;
  1259. } else {
  1260. mod = AR9170_TX_PHY_MOD_OFDM;
  1261. txpower = ar->power_2G_ofdm;
  1262. }
  1263. }
  1264. rate = &__ar9170_ratetable[idx];
  1265. phyrate = rate->hw_value & 0xF;
  1266. power = txpower[(rate->hw_value & 0x30) >> 4];
  1267. phyrate <<= AR9170_TX_PHY_MCS_SHIFT;
  1268. txc->phy_control |= cpu_to_le32(mod);
  1269. txc->phy_control |= cpu_to_le32(phyrate);
  1270. }
  1271. power <<= AR9170_TX_PHY_TX_PWR_SHIFT;
  1272. power &= AR9170_TX_PHY_TX_PWR_MASK;
  1273. txc->phy_control |= cpu_to_le32(power);
  1274. /* set TX chains */
  1275. if (ar->eeprom.tx_mask == 1) {
  1276. chains = AR9170_TX_PHY_TXCHAIN_1;
  1277. } else {
  1278. chains = AR9170_TX_PHY_TXCHAIN_2;
  1279. /* >= 36M legacy OFDM - use only one chain */
  1280. if (rate && rate->bitrate >= 360)
  1281. chains = AR9170_TX_PHY_TXCHAIN_1;
  1282. }
  1283. txc->phy_control |= cpu_to_le32(chains << AR9170_TX_PHY_TXCHAIN_SHIFT);
  1284. }
  1285. static bool ar9170_tx_ampdu(struct ar9170 *ar)
  1286. {
  1287. struct sk_buff_head agg;
  1288. struct ar9170_sta_tid *tid_info = NULL, *tmp;
  1289. struct sk_buff *skb, *first = NULL;
  1290. unsigned long flags, f2;
  1291. unsigned int i = 0;
  1292. u16 seq, queue, tmpssn;
  1293. bool run = false;
  1294. skb_queue_head_init(&agg);
  1295. spin_lock_irqsave(&ar->tx_ampdu_list_lock, flags);
  1296. if (list_empty(&ar->tx_ampdu_list)) {
  1297. #ifdef AR9170_TXAGG_DEBUG
  1298. printk(KERN_DEBUG "%s: aggregation list is empty.\n",
  1299. wiphy_name(ar->hw->wiphy));
  1300. #endif /* AR9170_TXAGG_DEBUG */
  1301. goto out_unlock;
  1302. }
  1303. list_for_each_entry_safe(tid_info, tmp, &ar->tx_ampdu_list, list) {
  1304. if (tid_info->state != AR9170_TID_STATE_COMPLETE) {
  1305. #ifdef AR9170_TXAGG_DEBUG
  1306. printk(KERN_DEBUG "%s: dangling aggregation entry!\n",
  1307. wiphy_name(ar->hw->wiphy));
  1308. #endif /* AR9170_TXAGG_DEBUG */
  1309. continue;
  1310. }
  1311. if (++i > 64) {
  1312. #ifdef AR9170_TXAGG_DEBUG
  1313. printk(KERN_DEBUG "%s: enough frames aggregated.\n",
  1314. wiphy_name(ar->hw->wiphy));
  1315. #endif /* AR9170_TXAGG_DEBUG */
  1316. break;
  1317. }
  1318. queue = TID_TO_WME_AC(tid_info->tid);
  1319. if (skb_queue_len(&ar->tx_pending[queue]) >=
  1320. AR9170_NUM_TX_AGG_MAX) {
  1321. #ifdef AR9170_TXAGG_DEBUG
  1322. printk(KERN_DEBUG "%s: queue %d full.\n",
  1323. wiphy_name(ar->hw->wiphy), queue);
  1324. #endif /* AR9170_TXAGG_DEBUG */
  1325. continue;
  1326. }
  1327. list_del_init(&tid_info->list);
  1328. spin_lock_irqsave(&tid_info->queue.lock, f2);
  1329. tmpssn = seq = tid_info->ssn;
  1330. first = skb_peek(&tid_info->queue);
  1331. if (likely(first))
  1332. tmpssn = ar9170_get_seq(first);
  1333. if (unlikely(tmpssn != seq)) {
  1334. #ifdef AR9170_TXAGG_DEBUG
  1335. printk(KERN_DEBUG "%s: ssn mismatch [%d != %d]\n.",
  1336. wiphy_name(ar->hw->wiphy), seq, tmpssn);
  1337. #endif /* AR9170_TXAGG_DEBUG */
  1338. tid_info->ssn = tmpssn;
  1339. }
  1340. #ifdef AR9170_TXAGG_DEBUG
  1341. printk(KERN_DEBUG "%s: generate A-MPDU for tid:%d ssn:%d with "
  1342. "%d queued frames.\n", wiphy_name(ar->hw->wiphy),
  1343. tid_info->tid, tid_info->ssn,
  1344. skb_queue_len(&tid_info->queue));
  1345. __ar9170_dump_txqueue(ar, &tid_info->queue);
  1346. #endif /* AR9170_TXAGG_DEBUG */
  1347. while ((skb = skb_peek(&tid_info->queue))) {
  1348. if (unlikely(ar9170_get_seq(skb) != seq))
  1349. break;
  1350. __skb_unlink(skb, &tid_info->queue);
  1351. tid_info->ssn = seq = GET_NEXT_SEQ(seq);
  1352. if (unlikely(skb_get_queue_mapping(skb) != queue)) {
  1353. #ifdef AR9170_TXAGG_DEBUG
  1354. printk(KERN_DEBUG "%s: tid:%d(q:%d) queue:%d "
  1355. "!match.\n", wiphy_name(ar->hw->wiphy),
  1356. tid_info->tid,
  1357. TID_TO_WME_AC(tid_info->tid),
  1358. skb_get_queue_mapping(skb));
  1359. #endif /* AR9170_TXAGG_DEBUG */
  1360. dev_kfree_skb_any(skb);
  1361. continue;
  1362. }
  1363. if (unlikely(first == skb)) {
  1364. ar9170_tx_prepare_phy(ar, skb);
  1365. __skb_queue_tail(&agg, skb);
  1366. first = skb;
  1367. } else {
  1368. ar9170_tx_copy_phy(ar, skb, first);
  1369. __skb_queue_tail(&agg, skb);
  1370. }
  1371. if (unlikely(skb_queue_len(&agg) ==
  1372. AR9170_NUM_TX_AGG_MAX))
  1373. break;
  1374. }
  1375. if (skb_queue_empty(&tid_info->queue))
  1376. tid_info->active = false;
  1377. else
  1378. list_add_tail(&tid_info->list,
  1379. &ar->tx_ampdu_list);
  1380. spin_unlock_irqrestore(&tid_info->queue.lock, f2);
  1381. if (unlikely(skb_queue_empty(&agg))) {
  1382. #ifdef AR9170_TXAGG_DEBUG
  1383. printk(KERN_DEBUG "%s: queued empty list!\n",
  1384. wiphy_name(ar->hw->wiphy));
  1385. #endif /* AR9170_TXAGG_DEBUG */
  1386. continue;
  1387. }
  1388. /*
  1389. * tell the FW/HW that this is the last frame,
  1390. * that way it will wait for the immediate block ack.
  1391. */
  1392. if (likely(skb_peek_tail(&agg)))
  1393. ar9170_tx_indicate_immba(ar, skb_peek_tail(&agg));
  1394. #ifdef AR9170_TXAGG_DEBUG
  1395. printk(KERN_DEBUG "%s: generated A-MPDU looks like this:\n",
  1396. wiphy_name(ar->hw->wiphy));
  1397. __ar9170_dump_txqueue(ar, &agg);
  1398. #endif /* AR9170_TXAGG_DEBUG */
  1399. spin_unlock_irqrestore(&ar->tx_ampdu_list_lock, flags);
  1400. spin_lock_irqsave(&ar->tx_pending[queue].lock, flags);
  1401. skb_queue_splice_tail_init(&agg, &ar->tx_pending[queue]);
  1402. spin_unlock_irqrestore(&ar->tx_pending[queue].lock, flags);
  1403. run = true;
  1404. spin_lock_irqsave(&ar->tx_ampdu_list_lock, flags);
  1405. }
  1406. out_unlock:
  1407. spin_unlock_irqrestore(&ar->tx_ampdu_list_lock, flags);
  1408. __skb_queue_purge(&agg);
  1409. return run;
  1410. }
  1411. static void ar9170_tx(struct ar9170 *ar)
  1412. {
  1413. struct sk_buff *skb;
  1414. unsigned long flags;
  1415. struct ieee80211_tx_info *info;
  1416. struct ar9170_tx_info *arinfo;
  1417. unsigned int i, frames, frames_failed, remaining_space;
  1418. int err;
  1419. bool schedule_garbagecollector = false;
  1420. BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
  1421. if (unlikely(!IS_STARTED(ar)))
  1422. return ;
  1423. remaining_space = AR9170_TX_MAX_PENDING;
  1424. for (i = 0; i < __AR9170_NUM_TXQ; i++) {
  1425. spin_lock_irqsave(&ar->tx_stats_lock, flags);
  1426. if (ar->tx_stats[i].len >= ar->tx_stats[i].limit) {
  1427. #ifdef AR9170_QUEUE_DEBUG
  1428. printk(KERN_DEBUG "%s: queue %d full\n",
  1429. wiphy_name(ar->hw->wiphy), i);
  1430. printk(KERN_DEBUG "%s: stuck frames: ===> \n",
  1431. wiphy_name(ar->hw->wiphy));
  1432. ar9170_dump_txqueue(ar, &ar->tx_pending[i]);
  1433. ar9170_dump_txqueue(ar, &ar->tx_status[i]);
  1434. #endif /* AR9170_QUEUE_DEBUG */
  1435. #ifdef AR9170_QUEUE_STOP_DEBUG
  1436. printk(KERN_DEBUG "%s: stop queue %d\n",
  1437. wiphy_name(ar->hw->wiphy), i);
  1438. __ar9170_dump_txstats(ar);
  1439. #endif /* AR9170_QUEUE_STOP_DEBUG */
  1440. ieee80211_stop_queue(ar->hw, i);
  1441. spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
  1442. continue;
  1443. }
  1444. frames = min(ar->tx_stats[i].limit - ar->tx_stats[i].len,
  1445. skb_queue_len(&ar->tx_pending[i]));
  1446. if (remaining_space < frames) {
  1447. #ifdef AR9170_QUEUE_DEBUG
  1448. printk(KERN_DEBUG "%s: tx quota reached queue:%d, "
  1449. "remaining slots:%d, needed:%d\n",
  1450. wiphy_name(ar->hw->wiphy), i, remaining_space,
  1451. frames);
  1452. #endif /* AR9170_QUEUE_DEBUG */
  1453. frames = remaining_space;
  1454. }
  1455. ar->tx_stats[i].len += frames;
  1456. ar->tx_stats[i].count += frames;
  1457. spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
  1458. if (!frames)
  1459. continue;
  1460. frames_failed = 0;
  1461. while (frames) {
  1462. skb = skb_dequeue(&ar->tx_pending[i]);
  1463. if (unlikely(!skb)) {
  1464. frames_failed += frames;
  1465. frames = 0;
  1466. break;
  1467. }
  1468. info = IEEE80211_SKB_CB(skb);
  1469. arinfo = (void *) info->rate_driver_data;
  1470. /* TODO: cancel stuck frames */
  1471. arinfo->timeout = jiffies +
  1472. msecs_to_jiffies(AR9170_TX_TIMEOUT);
  1473. if (arinfo->flags == AR9170_TX_FLAG_BLOCK_ACK)
  1474. ar->tx_ampdu_pending++;
  1475. #ifdef AR9170_QUEUE_DEBUG
  1476. printk(KERN_DEBUG "%s: send frame q:%d =>\n",
  1477. wiphy_name(ar->hw->wiphy), i);
  1478. ar9170_print_txheader(ar, skb);
  1479. #endif /* AR9170_QUEUE_DEBUG */
  1480. err = ar->tx(ar, skb);
  1481. if (unlikely(err)) {
  1482. if (arinfo->flags == AR9170_TX_FLAG_BLOCK_ACK)
  1483. ar->tx_ampdu_pending--;
  1484. frames_failed++;
  1485. dev_kfree_skb_any(skb);
  1486. } else {
  1487. remaining_space--;
  1488. schedule_garbagecollector = true;
  1489. }
  1490. frames--;
  1491. }
  1492. #ifdef AR9170_QUEUE_DEBUG
  1493. printk(KERN_DEBUG "%s: ar9170_tx report for queue %d\n",
  1494. wiphy_name(ar->hw->wiphy), i);
  1495. printk(KERN_DEBUG "%s: unprocessed pending frames left:\n",
  1496. wiphy_name(ar->hw->wiphy));
  1497. ar9170_dump_txqueue(ar, &ar->tx_pending[i]);
  1498. #endif /* AR9170_QUEUE_DEBUG */
  1499. if (unlikely(frames_failed)) {
  1500. #ifdef AR9170_QUEUE_DEBUG
  1501. printk(KERN_DEBUG "%s: frames failed %d =>\n",
  1502. wiphy_name(ar->hw->wiphy), frames_failed);
  1503. #endif /* AR9170_QUEUE_DEBUG */
  1504. spin_lock_irqsave(&ar->tx_stats_lock, flags);
  1505. ar->tx_stats[i].len -= frames_failed;
  1506. ar->tx_stats[i].count -= frames_failed;
  1507. #ifdef AR9170_QUEUE_STOP_DEBUG
  1508. printk(KERN_DEBUG "%s: wake queue %d\n",
  1509. wiphy_name(ar->hw->wiphy), i);
  1510. __ar9170_dump_txstats(ar);
  1511. #endif /* AR9170_QUEUE_STOP_DEBUG */
  1512. ieee80211_wake_queue(ar->hw, i);
  1513. spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
  1514. }
  1515. }
  1516. if (!schedule_garbagecollector)
  1517. return;
  1518. ieee80211_queue_delayed_work(ar->hw,
  1519. &ar->tx_janitor,
  1520. msecs_to_jiffies(AR9170_JANITOR_DELAY));
  1521. }
  1522. static bool ar9170_tx_ampdu_queue(struct ar9170 *ar, struct sk_buff *skb)
  1523. {
  1524. struct ieee80211_tx_info *txinfo;
  1525. struct ar9170_sta_info *sta_info;
  1526. struct ar9170_sta_tid *agg;
  1527. struct sk_buff *iter;
  1528. unsigned long flags, f2;
  1529. unsigned int max;
  1530. u16 tid, seq, qseq;
  1531. bool run = false, queue = false;
  1532. tid = ar9170_get_tid(skb);
  1533. seq = ar9170_get_seq(skb);
  1534. txinfo = IEEE80211_SKB_CB(skb);
  1535. sta_info = (void *) txinfo->control.sta->drv_priv;
  1536. agg = &sta_info->agg[tid];
  1537. max = sta_info->ampdu_max_len;
  1538. spin_lock_irqsave(&ar->tx_ampdu_list_lock, flags);
  1539. if (unlikely(agg->state != AR9170_TID_STATE_COMPLETE)) {
  1540. #ifdef AR9170_TXAGG_DEBUG
  1541. printk(KERN_DEBUG "%s: BlockACK session not fully initialized "
  1542. "for ESS:%pM tid:%d state:%d.\n",
  1543. wiphy_name(ar->hw->wiphy), agg->addr, agg->tid,
  1544. agg->state);
  1545. #endif /* AR9170_TXAGG_DEBUG */
  1546. goto err_unlock;
  1547. }
  1548. if (!agg->active) {
  1549. agg->active = true;
  1550. agg->ssn = seq;
  1551. queue = true;
  1552. }
  1553. /* check if seq is within the BA window */
  1554. if (unlikely(!BAW_WITHIN(agg->ssn, max, seq))) {
  1555. #ifdef AR9170_TXAGG_DEBUG
  1556. printk(KERN_DEBUG "%s: frame with tid:%d seq:%d does not "
  1557. "fit into BA window (%d - %d)\n",
  1558. wiphy_name(ar->hw->wiphy), tid, seq, agg->ssn,
  1559. (agg->ssn + max) & 0xfff);
  1560. #endif /* AR9170_TXAGG_DEBUG */
  1561. goto err_unlock;
  1562. }
  1563. spin_lock_irqsave(&agg->queue.lock, f2);
  1564. skb_queue_reverse_walk(&agg->queue, iter) {
  1565. qseq = ar9170_get_seq(iter);
  1566. if (GET_NEXT_SEQ(qseq) == seq) {
  1567. __skb_queue_after(&agg->queue, iter, skb);
  1568. goto queued;
  1569. }
  1570. }
  1571. __skb_queue_head(&agg->queue, skb);
  1572. queued:
  1573. spin_unlock_irqrestore(&agg->queue.lock, f2);
  1574. #ifdef AR9170_TXAGG_DEBUG
  1575. printk(KERN_DEBUG "%s: new aggregate %p queued.\n",
  1576. wiphy_name(ar->hw->wiphy), skb);
  1577. __ar9170_dump_txqueue(ar, &agg->queue);
  1578. #endif /* AR9170_TXAGG_DEBUG */
  1579. if (skb_queue_len(&agg->queue) >= AR9170_NUM_TX_AGG_MAX)
  1580. run = true;
  1581. if (queue)
  1582. list_add_tail(&agg->list, &ar->tx_ampdu_list);
  1583. spin_unlock_irqrestore(&ar->tx_ampdu_list_lock, flags);
  1584. return run;
  1585. err_unlock:
  1586. spin_unlock_irqrestore(&ar->tx_ampdu_list_lock, flags);
  1587. dev_kfree_skb_irq(skb);
  1588. return false;
  1589. }
  1590. int ar9170_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  1591. {
  1592. struct ar9170 *ar = hw->priv;
  1593. struct ieee80211_tx_info *info;
  1594. if (unlikely(!IS_STARTED(ar)))
  1595. goto err_free;
  1596. if (unlikely(ar9170_tx_prepare(ar, skb)))
  1597. goto err_free;
  1598. info = IEEE80211_SKB_CB(skb);
  1599. if (info->flags & IEEE80211_TX_CTL_AMPDU) {
  1600. bool run = ar9170_tx_ampdu_queue(ar, skb);
  1601. if (run || !ar->tx_ampdu_pending)
  1602. ar9170_tx_ampdu(ar);
  1603. } else {
  1604. unsigned int queue = skb_get_queue_mapping(skb);
  1605. ar9170_tx_prepare_phy(ar, skb);
  1606. skb_queue_tail(&ar->tx_pending[queue], skb);
  1607. }
  1608. ar9170_tx(ar);
  1609. return NETDEV_TX_OK;
  1610. err_free:
  1611. dev_kfree_skb_any(skb);
  1612. return NETDEV_TX_OK;
  1613. }
  1614. static int ar9170_op_add_interface(struct ieee80211_hw *hw,
  1615. struct ieee80211_if_init_conf *conf)
  1616. {
  1617. struct ar9170 *ar = hw->priv;
  1618. struct ath_common *common = &ar->common;
  1619. int err = 0;
  1620. mutex_lock(&ar->mutex);
  1621. if (ar->vif) {
  1622. err = -EBUSY;
  1623. goto unlock;
  1624. }
  1625. ar->vif = conf->vif;
  1626. memcpy(common->macaddr, conf->mac_addr, ETH_ALEN);
  1627. if (modparam_nohwcrypt || (ar->vif->type != NL80211_IFTYPE_STATION)) {
  1628. ar->rx_software_decryption = true;
  1629. ar->disable_offload = true;
  1630. }
  1631. ar->cur_filter = 0;
  1632. err = ar9170_update_frame_filter(ar, AR9170_MAC_REG_FTF_DEFAULTS);
  1633. if (err)
  1634. goto unlock;
  1635. err = ar9170_set_operating_mode(ar);
  1636. unlock:
  1637. mutex_unlock(&ar->mutex);
  1638. return err;
  1639. }
  1640. static void ar9170_op_remove_interface(struct ieee80211_hw *hw,
  1641. struct ieee80211_if_init_conf *conf)
  1642. {
  1643. struct ar9170 *ar = hw->priv;
  1644. mutex_lock(&ar->mutex);
  1645. ar->vif = NULL;
  1646. ar9170_update_frame_filter(ar, 0);
  1647. ar9170_set_beacon_timers(ar);
  1648. dev_kfree_skb(ar->beacon);
  1649. ar->beacon = NULL;
  1650. ar->sniffer_enabled = false;
  1651. ar->rx_software_decryption = false;
  1652. ar9170_set_operating_mode(ar);
  1653. mutex_unlock(&ar->mutex);
  1654. }
  1655. static int ar9170_op_config(struct ieee80211_hw *hw, u32 changed)
  1656. {
  1657. struct ar9170 *ar = hw->priv;
  1658. int err = 0;
  1659. mutex_lock(&ar->mutex);
  1660. if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
  1661. /* TODO */
  1662. err = 0;
  1663. }
  1664. if (changed & IEEE80211_CONF_CHANGE_PS) {
  1665. /* TODO */
  1666. err = 0;
  1667. }
  1668. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  1669. /* TODO */
  1670. err = 0;
  1671. }
  1672. if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
  1673. /*
  1674. * is it long_frame_max_tx_count or short_frame_max_tx_count?
  1675. */
  1676. err = ar9170_set_hwretry_limit(ar,
  1677. ar->hw->conf.long_frame_max_tx_count);
  1678. if (err)
  1679. goto out;
  1680. }
  1681. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  1682. /* adjust slot time for 5 GHz */
  1683. err = ar9170_set_slot_time(ar);
  1684. if (err)
  1685. goto out;
  1686. err = ar9170_set_dyn_sifs_ack(ar);
  1687. if (err)
  1688. goto out;
  1689. err = ar9170_set_channel(ar, hw->conf.channel,
  1690. AR9170_RFI_NONE,
  1691. nl80211_to_ar9170(hw->conf.channel_type));
  1692. if (err)
  1693. goto out;
  1694. }
  1695. out:
  1696. mutex_unlock(&ar->mutex);
  1697. return err;
  1698. }
  1699. static u64 ar9170_op_prepare_multicast(struct ieee80211_hw *hw, int mc_count,
  1700. struct dev_addr_list *mclist)
  1701. {
  1702. u64 mchash;
  1703. int i;
  1704. /* always get broadcast frames */
  1705. mchash = 1ULL << (0xff >> 2);
  1706. for (i = 0; i < mc_count; i++) {
  1707. if (WARN_ON(!mclist))
  1708. break;
  1709. mchash |= 1ULL << (mclist->dmi_addr[5] >> 2);
  1710. mclist = mclist->next;
  1711. }
  1712. return mchash;
  1713. }
  1714. static void ar9170_op_configure_filter(struct ieee80211_hw *hw,
  1715. unsigned int changed_flags,
  1716. unsigned int *new_flags,
  1717. u64 multicast)
  1718. {
  1719. struct ar9170 *ar = hw->priv;
  1720. if (unlikely(!IS_ACCEPTING_CMD(ar)))
  1721. return ;
  1722. mutex_lock(&ar->mutex);
  1723. /* mask supported flags */
  1724. *new_flags &= FIF_ALLMULTI | FIF_CONTROL | FIF_BCN_PRBRESP_PROMISC |
  1725. FIF_PROMISC_IN_BSS | FIF_FCSFAIL | FIF_PLCPFAIL;
  1726. ar->filter_state = *new_flags;
  1727. /*
  1728. * We can support more by setting the sniffer bit and
  1729. * then checking the error flags, later.
  1730. */
  1731. if (changed_flags & FIF_ALLMULTI && *new_flags & FIF_ALLMULTI)
  1732. multicast = ~0ULL;
  1733. if (multicast != ar->cur_mc_hash)
  1734. ar9170_update_multicast(ar, multicast);
  1735. if (changed_flags & FIF_CONTROL) {
  1736. u32 filter = AR9170_MAC_REG_FTF_PSPOLL |
  1737. AR9170_MAC_REG_FTF_RTS |
  1738. AR9170_MAC_REG_FTF_CTS |
  1739. AR9170_MAC_REG_FTF_ACK |
  1740. AR9170_MAC_REG_FTF_CFE |
  1741. AR9170_MAC_REG_FTF_CFE_ACK;
  1742. if (*new_flags & FIF_CONTROL)
  1743. filter |= ar->cur_filter;
  1744. else
  1745. filter &= (~ar->cur_filter);
  1746. ar9170_update_frame_filter(ar, filter);
  1747. }
  1748. if (changed_flags & FIF_PROMISC_IN_BSS) {
  1749. ar->sniffer_enabled = ((*new_flags) & FIF_PROMISC_IN_BSS) != 0;
  1750. ar9170_set_operating_mode(ar);
  1751. }
  1752. mutex_unlock(&ar->mutex);
  1753. }
  1754. static void ar9170_op_bss_info_changed(struct ieee80211_hw *hw,
  1755. struct ieee80211_vif *vif,
  1756. struct ieee80211_bss_conf *bss_conf,
  1757. u32 changed)
  1758. {
  1759. struct ar9170 *ar = hw->priv;
  1760. struct ath_common *common = &ar->common;
  1761. int err = 0;
  1762. mutex_lock(&ar->mutex);
  1763. if (changed & BSS_CHANGED_BSSID) {
  1764. memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
  1765. err = ar9170_set_operating_mode(ar);
  1766. if (err)
  1767. goto out;
  1768. }
  1769. if (changed & BSS_CHANGED_BEACON_ENABLED)
  1770. ar->enable_beacon = bss_conf->enable_beacon;
  1771. if (changed & BSS_CHANGED_BEACON) {
  1772. err = ar9170_update_beacon(ar);
  1773. if (err)
  1774. goto out;
  1775. }
  1776. if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON |
  1777. BSS_CHANGED_BEACON_INT)) {
  1778. err = ar9170_set_beacon_timers(ar);
  1779. if (err)
  1780. goto out;
  1781. }
  1782. if (changed & BSS_CHANGED_ASSOC) {
  1783. #ifndef CONFIG_AR9170_LEDS
  1784. /* enable assoc LED. */
  1785. err = ar9170_set_leds_state(ar, bss_conf->assoc ? 2 : 0);
  1786. #endif /* CONFIG_AR9170_LEDS */
  1787. }
  1788. if (changed & BSS_CHANGED_HT) {
  1789. /* TODO */
  1790. err = 0;
  1791. }
  1792. if (changed & BSS_CHANGED_ERP_SLOT) {
  1793. err = ar9170_set_slot_time(ar);
  1794. if (err)
  1795. goto out;
  1796. }
  1797. if (changed & BSS_CHANGED_BASIC_RATES) {
  1798. err = ar9170_set_basic_rates(ar);
  1799. if (err)
  1800. goto out;
  1801. }
  1802. out:
  1803. mutex_unlock(&ar->mutex);
  1804. }
  1805. static u64 ar9170_op_get_tsf(struct ieee80211_hw *hw)
  1806. {
  1807. struct ar9170 *ar = hw->priv;
  1808. int err;
  1809. u64 tsf;
  1810. #define NR 3
  1811. static const u32 addr[NR] = { AR9170_MAC_REG_TSF_H,
  1812. AR9170_MAC_REG_TSF_L,
  1813. AR9170_MAC_REG_TSF_H };
  1814. u32 val[NR];
  1815. int loops = 0;
  1816. mutex_lock(&ar->mutex);
  1817. while (loops++ < 10) {
  1818. err = ar9170_read_mreg(ar, NR, addr, val);
  1819. if (err || val[0] == val[2])
  1820. break;
  1821. }
  1822. mutex_unlock(&ar->mutex);
  1823. if (WARN_ON(err))
  1824. return 0;
  1825. tsf = val[0];
  1826. tsf = (tsf << 32) | val[1];
  1827. return tsf;
  1828. #undef NR
  1829. }
  1830. static int ar9170_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  1831. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  1832. struct ieee80211_key_conf *key)
  1833. {
  1834. struct ar9170 *ar = hw->priv;
  1835. int err = 0, i;
  1836. u8 ktype;
  1837. if ((!ar->vif) || (ar->disable_offload))
  1838. return -EOPNOTSUPP;
  1839. switch (key->alg) {
  1840. case ALG_WEP:
  1841. if (key->keylen == WLAN_KEY_LEN_WEP40)
  1842. ktype = AR9170_ENC_ALG_WEP64;
  1843. else
  1844. ktype = AR9170_ENC_ALG_WEP128;
  1845. break;
  1846. case ALG_TKIP:
  1847. ktype = AR9170_ENC_ALG_TKIP;
  1848. break;
  1849. case ALG_CCMP:
  1850. ktype = AR9170_ENC_ALG_AESCCMP;
  1851. break;
  1852. default:
  1853. return -EOPNOTSUPP;
  1854. }
  1855. mutex_lock(&ar->mutex);
  1856. if (cmd == SET_KEY) {
  1857. if (unlikely(!IS_STARTED(ar))) {
  1858. err = -EOPNOTSUPP;
  1859. goto out;
  1860. }
  1861. /* group keys need all-zeroes address */
  1862. if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  1863. sta = NULL;
  1864. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
  1865. for (i = 0; i < 64; i++)
  1866. if (!(ar->usedkeys & BIT(i)))
  1867. break;
  1868. if (i == 64) {
  1869. ar->rx_software_decryption = true;
  1870. ar9170_set_operating_mode(ar);
  1871. err = -ENOSPC;
  1872. goto out;
  1873. }
  1874. } else {
  1875. i = 64 + key->keyidx;
  1876. }
  1877. key->hw_key_idx = i;
  1878. err = ar9170_upload_key(ar, i, sta ? sta->addr : NULL, ktype, 0,
  1879. key->key, min_t(u8, 16, key->keylen));
  1880. if (err)
  1881. goto out;
  1882. if (key->alg == ALG_TKIP) {
  1883. err = ar9170_upload_key(ar, i, sta ? sta->addr : NULL,
  1884. ktype, 1, key->key + 16, 16);
  1885. if (err)
  1886. goto out;
  1887. /*
  1888. * hardware is not capable generating the MMIC
  1889. * for fragmented frames!
  1890. */
  1891. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  1892. }
  1893. if (i < 64)
  1894. ar->usedkeys |= BIT(i);
  1895. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1896. } else {
  1897. if (unlikely(!IS_STARTED(ar))) {
  1898. /* The device is gone... together with the key ;-) */
  1899. err = 0;
  1900. goto out;
  1901. }
  1902. err = ar9170_disable_key(ar, key->hw_key_idx);
  1903. if (err)
  1904. goto out;
  1905. if (key->hw_key_idx < 64) {
  1906. ar->usedkeys &= ~BIT(key->hw_key_idx);
  1907. } else {
  1908. err = ar9170_upload_key(ar, key->hw_key_idx, NULL,
  1909. AR9170_ENC_ALG_NONE, 0,
  1910. NULL, 0);
  1911. if (err)
  1912. goto out;
  1913. if (key->alg == ALG_TKIP) {
  1914. err = ar9170_upload_key(ar, key->hw_key_idx,
  1915. NULL,
  1916. AR9170_ENC_ALG_NONE, 1,
  1917. NULL, 0);
  1918. if (err)
  1919. goto out;
  1920. }
  1921. }
  1922. }
  1923. ar9170_regwrite_begin(ar);
  1924. ar9170_regwrite(AR9170_MAC_REG_ROLL_CALL_TBL_L, ar->usedkeys);
  1925. ar9170_regwrite(AR9170_MAC_REG_ROLL_CALL_TBL_H, ar->usedkeys >> 32);
  1926. ar9170_regwrite_finish();
  1927. err = ar9170_regwrite_result();
  1928. out:
  1929. mutex_unlock(&ar->mutex);
  1930. return err;
  1931. }
  1932. static void ar9170_sta_notify(struct ieee80211_hw *hw,
  1933. struct ieee80211_vif *vif,
  1934. enum sta_notify_cmd cmd,
  1935. struct ieee80211_sta *sta)
  1936. {
  1937. struct ar9170 *ar = hw->priv;
  1938. struct ar9170_sta_info *sta_info = (void *) sta->drv_priv;
  1939. unsigned int i;
  1940. switch (cmd) {
  1941. case STA_NOTIFY_ADD:
  1942. memset(sta_info, 0, sizeof(*sta_info));
  1943. if (!sta->ht_cap.ht_supported)
  1944. break;
  1945. if (sta->ht_cap.ampdu_density > ar->global_ampdu_density)
  1946. ar->global_ampdu_density = sta->ht_cap.ampdu_density;
  1947. if (sta->ht_cap.ampdu_factor < ar->global_ampdu_factor)
  1948. ar->global_ampdu_factor = sta->ht_cap.ampdu_factor;
  1949. for (i = 0; i < AR9170_NUM_TID; i++) {
  1950. sta_info->agg[i].state = AR9170_TID_STATE_SHUTDOWN;
  1951. sta_info->agg[i].active = false;
  1952. sta_info->agg[i].ssn = 0;
  1953. sta_info->agg[i].retry = 0;
  1954. sta_info->agg[i].tid = i;
  1955. INIT_LIST_HEAD(&sta_info->agg[i].list);
  1956. skb_queue_head_init(&sta_info->agg[i].queue);
  1957. }
  1958. sta_info->ampdu_max_len = 1 << (3 + sta->ht_cap.ampdu_factor);
  1959. break;
  1960. case STA_NOTIFY_REMOVE:
  1961. if (!sta->ht_cap.ht_supported)
  1962. break;
  1963. for (i = 0; i < AR9170_NUM_TID; i++) {
  1964. sta_info->agg[i].state = AR9170_TID_STATE_INVALID;
  1965. skb_queue_purge(&sta_info->agg[i].queue);
  1966. }
  1967. break;
  1968. default:
  1969. break;
  1970. }
  1971. }
  1972. static int ar9170_get_stats(struct ieee80211_hw *hw,
  1973. struct ieee80211_low_level_stats *stats)
  1974. {
  1975. struct ar9170 *ar = hw->priv;
  1976. u32 val;
  1977. int err;
  1978. mutex_lock(&ar->mutex);
  1979. err = ar9170_read_reg(ar, AR9170_MAC_REG_TX_RETRY, &val);
  1980. ar->stats.dot11ACKFailureCount += val;
  1981. memcpy(stats, &ar->stats, sizeof(*stats));
  1982. mutex_unlock(&ar->mutex);
  1983. return 0;
  1984. }
  1985. static int ar9170_get_tx_stats(struct ieee80211_hw *hw,
  1986. struct ieee80211_tx_queue_stats *tx_stats)
  1987. {
  1988. struct ar9170 *ar = hw->priv;
  1989. spin_lock_bh(&ar->tx_stats_lock);
  1990. memcpy(tx_stats, ar->tx_stats, sizeof(tx_stats[0]) * hw->queues);
  1991. spin_unlock_bh(&ar->tx_stats_lock);
  1992. return 0;
  1993. }
  1994. static int ar9170_conf_tx(struct ieee80211_hw *hw, u16 queue,
  1995. const struct ieee80211_tx_queue_params *param)
  1996. {
  1997. struct ar9170 *ar = hw->priv;
  1998. int ret;
  1999. mutex_lock(&ar->mutex);
  2000. if (queue < __AR9170_NUM_TXQ) {
  2001. memcpy(&ar->edcf[ar9170_qos_hwmap[queue]],
  2002. param, sizeof(*param));
  2003. ret = ar9170_set_qos(ar);
  2004. } else {
  2005. ret = -EINVAL;
  2006. }
  2007. mutex_unlock(&ar->mutex);
  2008. return ret;
  2009. }
  2010. static int ar9170_ampdu_action(struct ieee80211_hw *hw,
  2011. enum ieee80211_ampdu_mlme_action action,
  2012. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  2013. {
  2014. struct ar9170 *ar = hw->priv;
  2015. struct ar9170_sta_info *sta_info = (void *) sta->drv_priv;
  2016. struct ar9170_sta_tid *tid_info = &sta_info->agg[tid];
  2017. unsigned long flags;
  2018. if (!modparam_ht)
  2019. return -EOPNOTSUPP;
  2020. switch (action) {
  2021. case IEEE80211_AMPDU_TX_START:
  2022. spin_lock_irqsave(&ar->tx_ampdu_list_lock, flags);
  2023. if (tid_info->state != AR9170_TID_STATE_SHUTDOWN ||
  2024. !list_empty(&tid_info->list)) {
  2025. spin_unlock_irqrestore(&ar->tx_ampdu_list_lock, flags);
  2026. #ifdef AR9170_TXAGG_DEBUG
  2027. printk(KERN_INFO "%s: A-MPDU [ESS:[%pM] tid:[%d]] "
  2028. "is in a very bad state!\n",
  2029. wiphy_name(hw->wiphy), sta->addr, tid);
  2030. #endif /* AR9170_TXAGG_DEBUG */
  2031. return -EBUSY;
  2032. }
  2033. *ssn = tid_info->ssn;
  2034. tid_info->state = AR9170_TID_STATE_PROGRESS;
  2035. tid_info->active = false;
  2036. spin_unlock_irqrestore(&ar->tx_ampdu_list_lock, flags);
  2037. ieee80211_start_tx_ba_cb_irqsafe(hw, sta->addr, tid);
  2038. break;
  2039. case IEEE80211_AMPDU_TX_STOP:
  2040. spin_lock_irqsave(&ar->tx_ampdu_list_lock, flags);
  2041. tid_info->state = AR9170_TID_STATE_SHUTDOWN;
  2042. list_del_init(&tid_info->list);
  2043. tid_info->active = false;
  2044. skb_queue_purge(&tid_info->queue);
  2045. spin_unlock_irqrestore(&ar->tx_ampdu_list_lock, flags);
  2046. ieee80211_stop_tx_ba_cb_irqsafe(hw, sta->addr, tid);
  2047. break;
  2048. case IEEE80211_AMPDU_TX_OPERATIONAL:
  2049. #ifdef AR9170_TXAGG_DEBUG
  2050. printk(KERN_INFO "%s: A-MPDU for %pM [tid:%d] Operational.\n",
  2051. wiphy_name(hw->wiphy), sta->addr, tid);
  2052. #endif /* AR9170_TXAGG_DEBUG */
  2053. spin_lock_irqsave(&ar->tx_ampdu_list_lock, flags);
  2054. sta_info->agg[tid].state = AR9170_TID_STATE_COMPLETE;
  2055. spin_unlock_irqrestore(&ar->tx_ampdu_list_lock, flags);
  2056. break;
  2057. case IEEE80211_AMPDU_RX_START:
  2058. case IEEE80211_AMPDU_RX_STOP:
  2059. /* Handled by firmware */
  2060. break;
  2061. default:
  2062. return -EOPNOTSUPP;
  2063. }
  2064. return 0;
  2065. }
  2066. static const struct ieee80211_ops ar9170_ops = {
  2067. .start = ar9170_op_start,
  2068. .stop = ar9170_op_stop,
  2069. .tx = ar9170_op_tx,
  2070. .add_interface = ar9170_op_add_interface,
  2071. .remove_interface = ar9170_op_remove_interface,
  2072. .config = ar9170_op_config,
  2073. .prepare_multicast = ar9170_op_prepare_multicast,
  2074. .configure_filter = ar9170_op_configure_filter,
  2075. .conf_tx = ar9170_conf_tx,
  2076. .bss_info_changed = ar9170_op_bss_info_changed,
  2077. .get_tsf = ar9170_op_get_tsf,
  2078. .set_key = ar9170_set_key,
  2079. .sta_notify = ar9170_sta_notify,
  2080. .get_stats = ar9170_get_stats,
  2081. .get_tx_stats = ar9170_get_tx_stats,
  2082. .ampdu_action = ar9170_ampdu_action,
  2083. };
  2084. void *ar9170_alloc(size_t priv_size)
  2085. {
  2086. struct ieee80211_hw *hw;
  2087. struct ar9170 *ar;
  2088. struct sk_buff *skb;
  2089. int i;
  2090. /*
  2091. * this buffer is used for rx stream reconstruction.
  2092. * Under heavy load this device (or the transport layer?)
  2093. * tends to split the streams into seperate rx descriptors.
  2094. */
  2095. skb = __dev_alloc_skb(AR9170_MAX_RX_BUFFER_SIZE, GFP_KERNEL);
  2096. if (!skb)
  2097. goto err_nomem;
  2098. hw = ieee80211_alloc_hw(priv_size, &ar9170_ops);
  2099. if (!hw)
  2100. goto err_nomem;
  2101. ar = hw->priv;
  2102. ar->hw = hw;
  2103. ar->rx_failover = skb;
  2104. mutex_init(&ar->mutex);
  2105. spin_lock_init(&ar->cmdlock);
  2106. spin_lock_init(&ar->tx_stats_lock);
  2107. spin_lock_init(&ar->tx_ampdu_list_lock);
  2108. skb_queue_head_init(&ar->tx_status_ampdu);
  2109. for (i = 0; i < __AR9170_NUM_TXQ; i++) {
  2110. skb_queue_head_init(&ar->tx_status[i]);
  2111. skb_queue_head_init(&ar->tx_pending[i]);
  2112. }
  2113. ar9170_rx_reset_rx_mpdu(ar);
  2114. INIT_WORK(&ar->beacon_work, ar9170_new_beacon);
  2115. INIT_DELAYED_WORK(&ar->tx_janitor, ar9170_tx_janitor);
  2116. INIT_LIST_HEAD(&ar->tx_ampdu_list);
  2117. /* all hw supports 2.4 GHz, so set channel to 1 by default */
  2118. ar->channel = &ar9170_2ghz_chantable[0];
  2119. /* first part of wiphy init */
  2120. ar->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  2121. BIT(NL80211_IFTYPE_WDS) |
  2122. BIT(NL80211_IFTYPE_ADHOC);
  2123. ar->hw->flags |= IEEE80211_HW_RX_INCLUDES_FCS |
  2124. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  2125. IEEE80211_HW_SIGNAL_DBM |
  2126. IEEE80211_HW_NOISE_DBM;
  2127. if (modparam_ht) {
  2128. ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
  2129. } else {
  2130. ar9170_band_2GHz.ht_cap.ht_supported = false;
  2131. ar9170_band_5GHz.ht_cap.ht_supported = false;
  2132. }
  2133. ar->hw->queues = __AR9170_NUM_TXQ;
  2134. ar->hw->extra_tx_headroom = 8;
  2135. ar->hw->sta_data_size = sizeof(struct ar9170_sta_info);
  2136. ar->hw->max_rates = 1;
  2137. ar->hw->max_rate_tries = 3;
  2138. for (i = 0; i < ARRAY_SIZE(ar->noise); i++)
  2139. ar->noise[i] = -95; /* ATH_DEFAULT_NOISE_FLOOR */
  2140. return ar;
  2141. err_nomem:
  2142. kfree_skb(skb);
  2143. return ERR_PTR(-ENOMEM);
  2144. }
  2145. static int ar9170_read_eeprom(struct ar9170 *ar)
  2146. {
  2147. #define RW 8 /* number of words to read at once */
  2148. #define RB (sizeof(u32) * RW)
  2149. struct ath_regulatory *regulatory = &ar->common.regulatory;
  2150. u8 *eeprom = (void *)&ar->eeprom;
  2151. u8 *addr = ar->eeprom.mac_address;
  2152. __le32 offsets[RW];
  2153. unsigned int rx_streams, tx_streams, tx_params = 0;
  2154. int i, j, err, bands = 0;
  2155. BUILD_BUG_ON(sizeof(ar->eeprom) & 3);
  2156. BUILD_BUG_ON(RB > AR9170_MAX_CMD_LEN - 4);
  2157. #ifndef __CHECKER__
  2158. /* don't want to handle trailing remains */
  2159. BUILD_BUG_ON(sizeof(ar->eeprom) % RB);
  2160. #endif
  2161. for (i = 0; i < sizeof(ar->eeprom)/RB; i++) {
  2162. for (j = 0; j < RW; j++)
  2163. offsets[j] = cpu_to_le32(AR9170_EEPROM_START +
  2164. RB * i + 4 * j);
  2165. err = ar->exec_cmd(ar, AR9170_CMD_RREG,
  2166. RB, (u8 *) &offsets,
  2167. RB, eeprom + RB * i);
  2168. if (err)
  2169. return err;
  2170. }
  2171. #undef RW
  2172. #undef RB
  2173. if (ar->eeprom.length == cpu_to_le16(0xFFFF))
  2174. return -ENODATA;
  2175. if (ar->eeprom.operating_flags & AR9170_OPFLAG_2GHZ) {
  2176. ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &ar9170_band_2GHz;
  2177. bands++;
  2178. }
  2179. if (ar->eeprom.operating_flags & AR9170_OPFLAG_5GHZ) {
  2180. ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &ar9170_band_5GHz;
  2181. bands++;
  2182. }
  2183. rx_streams = hweight8(ar->eeprom.rx_mask);
  2184. tx_streams = hweight8(ar->eeprom.tx_mask);
  2185. if (rx_streams != tx_streams)
  2186. tx_params = IEEE80211_HT_MCS_TX_RX_DIFF;
  2187. if (tx_streams >= 1 && tx_streams <= IEEE80211_HT_MCS_TX_MAX_STREAMS)
  2188. tx_params = (tx_streams - 1) <<
  2189. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
  2190. ar9170_band_2GHz.ht_cap.mcs.tx_params |= tx_params;
  2191. ar9170_band_5GHz.ht_cap.mcs.tx_params |= tx_params;
  2192. /*
  2193. * I measured this, a bandswitch takes roughly
  2194. * 135 ms and a frequency switch about 80.
  2195. *
  2196. * FIXME: measure these values again once EEPROM settings
  2197. * are used, that will influence them!
  2198. */
  2199. if (bands == 2)
  2200. ar->hw->channel_change_time = 135 * 1000;
  2201. else
  2202. ar->hw->channel_change_time = 80 * 1000;
  2203. regulatory->current_rd = le16_to_cpu(ar->eeprom.reg_domain[0]);
  2204. regulatory->current_rd_ext = le16_to_cpu(ar->eeprom.reg_domain[1]);
  2205. /* second part of wiphy init */
  2206. SET_IEEE80211_PERM_ADDR(ar->hw, addr);
  2207. return bands ? 0 : -EINVAL;
  2208. }
  2209. static int ar9170_reg_notifier(struct wiphy *wiphy,
  2210. struct regulatory_request *request)
  2211. {
  2212. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  2213. struct ar9170 *ar = hw->priv;
  2214. return ath_reg_notifier_apply(wiphy, request, &ar->common.regulatory);
  2215. }
  2216. int ar9170_register(struct ar9170 *ar, struct device *pdev)
  2217. {
  2218. struct ath_regulatory *regulatory = &ar->common.regulatory;
  2219. int err;
  2220. /* try to read EEPROM, init MAC addr */
  2221. err = ar9170_read_eeprom(ar);
  2222. if (err)
  2223. goto err_out;
  2224. err = ath_regd_init(regulatory, ar->hw->wiphy,
  2225. ar9170_reg_notifier);
  2226. if (err)
  2227. goto err_out;
  2228. err = ieee80211_register_hw(ar->hw);
  2229. if (err)
  2230. goto err_out;
  2231. if (!ath_is_world_regd(regulatory))
  2232. regulatory_hint(ar->hw->wiphy, regulatory->alpha2);
  2233. err = ar9170_init_leds(ar);
  2234. if (err)
  2235. goto err_unreg;
  2236. #ifdef CONFIG_AR9170_LEDS
  2237. err = ar9170_register_leds(ar);
  2238. if (err)
  2239. goto err_unreg;
  2240. #endif /* CONFIG_AR9170_LEDS */
  2241. dev_info(pdev, "Atheros AR9170 is registered as '%s'\n",
  2242. wiphy_name(ar->hw->wiphy));
  2243. return err;
  2244. err_unreg:
  2245. ieee80211_unregister_hw(ar->hw);
  2246. err_out:
  2247. return err;
  2248. }
  2249. void ar9170_unregister(struct ar9170 *ar)
  2250. {
  2251. #ifdef CONFIG_AR9170_LEDS
  2252. ar9170_unregister_leds(ar);
  2253. #endif /* CONFIG_AR9170_LEDS */
  2254. kfree_skb(ar->rx_failover);
  2255. ieee80211_unregister_hw(ar->hw);
  2256. mutex_destroy(&ar->mutex);
  2257. }