farch.c 88 KB

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