tx.c 31 KB

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