checksum.S 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  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. * IP/TCP/UDP checksumming routines
  7. *
  8. * Xtensa version: Copyright (C) 2001 Tensilica, Inc. by Kevin Chea
  9. * Optimized by Joe Taylor
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. */
  16. #include <linux/errno.h>
  17. #include <linux/linkage.h>
  18. #include <variant/core.h>
  19. #include <asm/asmmacro.h>
  20. /*
  21. * computes a partial checksum, e.g. for TCP/UDP fragments
  22. */
  23. /*
  24. * unsigned int csum_partial(const unsigned char *buf, int len,
  25. * unsigned int sum);
  26. * a2 = buf
  27. * a3 = len
  28. * a4 = sum
  29. *
  30. * This function assumes 2- or 4-byte alignment. Other alignments will fail!
  31. */
  32. /* ONES_ADD converts twos-complement math to ones-complement. */
  33. #define ONES_ADD(sum, val) \
  34. add sum, sum, val ; \
  35. bgeu sum, val, 99f ; \
  36. addi sum, sum, 1 ; \
  37. 99: ;
  38. .text
  39. ENTRY(csum_partial)
  40. /*
  41. * Experiments with Ethernet and SLIP connections show that buf
  42. * is aligned on either a 2-byte or 4-byte boundary.
  43. */
  44. entry sp, 32
  45. extui a5, a2, 0, 2
  46. bnez a5, 8f /* branch if 2-byte aligned */
  47. /* Fall-through on common case, 4-byte alignment */
  48. 1:
  49. srli a5, a3, 5 /* 32-byte chunks */
  50. #if XCHAL_HAVE_LOOPS
  51. loopgtz a5, 2f
  52. #else
  53. beqz a5, 2f
  54. slli a5, a5, 5
  55. add a5, a5, a2 /* a5 = end of last 32-byte chunk */
  56. .Loop1:
  57. #endif
  58. l32i a6, a2, 0
  59. l32i a7, a2, 4
  60. ONES_ADD(a4, a6)
  61. ONES_ADD(a4, a7)
  62. l32i a6, a2, 8
  63. l32i a7, a2, 12
  64. ONES_ADD(a4, a6)
  65. ONES_ADD(a4, a7)
  66. l32i a6, a2, 16
  67. l32i a7, a2, 20
  68. ONES_ADD(a4, a6)
  69. ONES_ADD(a4, a7)
  70. l32i a6, a2, 24
  71. l32i a7, a2, 28
  72. ONES_ADD(a4, a6)
  73. ONES_ADD(a4, a7)
  74. addi a2, a2, 4*8
  75. #if !XCHAL_HAVE_LOOPS
  76. blt a2, a5, .Loop1
  77. #endif
  78. 2:
  79. extui a5, a3, 2, 3 /* remaining 4-byte chunks */
  80. #if XCHAL_HAVE_LOOPS
  81. loopgtz a5, 3f
  82. #else
  83. beqz a5, 3f
  84. slli a5, a5, 2
  85. add a5, a5, a2 /* a5 = end of last 4-byte chunk */
  86. .Loop2:
  87. #endif
  88. l32i a6, a2, 0
  89. ONES_ADD(a4, a6)
  90. addi a2, a2, 4
  91. #if !XCHAL_HAVE_LOOPS
  92. blt a2, a5, .Loop2
  93. #endif
  94. 3:
  95. _bbci.l a3, 1, 5f /* remaining 2-byte chunk */
  96. l16ui a6, a2, 0
  97. ONES_ADD(a4, a6)
  98. addi a2, a2, 2
  99. 5:
  100. _bbci.l a3, 0, 7f /* remaining 1-byte chunk */
  101. 6: l8ui a6, a2, 0
  102. #ifdef __XTENSA_EB__
  103. slli a6, a6, 8 /* load byte into bits 8..15 */
  104. #endif
  105. ONES_ADD(a4, a6)
  106. 7:
  107. mov a2, a4
  108. retw
  109. /* uncommon case, buf is 2-byte aligned */
  110. 8:
  111. beqz a3, 7b /* branch if len == 0 */
  112. beqi a3, 1, 6b /* branch if len == 1 */
  113. extui a5, a2, 0, 1
  114. bnez a5, 8f /* branch if 1-byte aligned */
  115. l16ui a6, a2, 0 /* common case, len >= 2 */
  116. ONES_ADD(a4, a6)
  117. addi a2, a2, 2 /* adjust buf */
  118. addi a3, a3, -2 /* adjust len */
  119. j 1b /* now buf is 4-byte aligned */
  120. /* case: odd-byte aligned, len > 1
  121. * This case is dog slow, so don't give us an odd address.
  122. * (I don't think this ever happens, but just in case.)
  123. */
  124. 8:
  125. srli a5, a3, 2 /* 4-byte chunks */
  126. #if XCHAL_HAVE_LOOPS
  127. loopgtz a5, 2f
  128. #else
  129. beqz a5, 2f
  130. slli a5, a5, 2
  131. add a5, a5, a2 /* a5 = end of last 4-byte chunk */
  132. .Loop3:
  133. #endif
  134. l8ui a6, a2, 0 /* bits 24..31 */
  135. l16ui a7, a2, 1 /* bits 8..23 */
  136. l8ui a8, a2, 3 /* bits 0.. 8 */
  137. #ifdef __XTENSA_EB__
  138. slli a6, a6, 24
  139. #else
  140. slli a8, a8, 24
  141. #endif
  142. slli a7, a7, 8
  143. or a7, a7, a6
  144. or a7, a7, a8
  145. ONES_ADD(a4, a7)
  146. addi a2, a2, 4
  147. #if !XCHAL_HAVE_LOOPS
  148. blt a2, a5, .Loop3
  149. #endif
  150. 2:
  151. _bbci.l a3, 1, 3f /* remaining 2-byte chunk, still odd addr */
  152. l8ui a6, a2, 0
  153. l8ui a7, a2, 1
  154. #ifdef __XTENSA_EB__
  155. slli a6, a6, 8
  156. #else
  157. slli a7, a7, 8
  158. #endif
  159. or a7, a7, a6
  160. ONES_ADD(a4, a7)
  161. addi a2, a2, 2
  162. 3:
  163. j 5b /* branch to handle the remaining byte */
  164. ENDPROC(csum_partial)
  165. /*
  166. * Copy from ds while checksumming, otherwise like csum_partial
  167. */
  168. /*
  169. unsigned int csum_partial_copy_generic (const char *src, char *dst, int len,
  170. int sum, int *src_err_ptr, int *dst_err_ptr)
  171. a2 = src
  172. a3 = dst
  173. a4 = len
  174. a5 = sum
  175. a6 = src_err_ptr
  176. a7 = dst_err_ptr
  177. a8 = temp
  178. a9 = temp
  179. a10 = temp
  180. a11 = original len for exception handling
  181. a12 = original dst for exception handling
  182. This function is optimized for 4-byte aligned addresses. Other
  183. alignments work, but not nearly as efficiently.
  184. */
  185. ENTRY(csum_partial_copy_generic)
  186. entry sp, 32
  187. mov a12, a3
  188. mov a11, a4
  189. or a10, a2, a3
  190. /* We optimize the following alignment tests for the 4-byte
  191. aligned case. Two bbsi.l instructions might seem more optimal
  192. (commented out below). However, both labels 5: and 3: are out
  193. of the imm8 range, so the assembler relaxes them into
  194. equivalent bbci.l, j combinations, which is actually
  195. slower. */
  196. extui a9, a10, 0, 2
  197. beqz a9, 1f /* branch if both are 4-byte aligned */
  198. bbsi.l a10, 0, 5f /* branch if one address is odd */
  199. j 3f /* one address is 2-byte aligned */
  200. /* _bbsi.l a10, 0, 5f */ /* branch if odd address */
  201. /* _bbsi.l a10, 1, 3f */ /* branch if 2-byte-aligned address */
  202. 1:
  203. /* src and dst are both 4-byte aligned */
  204. srli a10, a4, 5 /* 32-byte chunks */
  205. #if XCHAL_HAVE_LOOPS
  206. loopgtz a10, 2f
  207. #else
  208. beqz a10, 2f
  209. slli a10, a10, 5
  210. add a10, a10, a2 /* a10 = end of last 32-byte src chunk */
  211. .Loop5:
  212. #endif
  213. EX(10f) l32i a9, a2, 0
  214. EX(10f) l32i a8, a2, 4
  215. EX(11f) s32i a9, a3, 0
  216. EX(11f) s32i a8, a3, 4
  217. ONES_ADD(a5, a9)
  218. ONES_ADD(a5, a8)
  219. EX(10f) l32i a9, a2, 8
  220. EX(10f) l32i a8, a2, 12
  221. EX(11f) s32i a9, a3, 8
  222. EX(11f) s32i a8, a3, 12
  223. ONES_ADD(a5, a9)
  224. ONES_ADD(a5, a8)
  225. EX(10f) l32i a9, a2, 16
  226. EX(10f) l32i a8, a2, 20
  227. EX(11f) s32i a9, a3, 16
  228. EX(11f) s32i a8, a3, 20
  229. ONES_ADD(a5, a9)
  230. ONES_ADD(a5, a8)
  231. EX(10f) l32i a9, a2, 24
  232. EX(10f) l32i a8, a2, 28
  233. EX(11f) s32i a9, a3, 24
  234. EX(11f) s32i a8, a3, 28
  235. ONES_ADD(a5, a9)
  236. ONES_ADD(a5, a8)
  237. addi a2, a2, 32
  238. addi a3, a3, 32
  239. #if !XCHAL_HAVE_LOOPS
  240. blt a2, a10, .Loop5
  241. #endif
  242. 2:
  243. extui a10, a4, 2, 3 /* remaining 4-byte chunks */
  244. extui a4, a4, 0, 2 /* reset len for general-case, 2-byte chunks */
  245. #if XCHAL_HAVE_LOOPS
  246. loopgtz a10, 3f
  247. #else
  248. beqz a10, 3f
  249. slli a10, a10, 2
  250. add a10, a10, a2 /* a10 = end of last 4-byte src chunk */
  251. .Loop6:
  252. #endif
  253. EX(10f) l32i a9, a2, 0
  254. EX(11f) s32i a9, a3, 0
  255. ONES_ADD(a5, a9)
  256. addi a2, a2, 4
  257. addi a3, a3, 4
  258. #if !XCHAL_HAVE_LOOPS
  259. blt a2, a10, .Loop6
  260. #endif
  261. 3:
  262. /*
  263. Control comes to here in two cases: (1) It may fall through
  264. to here from the 4-byte alignment case to process, at most,
  265. one 2-byte chunk. (2) It branches to here from above if
  266. either src or dst is 2-byte aligned, and we process all bytes
  267. here, except for perhaps a trailing odd byte. It's
  268. inefficient, so align your addresses to 4-byte boundaries.
  269. a2 = src
  270. a3 = dst
  271. a4 = len
  272. a5 = sum
  273. */
  274. srli a10, a4, 1 /* 2-byte chunks */
  275. #if XCHAL_HAVE_LOOPS
  276. loopgtz a10, 4f
  277. #else
  278. beqz a10, 4f
  279. slli a10, a10, 1
  280. add a10, a10, a2 /* a10 = end of last 2-byte src chunk */
  281. .Loop7:
  282. #endif
  283. EX(10f) l16ui a9, a2, 0
  284. EX(11f) s16i a9, a3, 0
  285. ONES_ADD(a5, a9)
  286. addi a2, a2, 2
  287. addi a3, a3, 2
  288. #if !XCHAL_HAVE_LOOPS
  289. blt a2, a10, .Loop7
  290. #endif
  291. 4:
  292. /* This section processes a possible trailing odd byte. */
  293. _bbci.l a4, 0, 8f /* 1-byte chunk */
  294. EX(10f) l8ui a9, a2, 0
  295. EX(11f) s8i a9, a3, 0
  296. #ifdef __XTENSA_EB__
  297. slli a9, a9, 8 /* shift byte to bits 8..15 */
  298. #endif
  299. ONES_ADD(a5, a9)
  300. 8:
  301. mov a2, a5
  302. retw
  303. 5:
  304. /* Control branch to here when either src or dst is odd. We
  305. process all bytes using 8-bit accesses. Grossly inefficient,
  306. so don't feed us an odd address. */
  307. srli a10, a4, 1 /* handle in pairs for 16-bit csum */
  308. #if XCHAL_HAVE_LOOPS
  309. loopgtz a10, 6f
  310. #else
  311. beqz a10, 6f
  312. slli a10, a10, 1
  313. add a10, a10, a2 /* a10 = end of last odd-aligned, 2-byte src chunk */
  314. .Loop8:
  315. #endif
  316. EX(10f) l8ui a9, a2, 0
  317. EX(10f) l8ui a8, a2, 1
  318. EX(11f) s8i a9, a3, 0
  319. EX(11f) s8i a8, a3, 1
  320. #ifdef __XTENSA_EB__
  321. slli a9, a9, 8 /* combine into a single 16-bit value */
  322. #else /* for checksum computation */
  323. slli a8, a8, 8
  324. #endif
  325. or a9, a9, a8
  326. ONES_ADD(a5, a9)
  327. addi a2, a2, 2
  328. addi a3, a3, 2
  329. #if !XCHAL_HAVE_LOOPS
  330. blt a2, a10, .Loop8
  331. #endif
  332. 6:
  333. j 4b /* process the possible trailing odd byte */
  334. ENDPROC(csum_partial_copy_generic)
  335. # Exception handler:
  336. .section .fixup, "ax"
  337. /*
  338. a6 = src_err_ptr
  339. a7 = dst_err_ptr
  340. a11 = original len for exception handling
  341. a12 = original dst for exception handling
  342. */
  343. 10:
  344. _movi a2, -EFAULT
  345. s32i a2, a6, 0 /* src_err_ptr */
  346. # clear the complete destination - computing the rest
  347. # is too much work
  348. movi a2, 0
  349. #if XCHAL_HAVE_LOOPS
  350. loopgtz a11, 2f
  351. #else
  352. beqz a11, 2f
  353. add a11, a11, a12 /* a11 = ending address */
  354. .Leloop:
  355. #endif
  356. s8i a2, a12, 0
  357. addi a12, a12, 1
  358. #if !XCHAL_HAVE_LOOPS
  359. blt a12, a11, .Leloop
  360. #endif
  361. 2:
  362. retw
  363. 11:
  364. movi a2, -EFAULT
  365. s32i a2, a7, 0 /* dst_err_ptr */
  366. movi a2, 0
  367. retw
  368. .previous