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