tx.c 37 KB

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