tx.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  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. /*
  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 device *dma_dev = &efx->pci_dev->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 dma_map_single rather than dma_map_page
  153. * since this is more efficient on machines with sparse
  154. * memory.
  155. */
  156. unmap_single = true;
  157. dma_addr = dma_map_single(dma_dev, skb->data, len, PCI_DMA_TODEVICE);
  158. /* Process all fragments */
  159. while (1) {
  160. if (unlikely(dma_mapping_error(dma_dev, dma_addr)))
  161. goto dma_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(dma_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. dma_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. dma_unmap_single(dma_dev, unmap_addr, unmap_len,
  254. DMA_TO_DEVICE);
  255. else
  256. dma_unmap_page(dma_dev, unmap_addr, unmap_len,
  257. DMA_TO_DEVICE);
  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. netif_tx_wake_queue(tx_queue->core_txq);
  393. }
  394. /* Check whether the hardware queue is now empty */
  395. if ((int)(tx_queue->read_count - tx_queue->old_write_count) >= 0) {
  396. tx_queue->old_write_count = ACCESS_ONCE(tx_queue->write_count);
  397. if (tx_queue->read_count == tx_queue->old_write_count) {
  398. smp_mb();
  399. tx_queue->empty_read_count =
  400. tx_queue->read_count | EFX_EMPTY_COUNT_VALID;
  401. }
  402. }
  403. }
  404. int efx_probe_tx_queue(struct efx_tx_queue *tx_queue)
  405. {
  406. struct efx_nic *efx = tx_queue->efx;
  407. unsigned int entries;
  408. int i, rc;
  409. /* Create the smallest power-of-two aligned ring */
  410. entries = max(roundup_pow_of_two(efx->txq_entries), EFX_MIN_DMAQ_SIZE);
  411. EFX_BUG_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE);
  412. tx_queue->ptr_mask = entries - 1;
  413. netif_dbg(efx, probe, efx->net_dev,
  414. "creating TX queue %d size %#x mask %#x\n",
  415. tx_queue->queue, efx->txq_entries, tx_queue->ptr_mask);
  416. /* Allocate software ring */
  417. tx_queue->buffer = kcalloc(entries, sizeof(*tx_queue->buffer),
  418. GFP_KERNEL);
  419. if (!tx_queue->buffer)
  420. return -ENOMEM;
  421. for (i = 0; i <= tx_queue->ptr_mask; ++i)
  422. tx_queue->buffer[i].continuation = true;
  423. /* Allocate hardware ring */
  424. rc = efx_nic_probe_tx(tx_queue);
  425. if (rc)
  426. goto fail;
  427. return 0;
  428. fail:
  429. kfree(tx_queue->buffer);
  430. tx_queue->buffer = NULL;
  431. return rc;
  432. }
  433. void efx_init_tx_queue(struct efx_tx_queue *tx_queue)
  434. {
  435. netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
  436. "initialising TX queue %d\n", tx_queue->queue);
  437. tx_queue->insert_count = 0;
  438. tx_queue->write_count = 0;
  439. tx_queue->old_write_count = 0;
  440. tx_queue->read_count = 0;
  441. tx_queue->old_read_count = 0;
  442. tx_queue->empty_read_count = 0 | EFX_EMPTY_COUNT_VALID;
  443. /* Set up TX descriptor ring */
  444. efx_nic_init_tx(tx_queue);
  445. tx_queue->initialised = true;
  446. }
  447. void efx_release_tx_buffers(struct efx_tx_queue *tx_queue)
  448. {
  449. struct efx_tx_buffer *buffer;
  450. if (!tx_queue->buffer)
  451. return;
  452. /* Free any buffers left in the ring */
  453. while (tx_queue->read_count != tx_queue->write_count) {
  454. unsigned int pkts_compl = 0, bytes_compl = 0;
  455. buffer = &tx_queue->buffer[tx_queue->read_count & tx_queue->ptr_mask];
  456. efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl);
  457. buffer->continuation = true;
  458. buffer->len = 0;
  459. ++tx_queue->read_count;
  460. }
  461. netdev_tx_reset_queue(tx_queue->core_txq);
  462. }
  463. void efx_fini_tx_queue(struct efx_tx_queue *tx_queue)
  464. {
  465. if (!tx_queue->initialised)
  466. return;
  467. netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
  468. "shutting down TX queue %d\n", tx_queue->queue);
  469. tx_queue->initialised = false;
  470. /* Flush TX queue, remove descriptor ring */
  471. efx_nic_fini_tx(tx_queue);
  472. efx_release_tx_buffers(tx_queue);
  473. /* Free up TSO header cache */
  474. efx_fini_tso(tx_queue);
  475. }
  476. void efx_remove_tx_queue(struct efx_tx_queue *tx_queue)
  477. {
  478. if (!tx_queue->buffer)
  479. return;
  480. netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
  481. "destroying TX queue %d\n", tx_queue->queue);
  482. efx_nic_remove_tx(tx_queue);
  483. kfree(tx_queue->buffer);
  484. tx_queue->buffer = NULL;
  485. }
  486. /* Efx TCP segmentation acceleration.
  487. *
  488. * Why? Because by doing it here in the driver we can go significantly
  489. * faster than the GSO.
  490. *
  491. * Requires TX checksum offload support.
  492. */
  493. /* Number of bytes inserted at the start of a TSO header buffer,
  494. * similar to NET_IP_ALIGN.
  495. */
  496. #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  497. #define TSOH_OFFSET 0
  498. #else
  499. #define TSOH_OFFSET NET_IP_ALIGN
  500. #endif
  501. #define TSOH_BUFFER(tsoh) ((u8 *)(tsoh + 1) + TSOH_OFFSET)
  502. /* Total size of struct efx_tso_header, buffer and padding */
  503. #define TSOH_SIZE(hdr_len) \
  504. (sizeof(struct efx_tso_header) + TSOH_OFFSET + hdr_len)
  505. /* Size of blocks on free list. Larger blocks must be allocated from
  506. * the heap.
  507. */
  508. #define TSOH_STD_SIZE 128
  509. #define PTR_DIFF(p1, p2) ((u8 *)(p1) - (u8 *)(p2))
  510. #define ETH_HDR_LEN(skb) (skb_network_header(skb) - (skb)->data)
  511. #define SKB_TCP_OFF(skb) PTR_DIFF(tcp_hdr(skb), (skb)->data)
  512. #define SKB_IPV4_OFF(skb) PTR_DIFF(ip_hdr(skb), (skb)->data)
  513. #define SKB_IPV6_OFF(skb) PTR_DIFF(ipv6_hdr(skb), (skb)->data)
  514. /**
  515. * struct tso_state - TSO state for an SKB
  516. * @out_len: Remaining length in current segment
  517. * @seqnum: Current sequence number
  518. * @ipv4_id: Current IPv4 ID, host endian
  519. * @packet_space: Remaining space in current packet
  520. * @dma_addr: DMA address of current position
  521. * @in_len: Remaining length in current SKB fragment
  522. * @unmap_len: Length of SKB fragment
  523. * @unmap_addr: DMA address of SKB fragment
  524. * @unmap_single: DMA single vs page mapping flag
  525. * @protocol: Network protocol (after any VLAN header)
  526. * @header_len: Number of bytes of header
  527. * @full_packet_size: Number of bytes to put in each outgoing segment
  528. *
  529. * The state used during segmentation. It is put into this data structure
  530. * just to make it easy to pass into inline functions.
  531. */
  532. struct tso_state {
  533. /* Output position */
  534. unsigned out_len;
  535. unsigned seqnum;
  536. unsigned ipv4_id;
  537. unsigned packet_space;
  538. /* Input position */
  539. dma_addr_t dma_addr;
  540. unsigned in_len;
  541. unsigned unmap_len;
  542. dma_addr_t unmap_addr;
  543. bool unmap_single;
  544. __be16 protocol;
  545. unsigned header_len;
  546. int full_packet_size;
  547. };
  548. /*
  549. * Verify that our various assumptions about sk_buffs and the conditions
  550. * under which TSO will be attempted hold true. Return the protocol number.
  551. */
  552. static __be16 efx_tso_check_protocol(struct sk_buff *skb)
  553. {
  554. __be16 protocol = skb->protocol;
  555. EFX_BUG_ON_PARANOID(((struct ethhdr *)skb->data)->h_proto !=
  556. protocol);
  557. if (protocol == htons(ETH_P_8021Q)) {
  558. /* Find the encapsulated protocol; reset network header
  559. * and transport header based on that. */
  560. struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
  561. protocol = veh->h_vlan_encapsulated_proto;
  562. skb_set_network_header(skb, sizeof(*veh));
  563. if (protocol == htons(ETH_P_IP))
  564. skb_set_transport_header(skb, sizeof(*veh) +
  565. 4 * ip_hdr(skb)->ihl);
  566. else if (protocol == htons(ETH_P_IPV6))
  567. skb_set_transport_header(skb, sizeof(*veh) +
  568. sizeof(struct ipv6hdr));
  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->skb);
  706. EFX_BUG_ON_PARANOID(!buffer->continuation);
  707. EFX_BUG_ON_PARANOID(buffer->tsoh);
  708. buffer->dma_addr = dma_addr;
  709. dma_len = efx_max_tx_len(efx, dma_addr);
  710. /* If there is enough space to send then do so */
  711. if (dma_len >= len)
  712. break;
  713. buffer->len = dma_len; /* Don't set the other members */
  714. dma_addr += dma_len;
  715. len -= dma_len;
  716. }
  717. EFX_BUG_ON_PARANOID(!len);
  718. buffer->len = len;
  719. *final_buffer = buffer;
  720. return 0;
  721. }
  722. /*
  723. * Put a TSO header into the TX queue.
  724. *
  725. * This is special-cased because we know that it is small enough to fit in
  726. * a single fragment, and we know it doesn't cross a page boundary. It
  727. * also allows us to not worry about end-of-packet etc.
  728. */
  729. static void efx_tso_put_header(struct efx_tx_queue *tx_queue,
  730. struct efx_tso_header *tsoh, unsigned len)
  731. {
  732. struct efx_tx_buffer *buffer;
  733. buffer = &tx_queue->buffer[tx_queue->insert_count & tx_queue->ptr_mask];
  734. efx_tsoh_free(tx_queue, buffer);
  735. EFX_BUG_ON_PARANOID(buffer->len);
  736. EFX_BUG_ON_PARANOID(buffer->unmap_len);
  737. EFX_BUG_ON_PARANOID(buffer->skb);
  738. EFX_BUG_ON_PARANOID(!buffer->continuation);
  739. EFX_BUG_ON_PARANOID(buffer->tsoh);
  740. buffer->len = len;
  741. buffer->dma_addr = tsoh->dma_addr;
  742. buffer->tsoh = tsoh;
  743. ++tx_queue->insert_count;
  744. }
  745. /* Remove descriptors put into a tx_queue. */
  746. static void efx_enqueue_unwind(struct efx_tx_queue *tx_queue)
  747. {
  748. struct efx_tx_buffer *buffer;
  749. dma_addr_t unmap_addr;
  750. /* Work backwards until we hit the original insert pointer value */
  751. while (tx_queue->insert_count != tx_queue->write_count) {
  752. --tx_queue->insert_count;
  753. buffer = &tx_queue->buffer[tx_queue->insert_count &
  754. tx_queue->ptr_mask];
  755. efx_tsoh_free(tx_queue, buffer);
  756. EFX_BUG_ON_PARANOID(buffer->skb);
  757. if (buffer->unmap_len) {
  758. unmap_addr = (buffer->dma_addr + buffer->len -
  759. buffer->unmap_len);
  760. if (buffer->unmap_single)
  761. dma_unmap_single(&tx_queue->efx->pci_dev->dev,
  762. unmap_addr, buffer->unmap_len,
  763. DMA_TO_DEVICE);
  764. else
  765. dma_unmap_page(&tx_queue->efx->pci_dev->dev,
  766. unmap_addr, buffer->unmap_len,
  767. DMA_TO_DEVICE);
  768. buffer->unmap_len = 0;
  769. }
  770. buffer->len = 0;
  771. buffer->continuation = true;
  772. }
  773. }
  774. /* Parse the SKB header and initialise state. */
  775. static void tso_start(struct tso_state *st, const struct sk_buff *skb)
  776. {
  777. /* All ethernet/IP/TCP headers combined size is TCP header size
  778. * plus offset of TCP header relative to start of packet.
  779. */
  780. st->header_len = ((tcp_hdr(skb)->doff << 2u)
  781. + PTR_DIFF(tcp_hdr(skb), skb->data));
  782. st->full_packet_size = st->header_len + skb_shinfo(skb)->gso_size;
  783. if (st->protocol == htons(ETH_P_IP))
  784. st->ipv4_id = ntohs(ip_hdr(skb)->id);
  785. else
  786. st->ipv4_id = 0;
  787. st->seqnum = ntohl(tcp_hdr(skb)->seq);
  788. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->urg);
  789. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->syn);
  790. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->rst);
  791. st->out_len = skb->len - st->header_len;
  792. st->unmap_len = 0;
  793. st->unmap_single = false;
  794. }
  795. static int tso_get_fragment(struct tso_state *st, struct efx_nic *efx,
  796. skb_frag_t *frag)
  797. {
  798. st->unmap_addr = skb_frag_dma_map(&efx->pci_dev->dev, frag, 0,
  799. skb_frag_size(frag), DMA_TO_DEVICE);
  800. if (likely(!dma_mapping_error(&efx->pci_dev->dev, st->unmap_addr))) {
  801. st->unmap_single = false;
  802. st->unmap_len = skb_frag_size(frag);
  803. st->in_len = skb_frag_size(frag);
  804. st->dma_addr = st->unmap_addr;
  805. return 0;
  806. }
  807. return -ENOMEM;
  808. }
  809. static int tso_get_head_fragment(struct tso_state *st, struct efx_nic *efx,
  810. const struct sk_buff *skb)
  811. {
  812. int hl = st->header_len;
  813. int len = skb_headlen(skb) - hl;
  814. st->unmap_addr = dma_map_single(&efx->pci_dev->dev, skb->data + hl,
  815. len, DMA_TO_DEVICE);
  816. if (likely(!dma_mapping_error(&efx->pci_dev->dev, st->unmap_addr))) {
  817. st->unmap_single = true;
  818. st->unmap_len = len;
  819. st->in_len = len;
  820. st->dma_addr = st->unmap_addr;
  821. return 0;
  822. }
  823. return -ENOMEM;
  824. }
  825. /**
  826. * tso_fill_packet_with_fragment - form descriptors for the current fragment
  827. * @tx_queue: Efx TX queue
  828. * @skb: Socket buffer
  829. * @st: TSO state
  830. *
  831. * Form descriptors for the current fragment, until we reach the end
  832. * of fragment or end-of-packet. Return 0 on success, 1 if not enough
  833. * space in @tx_queue.
  834. */
  835. static int tso_fill_packet_with_fragment(struct efx_tx_queue *tx_queue,
  836. const struct sk_buff *skb,
  837. struct tso_state *st)
  838. {
  839. struct efx_tx_buffer *buffer;
  840. int n, end_of_packet, rc;
  841. if (st->in_len == 0)
  842. return 0;
  843. if (st->packet_space == 0)
  844. return 0;
  845. EFX_BUG_ON_PARANOID(st->in_len <= 0);
  846. EFX_BUG_ON_PARANOID(st->packet_space <= 0);
  847. n = min(st->in_len, st->packet_space);
  848. st->packet_space -= n;
  849. st->out_len -= n;
  850. st->in_len -= n;
  851. rc = efx_tx_queue_insert(tx_queue, st->dma_addr, n, &buffer);
  852. if (likely(rc == 0)) {
  853. if (st->out_len == 0)
  854. /* Transfer ownership of the skb */
  855. buffer->skb = skb;
  856. end_of_packet = st->out_len == 0 || st->packet_space == 0;
  857. buffer->continuation = !end_of_packet;
  858. if (st->in_len == 0) {
  859. /* Transfer ownership of the DMA mapping */
  860. buffer->unmap_len = st->unmap_len;
  861. buffer->unmap_single = st->unmap_single;
  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.unmap_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. }