tx.c 34 KB

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