tx.c 37 KB

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