tx.c 30 KB

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