OpenCores
URL https://opencores.org/ocsvn/or1k_soc_on_altera_embedded_dev_kit/or1k_soc_on_altera_embedded_dev_kit/trunk

Subversion Repositories or1k_soc_on_altera_embedded_dev_kit

[/] [or1k_soc_on_altera_embedded_dev_kit/] [trunk/] [linux-2.6/] [linux-2.6.24/] [arch/] [or32/] [lib/] [checksum.c] - Blame information for rev 7

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 7 xianfeng
/*
2
 * INET         An implementation of the TCP/IP protocol suite for the LINUX
3
 *              operating system.  INET is implemented using the  BSD Socket
4
 *              interface as the means of communication with the user level.
5
 *
6
 *              MIPS specific IP/TCP/UDP checksumming routines
7
 *
8
 * Authors:     Ralf Baechle, <ralf@waldorf-gmbh.de>
9
 *              Lots of code moved from tcp.c and ip.c; see those files
10
 *              for more names.
11
 *
12
 *              This program is free software; you can redistribute it and/or
13
 *              modify it under the terms of the GNU General Public License
14
 *              as published by the Free Software Foundation; either version
15
 *              2 of the License, or (at your option) any later version.
16
 *
17
 */
18
//#include <net/checksum.h>
19
#include <linux/types.h>
20
#include <asm/byteorder.h>
21
#include <asm/string.h>
22
#include <asm/uaccess.h>
23
#include <asm/or32-hf.h>
24
 
25
static inline unsigned short from32to16(unsigned int x)
26
{
27
 
28
        /* 32 bits --> 16 bits + carry */
29
        x = (x & 0xffff) + (x >> 16);
30
        /* 16 bits + carry --> 16 bits including carry */
31
        x = (x & 0xffff) + (x >> 16);
32
        return (unsigned short)x;
33
}
34
 
35
static inline unsigned int do_csum(const unsigned char * buff, int len)
36
{
37
        int odd, count;
38
        unsigned int result = 0;
39
 
40
        if (len <= 0)
41
                goto out;
42
        odd = 1 & (unsigned long) buff;
43
        if (odd) {
44
                result = be16_to_cpu(*buff);
45
                len--;
46
                buff++;
47
        }
48
        count = len >> 1;               /* nr of 16-bit words.. */
49
        if (count) {
50
                if (2 & (unsigned long) buff) {
51
                        result += *(unsigned short *) buff;
52
                        count--;
53
                        len -= 2;
54
                        buff += 2;
55
                }
56
                count >>= 1;            /* nr of 32-bit words.. */
57
                if (count) {
58
                        unsigned int carry = 0;
59
                        do {
60
                                unsigned int w = *(unsigned int *) buff;
61
                                count--;
62
                                buff += 4;
63
                                result += carry;
64
                                result += w;
65
                                carry = (w > result);
66
                        } while (count);
67
                        result += carry;
68
                        result = (result & 0xffff) + (result >> 16);
69
                }
70
                if (len & 2) {
71
                        result += *(unsigned short *) buff;
72
                        buff += 2;
73
                }
74
        }
75
        if (len & 1)
76
                result += le16_to_cpu(*buff);
77
        result = from32to16(result);
78
        if (odd)
79
                result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
80
out:
81
        return result;
82
}
83
 
84
/*
85
 * computes a partial checksum, e.g. for TCP/UDP fragments
86
 */
87
unsigned int csum_partial(const unsigned char *buff, int len,
88
                          unsigned int sum)
89
{
90
        unsigned int result = do_csum(buff, len);
91
 
92
        /* add in old sum, and carry.. */
93
        result += sum;
94
        if(sum > result)
95
                result += 1;
96
        return result;
97
}
98
 
99
/*
100
 * copy while checksumming, otherwise like csum_partial
101
 */
102
unsigned int csum_partial_copy_nocheck(const char *src, char *dst,
103
                                       int len, unsigned int sum)
104
{
105
        /*
106
         * It's 2:30 am and I don't feel like doing it real ...
107
         * This is lots slower than the real thing (tm)
108
         */
109
        sum = csum_partial(src, len, sum);
110
        memcpy(dst, src, len);
111
 
112
        return sum;
113
}
114
 
115
/*
116
 * Copy from userspace and compute checksum.  If we catch an exception
117
 * then zero the rest of the buffer.
118
 */
119
unsigned int csum_partial_copy_from_user (const char *src, char *dst,
120
                                          int len, unsigned int sum,
121
                                          int *err_ptr)
122
{
123
        int missing;
124
        phx_warn("CSUM");
125
 
126
        missing = copy_from_user(dst, src, len);
127
        if (missing) {
128
                memset(dst + len - missing, 0, missing);
129
                *err_ptr = -EFAULT;
130
        }
131
 
132
        return csum_partial(dst, len, sum);
133
}
134
 
135
/* __PHX__: OLD routines
136
            move to seperate file
137
 */
138
unsigned int csum_partial_copy_fromuser (const char *src, char *dst,
139
                                         int len, unsigned int sum)
140
{
141
        __copy_from_user(dst, src, len);
142
        return csum_partial(dst, len, sum);
143
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.