blob: 7b0aa19ba06254617eaa5b6b388046f40637feda [file] [log] [blame]
Mike Marciniszyn77241052015-07-30 15:17:43 -04001/*
Jubin John05d6ac12016-02-14 20:22:17 -08002 * Copyright(c) 2015, 2016 Intel Corporation.
Mike Marciniszyn77241052015-07-30 15:17:43 -04003 *
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 *
Mike Marciniszyn77241052015-07-30 15:17:43 -04009 * 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 *
Mike Marciniszyn77241052015-07-30 15:17:43 -040020 * 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
48#include "hfi.h"
49
50/* additive distance between non-SOP and SOP space */
51#define SOP_DISTANCE (TXE_PIO_SIZE / 2)
Jubin John8638b772016-02-14 20:19:24 -080052#define PIO_BLOCK_MASK (PIO_BLOCK_SIZE - 1)
Mike Marciniszyn77241052015-07-30 15:17:43 -040053/* number of QUADWORDs in a block */
Jubin John8638b772016-02-14 20:19:24 -080054#define PIO_BLOCK_QWS (PIO_BLOCK_SIZE / sizeof(u64))
Mike Marciniszyn77241052015-07-30 15:17:43 -040055
56/**
57 * pio_copy - copy data block to MMIO space
58 * @pbuf: a number of blocks allocated within a PIO send context
59 * @pbc: PBC to send
60 * @from: source, must be 8 byte aligned
61 * @count: number of DWORD (32-bit) quantities to copy from source
62 *
63 * Copy data from source to PIO Send Buffer memory, 8 bytes at a time.
64 * Must always write full BLOCK_SIZE bytes blocks. The first block must
65 * be written to the corresponding SOP=1 address.
66 *
67 * Known:
68 * o pbuf->start always starts on a block boundary
69 * o pbuf can wrap only at a block boundary
70 */
71void pio_copy(struct hfi1_devdata *dd, struct pio_buf *pbuf, u64 pbc,
72 const void *from, size_t count)
73{
74 void __iomem *dest = pbuf->start + SOP_DISTANCE;
75 void __iomem *send = dest + PIO_BLOCK_SIZE;
76 void __iomem *dend; /* 8-byte data end */
77
78 /* write the PBC */
79 writeq(pbc, dest);
80 dest += sizeof(u64);
81
82 /* calculate where the QWORD data ends - in SOP=1 space */
Jubin John8638b772016-02-14 20:19:24 -080083 dend = dest + ((count >> 1) * sizeof(u64));
Mike Marciniszyn77241052015-07-30 15:17:43 -040084
85 if (dend < send) {
Jubin John4d114fd2016-02-14 20:21:43 -080086 /*
87 * all QWORD data is within the SOP block, does *not*
88 * reach the end of the SOP block
89 */
Mike Marciniszyn77241052015-07-30 15:17:43 -040090
91 while (dest < dend) {
92 writeq(*(u64 *)from, dest);
93 from += sizeof(u64);
94 dest += sizeof(u64);
95 }
96 /*
97 * No boundary checks are needed here:
98 * 0. We're not on the SOP block boundary
99 * 1. The possible DWORD dangle will still be within
100 * the SOP block
101 * 2. We cannot wrap except on a block boundary.
102 */
103 } else {
104 /* QWORD data extends _to_ or beyond the SOP block */
105
106 /* write 8-byte SOP chunk data */
107 while (dest < send) {
108 writeq(*(u64 *)from, dest);
109 from += sizeof(u64);
110 dest += sizeof(u64);
111 }
112 /* drop out of the SOP range */
113 dest -= SOP_DISTANCE;
114 dend -= SOP_DISTANCE;
115
116 /*
117 * If the wrap comes before or matches the data end,
118 * copy until until the wrap, then wrap.
119 *
120 * If the data ends at the end of the SOP above and
121 * the buffer wraps, then pbuf->end == dend == dest
122 * and nothing will get written, but we will wrap in
123 * case there is a dangling DWORD.
124 */
125 if (pbuf->end <= dend) {
126 while (dest < pbuf->end) {
127 writeq(*(u64 *)from, dest);
128 from += sizeof(u64);
129 dest += sizeof(u64);
130 }
131
132 dest -= pbuf->size;
133 dend -= pbuf->size;
134 }
135
136 /* write 8-byte non-SOP, non-wrap chunk data */
137 while (dest < dend) {
138 writeq(*(u64 *)from, dest);
139 from += sizeof(u64);
140 dest += sizeof(u64);
141 }
142 }
143 /* at this point we have wrapped if we are going to wrap */
144
145 /* write dangling u32, if any */
146 if (count & 1) {
147 union mix val;
148
149 val.val64 = 0;
150 val.val32[0] = *(u32 *)from;
151 writeq(val.val64, dest);
152 dest += sizeof(u64);
153 }
Jubin John4d114fd2016-02-14 20:21:43 -0800154 /*
155 * fill in rest of block, no need to check pbuf->end
156 * as we only wrap on a block boundary
157 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400158 while (((unsigned long)dest & PIO_BLOCK_MASK) != 0) {
159 writeq(0, dest);
160 dest += sizeof(u64);
161 }
162
163 /* finished with this buffer */
Mike Marciniszyna0543742015-12-07 15:39:22 -0500164 this_cpu_dec(*pbuf->sc->buffers_allocated);
165 preempt_enable();
Mike Marciniszyn77241052015-07-30 15:17:43 -0400166}
167
168/* USE_SHIFTS is faster in user-space tests on a Xeon X5570 @ 2.93GHz */
169#define USE_SHIFTS 1
170#ifdef USE_SHIFTS
171/*
172 * Handle carry bytes using shifts and masks.
173 *
174 * NOTE: the value the unused portion of carry is expected to always be zero.
175 */
176
177/*
178 * "zero" shift - bit shift used to zero out upper bytes. Input is
179 * the count of LSB bytes to preserve.
180 */
Jubin John8638b772016-02-14 20:19:24 -0800181#define zshift(x) (8 * (8 - (x)))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400182
183/*
184 * "merge" shift - bit shift used to merge with carry bytes. Input is
185 * the LSB byte count to move beyond.
186 */
187#define mshift(x) (8 * (x))
188
189/*
190 * Read nbytes bytes from "from" and return them in the LSB bytes
191 * of pbuf->carry. Other bytes are zeroed. Any previous value
192 * pbuf->carry is lost.
193 *
194 * NOTES:
195 * o do not read from from if nbytes is zero
196 * o from may _not_ be u64 aligned
197 * o nbytes must not span a QW boundary
198 */
199static inline void read_low_bytes(struct pio_buf *pbuf, const void *from,
Jubin John17fb4f22016-02-14 20:21:52 -0800200 unsigned int nbytes)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400201{
202 unsigned long off;
203
204 if (nbytes == 0) {
205 pbuf->carry.val64 = 0;
206 } else {
207 /* align our pointer */
208 off = (unsigned long)from & 0x7;
209 from = (void *)((unsigned long)from & ~0x7l);
210 pbuf->carry.val64 = ((*(u64 *)from)
211 << zshift(nbytes + off))/* zero upper bytes */
212 >> zshift(nbytes); /* place at bottom */
213 }
214 pbuf->carry_bytes = nbytes;
215}
216
217/*
218 * Read nbytes bytes from "from" and put them at the next significant bytes
219 * of pbuf->carry. Unused bytes are zeroed. It is expected that the extra
220 * read does not overfill carry.
221 *
222 * NOTES:
223 * o from may _not_ be u64 aligned
224 * o nbytes may span a QW boundary
225 */
226static inline void read_extra_bytes(struct pio_buf *pbuf,
Jubin John17fb4f22016-02-14 20:21:52 -0800227 const void *from, unsigned int nbytes)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400228{
229 unsigned long off = (unsigned long)from & 0x7;
230 unsigned int room, xbytes;
231
232 /* align our pointer */
233 from = (void *)((unsigned long)from & ~0x7l);
234
235 /* check count first - don't read anything if count is zero */
236 while (nbytes) {
237 /* find the number of bytes in this u64 */
238 room = 8 - off; /* this u64 has room for this many bytes */
Bhumika Goyalc754db402016-02-26 15:34:31 +0530239 xbytes = min(room, nbytes);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400240
241 /*
242 * shift down to zero lower bytes, shift up to zero upper
243 * bytes, shift back down to move into place
244 */
245 pbuf->carry.val64 |= (((*(u64 *)from)
246 >> mshift(off))
247 << zshift(xbytes))
Jubin John8638b772016-02-14 20:19:24 -0800248 >> zshift(xbytes + pbuf->carry_bytes);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400249 off = 0;
250 pbuf->carry_bytes += xbytes;
251 nbytes -= xbytes;
252 from += sizeof(u64);
253 }
254}
255
256/*
Mike Marciniszyn77241052015-07-30 15:17:43 -0400257 * Write a quad word using parts of pbuf->carry and the next 8 bytes of src.
258 * Put the unused part of the next 8 bytes of src into the LSB bytes of
259 * pbuf->carry with the upper bytes zeroed..
260 *
261 * NOTES:
262 * o result must keep unused bytes zeroed
263 * o src must be u64 aligned
264 */
265static inline void merge_write8(
266 struct pio_buf *pbuf,
267 void __iomem *dest,
268 const void *src)
269{
270 u64 new, temp;
271
272 new = *(u64 *)src;
273 temp = pbuf->carry.val64 | (new << mshift(pbuf->carry_bytes));
274 writeq(temp, dest);
275 pbuf->carry.val64 = new >> zshift(pbuf->carry_bytes);
276}
277
278/*
279 * Write a quad word using all bytes of carry.
280 */
281static inline void carry8_write8(union mix carry, void __iomem *dest)
282{
283 writeq(carry.val64, dest);
284}
285
286/*
287 * Write a quad word using all the valid bytes of carry. If carry
288 * has zero valid bytes, nothing is written.
289 * Returns 0 on nothing written, non-zero on quad word written.
290 */
291static inline int carry_write8(struct pio_buf *pbuf, void __iomem *dest)
292{
293 if (pbuf->carry_bytes) {
294 /* unused bytes are always kept zeroed, so just write */
295 writeq(pbuf->carry.val64, dest);
296 return 1;
297 }
298
299 return 0;
300}
301
302#else /* USE_SHIFTS */
303/*
304 * Handle carry bytes using byte copies.
305 *
306 * NOTE: the value the unused portion of carry is left uninitialized.
307 */
308
309/*
310 * Jump copy - no-loop copy for < 8 bytes.
311 */
312static inline void jcopy(u8 *dest, const u8 *src, u32 n)
313{
314 switch (n) {
315 case 7:
316 *dest++ = *src++;
317 case 6:
318 *dest++ = *src++;
319 case 5:
320 *dest++ = *src++;
321 case 4:
322 *dest++ = *src++;
323 case 3:
324 *dest++ = *src++;
325 case 2:
326 *dest++ = *src++;
327 case 1:
328 *dest++ = *src++;
329 }
330}
331
332/*
333 * Read nbytes from "from" and and place them in the low bytes
334 * of pbuf->carry. Other bytes are left as-is. Any previous
335 * value in pbuf->carry is lost.
336 *
337 * NOTES:
338 * o do not read from from if nbytes is zero
339 * o from may _not_ be u64 aligned.
340 */
341static inline void read_low_bytes(struct pio_buf *pbuf, const void *from,
Jubin John17fb4f22016-02-14 20:21:52 -0800342 unsigned int nbytes)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400343{
344 jcopy(&pbuf->carry.val8[0], from, nbytes);
345 pbuf->carry_bytes = nbytes;
346}
347
348/*
349 * Read nbytes bytes from "from" and put them at the end of pbuf->carry.
350 * It is expected that the extra read does not overfill carry.
351 *
352 * NOTES:
353 * o from may _not_ be u64 aligned
354 * o nbytes may span a QW boundary
355 */
356static inline void read_extra_bytes(struct pio_buf *pbuf,
Jubin John17fb4f22016-02-14 20:21:52 -0800357 const void *from, unsigned int nbytes)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400358{
359 jcopy(&pbuf->carry.val8[pbuf->carry_bytes], from, nbytes);
360 pbuf->carry_bytes += nbytes;
361}
362
363/*
Mike Marciniszyn77241052015-07-30 15:17:43 -0400364 * Write a quad word using parts of pbuf->carry and the next 8 bytes of src.
365 * Put the unused part of the next 8 bytes of src into the low bytes of
366 * pbuf->carry.
367 */
368static inline void merge_write8(
369 struct pio_buf *pbuf,
370 void *dest,
371 const void *src)
372{
373 u32 remainder = 8 - pbuf->carry_bytes;
374
375 jcopy(&pbuf->carry.val8[pbuf->carry_bytes], src, remainder);
376 writeq(pbuf->carry.val64, dest);
Jubin John8638b772016-02-14 20:19:24 -0800377 jcopy(&pbuf->carry.val8[0], src + remainder, pbuf->carry_bytes);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400378}
379
380/*
381 * Write a quad word using all bytes of carry.
382 */
383static inline void carry8_write8(union mix carry, void *dest)
384{
385 writeq(carry.val64, dest);
386}
387
388/*
389 * Write a quad word using all the valid bytes of carry. If carry
390 * has zero valid bytes, nothing is written.
391 * Returns 0 on nothing written, non-zero on quad word written.
392 */
393static inline int carry_write8(struct pio_buf *pbuf, void *dest)
394{
395 if (pbuf->carry_bytes) {
396 u64 zero = 0;
397
398 jcopy(&pbuf->carry.val8[pbuf->carry_bytes], (u8 *)&zero,
Jubin John17fb4f22016-02-14 20:21:52 -0800399 8 - pbuf->carry_bytes);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400400 writeq(pbuf->carry.val64, dest);
401 return 1;
402 }
403
404 return 0;
405}
406#endif /* USE_SHIFTS */
407
408/*
409 * Segmented PIO Copy - start
410 *
411 * Start a PIO copy.
412 *
413 * @pbuf: destination buffer
414 * @pbc: the PBC for the PIO buffer
415 * @from: data source, QWORD aligned
416 * @nbytes: bytes to copy
417 */
418void seg_pio_copy_start(struct pio_buf *pbuf, u64 pbc,
Jubin John17fb4f22016-02-14 20:21:52 -0800419 const void *from, size_t nbytes)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400420{
421 void __iomem *dest = pbuf->start + SOP_DISTANCE;
422 void __iomem *send = dest + PIO_BLOCK_SIZE;
423 void __iomem *dend; /* 8-byte data end */
424
425 writeq(pbc, dest);
426 dest += sizeof(u64);
427
428 /* calculate where the QWORD data ends - in SOP=1 space */
Jubin John8638b772016-02-14 20:19:24 -0800429 dend = dest + ((nbytes >> 3) * sizeof(u64));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400430
431 if (dend < send) {
Jubin John4d114fd2016-02-14 20:21:43 -0800432 /*
433 * all QWORD data is within the SOP block, does *not*
434 * reach the end of the SOP block
435 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400436
437 while (dest < dend) {
438 writeq(*(u64 *)from, dest);
439 from += sizeof(u64);
440 dest += sizeof(u64);
441 }
442 /*
443 * No boundary checks are needed here:
444 * 0. We're not on the SOP block boundary
445 * 1. The possible DWORD dangle will still be within
446 * the SOP block
447 * 2. We cannot wrap except on a block boundary.
448 */
449 } else {
450 /* QWORD data extends _to_ or beyond the SOP block */
451
452 /* write 8-byte SOP chunk data */
453 while (dest < send) {
454 writeq(*(u64 *)from, dest);
455 from += sizeof(u64);
456 dest += sizeof(u64);
457 }
458 /* drop out of the SOP range */
459 dest -= SOP_DISTANCE;
460 dend -= SOP_DISTANCE;
461
462 /*
463 * If the wrap comes before or matches the data end,
464 * copy until until the wrap, then wrap.
465 *
466 * If the data ends at the end of the SOP above and
467 * the buffer wraps, then pbuf->end == dend == dest
468 * and nothing will get written, but we will wrap in
469 * case there is a dangling DWORD.
470 */
471 if (pbuf->end <= dend) {
472 while (dest < pbuf->end) {
473 writeq(*(u64 *)from, dest);
474 from += sizeof(u64);
475 dest += sizeof(u64);
476 }
477
478 dest -= pbuf->size;
479 dend -= pbuf->size;
480 }
481
482 /* write 8-byte non-SOP, non-wrap chunk data */
483 while (dest < dend) {
484 writeq(*(u64 *)from, dest);
485 from += sizeof(u64);
486 dest += sizeof(u64);
487 }
488 }
489 /* at this point we have wrapped if we are going to wrap */
490
491 /* ...but it doesn't matter as we're done writing */
492
493 /* save dangling bytes, if any */
494 read_low_bytes(pbuf, from, nbytes & 0x7);
495
496 pbuf->qw_written = 1 /*PBC*/ + (nbytes >> 3);
497}
498
499/*
500 * Mid copy helper, "mixed case" - source is 64-bit aligned but carry
501 * bytes are non-zero.
502 *
503 * Whole u64s must be written to the chip, so bytes must be manually merged.
504 *
505 * @pbuf: destination buffer
506 * @from: data source, is QWORD aligned.
507 * @nbytes: bytes to copy
508 *
509 * Must handle nbytes < 8.
510 */
511static void mid_copy_mix(struct pio_buf *pbuf, const void *from, size_t nbytes)
512{
513 void __iomem *dest = pbuf->start + (pbuf->qw_written * sizeof(u64));
514 void __iomem *dend; /* 8-byte data end */
Sebastian Sancheza4309d92016-09-25 07:41:32 -0700515 unsigned long qw_to_write = nbytes >> 3;
516 unsigned long bytes_left = nbytes & 0x7;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400517
518 /* calculate 8-byte data end */
519 dend = dest + (qw_to_write * sizeof(u64));
520
521 if (pbuf->qw_written < PIO_BLOCK_QWS) {
522 /*
523 * Still within SOP block. We don't need to check for
524 * wrap because we are still in the first block and
525 * can only wrap on block boundaries.
526 */
527 void __iomem *send; /* SOP end */
528 void __iomem *xend;
529
Jubin John4d114fd2016-02-14 20:21:43 -0800530 /*
531 * calculate the end of data or end of block, whichever
532 * comes first
533 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400534 send = pbuf->start + PIO_BLOCK_SIZE;
Bhumika Goyalc754db402016-02-26 15:34:31 +0530535 xend = min(send, dend);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400536
537 /* shift up to SOP=1 space */
538 dest += SOP_DISTANCE;
539 xend += SOP_DISTANCE;
540
541 /* write 8-byte chunk data */
542 while (dest < xend) {
543 merge_write8(pbuf, dest, from);
544 from += sizeof(u64);
545 dest += sizeof(u64);
546 }
547
548 /* shift down to SOP=0 space */
549 dest -= SOP_DISTANCE;
550 }
551 /*
552 * At this point dest could be (either, both, or neither):
553 * - at dend
554 * - at the wrap
555 */
556
557 /*
558 * If the wrap comes before or matches the data end,
559 * copy until until the wrap, then wrap.
560 *
561 * If dest is at the wrap, we will fall into the if,
562 * not do the loop, when wrap.
563 *
564 * If the data ends at the end of the SOP above and
565 * the buffer wraps, then pbuf->end == dend == dest
566 * and nothing will get written.
567 */
568 if (pbuf->end <= dend) {
569 while (dest < pbuf->end) {
570 merge_write8(pbuf, dest, from);
571 from += sizeof(u64);
572 dest += sizeof(u64);
573 }
574
575 dest -= pbuf->size;
576 dend -= pbuf->size;
577 }
578
579 /* write 8-byte non-SOP, non-wrap chunk data */
580 while (dest < dend) {
581 merge_write8(pbuf, dest, from);
582 from += sizeof(u64);
583 dest += sizeof(u64);
584 }
585
Mike Marciniszyn77241052015-07-30 15:17:43 -0400586 pbuf->qw_written += qw_to_write;
Sebastian Sancheza4309d92016-09-25 07:41:32 -0700587
588 /* handle carry and left-over bytes */
589 if (pbuf->carry_bytes + bytes_left >= 8) {
590 unsigned long nread;
591
592 /* there is enough to fill another qw - fill carry */
593 nread = 8 - pbuf->carry_bytes;
594 read_extra_bytes(pbuf, from, nread);
595
596 /*
597 * One more write - but need to make sure dest is correct.
598 * Check for wrap and the possibility the write
599 * should be in SOP space.
600 *
601 * The two checks immediately below cannot both be true, hence
602 * the else. If we have wrapped, we cannot still be within the
603 * first block. Conversely, if we are still in the first block,
604 * we cannot have wrapped. We do the wrap check first as that
605 * is more likely.
606 */
607 /* adjust if we have wrapped */
608 if (dest >= pbuf->end)
609 dest -= pbuf->size;
610 /* jump to the SOP range if within the first block */
611 else if (pbuf->qw_written < PIO_BLOCK_QWS)
612 dest += SOP_DISTANCE;
613
614 /* flush out full carry */
615 carry8_write8(pbuf->carry, dest);
616 pbuf->qw_written++;
617
618 /* now adjust and read the rest of the bytes into carry */
619 bytes_left -= nread;
620 from += nread; /* from is now not aligned */
621 read_low_bytes(pbuf, from, bytes_left);
622 } else {
623 /* not enough to fill another qw, append the rest to carry */
624 read_extra_bytes(pbuf, from, bytes_left);
625 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400626}
627
628/*
629 * Mid copy helper, "straight case" - source pointer is 64-bit aligned
630 * with no carry bytes.
631 *
632 * @pbuf: destination buffer
633 * @from: data source, is QWORD aligned
634 * @nbytes: bytes to copy
635 *
636 * Must handle nbytes < 8.
637 */
638static void mid_copy_straight(struct pio_buf *pbuf,
Jubin John17fb4f22016-02-14 20:21:52 -0800639 const void *from, size_t nbytes)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400640{
641 void __iomem *dest = pbuf->start + (pbuf->qw_written * sizeof(u64));
642 void __iomem *dend; /* 8-byte data end */
643
644 /* calculate 8-byte data end */
Jubin John8638b772016-02-14 20:19:24 -0800645 dend = dest + ((nbytes >> 3) * sizeof(u64));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400646
647 if (pbuf->qw_written < PIO_BLOCK_QWS) {
648 /*
649 * Still within SOP block. We don't need to check for
650 * wrap because we are still in the first block and
651 * can only wrap on block boundaries.
652 */
653 void __iomem *send; /* SOP end */
654 void __iomem *xend;
655
Jubin John4d114fd2016-02-14 20:21:43 -0800656 /*
657 * calculate the end of data or end of block, whichever
658 * comes first
659 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400660 send = pbuf->start + PIO_BLOCK_SIZE;
Bhumika Goyalc754db402016-02-26 15:34:31 +0530661 xend = min(send, dend);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400662
663 /* shift up to SOP=1 space */
664 dest += SOP_DISTANCE;
665 xend += SOP_DISTANCE;
666
667 /* write 8-byte chunk data */
668 while (dest < xend) {
669 writeq(*(u64 *)from, dest);
670 from += sizeof(u64);
671 dest += sizeof(u64);
672 }
673
674 /* shift down to SOP=0 space */
675 dest -= SOP_DISTANCE;
676 }
677 /*
678 * At this point dest could be (either, both, or neither):
679 * - at dend
680 * - at the wrap
681 */
682
683 /*
684 * If the wrap comes before or matches the data end,
685 * copy until until the wrap, then wrap.
686 *
687 * If dest is at the wrap, we will fall into the if,
688 * not do the loop, when wrap.
689 *
690 * If the data ends at the end of the SOP above and
691 * the buffer wraps, then pbuf->end == dend == dest
692 * and nothing will get written.
693 */
694 if (pbuf->end <= dend) {
695 while (dest < pbuf->end) {
696 writeq(*(u64 *)from, dest);
697 from += sizeof(u64);
698 dest += sizeof(u64);
699 }
700
701 dest -= pbuf->size;
702 dend -= pbuf->size;
703 }
704
705 /* write 8-byte non-SOP, non-wrap chunk data */
706 while (dest < dend) {
707 writeq(*(u64 *)from, dest);
708 from += sizeof(u64);
709 dest += sizeof(u64);
710 }
711
712 /* we know carry_bytes was zero on entry to this routine */
713 read_low_bytes(pbuf, from, nbytes & 0x7);
714
Jubin John8638b772016-02-14 20:19:24 -0800715 pbuf->qw_written += nbytes >> 3;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400716}
717
718/*
719 * Segmented PIO Copy - middle
720 *
721 * Must handle any aligned tail and any aligned source with any byte count.
722 *
723 * @pbuf: a number of blocks allocated within a PIO send context
724 * @from: data source
725 * @nbytes: number of bytes to copy
726 */
727void seg_pio_copy_mid(struct pio_buf *pbuf, const void *from, size_t nbytes)
728{
729 unsigned long from_align = (unsigned long)from & 0x7;
730
731 if (pbuf->carry_bytes + nbytes < 8) {
732 /* not enough bytes to fill a QW */
733 read_extra_bytes(pbuf, from, nbytes);
734 return;
735 }
736
737 if (from_align) {
738 /* misaligned source pointer - align it */
739 unsigned long to_align;
740
741 /* bytes to read to align "from" */
742 to_align = 8 - from_align;
743
744 /*
745 * In the advance-to-alignment logic below, we do not need
746 * to check if we are using more than nbytes. This is because
747 * if we are here, we already know that carry+nbytes will
748 * fill at least one QW.
749 */
750 if (pbuf->carry_bytes + to_align < 8) {
751 /* not enough align bytes to fill a QW */
752 read_extra_bytes(pbuf, from, to_align);
753 from += to_align;
754 nbytes -= to_align;
755 } else {
756 /* bytes to fill carry */
757 unsigned long to_fill = 8 - pbuf->carry_bytes;
758 /* bytes left over to be read */
759 unsigned long extra = to_align - to_fill;
760 void __iomem *dest;
761
762 /* fill carry... */
763 read_extra_bytes(pbuf, from, to_fill);
764 from += to_fill;
765 nbytes -= to_fill;
Sebastian Sanchez3e6c3b02016-08-31 07:24:20 -0700766 /* may not be enough valid bytes left to align */
767 if (extra > nbytes)
768 extra = nbytes;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400769
770 /* ...now write carry */
771 dest = pbuf->start + (pbuf->qw_written * sizeof(u64));
772
773 /*
774 * The two checks immediately below cannot both be
775 * true, hence the else. If we have wrapped, we
776 * cannot still be within the first block.
777 * Conversely, if we are still in the first block, we
778 * cannot have wrapped. We do the wrap check first
779 * as that is more likely.
780 */
781 /* adjust if we've wrapped */
782 if (dest >= pbuf->end)
783 dest -= pbuf->size;
784 /* jump to SOP range if within the first block */
785 else if (pbuf->qw_written < PIO_BLOCK_QWS)
786 dest += SOP_DISTANCE;
787
788 carry8_write8(pbuf->carry, dest);
789 pbuf->qw_written++;
790
791 /* read any extra bytes to do final alignment */
792 /* this will overwrite anything in pbuf->carry */
793 read_low_bytes(pbuf, from, extra);
794 from += extra;
795 nbytes -= extra;
Sebastian Sanchez3e6c3b02016-08-31 07:24:20 -0700796 /*
797 * If no bytes are left, return early - we are done.
798 * NOTE: This short-circuit is *required* because
799 * "extra" may have been reduced in size and "from"
800 * is not aligned, as required when leaving this
801 * if block.
802 */
803 if (nbytes == 0)
804 return;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400805 }
806
807 /* at this point, from is QW aligned */
808 }
809
810 if (pbuf->carry_bytes)
811 mid_copy_mix(pbuf, from, nbytes);
812 else
813 mid_copy_straight(pbuf, from, nbytes);
814}
815
816/*
817 * Segmented PIO Copy - end
818 *
819 * Write any remainder (in pbuf->carry) and finish writing the whole block.
820 *
821 * @pbuf: a number of blocks allocated within a PIO send context
822 */
823void seg_pio_copy_end(struct pio_buf *pbuf)
824{
825 void __iomem *dest = pbuf->start + (pbuf->qw_written * sizeof(u64));
826
827 /*
828 * The two checks immediately below cannot both be true, hence the
829 * else. If we have wrapped, we cannot still be within the first
830 * block. Conversely, if we are still in the first block, we
831 * cannot have wrapped. We do the wrap check first as that is
832 * more likely.
833 */
834 /* adjust if we have wrapped */
835 if (dest >= pbuf->end)
836 dest -= pbuf->size;
837 /* jump to the SOP range if within the first block */
838 else if (pbuf->qw_written < PIO_BLOCK_QWS)
839 dest += SOP_DISTANCE;
840
841 /* write final bytes, if any */
842 if (carry_write8(pbuf, dest)) {
843 dest += sizeof(u64);
844 /*
845 * NOTE: We do not need to recalculate whether dest needs
846 * SOP_DISTANCE or not.
847 *
848 * If we are in the first block and the dangle write
849 * keeps us in the same block, dest will need
850 * to retain SOP_DISTANCE in the loop below.
851 *
852 * If we are in the first block and the dangle write pushes
853 * us to the next block, then loop below will not run
854 * and dest is not used. Hence we do not need to update
855 * it.
856 *
857 * If we are past the first block, then SOP_DISTANCE
858 * was never added, so there is nothing to do.
859 */
860 }
861
862 /* fill in rest of block */
863 while (((unsigned long)dest & PIO_BLOCK_MASK) != 0) {
864 writeq(0, dest);
865 dest += sizeof(u64);
866 }
867
868 /* finished with this buffer */
Mike Marciniszyna0543742015-12-07 15:39:22 -0500869 this_cpu_dec(*pbuf->sc->buffers_allocated);
870 preempt_enable();
Mike Marciniszyn77241052015-07-30 15:17:43 -0400871}