tx.c 30 KB

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