1/**************************************************************************
2
3Copyright (c) 2007, Chelsio Inc.
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
8
9 1. Redistributions of source code must retain the above copyright notice,
10    this list of conditions and the following disclaimer.
11
12 2. Neither the name of the Chelsio Corporation nor the names of its
13    contributors may be used to endorse or promote products derived from
14    this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26POSSIBILITY OF SUCH DAMAGE.
27
28***************************************************************************/
29
30#include <sys/cdefs.h>
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/kernel.h>
35#include <sys/conf.h>
36#include <sys/bus.h>
37#include <sys/ioccom.h>
38#include <sys/mbuf.h>
39#include <sys/socket.h>
40#include <sys/sockio.h>
41#include <sys/sysctl.h>
42#include <sys/queue.h>
43
44#include <net/bpf.h>
45#include <net/if.h>
46#include <net/if_arp.h>
47#include <net/if_dl.h>
48#include <net/if_media.h>
49#include <net/if_types.h>
50
51#include <netinet/in_systm.h>
52#include <netinet/in.h>
53#include <netinet/ip.h>
54#include <netinet/tcp.h>
55#include <netinet/udp.h>
56
57#include <dev/pci/pcireg.h>
58#include <dev/pci/pcivar.h>
59
60#include <dev/pci/cxgb/cxgb_include.h>
61#include <altq/altq_conf.h>
62
63int cxgb_initialized = FALSE;
64
65int atomic_fetchadd_int(volatile int *p, int v)
66{
67    int tmp = *p;
68    *p += v;
69    return (tmp);
70}
71
72#if 0
73int atomic_add_int(volatile int *p, int v)
74{
75    return (*p += v);
76}
77#endif
78
79int atomic_load_acq_int(volatile int *p)
80{
81    return (*p);
82}
83
84void atomic_store_rel_int(volatile int *p, int v)
85{
86    *p = v;
87}
88
89u_short in_cksum_hdr(struct ip *ih)
90{
91	u_long sum = 0;
92	u_short *p = (u_short *)ih;
93	int i;
94
95        i = ih->ip_hl*2;
96	while (i--)
97		sum += *p++;
98
99	if (sum > 0xffff)
100		sum -= 0xffff;
101
102	return (~sum);
103}
104
105void m_cljset(struct mbuf *m, void *cl, int type)
106{
107    MEXTADD(m, cl, m->m_len, M_DEVBUF, NULL, NULL);
108}
109
110int
111_m_explode(struct mbuf *m)
112{
113        int i, offset, type, first, len;
114        uint8_t *cl;
115        struct mbuf *m0, *head = NULL;
116        struct mbuf_vec *mv;
117
118#ifdef INVARIANTS
119        len = m->m_len;
120        m0 = m->m_next;
121        while (m0) {
122                KASSERT((m0->m_flags & M_PKTHDR) == 0,
123                    ("pkthdr set on intermediate mbuf - pre"));
124                len += m0->m_len;
125                m0 = m0->m_next;
126
127        }
128        if (len != m->m_pkthdr.len)
129                panic("at start len=%d pktlen=%d", len, m->m_pkthdr.len);
130#endif
131        mv = (struct mbuf_vec *)((m)->m_pktdat);
132        first = mv->mv_first;
133        for (i = mv->mv_count + first - 1; i > first; i--) {
134                type = mbuf_vec_get_type(mv, i);
135                cl = mv->mv_vec[i].mi_base;
136                offset = mv->mv_vec[i].mi_offset;
137                len = mv->mv_vec[i].mi_len;
138#if 0
139                if (__predict_false(type == EXT_MBUF)) {
140                        m0 = (struct mbuf *)cl;
141                        KASSERT((m0->m_flags & M_EXT) == 0);
142                        m0->m_len = len;
143                        m0->m_data = cl + offset;
144                        goto skip_cluster;
145
146                } else
147#endif
148		if ((m0 = m_get(M_NOWAIT, MT_DATA)) == NULL) {
149                        /*
150                         * Check for extra memory leaks
151                         */
152                        m_freem(head);
153                        return (ENOMEM);
154                }
155                m0->m_flags = 0;
156
157                m0->m_len = mv->mv_vec[i].mi_len;
158                m_cljset(m0, (uint8_t *)cl, type);
159                if (offset)
160                        m_adj(m0, offset);
161//        skip_cluster:
162                m0->m_next = head;
163                m->m_len -= m0->m_len;
164                head = m0;
165        }
166        offset = mv->mv_vec[first].mi_offset;
167        cl = mv->mv_vec[first].mi_base;
168        type = mbuf_vec_get_type(mv, first);
169        m->m_flags &= ~(M_IOVEC);
170        m_cljset(m, cl, type);
171        if (offset)
172                m_adj(m, offset);
173        m->m_next = head;
174        head = m;
175        M_SANITY(m, 0);
176
177        return (0);
178}
179
180/*
181 * Allocate a chunk of memory using kmalloc or, if that fails, vmalloc.
182 * The allocated memory is cleared.
183 */
184void *
185cxgb_alloc_mem(unsigned long size)
186{
187    return malloc(size, M_DEVBUF, M_ZERO);
188}
189
190/*
191 * Free memory allocated through t3_alloc_mem().
192 */
193void
194cxgb_free_mem(void *addr)
195{
196    free(addr, M_DEVBUF);
197}
198
199void pci_enable_busmaster(device_t dev)
200{
201    adapter_t *sc = (adapter_t *)dev;
202    uint32_t reg;
203
204    t3_os_pci_read_config_4(sc, PCI_COMMAND_STATUS_REG, &reg);
205    reg |= PCI_COMMAND_MASTER_ENABLE;
206    t3_os_pci_write_config_4(sc, PCI_COMMAND_STATUS_REG, reg);
207}
208