tx.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2005-2009 Solarflare Communications Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation, incorporated herein by reference.
  9. */
  10. #include <linux/pci.h>
  11. #include <linux/tcp.h>
  12. #include <linux/ip.h>
  13. #include <linux/in.h>
  14. #include <linux/ipv6.h>
  15. #include <linux/slab.h>
  16. #include <net/ipv6.h>
  17. #include <linux/if_ether.h>
  18. #include <linux/highmem.h>
  19. #include "net_driver.h"
  20. #include "efx.h"
  21. #include "nic.h"
  22. #include "workarounds.h"
  23. /*
  24. * TX descriptor ring full threshold
  25. *
  26. * The tx_queue descriptor ring fill-level must fall below this value
  27. * before we restart the netif queue
  28. */
  29. #define EFX_TXQ_THRESHOLD (EFX_TXQ_MASK / 2u)
  30. /* We need to be able to nest calls to netif_tx_stop_queue(), partly
  31. * because of the 2 hardware queues associated with each core queue,
  32. * but also so that we can inhibit TX for reasons other than a full
  33. * hardware queue. */
  34. void efx_stop_queue(struct efx_channel *channel)
  35. {
  36. struct efx_nic *efx = channel->efx;
  37. if (!channel->tx_queue)
  38. return;
  39. spin_lock_bh(&channel->tx_stop_lock);
  40. netif_vdbg(efx, tx_queued, efx->net_dev, "stop TX queue\n");
  41. atomic_inc(&channel->tx_stop_count);
  42. netif_tx_stop_queue(
  43. netdev_get_tx_queue(
  44. efx->net_dev,
  45. channel->tx_queue->queue / EFX_TXQ_TYPES));
  46. spin_unlock_bh(&channel->tx_stop_lock);
  47. }
  48. /* Decrement core TX queue stop count and wake it if the count is 0 */
  49. void efx_wake_queue(struct efx_channel *channel)
  50. {
  51. struct efx_nic *efx = channel->efx;
  52. if (!channel->tx_queue)
  53. return;
  54. local_bh_disable();
  55. if (atomic_dec_and_lock(&channel->tx_stop_count,
  56. &channel->tx_stop_lock)) {
  57. netif_vdbg(efx, tx_queued, efx->net_dev, "waking TX queue\n");
  58. netif_tx_wake_queue(
  59. netdev_get_tx_queue(
  60. efx->net_dev,
  61. channel->tx_queue->queue / EFX_TXQ_TYPES));
  62. spin_unlock(&channel->tx_stop_lock);
  63. }
  64. local_bh_enable();
  65. }
  66. static void efx_dequeue_buffer(struct efx_tx_queue *tx_queue,
  67. struct efx_tx_buffer *buffer)
  68. {
  69. if (buffer->unmap_len) {
  70. struct pci_dev *pci_dev = tx_queue->efx->pci_dev;
  71. dma_addr_t unmap_addr = (buffer->dma_addr + buffer->len -
  72. buffer->unmap_len);
  73. if (buffer->unmap_single)
  74. pci_unmap_single(pci_dev, unmap_addr, buffer->unmap_len,
  75. PCI_DMA_TODEVICE);
  76. else
  77. pci_unmap_page(pci_dev, unmap_addr, buffer->unmap_len,
  78. PCI_DMA_TODEVICE);
  79. buffer->unmap_len = 0;
  80. buffer->unmap_single = false;
  81. }
  82. if (buffer->skb) {
  83. dev_kfree_skb_any((struct sk_buff *) buffer->skb);
  84. buffer->skb = NULL;
  85. netif_vdbg(tx_queue->efx, tx_done, tx_queue->efx->net_dev,
  86. "TX queue %d transmission id %x complete\n",
  87. tx_queue->queue, tx_queue->read_count);
  88. }
  89. }
  90. /**
  91. * struct efx_tso_header - a DMA mapped buffer for packet headers
  92. * @next: Linked list of free ones.
  93. * The list is protected by the TX queue lock.
  94. * @dma_unmap_len: Length to unmap for an oversize buffer, or 0.
  95. * @dma_addr: The DMA address of the header below.
  96. *
  97. * This controls the memory used for a TSO header. Use TSOH_DATA()
  98. * to find the packet header data. Use TSOH_SIZE() to calculate the
  99. * total size required for a given packet header length. TSO headers
  100. * in the free list are exactly %TSOH_STD_SIZE bytes in size.
  101. */
  102. struct efx_tso_header {
  103. union {
  104. struct efx_tso_header *next;
  105. size_t unmap_len;
  106. };
  107. dma_addr_t dma_addr;
  108. };
  109. static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
  110. struct sk_buff *skb);
  111. static void efx_fini_tso(struct efx_tx_queue *tx_queue);
  112. static void efx_tsoh_heap_free(struct efx_tx_queue *tx_queue,
  113. struct efx_tso_header *tsoh);
  114. static void efx_tsoh_free(struct efx_tx_queue *tx_queue,
  115. struct efx_tx_buffer *buffer)
  116. {
  117. if (buffer->tsoh) {
  118. if (likely(!buffer->tsoh->unmap_len)) {
  119. buffer->tsoh->next = tx_queue->tso_headers_free;
  120. tx_queue->tso_headers_free = buffer->tsoh;
  121. } else {
  122. efx_tsoh_heap_free(tx_queue, buffer->tsoh);
  123. }
  124. buffer->tsoh = NULL;
  125. }
  126. }
  127. static inline unsigned
  128. efx_max_tx_len(struct efx_nic *efx, dma_addr_t dma_addr)
  129. {
  130. /* Depending on the NIC revision, we can use descriptor
  131. * lengths up to 8K or 8K-1. However, since PCI Express
  132. * devices must split read requests at 4K boundaries, there is
  133. * little benefit from using descriptors that cross those
  134. * boundaries and we keep things simple by not doing so.
  135. */
  136. unsigned len = (~dma_addr & 0xfff) + 1;
  137. /* Work around hardware bug for unaligned buffers. */
  138. if (EFX_WORKAROUND_5391(efx) && (dma_addr & 0xf))
  139. len = min_t(unsigned, len, 512 - (dma_addr & 0xf));
  140. return len;
  141. }
  142. /*
  143. * Add a socket buffer to a TX queue
  144. *
  145. * This maps all fragments of a socket buffer for DMA and adds them to
  146. * the TX queue. The queue's insert pointer will be incremented by
  147. * the number of fragments in the socket buffer.
  148. *
  149. * If any DMA mapping fails, any mapped fragments will be unmapped,
  150. * the queue's insert pointer will be restored to its original value.
  151. *
  152. * This function is split out from efx_hard_start_xmit to allow the
  153. * loopback test to direct packets via specific TX queues.
  154. *
  155. * Returns NETDEV_TX_OK or NETDEV_TX_BUSY
  156. * You must hold netif_tx_lock() to call this function.
  157. */
  158. netdev_tx_t efx_enqueue_skb(struct efx_tx_queue *tx_queue, struct sk_buff *skb)
  159. {
  160. struct efx_nic *efx = tx_queue->efx;
  161. struct pci_dev *pci_dev = efx->pci_dev;
  162. struct efx_tx_buffer *buffer;
  163. skb_frag_t *fragment;
  164. struct page *page;
  165. int page_offset;
  166. unsigned int len, unmap_len = 0, fill_level, insert_ptr;
  167. dma_addr_t dma_addr, unmap_addr = 0;
  168. unsigned int dma_len;
  169. bool unmap_single;
  170. int q_space, i = 0;
  171. netdev_tx_t rc = NETDEV_TX_OK;
  172. EFX_BUG_ON_PARANOID(tx_queue->write_count != tx_queue->insert_count);
  173. if (skb_shinfo(skb)->gso_size)
  174. return efx_enqueue_skb_tso(tx_queue, skb);
  175. /* Get size of the initial fragment */
  176. len = skb_headlen(skb);
  177. /* Pad if necessary */
  178. if (EFX_WORKAROUND_15592(efx) && skb->len <= 32) {
  179. EFX_BUG_ON_PARANOID(skb->data_len);
  180. len = 32 + 1;
  181. if (skb_pad(skb, len - skb->len))
  182. return NETDEV_TX_OK;
  183. }
  184. fill_level = tx_queue->insert_count - tx_queue->old_read_count;
  185. q_space = EFX_TXQ_MASK - 1 - fill_level;
  186. /* Map for DMA. Use pci_map_single rather than pci_map_page
  187. * since this is more efficient on machines with sparse
  188. * memory.
  189. */
  190. unmap_single = true;
  191. dma_addr = pci_map_single(pci_dev, skb->data, len, PCI_DMA_TODEVICE);
  192. /* Process all fragments */
  193. while (1) {
  194. if (unlikely(pci_dma_mapping_error(pci_dev, dma_addr)))
  195. goto pci_err;
  196. /* Store fields for marking in the per-fragment final
  197. * descriptor */
  198. unmap_len = len;
  199. unmap_addr = dma_addr;
  200. /* Add to TX queue, splitting across DMA boundaries */
  201. do {
  202. if (unlikely(q_space-- <= 0)) {
  203. /* It might be that completions have
  204. * happened since the xmit path last
  205. * checked. Update the xmit path's
  206. * copy of read_count.
  207. */
  208. ++tx_queue->stopped;
  209. /* This memory barrier protects the
  210. * change of stopped from the access
  211. * of read_count. */
  212. smp_mb();
  213. tx_queue->old_read_count =
  214. *(volatile unsigned *)
  215. &tx_queue->read_count;
  216. fill_level = (tx_queue->insert_count
  217. - tx_queue->old_read_count);
  218. q_space = EFX_TXQ_MASK - 1 - fill_level;
  219. if (unlikely(q_space-- <= 0))
  220. goto stop;
  221. smp_mb();
  222. --tx_queue->stopped;
  223. }
  224. insert_ptr = tx_queue->insert_count & EFX_TXQ_MASK;
  225. buffer = &tx_queue->buffer[insert_ptr];
  226. efx_tsoh_free(tx_queue, buffer);
  227. EFX_BUG_ON_PARANOID(buffer->tsoh);
  228. EFX_BUG_ON_PARANOID(buffer->skb);
  229. EFX_BUG_ON_PARANOID(buffer->len);
  230. EFX_BUG_ON_PARANOID(!buffer->continuation);
  231. EFX_BUG_ON_PARANOID(buffer->unmap_len);
  232. dma_len = efx_max_tx_len(efx, dma_addr);
  233. if (likely(dma_len >= len))
  234. dma_len = len;
  235. /* Fill out per descriptor fields */
  236. buffer->len = dma_len;
  237. buffer->dma_addr = dma_addr;
  238. len -= dma_len;
  239. dma_addr += dma_len;
  240. ++tx_queue->insert_count;
  241. } while (len);
  242. /* Transfer ownership of the unmapping to the final buffer */
  243. buffer->unmap_single = unmap_single;
  244. buffer->unmap_len = unmap_len;
  245. unmap_len = 0;
  246. /* Get address and size of next fragment */
  247. if (i >= skb_shinfo(skb)->nr_frags)
  248. break;
  249. fragment = &skb_shinfo(skb)->frags[i];
  250. len = fragment->size;
  251. page = fragment->page;
  252. page_offset = fragment->page_offset;
  253. i++;
  254. /* Map for DMA */
  255. unmap_single = false;
  256. dma_addr = pci_map_page(pci_dev, page, page_offset, len,
  257. PCI_DMA_TODEVICE);
  258. }
  259. /* Transfer ownership of the skb to the final buffer */
  260. buffer->skb = skb;
  261. buffer->continuation = false;
  262. /* Pass off to hardware */
  263. efx_nic_push_buffers(tx_queue);
  264. return NETDEV_TX_OK;
  265. pci_err:
  266. netif_err(efx, tx_err, efx->net_dev,
  267. " TX queue %d could not map skb with %d bytes %d "
  268. "fragments for DMA\n", tx_queue->queue, skb->len,
  269. skb_shinfo(skb)->nr_frags + 1);
  270. /* Mark the packet as transmitted, and free the SKB ourselves */
  271. dev_kfree_skb_any(skb);
  272. goto unwind;
  273. stop:
  274. rc = NETDEV_TX_BUSY;
  275. if (tx_queue->stopped == 1)
  276. efx_stop_queue(tx_queue->channel);
  277. unwind:
  278. /* Work backwards until we hit the original insert pointer value */
  279. while (tx_queue->insert_count != tx_queue->write_count) {
  280. --tx_queue->insert_count;
  281. insert_ptr = tx_queue->insert_count & EFX_TXQ_MASK;
  282. buffer = &tx_queue->buffer[insert_ptr];
  283. efx_dequeue_buffer(tx_queue, buffer);
  284. buffer->len = 0;
  285. }
  286. /* Free the fragment we were mid-way through pushing */
  287. if (unmap_len) {
  288. if (unmap_single)
  289. pci_unmap_single(pci_dev, unmap_addr, unmap_len,
  290. PCI_DMA_TODEVICE);
  291. else
  292. pci_unmap_page(pci_dev, unmap_addr, unmap_len,
  293. PCI_DMA_TODEVICE);
  294. }
  295. return rc;
  296. }
  297. /* Remove packets from the TX queue
  298. *
  299. * This removes packets from the TX queue, up to and including the
  300. * specified index.
  301. */
  302. static void efx_dequeue_buffers(struct efx_tx_queue *tx_queue,
  303. unsigned int index)
  304. {
  305. struct efx_nic *efx = tx_queue->efx;
  306. unsigned int stop_index, read_ptr;
  307. stop_index = (index + 1) & EFX_TXQ_MASK;
  308. read_ptr = tx_queue->read_count & EFX_TXQ_MASK;
  309. while (read_ptr != stop_index) {
  310. struct efx_tx_buffer *buffer = &tx_queue->buffer[read_ptr];
  311. if (unlikely(buffer->len == 0)) {
  312. netif_err(efx, tx_err, efx->net_dev,
  313. "TX queue %d spurious TX completion id %x\n",
  314. tx_queue->queue, read_ptr);
  315. efx_schedule_reset(efx, RESET_TYPE_TX_SKIP);
  316. return;
  317. }
  318. efx_dequeue_buffer(tx_queue, buffer);
  319. buffer->continuation = true;
  320. buffer->len = 0;
  321. ++tx_queue->read_count;
  322. read_ptr = tx_queue->read_count & EFX_TXQ_MASK;
  323. }
  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. netdev_tx_t efx_hard_start_xmit(struct sk_buff *skb,
  335. struct net_device *net_dev)
  336. {
  337. struct efx_nic *efx = netdev_priv(net_dev);
  338. struct efx_tx_queue *tx_queue;
  339. if (unlikely(efx->port_inhibited))
  340. return NETDEV_TX_BUSY;
  341. tx_queue = &efx->tx_queue[EFX_TXQ_TYPES * skb_get_queue_mapping(skb)];
  342. if (likely(skb->ip_summed == CHECKSUM_PARTIAL))
  343. tx_queue += EFX_TXQ_TYPE_OFFLOAD;
  344. return efx_enqueue_skb(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_TXQ_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_TXQ_THRESHOLD) {
  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(tx_queue->channel);
  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. netif_dbg(efx, probe, efx->net_dev, "creating TX queue %d\n",
  377. tx_queue->queue);
  378. /* Allocate software ring */
  379. txq_size = EFX_TXQ_SIZE * sizeof(*tx_queue->buffer);
  380. tx_queue->buffer = kzalloc(txq_size, GFP_KERNEL);
  381. if (!tx_queue->buffer)
  382. return -ENOMEM;
  383. for (i = 0; i <= EFX_TXQ_MASK; ++i)
  384. tx_queue->buffer[i].continuation = true;
  385. /* Allocate hardware ring */
  386. rc = efx_nic_probe_tx(tx_queue);
  387. if (rc)
  388. goto fail;
  389. return 0;
  390. fail:
  391. kfree(tx_queue->buffer);
  392. tx_queue->buffer = NULL;
  393. return rc;
  394. }
  395. void efx_init_tx_queue(struct efx_tx_queue *tx_queue)
  396. {
  397. netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
  398. "initialising TX queue %d\n", tx_queue->queue);
  399. tx_queue->insert_count = 0;
  400. tx_queue->write_count = 0;
  401. tx_queue->read_count = 0;
  402. tx_queue->old_read_count = 0;
  403. BUG_ON(tx_queue->stopped);
  404. /* Set up TX descriptor ring */
  405. efx_nic_init_tx(tx_queue);
  406. }
  407. void efx_release_tx_buffers(struct efx_tx_queue *tx_queue)
  408. {
  409. struct efx_tx_buffer *buffer;
  410. if (!tx_queue->buffer)
  411. return;
  412. /* Free any buffers left in the ring */
  413. while (tx_queue->read_count != tx_queue->write_count) {
  414. buffer = &tx_queue->buffer[tx_queue->read_count & EFX_TXQ_MASK];
  415. efx_dequeue_buffer(tx_queue, buffer);
  416. buffer->continuation = true;
  417. buffer->len = 0;
  418. ++tx_queue->read_count;
  419. }
  420. }
  421. void efx_fini_tx_queue(struct efx_tx_queue *tx_queue)
  422. {
  423. netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
  424. "shutting down TX queue %d\n", tx_queue->queue);
  425. /* Flush TX queue, remove descriptor ring */
  426. efx_nic_fini_tx(tx_queue);
  427. efx_release_tx_buffers(tx_queue);
  428. /* Free up TSO header cache */
  429. efx_fini_tso(tx_queue);
  430. /* Release queue's stop on port, if any */
  431. if (tx_queue->stopped) {
  432. tx_queue->stopped = 0;
  433. efx_wake_queue(tx_queue->channel);
  434. }
  435. }
  436. void efx_remove_tx_queue(struct efx_tx_queue *tx_queue)
  437. {
  438. netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
  439. "destroying TX queue %d\n", tx_queue->queue);
  440. efx_nic_remove_tx(tx_queue);
  441. kfree(tx_queue->buffer);
  442. tx_queue->buffer = NULL;
  443. }
  444. /* Efx TCP segmentation acceleration.
  445. *
  446. * Why? Because by doing it here in the driver we can go significantly
  447. * faster than the GSO.
  448. *
  449. * Requires TX checksum offload support.
  450. */
  451. /* Number of bytes inserted at the start of a TSO header buffer,
  452. * similar to NET_IP_ALIGN.
  453. */
  454. #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  455. #define TSOH_OFFSET 0
  456. #else
  457. #define TSOH_OFFSET NET_IP_ALIGN
  458. #endif
  459. #define TSOH_BUFFER(tsoh) ((u8 *)(tsoh + 1) + TSOH_OFFSET)
  460. /* Total size of struct efx_tso_header, buffer and padding */
  461. #define TSOH_SIZE(hdr_len) \
  462. (sizeof(struct efx_tso_header) + TSOH_OFFSET + hdr_len)
  463. /* Size of blocks on free list. Larger blocks must be allocated from
  464. * the heap.
  465. */
  466. #define TSOH_STD_SIZE 128
  467. #define PTR_DIFF(p1, p2) ((u8 *)(p1) - (u8 *)(p2))
  468. #define ETH_HDR_LEN(skb) (skb_network_header(skb) - (skb)->data)
  469. #define SKB_TCP_OFF(skb) PTR_DIFF(tcp_hdr(skb), (skb)->data)
  470. #define SKB_IPV4_OFF(skb) PTR_DIFF(ip_hdr(skb), (skb)->data)
  471. #define SKB_IPV6_OFF(skb) PTR_DIFF(ipv6_hdr(skb), (skb)->data)
  472. /**
  473. * struct tso_state - TSO state for an SKB
  474. * @out_len: Remaining length in current segment
  475. * @seqnum: Current sequence number
  476. * @ipv4_id: Current IPv4 ID, host endian
  477. * @packet_space: Remaining space in current packet
  478. * @dma_addr: DMA address of current position
  479. * @in_len: Remaining length in current SKB fragment
  480. * @unmap_len: Length of SKB fragment
  481. * @unmap_addr: DMA address of SKB fragment
  482. * @unmap_single: DMA single vs page mapping flag
  483. * @protocol: Network protocol (after any VLAN header)
  484. * @header_len: Number of bytes of header
  485. * @full_packet_size: Number of bytes to put in each outgoing segment
  486. *
  487. * The state used during segmentation. It is put into this data structure
  488. * just to make it easy to pass into inline functions.
  489. */
  490. struct tso_state {
  491. /* Output position */
  492. unsigned out_len;
  493. unsigned seqnum;
  494. unsigned ipv4_id;
  495. unsigned packet_space;
  496. /* Input position */
  497. dma_addr_t dma_addr;
  498. unsigned in_len;
  499. unsigned unmap_len;
  500. dma_addr_t unmap_addr;
  501. bool unmap_single;
  502. __be16 protocol;
  503. unsigned header_len;
  504. int full_packet_size;
  505. };
  506. /*
  507. * Verify that our various assumptions about sk_buffs and the conditions
  508. * under which TSO will be attempted hold true. Return the protocol number.
  509. */
  510. static __be16 efx_tso_check_protocol(struct sk_buff *skb)
  511. {
  512. __be16 protocol = skb->protocol;
  513. EFX_BUG_ON_PARANOID(((struct ethhdr *)skb->data)->h_proto !=
  514. protocol);
  515. if (protocol == htons(ETH_P_8021Q)) {
  516. /* Find the encapsulated protocol; reset network header
  517. * and transport header based on that. */
  518. struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
  519. protocol = veh->h_vlan_encapsulated_proto;
  520. skb_set_network_header(skb, sizeof(*veh));
  521. if (protocol == htons(ETH_P_IP))
  522. skb_set_transport_header(skb, sizeof(*veh) +
  523. 4 * ip_hdr(skb)->ihl);
  524. else if (protocol == htons(ETH_P_IPV6))
  525. skb_set_transport_header(skb, sizeof(*veh) +
  526. sizeof(struct ipv6hdr));
  527. }
  528. if (protocol == htons(ETH_P_IP)) {
  529. EFX_BUG_ON_PARANOID(ip_hdr(skb)->protocol != IPPROTO_TCP);
  530. } else {
  531. EFX_BUG_ON_PARANOID(protocol != htons(ETH_P_IPV6));
  532. EFX_BUG_ON_PARANOID(ipv6_hdr(skb)->nexthdr != NEXTHDR_TCP);
  533. }
  534. EFX_BUG_ON_PARANOID((PTR_DIFF(tcp_hdr(skb), skb->data)
  535. + (tcp_hdr(skb)->doff << 2u)) >
  536. skb_headlen(skb));
  537. return protocol;
  538. }
  539. /*
  540. * Allocate a page worth of efx_tso_header structures, and string them
  541. * into the tx_queue->tso_headers_free linked list. Return 0 or -ENOMEM.
  542. */
  543. static int efx_tsoh_block_alloc(struct efx_tx_queue *tx_queue)
  544. {
  545. struct pci_dev *pci_dev = tx_queue->efx->pci_dev;
  546. struct efx_tso_header *tsoh;
  547. dma_addr_t dma_addr;
  548. u8 *base_kva, *kva;
  549. base_kva = pci_alloc_consistent(pci_dev, PAGE_SIZE, &dma_addr);
  550. if (base_kva == NULL) {
  551. netif_err(tx_queue->efx, tx_err, tx_queue->efx->net_dev,
  552. "Unable to allocate page for TSO headers\n");
  553. return -ENOMEM;
  554. }
  555. /* pci_alloc_consistent() allocates pages. */
  556. EFX_BUG_ON_PARANOID(dma_addr & (PAGE_SIZE - 1u));
  557. for (kva = base_kva; kva < base_kva + PAGE_SIZE; kva += TSOH_STD_SIZE) {
  558. tsoh = (struct efx_tso_header *)kva;
  559. tsoh->dma_addr = dma_addr + (TSOH_BUFFER(tsoh) - base_kva);
  560. tsoh->next = tx_queue->tso_headers_free;
  561. tx_queue->tso_headers_free = tsoh;
  562. }
  563. return 0;
  564. }
  565. /* Free up a TSO header, and all others in the same page. */
  566. static void efx_tsoh_block_free(struct efx_tx_queue *tx_queue,
  567. struct efx_tso_header *tsoh,
  568. struct pci_dev *pci_dev)
  569. {
  570. struct efx_tso_header **p;
  571. unsigned long base_kva;
  572. dma_addr_t base_dma;
  573. base_kva = (unsigned long)tsoh & PAGE_MASK;
  574. base_dma = tsoh->dma_addr & PAGE_MASK;
  575. p = &tx_queue->tso_headers_free;
  576. while (*p != NULL) {
  577. if (((unsigned long)*p & PAGE_MASK) == base_kva)
  578. *p = (*p)->next;
  579. else
  580. p = &(*p)->next;
  581. }
  582. pci_free_consistent(pci_dev, PAGE_SIZE, (void *)base_kva, base_dma);
  583. }
  584. static struct efx_tso_header *
  585. efx_tsoh_heap_alloc(struct efx_tx_queue *tx_queue, size_t header_len)
  586. {
  587. struct efx_tso_header *tsoh;
  588. tsoh = kmalloc(TSOH_SIZE(header_len), GFP_ATOMIC | GFP_DMA);
  589. if (unlikely(!tsoh))
  590. return NULL;
  591. tsoh->dma_addr = pci_map_single(tx_queue->efx->pci_dev,
  592. TSOH_BUFFER(tsoh), header_len,
  593. PCI_DMA_TODEVICE);
  594. if (unlikely(pci_dma_mapping_error(tx_queue->efx->pci_dev,
  595. tsoh->dma_addr))) {
  596. kfree(tsoh);
  597. return NULL;
  598. }
  599. tsoh->unmap_len = header_len;
  600. return tsoh;
  601. }
  602. static void
  603. efx_tsoh_heap_free(struct efx_tx_queue *tx_queue, struct efx_tso_header *tsoh)
  604. {
  605. pci_unmap_single(tx_queue->efx->pci_dev,
  606. tsoh->dma_addr, tsoh->unmap_len,
  607. PCI_DMA_TODEVICE);
  608. kfree(tsoh);
  609. }
  610. /**
  611. * efx_tx_queue_insert - push descriptors onto the TX queue
  612. * @tx_queue: Efx TX queue
  613. * @dma_addr: DMA address of fragment
  614. * @len: Length of fragment
  615. * @final_buffer: The final buffer inserted into the queue
  616. *
  617. * Push descriptors onto the TX queue. Return 0 on success or 1 if
  618. * @tx_queue full.
  619. */
  620. static int efx_tx_queue_insert(struct efx_tx_queue *tx_queue,
  621. dma_addr_t dma_addr, unsigned len,
  622. struct efx_tx_buffer **final_buffer)
  623. {
  624. struct efx_tx_buffer *buffer;
  625. struct efx_nic *efx = tx_queue->efx;
  626. unsigned dma_len, fill_level, insert_ptr;
  627. int q_space;
  628. EFX_BUG_ON_PARANOID(len <= 0);
  629. fill_level = tx_queue->insert_count - tx_queue->old_read_count;
  630. /* -1 as there is no way to represent all descriptors used */
  631. q_space = EFX_TXQ_MASK - 1 - fill_level;
  632. while (1) {
  633. if (unlikely(q_space-- <= 0)) {
  634. /* It might be that completions have happened
  635. * since the xmit path last checked. Update
  636. * the xmit path's copy of read_count.
  637. */
  638. ++tx_queue->stopped;
  639. /* This memory barrier protects the change of
  640. * stopped from the access of read_count. */
  641. smp_mb();
  642. tx_queue->old_read_count =
  643. *(volatile unsigned *)&tx_queue->read_count;
  644. fill_level = (tx_queue->insert_count
  645. - tx_queue->old_read_count);
  646. q_space = EFX_TXQ_MASK - 1 - fill_level;
  647. if (unlikely(q_space-- <= 0)) {
  648. *final_buffer = NULL;
  649. return 1;
  650. }
  651. smp_mb();
  652. --tx_queue->stopped;
  653. }
  654. insert_ptr = tx_queue->insert_count & EFX_TXQ_MASK;
  655. buffer = &tx_queue->buffer[insert_ptr];
  656. ++tx_queue->insert_count;
  657. EFX_BUG_ON_PARANOID(tx_queue->insert_count -
  658. tx_queue->read_count >
  659. EFX_TXQ_MASK);
  660. efx_tsoh_free(tx_queue, buffer);
  661. EFX_BUG_ON_PARANOID(buffer->len);
  662. EFX_BUG_ON_PARANOID(buffer->unmap_len);
  663. EFX_BUG_ON_PARANOID(buffer->skb);
  664. EFX_BUG_ON_PARANOID(!buffer->continuation);
  665. EFX_BUG_ON_PARANOID(buffer->tsoh);
  666. buffer->dma_addr = dma_addr;
  667. dma_len = efx_max_tx_len(efx, dma_addr);
  668. /* If there is enough space to send then do so */
  669. if (dma_len >= len)
  670. break;
  671. buffer->len = dma_len; /* Don't set the other members */
  672. dma_addr += dma_len;
  673. len -= dma_len;
  674. }
  675. EFX_BUG_ON_PARANOID(!len);
  676. buffer->len = len;
  677. *final_buffer = buffer;
  678. return 0;
  679. }
  680. /*
  681. * Put a TSO header into the TX queue.
  682. *
  683. * This is special-cased because we know that it is small enough to fit in
  684. * a single fragment, and we know it doesn't cross a page boundary. It
  685. * also allows us to not worry about end-of-packet etc.
  686. */
  687. static void efx_tso_put_header(struct efx_tx_queue *tx_queue,
  688. struct efx_tso_header *tsoh, unsigned len)
  689. {
  690. struct efx_tx_buffer *buffer;
  691. buffer = &tx_queue->buffer[tx_queue->insert_count & EFX_TXQ_MASK];
  692. efx_tsoh_free(tx_queue, buffer);
  693. EFX_BUG_ON_PARANOID(buffer->len);
  694. EFX_BUG_ON_PARANOID(buffer->unmap_len);
  695. EFX_BUG_ON_PARANOID(buffer->skb);
  696. EFX_BUG_ON_PARANOID(!buffer->continuation);
  697. EFX_BUG_ON_PARANOID(buffer->tsoh);
  698. buffer->len = len;
  699. buffer->dma_addr = tsoh->dma_addr;
  700. buffer->tsoh = tsoh;
  701. ++tx_queue->insert_count;
  702. }
  703. /* Remove descriptors put into a tx_queue. */
  704. static void efx_enqueue_unwind(struct efx_tx_queue *tx_queue)
  705. {
  706. struct efx_tx_buffer *buffer;
  707. dma_addr_t unmap_addr;
  708. /* Work backwards until we hit the original insert pointer value */
  709. while (tx_queue->insert_count != tx_queue->write_count) {
  710. --tx_queue->insert_count;
  711. buffer = &tx_queue->buffer[tx_queue->insert_count &
  712. EFX_TXQ_MASK];
  713. efx_tsoh_free(tx_queue, buffer);
  714. EFX_BUG_ON_PARANOID(buffer->skb);
  715. if (buffer->unmap_len) {
  716. unmap_addr = (buffer->dma_addr + buffer->len -
  717. buffer->unmap_len);
  718. if (buffer->unmap_single)
  719. pci_unmap_single(tx_queue->efx->pci_dev,
  720. unmap_addr, buffer->unmap_len,
  721. PCI_DMA_TODEVICE);
  722. else
  723. pci_unmap_page(tx_queue->efx->pci_dev,
  724. unmap_addr, buffer->unmap_len,
  725. PCI_DMA_TODEVICE);
  726. buffer->unmap_len = 0;
  727. }
  728. buffer->len = 0;
  729. buffer->continuation = true;
  730. }
  731. }
  732. /* Parse the SKB header and initialise state. */
  733. static void tso_start(struct tso_state *st, const struct sk_buff *skb)
  734. {
  735. /* All ethernet/IP/TCP headers combined size is TCP header size
  736. * plus offset of TCP header relative to start of packet.
  737. */
  738. st->header_len = ((tcp_hdr(skb)->doff << 2u)
  739. + PTR_DIFF(tcp_hdr(skb), skb->data));
  740. st->full_packet_size = st->header_len + skb_shinfo(skb)->gso_size;
  741. if (st->protocol == htons(ETH_P_IP))
  742. st->ipv4_id = ntohs(ip_hdr(skb)->id);
  743. else
  744. st->ipv4_id = 0;
  745. st->seqnum = ntohl(tcp_hdr(skb)->seq);
  746. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->urg);
  747. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->syn);
  748. EFX_BUG_ON_PARANOID(tcp_hdr(skb)->rst);
  749. st->packet_space = st->full_packet_size;
  750. st->out_len = skb->len - st->header_len;
  751. st->unmap_len = 0;
  752. st->unmap_single = false;
  753. }
  754. static int tso_get_fragment(struct tso_state *st, struct efx_nic *efx,
  755. skb_frag_t *frag)
  756. {
  757. st->unmap_addr = pci_map_page(efx->pci_dev, frag->page,
  758. frag->page_offset, frag->size,
  759. PCI_DMA_TODEVICE);
  760. if (likely(!pci_dma_mapping_error(efx->pci_dev, st->unmap_addr))) {
  761. st->unmap_single = false;
  762. st->unmap_len = frag->size;
  763. st->in_len = frag->size;
  764. st->dma_addr = st->unmap_addr;
  765. return 0;
  766. }
  767. return -ENOMEM;
  768. }
  769. static int tso_get_head_fragment(struct tso_state *st, struct efx_nic *efx,
  770. const struct sk_buff *skb)
  771. {
  772. int hl = st->header_len;
  773. int len = skb_headlen(skb) - hl;
  774. st->unmap_addr = pci_map_single(efx->pci_dev, skb->data + hl,
  775. len, PCI_DMA_TODEVICE);
  776. if (likely(!pci_dma_mapping_error(efx->pci_dev, st->unmap_addr))) {
  777. st->unmap_single = true;
  778. st->unmap_len = len;
  779. st->in_len = len;
  780. st->dma_addr = st->unmap_addr;
  781. return 0;
  782. }
  783. return -ENOMEM;
  784. }
  785. /**
  786. * tso_fill_packet_with_fragment - form descriptors for the current fragment
  787. * @tx_queue: Efx TX queue
  788. * @skb: Socket buffer
  789. * @st: TSO state
  790. *
  791. * Form descriptors for the current fragment, until we reach the end
  792. * of fragment or end-of-packet. Return 0 on success, 1 if not enough
  793. * space in @tx_queue.
  794. */
  795. static int tso_fill_packet_with_fragment(struct efx_tx_queue *tx_queue,
  796. const struct sk_buff *skb,
  797. struct tso_state *st)
  798. {
  799. struct efx_tx_buffer *buffer;
  800. int n, end_of_packet, rc;
  801. if (st->in_len == 0)
  802. return 0;
  803. if (st->packet_space == 0)
  804. return 0;
  805. EFX_BUG_ON_PARANOID(st->in_len <= 0);
  806. EFX_BUG_ON_PARANOID(st->packet_space <= 0);
  807. n = min(st->in_len, st->packet_space);
  808. st->packet_space -= n;
  809. st->out_len -= n;
  810. st->in_len -= n;
  811. rc = efx_tx_queue_insert(tx_queue, st->dma_addr, n, &buffer);
  812. if (likely(rc == 0)) {
  813. if (st->out_len == 0)
  814. /* Transfer ownership of the skb */
  815. buffer->skb = skb;
  816. end_of_packet = st->out_len == 0 || st->packet_space == 0;
  817. buffer->continuation = !end_of_packet;
  818. if (st->in_len == 0) {
  819. /* Transfer ownership of the pci mapping */
  820. buffer->unmap_len = st->unmap_len;
  821. buffer->unmap_single = st->unmap_single;
  822. st->unmap_len = 0;
  823. }
  824. }
  825. st->dma_addr += n;
  826. return rc;
  827. }
  828. /**
  829. * tso_start_new_packet - generate a new header and prepare for the new packet
  830. * @tx_queue: Efx TX queue
  831. * @skb: Socket buffer
  832. * @st: TSO state
  833. *
  834. * Generate a new header and prepare for the new packet. Return 0 on
  835. * success, or -1 if failed to alloc header.
  836. */
  837. static int tso_start_new_packet(struct efx_tx_queue *tx_queue,
  838. const struct sk_buff *skb,
  839. struct tso_state *st)
  840. {
  841. struct efx_tso_header *tsoh;
  842. struct tcphdr *tsoh_th;
  843. unsigned ip_length;
  844. u8 *header;
  845. /* Allocate a DMA-mapped header buffer. */
  846. if (likely(TSOH_SIZE(st->header_len) <= TSOH_STD_SIZE)) {
  847. if (tx_queue->tso_headers_free == NULL) {
  848. if (efx_tsoh_block_alloc(tx_queue))
  849. return -1;
  850. }
  851. EFX_BUG_ON_PARANOID(!tx_queue->tso_headers_free);
  852. tsoh = tx_queue->tso_headers_free;
  853. tx_queue->tso_headers_free = tsoh->next;
  854. tsoh->unmap_len = 0;
  855. } else {
  856. tx_queue->tso_long_headers++;
  857. tsoh = efx_tsoh_heap_alloc(tx_queue, st->header_len);
  858. if (unlikely(!tsoh))
  859. return -1;
  860. }
  861. header = TSOH_BUFFER(tsoh);
  862. tsoh_th = (struct tcphdr *)(header + SKB_TCP_OFF(skb));
  863. /* Copy and update the headers. */
  864. memcpy(header, skb->data, st->header_len);
  865. tsoh_th->seq = htonl(st->seqnum);
  866. st->seqnum += skb_shinfo(skb)->gso_size;
  867. if (st->out_len > skb_shinfo(skb)->gso_size) {
  868. /* This packet will not finish the TSO burst. */
  869. ip_length = st->full_packet_size - ETH_HDR_LEN(skb);
  870. tsoh_th->fin = 0;
  871. tsoh_th->psh = 0;
  872. } else {
  873. /* This packet will be the last in the TSO burst. */
  874. ip_length = st->header_len - ETH_HDR_LEN(skb) + st->out_len;
  875. tsoh_th->fin = tcp_hdr(skb)->fin;
  876. tsoh_th->psh = tcp_hdr(skb)->psh;
  877. }
  878. if (st->protocol == htons(ETH_P_IP)) {
  879. struct iphdr *tsoh_iph =
  880. (struct iphdr *)(header + SKB_IPV4_OFF(skb));
  881. tsoh_iph->tot_len = htons(ip_length);
  882. /* Linux leaves suitable gaps in the IP ID space for us to fill. */
  883. tsoh_iph->id = htons(st->ipv4_id);
  884. st->ipv4_id++;
  885. } else {
  886. struct ipv6hdr *tsoh_iph =
  887. (struct ipv6hdr *)(header + SKB_IPV6_OFF(skb));
  888. tsoh_iph->payload_len = htons(ip_length - sizeof(*tsoh_iph));
  889. }
  890. st->packet_space = skb_shinfo(skb)->gso_size;
  891. ++tx_queue->tso_packets;
  892. /* Form a descriptor for this header. */
  893. efx_tso_put_header(tx_queue, tsoh, st->header_len);
  894. return 0;
  895. }
  896. /**
  897. * efx_enqueue_skb_tso - segment and transmit a TSO socket buffer
  898. * @tx_queue: Efx TX queue
  899. * @skb: Socket buffer
  900. *
  901. * Context: You must hold netif_tx_lock() to call this function.
  902. *
  903. * Add socket buffer @skb to @tx_queue, doing TSO or return != 0 if
  904. * @skb was not enqueued. In all cases @skb is consumed. Return
  905. * %NETDEV_TX_OK or %NETDEV_TX_BUSY.
  906. */
  907. static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
  908. struct sk_buff *skb)
  909. {
  910. struct efx_nic *efx = tx_queue->efx;
  911. int frag_i, rc, rc2 = NETDEV_TX_OK;
  912. struct tso_state state;
  913. /* Find the packet protocol and sanity-check it */
  914. state.protocol = efx_tso_check_protocol(skb);
  915. EFX_BUG_ON_PARANOID(tx_queue->write_count != tx_queue->insert_count);
  916. tso_start(&state, skb);
  917. /* Assume that skb header area contains exactly the headers, and
  918. * all payload is in the frag list.
  919. */
  920. if (skb_headlen(skb) == state.header_len) {
  921. /* Grab the first payload fragment. */
  922. EFX_BUG_ON_PARANOID(skb_shinfo(skb)->nr_frags < 1);
  923. frag_i = 0;
  924. rc = tso_get_fragment(&state, efx,
  925. skb_shinfo(skb)->frags + frag_i);
  926. if (rc)
  927. goto mem_err;
  928. } else {
  929. rc = tso_get_head_fragment(&state, efx, skb);
  930. if (rc)
  931. goto mem_err;
  932. frag_i = -1;
  933. }
  934. if (tso_start_new_packet(tx_queue, skb, &state) < 0)
  935. goto mem_err;
  936. while (1) {
  937. rc = tso_fill_packet_with_fragment(tx_queue, skb, &state);
  938. if (unlikely(rc))
  939. goto stop;
  940. /* Move onto the next fragment? */
  941. if (state.in_len == 0) {
  942. if (++frag_i >= skb_shinfo(skb)->nr_frags)
  943. /* End of payload reached. */
  944. break;
  945. rc = tso_get_fragment(&state, efx,
  946. skb_shinfo(skb)->frags + frag_i);
  947. if (rc)
  948. goto mem_err;
  949. }
  950. /* Start at new packet? */
  951. if (state.packet_space == 0 &&
  952. tso_start_new_packet(tx_queue, skb, &state) < 0)
  953. goto mem_err;
  954. }
  955. /* Pass off to hardware */
  956. efx_nic_push_buffers(tx_queue);
  957. tx_queue->tso_bursts++;
  958. return NETDEV_TX_OK;
  959. mem_err:
  960. netif_err(efx, tx_err, efx->net_dev,
  961. "Out of memory for TSO headers, or PCI mapping error\n");
  962. dev_kfree_skb_any(skb);
  963. goto unwind;
  964. stop:
  965. rc2 = NETDEV_TX_BUSY;
  966. /* Stop the queue if it wasn't stopped before. */
  967. if (tx_queue->stopped == 1)
  968. efx_stop_queue(tx_queue->channel);
  969. unwind:
  970. /* Free the DMA mapping we were in the process of writing out */
  971. if (state.unmap_len) {
  972. if (state.unmap_single)
  973. pci_unmap_single(efx->pci_dev, state.unmap_addr,
  974. state.unmap_len, PCI_DMA_TODEVICE);
  975. else
  976. pci_unmap_page(efx->pci_dev, state.unmap_addr,
  977. state.unmap_len, PCI_DMA_TODEVICE);
  978. }
  979. efx_enqueue_unwind(tx_queue);
  980. return rc2;
  981. }
  982. /*
  983. * Free up all TSO datastructures associated with tx_queue. This
  984. * routine should be called only once the tx_queue is both empty and
  985. * will no longer be used.
  986. */
  987. static void efx_fini_tso(struct efx_tx_queue *tx_queue)
  988. {
  989. unsigned i;
  990. if (tx_queue->buffer) {
  991. for (i = 0; i <= EFX_TXQ_MASK; ++i)
  992. efx_tsoh_free(tx_queue, &tx_queue->buffer[i]);
  993. }
  994. while (tx_queue->tso_headers_free != NULL)
  995. efx_tsoh_block_free(tx_queue, tx_queue->tso_headers_free,
  996. tx_queue->efx->pci_dev);
  997. }