tx.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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(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. pci_unmap_page(pci_dev, unmap_addr, unmap_len,
  256. PCI_DMA_TODEVICE);
  257. return rc;
  258. }
  259. /* Remove packets from the TX queue
  260. *
  261. * This removes packets from the TX queue, up to and including the
  262. * specified index.
  263. */
  264. static inline void efx_dequeue_buffers(struct efx_tx_queue *tx_queue,
  265. unsigned int index)
  266. {
  267. struct efx_nic *efx = tx_queue->efx;
  268. unsigned int stop_index, read_ptr;
  269. unsigned int mask = tx_queue->efx->type->txd_ring_mask;
  270. stop_index = (index + 1) & mask;
  271. read_ptr = tx_queue->read_count & mask;
  272. while (read_ptr != stop_index) {
  273. struct efx_tx_buffer *buffer = &tx_queue->buffer[read_ptr];
  274. if (unlikely(buffer->len == 0)) {
  275. EFX_ERR(tx_queue->efx, "TX queue %d spurious TX "
  276. "completion id %x\n", tx_queue->queue,
  277. read_ptr);
  278. efx_schedule_reset(efx, RESET_TYPE_TX_SKIP);
  279. return;
  280. }
  281. efx_dequeue_buffer(tx_queue, buffer);
  282. buffer->continuation = 1;
  283. buffer->len = 0;
  284. ++tx_queue->read_count;
  285. read_ptr = tx_queue->read_count & mask;
  286. }
  287. }
  288. /* Initiate a packet transmission on the specified TX queue.
  289. * Note that returning anything other than NETDEV_TX_OK will cause the
  290. * OS to free the skb.
  291. *
  292. * This function is split out from efx_hard_start_xmit to allow the
  293. * loopback test to direct packets via specific TX queues. It is
  294. * therefore a non-static inline, so as not to penalise performance
  295. * for non-loopback transmissions.
  296. *
  297. * Context: netif_tx_lock held
  298. */
  299. inline int efx_xmit(struct efx_nic *efx,
  300. struct efx_tx_queue *tx_queue, struct sk_buff *skb)
  301. {
  302. int rc;
  303. /* Map fragments for DMA and add to TX queue */
  304. rc = efx_enqueue_skb(tx_queue, skb);
  305. if (unlikely(rc != NETDEV_TX_OK))
  306. goto out;
  307. /* Update last TX timer */
  308. efx->net_dev->trans_start = jiffies;
  309. out:
  310. return rc;
  311. }
  312. /* Initiate a packet transmission. We use one channel per CPU
  313. * (sharing when we have more CPUs than channels). On Falcon, the TX
  314. * completion events will be directed back to the CPU that transmitted
  315. * the packet, which should be cache-efficient.
  316. *
  317. * Context: non-blocking.
  318. * Note that returning anything other than NETDEV_TX_OK will cause the
  319. * OS to free the skb.
  320. */
  321. int efx_hard_start_xmit(struct sk_buff *skb, struct net_device *net_dev)
  322. {
  323. struct efx_nic *efx = net_dev->priv;
  324. return efx_xmit(efx, &efx->tx_queue[0], skb);
  325. }
  326. void efx_xmit_done(struct efx_tx_queue *tx_queue, unsigned int index)
  327. {
  328. unsigned fill_level;
  329. struct efx_nic *efx = tx_queue->efx;
  330. EFX_BUG_ON_PARANOID(index > efx->type->txd_ring_mask);
  331. efx_dequeue_buffers(tx_queue, index);
  332. /* See if we need to restart the netif queue. This barrier
  333. * separates the update of read_count from the test of
  334. * stopped. */
  335. smp_mb();
  336. if (unlikely(tx_queue->stopped)) {
  337. fill_level = tx_queue->insert_count - tx_queue->read_count;
  338. if (fill_level < EFX_NETDEV_TX_THRESHOLD(tx_queue)) {
  339. EFX_BUG_ON_PARANOID(!NET_DEV_REGISTERED(efx));
  340. /* Do this under netif_tx_lock(), to avoid racing
  341. * with efx_xmit(). */
  342. netif_tx_lock(efx->net_dev);
  343. if (tx_queue->stopped) {
  344. tx_queue->stopped = 0;
  345. efx_wake_queue(efx);
  346. }
  347. netif_tx_unlock(efx->net_dev);
  348. }
  349. }
  350. }
  351. int efx_probe_tx_queue(struct efx_tx_queue *tx_queue)
  352. {
  353. struct efx_nic *efx = tx_queue->efx;
  354. unsigned int txq_size;
  355. int i, rc;
  356. EFX_LOG(efx, "creating TX queue %d\n", tx_queue->queue);
  357. /* Allocate software ring */
  358. txq_size = (efx->type->txd_ring_mask + 1) * sizeof(*tx_queue->buffer);
  359. tx_queue->buffer = kzalloc(txq_size, GFP_KERNEL);
  360. if (!tx_queue->buffer) {
  361. rc = -ENOMEM;
  362. goto fail1;
  363. }
  364. for (i = 0; i <= efx->type->txd_ring_mask; ++i)
  365. tx_queue->buffer[i].continuation = 1;
  366. /* Allocate hardware ring */
  367. rc = falcon_probe_tx(tx_queue);
  368. if (rc)
  369. goto fail2;
  370. return 0;
  371. fail2:
  372. kfree(tx_queue->buffer);
  373. tx_queue->buffer = NULL;
  374. fail1:
  375. tx_queue->used = 0;
  376. return rc;
  377. }
  378. int efx_init_tx_queue(struct efx_tx_queue *tx_queue)
  379. {
  380. EFX_LOG(tx_queue->efx, "initialising TX queue %d\n", tx_queue->queue);
  381. tx_queue->insert_count = 0;
  382. tx_queue->write_count = 0;
  383. tx_queue->read_count = 0;
  384. tx_queue->old_read_count = 0;
  385. BUG_ON(tx_queue->stopped);
  386. /* Set up TX descriptor ring */
  387. return falcon_init_tx(tx_queue);
  388. }
  389. void efx_release_tx_buffers(struct efx_tx_queue *tx_queue)
  390. {
  391. struct efx_tx_buffer *buffer;
  392. if (!tx_queue->buffer)
  393. return;
  394. /* Free any buffers left in the ring */
  395. while (tx_queue->read_count != tx_queue->write_count) {
  396. buffer = &tx_queue->buffer[tx_queue->read_count &
  397. tx_queue->efx->type->txd_ring_mask];
  398. efx_dequeue_buffer(tx_queue, buffer);
  399. buffer->continuation = 1;
  400. buffer->len = 0;
  401. ++tx_queue->read_count;
  402. }
  403. }
  404. void efx_fini_tx_queue(struct efx_tx_queue *tx_queue)
  405. {
  406. EFX_LOG(tx_queue->efx, "shutting down TX queue %d\n", tx_queue->queue);
  407. /* Flush TX queue, remove descriptor ring */
  408. falcon_fini_tx(tx_queue);
  409. efx_release_tx_buffers(tx_queue);
  410. /* Free up TSO header cache */
  411. efx_fini_tso(tx_queue);
  412. /* Release queue's stop on port, if any */
  413. if (tx_queue->stopped) {
  414. tx_queue->stopped = 0;
  415. efx_wake_queue(tx_queue->efx);
  416. }
  417. }
  418. void efx_remove_tx_queue(struct efx_tx_queue *tx_queue)
  419. {
  420. EFX_LOG(tx_queue->efx, "destroying TX queue %d\n", tx_queue->queue);
  421. falcon_remove_tx(tx_queue);
  422. kfree(tx_queue->buffer);
  423. tx_queue->buffer = NULL;
  424. tx_queue->used = 0;
  425. }
  426. /* Efx TCP segmentation acceleration.
  427. *
  428. * Why? Because by doing it here in the driver we can go significantly
  429. * faster than the GSO.
  430. *
  431. * Requires TX checksum offload support.
  432. */
  433. /* Number of bytes inserted at the start of a TSO header buffer,
  434. * similar to NET_IP_ALIGN.
  435. */
  436. #if defined(__i386__) || defined(__x86_64__)
  437. #define TSOH_OFFSET 0
  438. #else
  439. #define TSOH_OFFSET NET_IP_ALIGN
  440. #endif
  441. #define TSOH_BUFFER(tsoh) ((u8 *)(tsoh + 1) + TSOH_OFFSET)
  442. /* Total size of struct efx_tso_header, buffer and padding */
  443. #define TSOH_SIZE(hdr_len) \
  444. (sizeof(struct efx_tso_header) + TSOH_OFFSET + hdr_len)
  445. /* Size of blocks on free list. Larger blocks must be allocated from
  446. * the heap.
  447. */
  448. #define TSOH_STD_SIZE 128
  449. #define PTR_DIFF(p1, p2) ((u8 *)(p1) - (u8 *)(p2))
  450. #define ETH_HDR_LEN(skb) (skb_network_header(skb) - (skb)->data)
  451. #define SKB_TCP_OFF(skb) PTR_DIFF(tcp_hdr(skb), (skb)->data)
  452. #define SKB_IPV4_OFF(skb) PTR_DIFF(ip_hdr(skb), (skb)->data)
  453. /**
  454. * struct tso_state - TSO state for an SKB
  455. * @remaining_len: Bytes of data we've yet to segment
  456. * @seqnum: Current sequence number
  457. * @packet_space: Remaining space in current packet
  458. * @ifc: Input fragment cursor.
  459. * Where we are in the current fragment of the incoming SKB. These
  460. * values get updated in place when we split a fragment over
  461. * multiple packets.
  462. * @p: Parameters.
  463. * These values are set once at the start of the TSO send and do
  464. * not get changed as the routine progresses.
  465. *
  466. * The state used during segmentation. It is put into this data structure
  467. * just to make it easy to pass into inline functions.
  468. */
  469. struct tso_state {
  470. unsigned remaining_len;
  471. unsigned seqnum;
  472. unsigned packet_space;
  473. struct {
  474. /* DMA address of current position */
  475. dma_addr_t dma_addr;
  476. /* Remaining length */
  477. unsigned int len;
  478. /* DMA address and length of the whole fragment */
  479. unsigned int unmap_len;
  480. dma_addr_t unmap_addr;
  481. struct page *page;
  482. unsigned page_off;
  483. } ifc;
  484. struct {
  485. /* The number of bytes of header */
  486. unsigned int header_length;
  487. /* The number of bytes to put in each outgoing segment. */
  488. int full_packet_size;
  489. /* Current IPv4 ID, host endian. */
  490. unsigned ipv4_id;
  491. } p;
  492. };
  493. /*
  494. * Verify that our various assumptions about sk_buffs and the conditions
  495. * under which TSO will be attempted hold true.
  496. */
  497. static inline void efx_tso_check_safe(const struct sk_buff *skb)
  498. {
  499. EFX_BUG_ON_PARANOID(skb->protocol != htons(ETH_P_IP));
  500. EFX_BUG_ON_PARANOID(((struct ethhdr *)skb->data)->h_proto !=
  501. skb->protocol);
  502. EFX_BUG_ON_PARANOID(ip_hdr(skb)->protocol != IPPROTO_TCP);
  503. EFX_BUG_ON_PARANOID((PTR_DIFF(tcp_hdr(skb), skb->data)
  504. + (tcp_hdr(skb)->doff << 2u)) >
  505. skb_headlen(skb));
  506. }
  507. /*
  508. * Allocate a page worth of efx_tso_header structures, and string them
  509. * into the tx_queue->tso_headers_free linked list. Return 0 or -ENOMEM.
  510. */
  511. static int efx_tsoh_block_alloc(struct efx_tx_queue *tx_queue)
  512. {
  513. struct pci_dev *pci_dev = tx_queue->efx->pci_dev;
  514. struct efx_tso_header *tsoh;
  515. dma_addr_t dma_addr;
  516. u8 *base_kva, *kva;
  517. base_kva = pci_alloc_consistent(pci_dev, PAGE_SIZE, &dma_addr);
  518. if (base_kva == NULL) {
  519. EFX_ERR(tx_queue->efx, "Unable to allocate page for TSO"
  520. " headers\n");
  521. return -ENOMEM;
  522. }
  523. /* pci_alloc_consistent() allocates pages. */
  524. EFX_BUG_ON_PARANOID(dma_addr & (PAGE_SIZE - 1u));
  525. for (kva = base_kva; kva < base_kva + PAGE_SIZE; kva += TSOH_STD_SIZE) {
  526. tsoh = (struct efx_tso_header *)kva;
  527. tsoh->dma_addr = dma_addr + (TSOH_BUFFER(tsoh) - base_kva);
  528. tsoh->next = tx_queue->tso_headers_free;
  529. tx_queue->tso_headers_free = tsoh;
  530. }
  531. return 0;
  532. }
  533. /* Free up a TSO header, and all others in the same page. */
  534. static void efx_tsoh_block_free(struct efx_tx_queue *tx_queue,
  535. struct efx_tso_header *tsoh,
  536. struct pci_dev *pci_dev)
  537. {
  538. struct efx_tso_header **p;
  539. unsigned long base_kva;
  540. dma_addr_t base_dma;
  541. base_kva = (unsigned long)tsoh & PAGE_MASK;
  542. base_dma = tsoh->dma_addr & PAGE_MASK;
  543. p = &tx_queue->tso_headers_free;
  544. while (*p != NULL)
  545. if (((unsigned long)*p & PAGE_MASK) == base_kva)
  546. *p = (*p)->next;
  547. else
  548. p = &(*p)->next;
  549. pci_free_consistent(pci_dev, PAGE_SIZE, (void *)base_kva, base_dma);
  550. }
  551. static struct efx_tso_header *
  552. efx_tsoh_heap_alloc(struct efx_tx_queue *tx_queue, size_t header_len)
  553. {
  554. struct efx_tso_header *tsoh;
  555. tsoh = kmalloc(TSOH_SIZE(header_len), GFP_ATOMIC | GFP_DMA);
  556. if (unlikely(!tsoh))
  557. return NULL;
  558. tsoh->dma_addr = pci_map_single(tx_queue->efx->pci_dev,
  559. TSOH_BUFFER(tsoh), header_len,
  560. PCI_DMA_TODEVICE);
  561. if (unlikely(pci_dma_mapping_error(tsoh->dma_addr))) {
  562. kfree(tsoh);
  563. return NULL;
  564. }
  565. tsoh->unmap_len = header_len;
  566. return tsoh;
  567. }
  568. static void
  569. efx_tsoh_heap_free(struct efx_tx_queue *tx_queue, struct efx_tso_header *tsoh)
  570. {
  571. pci_unmap_single(tx_queue->efx->pci_dev,
  572. tsoh->dma_addr, tsoh->unmap_len,
  573. PCI_DMA_TODEVICE);
  574. kfree(tsoh);
  575. }
  576. /**
  577. * efx_tx_queue_insert - push descriptors onto the TX queue
  578. * @tx_queue: Efx TX queue
  579. * @dma_addr: DMA address of fragment
  580. * @len: Length of fragment
  581. * @skb: Only non-null for end of last segment
  582. * @end_of_packet: True if last fragment in a packet
  583. * @unmap_addr: DMA address of fragment for unmapping
  584. * @unmap_len: Only set this in last segment of a fragment
  585. *
  586. * Push descriptors onto the TX queue. Return 0 on success or 1 if
  587. * @tx_queue full.
  588. */
  589. static int efx_tx_queue_insert(struct efx_tx_queue *tx_queue,
  590. dma_addr_t dma_addr, unsigned len,
  591. const struct sk_buff *skb, int end_of_packet,
  592. dma_addr_t unmap_addr, unsigned unmap_len)
  593. {
  594. struct efx_tx_buffer *buffer;
  595. struct efx_nic *efx = tx_queue->efx;
  596. unsigned dma_len, fill_level, insert_ptr, misalign;
  597. int q_space;
  598. EFX_BUG_ON_PARANOID(len <= 0);
  599. fill_level = tx_queue->insert_count - tx_queue->old_read_count;
  600. /* -1 as there is no way to represent all descriptors used */
  601. q_space = efx->type->txd_ring_mask - 1 - fill_level;
  602. while (1) {
  603. if (unlikely(q_space-- <= 0)) {
  604. /* It might be that completions have happened
  605. * since the xmit path last checked. Update
  606. * the xmit path's copy of read_count.
  607. */
  608. ++tx_queue->stopped;
  609. /* This memory barrier protects the change of
  610. * stopped from the access of read_count. */
  611. smp_mb();
  612. tx_queue->old_read_count =
  613. *(volatile unsigned *)&tx_queue->read_count;
  614. fill_level = (tx_queue->insert_count
  615. - tx_queue->old_read_count);
  616. q_space = efx->type->txd_ring_mask - 1 - fill_level;
  617. if (unlikely(q_space-- <= 0))
  618. return 1;
  619. smp_mb();
  620. --tx_queue->stopped;
  621. }
  622. insert_ptr = tx_queue->insert_count & efx->type->txd_ring_mask;
  623. buffer = &tx_queue->buffer[insert_ptr];
  624. ++tx_queue->insert_count;
  625. EFX_BUG_ON_PARANOID(tx_queue->insert_count -
  626. tx_queue->read_count >
  627. efx->type->txd_ring_mask);
  628. efx_tsoh_free(tx_queue, buffer);
  629. EFX_BUG_ON_PARANOID(buffer->len);
  630. EFX_BUG_ON_PARANOID(buffer->unmap_len);
  631. EFX_BUG_ON_PARANOID(buffer->skb);
  632. EFX_BUG_ON_PARANOID(buffer->continuation != 1);
  633. EFX_BUG_ON_PARANOID(buffer->tsoh);
  634. buffer->dma_addr = dma_addr;
  635. /* Ensure we do not cross a boundary unsupported by H/W */
  636. dma_len = (~dma_addr & efx->type->tx_dma_mask) + 1;
  637. misalign = (unsigned)dma_addr & efx->type->bug5391_mask;
  638. if (misalign && dma_len + misalign > 512)
  639. dma_len = 512 - misalign;
  640. /* If there is enough space to send then do so */
  641. if (dma_len >= len)
  642. break;
  643. buffer->len = dma_len; /* Don't set the other members */
  644. dma_addr += dma_len;
  645. len -= dma_len;
  646. }
  647. EFX_BUG_ON_PARANOID(!len);
  648. buffer->len = len;
  649. buffer->skb = skb;
  650. buffer->continuation = !end_of_packet;
  651. buffer->unmap_addr = unmap_addr;
  652. buffer->unmap_len = unmap_len;
  653. return 0;
  654. }
  655. /*
  656. * Put a TSO header into the TX queue.
  657. *
  658. * This is special-cased because we know that it is small enough to fit in
  659. * a single fragment, and we know it doesn't cross a page boundary. It
  660. * also allows us to not worry about end-of-packet etc.
  661. */
  662. static inline void efx_tso_put_header(struct efx_tx_queue *tx_queue,
  663. struct efx_tso_header *tsoh, unsigned len)
  664. {
  665. struct efx_tx_buffer *buffer;
  666. buffer = &tx_queue->buffer[tx_queue->insert_count &
  667. tx_queue->efx->type->txd_ring_mask];
  668. efx_tsoh_free(tx_queue, buffer);
  669. EFX_BUG_ON_PARANOID(buffer->len);
  670. EFX_BUG_ON_PARANOID(buffer->unmap_len);
  671. EFX_BUG_ON_PARANOID(buffer->skb);
  672. EFX_BUG_ON_PARANOID(buffer->continuation != 1);
  673. EFX_BUG_ON_PARANOID(buffer->tsoh);
  674. buffer->len = len;
  675. buffer->dma_addr = tsoh->dma_addr;
  676. buffer->tsoh = tsoh;
  677. ++tx_queue->insert_count;
  678. }
  679. /* Remove descriptors put into a tx_queue. */
  680. static void efx_enqueue_unwind(struct efx_tx_queue *tx_queue)
  681. {
  682. struct efx_tx_buffer *buffer;
  683. /* Work backwards until we hit the original insert pointer value */
  684. while (tx_queue->insert_count != tx_queue->write_count) {
  685. --tx_queue->insert_count;
  686. buffer = &tx_queue->buffer[tx_queue->insert_count &
  687. tx_queue->efx->type->txd_ring_mask];
  688. efx_tsoh_free(tx_queue, buffer);
  689. EFX_BUG_ON_PARANOID(buffer->skb);
  690. buffer->len = 0;
  691. buffer->continuation = 1;
  692. if (buffer->unmap_len) {
  693. pci_unmap_page(tx_queue->efx->pci_dev,
  694. buffer->unmap_addr,
  695. buffer->unmap_len, PCI_DMA_TODEVICE);
  696. buffer->unmap_len = 0;
  697. }
  698. }
  699. }
  700. /* Parse the SKB header and initialise state. */
  701. static inline void tso_start(struct tso_state *st, const struct sk_buff *skb)
  702. {
  703. /* All ethernet/IP/TCP headers combined size is TCP header size
  704. * plus offset of TCP header relative to start of packet.
  705. */
  706. st->p.header_length = ((tcp_hdr(skb)->doff << 2u)
  707. + PTR_DIFF(tcp_hdr(skb), skb->data));
  708. st->p.full_packet_size = (st->p.header_length
  709. + skb_shinfo(skb)->gso_size);
  710. st->p.ipv4_id = ntohs(ip_hdr(skb)->id);
  711. st->seqnum = ntohl(tcp_hdr(skb)->seq);
  712. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->urg);
  713. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->syn);
  714. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->rst);
  715. st->packet_space = st->p.full_packet_size;
  716. st->remaining_len = skb->len - st->p.header_length;
  717. }
  718. /**
  719. * tso_get_fragment - record fragment details and map for DMA
  720. * @st: TSO state
  721. * @efx: Efx NIC
  722. * @data: Pointer to fragment data
  723. * @len: Length of fragment
  724. *
  725. * Record fragment details and map for DMA. Return 0 on success, or
  726. * -%ENOMEM if DMA mapping fails.
  727. */
  728. static inline int tso_get_fragment(struct tso_state *st, struct efx_nic *efx,
  729. int len, struct page *page, int page_off)
  730. {
  731. st->ifc.unmap_addr = pci_map_page(efx->pci_dev, page, page_off,
  732. len, PCI_DMA_TODEVICE);
  733. if (likely(!pci_dma_mapping_error(st->ifc.unmap_addr))) {
  734. st->ifc.unmap_len = len;
  735. st->ifc.len = len;
  736. st->ifc.dma_addr = st->ifc.unmap_addr;
  737. st->ifc.page = page;
  738. st->ifc.page_off = page_off;
  739. return 0;
  740. }
  741. return -ENOMEM;
  742. }
  743. /**
  744. * tso_fill_packet_with_fragment - form descriptors for the current fragment
  745. * @tx_queue: Efx TX queue
  746. * @skb: Socket buffer
  747. * @st: TSO state
  748. *
  749. * Form descriptors for the current fragment, until we reach the end
  750. * of fragment or end-of-packet. Return 0 on success, 1 if not enough
  751. * space in @tx_queue.
  752. */
  753. static inline int tso_fill_packet_with_fragment(struct efx_tx_queue *tx_queue,
  754. const struct sk_buff *skb,
  755. struct tso_state *st)
  756. {
  757. int n, end_of_packet, rc;
  758. if (st->ifc.len == 0)
  759. return 0;
  760. if (st->packet_space == 0)
  761. return 0;
  762. EFX_BUG_ON_PARANOID(st->ifc.len <= 0);
  763. EFX_BUG_ON_PARANOID(st->packet_space <= 0);
  764. n = min(st->ifc.len, st->packet_space);
  765. st->packet_space -= n;
  766. st->remaining_len -= n;
  767. st->ifc.len -= n;
  768. st->ifc.page_off += n;
  769. end_of_packet = st->remaining_len == 0 || st->packet_space == 0;
  770. rc = efx_tx_queue_insert(tx_queue, st->ifc.dma_addr, n,
  771. st->remaining_len ? NULL : skb,
  772. end_of_packet, st->ifc.unmap_addr,
  773. st->ifc.len ? 0 : st->ifc.unmap_len);
  774. st->ifc.dma_addr += n;
  775. return rc;
  776. }
  777. /**
  778. * tso_start_new_packet - generate a new header and prepare for the new packet
  779. * @tx_queue: Efx TX queue
  780. * @skb: Socket buffer
  781. * @st: TSO state
  782. *
  783. * Generate a new header and prepare for the new packet. Return 0 on
  784. * success, or -1 if failed to alloc header.
  785. */
  786. static inline int tso_start_new_packet(struct efx_tx_queue *tx_queue,
  787. const struct sk_buff *skb,
  788. struct tso_state *st)
  789. {
  790. struct efx_tso_header *tsoh;
  791. struct iphdr *tsoh_iph;
  792. struct tcphdr *tsoh_th;
  793. unsigned ip_length;
  794. u8 *header;
  795. /* Allocate a DMA-mapped header buffer. */
  796. if (likely(TSOH_SIZE(st->p.header_length) <= TSOH_STD_SIZE)) {
  797. if (tx_queue->tso_headers_free == NULL)
  798. if (efx_tsoh_block_alloc(tx_queue))
  799. return -1;
  800. EFX_BUG_ON_PARANOID(!tx_queue->tso_headers_free);
  801. tsoh = tx_queue->tso_headers_free;
  802. tx_queue->tso_headers_free = tsoh->next;
  803. tsoh->unmap_len = 0;
  804. } else {
  805. tx_queue->tso_long_headers++;
  806. tsoh = efx_tsoh_heap_alloc(tx_queue, st->p.header_length);
  807. if (unlikely(!tsoh))
  808. return -1;
  809. }
  810. header = TSOH_BUFFER(tsoh);
  811. tsoh_th = (struct tcphdr *)(header + SKB_TCP_OFF(skb));
  812. tsoh_iph = (struct iphdr *)(header + SKB_IPV4_OFF(skb));
  813. /* Copy and update the headers. */
  814. memcpy(header, skb->data, st->p.header_length);
  815. tsoh_th->seq = htonl(st->seqnum);
  816. st->seqnum += skb_shinfo(skb)->gso_size;
  817. if (st->remaining_len > skb_shinfo(skb)->gso_size) {
  818. /* This packet will not finish the TSO burst. */
  819. ip_length = st->p.full_packet_size - ETH_HDR_LEN(skb);
  820. tsoh_th->fin = 0;
  821. tsoh_th->psh = 0;
  822. } else {
  823. /* This packet will be the last in the TSO burst. */
  824. ip_length = (st->p.header_length - ETH_HDR_LEN(skb)
  825. + st->remaining_len);
  826. tsoh_th->fin = tcp_hdr(skb)->fin;
  827. tsoh_th->psh = tcp_hdr(skb)->psh;
  828. }
  829. tsoh_iph->tot_len = htons(ip_length);
  830. /* Linux leaves suitable gaps in the IP ID space for us to fill. */
  831. tsoh_iph->id = htons(st->p.ipv4_id);
  832. st->p.ipv4_id++;
  833. st->packet_space = skb_shinfo(skb)->gso_size;
  834. ++tx_queue->tso_packets;
  835. /* Form a descriptor for this header. */
  836. efx_tso_put_header(tx_queue, tsoh, st->p.header_length);
  837. return 0;
  838. }
  839. /**
  840. * efx_enqueue_skb_tso - segment and transmit a TSO socket buffer
  841. * @tx_queue: Efx TX queue
  842. * @skb: Socket buffer
  843. *
  844. * Context: You must hold netif_tx_lock() to call this function.
  845. *
  846. * Add socket buffer @skb to @tx_queue, doing TSO or return != 0 if
  847. * @skb was not enqueued. In all cases @skb is consumed. Return
  848. * %NETDEV_TX_OK or %NETDEV_TX_BUSY.
  849. */
  850. static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
  851. const struct sk_buff *skb)
  852. {
  853. int frag_i, rc, rc2 = NETDEV_TX_OK;
  854. struct tso_state state;
  855. skb_frag_t *f;
  856. /* Verify TSO is safe - these checks should never fail. */
  857. efx_tso_check_safe(skb);
  858. EFX_BUG_ON_PARANOID(tx_queue->write_count != tx_queue->insert_count);
  859. tso_start(&state, skb);
  860. /* Assume that skb header area contains exactly the headers, and
  861. * all payload is in the frag list.
  862. */
  863. if (skb_headlen(skb) == state.p.header_length) {
  864. /* Grab the first payload fragment. */
  865. EFX_BUG_ON_PARANOID(skb_shinfo(skb)->nr_frags < 1);
  866. frag_i = 0;
  867. f = &skb_shinfo(skb)->frags[frag_i];
  868. rc = tso_get_fragment(&state, tx_queue->efx,
  869. f->size, f->page, f->page_offset);
  870. if (rc)
  871. goto mem_err;
  872. } else {
  873. /* It may look like this code fragment assumes that the
  874. * skb->data portion does not cross a page boundary, but
  875. * that is not the case. It is guaranteed to be direct
  876. * mapped memory, and therefore is physically contiguous,
  877. * and so DMA will work fine. kmap_atomic() on this region
  878. * will just return the direct mapping, so that will work
  879. * too.
  880. */
  881. int page_off = (unsigned long)skb->data & (PAGE_SIZE - 1);
  882. int hl = state.p.header_length;
  883. rc = tso_get_fragment(&state, tx_queue->efx,
  884. skb_headlen(skb) - hl,
  885. virt_to_page(skb->data), page_off + hl);
  886. if (rc)
  887. goto mem_err;
  888. frag_i = -1;
  889. }
  890. if (tso_start_new_packet(tx_queue, skb, &state) < 0)
  891. goto mem_err;
  892. while (1) {
  893. rc = tso_fill_packet_with_fragment(tx_queue, skb, &state);
  894. if (unlikely(rc))
  895. goto stop;
  896. /* Move onto the next fragment? */
  897. if (state.ifc.len == 0) {
  898. if (++frag_i >= skb_shinfo(skb)->nr_frags)
  899. /* End of payload reached. */
  900. break;
  901. f = &skb_shinfo(skb)->frags[frag_i];
  902. rc = tso_get_fragment(&state, tx_queue->efx,
  903. f->size, f->page, f->page_offset);
  904. if (rc)
  905. goto mem_err;
  906. }
  907. /* Start at new packet? */
  908. if (state.packet_space == 0 &&
  909. tso_start_new_packet(tx_queue, skb, &state) < 0)
  910. goto mem_err;
  911. }
  912. /* Pass off to hardware */
  913. falcon_push_buffers(tx_queue);
  914. tx_queue->tso_bursts++;
  915. return NETDEV_TX_OK;
  916. mem_err:
  917. EFX_ERR(tx_queue->efx, "Out of memory for TSO headers, or PCI mapping"
  918. " error\n");
  919. dev_kfree_skb_any((struct sk_buff *)skb);
  920. goto unwind;
  921. stop:
  922. rc2 = NETDEV_TX_BUSY;
  923. /* Stop the queue if it wasn't stopped before. */
  924. if (tx_queue->stopped == 1)
  925. efx_stop_queue(tx_queue->efx);
  926. unwind:
  927. efx_enqueue_unwind(tx_queue);
  928. return rc2;
  929. }
  930. /*
  931. * Free up all TSO datastructures associated with tx_queue. This
  932. * routine should be called only once the tx_queue is both empty and
  933. * will no longer be used.
  934. */
  935. static void efx_fini_tso(struct efx_tx_queue *tx_queue)
  936. {
  937. unsigned i;
  938. if (tx_queue->buffer)
  939. for (i = 0; i <= tx_queue->efx->type->txd_ring_mask; ++i)
  940. efx_tsoh_free(tx_queue, &tx_queue->buffer[i]);
  941. while (tx_queue->tso_headers_free != NULL)
  942. efx_tsoh_block_free(tx_queue, tx_queue->tso_headers_free,
  943. tx_queue->efx->pci_dev);
  944. }