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