tx.c 38 KB

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