in_cksum.h revision 154256
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.
1386144Stmm * 4. 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: head/sys/sparc64/include/in_cksum.h 154256 2006-01-12 11:40:39Z marius $
5986144Stmm */
6086144Stmm
6186144Stmm#ifndef _MACHINE_IN_CKSUM_H_
6286144Stmm#define	_MACHINE_IN_CKSUM_H_	1
6386144Stmm
6486144Stmm#include <sys/cdefs.h>
6586144Stmm
6686144Stmm#define in_cksum(m, len)	in_cksum_skip(m, len, 0)
6786144Stmm
6886144Stmmstatic __inline void
6986144Stmmin_cksum_update(struct ip *ip)
7086144Stmm{
7186144Stmm	int __tmp;
7286144Stmm
7386144Stmm	__tmp = (int)ip->ip_sum + 1;
7486144Stmm	ip->ip_sum = __tmp + (__tmp >> 16);
7586144Stmm}
7686144Stmm
7786144Stmmstatic __inline u_short
7886144Stmmin_addword(u_short sum, u_short b)
7986144Stmm{
8086144Stmm	u_long __ret, __tmp;
8186144Stmm
8286144Stmm	__asm(
8386144Stmm	    "sll %2, 16, %0\n"
8486144Stmm	    "sll %3, 16, %1\n"
8586144Stmm	    "addcc %0, %1, %0\n"
8686144Stmm	    "srl %0, 16, %0\n"
8786144Stmm	    "addc %0, 0, %0\n"
88154256Smarius	    : "=&r" (__ret), "=&r" (__tmp) : "r" (sum), "r" (b));
8986144Stmm	return (__ret);
9086144Stmm}
9186144Stmm
9286144Stmmstatic __inline u_short
9386144Stmmin_pseudo(u_int sum, u_int b, u_int c)
9486144Stmm{
9586144Stmm	u_long __tmp;
9686144Stmm
9786144Stmm	__asm(
9886144Stmm	    "addcc %0, %3, %0\n"
9986144Stmm	    "addccc %0, %4, %0\n"
10086144Stmm	    "addc %0, 0, %0\n"
10186144Stmm	    "sll %0, 16, %1\n"
10286144Stmm	    "addcc %0, %1, %0\n"
10386144Stmm	    "srl %0, 16, %0\n"
10486144Stmm	    "addc %0, 0, %0\n"
105154256Smarius	    : "=r" (sum), "=&r" (__tmp) : "0" (sum), "r" (b), "r" (c));
10686144Stmm	return (sum);
10786144Stmm}
10886144Stmm
10986144Stmmstatic __inline u_int
11086144Stmmin_cksum_hdr(struct ip *ip)
11186144Stmm{
11286144Stmm	u_long __ret, __tmp1, __tmp2, __tmp3, __tmp4;
11386144Stmm
11486144Stmm	/*
11586144Stmm	 * Use 32 bit memory accesses and additions - addition with carry only
116154256Smarius	 * works for 32 bits, and fixing up alignment issues for 64 is probably
11786144Stmm	 * more trouble than it's worth.
11886144Stmm	 * This may read outside of the ip header, but does not cross a page
11986144Stmm	 * boundary in doing so, so that should be OK.
12086144Stmm	 * Actually, this specialized implementation might be overkill - using
12186144Stmm	 * a generic implementation for both in_cksum_skip and in_cksum_hdr
12286144Stmm	 * should not be too much more expensive.
12386144Stmm	 */
12486144Stmm#define __LD_ADD(addr, tmp, sum, offs, mod) \
12586144Stmm    "lduw [" #addr " + " #offs "], " #tmp "\n" \
12686144Stmm    "add" # mod " " #sum ", " #tmp ", " #sum "\n"
12786144Stmm
12886144Stmm	__asm(
12986144Stmm	    "and %5, 3, %3\n"
13086144Stmm	    "andn %5, 3, %1\n"
13186144Stmm	    "brz,pt %3, 0f\n"
13286144Stmm	    " lduw [%1], %0\n"
13386144Stmm	    "mov 4, %4\n"
13486144Stmm	    "sub %4, %3, %4\n"
13586144Stmm	    "sll %4, 3, %4\n"		/* fix up unaligned buffers */
13686144Stmm	    "mov 1, %2\n"
13786144Stmm	    "sll %2, %4, %4\n"
13886144Stmm	    "sub %4, 1, %4\n"
13986144Stmm	    "lduw [%1 + 20], %2\n"
14086144Stmm	    "andn %2, %4, %2\n"
14186144Stmm	    "and %0, %4, %0\n"
14286144Stmm	    "or %0, %2, %0\n"
14386144Stmm	    "0:\n"
14486144Stmm	    __LD_ADD(%1, %2, %0, 4, cc)
14586144Stmm	    __LD_ADD(%1, %2, %0, 8, ccc)
14686144Stmm	    __LD_ADD(%1, %2, %0, 12, ccc)
14786144Stmm	    __LD_ADD(%1, %2, %0, 16, ccc)
14886144Stmm	    "addc %0, 0, %0\n"		/* reduce */
14986144Stmm	    "1:\n"
15086144Stmm	    "sll %0, 16, %2\n"
15186144Stmm	    "addcc %0, %2, %0\n"
15286144Stmm	    "srl %0, 16, %0\n"
15386144Stmm	    "addc %0, 0, %0\n"
15486144Stmm	    "andcc %3, 1, %3\n"		/* need to byte-swap? */
15586144Stmm	    "clr %3\n"
15686144Stmm	    "bne,a,pn %%xcc, 1b\n"
15786144Stmm	    " sll %0, 8, %0\n"
15886144Stmm	    "not %0\n"
15986144Stmm	    "sll %0, 16, %0\n"
16086144Stmm	    "srl %0, 16, %0\n"
161154256Smarius	    : "=&r" (__ret), "=r" (__tmp1), "=&r" (__tmp2), "=&r" (__tmp3),
162154256Smarius		"=&r" (__tmp4) : "1" (ip));
16386144Stmm#undef __LD_ADD
16486144Stmm	return (__ret);
16586144Stmm}
16686144Stmm
16786144Stmmu_short	in_cksum_skip(struct mbuf *m, int len, int skip);
16886144Stmm
16986144Stmm#endif /* _MACHINE_IN_CKSUM_H_ */
170