ethernet-tx.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This file is based on code from OCTEON SDK by Cavium Networks.
  4. *
  5. * Copyright (c) 2003-2010 Cavium Networks
  6. */
  7. #include <linux/module.h>
  8. #include <linux/kernel.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/etherdevice.h>
  11. #include <linux/ip.h>
  12. #include <linux/ratelimit.h>
  13. #include <linux/string.h>
  14. #include <linux/interrupt.h>
  15. #include <net/dst.h>
  16. #ifdef CONFIG_XFRM
  17. #include <linux/xfrm.h>
  18. #include <net/xfrm.h>
  19. #endif /* CONFIG_XFRM */
  20. #include <linux/atomic.h>
  21. #include <net/sch_generic.h>
  22. #include <asm/octeon/octeon.h>
  23. #include "ethernet-defines.h"
  24. #include "octeon-ethernet.h"
  25. #include "ethernet-tx.h"
  26. #include "ethernet-util.h"
  27. #include <asm/octeon/cvmx-wqe.h>
  28. #include <asm/octeon/cvmx-fau.h>
  29. #include <asm/octeon/cvmx-pip.h>
  30. #include <asm/octeon/cvmx-pko.h>
  31. #include <asm/octeon/cvmx-helper.h>
  32. #include <asm/octeon/cvmx-gmxx-defs.h>
  33. #define CVM_OCT_SKB_CB(skb) ((u64 *)((skb)->cb))
  34. /*
  35. * You can define GET_SKBUFF_QOS() to override how the skbuff output
  36. * function determines which output queue is used. The default
  37. * implementation always uses the base queue for the port. If, for
  38. * example, you wanted to use the skb->priority field, define
  39. * GET_SKBUFF_QOS as: #define GET_SKBUFF_QOS(skb) ((skb)->priority)
  40. */
  41. #ifndef GET_SKBUFF_QOS
  42. #define GET_SKBUFF_QOS(skb) 0
  43. #endif
  44. static void cvm_oct_tx_do_cleanup(unsigned long arg);
  45. static DECLARE_TASKLET(cvm_oct_tx_cleanup_tasklet, cvm_oct_tx_do_cleanup, 0);
  46. /* Maximum number of SKBs to try to free per xmit packet. */
  47. #define MAX_SKB_TO_FREE (MAX_OUT_QUEUE_DEPTH * 2)
  48. static inline int cvm_oct_adjust_skb_to_free(int skb_to_free, int fau)
  49. {
  50. int undo;
  51. undo = skb_to_free > 0 ? MAX_SKB_TO_FREE : skb_to_free +
  52. MAX_SKB_TO_FREE;
  53. if (undo > 0)
  54. cvmx_fau_atomic_add32(fau, -undo);
  55. skb_to_free = -skb_to_free > MAX_SKB_TO_FREE ? MAX_SKB_TO_FREE :
  56. -skb_to_free;
  57. return skb_to_free;
  58. }
  59. static void cvm_oct_kick_tx_poll_watchdog(void)
  60. {
  61. union cvmx_ciu_timx ciu_timx;
  62. ciu_timx.u64 = 0;
  63. ciu_timx.s.one_shot = 1;
  64. ciu_timx.s.len = cvm_oct_tx_poll_interval;
  65. cvmx_write_csr(CVMX_CIU_TIMX(1), ciu_timx.u64);
  66. }
  67. static void cvm_oct_free_tx_skbs(struct net_device *dev)
  68. {
  69. int skb_to_free;
  70. int qos, queues_per_port;
  71. int total_freed = 0;
  72. int total_remaining = 0;
  73. unsigned long flags;
  74. struct octeon_ethernet *priv = netdev_priv(dev);
  75. queues_per_port = cvmx_pko_get_num_queues(priv->port);
  76. /* Drain any pending packets in the free list */
  77. for (qos = 0; qos < queues_per_port; qos++) {
  78. if (skb_queue_len(&priv->tx_free_list[qos]) == 0)
  79. continue;
  80. skb_to_free = cvmx_fau_fetch_and_add32(priv->fau + qos * 4,
  81. MAX_SKB_TO_FREE);
  82. skb_to_free = cvm_oct_adjust_skb_to_free(skb_to_free,
  83. priv->fau + qos * 4);
  84. total_freed += skb_to_free;
  85. if (skb_to_free > 0) {
  86. struct sk_buff *to_free_list = NULL;
  87. spin_lock_irqsave(&priv->tx_free_list[qos].lock, flags);
  88. while (skb_to_free > 0) {
  89. struct sk_buff *t;
  90. t = __skb_dequeue(&priv->tx_free_list[qos]);
  91. t->next = to_free_list;
  92. to_free_list = t;
  93. skb_to_free--;
  94. }
  95. spin_unlock_irqrestore(&priv->tx_free_list[qos].lock,
  96. flags);
  97. /* Do the actual freeing outside of the lock. */
  98. while (to_free_list) {
  99. struct sk_buff *t = to_free_list;
  100. to_free_list = to_free_list->next;
  101. dev_kfree_skb_any(t);
  102. }
  103. }
  104. total_remaining += skb_queue_len(&priv->tx_free_list[qos]);
  105. }
  106. if (total_remaining < MAX_OUT_QUEUE_DEPTH && netif_queue_stopped(dev))
  107. netif_wake_queue(dev);
  108. if (total_remaining)
  109. cvm_oct_kick_tx_poll_watchdog();
  110. }
  111. /**
  112. * cvm_oct_xmit - transmit a packet
  113. * @skb: Packet to send
  114. * @dev: Device info structure
  115. *
  116. * Returns Always returns NETDEV_TX_OK
  117. */
  118. int cvm_oct_xmit(struct sk_buff *skb, struct net_device *dev)
  119. {
  120. cvmx_pko_command_word0_t pko_command;
  121. union cvmx_buf_ptr hw_buffer;
  122. u64 old_scratch;
  123. u64 old_scratch2;
  124. int qos;
  125. int i;
  126. enum {QUEUE_CORE, QUEUE_HW, QUEUE_DROP} queue_type;
  127. struct octeon_ethernet *priv = netdev_priv(dev);
  128. struct sk_buff *to_free_list;
  129. int skb_to_free;
  130. int buffers_to_free;
  131. u32 total_to_clean;
  132. unsigned long flags;
  133. #if REUSE_SKBUFFS_WITHOUT_FREE
  134. unsigned char *fpa_head;
  135. #endif
  136. /*
  137. * Prefetch the private data structure. It is larger than the
  138. * one cache line.
  139. */
  140. prefetch(priv);
  141. /*
  142. * The check on CVMX_PKO_QUEUES_PER_PORT_* is designed to
  143. * completely remove "qos" in the event neither interface
  144. * supports multiple queues per port.
  145. */
  146. if ((CVMX_PKO_QUEUES_PER_PORT_INTERFACE0 > 1) ||
  147. (CVMX_PKO_QUEUES_PER_PORT_INTERFACE1 > 1)) {
  148. qos = GET_SKBUFF_QOS(skb);
  149. if (qos <= 0)
  150. qos = 0;
  151. else if (qos >= cvmx_pko_get_num_queues(priv->port))
  152. qos = 0;
  153. } else {
  154. qos = 0;
  155. }
  156. if (USE_ASYNC_IOBDMA) {
  157. /* Save scratch in case userspace is using it */
  158. CVMX_SYNCIOBDMA;
  159. old_scratch = cvmx_scratch_read64(CVMX_SCR_SCRATCH);
  160. old_scratch2 = cvmx_scratch_read64(CVMX_SCR_SCRATCH + 8);
  161. /*
  162. * Fetch and increment the number of packets to be
  163. * freed.
  164. */
  165. cvmx_fau_async_fetch_and_add32(CVMX_SCR_SCRATCH + 8,
  166. FAU_NUM_PACKET_BUFFERS_TO_FREE,
  167. 0);
  168. cvmx_fau_async_fetch_and_add32(CVMX_SCR_SCRATCH,
  169. priv->fau + qos * 4,
  170. MAX_SKB_TO_FREE);
  171. }
  172. /*
  173. * We have space for 6 segment pointers, If there will be more
  174. * than that, we must linearize.
  175. */
  176. if (unlikely(skb_shinfo(skb)->nr_frags > 5)) {
  177. if (unlikely(__skb_linearize(skb))) {
  178. queue_type = QUEUE_DROP;
  179. if (USE_ASYNC_IOBDMA) {
  180. /*
  181. * Get the number of skbuffs in use
  182. * by the hardware
  183. */
  184. CVMX_SYNCIOBDMA;
  185. skb_to_free =
  186. cvmx_scratch_read64(CVMX_SCR_SCRATCH);
  187. } else {
  188. /*
  189. * Get the number of skbuffs in use
  190. * by the hardware
  191. */
  192. skb_to_free = cvmx_fau_fetch_and_add32(
  193. priv->fau + qos * 4, MAX_SKB_TO_FREE);
  194. }
  195. skb_to_free = cvm_oct_adjust_skb_to_free(skb_to_free,
  196. priv->fau +
  197. qos * 4);
  198. spin_lock_irqsave(&priv->tx_free_list[qos].lock, flags);
  199. goto skip_xmit;
  200. }
  201. }
  202. /*
  203. * The CN3XXX series of parts has an errata (GMX-401) which
  204. * causes the GMX block to hang if a collision occurs towards
  205. * the end of a <68 byte packet. As a workaround for this, we
  206. * pad packets to be 68 bytes whenever we are in half duplex
  207. * mode. We don't handle the case of having a small packet but
  208. * no room to add the padding. The kernel should always give
  209. * us at least a cache line
  210. */
  211. if ((skb->len < 64) && OCTEON_IS_MODEL(OCTEON_CN3XXX)) {
  212. union cvmx_gmxx_prtx_cfg gmx_prt_cfg;
  213. int interface = INTERFACE(priv->port);
  214. int index = INDEX(priv->port);
  215. if (interface < 2) {
  216. /* We only need to pad packet in half duplex mode */
  217. gmx_prt_cfg.u64 =
  218. cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
  219. if (gmx_prt_cfg.s.duplex == 0) {
  220. int add_bytes = 64 - skb->len;
  221. if ((skb_tail_pointer(skb) + add_bytes) <=
  222. skb_end_pointer(skb))
  223. __skb_put_zero(skb, add_bytes);
  224. }
  225. }
  226. }
  227. /* Build the PKO command */
  228. pko_command.u64 = 0;
  229. #ifdef __LITTLE_ENDIAN
  230. pko_command.s.le = 1;
  231. #endif
  232. pko_command.s.n2 = 1; /* Don't pollute L2 with the outgoing packet */
  233. pko_command.s.segs = 1;
  234. pko_command.s.total_bytes = skb->len;
  235. pko_command.s.size0 = CVMX_FAU_OP_SIZE_32;
  236. pko_command.s.subone0 = 1;
  237. pko_command.s.dontfree = 1;
  238. /* Build the PKO buffer pointer */
  239. hw_buffer.u64 = 0;
  240. if (skb_shinfo(skb)->nr_frags == 0) {
  241. hw_buffer.s.addr = XKPHYS_TO_PHYS((u64)skb->data);
  242. hw_buffer.s.pool = 0;
  243. hw_buffer.s.size = skb->len;
  244. } else {
  245. hw_buffer.s.addr = XKPHYS_TO_PHYS((u64)skb->data);
  246. hw_buffer.s.pool = 0;
  247. hw_buffer.s.size = skb_headlen(skb);
  248. CVM_OCT_SKB_CB(skb)[0] = hw_buffer.u64;
  249. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  250. struct skb_frag_struct *fs = skb_shinfo(skb)->frags + i;
  251. hw_buffer.s.addr = XKPHYS_TO_PHYS(
  252. (u64)(page_address(fs->page.p) +
  253. fs->page_offset));
  254. hw_buffer.s.size = fs->size;
  255. CVM_OCT_SKB_CB(skb)[i + 1] = hw_buffer.u64;
  256. }
  257. hw_buffer.s.addr = XKPHYS_TO_PHYS((u64)CVM_OCT_SKB_CB(skb));
  258. hw_buffer.s.size = skb_shinfo(skb)->nr_frags + 1;
  259. pko_command.s.segs = skb_shinfo(skb)->nr_frags + 1;
  260. pko_command.s.gather = 1;
  261. goto dont_put_skbuff_in_hw;
  262. }
  263. /*
  264. * See if we can put this skb in the FPA pool. Any strange
  265. * behavior from the Linux networking stack will most likely
  266. * be caused by a bug in the following code. If some field is
  267. * in use by the network stack and gets carried over when a
  268. * buffer is reused, bad things may happen. If in doubt and
  269. * you dont need the absolute best performance, disable the
  270. * define REUSE_SKBUFFS_WITHOUT_FREE. The reuse of buffers has
  271. * shown a 25% increase in performance under some loads.
  272. */
  273. #if REUSE_SKBUFFS_WITHOUT_FREE
  274. fpa_head = skb->head + 256 - ((unsigned long)skb->head & 0x7f);
  275. if (unlikely(skb->data < fpa_head)) {
  276. /* TX buffer beginning can't meet FPA alignment constraints */
  277. goto dont_put_skbuff_in_hw;
  278. }
  279. if (unlikely
  280. ((skb_end_pointer(skb) - fpa_head) < CVMX_FPA_PACKET_POOL_SIZE)) {
  281. /* TX buffer isn't large enough for the FPA */
  282. goto dont_put_skbuff_in_hw;
  283. }
  284. if (unlikely(skb_shared(skb))) {
  285. /* TX buffer sharing data with someone else */
  286. goto dont_put_skbuff_in_hw;
  287. }
  288. if (unlikely(skb_cloned(skb))) {
  289. /* TX buffer has been cloned */
  290. goto dont_put_skbuff_in_hw;
  291. }
  292. if (unlikely(skb_header_cloned(skb))) {
  293. /* TX buffer header has been cloned */
  294. goto dont_put_skbuff_in_hw;
  295. }
  296. if (unlikely(skb->destructor)) {
  297. /* TX buffer has a destructor */
  298. goto dont_put_skbuff_in_hw;
  299. }
  300. if (unlikely(skb_shinfo(skb)->nr_frags)) {
  301. /* TX buffer has fragments */
  302. goto dont_put_skbuff_in_hw;
  303. }
  304. if (unlikely
  305. (skb->truesize !=
  306. sizeof(*skb) + skb_end_offset(skb))) {
  307. /* TX buffer truesize has been changed */
  308. goto dont_put_skbuff_in_hw;
  309. }
  310. /*
  311. * We can use this buffer in the FPA. We don't need the FAU
  312. * update anymore
  313. */
  314. pko_command.s.dontfree = 0;
  315. hw_buffer.s.back = ((unsigned long)skb->data >> 7) -
  316. ((unsigned long)fpa_head >> 7);
  317. *(struct sk_buff **)(fpa_head - sizeof(void *)) = skb;
  318. /*
  319. * The skbuff will be reused without ever being freed. We must
  320. * cleanup a bunch of core things.
  321. */
  322. dst_release(skb_dst(skb));
  323. skb_dst_set(skb, NULL);
  324. #ifdef CONFIG_XFRM
  325. secpath_put(skb->sp);
  326. skb->sp = NULL;
  327. #endif
  328. nf_reset(skb);
  329. #ifdef CONFIG_NET_SCHED
  330. skb->tc_index = 0;
  331. skb_reset_tc(skb);
  332. #endif /* CONFIG_NET_SCHED */
  333. #endif /* REUSE_SKBUFFS_WITHOUT_FREE */
  334. dont_put_skbuff_in_hw:
  335. /* Check if we can use the hardware checksumming */
  336. if ((skb->protocol == htons(ETH_P_IP)) &&
  337. (ip_hdr(skb)->version == 4) &&
  338. (ip_hdr(skb)->ihl == 5) &&
  339. ((ip_hdr(skb)->frag_off == 0) ||
  340. (ip_hdr(skb)->frag_off == htons(1 << 14))) &&
  341. ((ip_hdr(skb)->protocol == IPPROTO_TCP) ||
  342. (ip_hdr(skb)->protocol == IPPROTO_UDP))) {
  343. /* Use hardware checksum calc */
  344. pko_command.s.ipoffp1 = skb_network_offset(skb) + 1;
  345. }
  346. if (USE_ASYNC_IOBDMA) {
  347. /* Get the number of skbuffs in use by the hardware */
  348. CVMX_SYNCIOBDMA;
  349. skb_to_free = cvmx_scratch_read64(CVMX_SCR_SCRATCH);
  350. buffers_to_free = cvmx_scratch_read64(CVMX_SCR_SCRATCH + 8);
  351. } else {
  352. /* Get the number of skbuffs in use by the hardware */
  353. skb_to_free = cvmx_fau_fetch_and_add32(priv->fau + qos * 4,
  354. MAX_SKB_TO_FREE);
  355. buffers_to_free =
  356. cvmx_fau_fetch_and_add32(FAU_NUM_PACKET_BUFFERS_TO_FREE, 0);
  357. }
  358. skb_to_free = cvm_oct_adjust_skb_to_free(skb_to_free,
  359. priv->fau + qos * 4);
  360. /*
  361. * If we're sending faster than the receive can free them then
  362. * don't do the HW free.
  363. */
  364. if ((buffers_to_free < -100) && !pko_command.s.dontfree)
  365. pko_command.s.dontfree = 1;
  366. if (pko_command.s.dontfree) {
  367. queue_type = QUEUE_CORE;
  368. pko_command.s.reg0 = priv->fau + qos * 4;
  369. } else {
  370. queue_type = QUEUE_HW;
  371. }
  372. if (USE_ASYNC_IOBDMA)
  373. cvmx_fau_async_fetch_and_add32(
  374. CVMX_SCR_SCRATCH, FAU_TOTAL_TX_TO_CLEAN, 1);
  375. spin_lock_irqsave(&priv->tx_free_list[qos].lock, flags);
  376. /* Drop this packet if we have too many already queued to the HW */
  377. if (unlikely(skb_queue_len(&priv->tx_free_list[qos]) >=
  378. MAX_OUT_QUEUE_DEPTH)) {
  379. if (dev->tx_queue_len != 0) {
  380. /* Drop the lock when notifying the core. */
  381. spin_unlock_irqrestore(&priv->tx_free_list[qos].lock,
  382. flags);
  383. netif_stop_queue(dev);
  384. spin_lock_irqsave(&priv->tx_free_list[qos].lock,
  385. flags);
  386. } else {
  387. /* If not using normal queueing. */
  388. queue_type = QUEUE_DROP;
  389. goto skip_xmit;
  390. }
  391. }
  392. cvmx_pko_send_packet_prepare(priv->port, priv->queue + qos,
  393. CVMX_PKO_LOCK_NONE);
  394. /* Send the packet to the output queue */
  395. if (unlikely(cvmx_pko_send_packet_finish(priv->port,
  396. priv->queue + qos,
  397. pko_command, hw_buffer,
  398. CVMX_PKO_LOCK_NONE))) {
  399. printk_ratelimited("%s: Failed to send the packet\n",
  400. dev->name);
  401. queue_type = QUEUE_DROP;
  402. }
  403. skip_xmit:
  404. to_free_list = NULL;
  405. switch (queue_type) {
  406. case QUEUE_DROP:
  407. skb->next = to_free_list;
  408. to_free_list = skb;
  409. dev->stats.tx_dropped++;
  410. break;
  411. case QUEUE_HW:
  412. cvmx_fau_atomic_add32(FAU_NUM_PACKET_BUFFERS_TO_FREE, -1);
  413. break;
  414. case QUEUE_CORE:
  415. __skb_queue_tail(&priv->tx_free_list[qos], skb);
  416. break;
  417. default:
  418. BUG();
  419. }
  420. while (skb_to_free > 0) {
  421. struct sk_buff *t = __skb_dequeue(&priv->tx_free_list[qos]);
  422. t->next = to_free_list;
  423. to_free_list = t;
  424. skb_to_free--;
  425. }
  426. spin_unlock_irqrestore(&priv->tx_free_list[qos].lock, flags);
  427. /* Do the actual freeing outside of the lock. */
  428. while (to_free_list) {
  429. struct sk_buff *t = to_free_list;
  430. to_free_list = to_free_list->next;
  431. dev_kfree_skb_any(t);
  432. }
  433. if (USE_ASYNC_IOBDMA) {
  434. CVMX_SYNCIOBDMA;
  435. total_to_clean = cvmx_scratch_read64(CVMX_SCR_SCRATCH);
  436. /* Restore the scratch area */
  437. cvmx_scratch_write64(CVMX_SCR_SCRATCH, old_scratch);
  438. cvmx_scratch_write64(CVMX_SCR_SCRATCH + 8, old_scratch2);
  439. } else {
  440. total_to_clean = cvmx_fau_fetch_and_add32(
  441. FAU_TOTAL_TX_TO_CLEAN, 1);
  442. }
  443. if (total_to_clean & 0x3ff) {
  444. /*
  445. * Schedule the cleanup tasklet every 1024 packets for
  446. * the pathological case of high traffic on one port
  447. * delaying clean up of packets on a different port
  448. * that is blocked waiting for the cleanup.
  449. */
  450. tasklet_schedule(&cvm_oct_tx_cleanup_tasklet);
  451. }
  452. cvm_oct_kick_tx_poll_watchdog();
  453. return NETDEV_TX_OK;
  454. }
  455. /**
  456. * cvm_oct_xmit_pow - transmit a packet to the POW
  457. * @skb: Packet to send
  458. * @dev: Device info structure
  459. * Returns Always returns zero
  460. */
  461. int cvm_oct_xmit_pow(struct sk_buff *skb, struct net_device *dev)
  462. {
  463. struct octeon_ethernet *priv = netdev_priv(dev);
  464. void *packet_buffer;
  465. void *copy_location;
  466. /* Get a work queue entry */
  467. cvmx_wqe_t *work = cvmx_fpa_alloc(CVMX_FPA_WQE_POOL);
  468. if (unlikely(!work)) {
  469. printk_ratelimited("%s: Failed to allocate a work queue entry\n",
  470. dev->name);
  471. dev->stats.tx_dropped++;
  472. dev_kfree_skb_any(skb);
  473. return 0;
  474. }
  475. /* Get a packet buffer */
  476. packet_buffer = cvmx_fpa_alloc(CVMX_FPA_PACKET_POOL);
  477. if (unlikely(!packet_buffer)) {
  478. printk_ratelimited("%s: Failed to allocate a packet buffer\n",
  479. dev->name);
  480. cvmx_fpa_free(work, CVMX_FPA_WQE_POOL, 1);
  481. dev->stats.tx_dropped++;
  482. dev_kfree_skb_any(skb);
  483. return 0;
  484. }
  485. /*
  486. * Calculate where we need to copy the data to. We need to
  487. * leave 8 bytes for a next pointer (unused). We also need to
  488. * include any configure skip. Then we need to align the IP
  489. * packet src and dest into the same 64bit word. The below
  490. * calculation may add a little extra, but that doesn't
  491. * hurt.
  492. */
  493. copy_location = packet_buffer + sizeof(u64);
  494. copy_location += ((CVMX_HELPER_FIRST_MBUFF_SKIP + 7) & 0xfff8) + 6;
  495. /*
  496. * We have to copy the packet since whoever processes this
  497. * packet will free it to a hardware pool. We can't use the
  498. * trick of counting outstanding packets like in
  499. * cvm_oct_xmit.
  500. */
  501. memcpy(copy_location, skb->data, skb->len);
  502. /*
  503. * Fill in some of the work queue fields. We may need to add
  504. * more if the software at the other end needs them.
  505. */
  506. if (!OCTEON_IS_MODEL(OCTEON_CN68XX))
  507. work->word0.pip.cn38xx.hw_chksum = skb->csum;
  508. work->word1.len = skb->len;
  509. cvmx_wqe_set_port(work, priv->port);
  510. cvmx_wqe_set_qos(work, priv->port & 0x7);
  511. cvmx_wqe_set_grp(work, pow_send_group);
  512. work->word1.tag_type = CVMX_HELPER_INPUT_TAG_TYPE;
  513. work->word1.tag = pow_send_group; /* FIXME */
  514. /* Default to zero. Sets of zero later are commented out */
  515. work->word2.u64 = 0;
  516. work->word2.s.bufs = 1;
  517. work->packet_ptr.u64 = 0;
  518. work->packet_ptr.s.addr = cvmx_ptr_to_phys(copy_location);
  519. work->packet_ptr.s.pool = CVMX_FPA_PACKET_POOL;
  520. work->packet_ptr.s.size = CVMX_FPA_PACKET_POOL_SIZE;
  521. work->packet_ptr.s.back = (copy_location - packet_buffer) >> 7;
  522. if (skb->protocol == htons(ETH_P_IP)) {
  523. work->word2.s.ip_offset = 14;
  524. #if 0
  525. work->word2.s.vlan_valid = 0; /* FIXME */
  526. work->word2.s.vlan_cfi = 0; /* FIXME */
  527. work->word2.s.vlan_id = 0; /* FIXME */
  528. work->word2.s.dec_ipcomp = 0; /* FIXME */
  529. #endif
  530. work->word2.s.tcp_or_udp =
  531. (ip_hdr(skb)->protocol == IPPROTO_TCP) ||
  532. (ip_hdr(skb)->protocol == IPPROTO_UDP);
  533. #if 0
  534. /* FIXME */
  535. work->word2.s.dec_ipsec = 0;
  536. /* We only support IPv4 right now */
  537. work->word2.s.is_v6 = 0;
  538. /* Hardware would set to zero */
  539. work->word2.s.software = 0;
  540. /* No error, packet is internal */
  541. work->word2.s.L4_error = 0;
  542. #endif
  543. work->word2.s.is_frag = !((ip_hdr(skb)->frag_off == 0) ||
  544. (ip_hdr(skb)->frag_off ==
  545. 1 << 14));
  546. #if 0
  547. /* Assume Linux is sending a good packet */
  548. work->word2.s.IP_exc = 0;
  549. #endif
  550. work->word2.s.is_bcast = (skb->pkt_type == PACKET_BROADCAST);
  551. work->word2.s.is_mcast = (skb->pkt_type == PACKET_MULTICAST);
  552. #if 0
  553. /* This is an IP packet */
  554. work->word2.s.not_IP = 0;
  555. /* No error, packet is internal */
  556. work->word2.s.rcv_error = 0;
  557. /* No error, packet is internal */
  558. work->word2.s.err_code = 0;
  559. #endif
  560. /*
  561. * When copying the data, include 4 bytes of the
  562. * ethernet header to align the same way hardware
  563. * does.
  564. */
  565. memcpy(work->packet_data, skb->data + 10,
  566. sizeof(work->packet_data));
  567. } else {
  568. #if 0
  569. work->word2.snoip.vlan_valid = 0; /* FIXME */
  570. work->word2.snoip.vlan_cfi = 0; /* FIXME */
  571. work->word2.snoip.vlan_id = 0; /* FIXME */
  572. work->word2.snoip.software = 0; /* Hardware would set to zero */
  573. #endif
  574. work->word2.snoip.is_rarp = skb->protocol == htons(ETH_P_RARP);
  575. work->word2.snoip.is_arp = skb->protocol == htons(ETH_P_ARP);
  576. work->word2.snoip.is_bcast =
  577. (skb->pkt_type == PACKET_BROADCAST);
  578. work->word2.snoip.is_mcast =
  579. (skb->pkt_type == PACKET_MULTICAST);
  580. work->word2.snoip.not_IP = 1; /* IP was done up above */
  581. #if 0
  582. /* No error, packet is internal */
  583. work->word2.snoip.rcv_error = 0;
  584. /* No error, packet is internal */
  585. work->word2.snoip.err_code = 0;
  586. #endif
  587. memcpy(work->packet_data, skb->data, sizeof(work->packet_data));
  588. }
  589. /* Submit the packet to the POW */
  590. cvmx_pow_work_submit(work, work->word1.tag, work->word1.tag_type,
  591. cvmx_wqe_get_qos(work), cvmx_wqe_get_grp(work));
  592. dev->stats.tx_packets++;
  593. dev->stats.tx_bytes += skb->len;
  594. dev_consume_skb_any(skb);
  595. return 0;
  596. }
  597. /**
  598. * cvm_oct_tx_shutdown_dev - free all skb that are currently queued for TX.
  599. * @dev: Device being shutdown
  600. *
  601. */
  602. void cvm_oct_tx_shutdown_dev(struct net_device *dev)
  603. {
  604. struct octeon_ethernet *priv = netdev_priv(dev);
  605. unsigned long flags;
  606. int qos;
  607. for (qos = 0; qos < 16; qos++) {
  608. spin_lock_irqsave(&priv->tx_free_list[qos].lock, flags);
  609. while (skb_queue_len(&priv->tx_free_list[qos]))
  610. dev_kfree_skb_any(__skb_dequeue
  611. (&priv->tx_free_list[qos]));
  612. spin_unlock_irqrestore(&priv->tx_free_list[qos].lock, flags);
  613. }
  614. }
  615. static void cvm_oct_tx_do_cleanup(unsigned long arg)
  616. {
  617. int port;
  618. for (port = 0; port < TOTAL_NUMBER_OF_PORTS; port++) {
  619. if (cvm_oct_device[port]) {
  620. struct net_device *dev = cvm_oct_device[port];
  621. cvm_oct_free_tx_skbs(dev);
  622. }
  623. }
  624. }
  625. static irqreturn_t cvm_oct_tx_cleanup_watchdog(int cpl, void *dev_id)
  626. {
  627. /* Disable the interrupt. */
  628. cvmx_write_csr(CVMX_CIU_TIMX(1), 0);
  629. /* Do the work in the tasklet. */
  630. tasklet_schedule(&cvm_oct_tx_cleanup_tasklet);
  631. return IRQ_HANDLED;
  632. }
  633. void cvm_oct_tx_initialize(void)
  634. {
  635. int i;
  636. /* Disable the interrupt. */
  637. cvmx_write_csr(CVMX_CIU_TIMX(1), 0);
  638. /* Register an IRQ handler to receive CIU_TIMX(1) interrupts */
  639. i = request_irq(OCTEON_IRQ_TIMER1,
  640. cvm_oct_tx_cleanup_watchdog, 0,
  641. "Ethernet", cvm_oct_device);
  642. if (i)
  643. panic("Could not acquire Ethernet IRQ %d\n", OCTEON_IRQ_TIMER1);
  644. }
  645. void cvm_oct_tx_shutdown(void)
  646. {
  647. /* Free the interrupt handler */
  648. free_irq(OCTEON_IRQ_TIMER1, cvm_oct_device);
  649. }