farch.c 88 KB

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