tx.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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_fini_tx_queue(struct efx_tx_queue *tx_queue)
  467. {
  468. struct efx_tx_buffer *buffer;
  469. netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
  470. "shutting down TX queue %d\n", tx_queue->queue);
  471. if (!tx_queue->buffer)
  472. return;
  473. /* Free any buffers left in the ring */
  474. while (tx_queue->read_count != tx_queue->write_count) {
  475. unsigned int pkts_compl = 0, bytes_compl = 0;
  476. buffer = &tx_queue->buffer[tx_queue->read_count & tx_queue->ptr_mask];
  477. efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl);
  478. ++tx_queue->read_count;
  479. }
  480. netdev_tx_reset_queue(tx_queue->core_txq);
  481. }
  482. void efx_remove_tx_queue(struct efx_tx_queue *tx_queue)
  483. {
  484. int i;
  485. if (!tx_queue->buffer)
  486. return;
  487. netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
  488. "destroying TX queue %d\n", tx_queue->queue);
  489. efx_nic_remove_tx(tx_queue);
  490. if (tx_queue->tsoh_page) {
  491. for (i = 0; i < efx_tsoh_page_count(tx_queue); i++)
  492. efx_nic_free_buffer(tx_queue->efx,
  493. &tx_queue->tsoh_page[i]);
  494. kfree(tx_queue->tsoh_page);
  495. tx_queue->tsoh_page = NULL;
  496. }
  497. kfree(tx_queue->buffer);
  498. tx_queue->buffer = NULL;
  499. }
  500. /* Efx TCP segmentation acceleration.
  501. *
  502. * Why? Because by doing it here in the driver we can go significantly
  503. * faster than the GSO.
  504. *
  505. * Requires TX checksum offload support.
  506. */
  507. /* Number of bytes inserted at the start of a TSO header buffer,
  508. * similar to NET_IP_ALIGN.
  509. */
  510. #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  511. #define TSOH_OFFSET 0
  512. #else
  513. #define TSOH_OFFSET NET_IP_ALIGN
  514. #endif
  515. #define PTR_DIFF(p1, p2) ((u8 *)(p1) - (u8 *)(p2))
  516. /**
  517. * struct tso_state - TSO state for an SKB
  518. * @out_len: Remaining length in current segment
  519. * @seqnum: Current sequence number
  520. * @ipv4_id: Current IPv4 ID, host endian
  521. * @packet_space: Remaining space in current packet
  522. * @dma_addr: DMA address of current position
  523. * @in_len: Remaining length in current SKB fragment
  524. * @unmap_len: Length of SKB fragment
  525. * @unmap_addr: DMA address of SKB fragment
  526. * @dma_flags: TX buffer flags for DMA mapping - %EFX_TX_BUF_MAP_SINGLE or 0
  527. * @protocol: Network protocol (after any VLAN header)
  528. * @ip_off: Offset of IP header
  529. * @tcp_off: Offset of TCP header
  530. * @header_len: Number of bytes of header
  531. * @ip_base_len: IPv4 tot_len or IPv6 payload_len, before TCP payload
  532. *
  533. * The state used during segmentation. It is put into this data structure
  534. * just to make it easy to pass into inline functions.
  535. */
  536. struct tso_state {
  537. /* Output position */
  538. unsigned out_len;
  539. unsigned seqnum;
  540. unsigned ipv4_id;
  541. unsigned packet_space;
  542. /* Input position */
  543. dma_addr_t dma_addr;
  544. unsigned in_len;
  545. unsigned unmap_len;
  546. dma_addr_t unmap_addr;
  547. unsigned short dma_flags;
  548. __be16 protocol;
  549. unsigned int ip_off;
  550. unsigned int tcp_off;
  551. unsigned header_len;
  552. unsigned int ip_base_len;
  553. };
  554. /*
  555. * Verify that our various assumptions about sk_buffs and the conditions
  556. * under which TSO will be attempted hold true. Return the protocol number.
  557. */
  558. static __be16 efx_tso_check_protocol(struct sk_buff *skb)
  559. {
  560. __be16 protocol = skb->protocol;
  561. EFX_BUG_ON_PARANOID(((struct ethhdr *)skb->data)->h_proto !=
  562. protocol);
  563. if (protocol == htons(ETH_P_8021Q)) {
  564. struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
  565. protocol = veh->h_vlan_encapsulated_proto;
  566. }
  567. if (protocol == htons(ETH_P_IP)) {
  568. EFX_BUG_ON_PARANOID(ip_hdr(skb)->protocol != IPPROTO_TCP);
  569. } else {
  570. EFX_BUG_ON_PARANOID(protocol != htons(ETH_P_IPV6));
  571. EFX_BUG_ON_PARANOID(ipv6_hdr(skb)->nexthdr != NEXTHDR_TCP);
  572. }
  573. EFX_BUG_ON_PARANOID((PTR_DIFF(tcp_hdr(skb), skb->data)
  574. + (tcp_hdr(skb)->doff << 2u)) >
  575. skb_headlen(skb));
  576. return protocol;
  577. }
  578. static u8 *efx_tsoh_get_buffer(struct efx_tx_queue *tx_queue,
  579. struct efx_tx_buffer *buffer, unsigned int len)
  580. {
  581. u8 *result;
  582. EFX_BUG_ON_PARANOID(buffer->len);
  583. EFX_BUG_ON_PARANOID(buffer->flags);
  584. EFX_BUG_ON_PARANOID(buffer->unmap_len);
  585. if (likely(len <= TSOH_STD_SIZE - TSOH_OFFSET)) {
  586. unsigned index =
  587. (tx_queue->insert_count & tx_queue->ptr_mask) / 2;
  588. struct efx_buffer *page_buf =
  589. &tx_queue->tsoh_page[index / TSOH_PER_PAGE];
  590. unsigned offset =
  591. TSOH_STD_SIZE * (index % TSOH_PER_PAGE) + TSOH_OFFSET;
  592. if (unlikely(!page_buf->addr) &&
  593. efx_nic_alloc_buffer(tx_queue->efx, page_buf, PAGE_SIZE,
  594. GFP_ATOMIC))
  595. return NULL;
  596. result = (u8 *)page_buf->addr + offset;
  597. buffer->dma_addr = page_buf->dma_addr + offset;
  598. buffer->flags = EFX_TX_BUF_CONT;
  599. } else {
  600. tx_queue->tso_long_headers++;
  601. buffer->heap_buf = kmalloc(TSOH_OFFSET + len, GFP_ATOMIC);
  602. if (unlikely(!buffer->heap_buf))
  603. return NULL;
  604. result = (u8 *)buffer->heap_buf + TSOH_OFFSET;
  605. buffer->flags = EFX_TX_BUF_CONT | EFX_TX_BUF_HEAP;
  606. }
  607. buffer->len = len;
  608. return result;
  609. }
  610. /**
  611. * efx_tx_queue_insert - push descriptors onto the TX queue
  612. * @tx_queue: Efx TX queue
  613. * @dma_addr: DMA address of fragment
  614. * @len: Length of fragment
  615. * @final_buffer: The final buffer inserted into the queue
  616. *
  617. * Push descriptors onto the TX queue.
  618. */
  619. static void efx_tx_queue_insert(struct efx_tx_queue *tx_queue,
  620. dma_addr_t dma_addr, unsigned len,
  621. struct efx_tx_buffer **final_buffer)
  622. {
  623. struct efx_tx_buffer *buffer;
  624. struct efx_nic *efx = tx_queue->efx;
  625. unsigned dma_len, insert_ptr;
  626. EFX_BUG_ON_PARANOID(len <= 0);
  627. while (1) {
  628. insert_ptr = tx_queue->insert_count & tx_queue->ptr_mask;
  629. buffer = &tx_queue->buffer[insert_ptr];
  630. ++tx_queue->insert_count;
  631. EFX_BUG_ON_PARANOID(tx_queue->insert_count -
  632. tx_queue->read_count >=
  633. efx->txq_entries);
  634. EFX_BUG_ON_PARANOID(buffer->len);
  635. EFX_BUG_ON_PARANOID(buffer->unmap_len);
  636. EFX_BUG_ON_PARANOID(buffer->flags);
  637. buffer->dma_addr = dma_addr;
  638. dma_len = efx_max_tx_len(efx, dma_addr);
  639. /* If there is enough space to send then do so */
  640. if (dma_len >= len)
  641. break;
  642. buffer->len = dma_len;
  643. buffer->flags = EFX_TX_BUF_CONT;
  644. dma_addr += dma_len;
  645. len -= dma_len;
  646. }
  647. EFX_BUG_ON_PARANOID(!len);
  648. buffer->len = len;
  649. *final_buffer = buffer;
  650. }
  651. /*
  652. * Put a TSO header into the TX queue.
  653. *
  654. * This is special-cased because we know that it is small enough to fit in
  655. * a single fragment, and we know it doesn't cross a page boundary. It
  656. * also allows us to not worry about end-of-packet etc.
  657. */
  658. static int efx_tso_put_header(struct efx_tx_queue *tx_queue,
  659. struct efx_tx_buffer *buffer, u8 *header)
  660. {
  661. if (unlikely(buffer->flags & EFX_TX_BUF_HEAP)) {
  662. buffer->dma_addr = dma_map_single(&tx_queue->efx->pci_dev->dev,
  663. header, buffer->len,
  664. DMA_TO_DEVICE);
  665. if (unlikely(dma_mapping_error(&tx_queue->efx->pci_dev->dev,
  666. buffer->dma_addr))) {
  667. kfree(buffer->heap_buf);
  668. buffer->len = 0;
  669. buffer->flags = 0;
  670. return -ENOMEM;
  671. }
  672. buffer->unmap_len = buffer->len;
  673. buffer->flags |= EFX_TX_BUF_MAP_SINGLE;
  674. }
  675. ++tx_queue->insert_count;
  676. return 0;
  677. }
  678. /* Remove buffers put into a tx_queue. None of the buffers must have
  679. * an skb attached.
  680. */
  681. static void efx_enqueue_unwind(struct efx_tx_queue *tx_queue)
  682. {
  683. struct efx_tx_buffer *buffer;
  684. /* Work backwards until we hit the original insert pointer value */
  685. while (tx_queue->insert_count != tx_queue->write_count) {
  686. --tx_queue->insert_count;
  687. buffer = &tx_queue->buffer[tx_queue->insert_count &
  688. tx_queue->ptr_mask];
  689. efx_dequeue_buffer(tx_queue, buffer, NULL, NULL);
  690. }
  691. }
  692. /* Parse the SKB header and initialise state. */
  693. static void tso_start(struct tso_state *st, const struct sk_buff *skb)
  694. {
  695. st->ip_off = skb_network_header(skb) - skb->data;
  696. st->tcp_off = skb_transport_header(skb) - skb->data;
  697. st->header_len = st->tcp_off + (tcp_hdr(skb)->doff << 2u);
  698. if (st->protocol == htons(ETH_P_IP)) {
  699. st->ip_base_len = st->header_len - st->ip_off;
  700. st->ipv4_id = ntohs(ip_hdr(skb)->id);
  701. } else {
  702. st->ip_base_len = st->header_len - st->tcp_off;
  703. st->ipv4_id = 0;
  704. }
  705. st->seqnum = ntohl(tcp_hdr(skb)->seq);
  706. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->urg);
  707. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->syn);
  708. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->rst);
  709. st->out_len = skb->len - st->header_len;
  710. st->unmap_len = 0;
  711. st->dma_flags = 0;
  712. }
  713. static int tso_get_fragment(struct tso_state *st, struct efx_nic *efx,
  714. skb_frag_t *frag)
  715. {
  716. st->unmap_addr = skb_frag_dma_map(&efx->pci_dev->dev, frag, 0,
  717. skb_frag_size(frag), DMA_TO_DEVICE);
  718. if (likely(!dma_mapping_error(&efx->pci_dev->dev, st->unmap_addr))) {
  719. st->dma_flags = 0;
  720. st->unmap_len = skb_frag_size(frag);
  721. st->in_len = skb_frag_size(frag);
  722. st->dma_addr = st->unmap_addr;
  723. return 0;
  724. }
  725. return -ENOMEM;
  726. }
  727. static int tso_get_head_fragment(struct tso_state *st, struct efx_nic *efx,
  728. const struct sk_buff *skb)
  729. {
  730. int hl = st->header_len;
  731. int len = skb_headlen(skb) - hl;
  732. st->unmap_addr = dma_map_single(&efx->pci_dev->dev, skb->data + hl,
  733. len, DMA_TO_DEVICE);
  734. if (likely(!dma_mapping_error(&efx->pci_dev->dev, st->unmap_addr))) {
  735. st->dma_flags = EFX_TX_BUF_MAP_SINGLE;
  736. st->unmap_len = len;
  737. st->in_len = len;
  738. st->dma_addr = st->unmap_addr;
  739. return 0;
  740. }
  741. return -ENOMEM;
  742. }
  743. /**
  744. * tso_fill_packet_with_fragment - form descriptors for the current fragment
  745. * @tx_queue: Efx TX queue
  746. * @skb: Socket buffer
  747. * @st: TSO state
  748. *
  749. * Form descriptors for the current fragment, until we reach the end
  750. * of fragment or end-of-packet.
  751. */
  752. static void tso_fill_packet_with_fragment(struct efx_tx_queue *tx_queue,
  753. const struct sk_buff *skb,
  754. struct tso_state *st)
  755. {
  756. struct efx_tx_buffer *buffer;
  757. int n;
  758. if (st->in_len == 0)
  759. return;
  760. if (st->packet_space == 0)
  761. return;
  762. EFX_BUG_ON_PARANOID(st->in_len <= 0);
  763. EFX_BUG_ON_PARANOID(st->packet_space <= 0);
  764. n = min(st->in_len, st->packet_space);
  765. st->packet_space -= n;
  766. st->out_len -= n;
  767. st->in_len -= n;
  768. efx_tx_queue_insert(tx_queue, st->dma_addr, n, &buffer);
  769. if (st->out_len == 0) {
  770. /* Transfer ownership of the skb */
  771. buffer->skb = skb;
  772. buffer->flags = EFX_TX_BUF_SKB;
  773. } else if (st->packet_space != 0) {
  774. buffer->flags = EFX_TX_BUF_CONT;
  775. }
  776. if (st->in_len == 0) {
  777. /* Transfer ownership of the DMA mapping */
  778. buffer->unmap_len = st->unmap_len;
  779. buffer->flags |= st->dma_flags;
  780. st->unmap_len = 0;
  781. }
  782. st->dma_addr += n;
  783. }
  784. /**
  785. * tso_start_new_packet - generate a new header and prepare for the new packet
  786. * @tx_queue: Efx TX queue
  787. * @skb: Socket buffer
  788. * @st: TSO state
  789. *
  790. * Generate a new header and prepare for the new packet. Return 0 on
  791. * success, or -%ENOMEM if failed to alloc header.
  792. */
  793. static int tso_start_new_packet(struct efx_tx_queue *tx_queue,
  794. const struct sk_buff *skb,
  795. struct tso_state *st)
  796. {
  797. struct efx_tx_buffer *buffer =
  798. &tx_queue->buffer[tx_queue->insert_count & tx_queue->ptr_mask];
  799. struct tcphdr *tsoh_th;
  800. unsigned ip_length;
  801. u8 *header;
  802. int rc;
  803. /* Allocate and insert a DMA-mapped header buffer. */
  804. header = efx_tsoh_get_buffer(tx_queue, buffer, st->header_len);
  805. if (!header)
  806. return -ENOMEM;
  807. tsoh_th = (struct tcphdr *)(header + st->tcp_off);
  808. /* Copy and update the headers. */
  809. memcpy(header, skb->data, st->header_len);
  810. tsoh_th->seq = htonl(st->seqnum);
  811. st->seqnum += skb_shinfo(skb)->gso_size;
  812. if (st->out_len > skb_shinfo(skb)->gso_size) {
  813. /* This packet will not finish the TSO burst. */
  814. st->packet_space = skb_shinfo(skb)->gso_size;
  815. tsoh_th->fin = 0;
  816. tsoh_th->psh = 0;
  817. } else {
  818. /* This packet will be the last in the TSO burst. */
  819. st->packet_space = st->out_len;
  820. tsoh_th->fin = tcp_hdr(skb)->fin;
  821. tsoh_th->psh = tcp_hdr(skb)->psh;
  822. }
  823. ip_length = st->ip_base_len + st->packet_space;
  824. if (st->protocol == htons(ETH_P_IP)) {
  825. struct iphdr *tsoh_iph = (struct iphdr *)(header + st->ip_off);
  826. tsoh_iph->tot_len = htons(ip_length);
  827. /* Linux leaves suitable gaps in the IP ID space for us to fill. */
  828. tsoh_iph->id = htons(st->ipv4_id);
  829. st->ipv4_id++;
  830. } else {
  831. struct ipv6hdr *tsoh_iph =
  832. (struct ipv6hdr *)(header + st->ip_off);
  833. tsoh_iph->payload_len = htons(ip_length);
  834. }
  835. rc = efx_tso_put_header(tx_queue, buffer, header);
  836. if (unlikely(rc))
  837. return rc;
  838. ++tx_queue->tso_packets;
  839. return 0;
  840. }
  841. /**
  842. * efx_enqueue_skb_tso - segment and transmit a TSO socket buffer
  843. * @tx_queue: Efx TX queue
  844. * @skb: Socket buffer
  845. *
  846. * Context: You must hold netif_tx_lock() to call this function.
  847. *
  848. * Add socket buffer @skb to @tx_queue, doing TSO or return != 0 if
  849. * @skb was not enqueued. In all cases @skb is consumed. Return
  850. * %NETDEV_TX_OK.
  851. */
  852. static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
  853. struct sk_buff *skb)
  854. {
  855. struct efx_nic *efx = tx_queue->efx;
  856. int frag_i, rc;
  857. struct tso_state state;
  858. /* Find the packet protocol and sanity-check it */
  859. state.protocol = efx_tso_check_protocol(skb);
  860. EFX_BUG_ON_PARANOID(tx_queue->write_count != tx_queue->insert_count);
  861. tso_start(&state, skb);
  862. /* Assume that skb header area contains exactly the headers, and
  863. * all payload is in the frag list.
  864. */
  865. if (skb_headlen(skb) == state.header_len) {
  866. /* Grab the first payload fragment. */
  867. EFX_BUG_ON_PARANOID(skb_shinfo(skb)->nr_frags < 1);
  868. frag_i = 0;
  869. rc = tso_get_fragment(&state, efx,
  870. skb_shinfo(skb)->frags + frag_i);
  871. if (rc)
  872. goto mem_err;
  873. } else {
  874. rc = tso_get_head_fragment(&state, efx, skb);
  875. if (rc)
  876. goto mem_err;
  877. frag_i = -1;
  878. }
  879. if (tso_start_new_packet(tx_queue, skb, &state) < 0)
  880. goto mem_err;
  881. while (1) {
  882. tso_fill_packet_with_fragment(tx_queue, skb, &state);
  883. /* Move onto the next fragment? */
  884. if (state.in_len == 0) {
  885. if (++frag_i >= skb_shinfo(skb)->nr_frags)
  886. /* End of payload reached. */
  887. break;
  888. rc = tso_get_fragment(&state, efx,
  889. skb_shinfo(skb)->frags + frag_i);
  890. if (rc)
  891. goto mem_err;
  892. }
  893. /* Start at new packet? */
  894. if (state.packet_space == 0 &&
  895. tso_start_new_packet(tx_queue, skb, &state) < 0)
  896. goto mem_err;
  897. }
  898. netdev_tx_sent_queue(tx_queue->core_txq, skb->len);
  899. /* Pass off to hardware */
  900. efx_nic_push_buffers(tx_queue);
  901. efx_tx_maybe_stop_queue(tx_queue);
  902. tx_queue->tso_bursts++;
  903. return NETDEV_TX_OK;
  904. mem_err:
  905. netif_err(efx, tx_err, efx->net_dev,
  906. "Out of memory for TSO headers, or DMA mapping error\n");
  907. dev_kfree_skb_any(skb);
  908. /* Free the DMA mapping we were in the process of writing out */
  909. if (state.unmap_len) {
  910. if (state.dma_flags & EFX_TX_BUF_MAP_SINGLE)
  911. dma_unmap_single(&efx->pci_dev->dev, state.unmap_addr,
  912. state.unmap_len, DMA_TO_DEVICE);
  913. else
  914. dma_unmap_page(&efx->pci_dev->dev, state.unmap_addr,
  915. state.unmap_len, DMA_TO_DEVICE);
  916. }
  917. efx_enqueue_unwind(tx_queue);
  918. return NETDEV_TX_OK;
  919. }