farch.c 85 KB

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