ethernet-tx.c 21 KB

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