tx.c 34 KB

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