tx.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2005-2009 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 "net_driver.h"
  20. #include "efx.h"
  21. #include "nic.h"
  22. #include "workarounds.h"
  23. /*
  24. * TX descriptor ring full threshold
  25. *
  26. * The tx_queue descriptor ring fill-level must fall below this value
  27. * before we restart the netif queue
  28. */
  29. #define EFX_TXQ_THRESHOLD(_efx) ((_efx)->txq_entries / 2u)
  30. static void efx_dequeue_buffer(struct efx_tx_queue *tx_queue,
  31. struct efx_tx_buffer *buffer)
  32. {
  33. if (buffer->unmap_len) {
  34. struct pci_dev *pci_dev = tx_queue->efx->pci_dev;
  35. dma_addr_t unmap_addr = (buffer->dma_addr + buffer->len -
  36. buffer->unmap_len);
  37. if (buffer->unmap_single)
  38. pci_unmap_single(pci_dev, unmap_addr, buffer->unmap_len,
  39. PCI_DMA_TODEVICE);
  40. else
  41. pci_unmap_page(pci_dev, unmap_addr, buffer->unmap_len,
  42. PCI_DMA_TODEVICE);
  43. buffer->unmap_len = 0;
  44. buffer->unmap_single = false;
  45. }
  46. if (buffer->skb) {
  47. dev_kfree_skb_any((struct sk_buff *) buffer->skb);
  48. buffer->skb = NULL;
  49. netif_vdbg(tx_queue->efx, tx_done, tx_queue->efx->net_dev,
  50. "TX queue %d transmission id %x complete\n",
  51. tx_queue->queue, tx_queue->read_count);
  52. }
  53. }
  54. /**
  55. * struct efx_tso_header - a DMA mapped buffer for packet headers
  56. * @next: Linked list of free ones.
  57. * The list is protected by the TX queue lock.
  58. * @dma_unmap_len: Length to unmap for an oversize buffer, or 0.
  59. * @dma_addr: The DMA address of the header below.
  60. *
  61. * This controls the memory used for a TSO header. Use TSOH_DATA()
  62. * to find the packet header data. Use TSOH_SIZE() to calculate the
  63. * total size required for a given packet header length. TSO headers
  64. * in the free list are exactly %TSOH_STD_SIZE bytes in size.
  65. */
  66. struct efx_tso_header {
  67. union {
  68. struct efx_tso_header *next;
  69. size_t unmap_len;
  70. };
  71. dma_addr_t dma_addr;
  72. };
  73. static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
  74. struct sk_buff *skb);
  75. static void efx_fini_tso(struct efx_tx_queue *tx_queue);
  76. static void efx_tsoh_heap_free(struct efx_tx_queue *tx_queue,
  77. struct efx_tso_header *tsoh);
  78. static void efx_tsoh_free(struct efx_tx_queue *tx_queue,
  79. struct efx_tx_buffer *buffer)
  80. {
  81. if (buffer->tsoh) {
  82. if (likely(!buffer->tsoh->unmap_len)) {
  83. buffer->tsoh->next = tx_queue->tso_headers_free;
  84. tx_queue->tso_headers_free = buffer->tsoh;
  85. } else {
  86. efx_tsoh_heap_free(tx_queue, buffer->tsoh);
  87. }
  88. buffer->tsoh = NULL;
  89. }
  90. }
  91. static inline unsigned
  92. efx_max_tx_len(struct efx_nic *efx, dma_addr_t dma_addr)
  93. {
  94. /* Depending on the NIC revision, we can use descriptor
  95. * lengths up to 8K or 8K-1. However, since PCI Express
  96. * devices must split read requests at 4K boundaries, there is
  97. * little benefit from using descriptors that cross those
  98. * boundaries and we keep things simple by not doing so.
  99. */
  100. unsigned len = (~dma_addr & 0xfff) + 1;
  101. /* Work around hardware bug for unaligned buffers. */
  102. if (EFX_WORKAROUND_5391(efx) && (dma_addr & 0xf))
  103. len = min_t(unsigned, len, 512 - (dma_addr & 0xf));
  104. return len;
  105. }
  106. /*
  107. * Add a socket buffer to a TX queue
  108. *
  109. * This maps all fragments of a socket buffer for DMA and adds them to
  110. * the TX queue. The queue's insert pointer will be incremented by
  111. * the number of fragments in the socket buffer.
  112. *
  113. * If any DMA mapping fails, any mapped fragments will be unmapped,
  114. * the queue's insert pointer will be restored to its original value.
  115. *
  116. * This function is split out from efx_hard_start_xmit to allow the
  117. * loopback test to direct packets via specific TX queues.
  118. *
  119. * Returns NETDEV_TX_OK or NETDEV_TX_BUSY
  120. * You must hold netif_tx_lock() to call this function.
  121. */
  122. netdev_tx_t efx_enqueue_skb(struct efx_tx_queue *tx_queue, struct sk_buff *skb)
  123. {
  124. struct efx_nic *efx = tx_queue->efx;
  125. struct pci_dev *pci_dev = efx->pci_dev;
  126. struct efx_tx_buffer *buffer;
  127. skb_frag_t *fragment;
  128. struct page *page;
  129. int page_offset;
  130. unsigned int len, unmap_len = 0, fill_level, insert_ptr;
  131. dma_addr_t dma_addr, unmap_addr = 0;
  132. unsigned int dma_len;
  133. bool unmap_single;
  134. int q_space, i = 0;
  135. netdev_tx_t rc = NETDEV_TX_OK;
  136. EFX_BUG_ON_PARANOID(tx_queue->write_count != tx_queue->insert_count);
  137. if (skb_shinfo(skb)->gso_size)
  138. return efx_enqueue_skb_tso(tx_queue, skb);
  139. /* Get size of the initial fragment */
  140. len = skb_headlen(skb);
  141. /* Pad if necessary */
  142. if (EFX_WORKAROUND_15592(efx) && skb->len <= 32) {
  143. EFX_BUG_ON_PARANOID(skb->data_len);
  144. len = 32 + 1;
  145. if (skb_pad(skb, len - skb->len))
  146. return NETDEV_TX_OK;
  147. }
  148. fill_level = tx_queue->insert_count - tx_queue->old_read_count;
  149. q_space = efx->txq_entries - 1 - fill_level;
  150. /* Map for DMA. Use pci_map_single rather than pci_map_page
  151. * since this is more efficient on machines with sparse
  152. * memory.
  153. */
  154. unmap_single = true;
  155. dma_addr = pci_map_single(pci_dev, skb->data, len, PCI_DMA_TODEVICE);
  156. /* Process all fragments */
  157. while (1) {
  158. if (unlikely(pci_dma_mapping_error(pci_dev, dma_addr)))
  159. goto pci_err;
  160. /* Store fields for marking in the per-fragment final
  161. * descriptor */
  162. unmap_len = len;
  163. unmap_addr = dma_addr;
  164. /* Add to TX queue, splitting across DMA boundaries */
  165. do {
  166. if (unlikely(q_space-- <= 0)) {
  167. /* It might be that completions have
  168. * happened since the xmit path last
  169. * checked. Update the xmit path's
  170. * copy of read_count.
  171. */
  172. netif_tx_stop_queue(tx_queue->core_txq);
  173. /* This memory barrier protects the
  174. * change of queue state from the access
  175. * of read_count. */
  176. smp_mb();
  177. tx_queue->old_read_count =
  178. ACCESS_ONCE(tx_queue->read_count);
  179. fill_level = (tx_queue->insert_count
  180. - tx_queue->old_read_count);
  181. q_space = efx->txq_entries - 1 - fill_level;
  182. if (unlikely(q_space-- <= 0)) {
  183. rc = NETDEV_TX_BUSY;
  184. goto unwind;
  185. }
  186. smp_mb();
  187. netif_tx_start_queue(tx_queue->core_txq);
  188. }
  189. insert_ptr = tx_queue->insert_count & tx_queue->ptr_mask;
  190. buffer = &tx_queue->buffer[insert_ptr];
  191. efx_tsoh_free(tx_queue, buffer);
  192. EFX_BUG_ON_PARANOID(buffer->tsoh);
  193. EFX_BUG_ON_PARANOID(buffer->skb);
  194. EFX_BUG_ON_PARANOID(buffer->len);
  195. EFX_BUG_ON_PARANOID(!buffer->continuation);
  196. EFX_BUG_ON_PARANOID(buffer->unmap_len);
  197. dma_len = efx_max_tx_len(efx, dma_addr);
  198. if (likely(dma_len >= len))
  199. dma_len = len;
  200. /* Fill out per descriptor fields */
  201. buffer->len = dma_len;
  202. buffer->dma_addr = dma_addr;
  203. len -= dma_len;
  204. dma_addr += dma_len;
  205. ++tx_queue->insert_count;
  206. } while (len);
  207. /* Transfer ownership of the unmapping to the final buffer */
  208. buffer->unmap_single = unmap_single;
  209. buffer->unmap_len = unmap_len;
  210. unmap_len = 0;
  211. /* Get address and size of next fragment */
  212. if (i >= skb_shinfo(skb)->nr_frags)
  213. break;
  214. fragment = &skb_shinfo(skb)->frags[i];
  215. len = fragment->size;
  216. page = fragment->page;
  217. page_offset = fragment->page_offset;
  218. i++;
  219. /* Map for DMA */
  220. unmap_single = false;
  221. dma_addr = pci_map_page(pci_dev, page, page_offset, len,
  222. PCI_DMA_TODEVICE);
  223. }
  224. /* Transfer ownership of the skb to the final buffer */
  225. buffer->skb = skb;
  226. buffer->continuation = false;
  227. /* Pass off to hardware */
  228. efx_nic_push_buffers(tx_queue);
  229. return NETDEV_TX_OK;
  230. pci_err:
  231. netif_err(efx, tx_err, efx->net_dev,
  232. " TX queue %d could not map skb with %d bytes %d "
  233. "fragments for DMA\n", tx_queue->queue, skb->len,
  234. skb_shinfo(skb)->nr_frags + 1);
  235. /* Mark the packet as transmitted, and free the SKB ourselves */
  236. dev_kfree_skb_any(skb);
  237. unwind:
  238. /* Work backwards until we hit the original insert pointer value */
  239. while (tx_queue->insert_count != tx_queue->write_count) {
  240. --tx_queue->insert_count;
  241. insert_ptr = tx_queue->insert_count & tx_queue->ptr_mask;
  242. buffer = &tx_queue->buffer[insert_ptr];
  243. efx_dequeue_buffer(tx_queue, buffer);
  244. buffer->len = 0;
  245. }
  246. /* Free the fragment we were mid-way through pushing */
  247. if (unmap_len) {
  248. if (unmap_single)
  249. pci_unmap_single(pci_dev, unmap_addr, unmap_len,
  250. PCI_DMA_TODEVICE);
  251. else
  252. pci_unmap_page(pci_dev, unmap_addr, unmap_len,
  253. PCI_DMA_TODEVICE);
  254. }
  255. return rc;
  256. }
  257. /* Remove packets from the TX queue
  258. *
  259. * This removes packets from the TX queue, up to and including the
  260. * specified index.
  261. */
  262. static void efx_dequeue_buffers(struct efx_tx_queue *tx_queue,
  263. unsigned int index)
  264. {
  265. struct efx_nic *efx = tx_queue->efx;
  266. unsigned int stop_index, read_ptr;
  267. stop_index = (index + 1) & tx_queue->ptr_mask;
  268. read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
  269. while (read_ptr != stop_index) {
  270. struct efx_tx_buffer *buffer = &tx_queue->buffer[read_ptr];
  271. if (unlikely(buffer->len == 0)) {
  272. netif_err(efx, tx_err, efx->net_dev,
  273. "TX queue %d spurious TX completion id %x\n",
  274. tx_queue->queue, read_ptr);
  275. efx_schedule_reset(efx, RESET_TYPE_TX_SKIP);
  276. return;
  277. }
  278. efx_dequeue_buffer(tx_queue, buffer);
  279. buffer->continuation = true;
  280. buffer->len = 0;
  281. ++tx_queue->read_count;
  282. read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
  283. }
  284. }
  285. /* Initiate a packet transmission. We use one channel per CPU
  286. * (sharing when we have more CPUs than channels). On Falcon, the TX
  287. * completion events will be directed back to the CPU that transmitted
  288. * the packet, which should be cache-efficient.
  289. *
  290. * Context: non-blocking.
  291. * Note that returning anything other than NETDEV_TX_OK will cause the
  292. * OS to free the skb.
  293. */
  294. netdev_tx_t efx_hard_start_xmit(struct sk_buff *skb,
  295. struct net_device *net_dev)
  296. {
  297. struct efx_nic *efx = netdev_priv(net_dev);
  298. struct efx_tx_queue *tx_queue;
  299. if (unlikely(efx->port_inhibited))
  300. return NETDEV_TX_BUSY;
  301. tx_queue = efx_get_tx_queue(efx, skb_get_queue_mapping(skb),
  302. skb->ip_summed == CHECKSUM_PARTIAL ?
  303. EFX_TXQ_TYPE_OFFLOAD : 0);
  304. return efx_enqueue_skb(tx_queue, skb);
  305. }
  306. void efx_xmit_done(struct efx_tx_queue *tx_queue, unsigned int index)
  307. {
  308. unsigned fill_level;
  309. struct efx_nic *efx = tx_queue->efx;
  310. EFX_BUG_ON_PARANOID(index > tx_queue->ptr_mask);
  311. efx_dequeue_buffers(tx_queue, index);
  312. /* See if we need to restart the netif queue. This barrier
  313. * separates the update of read_count from the test of the
  314. * queue state. */
  315. smp_mb();
  316. if (unlikely(netif_tx_queue_stopped(tx_queue->core_txq)) &&
  317. likely(efx->port_enabled)) {
  318. fill_level = tx_queue->insert_count - tx_queue->read_count;
  319. if (fill_level < EFX_TXQ_THRESHOLD(efx)) {
  320. EFX_BUG_ON_PARANOID(!efx_dev_registered(efx));
  321. netif_tx_wake_queue(tx_queue->core_txq);
  322. }
  323. }
  324. /* Check whether the hardware queue is now empty */
  325. if ((int)(tx_queue->read_count - tx_queue->old_write_count) >= 0) {
  326. tx_queue->old_write_count = ACCESS_ONCE(tx_queue->write_count);
  327. if (tx_queue->read_count == tx_queue->old_write_count) {
  328. smp_mb();
  329. tx_queue->empty_read_count =
  330. tx_queue->read_count | EFX_EMPTY_COUNT_VALID;
  331. }
  332. }
  333. }
  334. int efx_probe_tx_queue(struct efx_tx_queue *tx_queue)
  335. {
  336. struct efx_nic *efx = tx_queue->efx;
  337. unsigned int entries;
  338. int i, rc;
  339. /* Create the smallest power-of-two aligned ring */
  340. entries = max(roundup_pow_of_two(efx->txq_entries), EFX_MIN_DMAQ_SIZE);
  341. EFX_BUG_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE);
  342. tx_queue->ptr_mask = entries - 1;
  343. netif_dbg(efx, probe, efx->net_dev,
  344. "creating TX queue %d size %#x mask %#x\n",
  345. tx_queue->queue, efx->txq_entries, tx_queue->ptr_mask);
  346. /* Allocate software ring */
  347. tx_queue->buffer = kzalloc(entries * sizeof(*tx_queue->buffer),
  348. GFP_KERNEL);
  349. if (!tx_queue->buffer)
  350. return -ENOMEM;
  351. for (i = 0; i <= tx_queue->ptr_mask; ++i)
  352. tx_queue->buffer[i].continuation = true;
  353. /* Allocate hardware ring */
  354. rc = efx_nic_probe_tx(tx_queue);
  355. if (rc)
  356. goto fail;
  357. return 0;
  358. fail:
  359. kfree(tx_queue->buffer);
  360. tx_queue->buffer = NULL;
  361. return rc;
  362. }
  363. void efx_init_tx_queue(struct efx_tx_queue *tx_queue)
  364. {
  365. netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
  366. "initialising TX queue %d\n", tx_queue->queue);
  367. tx_queue->insert_count = 0;
  368. tx_queue->write_count = 0;
  369. tx_queue->old_write_count = 0;
  370. tx_queue->read_count = 0;
  371. tx_queue->old_read_count = 0;
  372. tx_queue->empty_read_count = 0 | EFX_EMPTY_COUNT_VALID;
  373. /* Set up TX descriptor ring */
  374. efx_nic_init_tx(tx_queue);
  375. }
  376. void efx_release_tx_buffers(struct efx_tx_queue *tx_queue)
  377. {
  378. struct efx_tx_buffer *buffer;
  379. if (!tx_queue->buffer)
  380. return;
  381. /* Free any buffers left in the ring */
  382. while (tx_queue->read_count != tx_queue->write_count) {
  383. buffer = &tx_queue->buffer[tx_queue->read_count & tx_queue->ptr_mask];
  384. efx_dequeue_buffer(tx_queue, buffer);
  385. buffer->continuation = true;
  386. buffer->len = 0;
  387. ++tx_queue->read_count;
  388. }
  389. }
  390. void efx_fini_tx_queue(struct efx_tx_queue *tx_queue)
  391. {
  392. netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
  393. "shutting down TX queue %d\n", tx_queue->queue);
  394. /* Flush TX queue, remove descriptor ring */
  395. efx_nic_fini_tx(tx_queue);
  396. efx_release_tx_buffers(tx_queue);
  397. /* Free up TSO header cache */
  398. efx_fini_tso(tx_queue);
  399. }
  400. void efx_remove_tx_queue(struct efx_tx_queue *tx_queue)
  401. {
  402. netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
  403. "destroying TX queue %d\n", tx_queue->queue);
  404. efx_nic_remove_tx(tx_queue);
  405. kfree(tx_queue->buffer);
  406. tx_queue->buffer = NULL;
  407. }
  408. /* Efx TCP segmentation acceleration.
  409. *
  410. * Why? Because by doing it here in the driver we can go significantly
  411. * faster than the GSO.
  412. *
  413. * Requires TX checksum offload support.
  414. */
  415. /* Number of bytes inserted at the start of a TSO header buffer,
  416. * similar to NET_IP_ALIGN.
  417. */
  418. #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  419. #define TSOH_OFFSET 0
  420. #else
  421. #define TSOH_OFFSET NET_IP_ALIGN
  422. #endif
  423. #define TSOH_BUFFER(tsoh) ((u8 *)(tsoh + 1) + TSOH_OFFSET)
  424. /* Total size of struct efx_tso_header, buffer and padding */
  425. #define TSOH_SIZE(hdr_len) \
  426. (sizeof(struct efx_tso_header) + TSOH_OFFSET + hdr_len)
  427. /* Size of blocks on free list. Larger blocks must be allocated from
  428. * the heap.
  429. */
  430. #define TSOH_STD_SIZE 128
  431. #define PTR_DIFF(p1, p2) ((u8 *)(p1) - (u8 *)(p2))
  432. #define ETH_HDR_LEN(skb) (skb_network_header(skb) - (skb)->data)
  433. #define SKB_TCP_OFF(skb) PTR_DIFF(tcp_hdr(skb), (skb)->data)
  434. #define SKB_IPV4_OFF(skb) PTR_DIFF(ip_hdr(skb), (skb)->data)
  435. #define SKB_IPV6_OFF(skb) PTR_DIFF(ipv6_hdr(skb), (skb)->data)
  436. /**
  437. * struct tso_state - TSO state for an SKB
  438. * @out_len: Remaining length in current segment
  439. * @seqnum: Current sequence number
  440. * @ipv4_id: Current IPv4 ID, host endian
  441. * @packet_space: Remaining space in current packet
  442. * @dma_addr: DMA address of current position
  443. * @in_len: Remaining length in current SKB fragment
  444. * @unmap_len: Length of SKB fragment
  445. * @unmap_addr: DMA address of SKB fragment
  446. * @unmap_single: DMA single vs page mapping flag
  447. * @protocol: Network protocol (after any VLAN header)
  448. * @header_len: Number of bytes of header
  449. * @full_packet_size: Number of bytes to put in each outgoing segment
  450. *
  451. * The state used during segmentation. It is put into this data structure
  452. * just to make it easy to pass into inline functions.
  453. */
  454. struct tso_state {
  455. /* Output position */
  456. unsigned out_len;
  457. unsigned seqnum;
  458. unsigned ipv4_id;
  459. unsigned packet_space;
  460. /* Input position */
  461. dma_addr_t dma_addr;
  462. unsigned in_len;
  463. unsigned unmap_len;
  464. dma_addr_t unmap_addr;
  465. bool unmap_single;
  466. __be16 protocol;
  467. unsigned header_len;
  468. int full_packet_size;
  469. };
  470. /*
  471. * Verify that our various assumptions about sk_buffs and the conditions
  472. * under which TSO will be attempted hold true. Return the protocol number.
  473. */
  474. static __be16 efx_tso_check_protocol(struct sk_buff *skb)
  475. {
  476. __be16 protocol = skb->protocol;
  477. EFX_BUG_ON_PARANOID(((struct ethhdr *)skb->data)->h_proto !=
  478. protocol);
  479. if (protocol == htons(ETH_P_8021Q)) {
  480. /* Find the encapsulated protocol; reset network header
  481. * and transport header based on that. */
  482. struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
  483. protocol = veh->h_vlan_encapsulated_proto;
  484. skb_set_network_header(skb, sizeof(*veh));
  485. if (protocol == htons(ETH_P_IP))
  486. skb_set_transport_header(skb, sizeof(*veh) +
  487. 4 * ip_hdr(skb)->ihl);
  488. else if (protocol == htons(ETH_P_IPV6))
  489. skb_set_transport_header(skb, sizeof(*veh) +
  490. sizeof(struct ipv6hdr));
  491. }
  492. if (protocol == htons(ETH_P_IP)) {
  493. EFX_BUG_ON_PARANOID(ip_hdr(skb)->protocol != IPPROTO_TCP);
  494. } else {
  495. EFX_BUG_ON_PARANOID(protocol != htons(ETH_P_IPV6));
  496. EFX_BUG_ON_PARANOID(ipv6_hdr(skb)->nexthdr != NEXTHDR_TCP);
  497. }
  498. EFX_BUG_ON_PARANOID((PTR_DIFF(tcp_hdr(skb), skb->data)
  499. + (tcp_hdr(skb)->doff << 2u)) >
  500. skb_headlen(skb));
  501. return protocol;
  502. }
  503. /*
  504. * Allocate a page worth of efx_tso_header structures, and string them
  505. * into the tx_queue->tso_headers_free linked list. Return 0 or -ENOMEM.
  506. */
  507. static int efx_tsoh_block_alloc(struct efx_tx_queue *tx_queue)
  508. {
  509. struct pci_dev *pci_dev = tx_queue->efx->pci_dev;
  510. struct efx_tso_header *tsoh;
  511. dma_addr_t dma_addr;
  512. u8 *base_kva, *kva;
  513. base_kva = pci_alloc_consistent(pci_dev, PAGE_SIZE, &dma_addr);
  514. if (base_kva == NULL) {
  515. netif_err(tx_queue->efx, tx_err, tx_queue->efx->net_dev,
  516. "Unable to allocate page for TSO headers\n");
  517. return -ENOMEM;
  518. }
  519. /* pci_alloc_consistent() allocates pages. */
  520. EFX_BUG_ON_PARANOID(dma_addr & (PAGE_SIZE - 1u));
  521. for (kva = base_kva; kva < base_kva + PAGE_SIZE; kva += TSOH_STD_SIZE) {
  522. tsoh = (struct efx_tso_header *)kva;
  523. tsoh->dma_addr = dma_addr + (TSOH_BUFFER(tsoh) - base_kva);
  524. tsoh->next = tx_queue->tso_headers_free;
  525. tx_queue->tso_headers_free = tsoh;
  526. }
  527. return 0;
  528. }
  529. /* Free up a TSO header, and all others in the same page. */
  530. static void efx_tsoh_block_free(struct efx_tx_queue *tx_queue,
  531. struct efx_tso_header *tsoh,
  532. struct pci_dev *pci_dev)
  533. {
  534. struct efx_tso_header **p;
  535. unsigned long base_kva;
  536. dma_addr_t base_dma;
  537. base_kva = (unsigned long)tsoh & PAGE_MASK;
  538. base_dma = tsoh->dma_addr & PAGE_MASK;
  539. p = &tx_queue->tso_headers_free;
  540. while (*p != NULL) {
  541. if (((unsigned long)*p & PAGE_MASK) == base_kva)
  542. *p = (*p)->next;
  543. else
  544. p = &(*p)->next;
  545. }
  546. pci_free_consistent(pci_dev, PAGE_SIZE, (void *)base_kva, base_dma);
  547. }
  548. static struct efx_tso_header *
  549. efx_tsoh_heap_alloc(struct efx_tx_queue *tx_queue, size_t header_len)
  550. {
  551. struct efx_tso_header *tsoh;
  552. tsoh = kmalloc(TSOH_SIZE(header_len), GFP_ATOMIC | GFP_DMA);
  553. if (unlikely(!tsoh))
  554. return NULL;
  555. tsoh->dma_addr = pci_map_single(tx_queue->efx->pci_dev,
  556. TSOH_BUFFER(tsoh), header_len,
  557. PCI_DMA_TODEVICE);
  558. if (unlikely(pci_dma_mapping_error(tx_queue->efx->pci_dev,
  559. tsoh->dma_addr))) {
  560. kfree(tsoh);
  561. return NULL;
  562. }
  563. tsoh->unmap_len = header_len;
  564. return tsoh;
  565. }
  566. static void
  567. efx_tsoh_heap_free(struct efx_tx_queue *tx_queue, struct efx_tso_header *tsoh)
  568. {
  569. pci_unmap_single(tx_queue->efx->pci_dev,
  570. tsoh->dma_addr, tsoh->unmap_len,
  571. PCI_DMA_TODEVICE);
  572. kfree(tsoh);
  573. }
  574. /**
  575. * efx_tx_queue_insert - push descriptors onto the TX queue
  576. * @tx_queue: Efx TX queue
  577. * @dma_addr: DMA address of fragment
  578. * @len: Length of fragment
  579. * @final_buffer: The final buffer inserted into the queue
  580. *
  581. * Push descriptors onto the TX queue. Return 0 on success or 1 if
  582. * @tx_queue full.
  583. */
  584. static int efx_tx_queue_insert(struct efx_tx_queue *tx_queue,
  585. dma_addr_t dma_addr, unsigned len,
  586. struct efx_tx_buffer **final_buffer)
  587. {
  588. struct efx_tx_buffer *buffer;
  589. struct efx_nic *efx = tx_queue->efx;
  590. unsigned dma_len, fill_level, insert_ptr;
  591. int q_space;
  592. EFX_BUG_ON_PARANOID(len <= 0);
  593. fill_level = tx_queue->insert_count - tx_queue->old_read_count;
  594. /* -1 as there is no way to represent all descriptors used */
  595. q_space = efx->txq_entries - 1 - fill_level;
  596. while (1) {
  597. if (unlikely(q_space-- <= 0)) {
  598. /* It might be that completions have happened
  599. * since the xmit path last checked. Update
  600. * the xmit path's copy of read_count.
  601. */
  602. netif_tx_stop_queue(tx_queue->core_txq);
  603. /* This memory barrier protects the change of
  604. * queue state from the access of read_count. */
  605. smp_mb();
  606. tx_queue->old_read_count =
  607. ACCESS_ONCE(tx_queue->read_count);
  608. fill_level = (tx_queue->insert_count
  609. - tx_queue->old_read_count);
  610. q_space = efx->txq_entries - 1 - fill_level;
  611. if (unlikely(q_space-- <= 0)) {
  612. *final_buffer = NULL;
  613. return 1;
  614. }
  615. smp_mb();
  616. netif_tx_start_queue(tx_queue->core_txq);
  617. }
  618. insert_ptr = tx_queue->insert_count & tx_queue->ptr_mask;
  619. buffer = &tx_queue->buffer[insert_ptr];
  620. ++tx_queue->insert_count;
  621. EFX_BUG_ON_PARANOID(tx_queue->insert_count -
  622. tx_queue->read_count >=
  623. efx->txq_entries);
  624. efx_tsoh_free(tx_queue, buffer);
  625. EFX_BUG_ON_PARANOID(buffer->len);
  626. EFX_BUG_ON_PARANOID(buffer->unmap_len);
  627. EFX_BUG_ON_PARANOID(buffer->skb);
  628. EFX_BUG_ON_PARANOID(!buffer->continuation);
  629. EFX_BUG_ON_PARANOID(buffer->tsoh);
  630. buffer->dma_addr = dma_addr;
  631. dma_len = efx_max_tx_len(efx, dma_addr);
  632. /* If there is enough space to send then do so */
  633. if (dma_len >= len)
  634. break;
  635. buffer->len = dma_len; /* Don't set the other members */
  636. dma_addr += dma_len;
  637. len -= dma_len;
  638. }
  639. EFX_BUG_ON_PARANOID(!len);
  640. buffer->len = len;
  641. *final_buffer = buffer;
  642. return 0;
  643. }
  644. /*
  645. * Put a TSO header into the TX queue.
  646. *
  647. * This is special-cased because we know that it is small enough to fit in
  648. * a single fragment, and we know it doesn't cross a page boundary. It
  649. * also allows us to not worry about end-of-packet etc.
  650. */
  651. static void efx_tso_put_header(struct efx_tx_queue *tx_queue,
  652. struct efx_tso_header *tsoh, unsigned len)
  653. {
  654. struct efx_tx_buffer *buffer;
  655. buffer = &tx_queue->buffer[tx_queue->insert_count & tx_queue->ptr_mask];
  656. efx_tsoh_free(tx_queue, buffer);
  657. EFX_BUG_ON_PARANOID(buffer->len);
  658. EFX_BUG_ON_PARANOID(buffer->unmap_len);
  659. EFX_BUG_ON_PARANOID(buffer->skb);
  660. EFX_BUG_ON_PARANOID(!buffer->continuation);
  661. EFX_BUG_ON_PARANOID(buffer->tsoh);
  662. buffer->len = len;
  663. buffer->dma_addr = tsoh->dma_addr;
  664. buffer->tsoh = tsoh;
  665. ++tx_queue->insert_count;
  666. }
  667. /* Remove descriptors put into a tx_queue. */
  668. static void efx_enqueue_unwind(struct efx_tx_queue *tx_queue)
  669. {
  670. struct efx_tx_buffer *buffer;
  671. dma_addr_t unmap_addr;
  672. /* Work backwards until we hit the original insert pointer value */
  673. while (tx_queue->insert_count != tx_queue->write_count) {
  674. --tx_queue->insert_count;
  675. buffer = &tx_queue->buffer[tx_queue->insert_count &
  676. tx_queue->ptr_mask];
  677. efx_tsoh_free(tx_queue, buffer);
  678. EFX_BUG_ON_PARANOID(buffer->skb);
  679. if (buffer->unmap_len) {
  680. unmap_addr = (buffer->dma_addr + buffer->len -
  681. buffer->unmap_len);
  682. if (buffer->unmap_single)
  683. pci_unmap_single(tx_queue->efx->pci_dev,
  684. unmap_addr, buffer->unmap_len,
  685. PCI_DMA_TODEVICE);
  686. else
  687. pci_unmap_page(tx_queue->efx->pci_dev,
  688. unmap_addr, buffer->unmap_len,
  689. PCI_DMA_TODEVICE);
  690. buffer->unmap_len = 0;
  691. }
  692. buffer->len = 0;
  693. buffer->continuation = true;
  694. }
  695. }
  696. /* Parse the SKB header and initialise state. */
  697. static void tso_start(struct tso_state *st, const struct sk_buff *skb)
  698. {
  699. /* All ethernet/IP/TCP headers combined size is TCP header size
  700. * plus offset of TCP header relative to start of packet.
  701. */
  702. st->header_len = ((tcp_hdr(skb)->doff << 2u)
  703. + PTR_DIFF(tcp_hdr(skb), skb->data));
  704. st->full_packet_size = st->header_len + skb_shinfo(skb)->gso_size;
  705. if (st->protocol == htons(ETH_P_IP))
  706. st->ipv4_id = ntohs(ip_hdr(skb)->id);
  707. else
  708. st->ipv4_id = 0;
  709. st->seqnum = ntohl(tcp_hdr(skb)->seq);
  710. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->urg);
  711. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->syn);
  712. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->rst);
  713. st->packet_space = st->full_packet_size;
  714. st->out_len = skb->len - st->header_len;
  715. st->unmap_len = 0;
  716. st->unmap_single = false;
  717. }
  718. static int tso_get_fragment(struct tso_state *st, struct efx_nic *efx,
  719. skb_frag_t *frag)
  720. {
  721. st->unmap_addr = pci_map_page(efx->pci_dev, frag->page,
  722. frag->page_offset, frag->size,
  723. PCI_DMA_TODEVICE);
  724. if (likely(!pci_dma_mapping_error(efx->pci_dev, st->unmap_addr))) {
  725. st->unmap_single = false;
  726. st->unmap_len = frag->size;
  727. st->in_len = frag->size;
  728. st->dma_addr = st->unmap_addr;
  729. return 0;
  730. }
  731. return -ENOMEM;
  732. }
  733. static int tso_get_head_fragment(struct tso_state *st, struct efx_nic *efx,
  734. const struct sk_buff *skb)
  735. {
  736. int hl = st->header_len;
  737. int len = skb_headlen(skb) - hl;
  738. st->unmap_addr = pci_map_single(efx->pci_dev, skb->data + hl,
  739. len, PCI_DMA_TODEVICE);
  740. if (likely(!pci_dma_mapping_error(efx->pci_dev, st->unmap_addr))) {
  741. st->unmap_single = true;
  742. st->unmap_len = len;
  743. st->in_len = len;
  744. st->dma_addr = st->unmap_addr;
  745. return 0;
  746. }
  747. return -ENOMEM;
  748. }
  749. /**
  750. * tso_fill_packet_with_fragment - form descriptors for the current fragment
  751. * @tx_queue: Efx TX queue
  752. * @skb: Socket buffer
  753. * @st: TSO state
  754. *
  755. * Form descriptors for the current fragment, until we reach the end
  756. * of fragment or end-of-packet. Return 0 on success, 1 if not enough
  757. * space in @tx_queue.
  758. */
  759. static int tso_fill_packet_with_fragment(struct efx_tx_queue *tx_queue,
  760. const struct sk_buff *skb,
  761. struct tso_state *st)
  762. {
  763. struct efx_tx_buffer *buffer;
  764. int n, end_of_packet, rc;
  765. if (st->in_len == 0)
  766. return 0;
  767. if (st->packet_space == 0)
  768. return 0;
  769. EFX_BUG_ON_PARANOID(st->in_len <= 0);
  770. EFX_BUG_ON_PARANOID(st->packet_space <= 0);
  771. n = min(st->in_len, st->packet_space);
  772. st->packet_space -= n;
  773. st->out_len -= n;
  774. st->in_len -= n;
  775. rc = efx_tx_queue_insert(tx_queue, st->dma_addr, n, &buffer);
  776. if (likely(rc == 0)) {
  777. if (st->out_len == 0)
  778. /* Transfer ownership of the skb */
  779. buffer->skb = skb;
  780. end_of_packet = st->out_len == 0 || st->packet_space == 0;
  781. buffer->continuation = !end_of_packet;
  782. if (st->in_len == 0) {
  783. /* Transfer ownership of the pci mapping */
  784. buffer->unmap_len = st->unmap_len;
  785. buffer->unmap_single = st->unmap_single;
  786. st->unmap_len = 0;
  787. }
  788. }
  789. st->dma_addr += n;
  790. return rc;
  791. }
  792. /**
  793. * tso_start_new_packet - generate a new header and prepare for the new packet
  794. * @tx_queue: Efx TX queue
  795. * @skb: Socket buffer
  796. * @st: TSO state
  797. *
  798. * Generate a new header and prepare for the new packet. Return 0 on
  799. * success, or -1 if failed to alloc header.
  800. */
  801. static int tso_start_new_packet(struct efx_tx_queue *tx_queue,
  802. const struct sk_buff *skb,
  803. struct tso_state *st)
  804. {
  805. struct efx_tso_header *tsoh;
  806. struct tcphdr *tsoh_th;
  807. unsigned ip_length;
  808. u8 *header;
  809. /* Allocate a DMA-mapped header buffer. */
  810. if (likely(TSOH_SIZE(st->header_len) <= TSOH_STD_SIZE)) {
  811. if (tx_queue->tso_headers_free == NULL) {
  812. if (efx_tsoh_block_alloc(tx_queue))
  813. return -1;
  814. }
  815. EFX_BUG_ON_PARANOID(!tx_queue->tso_headers_free);
  816. tsoh = tx_queue->tso_headers_free;
  817. tx_queue->tso_headers_free = tsoh->next;
  818. tsoh->unmap_len = 0;
  819. } else {
  820. tx_queue->tso_long_headers++;
  821. tsoh = efx_tsoh_heap_alloc(tx_queue, st->header_len);
  822. if (unlikely(!tsoh))
  823. return -1;
  824. }
  825. header = TSOH_BUFFER(tsoh);
  826. tsoh_th = (struct tcphdr *)(header + SKB_TCP_OFF(skb));
  827. /* Copy and update the headers. */
  828. memcpy(header, skb->data, st->header_len);
  829. tsoh_th->seq = htonl(st->seqnum);
  830. st->seqnum += skb_shinfo(skb)->gso_size;
  831. if (st->out_len > skb_shinfo(skb)->gso_size) {
  832. /* This packet will not finish the TSO burst. */
  833. ip_length = st->full_packet_size - ETH_HDR_LEN(skb);
  834. tsoh_th->fin = 0;
  835. tsoh_th->psh = 0;
  836. } else {
  837. /* This packet will be the last in the TSO burst. */
  838. ip_length = st->header_len - ETH_HDR_LEN(skb) + st->out_len;
  839. tsoh_th->fin = tcp_hdr(skb)->fin;
  840. tsoh_th->psh = tcp_hdr(skb)->psh;
  841. }
  842. if (st->protocol == htons(ETH_P_IP)) {
  843. struct iphdr *tsoh_iph =
  844. (struct iphdr *)(header + SKB_IPV4_OFF(skb));
  845. tsoh_iph->tot_len = htons(ip_length);
  846. /* Linux leaves suitable gaps in the IP ID space for us to fill. */
  847. tsoh_iph->id = htons(st->ipv4_id);
  848. st->ipv4_id++;
  849. } else {
  850. struct ipv6hdr *tsoh_iph =
  851. (struct ipv6hdr *)(header + SKB_IPV6_OFF(skb));
  852. tsoh_iph->payload_len = htons(ip_length - sizeof(*tsoh_iph));
  853. }
  854. st->packet_space = skb_shinfo(skb)->gso_size;
  855. ++tx_queue->tso_packets;
  856. /* Form a descriptor for this header. */
  857. efx_tso_put_header(tx_queue, tsoh, st->header_len);
  858. return 0;
  859. }
  860. /**
  861. * efx_enqueue_skb_tso - segment and transmit a TSO socket buffer
  862. * @tx_queue: Efx TX queue
  863. * @skb: Socket buffer
  864. *
  865. * Context: You must hold netif_tx_lock() to call this function.
  866. *
  867. * Add socket buffer @skb to @tx_queue, doing TSO or return != 0 if
  868. * @skb was not enqueued. In all cases @skb is consumed. Return
  869. * %NETDEV_TX_OK or %NETDEV_TX_BUSY.
  870. */
  871. static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
  872. struct sk_buff *skb)
  873. {
  874. struct efx_nic *efx = tx_queue->efx;
  875. int frag_i, rc, rc2 = NETDEV_TX_OK;
  876. struct tso_state state;
  877. /* Find the packet protocol and sanity-check it */
  878. state.protocol = efx_tso_check_protocol(skb);
  879. EFX_BUG_ON_PARANOID(tx_queue->write_count != tx_queue->insert_count);
  880. tso_start(&state, skb);
  881. /* Assume that skb header area contains exactly the headers, and
  882. * all payload is in the frag list.
  883. */
  884. if (skb_headlen(skb) == state.header_len) {
  885. /* Grab the first payload fragment. */
  886. EFX_BUG_ON_PARANOID(skb_shinfo(skb)->nr_frags < 1);
  887. frag_i = 0;
  888. rc = tso_get_fragment(&state, efx,
  889. skb_shinfo(skb)->frags + frag_i);
  890. if (rc)
  891. goto mem_err;
  892. } else {
  893. rc = tso_get_head_fragment(&state, efx, skb);
  894. if (rc)
  895. goto mem_err;
  896. frag_i = -1;
  897. }
  898. if (tso_start_new_packet(tx_queue, skb, &state) < 0)
  899. goto mem_err;
  900. while (1) {
  901. rc = tso_fill_packet_with_fragment(tx_queue, skb, &state);
  902. if (unlikely(rc)) {
  903. rc2 = NETDEV_TX_BUSY;
  904. goto unwind;
  905. }
  906. /* Move onto the next fragment? */
  907. if (state.in_len == 0) {
  908. if (++frag_i >= skb_shinfo(skb)->nr_frags)
  909. /* End of payload reached. */
  910. break;
  911. rc = tso_get_fragment(&state, efx,
  912. skb_shinfo(skb)->frags + frag_i);
  913. if (rc)
  914. goto mem_err;
  915. }
  916. /* Start at new packet? */
  917. if (state.packet_space == 0 &&
  918. tso_start_new_packet(tx_queue, skb, &state) < 0)
  919. goto mem_err;
  920. }
  921. /* Pass off to hardware */
  922. efx_nic_push_buffers(tx_queue);
  923. tx_queue->tso_bursts++;
  924. return NETDEV_TX_OK;
  925. mem_err:
  926. netif_err(efx, tx_err, efx->net_dev,
  927. "Out of memory for TSO headers, or PCI mapping error\n");
  928. dev_kfree_skb_any(skb);
  929. unwind:
  930. /* Free the DMA mapping we were in the process of writing out */
  931. if (state.unmap_len) {
  932. if (state.unmap_single)
  933. pci_unmap_single(efx->pci_dev, state.unmap_addr,
  934. state.unmap_len, PCI_DMA_TODEVICE);
  935. else
  936. pci_unmap_page(efx->pci_dev, state.unmap_addr,
  937. state.unmap_len, PCI_DMA_TODEVICE);
  938. }
  939. efx_enqueue_unwind(tx_queue);
  940. return rc2;
  941. }
  942. /*
  943. * Free up all TSO datastructures associated with tx_queue. This
  944. * routine should be called only once the tx_queue is both empty and
  945. * will no longer be used.
  946. */
  947. static void efx_fini_tso(struct efx_tx_queue *tx_queue)
  948. {
  949. unsigned i;
  950. if (tx_queue->buffer) {
  951. for (i = 0; i <= tx_queue->ptr_mask; ++i)
  952. efx_tsoh_free(tx_queue, &tx_queue->buffer[i]);
  953. }
  954. while (tx_queue->tso_headers_free != NULL)
  955. efx_tsoh_block_free(tx_queue, tx_queue->tso_headers_free,
  956. tx_queue->efx->pci_dev);
  957. }