ethernet-rx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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/cache.h>
  13. #include <linux/cpumask.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/ip.h>
  17. #include <linux/string.h>
  18. #include <linux/prefetch.h>
  19. #include <linux/ratelimit.h>
  20. #include <linux/smp.h>
  21. #include <linux/interrupt.h>
  22. #include <net/dst.h>
  23. #ifdef CONFIG_XFRM
  24. #include <linux/xfrm.h>
  25. #include <net/xfrm.h>
  26. #endif /* CONFIG_XFRM */
  27. #include <linux/atomic.h>
  28. #include <asm/octeon/octeon.h>
  29. #include "ethernet-defines.h"
  30. #include "ethernet-mem.h"
  31. #include "ethernet-rx.h"
  32. #include "octeon-ethernet.h"
  33. #include "ethernet-util.h"
  34. #include <asm/octeon/cvmx-helper.h>
  35. #include <asm/octeon/cvmx-wqe.h>
  36. #include <asm/octeon/cvmx-fau.h>
  37. #include <asm/octeon/cvmx-pow.h>
  38. #include <asm/octeon/cvmx-pip.h>
  39. #include <asm/octeon/cvmx-scratch.h>
  40. #include <asm/octeon/cvmx-gmxx-defs.h>
  41. static struct napi_struct cvm_oct_napi;
  42. /**
  43. * cvm_oct_do_interrupt - interrupt handler.
  44. * @cpl: Interrupt number. Unused
  45. * @dev_id: Cookie to identify the device. Unused
  46. *
  47. * The interrupt occurs whenever the POW has packets in our group.
  48. *
  49. */
  50. static irqreturn_t cvm_oct_do_interrupt(int cpl, void *dev_id)
  51. {
  52. /* Disable the IRQ and start napi_poll. */
  53. disable_irq_nosync(OCTEON_IRQ_WORKQ0 + pow_receive_group);
  54. napi_schedule(&cvm_oct_napi);
  55. return IRQ_HANDLED;
  56. }
  57. /**
  58. * cvm_oct_check_rcv_error - process receive errors
  59. * @work: Work queue entry pointing to the packet.
  60. *
  61. * Returns Non-zero if the packet can be dropped, zero otherwise.
  62. */
  63. static inline int cvm_oct_check_rcv_error(cvmx_wqe_t *work)
  64. {
  65. int port;
  66. if (octeon_has_feature(OCTEON_FEATURE_PKND))
  67. port = work->word0.pip.cn68xx.pknd;
  68. else
  69. port = work->word1.cn38xx.ipprt;
  70. if ((work->word2.snoip.err_code == 10) && (work->word1.len <= 64)) {
  71. /*
  72. * Ignore length errors on min size packets. Some
  73. * equipment incorrectly pads packets to 64+4FCS
  74. * instead of 60+4FCS. Note these packets still get
  75. * counted as frame errors.
  76. */
  77. } else if (work->word2.snoip.err_code == 5 ||
  78. work->word2.snoip.err_code == 7) {
  79. /*
  80. * We received a packet with either an alignment error
  81. * or a FCS error. This may be signalling that we are
  82. * running 10Mbps with GMXX_RXX_FRM_CTL[PRE_CHK]
  83. * off. If this is the case we need to parse the
  84. * packet to determine if we can remove a non spec
  85. * preamble and generate a correct packet.
  86. */
  87. int interface = cvmx_helper_get_interface_num(port);
  88. int index = cvmx_helper_get_interface_index_num(port);
  89. union cvmx_gmxx_rxx_frm_ctl gmxx_rxx_frm_ctl;
  90. gmxx_rxx_frm_ctl.u64 =
  91. cvmx_read_csr(CVMX_GMXX_RXX_FRM_CTL(index, interface));
  92. if (gmxx_rxx_frm_ctl.s.pre_chk == 0) {
  93. u8 *ptr =
  94. cvmx_phys_to_ptr(work->packet_ptr.s.addr);
  95. int i = 0;
  96. while (i < work->word1.len - 1) {
  97. if (*ptr != 0x55)
  98. break;
  99. ptr++;
  100. i++;
  101. }
  102. if (*ptr == 0xd5) {
  103. /*
  104. printk_ratelimited("Port %d received 0xd5 preamble\n",
  105. port);
  106. */
  107. work->packet_ptr.s.addr += i + 1;
  108. work->word1.len -= i + 5;
  109. } else if ((*ptr & 0xf) == 0xd) {
  110. /*
  111. printk_ratelimited("Port %d received 0x?d preamble\n",
  112. port);
  113. */
  114. work->packet_ptr.s.addr += i;
  115. work->word1.len -= i + 4;
  116. for (i = 0; i < work->word1.len; i++) {
  117. *ptr =
  118. ((*ptr & 0xf0) >> 4) |
  119. ((*(ptr + 1) & 0xf) << 4);
  120. ptr++;
  121. }
  122. } else {
  123. printk_ratelimited("Port %d unknown preamble, packet dropped\n",
  124. port);
  125. /*
  126. cvmx_helper_dump_packet(work);
  127. */
  128. cvm_oct_free_work(work);
  129. return 1;
  130. }
  131. }
  132. } else {
  133. printk_ratelimited("Port %d receive error code %d, packet dropped\n",
  134. port, work->word2.snoip.err_code);
  135. cvm_oct_free_work(work);
  136. return 1;
  137. }
  138. return 0;
  139. }
  140. /**
  141. * cvm_oct_napi_poll - the NAPI poll function.
  142. * @napi: The NAPI instance, or null if called from cvm_oct_poll_controller
  143. * @budget: Maximum number of packets to receive.
  144. *
  145. * Returns the number of packets processed.
  146. */
  147. static int cvm_oct_napi_poll(struct napi_struct *napi, int budget)
  148. {
  149. const int coreid = cvmx_get_core_num();
  150. u64 old_group_mask;
  151. u64 old_scratch;
  152. int rx_count = 0;
  153. int did_work_request = 0;
  154. int packet_not_copied;
  155. /* Prefetch cvm_oct_device since we know we need it soon */
  156. prefetch(cvm_oct_device);
  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. }
  162. /* Only allow work for our group (and preserve priorities) */
  163. if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
  164. old_group_mask = cvmx_read_csr(CVMX_SSO_PPX_GRP_MSK(coreid));
  165. cvmx_write_csr(CVMX_SSO_PPX_GRP_MSK(coreid),
  166. 1ull << pow_receive_group);
  167. cvmx_read_csr(CVMX_SSO_PPX_GRP_MSK(coreid)); /* Flush */
  168. } else {
  169. old_group_mask = cvmx_read_csr(CVMX_POW_PP_GRP_MSKX(coreid));
  170. cvmx_write_csr(CVMX_POW_PP_GRP_MSKX(coreid),
  171. (old_group_mask & ~0xFFFFull) | 1 << pow_receive_group);
  172. }
  173. if (USE_ASYNC_IOBDMA) {
  174. cvmx_pow_work_request_async(CVMX_SCR_SCRATCH, CVMX_POW_NO_WAIT);
  175. did_work_request = 1;
  176. }
  177. while (rx_count < budget) {
  178. struct sk_buff *skb = NULL;
  179. struct sk_buff **pskb = NULL;
  180. int skb_in_hw;
  181. cvmx_wqe_t *work;
  182. int port;
  183. if (USE_ASYNC_IOBDMA && did_work_request)
  184. work = cvmx_pow_work_response_async(CVMX_SCR_SCRATCH);
  185. else
  186. work = cvmx_pow_work_request_sync(CVMX_POW_NO_WAIT);
  187. prefetch(work);
  188. did_work_request = 0;
  189. if (work == NULL) {
  190. if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
  191. cvmx_write_csr(CVMX_SSO_WQ_IQ_DIS,
  192. 1ull << pow_receive_group);
  193. cvmx_write_csr(CVMX_SSO_WQ_INT,
  194. 1ull << pow_receive_group);
  195. } else {
  196. union cvmx_pow_wq_int wq_int;
  197. wq_int.u64 = 0;
  198. wq_int.s.iq_dis = 1 << pow_receive_group;
  199. wq_int.s.wq_int = 1 << pow_receive_group;
  200. cvmx_write_csr(CVMX_POW_WQ_INT, wq_int.u64);
  201. }
  202. break;
  203. }
  204. pskb = (struct sk_buff **)(cvm_oct_get_buffer_ptr(work->packet_ptr) -
  205. sizeof(void *));
  206. prefetch(pskb);
  207. if (USE_ASYNC_IOBDMA && rx_count < (budget - 1)) {
  208. cvmx_pow_work_request_async_nocheck(CVMX_SCR_SCRATCH,
  209. CVMX_POW_NO_WAIT);
  210. did_work_request = 1;
  211. }
  212. rx_count++;
  213. skb_in_hw = work->word2.s.bufs == 1;
  214. if (likely(skb_in_hw)) {
  215. skb = *pskb;
  216. prefetch(&skb->head);
  217. prefetch(&skb->len);
  218. }
  219. if (octeon_has_feature(OCTEON_FEATURE_PKND))
  220. port = work->word0.pip.cn68xx.pknd;
  221. else
  222. port = work->word1.cn38xx.ipprt;
  223. prefetch(cvm_oct_device[port]);
  224. /* Immediately throw away all packets with receive errors */
  225. if (unlikely(work->word2.snoip.rcv_error)) {
  226. if (cvm_oct_check_rcv_error(work))
  227. continue;
  228. }
  229. /*
  230. * We can only use the zero copy path if skbuffs are
  231. * in the FPA pool and the packet fits in a single
  232. * buffer.
  233. */
  234. if (likely(skb_in_hw)) {
  235. skb->data = skb->head + work->packet_ptr.s.addr -
  236. cvmx_ptr_to_phys(skb->head);
  237. prefetch(skb->data);
  238. skb->len = work->word1.len;
  239. skb_set_tail_pointer(skb, skb->len);
  240. packet_not_copied = 1;
  241. } else {
  242. /*
  243. * We have to copy the packet. First allocate
  244. * an skbuff for it.
  245. */
  246. skb = dev_alloc_skb(work->word1.len);
  247. if (!skb) {
  248. cvm_oct_free_work(work);
  249. continue;
  250. }
  251. /*
  252. * Check if we've received a packet that was
  253. * entirely stored in the work entry.
  254. */
  255. if (unlikely(work->word2.s.bufs == 0)) {
  256. u8 *ptr = work->packet_data;
  257. if (likely(!work->word2.s.not_IP)) {
  258. /*
  259. * The beginning of the packet
  260. * moves for IP packets.
  261. */
  262. if (work->word2.s.is_v6)
  263. ptr += 2;
  264. else
  265. ptr += 6;
  266. }
  267. memcpy(skb_put(skb, work->word1.len), ptr,
  268. work->word1.len);
  269. /* No packet buffers to free */
  270. } else {
  271. int segments = work->word2.s.bufs;
  272. union cvmx_buf_ptr segment_ptr =
  273. work->packet_ptr;
  274. int len = work->word1.len;
  275. while (segments--) {
  276. union cvmx_buf_ptr next_ptr =
  277. *(union cvmx_buf_ptr *)cvmx_phys_to_ptr(segment_ptr.s.addr - 8);
  278. /*
  279. * Octeon Errata PKI-100: The segment size is
  280. * wrong. Until it is fixed, calculate the
  281. * segment size based on the packet pool
  282. * buffer size. When it is fixed, the
  283. * following line should be replaced with this
  284. * one: int segment_size =
  285. * segment_ptr.s.size;
  286. */
  287. int segment_size =
  288. CVMX_FPA_PACKET_POOL_SIZE -
  289. (segment_ptr.s.addr -
  290. (((segment_ptr.s.addr >> 7) -
  291. segment_ptr.s.back) << 7));
  292. /*
  293. * Don't copy more than what
  294. * is left in the packet.
  295. */
  296. if (segment_size > len)
  297. segment_size = len;
  298. /* Copy the data into the packet */
  299. memcpy(skb_put(skb, segment_size),
  300. cvmx_phys_to_ptr(segment_ptr.s.addr),
  301. segment_size);
  302. len -= segment_size;
  303. segment_ptr = next_ptr;
  304. }
  305. }
  306. packet_not_copied = 0;
  307. }
  308. if (likely((port < TOTAL_NUMBER_OF_PORTS) &&
  309. cvm_oct_device[port])) {
  310. struct net_device *dev = cvm_oct_device[port];
  311. struct octeon_ethernet *priv = netdev_priv(dev);
  312. /*
  313. * Only accept packets for devices that are
  314. * currently up.
  315. */
  316. if (likely(dev->flags & IFF_UP)) {
  317. skb->protocol = eth_type_trans(skb, dev);
  318. skb->dev = dev;
  319. if (unlikely(work->word2.s.not_IP ||
  320. work->word2.s.IP_exc ||
  321. work->word2.s.L4_error ||
  322. !work->word2.s.tcp_or_udp))
  323. skb->ip_summed = CHECKSUM_NONE;
  324. else
  325. skb->ip_summed = CHECKSUM_UNNECESSARY;
  326. /* Increment RX stats for virtual ports */
  327. if (port >= CVMX_PIP_NUM_INPUT_PORTS) {
  328. #ifdef CONFIG_64BIT
  329. atomic64_add(1,
  330. (atomic64_t *)&priv->stats.rx_packets);
  331. atomic64_add(skb->len,
  332. (atomic64_t *)&priv->stats.rx_bytes);
  333. #else
  334. atomic_add(1,
  335. (atomic_t *)&priv->stats.rx_packets);
  336. atomic_add(skb->len,
  337. (atomic_t *)&priv->stats.rx_bytes);
  338. #endif
  339. }
  340. netif_receive_skb(skb);
  341. } else {
  342. /* Drop any packet received for a device that isn't up */
  343. /*
  344. printk_ratelimited("%s: Device not up, packet dropped\n",
  345. dev->name);
  346. */
  347. #ifdef CONFIG_64BIT
  348. atomic64_add(1,
  349. (atomic64_t *)&priv->stats.rx_dropped);
  350. #else
  351. atomic_add(1,
  352. (atomic_t *)&priv->stats.rx_dropped);
  353. #endif
  354. dev_kfree_skb_irq(skb);
  355. }
  356. } else {
  357. /*
  358. * Drop any packet received for a device that
  359. * doesn't exist.
  360. */
  361. printk_ratelimited("Port %d not controlled by Linux, packet dropped\n",
  362. port);
  363. dev_kfree_skb_irq(skb);
  364. }
  365. /*
  366. * Check to see if the skbuff and work share the same
  367. * packet buffer.
  368. */
  369. if (likely(packet_not_copied)) {
  370. /*
  371. * This buffer needs to be replaced, increment
  372. * the number of buffers we need to free by
  373. * one.
  374. */
  375. cvmx_fau_atomic_add32(FAU_NUM_PACKET_BUFFERS_TO_FREE,
  376. 1);
  377. cvmx_fpa_free(work, CVMX_FPA_WQE_POOL, 1);
  378. } else {
  379. cvm_oct_free_work(work);
  380. }
  381. }
  382. /* Restore the original POW group mask */
  383. if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
  384. cvmx_write_csr(CVMX_SSO_PPX_GRP_MSK(coreid), old_group_mask);
  385. cvmx_read_csr(CVMX_SSO_PPX_GRP_MSK(coreid)); /* Flush */
  386. } else {
  387. cvmx_write_csr(CVMX_POW_PP_GRP_MSKX(coreid), old_group_mask);
  388. }
  389. if (USE_ASYNC_IOBDMA) {
  390. /* Restore the scratch area */
  391. cvmx_scratch_write64(CVMX_SCR_SCRATCH, old_scratch);
  392. }
  393. cvm_oct_rx_refill_pool(0);
  394. if (rx_count < budget && napi != NULL) {
  395. /* No more work */
  396. napi_complete(napi);
  397. enable_irq(OCTEON_IRQ_WORKQ0 + pow_receive_group);
  398. }
  399. return rx_count;
  400. }
  401. #ifdef CONFIG_NET_POLL_CONTROLLER
  402. /**
  403. * cvm_oct_poll_controller - poll for receive packets
  404. * device.
  405. *
  406. * @dev: Device to poll. Unused
  407. */
  408. void cvm_oct_poll_controller(struct net_device *dev)
  409. {
  410. cvm_oct_napi_poll(NULL, 16);
  411. }
  412. #endif
  413. void cvm_oct_rx_initialize(void)
  414. {
  415. int i;
  416. struct net_device *dev_for_napi = NULL;
  417. for (i = 0; i < TOTAL_NUMBER_OF_PORTS; i++) {
  418. if (cvm_oct_device[i]) {
  419. dev_for_napi = cvm_oct_device[i];
  420. break;
  421. }
  422. }
  423. if (NULL == dev_for_napi)
  424. panic("No net_devices were allocated.");
  425. netif_napi_add(dev_for_napi, &cvm_oct_napi, cvm_oct_napi_poll,
  426. rx_napi_weight);
  427. napi_enable(&cvm_oct_napi);
  428. /* Register an IRQ handler to receive POW interrupts */
  429. i = request_irq(OCTEON_IRQ_WORKQ0 + pow_receive_group,
  430. cvm_oct_do_interrupt, 0, "Ethernet", cvm_oct_device);
  431. if (i)
  432. panic("Could not acquire Ethernet IRQ %d\n",
  433. OCTEON_IRQ_WORKQ0 + pow_receive_group);
  434. disable_irq_nosync(OCTEON_IRQ_WORKQ0 + pow_receive_group);
  435. /* Enable POW interrupt when our port has at least one packet */
  436. if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
  437. union cvmx_sso_wq_int_thrx int_thr;
  438. union cvmx_pow_wq_int_pc int_pc;
  439. int_thr.u64 = 0;
  440. int_thr.s.tc_en = 1;
  441. int_thr.s.tc_thr = 1;
  442. cvmx_write_csr(CVMX_SSO_WQ_INT_THRX(pow_receive_group),
  443. int_thr.u64);
  444. int_pc.u64 = 0;
  445. int_pc.s.pc_thr = 5;
  446. cvmx_write_csr(CVMX_SSO_WQ_INT_PC, int_pc.u64);
  447. } else {
  448. union cvmx_pow_wq_int_thrx int_thr;
  449. union cvmx_pow_wq_int_pc int_pc;
  450. int_thr.u64 = 0;
  451. int_thr.s.tc_en = 1;
  452. int_thr.s.tc_thr = 1;
  453. cvmx_write_csr(CVMX_POW_WQ_INT_THRX(pow_receive_group),
  454. int_thr.u64);
  455. int_pc.u64 = 0;
  456. int_pc.s.pc_thr = 5;
  457. cvmx_write_csr(CVMX_POW_WQ_INT_PC, int_pc.u64);
  458. }
  459. /* Schedule NAPI now. This will indirectly enable the interrupt. */
  460. napi_schedule(&cvm_oct_napi);
  461. }
  462. void cvm_oct_rx_shutdown(void)
  463. {
  464. netif_napi_del(&cvm_oct_napi);
  465. }