ethernet-mem.c revision 243882
1219019Sgabor/*************************************************************************
2263986StijlCopyright (c) 2003-2007  Cavium Networks (support@cavium.com). All rights
3219019Sgaborreserved.
4219019Sgabor
5219019Sgabor
6219019SgaborRedistribution and use in source and binary forms, with or without
7219019Sgabormodification, are permitted provided that the following conditions are
8219019Sgabormet:
9219019Sgabor
10219019Sgabor    * Redistributions of source code must retain the above copyright
11219019Sgabor      notice, this list of conditions and the following disclaimer.
12219019Sgabor
13219019Sgabor    * Redistributions in binary form must reproduce the above
14219019Sgabor      copyright notice, this list of conditions and the following
15219019Sgabor      disclaimer in the documentation and/or other materials provided
16219019Sgabor      with the distribution.
17219019Sgabor
18219019Sgabor    * Neither the name of Cavium Networks nor the names of
19219019Sgabor      its contributors may be used to endorse or promote products
20219019Sgabor      derived from this software without specific prior written
21219019Sgabor      permission.
22219019Sgabor
23219019SgaborThis Software, including technical data, may be subject to U.S. export  control laws, including the U.S. Export Administration Act and its  associated regulations, and may be subject to export or import  regulations in other countries.
24219019Sgabor
25219019SgaborTO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
26219019SgaborAND WITH ALL FAULTS AND CAVIUM  NETWORKS MAKES NO PROMISES, REPRESENTATIONS OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE  RISK ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
27219019Sgabor
28219019Sgabor*************************************************************************/
29219019Sgabor
30219019Sgabor#include <sys/cdefs.h>
31219019Sgabor__FBSDID("$FreeBSD: head/sys/mips/cavium/octe/ethernet-mem.c 243882 2012-12-05 08:04:20Z glebius $");
32219019Sgabor
33219019Sgabor#include <sys/param.h>
34219019Sgabor#include <sys/systm.h>
35219019Sgabor#include <sys/bus.h>
36219019Sgabor#include <sys/endian.h>
37219019Sgabor#include <sys/kernel.h>
38219019Sgabor#include <sys/mbuf.h>
39219019Sgabor#include <sys/socket.h>
40219019Sgabor
41219019Sgabor#include <net/ethernet.h>
42219019Sgabor#include <net/if.h>
43219019Sgabor
44219019Sgabor#include "wrapper-cvmx-includes.h"
45219019Sgabor#include "ethernet-headers.h"
46219019Sgabor
47219019Sgabor/**
48219019Sgabor * Fill the supplied hardware pool with mbufs
49219019Sgabor *
50219019Sgabor * @param pool     Pool to allocate an mbuf for
51219019Sgabor * @param size     Size of the buffer needed for the pool
52219019Sgabor * @param elements Number of buffers to allocate
53219019Sgabor */
54219019Sgaborint cvm_oct_mem_fill_fpa(int pool, int size, int elements)
55219019Sgabor{
56219019Sgabor	int freed = elements;
57219019Sgabor	while (freed) {
58219019Sgabor		KASSERT(size <= MCLBYTES - 128, ("mbuf clusters are too small"));
59219019Sgabor
60219019Sgabor		struct mbuf *m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
61219019Sgabor		if (__predict_false(m == NULL)) {
62219019Sgabor			printf("Failed to allocate mbuf for hardware pool %d\n", pool);
63219019Sgabor			break;
64219019Sgabor		}
65219019Sgabor
66219019Sgabor		m->m_data += 128 - (((uintptr_t)m->m_data) & 0x7f);
67219019Sgabor		*(struct mbuf **)(m->m_data - sizeof(void *)) = m;
68219019Sgabor		cvmx_fpa_free(m->m_data, pool, DONT_WRITEBACK(size/128));
69219019Sgabor		freed--;
70219019Sgabor	}
71252584Speter	return (elements - freed);
72252584Speter}
73219019Sgabor
74219019Sgabor
75219019Sgabor/**
76219019Sgabor * Free the supplied hardware pool of mbufs
77252584Speter *
78219019Sgabor * @param pool     Pool to allocate an mbuf for
79219019Sgabor * @param size     Size of the buffer needed for the pool
80219019Sgabor * @param elements Number of buffers to allocate
81219019Sgabor */
82219019Sgaborvoid cvm_oct_mem_empty_fpa(int pool, int size, int elements)
83219019Sgabor{
84219019Sgabor	char *memory;
85219019Sgabor
86219019Sgabor	do {
87219019Sgabor		memory = cvmx_fpa_alloc(pool);
88252584Speter		if (memory) {
89219019Sgabor			struct mbuf *m = *(struct mbuf **)(memory - sizeof(void *));
90219019Sgabor			elements--;
91219019Sgabor			m_freem(m);
92219019Sgabor		}
93219019Sgabor	} while (memory);
94219019Sgabor
95219019Sgabor	if (elements < 0)
96219019Sgabor		printf("Warning: Freeing of pool %u had too many mbufs (%d)\n", pool, elements);
97219019Sgabor	else if (elements > 0)
98219019Sgabor		printf("Warning: Freeing of pool %u is missing %d mbufs\n", pool, elements);
99219019Sgabor}
100219019Sgabor