txrx.c 62 KB

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