tx.c 37 KB

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