tx.c 30 KB

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