ethernet-tx.c 20 KB

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