tx_tso.c 12 KB

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