tx.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. /****************************************************************************
  2. * Driver for Solarflare network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2005-2013 Solarflare Communications Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation, incorporated herein by reference.
  9. */
  10. #include <linux/pci.h>
  11. #include <linux/tcp.h>
  12. #include <linux/ip.h>
  13. #include <linux/in.h>
  14. #include <linux/ipv6.h>
  15. #include <linux/slab.h>
  16. #include <net/ipv6.h>
  17. #include <linux/if_ether.h>
  18. #include <linux/highmem.h>
  19. #include <linux/cache.h>
  20. #include "net_driver.h"
  21. #include "efx.h"
  22. #include "io.h"
  23. #include "nic.h"
  24. #include "tx.h"
  25. #include "workarounds.h"
  26. #include "ef10_regs.h"
  27. #ifdef EFX_USE_PIO
  28. #define EFX_PIOBUF_SIZE_DEF ALIGN(256, L1_CACHE_BYTES)
  29. unsigned int efx_piobuf_size __read_mostly = EFX_PIOBUF_SIZE_DEF;
  30. #endif /* EFX_USE_PIO */
  31. static inline u8 *efx_tx_get_copy_buffer(struct efx_tx_queue *tx_queue,
  32. struct efx_tx_buffer *buffer)
  33. {
  34. unsigned int index = efx_tx_queue_get_insert_index(tx_queue);
  35. struct efx_buffer *page_buf =
  36. &tx_queue->cb_page[index >> (PAGE_SHIFT - EFX_TX_CB_ORDER)];
  37. unsigned int offset =
  38. ((index << EFX_TX_CB_ORDER) + NET_IP_ALIGN) & (PAGE_SIZE - 1);
  39. if (unlikely(!page_buf->addr) &&
  40. efx_nic_alloc_buffer(tx_queue->efx, page_buf, PAGE_SIZE,
  41. GFP_ATOMIC))
  42. return NULL;
  43. buffer->dma_addr = page_buf->dma_addr + offset;
  44. buffer->unmap_len = 0;
  45. return (u8 *)page_buf->addr + offset;
  46. }
  47. u8 *efx_tx_get_copy_buffer_limited(struct efx_tx_queue *tx_queue,
  48. struct efx_tx_buffer *buffer, size_t len)
  49. {
  50. if (len > EFX_TX_CB_SIZE)
  51. return NULL;
  52. return efx_tx_get_copy_buffer(tx_queue, buffer);
  53. }
  54. static void efx_dequeue_buffer(struct efx_tx_queue *tx_queue,
  55. struct efx_tx_buffer *buffer,
  56. unsigned int *pkts_compl,
  57. unsigned int *bytes_compl)
  58. {
  59. if (buffer->unmap_len) {
  60. struct device *dma_dev = &tx_queue->efx->pci_dev->dev;
  61. dma_addr_t unmap_addr = buffer->dma_addr - buffer->dma_offset;
  62. if (buffer->flags & EFX_TX_BUF_MAP_SINGLE)
  63. dma_unmap_single(dma_dev, unmap_addr, buffer->unmap_len,
  64. DMA_TO_DEVICE);
  65. else
  66. dma_unmap_page(dma_dev, unmap_addr, buffer->unmap_len,
  67. DMA_TO_DEVICE);
  68. buffer->unmap_len = 0;
  69. }
  70. if (buffer->flags & EFX_TX_BUF_SKB) {
  71. (*pkts_compl)++;
  72. (*bytes_compl) += buffer->skb->len;
  73. dev_consume_skb_any((struct sk_buff *)buffer->skb);
  74. netif_vdbg(tx_queue->efx, tx_done, tx_queue->efx->net_dev,
  75. "TX queue %d transmission id %x complete\n",
  76. tx_queue->queue, tx_queue->read_count);
  77. }
  78. buffer->len = 0;
  79. buffer->flags = 0;
  80. }
  81. unsigned int efx_tx_max_skb_descs(struct efx_nic *efx)
  82. {
  83. /* Header and payload descriptor for each output segment, plus
  84. * one for every input fragment boundary within a segment
  85. */
  86. unsigned int max_descs = EFX_TSO_MAX_SEGS * 2 + MAX_SKB_FRAGS;
  87. /* Possibly one more per segment for option descriptors */
  88. if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
  89. max_descs += EFX_TSO_MAX_SEGS;
  90. /* Possibly more for PCIe page boundaries within input fragments */
  91. if (PAGE_SIZE > EFX_PAGE_SIZE)
  92. max_descs += max_t(unsigned int, MAX_SKB_FRAGS,
  93. DIV_ROUND_UP(GSO_MAX_SIZE, EFX_PAGE_SIZE));
  94. return max_descs;
  95. }
  96. static void efx_tx_maybe_stop_queue(struct efx_tx_queue *txq1)
  97. {
  98. /* We need to consider both queues that the net core sees as one */
  99. struct efx_tx_queue *txq2 = efx_tx_queue_partner(txq1);
  100. struct efx_nic *efx = txq1->efx;
  101. unsigned int fill_level;
  102. fill_level = max(txq1->insert_count - txq1->old_read_count,
  103. txq2->insert_count - txq2->old_read_count);
  104. if (likely(fill_level < efx->txq_stop_thresh))
  105. return;
  106. /* We used the stale old_read_count above, which gives us a
  107. * pessimistic estimate of the fill level (which may even
  108. * validly be >= efx->txq_entries). Now try again using
  109. * read_count (more likely to be a cache miss).
  110. *
  111. * If we read read_count and then conditionally stop the
  112. * queue, it is possible for the completion path to race with
  113. * us and complete all outstanding descriptors in the middle,
  114. * after which there will be no more completions to wake it.
  115. * Therefore we stop the queue first, then read read_count
  116. * (with a memory barrier to ensure the ordering), then
  117. * restart the queue if the fill level turns out to be low
  118. * enough.
  119. */
  120. netif_tx_stop_queue(txq1->core_txq);
  121. smp_mb();
  122. txq1->old_read_count = ACCESS_ONCE(txq1->read_count);
  123. txq2->old_read_count = ACCESS_ONCE(txq2->read_count);
  124. fill_level = max(txq1->insert_count - txq1->old_read_count,
  125. txq2->insert_count - txq2->old_read_count);
  126. EFX_WARN_ON_ONCE_PARANOID(fill_level >= efx->txq_entries);
  127. if (likely(fill_level < efx->txq_stop_thresh)) {
  128. smp_mb();
  129. if (likely(!efx->loopback_selftest))
  130. netif_tx_start_queue(txq1->core_txq);
  131. }
  132. }
  133. static int efx_enqueue_skb_copy(struct efx_tx_queue *tx_queue,
  134. struct sk_buff *skb)
  135. {
  136. unsigned int copy_len = skb->len;
  137. struct efx_tx_buffer *buffer;
  138. u8 *copy_buffer;
  139. int rc;
  140. EFX_WARN_ON_ONCE_PARANOID(copy_len > EFX_TX_CB_SIZE);
  141. buffer = efx_tx_queue_get_insert_buffer(tx_queue);
  142. copy_buffer = efx_tx_get_copy_buffer(tx_queue, buffer);
  143. if (unlikely(!copy_buffer))
  144. return -ENOMEM;
  145. rc = skb_copy_bits(skb, 0, copy_buffer, copy_len);
  146. EFX_WARN_ON_PARANOID(rc);
  147. buffer->len = copy_len;
  148. buffer->skb = skb;
  149. buffer->flags = EFX_TX_BUF_SKB;
  150. ++tx_queue->insert_count;
  151. return rc;
  152. }
  153. #ifdef EFX_USE_PIO
  154. struct efx_short_copy_buffer {
  155. int used;
  156. u8 buf[L1_CACHE_BYTES];
  157. };
  158. /* Copy to PIO, respecting that writes to PIO buffers must be dword aligned.
  159. * Advances piobuf pointer. Leaves additional data in the copy buffer.
  160. */
  161. static void efx_memcpy_toio_aligned(struct efx_nic *efx, u8 __iomem **piobuf,
  162. u8 *data, int len,
  163. struct efx_short_copy_buffer *copy_buf)
  164. {
  165. int block_len = len & ~(sizeof(copy_buf->buf) - 1);
  166. __iowrite64_copy(*piobuf, data, block_len >> 3);
  167. *piobuf += block_len;
  168. len -= block_len;
  169. if (len) {
  170. data += block_len;
  171. BUG_ON(copy_buf->used);
  172. BUG_ON(len > sizeof(copy_buf->buf));
  173. memcpy(copy_buf->buf, data, len);
  174. copy_buf->used = len;
  175. }
  176. }
  177. /* Copy to PIO, respecting dword alignment, popping data from copy buffer first.
  178. * Advances piobuf pointer. Leaves additional data in the copy buffer.
  179. */
  180. static void efx_memcpy_toio_aligned_cb(struct efx_nic *efx, u8 __iomem **piobuf,
  181. u8 *data, int len,
  182. struct efx_short_copy_buffer *copy_buf)
  183. {
  184. if (copy_buf->used) {
  185. /* if the copy buffer is partially full, fill it up and write */
  186. int copy_to_buf =
  187. min_t(int, sizeof(copy_buf->buf) - copy_buf->used, len);
  188. memcpy(copy_buf->buf + copy_buf->used, data, copy_to_buf);
  189. copy_buf->used += copy_to_buf;
  190. /* if we didn't fill it up then we're done for now */
  191. if (copy_buf->used < sizeof(copy_buf->buf))
  192. return;
  193. __iowrite64_copy(*piobuf, copy_buf->buf,
  194. sizeof(copy_buf->buf) >> 3);
  195. *piobuf += sizeof(copy_buf->buf);
  196. data += copy_to_buf;
  197. len -= copy_to_buf;
  198. copy_buf->used = 0;
  199. }
  200. efx_memcpy_toio_aligned(efx, piobuf, data, len, copy_buf);
  201. }
  202. static void efx_flush_copy_buffer(struct efx_nic *efx, u8 __iomem *piobuf,
  203. struct efx_short_copy_buffer *copy_buf)
  204. {
  205. /* if there's anything in it, write the whole buffer, including junk */
  206. if (copy_buf->used)
  207. __iowrite64_copy(piobuf, copy_buf->buf,
  208. sizeof(copy_buf->buf) >> 3);
  209. }
  210. /* Traverse skb structure and copy fragments in to PIO buffer.
  211. * Advances piobuf pointer.
  212. */
  213. static void efx_skb_copy_bits_to_pio(struct efx_nic *efx, struct sk_buff *skb,
  214. u8 __iomem **piobuf,
  215. struct efx_short_copy_buffer *copy_buf)
  216. {
  217. int i;
  218. efx_memcpy_toio_aligned(efx, piobuf, skb->data, skb_headlen(skb),
  219. copy_buf);
  220. for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
  221. skb_frag_t *f = &skb_shinfo(skb)->frags[i];
  222. u8 *vaddr;
  223. vaddr = kmap_atomic(skb_frag_page(f));
  224. efx_memcpy_toio_aligned_cb(efx, piobuf, vaddr + f->page_offset,
  225. skb_frag_size(f), copy_buf);
  226. kunmap_atomic(vaddr);
  227. }
  228. EFX_WARN_ON_ONCE_PARANOID(skb_shinfo(skb)->frag_list);
  229. }
  230. static int efx_enqueue_skb_pio(struct efx_tx_queue *tx_queue,
  231. struct sk_buff *skb)
  232. {
  233. struct efx_tx_buffer *buffer =
  234. efx_tx_queue_get_insert_buffer(tx_queue);
  235. u8 __iomem *piobuf = tx_queue->piobuf;
  236. /* Copy to PIO buffer. Ensure the writes are padded to the end
  237. * of a cache line, as this is required for write-combining to be
  238. * effective on at least x86.
  239. */
  240. if (skb_shinfo(skb)->nr_frags) {
  241. /* The size of the copy buffer will ensure all writes
  242. * are the size of a cache line.
  243. */
  244. struct efx_short_copy_buffer copy_buf;
  245. copy_buf.used = 0;
  246. efx_skb_copy_bits_to_pio(tx_queue->efx, skb,
  247. &piobuf, &copy_buf);
  248. efx_flush_copy_buffer(tx_queue->efx, piobuf, &copy_buf);
  249. } else {
  250. /* Pad the write to the size of a cache line.
  251. * We can do this because we know the skb_shared_info struct is
  252. * after the source, and the destination buffer is big enough.
  253. */
  254. BUILD_BUG_ON(L1_CACHE_BYTES >
  255. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
  256. __iowrite64_copy(tx_queue->piobuf, skb->data,
  257. ALIGN(skb->len, L1_CACHE_BYTES) >> 3);
  258. }
  259. buffer->skb = skb;
  260. buffer->flags = EFX_TX_BUF_SKB | EFX_TX_BUF_OPTION;
  261. EFX_POPULATE_QWORD_5(buffer->option,
  262. ESF_DZ_TX_DESC_IS_OPT, 1,
  263. ESF_DZ_TX_OPTION_TYPE, ESE_DZ_TX_OPTION_DESC_PIO,
  264. ESF_DZ_TX_PIO_CONT, 0,
  265. ESF_DZ_TX_PIO_BYTE_CNT, skb->len,
  266. ESF_DZ_TX_PIO_BUF_ADDR,
  267. tx_queue->piobuf_offset);
  268. ++tx_queue->insert_count;
  269. return 0;
  270. }
  271. #endif /* EFX_USE_PIO */
  272. static struct efx_tx_buffer *efx_tx_map_chunk(struct efx_tx_queue *tx_queue,
  273. dma_addr_t dma_addr,
  274. size_t len)
  275. {
  276. const struct efx_nic_type *nic_type = tx_queue->efx->type;
  277. struct efx_tx_buffer *buffer;
  278. unsigned int dma_len;
  279. /* Map the fragment taking account of NIC-dependent DMA limits. */
  280. do {
  281. buffer = efx_tx_queue_get_insert_buffer(tx_queue);
  282. dma_len = nic_type->tx_limit_len(tx_queue, dma_addr, len);
  283. buffer->len = dma_len;
  284. buffer->dma_addr = dma_addr;
  285. buffer->flags = EFX_TX_BUF_CONT;
  286. len -= dma_len;
  287. dma_addr += dma_len;
  288. ++tx_queue->insert_count;
  289. } while (len);
  290. return buffer;
  291. }
  292. /* Map all data from an SKB for DMA and create descriptors on the queue.
  293. */
  294. static int efx_tx_map_data(struct efx_tx_queue *tx_queue, struct sk_buff *skb,
  295. unsigned int segment_count)
  296. {
  297. struct efx_nic *efx = tx_queue->efx;
  298. struct device *dma_dev = &efx->pci_dev->dev;
  299. unsigned int frag_index, nr_frags;
  300. dma_addr_t dma_addr, unmap_addr;
  301. unsigned short dma_flags;
  302. size_t len, unmap_len;
  303. nr_frags = skb_shinfo(skb)->nr_frags;
  304. frag_index = 0;
  305. /* Map header data. */
  306. len = skb_headlen(skb);
  307. dma_addr = dma_map_single(dma_dev, skb->data, len, DMA_TO_DEVICE);
  308. dma_flags = EFX_TX_BUF_MAP_SINGLE;
  309. unmap_len = len;
  310. unmap_addr = dma_addr;
  311. if (unlikely(dma_mapping_error(dma_dev, dma_addr)))
  312. return -EIO;
  313. if (segment_count) {
  314. /* For TSO we need to put the header in to a separate
  315. * descriptor. Map this separately if necessary.
  316. */
  317. size_t header_len = skb_transport_header(skb) - skb->data +
  318. (tcp_hdr(skb)->doff << 2u);
  319. if (header_len != len) {
  320. tx_queue->tso_long_headers++;
  321. efx_tx_map_chunk(tx_queue, dma_addr, header_len);
  322. len -= header_len;
  323. dma_addr += header_len;
  324. }
  325. }
  326. /* Add descriptors for each fragment. */
  327. do {
  328. struct efx_tx_buffer *buffer;
  329. skb_frag_t *fragment;
  330. buffer = efx_tx_map_chunk(tx_queue, dma_addr, len);
  331. /* The final descriptor for a fragment is responsible for
  332. * unmapping the whole fragment.
  333. */
  334. buffer->flags = EFX_TX_BUF_CONT | dma_flags;
  335. buffer->unmap_len = unmap_len;
  336. buffer->dma_offset = buffer->dma_addr - unmap_addr;
  337. if (frag_index >= nr_frags) {
  338. /* Store SKB details with the final buffer for
  339. * the completion.
  340. */
  341. buffer->skb = skb;
  342. buffer->flags = EFX_TX_BUF_SKB | dma_flags;
  343. return 0;
  344. }
  345. /* Move on to the next fragment. */
  346. fragment = &skb_shinfo(skb)->frags[frag_index++];
  347. len = skb_frag_size(fragment);
  348. dma_addr = skb_frag_dma_map(dma_dev, fragment,
  349. 0, len, DMA_TO_DEVICE);
  350. dma_flags = 0;
  351. unmap_len = len;
  352. unmap_addr = dma_addr;
  353. if (unlikely(dma_mapping_error(dma_dev, dma_addr)))
  354. return -EIO;
  355. } while (1);
  356. }
  357. /* Remove buffers put into a tx_queue. None of the buffers must have
  358. * an skb attached.
  359. */
  360. static void efx_enqueue_unwind(struct efx_tx_queue *tx_queue)
  361. {
  362. struct efx_tx_buffer *buffer;
  363. /* Work backwards until we hit the original insert pointer value */
  364. while (tx_queue->insert_count != tx_queue->write_count) {
  365. --tx_queue->insert_count;
  366. buffer = __efx_tx_queue_get_insert_buffer(tx_queue);
  367. efx_dequeue_buffer(tx_queue, buffer, NULL, NULL);
  368. }
  369. }
  370. /*
  371. * Fallback to software TSO.
  372. *
  373. * This is used if we are unable to send a GSO packet through hardware TSO.
  374. * This should only ever happen due to per-queue restrictions - unsupported
  375. * packets should first be filtered by the feature flags.
  376. *
  377. * Returns 0 on success, error code otherwise.
  378. */
  379. static int efx_tx_tso_fallback(struct efx_tx_queue *tx_queue,
  380. struct sk_buff *skb)
  381. {
  382. struct sk_buff *segments, *next;
  383. segments = skb_gso_segment(skb, 0);
  384. if (IS_ERR(segments))
  385. return PTR_ERR(segments);
  386. dev_kfree_skb_any(skb);
  387. skb = segments;
  388. while (skb) {
  389. next = skb->next;
  390. skb->next = NULL;
  391. if (next)
  392. skb->xmit_more = true;
  393. efx_enqueue_skb(tx_queue, skb);
  394. skb = next;
  395. }
  396. return 0;
  397. }
  398. /*
  399. * Add a socket buffer to a TX queue
  400. *
  401. * This maps all fragments of a socket buffer for DMA and adds them to
  402. * the TX queue. The queue's insert pointer will be incremented by
  403. * the number of fragments in the socket buffer.
  404. *
  405. * If any DMA mapping fails, any mapped fragments will be unmapped,
  406. * the queue's insert pointer will be restored to its original value.
  407. *
  408. * This function is split out from efx_hard_start_xmit to allow the
  409. * loopback test to direct packets via specific TX queues.
  410. *
  411. * Returns NETDEV_TX_OK.
  412. * You must hold netif_tx_lock() to call this function.
  413. */
  414. netdev_tx_t efx_enqueue_skb(struct efx_tx_queue *tx_queue, struct sk_buff *skb)
  415. {
  416. bool data_mapped = false;
  417. unsigned int segments;
  418. unsigned int skb_len;
  419. int rc;
  420. skb_len = skb->len;
  421. segments = skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 0;
  422. if (segments == 1)
  423. segments = 0; /* Don't use TSO for a single segment. */
  424. /* Handle TSO first - it's *possible* (although unlikely) that we might
  425. * be passed a packet to segment that's smaller than the copybreak/PIO
  426. * size limit.
  427. */
  428. if (segments) {
  429. EFX_WARN_ON_ONCE_PARANOID(!tx_queue->handle_tso);
  430. rc = tx_queue->handle_tso(tx_queue, skb, &data_mapped);
  431. if (rc == -EINVAL) {
  432. rc = efx_tx_tso_fallback(tx_queue, skb);
  433. tx_queue->tso_fallbacks++;
  434. if (rc == 0)
  435. return 0;
  436. }
  437. if (rc)
  438. goto err;
  439. #ifdef EFX_USE_PIO
  440. } else if (skb_len <= efx_piobuf_size && !skb->xmit_more &&
  441. efx_nic_may_tx_pio(tx_queue)) {
  442. /* Use PIO for short packets with an empty queue. */
  443. if (efx_enqueue_skb_pio(tx_queue, skb))
  444. goto err;
  445. tx_queue->pio_packets++;
  446. data_mapped = true;
  447. #endif
  448. } else if (skb->data_len && skb_len <= EFX_TX_CB_SIZE) {
  449. /* Pad short packets or coalesce short fragmented packets. */
  450. if (efx_enqueue_skb_copy(tx_queue, skb))
  451. goto err;
  452. tx_queue->cb_packets++;
  453. data_mapped = true;
  454. }
  455. /* Map for DMA and create descriptors if we haven't done so already. */
  456. if (!data_mapped && (efx_tx_map_data(tx_queue, skb, segments)))
  457. goto err;
  458. /* Update BQL */
  459. netdev_tx_sent_queue(tx_queue->core_txq, skb_len);
  460. /* Pass off to hardware */
  461. if (!skb->xmit_more || netif_xmit_stopped(tx_queue->core_txq)) {
  462. struct efx_tx_queue *txq2 = efx_tx_queue_partner(tx_queue);
  463. /* There could be packets left on the partner queue if those
  464. * SKBs had skb->xmit_more set. If we do not push those they
  465. * could be left for a long time and cause a netdev watchdog.
  466. */
  467. if (txq2->xmit_more_available)
  468. efx_nic_push_buffers(txq2);
  469. efx_nic_push_buffers(tx_queue);
  470. } else {
  471. tx_queue->xmit_more_available = skb->xmit_more;
  472. }
  473. if (segments) {
  474. tx_queue->tso_bursts++;
  475. tx_queue->tso_packets += segments;
  476. tx_queue->tx_packets += segments;
  477. } else {
  478. tx_queue->tx_packets++;
  479. }
  480. efx_tx_maybe_stop_queue(tx_queue);
  481. return NETDEV_TX_OK;
  482. err:
  483. efx_enqueue_unwind(tx_queue);
  484. dev_kfree_skb_any(skb);
  485. return NETDEV_TX_OK;
  486. }
  487. /* Remove packets from the TX queue
  488. *
  489. * This removes packets from the TX queue, up to and including the
  490. * specified index.
  491. */
  492. static void efx_dequeue_buffers(struct efx_tx_queue *tx_queue,
  493. unsigned int index,
  494. unsigned int *pkts_compl,
  495. unsigned int *bytes_compl)
  496. {
  497. struct efx_nic *efx = tx_queue->efx;
  498. unsigned int stop_index, read_ptr;
  499. stop_index = (index + 1) & tx_queue->ptr_mask;
  500. read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
  501. while (read_ptr != stop_index) {
  502. struct efx_tx_buffer *buffer = &tx_queue->buffer[read_ptr];
  503. if (!(buffer->flags & EFX_TX_BUF_OPTION) &&
  504. unlikely(buffer->len == 0)) {
  505. netif_err(efx, tx_err, efx->net_dev,
  506. "TX queue %d spurious TX completion id %x\n",
  507. tx_queue->queue, read_ptr);
  508. efx_schedule_reset(efx, RESET_TYPE_TX_SKIP);
  509. return;
  510. }
  511. efx_dequeue_buffer(tx_queue, buffer, pkts_compl, bytes_compl);
  512. ++tx_queue->read_count;
  513. read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
  514. }
  515. }
  516. /* Initiate a packet transmission. We use one channel per CPU
  517. * (sharing when we have more CPUs than channels). On Falcon, the TX
  518. * completion events will be directed back to the CPU that transmitted
  519. * the packet, which should be cache-efficient.
  520. *
  521. * Context: non-blocking.
  522. * Note that returning anything other than NETDEV_TX_OK will cause the
  523. * OS to free the skb.
  524. */
  525. netdev_tx_t efx_hard_start_xmit(struct sk_buff *skb,
  526. struct net_device *net_dev)
  527. {
  528. struct efx_nic *efx = netdev_priv(net_dev);
  529. struct efx_tx_queue *tx_queue;
  530. unsigned index, type;
  531. EFX_WARN_ON_PARANOID(!netif_device_present(net_dev));
  532. /* PTP "event" packet */
  533. if (unlikely(efx_xmit_with_hwtstamp(skb)) &&
  534. unlikely(efx_ptp_is_ptp_tx(efx, skb))) {
  535. return efx_ptp_tx(efx, skb);
  536. }
  537. index = skb_get_queue_mapping(skb);
  538. type = skb->ip_summed == CHECKSUM_PARTIAL ? EFX_TXQ_TYPE_OFFLOAD : 0;
  539. if (index >= efx->n_tx_channels) {
  540. index -= efx->n_tx_channels;
  541. type |= EFX_TXQ_TYPE_HIGHPRI;
  542. }
  543. tx_queue = efx_get_tx_queue(efx, index, type);
  544. return efx_enqueue_skb(tx_queue, skb);
  545. }
  546. void efx_init_tx_queue_core_txq(struct efx_tx_queue *tx_queue)
  547. {
  548. struct efx_nic *efx = tx_queue->efx;
  549. /* Must be inverse of queue lookup in efx_hard_start_xmit() */
  550. tx_queue->core_txq =
  551. netdev_get_tx_queue(efx->net_dev,
  552. tx_queue->queue / EFX_TXQ_TYPES +
  553. ((tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI) ?
  554. efx->n_tx_channels : 0));
  555. }
  556. int efx_setup_tc(struct net_device *net_dev, u32 handle, __be16 proto,
  557. struct tc_to_netdev *ntc)
  558. {
  559. struct efx_nic *efx = netdev_priv(net_dev);
  560. struct efx_channel *channel;
  561. struct efx_tx_queue *tx_queue;
  562. unsigned tc, num_tc;
  563. int rc;
  564. if (ntc->type != TC_SETUP_MQPRIO)
  565. return -EINVAL;
  566. num_tc = ntc->tc;
  567. if (num_tc > EFX_MAX_TX_TC)
  568. return -EINVAL;
  569. if (num_tc == net_dev->num_tc)
  570. return 0;
  571. for (tc = 0; tc < num_tc; tc++) {
  572. net_dev->tc_to_txq[tc].offset = tc * efx->n_tx_channels;
  573. net_dev->tc_to_txq[tc].count = efx->n_tx_channels;
  574. }
  575. if (num_tc > net_dev->num_tc) {
  576. /* Initialise high-priority queues as necessary */
  577. efx_for_each_channel(channel, efx) {
  578. efx_for_each_possible_channel_tx_queue(tx_queue,
  579. channel) {
  580. if (!(tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI))
  581. continue;
  582. if (!tx_queue->buffer) {
  583. rc = efx_probe_tx_queue(tx_queue);
  584. if (rc)
  585. return rc;
  586. }
  587. if (!tx_queue->initialised)
  588. efx_init_tx_queue(tx_queue);
  589. efx_init_tx_queue_core_txq(tx_queue);
  590. }
  591. }
  592. } else {
  593. /* Reduce number of classes before number of queues */
  594. net_dev->num_tc = num_tc;
  595. }
  596. rc = netif_set_real_num_tx_queues(net_dev,
  597. max_t(int, num_tc, 1) *
  598. efx->n_tx_channels);
  599. if (rc)
  600. return rc;
  601. /* Do not destroy high-priority queues when they become
  602. * unused. We would have to flush them first, and it is
  603. * fairly difficult to flush a subset of TX queues. Leave
  604. * it to efx_fini_channels().
  605. */
  606. net_dev->num_tc = num_tc;
  607. return 0;
  608. }
  609. void efx_xmit_done(struct efx_tx_queue *tx_queue, unsigned int index)
  610. {
  611. unsigned fill_level;
  612. struct efx_nic *efx = tx_queue->efx;
  613. struct efx_tx_queue *txq2;
  614. unsigned int pkts_compl = 0, bytes_compl = 0;
  615. EFX_WARN_ON_ONCE_PARANOID(index > tx_queue->ptr_mask);
  616. efx_dequeue_buffers(tx_queue, index, &pkts_compl, &bytes_compl);
  617. tx_queue->pkts_compl += pkts_compl;
  618. tx_queue->bytes_compl += bytes_compl;
  619. if (pkts_compl > 1)
  620. ++tx_queue->merge_events;
  621. /* See if we need to restart the netif queue. This memory
  622. * barrier ensures that we write read_count (inside
  623. * efx_dequeue_buffers()) before reading the queue status.
  624. */
  625. smp_mb();
  626. if (unlikely(netif_tx_queue_stopped(tx_queue->core_txq)) &&
  627. likely(efx->port_enabled) &&
  628. likely(netif_device_present(efx->net_dev))) {
  629. txq2 = efx_tx_queue_partner(tx_queue);
  630. fill_level = max(tx_queue->insert_count - tx_queue->read_count,
  631. txq2->insert_count - txq2->read_count);
  632. if (fill_level <= efx->txq_wake_thresh)
  633. netif_tx_wake_queue(tx_queue->core_txq);
  634. }
  635. /* Check whether the hardware queue is now empty */
  636. if ((int)(tx_queue->read_count - tx_queue->old_write_count) >= 0) {
  637. tx_queue->old_write_count = ACCESS_ONCE(tx_queue->write_count);
  638. if (tx_queue->read_count == tx_queue->old_write_count) {
  639. smp_mb();
  640. tx_queue->empty_read_count =
  641. tx_queue->read_count | EFX_EMPTY_COUNT_VALID;
  642. }
  643. }
  644. }
  645. static unsigned int efx_tx_cb_page_count(struct efx_tx_queue *tx_queue)
  646. {
  647. return DIV_ROUND_UP(tx_queue->ptr_mask + 1, PAGE_SIZE >> EFX_TX_CB_ORDER);
  648. }
  649. int efx_probe_tx_queue(struct efx_tx_queue *tx_queue)
  650. {
  651. struct efx_nic *efx = tx_queue->efx;
  652. unsigned int entries;
  653. int rc;
  654. /* Create the smallest power-of-two aligned ring */
  655. entries = max(roundup_pow_of_two(efx->txq_entries), EFX_MIN_DMAQ_SIZE);
  656. EFX_WARN_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE);
  657. tx_queue->ptr_mask = entries - 1;
  658. netif_dbg(efx, probe, efx->net_dev,
  659. "creating TX queue %d size %#x mask %#x\n",
  660. tx_queue->queue, efx->txq_entries, tx_queue->ptr_mask);
  661. /* Allocate software ring */
  662. tx_queue->buffer = kcalloc(entries, sizeof(*tx_queue->buffer),
  663. GFP_KERNEL);
  664. if (!tx_queue->buffer)
  665. return -ENOMEM;
  666. tx_queue->cb_page = kcalloc(efx_tx_cb_page_count(tx_queue),
  667. sizeof(tx_queue->cb_page[0]), GFP_KERNEL);
  668. if (!tx_queue->cb_page) {
  669. rc = -ENOMEM;
  670. goto fail1;
  671. }
  672. /* Allocate hardware ring */
  673. rc = efx_nic_probe_tx(tx_queue);
  674. if (rc)
  675. goto fail2;
  676. return 0;
  677. fail2:
  678. kfree(tx_queue->cb_page);
  679. tx_queue->cb_page = NULL;
  680. fail1:
  681. kfree(tx_queue->buffer);
  682. tx_queue->buffer = NULL;
  683. return rc;
  684. }
  685. void efx_init_tx_queue(struct efx_tx_queue *tx_queue)
  686. {
  687. struct efx_nic *efx = tx_queue->efx;
  688. netif_dbg(efx, drv, efx->net_dev,
  689. "initialising TX queue %d\n", tx_queue->queue);
  690. tx_queue->insert_count = 0;
  691. tx_queue->write_count = 0;
  692. tx_queue->packet_write_count = 0;
  693. tx_queue->old_write_count = 0;
  694. tx_queue->read_count = 0;
  695. tx_queue->old_read_count = 0;
  696. tx_queue->empty_read_count = 0 | EFX_EMPTY_COUNT_VALID;
  697. tx_queue->xmit_more_available = false;
  698. /* Set up default function pointers. These may get replaced by
  699. * efx_nic_init_tx() based off NIC/queue capabilities.
  700. */
  701. tx_queue->handle_tso = efx_enqueue_skb_tso;
  702. /* Set up TX descriptor ring */
  703. efx_nic_init_tx(tx_queue);
  704. tx_queue->initialised = true;
  705. }
  706. void efx_fini_tx_queue(struct efx_tx_queue *tx_queue)
  707. {
  708. struct efx_tx_buffer *buffer;
  709. netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
  710. "shutting down TX queue %d\n", tx_queue->queue);
  711. if (!tx_queue->buffer)
  712. return;
  713. /* Free any buffers left in the ring */
  714. while (tx_queue->read_count != tx_queue->write_count) {
  715. unsigned int pkts_compl = 0, bytes_compl = 0;
  716. buffer = &tx_queue->buffer[tx_queue->read_count & tx_queue->ptr_mask];
  717. efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl);
  718. ++tx_queue->read_count;
  719. }
  720. tx_queue->xmit_more_available = false;
  721. netdev_tx_reset_queue(tx_queue->core_txq);
  722. }
  723. void efx_remove_tx_queue(struct efx_tx_queue *tx_queue)
  724. {
  725. int i;
  726. if (!tx_queue->buffer)
  727. return;
  728. netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
  729. "destroying TX queue %d\n", tx_queue->queue);
  730. efx_nic_remove_tx(tx_queue);
  731. if (tx_queue->cb_page) {
  732. for (i = 0; i < efx_tx_cb_page_count(tx_queue); i++)
  733. efx_nic_free_buffer(tx_queue->efx,
  734. &tx_queue->cb_page[i]);
  735. kfree(tx_queue->cb_page);
  736. tx_queue->cb_page = NULL;
  737. }
  738. kfree(tx_queue->buffer);
  739. tx_queue->buffer = NULL;
  740. }