tx.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  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 "workarounds.h"
  25. #include "ef10_regs.h"
  26. #ifdef EFX_USE_PIO
  27. #define EFX_PIOBUF_SIZE_MAX ER_DZ_TX_PIOBUF_SIZE
  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 unsigned int
  32. efx_tx_queue_get_insert_index(const struct efx_tx_queue *tx_queue)
  33. {
  34. return tx_queue->insert_count & tx_queue->ptr_mask;
  35. }
  36. static inline struct efx_tx_buffer *
  37. __efx_tx_queue_get_insert_buffer(const struct efx_tx_queue *tx_queue)
  38. {
  39. return &tx_queue->buffer[efx_tx_queue_get_insert_index(tx_queue)];
  40. }
  41. static inline struct efx_tx_buffer *
  42. efx_tx_queue_get_insert_buffer(const struct efx_tx_queue *tx_queue)
  43. {
  44. struct efx_tx_buffer *buffer =
  45. __efx_tx_queue_get_insert_buffer(tx_queue);
  46. EFX_BUG_ON_PARANOID(buffer->len);
  47. EFX_BUG_ON_PARANOID(buffer->flags);
  48. EFX_BUG_ON_PARANOID(buffer->unmap_len);
  49. return buffer;
  50. }
  51. static void efx_dequeue_buffer(struct efx_tx_queue *tx_queue,
  52. struct efx_tx_buffer *buffer,
  53. unsigned int *pkts_compl,
  54. unsigned int *bytes_compl)
  55. {
  56. if (buffer->unmap_len) {
  57. struct device *dma_dev = &tx_queue->efx->pci_dev->dev;
  58. dma_addr_t unmap_addr = buffer->dma_addr - buffer->dma_offset;
  59. if (buffer->flags & EFX_TX_BUF_MAP_SINGLE)
  60. dma_unmap_single(dma_dev, unmap_addr, buffer->unmap_len,
  61. DMA_TO_DEVICE);
  62. else
  63. dma_unmap_page(dma_dev, unmap_addr, buffer->unmap_len,
  64. DMA_TO_DEVICE);
  65. buffer->unmap_len = 0;
  66. }
  67. if (buffer->flags & EFX_TX_BUF_SKB) {
  68. (*pkts_compl)++;
  69. (*bytes_compl) += buffer->skb->len;
  70. dev_consume_skb_any((struct sk_buff *)buffer->skb);
  71. netif_vdbg(tx_queue->efx, tx_done, tx_queue->efx->net_dev,
  72. "TX queue %d transmission id %x complete\n",
  73. tx_queue->queue, tx_queue->read_count);
  74. } else if (buffer->flags & EFX_TX_BUF_HEAP) {
  75. kfree(buffer->heap_buf);
  76. }
  77. buffer->len = 0;
  78. buffer->flags = 0;
  79. }
  80. static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
  81. struct sk_buff *skb);
  82. static inline unsigned
  83. efx_max_tx_len(struct efx_nic *efx, dma_addr_t dma_addr)
  84. {
  85. /* Depending on the NIC revision, we can use descriptor
  86. * lengths up to 8K or 8K-1. However, since PCI Express
  87. * devices must split read requests at 4K boundaries, there is
  88. * little benefit from using descriptors that cross those
  89. * boundaries and we keep things simple by not doing so.
  90. */
  91. unsigned len = (~dma_addr & (EFX_PAGE_SIZE - 1)) + 1;
  92. /* Work around hardware bug for unaligned buffers. */
  93. if (EFX_WORKAROUND_5391(efx) && (dma_addr & 0xf))
  94. len = min_t(unsigned, len, 512 - (dma_addr & 0xf));
  95. return len;
  96. }
  97. unsigned int efx_tx_max_skb_descs(struct efx_nic *efx)
  98. {
  99. /* Header and payload descriptor for each output segment, plus
  100. * one for every input fragment boundary within a segment
  101. */
  102. unsigned int max_descs = EFX_TSO_MAX_SEGS * 2 + MAX_SKB_FRAGS;
  103. /* Possibly one more per segment for the alignment workaround,
  104. * or for option descriptors
  105. */
  106. if (EFX_WORKAROUND_5391(efx) || efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
  107. max_descs += EFX_TSO_MAX_SEGS;
  108. /* Possibly more for PCIe page boundaries within input fragments */
  109. if (PAGE_SIZE > EFX_PAGE_SIZE)
  110. max_descs += max_t(unsigned int, MAX_SKB_FRAGS,
  111. DIV_ROUND_UP(GSO_MAX_SIZE, EFX_PAGE_SIZE));
  112. return max_descs;
  113. }
  114. static void efx_tx_maybe_stop_queue(struct efx_tx_queue *txq1)
  115. {
  116. /* We need to consider both queues that the net core sees as one */
  117. struct efx_tx_queue *txq2 = efx_tx_queue_partner(txq1);
  118. struct efx_nic *efx = txq1->efx;
  119. unsigned int fill_level;
  120. fill_level = max(txq1->insert_count - txq1->old_read_count,
  121. txq2->insert_count - txq2->old_read_count);
  122. if (likely(fill_level < efx->txq_stop_thresh))
  123. return;
  124. /* We used the stale old_read_count above, which gives us a
  125. * pessimistic estimate of the fill level (which may even
  126. * validly be >= efx->txq_entries). Now try again using
  127. * read_count (more likely to be a cache miss).
  128. *
  129. * If we read read_count and then conditionally stop the
  130. * queue, it is possible for the completion path to race with
  131. * us and complete all outstanding descriptors in the middle,
  132. * after which there will be no more completions to wake it.
  133. * Therefore we stop the queue first, then read read_count
  134. * (with a memory barrier to ensure the ordering), then
  135. * restart the queue if the fill level turns out to be low
  136. * enough.
  137. */
  138. netif_tx_stop_queue(txq1->core_txq);
  139. smp_mb();
  140. txq1->old_read_count = ACCESS_ONCE(txq1->read_count);
  141. txq2->old_read_count = ACCESS_ONCE(txq2->read_count);
  142. fill_level = max(txq1->insert_count - txq1->old_read_count,
  143. txq2->insert_count - txq2->old_read_count);
  144. EFX_BUG_ON_PARANOID(fill_level >= efx->txq_entries);
  145. if (likely(fill_level < efx->txq_stop_thresh)) {
  146. smp_mb();
  147. if (likely(!efx->loopback_selftest))
  148. netif_tx_start_queue(txq1->core_txq);
  149. }
  150. }
  151. #ifdef EFX_USE_PIO
  152. struct efx_short_copy_buffer {
  153. int used;
  154. u8 buf[L1_CACHE_BYTES];
  155. };
  156. /* Copy to PIO, respecting that writes to PIO buffers must be dword aligned.
  157. * Advances piobuf pointer. Leaves additional data in the copy buffer.
  158. */
  159. static void efx_memcpy_toio_aligned(struct efx_nic *efx, u8 __iomem **piobuf,
  160. u8 *data, int len,
  161. struct efx_short_copy_buffer *copy_buf)
  162. {
  163. int block_len = len & ~(sizeof(copy_buf->buf) - 1);
  164. __iowrite64_copy(*piobuf, data, block_len >> 3);
  165. *piobuf += block_len;
  166. len -= block_len;
  167. if (len) {
  168. data += block_len;
  169. BUG_ON(copy_buf->used);
  170. BUG_ON(len > sizeof(copy_buf->buf));
  171. memcpy(copy_buf->buf, data, len);
  172. copy_buf->used = len;
  173. }
  174. }
  175. /* Copy to PIO, respecting dword alignment, popping data from copy buffer first.
  176. * Advances piobuf pointer. Leaves additional data in the copy buffer.
  177. */
  178. static void efx_memcpy_toio_aligned_cb(struct efx_nic *efx, u8 __iomem **piobuf,
  179. u8 *data, int len,
  180. struct efx_short_copy_buffer *copy_buf)
  181. {
  182. if (copy_buf->used) {
  183. /* if the copy buffer is partially full, fill it up and write */
  184. int copy_to_buf =
  185. min_t(int, sizeof(copy_buf->buf) - copy_buf->used, len);
  186. memcpy(copy_buf->buf + copy_buf->used, data, copy_to_buf);
  187. copy_buf->used += copy_to_buf;
  188. /* if we didn't fill it up then we're done for now */
  189. if (copy_buf->used < sizeof(copy_buf->buf))
  190. return;
  191. __iowrite64_copy(*piobuf, copy_buf->buf,
  192. sizeof(copy_buf->buf) >> 3);
  193. *piobuf += sizeof(copy_buf->buf);
  194. data += copy_to_buf;
  195. len -= copy_to_buf;
  196. copy_buf->used = 0;
  197. }
  198. efx_memcpy_toio_aligned(efx, piobuf, data, len, copy_buf);
  199. }
  200. static void efx_flush_copy_buffer(struct efx_nic *efx, u8 __iomem *piobuf,
  201. struct efx_short_copy_buffer *copy_buf)
  202. {
  203. /* if there's anything in it, write the whole buffer, including junk */
  204. if (copy_buf->used)
  205. __iowrite64_copy(piobuf, copy_buf->buf,
  206. sizeof(copy_buf->buf) >> 3);
  207. }
  208. /* Traverse skb structure and copy fragments in to PIO buffer.
  209. * Advances piobuf pointer.
  210. */
  211. static void efx_skb_copy_bits_to_pio(struct efx_nic *efx, struct sk_buff *skb,
  212. u8 __iomem **piobuf,
  213. struct efx_short_copy_buffer *copy_buf)
  214. {
  215. int i;
  216. efx_memcpy_toio_aligned(efx, piobuf, skb->data, skb_headlen(skb),
  217. copy_buf);
  218. for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
  219. skb_frag_t *f = &skb_shinfo(skb)->frags[i];
  220. u8 *vaddr;
  221. vaddr = kmap_atomic(skb_frag_page(f));
  222. efx_memcpy_toio_aligned_cb(efx, piobuf, vaddr + f->page_offset,
  223. skb_frag_size(f), copy_buf);
  224. kunmap_atomic(vaddr);
  225. }
  226. EFX_BUG_ON_PARANOID(skb_shinfo(skb)->frag_list);
  227. }
  228. static struct efx_tx_buffer *
  229. efx_enqueue_skb_pio(struct efx_tx_queue *tx_queue, struct sk_buff *skb)
  230. {
  231. struct efx_tx_buffer *buffer =
  232. efx_tx_queue_get_insert_buffer(tx_queue);
  233. u8 __iomem *piobuf = tx_queue->piobuf;
  234. /* Copy to PIO buffer. Ensure the writes are padded to the end
  235. * of a cache line, as this is required for write-combining to be
  236. * effective on at least x86.
  237. */
  238. if (skb_shinfo(skb)->nr_frags) {
  239. /* The size of the copy buffer will ensure all writes
  240. * are the size of a cache line.
  241. */
  242. struct efx_short_copy_buffer copy_buf;
  243. copy_buf.used = 0;
  244. efx_skb_copy_bits_to_pio(tx_queue->efx, skb,
  245. &piobuf, &copy_buf);
  246. efx_flush_copy_buffer(tx_queue->efx, piobuf, &copy_buf);
  247. } else {
  248. /* Pad the write to the size of a cache line.
  249. * We can do this because we know the skb_shared_info sruct is
  250. * after the source, and the destination buffer is big enough.
  251. */
  252. BUILD_BUG_ON(L1_CACHE_BYTES >
  253. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
  254. __iowrite64_copy(tx_queue->piobuf, skb->data,
  255. ALIGN(skb->len, L1_CACHE_BYTES) >> 3);
  256. }
  257. EFX_POPULATE_QWORD_5(buffer->option,
  258. ESF_DZ_TX_DESC_IS_OPT, 1,
  259. ESF_DZ_TX_OPTION_TYPE, ESE_DZ_TX_OPTION_DESC_PIO,
  260. ESF_DZ_TX_PIO_CONT, 0,
  261. ESF_DZ_TX_PIO_BYTE_CNT, skb->len,
  262. ESF_DZ_TX_PIO_BUF_ADDR,
  263. tx_queue->piobuf_offset);
  264. ++tx_queue->pio_packets;
  265. ++tx_queue->insert_count;
  266. return buffer;
  267. }
  268. #endif /* EFX_USE_PIO */
  269. /*
  270. * Add a socket buffer to a TX queue
  271. *
  272. * This maps all fragments of a socket buffer for DMA and adds them to
  273. * the TX queue. The queue's insert pointer will be incremented by
  274. * the number of fragments in the socket buffer.
  275. *
  276. * If any DMA mapping fails, any mapped fragments will be unmapped,
  277. * the queue's insert pointer will be restored to its original value.
  278. *
  279. * This function is split out from efx_hard_start_xmit to allow the
  280. * loopback test to direct packets via specific TX queues.
  281. *
  282. * Returns NETDEV_TX_OK.
  283. * You must hold netif_tx_lock() to call this function.
  284. */
  285. netdev_tx_t efx_enqueue_skb(struct efx_tx_queue *tx_queue, struct sk_buff *skb)
  286. {
  287. struct efx_nic *efx = tx_queue->efx;
  288. struct device *dma_dev = &efx->pci_dev->dev;
  289. struct efx_tx_buffer *buffer;
  290. unsigned int old_insert_count = tx_queue->insert_count;
  291. skb_frag_t *fragment;
  292. unsigned int len, unmap_len = 0;
  293. dma_addr_t dma_addr, unmap_addr = 0;
  294. unsigned int dma_len;
  295. unsigned short dma_flags;
  296. int i = 0;
  297. if (skb_shinfo(skb)->gso_size)
  298. return efx_enqueue_skb_tso(tx_queue, skb);
  299. /* Get size of the initial fragment */
  300. len = skb_headlen(skb);
  301. /* Pad if necessary */
  302. if (EFX_WORKAROUND_15592(efx) && skb->len <= 32) {
  303. EFX_BUG_ON_PARANOID(skb->data_len);
  304. len = 32 + 1;
  305. if (skb_pad(skb, len - skb->len))
  306. return NETDEV_TX_OK;
  307. }
  308. /* Consider using PIO for short packets */
  309. #ifdef EFX_USE_PIO
  310. if (skb->len <= efx_piobuf_size && !skb->xmit_more &&
  311. efx_nic_may_tx_pio(tx_queue)) {
  312. buffer = efx_enqueue_skb_pio(tx_queue, skb);
  313. dma_flags = EFX_TX_BUF_OPTION;
  314. goto finish_packet;
  315. }
  316. #endif
  317. /* Map for DMA. Use dma_map_single rather than dma_map_page
  318. * since this is more efficient on machines with sparse
  319. * memory.
  320. */
  321. dma_flags = EFX_TX_BUF_MAP_SINGLE;
  322. dma_addr = dma_map_single(dma_dev, skb->data, len, PCI_DMA_TODEVICE);
  323. /* Process all fragments */
  324. while (1) {
  325. if (unlikely(dma_mapping_error(dma_dev, dma_addr)))
  326. goto dma_err;
  327. /* Store fields for marking in the per-fragment final
  328. * descriptor */
  329. unmap_len = len;
  330. unmap_addr = dma_addr;
  331. /* Add to TX queue, splitting across DMA boundaries */
  332. do {
  333. buffer = efx_tx_queue_get_insert_buffer(tx_queue);
  334. dma_len = efx_max_tx_len(efx, dma_addr);
  335. if (likely(dma_len >= len))
  336. dma_len = len;
  337. /* Fill out per descriptor fields */
  338. buffer->len = dma_len;
  339. buffer->dma_addr = dma_addr;
  340. buffer->flags = EFX_TX_BUF_CONT;
  341. len -= dma_len;
  342. dma_addr += dma_len;
  343. ++tx_queue->insert_count;
  344. } while (len);
  345. /* Transfer ownership of the unmapping to the final buffer */
  346. buffer->flags = EFX_TX_BUF_CONT | dma_flags;
  347. buffer->unmap_len = unmap_len;
  348. buffer->dma_offset = buffer->dma_addr - unmap_addr;
  349. unmap_len = 0;
  350. /* Get address and size of next fragment */
  351. if (i >= skb_shinfo(skb)->nr_frags)
  352. break;
  353. fragment = &skb_shinfo(skb)->frags[i];
  354. len = skb_frag_size(fragment);
  355. i++;
  356. /* Map for DMA */
  357. dma_flags = 0;
  358. dma_addr = skb_frag_dma_map(dma_dev, fragment, 0, len,
  359. DMA_TO_DEVICE);
  360. }
  361. /* Transfer ownership of the skb to the final buffer */
  362. #ifdef EFX_USE_PIO
  363. finish_packet:
  364. #endif
  365. buffer->skb = skb;
  366. buffer->flags = EFX_TX_BUF_SKB | dma_flags;
  367. netdev_tx_sent_queue(tx_queue->core_txq, skb->len);
  368. efx_tx_maybe_stop_queue(tx_queue);
  369. /* Pass off to hardware */
  370. if (!skb->xmit_more || netif_xmit_stopped(tx_queue->core_txq))
  371. efx_nic_push_buffers(tx_queue);
  372. tx_queue->tx_packets++;
  373. return NETDEV_TX_OK;
  374. dma_err:
  375. netif_err(efx, tx_err, efx->net_dev,
  376. " TX queue %d could not map skb with %d bytes %d "
  377. "fragments for DMA\n", tx_queue->queue, skb->len,
  378. skb_shinfo(skb)->nr_frags + 1);
  379. /* Mark the packet as transmitted, and free the SKB ourselves */
  380. dev_kfree_skb_any(skb);
  381. /* Work backwards until we hit the original insert pointer value */
  382. while (tx_queue->insert_count != old_insert_count) {
  383. unsigned int pkts_compl = 0, bytes_compl = 0;
  384. --tx_queue->insert_count;
  385. buffer = __efx_tx_queue_get_insert_buffer(tx_queue);
  386. efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl);
  387. }
  388. /* Free the fragment we were mid-way through pushing */
  389. if (unmap_len) {
  390. if (dma_flags & EFX_TX_BUF_MAP_SINGLE)
  391. dma_unmap_single(dma_dev, unmap_addr, unmap_len,
  392. DMA_TO_DEVICE);
  393. else
  394. dma_unmap_page(dma_dev, unmap_addr, unmap_len,
  395. DMA_TO_DEVICE);
  396. }
  397. return NETDEV_TX_OK;
  398. }
  399. /* Remove packets from the TX queue
  400. *
  401. * This removes packets from the TX queue, up to and including the
  402. * specified index.
  403. */
  404. static void efx_dequeue_buffers(struct efx_tx_queue *tx_queue,
  405. unsigned int index,
  406. unsigned int *pkts_compl,
  407. unsigned int *bytes_compl)
  408. {
  409. struct efx_nic *efx = tx_queue->efx;
  410. unsigned int stop_index, read_ptr;
  411. stop_index = (index + 1) & tx_queue->ptr_mask;
  412. read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
  413. while (read_ptr != stop_index) {
  414. struct efx_tx_buffer *buffer = &tx_queue->buffer[read_ptr];
  415. if (!(buffer->flags & EFX_TX_BUF_OPTION) &&
  416. unlikely(buffer->len == 0)) {
  417. netif_err(efx, tx_err, efx->net_dev,
  418. "TX queue %d spurious TX completion id %x\n",
  419. tx_queue->queue, read_ptr);
  420. efx_schedule_reset(efx, RESET_TYPE_TX_SKIP);
  421. return;
  422. }
  423. efx_dequeue_buffer(tx_queue, buffer, pkts_compl, bytes_compl);
  424. ++tx_queue->read_count;
  425. read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
  426. }
  427. }
  428. /* Initiate a packet transmission. We use one channel per CPU
  429. * (sharing when we have more CPUs than channels). On Falcon, the TX
  430. * completion events will be directed back to the CPU that transmitted
  431. * the packet, which should be cache-efficient.
  432. *
  433. * Context: non-blocking.
  434. * Note that returning anything other than NETDEV_TX_OK will cause the
  435. * OS to free the skb.
  436. */
  437. netdev_tx_t efx_hard_start_xmit(struct sk_buff *skb,
  438. struct net_device *net_dev)
  439. {
  440. struct efx_nic *efx = netdev_priv(net_dev);
  441. struct efx_tx_queue *tx_queue;
  442. unsigned index, type;
  443. EFX_WARN_ON_PARANOID(!netif_device_present(net_dev));
  444. /* PTP "event" packet */
  445. if (unlikely(efx_xmit_with_hwtstamp(skb)) &&
  446. unlikely(efx_ptp_is_ptp_tx(efx, skb))) {
  447. return efx_ptp_tx(efx, skb);
  448. }
  449. index = skb_get_queue_mapping(skb);
  450. type = skb->ip_summed == CHECKSUM_PARTIAL ? EFX_TXQ_TYPE_OFFLOAD : 0;
  451. if (index >= efx->n_tx_channels) {
  452. index -= efx->n_tx_channels;
  453. type |= EFX_TXQ_TYPE_HIGHPRI;
  454. }
  455. tx_queue = efx_get_tx_queue(efx, index, type);
  456. return efx_enqueue_skb(tx_queue, skb);
  457. }
  458. void efx_init_tx_queue_core_txq(struct efx_tx_queue *tx_queue)
  459. {
  460. struct efx_nic *efx = tx_queue->efx;
  461. /* Must be inverse of queue lookup in efx_hard_start_xmit() */
  462. tx_queue->core_txq =
  463. netdev_get_tx_queue(efx->net_dev,
  464. tx_queue->queue / EFX_TXQ_TYPES +
  465. ((tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI) ?
  466. efx->n_tx_channels : 0));
  467. }
  468. int efx_setup_tc(struct net_device *net_dev, u8 num_tc)
  469. {
  470. struct efx_nic *efx = netdev_priv(net_dev);
  471. struct efx_channel *channel;
  472. struct efx_tx_queue *tx_queue;
  473. unsigned tc;
  474. int rc;
  475. if (efx_nic_rev(efx) < EFX_REV_FALCON_B0 || num_tc > EFX_MAX_TX_TC)
  476. return -EINVAL;
  477. if (num_tc == net_dev->num_tc)
  478. return 0;
  479. for (tc = 0; tc < num_tc; tc++) {
  480. net_dev->tc_to_txq[tc].offset = tc * efx->n_tx_channels;
  481. net_dev->tc_to_txq[tc].count = efx->n_tx_channels;
  482. }
  483. if (num_tc > net_dev->num_tc) {
  484. /* Initialise high-priority queues as necessary */
  485. efx_for_each_channel(channel, efx) {
  486. efx_for_each_possible_channel_tx_queue(tx_queue,
  487. channel) {
  488. if (!(tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI))
  489. continue;
  490. if (!tx_queue->buffer) {
  491. rc = efx_probe_tx_queue(tx_queue);
  492. if (rc)
  493. return rc;
  494. }
  495. if (!tx_queue->initialised)
  496. efx_init_tx_queue(tx_queue);
  497. efx_init_tx_queue_core_txq(tx_queue);
  498. }
  499. }
  500. } else {
  501. /* Reduce number of classes before number of queues */
  502. net_dev->num_tc = num_tc;
  503. }
  504. rc = netif_set_real_num_tx_queues(net_dev,
  505. max_t(int, num_tc, 1) *
  506. efx->n_tx_channels);
  507. if (rc)
  508. return rc;
  509. /* Do not destroy high-priority queues when they become
  510. * unused. We would have to flush them first, and it is
  511. * fairly difficult to flush a subset of TX queues. Leave
  512. * it to efx_fini_channels().
  513. */
  514. net_dev->num_tc = num_tc;
  515. return 0;
  516. }
  517. void efx_xmit_done(struct efx_tx_queue *tx_queue, unsigned int index)
  518. {
  519. unsigned fill_level;
  520. struct efx_nic *efx = tx_queue->efx;
  521. struct efx_tx_queue *txq2;
  522. unsigned int pkts_compl = 0, bytes_compl = 0;
  523. EFX_BUG_ON_PARANOID(index > tx_queue->ptr_mask);
  524. efx_dequeue_buffers(tx_queue, index, &pkts_compl, &bytes_compl);
  525. tx_queue->pkts_compl += pkts_compl;
  526. tx_queue->bytes_compl += bytes_compl;
  527. if (pkts_compl > 1)
  528. ++tx_queue->merge_events;
  529. /* See if we need to restart the netif queue. This memory
  530. * barrier ensures that we write read_count (inside
  531. * efx_dequeue_buffers()) before reading the queue status.
  532. */
  533. smp_mb();
  534. if (unlikely(netif_tx_queue_stopped(tx_queue->core_txq)) &&
  535. likely(efx->port_enabled) &&
  536. likely(netif_device_present(efx->net_dev))) {
  537. txq2 = efx_tx_queue_partner(tx_queue);
  538. fill_level = max(tx_queue->insert_count - tx_queue->read_count,
  539. txq2->insert_count - txq2->read_count);
  540. if (fill_level <= efx->txq_wake_thresh)
  541. netif_tx_wake_queue(tx_queue->core_txq);
  542. }
  543. /* Check whether the hardware queue is now empty */
  544. if ((int)(tx_queue->read_count - tx_queue->old_write_count) >= 0) {
  545. tx_queue->old_write_count = ACCESS_ONCE(tx_queue->write_count);
  546. if (tx_queue->read_count == tx_queue->old_write_count) {
  547. smp_mb();
  548. tx_queue->empty_read_count =
  549. tx_queue->read_count | EFX_EMPTY_COUNT_VALID;
  550. }
  551. }
  552. }
  553. /* Size of page-based TSO header buffers. Larger blocks must be
  554. * allocated from the heap.
  555. */
  556. #define TSOH_STD_SIZE 128
  557. #define TSOH_PER_PAGE (PAGE_SIZE / TSOH_STD_SIZE)
  558. /* At most half the descriptors in the queue at any time will refer to
  559. * a TSO header buffer, since they must always be followed by a
  560. * payload descriptor referring to an skb.
  561. */
  562. static unsigned int efx_tsoh_page_count(struct efx_tx_queue *tx_queue)
  563. {
  564. return DIV_ROUND_UP(tx_queue->ptr_mask + 1, 2 * TSOH_PER_PAGE);
  565. }
  566. int efx_probe_tx_queue(struct efx_tx_queue *tx_queue)
  567. {
  568. struct efx_nic *efx = tx_queue->efx;
  569. unsigned int entries;
  570. int rc;
  571. /* Create the smallest power-of-two aligned ring */
  572. entries = max(roundup_pow_of_two(efx->txq_entries), EFX_MIN_DMAQ_SIZE);
  573. EFX_BUG_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE);
  574. tx_queue->ptr_mask = entries - 1;
  575. netif_dbg(efx, probe, efx->net_dev,
  576. "creating TX queue %d size %#x mask %#x\n",
  577. tx_queue->queue, efx->txq_entries, tx_queue->ptr_mask);
  578. /* Allocate software ring */
  579. tx_queue->buffer = kcalloc(entries, sizeof(*tx_queue->buffer),
  580. GFP_KERNEL);
  581. if (!tx_queue->buffer)
  582. return -ENOMEM;
  583. if (tx_queue->queue & EFX_TXQ_TYPE_OFFLOAD) {
  584. tx_queue->tsoh_page =
  585. kcalloc(efx_tsoh_page_count(tx_queue),
  586. sizeof(tx_queue->tsoh_page[0]), GFP_KERNEL);
  587. if (!tx_queue->tsoh_page) {
  588. rc = -ENOMEM;
  589. goto fail1;
  590. }
  591. }
  592. /* Allocate hardware ring */
  593. rc = efx_nic_probe_tx(tx_queue);
  594. if (rc)
  595. goto fail2;
  596. return 0;
  597. fail2:
  598. kfree(tx_queue->tsoh_page);
  599. tx_queue->tsoh_page = NULL;
  600. fail1:
  601. kfree(tx_queue->buffer);
  602. tx_queue->buffer = NULL;
  603. return rc;
  604. }
  605. void efx_init_tx_queue(struct efx_tx_queue *tx_queue)
  606. {
  607. netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
  608. "initialising TX queue %d\n", tx_queue->queue);
  609. tx_queue->insert_count = 0;
  610. tx_queue->write_count = 0;
  611. tx_queue->old_write_count = 0;
  612. tx_queue->read_count = 0;
  613. tx_queue->old_read_count = 0;
  614. tx_queue->empty_read_count = 0 | EFX_EMPTY_COUNT_VALID;
  615. /* Set up TX descriptor ring */
  616. efx_nic_init_tx(tx_queue);
  617. tx_queue->initialised = true;
  618. }
  619. void efx_fini_tx_queue(struct efx_tx_queue *tx_queue)
  620. {
  621. struct efx_tx_buffer *buffer;
  622. netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
  623. "shutting down TX queue %d\n", tx_queue->queue);
  624. if (!tx_queue->buffer)
  625. return;
  626. /* Free any buffers left in the ring */
  627. while (tx_queue->read_count != tx_queue->write_count) {
  628. unsigned int pkts_compl = 0, bytes_compl = 0;
  629. buffer = &tx_queue->buffer[tx_queue->read_count & tx_queue->ptr_mask];
  630. efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl);
  631. ++tx_queue->read_count;
  632. }
  633. netdev_tx_reset_queue(tx_queue->core_txq);
  634. }
  635. void efx_remove_tx_queue(struct efx_tx_queue *tx_queue)
  636. {
  637. int i;
  638. if (!tx_queue->buffer)
  639. return;
  640. netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
  641. "destroying TX queue %d\n", tx_queue->queue);
  642. efx_nic_remove_tx(tx_queue);
  643. if (tx_queue->tsoh_page) {
  644. for (i = 0; i < efx_tsoh_page_count(tx_queue); i++)
  645. efx_nic_free_buffer(tx_queue->efx,
  646. &tx_queue->tsoh_page[i]);
  647. kfree(tx_queue->tsoh_page);
  648. tx_queue->tsoh_page = NULL;
  649. }
  650. kfree(tx_queue->buffer);
  651. tx_queue->buffer = NULL;
  652. }
  653. /* Efx TCP segmentation acceleration.
  654. *
  655. * Why? Because by doing it here in the driver we can go significantly
  656. * faster than the GSO.
  657. *
  658. * Requires TX checksum offload support.
  659. */
  660. #define PTR_DIFF(p1, p2) ((u8 *)(p1) - (u8 *)(p2))
  661. /**
  662. * struct tso_state - TSO state for an SKB
  663. * @out_len: Remaining length in current segment
  664. * @seqnum: Current sequence number
  665. * @ipv4_id: Current IPv4 ID, host endian
  666. * @packet_space: Remaining space in current packet
  667. * @dma_addr: DMA address of current position
  668. * @in_len: Remaining length in current SKB fragment
  669. * @unmap_len: Length of SKB fragment
  670. * @unmap_addr: DMA address of SKB fragment
  671. * @dma_flags: TX buffer flags for DMA mapping - %EFX_TX_BUF_MAP_SINGLE or 0
  672. * @protocol: Network protocol (after any VLAN header)
  673. * @ip_off: Offset of IP header
  674. * @tcp_off: Offset of TCP header
  675. * @header_len: Number of bytes of header
  676. * @ip_base_len: IPv4 tot_len or IPv6 payload_len, before TCP payload
  677. * @header_dma_addr: Header DMA address, when using option descriptors
  678. * @header_unmap_len: Header DMA mapped length, or 0 if not using option
  679. * descriptors
  680. *
  681. * The state used during segmentation. It is put into this data structure
  682. * just to make it easy to pass into inline functions.
  683. */
  684. struct tso_state {
  685. /* Output position */
  686. unsigned out_len;
  687. unsigned seqnum;
  688. u16 ipv4_id;
  689. unsigned packet_space;
  690. /* Input position */
  691. dma_addr_t dma_addr;
  692. unsigned in_len;
  693. unsigned unmap_len;
  694. dma_addr_t unmap_addr;
  695. unsigned short dma_flags;
  696. __be16 protocol;
  697. unsigned int ip_off;
  698. unsigned int tcp_off;
  699. unsigned header_len;
  700. unsigned int ip_base_len;
  701. dma_addr_t header_dma_addr;
  702. unsigned int header_unmap_len;
  703. };
  704. /*
  705. * Verify that our various assumptions about sk_buffs and the conditions
  706. * under which TSO will be attempted hold true. Return the protocol number.
  707. */
  708. static __be16 efx_tso_check_protocol(struct sk_buff *skb)
  709. {
  710. __be16 protocol = skb->protocol;
  711. EFX_BUG_ON_PARANOID(((struct ethhdr *)skb->data)->h_proto !=
  712. protocol);
  713. if (protocol == htons(ETH_P_8021Q)) {
  714. struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
  715. protocol = veh->h_vlan_encapsulated_proto;
  716. }
  717. if (protocol == htons(ETH_P_IP)) {
  718. EFX_BUG_ON_PARANOID(ip_hdr(skb)->protocol != IPPROTO_TCP);
  719. } else {
  720. EFX_BUG_ON_PARANOID(protocol != htons(ETH_P_IPV6));
  721. EFX_BUG_ON_PARANOID(ipv6_hdr(skb)->nexthdr != NEXTHDR_TCP);
  722. }
  723. EFX_BUG_ON_PARANOID((PTR_DIFF(tcp_hdr(skb), skb->data)
  724. + (tcp_hdr(skb)->doff << 2u)) >
  725. skb_headlen(skb));
  726. return protocol;
  727. }
  728. static u8 *efx_tsoh_get_buffer(struct efx_tx_queue *tx_queue,
  729. struct efx_tx_buffer *buffer, unsigned int len)
  730. {
  731. u8 *result;
  732. EFX_BUG_ON_PARANOID(buffer->len);
  733. EFX_BUG_ON_PARANOID(buffer->flags);
  734. EFX_BUG_ON_PARANOID(buffer->unmap_len);
  735. if (likely(len <= TSOH_STD_SIZE - NET_IP_ALIGN)) {
  736. unsigned index =
  737. (tx_queue->insert_count & tx_queue->ptr_mask) / 2;
  738. struct efx_buffer *page_buf =
  739. &tx_queue->tsoh_page[index / TSOH_PER_PAGE];
  740. unsigned offset =
  741. TSOH_STD_SIZE * (index % TSOH_PER_PAGE) + NET_IP_ALIGN;
  742. if (unlikely(!page_buf->addr) &&
  743. efx_nic_alloc_buffer(tx_queue->efx, page_buf, PAGE_SIZE,
  744. GFP_ATOMIC))
  745. return NULL;
  746. result = (u8 *)page_buf->addr + offset;
  747. buffer->dma_addr = page_buf->dma_addr + offset;
  748. buffer->flags = EFX_TX_BUF_CONT;
  749. } else {
  750. tx_queue->tso_long_headers++;
  751. buffer->heap_buf = kmalloc(NET_IP_ALIGN + len, GFP_ATOMIC);
  752. if (unlikely(!buffer->heap_buf))
  753. return NULL;
  754. result = (u8 *)buffer->heap_buf + NET_IP_ALIGN;
  755. buffer->flags = EFX_TX_BUF_CONT | EFX_TX_BUF_HEAP;
  756. }
  757. buffer->len = len;
  758. return result;
  759. }
  760. /**
  761. * efx_tx_queue_insert - push descriptors onto the TX queue
  762. * @tx_queue: Efx TX queue
  763. * @dma_addr: DMA address of fragment
  764. * @len: Length of fragment
  765. * @final_buffer: The final buffer inserted into the queue
  766. *
  767. * Push descriptors onto the TX queue.
  768. */
  769. static void efx_tx_queue_insert(struct efx_tx_queue *tx_queue,
  770. dma_addr_t dma_addr, unsigned len,
  771. struct efx_tx_buffer **final_buffer)
  772. {
  773. struct efx_tx_buffer *buffer;
  774. struct efx_nic *efx = tx_queue->efx;
  775. unsigned dma_len;
  776. EFX_BUG_ON_PARANOID(len <= 0);
  777. while (1) {
  778. buffer = efx_tx_queue_get_insert_buffer(tx_queue);
  779. ++tx_queue->insert_count;
  780. EFX_BUG_ON_PARANOID(tx_queue->insert_count -
  781. tx_queue->read_count >=
  782. efx->txq_entries);
  783. buffer->dma_addr = dma_addr;
  784. dma_len = efx_max_tx_len(efx, dma_addr);
  785. /* If there is enough space to send then do so */
  786. if (dma_len >= len)
  787. break;
  788. buffer->len = dma_len;
  789. buffer->flags = EFX_TX_BUF_CONT;
  790. dma_addr += dma_len;
  791. len -= dma_len;
  792. }
  793. EFX_BUG_ON_PARANOID(!len);
  794. buffer->len = len;
  795. *final_buffer = buffer;
  796. }
  797. /*
  798. * Put a TSO header into the TX queue.
  799. *
  800. * This is special-cased because we know that it is small enough to fit in
  801. * a single fragment, and we know it doesn't cross a page boundary. It
  802. * also allows us to not worry about end-of-packet etc.
  803. */
  804. static int efx_tso_put_header(struct efx_tx_queue *tx_queue,
  805. struct efx_tx_buffer *buffer, u8 *header)
  806. {
  807. if (unlikely(buffer->flags & EFX_TX_BUF_HEAP)) {
  808. buffer->dma_addr = dma_map_single(&tx_queue->efx->pci_dev->dev,
  809. header, buffer->len,
  810. DMA_TO_DEVICE);
  811. if (unlikely(dma_mapping_error(&tx_queue->efx->pci_dev->dev,
  812. buffer->dma_addr))) {
  813. kfree(buffer->heap_buf);
  814. buffer->len = 0;
  815. buffer->flags = 0;
  816. return -ENOMEM;
  817. }
  818. buffer->unmap_len = buffer->len;
  819. buffer->dma_offset = 0;
  820. buffer->flags |= EFX_TX_BUF_MAP_SINGLE;
  821. }
  822. ++tx_queue->insert_count;
  823. return 0;
  824. }
  825. /* Remove buffers put into a tx_queue. None of the buffers must have
  826. * an skb attached.
  827. */
  828. static void efx_enqueue_unwind(struct efx_tx_queue *tx_queue,
  829. unsigned int insert_count)
  830. {
  831. struct efx_tx_buffer *buffer;
  832. /* Work backwards until we hit the original insert pointer value */
  833. while (tx_queue->insert_count != insert_count) {
  834. --tx_queue->insert_count;
  835. buffer = __efx_tx_queue_get_insert_buffer(tx_queue);
  836. efx_dequeue_buffer(tx_queue, buffer, NULL, NULL);
  837. }
  838. }
  839. /* Parse the SKB header and initialise state. */
  840. static int tso_start(struct tso_state *st, struct efx_nic *efx,
  841. const struct sk_buff *skb)
  842. {
  843. bool use_opt_desc = efx_nic_rev(efx) >= EFX_REV_HUNT_A0;
  844. struct device *dma_dev = &efx->pci_dev->dev;
  845. unsigned int header_len, in_len;
  846. dma_addr_t dma_addr;
  847. st->ip_off = skb_network_header(skb) - skb->data;
  848. st->tcp_off = skb_transport_header(skb) - skb->data;
  849. header_len = st->tcp_off + (tcp_hdr(skb)->doff << 2u);
  850. in_len = skb_headlen(skb) - header_len;
  851. st->header_len = header_len;
  852. st->in_len = in_len;
  853. if (st->protocol == htons(ETH_P_IP)) {
  854. st->ip_base_len = st->header_len - st->ip_off;
  855. st->ipv4_id = ntohs(ip_hdr(skb)->id);
  856. } else {
  857. st->ip_base_len = st->header_len - st->tcp_off;
  858. st->ipv4_id = 0;
  859. }
  860. st->seqnum = ntohl(tcp_hdr(skb)->seq);
  861. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->urg);
  862. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->syn);
  863. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->rst);
  864. st->out_len = skb->len - header_len;
  865. if (!use_opt_desc) {
  866. st->header_unmap_len = 0;
  867. if (likely(in_len == 0)) {
  868. st->dma_flags = 0;
  869. st->unmap_len = 0;
  870. return 0;
  871. }
  872. dma_addr = dma_map_single(dma_dev, skb->data + header_len,
  873. in_len, DMA_TO_DEVICE);
  874. st->dma_flags = EFX_TX_BUF_MAP_SINGLE;
  875. st->dma_addr = dma_addr;
  876. st->unmap_addr = dma_addr;
  877. st->unmap_len = in_len;
  878. } else {
  879. dma_addr = dma_map_single(dma_dev, skb->data,
  880. skb_headlen(skb), DMA_TO_DEVICE);
  881. st->header_dma_addr = dma_addr;
  882. st->header_unmap_len = skb_headlen(skb);
  883. st->dma_flags = 0;
  884. st->dma_addr = dma_addr + header_len;
  885. st->unmap_len = 0;
  886. }
  887. return unlikely(dma_mapping_error(dma_dev, dma_addr)) ? -ENOMEM : 0;
  888. }
  889. static int tso_get_fragment(struct tso_state *st, struct efx_nic *efx,
  890. skb_frag_t *frag)
  891. {
  892. st->unmap_addr = skb_frag_dma_map(&efx->pci_dev->dev, frag, 0,
  893. skb_frag_size(frag), DMA_TO_DEVICE);
  894. if (likely(!dma_mapping_error(&efx->pci_dev->dev, st->unmap_addr))) {
  895. st->dma_flags = 0;
  896. st->unmap_len = skb_frag_size(frag);
  897. st->in_len = skb_frag_size(frag);
  898. st->dma_addr = st->unmap_addr;
  899. return 0;
  900. }
  901. return -ENOMEM;
  902. }
  903. /**
  904. * tso_fill_packet_with_fragment - form descriptors for the current fragment
  905. * @tx_queue: Efx TX queue
  906. * @skb: Socket buffer
  907. * @st: TSO state
  908. *
  909. * Form descriptors for the current fragment, until we reach the end
  910. * of fragment or end-of-packet.
  911. */
  912. static void tso_fill_packet_with_fragment(struct efx_tx_queue *tx_queue,
  913. const struct sk_buff *skb,
  914. struct tso_state *st)
  915. {
  916. struct efx_tx_buffer *buffer;
  917. int n;
  918. if (st->in_len == 0)
  919. return;
  920. if (st->packet_space == 0)
  921. return;
  922. EFX_BUG_ON_PARANOID(st->in_len <= 0);
  923. EFX_BUG_ON_PARANOID(st->packet_space <= 0);
  924. n = min(st->in_len, st->packet_space);
  925. st->packet_space -= n;
  926. st->out_len -= n;
  927. st->in_len -= n;
  928. efx_tx_queue_insert(tx_queue, st->dma_addr, n, &buffer);
  929. if (st->out_len == 0) {
  930. /* Transfer ownership of the skb */
  931. buffer->skb = skb;
  932. buffer->flags = EFX_TX_BUF_SKB;
  933. } else if (st->packet_space != 0) {
  934. buffer->flags = EFX_TX_BUF_CONT;
  935. }
  936. if (st->in_len == 0) {
  937. /* Transfer ownership of the DMA mapping */
  938. buffer->unmap_len = st->unmap_len;
  939. buffer->dma_offset = buffer->unmap_len - buffer->len;
  940. buffer->flags |= st->dma_flags;
  941. st->unmap_len = 0;
  942. }
  943. st->dma_addr += n;
  944. }
  945. /**
  946. * tso_start_new_packet - generate a new header and prepare for the new packet
  947. * @tx_queue: Efx TX queue
  948. * @skb: Socket buffer
  949. * @st: TSO state
  950. *
  951. * Generate a new header and prepare for the new packet. Return 0 on
  952. * success, or -%ENOMEM if failed to alloc header.
  953. */
  954. static int tso_start_new_packet(struct efx_tx_queue *tx_queue,
  955. const struct sk_buff *skb,
  956. struct tso_state *st)
  957. {
  958. struct efx_tx_buffer *buffer =
  959. efx_tx_queue_get_insert_buffer(tx_queue);
  960. bool is_last = st->out_len <= skb_shinfo(skb)->gso_size;
  961. u8 tcp_flags_clear;
  962. if (!is_last) {
  963. st->packet_space = skb_shinfo(skb)->gso_size;
  964. tcp_flags_clear = 0x09; /* mask out FIN and PSH */
  965. } else {
  966. st->packet_space = st->out_len;
  967. tcp_flags_clear = 0x00;
  968. }
  969. if (!st->header_unmap_len) {
  970. /* Allocate and insert a DMA-mapped header buffer. */
  971. struct tcphdr *tsoh_th;
  972. unsigned ip_length;
  973. u8 *header;
  974. int rc;
  975. header = efx_tsoh_get_buffer(tx_queue, buffer, st->header_len);
  976. if (!header)
  977. return -ENOMEM;
  978. tsoh_th = (struct tcphdr *)(header + st->tcp_off);
  979. /* Copy and update the headers. */
  980. memcpy(header, skb->data, st->header_len);
  981. tsoh_th->seq = htonl(st->seqnum);
  982. ((u8 *)tsoh_th)[13] &= ~tcp_flags_clear;
  983. ip_length = st->ip_base_len + st->packet_space;
  984. if (st->protocol == htons(ETH_P_IP)) {
  985. struct iphdr *tsoh_iph =
  986. (struct iphdr *)(header + st->ip_off);
  987. tsoh_iph->tot_len = htons(ip_length);
  988. tsoh_iph->id = htons(st->ipv4_id);
  989. } else {
  990. struct ipv6hdr *tsoh_iph =
  991. (struct ipv6hdr *)(header + st->ip_off);
  992. tsoh_iph->payload_len = htons(ip_length);
  993. }
  994. rc = efx_tso_put_header(tx_queue, buffer, header);
  995. if (unlikely(rc))
  996. return rc;
  997. } else {
  998. /* Send the original headers with a TSO option descriptor
  999. * in front
  1000. */
  1001. u8 tcp_flags = ((u8 *)tcp_hdr(skb))[13] & ~tcp_flags_clear;
  1002. buffer->flags = EFX_TX_BUF_OPTION;
  1003. buffer->len = 0;
  1004. buffer->unmap_len = 0;
  1005. EFX_POPULATE_QWORD_5(buffer->option,
  1006. ESF_DZ_TX_DESC_IS_OPT, 1,
  1007. ESF_DZ_TX_OPTION_TYPE,
  1008. ESE_DZ_TX_OPTION_DESC_TSO,
  1009. ESF_DZ_TX_TSO_TCP_FLAGS, tcp_flags,
  1010. ESF_DZ_TX_TSO_IP_ID, st->ipv4_id,
  1011. ESF_DZ_TX_TSO_TCP_SEQNO, st->seqnum);
  1012. ++tx_queue->insert_count;
  1013. /* We mapped the headers in tso_start(). Unmap them
  1014. * when the last segment is completed.
  1015. */
  1016. buffer = efx_tx_queue_get_insert_buffer(tx_queue);
  1017. buffer->dma_addr = st->header_dma_addr;
  1018. buffer->len = st->header_len;
  1019. if (is_last) {
  1020. buffer->flags = EFX_TX_BUF_CONT | EFX_TX_BUF_MAP_SINGLE;
  1021. buffer->unmap_len = st->header_unmap_len;
  1022. buffer->dma_offset = 0;
  1023. /* Ensure we only unmap them once in case of a
  1024. * later DMA mapping error and rollback
  1025. */
  1026. st->header_unmap_len = 0;
  1027. } else {
  1028. buffer->flags = EFX_TX_BUF_CONT;
  1029. buffer->unmap_len = 0;
  1030. }
  1031. ++tx_queue->insert_count;
  1032. }
  1033. st->seqnum += skb_shinfo(skb)->gso_size;
  1034. /* Linux leaves suitable gaps in the IP ID space for us to fill. */
  1035. ++st->ipv4_id;
  1036. ++tx_queue->tso_packets;
  1037. ++tx_queue->tx_packets;
  1038. return 0;
  1039. }
  1040. /**
  1041. * efx_enqueue_skb_tso - segment and transmit a TSO socket buffer
  1042. * @tx_queue: Efx TX queue
  1043. * @skb: Socket buffer
  1044. *
  1045. * Context: You must hold netif_tx_lock() to call this function.
  1046. *
  1047. * Add socket buffer @skb to @tx_queue, doing TSO or return != 0 if
  1048. * @skb was not enqueued. In all cases @skb is consumed. Return
  1049. * %NETDEV_TX_OK.
  1050. */
  1051. static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
  1052. struct sk_buff *skb)
  1053. {
  1054. struct efx_nic *efx = tx_queue->efx;
  1055. unsigned int old_insert_count = tx_queue->insert_count;
  1056. int frag_i, rc;
  1057. struct tso_state state;
  1058. /* Find the packet protocol and sanity-check it */
  1059. state.protocol = efx_tso_check_protocol(skb);
  1060. rc = tso_start(&state, efx, skb);
  1061. if (rc)
  1062. goto mem_err;
  1063. if (likely(state.in_len == 0)) {
  1064. /* Grab the first payload fragment. */
  1065. EFX_BUG_ON_PARANOID(skb_shinfo(skb)->nr_frags < 1);
  1066. frag_i = 0;
  1067. rc = tso_get_fragment(&state, efx,
  1068. skb_shinfo(skb)->frags + frag_i);
  1069. if (rc)
  1070. goto mem_err;
  1071. } else {
  1072. /* Payload starts in the header area. */
  1073. frag_i = -1;
  1074. }
  1075. if (tso_start_new_packet(tx_queue, skb, &state) < 0)
  1076. goto mem_err;
  1077. while (1) {
  1078. tso_fill_packet_with_fragment(tx_queue, skb, &state);
  1079. /* Move onto the next fragment? */
  1080. if (state.in_len == 0) {
  1081. if (++frag_i >= skb_shinfo(skb)->nr_frags)
  1082. /* End of payload reached. */
  1083. break;
  1084. rc = tso_get_fragment(&state, efx,
  1085. skb_shinfo(skb)->frags + frag_i);
  1086. if (rc)
  1087. goto mem_err;
  1088. }
  1089. /* Start at new packet? */
  1090. if (state.packet_space == 0 &&
  1091. tso_start_new_packet(tx_queue, skb, &state) < 0)
  1092. goto mem_err;
  1093. }
  1094. netdev_tx_sent_queue(tx_queue->core_txq, skb->len);
  1095. efx_tx_maybe_stop_queue(tx_queue);
  1096. /* Pass off to hardware */
  1097. if (!skb->xmit_more || netif_xmit_stopped(tx_queue->core_txq))
  1098. efx_nic_push_buffers(tx_queue);
  1099. tx_queue->tso_bursts++;
  1100. return NETDEV_TX_OK;
  1101. mem_err:
  1102. netif_err(efx, tx_err, efx->net_dev,
  1103. "Out of memory for TSO headers, or DMA mapping error\n");
  1104. dev_kfree_skb_any(skb);
  1105. /* Free the DMA mapping we were in the process of writing out */
  1106. if (state.unmap_len) {
  1107. if (state.dma_flags & EFX_TX_BUF_MAP_SINGLE)
  1108. dma_unmap_single(&efx->pci_dev->dev, state.unmap_addr,
  1109. state.unmap_len, DMA_TO_DEVICE);
  1110. else
  1111. dma_unmap_page(&efx->pci_dev->dev, state.unmap_addr,
  1112. state.unmap_len, DMA_TO_DEVICE);
  1113. }
  1114. /* Free the header DMA mapping, if using option descriptors */
  1115. if (state.header_unmap_len)
  1116. dma_unmap_single(&efx->pci_dev->dev, state.header_dma_addr,
  1117. state.header_unmap_len, DMA_TO_DEVICE);
  1118. efx_enqueue_unwind(tx_queue, old_insert_count);
  1119. return NETDEV_TX_OK;
  1120. }