farch.c 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975
  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 int
  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. int tx_packets = 0;
  703. if (unlikely(ACCESS_ONCE(efx->reset_pending)))
  704. return 0;
  705. if (likely(EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_COMP))) {
  706. /* Transmit completion */
  707. tx_ev_desc_ptr = EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_DESC_PTR);
  708. tx_ev_q_label = EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_Q_LABEL);
  709. tx_queue = efx_channel_get_tx_queue(
  710. channel, tx_ev_q_label % EFX_TXQ_TYPES);
  711. tx_packets = ((tx_ev_desc_ptr - tx_queue->read_count) &
  712. tx_queue->ptr_mask);
  713. efx_xmit_done(tx_queue, tx_ev_desc_ptr);
  714. } else if (EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_WQ_FF_FULL)) {
  715. /* Rewrite the FIFO write pointer */
  716. tx_ev_q_label = EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_Q_LABEL);
  717. tx_queue = efx_channel_get_tx_queue(
  718. channel, tx_ev_q_label % EFX_TXQ_TYPES);
  719. netif_tx_lock(efx->net_dev);
  720. efx_farch_notify_tx_desc(tx_queue);
  721. netif_tx_unlock(efx->net_dev);
  722. } else if (EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_PKT_ERR)) {
  723. efx_schedule_reset(efx, RESET_TYPE_DMA_ERROR);
  724. } else {
  725. netif_err(efx, tx_err, efx->net_dev,
  726. "channel %d unexpected TX event "
  727. EFX_QWORD_FMT"\n", channel->channel,
  728. EFX_QWORD_VAL(*event));
  729. }
  730. return tx_packets;
  731. }
  732. /* Detect errors included in the rx_evt_pkt_ok bit. */
  733. static u16 efx_farch_handle_rx_not_ok(struct efx_rx_queue *rx_queue,
  734. const efx_qword_t *event)
  735. {
  736. struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
  737. struct efx_nic *efx = rx_queue->efx;
  738. bool rx_ev_buf_owner_id_err, rx_ev_ip_hdr_chksum_err;
  739. bool rx_ev_tcp_udp_chksum_err, rx_ev_eth_crc_err;
  740. bool rx_ev_frm_trunc, rx_ev_tobe_disc;
  741. bool rx_ev_other_err, rx_ev_pause_frm;
  742. bool rx_ev_hdr_type, rx_ev_mcast_pkt;
  743. unsigned rx_ev_pkt_type;
  744. rx_ev_hdr_type = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_HDR_TYPE);
  745. rx_ev_mcast_pkt = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_MCAST_PKT);
  746. rx_ev_tobe_disc = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_TOBE_DISC);
  747. rx_ev_pkt_type = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_PKT_TYPE);
  748. rx_ev_buf_owner_id_err = EFX_QWORD_FIELD(*event,
  749. FSF_AZ_RX_EV_BUF_OWNER_ID_ERR);
  750. rx_ev_ip_hdr_chksum_err = EFX_QWORD_FIELD(*event,
  751. FSF_AZ_RX_EV_IP_HDR_CHKSUM_ERR);
  752. rx_ev_tcp_udp_chksum_err = EFX_QWORD_FIELD(*event,
  753. FSF_AZ_RX_EV_TCP_UDP_CHKSUM_ERR);
  754. rx_ev_eth_crc_err = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_ETH_CRC_ERR);
  755. rx_ev_frm_trunc = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_FRM_TRUNC);
  756. rx_ev_pause_frm = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_PAUSE_FRM_ERR);
  757. /* Every error apart from tobe_disc and pause_frm */
  758. rx_ev_other_err = (rx_ev_tcp_udp_chksum_err |
  759. rx_ev_buf_owner_id_err | rx_ev_eth_crc_err |
  760. rx_ev_frm_trunc | rx_ev_ip_hdr_chksum_err);
  761. /* Count errors that are not in MAC stats. Ignore expected
  762. * checksum errors during self-test. */
  763. if (rx_ev_frm_trunc)
  764. ++channel->n_rx_frm_trunc;
  765. else if (rx_ev_tobe_disc)
  766. ++channel->n_rx_tobe_disc;
  767. else if (!efx->loopback_selftest) {
  768. if (rx_ev_ip_hdr_chksum_err)
  769. ++channel->n_rx_ip_hdr_chksum_err;
  770. else if (rx_ev_tcp_udp_chksum_err)
  771. ++channel->n_rx_tcp_udp_chksum_err;
  772. }
  773. /* TOBE_DISC is expected on unicast mismatches; don't print out an
  774. * error message. FRM_TRUNC indicates RXDP dropped the packet due
  775. * to a FIFO overflow.
  776. */
  777. #ifdef DEBUG
  778. if (rx_ev_other_err && net_ratelimit()) {
  779. netif_dbg(efx, rx_err, efx->net_dev,
  780. " RX queue %d unexpected RX event "
  781. EFX_QWORD_FMT "%s%s%s%s%s%s%s\n",
  782. efx_rx_queue_index(rx_queue), EFX_QWORD_VAL(*event),
  783. rx_ev_buf_owner_id_err ? " [OWNER_ID_ERR]" : "",
  784. rx_ev_ip_hdr_chksum_err ?
  785. " [IP_HDR_CHKSUM_ERR]" : "",
  786. rx_ev_tcp_udp_chksum_err ?
  787. " [TCP_UDP_CHKSUM_ERR]" : "",
  788. rx_ev_eth_crc_err ? " [ETH_CRC_ERR]" : "",
  789. rx_ev_frm_trunc ? " [FRM_TRUNC]" : "",
  790. rx_ev_tobe_disc ? " [TOBE_DISC]" : "",
  791. rx_ev_pause_frm ? " [PAUSE]" : "");
  792. }
  793. #endif
  794. if (efx->net_dev->features & NETIF_F_RXALL)
  795. /* don't discard frame for CRC error */
  796. rx_ev_eth_crc_err = false;
  797. /* The frame must be discarded if any of these are true. */
  798. return (rx_ev_eth_crc_err | rx_ev_frm_trunc |
  799. rx_ev_tobe_disc | rx_ev_pause_frm) ?
  800. EFX_RX_PKT_DISCARD : 0;
  801. }
  802. /* Handle receive events that are not in-order. Return true if this
  803. * can be handled as a partial packet discard, false if it's more
  804. * serious.
  805. */
  806. static bool
  807. efx_farch_handle_rx_bad_index(struct efx_rx_queue *rx_queue, unsigned index)
  808. {
  809. struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
  810. struct efx_nic *efx = rx_queue->efx;
  811. unsigned expected, dropped;
  812. if (rx_queue->scatter_n &&
  813. index == ((rx_queue->removed_count + rx_queue->scatter_n - 1) &
  814. rx_queue->ptr_mask)) {
  815. ++channel->n_rx_nodesc_trunc;
  816. return true;
  817. }
  818. expected = rx_queue->removed_count & rx_queue->ptr_mask;
  819. dropped = (index - expected) & rx_queue->ptr_mask;
  820. netif_info(efx, rx_err, efx->net_dev,
  821. "dropped %d events (index=%d expected=%d)\n",
  822. dropped, index, expected);
  823. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  824. return false;
  825. }
  826. /* Handle a packet received event
  827. *
  828. * The NIC gives a "discard" flag if it's a unicast packet with the
  829. * wrong destination address
  830. * Also "is multicast" and "matches multicast filter" flags can be used to
  831. * discard non-matching multicast packets.
  832. */
  833. static void
  834. efx_farch_handle_rx_event(struct efx_channel *channel, const efx_qword_t *event)
  835. {
  836. unsigned int rx_ev_desc_ptr, rx_ev_byte_cnt;
  837. unsigned int rx_ev_hdr_type, rx_ev_mcast_pkt;
  838. unsigned expected_ptr;
  839. bool rx_ev_pkt_ok, rx_ev_sop, rx_ev_cont;
  840. u16 flags;
  841. struct efx_rx_queue *rx_queue;
  842. struct efx_nic *efx = channel->efx;
  843. if (unlikely(ACCESS_ONCE(efx->reset_pending)))
  844. return;
  845. rx_ev_cont = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_JUMBO_CONT);
  846. rx_ev_sop = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_SOP);
  847. WARN_ON(EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_Q_LABEL) !=
  848. channel->channel);
  849. rx_queue = efx_channel_get_rx_queue(channel);
  850. rx_ev_desc_ptr = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_DESC_PTR);
  851. expected_ptr = ((rx_queue->removed_count + rx_queue->scatter_n) &
  852. rx_queue->ptr_mask);
  853. /* Check for partial drops and other errors */
  854. if (unlikely(rx_ev_desc_ptr != expected_ptr) ||
  855. unlikely(rx_ev_sop != (rx_queue->scatter_n == 0))) {
  856. if (rx_ev_desc_ptr != expected_ptr &&
  857. !efx_farch_handle_rx_bad_index(rx_queue, rx_ev_desc_ptr))
  858. return;
  859. /* Discard all pending fragments */
  860. if (rx_queue->scatter_n) {
  861. efx_rx_packet(
  862. rx_queue,
  863. rx_queue->removed_count & rx_queue->ptr_mask,
  864. rx_queue->scatter_n, 0, EFX_RX_PKT_DISCARD);
  865. rx_queue->removed_count += rx_queue->scatter_n;
  866. rx_queue->scatter_n = 0;
  867. }
  868. /* Return if there is no new fragment */
  869. if (rx_ev_desc_ptr != expected_ptr)
  870. return;
  871. /* Discard new fragment if not SOP */
  872. if (!rx_ev_sop) {
  873. efx_rx_packet(
  874. rx_queue,
  875. rx_queue->removed_count & rx_queue->ptr_mask,
  876. 1, 0, EFX_RX_PKT_DISCARD);
  877. ++rx_queue->removed_count;
  878. return;
  879. }
  880. }
  881. ++rx_queue->scatter_n;
  882. if (rx_ev_cont)
  883. return;
  884. rx_ev_byte_cnt = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_BYTE_CNT);
  885. rx_ev_pkt_ok = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_PKT_OK);
  886. rx_ev_hdr_type = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_HDR_TYPE);
  887. if (likely(rx_ev_pkt_ok)) {
  888. /* If packet is marked as OK then we can rely on the
  889. * hardware checksum and classification.
  890. */
  891. flags = 0;
  892. switch (rx_ev_hdr_type) {
  893. case FSE_CZ_RX_EV_HDR_TYPE_IPV4V6_TCP:
  894. flags |= EFX_RX_PKT_TCP;
  895. /* fall through */
  896. case FSE_CZ_RX_EV_HDR_TYPE_IPV4V6_UDP:
  897. flags |= EFX_RX_PKT_CSUMMED;
  898. /* fall through */
  899. case FSE_CZ_RX_EV_HDR_TYPE_IPV4V6_OTHER:
  900. case FSE_AZ_RX_EV_HDR_TYPE_OTHER:
  901. break;
  902. }
  903. } else {
  904. flags = efx_farch_handle_rx_not_ok(rx_queue, event);
  905. }
  906. /* Detect multicast packets that didn't match the filter */
  907. rx_ev_mcast_pkt = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_MCAST_PKT);
  908. if (rx_ev_mcast_pkt) {
  909. unsigned int rx_ev_mcast_hash_match =
  910. EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_MCAST_HASH_MATCH);
  911. if (unlikely(!rx_ev_mcast_hash_match)) {
  912. ++channel->n_rx_mcast_mismatch;
  913. flags |= EFX_RX_PKT_DISCARD;
  914. }
  915. }
  916. channel->irq_mod_score += 2;
  917. /* Handle received packet */
  918. efx_rx_packet(rx_queue,
  919. rx_queue->removed_count & rx_queue->ptr_mask,
  920. rx_queue->scatter_n, rx_ev_byte_cnt, flags);
  921. rx_queue->removed_count += rx_queue->scatter_n;
  922. rx_queue->scatter_n = 0;
  923. }
  924. /* If this flush done event corresponds to a &struct efx_tx_queue, then
  925. * send an %EFX_CHANNEL_MAGIC_TX_DRAIN event to drain the event queue
  926. * of all transmit completions.
  927. */
  928. static void
  929. efx_farch_handle_tx_flush_done(struct efx_nic *efx, efx_qword_t *event)
  930. {
  931. struct efx_tx_queue *tx_queue;
  932. int qid;
  933. qid = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_SUBDATA);
  934. if (qid < EFX_TXQ_TYPES * efx->n_tx_channels) {
  935. tx_queue = efx_get_tx_queue(efx, qid / EFX_TXQ_TYPES,
  936. qid % EFX_TXQ_TYPES);
  937. if (atomic_cmpxchg(&tx_queue->flush_outstanding, 1, 0)) {
  938. efx_farch_magic_event(tx_queue->channel,
  939. EFX_CHANNEL_MAGIC_TX_DRAIN(tx_queue));
  940. }
  941. }
  942. }
  943. /* If this flush done event corresponds to a &struct efx_rx_queue: If the flush
  944. * was successful then send an %EFX_CHANNEL_MAGIC_RX_DRAIN, otherwise add
  945. * the RX queue back to the mask of RX queues in need of flushing.
  946. */
  947. static void
  948. efx_farch_handle_rx_flush_done(struct efx_nic *efx, efx_qword_t *event)
  949. {
  950. struct efx_channel *channel;
  951. struct efx_rx_queue *rx_queue;
  952. int qid;
  953. bool failed;
  954. qid = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_RX_DESCQ_ID);
  955. failed = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_RX_FLUSH_FAIL);
  956. if (qid >= efx->n_channels)
  957. return;
  958. channel = efx_get_channel(efx, qid);
  959. if (!efx_channel_has_rx_queue(channel))
  960. return;
  961. rx_queue = efx_channel_get_rx_queue(channel);
  962. if (failed) {
  963. netif_info(efx, hw, efx->net_dev,
  964. "RXQ %d flush retry\n", qid);
  965. rx_queue->flush_pending = true;
  966. atomic_inc(&efx->rxq_flush_pending);
  967. } else {
  968. efx_farch_magic_event(efx_rx_queue_channel(rx_queue),
  969. EFX_CHANNEL_MAGIC_RX_DRAIN(rx_queue));
  970. }
  971. atomic_dec(&efx->rxq_flush_outstanding);
  972. if (efx_farch_flush_wake(efx))
  973. wake_up(&efx->flush_wq);
  974. }
  975. static void
  976. efx_farch_handle_drain_event(struct efx_channel *channel)
  977. {
  978. struct efx_nic *efx = channel->efx;
  979. WARN_ON(atomic_read(&efx->active_queues) == 0);
  980. atomic_dec(&efx->active_queues);
  981. if (efx_farch_flush_wake(efx))
  982. wake_up(&efx->flush_wq);
  983. }
  984. static void efx_farch_handle_generated_event(struct efx_channel *channel,
  985. efx_qword_t *event)
  986. {
  987. struct efx_nic *efx = channel->efx;
  988. struct efx_rx_queue *rx_queue =
  989. efx_channel_has_rx_queue(channel) ?
  990. efx_channel_get_rx_queue(channel) : NULL;
  991. unsigned magic, code;
  992. magic = EFX_QWORD_FIELD(*event, FSF_AZ_DRV_GEN_EV_MAGIC);
  993. code = _EFX_CHANNEL_MAGIC_CODE(magic);
  994. if (magic == EFX_CHANNEL_MAGIC_TEST(channel)) {
  995. channel->event_test_cpu = raw_smp_processor_id();
  996. } else if (rx_queue && magic == EFX_CHANNEL_MAGIC_FILL(rx_queue)) {
  997. /* The queue must be empty, so we won't receive any rx
  998. * events, so efx_process_channel() won't refill the
  999. * queue. Refill it here */
  1000. efx_fast_push_rx_descriptors(rx_queue, true);
  1001. } else if (rx_queue && magic == EFX_CHANNEL_MAGIC_RX_DRAIN(rx_queue)) {
  1002. efx_farch_handle_drain_event(channel);
  1003. } else if (code == _EFX_CHANNEL_MAGIC_TX_DRAIN) {
  1004. efx_farch_handle_drain_event(channel);
  1005. } else {
  1006. netif_dbg(efx, hw, efx->net_dev, "channel %d received "
  1007. "generated event "EFX_QWORD_FMT"\n",
  1008. channel->channel, EFX_QWORD_VAL(*event));
  1009. }
  1010. }
  1011. static void
  1012. efx_farch_handle_driver_event(struct efx_channel *channel, efx_qword_t *event)
  1013. {
  1014. struct efx_nic *efx = channel->efx;
  1015. unsigned int ev_sub_code;
  1016. unsigned int ev_sub_data;
  1017. ev_sub_code = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_SUBCODE);
  1018. ev_sub_data = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_SUBDATA);
  1019. switch (ev_sub_code) {
  1020. case FSE_AZ_TX_DESCQ_FLS_DONE_EV:
  1021. netif_vdbg(efx, hw, efx->net_dev, "channel %d TXQ %d flushed\n",
  1022. channel->channel, ev_sub_data);
  1023. efx_farch_handle_tx_flush_done(efx, event);
  1024. #ifdef CONFIG_SFC_SRIOV
  1025. efx_siena_sriov_tx_flush_done(efx, event);
  1026. #endif
  1027. break;
  1028. case FSE_AZ_RX_DESCQ_FLS_DONE_EV:
  1029. netif_vdbg(efx, hw, efx->net_dev, "channel %d RXQ %d flushed\n",
  1030. channel->channel, ev_sub_data);
  1031. efx_farch_handle_rx_flush_done(efx, event);
  1032. #ifdef CONFIG_SFC_SRIOV
  1033. efx_siena_sriov_rx_flush_done(efx, event);
  1034. #endif
  1035. break;
  1036. case FSE_AZ_EVQ_INIT_DONE_EV:
  1037. netif_dbg(efx, hw, efx->net_dev,
  1038. "channel %d EVQ %d initialised\n",
  1039. channel->channel, ev_sub_data);
  1040. break;
  1041. case FSE_AZ_SRM_UPD_DONE_EV:
  1042. netif_vdbg(efx, hw, efx->net_dev,
  1043. "channel %d SRAM update done\n", channel->channel);
  1044. break;
  1045. case FSE_AZ_WAKE_UP_EV:
  1046. netif_vdbg(efx, hw, efx->net_dev,
  1047. "channel %d RXQ %d wakeup event\n",
  1048. channel->channel, ev_sub_data);
  1049. break;
  1050. case FSE_AZ_TIMER_EV:
  1051. netif_vdbg(efx, hw, efx->net_dev,
  1052. "channel %d RX queue %d timer expired\n",
  1053. channel->channel, ev_sub_data);
  1054. break;
  1055. case FSE_AA_RX_RECOVER_EV:
  1056. netif_err(efx, rx_err, efx->net_dev,
  1057. "channel %d seen DRIVER RX_RESET event. "
  1058. "Resetting.\n", channel->channel);
  1059. atomic_inc(&efx->rx_reset);
  1060. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  1061. break;
  1062. case FSE_BZ_RX_DSC_ERROR_EV:
  1063. if (ev_sub_data < EFX_VI_BASE) {
  1064. netif_err(efx, rx_err, efx->net_dev,
  1065. "RX DMA Q %d reports descriptor fetch error."
  1066. " RX Q %d is disabled.\n", ev_sub_data,
  1067. ev_sub_data);
  1068. efx_schedule_reset(efx, RESET_TYPE_DMA_ERROR);
  1069. }
  1070. #ifdef CONFIG_SFC_SRIOV
  1071. else
  1072. efx_siena_sriov_desc_fetch_err(efx, ev_sub_data);
  1073. #endif
  1074. break;
  1075. case FSE_BZ_TX_DSC_ERROR_EV:
  1076. if (ev_sub_data < EFX_VI_BASE) {
  1077. netif_err(efx, tx_err, efx->net_dev,
  1078. "TX DMA Q %d reports descriptor fetch error."
  1079. " TX Q %d is disabled.\n", ev_sub_data,
  1080. ev_sub_data);
  1081. efx_schedule_reset(efx, RESET_TYPE_DMA_ERROR);
  1082. }
  1083. #ifdef CONFIG_SFC_SRIOV
  1084. else
  1085. efx_siena_sriov_desc_fetch_err(efx, ev_sub_data);
  1086. #endif
  1087. break;
  1088. default:
  1089. netif_vdbg(efx, hw, efx->net_dev,
  1090. "channel %d unknown driver event code %d "
  1091. "data %04x\n", channel->channel, ev_sub_code,
  1092. ev_sub_data);
  1093. break;
  1094. }
  1095. }
  1096. int efx_farch_ev_process(struct efx_channel *channel, int budget)
  1097. {
  1098. struct efx_nic *efx = channel->efx;
  1099. unsigned int read_ptr;
  1100. efx_qword_t event, *p_event;
  1101. int ev_code;
  1102. int tx_packets = 0;
  1103. int spent = 0;
  1104. if (budget <= 0)
  1105. return spent;
  1106. read_ptr = channel->eventq_read_ptr;
  1107. for (;;) {
  1108. p_event = efx_event(channel, read_ptr);
  1109. event = *p_event;
  1110. if (!efx_event_present(&event))
  1111. /* End of events */
  1112. break;
  1113. netif_vdbg(channel->efx, intr, channel->efx->net_dev,
  1114. "channel %d event is "EFX_QWORD_FMT"\n",
  1115. channel->channel, EFX_QWORD_VAL(event));
  1116. /* Clear this event by marking it all ones */
  1117. EFX_SET_QWORD(*p_event);
  1118. ++read_ptr;
  1119. ev_code = EFX_QWORD_FIELD(event, FSF_AZ_EV_CODE);
  1120. switch (ev_code) {
  1121. case FSE_AZ_EV_CODE_RX_EV:
  1122. efx_farch_handle_rx_event(channel, &event);
  1123. if (++spent == budget)
  1124. goto out;
  1125. break;
  1126. case FSE_AZ_EV_CODE_TX_EV:
  1127. tx_packets += efx_farch_handle_tx_event(channel,
  1128. &event);
  1129. if (tx_packets > efx->txq_entries) {
  1130. spent = budget;
  1131. goto out;
  1132. }
  1133. break;
  1134. case FSE_AZ_EV_CODE_DRV_GEN_EV:
  1135. efx_farch_handle_generated_event(channel, &event);
  1136. break;
  1137. case FSE_AZ_EV_CODE_DRIVER_EV:
  1138. efx_farch_handle_driver_event(channel, &event);
  1139. break;
  1140. #ifdef CONFIG_SFC_SRIOV
  1141. case FSE_CZ_EV_CODE_USER_EV:
  1142. efx_siena_sriov_event(channel, &event);
  1143. break;
  1144. #endif
  1145. case FSE_CZ_EV_CODE_MCDI_EV:
  1146. efx_mcdi_process_event(channel, &event);
  1147. break;
  1148. case FSE_AZ_EV_CODE_GLOBAL_EV:
  1149. if (efx->type->handle_global_event &&
  1150. efx->type->handle_global_event(channel, &event))
  1151. break;
  1152. /* else fall through */
  1153. default:
  1154. netif_err(channel->efx, hw, channel->efx->net_dev,
  1155. "channel %d unknown event type %d (data "
  1156. EFX_QWORD_FMT ")\n", channel->channel,
  1157. ev_code, EFX_QWORD_VAL(event));
  1158. }
  1159. }
  1160. out:
  1161. channel->eventq_read_ptr = read_ptr;
  1162. return spent;
  1163. }
  1164. /* Allocate buffer table entries for event queue */
  1165. int efx_farch_ev_probe(struct efx_channel *channel)
  1166. {
  1167. struct efx_nic *efx = channel->efx;
  1168. unsigned entries;
  1169. entries = channel->eventq_mask + 1;
  1170. return efx_alloc_special_buffer(efx, &channel->eventq,
  1171. entries * sizeof(efx_qword_t));
  1172. }
  1173. int efx_farch_ev_init(struct efx_channel *channel)
  1174. {
  1175. efx_oword_t reg;
  1176. struct efx_nic *efx = channel->efx;
  1177. netif_dbg(efx, hw, efx->net_dev,
  1178. "channel %d event queue in special buffers %d-%d\n",
  1179. channel->channel, channel->eventq.index,
  1180. channel->eventq.index + channel->eventq.entries - 1);
  1181. EFX_POPULATE_OWORD_3(reg,
  1182. FRF_CZ_TIMER_Q_EN, 1,
  1183. FRF_CZ_HOST_NOTIFY_MODE, 0,
  1184. FRF_CZ_TIMER_MODE, FFE_CZ_TIMER_MODE_DIS);
  1185. efx_writeo_table(efx, &reg, FR_BZ_TIMER_TBL, channel->channel);
  1186. /* Pin event queue buffer */
  1187. efx_init_special_buffer(efx, &channel->eventq);
  1188. /* Fill event queue with all ones (i.e. empty events) */
  1189. memset(channel->eventq.buf.addr, 0xff, channel->eventq.buf.len);
  1190. /* Push event queue to card */
  1191. EFX_POPULATE_OWORD_3(reg,
  1192. FRF_AZ_EVQ_EN, 1,
  1193. FRF_AZ_EVQ_SIZE, __ffs(channel->eventq.entries),
  1194. FRF_AZ_EVQ_BUF_BASE_ID, channel->eventq.index);
  1195. efx_writeo_table(efx, &reg, efx->type->evq_ptr_tbl_base,
  1196. channel->channel);
  1197. return 0;
  1198. }
  1199. void efx_farch_ev_fini(struct efx_channel *channel)
  1200. {
  1201. efx_oword_t reg;
  1202. struct efx_nic *efx = channel->efx;
  1203. /* Remove event queue from card */
  1204. EFX_ZERO_OWORD(reg);
  1205. efx_writeo_table(efx, &reg, efx->type->evq_ptr_tbl_base,
  1206. channel->channel);
  1207. efx_writeo_table(efx, &reg, FR_BZ_TIMER_TBL, channel->channel);
  1208. /* Unpin event queue */
  1209. efx_fini_special_buffer(efx, &channel->eventq);
  1210. }
  1211. /* Free buffers backing event queue */
  1212. void efx_farch_ev_remove(struct efx_channel *channel)
  1213. {
  1214. efx_free_special_buffer(channel->efx, &channel->eventq);
  1215. }
  1216. void efx_farch_ev_test_generate(struct efx_channel *channel)
  1217. {
  1218. efx_farch_magic_event(channel, EFX_CHANNEL_MAGIC_TEST(channel));
  1219. }
  1220. void efx_farch_rx_defer_refill(struct efx_rx_queue *rx_queue)
  1221. {
  1222. efx_farch_magic_event(efx_rx_queue_channel(rx_queue),
  1223. EFX_CHANNEL_MAGIC_FILL(rx_queue));
  1224. }
  1225. /**************************************************************************
  1226. *
  1227. * Hardware interrupts
  1228. * The hardware interrupt handler does very little work; all the event
  1229. * queue processing is carried out by per-channel tasklets.
  1230. *
  1231. **************************************************************************/
  1232. /* Enable/disable/generate interrupts */
  1233. static inline void efx_farch_interrupts(struct efx_nic *efx,
  1234. bool enabled, bool force)
  1235. {
  1236. efx_oword_t int_en_reg_ker;
  1237. EFX_POPULATE_OWORD_3(int_en_reg_ker,
  1238. FRF_AZ_KER_INT_LEVE_SEL, efx->irq_level,
  1239. FRF_AZ_KER_INT_KER, force,
  1240. FRF_AZ_DRV_INT_EN_KER, enabled);
  1241. efx_writeo(efx, &int_en_reg_ker, FR_AZ_INT_EN_KER);
  1242. }
  1243. void efx_farch_irq_enable_master(struct efx_nic *efx)
  1244. {
  1245. EFX_ZERO_OWORD(*((efx_oword_t *) efx->irq_status.addr));
  1246. wmb(); /* Ensure interrupt vector is clear before interrupts enabled */
  1247. efx_farch_interrupts(efx, true, false);
  1248. }
  1249. void efx_farch_irq_disable_master(struct efx_nic *efx)
  1250. {
  1251. /* Disable interrupts */
  1252. efx_farch_interrupts(efx, false, false);
  1253. }
  1254. /* Generate a test interrupt
  1255. * Interrupt must already have been enabled, otherwise nasty things
  1256. * may happen.
  1257. */
  1258. int efx_farch_irq_test_generate(struct efx_nic *efx)
  1259. {
  1260. efx_farch_interrupts(efx, true, true);
  1261. return 0;
  1262. }
  1263. /* Process a fatal interrupt
  1264. * Disable bus mastering ASAP and schedule a reset
  1265. */
  1266. irqreturn_t efx_farch_fatal_interrupt(struct efx_nic *efx)
  1267. {
  1268. efx_oword_t *int_ker = efx->irq_status.addr;
  1269. efx_oword_t fatal_intr;
  1270. int error, mem_perr;
  1271. efx_reado(efx, &fatal_intr, FR_AZ_FATAL_INTR_KER);
  1272. error = EFX_OWORD_FIELD(fatal_intr, FRF_AZ_FATAL_INTR);
  1273. netif_err(efx, hw, efx->net_dev, "SYSTEM ERROR "EFX_OWORD_FMT" status "
  1274. EFX_OWORD_FMT ": %s\n", EFX_OWORD_VAL(*int_ker),
  1275. EFX_OWORD_VAL(fatal_intr),
  1276. error ? "disabling bus mastering" : "no recognised error");
  1277. /* If this is a memory parity error dump which blocks are offending */
  1278. mem_perr = (EFX_OWORD_FIELD(fatal_intr, FRF_AZ_MEM_PERR_INT_KER) ||
  1279. EFX_OWORD_FIELD(fatal_intr, FRF_AZ_SRM_PERR_INT_KER));
  1280. if (mem_perr) {
  1281. efx_oword_t reg;
  1282. efx_reado(efx, &reg, FR_AZ_MEM_STAT);
  1283. netif_err(efx, hw, efx->net_dev,
  1284. "SYSTEM ERROR: memory parity error "EFX_OWORD_FMT"\n",
  1285. EFX_OWORD_VAL(reg));
  1286. }
  1287. /* Disable both devices */
  1288. pci_clear_master(efx->pci_dev);
  1289. efx_farch_irq_disable_master(efx);
  1290. /* Count errors and reset or disable the NIC accordingly */
  1291. if (efx->int_error_count == 0 ||
  1292. time_after(jiffies, efx->int_error_expire)) {
  1293. efx->int_error_count = 0;
  1294. efx->int_error_expire =
  1295. jiffies + EFX_INT_ERROR_EXPIRE * HZ;
  1296. }
  1297. if (++efx->int_error_count < EFX_MAX_INT_ERRORS) {
  1298. netif_err(efx, hw, efx->net_dev,
  1299. "SYSTEM ERROR - reset scheduled\n");
  1300. efx_schedule_reset(efx, RESET_TYPE_INT_ERROR);
  1301. } else {
  1302. netif_err(efx, hw, efx->net_dev,
  1303. "SYSTEM ERROR - max number of errors seen."
  1304. "NIC will be disabled\n");
  1305. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  1306. }
  1307. return IRQ_HANDLED;
  1308. }
  1309. /* Handle a legacy interrupt
  1310. * Acknowledges the interrupt and schedule event queue processing.
  1311. */
  1312. irqreturn_t efx_farch_legacy_interrupt(int irq, void *dev_id)
  1313. {
  1314. struct efx_nic *efx = dev_id;
  1315. bool soft_enabled = ACCESS_ONCE(efx->irq_soft_enabled);
  1316. efx_oword_t *int_ker = efx->irq_status.addr;
  1317. irqreturn_t result = IRQ_NONE;
  1318. struct efx_channel *channel;
  1319. efx_dword_t reg;
  1320. u32 queues;
  1321. int syserr;
  1322. /* Read the ISR which also ACKs the interrupts */
  1323. efx_readd(efx, &reg, FR_BZ_INT_ISR0);
  1324. queues = EFX_EXTRACT_DWORD(reg, 0, 31);
  1325. /* Legacy interrupts are disabled too late by the EEH kernel
  1326. * code. Disable them earlier.
  1327. * If an EEH error occurred, the read will have returned all ones.
  1328. */
  1329. if (EFX_DWORD_IS_ALL_ONES(reg) && efx_try_recovery(efx) &&
  1330. !efx->eeh_disabled_legacy_irq) {
  1331. disable_irq_nosync(efx->legacy_irq);
  1332. efx->eeh_disabled_legacy_irq = true;
  1333. }
  1334. /* Handle non-event-queue sources */
  1335. if (queues & (1U << efx->irq_level) && soft_enabled) {
  1336. syserr = EFX_OWORD_FIELD(*int_ker, FSF_AZ_NET_IVEC_FATAL_INT);
  1337. if (unlikely(syserr))
  1338. return efx_farch_fatal_interrupt(efx);
  1339. efx->last_irq_cpu = raw_smp_processor_id();
  1340. }
  1341. if (queues != 0) {
  1342. efx->irq_zero_count = 0;
  1343. /* Schedule processing of any interrupting queues */
  1344. if (likely(soft_enabled)) {
  1345. efx_for_each_channel(channel, efx) {
  1346. if (queues & 1)
  1347. efx_schedule_channel_irq(channel);
  1348. queues >>= 1;
  1349. }
  1350. }
  1351. result = IRQ_HANDLED;
  1352. } else {
  1353. efx_qword_t *event;
  1354. /* Legacy ISR read can return zero once (SF bug 15783) */
  1355. /* We can't return IRQ_HANDLED more than once on seeing ISR=0
  1356. * because this might be a shared interrupt. */
  1357. if (efx->irq_zero_count++ == 0)
  1358. result = IRQ_HANDLED;
  1359. /* Ensure we schedule or rearm all event queues */
  1360. if (likely(soft_enabled)) {
  1361. efx_for_each_channel(channel, efx) {
  1362. event = efx_event(channel,
  1363. channel->eventq_read_ptr);
  1364. if (efx_event_present(event))
  1365. efx_schedule_channel_irq(channel);
  1366. else
  1367. efx_farch_ev_read_ack(channel);
  1368. }
  1369. }
  1370. }
  1371. if (result == IRQ_HANDLED)
  1372. netif_vdbg(efx, intr, efx->net_dev,
  1373. "IRQ %d on CPU %d status " EFX_DWORD_FMT "\n",
  1374. irq, raw_smp_processor_id(), EFX_DWORD_VAL(reg));
  1375. return result;
  1376. }
  1377. /* Handle an MSI interrupt
  1378. *
  1379. * Handle an MSI hardware interrupt. This routine schedules event
  1380. * queue processing. No interrupt acknowledgement cycle is necessary.
  1381. * Also, we never need to check that the interrupt is for us, since
  1382. * MSI interrupts cannot be shared.
  1383. */
  1384. irqreturn_t efx_farch_msi_interrupt(int irq, void *dev_id)
  1385. {
  1386. struct efx_msi_context *context = dev_id;
  1387. struct efx_nic *efx = context->efx;
  1388. efx_oword_t *int_ker = efx->irq_status.addr;
  1389. int syserr;
  1390. netif_vdbg(efx, intr, efx->net_dev,
  1391. "IRQ %d on CPU %d status " EFX_OWORD_FMT "\n",
  1392. irq, raw_smp_processor_id(), EFX_OWORD_VAL(*int_ker));
  1393. if (!likely(ACCESS_ONCE(efx->irq_soft_enabled)))
  1394. return IRQ_HANDLED;
  1395. /* Handle non-event-queue sources */
  1396. if (context->index == efx->irq_level) {
  1397. syserr = EFX_OWORD_FIELD(*int_ker, FSF_AZ_NET_IVEC_FATAL_INT);
  1398. if (unlikely(syserr))
  1399. return efx_farch_fatal_interrupt(efx);
  1400. efx->last_irq_cpu = raw_smp_processor_id();
  1401. }
  1402. /* Schedule processing of the channel */
  1403. efx_schedule_channel_irq(efx->channel[context->index]);
  1404. return IRQ_HANDLED;
  1405. }
  1406. /* Setup RSS indirection table.
  1407. * This maps from the hash value of the packet to RXQ
  1408. */
  1409. void efx_farch_rx_push_indir_table(struct efx_nic *efx)
  1410. {
  1411. size_t i = 0;
  1412. efx_dword_t dword;
  1413. BUILD_BUG_ON(ARRAY_SIZE(efx->rx_indir_table) !=
  1414. FR_BZ_RX_INDIRECTION_TBL_ROWS);
  1415. for (i = 0; i < FR_BZ_RX_INDIRECTION_TBL_ROWS; i++) {
  1416. EFX_POPULATE_DWORD_1(dword, FRF_BZ_IT_QUEUE,
  1417. efx->rx_indir_table[i]);
  1418. efx_writed(efx, &dword,
  1419. FR_BZ_RX_INDIRECTION_TBL +
  1420. FR_BZ_RX_INDIRECTION_TBL_STEP * i);
  1421. }
  1422. }
  1423. void efx_farch_rx_pull_indir_table(struct efx_nic *efx)
  1424. {
  1425. size_t i = 0;
  1426. efx_dword_t dword;
  1427. BUILD_BUG_ON(ARRAY_SIZE(efx->rx_indir_table) !=
  1428. FR_BZ_RX_INDIRECTION_TBL_ROWS);
  1429. for (i = 0; i < FR_BZ_RX_INDIRECTION_TBL_ROWS; i++) {
  1430. efx_readd(efx, &dword,
  1431. FR_BZ_RX_INDIRECTION_TBL +
  1432. FR_BZ_RX_INDIRECTION_TBL_STEP * i);
  1433. efx->rx_indir_table[i] = EFX_DWORD_FIELD(dword, FRF_BZ_IT_QUEUE);
  1434. }
  1435. }
  1436. /* Looks at available SRAM resources and works out how many queues we
  1437. * can support, and where things like descriptor caches should live.
  1438. *
  1439. * SRAM is split up as follows:
  1440. * 0 buftbl entries for channels
  1441. * efx->vf_buftbl_base buftbl entries for SR-IOV
  1442. * efx->rx_dc_base RX descriptor caches
  1443. * efx->tx_dc_base TX descriptor caches
  1444. */
  1445. void efx_farch_dimension_resources(struct efx_nic *efx, unsigned sram_lim_qw)
  1446. {
  1447. unsigned vi_count, buftbl_min;
  1448. #ifdef CONFIG_SFC_SRIOV
  1449. struct siena_nic_data *nic_data = efx->nic_data;
  1450. #endif
  1451. /* Account for the buffer table entries backing the datapath channels
  1452. * and the descriptor caches for those channels.
  1453. */
  1454. buftbl_min = ((efx->n_rx_channels * EFX_MAX_DMAQ_SIZE +
  1455. efx->n_tx_channels * EFX_TXQ_TYPES * EFX_MAX_DMAQ_SIZE +
  1456. efx->n_channels * EFX_MAX_EVQ_SIZE)
  1457. * sizeof(efx_qword_t) / EFX_BUF_SIZE);
  1458. vi_count = max(efx->n_channels, efx->n_tx_channels * EFX_TXQ_TYPES);
  1459. #ifdef CONFIG_SFC_SRIOV
  1460. if (efx->type->sriov_wanted) {
  1461. if (efx->type->sriov_wanted(efx)) {
  1462. unsigned vi_dc_entries, buftbl_free;
  1463. unsigned entries_per_vf, vf_limit;
  1464. nic_data->vf_buftbl_base = buftbl_min;
  1465. vi_dc_entries = RX_DC_ENTRIES + TX_DC_ENTRIES;
  1466. vi_count = max(vi_count, EFX_VI_BASE);
  1467. buftbl_free = (sram_lim_qw - buftbl_min -
  1468. vi_count * vi_dc_entries);
  1469. entries_per_vf = ((vi_dc_entries +
  1470. EFX_VF_BUFTBL_PER_VI) *
  1471. efx_vf_size(efx));
  1472. vf_limit = min(buftbl_free / entries_per_vf,
  1473. (1024U - EFX_VI_BASE) >> efx->vi_scale);
  1474. if (efx->vf_count > vf_limit) {
  1475. netif_err(efx, probe, efx->net_dev,
  1476. "Reducing VF count from from %d to %d\n",
  1477. efx->vf_count, vf_limit);
  1478. efx->vf_count = vf_limit;
  1479. }
  1480. vi_count += efx->vf_count * efx_vf_size(efx);
  1481. }
  1482. }
  1483. #endif
  1484. efx->tx_dc_base = sram_lim_qw - vi_count * TX_DC_ENTRIES;
  1485. efx->rx_dc_base = efx->tx_dc_base - vi_count * RX_DC_ENTRIES;
  1486. }
  1487. u32 efx_farch_fpga_ver(struct efx_nic *efx)
  1488. {
  1489. efx_oword_t altera_build;
  1490. efx_reado(efx, &altera_build, FR_AZ_ALTERA_BUILD);
  1491. return EFX_OWORD_FIELD(altera_build, FRF_AZ_ALTERA_BUILD_VER);
  1492. }
  1493. void efx_farch_init_common(struct efx_nic *efx)
  1494. {
  1495. efx_oword_t temp;
  1496. /* Set positions of descriptor caches in SRAM. */
  1497. EFX_POPULATE_OWORD_1(temp, FRF_AZ_SRM_TX_DC_BASE_ADR, efx->tx_dc_base);
  1498. efx_writeo(efx, &temp, FR_AZ_SRM_TX_DC_CFG);
  1499. EFX_POPULATE_OWORD_1(temp, FRF_AZ_SRM_RX_DC_BASE_ADR, efx->rx_dc_base);
  1500. efx_writeo(efx, &temp, FR_AZ_SRM_RX_DC_CFG);
  1501. /* Set TX descriptor cache size. */
  1502. BUILD_BUG_ON(TX_DC_ENTRIES != (8 << TX_DC_ENTRIES_ORDER));
  1503. EFX_POPULATE_OWORD_1(temp, FRF_AZ_TX_DC_SIZE, TX_DC_ENTRIES_ORDER);
  1504. efx_writeo(efx, &temp, FR_AZ_TX_DC_CFG);
  1505. /* Set RX descriptor cache size. Set low watermark to size-8, as
  1506. * this allows most efficient prefetching.
  1507. */
  1508. BUILD_BUG_ON(RX_DC_ENTRIES != (8 << RX_DC_ENTRIES_ORDER));
  1509. EFX_POPULATE_OWORD_1(temp, FRF_AZ_RX_DC_SIZE, RX_DC_ENTRIES_ORDER);
  1510. efx_writeo(efx, &temp, FR_AZ_RX_DC_CFG);
  1511. EFX_POPULATE_OWORD_1(temp, FRF_AZ_RX_DC_PF_LWM, RX_DC_ENTRIES - 8);
  1512. efx_writeo(efx, &temp, FR_AZ_RX_DC_PF_WM);
  1513. /* Program INT_KER address */
  1514. EFX_POPULATE_OWORD_2(temp,
  1515. FRF_AZ_NORM_INT_VEC_DIS_KER,
  1516. EFX_INT_MODE_USE_MSI(efx),
  1517. FRF_AZ_INT_ADR_KER, efx->irq_status.dma_addr);
  1518. efx_writeo(efx, &temp, FR_AZ_INT_ADR_KER);
  1519. if (EFX_WORKAROUND_17213(efx) && !EFX_INT_MODE_USE_MSI(efx))
  1520. /* Use an interrupt level unused by event queues */
  1521. efx->irq_level = 0x1f;
  1522. else
  1523. /* Use a valid MSI-X vector */
  1524. efx->irq_level = 0;
  1525. /* Enable all the genuinely fatal interrupts. (They are still
  1526. * masked by the overall interrupt mask, controlled by
  1527. * falcon_interrupts()).
  1528. *
  1529. * Note: All other fatal interrupts are enabled
  1530. */
  1531. EFX_POPULATE_OWORD_3(temp,
  1532. FRF_AZ_ILL_ADR_INT_KER_EN, 1,
  1533. FRF_AZ_RBUF_OWN_INT_KER_EN, 1,
  1534. FRF_AZ_TBUF_OWN_INT_KER_EN, 1);
  1535. EFX_SET_OWORD_FIELD(temp, FRF_CZ_SRAM_PERR_INT_P_KER_EN, 1);
  1536. EFX_INVERT_OWORD(temp);
  1537. efx_writeo(efx, &temp, FR_AZ_FATAL_INTR_KER);
  1538. /* Disable the ugly timer-based TX DMA backoff and allow TX DMA to be
  1539. * controlled by the RX FIFO fill level. Set arbitration to one pkt/Q.
  1540. */
  1541. efx_reado(efx, &temp, FR_AZ_TX_RESERVED);
  1542. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_RX_SPACER, 0xfe);
  1543. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_RX_SPACER_EN, 1);
  1544. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_ONE_PKT_PER_Q, 1);
  1545. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_PUSH_EN, 1);
  1546. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_DIS_NON_IP_EV, 1);
  1547. /* Enable SW_EV to inherit in char driver - assume harmless here */
  1548. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_SOFT_EVT_EN, 1);
  1549. /* Prefetch threshold 2 => fetch when descriptor cache half empty */
  1550. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_PREF_THRESHOLD, 2);
  1551. /* Disable hardware watchdog which can misfire */
  1552. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_PREF_WD_TMR, 0x3fffff);
  1553. /* Squash TX of packets of 16 bytes or less */
  1554. EFX_SET_OWORD_FIELD(temp, FRF_BZ_TX_FLUSH_MIN_LEN_EN, 1);
  1555. efx_writeo(efx, &temp, FR_AZ_TX_RESERVED);
  1556. EFX_POPULATE_OWORD_4(temp,
  1557. /* Default values */
  1558. FRF_BZ_TX_PACE_SB_NOT_AF, 0x15,
  1559. FRF_BZ_TX_PACE_SB_AF, 0xb,
  1560. FRF_BZ_TX_PACE_FB_BASE, 0,
  1561. /* Allow large pace values in the fast bin. */
  1562. FRF_BZ_TX_PACE_BIN_TH,
  1563. FFE_BZ_TX_PACE_RESERVED);
  1564. efx_writeo(efx, &temp, FR_BZ_TX_PACE);
  1565. }
  1566. /**************************************************************************
  1567. *
  1568. * Filter tables
  1569. *
  1570. **************************************************************************
  1571. */
  1572. /* "Fudge factors" - difference between programmed value and actual depth.
  1573. * Due to pipelined implementation we need to program H/W with a value that
  1574. * is larger than the hop limit we want.
  1575. */
  1576. #define EFX_FARCH_FILTER_CTL_SRCH_FUDGE_WILD 3
  1577. #define EFX_FARCH_FILTER_CTL_SRCH_FUDGE_FULL 1
  1578. /* Hard maximum search limit. Hardware will time-out beyond 200-something.
  1579. * We also need to avoid infinite loops in efx_farch_filter_search() when the
  1580. * table is full.
  1581. */
  1582. #define EFX_FARCH_FILTER_CTL_SRCH_MAX 200
  1583. /* Don't try very hard to find space for performance hints, as this is
  1584. * counter-productive. */
  1585. #define EFX_FARCH_FILTER_CTL_SRCH_HINT_MAX 5
  1586. enum efx_farch_filter_type {
  1587. EFX_FARCH_FILTER_TCP_FULL = 0,
  1588. EFX_FARCH_FILTER_TCP_WILD,
  1589. EFX_FARCH_FILTER_UDP_FULL,
  1590. EFX_FARCH_FILTER_UDP_WILD,
  1591. EFX_FARCH_FILTER_MAC_FULL = 4,
  1592. EFX_FARCH_FILTER_MAC_WILD,
  1593. EFX_FARCH_FILTER_UC_DEF = 8,
  1594. EFX_FARCH_FILTER_MC_DEF,
  1595. EFX_FARCH_FILTER_TYPE_COUNT, /* number of specific types */
  1596. };
  1597. enum efx_farch_filter_table_id {
  1598. EFX_FARCH_FILTER_TABLE_RX_IP = 0,
  1599. EFX_FARCH_FILTER_TABLE_RX_MAC,
  1600. EFX_FARCH_FILTER_TABLE_RX_DEF,
  1601. EFX_FARCH_FILTER_TABLE_TX_MAC,
  1602. EFX_FARCH_FILTER_TABLE_COUNT,
  1603. };
  1604. enum efx_farch_filter_index {
  1605. EFX_FARCH_FILTER_INDEX_UC_DEF,
  1606. EFX_FARCH_FILTER_INDEX_MC_DEF,
  1607. EFX_FARCH_FILTER_SIZE_RX_DEF,
  1608. };
  1609. struct efx_farch_filter_spec {
  1610. u8 type:4;
  1611. u8 priority:4;
  1612. u8 flags;
  1613. u16 dmaq_id;
  1614. u32 data[3];
  1615. };
  1616. struct efx_farch_filter_table {
  1617. enum efx_farch_filter_table_id id;
  1618. u32 offset; /* address of table relative to BAR */
  1619. unsigned size; /* number of entries */
  1620. unsigned step; /* step between entries */
  1621. unsigned used; /* number currently used */
  1622. unsigned long *used_bitmap;
  1623. struct efx_farch_filter_spec *spec;
  1624. unsigned search_limit[EFX_FARCH_FILTER_TYPE_COUNT];
  1625. };
  1626. struct efx_farch_filter_state {
  1627. struct efx_farch_filter_table table[EFX_FARCH_FILTER_TABLE_COUNT];
  1628. };
  1629. static void
  1630. efx_farch_filter_table_clear_entry(struct efx_nic *efx,
  1631. struct efx_farch_filter_table *table,
  1632. unsigned int filter_idx);
  1633. /* The filter hash function is LFSR polynomial x^16 + x^3 + 1 of a 32-bit
  1634. * key derived from the n-tuple. The initial LFSR state is 0xffff. */
  1635. static u16 efx_farch_filter_hash(u32 key)
  1636. {
  1637. u16 tmp;
  1638. /* First 16 rounds */
  1639. tmp = 0x1fff ^ key >> 16;
  1640. tmp = tmp ^ tmp >> 3 ^ tmp >> 6;
  1641. tmp = tmp ^ tmp >> 9;
  1642. /* Last 16 rounds */
  1643. tmp = tmp ^ tmp << 13 ^ key;
  1644. tmp = tmp ^ tmp >> 3 ^ tmp >> 6;
  1645. return tmp ^ tmp >> 9;
  1646. }
  1647. /* To allow for hash collisions, filter search continues at these
  1648. * increments from the first possible entry selected by the hash. */
  1649. static u16 efx_farch_filter_increment(u32 key)
  1650. {
  1651. return key * 2 - 1;
  1652. }
  1653. static enum efx_farch_filter_table_id
  1654. efx_farch_filter_spec_table_id(const struct efx_farch_filter_spec *spec)
  1655. {
  1656. BUILD_BUG_ON(EFX_FARCH_FILTER_TABLE_RX_IP !=
  1657. (EFX_FARCH_FILTER_TCP_FULL >> 2));
  1658. BUILD_BUG_ON(EFX_FARCH_FILTER_TABLE_RX_IP !=
  1659. (EFX_FARCH_FILTER_TCP_WILD >> 2));
  1660. BUILD_BUG_ON(EFX_FARCH_FILTER_TABLE_RX_IP !=
  1661. (EFX_FARCH_FILTER_UDP_FULL >> 2));
  1662. BUILD_BUG_ON(EFX_FARCH_FILTER_TABLE_RX_IP !=
  1663. (EFX_FARCH_FILTER_UDP_WILD >> 2));
  1664. BUILD_BUG_ON(EFX_FARCH_FILTER_TABLE_RX_MAC !=
  1665. (EFX_FARCH_FILTER_MAC_FULL >> 2));
  1666. BUILD_BUG_ON(EFX_FARCH_FILTER_TABLE_RX_MAC !=
  1667. (EFX_FARCH_FILTER_MAC_WILD >> 2));
  1668. BUILD_BUG_ON(EFX_FARCH_FILTER_TABLE_TX_MAC !=
  1669. EFX_FARCH_FILTER_TABLE_RX_MAC + 2);
  1670. return (spec->type >> 2) + ((spec->flags & EFX_FILTER_FLAG_TX) ? 2 : 0);
  1671. }
  1672. static void efx_farch_filter_push_rx_config(struct efx_nic *efx)
  1673. {
  1674. struct efx_farch_filter_state *state = efx->filter_state;
  1675. struct efx_farch_filter_table *table;
  1676. efx_oword_t filter_ctl;
  1677. efx_reado(efx, &filter_ctl, FR_BZ_RX_FILTER_CTL);
  1678. table = &state->table[EFX_FARCH_FILTER_TABLE_RX_IP];
  1679. EFX_SET_OWORD_FIELD(filter_ctl, FRF_BZ_TCP_FULL_SRCH_LIMIT,
  1680. table->search_limit[EFX_FARCH_FILTER_TCP_FULL] +
  1681. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_FULL);
  1682. EFX_SET_OWORD_FIELD(filter_ctl, FRF_BZ_TCP_WILD_SRCH_LIMIT,
  1683. table->search_limit[EFX_FARCH_FILTER_TCP_WILD] +
  1684. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_WILD);
  1685. EFX_SET_OWORD_FIELD(filter_ctl, FRF_BZ_UDP_FULL_SRCH_LIMIT,
  1686. table->search_limit[EFX_FARCH_FILTER_UDP_FULL] +
  1687. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_FULL);
  1688. EFX_SET_OWORD_FIELD(filter_ctl, FRF_BZ_UDP_WILD_SRCH_LIMIT,
  1689. table->search_limit[EFX_FARCH_FILTER_UDP_WILD] +
  1690. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_WILD);
  1691. table = &state->table[EFX_FARCH_FILTER_TABLE_RX_MAC];
  1692. if (table->size) {
  1693. EFX_SET_OWORD_FIELD(
  1694. filter_ctl, FRF_CZ_ETHERNET_FULL_SEARCH_LIMIT,
  1695. table->search_limit[EFX_FARCH_FILTER_MAC_FULL] +
  1696. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_FULL);
  1697. EFX_SET_OWORD_FIELD(
  1698. filter_ctl, FRF_CZ_ETHERNET_WILDCARD_SEARCH_LIMIT,
  1699. table->search_limit[EFX_FARCH_FILTER_MAC_WILD] +
  1700. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_WILD);
  1701. }
  1702. table = &state->table[EFX_FARCH_FILTER_TABLE_RX_DEF];
  1703. if (table->size) {
  1704. EFX_SET_OWORD_FIELD(
  1705. filter_ctl, FRF_CZ_UNICAST_NOMATCH_Q_ID,
  1706. table->spec[EFX_FARCH_FILTER_INDEX_UC_DEF].dmaq_id);
  1707. EFX_SET_OWORD_FIELD(
  1708. filter_ctl, FRF_CZ_UNICAST_NOMATCH_RSS_ENABLED,
  1709. !!(table->spec[EFX_FARCH_FILTER_INDEX_UC_DEF].flags &
  1710. EFX_FILTER_FLAG_RX_RSS));
  1711. EFX_SET_OWORD_FIELD(
  1712. filter_ctl, FRF_CZ_MULTICAST_NOMATCH_Q_ID,
  1713. table->spec[EFX_FARCH_FILTER_INDEX_MC_DEF].dmaq_id);
  1714. EFX_SET_OWORD_FIELD(
  1715. filter_ctl, FRF_CZ_MULTICAST_NOMATCH_RSS_ENABLED,
  1716. !!(table->spec[EFX_FARCH_FILTER_INDEX_MC_DEF].flags &
  1717. EFX_FILTER_FLAG_RX_RSS));
  1718. /* There is a single bit to enable RX scatter for all
  1719. * unmatched packets. Only set it if scatter is
  1720. * enabled in both filter specs.
  1721. */
  1722. EFX_SET_OWORD_FIELD(
  1723. filter_ctl, FRF_BZ_SCATTER_ENBL_NO_MATCH_Q,
  1724. !!(table->spec[EFX_FARCH_FILTER_INDEX_UC_DEF].flags &
  1725. table->spec[EFX_FARCH_FILTER_INDEX_MC_DEF].flags &
  1726. EFX_FILTER_FLAG_RX_SCATTER));
  1727. } else {
  1728. /* We don't expose 'default' filters because unmatched
  1729. * packets always go to the queue number found in the
  1730. * RSS table. But we still need to set the RX scatter
  1731. * bit here.
  1732. */
  1733. EFX_SET_OWORD_FIELD(
  1734. filter_ctl, FRF_BZ_SCATTER_ENBL_NO_MATCH_Q,
  1735. efx->rx_scatter);
  1736. }
  1737. efx_writeo(efx, &filter_ctl, FR_BZ_RX_FILTER_CTL);
  1738. }
  1739. static void efx_farch_filter_push_tx_limits(struct efx_nic *efx)
  1740. {
  1741. struct efx_farch_filter_state *state = efx->filter_state;
  1742. struct efx_farch_filter_table *table;
  1743. efx_oword_t tx_cfg;
  1744. efx_reado(efx, &tx_cfg, FR_AZ_TX_CFG);
  1745. table = &state->table[EFX_FARCH_FILTER_TABLE_TX_MAC];
  1746. if (table->size) {
  1747. EFX_SET_OWORD_FIELD(
  1748. tx_cfg, FRF_CZ_TX_ETH_FILTER_FULL_SEARCH_RANGE,
  1749. table->search_limit[EFX_FARCH_FILTER_MAC_FULL] +
  1750. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_FULL);
  1751. EFX_SET_OWORD_FIELD(
  1752. tx_cfg, FRF_CZ_TX_ETH_FILTER_WILD_SEARCH_RANGE,
  1753. table->search_limit[EFX_FARCH_FILTER_MAC_WILD] +
  1754. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_WILD);
  1755. }
  1756. efx_writeo(efx, &tx_cfg, FR_AZ_TX_CFG);
  1757. }
  1758. static int
  1759. efx_farch_filter_from_gen_spec(struct efx_farch_filter_spec *spec,
  1760. const struct efx_filter_spec *gen_spec)
  1761. {
  1762. bool is_full = false;
  1763. if ((gen_spec->flags & EFX_FILTER_FLAG_RX_RSS) &&
  1764. gen_spec->rss_context != EFX_FILTER_RSS_CONTEXT_DEFAULT)
  1765. return -EINVAL;
  1766. spec->priority = gen_spec->priority;
  1767. spec->flags = gen_spec->flags;
  1768. spec->dmaq_id = gen_spec->dmaq_id;
  1769. switch (gen_spec->match_flags) {
  1770. case (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_IP_PROTO |
  1771. EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_LOC_PORT |
  1772. EFX_FILTER_MATCH_REM_HOST | EFX_FILTER_MATCH_REM_PORT):
  1773. is_full = true;
  1774. /* fall through */
  1775. case (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_IP_PROTO |
  1776. EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_LOC_PORT): {
  1777. __be32 rhost, host1, host2;
  1778. __be16 rport, port1, port2;
  1779. EFX_WARN_ON_PARANOID(!(gen_spec->flags & EFX_FILTER_FLAG_RX));
  1780. if (gen_spec->ether_type != htons(ETH_P_IP))
  1781. return -EPROTONOSUPPORT;
  1782. if (gen_spec->loc_port == 0 ||
  1783. (is_full && gen_spec->rem_port == 0))
  1784. return -EADDRNOTAVAIL;
  1785. switch (gen_spec->ip_proto) {
  1786. case IPPROTO_TCP:
  1787. spec->type = (is_full ? EFX_FARCH_FILTER_TCP_FULL :
  1788. EFX_FARCH_FILTER_TCP_WILD);
  1789. break;
  1790. case IPPROTO_UDP:
  1791. spec->type = (is_full ? EFX_FARCH_FILTER_UDP_FULL :
  1792. EFX_FARCH_FILTER_UDP_WILD);
  1793. break;
  1794. default:
  1795. return -EPROTONOSUPPORT;
  1796. }
  1797. /* Filter is constructed in terms of source and destination,
  1798. * with the odd wrinkle that the ports are swapped in a UDP
  1799. * wildcard filter. We need to convert from local and remote
  1800. * (= zero for wildcard) addresses.
  1801. */
  1802. rhost = is_full ? gen_spec->rem_host[0] : 0;
  1803. rport = is_full ? gen_spec->rem_port : 0;
  1804. host1 = rhost;
  1805. host2 = gen_spec->loc_host[0];
  1806. if (!is_full && gen_spec->ip_proto == IPPROTO_UDP) {
  1807. port1 = gen_spec->loc_port;
  1808. port2 = rport;
  1809. } else {
  1810. port1 = rport;
  1811. port2 = gen_spec->loc_port;
  1812. }
  1813. spec->data[0] = ntohl(host1) << 16 | ntohs(port1);
  1814. spec->data[1] = ntohs(port2) << 16 | ntohl(host1) >> 16;
  1815. spec->data[2] = ntohl(host2);
  1816. break;
  1817. }
  1818. case EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_OUTER_VID:
  1819. is_full = true;
  1820. /* fall through */
  1821. case EFX_FILTER_MATCH_LOC_MAC:
  1822. spec->type = (is_full ? EFX_FARCH_FILTER_MAC_FULL :
  1823. EFX_FARCH_FILTER_MAC_WILD);
  1824. spec->data[0] = is_full ? ntohs(gen_spec->outer_vid) : 0;
  1825. spec->data[1] = (gen_spec->loc_mac[2] << 24 |
  1826. gen_spec->loc_mac[3] << 16 |
  1827. gen_spec->loc_mac[4] << 8 |
  1828. gen_spec->loc_mac[5]);
  1829. spec->data[2] = (gen_spec->loc_mac[0] << 8 |
  1830. gen_spec->loc_mac[1]);
  1831. break;
  1832. case EFX_FILTER_MATCH_LOC_MAC_IG:
  1833. spec->type = (is_multicast_ether_addr(gen_spec->loc_mac) ?
  1834. EFX_FARCH_FILTER_MC_DEF :
  1835. EFX_FARCH_FILTER_UC_DEF);
  1836. memset(spec->data, 0, sizeof(spec->data)); /* ensure equality */
  1837. break;
  1838. default:
  1839. return -EPROTONOSUPPORT;
  1840. }
  1841. return 0;
  1842. }
  1843. static void
  1844. efx_farch_filter_to_gen_spec(struct efx_filter_spec *gen_spec,
  1845. const struct efx_farch_filter_spec *spec)
  1846. {
  1847. bool is_full = false;
  1848. /* *gen_spec should be completely initialised, to be consistent
  1849. * with efx_filter_init_{rx,tx}() and in case we want to copy
  1850. * it back to userland.
  1851. */
  1852. memset(gen_spec, 0, sizeof(*gen_spec));
  1853. gen_spec->priority = spec->priority;
  1854. gen_spec->flags = spec->flags;
  1855. gen_spec->dmaq_id = spec->dmaq_id;
  1856. switch (spec->type) {
  1857. case EFX_FARCH_FILTER_TCP_FULL:
  1858. case EFX_FARCH_FILTER_UDP_FULL:
  1859. is_full = true;
  1860. /* fall through */
  1861. case EFX_FARCH_FILTER_TCP_WILD:
  1862. case EFX_FARCH_FILTER_UDP_WILD: {
  1863. __be32 host1, host2;
  1864. __be16 port1, port2;
  1865. gen_spec->match_flags =
  1866. EFX_FILTER_MATCH_ETHER_TYPE |
  1867. EFX_FILTER_MATCH_IP_PROTO |
  1868. EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_LOC_PORT;
  1869. if (is_full)
  1870. gen_spec->match_flags |= (EFX_FILTER_MATCH_REM_HOST |
  1871. EFX_FILTER_MATCH_REM_PORT);
  1872. gen_spec->ether_type = htons(ETH_P_IP);
  1873. gen_spec->ip_proto =
  1874. (spec->type == EFX_FARCH_FILTER_TCP_FULL ||
  1875. spec->type == EFX_FARCH_FILTER_TCP_WILD) ?
  1876. IPPROTO_TCP : IPPROTO_UDP;
  1877. host1 = htonl(spec->data[0] >> 16 | spec->data[1] << 16);
  1878. port1 = htons(spec->data[0]);
  1879. host2 = htonl(spec->data[2]);
  1880. port2 = htons(spec->data[1] >> 16);
  1881. if (spec->flags & EFX_FILTER_FLAG_TX) {
  1882. gen_spec->loc_host[0] = host1;
  1883. gen_spec->rem_host[0] = host2;
  1884. } else {
  1885. gen_spec->loc_host[0] = host2;
  1886. gen_spec->rem_host[0] = host1;
  1887. }
  1888. if (!!(gen_spec->flags & EFX_FILTER_FLAG_TX) ^
  1889. (!is_full && gen_spec->ip_proto == IPPROTO_UDP)) {
  1890. gen_spec->loc_port = port1;
  1891. gen_spec->rem_port = port2;
  1892. } else {
  1893. gen_spec->loc_port = port2;
  1894. gen_spec->rem_port = port1;
  1895. }
  1896. break;
  1897. }
  1898. case EFX_FARCH_FILTER_MAC_FULL:
  1899. is_full = true;
  1900. /* fall through */
  1901. case EFX_FARCH_FILTER_MAC_WILD:
  1902. gen_spec->match_flags = EFX_FILTER_MATCH_LOC_MAC;
  1903. if (is_full)
  1904. gen_spec->match_flags |= EFX_FILTER_MATCH_OUTER_VID;
  1905. gen_spec->loc_mac[0] = spec->data[2] >> 8;
  1906. gen_spec->loc_mac[1] = spec->data[2];
  1907. gen_spec->loc_mac[2] = spec->data[1] >> 24;
  1908. gen_spec->loc_mac[3] = spec->data[1] >> 16;
  1909. gen_spec->loc_mac[4] = spec->data[1] >> 8;
  1910. gen_spec->loc_mac[5] = spec->data[1];
  1911. gen_spec->outer_vid = htons(spec->data[0]);
  1912. break;
  1913. case EFX_FARCH_FILTER_UC_DEF:
  1914. case EFX_FARCH_FILTER_MC_DEF:
  1915. gen_spec->match_flags = EFX_FILTER_MATCH_LOC_MAC_IG;
  1916. gen_spec->loc_mac[0] = spec->type == EFX_FARCH_FILTER_MC_DEF;
  1917. break;
  1918. default:
  1919. WARN_ON(1);
  1920. break;
  1921. }
  1922. }
  1923. static void
  1924. efx_farch_filter_init_rx_auto(struct efx_nic *efx,
  1925. struct efx_farch_filter_spec *spec)
  1926. {
  1927. /* If there's only one channel then disable RSS for non VF
  1928. * traffic, thereby allowing VFs to use RSS when the PF can't.
  1929. */
  1930. spec->priority = EFX_FILTER_PRI_AUTO;
  1931. spec->flags = (EFX_FILTER_FLAG_RX |
  1932. (efx_rss_enabled(efx) ? EFX_FILTER_FLAG_RX_RSS : 0) |
  1933. (efx->rx_scatter ? EFX_FILTER_FLAG_RX_SCATTER : 0));
  1934. spec->dmaq_id = 0;
  1935. }
  1936. /* Build a filter entry and return its n-tuple key. */
  1937. static u32 efx_farch_filter_build(efx_oword_t *filter,
  1938. struct efx_farch_filter_spec *spec)
  1939. {
  1940. u32 data3;
  1941. switch (efx_farch_filter_spec_table_id(spec)) {
  1942. case EFX_FARCH_FILTER_TABLE_RX_IP: {
  1943. bool is_udp = (spec->type == EFX_FARCH_FILTER_UDP_FULL ||
  1944. spec->type == EFX_FARCH_FILTER_UDP_WILD);
  1945. EFX_POPULATE_OWORD_7(
  1946. *filter,
  1947. FRF_BZ_RSS_EN,
  1948. !!(spec->flags & EFX_FILTER_FLAG_RX_RSS),
  1949. FRF_BZ_SCATTER_EN,
  1950. !!(spec->flags & EFX_FILTER_FLAG_RX_SCATTER),
  1951. FRF_BZ_TCP_UDP, is_udp,
  1952. FRF_BZ_RXQ_ID, spec->dmaq_id,
  1953. EFX_DWORD_2, spec->data[2],
  1954. EFX_DWORD_1, spec->data[1],
  1955. EFX_DWORD_0, spec->data[0]);
  1956. data3 = is_udp;
  1957. break;
  1958. }
  1959. case EFX_FARCH_FILTER_TABLE_RX_MAC: {
  1960. bool is_wild = spec->type == EFX_FARCH_FILTER_MAC_WILD;
  1961. EFX_POPULATE_OWORD_7(
  1962. *filter,
  1963. FRF_CZ_RMFT_RSS_EN,
  1964. !!(spec->flags & EFX_FILTER_FLAG_RX_RSS),
  1965. FRF_CZ_RMFT_SCATTER_EN,
  1966. !!(spec->flags & EFX_FILTER_FLAG_RX_SCATTER),
  1967. FRF_CZ_RMFT_RXQ_ID, spec->dmaq_id,
  1968. FRF_CZ_RMFT_WILDCARD_MATCH, is_wild,
  1969. FRF_CZ_RMFT_DEST_MAC_HI, spec->data[2],
  1970. FRF_CZ_RMFT_DEST_MAC_LO, spec->data[1],
  1971. FRF_CZ_RMFT_VLAN_ID, spec->data[0]);
  1972. data3 = is_wild;
  1973. break;
  1974. }
  1975. case EFX_FARCH_FILTER_TABLE_TX_MAC: {
  1976. bool is_wild = spec->type == EFX_FARCH_FILTER_MAC_WILD;
  1977. EFX_POPULATE_OWORD_5(*filter,
  1978. FRF_CZ_TMFT_TXQ_ID, spec->dmaq_id,
  1979. FRF_CZ_TMFT_WILDCARD_MATCH, is_wild,
  1980. FRF_CZ_TMFT_SRC_MAC_HI, spec->data[2],
  1981. FRF_CZ_TMFT_SRC_MAC_LO, spec->data[1],
  1982. FRF_CZ_TMFT_VLAN_ID, spec->data[0]);
  1983. data3 = is_wild | spec->dmaq_id << 1;
  1984. break;
  1985. }
  1986. default:
  1987. BUG();
  1988. }
  1989. return spec->data[0] ^ spec->data[1] ^ spec->data[2] ^ data3;
  1990. }
  1991. static bool efx_farch_filter_equal(const struct efx_farch_filter_spec *left,
  1992. const struct efx_farch_filter_spec *right)
  1993. {
  1994. if (left->type != right->type ||
  1995. memcmp(left->data, right->data, sizeof(left->data)))
  1996. return false;
  1997. if (left->flags & EFX_FILTER_FLAG_TX &&
  1998. left->dmaq_id != right->dmaq_id)
  1999. return false;
  2000. return true;
  2001. }
  2002. /*
  2003. * Construct/deconstruct external filter IDs. At least the RX filter
  2004. * IDs must be ordered by matching priority, for RX NFC semantics.
  2005. *
  2006. * Deconstruction needs to be robust against invalid IDs so that
  2007. * efx_filter_remove_id_safe() and efx_filter_get_filter_safe() can
  2008. * accept user-provided IDs.
  2009. */
  2010. #define EFX_FARCH_FILTER_MATCH_PRI_COUNT 5
  2011. static const u8 efx_farch_filter_type_match_pri[EFX_FARCH_FILTER_TYPE_COUNT] = {
  2012. [EFX_FARCH_FILTER_TCP_FULL] = 0,
  2013. [EFX_FARCH_FILTER_UDP_FULL] = 0,
  2014. [EFX_FARCH_FILTER_TCP_WILD] = 1,
  2015. [EFX_FARCH_FILTER_UDP_WILD] = 1,
  2016. [EFX_FARCH_FILTER_MAC_FULL] = 2,
  2017. [EFX_FARCH_FILTER_MAC_WILD] = 3,
  2018. [EFX_FARCH_FILTER_UC_DEF] = 4,
  2019. [EFX_FARCH_FILTER_MC_DEF] = 4,
  2020. };
  2021. static const enum efx_farch_filter_table_id efx_farch_filter_range_table[] = {
  2022. EFX_FARCH_FILTER_TABLE_RX_IP, /* RX match pri 0 */
  2023. EFX_FARCH_FILTER_TABLE_RX_IP,
  2024. EFX_FARCH_FILTER_TABLE_RX_MAC,
  2025. EFX_FARCH_FILTER_TABLE_RX_MAC,
  2026. EFX_FARCH_FILTER_TABLE_RX_DEF, /* RX match pri 4 */
  2027. EFX_FARCH_FILTER_TABLE_TX_MAC, /* TX match pri 0 */
  2028. EFX_FARCH_FILTER_TABLE_TX_MAC, /* TX match pri 1 */
  2029. };
  2030. #define EFX_FARCH_FILTER_INDEX_WIDTH 13
  2031. #define EFX_FARCH_FILTER_INDEX_MASK ((1 << EFX_FARCH_FILTER_INDEX_WIDTH) - 1)
  2032. static inline u32
  2033. efx_farch_filter_make_id(const struct efx_farch_filter_spec *spec,
  2034. unsigned int index)
  2035. {
  2036. unsigned int range;
  2037. range = efx_farch_filter_type_match_pri[spec->type];
  2038. if (!(spec->flags & EFX_FILTER_FLAG_RX))
  2039. range += EFX_FARCH_FILTER_MATCH_PRI_COUNT;
  2040. return range << EFX_FARCH_FILTER_INDEX_WIDTH | index;
  2041. }
  2042. static inline enum efx_farch_filter_table_id
  2043. efx_farch_filter_id_table_id(u32 id)
  2044. {
  2045. unsigned int range = id >> EFX_FARCH_FILTER_INDEX_WIDTH;
  2046. if (range < ARRAY_SIZE(efx_farch_filter_range_table))
  2047. return efx_farch_filter_range_table[range];
  2048. else
  2049. return EFX_FARCH_FILTER_TABLE_COUNT; /* invalid */
  2050. }
  2051. static inline unsigned int efx_farch_filter_id_index(u32 id)
  2052. {
  2053. return id & EFX_FARCH_FILTER_INDEX_MASK;
  2054. }
  2055. u32 efx_farch_filter_get_rx_id_limit(struct efx_nic *efx)
  2056. {
  2057. struct efx_farch_filter_state *state = efx->filter_state;
  2058. unsigned int range = EFX_FARCH_FILTER_MATCH_PRI_COUNT - 1;
  2059. enum efx_farch_filter_table_id table_id;
  2060. do {
  2061. table_id = efx_farch_filter_range_table[range];
  2062. if (state->table[table_id].size != 0)
  2063. return range << EFX_FARCH_FILTER_INDEX_WIDTH |
  2064. state->table[table_id].size;
  2065. } while (range--);
  2066. return 0;
  2067. }
  2068. s32 efx_farch_filter_insert(struct efx_nic *efx,
  2069. struct efx_filter_spec *gen_spec,
  2070. bool replace_equal)
  2071. {
  2072. struct efx_farch_filter_state *state = efx->filter_state;
  2073. struct efx_farch_filter_table *table;
  2074. struct efx_farch_filter_spec spec;
  2075. efx_oword_t filter;
  2076. int rep_index, ins_index;
  2077. unsigned int depth = 0;
  2078. int rc;
  2079. rc = efx_farch_filter_from_gen_spec(&spec, gen_spec);
  2080. if (rc)
  2081. return rc;
  2082. table = &state->table[efx_farch_filter_spec_table_id(&spec)];
  2083. if (table->size == 0)
  2084. return -EINVAL;
  2085. netif_vdbg(efx, hw, efx->net_dev,
  2086. "%s: type %d search_limit=%d", __func__, spec.type,
  2087. table->search_limit[spec.type]);
  2088. if (table->id == EFX_FARCH_FILTER_TABLE_RX_DEF) {
  2089. /* One filter spec per type */
  2090. BUILD_BUG_ON(EFX_FARCH_FILTER_INDEX_UC_DEF != 0);
  2091. BUILD_BUG_ON(EFX_FARCH_FILTER_INDEX_MC_DEF !=
  2092. EFX_FARCH_FILTER_MC_DEF - EFX_FARCH_FILTER_UC_DEF);
  2093. rep_index = spec.type - EFX_FARCH_FILTER_UC_DEF;
  2094. ins_index = rep_index;
  2095. spin_lock_bh(&efx->filter_lock);
  2096. } else {
  2097. /* Search concurrently for
  2098. * (1) a filter to be replaced (rep_index): any filter
  2099. * with the same match values, up to the current
  2100. * search depth for this type, and
  2101. * (2) the insertion point (ins_index): (1) or any
  2102. * free slot before it or up to the maximum search
  2103. * depth for this priority
  2104. * We fail if we cannot find (2).
  2105. *
  2106. * We can stop once either
  2107. * (a) we find (1), in which case we have definitely
  2108. * found (2) as well; or
  2109. * (b) we have searched exhaustively for (1), and have
  2110. * either found (2) or searched exhaustively for it
  2111. */
  2112. u32 key = efx_farch_filter_build(&filter, &spec);
  2113. unsigned int hash = efx_farch_filter_hash(key);
  2114. unsigned int incr = efx_farch_filter_increment(key);
  2115. unsigned int max_rep_depth = table->search_limit[spec.type];
  2116. unsigned int max_ins_depth =
  2117. spec.priority <= EFX_FILTER_PRI_HINT ?
  2118. EFX_FARCH_FILTER_CTL_SRCH_HINT_MAX :
  2119. EFX_FARCH_FILTER_CTL_SRCH_MAX;
  2120. unsigned int i = hash & (table->size - 1);
  2121. ins_index = -1;
  2122. depth = 1;
  2123. spin_lock_bh(&efx->filter_lock);
  2124. for (;;) {
  2125. if (!test_bit(i, table->used_bitmap)) {
  2126. if (ins_index < 0)
  2127. ins_index = i;
  2128. } else if (efx_farch_filter_equal(&spec,
  2129. &table->spec[i])) {
  2130. /* Case (a) */
  2131. if (ins_index < 0)
  2132. ins_index = i;
  2133. rep_index = i;
  2134. break;
  2135. }
  2136. if (depth >= max_rep_depth &&
  2137. (ins_index >= 0 || depth >= max_ins_depth)) {
  2138. /* Case (b) */
  2139. if (ins_index < 0) {
  2140. rc = -EBUSY;
  2141. goto out;
  2142. }
  2143. rep_index = -1;
  2144. break;
  2145. }
  2146. i = (i + incr) & (table->size - 1);
  2147. ++depth;
  2148. }
  2149. }
  2150. /* If we found a filter to be replaced, check whether we
  2151. * should do so
  2152. */
  2153. if (rep_index >= 0) {
  2154. struct efx_farch_filter_spec *saved_spec =
  2155. &table->spec[rep_index];
  2156. if (spec.priority == saved_spec->priority && !replace_equal) {
  2157. rc = -EEXIST;
  2158. goto out;
  2159. }
  2160. if (spec.priority < saved_spec->priority) {
  2161. rc = -EPERM;
  2162. goto out;
  2163. }
  2164. if (saved_spec->priority == EFX_FILTER_PRI_AUTO ||
  2165. saved_spec->flags & EFX_FILTER_FLAG_RX_OVER_AUTO)
  2166. spec.flags |= EFX_FILTER_FLAG_RX_OVER_AUTO;
  2167. }
  2168. /* Insert the filter */
  2169. if (ins_index != rep_index) {
  2170. __set_bit(ins_index, table->used_bitmap);
  2171. ++table->used;
  2172. }
  2173. table->spec[ins_index] = spec;
  2174. if (table->id == EFX_FARCH_FILTER_TABLE_RX_DEF) {
  2175. efx_farch_filter_push_rx_config(efx);
  2176. } else {
  2177. if (table->search_limit[spec.type] < depth) {
  2178. table->search_limit[spec.type] = depth;
  2179. if (spec.flags & EFX_FILTER_FLAG_TX)
  2180. efx_farch_filter_push_tx_limits(efx);
  2181. else
  2182. efx_farch_filter_push_rx_config(efx);
  2183. }
  2184. efx_writeo(efx, &filter,
  2185. table->offset + table->step * ins_index);
  2186. /* If we were able to replace a filter by inserting
  2187. * at a lower depth, clear the replaced filter
  2188. */
  2189. if (ins_index != rep_index && rep_index >= 0)
  2190. efx_farch_filter_table_clear_entry(efx, table,
  2191. rep_index);
  2192. }
  2193. netif_vdbg(efx, hw, efx->net_dev,
  2194. "%s: filter type %d index %d rxq %u set",
  2195. __func__, spec.type, ins_index, spec.dmaq_id);
  2196. rc = efx_farch_filter_make_id(&spec, ins_index);
  2197. out:
  2198. spin_unlock_bh(&efx->filter_lock);
  2199. return rc;
  2200. }
  2201. static void
  2202. efx_farch_filter_table_clear_entry(struct efx_nic *efx,
  2203. struct efx_farch_filter_table *table,
  2204. unsigned int filter_idx)
  2205. {
  2206. static efx_oword_t filter;
  2207. EFX_WARN_ON_PARANOID(!test_bit(filter_idx, table->used_bitmap));
  2208. BUG_ON(table->offset == 0); /* can't clear MAC default filters */
  2209. __clear_bit(filter_idx, table->used_bitmap);
  2210. --table->used;
  2211. memset(&table->spec[filter_idx], 0, sizeof(table->spec[0]));
  2212. efx_writeo(efx, &filter, table->offset + table->step * filter_idx);
  2213. /* If this filter required a greater search depth than
  2214. * any other, the search limit for its type can now be
  2215. * decreased. However, it is hard to determine that
  2216. * unless the table has become completely empty - in
  2217. * which case, all its search limits can be set to 0.
  2218. */
  2219. if (unlikely(table->used == 0)) {
  2220. memset(table->search_limit, 0, sizeof(table->search_limit));
  2221. if (table->id == EFX_FARCH_FILTER_TABLE_TX_MAC)
  2222. efx_farch_filter_push_tx_limits(efx);
  2223. else
  2224. efx_farch_filter_push_rx_config(efx);
  2225. }
  2226. }
  2227. static int efx_farch_filter_remove(struct efx_nic *efx,
  2228. struct efx_farch_filter_table *table,
  2229. unsigned int filter_idx,
  2230. enum efx_filter_priority priority)
  2231. {
  2232. struct efx_farch_filter_spec *spec = &table->spec[filter_idx];
  2233. if (!test_bit(filter_idx, table->used_bitmap) ||
  2234. spec->priority != priority)
  2235. return -ENOENT;
  2236. if (spec->flags & EFX_FILTER_FLAG_RX_OVER_AUTO) {
  2237. efx_farch_filter_init_rx_auto(efx, spec);
  2238. efx_farch_filter_push_rx_config(efx);
  2239. } else {
  2240. efx_farch_filter_table_clear_entry(efx, table, filter_idx);
  2241. }
  2242. return 0;
  2243. }
  2244. int efx_farch_filter_remove_safe(struct efx_nic *efx,
  2245. enum efx_filter_priority priority,
  2246. u32 filter_id)
  2247. {
  2248. struct efx_farch_filter_state *state = efx->filter_state;
  2249. enum efx_farch_filter_table_id table_id;
  2250. struct efx_farch_filter_table *table;
  2251. unsigned int filter_idx;
  2252. struct efx_farch_filter_spec *spec;
  2253. int rc;
  2254. table_id = efx_farch_filter_id_table_id(filter_id);
  2255. if ((unsigned int)table_id >= EFX_FARCH_FILTER_TABLE_COUNT)
  2256. return -ENOENT;
  2257. table = &state->table[table_id];
  2258. filter_idx = efx_farch_filter_id_index(filter_id);
  2259. if (filter_idx >= table->size)
  2260. return -ENOENT;
  2261. spec = &table->spec[filter_idx];
  2262. spin_lock_bh(&efx->filter_lock);
  2263. rc = efx_farch_filter_remove(efx, table, filter_idx, priority);
  2264. spin_unlock_bh(&efx->filter_lock);
  2265. return rc;
  2266. }
  2267. int efx_farch_filter_get_safe(struct efx_nic *efx,
  2268. enum efx_filter_priority priority,
  2269. u32 filter_id, struct efx_filter_spec *spec_buf)
  2270. {
  2271. struct efx_farch_filter_state *state = efx->filter_state;
  2272. enum efx_farch_filter_table_id table_id;
  2273. struct efx_farch_filter_table *table;
  2274. struct efx_farch_filter_spec *spec;
  2275. unsigned int filter_idx;
  2276. int rc;
  2277. table_id = efx_farch_filter_id_table_id(filter_id);
  2278. if ((unsigned int)table_id >= EFX_FARCH_FILTER_TABLE_COUNT)
  2279. return -ENOENT;
  2280. table = &state->table[table_id];
  2281. filter_idx = efx_farch_filter_id_index(filter_id);
  2282. if (filter_idx >= table->size)
  2283. return -ENOENT;
  2284. spec = &table->spec[filter_idx];
  2285. spin_lock_bh(&efx->filter_lock);
  2286. if (test_bit(filter_idx, table->used_bitmap) &&
  2287. spec->priority == priority) {
  2288. efx_farch_filter_to_gen_spec(spec_buf, spec);
  2289. rc = 0;
  2290. } else {
  2291. rc = -ENOENT;
  2292. }
  2293. spin_unlock_bh(&efx->filter_lock);
  2294. return rc;
  2295. }
  2296. static void
  2297. efx_farch_filter_table_clear(struct efx_nic *efx,
  2298. enum efx_farch_filter_table_id table_id,
  2299. enum efx_filter_priority priority)
  2300. {
  2301. struct efx_farch_filter_state *state = efx->filter_state;
  2302. struct efx_farch_filter_table *table = &state->table[table_id];
  2303. unsigned int filter_idx;
  2304. spin_lock_bh(&efx->filter_lock);
  2305. for (filter_idx = 0; filter_idx < table->size; ++filter_idx) {
  2306. if (table->spec[filter_idx].priority != EFX_FILTER_PRI_AUTO)
  2307. efx_farch_filter_remove(efx, table,
  2308. filter_idx, priority);
  2309. }
  2310. spin_unlock_bh(&efx->filter_lock);
  2311. }
  2312. int efx_farch_filter_clear_rx(struct efx_nic *efx,
  2313. enum efx_filter_priority priority)
  2314. {
  2315. efx_farch_filter_table_clear(efx, EFX_FARCH_FILTER_TABLE_RX_IP,
  2316. priority);
  2317. efx_farch_filter_table_clear(efx, EFX_FARCH_FILTER_TABLE_RX_MAC,
  2318. priority);
  2319. efx_farch_filter_table_clear(efx, EFX_FARCH_FILTER_TABLE_RX_DEF,
  2320. priority);
  2321. return 0;
  2322. }
  2323. u32 efx_farch_filter_count_rx_used(struct efx_nic *efx,
  2324. enum efx_filter_priority priority)
  2325. {
  2326. struct efx_farch_filter_state *state = efx->filter_state;
  2327. enum efx_farch_filter_table_id table_id;
  2328. struct efx_farch_filter_table *table;
  2329. unsigned int filter_idx;
  2330. u32 count = 0;
  2331. spin_lock_bh(&efx->filter_lock);
  2332. for (table_id = EFX_FARCH_FILTER_TABLE_RX_IP;
  2333. table_id <= EFX_FARCH_FILTER_TABLE_RX_DEF;
  2334. table_id++) {
  2335. table = &state->table[table_id];
  2336. for (filter_idx = 0; filter_idx < table->size; filter_idx++) {
  2337. if (test_bit(filter_idx, table->used_bitmap) &&
  2338. table->spec[filter_idx].priority == priority)
  2339. ++count;
  2340. }
  2341. }
  2342. spin_unlock_bh(&efx->filter_lock);
  2343. return count;
  2344. }
  2345. s32 efx_farch_filter_get_rx_ids(struct efx_nic *efx,
  2346. enum efx_filter_priority priority,
  2347. u32 *buf, u32 size)
  2348. {
  2349. struct efx_farch_filter_state *state = efx->filter_state;
  2350. enum efx_farch_filter_table_id table_id;
  2351. struct efx_farch_filter_table *table;
  2352. unsigned int filter_idx;
  2353. s32 count = 0;
  2354. spin_lock_bh(&efx->filter_lock);
  2355. for (table_id = EFX_FARCH_FILTER_TABLE_RX_IP;
  2356. table_id <= EFX_FARCH_FILTER_TABLE_RX_DEF;
  2357. table_id++) {
  2358. table = &state->table[table_id];
  2359. for (filter_idx = 0; filter_idx < table->size; filter_idx++) {
  2360. if (test_bit(filter_idx, table->used_bitmap) &&
  2361. table->spec[filter_idx].priority == priority) {
  2362. if (count == size) {
  2363. count = -EMSGSIZE;
  2364. goto out;
  2365. }
  2366. buf[count++] = efx_farch_filter_make_id(
  2367. &table->spec[filter_idx], filter_idx);
  2368. }
  2369. }
  2370. }
  2371. out:
  2372. spin_unlock_bh(&efx->filter_lock);
  2373. return count;
  2374. }
  2375. /* Restore filter stater after reset */
  2376. void efx_farch_filter_table_restore(struct efx_nic *efx)
  2377. {
  2378. struct efx_farch_filter_state *state = efx->filter_state;
  2379. enum efx_farch_filter_table_id table_id;
  2380. struct efx_farch_filter_table *table;
  2381. efx_oword_t filter;
  2382. unsigned int filter_idx;
  2383. spin_lock_bh(&efx->filter_lock);
  2384. for (table_id = 0; table_id < EFX_FARCH_FILTER_TABLE_COUNT; table_id++) {
  2385. table = &state->table[table_id];
  2386. /* Check whether this is a regular register table */
  2387. if (table->step == 0)
  2388. continue;
  2389. for (filter_idx = 0; filter_idx < table->size; filter_idx++) {
  2390. if (!test_bit(filter_idx, table->used_bitmap))
  2391. continue;
  2392. efx_farch_filter_build(&filter, &table->spec[filter_idx]);
  2393. efx_writeo(efx, &filter,
  2394. table->offset + table->step * filter_idx);
  2395. }
  2396. }
  2397. efx_farch_filter_push_rx_config(efx);
  2398. efx_farch_filter_push_tx_limits(efx);
  2399. spin_unlock_bh(&efx->filter_lock);
  2400. }
  2401. void efx_farch_filter_table_remove(struct efx_nic *efx)
  2402. {
  2403. struct efx_farch_filter_state *state = efx->filter_state;
  2404. enum efx_farch_filter_table_id table_id;
  2405. for (table_id = 0; table_id < EFX_FARCH_FILTER_TABLE_COUNT; table_id++) {
  2406. kfree(state->table[table_id].used_bitmap);
  2407. vfree(state->table[table_id].spec);
  2408. }
  2409. kfree(state);
  2410. }
  2411. int efx_farch_filter_table_probe(struct efx_nic *efx)
  2412. {
  2413. struct efx_farch_filter_state *state;
  2414. struct efx_farch_filter_table *table;
  2415. unsigned table_id;
  2416. state = kzalloc(sizeof(struct efx_farch_filter_state), GFP_KERNEL);
  2417. if (!state)
  2418. return -ENOMEM;
  2419. efx->filter_state = state;
  2420. table = &state->table[EFX_FARCH_FILTER_TABLE_RX_IP];
  2421. table->id = EFX_FARCH_FILTER_TABLE_RX_IP;
  2422. table->offset = FR_BZ_RX_FILTER_TBL0;
  2423. table->size = FR_BZ_RX_FILTER_TBL0_ROWS;
  2424. table->step = FR_BZ_RX_FILTER_TBL0_STEP;
  2425. table = &state->table[EFX_FARCH_FILTER_TABLE_RX_MAC];
  2426. table->id = EFX_FARCH_FILTER_TABLE_RX_MAC;
  2427. table->offset = FR_CZ_RX_MAC_FILTER_TBL0;
  2428. table->size = FR_CZ_RX_MAC_FILTER_TBL0_ROWS;
  2429. table->step = FR_CZ_RX_MAC_FILTER_TBL0_STEP;
  2430. table = &state->table[EFX_FARCH_FILTER_TABLE_RX_DEF];
  2431. table->id = EFX_FARCH_FILTER_TABLE_RX_DEF;
  2432. table->size = EFX_FARCH_FILTER_SIZE_RX_DEF;
  2433. table = &state->table[EFX_FARCH_FILTER_TABLE_TX_MAC];
  2434. table->id = EFX_FARCH_FILTER_TABLE_TX_MAC;
  2435. table->offset = FR_CZ_TX_MAC_FILTER_TBL0;
  2436. table->size = FR_CZ_TX_MAC_FILTER_TBL0_ROWS;
  2437. table->step = FR_CZ_TX_MAC_FILTER_TBL0_STEP;
  2438. for (table_id = 0; table_id < EFX_FARCH_FILTER_TABLE_COUNT; table_id++) {
  2439. table = &state->table[table_id];
  2440. if (table->size == 0)
  2441. continue;
  2442. table->used_bitmap = kcalloc(BITS_TO_LONGS(table->size),
  2443. sizeof(unsigned long),
  2444. GFP_KERNEL);
  2445. if (!table->used_bitmap)
  2446. goto fail;
  2447. table->spec = vzalloc(table->size * sizeof(*table->spec));
  2448. if (!table->spec)
  2449. goto fail;
  2450. }
  2451. table = &state->table[EFX_FARCH_FILTER_TABLE_RX_DEF];
  2452. if (table->size) {
  2453. /* RX default filters must always exist */
  2454. struct efx_farch_filter_spec *spec;
  2455. unsigned i;
  2456. for (i = 0; i < EFX_FARCH_FILTER_SIZE_RX_DEF; i++) {
  2457. spec = &table->spec[i];
  2458. spec->type = EFX_FARCH_FILTER_UC_DEF + i;
  2459. efx_farch_filter_init_rx_auto(efx, spec);
  2460. __set_bit(i, table->used_bitmap);
  2461. }
  2462. }
  2463. efx_farch_filter_push_rx_config(efx);
  2464. return 0;
  2465. fail:
  2466. efx_farch_filter_table_remove(efx);
  2467. return -ENOMEM;
  2468. }
  2469. /* Update scatter enable flags for filters pointing to our own RX queues */
  2470. void efx_farch_filter_update_rx_scatter(struct efx_nic *efx)
  2471. {
  2472. struct efx_farch_filter_state *state = efx->filter_state;
  2473. enum efx_farch_filter_table_id table_id;
  2474. struct efx_farch_filter_table *table;
  2475. efx_oword_t filter;
  2476. unsigned int filter_idx;
  2477. spin_lock_bh(&efx->filter_lock);
  2478. for (table_id = EFX_FARCH_FILTER_TABLE_RX_IP;
  2479. table_id <= EFX_FARCH_FILTER_TABLE_RX_DEF;
  2480. table_id++) {
  2481. table = &state->table[table_id];
  2482. for (filter_idx = 0; filter_idx < table->size; filter_idx++) {
  2483. if (!test_bit(filter_idx, table->used_bitmap) ||
  2484. table->spec[filter_idx].dmaq_id >=
  2485. efx->n_rx_channels)
  2486. continue;
  2487. if (efx->rx_scatter)
  2488. table->spec[filter_idx].flags |=
  2489. EFX_FILTER_FLAG_RX_SCATTER;
  2490. else
  2491. table->spec[filter_idx].flags &=
  2492. ~EFX_FILTER_FLAG_RX_SCATTER;
  2493. if (table_id == EFX_FARCH_FILTER_TABLE_RX_DEF)
  2494. /* Pushed by efx_farch_filter_push_rx_config() */
  2495. continue;
  2496. efx_farch_filter_build(&filter, &table->spec[filter_idx]);
  2497. efx_writeo(efx, &filter,
  2498. table->offset + table->step * filter_idx);
  2499. }
  2500. }
  2501. efx_farch_filter_push_rx_config(efx);
  2502. spin_unlock_bh(&efx->filter_lock);
  2503. }
  2504. #ifdef CONFIG_RFS_ACCEL
  2505. s32 efx_farch_filter_rfs_insert(struct efx_nic *efx,
  2506. struct efx_filter_spec *gen_spec)
  2507. {
  2508. return efx_farch_filter_insert(efx, gen_spec, true);
  2509. }
  2510. bool efx_farch_filter_rfs_expire_one(struct efx_nic *efx, u32 flow_id,
  2511. unsigned int index)
  2512. {
  2513. struct efx_farch_filter_state *state = efx->filter_state;
  2514. struct efx_farch_filter_table *table =
  2515. &state->table[EFX_FARCH_FILTER_TABLE_RX_IP];
  2516. if (test_bit(index, table->used_bitmap) &&
  2517. table->spec[index].priority == EFX_FILTER_PRI_HINT &&
  2518. rps_may_expire_flow(efx->net_dev, table->spec[index].dmaq_id,
  2519. flow_id, index)) {
  2520. efx_farch_filter_table_clear_entry(efx, table, index);
  2521. return true;
  2522. }
  2523. return false;
  2524. }
  2525. #endif /* CONFIG_RFS_ACCEL */
  2526. void efx_farch_filter_sync_rx_mode(struct efx_nic *efx)
  2527. {
  2528. struct net_device *net_dev = efx->net_dev;
  2529. struct netdev_hw_addr *ha;
  2530. union efx_multicast_hash *mc_hash = &efx->multicast_hash;
  2531. u32 crc;
  2532. int bit;
  2533. if (!efx_dev_registered(efx))
  2534. return;
  2535. netif_addr_lock_bh(net_dev);
  2536. efx->unicast_filter = !(net_dev->flags & IFF_PROMISC);
  2537. /* Build multicast hash table */
  2538. if (net_dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
  2539. memset(mc_hash, 0xff, sizeof(*mc_hash));
  2540. } else {
  2541. memset(mc_hash, 0x00, sizeof(*mc_hash));
  2542. netdev_for_each_mc_addr(ha, net_dev) {
  2543. crc = ether_crc_le(ETH_ALEN, ha->addr);
  2544. bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
  2545. __set_bit_le(bit, mc_hash);
  2546. }
  2547. /* Broadcast packets go through the multicast hash filter.
  2548. * ether_crc_le() of the broadcast address is 0xbe2612ff
  2549. * so we always add bit 0xff to the mask.
  2550. */
  2551. __set_bit_le(0xff, mc_hash);
  2552. }
  2553. netif_addr_unlock_bh(net_dev);
  2554. }