tx.c 30 KB

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