tx_tso.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /****************************************************************************
  2. * Driver for Solarflare network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2005-2015 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 <linux/moduleparam.h>
  20. #include <linux/cache.h>
  21. #include "net_driver.h"
  22. #include "efx.h"
  23. #include "io.h"
  24. #include "nic.h"
  25. #include "tx.h"
  26. #include "workarounds.h"
  27. #include "ef10_regs.h"
  28. /* Efx legacy TCP segmentation acceleration.
  29. *
  30. * Utilises firmware support to go faster than GSO (but not as fast as TSOv2).
  31. *
  32. * Requires TX checksum offload support.
  33. */
  34. #define PTR_DIFF(p1, p2) ((u8 *)(p1) - (u8 *)(p2))
  35. /**
  36. * struct tso_state - TSO state for an SKB
  37. * @out_len: Remaining length in current segment
  38. * @seqnum: Current sequence number
  39. * @ipv4_id: Current IPv4 ID, host endian
  40. * @packet_space: Remaining space in current packet
  41. * @dma_addr: DMA address of current position
  42. * @in_len: Remaining length in current SKB fragment
  43. * @unmap_len: Length of SKB fragment
  44. * @unmap_addr: DMA address of SKB fragment
  45. * @protocol: Network protocol (after any VLAN header)
  46. * @ip_off: Offset of IP header
  47. * @tcp_off: Offset of TCP header
  48. * @header_len: Number of bytes of header
  49. * @ip_base_len: IPv4 tot_len or IPv6 payload_len, before TCP payload
  50. * @header_dma_addr: Header DMA address
  51. * @header_unmap_len: Header DMA mapped length
  52. *
  53. * The state used during segmentation. It is put into this data structure
  54. * just to make it easy to pass into inline functions.
  55. */
  56. struct tso_state {
  57. /* Output position */
  58. unsigned int out_len;
  59. unsigned int seqnum;
  60. u16 ipv4_id;
  61. unsigned int packet_space;
  62. /* Input position */
  63. dma_addr_t dma_addr;
  64. unsigned int in_len;
  65. unsigned int unmap_len;
  66. dma_addr_t unmap_addr;
  67. __be16 protocol;
  68. unsigned int ip_off;
  69. unsigned int tcp_off;
  70. unsigned int header_len;
  71. unsigned int ip_base_len;
  72. dma_addr_t header_dma_addr;
  73. unsigned int header_unmap_len;
  74. };
  75. static inline void prefetch_ptr(struct efx_tx_queue *tx_queue)
  76. {
  77. unsigned int insert_ptr = efx_tx_queue_get_insert_index(tx_queue);
  78. char *ptr;
  79. ptr = (char *) (tx_queue->buffer + insert_ptr);
  80. prefetch(ptr);
  81. prefetch(ptr + 0x80);
  82. ptr = (char *) (((efx_qword_t *)tx_queue->txd.buf.addr) + insert_ptr);
  83. prefetch(ptr);
  84. prefetch(ptr + 0x80);
  85. }
  86. /**
  87. * efx_tx_queue_insert - push descriptors onto the TX queue
  88. * @tx_queue: Efx TX queue
  89. * @dma_addr: DMA address of fragment
  90. * @len: Length of fragment
  91. * @final_buffer: The final buffer inserted into the queue
  92. *
  93. * Push descriptors onto the TX queue.
  94. */
  95. static void efx_tx_queue_insert(struct efx_tx_queue *tx_queue,
  96. dma_addr_t dma_addr, unsigned int len,
  97. struct efx_tx_buffer **final_buffer)
  98. {
  99. struct efx_tx_buffer *buffer;
  100. unsigned int dma_len;
  101. EFX_WARN_ON_ONCE_PARANOID(len <= 0);
  102. while (1) {
  103. buffer = efx_tx_queue_get_insert_buffer(tx_queue);
  104. ++tx_queue->insert_count;
  105. EFX_WARN_ON_ONCE_PARANOID(tx_queue->insert_count -
  106. tx_queue->read_count >=
  107. tx_queue->efx->txq_entries);
  108. buffer->dma_addr = dma_addr;
  109. dma_len = tx_queue->efx->type->tx_limit_len(tx_queue,
  110. dma_addr, len);
  111. /* If there's space for everything this is our last buffer. */
  112. if (dma_len >= len)
  113. break;
  114. buffer->len = dma_len;
  115. buffer->flags = EFX_TX_BUF_CONT;
  116. dma_addr += dma_len;
  117. len -= dma_len;
  118. }
  119. EFX_WARN_ON_ONCE_PARANOID(!len);
  120. buffer->len = len;
  121. *final_buffer = buffer;
  122. }
  123. /*
  124. * Verify that our various assumptions about sk_buffs and the conditions
  125. * under which TSO will be attempted hold true. Return the protocol number.
  126. */
  127. static __be16 efx_tso_check_protocol(struct sk_buff *skb)
  128. {
  129. __be16 protocol = skb->protocol;
  130. EFX_WARN_ON_ONCE_PARANOID(((struct ethhdr *)skb->data)->h_proto !=
  131. protocol);
  132. if (protocol == htons(ETH_P_8021Q)) {
  133. struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
  134. protocol = veh->h_vlan_encapsulated_proto;
  135. }
  136. if (protocol == htons(ETH_P_IP)) {
  137. EFX_WARN_ON_ONCE_PARANOID(ip_hdr(skb)->protocol != IPPROTO_TCP);
  138. } else {
  139. EFX_WARN_ON_ONCE_PARANOID(protocol != htons(ETH_P_IPV6));
  140. EFX_WARN_ON_ONCE_PARANOID(ipv6_hdr(skb)->nexthdr != NEXTHDR_TCP);
  141. }
  142. EFX_WARN_ON_ONCE_PARANOID((PTR_DIFF(tcp_hdr(skb), skb->data) +
  143. (tcp_hdr(skb)->doff << 2u)) >
  144. skb_headlen(skb));
  145. return protocol;
  146. }
  147. /* Parse the SKB header and initialise state. */
  148. static int tso_start(struct tso_state *st, struct efx_nic *efx,
  149. struct efx_tx_queue *tx_queue,
  150. const struct sk_buff *skb)
  151. {
  152. struct device *dma_dev = &efx->pci_dev->dev;
  153. unsigned int header_len, in_len;
  154. dma_addr_t dma_addr;
  155. st->ip_off = skb_network_header(skb) - skb->data;
  156. st->tcp_off = skb_transport_header(skb) - skb->data;
  157. header_len = st->tcp_off + (tcp_hdr(skb)->doff << 2u);
  158. in_len = skb_headlen(skb) - header_len;
  159. st->header_len = header_len;
  160. st->in_len = in_len;
  161. if (st->protocol == htons(ETH_P_IP)) {
  162. st->ip_base_len = st->header_len - st->ip_off;
  163. st->ipv4_id = ntohs(ip_hdr(skb)->id);
  164. } else {
  165. st->ip_base_len = st->header_len - st->tcp_off;
  166. st->ipv4_id = 0;
  167. }
  168. st->seqnum = ntohl(tcp_hdr(skb)->seq);
  169. EFX_WARN_ON_ONCE_PARANOID(tcp_hdr(skb)->urg);
  170. EFX_WARN_ON_ONCE_PARANOID(tcp_hdr(skb)->syn);
  171. EFX_WARN_ON_ONCE_PARANOID(tcp_hdr(skb)->rst);
  172. st->out_len = skb->len - header_len;
  173. dma_addr = dma_map_single(dma_dev, skb->data,
  174. skb_headlen(skb), DMA_TO_DEVICE);
  175. st->header_dma_addr = dma_addr;
  176. st->header_unmap_len = skb_headlen(skb);
  177. st->dma_addr = dma_addr + header_len;
  178. st->unmap_len = 0;
  179. return unlikely(dma_mapping_error(dma_dev, dma_addr)) ? -ENOMEM : 0;
  180. }
  181. static int tso_get_fragment(struct tso_state *st, struct efx_nic *efx,
  182. skb_frag_t *frag)
  183. {
  184. st->unmap_addr = skb_frag_dma_map(&efx->pci_dev->dev, frag, 0,
  185. skb_frag_size(frag), DMA_TO_DEVICE);
  186. if (likely(!dma_mapping_error(&efx->pci_dev->dev, st->unmap_addr))) {
  187. st->unmap_len = skb_frag_size(frag);
  188. st->in_len = skb_frag_size(frag);
  189. st->dma_addr = st->unmap_addr;
  190. return 0;
  191. }
  192. return -ENOMEM;
  193. }
  194. /**
  195. * tso_fill_packet_with_fragment - form descriptors for the current fragment
  196. * @tx_queue: Efx TX queue
  197. * @skb: Socket buffer
  198. * @st: TSO state
  199. *
  200. * Form descriptors for the current fragment, until we reach the end
  201. * of fragment or end-of-packet.
  202. */
  203. static void tso_fill_packet_with_fragment(struct efx_tx_queue *tx_queue,
  204. const struct sk_buff *skb,
  205. struct tso_state *st)
  206. {
  207. struct efx_tx_buffer *buffer;
  208. int n;
  209. if (st->in_len == 0)
  210. return;
  211. if (st->packet_space == 0)
  212. return;
  213. EFX_WARN_ON_ONCE_PARANOID(st->in_len <= 0);
  214. EFX_WARN_ON_ONCE_PARANOID(st->packet_space <= 0);
  215. n = min(st->in_len, st->packet_space);
  216. st->packet_space -= n;
  217. st->out_len -= n;
  218. st->in_len -= n;
  219. efx_tx_queue_insert(tx_queue, st->dma_addr, n, &buffer);
  220. if (st->out_len == 0) {
  221. /* Transfer ownership of the skb */
  222. buffer->skb = skb;
  223. buffer->flags = EFX_TX_BUF_SKB;
  224. } else if (st->packet_space != 0) {
  225. buffer->flags = EFX_TX_BUF_CONT;
  226. }
  227. if (st->in_len == 0) {
  228. /* Transfer ownership of the DMA mapping */
  229. buffer->unmap_len = st->unmap_len;
  230. buffer->dma_offset = buffer->unmap_len - buffer->len;
  231. st->unmap_len = 0;
  232. }
  233. st->dma_addr += n;
  234. }
  235. #define TCP_FLAGS_OFFSET 13
  236. /**
  237. * tso_start_new_packet - generate a new header and prepare for the new packet
  238. * @tx_queue: Efx TX queue
  239. * @skb: Socket buffer
  240. * @st: TSO state
  241. *
  242. * Generate a new header and prepare for the new packet. Return 0 on
  243. * success, or -%ENOMEM if failed to alloc header, or other negative error.
  244. */
  245. static int tso_start_new_packet(struct efx_tx_queue *tx_queue,
  246. const struct sk_buff *skb,
  247. struct tso_state *st)
  248. {
  249. struct efx_tx_buffer *buffer =
  250. efx_tx_queue_get_insert_buffer(tx_queue);
  251. bool is_last = st->out_len <= skb_shinfo(skb)->gso_size;
  252. u8 tcp_flags_mask, tcp_flags;
  253. if (!is_last) {
  254. st->packet_space = skb_shinfo(skb)->gso_size;
  255. tcp_flags_mask = 0x09; /* mask out FIN and PSH */
  256. } else {
  257. st->packet_space = st->out_len;
  258. tcp_flags_mask = 0x00;
  259. }
  260. if (WARN_ON(!st->header_unmap_len))
  261. return -EINVAL;
  262. /* Send the original headers with a TSO option descriptor
  263. * in front
  264. */
  265. tcp_flags = ((u8 *)tcp_hdr(skb))[TCP_FLAGS_OFFSET] & ~tcp_flags_mask;
  266. buffer->flags = EFX_TX_BUF_OPTION;
  267. buffer->len = 0;
  268. buffer->unmap_len = 0;
  269. EFX_POPULATE_QWORD_5(buffer->option,
  270. ESF_DZ_TX_DESC_IS_OPT, 1,
  271. ESF_DZ_TX_OPTION_TYPE,
  272. ESE_DZ_TX_OPTION_DESC_TSO,
  273. ESF_DZ_TX_TSO_TCP_FLAGS, tcp_flags,
  274. ESF_DZ_TX_TSO_IP_ID, st->ipv4_id,
  275. ESF_DZ_TX_TSO_TCP_SEQNO, st->seqnum);
  276. ++tx_queue->insert_count;
  277. /* We mapped the headers in tso_start(). Unmap them
  278. * when the last segment is completed.
  279. */
  280. buffer = efx_tx_queue_get_insert_buffer(tx_queue);
  281. buffer->dma_addr = st->header_dma_addr;
  282. buffer->len = st->header_len;
  283. if (is_last) {
  284. buffer->flags = EFX_TX_BUF_CONT | EFX_TX_BUF_MAP_SINGLE;
  285. buffer->unmap_len = st->header_unmap_len;
  286. buffer->dma_offset = 0;
  287. /* Ensure we only unmap them once in case of a
  288. * later DMA mapping error and rollback
  289. */
  290. st->header_unmap_len = 0;
  291. } else {
  292. buffer->flags = EFX_TX_BUF_CONT;
  293. buffer->unmap_len = 0;
  294. }
  295. ++tx_queue->insert_count;
  296. st->seqnum += skb_shinfo(skb)->gso_size;
  297. /* Linux leaves suitable gaps in the IP ID space for us to fill. */
  298. ++st->ipv4_id;
  299. return 0;
  300. }
  301. /**
  302. * efx_enqueue_skb_tso - segment and transmit a TSO socket buffer
  303. * @tx_queue: Efx TX queue
  304. * @skb: Socket buffer
  305. * @data_mapped: Did we map the data? Always set to true
  306. * by this on success.
  307. *
  308. * Context: You must hold netif_tx_lock() to call this function.
  309. *
  310. * Add socket buffer @skb to @tx_queue, doing TSO or return != 0 if
  311. * @skb was not enqueued. @skb is consumed unless return value is
  312. * %EINVAL.
  313. */
  314. int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
  315. struct sk_buff *skb,
  316. bool *data_mapped)
  317. {
  318. struct efx_nic *efx = tx_queue->efx;
  319. int frag_i, rc;
  320. struct tso_state state;
  321. if (tx_queue->tso_version != 1)
  322. return -EINVAL;
  323. prefetch(skb->data);
  324. /* Find the packet protocol and sanity-check it */
  325. state.protocol = efx_tso_check_protocol(skb);
  326. EFX_WARN_ON_ONCE_PARANOID(tx_queue->write_count != tx_queue->insert_count);
  327. rc = tso_start(&state, efx, tx_queue, skb);
  328. if (rc)
  329. goto fail;
  330. if (likely(state.in_len == 0)) {
  331. /* Grab the first payload fragment. */
  332. EFX_WARN_ON_ONCE_PARANOID(skb_shinfo(skb)->nr_frags < 1);
  333. frag_i = 0;
  334. rc = tso_get_fragment(&state, efx,
  335. skb_shinfo(skb)->frags + frag_i);
  336. if (rc)
  337. goto fail;
  338. } else {
  339. /* Payload starts in the header area. */
  340. frag_i = -1;
  341. }
  342. rc = tso_start_new_packet(tx_queue, skb, &state);
  343. if (rc)
  344. goto fail;
  345. prefetch_ptr(tx_queue);
  346. while (1) {
  347. tso_fill_packet_with_fragment(tx_queue, skb, &state);
  348. /* Move onto the next fragment? */
  349. if (state.in_len == 0) {
  350. if (++frag_i >= skb_shinfo(skb)->nr_frags)
  351. /* End of payload reached. */
  352. break;
  353. rc = tso_get_fragment(&state, efx,
  354. skb_shinfo(skb)->frags + frag_i);
  355. if (rc)
  356. goto fail;
  357. }
  358. /* Start at new packet? */
  359. if (state.packet_space == 0) {
  360. rc = tso_start_new_packet(tx_queue, skb, &state);
  361. if (rc)
  362. goto fail;
  363. }
  364. }
  365. *data_mapped = true;
  366. return 0;
  367. fail:
  368. if (rc == -ENOMEM)
  369. netif_err(efx, tx_err, efx->net_dev,
  370. "Out of memory for TSO headers, or DMA mapping error\n");
  371. else
  372. netif_err(efx, tx_err, efx->net_dev, "TSO failed, rc = %d\n", rc);
  373. /* Free the DMA mapping we were in the process of writing out */
  374. if (state.unmap_len) {
  375. dma_unmap_page(&efx->pci_dev->dev, state.unmap_addr,
  376. state.unmap_len, DMA_TO_DEVICE);
  377. }
  378. /* Free the header DMA mapping */
  379. if (state.header_unmap_len)
  380. dma_unmap_single(&efx->pci_dev->dev, state.header_dma_addr,
  381. state.header_unmap_len, DMA_TO_DEVICE);
  382. return rc;
  383. }