nic.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2006-2011 Solarflare Communications Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation, incorporated herein by reference.
  9. */
  10. #include <linux/bitops.h>
  11. #include <linux/delay.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/pci.h>
  14. #include <linux/module.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/cpu_rmap.h>
  17. #include "net_driver.h"
  18. #include "bitfield.h"
  19. #include "efx.h"
  20. #include "nic.h"
  21. #include "regs.h"
  22. #include "io.h"
  23. #include "workarounds.h"
  24. /**************************************************************************
  25. *
  26. * Configurable values
  27. *
  28. **************************************************************************
  29. */
  30. /* This is set to 16 for a good reason. In summary, if larger than
  31. * 16, the descriptor cache holds more than a default socket
  32. * buffer's worth of packets (for UDP we can only have at most one
  33. * socket buffer's worth outstanding). This combined with the fact
  34. * that we only get 1 TX event per descriptor cache means the NIC
  35. * goes idle.
  36. */
  37. #define TX_DC_ENTRIES 16
  38. #define TX_DC_ENTRIES_ORDER 1
  39. #define RX_DC_ENTRIES 64
  40. #define RX_DC_ENTRIES_ORDER 3
  41. /* If EFX_MAX_INT_ERRORS internal errors occur within
  42. * EFX_INT_ERROR_EXPIRE seconds, we consider the NIC broken and
  43. * disable it.
  44. */
  45. #define EFX_INT_ERROR_EXPIRE 3600
  46. #define EFX_MAX_INT_ERRORS 5
  47. /* Depth of RX flush request fifo */
  48. #define EFX_RX_FLUSH_COUNT 4
  49. /* Driver generated events */
  50. #define _EFX_CHANNEL_MAGIC_TEST 0x000101
  51. #define _EFX_CHANNEL_MAGIC_FILL 0x000102
  52. #define _EFX_CHANNEL_MAGIC_RX_DRAIN 0x000103
  53. #define _EFX_CHANNEL_MAGIC_TX_DRAIN 0x000104
  54. #define _EFX_CHANNEL_MAGIC(_code, _data) ((_code) << 8 | (_data))
  55. #define _EFX_CHANNEL_MAGIC_CODE(_magic) ((_magic) >> 8)
  56. #define EFX_CHANNEL_MAGIC_TEST(_channel) \
  57. _EFX_CHANNEL_MAGIC(_EFX_CHANNEL_MAGIC_TEST, (_channel)->channel)
  58. #define EFX_CHANNEL_MAGIC_FILL(_rx_queue) \
  59. _EFX_CHANNEL_MAGIC(_EFX_CHANNEL_MAGIC_FILL, \
  60. efx_rx_queue_index(_rx_queue))
  61. #define EFX_CHANNEL_MAGIC_RX_DRAIN(_rx_queue) \
  62. _EFX_CHANNEL_MAGIC(_EFX_CHANNEL_MAGIC_RX_DRAIN, \
  63. efx_rx_queue_index(_rx_queue))
  64. #define EFX_CHANNEL_MAGIC_TX_DRAIN(_tx_queue) \
  65. _EFX_CHANNEL_MAGIC(_EFX_CHANNEL_MAGIC_TX_DRAIN, \
  66. (_tx_queue)->queue)
  67. static void efx_magic_event(struct efx_channel *channel, u32 magic);
  68. /**************************************************************************
  69. *
  70. * Solarstorm hardware access
  71. *
  72. **************************************************************************/
  73. static inline void efx_write_buf_tbl(struct efx_nic *efx, efx_qword_t *value,
  74. unsigned int index)
  75. {
  76. efx_sram_writeq(efx, efx->membase + efx->type->buf_tbl_base,
  77. value, index);
  78. }
  79. /* Read the current event from the event queue */
  80. static inline efx_qword_t *efx_event(struct efx_channel *channel,
  81. unsigned int index)
  82. {
  83. return ((efx_qword_t *) (channel->eventq.addr)) +
  84. (index & channel->eventq_mask);
  85. }
  86. /* See if an event is present
  87. *
  88. * We check both the high and low dword of the event for all ones. We
  89. * wrote all ones when we cleared the event, and no valid event can
  90. * have all ones in either its high or low dwords. This approach is
  91. * robust against reordering.
  92. *
  93. * Note that using a single 64-bit comparison is incorrect; even
  94. * though the CPU read will be atomic, the DMA write may not be.
  95. */
  96. static inline int efx_event_present(efx_qword_t *event)
  97. {
  98. return !(EFX_DWORD_IS_ALL_ONES(event->dword[0]) |
  99. EFX_DWORD_IS_ALL_ONES(event->dword[1]));
  100. }
  101. static bool efx_masked_compare_oword(const efx_oword_t *a, const efx_oword_t *b,
  102. const efx_oword_t *mask)
  103. {
  104. return ((a->u64[0] ^ b->u64[0]) & mask->u64[0]) ||
  105. ((a->u64[1] ^ b->u64[1]) & mask->u64[1]);
  106. }
  107. int efx_nic_test_registers(struct efx_nic *efx,
  108. const struct efx_nic_register_test *regs,
  109. size_t n_regs)
  110. {
  111. unsigned address = 0, i, j;
  112. efx_oword_t mask, imask, original, reg, buf;
  113. for (i = 0; i < n_regs; ++i) {
  114. address = regs[i].address;
  115. mask = imask = regs[i].mask;
  116. EFX_INVERT_OWORD(imask);
  117. efx_reado(efx, &original, address);
  118. /* bit sweep on and off */
  119. for (j = 0; j < 128; j++) {
  120. if (!EFX_EXTRACT_OWORD32(mask, j, j))
  121. continue;
  122. /* Test this testable bit can be set in isolation */
  123. EFX_AND_OWORD(reg, original, mask);
  124. EFX_SET_OWORD32(reg, j, j, 1);
  125. efx_writeo(efx, &reg, address);
  126. efx_reado(efx, &buf, address);
  127. if (efx_masked_compare_oword(&reg, &buf, &mask))
  128. goto fail;
  129. /* Test this testable bit can be cleared in isolation */
  130. EFX_OR_OWORD(reg, original, mask);
  131. EFX_SET_OWORD32(reg, j, j, 0);
  132. efx_writeo(efx, &reg, address);
  133. efx_reado(efx, &buf, address);
  134. if (efx_masked_compare_oword(&reg, &buf, &mask))
  135. goto fail;
  136. }
  137. efx_writeo(efx, &original, address);
  138. }
  139. return 0;
  140. fail:
  141. netif_err(efx, hw, efx->net_dev,
  142. "wrote "EFX_OWORD_FMT" read "EFX_OWORD_FMT
  143. " at address 0x%x mask "EFX_OWORD_FMT"\n", EFX_OWORD_VAL(reg),
  144. EFX_OWORD_VAL(buf), address, EFX_OWORD_VAL(mask));
  145. return -EIO;
  146. }
  147. /**************************************************************************
  148. *
  149. * Special buffer handling
  150. * Special buffers are used for event queues and the TX and RX
  151. * descriptor rings.
  152. *
  153. *************************************************************************/
  154. /*
  155. * Initialise a special buffer
  156. *
  157. * This will define a buffer (previously allocated via
  158. * efx_alloc_special_buffer()) in the buffer table, allowing
  159. * it to be used for event queues, descriptor rings etc.
  160. */
  161. static void
  162. efx_init_special_buffer(struct efx_nic *efx, struct efx_special_buffer *buffer)
  163. {
  164. efx_qword_t buf_desc;
  165. unsigned int index;
  166. dma_addr_t dma_addr;
  167. int i;
  168. EFX_BUG_ON_PARANOID(!buffer->addr);
  169. /* Write buffer descriptors to NIC */
  170. for (i = 0; i < buffer->entries; i++) {
  171. index = buffer->index + i;
  172. dma_addr = buffer->dma_addr + (i * EFX_BUF_SIZE);
  173. netif_dbg(efx, probe, efx->net_dev,
  174. "mapping special buffer %d at %llx\n",
  175. index, (unsigned long long)dma_addr);
  176. EFX_POPULATE_QWORD_3(buf_desc,
  177. FRF_AZ_BUF_ADR_REGION, 0,
  178. FRF_AZ_BUF_ADR_FBUF, dma_addr >> 12,
  179. FRF_AZ_BUF_OWNER_ID_FBUF, 0);
  180. efx_write_buf_tbl(efx, &buf_desc, index);
  181. }
  182. }
  183. /* Unmaps a buffer and clears the buffer table entries */
  184. static void
  185. efx_fini_special_buffer(struct efx_nic *efx, struct efx_special_buffer *buffer)
  186. {
  187. efx_oword_t buf_tbl_upd;
  188. unsigned int start = buffer->index;
  189. unsigned int end = (buffer->index + buffer->entries - 1);
  190. if (!buffer->entries)
  191. return;
  192. netif_dbg(efx, hw, efx->net_dev, "unmapping special buffers %d-%d\n",
  193. buffer->index, buffer->index + buffer->entries - 1);
  194. EFX_POPULATE_OWORD_4(buf_tbl_upd,
  195. FRF_AZ_BUF_UPD_CMD, 0,
  196. FRF_AZ_BUF_CLR_CMD, 1,
  197. FRF_AZ_BUF_CLR_END_ID, end,
  198. FRF_AZ_BUF_CLR_START_ID, start);
  199. efx_writeo(efx, &buf_tbl_upd, FR_AZ_BUF_TBL_UPD);
  200. }
  201. /*
  202. * Allocate a new special buffer
  203. *
  204. * This allocates memory for a new buffer, clears it and allocates a
  205. * new buffer ID range. It does not write into the buffer table.
  206. *
  207. * This call will allocate 4KB buffers, since 8KB buffers can't be
  208. * used for event queues and descriptor rings.
  209. */
  210. static int efx_alloc_special_buffer(struct efx_nic *efx,
  211. struct efx_special_buffer *buffer,
  212. unsigned int len)
  213. {
  214. len = ALIGN(len, EFX_BUF_SIZE);
  215. buffer->addr = dma_alloc_coherent(&efx->pci_dev->dev, len,
  216. &buffer->dma_addr, GFP_KERNEL);
  217. if (!buffer->addr)
  218. return -ENOMEM;
  219. buffer->len = len;
  220. buffer->entries = len / EFX_BUF_SIZE;
  221. BUG_ON(buffer->dma_addr & (EFX_BUF_SIZE - 1));
  222. /* Select new buffer ID */
  223. buffer->index = efx->next_buffer_table;
  224. efx->next_buffer_table += buffer->entries;
  225. #ifdef CONFIG_SFC_SRIOV
  226. BUG_ON(efx_sriov_enabled(efx) &&
  227. efx->vf_buftbl_base < efx->next_buffer_table);
  228. #endif
  229. netif_dbg(efx, probe, efx->net_dev,
  230. "allocating special buffers %d-%d at %llx+%x "
  231. "(virt %p phys %llx)\n", buffer->index,
  232. buffer->index + buffer->entries - 1,
  233. (u64)buffer->dma_addr, len,
  234. buffer->addr, (u64)virt_to_phys(buffer->addr));
  235. return 0;
  236. }
  237. static void
  238. efx_free_special_buffer(struct efx_nic *efx, struct efx_special_buffer *buffer)
  239. {
  240. if (!buffer->addr)
  241. return;
  242. netif_dbg(efx, hw, efx->net_dev,
  243. "deallocating special buffers %d-%d at %llx+%x "
  244. "(virt %p phys %llx)\n", buffer->index,
  245. buffer->index + buffer->entries - 1,
  246. (u64)buffer->dma_addr, buffer->len,
  247. buffer->addr, (u64)virt_to_phys(buffer->addr));
  248. dma_free_coherent(&efx->pci_dev->dev, buffer->len, buffer->addr,
  249. buffer->dma_addr);
  250. buffer->addr = NULL;
  251. buffer->entries = 0;
  252. }
  253. /**************************************************************************
  254. *
  255. * Generic buffer handling
  256. * These buffers are used for interrupt status, MAC stats, etc.
  257. *
  258. **************************************************************************/
  259. int efx_nic_alloc_buffer(struct efx_nic *efx, struct efx_buffer *buffer,
  260. unsigned int len)
  261. {
  262. buffer->addr = dma_alloc_coherent(&efx->pci_dev->dev, len,
  263. &buffer->dma_addr,
  264. GFP_ATOMIC | __GFP_ZERO);
  265. if (!buffer->addr)
  266. return -ENOMEM;
  267. buffer->len = len;
  268. return 0;
  269. }
  270. void efx_nic_free_buffer(struct efx_nic *efx, struct efx_buffer *buffer)
  271. {
  272. if (buffer->addr) {
  273. dma_free_coherent(&efx->pci_dev->dev, buffer->len,
  274. buffer->addr, buffer->dma_addr);
  275. buffer->addr = NULL;
  276. }
  277. }
  278. /**************************************************************************
  279. *
  280. * TX path
  281. *
  282. **************************************************************************/
  283. /* Returns a pointer to the specified transmit descriptor in the TX
  284. * descriptor queue belonging to the specified channel.
  285. */
  286. static inline efx_qword_t *
  287. efx_tx_desc(struct efx_tx_queue *tx_queue, unsigned int index)
  288. {
  289. return ((efx_qword_t *) (tx_queue->txd.addr)) + index;
  290. }
  291. /* This writes to the TX_DESC_WPTR; write pointer for TX descriptor ring */
  292. static inline void efx_notify_tx_desc(struct efx_tx_queue *tx_queue)
  293. {
  294. unsigned write_ptr;
  295. efx_dword_t reg;
  296. write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
  297. EFX_POPULATE_DWORD_1(reg, FRF_AZ_TX_DESC_WPTR_DWORD, write_ptr);
  298. efx_writed_page(tx_queue->efx, &reg,
  299. FR_AZ_TX_DESC_UPD_DWORD_P0, tx_queue->queue);
  300. }
  301. /* Write pointer and first descriptor for TX descriptor ring */
  302. static inline void efx_push_tx_desc(struct efx_tx_queue *tx_queue,
  303. const efx_qword_t *txd)
  304. {
  305. unsigned write_ptr;
  306. efx_oword_t reg;
  307. BUILD_BUG_ON(FRF_AZ_TX_DESC_LBN != 0);
  308. BUILD_BUG_ON(FR_AA_TX_DESC_UPD_KER != FR_BZ_TX_DESC_UPD_P0);
  309. write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
  310. EFX_POPULATE_OWORD_2(reg, FRF_AZ_TX_DESC_PUSH_CMD, true,
  311. FRF_AZ_TX_DESC_WPTR, write_ptr);
  312. reg.qword[0] = *txd;
  313. efx_writeo_page(tx_queue->efx, &reg,
  314. FR_BZ_TX_DESC_UPD_P0, tx_queue->queue);
  315. }
  316. static inline bool
  317. efx_may_push_tx_desc(struct efx_tx_queue *tx_queue, unsigned int write_count)
  318. {
  319. unsigned empty_read_count = ACCESS_ONCE(tx_queue->empty_read_count);
  320. if (empty_read_count == 0)
  321. return false;
  322. tx_queue->empty_read_count = 0;
  323. return ((empty_read_count ^ write_count) & ~EFX_EMPTY_COUNT_VALID) == 0
  324. && tx_queue->write_count - write_count == 1;
  325. }
  326. /* For each entry inserted into the software descriptor ring, create a
  327. * descriptor in the hardware TX descriptor ring (in host memory), and
  328. * write a doorbell.
  329. */
  330. void efx_nic_push_buffers(struct efx_tx_queue *tx_queue)
  331. {
  332. struct efx_tx_buffer *buffer;
  333. efx_qword_t *txd;
  334. unsigned write_ptr;
  335. unsigned old_write_count = tx_queue->write_count;
  336. BUG_ON(tx_queue->write_count == tx_queue->insert_count);
  337. do {
  338. write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
  339. buffer = &tx_queue->buffer[write_ptr];
  340. txd = efx_tx_desc(tx_queue, write_ptr);
  341. ++tx_queue->write_count;
  342. /* Create TX descriptor ring entry */
  343. BUILD_BUG_ON(EFX_TX_BUF_CONT != 1);
  344. EFX_POPULATE_QWORD_4(*txd,
  345. FSF_AZ_TX_KER_CONT,
  346. buffer->flags & EFX_TX_BUF_CONT,
  347. FSF_AZ_TX_KER_BYTE_COUNT, buffer->len,
  348. FSF_AZ_TX_KER_BUF_REGION, 0,
  349. FSF_AZ_TX_KER_BUF_ADDR, buffer->dma_addr);
  350. } while (tx_queue->write_count != tx_queue->insert_count);
  351. wmb(); /* Ensure descriptors are written before they are fetched */
  352. if (efx_may_push_tx_desc(tx_queue, old_write_count)) {
  353. txd = efx_tx_desc(tx_queue,
  354. old_write_count & tx_queue->ptr_mask);
  355. efx_push_tx_desc(tx_queue, txd);
  356. ++tx_queue->pushes;
  357. } else {
  358. efx_notify_tx_desc(tx_queue);
  359. }
  360. }
  361. /* Allocate hardware resources for a TX queue */
  362. int efx_nic_probe_tx(struct efx_tx_queue *tx_queue)
  363. {
  364. struct efx_nic *efx = tx_queue->efx;
  365. unsigned entries;
  366. entries = tx_queue->ptr_mask + 1;
  367. return efx_alloc_special_buffer(efx, &tx_queue->txd,
  368. entries * sizeof(efx_qword_t));
  369. }
  370. void efx_nic_init_tx(struct efx_tx_queue *tx_queue)
  371. {
  372. struct efx_nic *efx = tx_queue->efx;
  373. efx_oword_t reg;
  374. /* Pin TX descriptor ring */
  375. efx_init_special_buffer(efx, &tx_queue->txd);
  376. /* Push TX descriptor ring to card */
  377. EFX_POPULATE_OWORD_10(reg,
  378. FRF_AZ_TX_DESCQ_EN, 1,
  379. FRF_AZ_TX_ISCSI_DDIG_EN, 0,
  380. FRF_AZ_TX_ISCSI_HDIG_EN, 0,
  381. FRF_AZ_TX_DESCQ_BUF_BASE_ID, tx_queue->txd.index,
  382. FRF_AZ_TX_DESCQ_EVQ_ID,
  383. tx_queue->channel->channel,
  384. FRF_AZ_TX_DESCQ_OWNER_ID, 0,
  385. FRF_AZ_TX_DESCQ_LABEL, tx_queue->queue,
  386. FRF_AZ_TX_DESCQ_SIZE,
  387. __ffs(tx_queue->txd.entries),
  388. FRF_AZ_TX_DESCQ_TYPE, 0,
  389. FRF_BZ_TX_NON_IP_DROP_DIS, 1);
  390. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0) {
  391. int csum = tx_queue->queue & EFX_TXQ_TYPE_OFFLOAD;
  392. EFX_SET_OWORD_FIELD(reg, FRF_BZ_TX_IP_CHKSM_DIS, !csum);
  393. EFX_SET_OWORD_FIELD(reg, FRF_BZ_TX_TCP_CHKSM_DIS,
  394. !csum);
  395. }
  396. efx_writeo_table(efx, &reg, efx->type->txd_ptr_tbl_base,
  397. tx_queue->queue);
  398. if (efx_nic_rev(efx) < EFX_REV_FALCON_B0) {
  399. /* Only 128 bits in this register */
  400. BUILD_BUG_ON(EFX_MAX_TX_QUEUES > 128);
  401. efx_reado(efx, &reg, FR_AA_TX_CHKSM_CFG);
  402. if (tx_queue->queue & EFX_TXQ_TYPE_OFFLOAD)
  403. __clear_bit_le(tx_queue->queue, &reg);
  404. else
  405. __set_bit_le(tx_queue->queue, &reg);
  406. efx_writeo(efx, &reg, FR_AA_TX_CHKSM_CFG);
  407. }
  408. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0) {
  409. EFX_POPULATE_OWORD_1(reg,
  410. FRF_BZ_TX_PACE,
  411. (tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI) ?
  412. FFE_BZ_TX_PACE_OFF :
  413. FFE_BZ_TX_PACE_RESERVED);
  414. efx_writeo_table(efx, &reg, FR_BZ_TX_PACE_TBL,
  415. tx_queue->queue);
  416. }
  417. }
  418. static void efx_flush_tx_queue(struct efx_tx_queue *tx_queue)
  419. {
  420. struct efx_nic *efx = tx_queue->efx;
  421. efx_oword_t tx_flush_descq;
  422. WARN_ON(atomic_read(&tx_queue->flush_outstanding));
  423. atomic_set(&tx_queue->flush_outstanding, 1);
  424. EFX_POPULATE_OWORD_2(tx_flush_descq,
  425. FRF_AZ_TX_FLUSH_DESCQ_CMD, 1,
  426. FRF_AZ_TX_FLUSH_DESCQ, tx_queue->queue);
  427. efx_writeo(efx, &tx_flush_descq, FR_AZ_TX_FLUSH_DESCQ);
  428. }
  429. void efx_nic_fini_tx(struct efx_tx_queue *tx_queue)
  430. {
  431. struct efx_nic *efx = tx_queue->efx;
  432. efx_oword_t tx_desc_ptr;
  433. /* Remove TX descriptor ring from card */
  434. EFX_ZERO_OWORD(tx_desc_ptr);
  435. efx_writeo_table(efx, &tx_desc_ptr, efx->type->txd_ptr_tbl_base,
  436. tx_queue->queue);
  437. /* Unpin TX descriptor ring */
  438. efx_fini_special_buffer(efx, &tx_queue->txd);
  439. }
  440. /* Free buffers backing TX queue */
  441. void efx_nic_remove_tx(struct efx_tx_queue *tx_queue)
  442. {
  443. efx_free_special_buffer(tx_queue->efx, &tx_queue->txd);
  444. }
  445. /**************************************************************************
  446. *
  447. * RX path
  448. *
  449. **************************************************************************/
  450. /* Returns a pointer to the specified descriptor in the RX descriptor queue */
  451. static inline efx_qword_t *
  452. efx_rx_desc(struct efx_rx_queue *rx_queue, unsigned int index)
  453. {
  454. return ((efx_qword_t *) (rx_queue->rxd.addr)) + index;
  455. }
  456. /* This creates an entry in the RX descriptor queue */
  457. static inline void
  458. efx_build_rx_desc(struct efx_rx_queue *rx_queue, unsigned index)
  459. {
  460. struct efx_rx_buffer *rx_buf;
  461. efx_qword_t *rxd;
  462. rxd = efx_rx_desc(rx_queue, index);
  463. rx_buf = efx_rx_buffer(rx_queue, index);
  464. EFX_POPULATE_QWORD_3(*rxd,
  465. FSF_AZ_RX_KER_BUF_SIZE,
  466. rx_buf->len -
  467. rx_queue->efx->type->rx_buffer_padding,
  468. FSF_AZ_RX_KER_BUF_REGION, 0,
  469. FSF_AZ_RX_KER_BUF_ADDR, rx_buf->dma_addr);
  470. }
  471. /* This writes to the RX_DESC_WPTR register for the specified receive
  472. * descriptor ring.
  473. */
  474. void efx_nic_notify_rx_desc(struct efx_rx_queue *rx_queue)
  475. {
  476. struct efx_nic *efx = rx_queue->efx;
  477. efx_dword_t reg;
  478. unsigned write_ptr;
  479. while (rx_queue->notified_count != rx_queue->added_count) {
  480. efx_build_rx_desc(
  481. rx_queue,
  482. rx_queue->notified_count & rx_queue->ptr_mask);
  483. ++rx_queue->notified_count;
  484. }
  485. wmb();
  486. write_ptr = rx_queue->added_count & rx_queue->ptr_mask;
  487. EFX_POPULATE_DWORD_1(reg, FRF_AZ_RX_DESC_WPTR_DWORD, write_ptr);
  488. efx_writed_page(efx, &reg, FR_AZ_RX_DESC_UPD_DWORD_P0,
  489. efx_rx_queue_index(rx_queue));
  490. }
  491. int efx_nic_probe_rx(struct efx_rx_queue *rx_queue)
  492. {
  493. struct efx_nic *efx = rx_queue->efx;
  494. unsigned entries;
  495. entries = rx_queue->ptr_mask + 1;
  496. return efx_alloc_special_buffer(efx, &rx_queue->rxd,
  497. entries * sizeof(efx_qword_t));
  498. }
  499. void efx_nic_init_rx(struct efx_rx_queue *rx_queue)
  500. {
  501. efx_oword_t rx_desc_ptr;
  502. struct efx_nic *efx = rx_queue->efx;
  503. bool is_b0 = efx_nic_rev(efx) >= EFX_REV_FALCON_B0;
  504. bool iscsi_digest_en = is_b0;
  505. bool jumbo_en;
  506. /* For kernel-mode queues in Falcon A1, the JUMBO flag enables
  507. * DMA to continue after a PCIe page boundary (and scattering
  508. * is not possible). In Falcon B0 and Siena, it enables
  509. * scatter.
  510. */
  511. jumbo_en = !is_b0 || efx->rx_scatter;
  512. netif_dbg(efx, hw, efx->net_dev,
  513. "RX queue %d ring in special buffers %d-%d\n",
  514. efx_rx_queue_index(rx_queue), rx_queue->rxd.index,
  515. rx_queue->rxd.index + rx_queue->rxd.entries - 1);
  516. rx_queue->scatter_n = 0;
  517. /* Pin RX descriptor ring */
  518. efx_init_special_buffer(efx, &rx_queue->rxd);
  519. /* Push RX descriptor ring to card */
  520. EFX_POPULATE_OWORD_10(rx_desc_ptr,
  521. FRF_AZ_RX_ISCSI_DDIG_EN, iscsi_digest_en,
  522. FRF_AZ_RX_ISCSI_HDIG_EN, iscsi_digest_en,
  523. FRF_AZ_RX_DESCQ_BUF_BASE_ID, rx_queue->rxd.index,
  524. FRF_AZ_RX_DESCQ_EVQ_ID,
  525. efx_rx_queue_channel(rx_queue)->channel,
  526. FRF_AZ_RX_DESCQ_OWNER_ID, 0,
  527. FRF_AZ_RX_DESCQ_LABEL,
  528. efx_rx_queue_index(rx_queue),
  529. FRF_AZ_RX_DESCQ_SIZE,
  530. __ffs(rx_queue->rxd.entries),
  531. FRF_AZ_RX_DESCQ_TYPE, 0 /* kernel queue */ ,
  532. FRF_AZ_RX_DESCQ_JUMBO, jumbo_en,
  533. FRF_AZ_RX_DESCQ_EN, 1);
  534. efx_writeo_table(efx, &rx_desc_ptr, efx->type->rxd_ptr_tbl_base,
  535. efx_rx_queue_index(rx_queue));
  536. }
  537. static void efx_flush_rx_queue(struct efx_rx_queue *rx_queue)
  538. {
  539. struct efx_nic *efx = rx_queue->efx;
  540. efx_oword_t rx_flush_descq;
  541. EFX_POPULATE_OWORD_2(rx_flush_descq,
  542. FRF_AZ_RX_FLUSH_DESCQ_CMD, 1,
  543. FRF_AZ_RX_FLUSH_DESCQ,
  544. efx_rx_queue_index(rx_queue));
  545. efx_writeo(efx, &rx_flush_descq, FR_AZ_RX_FLUSH_DESCQ);
  546. }
  547. void efx_nic_fini_rx(struct efx_rx_queue *rx_queue)
  548. {
  549. efx_oword_t rx_desc_ptr;
  550. struct efx_nic *efx = rx_queue->efx;
  551. /* Remove RX descriptor ring from card */
  552. EFX_ZERO_OWORD(rx_desc_ptr);
  553. efx_writeo_table(efx, &rx_desc_ptr, efx->type->rxd_ptr_tbl_base,
  554. efx_rx_queue_index(rx_queue));
  555. /* Unpin RX descriptor ring */
  556. efx_fini_special_buffer(efx, &rx_queue->rxd);
  557. }
  558. /* Free buffers backing RX queue */
  559. void efx_nic_remove_rx(struct efx_rx_queue *rx_queue)
  560. {
  561. efx_free_special_buffer(rx_queue->efx, &rx_queue->rxd);
  562. }
  563. /**************************************************************************
  564. *
  565. * Flush handling
  566. *
  567. **************************************************************************/
  568. /* efx_nic_flush_queues() must be woken up when all flushes are completed,
  569. * or more RX flushes can be kicked off.
  570. */
  571. static bool efx_flush_wake(struct efx_nic *efx)
  572. {
  573. /* Ensure that all updates are visible to efx_nic_flush_queues() */
  574. smp_mb();
  575. return (atomic_read(&efx->drain_pending) == 0 ||
  576. (atomic_read(&efx->rxq_flush_outstanding) < EFX_RX_FLUSH_COUNT
  577. && atomic_read(&efx->rxq_flush_pending) > 0));
  578. }
  579. static bool efx_check_tx_flush_complete(struct efx_nic *efx)
  580. {
  581. bool i = true;
  582. efx_oword_t txd_ptr_tbl;
  583. struct efx_channel *channel;
  584. struct efx_tx_queue *tx_queue;
  585. efx_for_each_channel(channel, efx) {
  586. efx_for_each_channel_tx_queue(tx_queue, channel) {
  587. efx_reado_table(efx, &txd_ptr_tbl,
  588. FR_BZ_TX_DESC_PTR_TBL, tx_queue->queue);
  589. if (EFX_OWORD_FIELD(txd_ptr_tbl,
  590. FRF_AZ_TX_DESCQ_FLUSH) ||
  591. EFX_OWORD_FIELD(txd_ptr_tbl,
  592. FRF_AZ_TX_DESCQ_EN)) {
  593. netif_dbg(efx, hw, efx->net_dev,
  594. "flush did not complete on TXQ %d\n",
  595. tx_queue->queue);
  596. i = false;
  597. } else if (atomic_cmpxchg(&tx_queue->flush_outstanding,
  598. 1, 0)) {
  599. /* The flush is complete, but we didn't
  600. * receive a flush completion event
  601. */
  602. netif_dbg(efx, hw, efx->net_dev,
  603. "flush complete on TXQ %d, so drain "
  604. "the queue\n", tx_queue->queue);
  605. /* Don't need to increment drain_pending as it
  606. * has already been incremented for the queues
  607. * which did not drain
  608. */
  609. efx_magic_event(channel,
  610. EFX_CHANNEL_MAGIC_TX_DRAIN(
  611. tx_queue));
  612. }
  613. }
  614. }
  615. return i;
  616. }
  617. /* Flush all the transmit queues, and continue flushing receive queues until
  618. * they're all flushed. Wait for the DRAIN events to be recieved so that there
  619. * are no more RX and TX events left on any channel. */
  620. int efx_nic_flush_queues(struct efx_nic *efx)
  621. {
  622. unsigned timeout = msecs_to_jiffies(5000); /* 5s for all flushes and drains */
  623. struct efx_channel *channel;
  624. struct efx_rx_queue *rx_queue;
  625. struct efx_tx_queue *tx_queue;
  626. int rc = 0;
  627. efx->type->prepare_flush(efx);
  628. efx_for_each_channel(channel, efx) {
  629. efx_for_each_channel_tx_queue(tx_queue, channel) {
  630. atomic_inc(&efx->drain_pending);
  631. efx_flush_tx_queue(tx_queue);
  632. }
  633. efx_for_each_channel_rx_queue(rx_queue, channel) {
  634. atomic_inc(&efx->drain_pending);
  635. rx_queue->flush_pending = true;
  636. atomic_inc(&efx->rxq_flush_pending);
  637. }
  638. }
  639. while (timeout && atomic_read(&efx->drain_pending) > 0) {
  640. /* If SRIOV is enabled, then offload receive queue flushing to
  641. * the firmware (though we will still have to poll for
  642. * completion). If that fails, fall back to the old scheme.
  643. */
  644. if (efx_sriov_enabled(efx)) {
  645. rc = efx_mcdi_flush_rxqs(efx);
  646. if (!rc)
  647. goto wait;
  648. }
  649. /* The hardware supports four concurrent rx flushes, each of
  650. * which may need to be retried if there is an outstanding
  651. * descriptor fetch
  652. */
  653. efx_for_each_channel(channel, efx) {
  654. efx_for_each_channel_rx_queue(rx_queue, channel) {
  655. if (atomic_read(&efx->rxq_flush_outstanding) >=
  656. EFX_RX_FLUSH_COUNT)
  657. break;
  658. if (rx_queue->flush_pending) {
  659. rx_queue->flush_pending = false;
  660. atomic_dec(&efx->rxq_flush_pending);
  661. atomic_inc(&efx->rxq_flush_outstanding);
  662. efx_flush_rx_queue(rx_queue);
  663. }
  664. }
  665. }
  666. wait:
  667. timeout = wait_event_timeout(efx->flush_wq, efx_flush_wake(efx),
  668. timeout);
  669. }
  670. if (atomic_read(&efx->drain_pending) &&
  671. !efx_check_tx_flush_complete(efx)) {
  672. netif_err(efx, hw, efx->net_dev, "failed to flush %d queues "
  673. "(rx %d+%d)\n", atomic_read(&efx->drain_pending),
  674. atomic_read(&efx->rxq_flush_outstanding),
  675. atomic_read(&efx->rxq_flush_pending));
  676. rc = -ETIMEDOUT;
  677. atomic_set(&efx->drain_pending, 0);
  678. atomic_set(&efx->rxq_flush_pending, 0);
  679. atomic_set(&efx->rxq_flush_outstanding, 0);
  680. }
  681. efx->type->finish_flush(efx);
  682. return rc;
  683. }
  684. /**************************************************************************
  685. *
  686. * Event queue processing
  687. * Event queues are processed by per-channel tasklets.
  688. *
  689. **************************************************************************/
  690. /* Update a channel's event queue's read pointer (RPTR) register
  691. *
  692. * This writes the EVQ_RPTR_REG register for the specified channel's
  693. * event queue.
  694. */
  695. void efx_nic_eventq_read_ack(struct efx_channel *channel)
  696. {
  697. efx_dword_t reg;
  698. struct efx_nic *efx = channel->efx;
  699. EFX_POPULATE_DWORD_1(reg, FRF_AZ_EVQ_RPTR,
  700. channel->eventq_read_ptr & channel->eventq_mask);
  701. /* For Falcon A1, EVQ_RPTR_KER is documented as having a step size
  702. * of 4 bytes, but it is really 16 bytes just like later revisions.
  703. */
  704. efx_writed(efx, &reg,
  705. efx->type->evq_rptr_tbl_base +
  706. FR_BZ_EVQ_RPTR_STEP * channel->channel);
  707. }
  708. /* Use HW to insert a SW defined event */
  709. void efx_generate_event(struct efx_nic *efx, unsigned int evq,
  710. efx_qword_t *event)
  711. {
  712. efx_oword_t drv_ev_reg;
  713. BUILD_BUG_ON(FRF_AZ_DRV_EV_DATA_LBN != 0 ||
  714. FRF_AZ_DRV_EV_DATA_WIDTH != 64);
  715. drv_ev_reg.u32[0] = event->u32[0];
  716. drv_ev_reg.u32[1] = event->u32[1];
  717. drv_ev_reg.u32[2] = 0;
  718. drv_ev_reg.u32[3] = 0;
  719. EFX_SET_OWORD_FIELD(drv_ev_reg, FRF_AZ_DRV_EV_QID, evq);
  720. efx_writeo(efx, &drv_ev_reg, FR_AZ_DRV_EV);
  721. }
  722. static void efx_magic_event(struct efx_channel *channel, u32 magic)
  723. {
  724. efx_qword_t event;
  725. EFX_POPULATE_QWORD_2(event, FSF_AZ_EV_CODE,
  726. FSE_AZ_EV_CODE_DRV_GEN_EV,
  727. FSF_AZ_DRV_GEN_EV_MAGIC, magic);
  728. efx_generate_event(channel->efx, channel->channel, &event);
  729. }
  730. /* Handle a transmit completion event
  731. *
  732. * The NIC batches TX completion events; the message we receive is of
  733. * the form "complete all TX events up to this index".
  734. */
  735. static int
  736. efx_handle_tx_event(struct efx_channel *channel, efx_qword_t *event)
  737. {
  738. unsigned int tx_ev_desc_ptr;
  739. unsigned int tx_ev_q_label;
  740. struct efx_tx_queue *tx_queue;
  741. struct efx_nic *efx = channel->efx;
  742. int tx_packets = 0;
  743. if (unlikely(ACCESS_ONCE(efx->reset_pending)))
  744. return 0;
  745. if (likely(EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_COMP))) {
  746. /* Transmit completion */
  747. tx_ev_desc_ptr = EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_DESC_PTR);
  748. tx_ev_q_label = EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_Q_LABEL);
  749. tx_queue = efx_channel_get_tx_queue(
  750. channel, tx_ev_q_label % EFX_TXQ_TYPES);
  751. tx_packets = ((tx_ev_desc_ptr - tx_queue->read_count) &
  752. tx_queue->ptr_mask);
  753. efx_xmit_done(tx_queue, tx_ev_desc_ptr);
  754. } else if (EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_WQ_FF_FULL)) {
  755. /* Rewrite the FIFO write pointer */
  756. tx_ev_q_label = EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_Q_LABEL);
  757. tx_queue = efx_channel_get_tx_queue(
  758. channel, tx_ev_q_label % EFX_TXQ_TYPES);
  759. netif_tx_lock(efx->net_dev);
  760. efx_notify_tx_desc(tx_queue);
  761. netif_tx_unlock(efx->net_dev);
  762. } else if (EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_PKT_ERR) &&
  763. EFX_WORKAROUND_10727(efx)) {
  764. efx_schedule_reset(efx, RESET_TYPE_TX_DESC_FETCH);
  765. } else {
  766. netif_err(efx, tx_err, efx->net_dev,
  767. "channel %d unexpected TX event "
  768. EFX_QWORD_FMT"\n", channel->channel,
  769. EFX_QWORD_VAL(*event));
  770. }
  771. return tx_packets;
  772. }
  773. /* Detect errors included in the rx_evt_pkt_ok bit. */
  774. static u16 efx_handle_rx_not_ok(struct efx_rx_queue *rx_queue,
  775. const efx_qword_t *event)
  776. {
  777. struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
  778. struct efx_nic *efx = rx_queue->efx;
  779. bool rx_ev_buf_owner_id_err, rx_ev_ip_hdr_chksum_err;
  780. bool rx_ev_tcp_udp_chksum_err, rx_ev_eth_crc_err;
  781. bool rx_ev_frm_trunc, rx_ev_drib_nib, rx_ev_tobe_disc;
  782. bool rx_ev_other_err, rx_ev_pause_frm;
  783. bool rx_ev_hdr_type, rx_ev_mcast_pkt;
  784. unsigned rx_ev_pkt_type;
  785. rx_ev_hdr_type = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_HDR_TYPE);
  786. rx_ev_mcast_pkt = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_MCAST_PKT);
  787. rx_ev_tobe_disc = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_TOBE_DISC);
  788. rx_ev_pkt_type = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_PKT_TYPE);
  789. rx_ev_buf_owner_id_err = EFX_QWORD_FIELD(*event,
  790. FSF_AZ_RX_EV_BUF_OWNER_ID_ERR);
  791. rx_ev_ip_hdr_chksum_err = EFX_QWORD_FIELD(*event,
  792. FSF_AZ_RX_EV_IP_HDR_CHKSUM_ERR);
  793. rx_ev_tcp_udp_chksum_err = EFX_QWORD_FIELD(*event,
  794. FSF_AZ_RX_EV_TCP_UDP_CHKSUM_ERR);
  795. rx_ev_eth_crc_err = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_ETH_CRC_ERR);
  796. rx_ev_frm_trunc = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_FRM_TRUNC);
  797. rx_ev_drib_nib = ((efx_nic_rev(efx) >= EFX_REV_FALCON_B0) ?
  798. 0 : EFX_QWORD_FIELD(*event, FSF_AA_RX_EV_DRIB_NIB));
  799. rx_ev_pause_frm = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_PAUSE_FRM_ERR);
  800. /* Every error apart from tobe_disc and pause_frm */
  801. rx_ev_other_err = (rx_ev_drib_nib | rx_ev_tcp_udp_chksum_err |
  802. rx_ev_buf_owner_id_err | rx_ev_eth_crc_err |
  803. rx_ev_frm_trunc | rx_ev_ip_hdr_chksum_err);
  804. /* Count errors that are not in MAC stats. Ignore expected
  805. * checksum errors during self-test. */
  806. if (rx_ev_frm_trunc)
  807. ++channel->n_rx_frm_trunc;
  808. else if (rx_ev_tobe_disc)
  809. ++channel->n_rx_tobe_disc;
  810. else if (!efx->loopback_selftest) {
  811. if (rx_ev_ip_hdr_chksum_err)
  812. ++channel->n_rx_ip_hdr_chksum_err;
  813. else if (rx_ev_tcp_udp_chksum_err)
  814. ++channel->n_rx_tcp_udp_chksum_err;
  815. }
  816. /* TOBE_DISC is expected on unicast mismatches; don't print out an
  817. * error message. FRM_TRUNC indicates RXDP dropped the packet due
  818. * to a FIFO overflow.
  819. */
  820. #ifdef DEBUG
  821. if (rx_ev_other_err && net_ratelimit()) {
  822. netif_dbg(efx, rx_err, efx->net_dev,
  823. " RX queue %d unexpected RX event "
  824. EFX_QWORD_FMT "%s%s%s%s%s%s%s%s\n",
  825. efx_rx_queue_index(rx_queue), EFX_QWORD_VAL(*event),
  826. rx_ev_buf_owner_id_err ? " [OWNER_ID_ERR]" : "",
  827. rx_ev_ip_hdr_chksum_err ?
  828. " [IP_HDR_CHKSUM_ERR]" : "",
  829. rx_ev_tcp_udp_chksum_err ?
  830. " [TCP_UDP_CHKSUM_ERR]" : "",
  831. rx_ev_eth_crc_err ? " [ETH_CRC_ERR]" : "",
  832. rx_ev_frm_trunc ? " [FRM_TRUNC]" : "",
  833. rx_ev_drib_nib ? " [DRIB_NIB]" : "",
  834. rx_ev_tobe_disc ? " [TOBE_DISC]" : "",
  835. rx_ev_pause_frm ? " [PAUSE]" : "");
  836. }
  837. #endif
  838. /* The frame must be discarded if any of these are true. */
  839. return (rx_ev_eth_crc_err | rx_ev_frm_trunc | rx_ev_drib_nib |
  840. rx_ev_tobe_disc | rx_ev_pause_frm) ?
  841. EFX_RX_PKT_DISCARD : 0;
  842. }
  843. /* Handle receive events that are not in-order. Return true if this
  844. * can be handled as a partial packet discard, false if it's more
  845. * serious.
  846. */
  847. static bool
  848. efx_handle_rx_bad_index(struct efx_rx_queue *rx_queue, unsigned index)
  849. {
  850. struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
  851. struct efx_nic *efx = rx_queue->efx;
  852. unsigned expected, dropped;
  853. if (rx_queue->scatter_n &&
  854. index == ((rx_queue->removed_count + rx_queue->scatter_n - 1) &
  855. rx_queue->ptr_mask)) {
  856. ++channel->n_rx_nodesc_trunc;
  857. return true;
  858. }
  859. expected = rx_queue->removed_count & rx_queue->ptr_mask;
  860. dropped = (index - expected) & rx_queue->ptr_mask;
  861. netif_info(efx, rx_err, efx->net_dev,
  862. "dropped %d events (index=%d expected=%d)\n",
  863. dropped, index, expected);
  864. efx_schedule_reset(efx, EFX_WORKAROUND_5676(efx) ?
  865. RESET_TYPE_RX_RECOVERY : RESET_TYPE_DISABLE);
  866. return false;
  867. }
  868. /* Handle a packet received event
  869. *
  870. * The NIC gives a "discard" flag if it's a unicast packet with the
  871. * wrong destination address
  872. * Also "is multicast" and "matches multicast filter" flags can be used to
  873. * discard non-matching multicast packets.
  874. */
  875. static void
  876. efx_handle_rx_event(struct efx_channel *channel, const efx_qword_t *event)
  877. {
  878. unsigned int rx_ev_desc_ptr, rx_ev_byte_cnt;
  879. unsigned int rx_ev_hdr_type, rx_ev_mcast_pkt;
  880. unsigned expected_ptr;
  881. bool rx_ev_pkt_ok, rx_ev_sop, rx_ev_cont;
  882. u16 flags;
  883. struct efx_rx_queue *rx_queue;
  884. struct efx_nic *efx = channel->efx;
  885. if (unlikely(ACCESS_ONCE(efx->reset_pending)))
  886. return;
  887. rx_ev_cont = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_JUMBO_CONT);
  888. rx_ev_sop = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_SOP);
  889. WARN_ON(EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_Q_LABEL) !=
  890. channel->channel);
  891. rx_queue = efx_channel_get_rx_queue(channel);
  892. rx_ev_desc_ptr = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_DESC_PTR);
  893. expected_ptr = ((rx_queue->removed_count + rx_queue->scatter_n) &
  894. rx_queue->ptr_mask);
  895. /* Check for partial drops and other errors */
  896. if (unlikely(rx_ev_desc_ptr != expected_ptr) ||
  897. unlikely(rx_ev_sop != (rx_queue->scatter_n == 0))) {
  898. if (rx_ev_desc_ptr != expected_ptr &&
  899. !efx_handle_rx_bad_index(rx_queue, rx_ev_desc_ptr))
  900. return;
  901. /* Discard all pending fragments */
  902. if (rx_queue->scatter_n) {
  903. efx_rx_packet(
  904. rx_queue,
  905. rx_queue->removed_count & rx_queue->ptr_mask,
  906. rx_queue->scatter_n, 0, EFX_RX_PKT_DISCARD);
  907. rx_queue->removed_count += rx_queue->scatter_n;
  908. rx_queue->scatter_n = 0;
  909. }
  910. /* Return if there is no new fragment */
  911. if (rx_ev_desc_ptr != expected_ptr)
  912. return;
  913. /* Discard new fragment if not SOP */
  914. if (!rx_ev_sop) {
  915. efx_rx_packet(
  916. rx_queue,
  917. rx_queue->removed_count & rx_queue->ptr_mask,
  918. 1, 0, EFX_RX_PKT_DISCARD);
  919. ++rx_queue->removed_count;
  920. return;
  921. }
  922. }
  923. ++rx_queue->scatter_n;
  924. if (rx_ev_cont)
  925. return;
  926. rx_ev_byte_cnt = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_BYTE_CNT);
  927. rx_ev_pkt_ok = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_PKT_OK);
  928. rx_ev_hdr_type = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_HDR_TYPE);
  929. if (likely(rx_ev_pkt_ok)) {
  930. /* If packet is marked as OK then we can rely on the
  931. * hardware checksum and classification.
  932. */
  933. flags = 0;
  934. switch (rx_ev_hdr_type) {
  935. case FSE_CZ_RX_EV_HDR_TYPE_IPV4V6_TCP:
  936. flags |= EFX_RX_PKT_TCP;
  937. /* fall through */
  938. case FSE_CZ_RX_EV_HDR_TYPE_IPV4V6_UDP:
  939. flags |= EFX_RX_PKT_CSUMMED;
  940. /* fall through */
  941. case FSE_CZ_RX_EV_HDR_TYPE_IPV4V6_OTHER:
  942. case FSE_AZ_RX_EV_HDR_TYPE_OTHER:
  943. break;
  944. }
  945. } else {
  946. flags = efx_handle_rx_not_ok(rx_queue, event);
  947. }
  948. /* Detect multicast packets that didn't match the filter */
  949. rx_ev_mcast_pkt = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_MCAST_PKT);
  950. if (rx_ev_mcast_pkt) {
  951. unsigned int rx_ev_mcast_hash_match =
  952. EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_MCAST_HASH_MATCH);
  953. if (unlikely(!rx_ev_mcast_hash_match)) {
  954. ++channel->n_rx_mcast_mismatch;
  955. flags |= EFX_RX_PKT_DISCARD;
  956. }
  957. }
  958. channel->irq_mod_score += 2;
  959. /* Handle received packet */
  960. efx_rx_packet(rx_queue,
  961. rx_queue->removed_count & rx_queue->ptr_mask,
  962. rx_queue->scatter_n, rx_ev_byte_cnt, flags);
  963. rx_queue->removed_count += rx_queue->scatter_n;
  964. rx_queue->scatter_n = 0;
  965. }
  966. /* If this flush done event corresponds to a &struct efx_tx_queue, then
  967. * send an %EFX_CHANNEL_MAGIC_TX_DRAIN event to drain the event queue
  968. * of all transmit completions.
  969. */
  970. static void
  971. efx_handle_tx_flush_done(struct efx_nic *efx, efx_qword_t *event)
  972. {
  973. struct efx_tx_queue *tx_queue;
  974. int qid;
  975. qid = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_SUBDATA);
  976. if (qid < EFX_TXQ_TYPES * efx->n_tx_channels) {
  977. tx_queue = efx_get_tx_queue(efx, qid / EFX_TXQ_TYPES,
  978. qid % EFX_TXQ_TYPES);
  979. if (atomic_cmpxchg(&tx_queue->flush_outstanding, 1, 0)) {
  980. efx_magic_event(tx_queue->channel,
  981. EFX_CHANNEL_MAGIC_TX_DRAIN(tx_queue));
  982. }
  983. }
  984. }
  985. /* If this flush done event corresponds to a &struct efx_rx_queue: If the flush
  986. * was succesful then send an %EFX_CHANNEL_MAGIC_RX_DRAIN, otherwise add
  987. * the RX queue back to the mask of RX queues in need of flushing.
  988. */
  989. static void
  990. efx_handle_rx_flush_done(struct efx_nic *efx, efx_qword_t *event)
  991. {
  992. struct efx_channel *channel;
  993. struct efx_rx_queue *rx_queue;
  994. int qid;
  995. bool failed;
  996. qid = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_RX_DESCQ_ID);
  997. failed = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_RX_FLUSH_FAIL);
  998. if (qid >= efx->n_channels)
  999. return;
  1000. channel = efx_get_channel(efx, qid);
  1001. if (!efx_channel_has_rx_queue(channel))
  1002. return;
  1003. rx_queue = efx_channel_get_rx_queue(channel);
  1004. if (failed) {
  1005. netif_info(efx, hw, efx->net_dev,
  1006. "RXQ %d flush retry\n", qid);
  1007. rx_queue->flush_pending = true;
  1008. atomic_inc(&efx->rxq_flush_pending);
  1009. } else {
  1010. efx_magic_event(efx_rx_queue_channel(rx_queue),
  1011. EFX_CHANNEL_MAGIC_RX_DRAIN(rx_queue));
  1012. }
  1013. atomic_dec(&efx->rxq_flush_outstanding);
  1014. if (efx_flush_wake(efx))
  1015. wake_up(&efx->flush_wq);
  1016. }
  1017. static void
  1018. efx_handle_drain_event(struct efx_channel *channel)
  1019. {
  1020. struct efx_nic *efx = channel->efx;
  1021. WARN_ON(atomic_read(&efx->drain_pending) == 0);
  1022. atomic_dec(&efx->drain_pending);
  1023. if (efx_flush_wake(efx))
  1024. wake_up(&efx->flush_wq);
  1025. }
  1026. static void
  1027. efx_handle_generated_event(struct efx_channel *channel, efx_qword_t *event)
  1028. {
  1029. struct efx_nic *efx = channel->efx;
  1030. struct efx_rx_queue *rx_queue =
  1031. efx_channel_has_rx_queue(channel) ?
  1032. efx_channel_get_rx_queue(channel) : NULL;
  1033. unsigned magic, code;
  1034. magic = EFX_QWORD_FIELD(*event, FSF_AZ_DRV_GEN_EV_MAGIC);
  1035. code = _EFX_CHANNEL_MAGIC_CODE(magic);
  1036. if (magic == EFX_CHANNEL_MAGIC_TEST(channel)) {
  1037. channel->event_test_cpu = raw_smp_processor_id();
  1038. } else if (rx_queue && magic == EFX_CHANNEL_MAGIC_FILL(rx_queue)) {
  1039. /* The queue must be empty, so we won't receive any rx
  1040. * events, so efx_process_channel() won't refill the
  1041. * queue. Refill it here */
  1042. efx_fast_push_rx_descriptors(rx_queue);
  1043. } else if (rx_queue && magic == EFX_CHANNEL_MAGIC_RX_DRAIN(rx_queue)) {
  1044. rx_queue->enabled = false;
  1045. efx_handle_drain_event(channel);
  1046. } else if (code == _EFX_CHANNEL_MAGIC_TX_DRAIN) {
  1047. efx_handle_drain_event(channel);
  1048. } else {
  1049. netif_dbg(efx, hw, efx->net_dev, "channel %d received "
  1050. "generated event "EFX_QWORD_FMT"\n",
  1051. channel->channel, EFX_QWORD_VAL(*event));
  1052. }
  1053. }
  1054. static void
  1055. efx_handle_driver_event(struct efx_channel *channel, efx_qword_t *event)
  1056. {
  1057. struct efx_nic *efx = channel->efx;
  1058. unsigned int ev_sub_code;
  1059. unsigned int ev_sub_data;
  1060. ev_sub_code = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_SUBCODE);
  1061. ev_sub_data = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_SUBDATA);
  1062. switch (ev_sub_code) {
  1063. case FSE_AZ_TX_DESCQ_FLS_DONE_EV:
  1064. netif_vdbg(efx, hw, efx->net_dev, "channel %d TXQ %d flushed\n",
  1065. channel->channel, ev_sub_data);
  1066. efx_handle_tx_flush_done(efx, event);
  1067. efx_sriov_tx_flush_done(efx, event);
  1068. break;
  1069. case FSE_AZ_RX_DESCQ_FLS_DONE_EV:
  1070. netif_vdbg(efx, hw, efx->net_dev, "channel %d RXQ %d flushed\n",
  1071. channel->channel, ev_sub_data);
  1072. efx_handle_rx_flush_done(efx, event);
  1073. efx_sriov_rx_flush_done(efx, event);
  1074. break;
  1075. case FSE_AZ_EVQ_INIT_DONE_EV:
  1076. netif_dbg(efx, hw, efx->net_dev,
  1077. "channel %d EVQ %d initialised\n",
  1078. channel->channel, ev_sub_data);
  1079. break;
  1080. case FSE_AZ_SRM_UPD_DONE_EV:
  1081. netif_vdbg(efx, hw, efx->net_dev,
  1082. "channel %d SRAM update done\n", channel->channel);
  1083. break;
  1084. case FSE_AZ_WAKE_UP_EV:
  1085. netif_vdbg(efx, hw, efx->net_dev,
  1086. "channel %d RXQ %d wakeup event\n",
  1087. channel->channel, ev_sub_data);
  1088. break;
  1089. case FSE_AZ_TIMER_EV:
  1090. netif_vdbg(efx, hw, efx->net_dev,
  1091. "channel %d RX queue %d timer expired\n",
  1092. channel->channel, ev_sub_data);
  1093. break;
  1094. case FSE_AA_RX_RECOVER_EV:
  1095. netif_err(efx, rx_err, efx->net_dev,
  1096. "channel %d seen DRIVER RX_RESET event. "
  1097. "Resetting.\n", channel->channel);
  1098. atomic_inc(&efx->rx_reset);
  1099. efx_schedule_reset(efx,
  1100. EFX_WORKAROUND_6555(efx) ?
  1101. RESET_TYPE_RX_RECOVERY :
  1102. RESET_TYPE_DISABLE);
  1103. break;
  1104. case FSE_BZ_RX_DSC_ERROR_EV:
  1105. if (ev_sub_data < EFX_VI_BASE) {
  1106. netif_err(efx, rx_err, efx->net_dev,
  1107. "RX DMA Q %d reports descriptor fetch error."
  1108. " RX Q %d is disabled.\n", ev_sub_data,
  1109. ev_sub_data);
  1110. efx_schedule_reset(efx, RESET_TYPE_RX_DESC_FETCH);
  1111. } else
  1112. efx_sriov_desc_fetch_err(efx, ev_sub_data);
  1113. break;
  1114. case FSE_BZ_TX_DSC_ERROR_EV:
  1115. if (ev_sub_data < EFX_VI_BASE) {
  1116. netif_err(efx, tx_err, efx->net_dev,
  1117. "TX DMA Q %d reports descriptor fetch error."
  1118. " TX Q %d is disabled.\n", ev_sub_data,
  1119. ev_sub_data);
  1120. efx_schedule_reset(efx, RESET_TYPE_TX_DESC_FETCH);
  1121. } else
  1122. efx_sriov_desc_fetch_err(efx, ev_sub_data);
  1123. break;
  1124. default:
  1125. netif_vdbg(efx, hw, efx->net_dev,
  1126. "channel %d unknown driver event code %d "
  1127. "data %04x\n", channel->channel, ev_sub_code,
  1128. ev_sub_data);
  1129. break;
  1130. }
  1131. }
  1132. int efx_nic_process_eventq(struct efx_channel *channel, int budget)
  1133. {
  1134. struct efx_nic *efx = channel->efx;
  1135. unsigned int read_ptr;
  1136. efx_qword_t event, *p_event;
  1137. int ev_code;
  1138. int tx_packets = 0;
  1139. int spent = 0;
  1140. read_ptr = channel->eventq_read_ptr;
  1141. for (;;) {
  1142. p_event = efx_event(channel, read_ptr);
  1143. event = *p_event;
  1144. if (!efx_event_present(&event))
  1145. /* End of events */
  1146. break;
  1147. netif_vdbg(channel->efx, intr, channel->efx->net_dev,
  1148. "channel %d event is "EFX_QWORD_FMT"\n",
  1149. channel->channel, EFX_QWORD_VAL(event));
  1150. /* Clear this event by marking it all ones */
  1151. EFX_SET_QWORD(*p_event);
  1152. ++read_ptr;
  1153. ev_code = EFX_QWORD_FIELD(event, FSF_AZ_EV_CODE);
  1154. switch (ev_code) {
  1155. case FSE_AZ_EV_CODE_RX_EV:
  1156. efx_handle_rx_event(channel, &event);
  1157. if (++spent == budget)
  1158. goto out;
  1159. break;
  1160. case FSE_AZ_EV_CODE_TX_EV:
  1161. tx_packets += efx_handle_tx_event(channel, &event);
  1162. if (tx_packets > efx->txq_entries) {
  1163. spent = budget;
  1164. goto out;
  1165. }
  1166. break;
  1167. case FSE_AZ_EV_CODE_DRV_GEN_EV:
  1168. efx_handle_generated_event(channel, &event);
  1169. break;
  1170. case FSE_AZ_EV_CODE_DRIVER_EV:
  1171. efx_handle_driver_event(channel, &event);
  1172. break;
  1173. case FSE_CZ_EV_CODE_USER_EV:
  1174. efx_sriov_event(channel, &event);
  1175. break;
  1176. case FSE_CZ_EV_CODE_MCDI_EV:
  1177. efx_mcdi_process_event(channel, &event);
  1178. break;
  1179. case FSE_AZ_EV_CODE_GLOBAL_EV:
  1180. if (efx->type->handle_global_event &&
  1181. efx->type->handle_global_event(channel, &event))
  1182. break;
  1183. /* else fall through */
  1184. default:
  1185. netif_err(channel->efx, hw, channel->efx->net_dev,
  1186. "channel %d unknown event type %d (data "
  1187. EFX_QWORD_FMT ")\n", channel->channel,
  1188. ev_code, EFX_QWORD_VAL(event));
  1189. }
  1190. }
  1191. out:
  1192. channel->eventq_read_ptr = read_ptr;
  1193. return spent;
  1194. }
  1195. /* Check whether an event is present in the eventq at the current
  1196. * read pointer. Only useful for self-test.
  1197. */
  1198. bool efx_nic_event_present(struct efx_channel *channel)
  1199. {
  1200. return efx_event_present(efx_event(channel, channel->eventq_read_ptr));
  1201. }
  1202. /* Allocate buffer table entries for event queue */
  1203. int efx_nic_probe_eventq(struct efx_channel *channel)
  1204. {
  1205. struct efx_nic *efx = channel->efx;
  1206. unsigned entries;
  1207. entries = channel->eventq_mask + 1;
  1208. return efx_alloc_special_buffer(efx, &channel->eventq,
  1209. entries * sizeof(efx_qword_t));
  1210. }
  1211. void efx_nic_init_eventq(struct efx_channel *channel)
  1212. {
  1213. efx_oword_t reg;
  1214. struct efx_nic *efx = channel->efx;
  1215. netif_dbg(efx, hw, efx->net_dev,
  1216. "channel %d event queue in special buffers %d-%d\n",
  1217. channel->channel, channel->eventq.index,
  1218. channel->eventq.index + channel->eventq.entries - 1);
  1219. if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
  1220. EFX_POPULATE_OWORD_3(reg,
  1221. FRF_CZ_TIMER_Q_EN, 1,
  1222. FRF_CZ_HOST_NOTIFY_MODE, 0,
  1223. FRF_CZ_TIMER_MODE, FFE_CZ_TIMER_MODE_DIS);
  1224. efx_writeo_table(efx, &reg, FR_BZ_TIMER_TBL, channel->channel);
  1225. }
  1226. /* Pin event queue buffer */
  1227. efx_init_special_buffer(efx, &channel->eventq);
  1228. /* Fill event queue with all ones (i.e. empty events) */
  1229. memset(channel->eventq.addr, 0xff, channel->eventq.len);
  1230. /* Push event queue to card */
  1231. EFX_POPULATE_OWORD_3(reg,
  1232. FRF_AZ_EVQ_EN, 1,
  1233. FRF_AZ_EVQ_SIZE, __ffs(channel->eventq.entries),
  1234. FRF_AZ_EVQ_BUF_BASE_ID, channel->eventq.index);
  1235. efx_writeo_table(efx, &reg, efx->type->evq_ptr_tbl_base,
  1236. channel->channel);
  1237. efx->type->push_irq_moderation(channel);
  1238. }
  1239. void efx_nic_fini_eventq(struct efx_channel *channel)
  1240. {
  1241. efx_oword_t reg;
  1242. struct efx_nic *efx = channel->efx;
  1243. /* Remove event queue from card */
  1244. EFX_ZERO_OWORD(reg);
  1245. efx_writeo_table(efx, &reg, efx->type->evq_ptr_tbl_base,
  1246. channel->channel);
  1247. if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0)
  1248. efx_writeo_table(efx, &reg, FR_BZ_TIMER_TBL, channel->channel);
  1249. /* Unpin event queue */
  1250. efx_fini_special_buffer(efx, &channel->eventq);
  1251. }
  1252. /* Free buffers backing event queue */
  1253. void efx_nic_remove_eventq(struct efx_channel *channel)
  1254. {
  1255. efx_free_special_buffer(channel->efx, &channel->eventq);
  1256. }
  1257. void efx_nic_event_test_start(struct efx_channel *channel)
  1258. {
  1259. channel->event_test_cpu = -1;
  1260. smp_wmb();
  1261. efx_magic_event(channel, EFX_CHANNEL_MAGIC_TEST(channel));
  1262. }
  1263. void efx_nic_generate_fill_event(struct efx_rx_queue *rx_queue)
  1264. {
  1265. efx_magic_event(efx_rx_queue_channel(rx_queue),
  1266. EFX_CHANNEL_MAGIC_FILL(rx_queue));
  1267. }
  1268. /**************************************************************************
  1269. *
  1270. * Hardware interrupts
  1271. * The hardware interrupt handler does very little work; all the event
  1272. * queue processing is carried out by per-channel tasklets.
  1273. *
  1274. **************************************************************************/
  1275. /* Enable/disable/generate interrupts */
  1276. static inline void efx_nic_interrupts(struct efx_nic *efx,
  1277. bool enabled, bool force)
  1278. {
  1279. efx_oword_t int_en_reg_ker;
  1280. EFX_POPULATE_OWORD_3(int_en_reg_ker,
  1281. FRF_AZ_KER_INT_LEVE_SEL, efx->irq_level,
  1282. FRF_AZ_KER_INT_KER, force,
  1283. FRF_AZ_DRV_INT_EN_KER, enabled);
  1284. efx_writeo(efx, &int_en_reg_ker, FR_AZ_INT_EN_KER);
  1285. }
  1286. void efx_nic_enable_interrupts(struct efx_nic *efx)
  1287. {
  1288. EFX_ZERO_OWORD(*((efx_oword_t *) efx->irq_status.addr));
  1289. wmb(); /* Ensure interrupt vector is clear before interrupts enabled */
  1290. efx_nic_interrupts(efx, true, false);
  1291. }
  1292. void efx_nic_disable_interrupts(struct efx_nic *efx)
  1293. {
  1294. /* Disable interrupts */
  1295. efx_nic_interrupts(efx, false, false);
  1296. }
  1297. /* Generate a test interrupt
  1298. * Interrupt must already have been enabled, otherwise nasty things
  1299. * may happen.
  1300. */
  1301. void efx_nic_irq_test_start(struct efx_nic *efx)
  1302. {
  1303. efx->last_irq_cpu = -1;
  1304. smp_wmb();
  1305. efx_nic_interrupts(efx, true, true);
  1306. }
  1307. /* Process a fatal interrupt
  1308. * Disable bus mastering ASAP and schedule a reset
  1309. */
  1310. irqreturn_t efx_nic_fatal_interrupt(struct efx_nic *efx)
  1311. {
  1312. struct falcon_nic_data *nic_data = efx->nic_data;
  1313. efx_oword_t *int_ker = efx->irq_status.addr;
  1314. efx_oword_t fatal_intr;
  1315. int error, mem_perr;
  1316. efx_reado(efx, &fatal_intr, FR_AZ_FATAL_INTR_KER);
  1317. error = EFX_OWORD_FIELD(fatal_intr, FRF_AZ_FATAL_INTR);
  1318. netif_err(efx, hw, efx->net_dev, "SYSTEM ERROR "EFX_OWORD_FMT" status "
  1319. EFX_OWORD_FMT ": %s\n", EFX_OWORD_VAL(*int_ker),
  1320. EFX_OWORD_VAL(fatal_intr),
  1321. error ? "disabling bus mastering" : "no recognised error");
  1322. /* If this is a memory parity error dump which blocks are offending */
  1323. mem_perr = (EFX_OWORD_FIELD(fatal_intr, FRF_AZ_MEM_PERR_INT_KER) ||
  1324. EFX_OWORD_FIELD(fatal_intr, FRF_AZ_SRM_PERR_INT_KER));
  1325. if (mem_perr) {
  1326. efx_oword_t reg;
  1327. efx_reado(efx, &reg, FR_AZ_MEM_STAT);
  1328. netif_err(efx, hw, efx->net_dev,
  1329. "SYSTEM ERROR: memory parity error "EFX_OWORD_FMT"\n",
  1330. EFX_OWORD_VAL(reg));
  1331. }
  1332. /* Disable both devices */
  1333. pci_clear_master(efx->pci_dev);
  1334. if (efx_nic_is_dual_func(efx))
  1335. pci_clear_master(nic_data->pci_dev2);
  1336. efx_nic_disable_interrupts(efx);
  1337. /* Count errors and reset or disable the NIC accordingly */
  1338. if (efx->int_error_count == 0 ||
  1339. time_after(jiffies, efx->int_error_expire)) {
  1340. efx->int_error_count = 0;
  1341. efx->int_error_expire =
  1342. jiffies + EFX_INT_ERROR_EXPIRE * HZ;
  1343. }
  1344. if (++efx->int_error_count < EFX_MAX_INT_ERRORS) {
  1345. netif_err(efx, hw, efx->net_dev,
  1346. "SYSTEM ERROR - reset scheduled\n");
  1347. efx_schedule_reset(efx, RESET_TYPE_INT_ERROR);
  1348. } else {
  1349. netif_err(efx, hw, efx->net_dev,
  1350. "SYSTEM ERROR - max number of errors seen."
  1351. "NIC will be disabled\n");
  1352. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  1353. }
  1354. return IRQ_HANDLED;
  1355. }
  1356. /* Handle a legacy interrupt
  1357. * Acknowledges the interrupt and schedule event queue processing.
  1358. */
  1359. static irqreturn_t efx_legacy_interrupt(int irq, void *dev_id)
  1360. {
  1361. struct efx_nic *efx = dev_id;
  1362. efx_oword_t *int_ker = efx->irq_status.addr;
  1363. irqreturn_t result = IRQ_NONE;
  1364. struct efx_channel *channel;
  1365. efx_dword_t reg;
  1366. u32 queues;
  1367. int syserr;
  1368. /* Could this be ours? If interrupts are disabled then the
  1369. * channel state may not be valid.
  1370. */
  1371. if (!efx->legacy_irq_enabled)
  1372. return result;
  1373. /* Read the ISR which also ACKs the interrupts */
  1374. efx_readd(efx, &reg, FR_BZ_INT_ISR0);
  1375. queues = EFX_EXTRACT_DWORD(reg, 0, 31);
  1376. /* Legacy interrupts are disabled too late by the EEH kernel
  1377. * code. Disable them earlier.
  1378. * If an EEH error occurred, the read will have returned all ones.
  1379. */
  1380. if (EFX_DWORD_IS_ALL_ONES(reg) && efx_try_recovery(efx) &&
  1381. !efx->eeh_disabled_legacy_irq) {
  1382. disable_irq_nosync(efx->legacy_irq);
  1383. efx->eeh_disabled_legacy_irq = true;
  1384. }
  1385. /* Handle non-event-queue sources */
  1386. if (queues & (1U << efx->irq_level)) {
  1387. syserr = EFX_OWORD_FIELD(*int_ker, FSF_AZ_NET_IVEC_FATAL_INT);
  1388. if (unlikely(syserr))
  1389. return efx_nic_fatal_interrupt(efx);
  1390. efx->last_irq_cpu = raw_smp_processor_id();
  1391. }
  1392. if (queues != 0) {
  1393. if (EFX_WORKAROUND_15783(efx))
  1394. efx->irq_zero_count = 0;
  1395. /* Schedule processing of any interrupting queues */
  1396. efx_for_each_channel(channel, efx) {
  1397. if (queues & 1)
  1398. efx_schedule_channel_irq(channel);
  1399. queues >>= 1;
  1400. }
  1401. result = IRQ_HANDLED;
  1402. } else if (EFX_WORKAROUND_15783(efx)) {
  1403. efx_qword_t *event;
  1404. /* We can't return IRQ_HANDLED more than once on seeing ISR=0
  1405. * because this might be a shared interrupt. */
  1406. if (efx->irq_zero_count++ == 0)
  1407. result = IRQ_HANDLED;
  1408. /* Ensure we schedule or rearm all event queues */
  1409. efx_for_each_channel(channel, efx) {
  1410. event = efx_event(channel, channel->eventq_read_ptr);
  1411. if (efx_event_present(event))
  1412. efx_schedule_channel_irq(channel);
  1413. else
  1414. efx_nic_eventq_read_ack(channel);
  1415. }
  1416. }
  1417. if (result == IRQ_HANDLED)
  1418. netif_vdbg(efx, intr, efx->net_dev,
  1419. "IRQ %d on CPU %d status " EFX_DWORD_FMT "\n",
  1420. irq, raw_smp_processor_id(), EFX_DWORD_VAL(reg));
  1421. return result;
  1422. }
  1423. /* Handle an MSI interrupt
  1424. *
  1425. * Handle an MSI hardware interrupt. This routine schedules event
  1426. * queue processing. No interrupt acknowledgement cycle is necessary.
  1427. * Also, we never need to check that the interrupt is for us, since
  1428. * MSI interrupts cannot be shared.
  1429. */
  1430. static irqreturn_t efx_msi_interrupt(int irq, void *dev_id)
  1431. {
  1432. struct efx_channel *channel = *(struct efx_channel **)dev_id;
  1433. struct efx_nic *efx = channel->efx;
  1434. efx_oword_t *int_ker = efx->irq_status.addr;
  1435. int syserr;
  1436. netif_vdbg(efx, intr, efx->net_dev,
  1437. "IRQ %d on CPU %d status " EFX_OWORD_FMT "\n",
  1438. irq, raw_smp_processor_id(), EFX_OWORD_VAL(*int_ker));
  1439. /* Handle non-event-queue sources */
  1440. if (channel->channel == efx->irq_level) {
  1441. syserr = EFX_OWORD_FIELD(*int_ker, FSF_AZ_NET_IVEC_FATAL_INT);
  1442. if (unlikely(syserr))
  1443. return efx_nic_fatal_interrupt(efx);
  1444. efx->last_irq_cpu = raw_smp_processor_id();
  1445. }
  1446. /* Schedule processing of the channel */
  1447. efx_schedule_channel_irq(channel);
  1448. return IRQ_HANDLED;
  1449. }
  1450. /* Setup RSS indirection table.
  1451. * This maps from the hash value of the packet to RXQ
  1452. */
  1453. void efx_nic_push_rx_indir_table(struct efx_nic *efx)
  1454. {
  1455. size_t i = 0;
  1456. efx_dword_t dword;
  1457. if (efx_nic_rev(efx) < EFX_REV_FALCON_B0)
  1458. return;
  1459. BUILD_BUG_ON(ARRAY_SIZE(efx->rx_indir_table) !=
  1460. FR_BZ_RX_INDIRECTION_TBL_ROWS);
  1461. for (i = 0; i < FR_BZ_RX_INDIRECTION_TBL_ROWS; i++) {
  1462. EFX_POPULATE_DWORD_1(dword, FRF_BZ_IT_QUEUE,
  1463. efx->rx_indir_table[i]);
  1464. efx_writed(efx, &dword,
  1465. FR_BZ_RX_INDIRECTION_TBL +
  1466. FR_BZ_RX_INDIRECTION_TBL_STEP * i);
  1467. }
  1468. }
  1469. /* Hook interrupt handler(s)
  1470. * Try MSI and then legacy interrupts.
  1471. */
  1472. int efx_nic_init_interrupt(struct efx_nic *efx)
  1473. {
  1474. struct efx_channel *channel;
  1475. unsigned int n_irqs;
  1476. int rc;
  1477. if (!EFX_INT_MODE_USE_MSI(efx)) {
  1478. irq_handler_t handler;
  1479. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0)
  1480. handler = efx_legacy_interrupt;
  1481. else
  1482. handler = falcon_legacy_interrupt_a1;
  1483. rc = request_irq(efx->legacy_irq, handler, IRQF_SHARED,
  1484. efx->name, efx);
  1485. if (rc) {
  1486. netif_err(efx, drv, efx->net_dev,
  1487. "failed to hook legacy IRQ %d\n",
  1488. efx->pci_dev->irq);
  1489. goto fail1;
  1490. }
  1491. return 0;
  1492. }
  1493. #ifdef CONFIG_RFS_ACCEL
  1494. if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
  1495. efx->net_dev->rx_cpu_rmap =
  1496. alloc_irq_cpu_rmap(efx->n_rx_channels);
  1497. if (!efx->net_dev->rx_cpu_rmap) {
  1498. rc = -ENOMEM;
  1499. goto fail1;
  1500. }
  1501. }
  1502. #endif
  1503. /* Hook MSI or MSI-X interrupt */
  1504. n_irqs = 0;
  1505. efx_for_each_channel(channel, efx) {
  1506. rc = request_irq(channel->irq, efx_msi_interrupt,
  1507. IRQF_PROBE_SHARED, /* Not shared */
  1508. efx->channel_name[channel->channel],
  1509. &efx->channel[channel->channel]);
  1510. if (rc) {
  1511. netif_err(efx, drv, efx->net_dev,
  1512. "failed to hook IRQ %d\n", channel->irq);
  1513. goto fail2;
  1514. }
  1515. ++n_irqs;
  1516. #ifdef CONFIG_RFS_ACCEL
  1517. if (efx->interrupt_mode == EFX_INT_MODE_MSIX &&
  1518. channel->channel < efx->n_rx_channels) {
  1519. rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap,
  1520. channel->irq);
  1521. if (rc)
  1522. goto fail2;
  1523. }
  1524. #endif
  1525. }
  1526. return 0;
  1527. fail2:
  1528. #ifdef CONFIG_RFS_ACCEL
  1529. free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
  1530. efx->net_dev->rx_cpu_rmap = NULL;
  1531. #endif
  1532. efx_for_each_channel(channel, efx) {
  1533. if (n_irqs-- == 0)
  1534. break;
  1535. free_irq(channel->irq, &efx->channel[channel->channel]);
  1536. }
  1537. fail1:
  1538. return rc;
  1539. }
  1540. void efx_nic_fini_interrupt(struct efx_nic *efx)
  1541. {
  1542. struct efx_channel *channel;
  1543. efx_oword_t reg;
  1544. #ifdef CONFIG_RFS_ACCEL
  1545. free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
  1546. efx->net_dev->rx_cpu_rmap = NULL;
  1547. #endif
  1548. /* Disable MSI/MSI-X interrupts */
  1549. efx_for_each_channel(channel, efx)
  1550. free_irq(channel->irq, &efx->channel[channel->channel]);
  1551. /* ACK legacy interrupt */
  1552. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0)
  1553. efx_reado(efx, &reg, FR_BZ_INT_ISR0);
  1554. else
  1555. falcon_irq_ack_a1(efx);
  1556. /* Disable legacy interrupt */
  1557. if (efx->legacy_irq)
  1558. free_irq(efx->legacy_irq, efx);
  1559. }
  1560. /* Looks at available SRAM resources and works out how many queues we
  1561. * can support, and where things like descriptor caches should live.
  1562. *
  1563. * SRAM is split up as follows:
  1564. * 0 buftbl entries for channels
  1565. * efx->vf_buftbl_base buftbl entries for SR-IOV
  1566. * efx->rx_dc_base RX descriptor caches
  1567. * efx->tx_dc_base TX descriptor caches
  1568. */
  1569. void efx_nic_dimension_resources(struct efx_nic *efx, unsigned sram_lim_qw)
  1570. {
  1571. unsigned vi_count, buftbl_min;
  1572. /* Account for the buffer table entries backing the datapath channels
  1573. * and the descriptor caches for those channels.
  1574. */
  1575. buftbl_min = ((efx->n_rx_channels * EFX_MAX_DMAQ_SIZE +
  1576. efx->n_tx_channels * EFX_TXQ_TYPES * EFX_MAX_DMAQ_SIZE +
  1577. efx->n_channels * EFX_MAX_EVQ_SIZE)
  1578. * sizeof(efx_qword_t) / EFX_BUF_SIZE);
  1579. vi_count = max(efx->n_channels, efx->n_tx_channels * EFX_TXQ_TYPES);
  1580. #ifdef CONFIG_SFC_SRIOV
  1581. if (efx_sriov_wanted(efx)) {
  1582. unsigned vi_dc_entries, buftbl_free, entries_per_vf, vf_limit;
  1583. efx->vf_buftbl_base = buftbl_min;
  1584. vi_dc_entries = RX_DC_ENTRIES + TX_DC_ENTRIES;
  1585. vi_count = max(vi_count, EFX_VI_BASE);
  1586. buftbl_free = (sram_lim_qw - buftbl_min -
  1587. vi_count * vi_dc_entries);
  1588. entries_per_vf = ((vi_dc_entries + EFX_VF_BUFTBL_PER_VI) *
  1589. efx_vf_size(efx));
  1590. vf_limit = min(buftbl_free / entries_per_vf,
  1591. (1024U - EFX_VI_BASE) >> efx->vi_scale);
  1592. if (efx->vf_count > vf_limit) {
  1593. netif_err(efx, probe, efx->net_dev,
  1594. "Reducing VF count from from %d to %d\n",
  1595. efx->vf_count, vf_limit);
  1596. efx->vf_count = vf_limit;
  1597. }
  1598. vi_count += efx->vf_count * efx_vf_size(efx);
  1599. }
  1600. #endif
  1601. efx->tx_dc_base = sram_lim_qw - vi_count * TX_DC_ENTRIES;
  1602. efx->rx_dc_base = efx->tx_dc_base - vi_count * RX_DC_ENTRIES;
  1603. }
  1604. u32 efx_nic_fpga_ver(struct efx_nic *efx)
  1605. {
  1606. efx_oword_t altera_build;
  1607. efx_reado(efx, &altera_build, FR_AZ_ALTERA_BUILD);
  1608. return EFX_OWORD_FIELD(altera_build, FRF_AZ_ALTERA_BUILD_VER);
  1609. }
  1610. void efx_nic_init_common(struct efx_nic *efx)
  1611. {
  1612. efx_oword_t temp;
  1613. /* Set positions of descriptor caches in SRAM. */
  1614. EFX_POPULATE_OWORD_1(temp, FRF_AZ_SRM_TX_DC_BASE_ADR, efx->tx_dc_base);
  1615. efx_writeo(efx, &temp, FR_AZ_SRM_TX_DC_CFG);
  1616. EFX_POPULATE_OWORD_1(temp, FRF_AZ_SRM_RX_DC_BASE_ADR, efx->rx_dc_base);
  1617. efx_writeo(efx, &temp, FR_AZ_SRM_RX_DC_CFG);
  1618. /* Set TX descriptor cache size. */
  1619. BUILD_BUG_ON(TX_DC_ENTRIES != (8 << TX_DC_ENTRIES_ORDER));
  1620. EFX_POPULATE_OWORD_1(temp, FRF_AZ_TX_DC_SIZE, TX_DC_ENTRIES_ORDER);
  1621. efx_writeo(efx, &temp, FR_AZ_TX_DC_CFG);
  1622. /* Set RX descriptor cache size. Set low watermark to size-8, as
  1623. * this allows most efficient prefetching.
  1624. */
  1625. BUILD_BUG_ON(RX_DC_ENTRIES != (8 << RX_DC_ENTRIES_ORDER));
  1626. EFX_POPULATE_OWORD_1(temp, FRF_AZ_RX_DC_SIZE, RX_DC_ENTRIES_ORDER);
  1627. efx_writeo(efx, &temp, FR_AZ_RX_DC_CFG);
  1628. EFX_POPULATE_OWORD_1(temp, FRF_AZ_RX_DC_PF_LWM, RX_DC_ENTRIES - 8);
  1629. efx_writeo(efx, &temp, FR_AZ_RX_DC_PF_WM);
  1630. /* Program INT_KER address */
  1631. EFX_POPULATE_OWORD_2(temp,
  1632. FRF_AZ_NORM_INT_VEC_DIS_KER,
  1633. EFX_INT_MODE_USE_MSI(efx),
  1634. FRF_AZ_INT_ADR_KER, efx->irq_status.dma_addr);
  1635. efx_writeo(efx, &temp, FR_AZ_INT_ADR_KER);
  1636. if (EFX_WORKAROUND_17213(efx) && !EFX_INT_MODE_USE_MSI(efx))
  1637. /* Use an interrupt level unused by event queues */
  1638. efx->irq_level = 0x1f;
  1639. else
  1640. /* Use a valid MSI-X vector */
  1641. efx->irq_level = 0;
  1642. /* Enable all the genuinely fatal interrupts. (They are still
  1643. * masked by the overall interrupt mask, controlled by
  1644. * falcon_interrupts()).
  1645. *
  1646. * Note: All other fatal interrupts are enabled
  1647. */
  1648. EFX_POPULATE_OWORD_3(temp,
  1649. FRF_AZ_ILL_ADR_INT_KER_EN, 1,
  1650. FRF_AZ_RBUF_OWN_INT_KER_EN, 1,
  1651. FRF_AZ_TBUF_OWN_INT_KER_EN, 1);
  1652. if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0)
  1653. EFX_SET_OWORD_FIELD(temp, FRF_CZ_SRAM_PERR_INT_P_KER_EN, 1);
  1654. EFX_INVERT_OWORD(temp);
  1655. efx_writeo(efx, &temp, FR_AZ_FATAL_INTR_KER);
  1656. efx_nic_push_rx_indir_table(efx);
  1657. /* Disable the ugly timer-based TX DMA backoff and allow TX DMA to be
  1658. * controlled by the RX FIFO fill level. Set arbitration to one pkt/Q.
  1659. */
  1660. efx_reado(efx, &temp, FR_AZ_TX_RESERVED);
  1661. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_RX_SPACER, 0xfe);
  1662. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_RX_SPACER_EN, 1);
  1663. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_ONE_PKT_PER_Q, 1);
  1664. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_PUSH_EN, 1);
  1665. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_DIS_NON_IP_EV, 1);
  1666. /* Enable SW_EV to inherit in char driver - assume harmless here */
  1667. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_SOFT_EVT_EN, 1);
  1668. /* Prefetch threshold 2 => fetch when descriptor cache half empty */
  1669. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_PREF_THRESHOLD, 2);
  1670. /* Disable hardware watchdog which can misfire */
  1671. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_PREF_WD_TMR, 0x3fffff);
  1672. /* Squash TX of packets of 16 bytes or less */
  1673. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0)
  1674. EFX_SET_OWORD_FIELD(temp, FRF_BZ_TX_FLUSH_MIN_LEN_EN, 1);
  1675. efx_writeo(efx, &temp, FR_AZ_TX_RESERVED);
  1676. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0) {
  1677. EFX_POPULATE_OWORD_4(temp,
  1678. /* Default values */
  1679. FRF_BZ_TX_PACE_SB_NOT_AF, 0x15,
  1680. FRF_BZ_TX_PACE_SB_AF, 0xb,
  1681. FRF_BZ_TX_PACE_FB_BASE, 0,
  1682. /* Allow large pace values in the
  1683. * fast bin. */
  1684. FRF_BZ_TX_PACE_BIN_TH,
  1685. FFE_BZ_TX_PACE_RESERVED);
  1686. efx_writeo(efx, &temp, FR_BZ_TX_PACE);
  1687. }
  1688. }
  1689. /* Register dump */
  1690. #define REGISTER_REVISION_A 1
  1691. #define REGISTER_REVISION_B 2
  1692. #define REGISTER_REVISION_C 3
  1693. #define REGISTER_REVISION_Z 3 /* latest revision */
  1694. struct efx_nic_reg {
  1695. u32 offset:24;
  1696. u32 min_revision:2, max_revision:2;
  1697. };
  1698. #define REGISTER(name, min_rev, max_rev) { \
  1699. FR_ ## min_rev ## max_rev ## _ ## name, \
  1700. REGISTER_REVISION_ ## min_rev, REGISTER_REVISION_ ## max_rev \
  1701. }
  1702. #define REGISTER_AA(name) REGISTER(name, A, A)
  1703. #define REGISTER_AB(name) REGISTER(name, A, B)
  1704. #define REGISTER_AZ(name) REGISTER(name, A, Z)
  1705. #define REGISTER_BB(name) REGISTER(name, B, B)
  1706. #define REGISTER_BZ(name) REGISTER(name, B, Z)
  1707. #define REGISTER_CZ(name) REGISTER(name, C, Z)
  1708. static const struct efx_nic_reg efx_nic_regs[] = {
  1709. REGISTER_AZ(ADR_REGION),
  1710. REGISTER_AZ(INT_EN_KER),
  1711. REGISTER_BZ(INT_EN_CHAR),
  1712. REGISTER_AZ(INT_ADR_KER),
  1713. REGISTER_BZ(INT_ADR_CHAR),
  1714. /* INT_ACK_KER is WO */
  1715. /* INT_ISR0 is RC */
  1716. REGISTER_AZ(HW_INIT),
  1717. REGISTER_CZ(USR_EV_CFG),
  1718. REGISTER_AB(EE_SPI_HCMD),
  1719. REGISTER_AB(EE_SPI_HADR),
  1720. REGISTER_AB(EE_SPI_HDATA),
  1721. REGISTER_AB(EE_BASE_PAGE),
  1722. REGISTER_AB(EE_VPD_CFG0),
  1723. /* EE_VPD_SW_CNTL and EE_VPD_SW_DATA are not used */
  1724. /* PMBX_DBG_IADDR and PBMX_DBG_IDATA are indirect */
  1725. /* PCIE_CORE_INDIRECT is indirect */
  1726. REGISTER_AB(NIC_STAT),
  1727. REGISTER_AB(GPIO_CTL),
  1728. REGISTER_AB(GLB_CTL),
  1729. /* FATAL_INTR_KER and FATAL_INTR_CHAR are partly RC */
  1730. REGISTER_BZ(DP_CTRL),
  1731. REGISTER_AZ(MEM_STAT),
  1732. REGISTER_AZ(CS_DEBUG),
  1733. REGISTER_AZ(ALTERA_BUILD),
  1734. REGISTER_AZ(CSR_SPARE),
  1735. REGISTER_AB(PCIE_SD_CTL0123),
  1736. REGISTER_AB(PCIE_SD_CTL45),
  1737. REGISTER_AB(PCIE_PCS_CTL_STAT),
  1738. /* DEBUG_DATA_OUT is not used */
  1739. /* DRV_EV is WO */
  1740. REGISTER_AZ(EVQ_CTL),
  1741. REGISTER_AZ(EVQ_CNT1),
  1742. REGISTER_AZ(EVQ_CNT2),
  1743. REGISTER_AZ(BUF_TBL_CFG),
  1744. REGISTER_AZ(SRM_RX_DC_CFG),
  1745. REGISTER_AZ(SRM_TX_DC_CFG),
  1746. REGISTER_AZ(SRM_CFG),
  1747. /* BUF_TBL_UPD is WO */
  1748. REGISTER_AZ(SRM_UPD_EVQ),
  1749. REGISTER_AZ(SRAM_PARITY),
  1750. REGISTER_AZ(RX_CFG),
  1751. REGISTER_BZ(RX_FILTER_CTL),
  1752. /* RX_FLUSH_DESCQ is WO */
  1753. REGISTER_AZ(RX_DC_CFG),
  1754. REGISTER_AZ(RX_DC_PF_WM),
  1755. REGISTER_BZ(RX_RSS_TKEY),
  1756. /* RX_NODESC_DROP is RC */
  1757. REGISTER_AA(RX_SELF_RST),
  1758. /* RX_DEBUG, RX_PUSH_DROP are not used */
  1759. REGISTER_CZ(RX_RSS_IPV6_REG1),
  1760. REGISTER_CZ(RX_RSS_IPV6_REG2),
  1761. REGISTER_CZ(RX_RSS_IPV6_REG3),
  1762. /* TX_FLUSH_DESCQ is WO */
  1763. REGISTER_AZ(TX_DC_CFG),
  1764. REGISTER_AA(TX_CHKSM_CFG),
  1765. REGISTER_AZ(TX_CFG),
  1766. /* TX_PUSH_DROP is not used */
  1767. REGISTER_AZ(TX_RESERVED),
  1768. REGISTER_BZ(TX_PACE),
  1769. /* TX_PACE_DROP_QID is RC */
  1770. REGISTER_BB(TX_VLAN),
  1771. REGISTER_BZ(TX_IPFIL_PORTEN),
  1772. REGISTER_AB(MD_TXD),
  1773. REGISTER_AB(MD_RXD),
  1774. REGISTER_AB(MD_CS),
  1775. REGISTER_AB(MD_PHY_ADR),
  1776. REGISTER_AB(MD_ID),
  1777. /* MD_STAT is RC */
  1778. REGISTER_AB(MAC_STAT_DMA),
  1779. REGISTER_AB(MAC_CTRL),
  1780. REGISTER_BB(GEN_MODE),
  1781. REGISTER_AB(MAC_MC_HASH_REG0),
  1782. REGISTER_AB(MAC_MC_HASH_REG1),
  1783. REGISTER_AB(GM_CFG1),
  1784. REGISTER_AB(GM_CFG2),
  1785. /* GM_IPG and GM_HD are not used */
  1786. REGISTER_AB(GM_MAX_FLEN),
  1787. /* GM_TEST is not used */
  1788. REGISTER_AB(GM_ADR1),
  1789. REGISTER_AB(GM_ADR2),
  1790. REGISTER_AB(GMF_CFG0),
  1791. REGISTER_AB(GMF_CFG1),
  1792. REGISTER_AB(GMF_CFG2),
  1793. REGISTER_AB(GMF_CFG3),
  1794. REGISTER_AB(GMF_CFG4),
  1795. REGISTER_AB(GMF_CFG5),
  1796. REGISTER_BB(TX_SRC_MAC_CTL),
  1797. REGISTER_AB(XM_ADR_LO),
  1798. REGISTER_AB(XM_ADR_HI),
  1799. REGISTER_AB(XM_GLB_CFG),
  1800. REGISTER_AB(XM_TX_CFG),
  1801. REGISTER_AB(XM_RX_CFG),
  1802. REGISTER_AB(XM_MGT_INT_MASK),
  1803. REGISTER_AB(XM_FC),
  1804. REGISTER_AB(XM_PAUSE_TIME),
  1805. REGISTER_AB(XM_TX_PARAM),
  1806. REGISTER_AB(XM_RX_PARAM),
  1807. /* XM_MGT_INT_MSK (note no 'A') is RC */
  1808. REGISTER_AB(XX_PWR_RST),
  1809. REGISTER_AB(XX_SD_CTL),
  1810. REGISTER_AB(XX_TXDRV_CTL),
  1811. /* XX_PRBS_CTL, XX_PRBS_CHK and XX_PRBS_ERR are not used */
  1812. /* XX_CORE_STAT is partly RC */
  1813. };
  1814. struct efx_nic_reg_table {
  1815. u32 offset:24;
  1816. u32 min_revision:2, max_revision:2;
  1817. u32 step:6, rows:21;
  1818. };
  1819. #define REGISTER_TABLE_DIMENSIONS(_, offset, min_rev, max_rev, step, rows) { \
  1820. offset, \
  1821. REGISTER_REVISION_ ## min_rev, REGISTER_REVISION_ ## max_rev, \
  1822. step, rows \
  1823. }
  1824. #define REGISTER_TABLE(name, min_rev, max_rev) \
  1825. REGISTER_TABLE_DIMENSIONS( \
  1826. name, FR_ ## min_rev ## max_rev ## _ ## name, \
  1827. min_rev, max_rev, \
  1828. FR_ ## min_rev ## max_rev ## _ ## name ## _STEP, \
  1829. FR_ ## min_rev ## max_rev ## _ ## name ## _ROWS)
  1830. #define REGISTER_TABLE_AA(name) REGISTER_TABLE(name, A, A)
  1831. #define REGISTER_TABLE_AZ(name) REGISTER_TABLE(name, A, Z)
  1832. #define REGISTER_TABLE_BB(name) REGISTER_TABLE(name, B, B)
  1833. #define REGISTER_TABLE_BZ(name) REGISTER_TABLE(name, B, Z)
  1834. #define REGISTER_TABLE_BB_CZ(name) \
  1835. REGISTER_TABLE_DIMENSIONS(name, FR_BZ_ ## name, B, B, \
  1836. FR_BZ_ ## name ## _STEP, \
  1837. FR_BB_ ## name ## _ROWS), \
  1838. REGISTER_TABLE_DIMENSIONS(name, FR_BZ_ ## name, C, Z, \
  1839. FR_BZ_ ## name ## _STEP, \
  1840. FR_CZ_ ## name ## _ROWS)
  1841. #define REGISTER_TABLE_CZ(name) REGISTER_TABLE(name, C, Z)
  1842. static const struct efx_nic_reg_table efx_nic_reg_tables[] = {
  1843. /* DRIVER is not used */
  1844. /* EVQ_RPTR, TIMER_COMMAND, USR_EV and {RX,TX}_DESC_UPD are WO */
  1845. REGISTER_TABLE_BB(TX_IPFIL_TBL),
  1846. REGISTER_TABLE_BB(TX_SRC_MAC_TBL),
  1847. REGISTER_TABLE_AA(RX_DESC_PTR_TBL_KER),
  1848. REGISTER_TABLE_BB_CZ(RX_DESC_PTR_TBL),
  1849. REGISTER_TABLE_AA(TX_DESC_PTR_TBL_KER),
  1850. REGISTER_TABLE_BB_CZ(TX_DESC_PTR_TBL),
  1851. REGISTER_TABLE_AA(EVQ_PTR_TBL_KER),
  1852. REGISTER_TABLE_BB_CZ(EVQ_PTR_TBL),
  1853. /* We can't reasonably read all of the buffer table (up to 8MB!).
  1854. * However this driver will only use a few entries. Reading
  1855. * 1K entries allows for some expansion of queue count and
  1856. * size before we need to change the version. */
  1857. REGISTER_TABLE_DIMENSIONS(BUF_FULL_TBL_KER, FR_AA_BUF_FULL_TBL_KER,
  1858. A, A, 8, 1024),
  1859. REGISTER_TABLE_DIMENSIONS(BUF_FULL_TBL, FR_BZ_BUF_FULL_TBL,
  1860. B, Z, 8, 1024),
  1861. REGISTER_TABLE_CZ(RX_MAC_FILTER_TBL0),
  1862. REGISTER_TABLE_BB_CZ(TIMER_TBL),
  1863. REGISTER_TABLE_BB_CZ(TX_PACE_TBL),
  1864. REGISTER_TABLE_BZ(RX_INDIRECTION_TBL),
  1865. /* TX_FILTER_TBL0 is huge and not used by this driver */
  1866. REGISTER_TABLE_CZ(TX_MAC_FILTER_TBL0),
  1867. REGISTER_TABLE_CZ(MC_TREG_SMEM),
  1868. /* MSIX_PBA_TABLE is not mapped */
  1869. /* SRM_DBG is not mapped (and is redundant with BUF_FLL_TBL) */
  1870. REGISTER_TABLE_BZ(RX_FILTER_TBL0),
  1871. };
  1872. size_t efx_nic_get_regs_len(struct efx_nic *efx)
  1873. {
  1874. const struct efx_nic_reg *reg;
  1875. const struct efx_nic_reg_table *table;
  1876. size_t len = 0;
  1877. for (reg = efx_nic_regs;
  1878. reg < efx_nic_regs + ARRAY_SIZE(efx_nic_regs);
  1879. reg++)
  1880. if (efx->type->revision >= reg->min_revision &&
  1881. efx->type->revision <= reg->max_revision)
  1882. len += sizeof(efx_oword_t);
  1883. for (table = efx_nic_reg_tables;
  1884. table < efx_nic_reg_tables + ARRAY_SIZE(efx_nic_reg_tables);
  1885. table++)
  1886. if (efx->type->revision >= table->min_revision &&
  1887. efx->type->revision <= table->max_revision)
  1888. len += table->rows * min_t(size_t, table->step, 16);
  1889. return len;
  1890. }
  1891. void efx_nic_get_regs(struct efx_nic *efx, void *buf)
  1892. {
  1893. const struct efx_nic_reg *reg;
  1894. const struct efx_nic_reg_table *table;
  1895. for (reg = efx_nic_regs;
  1896. reg < efx_nic_regs + ARRAY_SIZE(efx_nic_regs);
  1897. reg++) {
  1898. if (efx->type->revision >= reg->min_revision &&
  1899. efx->type->revision <= reg->max_revision) {
  1900. efx_reado(efx, (efx_oword_t *)buf, reg->offset);
  1901. buf += sizeof(efx_oword_t);
  1902. }
  1903. }
  1904. for (table = efx_nic_reg_tables;
  1905. table < efx_nic_reg_tables + ARRAY_SIZE(efx_nic_reg_tables);
  1906. table++) {
  1907. size_t size, i;
  1908. if (!(efx->type->revision >= table->min_revision &&
  1909. efx->type->revision <= table->max_revision))
  1910. continue;
  1911. size = min_t(size_t, table->step, 16);
  1912. for (i = 0; i < table->rows; i++) {
  1913. switch (table->step) {
  1914. case 4: /* 32-bit SRAM */
  1915. efx_readd(efx, buf, table->offset + 4 * i);
  1916. break;
  1917. case 8: /* 64-bit SRAM */
  1918. efx_sram_readq(efx,
  1919. efx->membase + table->offset,
  1920. buf, i);
  1921. break;
  1922. case 16: /* 128-bit-readable register */
  1923. efx_reado_table(efx, buf, table->offset, i);
  1924. break;
  1925. case 32: /* 128-bit register, interleaved */
  1926. efx_reado_table(efx, buf, table->offset, 2 * i);
  1927. break;
  1928. default:
  1929. WARN_ON(1);
  1930. return;
  1931. }
  1932. buf += size;
  1933. }
  1934. }
  1935. }