tx.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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. /* PTP "event" packet */
  295. if (unlikely(efx_xmit_with_hwtstamp(skb)) &&
  296. unlikely(efx_ptp_is_ptp_tx(efx, skb))) {
  297. return efx_ptp_tx(efx, skb);
  298. }
  299. index = skb_get_queue_mapping(skb);
  300. type = skb->ip_summed == CHECKSUM_PARTIAL ? EFX_TXQ_TYPE_OFFLOAD : 0;
  301. if (index >= efx->n_tx_channels) {
  302. index -= efx->n_tx_channels;
  303. type |= EFX_TXQ_TYPE_HIGHPRI;
  304. }
  305. tx_queue = efx_get_tx_queue(efx, index, type);
  306. return efx_enqueue_skb(tx_queue, skb);
  307. }
  308. void efx_init_tx_queue_core_txq(struct efx_tx_queue *tx_queue)
  309. {
  310. struct efx_nic *efx = tx_queue->efx;
  311. /* Must be inverse of queue lookup in efx_hard_start_xmit() */
  312. tx_queue->core_txq =
  313. netdev_get_tx_queue(efx->net_dev,
  314. tx_queue->queue / EFX_TXQ_TYPES +
  315. ((tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI) ?
  316. efx->n_tx_channels : 0));
  317. }
  318. int efx_setup_tc(struct net_device *net_dev, u8 num_tc)
  319. {
  320. struct efx_nic *efx = netdev_priv(net_dev);
  321. struct efx_channel *channel;
  322. struct efx_tx_queue *tx_queue;
  323. unsigned tc;
  324. int rc;
  325. if (efx_nic_rev(efx) < EFX_REV_FALCON_B0 || num_tc > EFX_MAX_TX_TC)
  326. return -EINVAL;
  327. if (num_tc == net_dev->num_tc)
  328. return 0;
  329. for (tc = 0; tc < num_tc; tc++) {
  330. net_dev->tc_to_txq[tc].offset = tc * efx->n_tx_channels;
  331. net_dev->tc_to_txq[tc].count = efx->n_tx_channels;
  332. }
  333. if (num_tc > net_dev->num_tc) {
  334. /* Initialise high-priority queues as necessary */
  335. efx_for_each_channel(channel, efx) {
  336. efx_for_each_possible_channel_tx_queue(tx_queue,
  337. channel) {
  338. if (!(tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI))
  339. continue;
  340. if (!tx_queue->buffer) {
  341. rc = efx_probe_tx_queue(tx_queue);
  342. if (rc)
  343. return rc;
  344. }
  345. if (!tx_queue->initialised)
  346. efx_init_tx_queue(tx_queue);
  347. efx_init_tx_queue_core_txq(tx_queue);
  348. }
  349. }
  350. } else {
  351. /* Reduce number of classes before number of queues */
  352. net_dev->num_tc = num_tc;
  353. }
  354. rc = netif_set_real_num_tx_queues(net_dev,
  355. max_t(int, num_tc, 1) *
  356. efx->n_tx_channels);
  357. if (rc)
  358. return rc;
  359. /* Do not destroy high-priority queues when they become
  360. * unused. We would have to flush them first, and it is
  361. * fairly difficult to flush a subset of TX queues. Leave
  362. * it to efx_fini_channels().
  363. */
  364. net_dev->num_tc = num_tc;
  365. return 0;
  366. }
  367. void efx_xmit_done(struct efx_tx_queue *tx_queue, unsigned int index)
  368. {
  369. unsigned fill_level;
  370. struct efx_nic *efx = tx_queue->efx;
  371. struct efx_tx_queue *txq2;
  372. unsigned int pkts_compl = 0, bytes_compl = 0;
  373. EFX_BUG_ON_PARANOID(index > tx_queue->ptr_mask);
  374. efx_dequeue_buffers(tx_queue, index, &pkts_compl, &bytes_compl);
  375. netdev_tx_completed_queue(tx_queue->core_txq, pkts_compl, bytes_compl);
  376. /* See if we need to restart the netif queue. This memory
  377. * barrier ensures that we write read_count (inside
  378. * efx_dequeue_buffers()) before reading the queue status.
  379. */
  380. smp_mb();
  381. if (unlikely(netif_tx_queue_stopped(tx_queue->core_txq)) &&
  382. likely(efx->port_enabled) &&
  383. likely(netif_device_present(efx->net_dev))) {
  384. txq2 = efx_tx_queue_partner(tx_queue);
  385. fill_level = max(tx_queue->insert_count - tx_queue->read_count,
  386. txq2->insert_count - txq2->read_count);
  387. if (fill_level <= efx->txq_wake_thresh)
  388. netif_tx_wake_queue(tx_queue->core_txq);
  389. }
  390. /* Check whether the hardware queue is now empty */
  391. if ((int)(tx_queue->read_count - tx_queue->old_write_count) >= 0) {
  392. tx_queue->old_write_count = ACCESS_ONCE(tx_queue->write_count);
  393. if (tx_queue->read_count == tx_queue->old_write_count) {
  394. smp_mb();
  395. tx_queue->empty_read_count =
  396. tx_queue->read_count | EFX_EMPTY_COUNT_VALID;
  397. }
  398. }
  399. }
  400. /* Size of page-based TSO header buffers. Larger blocks must be
  401. * allocated from the heap.
  402. */
  403. #define TSOH_STD_SIZE 128
  404. #define TSOH_PER_PAGE (PAGE_SIZE / TSOH_STD_SIZE)
  405. /* At most half the descriptors in the queue at any time will refer to
  406. * a TSO header buffer, since they must always be followed by a
  407. * payload descriptor referring to an skb.
  408. */
  409. static unsigned int efx_tsoh_page_count(struct efx_tx_queue *tx_queue)
  410. {
  411. return DIV_ROUND_UP(tx_queue->ptr_mask + 1, 2 * TSOH_PER_PAGE);
  412. }
  413. int efx_probe_tx_queue(struct efx_tx_queue *tx_queue)
  414. {
  415. struct efx_nic *efx = tx_queue->efx;
  416. unsigned int entries;
  417. int rc;
  418. /* Create the smallest power-of-two aligned ring */
  419. entries = max(roundup_pow_of_two(efx->txq_entries), EFX_MIN_DMAQ_SIZE);
  420. EFX_BUG_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE);
  421. tx_queue->ptr_mask = entries - 1;
  422. netif_dbg(efx, probe, efx->net_dev,
  423. "creating TX queue %d size %#x mask %#x\n",
  424. tx_queue->queue, efx->txq_entries, tx_queue->ptr_mask);
  425. /* Allocate software ring */
  426. tx_queue->buffer = kcalloc(entries, sizeof(*tx_queue->buffer),
  427. GFP_KERNEL);
  428. if (!tx_queue->buffer)
  429. return -ENOMEM;
  430. if (tx_queue->queue & EFX_TXQ_TYPE_OFFLOAD) {
  431. tx_queue->tsoh_page =
  432. kcalloc(efx_tsoh_page_count(tx_queue),
  433. sizeof(tx_queue->tsoh_page[0]), GFP_KERNEL);
  434. if (!tx_queue->tsoh_page) {
  435. rc = -ENOMEM;
  436. goto fail1;
  437. }
  438. }
  439. /* Allocate hardware ring */
  440. rc = efx_nic_probe_tx(tx_queue);
  441. if (rc)
  442. goto fail2;
  443. return 0;
  444. fail2:
  445. kfree(tx_queue->tsoh_page);
  446. tx_queue->tsoh_page = NULL;
  447. fail1:
  448. kfree(tx_queue->buffer);
  449. tx_queue->buffer = NULL;
  450. return rc;
  451. }
  452. void efx_init_tx_queue(struct efx_tx_queue *tx_queue)
  453. {
  454. netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
  455. "initialising TX queue %d\n", tx_queue->queue);
  456. tx_queue->insert_count = 0;
  457. tx_queue->write_count = 0;
  458. tx_queue->old_write_count = 0;
  459. tx_queue->read_count = 0;
  460. tx_queue->old_read_count = 0;
  461. tx_queue->empty_read_count = 0 | EFX_EMPTY_COUNT_VALID;
  462. /* Set up TX descriptor ring */
  463. efx_nic_init_tx(tx_queue);
  464. tx_queue->initialised = true;
  465. }
  466. void efx_release_tx_buffers(struct efx_tx_queue *tx_queue)
  467. {
  468. struct efx_tx_buffer *buffer;
  469. if (!tx_queue->buffer)
  470. return;
  471. /* Free any buffers left in the ring */
  472. while (tx_queue->read_count != tx_queue->write_count) {
  473. unsigned int pkts_compl = 0, bytes_compl = 0;
  474. buffer = &tx_queue->buffer[tx_queue->read_count & tx_queue->ptr_mask];
  475. efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl);
  476. ++tx_queue->read_count;
  477. }
  478. netdev_tx_reset_queue(tx_queue->core_txq);
  479. }
  480. void efx_fini_tx_queue(struct efx_tx_queue *tx_queue)
  481. {
  482. if (!tx_queue->initialised)
  483. return;
  484. netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
  485. "shutting down TX queue %d\n", tx_queue->queue);
  486. tx_queue->initialised = false;
  487. /* Flush TX queue, remove descriptor ring */
  488. efx_nic_fini_tx(tx_queue);
  489. efx_release_tx_buffers(tx_queue);
  490. }
  491. void efx_remove_tx_queue(struct efx_tx_queue *tx_queue)
  492. {
  493. int i;
  494. if (!tx_queue->buffer)
  495. return;
  496. netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
  497. "destroying TX queue %d\n", tx_queue->queue);
  498. efx_nic_remove_tx(tx_queue);
  499. if (tx_queue->tsoh_page) {
  500. for (i = 0; i < efx_tsoh_page_count(tx_queue); i++)
  501. efx_nic_free_buffer(tx_queue->efx,
  502. &tx_queue->tsoh_page[i]);
  503. kfree(tx_queue->tsoh_page);
  504. tx_queue->tsoh_page = NULL;
  505. }
  506. kfree(tx_queue->buffer);
  507. tx_queue->buffer = NULL;
  508. }
  509. /* Efx TCP segmentation acceleration.
  510. *
  511. * Why? Because by doing it here in the driver we can go significantly
  512. * faster than the GSO.
  513. *
  514. * Requires TX checksum offload support.
  515. */
  516. /* Number of bytes inserted at the start of a TSO header buffer,
  517. * similar to NET_IP_ALIGN.
  518. */
  519. #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  520. #define TSOH_OFFSET 0
  521. #else
  522. #define TSOH_OFFSET NET_IP_ALIGN
  523. #endif
  524. #define PTR_DIFF(p1, p2) ((u8 *)(p1) - (u8 *)(p2))
  525. /**
  526. * struct tso_state - TSO state for an SKB
  527. * @out_len: Remaining length in current segment
  528. * @seqnum: Current sequence number
  529. * @ipv4_id: Current IPv4 ID, host endian
  530. * @packet_space: Remaining space in current packet
  531. * @dma_addr: DMA address of current position
  532. * @in_len: Remaining length in current SKB fragment
  533. * @unmap_len: Length of SKB fragment
  534. * @unmap_addr: DMA address of SKB fragment
  535. * @dma_flags: TX buffer flags for DMA mapping - %EFX_TX_BUF_MAP_SINGLE or 0
  536. * @protocol: Network protocol (after any VLAN header)
  537. * @ip_off: Offset of IP header
  538. * @tcp_off: Offset of TCP header
  539. * @header_len: Number of bytes of header
  540. * @ip_base_len: IPv4 tot_len or IPv6 payload_len, before TCP payload
  541. *
  542. * The state used during segmentation. It is put into this data structure
  543. * just to make it easy to pass into inline functions.
  544. */
  545. struct tso_state {
  546. /* Output position */
  547. unsigned out_len;
  548. unsigned seqnum;
  549. unsigned ipv4_id;
  550. unsigned packet_space;
  551. /* Input position */
  552. dma_addr_t dma_addr;
  553. unsigned in_len;
  554. unsigned unmap_len;
  555. dma_addr_t unmap_addr;
  556. unsigned short dma_flags;
  557. __be16 protocol;
  558. unsigned int ip_off;
  559. unsigned int tcp_off;
  560. unsigned header_len;
  561. unsigned int ip_base_len;
  562. };
  563. /*
  564. * Verify that our various assumptions about sk_buffs and the conditions
  565. * under which TSO will be attempted hold true. Return the protocol number.
  566. */
  567. static __be16 efx_tso_check_protocol(struct sk_buff *skb)
  568. {
  569. __be16 protocol = skb->protocol;
  570. EFX_BUG_ON_PARANOID(((struct ethhdr *)skb->data)->h_proto !=
  571. protocol);
  572. if (protocol == htons(ETH_P_8021Q)) {
  573. struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
  574. protocol = veh->h_vlan_encapsulated_proto;
  575. }
  576. if (protocol == htons(ETH_P_IP)) {
  577. EFX_BUG_ON_PARANOID(ip_hdr(skb)->protocol != IPPROTO_TCP);
  578. } else {
  579. EFX_BUG_ON_PARANOID(protocol != htons(ETH_P_IPV6));
  580. EFX_BUG_ON_PARANOID(ipv6_hdr(skb)->nexthdr != NEXTHDR_TCP);
  581. }
  582. EFX_BUG_ON_PARANOID((PTR_DIFF(tcp_hdr(skb), skb->data)
  583. + (tcp_hdr(skb)->doff << 2u)) >
  584. skb_headlen(skb));
  585. return protocol;
  586. }
  587. static u8 *efx_tsoh_get_buffer(struct efx_tx_queue *tx_queue,
  588. struct efx_tx_buffer *buffer, unsigned int len)
  589. {
  590. u8 *result;
  591. EFX_BUG_ON_PARANOID(buffer->len);
  592. EFX_BUG_ON_PARANOID(buffer->flags);
  593. EFX_BUG_ON_PARANOID(buffer->unmap_len);
  594. if (likely(len <= TSOH_STD_SIZE - TSOH_OFFSET)) {
  595. unsigned index =
  596. (tx_queue->insert_count & tx_queue->ptr_mask) / 2;
  597. struct efx_buffer *page_buf =
  598. &tx_queue->tsoh_page[index / TSOH_PER_PAGE];
  599. unsigned offset =
  600. TSOH_STD_SIZE * (index % TSOH_PER_PAGE) + TSOH_OFFSET;
  601. if (unlikely(!page_buf->addr) &&
  602. efx_nic_alloc_buffer(tx_queue->efx, page_buf, PAGE_SIZE))
  603. return NULL;
  604. result = (u8 *)page_buf->addr + offset;
  605. buffer->dma_addr = page_buf->dma_addr + offset;
  606. buffer->flags = EFX_TX_BUF_CONT;
  607. } else {
  608. tx_queue->tso_long_headers++;
  609. buffer->heap_buf = kmalloc(TSOH_OFFSET + len, GFP_ATOMIC);
  610. if (unlikely(!buffer->heap_buf))
  611. return NULL;
  612. result = (u8 *)buffer->heap_buf + TSOH_OFFSET;
  613. buffer->flags = EFX_TX_BUF_CONT | EFX_TX_BUF_HEAP;
  614. }
  615. buffer->len = len;
  616. return result;
  617. }
  618. /**
  619. * efx_tx_queue_insert - push descriptors onto the TX queue
  620. * @tx_queue: Efx TX queue
  621. * @dma_addr: DMA address of fragment
  622. * @len: Length of fragment
  623. * @final_buffer: The final buffer inserted into the queue
  624. *
  625. * Push descriptors onto the TX queue.
  626. */
  627. static void efx_tx_queue_insert(struct efx_tx_queue *tx_queue,
  628. dma_addr_t dma_addr, unsigned len,
  629. struct efx_tx_buffer **final_buffer)
  630. {
  631. struct efx_tx_buffer *buffer;
  632. struct efx_nic *efx = tx_queue->efx;
  633. unsigned dma_len, insert_ptr;
  634. EFX_BUG_ON_PARANOID(len <= 0);
  635. while (1) {
  636. insert_ptr = tx_queue->insert_count & tx_queue->ptr_mask;
  637. buffer = &tx_queue->buffer[insert_ptr];
  638. ++tx_queue->insert_count;
  639. EFX_BUG_ON_PARANOID(tx_queue->insert_count -
  640. tx_queue->read_count >=
  641. efx->txq_entries);
  642. EFX_BUG_ON_PARANOID(buffer->len);
  643. EFX_BUG_ON_PARANOID(buffer->unmap_len);
  644. EFX_BUG_ON_PARANOID(buffer->flags);
  645. buffer->dma_addr = dma_addr;
  646. dma_len = efx_max_tx_len(efx, dma_addr);
  647. /* If there is enough space to send then do so */
  648. if (dma_len >= len)
  649. break;
  650. buffer->len = dma_len;
  651. buffer->flags = EFX_TX_BUF_CONT;
  652. dma_addr += dma_len;
  653. len -= dma_len;
  654. }
  655. EFX_BUG_ON_PARANOID(!len);
  656. buffer->len = len;
  657. *final_buffer = buffer;
  658. }
  659. /*
  660. * Put a TSO header into the TX queue.
  661. *
  662. * This is special-cased because we know that it is small enough to fit in
  663. * a single fragment, and we know it doesn't cross a page boundary. It
  664. * also allows us to not worry about end-of-packet etc.
  665. */
  666. static int efx_tso_put_header(struct efx_tx_queue *tx_queue,
  667. struct efx_tx_buffer *buffer, u8 *header)
  668. {
  669. if (unlikely(buffer->flags & EFX_TX_BUF_HEAP)) {
  670. buffer->dma_addr = dma_map_single(&tx_queue->efx->pci_dev->dev,
  671. header, buffer->len,
  672. DMA_TO_DEVICE);
  673. if (unlikely(dma_mapping_error(&tx_queue->efx->pci_dev->dev,
  674. buffer->dma_addr))) {
  675. kfree(buffer->heap_buf);
  676. buffer->len = 0;
  677. buffer->flags = 0;
  678. return -ENOMEM;
  679. }
  680. buffer->unmap_len = buffer->len;
  681. buffer->flags |= EFX_TX_BUF_MAP_SINGLE;
  682. }
  683. ++tx_queue->insert_count;
  684. return 0;
  685. }
  686. /* Remove buffers put into a tx_queue. None of the buffers must have
  687. * an skb attached.
  688. */
  689. static void efx_enqueue_unwind(struct efx_tx_queue *tx_queue)
  690. {
  691. struct efx_tx_buffer *buffer;
  692. /* Work backwards until we hit the original insert pointer value */
  693. while (tx_queue->insert_count != tx_queue->write_count) {
  694. --tx_queue->insert_count;
  695. buffer = &tx_queue->buffer[tx_queue->insert_count &
  696. tx_queue->ptr_mask];
  697. efx_dequeue_buffer(tx_queue, buffer, NULL, NULL);
  698. }
  699. }
  700. /* Parse the SKB header and initialise state. */
  701. static void tso_start(struct tso_state *st, const struct sk_buff *skb)
  702. {
  703. st->ip_off = skb_network_header(skb) - skb->data;
  704. st->tcp_off = skb_transport_header(skb) - skb->data;
  705. st->header_len = st->tcp_off + (tcp_hdr(skb)->doff << 2u);
  706. if (st->protocol == htons(ETH_P_IP)) {
  707. st->ip_base_len = st->header_len - st->ip_off;
  708. st->ipv4_id = ntohs(ip_hdr(skb)->id);
  709. } else {
  710. st->ip_base_len = st->header_len - st->tcp_off;
  711. st->ipv4_id = 0;
  712. }
  713. st->seqnum = ntohl(tcp_hdr(skb)->seq);
  714. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->urg);
  715. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->syn);
  716. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->rst);
  717. st->out_len = skb->len - st->header_len;
  718. st->unmap_len = 0;
  719. st->dma_flags = 0;
  720. }
  721. static int tso_get_fragment(struct tso_state *st, struct efx_nic *efx,
  722. skb_frag_t *frag)
  723. {
  724. st->unmap_addr = skb_frag_dma_map(&efx->pci_dev->dev, frag, 0,
  725. skb_frag_size(frag), DMA_TO_DEVICE);
  726. if (likely(!dma_mapping_error(&efx->pci_dev->dev, st->unmap_addr))) {
  727. st->dma_flags = 0;
  728. st->unmap_len = skb_frag_size(frag);
  729. st->in_len = skb_frag_size(frag);
  730. st->dma_addr = st->unmap_addr;
  731. return 0;
  732. }
  733. return -ENOMEM;
  734. }
  735. static int tso_get_head_fragment(struct tso_state *st, struct efx_nic *efx,
  736. const struct sk_buff *skb)
  737. {
  738. int hl = st->header_len;
  739. int len = skb_headlen(skb) - hl;
  740. st->unmap_addr = dma_map_single(&efx->pci_dev->dev, skb->data + hl,
  741. len, DMA_TO_DEVICE);
  742. if (likely(!dma_mapping_error(&efx->pci_dev->dev, st->unmap_addr))) {
  743. st->dma_flags = EFX_TX_BUF_MAP_SINGLE;
  744. st->unmap_len = len;
  745. st->in_len = len;
  746. st->dma_addr = st->unmap_addr;
  747. return 0;
  748. }
  749. return -ENOMEM;
  750. }
  751. /**
  752. * tso_fill_packet_with_fragment - form descriptors for the current fragment
  753. * @tx_queue: Efx TX queue
  754. * @skb: Socket buffer
  755. * @st: TSO state
  756. *
  757. * Form descriptors for the current fragment, until we reach the end
  758. * of fragment or end-of-packet.
  759. */
  760. static void tso_fill_packet_with_fragment(struct efx_tx_queue *tx_queue,
  761. const struct sk_buff *skb,
  762. struct tso_state *st)
  763. {
  764. struct efx_tx_buffer *buffer;
  765. int n;
  766. if (st->in_len == 0)
  767. return;
  768. if (st->packet_space == 0)
  769. return;
  770. EFX_BUG_ON_PARANOID(st->in_len <= 0);
  771. EFX_BUG_ON_PARANOID(st->packet_space <= 0);
  772. n = min(st->in_len, st->packet_space);
  773. st->packet_space -= n;
  774. st->out_len -= n;
  775. st->in_len -= n;
  776. efx_tx_queue_insert(tx_queue, st->dma_addr, n, &buffer);
  777. if (st->out_len == 0) {
  778. /* Transfer ownership of the skb */
  779. buffer->skb = skb;
  780. buffer->flags = EFX_TX_BUF_SKB;
  781. } else if (st->packet_space != 0) {
  782. buffer->flags = EFX_TX_BUF_CONT;
  783. }
  784. if (st->in_len == 0) {
  785. /* Transfer ownership of the DMA mapping */
  786. buffer->unmap_len = st->unmap_len;
  787. buffer->flags |= st->dma_flags;
  788. st->unmap_len = 0;
  789. }
  790. st->dma_addr += n;
  791. }
  792. /**
  793. * tso_start_new_packet - generate a new header and prepare for the new packet
  794. * @tx_queue: Efx TX queue
  795. * @skb: Socket buffer
  796. * @st: TSO state
  797. *
  798. * Generate a new header and prepare for the new packet. Return 0 on
  799. * success, or -%ENOMEM if failed to alloc header.
  800. */
  801. static int tso_start_new_packet(struct efx_tx_queue *tx_queue,
  802. const struct sk_buff *skb,
  803. struct tso_state *st)
  804. {
  805. struct efx_tx_buffer *buffer =
  806. &tx_queue->buffer[tx_queue->insert_count & tx_queue->ptr_mask];
  807. struct tcphdr *tsoh_th;
  808. unsigned ip_length;
  809. u8 *header;
  810. int rc;
  811. /* Allocate and insert a DMA-mapped header buffer. */
  812. header = efx_tsoh_get_buffer(tx_queue, buffer, st->header_len);
  813. if (!header)
  814. return -ENOMEM;
  815. tsoh_th = (struct tcphdr *)(header + st->tcp_off);
  816. /* Copy and update the headers. */
  817. memcpy(header, skb->data, st->header_len);
  818. tsoh_th->seq = htonl(st->seqnum);
  819. st->seqnum += skb_shinfo(skb)->gso_size;
  820. if (st->out_len > skb_shinfo(skb)->gso_size) {
  821. /* This packet will not finish the TSO burst. */
  822. st->packet_space = skb_shinfo(skb)->gso_size;
  823. tsoh_th->fin = 0;
  824. tsoh_th->psh = 0;
  825. } else {
  826. /* This packet will be the last in the TSO burst. */
  827. st->packet_space = st->out_len;
  828. tsoh_th->fin = tcp_hdr(skb)->fin;
  829. tsoh_th->psh = tcp_hdr(skb)->psh;
  830. }
  831. ip_length = st->ip_base_len + st->packet_space;
  832. if (st->protocol == htons(ETH_P_IP)) {
  833. struct iphdr *tsoh_iph = (struct iphdr *)(header + st->ip_off);
  834. tsoh_iph->tot_len = htons(ip_length);
  835. /* Linux leaves suitable gaps in the IP ID space for us to fill. */
  836. tsoh_iph->id = htons(st->ipv4_id);
  837. st->ipv4_id++;
  838. } else {
  839. struct ipv6hdr *tsoh_iph =
  840. (struct ipv6hdr *)(header + st->ip_off);
  841. tsoh_iph->payload_len = htons(ip_length);
  842. }
  843. rc = efx_tso_put_header(tx_queue, buffer, header);
  844. if (unlikely(rc))
  845. return rc;
  846. ++tx_queue->tso_packets;
  847. return 0;
  848. }
  849. /**
  850. * efx_enqueue_skb_tso - segment and transmit a TSO socket buffer
  851. * @tx_queue: Efx TX queue
  852. * @skb: Socket buffer
  853. *
  854. * Context: You must hold netif_tx_lock() to call this function.
  855. *
  856. * Add socket buffer @skb to @tx_queue, doing TSO or return != 0 if
  857. * @skb was not enqueued. In all cases @skb is consumed. Return
  858. * %NETDEV_TX_OK.
  859. */
  860. static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
  861. struct sk_buff *skb)
  862. {
  863. struct efx_nic *efx = tx_queue->efx;
  864. int frag_i, rc;
  865. struct tso_state state;
  866. /* Find the packet protocol and sanity-check it */
  867. state.protocol = efx_tso_check_protocol(skb);
  868. EFX_BUG_ON_PARANOID(tx_queue->write_count != tx_queue->insert_count);
  869. tso_start(&state, skb);
  870. /* Assume that skb header area contains exactly the headers, and
  871. * all payload is in the frag list.
  872. */
  873. if (skb_headlen(skb) == state.header_len) {
  874. /* Grab the first payload fragment. */
  875. EFX_BUG_ON_PARANOID(skb_shinfo(skb)->nr_frags < 1);
  876. frag_i = 0;
  877. rc = tso_get_fragment(&state, efx,
  878. skb_shinfo(skb)->frags + frag_i);
  879. if (rc)
  880. goto mem_err;
  881. } else {
  882. rc = tso_get_head_fragment(&state, efx, skb);
  883. if (rc)
  884. goto mem_err;
  885. frag_i = -1;
  886. }
  887. if (tso_start_new_packet(tx_queue, skb, &state) < 0)
  888. goto mem_err;
  889. while (1) {
  890. tso_fill_packet_with_fragment(tx_queue, skb, &state);
  891. /* Move onto the next fragment? */
  892. if (state.in_len == 0) {
  893. if (++frag_i >= skb_shinfo(skb)->nr_frags)
  894. /* End of payload reached. */
  895. break;
  896. rc = tso_get_fragment(&state, efx,
  897. skb_shinfo(skb)->frags + frag_i);
  898. if (rc)
  899. goto mem_err;
  900. }
  901. /* Start at new packet? */
  902. if (state.packet_space == 0 &&
  903. tso_start_new_packet(tx_queue, skb, &state) < 0)
  904. goto mem_err;
  905. }
  906. netdev_tx_sent_queue(tx_queue->core_txq, skb->len);
  907. /* Pass off to hardware */
  908. efx_nic_push_buffers(tx_queue);
  909. efx_tx_maybe_stop_queue(tx_queue);
  910. tx_queue->tso_bursts++;
  911. return NETDEV_TX_OK;
  912. mem_err:
  913. netif_err(efx, tx_err, efx->net_dev,
  914. "Out of memory for TSO headers, or DMA mapping error\n");
  915. dev_kfree_skb_any(skb);
  916. /* Free the DMA mapping we were in the process of writing out */
  917. if (state.unmap_len) {
  918. if (state.dma_flags & EFX_TX_BUF_MAP_SINGLE)
  919. dma_unmap_single(&efx->pci_dev->dev, state.unmap_addr,
  920. state.unmap_len, DMA_TO_DEVICE);
  921. else
  922. dma_unmap_page(&efx->pci_dev->dev, state.unmap_addr,
  923. state.unmap_len, DMA_TO_DEVICE);
  924. }
  925. efx_enqueue_unwind(tx_queue);
  926. return NETDEV_TX_OK;
  927. }