farch.c 87 KB

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