tx.c 31 KB

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