pio_copy.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /*
  2. * Copyright(c) 2015, 2016 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include "hfi.h"
  48. /* additive distance between non-SOP and SOP space */
  49. #define SOP_DISTANCE (TXE_PIO_SIZE / 2)
  50. #define PIO_BLOCK_MASK (PIO_BLOCK_SIZE - 1)
  51. /* number of QUADWORDs in a block */
  52. #define PIO_BLOCK_QWS (PIO_BLOCK_SIZE / sizeof(u64))
  53. /**
  54. * pio_copy - copy data block to MMIO space
  55. * @pbuf: a number of blocks allocated within a PIO send context
  56. * @pbc: PBC to send
  57. * @from: source, must be 8 byte aligned
  58. * @count: number of DWORD (32-bit) quantities to copy from source
  59. *
  60. * Copy data from source to PIO Send Buffer memory, 8 bytes at a time.
  61. * Must always write full BLOCK_SIZE bytes blocks. The first block must
  62. * be written to the corresponding SOP=1 address.
  63. *
  64. * Known:
  65. * o pbuf->start always starts on a block boundary
  66. * o pbuf can wrap only at a block boundary
  67. */
  68. void pio_copy(struct hfi1_devdata *dd, struct pio_buf *pbuf, u64 pbc,
  69. const void *from, size_t count)
  70. {
  71. void __iomem *dest = pbuf->start + SOP_DISTANCE;
  72. void __iomem *send = dest + PIO_BLOCK_SIZE;
  73. void __iomem *dend; /* 8-byte data end */
  74. /* write the PBC */
  75. writeq(pbc, dest);
  76. dest += sizeof(u64);
  77. /* calculate where the QWORD data ends - in SOP=1 space */
  78. dend = dest + ((count >> 1) * sizeof(u64));
  79. if (dend < send) {
  80. /*
  81. * all QWORD data is within the SOP block, does *not*
  82. * reach the end of the SOP block
  83. */
  84. while (dest < dend) {
  85. writeq(*(u64 *)from, dest);
  86. from += sizeof(u64);
  87. dest += sizeof(u64);
  88. }
  89. /*
  90. * No boundary checks are needed here:
  91. * 0. We're not on the SOP block boundary
  92. * 1. The possible DWORD dangle will still be within
  93. * the SOP block
  94. * 2. We cannot wrap except on a block boundary.
  95. */
  96. } else {
  97. /* QWORD data extends _to_ or beyond the SOP block */
  98. /* write 8-byte SOP chunk data */
  99. while (dest < send) {
  100. writeq(*(u64 *)from, dest);
  101. from += sizeof(u64);
  102. dest += sizeof(u64);
  103. }
  104. /* drop out of the SOP range */
  105. dest -= SOP_DISTANCE;
  106. dend -= SOP_DISTANCE;
  107. /*
  108. * If the wrap comes before or matches the data end,
  109. * copy until until the wrap, then wrap.
  110. *
  111. * If the data ends at the end of the SOP above and
  112. * the buffer wraps, then pbuf->end == dend == dest
  113. * and nothing will get written, but we will wrap in
  114. * case there is a dangling DWORD.
  115. */
  116. if (pbuf->end <= dend) {
  117. while (dest < pbuf->end) {
  118. writeq(*(u64 *)from, dest);
  119. from += sizeof(u64);
  120. dest += sizeof(u64);
  121. }
  122. dest -= pbuf->sc->size;
  123. dend -= pbuf->sc->size;
  124. }
  125. /* write 8-byte non-SOP, non-wrap chunk data */
  126. while (dest < dend) {
  127. writeq(*(u64 *)from, dest);
  128. from += sizeof(u64);
  129. dest += sizeof(u64);
  130. }
  131. }
  132. /* at this point we have wrapped if we are going to wrap */
  133. /* write dangling u32, if any */
  134. if (count & 1) {
  135. union mix val;
  136. val.val64 = 0;
  137. val.val32[0] = *(u32 *)from;
  138. writeq(val.val64, dest);
  139. dest += sizeof(u64);
  140. }
  141. /*
  142. * fill in rest of block, no need to check pbuf->end
  143. * as we only wrap on a block boundary
  144. */
  145. while (((unsigned long)dest & PIO_BLOCK_MASK) != 0) {
  146. writeq(0, dest);
  147. dest += sizeof(u64);
  148. }
  149. /* finished with this buffer */
  150. this_cpu_dec(*pbuf->sc->buffers_allocated);
  151. preempt_enable();
  152. }
  153. /*
  154. * Handle carry bytes using shifts and masks.
  155. *
  156. * NOTE: the value the unused portion of carry is expected to always be zero.
  157. */
  158. /*
  159. * "zero" shift - bit shift used to zero out upper bytes. Input is
  160. * the count of LSB bytes to preserve.
  161. */
  162. #define zshift(x) (8 * (8 - (x)))
  163. /*
  164. * "merge" shift - bit shift used to merge with carry bytes. Input is
  165. * the LSB byte count to move beyond.
  166. */
  167. #define mshift(x) (8 * (x))
  168. /*
  169. * Jump copy - no-loop copy for < 8 bytes.
  170. */
  171. static inline void jcopy(u8 *dest, const u8 *src, u32 n)
  172. {
  173. switch (n) {
  174. case 7:
  175. *dest++ = *src++;
  176. /* fall through */
  177. case 6:
  178. *dest++ = *src++;
  179. /* fall through */
  180. case 5:
  181. *dest++ = *src++;
  182. /* fall through */
  183. case 4:
  184. *dest++ = *src++;
  185. /* fall through */
  186. case 3:
  187. *dest++ = *src++;
  188. /* fall through */
  189. case 2:
  190. *dest++ = *src++;
  191. /* fall through */
  192. case 1:
  193. *dest++ = *src++;
  194. /* fall through */
  195. }
  196. }
  197. /*
  198. * Read nbytes from "from" and and place them in the low bytes
  199. * of pbuf->carry. Other bytes are left as-is. Any previous
  200. * value in pbuf->carry is lost.
  201. *
  202. * NOTES:
  203. * o do not read from from if nbytes is zero
  204. * o from may _not_ be u64 aligned.
  205. */
  206. static inline void read_low_bytes(struct pio_buf *pbuf, const void *from,
  207. unsigned int nbytes)
  208. {
  209. pbuf->carry.val64 = 0;
  210. jcopy(&pbuf->carry.val8[0], from, nbytes);
  211. pbuf->carry_bytes = nbytes;
  212. }
  213. /*
  214. * Read nbytes bytes from "from" and put them at the end of pbuf->carry.
  215. * It is expected that the extra read does not overfill carry.
  216. *
  217. * NOTES:
  218. * o from may _not_ be u64 aligned
  219. * o nbytes may span a QW boundary
  220. */
  221. static inline void read_extra_bytes(struct pio_buf *pbuf,
  222. const void *from, unsigned int nbytes)
  223. {
  224. jcopy(&pbuf->carry.val8[pbuf->carry_bytes], from, nbytes);
  225. pbuf->carry_bytes += nbytes;
  226. }
  227. /*
  228. * Write a quad word using parts of pbuf->carry and the next 8 bytes of src.
  229. * Put the unused part of the next 8 bytes of src into the LSB bytes of
  230. * pbuf->carry with the upper bytes zeroed..
  231. *
  232. * NOTES:
  233. * o result must keep unused bytes zeroed
  234. * o src must be u64 aligned
  235. */
  236. static inline void merge_write8(
  237. struct pio_buf *pbuf,
  238. void __iomem *dest,
  239. const void *src)
  240. {
  241. u64 new, temp;
  242. new = *(u64 *)src;
  243. temp = pbuf->carry.val64 | (new << mshift(pbuf->carry_bytes));
  244. writeq(temp, dest);
  245. pbuf->carry.val64 = new >> zshift(pbuf->carry_bytes);
  246. }
  247. /*
  248. * Write a quad word using all bytes of carry.
  249. */
  250. static inline void carry8_write8(union mix carry, void __iomem *dest)
  251. {
  252. writeq(carry.val64, dest);
  253. }
  254. /*
  255. * Write a quad word using all the valid bytes of carry. If carry
  256. * has zero valid bytes, nothing is written.
  257. * Returns 0 on nothing written, non-zero on quad word written.
  258. */
  259. static inline int carry_write8(struct pio_buf *pbuf, void __iomem *dest)
  260. {
  261. if (pbuf->carry_bytes) {
  262. /* unused bytes are always kept zeroed, so just write */
  263. writeq(pbuf->carry.val64, dest);
  264. return 1;
  265. }
  266. return 0;
  267. }
  268. /*
  269. * Segmented PIO Copy - start
  270. *
  271. * Start a PIO copy.
  272. *
  273. * @pbuf: destination buffer
  274. * @pbc: the PBC for the PIO buffer
  275. * @from: data source, QWORD aligned
  276. * @nbytes: bytes to copy
  277. */
  278. void seg_pio_copy_start(struct pio_buf *pbuf, u64 pbc,
  279. const void *from, size_t nbytes)
  280. {
  281. void __iomem *dest = pbuf->start + SOP_DISTANCE;
  282. void __iomem *send = dest + PIO_BLOCK_SIZE;
  283. void __iomem *dend; /* 8-byte data end */
  284. writeq(pbc, dest);
  285. dest += sizeof(u64);
  286. /* calculate where the QWORD data ends - in SOP=1 space */
  287. dend = dest + ((nbytes >> 3) * sizeof(u64));
  288. if (dend < send) {
  289. /*
  290. * all QWORD data is within the SOP block, does *not*
  291. * reach the end of the SOP block
  292. */
  293. while (dest < dend) {
  294. writeq(*(u64 *)from, dest);
  295. from += sizeof(u64);
  296. dest += sizeof(u64);
  297. }
  298. /*
  299. * No boundary checks are needed here:
  300. * 0. We're not on the SOP block boundary
  301. * 1. The possible DWORD dangle will still be within
  302. * the SOP block
  303. * 2. We cannot wrap except on a block boundary.
  304. */
  305. } else {
  306. /* QWORD data extends _to_ or beyond the SOP block */
  307. /* write 8-byte SOP chunk data */
  308. while (dest < send) {
  309. writeq(*(u64 *)from, dest);
  310. from += sizeof(u64);
  311. dest += sizeof(u64);
  312. }
  313. /* drop out of the SOP range */
  314. dest -= SOP_DISTANCE;
  315. dend -= SOP_DISTANCE;
  316. /*
  317. * If the wrap comes before or matches the data end,
  318. * copy until until the wrap, then wrap.
  319. *
  320. * If the data ends at the end of the SOP above and
  321. * the buffer wraps, then pbuf->end == dend == dest
  322. * and nothing will get written, but we will wrap in
  323. * case there is a dangling DWORD.
  324. */
  325. if (pbuf->end <= dend) {
  326. while (dest < pbuf->end) {
  327. writeq(*(u64 *)from, dest);
  328. from += sizeof(u64);
  329. dest += sizeof(u64);
  330. }
  331. dest -= pbuf->sc->size;
  332. dend -= pbuf->sc->size;
  333. }
  334. /* write 8-byte non-SOP, non-wrap chunk data */
  335. while (dest < dend) {
  336. writeq(*(u64 *)from, dest);
  337. from += sizeof(u64);
  338. dest += sizeof(u64);
  339. }
  340. }
  341. /* at this point we have wrapped if we are going to wrap */
  342. /* ...but it doesn't matter as we're done writing */
  343. /* save dangling bytes, if any */
  344. read_low_bytes(pbuf, from, nbytes & 0x7);
  345. pbuf->qw_written = 1 /*PBC*/ + (nbytes >> 3);
  346. }
  347. /*
  348. * Mid copy helper, "mixed case" - source is 64-bit aligned but carry
  349. * bytes are non-zero.
  350. *
  351. * Whole u64s must be written to the chip, so bytes must be manually merged.
  352. *
  353. * @pbuf: destination buffer
  354. * @from: data source, is QWORD aligned.
  355. * @nbytes: bytes to copy
  356. *
  357. * Must handle nbytes < 8.
  358. */
  359. static void mid_copy_mix(struct pio_buf *pbuf, const void *from, size_t nbytes)
  360. {
  361. void __iomem *dest = pbuf->start + (pbuf->qw_written * sizeof(u64));
  362. void __iomem *dend; /* 8-byte data end */
  363. unsigned long qw_to_write = nbytes >> 3;
  364. unsigned long bytes_left = nbytes & 0x7;
  365. /* calculate 8-byte data end */
  366. dend = dest + (qw_to_write * sizeof(u64));
  367. if (pbuf->qw_written < PIO_BLOCK_QWS) {
  368. /*
  369. * Still within SOP block. We don't need to check for
  370. * wrap because we are still in the first block and
  371. * can only wrap on block boundaries.
  372. */
  373. void __iomem *send; /* SOP end */
  374. void __iomem *xend;
  375. /*
  376. * calculate the end of data or end of block, whichever
  377. * comes first
  378. */
  379. send = pbuf->start + PIO_BLOCK_SIZE;
  380. xend = min(send, dend);
  381. /* shift up to SOP=1 space */
  382. dest += SOP_DISTANCE;
  383. xend += SOP_DISTANCE;
  384. /* write 8-byte chunk data */
  385. while (dest < xend) {
  386. merge_write8(pbuf, dest, from);
  387. from += sizeof(u64);
  388. dest += sizeof(u64);
  389. }
  390. /* shift down to SOP=0 space */
  391. dest -= SOP_DISTANCE;
  392. }
  393. /*
  394. * At this point dest could be (either, both, or neither):
  395. * - at dend
  396. * - at the wrap
  397. */
  398. /*
  399. * If the wrap comes before or matches the data end,
  400. * copy until until the wrap, then wrap.
  401. *
  402. * If dest is at the wrap, we will fall into the if,
  403. * not do the loop, when wrap.
  404. *
  405. * If the data ends at the end of the SOP above and
  406. * the buffer wraps, then pbuf->end == dend == dest
  407. * and nothing will get written.
  408. */
  409. if (pbuf->end <= dend) {
  410. while (dest < pbuf->end) {
  411. merge_write8(pbuf, dest, from);
  412. from += sizeof(u64);
  413. dest += sizeof(u64);
  414. }
  415. dest -= pbuf->sc->size;
  416. dend -= pbuf->sc->size;
  417. }
  418. /* write 8-byte non-SOP, non-wrap chunk data */
  419. while (dest < dend) {
  420. merge_write8(pbuf, dest, from);
  421. from += sizeof(u64);
  422. dest += sizeof(u64);
  423. }
  424. pbuf->qw_written += qw_to_write;
  425. /* handle carry and left-over bytes */
  426. if (pbuf->carry_bytes + bytes_left >= 8) {
  427. unsigned long nread;
  428. /* there is enough to fill another qw - fill carry */
  429. nread = 8 - pbuf->carry_bytes;
  430. read_extra_bytes(pbuf, from, nread);
  431. /*
  432. * One more write - but need to make sure dest is correct.
  433. * Check for wrap and the possibility the write
  434. * should be in SOP space.
  435. *
  436. * The two checks immediately below cannot both be true, hence
  437. * the else. If we have wrapped, we cannot still be within the
  438. * first block. Conversely, if we are still in the first block,
  439. * we cannot have wrapped. We do the wrap check first as that
  440. * is more likely.
  441. */
  442. /* adjust if we have wrapped */
  443. if (dest >= pbuf->end)
  444. dest -= pbuf->sc->size;
  445. /* jump to the SOP range if within the first block */
  446. else if (pbuf->qw_written < PIO_BLOCK_QWS)
  447. dest += SOP_DISTANCE;
  448. /* flush out full carry */
  449. carry8_write8(pbuf->carry, dest);
  450. pbuf->qw_written++;
  451. /* now adjust and read the rest of the bytes into carry */
  452. bytes_left -= nread;
  453. from += nread; /* from is now not aligned */
  454. read_low_bytes(pbuf, from, bytes_left);
  455. } else {
  456. /* not enough to fill another qw, append the rest to carry */
  457. read_extra_bytes(pbuf, from, bytes_left);
  458. }
  459. }
  460. /*
  461. * Mid copy helper, "straight case" - source pointer is 64-bit aligned
  462. * with no carry bytes.
  463. *
  464. * @pbuf: destination buffer
  465. * @from: data source, is QWORD aligned
  466. * @nbytes: bytes to copy
  467. *
  468. * Must handle nbytes < 8.
  469. */
  470. static void mid_copy_straight(struct pio_buf *pbuf,
  471. const void *from, size_t nbytes)
  472. {
  473. void __iomem *dest = pbuf->start + (pbuf->qw_written * sizeof(u64));
  474. void __iomem *dend; /* 8-byte data end */
  475. /* calculate 8-byte data end */
  476. dend = dest + ((nbytes >> 3) * sizeof(u64));
  477. if (pbuf->qw_written < PIO_BLOCK_QWS) {
  478. /*
  479. * Still within SOP block. We don't need to check for
  480. * wrap because we are still in the first block and
  481. * can only wrap on block boundaries.
  482. */
  483. void __iomem *send; /* SOP end */
  484. void __iomem *xend;
  485. /*
  486. * calculate the end of data or end of block, whichever
  487. * comes first
  488. */
  489. send = pbuf->start + PIO_BLOCK_SIZE;
  490. xend = min(send, dend);
  491. /* shift up to SOP=1 space */
  492. dest += SOP_DISTANCE;
  493. xend += SOP_DISTANCE;
  494. /* write 8-byte chunk data */
  495. while (dest < xend) {
  496. writeq(*(u64 *)from, dest);
  497. from += sizeof(u64);
  498. dest += sizeof(u64);
  499. }
  500. /* shift down to SOP=0 space */
  501. dest -= SOP_DISTANCE;
  502. }
  503. /*
  504. * At this point dest could be (either, both, or neither):
  505. * - at dend
  506. * - at the wrap
  507. */
  508. /*
  509. * If the wrap comes before or matches the data end,
  510. * copy until until the wrap, then wrap.
  511. *
  512. * If dest is at the wrap, we will fall into the if,
  513. * not do the loop, when wrap.
  514. *
  515. * If the data ends at the end of the SOP above and
  516. * the buffer wraps, then pbuf->end == dend == dest
  517. * and nothing will get written.
  518. */
  519. if (pbuf->end <= dend) {
  520. while (dest < pbuf->end) {
  521. writeq(*(u64 *)from, dest);
  522. from += sizeof(u64);
  523. dest += sizeof(u64);
  524. }
  525. dest -= pbuf->sc->size;
  526. dend -= pbuf->sc->size;
  527. }
  528. /* write 8-byte non-SOP, non-wrap chunk data */
  529. while (dest < dend) {
  530. writeq(*(u64 *)from, dest);
  531. from += sizeof(u64);
  532. dest += sizeof(u64);
  533. }
  534. /* we know carry_bytes was zero on entry to this routine */
  535. read_low_bytes(pbuf, from, nbytes & 0x7);
  536. pbuf->qw_written += nbytes >> 3;
  537. }
  538. /*
  539. * Segmented PIO Copy - middle
  540. *
  541. * Must handle any aligned tail and any aligned source with any byte count.
  542. *
  543. * @pbuf: a number of blocks allocated within a PIO send context
  544. * @from: data source
  545. * @nbytes: number of bytes to copy
  546. */
  547. void seg_pio_copy_mid(struct pio_buf *pbuf, const void *from, size_t nbytes)
  548. {
  549. unsigned long from_align = (unsigned long)from & 0x7;
  550. if (pbuf->carry_bytes + nbytes < 8) {
  551. /* not enough bytes to fill a QW */
  552. read_extra_bytes(pbuf, from, nbytes);
  553. return;
  554. }
  555. if (from_align) {
  556. /* misaligned source pointer - align it */
  557. unsigned long to_align;
  558. /* bytes to read to align "from" */
  559. to_align = 8 - from_align;
  560. /*
  561. * In the advance-to-alignment logic below, we do not need
  562. * to check if we are using more than nbytes. This is because
  563. * if we are here, we already know that carry+nbytes will
  564. * fill at least one QW.
  565. */
  566. if (pbuf->carry_bytes + to_align < 8) {
  567. /* not enough align bytes to fill a QW */
  568. read_extra_bytes(pbuf, from, to_align);
  569. from += to_align;
  570. nbytes -= to_align;
  571. } else {
  572. /* bytes to fill carry */
  573. unsigned long to_fill = 8 - pbuf->carry_bytes;
  574. /* bytes left over to be read */
  575. unsigned long extra = to_align - to_fill;
  576. void __iomem *dest;
  577. /* fill carry... */
  578. read_extra_bytes(pbuf, from, to_fill);
  579. from += to_fill;
  580. nbytes -= to_fill;
  581. /* may not be enough valid bytes left to align */
  582. if (extra > nbytes)
  583. extra = nbytes;
  584. /* ...now write carry */
  585. dest = pbuf->start + (pbuf->qw_written * sizeof(u64));
  586. /*
  587. * The two checks immediately below cannot both be
  588. * true, hence the else. If we have wrapped, we
  589. * cannot still be within the first block.
  590. * Conversely, if we are still in the first block, we
  591. * cannot have wrapped. We do the wrap check first
  592. * as that is more likely.
  593. */
  594. /* adjust if we've wrapped */
  595. if (dest >= pbuf->end)
  596. dest -= pbuf->sc->size;
  597. /* jump to SOP range if within the first block */
  598. else if (pbuf->qw_written < PIO_BLOCK_QWS)
  599. dest += SOP_DISTANCE;
  600. carry8_write8(pbuf->carry, dest);
  601. pbuf->qw_written++;
  602. /* read any extra bytes to do final alignment */
  603. /* this will overwrite anything in pbuf->carry */
  604. read_low_bytes(pbuf, from, extra);
  605. from += extra;
  606. nbytes -= extra;
  607. /*
  608. * If no bytes are left, return early - we are done.
  609. * NOTE: This short-circuit is *required* because
  610. * "extra" may have been reduced in size and "from"
  611. * is not aligned, as required when leaving this
  612. * if block.
  613. */
  614. if (nbytes == 0)
  615. return;
  616. }
  617. /* at this point, from is QW aligned */
  618. }
  619. if (pbuf->carry_bytes)
  620. mid_copy_mix(pbuf, from, nbytes);
  621. else
  622. mid_copy_straight(pbuf, from, nbytes);
  623. }
  624. /*
  625. * Segmented PIO Copy - end
  626. *
  627. * Write any remainder (in pbuf->carry) and finish writing the whole block.
  628. *
  629. * @pbuf: a number of blocks allocated within a PIO send context
  630. */
  631. void seg_pio_copy_end(struct pio_buf *pbuf)
  632. {
  633. void __iomem *dest = pbuf->start + (pbuf->qw_written * sizeof(u64));
  634. /*
  635. * The two checks immediately below cannot both be true, hence the
  636. * else. If we have wrapped, we cannot still be within the first
  637. * block. Conversely, if we are still in the first block, we
  638. * cannot have wrapped. We do the wrap check first as that is
  639. * more likely.
  640. */
  641. /* adjust if we have wrapped */
  642. if (dest >= pbuf->end)
  643. dest -= pbuf->sc->size;
  644. /* jump to the SOP range if within the first block */
  645. else if (pbuf->qw_written < PIO_BLOCK_QWS)
  646. dest += SOP_DISTANCE;
  647. /* write final bytes, if any */
  648. if (carry_write8(pbuf, dest)) {
  649. dest += sizeof(u64);
  650. /*
  651. * NOTE: We do not need to recalculate whether dest needs
  652. * SOP_DISTANCE or not.
  653. *
  654. * If we are in the first block and the dangle write
  655. * keeps us in the same block, dest will need
  656. * to retain SOP_DISTANCE in the loop below.
  657. *
  658. * If we are in the first block and the dangle write pushes
  659. * us to the next block, then loop below will not run
  660. * and dest is not used. Hence we do not need to update
  661. * it.
  662. *
  663. * If we are past the first block, then SOP_DISTANCE
  664. * was never added, so there is nothing to do.
  665. */
  666. }
  667. /* fill in rest of block */
  668. while (((unsigned long)dest & PIO_BLOCK_MASK) != 0) {
  669. writeq(0, dest);
  670. dest += sizeof(u64);
  671. }
  672. /* finished with this buffer */
  673. this_cpu_dec(*pbuf->sc->buffers_allocated);
  674. preempt_enable();
  675. }