farch.c 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987
  1. /****************************************************************************
  2. * Driver for Solarflare network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2006-2013 Solarflare Communications Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation, incorporated herein by reference.
  9. */
  10. #include <linux/bitops.h>
  11. #include <linux/delay.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/pci.h>
  14. #include <linux/module.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/crc32.h>
  17. #include "net_driver.h"
  18. #include "bitfield.h"
  19. #include "efx.h"
  20. #include "nic.h"
  21. #include "farch_regs.h"
  22. #include "sriov.h"
  23. #include "siena_sriov.h"
  24. #include "io.h"
  25. #include "workarounds.h"
  26. /* Falcon-architecture (SFC4000 and SFC9000-family) support */
  27. /**************************************************************************
  28. *
  29. * Configurable values
  30. *
  31. **************************************************************************
  32. */
  33. /* This is set to 16 for a good reason. In summary, if larger than
  34. * 16, the descriptor cache holds more than a default socket
  35. * buffer's worth of packets (for UDP we can only have at most one
  36. * socket buffer's worth outstanding). This combined with the fact
  37. * that we only get 1 TX event per descriptor cache means the NIC
  38. * goes idle.
  39. */
  40. #define TX_DC_ENTRIES 16
  41. #define TX_DC_ENTRIES_ORDER 1
  42. #define RX_DC_ENTRIES 64
  43. #define RX_DC_ENTRIES_ORDER 3
  44. /* If EFX_MAX_INT_ERRORS internal errors occur within
  45. * EFX_INT_ERROR_EXPIRE seconds, we consider the NIC broken and
  46. * disable it.
  47. */
  48. #define EFX_INT_ERROR_EXPIRE 3600
  49. #define EFX_MAX_INT_ERRORS 5
  50. /* Depth of RX flush request fifo */
  51. #define EFX_RX_FLUSH_COUNT 4
  52. /* Driver generated events */
  53. #define _EFX_CHANNEL_MAGIC_TEST 0x000101
  54. #define _EFX_CHANNEL_MAGIC_FILL 0x000102
  55. #define _EFX_CHANNEL_MAGIC_RX_DRAIN 0x000103
  56. #define _EFX_CHANNEL_MAGIC_TX_DRAIN 0x000104
  57. #define _EFX_CHANNEL_MAGIC(_code, _data) ((_code) << 8 | (_data))
  58. #define _EFX_CHANNEL_MAGIC_CODE(_magic) ((_magic) >> 8)
  59. #define EFX_CHANNEL_MAGIC_TEST(_channel) \
  60. _EFX_CHANNEL_MAGIC(_EFX_CHANNEL_MAGIC_TEST, (_channel)->channel)
  61. #define EFX_CHANNEL_MAGIC_FILL(_rx_queue) \
  62. _EFX_CHANNEL_MAGIC(_EFX_CHANNEL_MAGIC_FILL, \
  63. efx_rx_queue_index(_rx_queue))
  64. #define EFX_CHANNEL_MAGIC_RX_DRAIN(_rx_queue) \
  65. _EFX_CHANNEL_MAGIC(_EFX_CHANNEL_MAGIC_RX_DRAIN, \
  66. efx_rx_queue_index(_rx_queue))
  67. #define EFX_CHANNEL_MAGIC_TX_DRAIN(_tx_queue) \
  68. _EFX_CHANNEL_MAGIC(_EFX_CHANNEL_MAGIC_TX_DRAIN, \
  69. (_tx_queue)->queue)
  70. static void efx_farch_magic_event(struct efx_channel *channel, u32 magic);
  71. /**************************************************************************
  72. *
  73. * Hardware access
  74. *
  75. **************************************************************************/
  76. static inline void efx_write_buf_tbl(struct efx_nic *efx, efx_qword_t *value,
  77. unsigned int index)
  78. {
  79. efx_sram_writeq(efx, efx->membase + efx->type->buf_tbl_base,
  80. value, index);
  81. }
  82. static bool efx_masked_compare_oword(const efx_oword_t *a, const efx_oword_t *b,
  83. const efx_oword_t *mask)
  84. {
  85. return ((a->u64[0] ^ b->u64[0]) & mask->u64[0]) ||
  86. ((a->u64[1] ^ b->u64[1]) & mask->u64[1]);
  87. }
  88. int efx_farch_test_registers(struct efx_nic *efx,
  89. const struct efx_farch_register_test *regs,
  90. size_t n_regs)
  91. {
  92. unsigned address = 0, i, j;
  93. efx_oword_t mask, imask, original, reg, buf;
  94. for (i = 0; i < n_regs; ++i) {
  95. address = regs[i].address;
  96. mask = imask = regs[i].mask;
  97. EFX_INVERT_OWORD(imask);
  98. efx_reado(efx, &original, address);
  99. /* bit sweep on and off */
  100. for (j = 0; j < 128; j++) {
  101. if (!EFX_EXTRACT_OWORD32(mask, j, j))
  102. continue;
  103. /* Test this testable bit can be set in isolation */
  104. EFX_AND_OWORD(reg, original, mask);
  105. EFX_SET_OWORD32(reg, j, j, 1);
  106. efx_writeo(efx, &reg, address);
  107. efx_reado(efx, &buf, address);
  108. if (efx_masked_compare_oword(&reg, &buf, &mask))
  109. goto fail;
  110. /* Test this testable bit can be cleared in isolation */
  111. EFX_OR_OWORD(reg, original, mask);
  112. EFX_SET_OWORD32(reg, j, j, 0);
  113. efx_writeo(efx, &reg, address);
  114. efx_reado(efx, &buf, address);
  115. if (efx_masked_compare_oword(&reg, &buf, &mask))
  116. goto fail;
  117. }
  118. efx_writeo(efx, &original, address);
  119. }
  120. return 0;
  121. fail:
  122. netif_err(efx, hw, efx->net_dev,
  123. "wrote "EFX_OWORD_FMT" read "EFX_OWORD_FMT
  124. " at address 0x%x mask "EFX_OWORD_FMT"\n", EFX_OWORD_VAL(reg),
  125. EFX_OWORD_VAL(buf), address, EFX_OWORD_VAL(mask));
  126. return -EIO;
  127. }
  128. /**************************************************************************
  129. *
  130. * Special buffer handling
  131. * Special buffers are used for event queues and the TX and RX
  132. * descriptor rings.
  133. *
  134. *************************************************************************/
  135. /*
  136. * Initialise a special buffer
  137. *
  138. * This will define a buffer (previously allocated via
  139. * efx_alloc_special_buffer()) in the buffer table, allowing
  140. * it to be used for event queues, descriptor rings etc.
  141. */
  142. static void
  143. efx_init_special_buffer(struct efx_nic *efx, struct efx_special_buffer *buffer)
  144. {
  145. efx_qword_t buf_desc;
  146. unsigned int index;
  147. dma_addr_t dma_addr;
  148. int i;
  149. EFX_BUG_ON_PARANOID(!buffer->buf.addr);
  150. /* Write buffer descriptors to NIC */
  151. for (i = 0; i < buffer->entries; i++) {
  152. index = buffer->index + i;
  153. dma_addr = buffer->buf.dma_addr + (i * EFX_BUF_SIZE);
  154. netif_dbg(efx, probe, efx->net_dev,
  155. "mapping special buffer %d at %llx\n",
  156. index, (unsigned long long)dma_addr);
  157. EFX_POPULATE_QWORD_3(buf_desc,
  158. FRF_AZ_BUF_ADR_REGION, 0,
  159. FRF_AZ_BUF_ADR_FBUF, dma_addr >> 12,
  160. FRF_AZ_BUF_OWNER_ID_FBUF, 0);
  161. efx_write_buf_tbl(efx, &buf_desc, index);
  162. }
  163. }
  164. /* Unmaps a buffer and clears the buffer table entries */
  165. static void
  166. efx_fini_special_buffer(struct efx_nic *efx, struct efx_special_buffer *buffer)
  167. {
  168. efx_oword_t buf_tbl_upd;
  169. unsigned int start = buffer->index;
  170. unsigned int end = (buffer->index + buffer->entries - 1);
  171. if (!buffer->entries)
  172. return;
  173. netif_dbg(efx, hw, efx->net_dev, "unmapping special buffers %d-%d\n",
  174. buffer->index, buffer->index + buffer->entries - 1);
  175. EFX_POPULATE_OWORD_4(buf_tbl_upd,
  176. FRF_AZ_BUF_UPD_CMD, 0,
  177. FRF_AZ_BUF_CLR_CMD, 1,
  178. FRF_AZ_BUF_CLR_END_ID, end,
  179. FRF_AZ_BUF_CLR_START_ID, start);
  180. efx_writeo(efx, &buf_tbl_upd, FR_AZ_BUF_TBL_UPD);
  181. }
  182. /*
  183. * Allocate a new special buffer
  184. *
  185. * This allocates memory for a new buffer, clears it and allocates a
  186. * new buffer ID range. It does not write into the buffer table.
  187. *
  188. * This call will allocate 4KB buffers, since 8KB buffers can't be
  189. * used for event queues and descriptor rings.
  190. */
  191. static int efx_alloc_special_buffer(struct efx_nic *efx,
  192. struct efx_special_buffer *buffer,
  193. unsigned int len)
  194. {
  195. #ifdef CONFIG_SFC_SRIOV
  196. struct siena_nic_data *nic_data = efx->nic_data;
  197. #endif
  198. len = ALIGN(len, EFX_BUF_SIZE);
  199. if (efx_nic_alloc_buffer(efx, &buffer->buf, len, GFP_KERNEL))
  200. return -ENOMEM;
  201. buffer->entries = len / EFX_BUF_SIZE;
  202. BUG_ON(buffer->buf.dma_addr & (EFX_BUF_SIZE - 1));
  203. /* Select new buffer ID */
  204. buffer->index = efx->next_buffer_table;
  205. efx->next_buffer_table += buffer->entries;
  206. #ifdef CONFIG_SFC_SRIOV
  207. BUG_ON(efx_siena_sriov_enabled(efx) &&
  208. nic_data->vf_buftbl_base < efx->next_buffer_table);
  209. #endif
  210. netif_dbg(efx, probe, efx->net_dev,
  211. "allocating special buffers %d-%d at %llx+%x "
  212. "(virt %p phys %llx)\n", buffer->index,
  213. buffer->index + buffer->entries - 1,
  214. (u64)buffer->buf.dma_addr, len,
  215. buffer->buf.addr, (u64)virt_to_phys(buffer->buf.addr));
  216. return 0;
  217. }
  218. static void
  219. efx_free_special_buffer(struct efx_nic *efx, struct efx_special_buffer *buffer)
  220. {
  221. if (!buffer->buf.addr)
  222. return;
  223. netif_dbg(efx, hw, efx->net_dev,
  224. "deallocating special buffers %d-%d at %llx+%x "
  225. "(virt %p phys %llx)\n", buffer->index,
  226. buffer->index + buffer->entries - 1,
  227. (u64)buffer->buf.dma_addr, buffer->buf.len,
  228. buffer->buf.addr, (u64)virt_to_phys(buffer->buf.addr));
  229. efx_nic_free_buffer(efx, &buffer->buf);
  230. buffer->entries = 0;
  231. }
  232. /**************************************************************************
  233. *
  234. * TX path
  235. *
  236. **************************************************************************/
  237. /* This writes to the TX_DESC_WPTR; write pointer for TX descriptor ring */
  238. static inline void efx_farch_notify_tx_desc(struct efx_tx_queue *tx_queue)
  239. {
  240. unsigned write_ptr;
  241. efx_dword_t reg;
  242. write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
  243. EFX_POPULATE_DWORD_1(reg, FRF_AZ_TX_DESC_WPTR_DWORD, write_ptr);
  244. efx_writed_page(tx_queue->efx, &reg,
  245. FR_AZ_TX_DESC_UPD_DWORD_P0, tx_queue->queue);
  246. }
  247. /* Write pointer and first descriptor for TX descriptor ring */
  248. static inline void efx_farch_push_tx_desc(struct efx_tx_queue *tx_queue,
  249. const efx_qword_t *txd)
  250. {
  251. unsigned write_ptr;
  252. efx_oword_t reg;
  253. BUILD_BUG_ON(FRF_AZ_TX_DESC_LBN != 0);
  254. BUILD_BUG_ON(FR_AA_TX_DESC_UPD_KER != FR_BZ_TX_DESC_UPD_P0);
  255. write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
  256. EFX_POPULATE_OWORD_2(reg, FRF_AZ_TX_DESC_PUSH_CMD, true,
  257. FRF_AZ_TX_DESC_WPTR, write_ptr);
  258. reg.qword[0] = *txd;
  259. efx_writeo_page(tx_queue->efx, &reg,
  260. FR_BZ_TX_DESC_UPD_P0, tx_queue->queue);
  261. }
  262. /* For each entry inserted into the software descriptor ring, create a
  263. * descriptor in the hardware TX descriptor ring (in host memory), and
  264. * write a doorbell.
  265. */
  266. void efx_farch_tx_write(struct efx_tx_queue *tx_queue)
  267. {
  268. struct efx_tx_buffer *buffer;
  269. efx_qword_t *txd;
  270. unsigned write_ptr;
  271. unsigned old_write_count = tx_queue->write_count;
  272. BUG_ON(tx_queue->write_count == tx_queue->insert_count);
  273. do {
  274. write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
  275. buffer = &tx_queue->buffer[write_ptr];
  276. txd = efx_tx_desc(tx_queue, write_ptr);
  277. ++tx_queue->write_count;
  278. EFX_BUG_ON_PARANOID(buffer->flags & EFX_TX_BUF_OPTION);
  279. /* Create TX descriptor ring entry */
  280. BUILD_BUG_ON(EFX_TX_BUF_CONT != 1);
  281. EFX_POPULATE_QWORD_4(*txd,
  282. FSF_AZ_TX_KER_CONT,
  283. buffer->flags & EFX_TX_BUF_CONT,
  284. FSF_AZ_TX_KER_BYTE_COUNT, buffer->len,
  285. FSF_AZ_TX_KER_BUF_REGION, 0,
  286. FSF_AZ_TX_KER_BUF_ADDR, buffer->dma_addr);
  287. } while (tx_queue->write_count != tx_queue->insert_count);
  288. wmb(); /* Ensure descriptors are written before they are fetched */
  289. if (efx_nic_may_push_tx_desc(tx_queue, old_write_count)) {
  290. txd = efx_tx_desc(tx_queue,
  291. old_write_count & tx_queue->ptr_mask);
  292. efx_farch_push_tx_desc(tx_queue, txd);
  293. ++tx_queue->pushes;
  294. } else {
  295. efx_farch_notify_tx_desc(tx_queue);
  296. }
  297. }
  298. /* Allocate hardware resources for a TX queue */
  299. int efx_farch_tx_probe(struct efx_tx_queue *tx_queue)
  300. {
  301. struct efx_nic *efx = tx_queue->efx;
  302. unsigned entries;
  303. entries = tx_queue->ptr_mask + 1;
  304. return efx_alloc_special_buffer(efx, &tx_queue->txd,
  305. entries * sizeof(efx_qword_t));
  306. }
  307. void efx_farch_tx_init(struct efx_tx_queue *tx_queue)
  308. {
  309. struct efx_nic *efx = tx_queue->efx;
  310. efx_oword_t reg;
  311. /* Pin TX descriptor ring */
  312. efx_init_special_buffer(efx, &tx_queue->txd);
  313. /* Push TX descriptor ring to card */
  314. EFX_POPULATE_OWORD_10(reg,
  315. FRF_AZ_TX_DESCQ_EN, 1,
  316. FRF_AZ_TX_ISCSI_DDIG_EN, 0,
  317. FRF_AZ_TX_ISCSI_HDIG_EN, 0,
  318. FRF_AZ_TX_DESCQ_BUF_BASE_ID, tx_queue->txd.index,
  319. FRF_AZ_TX_DESCQ_EVQ_ID,
  320. tx_queue->channel->channel,
  321. FRF_AZ_TX_DESCQ_OWNER_ID, 0,
  322. FRF_AZ_TX_DESCQ_LABEL, tx_queue->queue,
  323. FRF_AZ_TX_DESCQ_SIZE,
  324. __ffs(tx_queue->txd.entries),
  325. FRF_AZ_TX_DESCQ_TYPE, 0,
  326. FRF_BZ_TX_NON_IP_DROP_DIS, 1);
  327. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0) {
  328. int csum = tx_queue->queue & EFX_TXQ_TYPE_OFFLOAD;
  329. EFX_SET_OWORD_FIELD(reg, FRF_BZ_TX_IP_CHKSM_DIS, !csum);
  330. EFX_SET_OWORD_FIELD(reg, FRF_BZ_TX_TCP_CHKSM_DIS,
  331. !csum);
  332. }
  333. efx_writeo_table(efx, &reg, efx->type->txd_ptr_tbl_base,
  334. tx_queue->queue);
  335. if (efx_nic_rev(efx) < EFX_REV_FALCON_B0) {
  336. /* Only 128 bits in this register */
  337. BUILD_BUG_ON(EFX_MAX_TX_QUEUES > 128);
  338. efx_reado(efx, &reg, FR_AA_TX_CHKSM_CFG);
  339. if (tx_queue->queue & EFX_TXQ_TYPE_OFFLOAD)
  340. __clear_bit_le(tx_queue->queue, &reg);
  341. else
  342. __set_bit_le(tx_queue->queue, &reg);
  343. efx_writeo(efx, &reg, FR_AA_TX_CHKSM_CFG);
  344. }
  345. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0) {
  346. EFX_POPULATE_OWORD_1(reg,
  347. FRF_BZ_TX_PACE,
  348. (tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI) ?
  349. FFE_BZ_TX_PACE_OFF :
  350. FFE_BZ_TX_PACE_RESERVED);
  351. efx_writeo_table(efx, &reg, FR_BZ_TX_PACE_TBL,
  352. tx_queue->queue);
  353. }
  354. }
  355. static void efx_farch_flush_tx_queue(struct efx_tx_queue *tx_queue)
  356. {
  357. struct efx_nic *efx = tx_queue->efx;
  358. efx_oword_t tx_flush_descq;
  359. WARN_ON(atomic_read(&tx_queue->flush_outstanding));
  360. atomic_set(&tx_queue->flush_outstanding, 1);
  361. EFX_POPULATE_OWORD_2(tx_flush_descq,
  362. FRF_AZ_TX_FLUSH_DESCQ_CMD, 1,
  363. FRF_AZ_TX_FLUSH_DESCQ, tx_queue->queue);
  364. efx_writeo(efx, &tx_flush_descq, FR_AZ_TX_FLUSH_DESCQ);
  365. }
  366. void efx_farch_tx_fini(struct efx_tx_queue *tx_queue)
  367. {
  368. struct efx_nic *efx = tx_queue->efx;
  369. efx_oword_t tx_desc_ptr;
  370. /* Remove TX descriptor ring from card */
  371. EFX_ZERO_OWORD(tx_desc_ptr);
  372. efx_writeo_table(efx, &tx_desc_ptr, efx->type->txd_ptr_tbl_base,
  373. tx_queue->queue);
  374. /* Unpin TX descriptor ring */
  375. efx_fini_special_buffer(efx, &tx_queue->txd);
  376. }
  377. /* Free buffers backing TX queue */
  378. void efx_farch_tx_remove(struct efx_tx_queue *tx_queue)
  379. {
  380. efx_free_special_buffer(tx_queue->efx, &tx_queue->txd);
  381. }
  382. /**************************************************************************
  383. *
  384. * RX path
  385. *
  386. **************************************************************************/
  387. /* This creates an entry in the RX descriptor queue */
  388. static inline void
  389. efx_farch_build_rx_desc(struct efx_rx_queue *rx_queue, unsigned index)
  390. {
  391. struct efx_rx_buffer *rx_buf;
  392. efx_qword_t *rxd;
  393. rxd = efx_rx_desc(rx_queue, index);
  394. rx_buf = efx_rx_buffer(rx_queue, index);
  395. EFX_POPULATE_QWORD_3(*rxd,
  396. FSF_AZ_RX_KER_BUF_SIZE,
  397. rx_buf->len -
  398. rx_queue->efx->type->rx_buffer_padding,
  399. FSF_AZ_RX_KER_BUF_REGION, 0,
  400. FSF_AZ_RX_KER_BUF_ADDR, rx_buf->dma_addr);
  401. }
  402. /* This writes to the RX_DESC_WPTR register for the specified receive
  403. * descriptor ring.
  404. */
  405. void efx_farch_rx_write(struct efx_rx_queue *rx_queue)
  406. {
  407. struct efx_nic *efx = rx_queue->efx;
  408. efx_dword_t reg;
  409. unsigned write_ptr;
  410. while (rx_queue->notified_count != rx_queue->added_count) {
  411. efx_farch_build_rx_desc(
  412. rx_queue,
  413. rx_queue->notified_count & rx_queue->ptr_mask);
  414. ++rx_queue->notified_count;
  415. }
  416. wmb();
  417. write_ptr = rx_queue->added_count & rx_queue->ptr_mask;
  418. EFX_POPULATE_DWORD_1(reg, FRF_AZ_RX_DESC_WPTR_DWORD, write_ptr);
  419. efx_writed_page(efx, &reg, FR_AZ_RX_DESC_UPD_DWORD_P0,
  420. efx_rx_queue_index(rx_queue));
  421. }
  422. int efx_farch_rx_probe(struct efx_rx_queue *rx_queue)
  423. {
  424. struct efx_nic *efx = rx_queue->efx;
  425. unsigned entries;
  426. entries = rx_queue->ptr_mask + 1;
  427. return efx_alloc_special_buffer(efx, &rx_queue->rxd,
  428. entries * sizeof(efx_qword_t));
  429. }
  430. void efx_farch_rx_init(struct efx_rx_queue *rx_queue)
  431. {
  432. efx_oword_t rx_desc_ptr;
  433. struct efx_nic *efx = rx_queue->efx;
  434. bool is_b0 = efx_nic_rev(efx) >= EFX_REV_FALCON_B0;
  435. bool iscsi_digest_en = is_b0;
  436. bool jumbo_en;
  437. /* For kernel-mode queues in Falcon A1, the JUMBO flag enables
  438. * DMA to continue after a PCIe page boundary (and scattering
  439. * is not possible). In Falcon B0 and Siena, it enables
  440. * scatter.
  441. */
  442. jumbo_en = !is_b0 || efx->rx_scatter;
  443. netif_dbg(efx, hw, efx->net_dev,
  444. "RX queue %d ring in special buffers %d-%d\n",
  445. efx_rx_queue_index(rx_queue), rx_queue->rxd.index,
  446. rx_queue->rxd.index + rx_queue->rxd.entries - 1);
  447. rx_queue->scatter_n = 0;
  448. /* Pin RX descriptor ring */
  449. efx_init_special_buffer(efx, &rx_queue->rxd);
  450. /* Push RX descriptor ring to card */
  451. EFX_POPULATE_OWORD_10(rx_desc_ptr,
  452. FRF_AZ_RX_ISCSI_DDIG_EN, iscsi_digest_en,
  453. FRF_AZ_RX_ISCSI_HDIG_EN, iscsi_digest_en,
  454. FRF_AZ_RX_DESCQ_BUF_BASE_ID, rx_queue->rxd.index,
  455. FRF_AZ_RX_DESCQ_EVQ_ID,
  456. efx_rx_queue_channel(rx_queue)->channel,
  457. FRF_AZ_RX_DESCQ_OWNER_ID, 0,
  458. FRF_AZ_RX_DESCQ_LABEL,
  459. efx_rx_queue_index(rx_queue),
  460. FRF_AZ_RX_DESCQ_SIZE,
  461. __ffs(rx_queue->rxd.entries),
  462. FRF_AZ_RX_DESCQ_TYPE, 0 /* kernel queue */ ,
  463. FRF_AZ_RX_DESCQ_JUMBO, jumbo_en,
  464. FRF_AZ_RX_DESCQ_EN, 1);
  465. efx_writeo_table(efx, &rx_desc_ptr, efx->type->rxd_ptr_tbl_base,
  466. efx_rx_queue_index(rx_queue));
  467. }
  468. static void efx_farch_flush_rx_queue(struct efx_rx_queue *rx_queue)
  469. {
  470. struct efx_nic *efx = rx_queue->efx;
  471. efx_oword_t rx_flush_descq;
  472. EFX_POPULATE_OWORD_2(rx_flush_descq,
  473. FRF_AZ_RX_FLUSH_DESCQ_CMD, 1,
  474. FRF_AZ_RX_FLUSH_DESCQ,
  475. efx_rx_queue_index(rx_queue));
  476. efx_writeo(efx, &rx_flush_descq, FR_AZ_RX_FLUSH_DESCQ);
  477. }
  478. void efx_farch_rx_fini(struct efx_rx_queue *rx_queue)
  479. {
  480. efx_oword_t rx_desc_ptr;
  481. struct efx_nic *efx = rx_queue->efx;
  482. /* Remove RX descriptor ring from card */
  483. EFX_ZERO_OWORD(rx_desc_ptr);
  484. efx_writeo_table(efx, &rx_desc_ptr, efx->type->rxd_ptr_tbl_base,
  485. efx_rx_queue_index(rx_queue));
  486. /* Unpin RX descriptor ring */
  487. efx_fini_special_buffer(efx, &rx_queue->rxd);
  488. }
  489. /* Free buffers backing RX queue */
  490. void efx_farch_rx_remove(struct efx_rx_queue *rx_queue)
  491. {
  492. efx_free_special_buffer(rx_queue->efx, &rx_queue->rxd);
  493. }
  494. /**************************************************************************
  495. *
  496. * Flush handling
  497. *
  498. **************************************************************************/
  499. /* efx_farch_flush_queues() must be woken up when all flushes are completed,
  500. * or more RX flushes can be kicked off.
  501. */
  502. static bool efx_farch_flush_wake(struct efx_nic *efx)
  503. {
  504. /* Ensure that all updates are visible to efx_farch_flush_queues() */
  505. smp_mb();
  506. return (atomic_read(&efx->active_queues) == 0 ||
  507. (atomic_read(&efx->rxq_flush_outstanding) < EFX_RX_FLUSH_COUNT
  508. && atomic_read(&efx->rxq_flush_pending) > 0));
  509. }
  510. static bool efx_check_tx_flush_complete(struct efx_nic *efx)
  511. {
  512. bool i = true;
  513. efx_oword_t txd_ptr_tbl;
  514. struct efx_channel *channel;
  515. struct efx_tx_queue *tx_queue;
  516. efx_for_each_channel(channel, efx) {
  517. efx_for_each_channel_tx_queue(tx_queue, channel) {
  518. efx_reado_table(efx, &txd_ptr_tbl,
  519. FR_BZ_TX_DESC_PTR_TBL, tx_queue->queue);
  520. if (EFX_OWORD_FIELD(txd_ptr_tbl,
  521. FRF_AZ_TX_DESCQ_FLUSH) ||
  522. EFX_OWORD_FIELD(txd_ptr_tbl,
  523. FRF_AZ_TX_DESCQ_EN)) {
  524. netif_dbg(efx, hw, efx->net_dev,
  525. "flush did not complete on TXQ %d\n",
  526. tx_queue->queue);
  527. i = false;
  528. } else if (atomic_cmpxchg(&tx_queue->flush_outstanding,
  529. 1, 0)) {
  530. /* The flush is complete, but we didn't
  531. * receive a flush completion event
  532. */
  533. netif_dbg(efx, hw, efx->net_dev,
  534. "flush complete on TXQ %d, so drain "
  535. "the queue\n", tx_queue->queue);
  536. /* Don't need to increment active_queues as it
  537. * has already been incremented for the queues
  538. * which did not drain
  539. */
  540. efx_farch_magic_event(channel,
  541. EFX_CHANNEL_MAGIC_TX_DRAIN(
  542. tx_queue));
  543. }
  544. }
  545. }
  546. return i;
  547. }
  548. /* Flush all the transmit queues, and continue flushing receive queues until
  549. * they're all flushed. Wait for the DRAIN events to be received so that there
  550. * are no more RX and TX events left on any channel. */
  551. static int efx_farch_do_flush(struct efx_nic *efx)
  552. {
  553. unsigned timeout = msecs_to_jiffies(5000); /* 5s for all flushes and drains */
  554. struct efx_channel *channel;
  555. struct efx_rx_queue *rx_queue;
  556. struct efx_tx_queue *tx_queue;
  557. int rc = 0;
  558. efx_for_each_channel(channel, efx) {
  559. efx_for_each_channel_tx_queue(tx_queue, channel) {
  560. efx_farch_flush_tx_queue(tx_queue);
  561. }
  562. efx_for_each_channel_rx_queue(rx_queue, channel) {
  563. rx_queue->flush_pending = true;
  564. atomic_inc(&efx->rxq_flush_pending);
  565. }
  566. }
  567. while (timeout && atomic_read(&efx->active_queues) > 0) {
  568. /* If SRIOV is enabled, then offload receive queue flushing to
  569. * the firmware (though we will still have to poll for
  570. * completion). If that fails, fall back to the old scheme.
  571. */
  572. if (efx_siena_sriov_enabled(efx)) {
  573. rc = efx_mcdi_flush_rxqs(efx);
  574. if (!rc)
  575. goto wait;
  576. }
  577. /* The hardware supports four concurrent rx flushes, each of
  578. * which may need to be retried if there is an outstanding
  579. * descriptor fetch
  580. */
  581. efx_for_each_channel(channel, efx) {
  582. efx_for_each_channel_rx_queue(rx_queue, channel) {
  583. if (atomic_read(&efx->rxq_flush_outstanding) >=
  584. EFX_RX_FLUSH_COUNT)
  585. break;
  586. if (rx_queue->flush_pending) {
  587. rx_queue->flush_pending = false;
  588. atomic_dec(&efx->rxq_flush_pending);
  589. atomic_inc(&efx->rxq_flush_outstanding);
  590. efx_farch_flush_rx_queue(rx_queue);
  591. }
  592. }
  593. }
  594. wait:
  595. timeout = wait_event_timeout(efx->flush_wq,
  596. efx_farch_flush_wake(efx),
  597. timeout);
  598. }
  599. if (atomic_read(&efx->active_queues) &&
  600. !efx_check_tx_flush_complete(efx)) {
  601. netif_err(efx, hw, efx->net_dev, "failed to flush %d queues "
  602. "(rx %d+%d)\n", atomic_read(&efx->active_queues),
  603. atomic_read(&efx->rxq_flush_outstanding),
  604. atomic_read(&efx->rxq_flush_pending));
  605. rc = -ETIMEDOUT;
  606. atomic_set(&efx->active_queues, 0);
  607. atomic_set(&efx->rxq_flush_pending, 0);
  608. atomic_set(&efx->rxq_flush_outstanding, 0);
  609. }
  610. return rc;
  611. }
  612. int efx_farch_fini_dmaq(struct efx_nic *efx)
  613. {
  614. struct efx_channel *channel;
  615. struct efx_tx_queue *tx_queue;
  616. struct efx_rx_queue *rx_queue;
  617. int rc = 0;
  618. /* Do not attempt to write to the NIC during EEH recovery */
  619. if (efx->state != STATE_RECOVERY) {
  620. /* Only perform flush if DMA is enabled */
  621. if (efx->pci_dev->is_busmaster) {
  622. efx->type->prepare_flush(efx);
  623. rc = efx_farch_do_flush(efx);
  624. efx->type->finish_flush(efx);
  625. }
  626. efx_for_each_channel(channel, efx) {
  627. efx_for_each_channel_rx_queue(rx_queue, channel)
  628. efx_farch_rx_fini(rx_queue);
  629. efx_for_each_channel_tx_queue(tx_queue, channel)
  630. efx_farch_tx_fini(tx_queue);
  631. }
  632. }
  633. return rc;
  634. }
  635. /* Reset queue and flush accounting after FLR
  636. *
  637. * One possible cause of FLR recovery is that DMA may be failing (eg. if bus
  638. * mastering was disabled), in which case we don't receive (RXQ) flush
  639. * completion events. This means that efx->rxq_flush_outstanding remained at 4
  640. * after the FLR; also, efx->active_queues was non-zero (as no flush completion
  641. * events were received, and we didn't go through efx_check_tx_flush_complete())
  642. * If we don't fix this up, on the next call to efx_realloc_channels() we won't
  643. * flush any RX queues because efx->rxq_flush_outstanding is at the limit of 4
  644. * for batched flush requests; and the efx->active_queues gets messed up because
  645. * we keep incrementing for the newly initialised queues, but it never went to
  646. * zero previously. Then we get a timeout every time we try to restart the
  647. * queues, as it doesn't go back to zero when we should be flushing the queues.
  648. */
  649. void efx_farch_finish_flr(struct efx_nic *efx)
  650. {
  651. atomic_set(&efx->rxq_flush_pending, 0);
  652. atomic_set(&efx->rxq_flush_outstanding, 0);
  653. atomic_set(&efx->active_queues, 0);
  654. }
  655. /**************************************************************************
  656. *
  657. * Event queue processing
  658. * Event queues are processed by per-channel tasklets.
  659. *
  660. **************************************************************************/
  661. /* Update a channel's event queue's read pointer (RPTR) register
  662. *
  663. * This writes the EVQ_RPTR_REG register for the specified channel's
  664. * event queue.
  665. */
  666. void efx_farch_ev_read_ack(struct efx_channel *channel)
  667. {
  668. efx_dword_t reg;
  669. struct efx_nic *efx = channel->efx;
  670. EFX_POPULATE_DWORD_1(reg, FRF_AZ_EVQ_RPTR,
  671. channel->eventq_read_ptr & channel->eventq_mask);
  672. /* For Falcon A1, EVQ_RPTR_KER is documented as having a step size
  673. * of 4 bytes, but it is really 16 bytes just like later revisions.
  674. */
  675. efx_writed(efx, &reg,
  676. efx->type->evq_rptr_tbl_base +
  677. FR_BZ_EVQ_RPTR_STEP * channel->channel);
  678. }
  679. /* Use HW to insert a SW defined event */
  680. void efx_farch_generate_event(struct efx_nic *efx, unsigned int evq,
  681. efx_qword_t *event)
  682. {
  683. efx_oword_t drv_ev_reg;
  684. BUILD_BUG_ON(FRF_AZ_DRV_EV_DATA_LBN != 0 ||
  685. FRF_AZ_DRV_EV_DATA_WIDTH != 64);
  686. drv_ev_reg.u32[0] = event->u32[0];
  687. drv_ev_reg.u32[1] = event->u32[1];
  688. drv_ev_reg.u32[2] = 0;
  689. drv_ev_reg.u32[3] = 0;
  690. EFX_SET_OWORD_FIELD(drv_ev_reg, FRF_AZ_DRV_EV_QID, evq);
  691. efx_writeo(efx, &drv_ev_reg, FR_AZ_DRV_EV);
  692. }
  693. static void efx_farch_magic_event(struct efx_channel *channel, u32 magic)
  694. {
  695. efx_qword_t event;
  696. EFX_POPULATE_QWORD_2(event, FSF_AZ_EV_CODE,
  697. FSE_AZ_EV_CODE_DRV_GEN_EV,
  698. FSF_AZ_DRV_GEN_EV_MAGIC, magic);
  699. efx_farch_generate_event(channel->efx, channel->channel, &event);
  700. }
  701. /* Handle a transmit completion event
  702. *
  703. * The NIC batches TX completion events; the message we receive is of
  704. * the form "complete all TX events up to this index".
  705. */
  706. static int
  707. efx_farch_handle_tx_event(struct efx_channel *channel, efx_qword_t *event)
  708. {
  709. unsigned int tx_ev_desc_ptr;
  710. unsigned int tx_ev_q_label;
  711. struct efx_tx_queue *tx_queue;
  712. struct efx_nic *efx = channel->efx;
  713. int tx_packets = 0;
  714. if (unlikely(ACCESS_ONCE(efx->reset_pending)))
  715. return 0;
  716. if (likely(EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_COMP))) {
  717. /* Transmit completion */
  718. tx_ev_desc_ptr = EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_DESC_PTR);
  719. tx_ev_q_label = EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_Q_LABEL);
  720. tx_queue = efx_channel_get_tx_queue(
  721. channel, tx_ev_q_label % EFX_TXQ_TYPES);
  722. tx_packets = ((tx_ev_desc_ptr - tx_queue->read_count) &
  723. tx_queue->ptr_mask);
  724. efx_xmit_done(tx_queue, tx_ev_desc_ptr);
  725. } else if (EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_WQ_FF_FULL)) {
  726. /* Rewrite the FIFO write pointer */
  727. tx_ev_q_label = EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_Q_LABEL);
  728. tx_queue = efx_channel_get_tx_queue(
  729. channel, tx_ev_q_label % EFX_TXQ_TYPES);
  730. netif_tx_lock(efx->net_dev);
  731. efx_farch_notify_tx_desc(tx_queue);
  732. netif_tx_unlock(efx->net_dev);
  733. } else if (EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_PKT_ERR)) {
  734. efx_schedule_reset(efx, RESET_TYPE_DMA_ERROR);
  735. } else {
  736. netif_err(efx, tx_err, efx->net_dev,
  737. "channel %d unexpected TX event "
  738. EFX_QWORD_FMT"\n", channel->channel,
  739. EFX_QWORD_VAL(*event));
  740. }
  741. return tx_packets;
  742. }
  743. /* Detect errors included in the rx_evt_pkt_ok bit. */
  744. static u16 efx_farch_handle_rx_not_ok(struct efx_rx_queue *rx_queue,
  745. const efx_qword_t *event)
  746. {
  747. struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
  748. struct efx_nic *efx = rx_queue->efx;
  749. bool rx_ev_buf_owner_id_err, rx_ev_ip_hdr_chksum_err;
  750. bool rx_ev_tcp_udp_chksum_err, rx_ev_eth_crc_err;
  751. bool rx_ev_frm_trunc, rx_ev_drib_nib, rx_ev_tobe_disc;
  752. bool rx_ev_other_err, rx_ev_pause_frm;
  753. bool rx_ev_hdr_type, rx_ev_mcast_pkt;
  754. unsigned rx_ev_pkt_type;
  755. rx_ev_hdr_type = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_HDR_TYPE);
  756. rx_ev_mcast_pkt = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_MCAST_PKT);
  757. rx_ev_tobe_disc = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_TOBE_DISC);
  758. rx_ev_pkt_type = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_PKT_TYPE);
  759. rx_ev_buf_owner_id_err = EFX_QWORD_FIELD(*event,
  760. FSF_AZ_RX_EV_BUF_OWNER_ID_ERR);
  761. rx_ev_ip_hdr_chksum_err = EFX_QWORD_FIELD(*event,
  762. FSF_AZ_RX_EV_IP_HDR_CHKSUM_ERR);
  763. rx_ev_tcp_udp_chksum_err = EFX_QWORD_FIELD(*event,
  764. FSF_AZ_RX_EV_TCP_UDP_CHKSUM_ERR);
  765. rx_ev_eth_crc_err = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_ETH_CRC_ERR);
  766. rx_ev_frm_trunc = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_FRM_TRUNC);
  767. rx_ev_drib_nib = ((efx_nic_rev(efx) >= EFX_REV_FALCON_B0) ?
  768. 0 : EFX_QWORD_FIELD(*event, FSF_AA_RX_EV_DRIB_NIB));
  769. rx_ev_pause_frm = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_PAUSE_FRM_ERR);
  770. /* Every error apart from tobe_disc and pause_frm */
  771. rx_ev_other_err = (rx_ev_drib_nib | rx_ev_tcp_udp_chksum_err |
  772. rx_ev_buf_owner_id_err | rx_ev_eth_crc_err |
  773. rx_ev_frm_trunc | rx_ev_ip_hdr_chksum_err);
  774. /* Count errors that are not in MAC stats. Ignore expected
  775. * checksum errors during self-test. */
  776. if (rx_ev_frm_trunc)
  777. ++channel->n_rx_frm_trunc;
  778. else if (rx_ev_tobe_disc)
  779. ++channel->n_rx_tobe_disc;
  780. else if (!efx->loopback_selftest) {
  781. if (rx_ev_ip_hdr_chksum_err)
  782. ++channel->n_rx_ip_hdr_chksum_err;
  783. else if (rx_ev_tcp_udp_chksum_err)
  784. ++channel->n_rx_tcp_udp_chksum_err;
  785. }
  786. /* TOBE_DISC is expected on unicast mismatches; don't print out an
  787. * error message. FRM_TRUNC indicates RXDP dropped the packet due
  788. * to a FIFO overflow.
  789. */
  790. #ifdef DEBUG
  791. if (rx_ev_other_err && net_ratelimit()) {
  792. netif_dbg(efx, rx_err, efx->net_dev,
  793. " RX queue %d unexpected RX event "
  794. EFX_QWORD_FMT "%s%s%s%s%s%s%s%s\n",
  795. efx_rx_queue_index(rx_queue), EFX_QWORD_VAL(*event),
  796. rx_ev_buf_owner_id_err ? " [OWNER_ID_ERR]" : "",
  797. rx_ev_ip_hdr_chksum_err ?
  798. " [IP_HDR_CHKSUM_ERR]" : "",
  799. rx_ev_tcp_udp_chksum_err ?
  800. " [TCP_UDP_CHKSUM_ERR]" : "",
  801. rx_ev_eth_crc_err ? " [ETH_CRC_ERR]" : "",
  802. rx_ev_frm_trunc ? " [FRM_TRUNC]" : "",
  803. rx_ev_drib_nib ? " [DRIB_NIB]" : "",
  804. rx_ev_tobe_disc ? " [TOBE_DISC]" : "",
  805. rx_ev_pause_frm ? " [PAUSE]" : "");
  806. }
  807. #endif
  808. /* The frame must be discarded if any of these are true. */
  809. return (rx_ev_eth_crc_err | rx_ev_frm_trunc | rx_ev_drib_nib |
  810. rx_ev_tobe_disc | rx_ev_pause_frm) ?
  811. EFX_RX_PKT_DISCARD : 0;
  812. }
  813. /* Handle receive events that are not in-order. Return true if this
  814. * can be handled as a partial packet discard, false if it's more
  815. * serious.
  816. */
  817. static bool
  818. efx_farch_handle_rx_bad_index(struct efx_rx_queue *rx_queue, unsigned index)
  819. {
  820. struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
  821. struct efx_nic *efx = rx_queue->efx;
  822. unsigned expected, dropped;
  823. if (rx_queue->scatter_n &&
  824. index == ((rx_queue->removed_count + rx_queue->scatter_n - 1) &
  825. rx_queue->ptr_mask)) {
  826. ++channel->n_rx_nodesc_trunc;
  827. return true;
  828. }
  829. expected = rx_queue->removed_count & rx_queue->ptr_mask;
  830. dropped = (index - expected) & rx_queue->ptr_mask;
  831. netif_info(efx, rx_err, efx->net_dev,
  832. "dropped %d events (index=%d expected=%d)\n",
  833. dropped, index, expected);
  834. efx_schedule_reset(efx, EFX_WORKAROUND_5676(efx) ?
  835. RESET_TYPE_RX_RECOVERY : RESET_TYPE_DISABLE);
  836. return false;
  837. }
  838. /* Handle a packet received event
  839. *
  840. * The NIC gives a "discard" flag if it's a unicast packet with the
  841. * wrong destination address
  842. * Also "is multicast" and "matches multicast filter" flags can be used to
  843. * discard non-matching multicast packets.
  844. */
  845. static void
  846. efx_farch_handle_rx_event(struct efx_channel *channel, const efx_qword_t *event)
  847. {
  848. unsigned int rx_ev_desc_ptr, rx_ev_byte_cnt;
  849. unsigned int rx_ev_hdr_type, rx_ev_mcast_pkt;
  850. unsigned expected_ptr;
  851. bool rx_ev_pkt_ok, rx_ev_sop, rx_ev_cont;
  852. u16 flags;
  853. struct efx_rx_queue *rx_queue;
  854. struct efx_nic *efx = channel->efx;
  855. if (unlikely(ACCESS_ONCE(efx->reset_pending)))
  856. return;
  857. rx_ev_cont = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_JUMBO_CONT);
  858. rx_ev_sop = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_SOP);
  859. WARN_ON(EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_Q_LABEL) !=
  860. channel->channel);
  861. rx_queue = efx_channel_get_rx_queue(channel);
  862. rx_ev_desc_ptr = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_DESC_PTR);
  863. expected_ptr = ((rx_queue->removed_count + rx_queue->scatter_n) &
  864. rx_queue->ptr_mask);
  865. /* Check for partial drops and other errors */
  866. if (unlikely(rx_ev_desc_ptr != expected_ptr) ||
  867. unlikely(rx_ev_sop != (rx_queue->scatter_n == 0))) {
  868. if (rx_ev_desc_ptr != expected_ptr &&
  869. !efx_farch_handle_rx_bad_index(rx_queue, rx_ev_desc_ptr))
  870. return;
  871. /* Discard all pending fragments */
  872. if (rx_queue->scatter_n) {
  873. efx_rx_packet(
  874. rx_queue,
  875. rx_queue->removed_count & rx_queue->ptr_mask,
  876. rx_queue->scatter_n, 0, EFX_RX_PKT_DISCARD);
  877. rx_queue->removed_count += rx_queue->scatter_n;
  878. rx_queue->scatter_n = 0;
  879. }
  880. /* Return if there is no new fragment */
  881. if (rx_ev_desc_ptr != expected_ptr)
  882. return;
  883. /* Discard new fragment if not SOP */
  884. if (!rx_ev_sop) {
  885. efx_rx_packet(
  886. rx_queue,
  887. rx_queue->removed_count & rx_queue->ptr_mask,
  888. 1, 0, EFX_RX_PKT_DISCARD);
  889. ++rx_queue->removed_count;
  890. return;
  891. }
  892. }
  893. ++rx_queue->scatter_n;
  894. if (rx_ev_cont)
  895. return;
  896. rx_ev_byte_cnt = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_BYTE_CNT);
  897. rx_ev_pkt_ok = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_PKT_OK);
  898. rx_ev_hdr_type = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_HDR_TYPE);
  899. if (likely(rx_ev_pkt_ok)) {
  900. /* If packet is marked as OK then we can rely on the
  901. * hardware checksum and classification.
  902. */
  903. flags = 0;
  904. switch (rx_ev_hdr_type) {
  905. case FSE_CZ_RX_EV_HDR_TYPE_IPV4V6_TCP:
  906. flags |= EFX_RX_PKT_TCP;
  907. /* fall through */
  908. case FSE_CZ_RX_EV_HDR_TYPE_IPV4V6_UDP:
  909. flags |= EFX_RX_PKT_CSUMMED;
  910. /* fall through */
  911. case FSE_CZ_RX_EV_HDR_TYPE_IPV4V6_OTHER:
  912. case FSE_AZ_RX_EV_HDR_TYPE_OTHER:
  913. break;
  914. }
  915. } else {
  916. flags = efx_farch_handle_rx_not_ok(rx_queue, event);
  917. }
  918. /* Detect multicast packets that didn't match the filter */
  919. rx_ev_mcast_pkt = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_MCAST_PKT);
  920. if (rx_ev_mcast_pkt) {
  921. unsigned int rx_ev_mcast_hash_match =
  922. EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_MCAST_HASH_MATCH);
  923. if (unlikely(!rx_ev_mcast_hash_match)) {
  924. ++channel->n_rx_mcast_mismatch;
  925. flags |= EFX_RX_PKT_DISCARD;
  926. }
  927. }
  928. channel->irq_mod_score += 2;
  929. /* Handle received packet */
  930. efx_rx_packet(rx_queue,
  931. rx_queue->removed_count & rx_queue->ptr_mask,
  932. rx_queue->scatter_n, rx_ev_byte_cnt, flags);
  933. rx_queue->removed_count += rx_queue->scatter_n;
  934. rx_queue->scatter_n = 0;
  935. }
  936. /* If this flush done event corresponds to a &struct efx_tx_queue, then
  937. * send an %EFX_CHANNEL_MAGIC_TX_DRAIN event to drain the event queue
  938. * of all transmit completions.
  939. */
  940. static void
  941. efx_farch_handle_tx_flush_done(struct efx_nic *efx, efx_qword_t *event)
  942. {
  943. struct efx_tx_queue *tx_queue;
  944. int qid;
  945. qid = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_SUBDATA);
  946. if (qid < EFX_TXQ_TYPES * efx->n_tx_channels) {
  947. tx_queue = efx_get_tx_queue(efx, qid / EFX_TXQ_TYPES,
  948. qid % EFX_TXQ_TYPES);
  949. if (atomic_cmpxchg(&tx_queue->flush_outstanding, 1, 0)) {
  950. efx_farch_magic_event(tx_queue->channel,
  951. EFX_CHANNEL_MAGIC_TX_DRAIN(tx_queue));
  952. }
  953. }
  954. }
  955. /* If this flush done event corresponds to a &struct efx_rx_queue: If the flush
  956. * was successful then send an %EFX_CHANNEL_MAGIC_RX_DRAIN, otherwise add
  957. * the RX queue back to the mask of RX queues in need of flushing.
  958. */
  959. static void
  960. efx_farch_handle_rx_flush_done(struct efx_nic *efx, efx_qword_t *event)
  961. {
  962. struct efx_channel *channel;
  963. struct efx_rx_queue *rx_queue;
  964. int qid;
  965. bool failed;
  966. qid = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_RX_DESCQ_ID);
  967. failed = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_RX_FLUSH_FAIL);
  968. if (qid >= efx->n_channels)
  969. return;
  970. channel = efx_get_channel(efx, qid);
  971. if (!efx_channel_has_rx_queue(channel))
  972. return;
  973. rx_queue = efx_channel_get_rx_queue(channel);
  974. if (failed) {
  975. netif_info(efx, hw, efx->net_dev,
  976. "RXQ %d flush retry\n", qid);
  977. rx_queue->flush_pending = true;
  978. atomic_inc(&efx->rxq_flush_pending);
  979. } else {
  980. efx_farch_magic_event(efx_rx_queue_channel(rx_queue),
  981. EFX_CHANNEL_MAGIC_RX_DRAIN(rx_queue));
  982. }
  983. atomic_dec(&efx->rxq_flush_outstanding);
  984. if (efx_farch_flush_wake(efx))
  985. wake_up(&efx->flush_wq);
  986. }
  987. static void
  988. efx_farch_handle_drain_event(struct efx_channel *channel)
  989. {
  990. struct efx_nic *efx = channel->efx;
  991. WARN_ON(atomic_read(&efx->active_queues) == 0);
  992. atomic_dec(&efx->active_queues);
  993. if (efx_farch_flush_wake(efx))
  994. wake_up(&efx->flush_wq);
  995. }
  996. static void efx_farch_handle_generated_event(struct efx_channel *channel,
  997. efx_qword_t *event)
  998. {
  999. struct efx_nic *efx = channel->efx;
  1000. struct efx_rx_queue *rx_queue =
  1001. efx_channel_has_rx_queue(channel) ?
  1002. efx_channel_get_rx_queue(channel) : NULL;
  1003. unsigned magic, code;
  1004. magic = EFX_QWORD_FIELD(*event, FSF_AZ_DRV_GEN_EV_MAGIC);
  1005. code = _EFX_CHANNEL_MAGIC_CODE(magic);
  1006. if (magic == EFX_CHANNEL_MAGIC_TEST(channel)) {
  1007. channel->event_test_cpu = raw_smp_processor_id();
  1008. } else if (rx_queue && magic == EFX_CHANNEL_MAGIC_FILL(rx_queue)) {
  1009. /* The queue must be empty, so we won't receive any rx
  1010. * events, so efx_process_channel() won't refill the
  1011. * queue. Refill it here */
  1012. efx_fast_push_rx_descriptors(rx_queue, true);
  1013. } else if (rx_queue && magic == EFX_CHANNEL_MAGIC_RX_DRAIN(rx_queue)) {
  1014. efx_farch_handle_drain_event(channel);
  1015. } else if (code == _EFX_CHANNEL_MAGIC_TX_DRAIN) {
  1016. efx_farch_handle_drain_event(channel);
  1017. } else {
  1018. netif_dbg(efx, hw, efx->net_dev, "channel %d received "
  1019. "generated event "EFX_QWORD_FMT"\n",
  1020. channel->channel, EFX_QWORD_VAL(*event));
  1021. }
  1022. }
  1023. static void
  1024. efx_farch_handle_driver_event(struct efx_channel *channel, efx_qword_t *event)
  1025. {
  1026. struct efx_nic *efx = channel->efx;
  1027. unsigned int ev_sub_code;
  1028. unsigned int ev_sub_data;
  1029. ev_sub_code = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_SUBCODE);
  1030. ev_sub_data = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_SUBDATA);
  1031. switch (ev_sub_code) {
  1032. case FSE_AZ_TX_DESCQ_FLS_DONE_EV:
  1033. netif_vdbg(efx, hw, efx->net_dev, "channel %d TXQ %d flushed\n",
  1034. channel->channel, ev_sub_data);
  1035. efx_farch_handle_tx_flush_done(efx, event);
  1036. #ifdef CONFIG_SFC_SRIOV
  1037. efx_siena_sriov_tx_flush_done(efx, event);
  1038. #endif
  1039. break;
  1040. case FSE_AZ_RX_DESCQ_FLS_DONE_EV:
  1041. netif_vdbg(efx, hw, efx->net_dev, "channel %d RXQ %d flushed\n",
  1042. channel->channel, ev_sub_data);
  1043. efx_farch_handle_rx_flush_done(efx, event);
  1044. #ifdef CONFIG_SFC_SRIOV
  1045. efx_siena_sriov_rx_flush_done(efx, event);
  1046. #endif
  1047. break;
  1048. case FSE_AZ_EVQ_INIT_DONE_EV:
  1049. netif_dbg(efx, hw, efx->net_dev,
  1050. "channel %d EVQ %d initialised\n",
  1051. channel->channel, ev_sub_data);
  1052. break;
  1053. case FSE_AZ_SRM_UPD_DONE_EV:
  1054. netif_vdbg(efx, hw, efx->net_dev,
  1055. "channel %d SRAM update done\n", channel->channel);
  1056. break;
  1057. case FSE_AZ_WAKE_UP_EV:
  1058. netif_vdbg(efx, hw, efx->net_dev,
  1059. "channel %d RXQ %d wakeup event\n",
  1060. channel->channel, ev_sub_data);
  1061. break;
  1062. case FSE_AZ_TIMER_EV:
  1063. netif_vdbg(efx, hw, efx->net_dev,
  1064. "channel %d RX queue %d timer expired\n",
  1065. channel->channel, ev_sub_data);
  1066. break;
  1067. case FSE_AA_RX_RECOVER_EV:
  1068. netif_err(efx, rx_err, efx->net_dev,
  1069. "channel %d seen DRIVER RX_RESET event. "
  1070. "Resetting.\n", channel->channel);
  1071. atomic_inc(&efx->rx_reset);
  1072. efx_schedule_reset(efx,
  1073. EFX_WORKAROUND_6555(efx) ?
  1074. RESET_TYPE_RX_RECOVERY :
  1075. RESET_TYPE_DISABLE);
  1076. break;
  1077. case FSE_BZ_RX_DSC_ERROR_EV:
  1078. if (ev_sub_data < EFX_VI_BASE) {
  1079. netif_err(efx, rx_err, efx->net_dev,
  1080. "RX DMA Q %d reports descriptor fetch error."
  1081. " RX Q %d is disabled.\n", ev_sub_data,
  1082. ev_sub_data);
  1083. efx_schedule_reset(efx, RESET_TYPE_DMA_ERROR);
  1084. }
  1085. #ifdef CONFIG_SFC_SRIOV
  1086. else
  1087. efx_siena_sriov_desc_fetch_err(efx, ev_sub_data);
  1088. #endif
  1089. break;
  1090. case FSE_BZ_TX_DSC_ERROR_EV:
  1091. if (ev_sub_data < EFX_VI_BASE) {
  1092. netif_err(efx, tx_err, efx->net_dev,
  1093. "TX DMA Q %d reports descriptor fetch error."
  1094. " TX Q %d is disabled.\n", ev_sub_data,
  1095. ev_sub_data);
  1096. efx_schedule_reset(efx, RESET_TYPE_DMA_ERROR);
  1097. }
  1098. #ifdef CONFIG_SFC_SRIOV
  1099. else
  1100. efx_siena_sriov_desc_fetch_err(efx, ev_sub_data);
  1101. #endif
  1102. break;
  1103. default:
  1104. netif_vdbg(efx, hw, efx->net_dev,
  1105. "channel %d unknown driver event code %d "
  1106. "data %04x\n", channel->channel, ev_sub_code,
  1107. ev_sub_data);
  1108. break;
  1109. }
  1110. }
  1111. int efx_farch_ev_process(struct efx_channel *channel, int budget)
  1112. {
  1113. struct efx_nic *efx = channel->efx;
  1114. unsigned int read_ptr;
  1115. efx_qword_t event, *p_event;
  1116. int ev_code;
  1117. int tx_packets = 0;
  1118. int spent = 0;
  1119. if (budget <= 0)
  1120. return spent;
  1121. read_ptr = channel->eventq_read_ptr;
  1122. for (;;) {
  1123. p_event = efx_event(channel, read_ptr);
  1124. event = *p_event;
  1125. if (!efx_event_present(&event))
  1126. /* End of events */
  1127. break;
  1128. netif_vdbg(channel->efx, intr, channel->efx->net_dev,
  1129. "channel %d event is "EFX_QWORD_FMT"\n",
  1130. channel->channel, EFX_QWORD_VAL(event));
  1131. /* Clear this event by marking it all ones */
  1132. EFX_SET_QWORD(*p_event);
  1133. ++read_ptr;
  1134. ev_code = EFX_QWORD_FIELD(event, FSF_AZ_EV_CODE);
  1135. switch (ev_code) {
  1136. case FSE_AZ_EV_CODE_RX_EV:
  1137. efx_farch_handle_rx_event(channel, &event);
  1138. if (++spent == budget)
  1139. goto out;
  1140. break;
  1141. case FSE_AZ_EV_CODE_TX_EV:
  1142. tx_packets += efx_farch_handle_tx_event(channel,
  1143. &event);
  1144. if (tx_packets > efx->txq_entries) {
  1145. spent = budget;
  1146. goto out;
  1147. }
  1148. break;
  1149. case FSE_AZ_EV_CODE_DRV_GEN_EV:
  1150. efx_farch_handle_generated_event(channel, &event);
  1151. break;
  1152. case FSE_AZ_EV_CODE_DRIVER_EV:
  1153. efx_farch_handle_driver_event(channel, &event);
  1154. break;
  1155. #ifdef CONFIG_SFC_SRIOV
  1156. case FSE_CZ_EV_CODE_USER_EV:
  1157. efx_siena_sriov_event(channel, &event);
  1158. break;
  1159. #endif
  1160. case FSE_CZ_EV_CODE_MCDI_EV:
  1161. efx_mcdi_process_event(channel, &event);
  1162. break;
  1163. case FSE_AZ_EV_CODE_GLOBAL_EV:
  1164. if (efx->type->handle_global_event &&
  1165. efx->type->handle_global_event(channel, &event))
  1166. break;
  1167. /* else fall through */
  1168. default:
  1169. netif_err(channel->efx, hw, channel->efx->net_dev,
  1170. "channel %d unknown event type %d (data "
  1171. EFX_QWORD_FMT ")\n", channel->channel,
  1172. ev_code, EFX_QWORD_VAL(event));
  1173. }
  1174. }
  1175. out:
  1176. channel->eventq_read_ptr = read_ptr;
  1177. return spent;
  1178. }
  1179. /* Allocate buffer table entries for event queue */
  1180. int efx_farch_ev_probe(struct efx_channel *channel)
  1181. {
  1182. struct efx_nic *efx = channel->efx;
  1183. unsigned entries;
  1184. entries = channel->eventq_mask + 1;
  1185. return efx_alloc_special_buffer(efx, &channel->eventq,
  1186. entries * sizeof(efx_qword_t));
  1187. }
  1188. int efx_farch_ev_init(struct efx_channel *channel)
  1189. {
  1190. efx_oword_t reg;
  1191. struct efx_nic *efx = channel->efx;
  1192. netif_dbg(efx, hw, efx->net_dev,
  1193. "channel %d event queue in special buffers %d-%d\n",
  1194. channel->channel, channel->eventq.index,
  1195. channel->eventq.index + channel->eventq.entries - 1);
  1196. if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
  1197. EFX_POPULATE_OWORD_3(reg,
  1198. FRF_CZ_TIMER_Q_EN, 1,
  1199. FRF_CZ_HOST_NOTIFY_MODE, 0,
  1200. FRF_CZ_TIMER_MODE, FFE_CZ_TIMER_MODE_DIS);
  1201. efx_writeo_table(efx, &reg, FR_BZ_TIMER_TBL, channel->channel);
  1202. }
  1203. /* Pin event queue buffer */
  1204. efx_init_special_buffer(efx, &channel->eventq);
  1205. /* Fill event queue with all ones (i.e. empty events) */
  1206. memset(channel->eventq.buf.addr, 0xff, channel->eventq.buf.len);
  1207. /* Push event queue to card */
  1208. EFX_POPULATE_OWORD_3(reg,
  1209. FRF_AZ_EVQ_EN, 1,
  1210. FRF_AZ_EVQ_SIZE, __ffs(channel->eventq.entries),
  1211. FRF_AZ_EVQ_BUF_BASE_ID, channel->eventq.index);
  1212. efx_writeo_table(efx, &reg, efx->type->evq_ptr_tbl_base,
  1213. channel->channel);
  1214. return 0;
  1215. }
  1216. void efx_farch_ev_fini(struct efx_channel *channel)
  1217. {
  1218. efx_oword_t reg;
  1219. struct efx_nic *efx = channel->efx;
  1220. /* Remove event queue from card */
  1221. EFX_ZERO_OWORD(reg);
  1222. efx_writeo_table(efx, &reg, efx->type->evq_ptr_tbl_base,
  1223. channel->channel);
  1224. if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0)
  1225. efx_writeo_table(efx, &reg, FR_BZ_TIMER_TBL, channel->channel);
  1226. /* Unpin event queue */
  1227. efx_fini_special_buffer(efx, &channel->eventq);
  1228. }
  1229. /* Free buffers backing event queue */
  1230. void efx_farch_ev_remove(struct efx_channel *channel)
  1231. {
  1232. efx_free_special_buffer(channel->efx, &channel->eventq);
  1233. }
  1234. void efx_farch_ev_test_generate(struct efx_channel *channel)
  1235. {
  1236. efx_farch_magic_event(channel, EFX_CHANNEL_MAGIC_TEST(channel));
  1237. }
  1238. void efx_farch_rx_defer_refill(struct efx_rx_queue *rx_queue)
  1239. {
  1240. efx_farch_magic_event(efx_rx_queue_channel(rx_queue),
  1241. EFX_CHANNEL_MAGIC_FILL(rx_queue));
  1242. }
  1243. /**************************************************************************
  1244. *
  1245. * Hardware interrupts
  1246. * The hardware interrupt handler does very little work; all the event
  1247. * queue processing is carried out by per-channel tasklets.
  1248. *
  1249. **************************************************************************/
  1250. /* Enable/disable/generate interrupts */
  1251. static inline void efx_farch_interrupts(struct efx_nic *efx,
  1252. bool enabled, bool force)
  1253. {
  1254. efx_oword_t int_en_reg_ker;
  1255. EFX_POPULATE_OWORD_3(int_en_reg_ker,
  1256. FRF_AZ_KER_INT_LEVE_SEL, efx->irq_level,
  1257. FRF_AZ_KER_INT_KER, force,
  1258. FRF_AZ_DRV_INT_EN_KER, enabled);
  1259. efx_writeo(efx, &int_en_reg_ker, FR_AZ_INT_EN_KER);
  1260. }
  1261. void efx_farch_irq_enable_master(struct efx_nic *efx)
  1262. {
  1263. EFX_ZERO_OWORD(*((efx_oword_t *) efx->irq_status.addr));
  1264. wmb(); /* Ensure interrupt vector is clear before interrupts enabled */
  1265. efx_farch_interrupts(efx, true, false);
  1266. }
  1267. void efx_farch_irq_disable_master(struct efx_nic *efx)
  1268. {
  1269. /* Disable interrupts */
  1270. efx_farch_interrupts(efx, false, false);
  1271. }
  1272. /* Generate a test interrupt
  1273. * Interrupt must already have been enabled, otherwise nasty things
  1274. * may happen.
  1275. */
  1276. void efx_farch_irq_test_generate(struct efx_nic *efx)
  1277. {
  1278. efx_farch_interrupts(efx, true, true);
  1279. }
  1280. /* Process a fatal interrupt
  1281. * Disable bus mastering ASAP and schedule a reset
  1282. */
  1283. irqreturn_t efx_farch_fatal_interrupt(struct efx_nic *efx)
  1284. {
  1285. struct falcon_nic_data *nic_data = efx->nic_data;
  1286. efx_oword_t *int_ker = efx->irq_status.addr;
  1287. efx_oword_t fatal_intr;
  1288. int error, mem_perr;
  1289. efx_reado(efx, &fatal_intr, FR_AZ_FATAL_INTR_KER);
  1290. error = EFX_OWORD_FIELD(fatal_intr, FRF_AZ_FATAL_INTR);
  1291. netif_err(efx, hw, efx->net_dev, "SYSTEM ERROR "EFX_OWORD_FMT" status "
  1292. EFX_OWORD_FMT ": %s\n", EFX_OWORD_VAL(*int_ker),
  1293. EFX_OWORD_VAL(fatal_intr),
  1294. error ? "disabling bus mastering" : "no recognised error");
  1295. /* If this is a memory parity error dump which blocks are offending */
  1296. mem_perr = (EFX_OWORD_FIELD(fatal_intr, FRF_AZ_MEM_PERR_INT_KER) ||
  1297. EFX_OWORD_FIELD(fatal_intr, FRF_AZ_SRM_PERR_INT_KER));
  1298. if (mem_perr) {
  1299. efx_oword_t reg;
  1300. efx_reado(efx, &reg, FR_AZ_MEM_STAT);
  1301. netif_err(efx, hw, efx->net_dev,
  1302. "SYSTEM ERROR: memory parity error "EFX_OWORD_FMT"\n",
  1303. EFX_OWORD_VAL(reg));
  1304. }
  1305. /* Disable both devices */
  1306. pci_clear_master(efx->pci_dev);
  1307. if (efx_nic_is_dual_func(efx))
  1308. pci_clear_master(nic_data->pci_dev2);
  1309. efx_farch_irq_disable_master(efx);
  1310. /* Count errors and reset or disable the NIC accordingly */
  1311. if (efx->int_error_count == 0 ||
  1312. time_after(jiffies, efx->int_error_expire)) {
  1313. efx->int_error_count = 0;
  1314. efx->int_error_expire =
  1315. jiffies + EFX_INT_ERROR_EXPIRE * HZ;
  1316. }
  1317. if (++efx->int_error_count < EFX_MAX_INT_ERRORS) {
  1318. netif_err(efx, hw, efx->net_dev,
  1319. "SYSTEM ERROR - reset scheduled\n");
  1320. efx_schedule_reset(efx, RESET_TYPE_INT_ERROR);
  1321. } else {
  1322. netif_err(efx, hw, efx->net_dev,
  1323. "SYSTEM ERROR - max number of errors seen."
  1324. "NIC will be disabled\n");
  1325. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  1326. }
  1327. return IRQ_HANDLED;
  1328. }
  1329. /* Handle a legacy interrupt
  1330. * Acknowledges the interrupt and schedule event queue processing.
  1331. */
  1332. irqreturn_t efx_farch_legacy_interrupt(int irq, void *dev_id)
  1333. {
  1334. struct efx_nic *efx = dev_id;
  1335. bool soft_enabled = ACCESS_ONCE(efx->irq_soft_enabled);
  1336. efx_oword_t *int_ker = efx->irq_status.addr;
  1337. irqreturn_t result = IRQ_NONE;
  1338. struct efx_channel *channel;
  1339. efx_dword_t reg;
  1340. u32 queues;
  1341. int syserr;
  1342. /* Read the ISR which also ACKs the interrupts */
  1343. efx_readd(efx, &reg, FR_BZ_INT_ISR0);
  1344. queues = EFX_EXTRACT_DWORD(reg, 0, 31);
  1345. /* Legacy interrupts are disabled too late by the EEH kernel
  1346. * code. Disable them earlier.
  1347. * If an EEH error occurred, the read will have returned all ones.
  1348. */
  1349. if (EFX_DWORD_IS_ALL_ONES(reg) && efx_try_recovery(efx) &&
  1350. !efx->eeh_disabled_legacy_irq) {
  1351. disable_irq_nosync(efx->legacy_irq);
  1352. efx->eeh_disabled_legacy_irq = true;
  1353. }
  1354. /* Handle non-event-queue sources */
  1355. if (queues & (1U << efx->irq_level) && soft_enabled) {
  1356. syserr = EFX_OWORD_FIELD(*int_ker, FSF_AZ_NET_IVEC_FATAL_INT);
  1357. if (unlikely(syserr))
  1358. return efx_farch_fatal_interrupt(efx);
  1359. efx->last_irq_cpu = raw_smp_processor_id();
  1360. }
  1361. if (queues != 0) {
  1362. efx->irq_zero_count = 0;
  1363. /* Schedule processing of any interrupting queues */
  1364. if (likely(soft_enabled)) {
  1365. efx_for_each_channel(channel, efx) {
  1366. if (queues & 1)
  1367. efx_schedule_channel_irq(channel);
  1368. queues >>= 1;
  1369. }
  1370. }
  1371. result = IRQ_HANDLED;
  1372. } else {
  1373. efx_qword_t *event;
  1374. /* Legacy ISR read can return zero once (SF bug 15783) */
  1375. /* We can't return IRQ_HANDLED more than once on seeing ISR=0
  1376. * because this might be a shared interrupt. */
  1377. if (efx->irq_zero_count++ == 0)
  1378. result = IRQ_HANDLED;
  1379. /* Ensure we schedule or rearm all event queues */
  1380. if (likely(soft_enabled)) {
  1381. efx_for_each_channel(channel, efx) {
  1382. event = efx_event(channel,
  1383. channel->eventq_read_ptr);
  1384. if (efx_event_present(event))
  1385. efx_schedule_channel_irq(channel);
  1386. else
  1387. efx_farch_ev_read_ack(channel);
  1388. }
  1389. }
  1390. }
  1391. if (result == IRQ_HANDLED)
  1392. netif_vdbg(efx, intr, efx->net_dev,
  1393. "IRQ %d on CPU %d status " EFX_DWORD_FMT "\n",
  1394. irq, raw_smp_processor_id(), EFX_DWORD_VAL(reg));
  1395. return result;
  1396. }
  1397. /* Handle an MSI interrupt
  1398. *
  1399. * Handle an MSI hardware interrupt. This routine schedules event
  1400. * queue processing. No interrupt acknowledgement cycle is necessary.
  1401. * Also, we never need to check that the interrupt is for us, since
  1402. * MSI interrupts cannot be shared.
  1403. */
  1404. irqreturn_t efx_farch_msi_interrupt(int irq, void *dev_id)
  1405. {
  1406. struct efx_msi_context *context = dev_id;
  1407. struct efx_nic *efx = context->efx;
  1408. efx_oword_t *int_ker = efx->irq_status.addr;
  1409. int syserr;
  1410. netif_vdbg(efx, intr, efx->net_dev,
  1411. "IRQ %d on CPU %d status " EFX_OWORD_FMT "\n",
  1412. irq, raw_smp_processor_id(), EFX_OWORD_VAL(*int_ker));
  1413. if (!likely(ACCESS_ONCE(efx->irq_soft_enabled)))
  1414. return IRQ_HANDLED;
  1415. /* Handle non-event-queue sources */
  1416. if (context->index == efx->irq_level) {
  1417. syserr = EFX_OWORD_FIELD(*int_ker, FSF_AZ_NET_IVEC_FATAL_INT);
  1418. if (unlikely(syserr))
  1419. return efx_farch_fatal_interrupt(efx);
  1420. efx->last_irq_cpu = raw_smp_processor_id();
  1421. }
  1422. /* Schedule processing of the channel */
  1423. efx_schedule_channel_irq(efx->channel[context->index]);
  1424. return IRQ_HANDLED;
  1425. }
  1426. /* Setup RSS indirection table.
  1427. * This maps from the hash value of the packet to RXQ
  1428. */
  1429. void efx_farch_rx_push_indir_table(struct efx_nic *efx)
  1430. {
  1431. size_t i = 0;
  1432. efx_dword_t dword;
  1433. BUG_ON(efx_nic_rev(efx) < EFX_REV_FALCON_B0);
  1434. BUILD_BUG_ON(ARRAY_SIZE(efx->rx_indir_table) !=
  1435. FR_BZ_RX_INDIRECTION_TBL_ROWS);
  1436. for (i = 0; i < FR_BZ_RX_INDIRECTION_TBL_ROWS; i++) {
  1437. EFX_POPULATE_DWORD_1(dword, FRF_BZ_IT_QUEUE,
  1438. efx->rx_indir_table[i]);
  1439. efx_writed(efx, &dword,
  1440. FR_BZ_RX_INDIRECTION_TBL +
  1441. FR_BZ_RX_INDIRECTION_TBL_STEP * i);
  1442. }
  1443. }
  1444. /* Looks at available SRAM resources and works out how many queues we
  1445. * can support, and where things like descriptor caches should live.
  1446. *
  1447. * SRAM is split up as follows:
  1448. * 0 buftbl entries for channels
  1449. * efx->vf_buftbl_base buftbl entries for SR-IOV
  1450. * efx->rx_dc_base RX descriptor caches
  1451. * efx->tx_dc_base TX descriptor caches
  1452. */
  1453. void efx_farch_dimension_resources(struct efx_nic *efx, unsigned sram_lim_qw)
  1454. {
  1455. unsigned vi_count, buftbl_min;
  1456. #ifdef CONFIG_SFC_SRIOV
  1457. struct siena_nic_data *nic_data = efx->nic_data;
  1458. #endif
  1459. /* Account for the buffer table entries backing the datapath channels
  1460. * and the descriptor caches for those channels.
  1461. */
  1462. buftbl_min = ((efx->n_rx_channels * EFX_MAX_DMAQ_SIZE +
  1463. efx->n_tx_channels * EFX_TXQ_TYPES * EFX_MAX_DMAQ_SIZE +
  1464. efx->n_channels * EFX_MAX_EVQ_SIZE)
  1465. * sizeof(efx_qword_t) / EFX_BUF_SIZE);
  1466. vi_count = max(efx->n_channels, efx->n_tx_channels * EFX_TXQ_TYPES);
  1467. #ifdef CONFIG_SFC_SRIOV
  1468. if (efx->type->sriov_wanted) {
  1469. if (efx->type->sriov_wanted(efx)) {
  1470. unsigned vi_dc_entries, buftbl_free;
  1471. unsigned entries_per_vf, vf_limit;
  1472. nic_data->vf_buftbl_base = buftbl_min;
  1473. vi_dc_entries = RX_DC_ENTRIES + TX_DC_ENTRIES;
  1474. vi_count = max(vi_count, EFX_VI_BASE);
  1475. buftbl_free = (sram_lim_qw - buftbl_min -
  1476. vi_count * vi_dc_entries);
  1477. entries_per_vf = ((vi_dc_entries +
  1478. EFX_VF_BUFTBL_PER_VI) *
  1479. efx_vf_size(efx));
  1480. vf_limit = min(buftbl_free / entries_per_vf,
  1481. (1024U - EFX_VI_BASE) >> efx->vi_scale);
  1482. if (efx->vf_count > vf_limit) {
  1483. netif_err(efx, probe, efx->net_dev,
  1484. "Reducing VF count from from %d to %d\n",
  1485. efx->vf_count, vf_limit);
  1486. efx->vf_count = vf_limit;
  1487. }
  1488. vi_count += efx->vf_count * efx_vf_size(efx);
  1489. }
  1490. }
  1491. #endif
  1492. efx->tx_dc_base = sram_lim_qw - vi_count * TX_DC_ENTRIES;
  1493. efx->rx_dc_base = efx->tx_dc_base - vi_count * RX_DC_ENTRIES;
  1494. }
  1495. u32 efx_farch_fpga_ver(struct efx_nic *efx)
  1496. {
  1497. efx_oword_t altera_build;
  1498. efx_reado(efx, &altera_build, FR_AZ_ALTERA_BUILD);
  1499. return EFX_OWORD_FIELD(altera_build, FRF_AZ_ALTERA_BUILD_VER);
  1500. }
  1501. void efx_farch_init_common(struct efx_nic *efx)
  1502. {
  1503. efx_oword_t temp;
  1504. /* Set positions of descriptor caches in SRAM. */
  1505. EFX_POPULATE_OWORD_1(temp, FRF_AZ_SRM_TX_DC_BASE_ADR, efx->tx_dc_base);
  1506. efx_writeo(efx, &temp, FR_AZ_SRM_TX_DC_CFG);
  1507. EFX_POPULATE_OWORD_1(temp, FRF_AZ_SRM_RX_DC_BASE_ADR, efx->rx_dc_base);
  1508. efx_writeo(efx, &temp, FR_AZ_SRM_RX_DC_CFG);
  1509. /* Set TX descriptor cache size. */
  1510. BUILD_BUG_ON(TX_DC_ENTRIES != (8 << TX_DC_ENTRIES_ORDER));
  1511. EFX_POPULATE_OWORD_1(temp, FRF_AZ_TX_DC_SIZE, TX_DC_ENTRIES_ORDER);
  1512. efx_writeo(efx, &temp, FR_AZ_TX_DC_CFG);
  1513. /* Set RX descriptor cache size. Set low watermark to size-8, as
  1514. * this allows most efficient prefetching.
  1515. */
  1516. BUILD_BUG_ON(RX_DC_ENTRIES != (8 << RX_DC_ENTRIES_ORDER));
  1517. EFX_POPULATE_OWORD_1(temp, FRF_AZ_RX_DC_SIZE, RX_DC_ENTRIES_ORDER);
  1518. efx_writeo(efx, &temp, FR_AZ_RX_DC_CFG);
  1519. EFX_POPULATE_OWORD_1(temp, FRF_AZ_RX_DC_PF_LWM, RX_DC_ENTRIES - 8);
  1520. efx_writeo(efx, &temp, FR_AZ_RX_DC_PF_WM);
  1521. /* Program INT_KER address */
  1522. EFX_POPULATE_OWORD_2(temp,
  1523. FRF_AZ_NORM_INT_VEC_DIS_KER,
  1524. EFX_INT_MODE_USE_MSI(efx),
  1525. FRF_AZ_INT_ADR_KER, efx->irq_status.dma_addr);
  1526. efx_writeo(efx, &temp, FR_AZ_INT_ADR_KER);
  1527. if (EFX_WORKAROUND_17213(efx) && !EFX_INT_MODE_USE_MSI(efx))
  1528. /* Use an interrupt level unused by event queues */
  1529. efx->irq_level = 0x1f;
  1530. else
  1531. /* Use a valid MSI-X vector */
  1532. efx->irq_level = 0;
  1533. /* Enable all the genuinely fatal interrupts. (They are still
  1534. * masked by the overall interrupt mask, controlled by
  1535. * falcon_interrupts()).
  1536. *
  1537. * Note: All other fatal interrupts are enabled
  1538. */
  1539. EFX_POPULATE_OWORD_3(temp,
  1540. FRF_AZ_ILL_ADR_INT_KER_EN, 1,
  1541. FRF_AZ_RBUF_OWN_INT_KER_EN, 1,
  1542. FRF_AZ_TBUF_OWN_INT_KER_EN, 1);
  1543. if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0)
  1544. EFX_SET_OWORD_FIELD(temp, FRF_CZ_SRAM_PERR_INT_P_KER_EN, 1);
  1545. EFX_INVERT_OWORD(temp);
  1546. efx_writeo(efx, &temp, FR_AZ_FATAL_INTR_KER);
  1547. /* Disable the ugly timer-based TX DMA backoff and allow TX DMA to be
  1548. * controlled by the RX FIFO fill level. Set arbitration to one pkt/Q.
  1549. */
  1550. efx_reado(efx, &temp, FR_AZ_TX_RESERVED);
  1551. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_RX_SPACER, 0xfe);
  1552. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_RX_SPACER_EN, 1);
  1553. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_ONE_PKT_PER_Q, 1);
  1554. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_PUSH_EN, 1);
  1555. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_DIS_NON_IP_EV, 1);
  1556. /* Enable SW_EV to inherit in char driver - assume harmless here */
  1557. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_SOFT_EVT_EN, 1);
  1558. /* Prefetch threshold 2 => fetch when descriptor cache half empty */
  1559. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_PREF_THRESHOLD, 2);
  1560. /* Disable hardware watchdog which can misfire */
  1561. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_PREF_WD_TMR, 0x3fffff);
  1562. /* Squash TX of packets of 16 bytes or less */
  1563. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0)
  1564. EFX_SET_OWORD_FIELD(temp, FRF_BZ_TX_FLUSH_MIN_LEN_EN, 1);
  1565. efx_writeo(efx, &temp, FR_AZ_TX_RESERVED);
  1566. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0) {
  1567. EFX_POPULATE_OWORD_4(temp,
  1568. /* Default values */
  1569. FRF_BZ_TX_PACE_SB_NOT_AF, 0x15,
  1570. FRF_BZ_TX_PACE_SB_AF, 0xb,
  1571. FRF_BZ_TX_PACE_FB_BASE, 0,
  1572. /* Allow large pace values in the
  1573. * fast bin. */
  1574. FRF_BZ_TX_PACE_BIN_TH,
  1575. FFE_BZ_TX_PACE_RESERVED);
  1576. efx_writeo(efx, &temp, FR_BZ_TX_PACE);
  1577. }
  1578. }
  1579. /**************************************************************************
  1580. *
  1581. * Filter tables
  1582. *
  1583. **************************************************************************
  1584. */
  1585. /* "Fudge factors" - difference between programmed value and actual depth.
  1586. * Due to pipelined implementation we need to program H/W with a value that
  1587. * is larger than the hop limit we want.
  1588. */
  1589. #define EFX_FARCH_FILTER_CTL_SRCH_FUDGE_WILD 3
  1590. #define EFX_FARCH_FILTER_CTL_SRCH_FUDGE_FULL 1
  1591. /* Hard maximum search limit. Hardware will time-out beyond 200-something.
  1592. * We also need to avoid infinite loops in efx_farch_filter_search() when the
  1593. * table is full.
  1594. */
  1595. #define EFX_FARCH_FILTER_CTL_SRCH_MAX 200
  1596. /* Don't try very hard to find space for performance hints, as this is
  1597. * counter-productive. */
  1598. #define EFX_FARCH_FILTER_CTL_SRCH_HINT_MAX 5
  1599. enum efx_farch_filter_type {
  1600. EFX_FARCH_FILTER_TCP_FULL = 0,
  1601. EFX_FARCH_FILTER_TCP_WILD,
  1602. EFX_FARCH_FILTER_UDP_FULL,
  1603. EFX_FARCH_FILTER_UDP_WILD,
  1604. EFX_FARCH_FILTER_MAC_FULL = 4,
  1605. EFX_FARCH_FILTER_MAC_WILD,
  1606. EFX_FARCH_FILTER_UC_DEF = 8,
  1607. EFX_FARCH_FILTER_MC_DEF,
  1608. EFX_FARCH_FILTER_TYPE_COUNT, /* number of specific types */
  1609. };
  1610. enum efx_farch_filter_table_id {
  1611. EFX_FARCH_FILTER_TABLE_RX_IP = 0,
  1612. EFX_FARCH_FILTER_TABLE_RX_MAC,
  1613. EFX_FARCH_FILTER_TABLE_RX_DEF,
  1614. EFX_FARCH_FILTER_TABLE_TX_MAC,
  1615. EFX_FARCH_FILTER_TABLE_COUNT,
  1616. };
  1617. enum efx_farch_filter_index {
  1618. EFX_FARCH_FILTER_INDEX_UC_DEF,
  1619. EFX_FARCH_FILTER_INDEX_MC_DEF,
  1620. EFX_FARCH_FILTER_SIZE_RX_DEF,
  1621. };
  1622. struct efx_farch_filter_spec {
  1623. u8 type:4;
  1624. u8 priority:4;
  1625. u8 flags;
  1626. u16 dmaq_id;
  1627. u32 data[3];
  1628. };
  1629. struct efx_farch_filter_table {
  1630. enum efx_farch_filter_table_id id;
  1631. u32 offset; /* address of table relative to BAR */
  1632. unsigned size; /* number of entries */
  1633. unsigned step; /* step between entries */
  1634. unsigned used; /* number currently used */
  1635. unsigned long *used_bitmap;
  1636. struct efx_farch_filter_spec *spec;
  1637. unsigned search_limit[EFX_FARCH_FILTER_TYPE_COUNT];
  1638. };
  1639. struct efx_farch_filter_state {
  1640. struct efx_farch_filter_table table[EFX_FARCH_FILTER_TABLE_COUNT];
  1641. };
  1642. static void
  1643. efx_farch_filter_table_clear_entry(struct efx_nic *efx,
  1644. struct efx_farch_filter_table *table,
  1645. unsigned int filter_idx);
  1646. /* The filter hash function is LFSR polynomial x^16 + x^3 + 1 of a 32-bit
  1647. * key derived from the n-tuple. The initial LFSR state is 0xffff. */
  1648. static u16 efx_farch_filter_hash(u32 key)
  1649. {
  1650. u16 tmp;
  1651. /* First 16 rounds */
  1652. tmp = 0x1fff ^ key >> 16;
  1653. tmp = tmp ^ tmp >> 3 ^ tmp >> 6;
  1654. tmp = tmp ^ tmp >> 9;
  1655. /* Last 16 rounds */
  1656. tmp = tmp ^ tmp << 13 ^ key;
  1657. tmp = tmp ^ tmp >> 3 ^ tmp >> 6;
  1658. return tmp ^ tmp >> 9;
  1659. }
  1660. /* To allow for hash collisions, filter search continues at these
  1661. * increments from the first possible entry selected by the hash. */
  1662. static u16 efx_farch_filter_increment(u32 key)
  1663. {
  1664. return key * 2 - 1;
  1665. }
  1666. static enum efx_farch_filter_table_id
  1667. efx_farch_filter_spec_table_id(const struct efx_farch_filter_spec *spec)
  1668. {
  1669. BUILD_BUG_ON(EFX_FARCH_FILTER_TABLE_RX_IP !=
  1670. (EFX_FARCH_FILTER_TCP_FULL >> 2));
  1671. BUILD_BUG_ON(EFX_FARCH_FILTER_TABLE_RX_IP !=
  1672. (EFX_FARCH_FILTER_TCP_WILD >> 2));
  1673. BUILD_BUG_ON(EFX_FARCH_FILTER_TABLE_RX_IP !=
  1674. (EFX_FARCH_FILTER_UDP_FULL >> 2));
  1675. BUILD_BUG_ON(EFX_FARCH_FILTER_TABLE_RX_IP !=
  1676. (EFX_FARCH_FILTER_UDP_WILD >> 2));
  1677. BUILD_BUG_ON(EFX_FARCH_FILTER_TABLE_RX_MAC !=
  1678. (EFX_FARCH_FILTER_MAC_FULL >> 2));
  1679. BUILD_BUG_ON(EFX_FARCH_FILTER_TABLE_RX_MAC !=
  1680. (EFX_FARCH_FILTER_MAC_WILD >> 2));
  1681. BUILD_BUG_ON(EFX_FARCH_FILTER_TABLE_TX_MAC !=
  1682. EFX_FARCH_FILTER_TABLE_RX_MAC + 2);
  1683. return (spec->type >> 2) + ((spec->flags & EFX_FILTER_FLAG_TX) ? 2 : 0);
  1684. }
  1685. static void efx_farch_filter_push_rx_config(struct efx_nic *efx)
  1686. {
  1687. struct efx_farch_filter_state *state = efx->filter_state;
  1688. struct efx_farch_filter_table *table;
  1689. efx_oword_t filter_ctl;
  1690. efx_reado(efx, &filter_ctl, FR_BZ_RX_FILTER_CTL);
  1691. table = &state->table[EFX_FARCH_FILTER_TABLE_RX_IP];
  1692. EFX_SET_OWORD_FIELD(filter_ctl, FRF_BZ_TCP_FULL_SRCH_LIMIT,
  1693. table->search_limit[EFX_FARCH_FILTER_TCP_FULL] +
  1694. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_FULL);
  1695. EFX_SET_OWORD_FIELD(filter_ctl, FRF_BZ_TCP_WILD_SRCH_LIMIT,
  1696. table->search_limit[EFX_FARCH_FILTER_TCP_WILD] +
  1697. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_WILD);
  1698. EFX_SET_OWORD_FIELD(filter_ctl, FRF_BZ_UDP_FULL_SRCH_LIMIT,
  1699. table->search_limit[EFX_FARCH_FILTER_UDP_FULL] +
  1700. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_FULL);
  1701. EFX_SET_OWORD_FIELD(filter_ctl, FRF_BZ_UDP_WILD_SRCH_LIMIT,
  1702. table->search_limit[EFX_FARCH_FILTER_UDP_WILD] +
  1703. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_WILD);
  1704. table = &state->table[EFX_FARCH_FILTER_TABLE_RX_MAC];
  1705. if (table->size) {
  1706. EFX_SET_OWORD_FIELD(
  1707. filter_ctl, FRF_CZ_ETHERNET_FULL_SEARCH_LIMIT,
  1708. table->search_limit[EFX_FARCH_FILTER_MAC_FULL] +
  1709. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_FULL);
  1710. EFX_SET_OWORD_FIELD(
  1711. filter_ctl, FRF_CZ_ETHERNET_WILDCARD_SEARCH_LIMIT,
  1712. table->search_limit[EFX_FARCH_FILTER_MAC_WILD] +
  1713. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_WILD);
  1714. }
  1715. table = &state->table[EFX_FARCH_FILTER_TABLE_RX_DEF];
  1716. if (table->size) {
  1717. EFX_SET_OWORD_FIELD(
  1718. filter_ctl, FRF_CZ_UNICAST_NOMATCH_Q_ID,
  1719. table->spec[EFX_FARCH_FILTER_INDEX_UC_DEF].dmaq_id);
  1720. EFX_SET_OWORD_FIELD(
  1721. filter_ctl, FRF_CZ_UNICAST_NOMATCH_RSS_ENABLED,
  1722. !!(table->spec[EFX_FARCH_FILTER_INDEX_UC_DEF].flags &
  1723. EFX_FILTER_FLAG_RX_RSS));
  1724. EFX_SET_OWORD_FIELD(
  1725. filter_ctl, FRF_CZ_MULTICAST_NOMATCH_Q_ID,
  1726. table->spec[EFX_FARCH_FILTER_INDEX_MC_DEF].dmaq_id);
  1727. EFX_SET_OWORD_FIELD(
  1728. filter_ctl, FRF_CZ_MULTICAST_NOMATCH_RSS_ENABLED,
  1729. !!(table->spec[EFX_FARCH_FILTER_INDEX_MC_DEF].flags &
  1730. EFX_FILTER_FLAG_RX_RSS));
  1731. /* There is a single bit to enable RX scatter for all
  1732. * unmatched packets. Only set it if scatter is
  1733. * enabled in both filter specs.
  1734. */
  1735. EFX_SET_OWORD_FIELD(
  1736. filter_ctl, FRF_BZ_SCATTER_ENBL_NO_MATCH_Q,
  1737. !!(table->spec[EFX_FARCH_FILTER_INDEX_UC_DEF].flags &
  1738. table->spec[EFX_FARCH_FILTER_INDEX_MC_DEF].flags &
  1739. EFX_FILTER_FLAG_RX_SCATTER));
  1740. } else if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0) {
  1741. /* We don't expose 'default' filters because unmatched
  1742. * packets always go to the queue number found in the
  1743. * RSS table. But we still need to set the RX scatter
  1744. * bit here.
  1745. */
  1746. EFX_SET_OWORD_FIELD(
  1747. filter_ctl, FRF_BZ_SCATTER_ENBL_NO_MATCH_Q,
  1748. efx->rx_scatter);
  1749. }
  1750. efx_writeo(efx, &filter_ctl, FR_BZ_RX_FILTER_CTL);
  1751. }
  1752. static void efx_farch_filter_push_tx_limits(struct efx_nic *efx)
  1753. {
  1754. struct efx_farch_filter_state *state = efx->filter_state;
  1755. struct efx_farch_filter_table *table;
  1756. efx_oword_t tx_cfg;
  1757. efx_reado(efx, &tx_cfg, FR_AZ_TX_CFG);
  1758. table = &state->table[EFX_FARCH_FILTER_TABLE_TX_MAC];
  1759. if (table->size) {
  1760. EFX_SET_OWORD_FIELD(
  1761. tx_cfg, FRF_CZ_TX_ETH_FILTER_FULL_SEARCH_RANGE,
  1762. table->search_limit[EFX_FARCH_FILTER_MAC_FULL] +
  1763. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_FULL);
  1764. EFX_SET_OWORD_FIELD(
  1765. tx_cfg, FRF_CZ_TX_ETH_FILTER_WILD_SEARCH_RANGE,
  1766. table->search_limit[EFX_FARCH_FILTER_MAC_WILD] +
  1767. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_WILD);
  1768. }
  1769. efx_writeo(efx, &tx_cfg, FR_AZ_TX_CFG);
  1770. }
  1771. static int
  1772. efx_farch_filter_from_gen_spec(struct efx_farch_filter_spec *spec,
  1773. const struct efx_filter_spec *gen_spec)
  1774. {
  1775. bool is_full = false;
  1776. if ((gen_spec->flags & EFX_FILTER_FLAG_RX_RSS) &&
  1777. gen_spec->rss_context != EFX_FILTER_RSS_CONTEXT_DEFAULT)
  1778. return -EINVAL;
  1779. spec->priority = gen_spec->priority;
  1780. spec->flags = gen_spec->flags;
  1781. spec->dmaq_id = gen_spec->dmaq_id;
  1782. switch (gen_spec->match_flags) {
  1783. case (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_IP_PROTO |
  1784. EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_LOC_PORT |
  1785. EFX_FILTER_MATCH_REM_HOST | EFX_FILTER_MATCH_REM_PORT):
  1786. is_full = true;
  1787. /* fall through */
  1788. case (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_IP_PROTO |
  1789. EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_LOC_PORT): {
  1790. __be32 rhost, host1, host2;
  1791. __be16 rport, port1, port2;
  1792. EFX_BUG_ON_PARANOID(!(gen_spec->flags & EFX_FILTER_FLAG_RX));
  1793. if (gen_spec->ether_type != htons(ETH_P_IP))
  1794. return -EPROTONOSUPPORT;
  1795. if (gen_spec->loc_port == 0 ||
  1796. (is_full && gen_spec->rem_port == 0))
  1797. return -EADDRNOTAVAIL;
  1798. switch (gen_spec->ip_proto) {
  1799. case IPPROTO_TCP:
  1800. spec->type = (is_full ? EFX_FARCH_FILTER_TCP_FULL :
  1801. EFX_FARCH_FILTER_TCP_WILD);
  1802. break;
  1803. case IPPROTO_UDP:
  1804. spec->type = (is_full ? EFX_FARCH_FILTER_UDP_FULL :
  1805. EFX_FARCH_FILTER_UDP_WILD);
  1806. break;
  1807. default:
  1808. return -EPROTONOSUPPORT;
  1809. }
  1810. /* Filter is constructed in terms of source and destination,
  1811. * with the odd wrinkle that the ports are swapped in a UDP
  1812. * wildcard filter. We need to convert from local and remote
  1813. * (= zero for wildcard) addresses.
  1814. */
  1815. rhost = is_full ? gen_spec->rem_host[0] : 0;
  1816. rport = is_full ? gen_spec->rem_port : 0;
  1817. host1 = rhost;
  1818. host2 = gen_spec->loc_host[0];
  1819. if (!is_full && gen_spec->ip_proto == IPPROTO_UDP) {
  1820. port1 = gen_spec->loc_port;
  1821. port2 = rport;
  1822. } else {
  1823. port1 = rport;
  1824. port2 = gen_spec->loc_port;
  1825. }
  1826. spec->data[0] = ntohl(host1) << 16 | ntohs(port1);
  1827. spec->data[1] = ntohs(port2) << 16 | ntohl(host1) >> 16;
  1828. spec->data[2] = ntohl(host2);
  1829. break;
  1830. }
  1831. case EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_OUTER_VID:
  1832. is_full = true;
  1833. /* fall through */
  1834. case EFX_FILTER_MATCH_LOC_MAC:
  1835. spec->type = (is_full ? EFX_FARCH_FILTER_MAC_FULL :
  1836. EFX_FARCH_FILTER_MAC_WILD);
  1837. spec->data[0] = is_full ? ntohs(gen_spec->outer_vid) : 0;
  1838. spec->data[1] = (gen_spec->loc_mac[2] << 24 |
  1839. gen_spec->loc_mac[3] << 16 |
  1840. gen_spec->loc_mac[4] << 8 |
  1841. gen_spec->loc_mac[5]);
  1842. spec->data[2] = (gen_spec->loc_mac[0] << 8 |
  1843. gen_spec->loc_mac[1]);
  1844. break;
  1845. case EFX_FILTER_MATCH_LOC_MAC_IG:
  1846. spec->type = (is_multicast_ether_addr(gen_spec->loc_mac) ?
  1847. EFX_FARCH_FILTER_MC_DEF :
  1848. EFX_FARCH_FILTER_UC_DEF);
  1849. memset(spec->data, 0, sizeof(spec->data)); /* ensure equality */
  1850. break;
  1851. default:
  1852. return -EPROTONOSUPPORT;
  1853. }
  1854. return 0;
  1855. }
  1856. static void
  1857. efx_farch_filter_to_gen_spec(struct efx_filter_spec *gen_spec,
  1858. const struct efx_farch_filter_spec *spec)
  1859. {
  1860. bool is_full = false;
  1861. /* *gen_spec should be completely initialised, to be consistent
  1862. * with efx_filter_init_{rx,tx}() and in case we want to copy
  1863. * it back to userland.
  1864. */
  1865. memset(gen_spec, 0, sizeof(*gen_spec));
  1866. gen_spec->priority = spec->priority;
  1867. gen_spec->flags = spec->flags;
  1868. gen_spec->dmaq_id = spec->dmaq_id;
  1869. switch (spec->type) {
  1870. case EFX_FARCH_FILTER_TCP_FULL:
  1871. case EFX_FARCH_FILTER_UDP_FULL:
  1872. is_full = true;
  1873. /* fall through */
  1874. case EFX_FARCH_FILTER_TCP_WILD:
  1875. case EFX_FARCH_FILTER_UDP_WILD: {
  1876. __be32 host1, host2;
  1877. __be16 port1, port2;
  1878. gen_spec->match_flags =
  1879. EFX_FILTER_MATCH_ETHER_TYPE |
  1880. EFX_FILTER_MATCH_IP_PROTO |
  1881. EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_LOC_PORT;
  1882. if (is_full)
  1883. gen_spec->match_flags |= (EFX_FILTER_MATCH_REM_HOST |
  1884. EFX_FILTER_MATCH_REM_PORT);
  1885. gen_spec->ether_type = htons(ETH_P_IP);
  1886. gen_spec->ip_proto =
  1887. (spec->type == EFX_FARCH_FILTER_TCP_FULL ||
  1888. spec->type == EFX_FARCH_FILTER_TCP_WILD) ?
  1889. IPPROTO_TCP : IPPROTO_UDP;
  1890. host1 = htonl(spec->data[0] >> 16 | spec->data[1] << 16);
  1891. port1 = htons(spec->data[0]);
  1892. host2 = htonl(spec->data[2]);
  1893. port2 = htons(spec->data[1] >> 16);
  1894. if (spec->flags & EFX_FILTER_FLAG_TX) {
  1895. gen_spec->loc_host[0] = host1;
  1896. gen_spec->rem_host[0] = host2;
  1897. } else {
  1898. gen_spec->loc_host[0] = host2;
  1899. gen_spec->rem_host[0] = host1;
  1900. }
  1901. if (!!(gen_spec->flags & EFX_FILTER_FLAG_TX) ^
  1902. (!is_full && gen_spec->ip_proto == IPPROTO_UDP)) {
  1903. gen_spec->loc_port = port1;
  1904. gen_spec->rem_port = port2;
  1905. } else {
  1906. gen_spec->loc_port = port2;
  1907. gen_spec->rem_port = port1;
  1908. }
  1909. break;
  1910. }
  1911. case EFX_FARCH_FILTER_MAC_FULL:
  1912. is_full = true;
  1913. /* fall through */
  1914. case EFX_FARCH_FILTER_MAC_WILD:
  1915. gen_spec->match_flags = EFX_FILTER_MATCH_LOC_MAC;
  1916. if (is_full)
  1917. gen_spec->match_flags |= EFX_FILTER_MATCH_OUTER_VID;
  1918. gen_spec->loc_mac[0] = spec->data[2] >> 8;
  1919. gen_spec->loc_mac[1] = spec->data[2];
  1920. gen_spec->loc_mac[2] = spec->data[1] >> 24;
  1921. gen_spec->loc_mac[3] = spec->data[1] >> 16;
  1922. gen_spec->loc_mac[4] = spec->data[1] >> 8;
  1923. gen_spec->loc_mac[5] = spec->data[1];
  1924. gen_spec->outer_vid = htons(spec->data[0]);
  1925. break;
  1926. case EFX_FARCH_FILTER_UC_DEF:
  1927. case EFX_FARCH_FILTER_MC_DEF:
  1928. gen_spec->match_flags = EFX_FILTER_MATCH_LOC_MAC_IG;
  1929. gen_spec->loc_mac[0] = spec->type == EFX_FARCH_FILTER_MC_DEF;
  1930. break;
  1931. default:
  1932. WARN_ON(1);
  1933. break;
  1934. }
  1935. }
  1936. static void
  1937. efx_farch_filter_init_rx_auto(struct efx_nic *efx,
  1938. struct efx_farch_filter_spec *spec)
  1939. {
  1940. /* If there's only one channel then disable RSS for non VF
  1941. * traffic, thereby allowing VFs to use RSS when the PF can't.
  1942. */
  1943. spec->priority = EFX_FILTER_PRI_AUTO;
  1944. spec->flags = (EFX_FILTER_FLAG_RX |
  1945. (efx->n_rx_channels > 1 ? EFX_FILTER_FLAG_RX_RSS : 0) |
  1946. (efx->rx_scatter ? EFX_FILTER_FLAG_RX_SCATTER : 0));
  1947. spec->dmaq_id = 0;
  1948. }
  1949. /* Build a filter entry and return its n-tuple key. */
  1950. static u32 efx_farch_filter_build(efx_oword_t *filter,
  1951. struct efx_farch_filter_spec *spec)
  1952. {
  1953. u32 data3;
  1954. switch (efx_farch_filter_spec_table_id(spec)) {
  1955. case EFX_FARCH_FILTER_TABLE_RX_IP: {
  1956. bool is_udp = (spec->type == EFX_FARCH_FILTER_UDP_FULL ||
  1957. spec->type == EFX_FARCH_FILTER_UDP_WILD);
  1958. EFX_POPULATE_OWORD_7(
  1959. *filter,
  1960. FRF_BZ_RSS_EN,
  1961. !!(spec->flags & EFX_FILTER_FLAG_RX_RSS),
  1962. FRF_BZ_SCATTER_EN,
  1963. !!(spec->flags & EFX_FILTER_FLAG_RX_SCATTER),
  1964. FRF_BZ_TCP_UDP, is_udp,
  1965. FRF_BZ_RXQ_ID, spec->dmaq_id,
  1966. EFX_DWORD_2, spec->data[2],
  1967. EFX_DWORD_1, spec->data[1],
  1968. EFX_DWORD_0, spec->data[0]);
  1969. data3 = is_udp;
  1970. break;
  1971. }
  1972. case EFX_FARCH_FILTER_TABLE_RX_MAC: {
  1973. bool is_wild = spec->type == EFX_FARCH_FILTER_MAC_WILD;
  1974. EFX_POPULATE_OWORD_7(
  1975. *filter,
  1976. FRF_CZ_RMFT_RSS_EN,
  1977. !!(spec->flags & EFX_FILTER_FLAG_RX_RSS),
  1978. FRF_CZ_RMFT_SCATTER_EN,
  1979. !!(spec->flags & EFX_FILTER_FLAG_RX_SCATTER),
  1980. FRF_CZ_RMFT_RXQ_ID, spec->dmaq_id,
  1981. FRF_CZ_RMFT_WILDCARD_MATCH, is_wild,
  1982. FRF_CZ_RMFT_DEST_MAC_HI, spec->data[2],
  1983. FRF_CZ_RMFT_DEST_MAC_LO, spec->data[1],
  1984. FRF_CZ_RMFT_VLAN_ID, spec->data[0]);
  1985. data3 = is_wild;
  1986. break;
  1987. }
  1988. case EFX_FARCH_FILTER_TABLE_TX_MAC: {
  1989. bool is_wild = spec->type == EFX_FARCH_FILTER_MAC_WILD;
  1990. EFX_POPULATE_OWORD_5(*filter,
  1991. FRF_CZ_TMFT_TXQ_ID, spec->dmaq_id,
  1992. FRF_CZ_TMFT_WILDCARD_MATCH, is_wild,
  1993. FRF_CZ_TMFT_SRC_MAC_HI, spec->data[2],
  1994. FRF_CZ_TMFT_SRC_MAC_LO, spec->data[1],
  1995. FRF_CZ_TMFT_VLAN_ID, spec->data[0]);
  1996. data3 = is_wild | spec->dmaq_id << 1;
  1997. break;
  1998. }
  1999. default:
  2000. BUG();
  2001. }
  2002. return spec->data[0] ^ spec->data[1] ^ spec->data[2] ^ data3;
  2003. }
  2004. static bool efx_farch_filter_equal(const struct efx_farch_filter_spec *left,
  2005. const struct efx_farch_filter_spec *right)
  2006. {
  2007. if (left->type != right->type ||
  2008. memcmp(left->data, right->data, sizeof(left->data)))
  2009. return false;
  2010. if (left->flags & EFX_FILTER_FLAG_TX &&
  2011. left->dmaq_id != right->dmaq_id)
  2012. return false;
  2013. return true;
  2014. }
  2015. /*
  2016. * Construct/deconstruct external filter IDs. At least the RX filter
  2017. * IDs must be ordered by matching priority, for RX NFC semantics.
  2018. *
  2019. * Deconstruction needs to be robust against invalid IDs so that
  2020. * efx_filter_remove_id_safe() and efx_filter_get_filter_safe() can
  2021. * accept user-provided IDs.
  2022. */
  2023. #define EFX_FARCH_FILTER_MATCH_PRI_COUNT 5
  2024. static const u8 efx_farch_filter_type_match_pri[EFX_FARCH_FILTER_TYPE_COUNT] = {
  2025. [EFX_FARCH_FILTER_TCP_FULL] = 0,
  2026. [EFX_FARCH_FILTER_UDP_FULL] = 0,
  2027. [EFX_FARCH_FILTER_TCP_WILD] = 1,
  2028. [EFX_FARCH_FILTER_UDP_WILD] = 1,
  2029. [EFX_FARCH_FILTER_MAC_FULL] = 2,
  2030. [EFX_FARCH_FILTER_MAC_WILD] = 3,
  2031. [EFX_FARCH_FILTER_UC_DEF] = 4,
  2032. [EFX_FARCH_FILTER_MC_DEF] = 4,
  2033. };
  2034. static const enum efx_farch_filter_table_id efx_farch_filter_range_table[] = {
  2035. EFX_FARCH_FILTER_TABLE_RX_IP, /* RX match pri 0 */
  2036. EFX_FARCH_FILTER_TABLE_RX_IP,
  2037. EFX_FARCH_FILTER_TABLE_RX_MAC,
  2038. EFX_FARCH_FILTER_TABLE_RX_MAC,
  2039. EFX_FARCH_FILTER_TABLE_RX_DEF, /* RX match pri 4 */
  2040. EFX_FARCH_FILTER_TABLE_TX_MAC, /* TX match pri 0 */
  2041. EFX_FARCH_FILTER_TABLE_TX_MAC, /* TX match pri 1 */
  2042. };
  2043. #define EFX_FARCH_FILTER_INDEX_WIDTH 13
  2044. #define EFX_FARCH_FILTER_INDEX_MASK ((1 << EFX_FARCH_FILTER_INDEX_WIDTH) - 1)
  2045. static inline u32
  2046. efx_farch_filter_make_id(const struct efx_farch_filter_spec *spec,
  2047. unsigned int index)
  2048. {
  2049. unsigned int range;
  2050. range = efx_farch_filter_type_match_pri[spec->type];
  2051. if (!(spec->flags & EFX_FILTER_FLAG_RX))
  2052. range += EFX_FARCH_FILTER_MATCH_PRI_COUNT;
  2053. return range << EFX_FARCH_FILTER_INDEX_WIDTH | index;
  2054. }
  2055. static inline enum efx_farch_filter_table_id
  2056. efx_farch_filter_id_table_id(u32 id)
  2057. {
  2058. unsigned int range = id >> EFX_FARCH_FILTER_INDEX_WIDTH;
  2059. if (range < ARRAY_SIZE(efx_farch_filter_range_table))
  2060. return efx_farch_filter_range_table[range];
  2061. else
  2062. return EFX_FARCH_FILTER_TABLE_COUNT; /* invalid */
  2063. }
  2064. static inline unsigned int efx_farch_filter_id_index(u32 id)
  2065. {
  2066. return id & EFX_FARCH_FILTER_INDEX_MASK;
  2067. }
  2068. u32 efx_farch_filter_get_rx_id_limit(struct efx_nic *efx)
  2069. {
  2070. struct efx_farch_filter_state *state = efx->filter_state;
  2071. unsigned int range = EFX_FARCH_FILTER_MATCH_PRI_COUNT - 1;
  2072. enum efx_farch_filter_table_id table_id;
  2073. do {
  2074. table_id = efx_farch_filter_range_table[range];
  2075. if (state->table[table_id].size != 0)
  2076. return range << EFX_FARCH_FILTER_INDEX_WIDTH |
  2077. state->table[table_id].size;
  2078. } while (range--);
  2079. return 0;
  2080. }
  2081. s32 efx_farch_filter_insert(struct efx_nic *efx,
  2082. struct efx_filter_spec *gen_spec,
  2083. bool replace_equal)
  2084. {
  2085. struct efx_farch_filter_state *state = efx->filter_state;
  2086. struct efx_farch_filter_table *table;
  2087. struct efx_farch_filter_spec spec;
  2088. efx_oword_t filter;
  2089. int rep_index, ins_index;
  2090. unsigned int depth = 0;
  2091. int rc;
  2092. rc = efx_farch_filter_from_gen_spec(&spec, gen_spec);
  2093. if (rc)
  2094. return rc;
  2095. table = &state->table[efx_farch_filter_spec_table_id(&spec)];
  2096. if (table->size == 0)
  2097. return -EINVAL;
  2098. netif_vdbg(efx, hw, efx->net_dev,
  2099. "%s: type %d search_limit=%d", __func__, spec.type,
  2100. table->search_limit[spec.type]);
  2101. if (table->id == EFX_FARCH_FILTER_TABLE_RX_DEF) {
  2102. /* One filter spec per type */
  2103. BUILD_BUG_ON(EFX_FARCH_FILTER_INDEX_UC_DEF != 0);
  2104. BUILD_BUG_ON(EFX_FARCH_FILTER_INDEX_MC_DEF !=
  2105. EFX_FARCH_FILTER_MC_DEF - EFX_FARCH_FILTER_UC_DEF);
  2106. rep_index = spec.type - EFX_FARCH_FILTER_UC_DEF;
  2107. ins_index = rep_index;
  2108. spin_lock_bh(&efx->filter_lock);
  2109. } else {
  2110. /* Search concurrently for
  2111. * (1) a filter to be replaced (rep_index): any filter
  2112. * with the same match values, up to the current
  2113. * search depth for this type, and
  2114. * (2) the insertion point (ins_index): (1) or any
  2115. * free slot before it or up to the maximum search
  2116. * depth for this priority
  2117. * We fail if we cannot find (2).
  2118. *
  2119. * We can stop once either
  2120. * (a) we find (1), in which case we have definitely
  2121. * found (2) as well; or
  2122. * (b) we have searched exhaustively for (1), and have
  2123. * either found (2) or searched exhaustively for it
  2124. */
  2125. u32 key = efx_farch_filter_build(&filter, &spec);
  2126. unsigned int hash = efx_farch_filter_hash(key);
  2127. unsigned int incr = efx_farch_filter_increment(key);
  2128. unsigned int max_rep_depth = table->search_limit[spec.type];
  2129. unsigned int max_ins_depth =
  2130. spec.priority <= EFX_FILTER_PRI_HINT ?
  2131. EFX_FARCH_FILTER_CTL_SRCH_HINT_MAX :
  2132. EFX_FARCH_FILTER_CTL_SRCH_MAX;
  2133. unsigned int i = hash & (table->size - 1);
  2134. ins_index = -1;
  2135. depth = 1;
  2136. spin_lock_bh(&efx->filter_lock);
  2137. for (;;) {
  2138. if (!test_bit(i, table->used_bitmap)) {
  2139. if (ins_index < 0)
  2140. ins_index = i;
  2141. } else if (efx_farch_filter_equal(&spec,
  2142. &table->spec[i])) {
  2143. /* Case (a) */
  2144. if (ins_index < 0)
  2145. ins_index = i;
  2146. rep_index = i;
  2147. break;
  2148. }
  2149. if (depth >= max_rep_depth &&
  2150. (ins_index >= 0 || depth >= max_ins_depth)) {
  2151. /* Case (b) */
  2152. if (ins_index < 0) {
  2153. rc = -EBUSY;
  2154. goto out;
  2155. }
  2156. rep_index = -1;
  2157. break;
  2158. }
  2159. i = (i + incr) & (table->size - 1);
  2160. ++depth;
  2161. }
  2162. }
  2163. /* If we found a filter to be replaced, check whether we
  2164. * should do so
  2165. */
  2166. if (rep_index >= 0) {
  2167. struct efx_farch_filter_spec *saved_spec =
  2168. &table->spec[rep_index];
  2169. if (spec.priority == saved_spec->priority && !replace_equal) {
  2170. rc = -EEXIST;
  2171. goto out;
  2172. }
  2173. if (spec.priority < saved_spec->priority) {
  2174. rc = -EPERM;
  2175. goto out;
  2176. }
  2177. if (saved_spec->priority == EFX_FILTER_PRI_AUTO ||
  2178. saved_spec->flags & EFX_FILTER_FLAG_RX_OVER_AUTO)
  2179. spec.flags |= EFX_FILTER_FLAG_RX_OVER_AUTO;
  2180. }
  2181. /* Insert the filter */
  2182. if (ins_index != rep_index) {
  2183. __set_bit(ins_index, table->used_bitmap);
  2184. ++table->used;
  2185. }
  2186. table->spec[ins_index] = spec;
  2187. if (table->id == EFX_FARCH_FILTER_TABLE_RX_DEF) {
  2188. efx_farch_filter_push_rx_config(efx);
  2189. } else {
  2190. if (table->search_limit[spec.type] < depth) {
  2191. table->search_limit[spec.type] = depth;
  2192. if (spec.flags & EFX_FILTER_FLAG_TX)
  2193. efx_farch_filter_push_tx_limits(efx);
  2194. else
  2195. efx_farch_filter_push_rx_config(efx);
  2196. }
  2197. efx_writeo(efx, &filter,
  2198. table->offset + table->step * ins_index);
  2199. /* If we were able to replace a filter by inserting
  2200. * at a lower depth, clear the replaced filter
  2201. */
  2202. if (ins_index != rep_index && rep_index >= 0)
  2203. efx_farch_filter_table_clear_entry(efx, table,
  2204. rep_index);
  2205. }
  2206. netif_vdbg(efx, hw, efx->net_dev,
  2207. "%s: filter type %d index %d rxq %u set",
  2208. __func__, spec.type, ins_index, spec.dmaq_id);
  2209. rc = efx_farch_filter_make_id(&spec, ins_index);
  2210. out:
  2211. spin_unlock_bh(&efx->filter_lock);
  2212. return rc;
  2213. }
  2214. static void
  2215. efx_farch_filter_table_clear_entry(struct efx_nic *efx,
  2216. struct efx_farch_filter_table *table,
  2217. unsigned int filter_idx)
  2218. {
  2219. static efx_oword_t filter;
  2220. EFX_WARN_ON_PARANOID(!test_bit(filter_idx, table->used_bitmap));
  2221. BUG_ON(table->offset == 0); /* can't clear MAC default filters */
  2222. __clear_bit(filter_idx, table->used_bitmap);
  2223. --table->used;
  2224. memset(&table->spec[filter_idx], 0, sizeof(table->spec[0]));
  2225. efx_writeo(efx, &filter, table->offset + table->step * filter_idx);
  2226. /* If this filter required a greater search depth than
  2227. * any other, the search limit for its type can now be
  2228. * decreased. However, it is hard to determine that
  2229. * unless the table has become completely empty - in
  2230. * which case, all its search limits can be set to 0.
  2231. */
  2232. if (unlikely(table->used == 0)) {
  2233. memset(table->search_limit, 0, sizeof(table->search_limit));
  2234. if (table->id == EFX_FARCH_FILTER_TABLE_TX_MAC)
  2235. efx_farch_filter_push_tx_limits(efx);
  2236. else
  2237. efx_farch_filter_push_rx_config(efx);
  2238. }
  2239. }
  2240. static int efx_farch_filter_remove(struct efx_nic *efx,
  2241. struct efx_farch_filter_table *table,
  2242. unsigned int filter_idx,
  2243. enum efx_filter_priority priority)
  2244. {
  2245. struct efx_farch_filter_spec *spec = &table->spec[filter_idx];
  2246. if (!test_bit(filter_idx, table->used_bitmap) ||
  2247. spec->priority != priority)
  2248. return -ENOENT;
  2249. if (spec->flags & EFX_FILTER_FLAG_RX_OVER_AUTO) {
  2250. efx_farch_filter_init_rx_auto(efx, spec);
  2251. efx_farch_filter_push_rx_config(efx);
  2252. } else {
  2253. efx_farch_filter_table_clear_entry(efx, table, filter_idx);
  2254. }
  2255. return 0;
  2256. }
  2257. int efx_farch_filter_remove_safe(struct efx_nic *efx,
  2258. enum efx_filter_priority priority,
  2259. u32 filter_id)
  2260. {
  2261. struct efx_farch_filter_state *state = efx->filter_state;
  2262. enum efx_farch_filter_table_id table_id;
  2263. struct efx_farch_filter_table *table;
  2264. unsigned int filter_idx;
  2265. struct efx_farch_filter_spec *spec;
  2266. int rc;
  2267. table_id = efx_farch_filter_id_table_id(filter_id);
  2268. if ((unsigned int)table_id >= EFX_FARCH_FILTER_TABLE_COUNT)
  2269. return -ENOENT;
  2270. table = &state->table[table_id];
  2271. filter_idx = efx_farch_filter_id_index(filter_id);
  2272. if (filter_idx >= table->size)
  2273. return -ENOENT;
  2274. spec = &table->spec[filter_idx];
  2275. spin_lock_bh(&efx->filter_lock);
  2276. rc = efx_farch_filter_remove(efx, table, filter_idx, priority);
  2277. spin_unlock_bh(&efx->filter_lock);
  2278. return rc;
  2279. }
  2280. int efx_farch_filter_get_safe(struct efx_nic *efx,
  2281. enum efx_filter_priority priority,
  2282. u32 filter_id, struct efx_filter_spec *spec_buf)
  2283. {
  2284. struct efx_farch_filter_state *state = efx->filter_state;
  2285. enum efx_farch_filter_table_id table_id;
  2286. struct efx_farch_filter_table *table;
  2287. struct efx_farch_filter_spec *spec;
  2288. unsigned int filter_idx;
  2289. int rc;
  2290. table_id = efx_farch_filter_id_table_id(filter_id);
  2291. if ((unsigned int)table_id >= EFX_FARCH_FILTER_TABLE_COUNT)
  2292. return -ENOENT;
  2293. table = &state->table[table_id];
  2294. filter_idx = efx_farch_filter_id_index(filter_id);
  2295. if (filter_idx >= table->size)
  2296. return -ENOENT;
  2297. spec = &table->spec[filter_idx];
  2298. spin_lock_bh(&efx->filter_lock);
  2299. if (test_bit(filter_idx, table->used_bitmap) &&
  2300. spec->priority == priority) {
  2301. efx_farch_filter_to_gen_spec(spec_buf, spec);
  2302. rc = 0;
  2303. } else {
  2304. rc = -ENOENT;
  2305. }
  2306. spin_unlock_bh(&efx->filter_lock);
  2307. return rc;
  2308. }
  2309. static void
  2310. efx_farch_filter_table_clear(struct efx_nic *efx,
  2311. enum efx_farch_filter_table_id table_id,
  2312. enum efx_filter_priority priority)
  2313. {
  2314. struct efx_farch_filter_state *state = efx->filter_state;
  2315. struct efx_farch_filter_table *table = &state->table[table_id];
  2316. unsigned int filter_idx;
  2317. spin_lock_bh(&efx->filter_lock);
  2318. for (filter_idx = 0; filter_idx < table->size; ++filter_idx) {
  2319. if (table->spec[filter_idx].priority != EFX_FILTER_PRI_AUTO)
  2320. efx_farch_filter_remove(efx, table,
  2321. filter_idx, priority);
  2322. }
  2323. spin_unlock_bh(&efx->filter_lock);
  2324. }
  2325. int efx_farch_filter_clear_rx(struct efx_nic *efx,
  2326. enum efx_filter_priority priority)
  2327. {
  2328. efx_farch_filter_table_clear(efx, EFX_FARCH_FILTER_TABLE_RX_IP,
  2329. priority);
  2330. efx_farch_filter_table_clear(efx, EFX_FARCH_FILTER_TABLE_RX_MAC,
  2331. priority);
  2332. efx_farch_filter_table_clear(efx, EFX_FARCH_FILTER_TABLE_RX_DEF,
  2333. priority);
  2334. return 0;
  2335. }
  2336. u32 efx_farch_filter_count_rx_used(struct efx_nic *efx,
  2337. enum efx_filter_priority priority)
  2338. {
  2339. struct efx_farch_filter_state *state = efx->filter_state;
  2340. enum efx_farch_filter_table_id table_id;
  2341. struct efx_farch_filter_table *table;
  2342. unsigned int filter_idx;
  2343. u32 count = 0;
  2344. spin_lock_bh(&efx->filter_lock);
  2345. for (table_id = EFX_FARCH_FILTER_TABLE_RX_IP;
  2346. table_id <= EFX_FARCH_FILTER_TABLE_RX_DEF;
  2347. table_id++) {
  2348. table = &state->table[table_id];
  2349. for (filter_idx = 0; filter_idx < table->size; filter_idx++) {
  2350. if (test_bit(filter_idx, table->used_bitmap) &&
  2351. table->spec[filter_idx].priority == priority)
  2352. ++count;
  2353. }
  2354. }
  2355. spin_unlock_bh(&efx->filter_lock);
  2356. return count;
  2357. }
  2358. s32 efx_farch_filter_get_rx_ids(struct efx_nic *efx,
  2359. enum efx_filter_priority priority,
  2360. u32 *buf, u32 size)
  2361. {
  2362. struct efx_farch_filter_state *state = efx->filter_state;
  2363. enum efx_farch_filter_table_id table_id;
  2364. struct efx_farch_filter_table *table;
  2365. unsigned int filter_idx;
  2366. s32 count = 0;
  2367. spin_lock_bh(&efx->filter_lock);
  2368. for (table_id = EFX_FARCH_FILTER_TABLE_RX_IP;
  2369. table_id <= EFX_FARCH_FILTER_TABLE_RX_DEF;
  2370. table_id++) {
  2371. table = &state->table[table_id];
  2372. for (filter_idx = 0; filter_idx < table->size; filter_idx++) {
  2373. if (test_bit(filter_idx, table->used_bitmap) &&
  2374. table->spec[filter_idx].priority == priority) {
  2375. if (count == size) {
  2376. count = -EMSGSIZE;
  2377. goto out;
  2378. }
  2379. buf[count++] = efx_farch_filter_make_id(
  2380. &table->spec[filter_idx], filter_idx);
  2381. }
  2382. }
  2383. }
  2384. out:
  2385. spin_unlock_bh(&efx->filter_lock);
  2386. return count;
  2387. }
  2388. /* Restore filter stater after reset */
  2389. void efx_farch_filter_table_restore(struct efx_nic *efx)
  2390. {
  2391. struct efx_farch_filter_state *state = efx->filter_state;
  2392. enum efx_farch_filter_table_id table_id;
  2393. struct efx_farch_filter_table *table;
  2394. efx_oword_t filter;
  2395. unsigned int filter_idx;
  2396. spin_lock_bh(&efx->filter_lock);
  2397. for (table_id = 0; table_id < EFX_FARCH_FILTER_TABLE_COUNT; table_id++) {
  2398. table = &state->table[table_id];
  2399. /* Check whether this is a regular register table */
  2400. if (table->step == 0)
  2401. continue;
  2402. for (filter_idx = 0; filter_idx < table->size; filter_idx++) {
  2403. if (!test_bit(filter_idx, table->used_bitmap))
  2404. continue;
  2405. efx_farch_filter_build(&filter, &table->spec[filter_idx]);
  2406. efx_writeo(efx, &filter,
  2407. table->offset + table->step * filter_idx);
  2408. }
  2409. }
  2410. efx_farch_filter_push_rx_config(efx);
  2411. efx_farch_filter_push_tx_limits(efx);
  2412. spin_unlock_bh(&efx->filter_lock);
  2413. }
  2414. void efx_farch_filter_table_remove(struct efx_nic *efx)
  2415. {
  2416. struct efx_farch_filter_state *state = efx->filter_state;
  2417. enum efx_farch_filter_table_id table_id;
  2418. for (table_id = 0; table_id < EFX_FARCH_FILTER_TABLE_COUNT; table_id++) {
  2419. kfree(state->table[table_id].used_bitmap);
  2420. vfree(state->table[table_id].spec);
  2421. }
  2422. kfree(state);
  2423. }
  2424. int efx_farch_filter_table_probe(struct efx_nic *efx)
  2425. {
  2426. struct efx_farch_filter_state *state;
  2427. struct efx_farch_filter_table *table;
  2428. unsigned table_id;
  2429. state = kzalloc(sizeof(struct efx_farch_filter_state), GFP_KERNEL);
  2430. if (!state)
  2431. return -ENOMEM;
  2432. efx->filter_state = state;
  2433. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0) {
  2434. table = &state->table[EFX_FARCH_FILTER_TABLE_RX_IP];
  2435. table->id = EFX_FARCH_FILTER_TABLE_RX_IP;
  2436. table->offset = FR_BZ_RX_FILTER_TBL0;
  2437. table->size = FR_BZ_RX_FILTER_TBL0_ROWS;
  2438. table->step = FR_BZ_RX_FILTER_TBL0_STEP;
  2439. }
  2440. if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
  2441. table = &state->table[EFX_FARCH_FILTER_TABLE_RX_MAC];
  2442. table->id = EFX_FARCH_FILTER_TABLE_RX_MAC;
  2443. table->offset = FR_CZ_RX_MAC_FILTER_TBL0;
  2444. table->size = FR_CZ_RX_MAC_FILTER_TBL0_ROWS;
  2445. table->step = FR_CZ_RX_MAC_FILTER_TBL0_STEP;
  2446. table = &state->table[EFX_FARCH_FILTER_TABLE_RX_DEF];
  2447. table->id = EFX_FARCH_FILTER_TABLE_RX_DEF;
  2448. table->size = EFX_FARCH_FILTER_SIZE_RX_DEF;
  2449. table = &state->table[EFX_FARCH_FILTER_TABLE_TX_MAC];
  2450. table->id = EFX_FARCH_FILTER_TABLE_TX_MAC;
  2451. table->offset = FR_CZ_TX_MAC_FILTER_TBL0;
  2452. table->size = FR_CZ_TX_MAC_FILTER_TBL0_ROWS;
  2453. table->step = FR_CZ_TX_MAC_FILTER_TBL0_STEP;
  2454. }
  2455. for (table_id = 0; table_id < EFX_FARCH_FILTER_TABLE_COUNT; table_id++) {
  2456. table = &state->table[table_id];
  2457. if (table->size == 0)
  2458. continue;
  2459. table->used_bitmap = kcalloc(BITS_TO_LONGS(table->size),
  2460. sizeof(unsigned long),
  2461. GFP_KERNEL);
  2462. if (!table->used_bitmap)
  2463. goto fail;
  2464. table->spec = vzalloc(table->size * sizeof(*table->spec));
  2465. if (!table->spec)
  2466. goto fail;
  2467. }
  2468. table = &state->table[EFX_FARCH_FILTER_TABLE_RX_DEF];
  2469. if (table->size) {
  2470. /* RX default filters must always exist */
  2471. struct efx_farch_filter_spec *spec;
  2472. unsigned i;
  2473. for (i = 0; i < EFX_FARCH_FILTER_SIZE_RX_DEF; i++) {
  2474. spec = &table->spec[i];
  2475. spec->type = EFX_FARCH_FILTER_UC_DEF + i;
  2476. efx_farch_filter_init_rx_auto(efx, spec);
  2477. __set_bit(i, table->used_bitmap);
  2478. }
  2479. }
  2480. efx_farch_filter_push_rx_config(efx);
  2481. return 0;
  2482. fail:
  2483. efx_farch_filter_table_remove(efx);
  2484. return -ENOMEM;
  2485. }
  2486. /* Update scatter enable flags for filters pointing to our own RX queues */
  2487. void efx_farch_filter_update_rx_scatter(struct efx_nic *efx)
  2488. {
  2489. struct efx_farch_filter_state *state = efx->filter_state;
  2490. enum efx_farch_filter_table_id table_id;
  2491. struct efx_farch_filter_table *table;
  2492. efx_oword_t filter;
  2493. unsigned int filter_idx;
  2494. spin_lock_bh(&efx->filter_lock);
  2495. for (table_id = EFX_FARCH_FILTER_TABLE_RX_IP;
  2496. table_id <= EFX_FARCH_FILTER_TABLE_RX_DEF;
  2497. table_id++) {
  2498. table = &state->table[table_id];
  2499. for (filter_idx = 0; filter_idx < table->size; filter_idx++) {
  2500. if (!test_bit(filter_idx, table->used_bitmap) ||
  2501. table->spec[filter_idx].dmaq_id >=
  2502. efx->n_rx_channels)
  2503. continue;
  2504. if (efx->rx_scatter)
  2505. table->spec[filter_idx].flags |=
  2506. EFX_FILTER_FLAG_RX_SCATTER;
  2507. else
  2508. table->spec[filter_idx].flags &=
  2509. ~EFX_FILTER_FLAG_RX_SCATTER;
  2510. if (table_id == EFX_FARCH_FILTER_TABLE_RX_DEF)
  2511. /* Pushed by efx_farch_filter_push_rx_config() */
  2512. continue;
  2513. efx_farch_filter_build(&filter, &table->spec[filter_idx]);
  2514. efx_writeo(efx, &filter,
  2515. table->offset + table->step * filter_idx);
  2516. }
  2517. }
  2518. efx_farch_filter_push_rx_config(efx);
  2519. spin_unlock_bh(&efx->filter_lock);
  2520. }
  2521. #ifdef CONFIG_RFS_ACCEL
  2522. s32 efx_farch_filter_rfs_insert(struct efx_nic *efx,
  2523. struct efx_filter_spec *gen_spec)
  2524. {
  2525. return efx_farch_filter_insert(efx, gen_spec, true);
  2526. }
  2527. bool efx_farch_filter_rfs_expire_one(struct efx_nic *efx, u32 flow_id,
  2528. unsigned int index)
  2529. {
  2530. struct efx_farch_filter_state *state = efx->filter_state;
  2531. struct efx_farch_filter_table *table =
  2532. &state->table[EFX_FARCH_FILTER_TABLE_RX_IP];
  2533. if (test_bit(index, table->used_bitmap) &&
  2534. table->spec[index].priority == EFX_FILTER_PRI_HINT &&
  2535. rps_may_expire_flow(efx->net_dev, table->spec[index].dmaq_id,
  2536. flow_id, index)) {
  2537. efx_farch_filter_table_clear_entry(efx, table, index);
  2538. return true;
  2539. }
  2540. return false;
  2541. }
  2542. #endif /* CONFIG_RFS_ACCEL */
  2543. void efx_farch_filter_sync_rx_mode(struct efx_nic *efx)
  2544. {
  2545. struct net_device *net_dev = efx->net_dev;
  2546. struct netdev_hw_addr *ha;
  2547. union efx_multicast_hash *mc_hash = &efx->multicast_hash;
  2548. u32 crc;
  2549. int bit;
  2550. if (!efx_dev_registered(efx))
  2551. return;
  2552. netif_addr_lock_bh(net_dev);
  2553. efx->unicast_filter = !(net_dev->flags & IFF_PROMISC);
  2554. /* Build multicast hash table */
  2555. if (net_dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
  2556. memset(mc_hash, 0xff, sizeof(*mc_hash));
  2557. } else {
  2558. memset(mc_hash, 0x00, sizeof(*mc_hash));
  2559. netdev_for_each_mc_addr(ha, net_dev) {
  2560. crc = ether_crc_le(ETH_ALEN, ha->addr);
  2561. bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
  2562. __set_bit_le(bit, mc_hash);
  2563. }
  2564. /* Broadcast packets go through the multicast hash filter.
  2565. * ether_crc_le() of the broadcast address is 0xbe2612ff
  2566. * so we always add bit 0xff to the mask.
  2567. */
  2568. __set_bit_le(0xff, mc_hash);
  2569. }
  2570. netif_addr_unlock_bh(net_dev);
  2571. }