ethernet-tx.c 21 KB

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