tx.c 38 KB

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