farch.c 87 KB

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