tx.c 37 KB

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