tx.c 34 KB

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