186144Stmm/*-
286144Stmm * Copyright (c) 1990 The Regents of the University of California.
386144Stmm * All rights reserved.
486144Stmm *
586144Stmm * Redistribution and use in source and binary forms, with or without
686144Stmm * modification, are permitted provided that the following conditions
786144Stmm * are met:
886144Stmm * 1. Redistributions of source code must retain the above copyright
986144Stmm *    notice, this list of conditions and the following disclaimer.
1086144Stmm * 2. Redistributions in binary form must reproduce the above copyright
1186144Stmm *    notice, this list of conditions and the following disclaimer in the
1286144Stmm *    documentation and/or other materials provided with the distribution.
13286336Semaste * 3. Neither the name of the University nor the names of its contributors
1486144Stmm *    may be used to endorse or promote products derived from this software
1586144Stmm *    without specific prior written permission.
1686144Stmm *
1786144Stmm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1886144Stmm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1986144Stmm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2086144Stmm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2186144Stmm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2286144Stmm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2386144Stmm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2486144Stmm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2586144Stmm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2686144Stmm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2786144Stmm * SUCH DAMAGE.
2886144Stmm */
2986144Stmm/*-
3086144Stmm * Copyright (c) 2001 by Thomas Moestl <tmm@FreeBSD.org>.
3186144Stmm * All rights reserved.
3286144Stmm *
3386144Stmm * Redistribution and use in source and binary forms, with or without
3486144Stmm * modification, are permitted provided that the following conditions
3586144Stmm * are met:
3686144Stmm * 1. Redistributions of source code must retain the above copyright
3786144Stmm *    notice, this list of conditions and the following disclaimer.
3886144Stmm * 2. Redistributions in binary form must reproduce the above copyright
3986144Stmm *    notice, this list of conditions and the following disclaimer in the
4086144Stmm *    documentation and/or other materials provided with the distribution.
4186144Stmm *
4286144Stmm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
4386144Stmm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
4486144Stmm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
4586144Stmm * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
4686144Stmm * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
4786144Stmm * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
4886144Stmm * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
4986144Stmm * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
5086144Stmm * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
5186144Stmm * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5286144Stmm *
5386144Stmm *	from tahoe:	in_cksum.c	1.2	86/01/05
5486144Stmm *	from:		@(#)in_cksum.c	1.3 (Berkeley) 1/19/91
5586144Stmm *	from: Id: in_cksum.c,v 1.8 1995/12/03 18:35:19 bde Exp
5686144Stmm *	from: FreeBSD: src/sys/alpha/include/in_cksum.h,v 1.5 2000/05/06
5786144Stmm *
5886144Stmm * $FreeBSD$
5986144Stmm */
6086144Stmm
6186144Stmm#ifndef _MACHINE_IN_CKSUM_H_
6286144Stmm#define	_MACHINE_IN_CKSUM_H_	1
6386144Stmm
6486144Stmm#include <sys/cdefs.h>
6586144Stmm
66180011Smarius#define	in_cksum(m, len)	in_cksum_skip(m, len, 0)
6786144Stmm
68235941Sbz#if defined(IPVERSION) && (IPVERSION == 4)
6986144Stmmstatic __inline void
7086144Stmmin_cksum_update(struct ip *ip)
7186144Stmm{
7286144Stmm	int __tmp;
7386144Stmm
7486144Stmm	__tmp = (int)ip->ip_sum + 1;
7586144Stmm	ip->ip_sum = __tmp + (__tmp >> 16);
7686144Stmm}
77235941Sbz#endif
7886144Stmm
7986144Stmmstatic __inline u_short
8086144Stmmin_addword(u_short sum, u_short b)
8186144Stmm{
8286144Stmm	u_long __ret, __tmp;
8386144Stmm
84180297Smarius	__asm(
8586144Stmm	    "sll %2, 16, %0\n"
8686144Stmm	    "sll %3, 16, %1\n"
8786144Stmm	    "addcc %0, %1, %0\n"
8886144Stmm	    "srl %0, 16, %0\n"
8986144Stmm	    "addc %0, 0, %0\n"
90180073Smarius	    : "=&r" (__ret), "=&r" (__tmp) : "r" (sum), "r" (b) : "cc");
9186144Stmm	return (__ret);
9286144Stmm}
9386144Stmm
9486144Stmmstatic __inline u_short
9586144Stmmin_pseudo(u_int sum, u_int b, u_int c)
9686144Stmm{
9786144Stmm	u_long __tmp;
98180011Smarius
99180297Smarius	__asm(
10086144Stmm	    "addcc %0, %3, %0\n"
10186144Stmm	    "addccc %0, %4, %0\n"
10286144Stmm	    "addc %0, 0, %0\n"
10386144Stmm	    "sll %0, 16, %1\n"
10486144Stmm	    "addcc %0, %1, %0\n"
10586144Stmm	    "srl %0, 16, %0\n"
10686144Stmm	    "addc %0, 0, %0\n"
107180073Smarius	    : "=r" (sum), "=&r" (__tmp) : "0" (sum), "r" (b), "r" (c) : "cc");
10886144Stmm	return (sum);
10986144Stmm}
11086144Stmm
111235941Sbz#if defined(IPVERSION) && (IPVERSION == 4)
11286144Stmmstatic __inline u_int
11386144Stmmin_cksum_hdr(struct ip *ip)
11486144Stmm{
11586144Stmm	u_long __ret, __tmp1, __tmp2, __tmp3, __tmp4;
11686144Stmm
11786144Stmm	/*
118180297Smarius	 * Use 32-bit memory accesses and additions - addition with carry only
119154256Smarius	 * works for 32 bits, and fixing up alignment issues for 64 is probably
12086144Stmm	 * more trouble than it's worth.
12186144Stmm	 * This may read outside of the ip header, but does not cross a page
12286144Stmm	 * boundary in doing so, so that should be OK.
12386144Stmm	 * Actually, this specialized implementation might be overkill - using
12486144Stmm	 * a generic implementation for both in_cksum_skip and in_cksum_hdr
12586144Stmm	 * should not be too much more expensive.
12686144Stmm	 */
127180011Smarius#define	__LD_ADD(addr, tmp, sum, offs, mod)				\
128180011Smarius    "lduw [" #addr " + " #offs "], " #tmp "\n"				\
12986144Stmm    "add" # mod " " #sum ", " #tmp ", " #sum "\n"
13086144Stmm
131180297Smarius	__asm(
13286144Stmm	    "and %5, 3, %3\n"
13386144Stmm	    "andn %5, 3, %1\n"
13486144Stmm	    "brz,pt %3, 0f\n"
13586144Stmm	    " lduw [%1], %0\n"
13686144Stmm	    "mov 4, %4\n"
13786144Stmm	    "sub %4, %3, %4\n"
13886144Stmm	    "sll %4, 3, %4\n"		/* fix up unaligned buffers */
13986144Stmm	    "mov 1, %2\n"
14086144Stmm	    "sll %2, %4, %4\n"
14186144Stmm	    "sub %4, 1, %4\n"
14286144Stmm	    "lduw [%1 + 20], %2\n"
14386144Stmm	    "andn %2, %4, %2\n"
14486144Stmm	    "and %0, %4, %0\n"
14586144Stmm	    "or %0, %2, %0\n"
14686144Stmm	    "0:\n"
14786144Stmm	    __LD_ADD(%1, %2, %0, 4, cc)
14886144Stmm	    __LD_ADD(%1, %2, %0, 8, ccc)
14986144Stmm	    __LD_ADD(%1, %2, %0, 12, ccc)
15086144Stmm	    __LD_ADD(%1, %2, %0, 16, ccc)
15186144Stmm	    "addc %0, 0, %0\n"		/* reduce */
15286144Stmm	    "1:\n"
15386144Stmm	    "sll %0, 16, %2\n"
15486144Stmm	    "addcc %0, %2, %0\n"
15586144Stmm	    "srl %0, 16, %0\n"
15686144Stmm	    "addc %0, 0, %0\n"
15786144Stmm	    "andcc %3, 1, %3\n"		/* need to byte-swap? */
15886144Stmm	    "clr %3\n"
15986144Stmm	    "bne,a,pn %%xcc, 1b\n"
16086144Stmm	    " sll %0, 8, %0\n"
16186144Stmm	    "not %0\n"
16286144Stmm	    "sll %0, 16, %0\n"
16386144Stmm	    "srl %0, 16, %0\n"
164154256Smarius	    : "=&r" (__ret), "=r" (__tmp1), "=&r" (__tmp2), "=&r" (__tmp3),
165180073Smarius	    "=&r" (__tmp4) : "1" (ip) : "cc");
16686144Stmm#undef __LD_ADD
16786144Stmm	return (__ret);
16886144Stmm}
169235941Sbz#endif
17086144Stmm
171198502Smarius#ifdef _KERNEL
17286144Stmmu_short	in_cksum_skip(struct mbuf *m, int len, int skip);
173198502Smarius#endif
17486144Stmm
17586144Stmm#endif /* _MACHINE_IN_CKSUM_H_ */
176