farch.c 88 KB

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