tx.c 37 KB

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