ethernet-tx.c 21 KB

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