nic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /****************************************************************************
  3. * Driver for Solarflare network controllers and boards
  4. * Copyright 2005-2006 Fen Systems Ltd.
  5. * Copyright 2006-2013 Solarflare Communications Inc.
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/delay.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/pci.h>
  11. #include <linux/module.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/cpu_rmap.h>
  14. #include "net_driver.h"
  15. #include "bitfield.h"
  16. #include "efx.h"
  17. #include "nic.h"
  18. #include "ef10_regs.h"
  19. #include "io.h"
  20. #include "workarounds.h"
  21. #include "mcdi_pcol.h"
  22. /**************************************************************************
  23. *
  24. * Generic buffer handling
  25. * These buffers are used for interrupt status, MAC stats, etc.
  26. *
  27. **************************************************************************/
  28. int efx_nic_alloc_buffer(struct efx_nic *efx, struct efx_buffer *buffer,
  29. unsigned int len, gfp_t gfp_flags)
  30. {
  31. buffer->addr = dma_alloc_coherent(&efx->pci_dev->dev, len,
  32. &buffer->dma_addr, gfp_flags);
  33. if (!buffer->addr)
  34. return -ENOMEM;
  35. buffer->len = len;
  36. return 0;
  37. }
  38. void efx_nic_free_buffer(struct efx_nic *efx, struct efx_buffer *buffer)
  39. {
  40. if (buffer->addr) {
  41. dma_free_coherent(&efx->pci_dev->dev, buffer->len,
  42. buffer->addr, buffer->dma_addr);
  43. buffer->addr = NULL;
  44. }
  45. }
  46. /* Check whether an event is present in the eventq at the current
  47. * read pointer. Only useful for self-test.
  48. */
  49. bool efx_nic_event_present(struct efx_channel *channel)
  50. {
  51. return efx_event_present(efx_event(channel, channel->eventq_read_ptr));
  52. }
  53. void efx_nic_event_test_start(struct efx_channel *channel)
  54. {
  55. channel->event_test_cpu = -1;
  56. smp_wmb();
  57. channel->efx->type->ev_test_generate(channel);
  58. }
  59. int efx_nic_irq_test_start(struct efx_nic *efx)
  60. {
  61. efx->last_irq_cpu = -1;
  62. smp_wmb();
  63. return efx->type->irq_test_generate(efx);
  64. }
  65. /* Hook interrupt handler(s)
  66. * Try MSI and then legacy interrupts.
  67. */
  68. int efx_nic_init_interrupt(struct efx_nic *efx)
  69. {
  70. struct efx_channel *channel;
  71. unsigned int n_irqs;
  72. int rc;
  73. if (!EFX_INT_MODE_USE_MSI(efx)) {
  74. rc = request_irq(efx->legacy_irq,
  75. efx->type->irq_handle_legacy, IRQF_SHARED,
  76. efx->name, efx);
  77. if (rc) {
  78. netif_err(efx, drv, efx->net_dev,
  79. "failed to hook legacy IRQ %d\n",
  80. efx->pci_dev->irq);
  81. goto fail1;
  82. }
  83. efx->irqs_hooked = true;
  84. return 0;
  85. }
  86. #ifdef CONFIG_RFS_ACCEL
  87. if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
  88. efx->net_dev->rx_cpu_rmap =
  89. alloc_irq_cpu_rmap(efx->n_rx_channels);
  90. if (!efx->net_dev->rx_cpu_rmap) {
  91. rc = -ENOMEM;
  92. goto fail1;
  93. }
  94. }
  95. #endif
  96. /* Hook MSI or MSI-X interrupt */
  97. n_irqs = 0;
  98. efx_for_each_channel(channel, efx) {
  99. rc = request_irq(channel->irq, efx->type->irq_handle_msi,
  100. IRQF_PROBE_SHARED, /* Not shared */
  101. efx->msi_context[channel->channel].name,
  102. &efx->msi_context[channel->channel]);
  103. if (rc) {
  104. netif_err(efx, drv, efx->net_dev,
  105. "failed to hook IRQ %d\n", channel->irq);
  106. goto fail2;
  107. }
  108. ++n_irqs;
  109. #ifdef CONFIG_RFS_ACCEL
  110. if (efx->interrupt_mode == EFX_INT_MODE_MSIX &&
  111. channel->channel < efx->n_rx_channels) {
  112. rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap,
  113. channel->irq);
  114. if (rc)
  115. goto fail2;
  116. }
  117. #endif
  118. }
  119. efx->irqs_hooked = true;
  120. return 0;
  121. fail2:
  122. #ifdef CONFIG_RFS_ACCEL
  123. free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
  124. efx->net_dev->rx_cpu_rmap = NULL;
  125. #endif
  126. efx_for_each_channel(channel, efx) {
  127. if (n_irqs-- == 0)
  128. break;
  129. free_irq(channel->irq, &efx->msi_context[channel->channel]);
  130. }
  131. fail1:
  132. return rc;
  133. }
  134. void efx_nic_fini_interrupt(struct efx_nic *efx)
  135. {
  136. struct efx_channel *channel;
  137. #ifdef CONFIG_RFS_ACCEL
  138. free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
  139. efx->net_dev->rx_cpu_rmap = NULL;
  140. #endif
  141. if (!efx->irqs_hooked)
  142. return;
  143. if (EFX_INT_MODE_USE_MSI(efx)) {
  144. /* Disable MSI/MSI-X interrupts */
  145. efx_for_each_channel(channel, efx)
  146. free_irq(channel->irq,
  147. &efx->msi_context[channel->channel]);
  148. } else {
  149. /* Disable legacy interrupt */
  150. free_irq(efx->legacy_irq, efx);
  151. }
  152. efx->irqs_hooked = false;
  153. }
  154. /* Register dump */
  155. #define REGISTER_REVISION_ED 4
  156. #define REGISTER_REVISION_EZ 4 /* latest EF10 revision */
  157. struct efx_nic_reg {
  158. u32 offset:24;
  159. u32 min_revision:3, max_revision:3;
  160. };
  161. #define REGISTER(name, arch, min_rev, max_rev) { \
  162. arch ## R_ ## min_rev ## max_rev ## _ ## name, \
  163. REGISTER_REVISION_ ## arch ## min_rev, \
  164. REGISTER_REVISION_ ## arch ## max_rev \
  165. }
  166. #define REGISTER_DZ(name) REGISTER(name, E, D, Z)
  167. static const struct efx_nic_reg efx_nic_regs[] = {
  168. /* XX_PRBS_CTL, XX_PRBS_CHK and XX_PRBS_ERR are not used */
  169. /* XX_CORE_STAT is partly RC */
  170. REGISTER_DZ(BIU_HW_REV_ID),
  171. REGISTER_DZ(MC_DB_LWRD),
  172. REGISTER_DZ(MC_DB_HWRD),
  173. };
  174. struct efx_nic_reg_table {
  175. u32 offset:24;
  176. u32 min_revision:3, max_revision:3;
  177. u32 step:6, rows:21;
  178. };
  179. #define REGISTER_TABLE_DIMENSIONS(_, offset, arch, min_rev, max_rev, step, rows) { \
  180. offset, \
  181. REGISTER_REVISION_ ## arch ## min_rev, \
  182. REGISTER_REVISION_ ## arch ## max_rev, \
  183. step, rows \
  184. }
  185. #define REGISTER_TABLE(name, arch, min_rev, max_rev) \
  186. REGISTER_TABLE_DIMENSIONS( \
  187. name, arch ## R_ ## min_rev ## max_rev ## _ ## name, \
  188. arch, min_rev, max_rev, \
  189. arch ## R_ ## min_rev ## max_rev ## _ ## name ## _STEP, \
  190. arch ## R_ ## min_rev ## max_rev ## _ ## name ## _ROWS)
  191. #define REGISTER_TABLE_DZ(name) REGISTER_TABLE(name, E, D, Z)
  192. static const struct efx_nic_reg_table efx_nic_reg_tables[] = {
  193. REGISTER_TABLE_DZ(BIU_MC_SFT_STATUS),
  194. };
  195. size_t efx_nic_get_regs_len(struct efx_nic *efx)
  196. {
  197. const struct efx_nic_reg *reg;
  198. const struct efx_nic_reg_table *table;
  199. size_t len = 0;
  200. for (reg = efx_nic_regs;
  201. reg < efx_nic_regs + ARRAY_SIZE(efx_nic_regs);
  202. reg++)
  203. if (efx->type->revision >= reg->min_revision &&
  204. efx->type->revision <= reg->max_revision)
  205. len += sizeof(efx_oword_t);
  206. for (table = efx_nic_reg_tables;
  207. table < efx_nic_reg_tables + ARRAY_SIZE(efx_nic_reg_tables);
  208. table++)
  209. if (efx->type->revision >= table->min_revision &&
  210. efx->type->revision <= table->max_revision)
  211. len += table->rows * min_t(size_t, table->step, 16);
  212. return len;
  213. }
  214. void efx_nic_get_regs(struct efx_nic *efx, void *buf)
  215. {
  216. const struct efx_nic_reg *reg;
  217. const struct efx_nic_reg_table *table;
  218. for (reg = efx_nic_regs;
  219. reg < efx_nic_regs + ARRAY_SIZE(efx_nic_regs);
  220. reg++) {
  221. if (efx->type->revision >= reg->min_revision &&
  222. efx->type->revision <= reg->max_revision) {
  223. efx_reado(efx, (efx_oword_t *)buf, reg->offset);
  224. buf += sizeof(efx_oword_t);
  225. }
  226. }
  227. for (table = efx_nic_reg_tables;
  228. table < efx_nic_reg_tables + ARRAY_SIZE(efx_nic_reg_tables);
  229. table++) {
  230. size_t size, i;
  231. if (!(efx->type->revision >= table->min_revision &&
  232. efx->type->revision <= table->max_revision))
  233. continue;
  234. size = min_t(size_t, table->step, 16);
  235. for (i = 0; i < table->rows; i++) {
  236. switch (table->step) {
  237. case 4: /* 32-bit SRAM */
  238. efx_readd(efx, buf, table->offset + 4 * i);
  239. break;
  240. case 16: /* 128-bit-readable register */
  241. efx_reado_table(efx, buf, table->offset, i);
  242. break;
  243. case 32: /* 128-bit register, interleaved */
  244. efx_reado_table(efx, buf, table->offset, 2 * i);
  245. break;
  246. default:
  247. WARN_ON(1);
  248. return;
  249. }
  250. buf += size;
  251. }
  252. }
  253. }
  254. /**
  255. * efx_nic_describe_stats - Describe supported statistics for ethtool
  256. * @desc: Array of &struct efx_hw_stat_desc describing the statistics
  257. * @count: Length of the @desc array
  258. * @mask: Bitmask of which elements of @desc are enabled
  259. * @names: Buffer to copy names to, or %NULL. The names are copied
  260. * starting at intervals of %ETH_GSTRING_LEN bytes.
  261. *
  262. * Returns the number of visible statistics, i.e. the number of set
  263. * bits in the first @count bits of @mask for which a name is defined.
  264. */
  265. size_t efx_nic_describe_stats(const struct efx_hw_stat_desc *desc, size_t count,
  266. const unsigned long *mask, u8 **names)
  267. {
  268. size_t visible = 0;
  269. size_t index;
  270. for_each_set_bit(index, mask, count) {
  271. if (desc[index].name) {
  272. if (names)
  273. ethtool_puts(names, desc[index].name);
  274. ++visible;
  275. }
  276. }
  277. return visible;
  278. }
  279. /**
  280. * efx_nic_copy_stats - Copy stats from the DMA buffer in to an
  281. * intermediate buffer. This is used to get a consistent
  282. * set of stats while the DMA buffer can be written at any time
  283. * by the NIC.
  284. * @efx: The associated NIC.
  285. * @dest: Destination buffer. Must be the same size as the DMA buffer.
  286. */
  287. int efx_nic_copy_stats(struct efx_nic *efx, __le64 *dest)
  288. {
  289. __le64 *dma_stats = efx->stats_buffer.addr;
  290. __le64 generation_start, generation_end;
  291. int rc = 0, retry;
  292. if (!dest)
  293. return 0;
  294. if (!dma_stats)
  295. goto return_zeroes;
  296. /* If we're unlucky enough to read statistics during the DMA, wait
  297. * up to 10ms for it to finish (typically takes <500us)
  298. */
  299. for (retry = 0; retry < 100; ++retry) {
  300. generation_end = dma_stats[efx->num_mac_stats - 1];
  301. if (generation_end == EFX_MC_STATS_GENERATION_INVALID)
  302. goto return_zeroes;
  303. rmb();
  304. memcpy(dest, dma_stats, efx->num_mac_stats * sizeof(__le64));
  305. rmb();
  306. generation_start = dma_stats[MC_CMD_MAC_GENERATION_START];
  307. if (generation_end == generation_start)
  308. return 0; /* return good data */
  309. udelay(100);
  310. }
  311. rc = -EIO;
  312. return_zeroes:
  313. memset(dest, 0, efx->num_mac_stats * sizeof(u64));
  314. return rc;
  315. }
  316. /**
  317. * efx_nic_update_stats - Convert statistics DMA buffer to array of u64
  318. * @desc: Array of &struct efx_hw_stat_desc describing the DMA buffer
  319. * layout. DMA widths of 0, 16, 32 and 64 are supported; where
  320. * the width is specified as 0 the corresponding element of
  321. * @stats is not updated.
  322. * @count: Length of the @desc array
  323. * @mask: Bitmask of which elements of @desc are enabled
  324. * @stats: Buffer to update with the converted statistics. The length
  325. * of this array must be at least @count.
  326. * @dma_buf: DMA buffer containing hardware statistics
  327. * @accumulate: If set, the converted values will be added rather than
  328. * directly stored to the corresponding elements of @stats
  329. */
  330. void efx_nic_update_stats(const struct efx_hw_stat_desc *desc, size_t count,
  331. const unsigned long *mask,
  332. u64 *stats, const void *dma_buf, bool accumulate)
  333. {
  334. size_t index;
  335. for_each_set_bit(index, mask, count) {
  336. if (desc[index].dma_width) {
  337. const void *addr = dma_buf + desc[index].offset;
  338. u64 val;
  339. switch (desc[index].dma_width) {
  340. case 16:
  341. val = le16_to_cpup((__le16 *)addr);
  342. break;
  343. case 32:
  344. val = le32_to_cpup((__le32 *)addr);
  345. break;
  346. case 64:
  347. val = le64_to_cpup((__le64 *)addr);
  348. break;
  349. default:
  350. WARN_ON(1);
  351. val = 0;
  352. break;
  353. }
  354. if (accumulate)
  355. stats[index] += val;
  356. else
  357. stats[index] = val;
  358. }
  359. }
  360. }
  361. void efx_nic_fix_nodesc_drop_stat(struct efx_nic *efx, u64 *rx_nodesc_drops)
  362. {
  363. /* if down, or this is the first update after coming up */
  364. if (!(efx->net_dev->flags & IFF_UP) || !efx->rx_nodesc_drops_prev_state)
  365. efx->rx_nodesc_drops_while_down +=
  366. *rx_nodesc_drops - efx->rx_nodesc_drops_total;
  367. efx->rx_nodesc_drops_total = *rx_nodesc_drops;
  368. efx->rx_nodesc_drops_prev_state = !!(efx->net_dev->flags & IFF_UP);
  369. *rx_nodesc_drops -= efx->rx_nodesc_drops_while_down;
  370. }