txrx.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231
  1. /*
  2. * Copyright (c) 2012-2017 Qualcomm Atheros, Inc.
  3. * Copyright (c) 2018, The Linux Foundation. All rights reserved.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <linux/etherdevice.h>
  18. #include <net/ieee80211_radiotap.h>
  19. #include <linux/if_arp.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/ip.h>
  22. #include <linux/ipv6.h>
  23. #include <net/ipv6.h>
  24. #include <linux/prefetch.h>
  25. #include "wil6210.h"
  26. #include "wmi.h"
  27. #include "txrx.h"
  28. #include "trace.h"
  29. static bool rtap_include_phy_info;
  30. module_param(rtap_include_phy_info, bool, 0444);
  31. MODULE_PARM_DESC(rtap_include_phy_info,
  32. " Include PHY info in the radiotap header, default - no");
  33. bool rx_align_2;
  34. module_param(rx_align_2, bool, 0444);
  35. MODULE_PARM_DESC(rx_align_2, " align Rx buffers on 4*n+2, default - no");
  36. bool rx_large_buf;
  37. module_param(rx_large_buf, bool, 0444);
  38. MODULE_PARM_DESC(rx_large_buf, " allocate 8KB RX buffers, default - no");
  39. static inline uint wil_rx_snaplen(void)
  40. {
  41. return rx_align_2 ? 6 : 0;
  42. }
  43. static inline int wil_vring_is_empty(struct vring *vring)
  44. {
  45. return vring->swhead == vring->swtail;
  46. }
  47. static inline u32 wil_vring_next_tail(struct vring *vring)
  48. {
  49. return (vring->swtail + 1) % vring->size;
  50. }
  51. static inline void wil_vring_advance_head(struct vring *vring, int n)
  52. {
  53. vring->swhead = (vring->swhead + n) % vring->size;
  54. }
  55. static inline int wil_vring_is_full(struct vring *vring)
  56. {
  57. return wil_vring_next_tail(vring) == vring->swhead;
  58. }
  59. /* Used space in Tx Vring */
  60. static inline int wil_vring_used_tx(struct vring *vring)
  61. {
  62. u32 swhead = vring->swhead;
  63. u32 swtail = vring->swtail;
  64. return (vring->size + swhead - swtail) % vring->size;
  65. }
  66. /* Available space in Tx Vring */
  67. static inline int wil_vring_avail_tx(struct vring *vring)
  68. {
  69. return vring->size - wil_vring_used_tx(vring) - 1;
  70. }
  71. /* wil_vring_wmark_low - low watermark for available descriptor space */
  72. static inline int wil_vring_wmark_low(struct vring *vring)
  73. {
  74. return vring->size/8;
  75. }
  76. /* wil_vring_wmark_high - high watermark for available descriptor space */
  77. static inline int wil_vring_wmark_high(struct vring *vring)
  78. {
  79. return vring->size/4;
  80. }
  81. /* returns true if num avail descriptors is lower than wmark_low */
  82. static inline int wil_vring_avail_low(struct vring *vring)
  83. {
  84. return wil_vring_avail_tx(vring) < wil_vring_wmark_low(vring);
  85. }
  86. /* returns true if num avail descriptors is higher than wmark_high */
  87. static inline int wil_vring_avail_high(struct vring *vring)
  88. {
  89. return wil_vring_avail_tx(vring) > wil_vring_wmark_high(vring);
  90. }
  91. /* returns true when all tx vrings are empty */
  92. bool wil_is_tx_idle(struct wil6210_priv *wil)
  93. {
  94. int i;
  95. unsigned long data_comp_to;
  96. for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
  97. struct vring *vring = &wil->vring_tx[i];
  98. int vring_index = vring - wil->vring_tx;
  99. struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
  100. spin_lock(&txdata->lock);
  101. if (!vring->va || !txdata->enabled) {
  102. spin_unlock(&txdata->lock);
  103. continue;
  104. }
  105. data_comp_to = jiffies + msecs_to_jiffies(
  106. WIL_DATA_COMPLETION_TO_MS);
  107. if (test_bit(wil_status_napi_en, wil->status)) {
  108. while (!wil_vring_is_empty(vring)) {
  109. if (time_after(jiffies, data_comp_to)) {
  110. wil_dbg_pm(wil,
  111. "TO waiting for idle tx\n");
  112. spin_unlock(&txdata->lock);
  113. return false;
  114. }
  115. wil_dbg_ratelimited(wil,
  116. "tx vring is not empty -> NAPI\n");
  117. spin_unlock(&txdata->lock);
  118. napi_synchronize(&wil->napi_tx);
  119. msleep(20);
  120. spin_lock(&txdata->lock);
  121. if (!vring->va || !txdata->enabled)
  122. break;
  123. }
  124. }
  125. spin_unlock(&txdata->lock);
  126. }
  127. return true;
  128. }
  129. /* wil_val_in_range - check if value in [min,max) */
  130. static inline bool wil_val_in_range(int val, int min, int max)
  131. {
  132. return val >= min && val < max;
  133. }
  134. static int wil_vring_alloc(struct wil6210_priv *wil, struct vring *vring)
  135. {
  136. struct device *dev = wil_to_dev(wil);
  137. size_t sz = vring->size * sizeof(vring->va[0]);
  138. uint i;
  139. wil_dbg_misc(wil, "vring_alloc:\n");
  140. BUILD_BUG_ON(sizeof(vring->va[0]) != 32);
  141. vring->swhead = 0;
  142. vring->swtail = 0;
  143. vring->ctx = kcalloc(vring->size, sizeof(vring->ctx[0]), GFP_KERNEL);
  144. if (!vring->ctx) {
  145. vring->va = NULL;
  146. return -ENOMEM;
  147. }
  148. /* vring->va should be aligned on its size rounded up to power of 2
  149. * This is granted by the dma_alloc_coherent.
  150. *
  151. * HW has limitation that all vrings addresses must share the same
  152. * upper 16 msb bits part of 48 bits address. To workaround that,
  153. * if we are using more than 32 bit addresses switch to 32 bit
  154. * allocation before allocating vring memory.
  155. *
  156. * There's no check for the return value of dma_set_mask_and_coherent,
  157. * since we assume if we were able to set the mask during
  158. * initialization in this system it will not fail if we set it again
  159. */
  160. if (wil->dma_addr_size > 32)
  161. dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  162. vring->va = dma_alloc_coherent(dev, sz, &vring->pa, GFP_KERNEL);
  163. if (!vring->va) {
  164. kfree(vring->ctx);
  165. vring->ctx = NULL;
  166. return -ENOMEM;
  167. }
  168. if (wil->dma_addr_size > 32)
  169. dma_set_mask_and_coherent(dev,
  170. DMA_BIT_MASK(wil->dma_addr_size));
  171. /* initially, all descriptors are SW owned
  172. * For Tx and Rx, ownership bit is at the same location, thus
  173. * we can use any
  174. */
  175. for (i = 0; i < vring->size; i++) {
  176. volatile struct vring_tx_desc *_d = &vring->va[i].tx;
  177. _d->dma.status = TX_DMA_STATUS_DU;
  178. }
  179. wil_dbg_misc(wil, "vring[%d] 0x%p:%pad 0x%p\n", vring->size,
  180. vring->va, &vring->pa, vring->ctx);
  181. return 0;
  182. }
  183. static void wil_txdesc_unmap(struct device *dev, struct vring_tx_desc *d,
  184. struct wil_ctx *ctx)
  185. {
  186. dma_addr_t pa = wil_desc_addr(&d->dma.addr);
  187. u16 dmalen = le16_to_cpu(d->dma.length);
  188. switch (ctx->mapped_as) {
  189. case wil_mapped_as_single:
  190. dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE);
  191. break;
  192. case wil_mapped_as_page:
  193. dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE);
  194. break;
  195. default:
  196. break;
  197. }
  198. }
  199. static void wil_vring_free(struct wil6210_priv *wil, struct vring *vring,
  200. int tx)
  201. {
  202. struct device *dev = wil_to_dev(wil);
  203. size_t sz = vring->size * sizeof(vring->va[0]);
  204. lockdep_assert_held(&wil->mutex);
  205. if (tx) {
  206. int vring_index = vring - wil->vring_tx;
  207. wil_dbg_misc(wil, "free Tx vring %d [%d] 0x%p:%pad 0x%p\n",
  208. vring_index, vring->size, vring->va,
  209. &vring->pa, vring->ctx);
  210. } else {
  211. wil_dbg_misc(wil, "free Rx vring [%d] 0x%p:%pad 0x%p\n",
  212. vring->size, vring->va,
  213. &vring->pa, vring->ctx);
  214. }
  215. while (!wil_vring_is_empty(vring)) {
  216. dma_addr_t pa;
  217. u16 dmalen;
  218. struct wil_ctx *ctx;
  219. if (tx) {
  220. struct vring_tx_desc dd, *d = &dd;
  221. volatile struct vring_tx_desc *_d =
  222. &vring->va[vring->swtail].tx;
  223. ctx = &vring->ctx[vring->swtail];
  224. if (!ctx) {
  225. wil_dbg_txrx(wil,
  226. "ctx(%d) was already completed\n",
  227. vring->swtail);
  228. vring->swtail = wil_vring_next_tail(vring);
  229. continue;
  230. }
  231. *d = *_d;
  232. wil_txdesc_unmap(dev, d, ctx);
  233. if (ctx->skb)
  234. dev_kfree_skb_any(ctx->skb);
  235. vring->swtail = wil_vring_next_tail(vring);
  236. } else { /* rx */
  237. struct vring_rx_desc dd, *d = &dd;
  238. volatile struct vring_rx_desc *_d =
  239. &vring->va[vring->swhead].rx;
  240. ctx = &vring->ctx[vring->swhead];
  241. *d = *_d;
  242. pa = wil_desc_addr(&d->dma.addr);
  243. dmalen = le16_to_cpu(d->dma.length);
  244. dma_unmap_single(dev, pa, dmalen, DMA_FROM_DEVICE);
  245. kfree_skb(ctx->skb);
  246. wil_vring_advance_head(vring, 1);
  247. }
  248. }
  249. dma_free_coherent(dev, sz, (void *)vring->va, vring->pa);
  250. kfree(vring->ctx);
  251. vring->pa = 0;
  252. vring->va = NULL;
  253. vring->ctx = NULL;
  254. }
  255. /**
  256. * Allocate one skb for Rx VRING
  257. *
  258. * Safe to call from IRQ
  259. */
  260. static int wil_vring_alloc_skb(struct wil6210_priv *wil, struct vring *vring,
  261. u32 i, int headroom)
  262. {
  263. struct device *dev = wil_to_dev(wil);
  264. unsigned int sz = wil->rx_buf_len + ETH_HLEN + wil_rx_snaplen();
  265. struct vring_rx_desc dd, *d = &dd;
  266. volatile struct vring_rx_desc *_d = &vring->va[i].rx;
  267. dma_addr_t pa;
  268. struct sk_buff *skb = dev_alloc_skb(sz + headroom);
  269. if (unlikely(!skb))
  270. return -ENOMEM;
  271. skb_reserve(skb, headroom);
  272. skb_put(skb, sz);
  273. pa = dma_map_single(dev, skb->data, skb->len, DMA_FROM_DEVICE);
  274. if (unlikely(dma_mapping_error(dev, pa))) {
  275. kfree_skb(skb);
  276. return -ENOMEM;
  277. }
  278. d->dma.d0 = RX_DMA_D0_CMD_DMA_RT | RX_DMA_D0_CMD_DMA_IT;
  279. wil_desc_addr_set(&d->dma.addr, pa);
  280. /* ip_length don't care */
  281. /* b11 don't care */
  282. /* error don't care */
  283. d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
  284. d->dma.length = cpu_to_le16(sz);
  285. *_d = *d;
  286. vring->ctx[i].skb = skb;
  287. return 0;
  288. }
  289. /**
  290. * Adds radiotap header
  291. *
  292. * Any error indicated as "Bad FCS"
  293. *
  294. * Vendor data for 04:ce:14-1 (Wilocity-1) consists of:
  295. * - Rx descriptor: 32 bytes
  296. * - Phy info
  297. */
  298. static void wil_rx_add_radiotap_header(struct wil6210_priv *wil,
  299. struct sk_buff *skb)
  300. {
  301. struct wil6210_rtap {
  302. struct ieee80211_radiotap_header rthdr;
  303. /* fields should be in the order of bits in rthdr.it_present */
  304. /* flags */
  305. u8 flags;
  306. /* channel */
  307. __le16 chnl_freq __aligned(2);
  308. __le16 chnl_flags;
  309. /* MCS */
  310. u8 mcs_present;
  311. u8 mcs_flags;
  312. u8 mcs_index;
  313. } __packed;
  314. struct wil6210_rtap_vendor {
  315. struct wil6210_rtap rtap;
  316. /* vendor */
  317. u8 vendor_oui[3] __aligned(2);
  318. u8 vendor_ns;
  319. __le16 vendor_skip;
  320. u8 vendor_data[0];
  321. } __packed;
  322. struct vring_rx_desc *d = wil_skb_rxdesc(skb);
  323. struct wil6210_rtap_vendor *rtap_vendor;
  324. int rtap_len = sizeof(struct wil6210_rtap);
  325. int phy_length = 0; /* phy info header size, bytes */
  326. static char phy_data[128];
  327. struct ieee80211_channel *ch = wil->monitor_chandef.chan;
  328. if (rtap_include_phy_info) {
  329. rtap_len = sizeof(*rtap_vendor) + sizeof(*d);
  330. /* calculate additional length */
  331. if (d->dma.status & RX_DMA_STATUS_PHY_INFO) {
  332. /**
  333. * PHY info starts from 8-byte boundary
  334. * there are 8-byte lines, last line may be partially
  335. * written (HW bug), thus FW configures for last line
  336. * to be excessive. Driver skips this last line.
  337. */
  338. int len = min_t(int, 8 + sizeof(phy_data),
  339. wil_rxdesc_phy_length(d));
  340. if (len > 8) {
  341. void *p = skb_tail_pointer(skb);
  342. void *pa = PTR_ALIGN(p, 8);
  343. if (skb_tailroom(skb) >= len + (pa - p)) {
  344. phy_length = len - 8;
  345. memcpy(phy_data, pa, phy_length);
  346. }
  347. }
  348. }
  349. rtap_len += phy_length;
  350. }
  351. if (skb_headroom(skb) < rtap_len &&
  352. pskb_expand_head(skb, rtap_len, 0, GFP_ATOMIC)) {
  353. wil_err(wil, "Unable to expand headroom to %d\n", rtap_len);
  354. return;
  355. }
  356. rtap_vendor = skb_push(skb, rtap_len);
  357. memset(rtap_vendor, 0, rtap_len);
  358. rtap_vendor->rtap.rthdr.it_version = PKTHDR_RADIOTAP_VERSION;
  359. rtap_vendor->rtap.rthdr.it_len = cpu_to_le16(rtap_len);
  360. rtap_vendor->rtap.rthdr.it_present = cpu_to_le32(
  361. (1 << IEEE80211_RADIOTAP_FLAGS) |
  362. (1 << IEEE80211_RADIOTAP_CHANNEL) |
  363. (1 << IEEE80211_RADIOTAP_MCS));
  364. if (d->dma.status & RX_DMA_STATUS_ERROR)
  365. rtap_vendor->rtap.flags |= IEEE80211_RADIOTAP_F_BADFCS;
  366. rtap_vendor->rtap.chnl_freq = cpu_to_le16(ch ? ch->center_freq : 58320);
  367. rtap_vendor->rtap.chnl_flags = cpu_to_le16(0);
  368. rtap_vendor->rtap.mcs_present = IEEE80211_RADIOTAP_MCS_HAVE_MCS;
  369. rtap_vendor->rtap.mcs_flags = 0;
  370. rtap_vendor->rtap.mcs_index = wil_rxdesc_mcs(d);
  371. if (rtap_include_phy_info) {
  372. rtap_vendor->rtap.rthdr.it_present |= cpu_to_le32(1 <<
  373. IEEE80211_RADIOTAP_VENDOR_NAMESPACE);
  374. /* OUI for Wilocity 04:ce:14 */
  375. rtap_vendor->vendor_oui[0] = 0x04;
  376. rtap_vendor->vendor_oui[1] = 0xce;
  377. rtap_vendor->vendor_oui[2] = 0x14;
  378. rtap_vendor->vendor_ns = 1;
  379. /* Rx descriptor + PHY data */
  380. rtap_vendor->vendor_skip = cpu_to_le16(sizeof(*d) +
  381. phy_length);
  382. memcpy(rtap_vendor->vendor_data, (void *)d, sizeof(*d));
  383. memcpy(rtap_vendor->vendor_data + sizeof(*d), phy_data,
  384. phy_length);
  385. }
  386. }
  387. /* similar to ieee80211_ version, but FC contain only 1-st byte */
  388. static inline int wil_is_back_req(u8 fc)
  389. {
  390. return (fc & (IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) ==
  391. (IEEE80211_FTYPE_CTL | IEEE80211_STYPE_BACK_REQ);
  392. }
  393. bool wil_is_rx_idle(struct wil6210_priv *wil)
  394. {
  395. struct vring_rx_desc *_d;
  396. struct vring *vring = &wil->vring_rx;
  397. _d = (struct vring_rx_desc *)&vring->va[vring->swhead].rx;
  398. if (_d->dma.status & RX_DMA_STATUS_DU)
  399. return false;
  400. return true;
  401. }
  402. /**
  403. * reap 1 frame from @swhead
  404. *
  405. * Rx descriptor copied to skb->cb
  406. *
  407. * Safe to call from IRQ
  408. */
  409. static struct sk_buff *wil_vring_reap_rx(struct wil6210_priv *wil,
  410. struct vring *vring)
  411. {
  412. struct device *dev = wil_to_dev(wil);
  413. struct wil6210_vif *vif;
  414. struct net_device *ndev;
  415. volatile struct vring_rx_desc *_d;
  416. struct vring_rx_desc *d;
  417. struct sk_buff *skb;
  418. dma_addr_t pa;
  419. unsigned int snaplen = wil_rx_snaplen();
  420. unsigned int sz = wil->rx_buf_len + ETH_HLEN + snaplen;
  421. u16 dmalen;
  422. u8 ftype;
  423. int cid, mid;
  424. int i;
  425. struct wil_net_stats *stats;
  426. BUILD_BUG_ON(sizeof(struct vring_rx_desc) > sizeof(skb->cb));
  427. again:
  428. if (unlikely(wil_vring_is_empty(vring)))
  429. return NULL;
  430. i = (int)vring->swhead;
  431. _d = &vring->va[i].rx;
  432. if (unlikely(!(_d->dma.status & RX_DMA_STATUS_DU))) {
  433. /* it is not error, we just reached end of Rx done area */
  434. return NULL;
  435. }
  436. skb = vring->ctx[i].skb;
  437. vring->ctx[i].skb = NULL;
  438. wil_vring_advance_head(vring, 1);
  439. if (!skb) {
  440. wil_err(wil, "No Rx skb at [%d]\n", i);
  441. goto again;
  442. }
  443. d = wil_skb_rxdesc(skb);
  444. *d = *_d;
  445. pa = wil_desc_addr(&d->dma.addr);
  446. dma_unmap_single(dev, pa, sz, DMA_FROM_DEVICE);
  447. dmalen = le16_to_cpu(d->dma.length);
  448. trace_wil6210_rx(i, d);
  449. wil_dbg_txrx(wil, "Rx[%3d] : %d bytes\n", i, dmalen);
  450. wil_hex_dump_txrx("RxD ", DUMP_PREFIX_NONE, 32, 4,
  451. (const void *)d, sizeof(*d), false);
  452. cid = wil_rxdesc_cid(d);
  453. mid = wil_rxdesc_mid(d);
  454. vif = wil->vifs[mid];
  455. if (unlikely(!vif)) {
  456. wil_dbg_txrx(wil, "skipped RX descriptor with invalid mid %d",
  457. mid);
  458. kfree_skb(skb);
  459. goto again;
  460. }
  461. ndev = vif_to_ndev(vif);
  462. stats = &wil->sta[cid].stats;
  463. if (unlikely(dmalen > sz)) {
  464. wil_err(wil, "Rx size too large: %d bytes!\n", dmalen);
  465. stats->rx_large_frame++;
  466. kfree_skb(skb);
  467. goto again;
  468. }
  469. skb_trim(skb, dmalen);
  470. prefetch(skb->data);
  471. wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
  472. skb->data, skb_headlen(skb), false);
  473. stats->last_mcs_rx = wil_rxdesc_mcs(d);
  474. if (stats->last_mcs_rx < ARRAY_SIZE(stats->rx_per_mcs))
  475. stats->rx_per_mcs[stats->last_mcs_rx]++;
  476. /* use radiotap header only if required */
  477. if (ndev->type == ARPHRD_IEEE80211_RADIOTAP)
  478. wil_rx_add_radiotap_header(wil, skb);
  479. /* no extra checks if in sniffer mode */
  480. if (ndev->type != ARPHRD_ETHER)
  481. return skb;
  482. /* Non-data frames may be delivered through Rx DMA channel (ex: BAR)
  483. * Driver should recognize it by frame type, that is found
  484. * in Rx descriptor. If type is not data, it is 802.11 frame as is
  485. */
  486. ftype = wil_rxdesc_ftype(d) << 2;
  487. if (unlikely(ftype != IEEE80211_FTYPE_DATA)) {
  488. u8 fc1 = wil_rxdesc_fc1(d);
  489. int tid = wil_rxdesc_tid(d);
  490. u16 seq = wil_rxdesc_seq(d);
  491. wil_dbg_txrx(wil,
  492. "Non-data frame FC[7:0] 0x%02x MID %d CID %d TID %d Seq 0x%03x\n",
  493. fc1, mid, cid, tid, seq);
  494. stats->rx_non_data_frame++;
  495. if (wil_is_back_req(fc1)) {
  496. wil_dbg_txrx(wil,
  497. "BAR: MID %d CID %d TID %d Seq 0x%03x\n",
  498. mid, cid, tid, seq);
  499. wil_rx_bar(wil, vif, cid, tid, seq);
  500. } else {
  501. /* print again all info. One can enable only this
  502. * without overhead for printing every Rx frame
  503. */
  504. wil_dbg_txrx(wil,
  505. "Unhandled non-data frame FC[7:0] 0x%02x MID %d CID %d TID %d Seq 0x%03x\n",
  506. fc1, mid, cid, tid, seq);
  507. wil_hex_dump_txrx("RxD ", DUMP_PREFIX_NONE, 32, 4,
  508. (const void *)d, sizeof(*d), false);
  509. wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
  510. skb->data, skb_headlen(skb), false);
  511. }
  512. kfree_skb(skb);
  513. goto again;
  514. }
  515. if (unlikely(skb->len < ETH_HLEN + snaplen)) {
  516. wil_err(wil, "Short frame, len = %d\n", skb->len);
  517. stats->rx_short_frame++;
  518. kfree_skb(skb);
  519. goto again;
  520. }
  521. /* L4 IDENT is on when HW calculated checksum, check status
  522. * and in case of error drop the packet
  523. * higher stack layers will handle retransmission (if required)
  524. */
  525. if (likely(d->dma.status & RX_DMA_STATUS_L4I)) {
  526. /* L4 protocol identified, csum calculated */
  527. if (likely((d->dma.error & RX_DMA_ERROR_L4_ERR) == 0))
  528. skb->ip_summed = CHECKSUM_UNNECESSARY;
  529. /* If HW reports bad checksum, let IP stack re-check it
  530. * For example, HW don't understand Microsoft IP stack that
  531. * mis-calculates TCP checksum - if it should be 0x0,
  532. * it writes 0xffff in violation of RFC 1624
  533. */
  534. }
  535. if (snaplen) {
  536. /* Packet layout
  537. * +-------+-------+---------+------------+------+
  538. * | SA(6) | DA(6) | SNAP(6) | ETHTYPE(2) | DATA |
  539. * +-------+-------+---------+------------+------+
  540. * Need to remove SNAP, shifting SA and DA forward
  541. */
  542. memmove(skb->data + snaplen, skb->data, 2 * ETH_ALEN);
  543. skb_pull(skb, snaplen);
  544. }
  545. return skb;
  546. }
  547. /**
  548. * allocate and fill up to @count buffers in rx ring
  549. * buffers posted at @swtail
  550. * Note: we have a single RX queue for servicing all VIFs, but we
  551. * allocate skbs with headroom according to main interface only. This
  552. * means it will not work with monitor interface together with other VIFs.
  553. * Currently we only support monitor interface on its own without other VIFs,
  554. * and we will need to fix this code once we add support.
  555. */
  556. static int wil_rx_refill(struct wil6210_priv *wil, int count)
  557. {
  558. struct net_device *ndev = wil->main_ndev;
  559. struct vring *v = &wil->vring_rx;
  560. u32 next_tail;
  561. int rc = 0;
  562. int headroom = ndev->type == ARPHRD_IEEE80211_RADIOTAP ?
  563. WIL6210_RTAP_SIZE : 0;
  564. for (; next_tail = wil_vring_next_tail(v),
  565. (next_tail != v->swhead) && (count-- > 0);
  566. v->swtail = next_tail) {
  567. rc = wil_vring_alloc_skb(wil, v, v->swtail, headroom);
  568. if (unlikely(rc)) {
  569. wil_err(wil, "Error %d in wil_rx_refill[%d]\n",
  570. rc, v->swtail);
  571. break;
  572. }
  573. }
  574. /* make sure all writes to descriptors (shared memory) are done before
  575. * committing them to HW
  576. */
  577. wmb();
  578. wil_w(wil, v->hwtail, v->swtail);
  579. return rc;
  580. }
  581. /**
  582. * reverse_memcmp - Compare two areas of memory, in reverse order
  583. * @cs: One area of memory
  584. * @ct: Another area of memory
  585. * @count: The size of the area.
  586. *
  587. * Cut'n'paste from original memcmp (see lib/string.c)
  588. * with minimal modifications
  589. */
  590. static int reverse_memcmp(const void *cs, const void *ct, size_t count)
  591. {
  592. const unsigned char *su1, *su2;
  593. int res = 0;
  594. for (su1 = cs + count - 1, su2 = ct + count - 1; count > 0;
  595. --su1, --su2, count--) {
  596. res = *su1 - *su2;
  597. if (res)
  598. break;
  599. }
  600. return res;
  601. }
  602. static int wil_rx_crypto_check(struct wil6210_priv *wil, struct sk_buff *skb)
  603. {
  604. struct vring_rx_desc *d = wil_skb_rxdesc(skb);
  605. int cid = wil_rxdesc_cid(d);
  606. int tid = wil_rxdesc_tid(d);
  607. int key_id = wil_rxdesc_key_id(d);
  608. int mc = wil_rxdesc_mcast(d);
  609. struct wil_sta_info *s = &wil->sta[cid];
  610. struct wil_tid_crypto_rx *c = mc ? &s->group_crypto_rx :
  611. &s->tid_crypto_rx[tid];
  612. struct wil_tid_crypto_rx_single *cc = &c->key_id[key_id];
  613. const u8 *pn = (u8 *)&d->mac.pn_15_0;
  614. if (!cc->key_set) {
  615. wil_err_ratelimited(wil,
  616. "Key missing. CID %d TID %d MCast %d KEY_ID %d\n",
  617. cid, tid, mc, key_id);
  618. return -EINVAL;
  619. }
  620. if (reverse_memcmp(pn, cc->pn, IEEE80211_GCMP_PN_LEN) <= 0) {
  621. wil_err_ratelimited(wil,
  622. "Replay attack. CID %d TID %d MCast %d KEY_ID %d PN %6phN last %6phN\n",
  623. cid, tid, mc, key_id, pn, cc->pn);
  624. return -EINVAL;
  625. }
  626. memcpy(cc->pn, pn, IEEE80211_GCMP_PN_LEN);
  627. return 0;
  628. }
  629. /*
  630. * Pass Rx packet to the netif. Update statistics.
  631. * Called in softirq context (NAPI poll).
  632. */
  633. void wil_netif_rx_any(struct sk_buff *skb, struct net_device *ndev)
  634. {
  635. gro_result_t rc = GRO_NORMAL;
  636. struct wil6210_vif *vif = ndev_to_vif(ndev);
  637. struct wil6210_priv *wil = ndev_to_wil(ndev);
  638. struct wireless_dev *wdev = vif_to_wdev(vif);
  639. unsigned int len = skb->len;
  640. struct vring_rx_desc *d = wil_skb_rxdesc(skb);
  641. int cid = wil_rxdesc_cid(d); /* always 0..7, no need to check */
  642. int security = wil_rxdesc_security(d);
  643. struct ethhdr *eth = (void *)skb->data;
  644. /* here looking for DA, not A1, thus Rxdesc's 'mcast' indication
  645. * is not suitable, need to look at data
  646. */
  647. int mcast = is_multicast_ether_addr(eth->h_dest);
  648. struct wil_net_stats *stats = &wil->sta[cid].stats;
  649. struct sk_buff *xmit_skb = NULL;
  650. static const char * const gro_res_str[] = {
  651. [GRO_MERGED] = "GRO_MERGED",
  652. [GRO_MERGED_FREE] = "GRO_MERGED_FREE",
  653. [GRO_HELD] = "GRO_HELD",
  654. [GRO_NORMAL] = "GRO_NORMAL",
  655. [GRO_DROP] = "GRO_DROP",
  656. };
  657. if (ndev->features & NETIF_F_RXHASH)
  658. /* fake L4 to ensure it won't be re-calculated later
  659. * set hash to any non-zero value to activate rps
  660. * mechanism, core will be chosen according
  661. * to user-level rps configuration.
  662. */
  663. skb_set_hash(skb, 1, PKT_HASH_TYPE_L4);
  664. skb_orphan(skb);
  665. if (security && (wil_rx_crypto_check(wil, skb) != 0)) {
  666. rc = GRO_DROP;
  667. dev_kfree_skb(skb);
  668. stats->rx_replay++;
  669. goto stats;
  670. }
  671. if (wdev->iftype == NL80211_IFTYPE_AP && !vif->ap_isolate) {
  672. if (mcast) {
  673. /* send multicast frames both to higher layers in
  674. * local net stack and back to the wireless medium
  675. */
  676. xmit_skb = skb_copy(skb, GFP_ATOMIC);
  677. } else {
  678. int xmit_cid = wil_find_cid(wil, vif->mid,
  679. eth->h_dest);
  680. if (xmit_cid >= 0) {
  681. /* The destination station is associated to
  682. * this AP (in this VLAN), so send the frame
  683. * directly to it and do not pass it to local
  684. * net stack.
  685. */
  686. xmit_skb = skb;
  687. skb = NULL;
  688. }
  689. }
  690. }
  691. if (xmit_skb) {
  692. /* Send to wireless media and increase priority by 256 to
  693. * keep the received priority instead of reclassifying
  694. * the frame (see cfg80211_classify8021d).
  695. */
  696. xmit_skb->dev = ndev;
  697. xmit_skb->priority += 256;
  698. xmit_skb->protocol = htons(ETH_P_802_3);
  699. skb_reset_network_header(xmit_skb);
  700. skb_reset_mac_header(xmit_skb);
  701. wil_dbg_txrx(wil, "Rx -> Tx %d bytes\n", len);
  702. dev_queue_xmit(xmit_skb);
  703. }
  704. if (skb) { /* deliver to local stack */
  705. skb->protocol = eth_type_trans(skb, ndev);
  706. skb->dev = ndev;
  707. rc = napi_gro_receive(&wil->napi_rx, skb);
  708. wil_dbg_txrx(wil, "Rx complete %d bytes => %s\n",
  709. len, gro_res_str[rc]);
  710. }
  711. stats:
  712. /* statistics. rc set to GRO_NORMAL for AP bridging */
  713. if (unlikely(rc == GRO_DROP)) {
  714. ndev->stats.rx_dropped++;
  715. stats->rx_dropped++;
  716. wil_dbg_txrx(wil, "Rx drop %d bytes\n", len);
  717. } else {
  718. ndev->stats.rx_packets++;
  719. stats->rx_packets++;
  720. ndev->stats.rx_bytes += len;
  721. stats->rx_bytes += len;
  722. if (mcast)
  723. ndev->stats.multicast++;
  724. }
  725. }
  726. /**
  727. * Proceed all completed skb's from Rx VRING
  728. *
  729. * Safe to call from NAPI poll, i.e. softirq with interrupts enabled
  730. */
  731. void wil_rx_handle(struct wil6210_priv *wil, int *quota)
  732. {
  733. struct net_device *ndev = wil->main_ndev;
  734. struct wireless_dev *wdev = ndev->ieee80211_ptr;
  735. struct vring *v = &wil->vring_rx;
  736. struct sk_buff *skb;
  737. if (unlikely(!v->va)) {
  738. wil_err(wil, "Rx IRQ while Rx not yet initialized\n");
  739. return;
  740. }
  741. wil_dbg_txrx(wil, "rx_handle\n");
  742. while ((*quota > 0) && (NULL != (skb = wil_vring_reap_rx(wil, v)))) {
  743. (*quota)--;
  744. /* monitor is currently supported on main interface only */
  745. if (wdev->iftype == NL80211_IFTYPE_MONITOR) {
  746. skb->dev = ndev;
  747. skb_reset_mac_header(skb);
  748. skb->ip_summed = CHECKSUM_UNNECESSARY;
  749. skb->pkt_type = PACKET_OTHERHOST;
  750. skb->protocol = htons(ETH_P_802_2);
  751. wil_netif_rx_any(skb, ndev);
  752. } else {
  753. wil_rx_reorder(wil, skb);
  754. }
  755. }
  756. wil_rx_refill(wil, v->size);
  757. }
  758. static void wil_rx_buf_len_init(struct wil6210_priv *wil)
  759. {
  760. wil->rx_buf_len = rx_large_buf ?
  761. WIL_MAX_ETH_MTU : TXRX_BUF_LEN_DEFAULT - WIL_MAX_MPDU_OVERHEAD;
  762. if (mtu_max > wil->rx_buf_len) {
  763. /* do not allow RX buffers to be smaller than mtu_max, for
  764. * backward compatibility (mtu_max parameter was also used
  765. * to support receiving large packets)
  766. */
  767. wil_info(wil, "Override RX buffer to mtu_max(%d)\n", mtu_max);
  768. wil->rx_buf_len = mtu_max;
  769. }
  770. }
  771. int wil_rx_init(struct wil6210_priv *wil, u16 size)
  772. {
  773. struct vring *vring = &wil->vring_rx;
  774. int rc;
  775. wil_dbg_misc(wil, "rx_init\n");
  776. if (vring->va) {
  777. wil_err(wil, "Rx ring already allocated\n");
  778. return -EINVAL;
  779. }
  780. wil_rx_buf_len_init(wil);
  781. vring->size = size;
  782. rc = wil_vring_alloc(wil, vring);
  783. if (rc)
  784. return rc;
  785. rc = wmi_rx_chain_add(wil, vring);
  786. if (rc)
  787. goto err_free;
  788. rc = wil_rx_refill(wil, vring->size);
  789. if (rc)
  790. goto err_free;
  791. return 0;
  792. err_free:
  793. wil_vring_free(wil, vring, 0);
  794. return rc;
  795. }
  796. void wil_rx_fini(struct wil6210_priv *wil)
  797. {
  798. struct vring *vring = &wil->vring_rx;
  799. wil_dbg_misc(wil, "rx_fini\n");
  800. if (vring->va)
  801. wil_vring_free(wil, vring, 0);
  802. }
  803. static inline void wil_tx_data_init(struct vring_tx_data *txdata)
  804. {
  805. spin_lock_bh(&txdata->lock);
  806. txdata->dot1x_open = 0;
  807. txdata->enabled = 0;
  808. txdata->idle = 0;
  809. txdata->last_idle = 0;
  810. txdata->begin = 0;
  811. txdata->agg_wsize = 0;
  812. txdata->agg_timeout = 0;
  813. txdata->agg_amsdu = 0;
  814. txdata->addba_in_progress = false;
  815. txdata->mid = U8_MAX;
  816. spin_unlock_bh(&txdata->lock);
  817. }
  818. int wil_vring_init_tx(struct wil6210_vif *vif, int id, int size,
  819. int cid, int tid)
  820. {
  821. struct wil6210_priv *wil = vif_to_wil(vif);
  822. int rc;
  823. struct wmi_vring_cfg_cmd cmd = {
  824. .action = cpu_to_le32(WMI_VRING_CMD_ADD),
  825. .vring_cfg = {
  826. .tx_sw_ring = {
  827. .max_mpdu_size =
  828. cpu_to_le16(wil_mtu2macbuf(mtu_max)),
  829. .ring_size = cpu_to_le16(size),
  830. },
  831. .ringid = id,
  832. .cidxtid = mk_cidxtid(cid, tid),
  833. .encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
  834. .mac_ctrl = 0,
  835. .to_resolution = 0,
  836. .agg_max_wsize = 0,
  837. .schd_params = {
  838. .priority = cpu_to_le16(0),
  839. .timeslot_us = cpu_to_le16(0xfff),
  840. },
  841. },
  842. };
  843. struct {
  844. struct wmi_cmd_hdr wmi;
  845. struct wmi_vring_cfg_done_event cmd;
  846. } __packed reply;
  847. struct vring *vring = &wil->vring_tx[id];
  848. struct vring_tx_data *txdata = &wil->vring_tx_data[id];
  849. wil_dbg_misc(wil, "vring_init_tx: max_mpdu_size %d\n",
  850. cmd.vring_cfg.tx_sw_ring.max_mpdu_size);
  851. lockdep_assert_held(&wil->mutex);
  852. if (vring->va) {
  853. wil_err(wil, "Tx ring [%d] already allocated\n", id);
  854. rc = -EINVAL;
  855. goto out;
  856. }
  857. wil_tx_data_init(txdata);
  858. vring->size = size;
  859. rc = wil_vring_alloc(wil, vring);
  860. if (rc)
  861. goto out;
  862. wil->vring2cid_tid[id][0] = cid;
  863. wil->vring2cid_tid[id][1] = tid;
  864. cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
  865. if (!vif->privacy)
  866. txdata->dot1x_open = true;
  867. rc = wmi_call(wil, WMI_VRING_CFG_CMDID, vif->mid, &cmd, sizeof(cmd),
  868. WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100);
  869. if (rc)
  870. goto out_free;
  871. if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) {
  872. wil_err(wil, "Tx config failed, status 0x%02x\n",
  873. reply.cmd.status);
  874. rc = -EINVAL;
  875. goto out_free;
  876. }
  877. spin_lock_bh(&txdata->lock);
  878. vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr);
  879. txdata->mid = vif->mid;
  880. txdata->enabled = 1;
  881. spin_unlock_bh(&txdata->lock);
  882. if (txdata->dot1x_open && (agg_wsize >= 0))
  883. wil_addba_tx_request(wil, id, agg_wsize);
  884. return 0;
  885. out_free:
  886. spin_lock_bh(&txdata->lock);
  887. txdata->dot1x_open = false;
  888. txdata->enabled = 0;
  889. spin_unlock_bh(&txdata->lock);
  890. wil_vring_free(wil, vring, 1);
  891. wil->vring2cid_tid[id][0] = WIL6210_MAX_CID;
  892. wil->vring2cid_tid[id][1] = 0;
  893. out:
  894. return rc;
  895. }
  896. int wil_vring_init_bcast(struct wil6210_vif *vif, int id, int size)
  897. {
  898. struct wil6210_priv *wil = vif_to_wil(vif);
  899. int rc;
  900. struct wmi_bcast_vring_cfg_cmd cmd = {
  901. .action = cpu_to_le32(WMI_VRING_CMD_ADD),
  902. .vring_cfg = {
  903. .tx_sw_ring = {
  904. .max_mpdu_size =
  905. cpu_to_le16(wil_mtu2macbuf(mtu_max)),
  906. .ring_size = cpu_to_le16(size),
  907. },
  908. .ringid = id,
  909. .encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
  910. },
  911. };
  912. struct {
  913. struct wmi_cmd_hdr wmi;
  914. struct wmi_vring_cfg_done_event cmd;
  915. } __packed reply;
  916. struct vring *vring = &wil->vring_tx[id];
  917. struct vring_tx_data *txdata = &wil->vring_tx_data[id];
  918. wil_dbg_misc(wil, "vring_init_bcast: max_mpdu_size %d\n",
  919. cmd.vring_cfg.tx_sw_ring.max_mpdu_size);
  920. lockdep_assert_held(&wil->mutex);
  921. if (vring->va) {
  922. wil_err(wil, "Tx ring [%d] already allocated\n", id);
  923. rc = -EINVAL;
  924. goto out;
  925. }
  926. wil_tx_data_init(txdata);
  927. vring->size = size;
  928. rc = wil_vring_alloc(wil, vring);
  929. if (rc)
  930. goto out;
  931. wil->vring2cid_tid[id][0] = WIL6210_MAX_CID; /* CID */
  932. wil->vring2cid_tid[id][1] = 0; /* TID */
  933. cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
  934. if (!vif->privacy)
  935. txdata->dot1x_open = true;
  936. rc = wmi_call(wil, WMI_BCAST_VRING_CFG_CMDID, vif->mid,
  937. &cmd, sizeof(cmd),
  938. WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100);
  939. if (rc)
  940. goto out_free;
  941. if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) {
  942. wil_err(wil, "Tx config failed, status 0x%02x\n",
  943. reply.cmd.status);
  944. rc = -EINVAL;
  945. goto out_free;
  946. }
  947. spin_lock_bh(&txdata->lock);
  948. vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr);
  949. txdata->mid = vif->mid;
  950. txdata->enabled = 1;
  951. spin_unlock_bh(&txdata->lock);
  952. return 0;
  953. out_free:
  954. spin_lock_bh(&txdata->lock);
  955. txdata->enabled = 0;
  956. txdata->dot1x_open = false;
  957. spin_unlock_bh(&txdata->lock);
  958. wil_vring_free(wil, vring, 1);
  959. out:
  960. return rc;
  961. }
  962. void wil_vring_fini_tx(struct wil6210_priv *wil, int id)
  963. {
  964. struct vring *vring = &wil->vring_tx[id];
  965. struct vring_tx_data *txdata = &wil->vring_tx_data[id];
  966. lockdep_assert_held(&wil->mutex);
  967. if (!vring->va)
  968. return;
  969. wil_dbg_misc(wil, "vring_fini_tx: id=%d\n", id);
  970. spin_lock_bh(&txdata->lock);
  971. txdata->dot1x_open = false;
  972. txdata->mid = U8_MAX;
  973. txdata->enabled = 0; /* no Tx can be in progress or start anew */
  974. spin_unlock_bh(&txdata->lock);
  975. /* napi_synchronize waits for completion of the current NAPI but will
  976. * not prevent the next NAPI run.
  977. * Add a memory barrier to guarantee that txdata->enabled is zeroed
  978. * before napi_synchronize so that the next scheduled NAPI will not
  979. * handle this vring
  980. */
  981. wmb();
  982. /* make sure NAPI won't touch this vring */
  983. if (test_bit(wil_status_napi_en, wil->status))
  984. napi_synchronize(&wil->napi_tx);
  985. wil_vring_free(wil, vring, 1);
  986. }
  987. static struct vring *wil_find_tx_ucast(struct wil6210_priv *wil,
  988. struct wil6210_vif *vif,
  989. struct sk_buff *skb)
  990. {
  991. int i;
  992. struct ethhdr *eth = (void *)skb->data;
  993. int cid = wil_find_cid(wil, vif->mid, eth->h_dest);
  994. if (cid < 0)
  995. return NULL;
  996. /* TODO: fix for multiple TID */
  997. for (i = 0; i < ARRAY_SIZE(wil->vring2cid_tid); i++) {
  998. if (!wil->vring_tx_data[i].dot1x_open &&
  999. (skb->protocol != cpu_to_be16(ETH_P_PAE)))
  1000. continue;
  1001. if (wil->vring2cid_tid[i][0] == cid) {
  1002. struct vring *v = &wil->vring_tx[i];
  1003. struct vring_tx_data *txdata = &wil->vring_tx_data[i];
  1004. wil_dbg_txrx(wil, "find_tx_ucast: (%pM) -> [%d]\n",
  1005. eth->h_dest, i);
  1006. if (v->va && txdata->enabled) {
  1007. return v;
  1008. } else {
  1009. wil_dbg_txrx(wil,
  1010. "find_tx_ucast: vring[%d] not valid\n",
  1011. i);
  1012. return NULL;
  1013. }
  1014. }
  1015. }
  1016. return NULL;
  1017. }
  1018. static int wil_tx_vring(struct wil6210_priv *wil, struct wil6210_vif *vif,
  1019. struct vring *vring, struct sk_buff *skb);
  1020. static struct vring *wil_find_tx_vring_sta(struct wil6210_priv *wil,
  1021. struct wil6210_vif *vif,
  1022. struct sk_buff *skb)
  1023. {
  1024. struct vring *v;
  1025. int i;
  1026. u8 cid;
  1027. struct vring_tx_data *txdata;
  1028. /* In the STA mode, it is expected to have only 1 VRING
  1029. * for the AP we connected to.
  1030. * find 1-st vring eligible for this skb and use it.
  1031. */
  1032. for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
  1033. v = &wil->vring_tx[i];
  1034. txdata = &wil->vring_tx_data[i];
  1035. if (!v->va || !txdata->enabled || txdata->mid != vif->mid)
  1036. continue;
  1037. cid = wil->vring2cid_tid[i][0];
  1038. if (cid >= WIL6210_MAX_CID) /* skip BCAST */
  1039. continue;
  1040. if (!wil->vring_tx_data[i].dot1x_open &&
  1041. (skb->protocol != cpu_to_be16(ETH_P_PAE)))
  1042. continue;
  1043. wil_dbg_txrx(wil, "Tx -> ring %d\n", i);
  1044. return v;
  1045. }
  1046. wil_dbg_txrx(wil, "Tx while no vrings active?\n");
  1047. return NULL;
  1048. }
  1049. /* Use one of 2 strategies:
  1050. *
  1051. * 1. New (real broadcast):
  1052. * use dedicated broadcast vring
  1053. * 2. Old (pseudo-DMS):
  1054. * Find 1-st vring and return it;
  1055. * duplicate skb and send it to other active vrings;
  1056. * in all cases override dest address to unicast peer's address
  1057. * Use old strategy when new is not supported yet:
  1058. * - for PBSS
  1059. */
  1060. static struct vring *wil_find_tx_bcast_1(struct wil6210_priv *wil,
  1061. struct wil6210_vif *vif,
  1062. struct sk_buff *skb)
  1063. {
  1064. struct vring *v;
  1065. struct vring_tx_data *txdata;
  1066. int i = vif->bcast_vring;
  1067. if (i < 0)
  1068. return NULL;
  1069. v = &wil->vring_tx[i];
  1070. txdata = &wil->vring_tx_data[i];
  1071. if (!v->va || !txdata->enabled)
  1072. return NULL;
  1073. if (!wil->vring_tx_data[i].dot1x_open &&
  1074. (skb->protocol != cpu_to_be16(ETH_P_PAE)))
  1075. return NULL;
  1076. return v;
  1077. }
  1078. static void wil_set_da_for_vring(struct wil6210_priv *wil,
  1079. struct sk_buff *skb, int vring_index)
  1080. {
  1081. struct ethhdr *eth = (void *)skb->data;
  1082. int cid = wil->vring2cid_tid[vring_index][0];
  1083. ether_addr_copy(eth->h_dest, wil->sta[cid].addr);
  1084. }
  1085. static struct vring *wil_find_tx_bcast_2(struct wil6210_priv *wil,
  1086. struct wil6210_vif *vif,
  1087. struct sk_buff *skb)
  1088. {
  1089. struct vring *v, *v2;
  1090. struct sk_buff *skb2;
  1091. int i;
  1092. u8 cid;
  1093. struct ethhdr *eth = (void *)skb->data;
  1094. char *src = eth->h_source;
  1095. struct vring_tx_data *txdata, *txdata2;
  1096. /* find 1-st vring eligible for data */
  1097. for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
  1098. v = &wil->vring_tx[i];
  1099. txdata = &wil->vring_tx_data[i];
  1100. if (!v->va || !txdata->enabled || txdata->mid != vif->mid)
  1101. continue;
  1102. cid = wil->vring2cid_tid[i][0];
  1103. if (cid >= WIL6210_MAX_CID) /* skip BCAST */
  1104. continue;
  1105. if (!wil->vring_tx_data[i].dot1x_open &&
  1106. (skb->protocol != cpu_to_be16(ETH_P_PAE)))
  1107. continue;
  1108. /* don't Tx back to source when re-routing Rx->Tx at the AP */
  1109. if (0 == memcmp(wil->sta[cid].addr, src, ETH_ALEN))
  1110. continue;
  1111. goto found;
  1112. }
  1113. wil_dbg_txrx(wil, "Tx while no vrings active?\n");
  1114. return NULL;
  1115. found:
  1116. wil_dbg_txrx(wil, "BCAST -> ring %d\n", i);
  1117. wil_set_da_for_vring(wil, skb, i);
  1118. /* find other active vrings and duplicate skb for each */
  1119. for (i++; i < WIL6210_MAX_TX_RINGS; i++) {
  1120. v2 = &wil->vring_tx[i];
  1121. txdata2 = &wil->vring_tx_data[i];
  1122. if (!v2->va || txdata2->mid != vif->mid)
  1123. continue;
  1124. cid = wil->vring2cid_tid[i][0];
  1125. if (cid >= WIL6210_MAX_CID) /* skip BCAST */
  1126. continue;
  1127. if (!wil->vring_tx_data[i].dot1x_open &&
  1128. (skb->protocol != cpu_to_be16(ETH_P_PAE)))
  1129. continue;
  1130. if (0 == memcmp(wil->sta[cid].addr, src, ETH_ALEN))
  1131. continue;
  1132. skb2 = skb_copy(skb, GFP_ATOMIC);
  1133. if (skb2) {
  1134. wil_dbg_txrx(wil, "BCAST DUP -> ring %d\n", i);
  1135. wil_set_da_for_vring(wil, skb2, i);
  1136. wil_tx_vring(wil, vif, v2, skb2);
  1137. } else {
  1138. wil_err(wil, "skb_copy failed\n");
  1139. }
  1140. }
  1141. return v;
  1142. }
  1143. static int wil_tx_desc_map(struct vring_tx_desc *d, dma_addr_t pa, u32 len,
  1144. int vring_index)
  1145. {
  1146. wil_desc_addr_set(&d->dma.addr, pa);
  1147. d->dma.ip_length = 0;
  1148. /* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/
  1149. d->dma.b11 = 0/*14 | BIT(7)*/;
  1150. d->dma.error = 0;
  1151. d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
  1152. d->dma.length = cpu_to_le16((u16)len);
  1153. d->dma.d0 = (vring_index << DMA_CFG_DESC_TX_0_QID_POS);
  1154. d->mac.d[0] = 0;
  1155. d->mac.d[1] = 0;
  1156. d->mac.d[2] = 0;
  1157. d->mac.ucode_cmd = 0;
  1158. /* translation type: 0 - bypass; 1 - 802.3; 2 - native wifi */
  1159. d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) |
  1160. (1 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS);
  1161. return 0;
  1162. }
  1163. static inline
  1164. void wil_tx_desc_set_nr_frags(struct vring_tx_desc *d, int nr_frags)
  1165. {
  1166. d->mac.d[2] |= (nr_frags << MAC_CFG_DESC_TX_2_NUM_OF_DESCRIPTORS_POS);
  1167. }
  1168. /**
  1169. * Sets the descriptor @d up for csum and/or TSO offloading. The corresponding
  1170. * @skb is used to obtain the protocol and headers length.
  1171. * @tso_desc_type is a descriptor type for TSO: 0 - a header, 1 - first data,
  1172. * 2 - middle, 3 - last descriptor.
  1173. */
  1174. static void wil_tx_desc_offload_setup_tso(struct vring_tx_desc *d,
  1175. struct sk_buff *skb,
  1176. int tso_desc_type, bool is_ipv4,
  1177. int tcp_hdr_len, int skb_net_hdr_len)
  1178. {
  1179. d->dma.b11 = ETH_HLEN; /* MAC header length */
  1180. d->dma.b11 |= is_ipv4 << DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS;
  1181. d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS);
  1182. /* L4 header len: TCP header length */
  1183. d->dma.d0 |= (tcp_hdr_len & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
  1184. /* Setup TSO: bit and desc type */
  1185. d->dma.d0 |= (BIT(DMA_CFG_DESC_TX_0_TCP_SEG_EN_POS)) |
  1186. (tso_desc_type << DMA_CFG_DESC_TX_0_SEGMENT_BUF_DETAILS_POS);
  1187. d->dma.d0 |= (is_ipv4 << DMA_CFG_DESC_TX_0_IPV4_CHECKSUM_EN_POS);
  1188. d->dma.ip_length = skb_net_hdr_len;
  1189. /* Enable TCP/UDP checksum */
  1190. d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS);
  1191. /* Calculate pseudo-header */
  1192. d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS);
  1193. }
  1194. /**
  1195. * Sets the descriptor @d up for csum. The corresponding
  1196. * @skb is used to obtain the protocol and headers length.
  1197. * Returns the protocol: 0 - not TCP, 1 - TCPv4, 2 - TCPv6.
  1198. * Note, if d==NULL, the function only returns the protocol result.
  1199. *
  1200. * It is very similar to previous wil_tx_desc_offload_setup_tso. This
  1201. * is "if unrolling" to optimize the critical path.
  1202. */
  1203. static int wil_tx_desc_offload_setup(struct vring_tx_desc *d,
  1204. struct sk_buff *skb){
  1205. int protocol;
  1206. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1207. return 0;
  1208. d->dma.b11 = ETH_HLEN; /* MAC header length */
  1209. switch (skb->protocol) {
  1210. case cpu_to_be16(ETH_P_IP):
  1211. protocol = ip_hdr(skb)->protocol;
  1212. d->dma.b11 |= BIT(DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS);
  1213. break;
  1214. case cpu_to_be16(ETH_P_IPV6):
  1215. protocol = ipv6_hdr(skb)->nexthdr;
  1216. break;
  1217. default:
  1218. return -EINVAL;
  1219. }
  1220. switch (protocol) {
  1221. case IPPROTO_TCP:
  1222. d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS);
  1223. /* L4 header len: TCP header length */
  1224. d->dma.d0 |=
  1225. (tcp_hdrlen(skb) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
  1226. break;
  1227. case IPPROTO_UDP:
  1228. /* L4 header len: UDP header length */
  1229. d->dma.d0 |=
  1230. (sizeof(struct udphdr) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
  1231. break;
  1232. default:
  1233. return -EINVAL;
  1234. }
  1235. d->dma.ip_length = skb_network_header_len(skb);
  1236. /* Enable TCP/UDP checksum */
  1237. d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS);
  1238. /* Calculate pseudo-header */
  1239. d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS);
  1240. return 0;
  1241. }
  1242. static inline void wil_tx_last_desc(struct vring_tx_desc *d)
  1243. {
  1244. d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS) |
  1245. BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS) |
  1246. BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
  1247. }
  1248. static inline void wil_set_tx_desc_last_tso(volatile struct vring_tx_desc *d)
  1249. {
  1250. d->dma.d0 |= wil_tso_type_lst <<
  1251. DMA_CFG_DESC_TX_0_SEGMENT_BUF_DETAILS_POS;
  1252. }
  1253. static int __wil_tx_vring_tso(struct wil6210_priv *wil, struct wil6210_vif *vif,
  1254. struct vring *vring, struct sk_buff *skb)
  1255. {
  1256. struct device *dev = wil_to_dev(wil);
  1257. /* point to descriptors in shared memory */
  1258. volatile struct vring_tx_desc *_desc = NULL, *_hdr_desc,
  1259. *_first_desc = NULL;
  1260. /* pointers to shadow descriptors */
  1261. struct vring_tx_desc desc_mem, hdr_desc_mem, first_desc_mem,
  1262. *d = &hdr_desc_mem, *hdr_desc = &hdr_desc_mem,
  1263. *first_desc = &first_desc_mem;
  1264. /* pointer to shadow descriptors' context */
  1265. struct wil_ctx *hdr_ctx, *first_ctx = NULL;
  1266. int descs_used = 0; /* total number of used descriptors */
  1267. int sg_desc_cnt = 0; /* number of descriptors for current mss*/
  1268. u32 swhead = vring->swhead;
  1269. int used, avail = wil_vring_avail_tx(vring);
  1270. int nr_frags = skb_shinfo(skb)->nr_frags;
  1271. int min_desc_required = nr_frags + 1;
  1272. int mss = skb_shinfo(skb)->gso_size; /* payload size w/o headers */
  1273. int f, len, hdrlen, headlen;
  1274. int vring_index = vring - wil->vring_tx;
  1275. struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
  1276. uint i = swhead;
  1277. dma_addr_t pa;
  1278. const skb_frag_t *frag = NULL;
  1279. int rem_data = mss;
  1280. int lenmss;
  1281. int hdr_compensation_need = true;
  1282. int desc_tso_type = wil_tso_type_first;
  1283. bool is_ipv4;
  1284. int tcp_hdr_len;
  1285. int skb_net_hdr_len;
  1286. int gso_type;
  1287. int rc = -EINVAL;
  1288. wil_dbg_txrx(wil, "tx_vring_tso: %d bytes to vring %d\n", skb->len,
  1289. vring_index);
  1290. if (unlikely(!txdata->enabled))
  1291. return -EINVAL;
  1292. /* A typical page 4K is 3-4 payloads, we assume each fragment
  1293. * is a full payload, that's how min_desc_required has been
  1294. * calculated. In real we might need more or less descriptors,
  1295. * this is the initial check only.
  1296. */
  1297. if (unlikely(avail < min_desc_required)) {
  1298. wil_err_ratelimited(wil,
  1299. "TSO: Tx ring[%2d] full. No space for %d fragments\n",
  1300. vring_index, min_desc_required);
  1301. return -ENOMEM;
  1302. }
  1303. /* Header Length = MAC header len + IP header len + TCP header len*/
  1304. hdrlen = ETH_HLEN +
  1305. (int)skb_network_header_len(skb) +
  1306. tcp_hdrlen(skb);
  1307. gso_type = skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV6 | SKB_GSO_TCPV4);
  1308. switch (gso_type) {
  1309. case SKB_GSO_TCPV4:
  1310. /* TCP v4, zero out the IP length and IPv4 checksum fields
  1311. * as required by the offloading doc
  1312. */
  1313. ip_hdr(skb)->tot_len = 0;
  1314. ip_hdr(skb)->check = 0;
  1315. is_ipv4 = true;
  1316. break;
  1317. case SKB_GSO_TCPV6:
  1318. /* TCP v6, zero out the payload length */
  1319. ipv6_hdr(skb)->payload_len = 0;
  1320. is_ipv4 = false;
  1321. break;
  1322. default:
  1323. /* other than TCPv4 or TCPv6 types are not supported for TSO.
  1324. * It is also illegal for both to be set simultaneously
  1325. */
  1326. return -EINVAL;
  1327. }
  1328. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1329. return -EINVAL;
  1330. /* tcp header length and skb network header length are fixed for all
  1331. * packet's descriptors - read then once here
  1332. */
  1333. tcp_hdr_len = tcp_hdrlen(skb);
  1334. skb_net_hdr_len = skb_network_header_len(skb);
  1335. _hdr_desc = &vring->va[i].tx;
  1336. pa = dma_map_single(dev, skb->data, hdrlen, DMA_TO_DEVICE);
  1337. if (unlikely(dma_mapping_error(dev, pa))) {
  1338. wil_err(wil, "TSO: Skb head DMA map error\n");
  1339. goto err_exit;
  1340. }
  1341. wil_tx_desc_map(hdr_desc, pa, hdrlen, vring_index);
  1342. wil_tx_desc_offload_setup_tso(hdr_desc, skb, wil_tso_type_hdr, is_ipv4,
  1343. tcp_hdr_len, skb_net_hdr_len);
  1344. wil_tx_last_desc(hdr_desc);
  1345. vring->ctx[i].mapped_as = wil_mapped_as_single;
  1346. hdr_ctx = &vring->ctx[i];
  1347. descs_used++;
  1348. headlen = skb_headlen(skb) - hdrlen;
  1349. for (f = headlen ? -1 : 0; f < nr_frags; f++) {
  1350. if (headlen) {
  1351. len = headlen;
  1352. wil_dbg_txrx(wil, "TSO: process skb head, len %u\n",
  1353. len);
  1354. } else {
  1355. frag = &skb_shinfo(skb)->frags[f];
  1356. len = frag->size;
  1357. wil_dbg_txrx(wil, "TSO: frag[%d]: len %u\n", f, len);
  1358. }
  1359. while (len) {
  1360. wil_dbg_txrx(wil,
  1361. "TSO: len %d, rem_data %d, descs_used %d\n",
  1362. len, rem_data, descs_used);
  1363. if (descs_used == avail) {
  1364. wil_err_ratelimited(wil, "TSO: ring overflow\n");
  1365. rc = -ENOMEM;
  1366. goto mem_error;
  1367. }
  1368. lenmss = min_t(int, rem_data, len);
  1369. i = (swhead + descs_used) % vring->size;
  1370. wil_dbg_txrx(wil, "TSO: lenmss %d, i %d\n", lenmss, i);
  1371. if (!headlen) {
  1372. pa = skb_frag_dma_map(dev, frag,
  1373. frag->size - len, lenmss,
  1374. DMA_TO_DEVICE);
  1375. vring->ctx[i].mapped_as = wil_mapped_as_page;
  1376. } else {
  1377. pa = dma_map_single(dev,
  1378. skb->data +
  1379. skb_headlen(skb) - headlen,
  1380. lenmss,
  1381. DMA_TO_DEVICE);
  1382. vring->ctx[i].mapped_as = wil_mapped_as_single;
  1383. headlen -= lenmss;
  1384. }
  1385. if (unlikely(dma_mapping_error(dev, pa))) {
  1386. wil_err(wil, "TSO: DMA map page error\n");
  1387. goto mem_error;
  1388. }
  1389. _desc = &vring->va[i].tx;
  1390. if (!_first_desc) {
  1391. _first_desc = _desc;
  1392. first_ctx = &vring->ctx[i];
  1393. d = first_desc;
  1394. } else {
  1395. d = &desc_mem;
  1396. }
  1397. wil_tx_desc_map(d, pa, lenmss, vring_index);
  1398. wil_tx_desc_offload_setup_tso(d, skb, desc_tso_type,
  1399. is_ipv4, tcp_hdr_len,
  1400. skb_net_hdr_len);
  1401. /* use tso_type_first only once */
  1402. desc_tso_type = wil_tso_type_mid;
  1403. descs_used++; /* desc used so far */
  1404. sg_desc_cnt++; /* desc used for this segment */
  1405. len -= lenmss;
  1406. rem_data -= lenmss;
  1407. wil_dbg_txrx(wil,
  1408. "TSO: len %d, rem_data %d, descs_used %d, sg_desc_cnt %d,\n",
  1409. len, rem_data, descs_used, sg_desc_cnt);
  1410. /* Close the segment if reached mss size or last frag*/
  1411. if (rem_data == 0 || (f == nr_frags - 1 && len == 0)) {
  1412. if (hdr_compensation_need) {
  1413. /* first segment include hdr desc for
  1414. * release
  1415. */
  1416. hdr_ctx->nr_frags = sg_desc_cnt;
  1417. wil_tx_desc_set_nr_frags(first_desc,
  1418. sg_desc_cnt +
  1419. 1);
  1420. hdr_compensation_need = false;
  1421. } else {
  1422. wil_tx_desc_set_nr_frags(first_desc,
  1423. sg_desc_cnt);
  1424. }
  1425. first_ctx->nr_frags = sg_desc_cnt - 1;
  1426. wil_tx_last_desc(d);
  1427. /* first descriptor may also be the last
  1428. * for this mss - make sure not to copy
  1429. * it twice
  1430. */
  1431. if (first_desc != d)
  1432. *_first_desc = *first_desc;
  1433. /*last descriptor will be copied at the end
  1434. * of this TS processing
  1435. */
  1436. if (f < nr_frags - 1 || len > 0)
  1437. *_desc = *d;
  1438. rem_data = mss;
  1439. _first_desc = NULL;
  1440. sg_desc_cnt = 0;
  1441. } else if (first_desc != d) /* update mid descriptor */
  1442. *_desc = *d;
  1443. }
  1444. }
  1445. /* first descriptor may also be the last.
  1446. * in this case d pointer is invalid
  1447. */
  1448. if (_first_desc == _desc)
  1449. d = first_desc;
  1450. /* Last data descriptor */
  1451. wil_set_tx_desc_last_tso(d);
  1452. *_desc = *d;
  1453. /* Fill the total number of descriptors in first desc (hdr)*/
  1454. wil_tx_desc_set_nr_frags(hdr_desc, descs_used);
  1455. *_hdr_desc = *hdr_desc;
  1456. /* hold reference to skb
  1457. * to prevent skb release before accounting
  1458. * in case of immediate "tx done"
  1459. */
  1460. vring->ctx[i].skb = skb_get(skb);
  1461. /* performance monitoring */
  1462. used = wil_vring_used_tx(vring);
  1463. if (wil_val_in_range(wil->vring_idle_trsh,
  1464. used, used + descs_used)) {
  1465. txdata->idle += get_cycles() - txdata->last_idle;
  1466. wil_dbg_txrx(wil, "Ring[%2d] not idle %d -> %d\n",
  1467. vring_index, used, used + descs_used);
  1468. }
  1469. /* Make sure to advance the head only after descriptor update is done.
  1470. * This will prevent a race condition where the completion thread
  1471. * will see the DU bit set from previous run and will handle the
  1472. * skb before it was completed.
  1473. */
  1474. wmb();
  1475. /* advance swhead */
  1476. wil_vring_advance_head(vring, descs_used);
  1477. wil_dbg_txrx(wil, "TSO: Tx swhead %d -> %d\n", swhead, vring->swhead);
  1478. /* make sure all writes to descriptors (shared memory) are done before
  1479. * committing them to HW
  1480. */
  1481. wmb();
  1482. wil_w(wil, vring->hwtail, vring->swhead);
  1483. return 0;
  1484. mem_error:
  1485. while (descs_used > 0) {
  1486. struct wil_ctx *ctx;
  1487. i = (swhead + descs_used - 1) % vring->size;
  1488. d = (struct vring_tx_desc *)&vring->va[i].tx;
  1489. _desc = &vring->va[i].tx;
  1490. *d = *_desc;
  1491. _desc->dma.status = TX_DMA_STATUS_DU;
  1492. ctx = &vring->ctx[i];
  1493. wil_txdesc_unmap(dev, d, ctx);
  1494. memset(ctx, 0, sizeof(*ctx));
  1495. descs_used--;
  1496. }
  1497. err_exit:
  1498. return rc;
  1499. }
  1500. static int __wil_tx_vring(struct wil6210_priv *wil, struct wil6210_vif *vif,
  1501. struct vring *vring, struct sk_buff *skb)
  1502. {
  1503. struct device *dev = wil_to_dev(wil);
  1504. struct vring_tx_desc dd, *d = &dd;
  1505. volatile struct vring_tx_desc *_d;
  1506. u32 swhead = vring->swhead;
  1507. int avail = wil_vring_avail_tx(vring);
  1508. int nr_frags = skb_shinfo(skb)->nr_frags;
  1509. uint f = 0;
  1510. int vring_index = vring - wil->vring_tx;
  1511. struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
  1512. uint i = swhead;
  1513. dma_addr_t pa;
  1514. int used;
  1515. bool mcast = (vring_index == vif->bcast_vring);
  1516. uint len = skb_headlen(skb);
  1517. wil_dbg_txrx(wil, "tx_vring: %d bytes to vring %d\n", skb->len,
  1518. vring_index);
  1519. if (unlikely(!txdata->enabled))
  1520. return -EINVAL;
  1521. if (unlikely(avail < 1 + nr_frags)) {
  1522. wil_err_ratelimited(wil,
  1523. "Tx ring[%2d] full. No space for %d fragments\n",
  1524. vring_index, 1 + nr_frags);
  1525. return -ENOMEM;
  1526. }
  1527. _d = &vring->va[i].tx;
  1528. pa = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
  1529. wil_dbg_txrx(wil, "Tx[%2d] skb %d bytes 0x%p -> %pad\n", vring_index,
  1530. skb_headlen(skb), skb->data, &pa);
  1531. wil_hex_dump_txrx("Tx ", DUMP_PREFIX_OFFSET, 16, 1,
  1532. skb->data, skb_headlen(skb), false);
  1533. if (unlikely(dma_mapping_error(dev, pa)))
  1534. return -EINVAL;
  1535. vring->ctx[i].mapped_as = wil_mapped_as_single;
  1536. /* 1-st segment */
  1537. wil_tx_desc_map(d, pa, len, vring_index);
  1538. if (unlikely(mcast)) {
  1539. d->mac.d[0] |= BIT(MAC_CFG_DESC_TX_0_MCS_EN_POS); /* MCS 0 */
  1540. if (unlikely(len > WIL_BCAST_MCS0_LIMIT)) /* set MCS 1 */
  1541. d->mac.d[0] |= (1 << MAC_CFG_DESC_TX_0_MCS_INDEX_POS);
  1542. }
  1543. /* Process TCP/UDP checksum offloading */
  1544. if (unlikely(wil_tx_desc_offload_setup(d, skb))) {
  1545. wil_err(wil, "Tx[%2d] Failed to set cksum, drop packet\n",
  1546. vring_index);
  1547. goto dma_error;
  1548. }
  1549. vring->ctx[i].nr_frags = nr_frags;
  1550. wil_tx_desc_set_nr_frags(d, nr_frags + 1);
  1551. /* middle segments */
  1552. for (; f < nr_frags; f++) {
  1553. const struct skb_frag_struct *frag =
  1554. &skb_shinfo(skb)->frags[f];
  1555. int len = skb_frag_size(frag);
  1556. *_d = *d;
  1557. wil_dbg_txrx(wil, "Tx[%2d] desc[%4d]\n", vring_index, i);
  1558. wil_hex_dump_txrx("TxD ", DUMP_PREFIX_NONE, 32, 4,
  1559. (const void *)d, sizeof(*d), false);
  1560. i = (swhead + f + 1) % vring->size;
  1561. _d = &vring->va[i].tx;
  1562. pa = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag),
  1563. DMA_TO_DEVICE);
  1564. if (unlikely(dma_mapping_error(dev, pa))) {
  1565. wil_err(wil, "Tx[%2d] failed to map fragment\n",
  1566. vring_index);
  1567. goto dma_error;
  1568. }
  1569. vring->ctx[i].mapped_as = wil_mapped_as_page;
  1570. wil_tx_desc_map(d, pa, len, vring_index);
  1571. /* no need to check return code -
  1572. * if it succeeded for 1-st descriptor,
  1573. * it will succeed here too
  1574. */
  1575. wil_tx_desc_offload_setup(d, skb);
  1576. }
  1577. /* for the last seg only */
  1578. d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS);
  1579. d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS);
  1580. d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
  1581. *_d = *d;
  1582. wil_dbg_txrx(wil, "Tx[%2d] desc[%4d]\n", vring_index, i);
  1583. wil_hex_dump_txrx("TxD ", DUMP_PREFIX_NONE, 32, 4,
  1584. (const void *)d, sizeof(*d), false);
  1585. /* hold reference to skb
  1586. * to prevent skb release before accounting
  1587. * in case of immediate "tx done"
  1588. */
  1589. vring->ctx[i].skb = skb_get(skb);
  1590. /* performance monitoring */
  1591. used = wil_vring_used_tx(vring);
  1592. if (wil_val_in_range(wil->vring_idle_trsh,
  1593. used, used + nr_frags + 1)) {
  1594. txdata->idle += get_cycles() - txdata->last_idle;
  1595. wil_dbg_txrx(wil, "Ring[%2d] not idle %d -> %d\n",
  1596. vring_index, used, used + nr_frags + 1);
  1597. }
  1598. /* Make sure to advance the head only after descriptor update is done.
  1599. * This will prevent a race condition where the completion thread
  1600. * will see the DU bit set from previous run and will handle the
  1601. * skb before it was completed.
  1602. */
  1603. wmb();
  1604. /* advance swhead */
  1605. wil_vring_advance_head(vring, nr_frags + 1);
  1606. wil_dbg_txrx(wil, "Tx[%2d] swhead %d -> %d\n", vring_index, swhead,
  1607. vring->swhead);
  1608. trace_wil6210_tx(vring_index, swhead, skb->len, nr_frags);
  1609. /* make sure all writes to descriptors (shared memory) are done before
  1610. * committing them to HW
  1611. */
  1612. wmb();
  1613. wil_w(wil, vring->hwtail, vring->swhead);
  1614. return 0;
  1615. dma_error:
  1616. /* unmap what we have mapped */
  1617. nr_frags = f + 1; /* frags mapped + one for skb head */
  1618. for (f = 0; f < nr_frags; f++) {
  1619. struct wil_ctx *ctx;
  1620. i = (swhead + f) % vring->size;
  1621. ctx = &vring->ctx[i];
  1622. _d = &vring->va[i].tx;
  1623. *d = *_d;
  1624. _d->dma.status = TX_DMA_STATUS_DU;
  1625. wil_txdesc_unmap(dev, d, ctx);
  1626. memset(ctx, 0, sizeof(*ctx));
  1627. }
  1628. return -EINVAL;
  1629. }
  1630. static int wil_tx_vring(struct wil6210_priv *wil, struct wil6210_vif *vif,
  1631. struct vring *vring, struct sk_buff *skb)
  1632. {
  1633. int vring_index = vring - wil->vring_tx;
  1634. struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
  1635. int rc;
  1636. spin_lock(&txdata->lock);
  1637. if (test_bit(wil_status_suspending, wil->status) ||
  1638. test_bit(wil_status_suspended, wil->status) ||
  1639. test_bit(wil_status_resuming, wil->status)) {
  1640. wil_dbg_txrx(wil,
  1641. "suspend/resume in progress. drop packet\n");
  1642. spin_unlock(&txdata->lock);
  1643. return -EINVAL;
  1644. }
  1645. rc = (skb_is_gso(skb) ? __wil_tx_vring_tso : __wil_tx_vring)
  1646. (wil, vif, vring, skb);
  1647. spin_unlock(&txdata->lock);
  1648. return rc;
  1649. }
  1650. /**
  1651. * Check status of tx vrings and stop/wake net queues if needed
  1652. * It will start/stop net queues of a specific VIF net_device.
  1653. *
  1654. * This function does one of two checks:
  1655. * In case check_stop is true, will check if net queues need to be stopped. If
  1656. * the conditions for stopping are met, netif_tx_stop_all_queues() is called.
  1657. * In case check_stop is false, will check if net queues need to be waked. If
  1658. * the conditions for waking are met, netif_tx_wake_all_queues() is called.
  1659. * vring is the vring which is currently being modified by either adding
  1660. * descriptors (tx) into it or removing descriptors (tx complete) from it. Can
  1661. * be null when irrelevant (e.g. connect/disconnect events).
  1662. *
  1663. * The implementation is to stop net queues if modified vring has low
  1664. * descriptor availability. Wake if all vrings are not in low descriptor
  1665. * availability and modified vring has high descriptor availability.
  1666. */
  1667. static inline void __wil_update_net_queues(struct wil6210_priv *wil,
  1668. struct wil6210_vif *vif,
  1669. struct vring *vring,
  1670. bool check_stop)
  1671. {
  1672. int i;
  1673. if (unlikely(!vif))
  1674. return;
  1675. if (vring)
  1676. wil_dbg_txrx(wil, "vring %d, mid %d, check_stop=%d, stopped=%d",
  1677. (int)(vring - wil->vring_tx), vif->mid, check_stop,
  1678. vif->net_queue_stopped);
  1679. else
  1680. wil_dbg_txrx(wil, "check_stop=%d, mid=%d, stopped=%d",
  1681. check_stop, vif->mid, vif->net_queue_stopped);
  1682. if (check_stop == vif->net_queue_stopped)
  1683. /* net queues already in desired state */
  1684. return;
  1685. if (check_stop) {
  1686. if (!vring || unlikely(wil_vring_avail_low(vring))) {
  1687. /* not enough room in the vring */
  1688. netif_tx_stop_all_queues(vif_to_ndev(vif));
  1689. vif->net_queue_stopped = true;
  1690. wil_dbg_txrx(wil, "netif_tx_stop called\n");
  1691. }
  1692. return;
  1693. }
  1694. /* Do not wake the queues in suspend flow */
  1695. if (test_bit(wil_status_suspending, wil->status) ||
  1696. test_bit(wil_status_suspended, wil->status))
  1697. return;
  1698. /* check wake */
  1699. for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
  1700. struct vring *cur_vring = &wil->vring_tx[i];
  1701. struct vring_tx_data *txdata = &wil->vring_tx_data[i];
  1702. if (txdata->mid != vif->mid || !cur_vring->va ||
  1703. !txdata->enabled || cur_vring == vring)
  1704. continue;
  1705. if (wil_vring_avail_low(cur_vring)) {
  1706. wil_dbg_txrx(wil, "vring %d full, can't wake\n",
  1707. (int)(cur_vring - wil->vring_tx));
  1708. return;
  1709. }
  1710. }
  1711. if (!vring || wil_vring_avail_high(vring)) {
  1712. /* enough room in the vring */
  1713. wil_dbg_txrx(wil, "calling netif_tx_wake\n");
  1714. netif_tx_wake_all_queues(vif_to_ndev(vif));
  1715. vif->net_queue_stopped = false;
  1716. }
  1717. }
  1718. void wil_update_net_queues(struct wil6210_priv *wil, struct wil6210_vif *vif,
  1719. struct vring *vring, bool check_stop)
  1720. {
  1721. spin_lock(&wil->net_queue_lock);
  1722. __wil_update_net_queues(wil, vif, vring, check_stop);
  1723. spin_unlock(&wil->net_queue_lock);
  1724. }
  1725. void wil_update_net_queues_bh(struct wil6210_priv *wil, struct wil6210_vif *vif,
  1726. struct vring *vring, bool check_stop)
  1727. {
  1728. spin_lock_bh(&wil->net_queue_lock);
  1729. __wil_update_net_queues(wil, vif, vring, check_stop);
  1730. spin_unlock_bh(&wil->net_queue_lock);
  1731. }
  1732. netdev_tx_t wil_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  1733. {
  1734. struct wil6210_vif *vif = ndev_to_vif(ndev);
  1735. struct wil6210_priv *wil = vif_to_wil(vif);
  1736. struct ethhdr *eth = (void *)skb->data;
  1737. bool bcast = is_multicast_ether_addr(eth->h_dest);
  1738. struct vring *vring;
  1739. static bool pr_once_fw;
  1740. int rc;
  1741. wil_dbg_txrx(wil, "start_xmit\n");
  1742. if (unlikely(!test_bit(wil_status_fwready, wil->status))) {
  1743. if (!pr_once_fw) {
  1744. wil_err(wil, "FW not ready\n");
  1745. pr_once_fw = true;
  1746. }
  1747. goto drop;
  1748. }
  1749. if (unlikely(!test_bit(wil_vif_fwconnected, vif->status))) {
  1750. wil_dbg_ratelimited(wil,
  1751. "VIF not connected, packet dropped\n");
  1752. goto drop;
  1753. }
  1754. if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_MONITOR)) {
  1755. wil_err(wil, "Xmit in monitor mode not supported\n");
  1756. goto drop;
  1757. }
  1758. pr_once_fw = false;
  1759. /* find vring */
  1760. if (vif->wdev.iftype == NL80211_IFTYPE_STATION && !vif->pbss) {
  1761. /* in STA mode (ESS), all to same VRING (to AP) */
  1762. vring = wil_find_tx_vring_sta(wil, vif, skb);
  1763. } else if (bcast) {
  1764. if (vif->pbss)
  1765. /* in pbss, no bcast VRING - duplicate skb in
  1766. * all stations VRINGs
  1767. */
  1768. vring = wil_find_tx_bcast_2(wil, vif, skb);
  1769. else if (vif->wdev.iftype == NL80211_IFTYPE_AP)
  1770. /* AP has a dedicated bcast VRING */
  1771. vring = wil_find_tx_bcast_1(wil, vif, skb);
  1772. else
  1773. /* unexpected combination, fallback to duplicating
  1774. * the skb in all stations VRINGs
  1775. */
  1776. vring = wil_find_tx_bcast_2(wil, vif, skb);
  1777. } else {
  1778. /* unicast, find specific VRING by dest. address */
  1779. vring = wil_find_tx_ucast(wil, vif, skb);
  1780. }
  1781. if (unlikely(!vring)) {
  1782. wil_dbg_txrx(wil, "No Tx VRING found for %pM\n", eth->h_dest);
  1783. goto drop;
  1784. }
  1785. /* set up vring entry */
  1786. rc = wil_tx_vring(wil, vif, vring, skb);
  1787. switch (rc) {
  1788. case 0:
  1789. /* shall we stop net queues? */
  1790. wil_update_net_queues_bh(wil, vif, vring, true);
  1791. /* statistics will be updated on the tx_complete */
  1792. dev_kfree_skb_any(skb);
  1793. return NETDEV_TX_OK;
  1794. case -ENOMEM:
  1795. return NETDEV_TX_BUSY;
  1796. default:
  1797. break; /* goto drop; */
  1798. }
  1799. drop:
  1800. ndev->stats.tx_dropped++;
  1801. dev_kfree_skb_any(skb);
  1802. return NET_XMIT_DROP;
  1803. }
  1804. static inline bool wil_need_txstat(struct sk_buff *skb)
  1805. {
  1806. struct ethhdr *eth = (void *)skb->data;
  1807. return is_unicast_ether_addr(eth->h_dest) && skb->sk &&
  1808. (skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS);
  1809. }
  1810. static inline void wil_consume_skb(struct sk_buff *skb, bool acked)
  1811. {
  1812. if (unlikely(wil_need_txstat(skb)))
  1813. skb_complete_wifi_ack(skb, acked);
  1814. else
  1815. acked ? dev_consume_skb_any(skb) : dev_kfree_skb_any(skb);
  1816. }
  1817. /**
  1818. * Clean up transmitted skb's from the Tx VRING
  1819. *
  1820. * Return number of descriptors cleared
  1821. *
  1822. * Safe to call from IRQ
  1823. */
  1824. int wil_tx_complete(struct wil6210_vif *vif, int ringid)
  1825. {
  1826. struct wil6210_priv *wil = vif_to_wil(vif);
  1827. struct net_device *ndev = vif_to_ndev(vif);
  1828. struct device *dev = wil_to_dev(wil);
  1829. struct vring *vring = &wil->vring_tx[ringid];
  1830. struct vring_tx_data *txdata = &wil->vring_tx_data[ringid];
  1831. int done = 0;
  1832. int cid = wil->vring2cid_tid[ringid][0];
  1833. struct wil_net_stats *stats = NULL;
  1834. volatile struct vring_tx_desc *_d;
  1835. int used_before_complete;
  1836. int used_new;
  1837. if (unlikely(!vring->va)) {
  1838. wil_err(wil, "Tx irq[%d]: vring not initialized\n", ringid);
  1839. return 0;
  1840. }
  1841. if (unlikely(!txdata->enabled)) {
  1842. wil_info(wil, "Tx irq[%d]: vring disabled\n", ringid);
  1843. return 0;
  1844. }
  1845. wil_dbg_txrx(wil, "tx_complete: (%d)\n", ringid);
  1846. used_before_complete = wil_vring_used_tx(vring);
  1847. if (cid < WIL6210_MAX_CID)
  1848. stats = &wil->sta[cid].stats;
  1849. while (!wil_vring_is_empty(vring)) {
  1850. int new_swtail;
  1851. struct wil_ctx *ctx = &vring->ctx[vring->swtail];
  1852. /**
  1853. * For the fragmented skb, HW will set DU bit only for the
  1854. * last fragment. look for it.
  1855. * In TSO the first DU will include hdr desc
  1856. */
  1857. int lf = (vring->swtail + ctx->nr_frags) % vring->size;
  1858. /* TODO: check we are not past head */
  1859. _d = &vring->va[lf].tx;
  1860. if (unlikely(!(_d->dma.status & TX_DMA_STATUS_DU)))
  1861. break;
  1862. new_swtail = (lf + 1) % vring->size;
  1863. while (vring->swtail != new_swtail) {
  1864. struct vring_tx_desc dd, *d = &dd;
  1865. u16 dmalen;
  1866. struct sk_buff *skb;
  1867. ctx = &vring->ctx[vring->swtail];
  1868. skb = ctx->skb;
  1869. _d = &vring->va[vring->swtail].tx;
  1870. *d = *_d;
  1871. dmalen = le16_to_cpu(d->dma.length);
  1872. trace_wil6210_tx_done(ringid, vring->swtail, dmalen,
  1873. d->dma.error);
  1874. wil_dbg_txrx(wil,
  1875. "TxC[%2d][%3d] : %d bytes, status 0x%02x err 0x%02x\n",
  1876. ringid, vring->swtail, dmalen,
  1877. d->dma.status, d->dma.error);
  1878. wil_hex_dump_txrx("TxCD ", DUMP_PREFIX_NONE, 32, 4,
  1879. (const void *)d, sizeof(*d), false);
  1880. wil_txdesc_unmap(dev, d, ctx);
  1881. if (skb) {
  1882. if (likely(d->dma.error == 0)) {
  1883. ndev->stats.tx_packets++;
  1884. ndev->stats.tx_bytes += skb->len;
  1885. if (stats) {
  1886. stats->tx_packets++;
  1887. stats->tx_bytes += skb->len;
  1888. }
  1889. } else {
  1890. ndev->stats.tx_errors++;
  1891. if (stats)
  1892. stats->tx_errors++;
  1893. }
  1894. wil_consume_skb(skb, d->dma.error == 0);
  1895. }
  1896. memset(ctx, 0, sizeof(*ctx));
  1897. /* Make sure the ctx is zeroed before updating the tail
  1898. * to prevent a case where wil_tx_vring will see
  1899. * this descriptor as used and handle it before ctx zero
  1900. * is completed.
  1901. */
  1902. wmb();
  1903. /* There is no need to touch HW descriptor:
  1904. * - ststus bit TX_DMA_STATUS_DU is set by design,
  1905. * so hardware will not try to process this desc.,
  1906. * - rest of descriptor will be initialized on Tx.
  1907. */
  1908. vring->swtail = wil_vring_next_tail(vring);
  1909. done++;
  1910. }
  1911. }
  1912. /* performance monitoring */
  1913. used_new = wil_vring_used_tx(vring);
  1914. if (wil_val_in_range(wil->vring_idle_trsh,
  1915. used_new, used_before_complete)) {
  1916. wil_dbg_txrx(wil, "Ring[%2d] idle %d -> %d\n",
  1917. ringid, used_before_complete, used_new);
  1918. txdata->last_idle = get_cycles();
  1919. }
  1920. /* shall we wake net queues? */
  1921. if (done)
  1922. wil_update_net_queues(wil, vif, vring, false);
  1923. return done;
  1924. }