tx.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2005-2009 Solarflare Communications Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation, incorporated herein by reference.
  9. */
  10. #include <linux/pci.h>
  11. #include <linux/tcp.h>
  12. #include <linux/ip.h>
  13. #include <linux/in.h>
  14. #include <linux/ipv6.h>
  15. #include <linux/slab.h>
  16. #include <net/ipv6.h>
  17. #include <linux/if_ether.h>
  18. #include <linux/highmem.h>
  19. #include "net_driver.h"
  20. #include "efx.h"
  21. #include "nic.h"
  22. #include "workarounds.h"
  23. /*
  24. * TX descriptor ring full threshold
  25. *
  26. * The tx_queue descriptor ring fill-level must fall below this value
  27. * before we restart the netif queue
  28. */
  29. #define EFX_TXQ_THRESHOLD (EFX_TXQ_MASK / 2u)
  30. /* We want to be able to nest calls to netif_stop_queue(), since each
  31. * channel can have an individual stop on the queue.
  32. */
  33. void efx_stop_queue(struct efx_nic *efx)
  34. {
  35. spin_lock_bh(&efx->netif_stop_lock);
  36. EFX_TRACE(efx, "stop TX queue\n");
  37. atomic_inc(&efx->netif_stop_count);
  38. netif_stop_queue(efx->net_dev);
  39. spin_unlock_bh(&efx->netif_stop_lock);
  40. }
  41. /* Wake netif's TX queue
  42. * We want to be able to nest calls to netif_stop_queue(), since each
  43. * channel can have an individual stop on the queue.
  44. */
  45. void efx_wake_queue(struct efx_nic *efx)
  46. {
  47. local_bh_disable();
  48. if (atomic_dec_and_lock(&efx->netif_stop_count,
  49. &efx->netif_stop_lock)) {
  50. EFX_TRACE(efx, "waking TX queue\n");
  51. netif_wake_queue(efx->net_dev);
  52. spin_unlock(&efx->netif_stop_lock);
  53. }
  54. local_bh_enable();
  55. }
  56. static void efx_dequeue_buffer(struct efx_tx_queue *tx_queue,
  57. struct efx_tx_buffer *buffer)
  58. {
  59. if (buffer->unmap_len) {
  60. struct pci_dev *pci_dev = tx_queue->efx->pci_dev;
  61. dma_addr_t unmap_addr = (buffer->dma_addr + buffer->len -
  62. buffer->unmap_len);
  63. if (buffer->unmap_single)
  64. pci_unmap_single(pci_dev, unmap_addr, buffer->unmap_len,
  65. PCI_DMA_TODEVICE);
  66. else
  67. pci_unmap_page(pci_dev, unmap_addr, buffer->unmap_len,
  68. PCI_DMA_TODEVICE);
  69. buffer->unmap_len = 0;
  70. buffer->unmap_single = false;
  71. }
  72. if (buffer->skb) {
  73. dev_kfree_skb_any((struct sk_buff *) buffer->skb);
  74. buffer->skb = NULL;
  75. EFX_TRACE(tx_queue->efx, "TX queue %d transmission id %x "
  76. "complete\n", tx_queue->queue, read_ptr);
  77. }
  78. }
  79. /**
  80. * struct efx_tso_header - a DMA mapped buffer for packet headers
  81. * @next: Linked list of free ones.
  82. * The list is protected by the TX queue lock.
  83. * @dma_unmap_len: Length to unmap for an oversize buffer, or 0.
  84. * @dma_addr: The DMA address of the header below.
  85. *
  86. * This controls the memory used for a TSO header. Use TSOH_DATA()
  87. * to find the packet header data. Use TSOH_SIZE() to calculate the
  88. * total size required for a given packet header length. TSO headers
  89. * in the free list are exactly %TSOH_STD_SIZE bytes in size.
  90. */
  91. struct efx_tso_header {
  92. union {
  93. struct efx_tso_header *next;
  94. size_t unmap_len;
  95. };
  96. dma_addr_t dma_addr;
  97. };
  98. static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
  99. struct sk_buff *skb);
  100. static void efx_fini_tso(struct efx_tx_queue *tx_queue);
  101. static void efx_tsoh_heap_free(struct efx_tx_queue *tx_queue,
  102. struct efx_tso_header *tsoh);
  103. static void efx_tsoh_free(struct efx_tx_queue *tx_queue,
  104. struct efx_tx_buffer *buffer)
  105. {
  106. if (buffer->tsoh) {
  107. if (likely(!buffer->tsoh->unmap_len)) {
  108. buffer->tsoh->next = tx_queue->tso_headers_free;
  109. tx_queue->tso_headers_free = buffer->tsoh;
  110. } else {
  111. efx_tsoh_heap_free(tx_queue, buffer->tsoh);
  112. }
  113. buffer->tsoh = NULL;
  114. }
  115. }
  116. static inline unsigned
  117. efx_max_tx_len(struct efx_nic *efx, dma_addr_t dma_addr)
  118. {
  119. /* Depending on the NIC revision, we can use descriptor
  120. * lengths up to 8K or 8K-1. However, since PCI Express
  121. * devices must split read requests at 4K boundaries, there is
  122. * little benefit from using descriptors that cross those
  123. * boundaries and we keep things simple by not doing so.
  124. */
  125. unsigned len = (~dma_addr & 0xfff) + 1;
  126. /* Work around hardware bug for unaligned buffers. */
  127. if (EFX_WORKAROUND_5391(efx) && (dma_addr & 0xf))
  128. len = min_t(unsigned, len, 512 - (dma_addr & 0xf));
  129. return len;
  130. }
  131. /*
  132. * Add a socket buffer to a TX queue
  133. *
  134. * This maps all fragments of a socket buffer for DMA and adds them to
  135. * the TX queue. The queue's insert pointer will be incremented by
  136. * the number of fragments in the socket buffer.
  137. *
  138. * If any DMA mapping fails, any mapped fragments will be unmapped,
  139. * the queue's insert pointer will be restored to its original value.
  140. *
  141. * This function is split out from efx_hard_start_xmit to allow the
  142. * loopback test to direct packets via specific TX queues.
  143. *
  144. * Returns NETDEV_TX_OK or NETDEV_TX_BUSY
  145. * You must hold netif_tx_lock() to call this function.
  146. */
  147. netdev_tx_t efx_enqueue_skb(struct efx_tx_queue *tx_queue, struct sk_buff *skb)
  148. {
  149. struct efx_nic *efx = tx_queue->efx;
  150. struct pci_dev *pci_dev = efx->pci_dev;
  151. struct efx_tx_buffer *buffer;
  152. skb_frag_t *fragment;
  153. struct page *page;
  154. int page_offset;
  155. unsigned int len, unmap_len = 0, fill_level, insert_ptr;
  156. dma_addr_t dma_addr, unmap_addr = 0;
  157. unsigned int dma_len;
  158. bool unmap_single;
  159. int q_space, i = 0;
  160. netdev_tx_t rc = NETDEV_TX_OK;
  161. EFX_BUG_ON_PARANOID(tx_queue->write_count != tx_queue->insert_count);
  162. if (skb_shinfo(skb)->gso_size)
  163. return efx_enqueue_skb_tso(tx_queue, skb);
  164. /* Get size of the initial fragment */
  165. len = skb_headlen(skb);
  166. /* Pad if necessary */
  167. if (EFX_WORKAROUND_15592(efx) && skb->len <= 32) {
  168. EFX_BUG_ON_PARANOID(skb->data_len);
  169. len = 32 + 1;
  170. if (skb_pad(skb, len - skb->len))
  171. return NETDEV_TX_OK;
  172. }
  173. fill_level = tx_queue->insert_count - tx_queue->old_read_count;
  174. q_space = EFX_TXQ_MASK - 1 - fill_level;
  175. /* Map for DMA. Use pci_map_single rather than pci_map_page
  176. * since this is more efficient on machines with sparse
  177. * memory.
  178. */
  179. unmap_single = true;
  180. dma_addr = pci_map_single(pci_dev, skb->data, len, PCI_DMA_TODEVICE);
  181. /* Process all fragments */
  182. while (1) {
  183. if (unlikely(pci_dma_mapping_error(pci_dev, dma_addr)))
  184. goto pci_err;
  185. /* Store fields for marking in the per-fragment final
  186. * descriptor */
  187. unmap_len = len;
  188. unmap_addr = dma_addr;
  189. /* Add to TX queue, splitting across DMA boundaries */
  190. do {
  191. if (unlikely(q_space-- <= 0)) {
  192. /* It might be that completions have
  193. * happened since the xmit path last
  194. * checked. Update the xmit path's
  195. * copy of read_count.
  196. */
  197. ++tx_queue->stopped;
  198. /* This memory barrier protects the
  199. * change of stopped from the access
  200. * of read_count. */
  201. smp_mb();
  202. tx_queue->old_read_count =
  203. *(volatile unsigned *)
  204. &tx_queue->read_count;
  205. fill_level = (tx_queue->insert_count
  206. - tx_queue->old_read_count);
  207. q_space = EFX_TXQ_MASK - 1 - fill_level;
  208. if (unlikely(q_space-- <= 0))
  209. goto stop;
  210. smp_mb();
  211. --tx_queue->stopped;
  212. }
  213. insert_ptr = tx_queue->insert_count & EFX_TXQ_MASK;
  214. buffer = &tx_queue->buffer[insert_ptr];
  215. efx_tsoh_free(tx_queue, buffer);
  216. EFX_BUG_ON_PARANOID(buffer->tsoh);
  217. EFX_BUG_ON_PARANOID(buffer->skb);
  218. EFX_BUG_ON_PARANOID(buffer->len);
  219. EFX_BUG_ON_PARANOID(!buffer->continuation);
  220. EFX_BUG_ON_PARANOID(buffer->unmap_len);
  221. dma_len = efx_max_tx_len(efx, dma_addr);
  222. if (likely(dma_len >= len))
  223. dma_len = len;
  224. /* Fill out per descriptor fields */
  225. buffer->len = dma_len;
  226. buffer->dma_addr = dma_addr;
  227. len -= dma_len;
  228. dma_addr += dma_len;
  229. ++tx_queue->insert_count;
  230. } while (len);
  231. /* Transfer ownership of the unmapping to the final buffer */
  232. buffer->unmap_single = unmap_single;
  233. buffer->unmap_len = unmap_len;
  234. unmap_len = 0;
  235. /* Get address and size of next fragment */
  236. if (i >= skb_shinfo(skb)->nr_frags)
  237. break;
  238. fragment = &skb_shinfo(skb)->frags[i];
  239. len = fragment->size;
  240. page = fragment->page;
  241. page_offset = fragment->page_offset;
  242. i++;
  243. /* Map for DMA */
  244. unmap_single = false;
  245. dma_addr = pci_map_page(pci_dev, page, page_offset, len,
  246. PCI_DMA_TODEVICE);
  247. }
  248. /* Transfer ownership of the skb to the final buffer */
  249. buffer->skb = skb;
  250. buffer->continuation = false;
  251. /* Pass off to hardware */
  252. efx_nic_push_buffers(tx_queue);
  253. return NETDEV_TX_OK;
  254. pci_err:
  255. EFX_ERR_RL(efx, " TX queue %d could not map skb with %d bytes %d "
  256. "fragments for DMA\n", tx_queue->queue, skb->len,
  257. skb_shinfo(skb)->nr_frags + 1);
  258. /* Mark the packet as transmitted, and free the SKB ourselves */
  259. dev_kfree_skb_any(skb);
  260. goto unwind;
  261. stop:
  262. rc = NETDEV_TX_BUSY;
  263. if (tx_queue->stopped == 1)
  264. efx_stop_queue(efx);
  265. unwind:
  266. /* Work backwards until we hit the original insert pointer value */
  267. while (tx_queue->insert_count != tx_queue->write_count) {
  268. --tx_queue->insert_count;
  269. insert_ptr = tx_queue->insert_count & EFX_TXQ_MASK;
  270. buffer = &tx_queue->buffer[insert_ptr];
  271. efx_dequeue_buffer(tx_queue, buffer);
  272. buffer->len = 0;
  273. }
  274. /* Free the fragment we were mid-way through pushing */
  275. if (unmap_len) {
  276. if (unmap_single)
  277. pci_unmap_single(pci_dev, unmap_addr, unmap_len,
  278. PCI_DMA_TODEVICE);
  279. else
  280. pci_unmap_page(pci_dev, unmap_addr, unmap_len,
  281. PCI_DMA_TODEVICE);
  282. }
  283. return rc;
  284. }
  285. /* Remove packets from the TX queue
  286. *
  287. * This removes packets from the TX queue, up to and including the
  288. * specified index.
  289. */
  290. static void efx_dequeue_buffers(struct efx_tx_queue *tx_queue,
  291. unsigned int index)
  292. {
  293. struct efx_nic *efx = tx_queue->efx;
  294. unsigned int stop_index, read_ptr;
  295. stop_index = (index + 1) & EFX_TXQ_MASK;
  296. read_ptr = tx_queue->read_count & EFX_TXQ_MASK;
  297. while (read_ptr != stop_index) {
  298. struct efx_tx_buffer *buffer = &tx_queue->buffer[read_ptr];
  299. if (unlikely(buffer->len == 0)) {
  300. EFX_ERR(tx_queue->efx, "TX queue %d spurious TX "
  301. "completion id %x\n", tx_queue->queue,
  302. read_ptr);
  303. efx_schedule_reset(efx, RESET_TYPE_TX_SKIP);
  304. return;
  305. }
  306. efx_dequeue_buffer(tx_queue, buffer);
  307. buffer->continuation = true;
  308. buffer->len = 0;
  309. ++tx_queue->read_count;
  310. read_ptr = tx_queue->read_count & EFX_TXQ_MASK;
  311. }
  312. }
  313. /* Initiate a packet transmission. We use one channel per CPU
  314. * (sharing when we have more CPUs than channels). On Falcon, the TX
  315. * completion events will be directed back to the CPU that transmitted
  316. * the packet, which should be cache-efficient.
  317. *
  318. * Context: non-blocking.
  319. * Note that returning anything other than NETDEV_TX_OK will cause the
  320. * OS to free the skb.
  321. */
  322. netdev_tx_t efx_hard_start_xmit(struct sk_buff *skb,
  323. struct net_device *net_dev)
  324. {
  325. struct efx_nic *efx = netdev_priv(net_dev);
  326. struct efx_tx_queue *tx_queue;
  327. if (unlikely(efx->port_inhibited))
  328. return NETDEV_TX_BUSY;
  329. if (likely(skb->ip_summed == CHECKSUM_PARTIAL))
  330. tx_queue = &efx->tx_queue[EFX_TX_QUEUE_OFFLOAD_CSUM];
  331. else
  332. tx_queue = &efx->tx_queue[EFX_TX_QUEUE_NO_CSUM];
  333. return efx_enqueue_skb(tx_queue, skb);
  334. }
  335. void efx_xmit_done(struct efx_tx_queue *tx_queue, unsigned int index)
  336. {
  337. unsigned fill_level;
  338. struct efx_nic *efx = tx_queue->efx;
  339. EFX_BUG_ON_PARANOID(index > EFX_TXQ_MASK);
  340. efx_dequeue_buffers(tx_queue, index);
  341. /* See if we need to restart the netif queue. This barrier
  342. * separates the update of read_count from the test of
  343. * stopped. */
  344. smp_mb();
  345. if (unlikely(tx_queue->stopped) && likely(efx->port_enabled)) {
  346. fill_level = tx_queue->insert_count - tx_queue->read_count;
  347. if (fill_level < EFX_TXQ_THRESHOLD) {
  348. EFX_BUG_ON_PARANOID(!efx_dev_registered(efx));
  349. /* Do this under netif_tx_lock(), to avoid racing
  350. * with efx_xmit(). */
  351. netif_tx_lock(efx->net_dev);
  352. if (tx_queue->stopped) {
  353. tx_queue->stopped = 0;
  354. efx_wake_queue(efx);
  355. }
  356. netif_tx_unlock(efx->net_dev);
  357. }
  358. }
  359. }
  360. int efx_probe_tx_queue(struct efx_tx_queue *tx_queue)
  361. {
  362. struct efx_nic *efx = tx_queue->efx;
  363. unsigned int txq_size;
  364. int i, rc;
  365. EFX_LOG(efx, "creating TX queue %d\n", tx_queue->queue);
  366. /* Allocate software ring */
  367. txq_size = EFX_TXQ_SIZE * sizeof(*tx_queue->buffer);
  368. tx_queue->buffer = kzalloc(txq_size, GFP_KERNEL);
  369. if (!tx_queue->buffer)
  370. return -ENOMEM;
  371. for (i = 0; i <= EFX_TXQ_MASK; ++i)
  372. tx_queue->buffer[i].continuation = true;
  373. /* Allocate hardware ring */
  374. rc = efx_nic_probe_tx(tx_queue);
  375. if (rc)
  376. goto fail;
  377. return 0;
  378. fail:
  379. kfree(tx_queue->buffer);
  380. tx_queue->buffer = NULL;
  381. return rc;
  382. }
  383. void efx_init_tx_queue(struct efx_tx_queue *tx_queue)
  384. {
  385. EFX_LOG(tx_queue->efx, "initialising TX queue %d\n", tx_queue->queue);
  386. tx_queue->insert_count = 0;
  387. tx_queue->write_count = 0;
  388. tx_queue->read_count = 0;
  389. tx_queue->old_read_count = 0;
  390. BUG_ON(tx_queue->stopped);
  391. /* Set up TX descriptor ring */
  392. efx_nic_init_tx(tx_queue);
  393. }
  394. void efx_release_tx_buffers(struct efx_tx_queue *tx_queue)
  395. {
  396. struct efx_tx_buffer *buffer;
  397. if (!tx_queue->buffer)
  398. return;
  399. /* Free any buffers left in the ring */
  400. while (tx_queue->read_count != tx_queue->write_count) {
  401. buffer = &tx_queue->buffer[tx_queue->read_count & EFX_TXQ_MASK];
  402. efx_dequeue_buffer(tx_queue, buffer);
  403. buffer->continuation = true;
  404. buffer->len = 0;
  405. ++tx_queue->read_count;
  406. }
  407. }
  408. void efx_fini_tx_queue(struct efx_tx_queue *tx_queue)
  409. {
  410. EFX_LOG(tx_queue->efx, "shutting down TX queue %d\n", tx_queue->queue);
  411. /* Flush TX queue, remove descriptor ring */
  412. efx_nic_fini_tx(tx_queue);
  413. efx_release_tx_buffers(tx_queue);
  414. /* Free up TSO header cache */
  415. efx_fini_tso(tx_queue);
  416. /* Release queue's stop on port, if any */
  417. if (tx_queue->stopped) {
  418. tx_queue->stopped = 0;
  419. efx_wake_queue(tx_queue->efx);
  420. }
  421. }
  422. void efx_remove_tx_queue(struct efx_tx_queue *tx_queue)
  423. {
  424. EFX_LOG(tx_queue->efx, "destroying TX queue %d\n", tx_queue->queue);
  425. efx_nic_remove_tx(tx_queue);
  426. kfree(tx_queue->buffer);
  427. tx_queue->buffer = NULL;
  428. }
  429. /* Efx TCP segmentation acceleration.
  430. *
  431. * Why? Because by doing it here in the driver we can go significantly
  432. * faster than the GSO.
  433. *
  434. * Requires TX checksum offload support.
  435. */
  436. /* Number of bytes inserted at the start of a TSO header buffer,
  437. * similar to NET_IP_ALIGN.
  438. */
  439. #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  440. #define TSOH_OFFSET 0
  441. #else
  442. #define TSOH_OFFSET NET_IP_ALIGN
  443. #endif
  444. #define TSOH_BUFFER(tsoh) ((u8 *)(tsoh + 1) + TSOH_OFFSET)
  445. /* Total size of struct efx_tso_header, buffer and padding */
  446. #define TSOH_SIZE(hdr_len) \
  447. (sizeof(struct efx_tso_header) + TSOH_OFFSET + hdr_len)
  448. /* Size of blocks on free list. Larger blocks must be allocated from
  449. * the heap.
  450. */
  451. #define TSOH_STD_SIZE 128
  452. #define PTR_DIFF(p1, p2) ((u8 *)(p1) - (u8 *)(p2))
  453. #define ETH_HDR_LEN(skb) (skb_network_header(skb) - (skb)->data)
  454. #define SKB_TCP_OFF(skb) PTR_DIFF(tcp_hdr(skb), (skb)->data)
  455. #define SKB_IPV4_OFF(skb) PTR_DIFF(ip_hdr(skb), (skb)->data)
  456. #define SKB_IPV6_OFF(skb) PTR_DIFF(ipv6_hdr(skb), (skb)->data)
  457. /**
  458. * struct tso_state - TSO state for an SKB
  459. * @out_len: Remaining length in current segment
  460. * @seqnum: Current sequence number
  461. * @ipv4_id: Current IPv4 ID, host endian
  462. * @packet_space: Remaining space in current packet
  463. * @dma_addr: DMA address of current position
  464. * @in_len: Remaining length in current SKB fragment
  465. * @unmap_len: Length of SKB fragment
  466. * @unmap_addr: DMA address of SKB fragment
  467. * @unmap_single: DMA single vs page mapping flag
  468. * @protocol: Network protocol (after any VLAN header)
  469. * @header_len: Number of bytes of header
  470. * @full_packet_size: Number of bytes to put in each outgoing segment
  471. *
  472. * The state used during segmentation. It is put into this data structure
  473. * just to make it easy to pass into inline functions.
  474. */
  475. struct tso_state {
  476. /* Output position */
  477. unsigned out_len;
  478. unsigned seqnum;
  479. unsigned ipv4_id;
  480. unsigned packet_space;
  481. /* Input position */
  482. dma_addr_t dma_addr;
  483. unsigned in_len;
  484. unsigned unmap_len;
  485. dma_addr_t unmap_addr;
  486. bool unmap_single;
  487. __be16 protocol;
  488. unsigned header_len;
  489. int full_packet_size;
  490. };
  491. /*
  492. * Verify that our various assumptions about sk_buffs and the conditions
  493. * under which TSO will be attempted hold true. Return the protocol number.
  494. */
  495. static __be16 efx_tso_check_protocol(struct sk_buff *skb)
  496. {
  497. __be16 protocol = skb->protocol;
  498. EFX_BUG_ON_PARANOID(((struct ethhdr *)skb->data)->h_proto !=
  499. protocol);
  500. if (protocol == htons(ETH_P_8021Q)) {
  501. /* Find the encapsulated protocol; reset network header
  502. * and transport header based on that. */
  503. struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
  504. protocol = veh->h_vlan_encapsulated_proto;
  505. skb_set_network_header(skb, sizeof(*veh));
  506. if (protocol == htons(ETH_P_IP))
  507. skb_set_transport_header(skb, sizeof(*veh) +
  508. 4 * ip_hdr(skb)->ihl);
  509. else if (protocol == htons(ETH_P_IPV6))
  510. skb_set_transport_header(skb, sizeof(*veh) +
  511. sizeof(struct ipv6hdr));
  512. }
  513. if (protocol == htons(ETH_P_IP)) {
  514. EFX_BUG_ON_PARANOID(ip_hdr(skb)->protocol != IPPROTO_TCP);
  515. } else {
  516. EFX_BUG_ON_PARANOID(protocol != htons(ETH_P_IPV6));
  517. EFX_BUG_ON_PARANOID(ipv6_hdr(skb)->nexthdr != NEXTHDR_TCP);
  518. }
  519. EFX_BUG_ON_PARANOID((PTR_DIFF(tcp_hdr(skb), skb->data)
  520. + (tcp_hdr(skb)->doff << 2u)) >
  521. skb_headlen(skb));
  522. return protocol;
  523. }
  524. /*
  525. * Allocate a page worth of efx_tso_header structures, and string them
  526. * into the tx_queue->tso_headers_free linked list. Return 0 or -ENOMEM.
  527. */
  528. static int efx_tsoh_block_alloc(struct efx_tx_queue *tx_queue)
  529. {
  530. struct pci_dev *pci_dev = tx_queue->efx->pci_dev;
  531. struct efx_tso_header *tsoh;
  532. dma_addr_t dma_addr;
  533. u8 *base_kva, *kva;
  534. base_kva = pci_alloc_consistent(pci_dev, PAGE_SIZE, &dma_addr);
  535. if (base_kva == NULL) {
  536. EFX_ERR(tx_queue->efx, "Unable to allocate page for TSO"
  537. " headers\n");
  538. return -ENOMEM;
  539. }
  540. /* pci_alloc_consistent() allocates pages. */
  541. EFX_BUG_ON_PARANOID(dma_addr & (PAGE_SIZE - 1u));
  542. for (kva = base_kva; kva < base_kva + PAGE_SIZE; kva += TSOH_STD_SIZE) {
  543. tsoh = (struct efx_tso_header *)kva;
  544. tsoh->dma_addr = dma_addr + (TSOH_BUFFER(tsoh) - base_kva);
  545. tsoh->next = tx_queue->tso_headers_free;
  546. tx_queue->tso_headers_free = tsoh;
  547. }
  548. return 0;
  549. }
  550. /* Free up a TSO header, and all others in the same page. */
  551. static void efx_tsoh_block_free(struct efx_tx_queue *tx_queue,
  552. struct efx_tso_header *tsoh,
  553. struct pci_dev *pci_dev)
  554. {
  555. struct efx_tso_header **p;
  556. unsigned long base_kva;
  557. dma_addr_t base_dma;
  558. base_kva = (unsigned long)tsoh & PAGE_MASK;
  559. base_dma = tsoh->dma_addr & PAGE_MASK;
  560. p = &tx_queue->tso_headers_free;
  561. while (*p != NULL) {
  562. if (((unsigned long)*p & PAGE_MASK) == base_kva)
  563. *p = (*p)->next;
  564. else
  565. p = &(*p)->next;
  566. }
  567. pci_free_consistent(pci_dev, PAGE_SIZE, (void *)base_kva, base_dma);
  568. }
  569. static struct efx_tso_header *
  570. efx_tsoh_heap_alloc(struct efx_tx_queue *tx_queue, size_t header_len)
  571. {
  572. struct efx_tso_header *tsoh;
  573. tsoh = kmalloc(TSOH_SIZE(header_len), GFP_ATOMIC | GFP_DMA);
  574. if (unlikely(!tsoh))
  575. return NULL;
  576. tsoh->dma_addr = pci_map_single(tx_queue->efx->pci_dev,
  577. TSOH_BUFFER(tsoh), header_len,
  578. PCI_DMA_TODEVICE);
  579. if (unlikely(pci_dma_mapping_error(tx_queue->efx->pci_dev,
  580. tsoh->dma_addr))) {
  581. kfree(tsoh);
  582. return NULL;
  583. }
  584. tsoh->unmap_len = header_len;
  585. return tsoh;
  586. }
  587. static void
  588. efx_tsoh_heap_free(struct efx_tx_queue *tx_queue, struct efx_tso_header *tsoh)
  589. {
  590. pci_unmap_single(tx_queue->efx->pci_dev,
  591. tsoh->dma_addr, tsoh->unmap_len,
  592. PCI_DMA_TODEVICE);
  593. kfree(tsoh);
  594. }
  595. /**
  596. * efx_tx_queue_insert - push descriptors onto the TX queue
  597. * @tx_queue: Efx TX queue
  598. * @dma_addr: DMA address of fragment
  599. * @len: Length of fragment
  600. * @final_buffer: The final buffer inserted into the queue
  601. *
  602. * Push descriptors onto the TX queue. Return 0 on success or 1 if
  603. * @tx_queue full.
  604. */
  605. static int efx_tx_queue_insert(struct efx_tx_queue *tx_queue,
  606. dma_addr_t dma_addr, unsigned len,
  607. struct efx_tx_buffer **final_buffer)
  608. {
  609. struct efx_tx_buffer *buffer;
  610. struct efx_nic *efx = tx_queue->efx;
  611. unsigned dma_len, fill_level, insert_ptr;
  612. int q_space;
  613. EFX_BUG_ON_PARANOID(len <= 0);
  614. fill_level = tx_queue->insert_count - tx_queue->old_read_count;
  615. /* -1 as there is no way to represent all descriptors used */
  616. q_space = EFX_TXQ_MASK - 1 - fill_level;
  617. while (1) {
  618. if (unlikely(q_space-- <= 0)) {
  619. /* It might be that completions have happened
  620. * since the xmit path last checked. Update
  621. * the xmit path's copy of read_count.
  622. */
  623. ++tx_queue->stopped;
  624. /* This memory barrier protects the change of
  625. * stopped from the access of read_count. */
  626. smp_mb();
  627. tx_queue->old_read_count =
  628. *(volatile unsigned *)&tx_queue->read_count;
  629. fill_level = (tx_queue->insert_count
  630. - tx_queue->old_read_count);
  631. q_space = EFX_TXQ_MASK - 1 - fill_level;
  632. if (unlikely(q_space-- <= 0)) {
  633. *final_buffer = NULL;
  634. return 1;
  635. }
  636. smp_mb();
  637. --tx_queue->stopped;
  638. }
  639. insert_ptr = tx_queue->insert_count & EFX_TXQ_MASK;
  640. buffer = &tx_queue->buffer[insert_ptr];
  641. ++tx_queue->insert_count;
  642. EFX_BUG_ON_PARANOID(tx_queue->insert_count -
  643. tx_queue->read_count >
  644. EFX_TXQ_MASK);
  645. efx_tsoh_free(tx_queue, buffer);
  646. EFX_BUG_ON_PARANOID(buffer->len);
  647. EFX_BUG_ON_PARANOID(buffer->unmap_len);
  648. EFX_BUG_ON_PARANOID(buffer->skb);
  649. EFX_BUG_ON_PARANOID(!buffer->continuation);
  650. EFX_BUG_ON_PARANOID(buffer->tsoh);
  651. buffer->dma_addr = dma_addr;
  652. dma_len = efx_max_tx_len(efx, dma_addr);
  653. /* If there is enough space to send then do so */
  654. if (dma_len >= len)
  655. break;
  656. buffer->len = dma_len; /* Don't set the other members */
  657. dma_addr += dma_len;
  658. len -= dma_len;
  659. }
  660. EFX_BUG_ON_PARANOID(!len);
  661. buffer->len = len;
  662. *final_buffer = buffer;
  663. return 0;
  664. }
  665. /*
  666. * Put a TSO header into the TX queue.
  667. *
  668. * This is special-cased because we know that it is small enough to fit in
  669. * a single fragment, and we know it doesn't cross a page boundary. It
  670. * also allows us to not worry about end-of-packet etc.
  671. */
  672. static void efx_tso_put_header(struct efx_tx_queue *tx_queue,
  673. struct efx_tso_header *tsoh, unsigned len)
  674. {
  675. struct efx_tx_buffer *buffer;
  676. buffer = &tx_queue->buffer[tx_queue->insert_count & EFX_TXQ_MASK];
  677. efx_tsoh_free(tx_queue, buffer);
  678. EFX_BUG_ON_PARANOID(buffer->len);
  679. EFX_BUG_ON_PARANOID(buffer->unmap_len);
  680. EFX_BUG_ON_PARANOID(buffer->skb);
  681. EFX_BUG_ON_PARANOID(!buffer->continuation);
  682. EFX_BUG_ON_PARANOID(buffer->tsoh);
  683. buffer->len = len;
  684. buffer->dma_addr = tsoh->dma_addr;
  685. buffer->tsoh = tsoh;
  686. ++tx_queue->insert_count;
  687. }
  688. /* Remove descriptors put into a tx_queue. */
  689. static void efx_enqueue_unwind(struct efx_tx_queue *tx_queue)
  690. {
  691. struct efx_tx_buffer *buffer;
  692. dma_addr_t unmap_addr;
  693. /* Work backwards until we hit the original insert pointer value */
  694. while (tx_queue->insert_count != tx_queue->write_count) {
  695. --tx_queue->insert_count;
  696. buffer = &tx_queue->buffer[tx_queue->insert_count &
  697. EFX_TXQ_MASK];
  698. efx_tsoh_free(tx_queue, buffer);
  699. EFX_BUG_ON_PARANOID(buffer->skb);
  700. if (buffer->unmap_len) {
  701. unmap_addr = (buffer->dma_addr + buffer->len -
  702. buffer->unmap_len);
  703. if (buffer->unmap_single)
  704. pci_unmap_single(tx_queue->efx->pci_dev,
  705. unmap_addr, buffer->unmap_len,
  706. PCI_DMA_TODEVICE);
  707. else
  708. pci_unmap_page(tx_queue->efx->pci_dev,
  709. unmap_addr, buffer->unmap_len,
  710. PCI_DMA_TODEVICE);
  711. buffer->unmap_len = 0;
  712. }
  713. buffer->len = 0;
  714. buffer->continuation = true;
  715. }
  716. }
  717. /* Parse the SKB header and initialise state. */
  718. static void tso_start(struct tso_state *st, const struct sk_buff *skb)
  719. {
  720. /* All ethernet/IP/TCP headers combined size is TCP header size
  721. * plus offset of TCP header relative to start of packet.
  722. */
  723. st->header_len = ((tcp_hdr(skb)->doff << 2u)
  724. + PTR_DIFF(tcp_hdr(skb), skb->data));
  725. st->full_packet_size = st->header_len + skb_shinfo(skb)->gso_size;
  726. if (st->protocol == htons(ETH_P_IP))
  727. st->ipv4_id = ntohs(ip_hdr(skb)->id);
  728. else
  729. st->ipv4_id = 0;
  730. st->seqnum = ntohl(tcp_hdr(skb)->seq);
  731. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->urg);
  732. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->syn);
  733. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->rst);
  734. st->packet_space = st->full_packet_size;
  735. st->out_len = skb->len - st->header_len;
  736. st->unmap_len = 0;
  737. st->unmap_single = false;
  738. }
  739. static int tso_get_fragment(struct tso_state *st, struct efx_nic *efx,
  740. skb_frag_t *frag)
  741. {
  742. st->unmap_addr = pci_map_page(efx->pci_dev, frag->page,
  743. frag->page_offset, frag->size,
  744. PCI_DMA_TODEVICE);
  745. if (likely(!pci_dma_mapping_error(efx->pci_dev, st->unmap_addr))) {
  746. st->unmap_single = false;
  747. st->unmap_len = frag->size;
  748. st->in_len = frag->size;
  749. st->dma_addr = st->unmap_addr;
  750. return 0;
  751. }
  752. return -ENOMEM;
  753. }
  754. static int tso_get_head_fragment(struct tso_state *st, struct efx_nic *efx,
  755. const struct sk_buff *skb)
  756. {
  757. int hl = st->header_len;
  758. int len = skb_headlen(skb) - hl;
  759. st->unmap_addr = pci_map_single(efx->pci_dev, skb->data + hl,
  760. len, PCI_DMA_TODEVICE);
  761. if (likely(!pci_dma_mapping_error(efx->pci_dev, st->unmap_addr))) {
  762. st->unmap_single = true;
  763. st->unmap_len = len;
  764. st->in_len = len;
  765. st->dma_addr = st->unmap_addr;
  766. return 0;
  767. }
  768. return -ENOMEM;
  769. }
  770. /**
  771. * tso_fill_packet_with_fragment - form descriptors for the current fragment
  772. * @tx_queue: Efx TX queue
  773. * @skb: Socket buffer
  774. * @st: TSO state
  775. *
  776. * Form descriptors for the current fragment, until we reach the end
  777. * of fragment or end-of-packet. Return 0 on success, 1 if not enough
  778. * space in @tx_queue.
  779. */
  780. static int tso_fill_packet_with_fragment(struct efx_tx_queue *tx_queue,
  781. const struct sk_buff *skb,
  782. struct tso_state *st)
  783. {
  784. struct efx_tx_buffer *buffer;
  785. int n, end_of_packet, rc;
  786. if (st->in_len == 0)
  787. return 0;
  788. if (st->packet_space == 0)
  789. return 0;
  790. EFX_BUG_ON_PARANOID(st->in_len <= 0);
  791. EFX_BUG_ON_PARANOID(st->packet_space <= 0);
  792. n = min(st->in_len, st->packet_space);
  793. st->packet_space -= n;
  794. st->out_len -= n;
  795. st->in_len -= n;
  796. rc = efx_tx_queue_insert(tx_queue, st->dma_addr, n, &buffer);
  797. if (likely(rc == 0)) {
  798. if (st->out_len == 0)
  799. /* Transfer ownership of the skb */
  800. buffer->skb = skb;
  801. end_of_packet = st->out_len == 0 || st->packet_space == 0;
  802. buffer->continuation = !end_of_packet;
  803. if (st->in_len == 0) {
  804. /* Transfer ownership of the pci mapping */
  805. buffer->unmap_len = st->unmap_len;
  806. buffer->unmap_single = st->unmap_single;
  807. st->unmap_len = 0;
  808. }
  809. }
  810. st->dma_addr += n;
  811. return rc;
  812. }
  813. /**
  814. * tso_start_new_packet - generate a new header and prepare for the new packet
  815. * @tx_queue: Efx TX queue
  816. * @skb: Socket buffer
  817. * @st: TSO state
  818. *
  819. * Generate a new header and prepare for the new packet. Return 0 on
  820. * success, or -1 if failed to alloc header.
  821. */
  822. static int tso_start_new_packet(struct efx_tx_queue *tx_queue,
  823. const struct sk_buff *skb,
  824. struct tso_state *st)
  825. {
  826. struct efx_tso_header *tsoh;
  827. struct tcphdr *tsoh_th;
  828. unsigned ip_length;
  829. u8 *header;
  830. /* Allocate a DMA-mapped header buffer. */
  831. if (likely(TSOH_SIZE(st->header_len) <= TSOH_STD_SIZE)) {
  832. if (tx_queue->tso_headers_free == NULL) {
  833. if (efx_tsoh_block_alloc(tx_queue))
  834. return -1;
  835. }
  836. EFX_BUG_ON_PARANOID(!tx_queue->tso_headers_free);
  837. tsoh = tx_queue->tso_headers_free;
  838. tx_queue->tso_headers_free = tsoh->next;
  839. tsoh->unmap_len = 0;
  840. } else {
  841. tx_queue->tso_long_headers++;
  842. tsoh = efx_tsoh_heap_alloc(tx_queue, st->header_len);
  843. if (unlikely(!tsoh))
  844. return -1;
  845. }
  846. header = TSOH_BUFFER(tsoh);
  847. tsoh_th = (struct tcphdr *)(header + SKB_TCP_OFF(skb));
  848. /* Copy and update the headers. */
  849. memcpy(header, skb->data, st->header_len);
  850. tsoh_th->seq = htonl(st->seqnum);
  851. st->seqnum += skb_shinfo(skb)->gso_size;
  852. if (st->out_len > skb_shinfo(skb)->gso_size) {
  853. /* This packet will not finish the TSO burst. */
  854. ip_length = st->full_packet_size - ETH_HDR_LEN(skb);
  855. tsoh_th->fin = 0;
  856. tsoh_th->psh = 0;
  857. } else {
  858. /* This packet will be the last in the TSO burst. */
  859. ip_length = st->header_len - ETH_HDR_LEN(skb) + st->out_len;
  860. tsoh_th->fin = tcp_hdr(skb)->fin;
  861. tsoh_th->psh = tcp_hdr(skb)->psh;
  862. }
  863. if (st->protocol == htons(ETH_P_IP)) {
  864. struct iphdr *tsoh_iph =
  865. (struct iphdr *)(header + SKB_IPV4_OFF(skb));
  866. tsoh_iph->tot_len = htons(ip_length);
  867. /* Linux leaves suitable gaps in the IP ID space for us to fill. */
  868. tsoh_iph->id = htons(st->ipv4_id);
  869. st->ipv4_id++;
  870. } else {
  871. struct ipv6hdr *tsoh_iph =
  872. (struct ipv6hdr *)(header + SKB_IPV6_OFF(skb));
  873. tsoh_iph->payload_len = htons(ip_length - sizeof(*tsoh_iph));
  874. }
  875. st->packet_space = skb_shinfo(skb)->gso_size;
  876. ++tx_queue->tso_packets;
  877. /* Form a descriptor for this header. */
  878. efx_tso_put_header(tx_queue, tsoh, st->header_len);
  879. return 0;
  880. }
  881. /**
  882. * efx_enqueue_skb_tso - segment and transmit a TSO socket buffer
  883. * @tx_queue: Efx TX queue
  884. * @skb: Socket buffer
  885. *
  886. * Context: You must hold netif_tx_lock() to call this function.
  887. *
  888. * Add socket buffer @skb to @tx_queue, doing TSO or return != 0 if
  889. * @skb was not enqueued. In all cases @skb is consumed. Return
  890. * %NETDEV_TX_OK or %NETDEV_TX_BUSY.
  891. */
  892. static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
  893. struct sk_buff *skb)
  894. {
  895. struct efx_nic *efx = tx_queue->efx;
  896. int frag_i, rc, rc2 = NETDEV_TX_OK;
  897. struct tso_state state;
  898. /* Find the packet protocol and sanity-check it */
  899. state.protocol = efx_tso_check_protocol(skb);
  900. EFX_BUG_ON_PARANOID(tx_queue->write_count != tx_queue->insert_count);
  901. tso_start(&state, skb);
  902. /* Assume that skb header area contains exactly the headers, and
  903. * all payload is in the frag list.
  904. */
  905. if (skb_headlen(skb) == state.header_len) {
  906. /* Grab the first payload fragment. */
  907. EFX_BUG_ON_PARANOID(skb_shinfo(skb)->nr_frags < 1);
  908. frag_i = 0;
  909. rc = tso_get_fragment(&state, efx,
  910. skb_shinfo(skb)->frags + frag_i);
  911. if (rc)
  912. goto mem_err;
  913. } else {
  914. rc = tso_get_head_fragment(&state, efx, skb);
  915. if (rc)
  916. goto mem_err;
  917. frag_i = -1;
  918. }
  919. if (tso_start_new_packet(tx_queue, skb, &state) < 0)
  920. goto mem_err;
  921. while (1) {
  922. rc = tso_fill_packet_with_fragment(tx_queue, skb, &state);
  923. if (unlikely(rc))
  924. goto stop;
  925. /* Move onto the next fragment? */
  926. if (state.in_len == 0) {
  927. if (++frag_i >= skb_shinfo(skb)->nr_frags)
  928. /* End of payload reached. */
  929. break;
  930. rc = tso_get_fragment(&state, efx,
  931. skb_shinfo(skb)->frags + frag_i);
  932. if (rc)
  933. goto mem_err;
  934. }
  935. /* Start at new packet? */
  936. if (state.packet_space == 0 &&
  937. tso_start_new_packet(tx_queue, skb, &state) < 0)
  938. goto mem_err;
  939. }
  940. /* Pass off to hardware */
  941. efx_nic_push_buffers(tx_queue);
  942. tx_queue->tso_bursts++;
  943. return NETDEV_TX_OK;
  944. mem_err:
  945. EFX_ERR(efx, "Out of memory for TSO headers, or PCI mapping error\n");
  946. dev_kfree_skb_any(skb);
  947. goto unwind;
  948. stop:
  949. rc2 = NETDEV_TX_BUSY;
  950. /* Stop the queue if it wasn't stopped before. */
  951. if (tx_queue->stopped == 1)
  952. efx_stop_queue(efx);
  953. unwind:
  954. /* Free the DMA mapping we were in the process of writing out */
  955. if (state.unmap_len) {
  956. if (state.unmap_single)
  957. pci_unmap_single(efx->pci_dev, state.unmap_addr,
  958. state.unmap_len, PCI_DMA_TODEVICE);
  959. else
  960. pci_unmap_page(efx->pci_dev, state.unmap_addr,
  961. state.unmap_len, PCI_DMA_TODEVICE);
  962. }
  963. efx_enqueue_unwind(tx_queue);
  964. return rc2;
  965. }
  966. /*
  967. * Free up all TSO datastructures associated with tx_queue. This
  968. * routine should be called only once the tx_queue is both empty and
  969. * will no longer be used.
  970. */
  971. static void efx_fini_tso(struct efx_tx_queue *tx_queue)
  972. {
  973. unsigned i;
  974. if (tx_queue->buffer) {
  975. for (i = 0; i <= EFX_TXQ_MASK; ++i)
  976. efx_tsoh_free(tx_queue, &tx_queue->buffer[i]);
  977. }
  978. while (tx_queue->tso_headers_free != NULL)
  979. efx_tsoh_block_free(tx_queue, tx_queue->tso_headers_free,
  980. tx_queue->efx->pci_dev);
  981. }