fwsignal.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498
  1. /*
  2. * Copyright (c) 2010 Broadcom Corporation
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/types.h>
  17. #include <linux/module.h>
  18. #include <linux/if_ether.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/etherdevice.h>
  23. #include <linux/err.h>
  24. #include <linux/jiffies.h>
  25. #include <net/cfg80211.h>
  26. #include <brcmu_utils.h>
  27. #include <brcmu_wifi.h>
  28. #include "core.h"
  29. #include "debug.h"
  30. #include "bus.h"
  31. #include "fwil.h"
  32. #include "fwil_types.h"
  33. #include "fweh.h"
  34. #include "fwsignal.h"
  35. #include "p2p.h"
  36. #include "cfg80211.h"
  37. #include "proto.h"
  38. #include "bcdc.h"
  39. #include "common.h"
  40. /**
  41. * DOC: Firmware Signalling
  42. *
  43. * Firmware can send signals to host and vice versa, which are passed in the
  44. * data packets using TLV based header. This signalling layer is on top of the
  45. * BDC bus protocol layer.
  46. */
  47. /*
  48. * single definition for firmware-driver flow control tlv's.
  49. *
  50. * each tlv is specified by BRCMF_FWS_TLV_DEF(name, ID, length).
  51. * A length value 0 indicates variable length tlv.
  52. */
  53. #define BRCMF_FWS_TLV_DEFLIST \
  54. BRCMF_FWS_TLV_DEF(MAC_OPEN, 1, 1) \
  55. BRCMF_FWS_TLV_DEF(MAC_CLOSE, 2, 1) \
  56. BRCMF_FWS_TLV_DEF(MAC_REQUEST_CREDIT, 3, 2) \
  57. BRCMF_FWS_TLV_DEF(TXSTATUS, 4, 4) \
  58. BRCMF_FWS_TLV_DEF(PKTTAG, 5, 4) \
  59. BRCMF_FWS_TLV_DEF(MACDESC_ADD, 6, 8) \
  60. BRCMF_FWS_TLV_DEF(MACDESC_DEL, 7, 8) \
  61. BRCMF_FWS_TLV_DEF(RSSI, 8, 1) \
  62. BRCMF_FWS_TLV_DEF(INTERFACE_OPEN, 9, 1) \
  63. BRCMF_FWS_TLV_DEF(INTERFACE_CLOSE, 10, 1) \
  64. BRCMF_FWS_TLV_DEF(FIFO_CREDITBACK, 11, 6) \
  65. BRCMF_FWS_TLV_DEF(PENDING_TRAFFIC_BMP, 12, 2) \
  66. BRCMF_FWS_TLV_DEF(MAC_REQUEST_PACKET, 13, 3) \
  67. BRCMF_FWS_TLV_DEF(HOST_REORDER_RXPKTS, 14, 10) \
  68. BRCMF_FWS_TLV_DEF(TRANS_ID, 18, 6) \
  69. BRCMF_FWS_TLV_DEF(COMP_TXSTATUS, 19, 1) \
  70. BRCMF_FWS_TLV_DEF(FILLER, 255, 0)
  71. /*
  72. * enum brcmf_fws_tlv_type - definition of tlv identifiers.
  73. */
  74. #define BRCMF_FWS_TLV_DEF(name, id, len) \
  75. BRCMF_FWS_TYPE_ ## name = id,
  76. enum brcmf_fws_tlv_type {
  77. BRCMF_FWS_TLV_DEFLIST
  78. BRCMF_FWS_TYPE_INVALID
  79. };
  80. #undef BRCMF_FWS_TLV_DEF
  81. /*
  82. * enum brcmf_fws_tlv_len - definition of tlv lengths.
  83. */
  84. #define BRCMF_FWS_TLV_DEF(name, id, len) \
  85. BRCMF_FWS_TYPE_ ## name ## _LEN = (len),
  86. enum brcmf_fws_tlv_len {
  87. BRCMF_FWS_TLV_DEFLIST
  88. };
  89. #undef BRCMF_FWS_TLV_DEF
  90. /* AMPDU rx reordering definitions */
  91. #define BRCMF_RXREORDER_FLOWID_OFFSET 0
  92. #define BRCMF_RXREORDER_MAXIDX_OFFSET 2
  93. #define BRCMF_RXREORDER_FLAGS_OFFSET 4
  94. #define BRCMF_RXREORDER_CURIDX_OFFSET 6
  95. #define BRCMF_RXREORDER_EXPIDX_OFFSET 8
  96. #define BRCMF_RXREORDER_DEL_FLOW 0x01
  97. #define BRCMF_RXREORDER_FLUSH_ALL 0x02
  98. #define BRCMF_RXREORDER_CURIDX_VALID 0x04
  99. #define BRCMF_RXREORDER_EXPIDX_VALID 0x08
  100. #define BRCMF_RXREORDER_NEW_HOLE 0x10
  101. #ifdef DEBUG
  102. /*
  103. * brcmf_fws_tlv_names - array of tlv names.
  104. */
  105. #define BRCMF_FWS_TLV_DEF(name, id, len) \
  106. { id, #name },
  107. static struct {
  108. enum brcmf_fws_tlv_type id;
  109. const char *name;
  110. } brcmf_fws_tlv_names[] = {
  111. BRCMF_FWS_TLV_DEFLIST
  112. };
  113. #undef BRCMF_FWS_TLV_DEF
  114. static const char *brcmf_fws_get_tlv_name(enum brcmf_fws_tlv_type id)
  115. {
  116. int i;
  117. for (i = 0; i < ARRAY_SIZE(brcmf_fws_tlv_names); i++)
  118. if (brcmf_fws_tlv_names[i].id == id)
  119. return brcmf_fws_tlv_names[i].name;
  120. return "INVALID";
  121. }
  122. #else
  123. static const char *brcmf_fws_get_tlv_name(enum brcmf_fws_tlv_type id)
  124. {
  125. return "NODEBUG";
  126. }
  127. #endif /* DEBUG */
  128. /*
  129. * The PKTTAG tlv has additional bytes when firmware-signalling
  130. * mode has REUSESEQ flag set.
  131. */
  132. #define BRCMF_FWS_TYPE_SEQ_LEN 2
  133. /*
  134. * flags used to enable tlv signalling from firmware.
  135. */
  136. #define BRCMF_FWS_FLAGS_RSSI_SIGNALS 0x0001
  137. #define BRCMF_FWS_FLAGS_XONXOFF_SIGNALS 0x0002
  138. #define BRCMF_FWS_FLAGS_CREDIT_STATUS_SIGNALS 0x0004
  139. #define BRCMF_FWS_FLAGS_HOST_PROPTXSTATUS_ACTIVE 0x0008
  140. #define BRCMF_FWS_FLAGS_PSQ_GENERATIONFSM_ENABLE 0x0010
  141. #define BRCMF_FWS_FLAGS_PSQ_ZERO_BUFFER_ENABLE 0x0020
  142. #define BRCMF_FWS_FLAGS_HOST_RXREORDER_ACTIVE 0x0040
  143. #define BRCMF_FWS_MAC_DESC_TABLE_SIZE 32
  144. #define BRCMF_FWS_MAC_DESC_ID_INVALID 0xff
  145. #define BRCMF_FWS_HOSTIF_FLOWSTATE_OFF 0
  146. #define BRCMF_FWS_HOSTIF_FLOWSTATE_ON 1
  147. #define BRCMF_FWS_FLOWCONTROL_HIWATER 128
  148. #define BRCMF_FWS_FLOWCONTROL_LOWATER 64
  149. #define BRCMF_FWS_PSQ_PREC_COUNT ((BRCMF_FWS_FIFO_COUNT + 1) * 2)
  150. #define BRCMF_FWS_PSQ_LEN 256
  151. #define BRCMF_FWS_HTOD_FLAG_PKTFROMHOST 0x01
  152. #define BRCMF_FWS_HTOD_FLAG_PKT_REQUESTED 0x02
  153. #define BRCMF_FWS_RET_OK_NOSCHEDULE 0
  154. #define BRCMF_FWS_RET_OK_SCHEDULE 1
  155. #define BRCMF_FWS_MODE_REUSESEQ_SHIFT 3 /* seq reuse */
  156. #define BRCMF_FWS_MODE_SET_REUSESEQ(x, val) ((x) = \
  157. ((x) & ~(1 << BRCMF_FWS_MODE_REUSESEQ_SHIFT)) | \
  158. (((val) & 1) << BRCMF_FWS_MODE_REUSESEQ_SHIFT))
  159. #define BRCMF_FWS_MODE_GET_REUSESEQ(x) \
  160. (((x) >> BRCMF_FWS_MODE_REUSESEQ_SHIFT) & 1)
  161. /**
  162. * enum brcmf_fws_skb_state - indicates processing state of skb.
  163. *
  164. * @BRCMF_FWS_SKBSTATE_NEW: sk_buff is newly arrived in the driver.
  165. * @BRCMF_FWS_SKBSTATE_DELAYED: sk_buff had to wait on queue.
  166. * @BRCMF_FWS_SKBSTATE_SUPPRESSED: sk_buff has been suppressed by firmware.
  167. * @BRCMF_FWS_SKBSTATE_TIM: allocated for TIM update info.
  168. */
  169. enum brcmf_fws_skb_state {
  170. BRCMF_FWS_SKBSTATE_NEW,
  171. BRCMF_FWS_SKBSTATE_DELAYED,
  172. BRCMF_FWS_SKBSTATE_SUPPRESSED,
  173. BRCMF_FWS_SKBSTATE_TIM
  174. };
  175. /**
  176. * struct brcmf_skbuff_cb - control buffer associated with skbuff.
  177. *
  178. * @bus_flags: 2 bytes reserved for bus specific parameters
  179. * @if_flags: holds interface index and packet related flags.
  180. * @htod: host to device packet identifier (used in PKTTAG tlv).
  181. * @htod_seq: this 16-bit is original seq number for every suppress packet.
  182. * @state: transmit state of the packet.
  183. * @mac: descriptor related to destination for this packet.
  184. *
  185. * This information is stored in control buffer struct sk_buff::cb, which
  186. * provides 48 bytes of storage so this structure should not exceed that.
  187. */
  188. struct brcmf_skbuff_cb {
  189. u16 bus_flags;
  190. u16 if_flags;
  191. u32 htod;
  192. u16 htod_seq;
  193. enum brcmf_fws_skb_state state;
  194. struct brcmf_fws_mac_descriptor *mac;
  195. };
  196. /*
  197. * macro casting skbuff control buffer to struct brcmf_skbuff_cb.
  198. */
  199. #define brcmf_skbcb(skb) ((struct brcmf_skbuff_cb *)((skb)->cb))
  200. /*
  201. * sk_buff control if flags
  202. *
  203. * b[11] - packet sent upon firmware request.
  204. * b[10] - packet only contains signalling data.
  205. * b[9] - packet is a tx packet.
  206. * b[8] - packet used requested credit
  207. * b[7] - interface in AP mode.
  208. * b[3:0] - interface index.
  209. */
  210. #define BRCMF_SKB_IF_FLAGS_REQUESTED_MASK 0x0800
  211. #define BRCMF_SKB_IF_FLAGS_REQUESTED_SHIFT 11
  212. #define BRCMF_SKB_IF_FLAGS_SIGNAL_ONLY_MASK 0x0400
  213. #define BRCMF_SKB_IF_FLAGS_SIGNAL_ONLY_SHIFT 10
  214. #define BRCMF_SKB_IF_FLAGS_TRANSMIT_MASK 0x0200
  215. #define BRCMF_SKB_IF_FLAGS_TRANSMIT_SHIFT 9
  216. #define BRCMF_SKB_IF_FLAGS_REQ_CREDIT_MASK 0x0100
  217. #define BRCMF_SKB_IF_FLAGS_REQ_CREDIT_SHIFT 8
  218. #define BRCMF_SKB_IF_FLAGS_IF_AP_MASK 0x0080
  219. #define BRCMF_SKB_IF_FLAGS_IF_AP_SHIFT 7
  220. #define BRCMF_SKB_IF_FLAGS_INDEX_MASK 0x000f
  221. #define BRCMF_SKB_IF_FLAGS_INDEX_SHIFT 0
  222. #define brcmf_skb_if_flags_set_field(skb, field, value) \
  223. brcmu_maskset16(&(brcmf_skbcb(skb)->if_flags), \
  224. BRCMF_SKB_IF_FLAGS_ ## field ## _MASK, \
  225. BRCMF_SKB_IF_FLAGS_ ## field ## _SHIFT, (value))
  226. #define brcmf_skb_if_flags_get_field(skb, field) \
  227. brcmu_maskget16(brcmf_skbcb(skb)->if_flags, \
  228. BRCMF_SKB_IF_FLAGS_ ## field ## _MASK, \
  229. BRCMF_SKB_IF_FLAGS_ ## field ## _SHIFT)
  230. /*
  231. * sk_buff control packet identifier
  232. *
  233. * 32-bit packet identifier used in PKTTAG tlv from host to dongle.
  234. *
  235. * - Generated at the host (e.g. dhd)
  236. * - Seen as a generic sequence number by firmware except for the flags field.
  237. *
  238. * Generation : b[31] => generation number for this packet [host->fw]
  239. * OR, current generation number [fw->host]
  240. * Flags : b[30:27] => command, status flags
  241. * FIFO-AC : b[26:24] => AC-FIFO id
  242. * h-slot : b[23:8] => hanger-slot
  243. * freerun : b[7:0] => A free running counter
  244. */
  245. #define BRCMF_SKB_HTOD_TAG_GENERATION_MASK 0x80000000
  246. #define BRCMF_SKB_HTOD_TAG_GENERATION_SHIFT 31
  247. #define BRCMF_SKB_HTOD_TAG_FLAGS_MASK 0x78000000
  248. #define BRCMF_SKB_HTOD_TAG_FLAGS_SHIFT 27
  249. #define BRCMF_SKB_HTOD_TAG_FIFO_MASK 0x07000000
  250. #define BRCMF_SKB_HTOD_TAG_FIFO_SHIFT 24
  251. #define BRCMF_SKB_HTOD_TAG_HSLOT_MASK 0x00ffff00
  252. #define BRCMF_SKB_HTOD_TAG_HSLOT_SHIFT 8
  253. #define BRCMF_SKB_HTOD_TAG_FREERUN_MASK 0x000000ff
  254. #define BRCMF_SKB_HTOD_TAG_FREERUN_SHIFT 0
  255. #define brcmf_skb_htod_tag_set_field(skb, field, value) \
  256. brcmu_maskset32(&(brcmf_skbcb(skb)->htod), \
  257. BRCMF_SKB_HTOD_TAG_ ## field ## _MASK, \
  258. BRCMF_SKB_HTOD_TAG_ ## field ## _SHIFT, (value))
  259. #define brcmf_skb_htod_tag_get_field(skb, field) \
  260. brcmu_maskget32(brcmf_skbcb(skb)->htod, \
  261. BRCMF_SKB_HTOD_TAG_ ## field ## _MASK, \
  262. BRCMF_SKB_HTOD_TAG_ ## field ## _SHIFT)
  263. #define BRCMF_SKB_HTOD_SEQ_FROMFW_MASK 0x2000
  264. #define BRCMF_SKB_HTOD_SEQ_FROMFW_SHIFT 13
  265. #define BRCMF_SKB_HTOD_SEQ_FROMDRV_MASK 0x1000
  266. #define BRCMF_SKB_HTOD_SEQ_FROMDRV_SHIFT 12
  267. #define BRCMF_SKB_HTOD_SEQ_NR_MASK 0x0fff
  268. #define BRCMF_SKB_HTOD_SEQ_NR_SHIFT 0
  269. #define brcmf_skb_htod_seq_set_field(skb, field, value) \
  270. brcmu_maskset16(&(brcmf_skbcb(skb)->htod_seq), \
  271. BRCMF_SKB_HTOD_SEQ_ ## field ## _MASK, \
  272. BRCMF_SKB_HTOD_SEQ_ ## field ## _SHIFT, (value))
  273. #define brcmf_skb_htod_seq_get_field(skb, field) \
  274. brcmu_maskget16(brcmf_skbcb(skb)->htod_seq, \
  275. BRCMF_SKB_HTOD_SEQ_ ## field ## _MASK, \
  276. BRCMF_SKB_HTOD_SEQ_ ## field ## _SHIFT)
  277. #define BRCMF_FWS_TXSTAT_GENERATION_MASK 0x80000000
  278. #define BRCMF_FWS_TXSTAT_GENERATION_SHIFT 31
  279. #define BRCMF_FWS_TXSTAT_FLAGS_MASK 0x78000000
  280. #define BRCMF_FWS_TXSTAT_FLAGS_SHIFT 27
  281. #define BRCMF_FWS_TXSTAT_FIFO_MASK 0x07000000
  282. #define BRCMF_FWS_TXSTAT_FIFO_SHIFT 24
  283. #define BRCMF_FWS_TXSTAT_HSLOT_MASK 0x00FFFF00
  284. #define BRCMF_FWS_TXSTAT_HSLOT_SHIFT 8
  285. #define BRCMF_FWS_TXSTAT_FREERUN_MASK 0x000000FF
  286. #define BRCMF_FWS_TXSTAT_FREERUN_SHIFT 0
  287. #define brcmf_txstatus_get_field(txs, field) \
  288. brcmu_maskget32(txs, BRCMF_FWS_TXSTAT_ ## field ## _MASK, \
  289. BRCMF_FWS_TXSTAT_ ## field ## _SHIFT)
  290. /* How long to defer borrowing in jiffies */
  291. #define BRCMF_FWS_BORROW_DEFER_PERIOD (HZ / 10)
  292. /**
  293. * enum brcmf_fws_fifo - fifo indices used by dongle firmware.
  294. *
  295. * @BRCMF_FWS_FIFO_FIRST: first fifo, ie. background.
  296. * @BRCMF_FWS_FIFO_AC_BK: fifo for background traffic.
  297. * @BRCMF_FWS_FIFO_AC_BE: fifo for best-effort traffic.
  298. * @BRCMF_FWS_FIFO_AC_VI: fifo for video traffic.
  299. * @BRCMF_FWS_FIFO_AC_VO: fifo for voice traffic.
  300. * @BRCMF_FWS_FIFO_BCMC: fifo for broadcast/multicast (AP only).
  301. * @BRCMF_FWS_FIFO_ATIM: fifo for ATIM (AP only).
  302. * @BRCMF_FWS_FIFO_COUNT: number of fifos.
  303. */
  304. enum brcmf_fws_fifo {
  305. BRCMF_FWS_FIFO_FIRST,
  306. BRCMF_FWS_FIFO_AC_BK = BRCMF_FWS_FIFO_FIRST,
  307. BRCMF_FWS_FIFO_AC_BE,
  308. BRCMF_FWS_FIFO_AC_VI,
  309. BRCMF_FWS_FIFO_AC_VO,
  310. BRCMF_FWS_FIFO_BCMC,
  311. BRCMF_FWS_FIFO_ATIM,
  312. BRCMF_FWS_FIFO_COUNT
  313. };
  314. /**
  315. * enum brcmf_fws_txstatus - txstatus flag values.
  316. *
  317. * @BRCMF_FWS_TXSTATUS_DISCARD:
  318. * host is free to discard the packet.
  319. * @BRCMF_FWS_TXSTATUS_CORE_SUPPRESS:
  320. * 802.11 core suppressed the packet.
  321. * @BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS:
  322. * firmware suppress the packet as device is already in PS mode.
  323. * @BRCMF_FWS_TXSTATUS_FW_TOSSED:
  324. * firmware tossed the packet.
  325. * @BRCMF_FWS_TXSTATUS_HOST_TOSSED:
  326. * host tossed the packet.
  327. */
  328. enum brcmf_fws_txstatus {
  329. BRCMF_FWS_TXSTATUS_DISCARD,
  330. BRCMF_FWS_TXSTATUS_CORE_SUPPRESS,
  331. BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS,
  332. BRCMF_FWS_TXSTATUS_FW_TOSSED,
  333. BRCMF_FWS_TXSTATUS_HOST_TOSSED
  334. };
  335. enum brcmf_fws_fcmode {
  336. BRCMF_FWS_FCMODE_NONE,
  337. BRCMF_FWS_FCMODE_IMPLIED_CREDIT,
  338. BRCMF_FWS_FCMODE_EXPLICIT_CREDIT
  339. };
  340. enum brcmf_fws_mac_desc_state {
  341. BRCMF_FWS_STATE_OPEN = 1,
  342. BRCMF_FWS_STATE_CLOSE
  343. };
  344. /**
  345. * struct brcmf_fws_mac_descriptor - firmware signalling data per node/interface
  346. *
  347. * @occupied: slot is in use.
  348. * @mac_handle: handle for mac entry determined by firmware.
  349. * @interface_id: interface index.
  350. * @state: current state.
  351. * @suppressed: mac entry is suppressed.
  352. * @generation: generation bit.
  353. * @ac_bitmap: ac queue bitmap.
  354. * @requested_credit: credits requested by firmware.
  355. * @ea: ethernet address.
  356. * @seq: per-node free-running sequence.
  357. * @psq: power-save queue.
  358. * @transit_count: packet in transit to firmware.
  359. */
  360. struct brcmf_fws_mac_descriptor {
  361. char name[16];
  362. u8 occupied;
  363. u8 mac_handle;
  364. u8 interface_id;
  365. u8 state;
  366. bool suppressed;
  367. u8 generation;
  368. u8 ac_bitmap;
  369. u8 requested_credit;
  370. u8 requested_packet;
  371. u8 ea[ETH_ALEN];
  372. u8 seq[BRCMF_FWS_FIFO_COUNT];
  373. struct pktq psq;
  374. int transit_count;
  375. int suppr_transit_count;
  376. bool send_tim_signal;
  377. u8 traffic_pending_bmp;
  378. u8 traffic_lastreported_bmp;
  379. };
  380. #define BRCMF_FWS_HANGER_MAXITEMS 1024
  381. /**
  382. * enum brcmf_fws_hanger_item_state - state of hanger item.
  383. *
  384. * @BRCMF_FWS_HANGER_ITEM_STATE_FREE: item is free for use.
  385. * @BRCMF_FWS_HANGER_ITEM_STATE_INUSE: item is in use.
  386. * @BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED: item was suppressed.
  387. */
  388. enum brcmf_fws_hanger_item_state {
  389. BRCMF_FWS_HANGER_ITEM_STATE_FREE = 1,
  390. BRCMF_FWS_HANGER_ITEM_STATE_INUSE,
  391. BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED
  392. };
  393. /**
  394. * struct brcmf_fws_hanger_item - single entry for tx pending packet.
  395. *
  396. * @state: entry is either free or occupied.
  397. * @pkt: packet itself.
  398. */
  399. struct brcmf_fws_hanger_item {
  400. enum brcmf_fws_hanger_item_state state;
  401. struct sk_buff *pkt;
  402. };
  403. /**
  404. * struct brcmf_fws_hanger - holds packets awaiting firmware txstatus.
  405. *
  406. * @pushed: packets pushed to await txstatus.
  407. * @popped: packets popped upon handling txstatus.
  408. * @failed_to_push: packets that could not be pushed.
  409. * @failed_to_pop: packets that could not be popped.
  410. * @failed_slotfind: packets for which failed to find an entry.
  411. * @slot_pos: last returned item index for a free entry.
  412. * @items: array of hanger items.
  413. */
  414. struct brcmf_fws_hanger {
  415. u32 pushed;
  416. u32 popped;
  417. u32 failed_to_push;
  418. u32 failed_to_pop;
  419. u32 failed_slotfind;
  420. u32 slot_pos;
  421. struct brcmf_fws_hanger_item items[BRCMF_FWS_HANGER_MAXITEMS];
  422. };
  423. struct brcmf_fws_macdesc_table {
  424. struct brcmf_fws_mac_descriptor nodes[BRCMF_FWS_MAC_DESC_TABLE_SIZE];
  425. struct brcmf_fws_mac_descriptor iface[BRCMF_MAX_IFS];
  426. struct brcmf_fws_mac_descriptor other;
  427. };
  428. struct brcmf_fws_stats {
  429. u32 tlv_parse_failed;
  430. u32 tlv_invalid_type;
  431. u32 header_only_pkt;
  432. u32 header_pulls;
  433. u32 pkt2bus;
  434. u32 send_pkts[5];
  435. u32 requested_sent[5];
  436. u32 generic_error;
  437. u32 mac_update_failed;
  438. u32 mac_ps_update_failed;
  439. u32 if_update_failed;
  440. u32 packet_request_failed;
  441. u32 credit_request_failed;
  442. u32 rollback_success;
  443. u32 rollback_failed;
  444. u32 delayq_full_error;
  445. u32 supprq_full_error;
  446. u32 txs_indicate;
  447. u32 txs_discard;
  448. u32 txs_supp_core;
  449. u32 txs_supp_ps;
  450. u32 txs_tossed;
  451. u32 txs_host_tossed;
  452. u32 bus_flow_block;
  453. u32 fws_flow_block;
  454. };
  455. struct brcmf_fws_info {
  456. struct brcmf_pub *drvr;
  457. spinlock_t spinlock;
  458. ulong flags;
  459. struct brcmf_fws_stats stats;
  460. struct brcmf_fws_hanger hanger;
  461. enum brcmf_fws_fcmode fcmode;
  462. bool fw_signals;
  463. bool bcmc_credit_check;
  464. struct brcmf_fws_macdesc_table desc;
  465. struct workqueue_struct *fws_wq;
  466. struct work_struct fws_dequeue_work;
  467. u32 fifo_enqpkt[BRCMF_FWS_FIFO_COUNT];
  468. int fifo_credit[BRCMF_FWS_FIFO_COUNT];
  469. int credits_borrowed[BRCMF_FWS_FIFO_AC_VO + 1];
  470. int deq_node_pos[BRCMF_FWS_FIFO_COUNT];
  471. u32 fifo_credit_map;
  472. u32 fifo_delay_map;
  473. unsigned long borrow_defer_timestamp;
  474. bool bus_flow_blocked;
  475. bool creditmap_received;
  476. u8 mode;
  477. bool avoid_queueing;
  478. };
  479. /*
  480. * brcmf_fws_prio2fifo - mapping from 802.1d priority to firmware fifo index.
  481. */
  482. static const int brcmf_fws_prio2fifo[] = {
  483. BRCMF_FWS_FIFO_AC_BE,
  484. BRCMF_FWS_FIFO_AC_BK,
  485. BRCMF_FWS_FIFO_AC_BK,
  486. BRCMF_FWS_FIFO_AC_BE,
  487. BRCMF_FWS_FIFO_AC_VI,
  488. BRCMF_FWS_FIFO_AC_VI,
  489. BRCMF_FWS_FIFO_AC_VO,
  490. BRCMF_FWS_FIFO_AC_VO
  491. };
  492. #define BRCMF_FWS_TLV_DEF(name, id, len) \
  493. case BRCMF_FWS_TYPE_ ## name: \
  494. return len;
  495. /**
  496. * brcmf_fws_get_tlv_len() - returns defined length for given tlv id.
  497. *
  498. * @fws: firmware-signalling information.
  499. * @id: identifier of the TLV.
  500. *
  501. * Return: the specified length for the given TLV; Otherwise -EINVAL.
  502. */
  503. static int brcmf_fws_get_tlv_len(struct brcmf_fws_info *fws,
  504. enum brcmf_fws_tlv_type id)
  505. {
  506. switch (id) {
  507. BRCMF_FWS_TLV_DEFLIST
  508. default:
  509. fws->stats.tlv_invalid_type++;
  510. break;
  511. }
  512. return -EINVAL;
  513. }
  514. #undef BRCMF_FWS_TLV_DEF
  515. static void brcmf_fws_lock(struct brcmf_fws_info *fws)
  516. __acquires(&fws->spinlock)
  517. {
  518. spin_lock_irqsave(&fws->spinlock, fws->flags);
  519. }
  520. static void brcmf_fws_unlock(struct brcmf_fws_info *fws)
  521. __releases(&fws->spinlock)
  522. {
  523. spin_unlock_irqrestore(&fws->spinlock, fws->flags);
  524. }
  525. static bool brcmf_fws_ifidx_match(struct sk_buff *skb, void *arg)
  526. {
  527. u32 ifidx = brcmf_skb_if_flags_get_field(skb, INDEX);
  528. return ifidx == *(int *)arg;
  529. }
  530. static void brcmf_fws_hanger_init(struct brcmf_fws_hanger *hanger)
  531. {
  532. int i;
  533. memset(hanger, 0, sizeof(*hanger));
  534. for (i = 0; i < ARRAY_SIZE(hanger->items); i++)
  535. hanger->items[i].state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
  536. }
  537. static u32 brcmf_fws_hanger_get_free_slot(struct brcmf_fws_hanger *h)
  538. {
  539. u32 i;
  540. i = (h->slot_pos + 1) % BRCMF_FWS_HANGER_MAXITEMS;
  541. while (i != h->slot_pos) {
  542. if (h->items[i].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
  543. h->slot_pos = i;
  544. goto done;
  545. }
  546. i++;
  547. if (i == BRCMF_FWS_HANGER_MAXITEMS)
  548. i = 0;
  549. }
  550. brcmf_err("all slots occupied\n");
  551. h->failed_slotfind++;
  552. i = BRCMF_FWS_HANGER_MAXITEMS;
  553. done:
  554. return i;
  555. }
  556. static int brcmf_fws_hanger_pushpkt(struct brcmf_fws_hanger *h,
  557. struct sk_buff *pkt, u32 slot_id)
  558. {
  559. if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
  560. return -ENOENT;
  561. if (h->items[slot_id].state != BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
  562. brcmf_err("slot is not free\n");
  563. h->failed_to_push++;
  564. return -EINVAL;
  565. }
  566. h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_INUSE;
  567. h->items[slot_id].pkt = pkt;
  568. h->pushed++;
  569. return 0;
  570. }
  571. static inline int brcmf_fws_hanger_poppkt(struct brcmf_fws_hanger *h,
  572. u32 slot_id, struct sk_buff **pktout,
  573. bool remove_item)
  574. {
  575. if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
  576. return -ENOENT;
  577. if (h->items[slot_id].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
  578. brcmf_err("entry not in use\n");
  579. h->failed_to_pop++;
  580. return -EINVAL;
  581. }
  582. *pktout = h->items[slot_id].pkt;
  583. if (remove_item) {
  584. h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
  585. h->items[slot_id].pkt = NULL;
  586. h->popped++;
  587. }
  588. return 0;
  589. }
  590. static void brcmf_fws_psq_flush(struct brcmf_fws_info *fws, struct pktq *q,
  591. int ifidx)
  592. {
  593. bool (*matchfn)(struct sk_buff *, void *) = NULL;
  594. struct sk_buff *skb;
  595. int prec;
  596. u32 hslot;
  597. if (ifidx != -1)
  598. matchfn = brcmf_fws_ifidx_match;
  599. for (prec = 0; prec < q->num_prec; prec++) {
  600. skb = brcmu_pktq_pdeq_match(q, prec, matchfn, &ifidx);
  601. while (skb) {
  602. hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
  603. brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &skb,
  604. true);
  605. brcmu_pkt_buf_free_skb(skb);
  606. skb = brcmu_pktq_pdeq_match(q, prec, matchfn, &ifidx);
  607. }
  608. }
  609. }
  610. static int brcmf_fws_hanger_mark_suppressed(struct brcmf_fws_hanger *h,
  611. u32 slot_id)
  612. {
  613. if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
  614. return -ENOENT;
  615. if (h->items[slot_id].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
  616. brcmf_err("entry not in use\n");
  617. return -EINVAL;
  618. }
  619. h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED;
  620. return 0;
  621. }
  622. static void brcmf_fws_hanger_cleanup(struct brcmf_fws_info *fws,
  623. bool (*fn)(struct sk_buff *, void *),
  624. int ifidx)
  625. {
  626. struct brcmf_fws_hanger *h = &fws->hanger;
  627. struct sk_buff *skb;
  628. int i;
  629. enum brcmf_fws_hanger_item_state s;
  630. for (i = 0; i < ARRAY_SIZE(h->items); i++) {
  631. s = h->items[i].state;
  632. if (s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE ||
  633. s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED) {
  634. skb = h->items[i].pkt;
  635. if (fn == NULL || fn(skb, &ifidx)) {
  636. /* suppress packets freed from psq */
  637. if (s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE)
  638. brcmu_pkt_buf_free_skb(skb);
  639. h->items[i].state =
  640. BRCMF_FWS_HANGER_ITEM_STATE_FREE;
  641. }
  642. }
  643. }
  644. }
  645. static void brcmf_fws_macdesc_set_name(struct brcmf_fws_info *fws,
  646. struct brcmf_fws_mac_descriptor *desc)
  647. {
  648. if (desc == &fws->desc.other)
  649. strlcpy(desc->name, "MAC-OTHER", sizeof(desc->name));
  650. else if (desc->mac_handle)
  651. scnprintf(desc->name, sizeof(desc->name), "MAC-%d:%d",
  652. desc->mac_handle, desc->interface_id);
  653. else
  654. scnprintf(desc->name, sizeof(desc->name), "MACIF:%d",
  655. desc->interface_id);
  656. }
  657. static void brcmf_fws_macdesc_init(struct brcmf_fws_mac_descriptor *desc,
  658. u8 *addr, u8 ifidx)
  659. {
  660. brcmf_dbg(TRACE,
  661. "enter: desc %p ea=%pM, ifidx=%u\n", desc, addr, ifidx);
  662. desc->occupied = 1;
  663. desc->state = BRCMF_FWS_STATE_OPEN;
  664. desc->requested_credit = 0;
  665. desc->requested_packet = 0;
  666. /* depending on use may need ifp->bsscfgidx instead */
  667. desc->interface_id = ifidx;
  668. desc->ac_bitmap = 0xff; /* update this when handling APSD */
  669. if (addr)
  670. memcpy(&desc->ea[0], addr, ETH_ALEN);
  671. }
  672. static
  673. void brcmf_fws_macdesc_deinit(struct brcmf_fws_mac_descriptor *desc)
  674. {
  675. brcmf_dbg(TRACE,
  676. "enter: ea=%pM, ifidx=%u\n", desc->ea, desc->interface_id);
  677. desc->occupied = 0;
  678. desc->state = BRCMF_FWS_STATE_CLOSE;
  679. desc->requested_credit = 0;
  680. desc->requested_packet = 0;
  681. }
  682. static struct brcmf_fws_mac_descriptor *
  683. brcmf_fws_macdesc_lookup(struct brcmf_fws_info *fws, u8 *ea)
  684. {
  685. struct brcmf_fws_mac_descriptor *entry;
  686. int i;
  687. if (ea == NULL)
  688. return ERR_PTR(-EINVAL);
  689. entry = &fws->desc.nodes[0];
  690. for (i = 0; i < ARRAY_SIZE(fws->desc.nodes); i++) {
  691. if (entry->occupied && !memcmp(entry->ea, ea, ETH_ALEN))
  692. return entry;
  693. entry++;
  694. }
  695. return ERR_PTR(-ENOENT);
  696. }
  697. static struct brcmf_fws_mac_descriptor*
  698. brcmf_fws_macdesc_find(struct brcmf_fws_info *fws, struct brcmf_if *ifp, u8 *da)
  699. {
  700. struct brcmf_fws_mac_descriptor *entry = &fws->desc.other;
  701. bool multicast;
  702. multicast = is_multicast_ether_addr(da);
  703. /* Multicast destination, STA and P2P clients get the interface entry.
  704. * STA/GC gets the Mac Entry for TDLS destinations, TDLS destinations
  705. * have their own entry.
  706. */
  707. if (multicast && ifp->fws_desc) {
  708. entry = ifp->fws_desc;
  709. goto done;
  710. }
  711. entry = brcmf_fws_macdesc_lookup(fws, da);
  712. if (IS_ERR(entry))
  713. entry = ifp->fws_desc;
  714. done:
  715. return entry;
  716. }
  717. static bool brcmf_fws_macdesc_closed(struct brcmf_fws_info *fws,
  718. struct brcmf_fws_mac_descriptor *entry,
  719. int fifo)
  720. {
  721. struct brcmf_fws_mac_descriptor *if_entry;
  722. bool closed;
  723. /* for unique destination entries the related interface
  724. * may be closed.
  725. */
  726. if (entry->mac_handle) {
  727. if_entry = &fws->desc.iface[entry->interface_id];
  728. if (if_entry->state == BRCMF_FWS_STATE_CLOSE)
  729. return true;
  730. }
  731. /* an entry is closed when the state is closed and
  732. * the firmware did not request anything.
  733. */
  734. closed = entry->state == BRCMF_FWS_STATE_CLOSE &&
  735. !entry->requested_credit && !entry->requested_packet;
  736. /* Or firmware does not allow traffic for given fifo */
  737. return closed || !(entry->ac_bitmap & BIT(fifo));
  738. }
  739. static void brcmf_fws_macdesc_cleanup(struct brcmf_fws_info *fws,
  740. struct brcmf_fws_mac_descriptor *entry,
  741. int ifidx)
  742. {
  743. if (entry->occupied && (ifidx == -1 || ifidx == entry->interface_id)) {
  744. brcmf_fws_psq_flush(fws, &entry->psq, ifidx);
  745. entry->occupied = !!(entry->psq.len);
  746. }
  747. }
  748. static void brcmf_fws_bus_txq_cleanup(struct brcmf_fws_info *fws,
  749. bool (*fn)(struct sk_buff *, void *),
  750. int ifidx)
  751. {
  752. struct brcmf_fws_hanger_item *hi;
  753. struct pktq *txq;
  754. struct sk_buff *skb;
  755. int prec;
  756. u32 hslot;
  757. txq = brcmf_bus_gettxq(fws->drvr->bus_if);
  758. if (IS_ERR(txq)) {
  759. brcmf_dbg(TRACE, "no txq to clean up\n");
  760. return;
  761. }
  762. for (prec = 0; prec < txq->num_prec; prec++) {
  763. skb = brcmu_pktq_pdeq_match(txq, prec, fn, &ifidx);
  764. while (skb) {
  765. hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
  766. hi = &fws->hanger.items[hslot];
  767. WARN_ON(skb != hi->pkt);
  768. hi->state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
  769. brcmu_pkt_buf_free_skb(skb);
  770. skb = brcmu_pktq_pdeq_match(txq, prec, fn, &ifidx);
  771. }
  772. }
  773. }
  774. static void brcmf_fws_cleanup(struct brcmf_fws_info *fws, int ifidx)
  775. {
  776. int i;
  777. struct brcmf_fws_mac_descriptor *table;
  778. bool (*matchfn)(struct sk_buff *, void *) = NULL;
  779. if (fws == NULL)
  780. return;
  781. if (ifidx != -1)
  782. matchfn = brcmf_fws_ifidx_match;
  783. /* cleanup individual nodes */
  784. table = &fws->desc.nodes[0];
  785. for (i = 0; i < ARRAY_SIZE(fws->desc.nodes); i++)
  786. brcmf_fws_macdesc_cleanup(fws, &table[i], ifidx);
  787. brcmf_fws_macdesc_cleanup(fws, &fws->desc.other, ifidx);
  788. brcmf_fws_bus_txq_cleanup(fws, matchfn, ifidx);
  789. brcmf_fws_hanger_cleanup(fws, matchfn, ifidx);
  790. }
  791. static u8 brcmf_fws_hdrpush(struct brcmf_fws_info *fws, struct sk_buff *skb)
  792. {
  793. struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
  794. u8 *wlh;
  795. u16 data_offset = 0;
  796. u8 fillers;
  797. __le32 pkttag = cpu_to_le32(brcmf_skbcb(skb)->htod);
  798. __le16 pktseq = cpu_to_le16(brcmf_skbcb(skb)->htod_seq);
  799. brcmf_dbg(TRACE, "enter: %s, idx=%d hslot=%d htod %X seq %X\n",
  800. entry->name, brcmf_skb_if_flags_get_field(skb, INDEX),
  801. (le32_to_cpu(pkttag) >> 8) & 0xffff,
  802. brcmf_skbcb(skb)->htod, brcmf_skbcb(skb)->htod_seq);
  803. if (entry->send_tim_signal)
  804. data_offset += 2 + BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN;
  805. if (BRCMF_FWS_MODE_GET_REUSESEQ(fws->mode))
  806. data_offset += BRCMF_FWS_TYPE_SEQ_LEN;
  807. /* +2 is for Type[1] and Len[1] in TLV, plus TIM signal */
  808. data_offset += 2 + BRCMF_FWS_TYPE_PKTTAG_LEN;
  809. fillers = round_up(data_offset, 4) - data_offset;
  810. data_offset += fillers;
  811. skb_push(skb, data_offset);
  812. wlh = skb->data;
  813. wlh[0] = BRCMF_FWS_TYPE_PKTTAG;
  814. wlh[1] = BRCMF_FWS_TYPE_PKTTAG_LEN;
  815. memcpy(&wlh[2], &pkttag, sizeof(pkttag));
  816. if (BRCMF_FWS_MODE_GET_REUSESEQ(fws->mode)) {
  817. wlh[1] += BRCMF_FWS_TYPE_SEQ_LEN;
  818. memcpy(&wlh[2 + BRCMF_FWS_TYPE_PKTTAG_LEN], &pktseq,
  819. sizeof(pktseq));
  820. }
  821. wlh += wlh[1] + 2;
  822. if (entry->send_tim_signal) {
  823. entry->send_tim_signal = 0;
  824. wlh[0] = BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP;
  825. wlh[1] = BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN;
  826. wlh[2] = entry->mac_handle;
  827. wlh[3] = entry->traffic_pending_bmp;
  828. brcmf_dbg(TRACE, "adding TIM info: handle %d bmp 0x%X\n",
  829. entry->mac_handle, entry->traffic_pending_bmp);
  830. wlh += BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN + 2;
  831. entry->traffic_lastreported_bmp = entry->traffic_pending_bmp;
  832. }
  833. if (fillers)
  834. memset(wlh, BRCMF_FWS_TYPE_FILLER, fillers);
  835. return (u8)(data_offset >> 2);
  836. }
  837. static bool brcmf_fws_tim_update(struct brcmf_fws_info *fws,
  838. struct brcmf_fws_mac_descriptor *entry,
  839. int fifo, bool send_immediately)
  840. {
  841. struct sk_buff *skb;
  842. struct brcmf_skbuff_cb *skcb;
  843. s32 err;
  844. u32 len;
  845. u8 data_offset;
  846. int ifidx;
  847. /* check delayedQ and suppressQ in one call using bitmap */
  848. if (brcmu_pktq_mlen(&entry->psq, 3 << (fifo * 2)) == 0)
  849. entry->traffic_pending_bmp &= ~NBITVAL(fifo);
  850. else
  851. entry->traffic_pending_bmp |= NBITVAL(fifo);
  852. entry->send_tim_signal = false;
  853. if (entry->traffic_lastreported_bmp != entry->traffic_pending_bmp)
  854. entry->send_tim_signal = true;
  855. if (send_immediately && entry->send_tim_signal &&
  856. entry->state == BRCMF_FWS_STATE_CLOSE) {
  857. /* create a dummy packet and sent that. The traffic */
  858. /* bitmap info will automatically be attached to that packet */
  859. len = BRCMF_FWS_TYPE_PKTTAG_LEN + 2 +
  860. BRCMF_FWS_TYPE_SEQ_LEN +
  861. BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN + 2 +
  862. 4 + fws->drvr->hdrlen;
  863. skb = brcmu_pkt_buf_get_skb(len);
  864. if (skb == NULL)
  865. return false;
  866. skb_pull(skb, len);
  867. skcb = brcmf_skbcb(skb);
  868. skcb->mac = entry;
  869. skcb->state = BRCMF_FWS_SKBSTATE_TIM;
  870. skcb->htod = 0;
  871. skcb->htod_seq = 0;
  872. data_offset = brcmf_fws_hdrpush(fws, skb);
  873. ifidx = brcmf_skb_if_flags_get_field(skb, INDEX);
  874. brcmf_fws_unlock(fws);
  875. err = brcmf_proto_txdata(fws->drvr, ifidx, data_offset, skb);
  876. brcmf_fws_lock(fws);
  877. if (err)
  878. brcmu_pkt_buf_free_skb(skb);
  879. return true;
  880. }
  881. return false;
  882. }
  883. static void
  884. brcmf_fws_flow_control_check(struct brcmf_fws_info *fws, struct pktq *pq,
  885. u8 if_id)
  886. {
  887. struct brcmf_if *ifp = brcmf_get_ifp(fws->drvr, if_id);
  888. if (WARN_ON(!ifp))
  889. return;
  890. if ((ifp->netif_stop & BRCMF_NETIF_STOP_REASON_FWS_FC) &&
  891. pq->len <= BRCMF_FWS_FLOWCONTROL_LOWATER)
  892. brcmf_txflowblock_if(ifp,
  893. BRCMF_NETIF_STOP_REASON_FWS_FC, false);
  894. if (!(ifp->netif_stop & BRCMF_NETIF_STOP_REASON_FWS_FC) &&
  895. pq->len >= BRCMF_FWS_FLOWCONTROL_HIWATER) {
  896. fws->stats.fws_flow_block++;
  897. brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_FWS_FC, true);
  898. }
  899. return;
  900. }
  901. static int brcmf_fws_rssi_indicate(struct brcmf_fws_info *fws, s8 rssi)
  902. {
  903. brcmf_dbg(CTL, "rssi %d\n", rssi);
  904. return 0;
  905. }
  906. static
  907. int brcmf_fws_macdesc_indicate(struct brcmf_fws_info *fws, u8 type, u8 *data)
  908. {
  909. struct brcmf_fws_mac_descriptor *entry, *existing;
  910. u8 mac_handle;
  911. u8 ifidx;
  912. u8 *addr;
  913. mac_handle = *data++;
  914. ifidx = *data++;
  915. addr = data;
  916. entry = &fws->desc.nodes[mac_handle & 0x1F];
  917. if (type == BRCMF_FWS_TYPE_MACDESC_DEL) {
  918. if (entry->occupied) {
  919. brcmf_dbg(TRACE, "deleting %s mac %pM\n",
  920. entry->name, addr);
  921. brcmf_fws_lock(fws);
  922. brcmf_fws_macdesc_cleanup(fws, entry, -1);
  923. brcmf_fws_macdesc_deinit(entry);
  924. brcmf_fws_unlock(fws);
  925. } else
  926. fws->stats.mac_update_failed++;
  927. return 0;
  928. }
  929. existing = brcmf_fws_macdesc_lookup(fws, addr);
  930. if (IS_ERR(existing)) {
  931. if (!entry->occupied) {
  932. brcmf_fws_lock(fws);
  933. entry->mac_handle = mac_handle;
  934. brcmf_fws_macdesc_init(entry, addr, ifidx);
  935. brcmf_fws_macdesc_set_name(fws, entry);
  936. brcmu_pktq_init(&entry->psq, BRCMF_FWS_PSQ_PREC_COUNT,
  937. BRCMF_FWS_PSQ_LEN);
  938. brcmf_fws_unlock(fws);
  939. brcmf_dbg(TRACE, "add %s mac %pM\n", entry->name, addr);
  940. } else {
  941. fws->stats.mac_update_failed++;
  942. }
  943. } else {
  944. if (entry != existing) {
  945. brcmf_dbg(TRACE, "copy mac %s\n", existing->name);
  946. brcmf_fws_lock(fws);
  947. memcpy(entry, existing,
  948. offsetof(struct brcmf_fws_mac_descriptor, psq));
  949. entry->mac_handle = mac_handle;
  950. brcmf_fws_macdesc_deinit(existing);
  951. brcmf_fws_macdesc_set_name(fws, entry);
  952. brcmf_fws_unlock(fws);
  953. brcmf_dbg(TRACE, "relocate %s mac %pM\n", entry->name,
  954. addr);
  955. } else {
  956. brcmf_dbg(TRACE, "use existing\n");
  957. WARN_ON(entry->mac_handle != mac_handle);
  958. /* TODO: what should we do here: continue, reinit, .. */
  959. }
  960. }
  961. return 0;
  962. }
  963. static int brcmf_fws_macdesc_state_indicate(struct brcmf_fws_info *fws,
  964. u8 type, u8 *data)
  965. {
  966. struct brcmf_fws_mac_descriptor *entry;
  967. u8 mac_handle;
  968. int ret;
  969. mac_handle = data[0];
  970. entry = &fws->desc.nodes[mac_handle & 0x1F];
  971. if (!entry->occupied) {
  972. fws->stats.mac_ps_update_failed++;
  973. return -ESRCH;
  974. }
  975. brcmf_fws_lock(fws);
  976. /* a state update should wipe old credits */
  977. entry->requested_credit = 0;
  978. entry->requested_packet = 0;
  979. if (type == BRCMF_FWS_TYPE_MAC_OPEN) {
  980. entry->state = BRCMF_FWS_STATE_OPEN;
  981. ret = BRCMF_FWS_RET_OK_SCHEDULE;
  982. } else {
  983. entry->state = BRCMF_FWS_STATE_CLOSE;
  984. brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_BK, false);
  985. brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_BE, false);
  986. brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_VI, false);
  987. brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_VO, true);
  988. ret = BRCMF_FWS_RET_OK_NOSCHEDULE;
  989. }
  990. brcmf_fws_unlock(fws);
  991. return ret;
  992. }
  993. static int brcmf_fws_interface_state_indicate(struct brcmf_fws_info *fws,
  994. u8 type, u8 *data)
  995. {
  996. struct brcmf_fws_mac_descriptor *entry;
  997. u8 ifidx;
  998. int ret;
  999. ifidx = data[0];
  1000. if (ifidx >= BRCMF_MAX_IFS) {
  1001. ret = -ERANGE;
  1002. goto fail;
  1003. }
  1004. entry = &fws->desc.iface[ifidx];
  1005. if (!entry->occupied) {
  1006. ret = -ESRCH;
  1007. goto fail;
  1008. }
  1009. brcmf_dbg(TRACE, "%s (%d): %s\n", brcmf_fws_get_tlv_name(type), type,
  1010. entry->name);
  1011. brcmf_fws_lock(fws);
  1012. switch (type) {
  1013. case BRCMF_FWS_TYPE_INTERFACE_OPEN:
  1014. entry->state = BRCMF_FWS_STATE_OPEN;
  1015. ret = BRCMF_FWS_RET_OK_SCHEDULE;
  1016. break;
  1017. case BRCMF_FWS_TYPE_INTERFACE_CLOSE:
  1018. entry->state = BRCMF_FWS_STATE_CLOSE;
  1019. ret = BRCMF_FWS_RET_OK_NOSCHEDULE;
  1020. break;
  1021. default:
  1022. ret = -EINVAL;
  1023. brcmf_fws_unlock(fws);
  1024. goto fail;
  1025. }
  1026. brcmf_fws_unlock(fws);
  1027. return ret;
  1028. fail:
  1029. fws->stats.if_update_failed++;
  1030. return ret;
  1031. }
  1032. static int brcmf_fws_request_indicate(struct brcmf_fws_info *fws, u8 type,
  1033. u8 *data)
  1034. {
  1035. struct brcmf_fws_mac_descriptor *entry;
  1036. entry = &fws->desc.nodes[data[1] & 0x1F];
  1037. if (!entry->occupied) {
  1038. if (type == BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT)
  1039. fws->stats.credit_request_failed++;
  1040. else
  1041. fws->stats.packet_request_failed++;
  1042. return -ESRCH;
  1043. }
  1044. brcmf_dbg(TRACE, "%s (%d): %s cnt %d bmp %d\n",
  1045. brcmf_fws_get_tlv_name(type), type, entry->name,
  1046. data[0], data[2]);
  1047. brcmf_fws_lock(fws);
  1048. if (type == BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT)
  1049. entry->requested_credit = data[0];
  1050. else
  1051. entry->requested_packet = data[0];
  1052. entry->ac_bitmap = data[2];
  1053. brcmf_fws_unlock(fws);
  1054. return BRCMF_FWS_RET_OK_SCHEDULE;
  1055. }
  1056. static void
  1057. brcmf_fws_macdesc_use_req_credit(struct brcmf_fws_mac_descriptor *entry,
  1058. struct sk_buff *skb)
  1059. {
  1060. if (entry->requested_credit > 0) {
  1061. entry->requested_credit--;
  1062. brcmf_skb_if_flags_set_field(skb, REQUESTED, 1);
  1063. brcmf_skb_if_flags_set_field(skb, REQ_CREDIT, 1);
  1064. if (entry->state != BRCMF_FWS_STATE_CLOSE)
  1065. brcmf_err("requested credit set while mac not closed!\n");
  1066. } else if (entry->requested_packet > 0) {
  1067. entry->requested_packet--;
  1068. brcmf_skb_if_flags_set_field(skb, REQUESTED, 1);
  1069. brcmf_skb_if_flags_set_field(skb, REQ_CREDIT, 0);
  1070. if (entry->state != BRCMF_FWS_STATE_CLOSE)
  1071. brcmf_err("requested packet set while mac not closed!\n");
  1072. } else {
  1073. brcmf_skb_if_flags_set_field(skb, REQUESTED, 0);
  1074. brcmf_skb_if_flags_set_field(skb, REQ_CREDIT, 0);
  1075. }
  1076. }
  1077. static void brcmf_fws_macdesc_return_req_credit(struct sk_buff *skb)
  1078. {
  1079. struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
  1080. if ((brcmf_skb_if_flags_get_field(skb, REQ_CREDIT)) &&
  1081. (entry->state == BRCMF_FWS_STATE_CLOSE))
  1082. entry->requested_credit++;
  1083. }
  1084. static void brcmf_fws_return_credits(struct brcmf_fws_info *fws,
  1085. u8 fifo, u8 credits)
  1086. {
  1087. int lender_ac;
  1088. int *borrowed;
  1089. int *fifo_credit;
  1090. if (!credits)
  1091. return;
  1092. fws->fifo_credit_map |= 1 << fifo;
  1093. if ((fifo == BRCMF_FWS_FIFO_AC_BE) &&
  1094. (fws->credits_borrowed[0])) {
  1095. for (lender_ac = BRCMF_FWS_FIFO_AC_VO; lender_ac >= 0;
  1096. lender_ac--) {
  1097. borrowed = &fws->credits_borrowed[lender_ac];
  1098. if (*borrowed) {
  1099. fws->fifo_credit_map |= (1 << lender_ac);
  1100. fifo_credit = &fws->fifo_credit[lender_ac];
  1101. if (*borrowed >= credits) {
  1102. *borrowed -= credits;
  1103. *fifo_credit += credits;
  1104. return;
  1105. } else {
  1106. credits -= *borrowed;
  1107. *fifo_credit += *borrowed;
  1108. *borrowed = 0;
  1109. }
  1110. }
  1111. }
  1112. }
  1113. fws->fifo_credit[fifo] += credits;
  1114. }
  1115. static void brcmf_fws_schedule_deq(struct brcmf_fws_info *fws)
  1116. {
  1117. /* only schedule dequeue when there are credits for delayed traffic */
  1118. if ((fws->fifo_credit_map & fws->fifo_delay_map) ||
  1119. (!brcmf_fws_fc_active(fws) && fws->fifo_delay_map))
  1120. queue_work(fws->fws_wq, &fws->fws_dequeue_work);
  1121. }
  1122. static int brcmf_fws_enq(struct brcmf_fws_info *fws,
  1123. enum brcmf_fws_skb_state state, int fifo,
  1124. struct sk_buff *p)
  1125. {
  1126. int prec = 2 * fifo;
  1127. u32 *qfull_stat = &fws->stats.delayq_full_error;
  1128. struct brcmf_fws_mac_descriptor *entry;
  1129. struct pktq *pq;
  1130. struct sk_buff_head *queue;
  1131. struct sk_buff *p_head;
  1132. struct sk_buff *p_tail;
  1133. u32 fr_new;
  1134. u32 fr_compare;
  1135. entry = brcmf_skbcb(p)->mac;
  1136. if (entry == NULL) {
  1137. brcmf_err("no mac descriptor found for skb %p\n", p);
  1138. return -ENOENT;
  1139. }
  1140. brcmf_dbg(DATA, "enter: fifo %d skb %p\n", fifo, p);
  1141. if (state == BRCMF_FWS_SKBSTATE_SUPPRESSED) {
  1142. prec += 1;
  1143. qfull_stat = &fws->stats.supprq_full_error;
  1144. /* Fix out of order delivery of frames. Dont assume frame */
  1145. /* can be inserted at the end, but look for correct position */
  1146. pq = &entry->psq;
  1147. if (pktq_full(pq) || pktq_pfull(pq, prec)) {
  1148. *qfull_stat += 1;
  1149. return -ENFILE;
  1150. }
  1151. queue = &pq->q[prec].skblist;
  1152. p_head = skb_peek(queue);
  1153. p_tail = skb_peek_tail(queue);
  1154. fr_new = brcmf_skb_htod_tag_get_field(p, FREERUN);
  1155. while (p_head != p_tail) {
  1156. fr_compare = brcmf_skb_htod_tag_get_field(p_tail,
  1157. FREERUN);
  1158. /* be sure to handle wrap of 256 */
  1159. if (((fr_new > fr_compare) &&
  1160. ((fr_new - fr_compare) < 128)) ||
  1161. ((fr_new < fr_compare) &&
  1162. ((fr_compare - fr_new) > 128)))
  1163. break;
  1164. p_tail = skb_queue_prev(queue, p_tail);
  1165. }
  1166. /* Position found. Determine what to do */
  1167. if (p_tail == NULL) {
  1168. /* empty list */
  1169. __skb_queue_tail(queue, p);
  1170. } else {
  1171. fr_compare = brcmf_skb_htod_tag_get_field(p_tail,
  1172. FREERUN);
  1173. if (((fr_new > fr_compare) &&
  1174. ((fr_new - fr_compare) < 128)) ||
  1175. ((fr_new < fr_compare) &&
  1176. ((fr_compare - fr_new) > 128))) {
  1177. /* After tail */
  1178. __skb_queue_after(queue, p_tail, p);
  1179. } else {
  1180. /* Before tail */
  1181. __skb_insert(p, p_tail->prev, p_tail, queue);
  1182. }
  1183. }
  1184. /* Complete the counters and statistics */
  1185. pq->len++;
  1186. if (pq->hi_prec < prec)
  1187. pq->hi_prec = (u8) prec;
  1188. } else if (brcmu_pktq_penq(&entry->psq, prec, p) == NULL) {
  1189. *qfull_stat += 1;
  1190. return -ENFILE;
  1191. }
  1192. /* increment total enqueued packet count */
  1193. fws->fifo_delay_map |= 1 << fifo;
  1194. fws->fifo_enqpkt[fifo]++;
  1195. /* update the sk_buff state */
  1196. brcmf_skbcb(p)->state = state;
  1197. /*
  1198. * A packet has been pushed so update traffic
  1199. * availability bitmap, if applicable
  1200. */
  1201. brcmf_fws_tim_update(fws, entry, fifo, true);
  1202. brcmf_fws_flow_control_check(fws, &entry->psq,
  1203. brcmf_skb_if_flags_get_field(p, INDEX));
  1204. return 0;
  1205. }
  1206. static struct sk_buff *brcmf_fws_deq(struct brcmf_fws_info *fws, int fifo)
  1207. {
  1208. struct brcmf_fws_mac_descriptor *table;
  1209. struct brcmf_fws_mac_descriptor *entry;
  1210. struct sk_buff *p;
  1211. int num_nodes;
  1212. int node_pos;
  1213. int prec_out;
  1214. int pmsk;
  1215. int i;
  1216. table = (struct brcmf_fws_mac_descriptor *)&fws->desc;
  1217. num_nodes = sizeof(fws->desc) / sizeof(struct brcmf_fws_mac_descriptor);
  1218. node_pos = fws->deq_node_pos[fifo];
  1219. for (i = 0; i < num_nodes; i++) {
  1220. entry = &table[(node_pos + i) % num_nodes];
  1221. if (!entry->occupied ||
  1222. brcmf_fws_macdesc_closed(fws, entry, fifo))
  1223. continue;
  1224. if (entry->suppressed)
  1225. pmsk = 2;
  1226. else
  1227. pmsk = 3;
  1228. p = brcmu_pktq_mdeq(&entry->psq, pmsk << (fifo * 2), &prec_out);
  1229. if (p == NULL) {
  1230. if (entry->suppressed) {
  1231. if (entry->suppr_transit_count)
  1232. continue;
  1233. entry->suppressed = false;
  1234. p = brcmu_pktq_mdeq(&entry->psq,
  1235. 1 << (fifo * 2), &prec_out);
  1236. }
  1237. }
  1238. if (p == NULL)
  1239. continue;
  1240. brcmf_fws_macdesc_use_req_credit(entry, p);
  1241. /* move dequeue position to ensure fair round-robin */
  1242. fws->deq_node_pos[fifo] = (node_pos + i + 1) % num_nodes;
  1243. brcmf_fws_flow_control_check(fws, &entry->psq,
  1244. brcmf_skb_if_flags_get_field(p,
  1245. INDEX)
  1246. );
  1247. /*
  1248. * A packet has been picked up, update traffic
  1249. * availability bitmap, if applicable
  1250. */
  1251. brcmf_fws_tim_update(fws, entry, fifo, false);
  1252. /*
  1253. * decrement total enqueued fifo packets and
  1254. * clear delay bitmap if done.
  1255. */
  1256. fws->fifo_enqpkt[fifo]--;
  1257. if (fws->fifo_enqpkt[fifo] == 0)
  1258. fws->fifo_delay_map &= ~(1 << fifo);
  1259. goto done;
  1260. }
  1261. p = NULL;
  1262. done:
  1263. brcmf_dbg(DATA, "exit: fifo %d skb %p\n", fifo, p);
  1264. return p;
  1265. }
  1266. static int brcmf_fws_txstatus_suppressed(struct brcmf_fws_info *fws, int fifo,
  1267. struct sk_buff *skb,
  1268. u32 genbit, u16 seq)
  1269. {
  1270. struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
  1271. u32 hslot;
  1272. int ret;
  1273. hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
  1274. /* this packet was suppressed */
  1275. if (!entry->suppressed) {
  1276. entry->suppressed = true;
  1277. entry->suppr_transit_count = entry->transit_count;
  1278. brcmf_dbg(DATA, "suppress %s: transit %d\n",
  1279. entry->name, entry->transit_count);
  1280. }
  1281. entry->generation = genbit;
  1282. brcmf_skb_htod_tag_set_field(skb, GENERATION, genbit);
  1283. brcmf_skbcb(skb)->htod_seq = seq;
  1284. if (brcmf_skb_htod_seq_get_field(skb, FROMFW)) {
  1285. brcmf_skb_htod_seq_set_field(skb, FROMDRV, 1);
  1286. brcmf_skb_htod_seq_set_field(skb, FROMFW, 0);
  1287. } else {
  1288. brcmf_skb_htod_seq_set_field(skb, FROMDRV, 0);
  1289. }
  1290. ret = brcmf_fws_enq(fws, BRCMF_FWS_SKBSTATE_SUPPRESSED, fifo, skb);
  1291. if (ret != 0) {
  1292. /* suppress q is full drop this packet */
  1293. brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &skb, true);
  1294. } else {
  1295. /* Mark suppressed to avoid a double free during wlfc cleanup */
  1296. brcmf_fws_hanger_mark_suppressed(&fws->hanger, hslot);
  1297. }
  1298. return ret;
  1299. }
  1300. static int
  1301. brcmf_fws_txs_process(struct brcmf_fws_info *fws, u8 flags, u32 hslot,
  1302. u32 genbit, u16 seq)
  1303. {
  1304. u32 fifo;
  1305. int ret;
  1306. bool remove_from_hanger = true;
  1307. struct sk_buff *skb;
  1308. struct brcmf_skbuff_cb *skcb;
  1309. struct brcmf_fws_mac_descriptor *entry = NULL;
  1310. struct brcmf_if *ifp;
  1311. brcmf_dbg(DATA, "flags %d\n", flags);
  1312. if (flags == BRCMF_FWS_TXSTATUS_DISCARD)
  1313. fws->stats.txs_discard++;
  1314. else if (flags == BRCMF_FWS_TXSTATUS_CORE_SUPPRESS) {
  1315. fws->stats.txs_supp_core++;
  1316. remove_from_hanger = false;
  1317. } else if (flags == BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS) {
  1318. fws->stats.txs_supp_ps++;
  1319. remove_from_hanger = false;
  1320. } else if (flags == BRCMF_FWS_TXSTATUS_FW_TOSSED)
  1321. fws->stats.txs_tossed++;
  1322. else if (flags == BRCMF_FWS_TXSTATUS_HOST_TOSSED)
  1323. fws->stats.txs_host_tossed++;
  1324. else
  1325. brcmf_err("unexpected txstatus\n");
  1326. ret = brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &skb,
  1327. remove_from_hanger);
  1328. if (ret != 0) {
  1329. brcmf_err("no packet in hanger slot: hslot=%d\n", hslot);
  1330. return ret;
  1331. }
  1332. skcb = brcmf_skbcb(skb);
  1333. entry = skcb->mac;
  1334. if (WARN_ON(!entry)) {
  1335. brcmu_pkt_buf_free_skb(skb);
  1336. return -EINVAL;
  1337. }
  1338. entry->transit_count--;
  1339. if (entry->suppressed && entry->suppr_transit_count)
  1340. entry->suppr_transit_count--;
  1341. brcmf_dbg(DATA, "%s flags %d htod %X seq %X\n", entry->name, flags,
  1342. skcb->htod, seq);
  1343. /* pick up the implicit credit from this packet */
  1344. fifo = brcmf_skb_htod_tag_get_field(skb, FIFO);
  1345. if ((fws->fcmode == BRCMF_FWS_FCMODE_IMPLIED_CREDIT) ||
  1346. (brcmf_skb_if_flags_get_field(skb, REQ_CREDIT)) ||
  1347. (flags == BRCMF_FWS_TXSTATUS_HOST_TOSSED)) {
  1348. brcmf_fws_return_credits(fws, fifo, 1);
  1349. brcmf_fws_schedule_deq(fws);
  1350. }
  1351. brcmf_fws_macdesc_return_req_credit(skb);
  1352. ret = brcmf_proto_hdrpull(fws->drvr, false, skb, &ifp);
  1353. if (ret) {
  1354. brcmu_pkt_buf_free_skb(skb);
  1355. return -EINVAL;
  1356. }
  1357. if (!remove_from_hanger)
  1358. ret = brcmf_fws_txstatus_suppressed(fws, fifo, skb,
  1359. genbit, seq);
  1360. if (remove_from_hanger || ret)
  1361. brcmf_txfinalize(ifp, skb, true);
  1362. return 0;
  1363. }
  1364. static int brcmf_fws_fifocreditback_indicate(struct brcmf_fws_info *fws,
  1365. u8 *data)
  1366. {
  1367. int i;
  1368. if (fws->fcmode != BRCMF_FWS_FCMODE_EXPLICIT_CREDIT) {
  1369. brcmf_dbg(INFO, "ignored\n");
  1370. return BRCMF_FWS_RET_OK_NOSCHEDULE;
  1371. }
  1372. brcmf_dbg(DATA, "enter: data %pM\n", data);
  1373. brcmf_fws_lock(fws);
  1374. for (i = 0; i < BRCMF_FWS_FIFO_COUNT; i++)
  1375. brcmf_fws_return_credits(fws, i, data[i]);
  1376. brcmf_dbg(DATA, "map: credit %x delay %x\n", fws->fifo_credit_map,
  1377. fws->fifo_delay_map);
  1378. brcmf_fws_unlock(fws);
  1379. return BRCMF_FWS_RET_OK_SCHEDULE;
  1380. }
  1381. static int brcmf_fws_txstatus_indicate(struct brcmf_fws_info *fws, u8 *data)
  1382. {
  1383. __le32 status_le;
  1384. __le16 seq_le;
  1385. u32 status;
  1386. u32 hslot;
  1387. u32 genbit;
  1388. u8 flags;
  1389. u16 seq;
  1390. fws->stats.txs_indicate++;
  1391. memcpy(&status_le, data, sizeof(status_le));
  1392. status = le32_to_cpu(status_le);
  1393. flags = brcmf_txstatus_get_field(status, FLAGS);
  1394. hslot = brcmf_txstatus_get_field(status, HSLOT);
  1395. genbit = brcmf_txstatus_get_field(status, GENERATION);
  1396. if (BRCMF_FWS_MODE_GET_REUSESEQ(fws->mode)) {
  1397. memcpy(&seq_le, &data[BRCMF_FWS_TYPE_PKTTAG_LEN],
  1398. sizeof(seq_le));
  1399. seq = le16_to_cpu(seq_le);
  1400. } else {
  1401. seq = 0;
  1402. }
  1403. brcmf_fws_lock(fws);
  1404. brcmf_fws_txs_process(fws, flags, hslot, genbit, seq);
  1405. brcmf_fws_unlock(fws);
  1406. return BRCMF_FWS_RET_OK_NOSCHEDULE;
  1407. }
  1408. static int brcmf_fws_dbg_seqnum_check(struct brcmf_fws_info *fws, u8 *data)
  1409. {
  1410. __le32 timestamp;
  1411. memcpy(&timestamp, &data[2], sizeof(timestamp));
  1412. brcmf_dbg(CTL, "received: seq %d, timestamp %d\n", data[1],
  1413. le32_to_cpu(timestamp));
  1414. return 0;
  1415. }
  1416. static int brcmf_fws_notify_credit_map(struct brcmf_if *ifp,
  1417. const struct brcmf_event_msg *e,
  1418. void *data)
  1419. {
  1420. struct brcmf_fws_info *fws = drvr_to_fws(ifp->drvr);
  1421. int i;
  1422. u8 *credits = data;
  1423. if (e->datalen < BRCMF_FWS_FIFO_COUNT) {
  1424. brcmf_err("event payload too small (%d)\n", e->datalen);
  1425. return -EINVAL;
  1426. }
  1427. if (fws->creditmap_received)
  1428. return 0;
  1429. fws->creditmap_received = true;
  1430. brcmf_dbg(TRACE, "enter: credits %pM\n", credits);
  1431. brcmf_fws_lock(fws);
  1432. for (i = 0; i < ARRAY_SIZE(fws->fifo_credit); i++) {
  1433. if (*credits)
  1434. fws->fifo_credit_map |= 1 << i;
  1435. else
  1436. fws->fifo_credit_map &= ~(1 << i);
  1437. fws->fifo_credit[i] = *credits++;
  1438. }
  1439. brcmf_fws_schedule_deq(fws);
  1440. brcmf_fws_unlock(fws);
  1441. return 0;
  1442. }
  1443. static int brcmf_fws_notify_bcmc_credit_support(struct brcmf_if *ifp,
  1444. const struct brcmf_event_msg *e,
  1445. void *data)
  1446. {
  1447. struct brcmf_fws_info *fws = drvr_to_fws(ifp->drvr);
  1448. if (fws) {
  1449. brcmf_fws_lock(fws);
  1450. fws->bcmc_credit_check = true;
  1451. brcmf_fws_unlock(fws);
  1452. }
  1453. return 0;
  1454. }
  1455. static void brcmf_rxreorder_get_skb_list(struct brcmf_ampdu_rx_reorder *rfi,
  1456. u8 start, u8 end,
  1457. struct sk_buff_head *skb_list)
  1458. {
  1459. /* initialize return list */
  1460. __skb_queue_head_init(skb_list);
  1461. if (rfi->pend_pkts == 0) {
  1462. brcmf_dbg(INFO, "no packets in reorder queue\n");
  1463. return;
  1464. }
  1465. do {
  1466. if (rfi->pktslots[start]) {
  1467. __skb_queue_tail(skb_list, rfi->pktslots[start]);
  1468. rfi->pktslots[start] = NULL;
  1469. }
  1470. start++;
  1471. if (start > rfi->max_idx)
  1472. start = 0;
  1473. } while (start != end);
  1474. rfi->pend_pkts -= skb_queue_len(skb_list);
  1475. }
  1476. void brcmf_fws_rxreorder(struct brcmf_if *ifp, struct sk_buff *pkt)
  1477. {
  1478. u8 *reorder_data;
  1479. u8 flow_id, max_idx, cur_idx, exp_idx, end_idx;
  1480. struct brcmf_ampdu_rx_reorder *rfi;
  1481. struct sk_buff_head reorder_list;
  1482. struct sk_buff *pnext;
  1483. u8 flags;
  1484. u32 buf_size;
  1485. reorder_data = ((struct brcmf_skb_reorder_data *)pkt->cb)->reorder;
  1486. flow_id = reorder_data[BRCMF_RXREORDER_FLOWID_OFFSET];
  1487. flags = reorder_data[BRCMF_RXREORDER_FLAGS_OFFSET];
  1488. /* validate flags and flow id */
  1489. if (flags == 0xFF) {
  1490. brcmf_err("invalid flags...so ignore this packet\n");
  1491. brcmf_netif_rx(ifp, pkt);
  1492. return;
  1493. }
  1494. rfi = ifp->drvr->reorder_flows[flow_id];
  1495. if (flags & BRCMF_RXREORDER_DEL_FLOW) {
  1496. brcmf_dbg(INFO, "flow-%d: delete\n",
  1497. flow_id);
  1498. if (rfi == NULL) {
  1499. brcmf_dbg(INFO, "received flags to cleanup, but no flow (%d) yet\n",
  1500. flow_id);
  1501. brcmf_netif_rx(ifp, pkt);
  1502. return;
  1503. }
  1504. brcmf_rxreorder_get_skb_list(rfi, rfi->exp_idx, rfi->exp_idx,
  1505. &reorder_list);
  1506. /* add the last packet */
  1507. __skb_queue_tail(&reorder_list, pkt);
  1508. kfree(rfi);
  1509. ifp->drvr->reorder_flows[flow_id] = NULL;
  1510. goto netif_rx;
  1511. }
  1512. /* from here on we need a flow reorder instance */
  1513. if (rfi == NULL) {
  1514. buf_size = sizeof(*rfi);
  1515. max_idx = reorder_data[BRCMF_RXREORDER_MAXIDX_OFFSET];
  1516. buf_size += (max_idx + 1) * sizeof(pkt);
  1517. /* allocate space for flow reorder info */
  1518. brcmf_dbg(INFO, "flow-%d: start, maxidx %d\n",
  1519. flow_id, max_idx);
  1520. rfi = kzalloc(buf_size, GFP_ATOMIC);
  1521. if (rfi == NULL) {
  1522. brcmf_err("failed to alloc buffer\n");
  1523. brcmf_netif_rx(ifp, pkt);
  1524. return;
  1525. }
  1526. ifp->drvr->reorder_flows[flow_id] = rfi;
  1527. rfi->pktslots = (struct sk_buff **)(rfi + 1);
  1528. rfi->max_idx = max_idx;
  1529. }
  1530. if (flags & BRCMF_RXREORDER_NEW_HOLE) {
  1531. if (rfi->pend_pkts) {
  1532. brcmf_rxreorder_get_skb_list(rfi, rfi->exp_idx,
  1533. rfi->exp_idx,
  1534. &reorder_list);
  1535. WARN_ON(rfi->pend_pkts);
  1536. } else {
  1537. __skb_queue_head_init(&reorder_list);
  1538. }
  1539. rfi->cur_idx = reorder_data[BRCMF_RXREORDER_CURIDX_OFFSET];
  1540. rfi->exp_idx = reorder_data[BRCMF_RXREORDER_EXPIDX_OFFSET];
  1541. rfi->max_idx = reorder_data[BRCMF_RXREORDER_MAXIDX_OFFSET];
  1542. rfi->pktslots[rfi->cur_idx] = pkt;
  1543. rfi->pend_pkts++;
  1544. brcmf_dbg(DATA, "flow-%d: new hole %d (%d), pending %d\n",
  1545. flow_id, rfi->cur_idx, rfi->exp_idx, rfi->pend_pkts);
  1546. } else if (flags & BRCMF_RXREORDER_CURIDX_VALID) {
  1547. cur_idx = reorder_data[BRCMF_RXREORDER_CURIDX_OFFSET];
  1548. exp_idx = reorder_data[BRCMF_RXREORDER_EXPIDX_OFFSET];
  1549. if ((exp_idx == rfi->exp_idx) && (cur_idx != rfi->exp_idx)) {
  1550. /* still in the current hole */
  1551. /* enqueue the current on the buffer chain */
  1552. if (rfi->pktslots[cur_idx] != NULL) {
  1553. brcmf_dbg(INFO, "HOLE: ERROR buffer pending..free it\n");
  1554. brcmu_pkt_buf_free_skb(rfi->pktslots[cur_idx]);
  1555. rfi->pktslots[cur_idx] = NULL;
  1556. }
  1557. rfi->pktslots[cur_idx] = pkt;
  1558. rfi->pend_pkts++;
  1559. rfi->cur_idx = cur_idx;
  1560. brcmf_dbg(DATA, "flow-%d: store pkt %d (%d), pending %d\n",
  1561. flow_id, cur_idx, exp_idx, rfi->pend_pkts);
  1562. /* can return now as there is no reorder
  1563. * list to process.
  1564. */
  1565. return;
  1566. }
  1567. if (rfi->exp_idx == cur_idx) {
  1568. if (rfi->pktslots[cur_idx] != NULL) {
  1569. brcmf_dbg(INFO, "error buffer pending..free it\n");
  1570. brcmu_pkt_buf_free_skb(rfi->pktslots[cur_idx]);
  1571. rfi->pktslots[cur_idx] = NULL;
  1572. }
  1573. rfi->pktslots[cur_idx] = pkt;
  1574. rfi->pend_pkts++;
  1575. /* got the expected one. flush from current to expected
  1576. * and update expected
  1577. */
  1578. brcmf_dbg(DATA, "flow-%d: expected %d (%d), pending %d\n",
  1579. flow_id, cur_idx, exp_idx, rfi->pend_pkts);
  1580. rfi->cur_idx = cur_idx;
  1581. rfi->exp_idx = exp_idx;
  1582. brcmf_rxreorder_get_skb_list(rfi, cur_idx, exp_idx,
  1583. &reorder_list);
  1584. brcmf_dbg(DATA, "flow-%d: freeing buffers %d, pending %d\n",
  1585. flow_id, skb_queue_len(&reorder_list),
  1586. rfi->pend_pkts);
  1587. } else {
  1588. u8 end_idx;
  1589. brcmf_dbg(DATA, "flow-%d (0x%x): both moved, old %d/%d, new %d/%d\n",
  1590. flow_id, flags, rfi->cur_idx, rfi->exp_idx,
  1591. cur_idx, exp_idx);
  1592. if (flags & BRCMF_RXREORDER_FLUSH_ALL)
  1593. end_idx = rfi->exp_idx;
  1594. else
  1595. end_idx = exp_idx;
  1596. /* flush pkts first */
  1597. brcmf_rxreorder_get_skb_list(rfi, rfi->exp_idx, end_idx,
  1598. &reorder_list);
  1599. if (exp_idx == ((cur_idx + 1) % (rfi->max_idx + 1))) {
  1600. __skb_queue_tail(&reorder_list, pkt);
  1601. } else {
  1602. rfi->pktslots[cur_idx] = pkt;
  1603. rfi->pend_pkts++;
  1604. }
  1605. rfi->exp_idx = exp_idx;
  1606. rfi->cur_idx = cur_idx;
  1607. }
  1608. } else {
  1609. /* explicity window move updating the expected index */
  1610. exp_idx = reorder_data[BRCMF_RXREORDER_EXPIDX_OFFSET];
  1611. brcmf_dbg(DATA, "flow-%d (0x%x): change expected: %d -> %d\n",
  1612. flow_id, flags, rfi->exp_idx, exp_idx);
  1613. if (flags & BRCMF_RXREORDER_FLUSH_ALL)
  1614. end_idx = rfi->exp_idx;
  1615. else
  1616. end_idx = exp_idx;
  1617. brcmf_rxreorder_get_skb_list(rfi, rfi->exp_idx, end_idx,
  1618. &reorder_list);
  1619. __skb_queue_tail(&reorder_list, pkt);
  1620. /* set the new expected idx */
  1621. rfi->exp_idx = exp_idx;
  1622. }
  1623. netif_rx:
  1624. skb_queue_walk_safe(&reorder_list, pkt, pnext) {
  1625. __skb_unlink(pkt, &reorder_list);
  1626. brcmf_netif_rx(ifp, pkt);
  1627. }
  1628. }
  1629. void brcmf_fws_hdrpull(struct brcmf_if *ifp, s16 siglen, struct sk_buff *skb)
  1630. {
  1631. struct brcmf_skb_reorder_data *rd;
  1632. struct brcmf_fws_info *fws = drvr_to_fws(ifp->drvr);
  1633. u8 *signal_data;
  1634. s16 data_len;
  1635. u8 type;
  1636. u8 len;
  1637. u8 *data;
  1638. s32 status;
  1639. s32 err;
  1640. brcmf_dbg(HDRS, "enter: ifidx %d, skblen %u, sig %d\n",
  1641. ifp->ifidx, skb->len, siglen);
  1642. WARN_ON(siglen > skb->len);
  1643. if (!siglen)
  1644. return;
  1645. /* if flow control disabled, skip to packet data and leave */
  1646. if ((!fws) || (!fws->fw_signals)) {
  1647. skb_pull(skb, siglen);
  1648. return;
  1649. }
  1650. fws->stats.header_pulls++;
  1651. data_len = siglen;
  1652. signal_data = skb->data;
  1653. status = BRCMF_FWS_RET_OK_NOSCHEDULE;
  1654. while (data_len > 0) {
  1655. /* extract tlv info */
  1656. type = signal_data[0];
  1657. /* FILLER type is actually not a TLV, but
  1658. * a single byte that can be skipped.
  1659. */
  1660. if (type == BRCMF_FWS_TYPE_FILLER) {
  1661. signal_data += 1;
  1662. data_len -= 1;
  1663. continue;
  1664. }
  1665. len = signal_data[1];
  1666. data = signal_data + 2;
  1667. brcmf_dbg(HDRS, "tlv type=%s (%d), len=%d (%d)\n",
  1668. brcmf_fws_get_tlv_name(type), type, len,
  1669. brcmf_fws_get_tlv_len(fws, type));
  1670. /* abort parsing when length invalid */
  1671. if (data_len < len + 2)
  1672. break;
  1673. if (len < brcmf_fws_get_tlv_len(fws, type))
  1674. break;
  1675. err = BRCMF_FWS_RET_OK_NOSCHEDULE;
  1676. switch (type) {
  1677. case BRCMF_FWS_TYPE_COMP_TXSTATUS:
  1678. break;
  1679. case BRCMF_FWS_TYPE_HOST_REORDER_RXPKTS:
  1680. rd = (struct brcmf_skb_reorder_data *)skb->cb;
  1681. rd->reorder = data;
  1682. break;
  1683. case BRCMF_FWS_TYPE_MACDESC_ADD:
  1684. case BRCMF_FWS_TYPE_MACDESC_DEL:
  1685. brcmf_fws_macdesc_indicate(fws, type, data);
  1686. break;
  1687. case BRCMF_FWS_TYPE_MAC_OPEN:
  1688. case BRCMF_FWS_TYPE_MAC_CLOSE:
  1689. err = brcmf_fws_macdesc_state_indicate(fws, type, data);
  1690. break;
  1691. case BRCMF_FWS_TYPE_INTERFACE_OPEN:
  1692. case BRCMF_FWS_TYPE_INTERFACE_CLOSE:
  1693. err = brcmf_fws_interface_state_indicate(fws, type,
  1694. data);
  1695. break;
  1696. case BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT:
  1697. case BRCMF_FWS_TYPE_MAC_REQUEST_PACKET:
  1698. err = brcmf_fws_request_indicate(fws, type, data);
  1699. break;
  1700. case BRCMF_FWS_TYPE_TXSTATUS:
  1701. brcmf_fws_txstatus_indicate(fws, data);
  1702. break;
  1703. case BRCMF_FWS_TYPE_FIFO_CREDITBACK:
  1704. err = brcmf_fws_fifocreditback_indicate(fws, data);
  1705. break;
  1706. case BRCMF_FWS_TYPE_RSSI:
  1707. brcmf_fws_rssi_indicate(fws, *data);
  1708. break;
  1709. case BRCMF_FWS_TYPE_TRANS_ID:
  1710. brcmf_fws_dbg_seqnum_check(fws, data);
  1711. break;
  1712. case BRCMF_FWS_TYPE_PKTTAG:
  1713. case BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP:
  1714. default:
  1715. fws->stats.tlv_invalid_type++;
  1716. break;
  1717. }
  1718. if (err == BRCMF_FWS_RET_OK_SCHEDULE)
  1719. status = BRCMF_FWS_RET_OK_SCHEDULE;
  1720. signal_data += len + 2;
  1721. data_len -= len + 2;
  1722. }
  1723. if (data_len != 0)
  1724. fws->stats.tlv_parse_failed++;
  1725. if (status == BRCMF_FWS_RET_OK_SCHEDULE)
  1726. brcmf_fws_schedule_deq(fws);
  1727. /* signalling processing result does
  1728. * not affect the actual ethernet packet.
  1729. */
  1730. skb_pull(skb, siglen);
  1731. /* this may be a signal-only packet
  1732. */
  1733. if (skb->len == 0)
  1734. fws->stats.header_only_pkt++;
  1735. }
  1736. static u8 brcmf_fws_precommit_skb(struct brcmf_fws_info *fws, int fifo,
  1737. struct sk_buff *p)
  1738. {
  1739. struct brcmf_skbuff_cb *skcb = brcmf_skbcb(p);
  1740. struct brcmf_fws_mac_descriptor *entry = skcb->mac;
  1741. u8 flags;
  1742. if (skcb->state != BRCMF_FWS_SKBSTATE_SUPPRESSED)
  1743. brcmf_skb_htod_tag_set_field(p, GENERATION, entry->generation);
  1744. flags = BRCMF_FWS_HTOD_FLAG_PKTFROMHOST;
  1745. if (brcmf_skb_if_flags_get_field(p, REQUESTED)) {
  1746. /*
  1747. * Indicate that this packet is being sent in response to an
  1748. * explicit request from the firmware side.
  1749. */
  1750. flags |= BRCMF_FWS_HTOD_FLAG_PKT_REQUESTED;
  1751. }
  1752. brcmf_skb_htod_tag_set_field(p, FLAGS, flags);
  1753. return brcmf_fws_hdrpush(fws, p);
  1754. }
  1755. static void brcmf_fws_rollback_toq(struct brcmf_fws_info *fws,
  1756. struct sk_buff *skb, int fifo)
  1757. {
  1758. struct brcmf_fws_mac_descriptor *entry;
  1759. struct sk_buff *pktout;
  1760. int qidx, hslot;
  1761. int rc = 0;
  1762. entry = brcmf_skbcb(skb)->mac;
  1763. if (entry->occupied) {
  1764. qidx = 2 * fifo;
  1765. if (brcmf_skbcb(skb)->state == BRCMF_FWS_SKBSTATE_SUPPRESSED)
  1766. qidx++;
  1767. pktout = brcmu_pktq_penq_head(&entry->psq, qidx, skb);
  1768. if (pktout == NULL) {
  1769. brcmf_err("%s queue %d full\n", entry->name, qidx);
  1770. rc = -ENOSPC;
  1771. }
  1772. } else {
  1773. brcmf_err("%s entry removed\n", entry->name);
  1774. rc = -ENOENT;
  1775. }
  1776. if (rc) {
  1777. fws->stats.rollback_failed++;
  1778. hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
  1779. brcmf_fws_txs_process(fws, BRCMF_FWS_TXSTATUS_HOST_TOSSED,
  1780. hslot, 0, 0);
  1781. } else {
  1782. fws->stats.rollback_success++;
  1783. brcmf_fws_return_credits(fws, fifo, 1);
  1784. brcmf_fws_macdesc_return_req_credit(skb);
  1785. }
  1786. }
  1787. static int brcmf_fws_borrow_credit(struct brcmf_fws_info *fws)
  1788. {
  1789. int lender_ac;
  1790. if (time_after(fws->borrow_defer_timestamp, jiffies)) {
  1791. fws->fifo_credit_map &= ~(1 << BRCMF_FWS_FIFO_AC_BE);
  1792. return -ENAVAIL;
  1793. }
  1794. for (lender_ac = 0; lender_ac <= BRCMF_FWS_FIFO_AC_VO; lender_ac++) {
  1795. if (fws->fifo_credit[lender_ac]) {
  1796. fws->credits_borrowed[lender_ac]++;
  1797. fws->fifo_credit[lender_ac]--;
  1798. if (fws->fifo_credit[lender_ac] == 0)
  1799. fws->fifo_credit_map &= ~(1 << lender_ac);
  1800. fws->fifo_credit_map |= (1 << BRCMF_FWS_FIFO_AC_BE);
  1801. brcmf_dbg(DATA, "borrow credit from: %d\n", lender_ac);
  1802. return 0;
  1803. }
  1804. }
  1805. fws->fifo_credit_map &= ~(1 << BRCMF_FWS_FIFO_AC_BE);
  1806. return -ENAVAIL;
  1807. }
  1808. static int brcmf_fws_commit_skb(struct brcmf_fws_info *fws, int fifo,
  1809. struct sk_buff *skb)
  1810. {
  1811. struct brcmf_skbuff_cb *skcb = brcmf_skbcb(skb);
  1812. struct brcmf_fws_mac_descriptor *entry;
  1813. int rc;
  1814. u8 ifidx;
  1815. u8 data_offset;
  1816. entry = skcb->mac;
  1817. if (IS_ERR(entry))
  1818. return PTR_ERR(entry);
  1819. data_offset = brcmf_fws_precommit_skb(fws, fifo, skb);
  1820. entry->transit_count++;
  1821. if (entry->suppressed)
  1822. entry->suppr_transit_count++;
  1823. ifidx = brcmf_skb_if_flags_get_field(skb, INDEX);
  1824. brcmf_fws_unlock(fws);
  1825. rc = brcmf_proto_txdata(fws->drvr, ifidx, data_offset, skb);
  1826. brcmf_fws_lock(fws);
  1827. brcmf_dbg(DATA, "%s flags %X htod %X bus_tx %d\n", entry->name,
  1828. skcb->if_flags, skcb->htod, rc);
  1829. if (rc < 0) {
  1830. entry->transit_count--;
  1831. if (entry->suppressed)
  1832. entry->suppr_transit_count--;
  1833. (void)brcmf_proto_hdrpull(fws->drvr, false, skb, NULL);
  1834. goto rollback;
  1835. }
  1836. fws->stats.pkt2bus++;
  1837. fws->stats.send_pkts[fifo]++;
  1838. if (brcmf_skb_if_flags_get_field(skb, REQUESTED))
  1839. fws->stats.requested_sent[fifo]++;
  1840. return rc;
  1841. rollback:
  1842. brcmf_fws_rollback_toq(fws, skb, fifo);
  1843. return rc;
  1844. }
  1845. static int brcmf_fws_assign_htod(struct brcmf_fws_info *fws, struct sk_buff *p,
  1846. int fifo)
  1847. {
  1848. struct brcmf_skbuff_cb *skcb = brcmf_skbcb(p);
  1849. int rc, hslot;
  1850. skcb->htod = 0;
  1851. skcb->htod_seq = 0;
  1852. hslot = brcmf_fws_hanger_get_free_slot(&fws->hanger);
  1853. brcmf_skb_htod_tag_set_field(p, HSLOT, hslot);
  1854. brcmf_skb_htod_tag_set_field(p, FREERUN, skcb->mac->seq[fifo]);
  1855. brcmf_skb_htod_tag_set_field(p, FIFO, fifo);
  1856. rc = brcmf_fws_hanger_pushpkt(&fws->hanger, p, hslot);
  1857. if (!rc)
  1858. skcb->mac->seq[fifo]++;
  1859. else
  1860. fws->stats.generic_error++;
  1861. return rc;
  1862. }
  1863. int brcmf_fws_process_skb(struct brcmf_if *ifp, struct sk_buff *skb)
  1864. {
  1865. struct brcmf_fws_info *fws = drvr_to_fws(ifp->drvr);
  1866. struct brcmf_skbuff_cb *skcb = brcmf_skbcb(skb);
  1867. struct ethhdr *eh = (struct ethhdr *)(skb->data);
  1868. int fifo = BRCMF_FWS_FIFO_BCMC;
  1869. bool multicast = is_multicast_ether_addr(eh->h_dest);
  1870. int rc = 0;
  1871. brcmf_dbg(DATA, "tx proto=0x%X\n", ntohs(eh->h_proto));
  1872. /* set control buffer information */
  1873. skcb->if_flags = 0;
  1874. skcb->state = BRCMF_FWS_SKBSTATE_NEW;
  1875. brcmf_skb_if_flags_set_field(skb, INDEX, ifp->ifidx);
  1876. if (!multicast)
  1877. fifo = brcmf_fws_prio2fifo[skb->priority];
  1878. brcmf_fws_lock(fws);
  1879. if (fifo != BRCMF_FWS_FIFO_AC_BE && fifo < BRCMF_FWS_FIFO_BCMC)
  1880. fws->borrow_defer_timestamp = jiffies +
  1881. BRCMF_FWS_BORROW_DEFER_PERIOD;
  1882. skcb->mac = brcmf_fws_macdesc_find(fws, ifp, eh->h_dest);
  1883. brcmf_dbg(DATA, "%s mac %pM multi %d fifo %d\n", skcb->mac->name,
  1884. eh->h_dest, multicast, fifo);
  1885. if (!brcmf_fws_assign_htod(fws, skb, fifo)) {
  1886. brcmf_fws_enq(fws, BRCMF_FWS_SKBSTATE_DELAYED, fifo, skb);
  1887. brcmf_fws_schedule_deq(fws);
  1888. } else {
  1889. brcmf_err("drop skb: no hanger slot\n");
  1890. brcmf_txfinalize(ifp, skb, false);
  1891. rc = -ENOMEM;
  1892. }
  1893. brcmf_fws_unlock(fws);
  1894. return rc;
  1895. }
  1896. void brcmf_fws_reset_interface(struct brcmf_if *ifp)
  1897. {
  1898. struct brcmf_fws_mac_descriptor *entry = ifp->fws_desc;
  1899. brcmf_dbg(TRACE, "enter: bsscfgidx=%d\n", ifp->bsscfgidx);
  1900. if (!entry)
  1901. return;
  1902. brcmf_fws_macdesc_init(entry, ifp->mac_addr, ifp->ifidx);
  1903. }
  1904. void brcmf_fws_add_interface(struct brcmf_if *ifp)
  1905. {
  1906. struct brcmf_fws_info *fws = drvr_to_fws(ifp->drvr);
  1907. struct brcmf_fws_mac_descriptor *entry;
  1908. if (!ifp->ndev || !brcmf_fws_queue_skbs(fws))
  1909. return;
  1910. entry = &fws->desc.iface[ifp->ifidx];
  1911. ifp->fws_desc = entry;
  1912. brcmf_fws_macdesc_init(entry, ifp->mac_addr, ifp->ifidx);
  1913. brcmf_fws_macdesc_set_name(fws, entry);
  1914. brcmu_pktq_init(&entry->psq, BRCMF_FWS_PSQ_PREC_COUNT,
  1915. BRCMF_FWS_PSQ_LEN);
  1916. brcmf_dbg(TRACE, "added %s\n", entry->name);
  1917. }
  1918. void brcmf_fws_del_interface(struct brcmf_if *ifp)
  1919. {
  1920. struct brcmf_fws_mac_descriptor *entry = ifp->fws_desc;
  1921. struct brcmf_fws_info *fws = drvr_to_fws(ifp->drvr);
  1922. if (!entry)
  1923. return;
  1924. brcmf_fws_lock(fws);
  1925. ifp->fws_desc = NULL;
  1926. brcmf_dbg(TRACE, "deleting %s\n", entry->name);
  1927. brcmf_fws_macdesc_cleanup(fws, &fws->desc.iface[ifp->ifidx],
  1928. ifp->ifidx);
  1929. brcmf_fws_macdesc_deinit(entry);
  1930. brcmf_fws_cleanup(fws, ifp->ifidx);
  1931. brcmf_fws_unlock(fws);
  1932. }
  1933. static void brcmf_fws_dequeue_worker(struct work_struct *worker)
  1934. {
  1935. struct brcmf_fws_info *fws;
  1936. struct brcmf_pub *drvr;
  1937. struct sk_buff *skb;
  1938. int fifo;
  1939. u32 hslot;
  1940. u32 ifidx;
  1941. int ret;
  1942. fws = container_of(worker, struct brcmf_fws_info, fws_dequeue_work);
  1943. drvr = fws->drvr;
  1944. brcmf_fws_lock(fws);
  1945. for (fifo = BRCMF_FWS_FIFO_BCMC; fifo >= 0 && !fws->bus_flow_blocked;
  1946. fifo--) {
  1947. if (!brcmf_fws_fc_active(fws)) {
  1948. while ((skb = brcmf_fws_deq(fws, fifo)) != NULL) {
  1949. hslot = brcmf_skb_htod_tag_get_field(skb,
  1950. HSLOT);
  1951. brcmf_fws_hanger_poppkt(&fws->hanger, hslot,
  1952. &skb, true);
  1953. ifidx = brcmf_skb_if_flags_get_field(skb,
  1954. INDEX);
  1955. /* Use proto layer to send data frame */
  1956. brcmf_fws_unlock(fws);
  1957. ret = brcmf_proto_txdata(drvr, ifidx, 0, skb);
  1958. brcmf_fws_lock(fws);
  1959. if (ret < 0)
  1960. brcmf_txfinalize(brcmf_get_ifp(drvr,
  1961. ifidx),
  1962. skb, false);
  1963. if (fws->bus_flow_blocked)
  1964. break;
  1965. }
  1966. continue;
  1967. }
  1968. while ((fws->fifo_credit[fifo]) || ((!fws->bcmc_credit_check) &&
  1969. (fifo == BRCMF_FWS_FIFO_BCMC))) {
  1970. skb = brcmf_fws_deq(fws, fifo);
  1971. if (!skb)
  1972. break;
  1973. fws->fifo_credit[fifo]--;
  1974. if (brcmf_fws_commit_skb(fws, fifo, skb))
  1975. break;
  1976. if (fws->bus_flow_blocked)
  1977. break;
  1978. }
  1979. if ((fifo == BRCMF_FWS_FIFO_AC_BE) &&
  1980. (fws->fifo_credit[fifo] == 0) &&
  1981. (!fws->bus_flow_blocked)) {
  1982. while (brcmf_fws_borrow_credit(fws) == 0) {
  1983. skb = brcmf_fws_deq(fws, fifo);
  1984. if (!skb) {
  1985. brcmf_fws_return_credits(fws, fifo, 1);
  1986. break;
  1987. }
  1988. if (brcmf_fws_commit_skb(fws, fifo, skb))
  1989. break;
  1990. if (fws->bus_flow_blocked)
  1991. break;
  1992. }
  1993. }
  1994. }
  1995. brcmf_fws_unlock(fws);
  1996. }
  1997. #ifdef DEBUG
  1998. static int brcmf_debugfs_fws_stats_read(struct seq_file *seq, void *data)
  1999. {
  2000. struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
  2001. struct brcmf_fws_stats *fwstats = &(drvr_to_fws(bus_if->drvr)->stats);
  2002. seq_printf(seq,
  2003. "header_pulls: %u\n"
  2004. "header_only_pkt: %u\n"
  2005. "tlv_parse_failed: %u\n"
  2006. "tlv_invalid_type: %u\n"
  2007. "mac_update_fails: %u\n"
  2008. "ps_update_fails: %u\n"
  2009. "if_update_fails: %u\n"
  2010. "pkt2bus: %u\n"
  2011. "generic_error: %u\n"
  2012. "rollback_success: %u\n"
  2013. "rollback_failed: %u\n"
  2014. "delayq_full: %u\n"
  2015. "supprq_full: %u\n"
  2016. "txs_indicate: %u\n"
  2017. "txs_discard: %u\n"
  2018. "txs_suppr_core: %u\n"
  2019. "txs_suppr_ps: %u\n"
  2020. "txs_tossed: %u\n"
  2021. "txs_host_tossed: %u\n"
  2022. "bus_flow_block: %u\n"
  2023. "fws_flow_block: %u\n"
  2024. "send_pkts: BK:%u BE:%u VO:%u VI:%u BCMC:%u\n"
  2025. "requested_sent: BK:%u BE:%u VO:%u VI:%u BCMC:%u\n",
  2026. fwstats->header_pulls,
  2027. fwstats->header_only_pkt,
  2028. fwstats->tlv_parse_failed,
  2029. fwstats->tlv_invalid_type,
  2030. fwstats->mac_update_failed,
  2031. fwstats->mac_ps_update_failed,
  2032. fwstats->if_update_failed,
  2033. fwstats->pkt2bus,
  2034. fwstats->generic_error,
  2035. fwstats->rollback_success,
  2036. fwstats->rollback_failed,
  2037. fwstats->delayq_full_error,
  2038. fwstats->supprq_full_error,
  2039. fwstats->txs_indicate,
  2040. fwstats->txs_discard,
  2041. fwstats->txs_supp_core,
  2042. fwstats->txs_supp_ps,
  2043. fwstats->txs_tossed,
  2044. fwstats->txs_host_tossed,
  2045. fwstats->bus_flow_block,
  2046. fwstats->fws_flow_block,
  2047. fwstats->send_pkts[0], fwstats->send_pkts[1],
  2048. fwstats->send_pkts[2], fwstats->send_pkts[3],
  2049. fwstats->send_pkts[4],
  2050. fwstats->requested_sent[0],
  2051. fwstats->requested_sent[1],
  2052. fwstats->requested_sent[2],
  2053. fwstats->requested_sent[3],
  2054. fwstats->requested_sent[4]);
  2055. return 0;
  2056. }
  2057. #else
  2058. static int brcmf_debugfs_fws_stats_read(struct seq_file *seq, void *data)
  2059. {
  2060. return 0;
  2061. }
  2062. #endif
  2063. struct brcmf_fws_info *brcmf_fws_attach(struct brcmf_pub *drvr)
  2064. {
  2065. struct brcmf_fws_info *fws;
  2066. struct brcmf_if *ifp;
  2067. u32 tlv = BRCMF_FWS_FLAGS_RSSI_SIGNALS;
  2068. int rc;
  2069. u32 mode;
  2070. fws = kzalloc(sizeof(*fws), GFP_KERNEL);
  2071. if (!fws) {
  2072. rc = -ENOMEM;
  2073. goto fail;
  2074. }
  2075. spin_lock_init(&fws->spinlock);
  2076. /* store drvr reference */
  2077. fws->drvr = drvr;
  2078. fws->fcmode = drvr->settings->fcmode;
  2079. if ((drvr->bus_if->always_use_fws_queue == false) &&
  2080. (fws->fcmode == BRCMF_FWS_FCMODE_NONE)) {
  2081. fws->avoid_queueing = true;
  2082. brcmf_dbg(INFO, "FWS queueing will be avoided\n");
  2083. return fws;
  2084. }
  2085. fws->fws_wq = create_singlethread_workqueue("brcmf_fws_wq");
  2086. if (fws->fws_wq == NULL) {
  2087. brcmf_err("workqueue creation failed\n");
  2088. rc = -EBADF;
  2089. goto fail;
  2090. }
  2091. INIT_WORK(&fws->fws_dequeue_work, brcmf_fws_dequeue_worker);
  2092. /* enable firmware signalling if fcmode active */
  2093. if (fws->fcmode != BRCMF_FWS_FCMODE_NONE)
  2094. tlv |= BRCMF_FWS_FLAGS_XONXOFF_SIGNALS |
  2095. BRCMF_FWS_FLAGS_CREDIT_STATUS_SIGNALS |
  2096. BRCMF_FWS_FLAGS_HOST_PROPTXSTATUS_ACTIVE |
  2097. BRCMF_FWS_FLAGS_HOST_RXREORDER_ACTIVE;
  2098. rc = brcmf_fweh_register(drvr, BRCMF_E_FIFO_CREDIT_MAP,
  2099. brcmf_fws_notify_credit_map);
  2100. if (rc < 0) {
  2101. brcmf_err("register credit map handler failed\n");
  2102. goto fail;
  2103. }
  2104. rc = brcmf_fweh_register(drvr, BRCMF_E_BCMC_CREDIT_SUPPORT,
  2105. brcmf_fws_notify_bcmc_credit_support);
  2106. if (rc < 0) {
  2107. brcmf_err("register bcmc credit handler failed\n");
  2108. brcmf_fweh_unregister(drvr, BRCMF_E_FIFO_CREDIT_MAP);
  2109. goto fail;
  2110. }
  2111. /* Setting the iovar may fail if feature is unsupported
  2112. * so leave the rc as is so driver initialization can
  2113. * continue. Set mode back to none indicating not enabled.
  2114. */
  2115. fws->fw_signals = true;
  2116. ifp = brcmf_get_ifp(drvr, 0);
  2117. if (brcmf_fil_iovar_int_set(ifp, "tlv", tlv)) {
  2118. brcmf_err("failed to set bdcv2 tlv signaling\n");
  2119. fws->fcmode = BRCMF_FWS_FCMODE_NONE;
  2120. fws->fw_signals = false;
  2121. }
  2122. if (brcmf_fil_iovar_int_set(ifp, "ampdu_hostreorder", 1))
  2123. brcmf_dbg(INFO, "enabling AMPDU host-reorder failed\n");
  2124. /* Enable seq number reuse, if supported */
  2125. if (brcmf_fil_iovar_int_get(ifp, "wlfc_mode", &mode) == 0) {
  2126. if (BRCMF_FWS_MODE_GET_REUSESEQ(mode)) {
  2127. mode = 0;
  2128. BRCMF_FWS_MODE_SET_REUSESEQ(mode, 1);
  2129. if (brcmf_fil_iovar_int_set(ifp,
  2130. "wlfc_mode", mode) == 0) {
  2131. BRCMF_FWS_MODE_SET_REUSESEQ(fws->mode, 1);
  2132. }
  2133. }
  2134. }
  2135. brcmf_fws_hanger_init(&fws->hanger);
  2136. brcmf_fws_macdesc_init(&fws->desc.other, NULL, 0);
  2137. brcmf_fws_macdesc_set_name(fws, &fws->desc.other);
  2138. brcmf_dbg(INFO, "added %s\n", fws->desc.other.name);
  2139. brcmu_pktq_init(&fws->desc.other.psq, BRCMF_FWS_PSQ_PREC_COUNT,
  2140. BRCMF_FWS_PSQ_LEN);
  2141. brcmf_dbg(INFO, "%s bdcv2 tlv signaling [%x]\n",
  2142. fws->fw_signals ? "enabled" : "disabled", tlv);
  2143. return fws;
  2144. fail:
  2145. brcmf_fws_detach_pre_delif(fws);
  2146. brcmf_fws_detach_post_delif(fws);
  2147. return ERR_PTR(rc);
  2148. }
  2149. void brcmf_fws_detach_pre_delif(struct brcmf_fws_info *fws)
  2150. {
  2151. if (!fws)
  2152. return;
  2153. if (fws->fws_wq) {
  2154. destroy_workqueue(fws->fws_wq);
  2155. fws->fws_wq = NULL;
  2156. }
  2157. }
  2158. void brcmf_fws_detach_post_delif(struct brcmf_fws_info *fws)
  2159. {
  2160. if (!fws)
  2161. return;
  2162. /* cleanup */
  2163. brcmf_fws_lock(fws);
  2164. brcmf_fws_cleanup(fws, -1);
  2165. brcmf_fws_unlock(fws);
  2166. /* free top structure */
  2167. kfree(fws);
  2168. }
  2169. void brcmf_fws_debugfs_create(struct brcmf_pub *drvr)
  2170. {
  2171. /* create debugfs file for statistics */
  2172. brcmf_debugfs_add_entry(drvr, "fws_stats",
  2173. brcmf_debugfs_fws_stats_read);
  2174. }
  2175. bool brcmf_fws_queue_skbs(struct brcmf_fws_info *fws)
  2176. {
  2177. return !fws->avoid_queueing;
  2178. }
  2179. bool brcmf_fws_fc_active(struct brcmf_fws_info *fws)
  2180. {
  2181. if (!fws->creditmap_received)
  2182. return false;
  2183. return fws->fcmode != BRCMF_FWS_FCMODE_NONE;
  2184. }
  2185. void brcmf_fws_bustxfail(struct brcmf_fws_info *fws, struct sk_buff *skb)
  2186. {
  2187. u32 hslot;
  2188. if (brcmf_skbcb(skb)->state == BRCMF_FWS_SKBSTATE_TIM) {
  2189. brcmu_pkt_buf_free_skb(skb);
  2190. return;
  2191. }
  2192. brcmf_fws_lock(fws);
  2193. hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
  2194. brcmf_fws_txs_process(fws, BRCMF_FWS_TXSTATUS_HOST_TOSSED, hslot, 0, 0);
  2195. brcmf_fws_unlock(fws);
  2196. }
  2197. void brcmf_fws_bus_blocked(struct brcmf_pub *drvr, bool flow_blocked)
  2198. {
  2199. struct brcmf_fws_info *fws = drvr_to_fws(drvr);
  2200. struct brcmf_if *ifp;
  2201. int i;
  2202. if (fws->avoid_queueing) {
  2203. for (i = 0; i < BRCMF_MAX_IFS; i++) {
  2204. ifp = drvr->iflist[i];
  2205. if (!ifp || !ifp->ndev)
  2206. continue;
  2207. brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_FLOW,
  2208. flow_blocked);
  2209. }
  2210. } else {
  2211. fws->bus_flow_blocked = flow_blocked;
  2212. if (!flow_blocked)
  2213. brcmf_fws_schedule_deq(fws);
  2214. else
  2215. fws->stats.bus_flow_block++;
  2216. }
  2217. }