blob: 86a7f365b62481cc92ead966653feb7a47d66cf2 [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 <linux/delay.h>
49#include "hfi.h"
50#include "qp.h"
51#include "trace.h"
52
53#define SC_CTXT_PACKET_EGRESS_TIMEOUT 350 /* in chip cycles */
54
55#define SC(name) SEND_CTXT_##name
56/*
57 * Send Context functions
58 */
59static void sc_wait_for_packet_egress(struct send_context *sc, int pause);
60
61/*
62 * Set the CM reset bit and wait for it to clear. Use the provided
63 * sendctrl register. This routine has no locking.
64 */
65void __cm_reset(struct hfi1_devdata *dd, u64 sendctrl)
66{
67 write_csr(dd, SEND_CTRL, sendctrl | SEND_CTRL_CM_RESET_SMASK);
68 while (1) {
69 udelay(1);
70 sendctrl = read_csr(dd, SEND_CTRL);
71 if ((sendctrl & SEND_CTRL_CM_RESET_SMASK) == 0)
72 break;
73 }
74}
75
76/* defined in header release 48 and higher */
77#ifndef SEND_CTRL_UNSUPPORTED_VL_SHIFT
78#define SEND_CTRL_UNSUPPORTED_VL_SHIFT 3
79#define SEND_CTRL_UNSUPPORTED_VL_MASK 0xffull
80#define SEND_CTRL_UNSUPPORTED_VL_SMASK (SEND_CTRL_UNSUPPORTED_VL_MASK \
81 << SEND_CTRL_UNSUPPORTED_VL_SHIFT)
82#endif
83
84/* global control of PIO send */
85void pio_send_control(struct hfi1_devdata *dd, int op)
86{
87 u64 reg, mask;
88 unsigned long flags;
89 int write = 1; /* write sendctrl back */
90 int flush = 0; /* re-read sendctrl to make sure it is flushed */
91
92 spin_lock_irqsave(&dd->sendctrl_lock, flags);
93
94 reg = read_csr(dd, SEND_CTRL);
95 switch (op) {
96 case PSC_GLOBAL_ENABLE:
97 reg |= SEND_CTRL_SEND_ENABLE_SMASK;
98 /* Fall through */
99 case PSC_DATA_VL_ENABLE:
100 /* Disallow sending on VLs not enabled */
Jubin John8638b772016-02-14 20:19:24 -0800101 mask = (((~0ull) << num_vls) & SEND_CTRL_UNSUPPORTED_VL_MASK) <<
Mike Marciniszyn77241052015-07-30 15:17:43 -0400102 SEND_CTRL_UNSUPPORTED_VL_SHIFT;
103 reg = (reg & ~SEND_CTRL_UNSUPPORTED_VL_SMASK) | mask;
104 break;
105 case PSC_GLOBAL_DISABLE:
106 reg &= ~SEND_CTRL_SEND_ENABLE_SMASK;
107 break;
108 case PSC_GLOBAL_VLARB_ENABLE:
109 reg |= SEND_CTRL_VL_ARBITER_ENABLE_SMASK;
110 break;
111 case PSC_GLOBAL_VLARB_DISABLE:
112 reg &= ~SEND_CTRL_VL_ARBITER_ENABLE_SMASK;
113 break;
114 case PSC_CM_RESET:
115 __cm_reset(dd, reg);
116 write = 0; /* CSR already written (and flushed) */
117 break;
118 case PSC_DATA_VL_DISABLE:
119 reg |= SEND_CTRL_UNSUPPORTED_VL_SMASK;
120 flush = 1;
121 break;
122 default:
123 dd_dev_err(dd, "%s: invalid control %d\n", __func__, op);
124 break;
125 }
126
127 if (write) {
128 write_csr(dd, SEND_CTRL, reg);
129 if (flush)
Jubin John50e5dcb2016-02-14 20:19:41 -0800130 (void)read_csr(dd, SEND_CTRL); /* flush write */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400131 }
132
133 spin_unlock_irqrestore(&dd->sendctrl_lock, flags);
134}
135
136/* number of send context memory pools */
137#define NUM_SC_POOLS 2
138
139/* Send Context Size (SCS) wildcards */
140#define SCS_POOL_0 -1
141#define SCS_POOL_1 -2
Jianxin Xiong44306f12016-04-12 11:30:28 -0700142
Mike Marciniszyn77241052015-07-30 15:17:43 -0400143/* Send Context Count (SCC) wildcards */
144#define SCC_PER_VL -1
145#define SCC_PER_CPU -2
Mike Marciniszyn77241052015-07-30 15:17:43 -0400146#define SCC_PER_KRCVQ -3
Jianxin Xiong44306f12016-04-12 11:30:28 -0700147
148/* Send Context Size (SCS) constants */
149#define SCS_ACK_CREDITS 32
150#define SCS_VL15_CREDITS 102 /* 3 pkts of 2048B data + 128B header */
151
152#define PIO_THRESHOLD_CEILING 4096
Mike Marciniszyn77241052015-07-30 15:17:43 -0400153
154#define PIO_WAIT_BATCH_SIZE 5
155
156/* default send context sizes */
157static struct sc_config_sizes sc_config_sizes[SC_MAX] = {
158 [SC_KERNEL] = { .size = SCS_POOL_0, /* even divide, pool 0 */
Jianxin Xiong44306f12016-04-12 11:30:28 -0700159 .count = SCC_PER_VL }, /* one per NUMA */
160 [SC_ACK] = { .size = SCS_ACK_CREDITS,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400161 .count = SCC_PER_KRCVQ },
162 [SC_USER] = { .size = SCS_POOL_0, /* even divide, pool 0 */
163 .count = SCC_PER_CPU }, /* one per CPU */
Jianxin Xiong44306f12016-04-12 11:30:28 -0700164 [SC_VL15] = { .size = SCS_VL15_CREDITS,
165 .count = 1 },
Mike Marciniszyn77241052015-07-30 15:17:43 -0400166
167};
168
169/* send context memory pool configuration */
170struct mem_pool_config {
171 int centipercent; /* % of memory, in 100ths of 1% */
172 int absolute_blocks; /* absolute block count */
173};
174
175/* default memory pool configuration: 100% in pool 0 */
176static struct mem_pool_config sc_mem_pool_config[NUM_SC_POOLS] = {
177 /* centi%, abs blocks */
178 { 10000, -1 }, /* pool 0 */
179 { 0, -1 }, /* pool 1 */
180};
181
182/* memory pool information, used when calculating final sizes */
183struct mem_pool_info {
Jubin John4d114fd2016-02-14 20:21:43 -0800184 int centipercent; /*
185 * 100th of 1% of memory to use, -1 if blocks
186 * already set
187 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400188 int count; /* count of contexts in the pool */
189 int blocks; /* block size of the pool */
190 int size; /* context size, in blocks */
191};
192
193/*
194 * Convert a pool wildcard to a valid pool index. The wildcards
195 * start at -1 and increase negatively. Map them as:
196 * -1 => 0
197 * -2 => 1
198 * etc.
199 *
200 * Return -1 on non-wildcard input, otherwise convert to a pool number.
201 */
202static int wildcard_to_pool(int wc)
203{
204 if (wc >= 0)
205 return -1; /* non-wildcard */
206 return -wc - 1;
207}
208
209static const char *sc_type_names[SC_MAX] = {
210 "kernel",
211 "ack",
Jianxin Xiong44306f12016-04-12 11:30:28 -0700212 "user",
213 "vl15"
Mike Marciniszyn77241052015-07-30 15:17:43 -0400214};
215
216static const char *sc_type_name(int index)
217{
218 if (index < 0 || index >= SC_MAX)
219 return "unknown";
220 return sc_type_names[index];
221}
222
223/*
224 * Read the send context memory pool configuration and send context
225 * size configuration. Replace any wildcards and come up with final
226 * counts and sizes for the send context types.
227 */
228int init_sc_pools_and_sizes(struct hfi1_devdata *dd)
229{
230 struct mem_pool_info mem_pool_info[NUM_SC_POOLS] = { { 0 } };
231 int total_blocks = (dd->chip_pio_mem_size / PIO_BLOCK_SIZE) - 1;
232 int total_contexts = 0;
233 int fixed_blocks;
234 int pool_blocks;
235 int used_blocks;
236 int cp_total; /* centipercent total */
237 int ab_total; /* absolute block total */
238 int extra;
239 int i;
240
241 /*
Jianxin Xiong44306f12016-04-12 11:30:28 -0700242 * When SDMA is enabled, kernel context pio packet size is capped by
243 * "piothreshold". Reduce pio buffer allocation for kernel context by
244 * setting it to a fixed size. The allocation allows 3-deep buffering
245 * of the largest pio packets plus up to 128 bytes header, sufficient
246 * to maintain verbs performance.
247 *
248 * When SDMA is disabled, keep the default pooling allocation.
249 */
250 if (HFI1_CAP_IS_KSET(SDMA)) {
251 u16 max_pkt_size = (piothreshold < PIO_THRESHOLD_CEILING) ?
252 piothreshold : PIO_THRESHOLD_CEILING;
253 sc_config_sizes[SC_KERNEL].size =
254 3 * (max_pkt_size + 128) / PIO_BLOCK_SIZE;
255 }
256
257 /*
Mike Marciniszyn77241052015-07-30 15:17:43 -0400258 * Step 0:
259 * - copy the centipercents/absolute sizes from the pool config
260 * - sanity check these values
261 * - add up centipercents, then later check for full value
262 * - add up absolute blocks, then later check for over-commit
263 */
264 cp_total = 0;
265 ab_total = 0;
266 for (i = 0; i < NUM_SC_POOLS; i++) {
267 int cp = sc_mem_pool_config[i].centipercent;
268 int ab = sc_mem_pool_config[i].absolute_blocks;
269
270 /*
271 * A negative value is "unused" or "invalid". Both *can*
272 * be valid, but centipercent wins, so check that first
273 */
274 if (cp >= 0) { /* centipercent valid */
275 cp_total += cp;
276 } else if (ab >= 0) { /* absolute blocks valid */
277 ab_total += ab;
278 } else { /* neither valid */
279 dd_dev_err(
280 dd,
281 "Send context memory pool %d: both the block count and centipercent are invalid\n",
282 i);
283 return -EINVAL;
284 }
285
286 mem_pool_info[i].centipercent = cp;
287 mem_pool_info[i].blocks = ab;
288 }
289
290 /* do not use both % and absolute blocks for different pools */
291 if (cp_total != 0 && ab_total != 0) {
292 dd_dev_err(
293 dd,
294 "All send context memory pools must be described as either centipercent or blocks, no mixing between pools\n");
295 return -EINVAL;
296 }
297
298 /* if any percentages are present, they must add up to 100% x 100 */
299 if (cp_total != 0 && cp_total != 10000) {
300 dd_dev_err(
301 dd,
302 "Send context memory pool centipercent is %d, expecting 10000\n",
303 cp_total);
304 return -EINVAL;
305 }
306
307 /* the absolute pool total cannot be more than the mem total */
308 if (ab_total > total_blocks) {
309 dd_dev_err(
310 dd,
311 "Send context memory pool absolute block count %d is larger than the memory size %d\n",
312 ab_total, total_blocks);
313 return -EINVAL;
314 }
315
316 /*
317 * Step 2:
318 * - copy from the context size config
319 * - replace context type wildcard counts with real values
320 * - add up non-memory pool block sizes
321 * - add up memory pool user counts
322 */
323 fixed_blocks = 0;
324 for (i = 0; i < SC_MAX; i++) {
325 int count = sc_config_sizes[i].count;
326 int size = sc_config_sizes[i].size;
327 int pool;
328
329 /*
330 * Sanity check count: Either a positive value or
331 * one of the expected wildcards is valid. The positive
332 * value is checked later when we compare against total
333 * memory available.
334 */
335 if (i == SC_ACK) {
336 count = dd->n_krcv_queues;
337 } else if (i == SC_KERNEL) {
Jianxin Xiong44306f12016-04-12 11:30:28 -0700338 count = INIT_SC_PER_VL * num_vls;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400339 } else if (count == SCC_PER_CPU) {
340 count = dd->num_rcv_contexts - dd->n_krcv_queues;
341 } else if (count < 0) {
342 dd_dev_err(
343 dd,
344 "%s send context invalid count wildcard %d\n",
345 sc_type_name(i), count);
346 return -EINVAL;
347 }
348 if (total_contexts + count > dd->chip_send_contexts)
349 count = dd->chip_send_contexts - total_contexts;
350
351 total_contexts += count;
352
353 /*
354 * Sanity check pool: The conversion will return a pool
355 * number or -1 if a fixed (non-negative) value. The fixed
356 * value is checked later when we compare against
357 * total memory available.
358 */
359 pool = wildcard_to_pool(size);
360 if (pool == -1) { /* non-wildcard */
361 fixed_blocks += size * count;
362 } else if (pool < NUM_SC_POOLS) { /* valid wildcard */
363 mem_pool_info[pool].count += count;
364 } else { /* invalid wildcard */
365 dd_dev_err(
366 dd,
367 "%s send context invalid pool wildcard %d\n",
368 sc_type_name(i), size);
369 return -EINVAL;
370 }
371
372 dd->sc_sizes[i].count = count;
373 dd->sc_sizes[i].size = size;
374 }
375 if (fixed_blocks > total_blocks) {
376 dd_dev_err(
377 dd,
378 "Send context fixed block count, %u, larger than total block count %u\n",
379 fixed_blocks, total_blocks);
380 return -EINVAL;
381 }
382
383 /* step 3: calculate the blocks in the pools, and pool context sizes */
384 pool_blocks = total_blocks - fixed_blocks;
385 if (ab_total > pool_blocks) {
386 dd_dev_err(
387 dd,
388 "Send context fixed pool sizes, %u, larger than pool block count %u\n",
389 ab_total, pool_blocks);
390 return -EINVAL;
391 }
392 /* subtract off the fixed pool blocks */
393 pool_blocks -= ab_total;
394
395 for (i = 0; i < NUM_SC_POOLS; i++) {
396 struct mem_pool_info *pi = &mem_pool_info[i];
397
398 /* % beats absolute blocks */
399 if (pi->centipercent >= 0)
400 pi->blocks = (pool_blocks * pi->centipercent) / 10000;
401
402 if (pi->blocks == 0 && pi->count != 0) {
403 dd_dev_err(
404 dd,
405 "Send context memory pool %d has %u contexts, but no blocks\n",
406 i, pi->count);
407 return -EINVAL;
408 }
409 if (pi->count == 0) {
410 /* warn about wasted blocks */
411 if (pi->blocks != 0)
412 dd_dev_err(
413 dd,
414 "Send context memory pool %d has %u blocks, but zero contexts\n",
415 i, pi->blocks);
416 pi->size = 0;
417 } else {
418 pi->size = pi->blocks / pi->count;
419 }
420 }
421
422 /* step 4: fill in the context type sizes from the pool sizes */
423 used_blocks = 0;
424 for (i = 0; i < SC_MAX; i++) {
425 if (dd->sc_sizes[i].size < 0) {
426 unsigned pool = wildcard_to_pool(dd->sc_sizes[i].size);
427
428 WARN_ON_ONCE(pool >= NUM_SC_POOLS);
429 dd->sc_sizes[i].size = mem_pool_info[pool].size;
430 }
431 /* make sure we are not larger than what is allowed by the HW */
432#define PIO_MAX_BLOCKS 1024
433 if (dd->sc_sizes[i].size > PIO_MAX_BLOCKS)
434 dd->sc_sizes[i].size = PIO_MAX_BLOCKS;
435
436 /* calculate our total usage */
437 used_blocks += dd->sc_sizes[i].size * dd->sc_sizes[i].count;
438 }
439 extra = total_blocks - used_blocks;
440 if (extra != 0)
441 dd_dev_info(dd, "unused send context blocks: %d\n", extra);
442
443 return total_contexts;
444}
445
446int init_send_contexts(struct hfi1_devdata *dd)
447{
448 u16 base;
449 int ret, i, j, context;
450
451 ret = init_credit_return(dd);
452 if (ret)
453 return ret;
454
455 dd->hw_to_sw = kmalloc_array(TXE_NUM_CONTEXTS, sizeof(u8),
456 GFP_KERNEL);
457 dd->send_contexts = kcalloc(dd->num_send_contexts,
458 sizeof(struct send_context_info),
459 GFP_KERNEL);
460 if (!dd->send_contexts || !dd->hw_to_sw) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400461 kfree(dd->hw_to_sw);
462 kfree(dd->send_contexts);
463 free_credit_return(dd);
464 return -ENOMEM;
465 }
466
467 /* hardware context map starts with invalid send context indices */
468 for (i = 0; i < TXE_NUM_CONTEXTS; i++)
469 dd->hw_to_sw[i] = INVALID_SCI;
470
471 /*
472 * All send contexts have their credit sizes. Allocate credits
473 * for each context one after another from the global space.
474 */
475 context = 0;
476 base = 1;
477 for (i = 0; i < SC_MAX; i++) {
478 struct sc_config_sizes *scs = &dd->sc_sizes[i];
479
480 for (j = 0; j < scs->count; j++) {
481 struct send_context_info *sci =
482 &dd->send_contexts[context];
483 sci->type = i;
484 sci->base = base;
485 sci->credits = scs->size;
486
487 context++;
488 base += scs->size;
489 }
490 }
491
492 return 0;
493}
494
495/*
496 * Allocate a software index and hardware context of the given type.
497 *
498 * Must be called with dd->sc_lock held.
499 */
500static int sc_hw_alloc(struct hfi1_devdata *dd, int type, u32 *sw_index,
501 u32 *hw_context)
502{
503 struct send_context_info *sci;
504 u32 index;
505 u32 context;
506
507 for (index = 0, sci = &dd->send_contexts[0];
508 index < dd->num_send_contexts; index++, sci++) {
509 if (sci->type == type && sci->allocated == 0) {
510 sci->allocated = 1;
511 /* use a 1:1 mapping, but make them non-equal */
512 context = dd->chip_send_contexts - index - 1;
513 dd->hw_to_sw[context] = index;
514 *sw_index = index;
515 *hw_context = context;
516 return 0; /* success */
517 }
518 }
519 dd_dev_err(dd, "Unable to locate a free type %d send context\n", type);
520 return -ENOSPC;
521}
522
523/*
524 * Free the send context given by its software index.
525 *
526 * Must be called with dd->sc_lock held.
527 */
528static void sc_hw_free(struct hfi1_devdata *dd, u32 sw_index, u32 hw_context)
529{
530 struct send_context_info *sci;
531
532 sci = &dd->send_contexts[sw_index];
533 if (!sci->allocated) {
534 dd_dev_err(dd, "%s: sw_index %u not allocated? hw_context %u\n",
Jubin John17fb4f22016-02-14 20:21:52 -0800535 __func__, sw_index, hw_context);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400536 }
537 sci->allocated = 0;
538 dd->hw_to_sw[hw_context] = INVALID_SCI;
539}
540
541/* return the base context of a context in a group */
542static inline u32 group_context(u32 context, u32 group)
543{
544 return (context >> group) << group;
545}
546
547/* return the size of a group */
548static inline u32 group_size(u32 group)
549{
550 return 1 << group;
551}
552
553/*
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700554 * Obtain the credit return addresses, kernel virtual and bus, for the
Mike Marciniszyn77241052015-07-30 15:17:43 -0400555 * given sc.
556 *
557 * To understand this routine:
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700558 * o va and dma are arrays of struct credit_return. One for each physical
Mike Marciniszyn77241052015-07-30 15:17:43 -0400559 * send context, per NUMA.
560 * o Each send context always looks in its relative location in a struct
561 * credit_return for its credit return.
562 * o Each send context in a group must have its return address CSR programmed
563 * with the same value. Use the address of the first send context in the
564 * group.
565 */
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700566static void cr_group_addresses(struct send_context *sc, dma_addr_t *dma)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400567{
568 u32 gc = group_context(sc->hw_context, sc->group);
569 u32 index = sc->hw_context & 0x7;
570
571 sc->hw_free = &sc->dd->cr_base[sc->node].va[gc].cr[index];
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700572 *dma = (unsigned long)
573 &((struct credit_return *)sc->dd->cr_base[sc->node].dma)[gc];
Mike Marciniszyn77241052015-07-30 15:17:43 -0400574}
575
576/*
577 * Work queue function triggered in error interrupt routine for
578 * kernel contexts.
579 */
580static void sc_halted(struct work_struct *work)
581{
582 struct send_context *sc;
583
584 sc = container_of(work, struct send_context, halt_work);
585 sc_restart(sc);
586}
587
588/*
589 * Calculate PIO block threshold for this send context using the given MTU.
590 * Trigger a return when one MTU plus optional header of credits remain.
591 *
592 * Parameter mtu is in bytes.
593 * Parameter hdrqentsize is in DWORDs.
594 *
595 * Return value is what to write into the CSR: trigger return when
596 * unreturned credits pass this count.
597 */
598u32 sc_mtu_to_threshold(struct send_context *sc, u32 mtu, u32 hdrqentsize)
599{
600 u32 release_credits;
601 u32 threshold;
602
603 /* add in the header size, then divide by the PIO block size */
604 mtu += hdrqentsize << 2;
605 release_credits = DIV_ROUND_UP(mtu, PIO_BLOCK_SIZE);
606
607 /* check against this context's credits */
608 if (sc->credits <= release_credits)
609 threshold = 1;
610 else
611 threshold = sc->credits - release_credits;
612
613 return threshold;
614}
615
616/*
617 * Calculate credit threshold in terms of percent of the allocated credits.
618 * Trigger when unreturned credits equal or exceed the percentage of the whole.
619 *
620 * Return value is what to write into the CSR: trigger return when
621 * unreturned credits pass this count.
622 */
Jianxin Xiong44306f12016-04-12 11:30:28 -0700623u32 sc_percent_to_threshold(struct send_context *sc, u32 percent)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400624{
625 return (sc->credits * percent) / 100;
626}
627
628/*
629 * Set the credit return threshold.
630 */
631void sc_set_cr_threshold(struct send_context *sc, u32 new_threshold)
632{
633 unsigned long flags;
634 u32 old_threshold;
635 int force_return = 0;
636
637 spin_lock_irqsave(&sc->credit_ctrl_lock, flags);
638
639 old_threshold = (sc->credit_ctrl >>
640 SC(CREDIT_CTRL_THRESHOLD_SHIFT))
641 & SC(CREDIT_CTRL_THRESHOLD_MASK);
642
643 if (new_threshold != old_threshold) {
644 sc->credit_ctrl =
645 (sc->credit_ctrl
646 & ~SC(CREDIT_CTRL_THRESHOLD_SMASK))
647 | ((new_threshold
648 & SC(CREDIT_CTRL_THRESHOLD_MASK))
649 << SC(CREDIT_CTRL_THRESHOLD_SHIFT));
650 write_kctxt_csr(sc->dd, sc->hw_context,
Jubin John17fb4f22016-02-14 20:21:52 -0800651 SC(CREDIT_CTRL), sc->credit_ctrl);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400652
653 /* force a credit return on change to avoid a possible stall */
654 force_return = 1;
655 }
656
657 spin_unlock_irqrestore(&sc->credit_ctrl_lock, flags);
658
659 if (force_return)
660 sc_return_credits(sc);
661}
662
663/*
664 * set_pio_integrity
665 *
666 * Set the CHECK_ENABLE register for the send context 'sc'.
667 */
668void set_pio_integrity(struct send_context *sc)
669{
670 struct hfi1_devdata *dd = sc->dd;
671 u64 reg = 0;
672 u32 hw_context = sc->hw_context;
673 int type = sc->type;
674
675 /*
676 * No integrity checks if HFI1_CAP_NO_INTEGRITY is set, or if
677 * we're snooping.
678 */
679 if (likely(!HFI1_CAP_IS_KSET(NO_INTEGRITY)) &&
680 dd->hfi1_snoop.mode_flag != HFI1_PORT_SNOOP_MODE)
681 reg = hfi1_pkt_default_send_ctxt_mask(dd, type);
682
683 write_kctxt_csr(dd, hw_context, SC(CHECK_ENABLE), reg);
684}
685
Mike Marciniszyna0543742015-12-07 15:39:22 -0500686static u32 get_buffers_allocated(struct send_context *sc)
687{
688 int cpu;
689 u32 ret = 0;
690
691 for_each_possible_cpu(cpu)
692 ret += *per_cpu_ptr(sc->buffers_allocated, cpu);
693 return ret;
694}
695
696static void reset_buffers_allocated(struct send_context *sc)
697{
698 int cpu;
699
700 for_each_possible_cpu(cpu)
701 (*per_cpu_ptr(sc->buffers_allocated, cpu)) = 0;
702}
703
Mike Marciniszyn77241052015-07-30 15:17:43 -0400704/*
705 * Allocate a NUMA relative send context structure of the given type along
706 * with a HW context.
707 */
708struct send_context *sc_alloc(struct hfi1_devdata *dd, int type,
709 uint hdrqentsize, int numa)
710{
711 struct send_context_info *sci;
Mike Marciniszyna0543742015-12-07 15:39:22 -0500712 struct send_context *sc = NULL;
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700713 dma_addr_t dma;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400714 unsigned long flags;
715 u64 reg;
716 u32 thresh;
717 u32 sw_index;
718 u32 hw_context;
719 int ret;
720 u8 opval, opmask;
721
722 /* do not allocate while frozen */
723 if (dd->flags & HFI1_FROZEN)
724 return NULL;
725
Jubin Johnfcdd76d2016-02-14 20:21:16 -0800726 sc = kzalloc_node(sizeof(*sc), GFP_KERNEL, numa);
Alison Schofield806e6e12015-10-12 14:28:36 -0700727 if (!sc)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400728 return NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400729
Mike Marciniszyna0543742015-12-07 15:39:22 -0500730 sc->buffers_allocated = alloc_percpu(u32);
731 if (!sc->buffers_allocated) {
732 kfree(sc);
733 dd_dev_err(dd,
734 "Cannot allocate buffers_allocated per cpu counters\n"
735 );
736 return NULL;
737 }
738
Mike Marciniszyn77241052015-07-30 15:17:43 -0400739 spin_lock_irqsave(&dd->sc_lock, flags);
740 ret = sc_hw_alloc(dd, type, &sw_index, &hw_context);
741 if (ret) {
742 spin_unlock_irqrestore(&dd->sc_lock, flags);
Mike Marciniszyna0543742015-12-07 15:39:22 -0500743 free_percpu(sc->buffers_allocated);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400744 kfree(sc);
745 return NULL;
746 }
747
748 sci = &dd->send_contexts[sw_index];
749 sci->sc = sc;
750
751 sc->dd = dd;
752 sc->node = numa;
753 sc->type = type;
754 spin_lock_init(&sc->alloc_lock);
755 spin_lock_init(&sc->release_lock);
756 spin_lock_init(&sc->credit_ctrl_lock);
757 INIT_LIST_HEAD(&sc->piowait);
758 INIT_WORK(&sc->halt_work, sc_halted);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400759 init_waitqueue_head(&sc->halt_wait);
760
761 /* grouping is always single context for now */
762 sc->group = 0;
763
764 sc->sw_index = sw_index;
765 sc->hw_context = hw_context;
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700766 cr_group_addresses(sc, &dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400767 sc->credits = sci->credits;
Sebastian Sanchez8af8d292016-10-25 13:12:34 -0700768 sc->size = sc->credits * PIO_BLOCK_SIZE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400769
770/* PIO Send Memory Address details */
771#define PIO_ADDR_CONTEXT_MASK 0xfful
772#define PIO_ADDR_CONTEXT_SHIFT 16
773 sc->base_addr = dd->piobase + ((hw_context & PIO_ADDR_CONTEXT_MASK)
774 << PIO_ADDR_CONTEXT_SHIFT);
775
776 /* set base and credits */
777 reg = ((sci->credits & SC(CTRL_CTXT_DEPTH_MASK))
778 << SC(CTRL_CTXT_DEPTH_SHIFT))
779 | ((sci->base & SC(CTRL_CTXT_BASE_MASK))
780 << SC(CTRL_CTXT_BASE_SHIFT));
781 write_kctxt_csr(dd, hw_context, SC(CTRL), reg);
782
783 set_pio_integrity(sc);
784
785 /* unmask all errors */
786 write_kctxt_csr(dd, hw_context, SC(ERR_MASK), (u64)-1);
787
788 /* set the default partition key */
789 write_kctxt_csr(dd, hw_context, SC(CHECK_PARTITION_KEY),
Harish Chegondibf640092016-03-05 08:49:29 -0800790 (SC(CHECK_PARTITION_KEY_VALUE_MASK) &
791 DEFAULT_PKEY) <<
Jubin John17fb4f22016-02-14 20:21:52 -0800792 SC(CHECK_PARTITION_KEY_VALUE_SHIFT));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400793
794 /* per context type checks */
795 if (type == SC_USER) {
796 opval = USER_OPCODE_CHECK_VAL;
797 opmask = USER_OPCODE_CHECK_MASK;
798 } else {
799 opval = OPCODE_CHECK_VAL_DISABLED;
800 opmask = OPCODE_CHECK_MASK_DISABLED;
801 }
802
803 /* set the send context check opcode mask and value */
804 write_kctxt_csr(dd, hw_context, SC(CHECK_OPCODE),
Jubin John17fb4f22016-02-14 20:21:52 -0800805 ((u64)opmask << SC(CHECK_OPCODE_MASK_SHIFT)) |
806 ((u64)opval << SC(CHECK_OPCODE_VALUE_SHIFT)));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400807
808 /* set up credit return */
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700809 reg = dma & SC(CREDIT_RETURN_ADDR_ADDRESS_SMASK);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400810 write_kctxt_csr(dd, hw_context, SC(CREDIT_RETURN_ADDR), reg);
811
812 /*
813 * Calculate the initial credit return threshold.
814 *
815 * For Ack contexts, set a threshold for half the credits.
816 * For User contexts use the given percentage. This has been
817 * sanitized on driver start-up.
Jianxin Xiong44306f12016-04-12 11:30:28 -0700818 * For Kernel contexts, use the default MTU plus a header
819 * or half the credits, whichever is smaller. This should
820 * work for both the 3-deep buffering allocation and the
821 * pooling allocation.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400822 */
823 if (type == SC_ACK) {
824 thresh = sc_percent_to_threshold(sc, 50);
825 } else if (type == SC_USER) {
826 thresh = sc_percent_to_threshold(sc,
Jubin John17fb4f22016-02-14 20:21:52 -0800827 user_credit_return_threshold);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400828 } else { /* kernel */
Jianxin Xiong44306f12016-04-12 11:30:28 -0700829 thresh = min(sc_percent_to_threshold(sc, 50),
830 sc_mtu_to_threshold(sc, hfi1_max_mtu,
831 hdrqentsize));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400832 }
833 reg = thresh << SC(CREDIT_CTRL_THRESHOLD_SHIFT);
834 /* add in early return */
835 if (type == SC_USER && HFI1_CAP_IS_USET(EARLY_CREDIT_RETURN))
836 reg |= SC(CREDIT_CTRL_EARLY_RETURN_SMASK);
837 else if (HFI1_CAP_IS_KSET(EARLY_CREDIT_RETURN)) /* kernel, ack */
838 reg |= SC(CREDIT_CTRL_EARLY_RETURN_SMASK);
839
840 /* set up write-through credit_ctrl */
841 sc->credit_ctrl = reg;
842 write_kctxt_csr(dd, hw_context, SC(CREDIT_CTRL), reg);
843
844 /* User send contexts should not allow sending on VL15 */
845 if (type == SC_USER) {
846 reg = 1ULL << 15;
847 write_kctxt_csr(dd, hw_context, SC(CHECK_VL), reg);
848 }
849
850 spin_unlock_irqrestore(&dd->sc_lock, flags);
851
852 /*
853 * Allocate shadow ring to track outstanding PIO buffers _after_
854 * unlocking. We don't know the size until the lock is held and
855 * we can't allocate while the lock is held. No one is using
856 * the context yet, so allocate it now.
857 *
858 * User contexts do not get a shadow ring.
859 */
860 if (type != SC_USER) {
861 /*
862 * Size the shadow ring 1 larger than the number of credits
863 * so head == tail can mean empty.
864 */
865 sc->sr_size = sci->credits + 1;
866 sc->sr = kzalloc_node(sizeof(union pio_shadow_ring) *
867 sc->sr_size, GFP_KERNEL, numa);
868 if (!sc->sr) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400869 sc_free(sc);
870 return NULL;
871 }
872 }
873
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500874 hfi1_cdbg(PIO,
875 "Send context %u(%u) %s group %u credits %u credit_ctrl 0x%llx threshold %u\n",
876 sw_index,
877 hw_context,
878 sc_type_name(type),
879 sc->group,
880 sc->credits,
881 sc->credit_ctrl,
882 thresh);
883
Mike Marciniszyn77241052015-07-30 15:17:43 -0400884 return sc;
885}
886
887/* free a per-NUMA send context structure */
888void sc_free(struct send_context *sc)
889{
890 struct hfi1_devdata *dd;
891 unsigned long flags;
892 u32 sw_index;
893 u32 hw_context;
894
895 if (!sc)
896 return;
897
898 sc->flags |= SCF_IN_FREE; /* ensure no restarts */
899 dd = sc->dd;
900 if (!list_empty(&sc->piowait))
901 dd_dev_err(dd, "piowait list not empty!\n");
902 sw_index = sc->sw_index;
903 hw_context = sc->hw_context;
904 sc_disable(sc); /* make sure the HW is disabled */
905 flush_work(&sc->halt_work);
906
907 spin_lock_irqsave(&dd->sc_lock, flags);
908 dd->send_contexts[sw_index].sc = NULL;
909
910 /* clear/disable all registers set in sc_alloc */
911 write_kctxt_csr(dd, hw_context, SC(CTRL), 0);
912 write_kctxt_csr(dd, hw_context, SC(CHECK_ENABLE), 0);
913 write_kctxt_csr(dd, hw_context, SC(ERR_MASK), 0);
914 write_kctxt_csr(dd, hw_context, SC(CHECK_PARTITION_KEY), 0);
915 write_kctxt_csr(dd, hw_context, SC(CHECK_OPCODE), 0);
916 write_kctxt_csr(dd, hw_context, SC(CREDIT_RETURN_ADDR), 0);
917 write_kctxt_csr(dd, hw_context, SC(CREDIT_CTRL), 0);
918
919 /* release the index and context for re-use */
920 sc_hw_free(dd, sw_index, hw_context);
921 spin_unlock_irqrestore(&dd->sc_lock, flags);
922
923 kfree(sc->sr);
Mike Marciniszyna0543742015-12-07 15:39:22 -0500924 free_percpu(sc->buffers_allocated);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400925 kfree(sc);
926}
927
928/* disable the context */
929void sc_disable(struct send_context *sc)
930{
931 u64 reg;
932 unsigned long flags;
933 struct pio_buf *pbuf;
934
935 if (!sc)
936 return;
937
938 /* do all steps, even if already disabled */
939 spin_lock_irqsave(&sc->alloc_lock, flags);
940 reg = read_kctxt_csr(sc->dd, sc->hw_context, SC(CTRL));
941 reg &= ~SC(CTRL_CTXT_ENABLE_SMASK);
942 sc->flags &= ~SCF_ENABLED;
943 sc_wait_for_packet_egress(sc, 1);
944 write_kctxt_csr(sc->dd, sc->hw_context, SC(CTRL), reg);
945 spin_unlock_irqrestore(&sc->alloc_lock, flags);
946
947 /*
948 * Flush any waiters. Once the context is disabled,
949 * credit return interrupts are stopped (although there
950 * could be one in-process when the context is disabled).
951 * Wait one microsecond for any lingering interrupts, then
952 * proceed with the flush.
953 */
954 udelay(1);
955 spin_lock_irqsave(&sc->release_lock, flags);
956 if (sc->sr) { /* this context has a shadow ring */
957 while (sc->sr_tail != sc->sr_head) {
958 pbuf = &sc->sr[sc->sr_tail].pbuf;
959 if (pbuf->cb)
960 (*pbuf->cb)(pbuf->arg, PRC_SC_DISABLE);
961 sc->sr_tail++;
962 if (sc->sr_tail >= sc->sr_size)
963 sc->sr_tail = 0;
964 }
965 }
966 spin_unlock_irqrestore(&sc->release_lock, flags);
967}
968
969/* return SendEgressCtxtStatus.PacketOccupancy */
970#define packet_occupancy(r) \
971 (((r) & SEND_EGRESS_CTXT_STATUS_CTXT_EGRESS_PACKET_OCCUPANCY_SMASK)\
972 >> SEND_EGRESS_CTXT_STATUS_CTXT_EGRESS_PACKET_OCCUPANCY_SHIFT)
973
974/* is egress halted on the context? */
975#define egress_halted(r) \
976 ((r) & SEND_EGRESS_CTXT_STATUS_CTXT_EGRESS_HALT_STATUS_SMASK)
977
978/* wait for packet egress, optionally pause for credit return */
979static void sc_wait_for_packet_egress(struct send_context *sc, int pause)
980{
981 struct hfi1_devdata *dd = sc->dd;
Vennila Megavannan25d97dd2015-10-26 10:28:30 -0400982 u64 reg = 0;
983 u64 reg_prev;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400984 u32 loop = 0;
985
986 while (1) {
Vennila Megavannan25d97dd2015-10-26 10:28:30 -0400987 reg_prev = reg;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400988 reg = read_csr(dd, sc->hw_context * 8 +
989 SEND_EGRESS_CTXT_STATUS);
990 /* done if egress is stopped */
991 if (egress_halted(reg))
992 break;
993 reg = packet_occupancy(reg);
994 if (reg == 0)
995 break;
Vennila Megavannan25d97dd2015-10-26 10:28:30 -0400996 /* counter is reset if occupancy count changes */
997 if (reg != reg_prev)
998 loop = 0;
Jubin Johnc3c64a92016-06-09 07:51:27 -0700999 if (loop > 50000) {
Vennila Megavannan25d97dd2015-10-26 10:28:30 -04001000 /* timed out - bounce the link */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001001 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001002 "%s: context %u(%u) timeout waiting for packets to egress, remaining count %u, bouncing link\n",
1003 __func__, sc->sw_index,
1004 sc->hw_context, (u32)reg);
Vennila Megavannan25d97dd2015-10-26 10:28:30 -04001005 queue_work(dd->pport->hfi1_wq,
Jubin John17fb4f22016-02-14 20:21:52 -08001006 &dd->pport->link_bounce_work);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001007 break;
1008 }
1009 loop++;
1010 udelay(1);
1011 }
1012
1013 if (pause)
1014 /* Add additional delay to ensure chip returns all credits */
1015 pause_for_credit_return(dd);
1016}
1017
1018void sc_wait(struct hfi1_devdata *dd)
1019{
1020 int i;
1021
1022 for (i = 0; i < dd->num_send_contexts; i++) {
1023 struct send_context *sc = dd->send_contexts[i].sc;
1024
1025 if (!sc)
1026 continue;
1027 sc_wait_for_packet_egress(sc, 0);
1028 }
1029}
1030
1031/*
1032 * Restart a context after it has been halted due to error.
1033 *
1034 * If the first step fails - wait for the halt to be asserted, return early.
1035 * Otherwise complain about timeouts but keep going.
1036 *
1037 * It is expected that allocations (enabled flag bit) have been shut off
1038 * already (only applies to kernel contexts).
1039 */
1040int sc_restart(struct send_context *sc)
1041{
1042 struct hfi1_devdata *dd = sc->dd;
1043 u64 reg;
1044 u32 loop;
1045 int count;
1046
1047 /* bounce off if not halted, or being free'd */
1048 if (!(sc->flags & SCF_HALTED) || (sc->flags & SCF_IN_FREE))
1049 return -EINVAL;
1050
1051 dd_dev_info(dd, "restarting send context %u(%u)\n", sc->sw_index,
Jubin John17fb4f22016-02-14 20:21:52 -08001052 sc->hw_context);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001053
1054 /*
1055 * Step 1: Wait for the context to actually halt.
1056 *
1057 * The error interrupt is asynchronous to actually setting halt
1058 * on the context.
1059 */
1060 loop = 0;
1061 while (1) {
1062 reg = read_kctxt_csr(dd, sc->hw_context, SC(STATUS));
1063 if (reg & SC(STATUS_CTXT_HALTED_SMASK))
1064 break;
1065 if (loop > 100) {
1066 dd_dev_err(dd, "%s: context %u(%u) not halting, skipping\n",
Jubin John17fb4f22016-02-14 20:21:52 -08001067 __func__, sc->sw_index, sc->hw_context);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001068 return -ETIME;
1069 }
1070 loop++;
1071 udelay(1);
1072 }
1073
1074 /*
1075 * Step 2: Ensure no users are still trying to write to PIO.
1076 *
1077 * For kernel contexts, we have already turned off buffer allocation.
1078 * Now wait for the buffer count to go to zero.
1079 *
1080 * For user contexts, the user handling code has cut off write access
1081 * to the context's PIO pages before calling this routine and will
1082 * restore write access after this routine returns.
1083 */
1084 if (sc->type != SC_USER) {
1085 /* kernel context */
1086 loop = 0;
1087 while (1) {
Mike Marciniszyna0543742015-12-07 15:39:22 -05001088 count = get_buffers_allocated(sc);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001089 if (count == 0)
1090 break;
1091 if (loop > 100) {
1092 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001093 "%s: context %u(%u) timeout waiting for PIO buffers to zero, remaining %d\n",
1094 __func__, sc->sw_index,
1095 sc->hw_context, count);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001096 }
1097 loop++;
1098 udelay(1);
1099 }
1100 }
1101
1102 /*
1103 * Step 3: Wait for all packets to egress.
1104 * This is done while disabling the send context
1105 *
1106 * Step 4: Disable the context
1107 *
1108 * This is a superset of the halt. After the disable, the
1109 * errors can be cleared.
1110 */
1111 sc_disable(sc);
1112
1113 /*
1114 * Step 5: Enable the context
1115 *
1116 * This enable will clear the halted flag and per-send context
1117 * error flags.
1118 */
1119 return sc_enable(sc);
1120}
1121
1122/*
1123 * PIO freeze processing. To be called after the TXE block is fully frozen.
1124 * Go through all frozen send contexts and disable them. The contexts are
1125 * already stopped by the freeze.
1126 */
1127void pio_freeze(struct hfi1_devdata *dd)
1128{
1129 struct send_context *sc;
1130 int i;
1131
1132 for (i = 0; i < dd->num_send_contexts; i++) {
1133 sc = dd->send_contexts[i].sc;
1134 /*
1135 * Don't disable unallocated, unfrozen, or user send contexts.
1136 * User send contexts will be disabled when the process
1137 * calls into the driver to reset its context.
1138 */
1139 if (!sc || !(sc->flags & SCF_FROZEN) || sc->type == SC_USER)
1140 continue;
1141
1142 /* only need to disable, the context is already stopped */
1143 sc_disable(sc);
1144 }
1145}
1146
1147/*
1148 * Unfreeze PIO for kernel send contexts. The precondition for calling this
1149 * is that all PIO send contexts have been disabled and the SPC freeze has
1150 * been cleared. Now perform the last step and re-enable each kernel context.
1151 * User (PSM) processing will occur when PSM calls into the kernel to
1152 * acknowledge the freeze.
1153 */
1154void pio_kernel_unfreeze(struct hfi1_devdata *dd)
1155{
1156 struct send_context *sc;
1157 int i;
1158
1159 for (i = 0; i < dd->num_send_contexts; i++) {
1160 sc = dd->send_contexts[i].sc;
1161 if (!sc || !(sc->flags & SCF_FROZEN) || sc->type == SC_USER)
1162 continue;
1163
1164 sc_enable(sc); /* will clear the sc frozen flag */
1165 }
1166}
1167
1168/*
1169 * Wait for the SendPioInitCtxt.PioInitInProgress bit to clear.
1170 * Returns:
1171 * -ETIMEDOUT - if we wait too long
1172 * -EIO - if there was an error
1173 */
1174static int pio_init_wait_progress(struct hfi1_devdata *dd)
1175{
1176 u64 reg;
1177 int max, count = 0;
1178
1179 /* max is the longest possible HW init time / delay */
1180 max = (dd->icode == ICODE_FPGA_EMULATION) ? 120 : 5;
1181 while (1) {
1182 reg = read_csr(dd, SEND_PIO_INIT_CTXT);
1183 if (!(reg & SEND_PIO_INIT_CTXT_PIO_INIT_IN_PROGRESS_SMASK))
1184 break;
1185 if (count >= max)
1186 return -ETIMEDOUT;
1187 udelay(5);
1188 count++;
1189 }
1190
1191 return reg & SEND_PIO_INIT_CTXT_PIO_INIT_ERR_SMASK ? -EIO : 0;
1192}
1193
1194/*
1195 * Reset all of the send contexts to their power-on state. Used
1196 * only during manual init - no lock against sc_enable needed.
1197 */
1198void pio_reset_all(struct hfi1_devdata *dd)
1199{
1200 int ret;
1201
1202 /* make sure the init engine is not busy */
1203 ret = pio_init_wait_progress(dd);
1204 /* ignore any timeout */
1205 if (ret == -EIO) {
1206 /* clear the error */
1207 write_csr(dd, SEND_PIO_ERR_CLEAR,
Jubin John17fb4f22016-02-14 20:21:52 -08001208 SEND_PIO_ERR_CLEAR_PIO_INIT_SM_IN_ERR_SMASK);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001209 }
1210
1211 /* reset init all */
1212 write_csr(dd, SEND_PIO_INIT_CTXT,
Jubin John17fb4f22016-02-14 20:21:52 -08001213 SEND_PIO_INIT_CTXT_PIO_ALL_CTXT_INIT_SMASK);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001214 udelay(2);
1215 ret = pio_init_wait_progress(dd);
1216 if (ret < 0) {
1217 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001218 "PIO send context init %s while initializing all PIO blocks\n",
1219 ret == -ETIMEDOUT ? "is stuck" : "had an error");
Mike Marciniszyn77241052015-07-30 15:17:43 -04001220 }
1221}
1222
1223/* enable the context */
1224int sc_enable(struct send_context *sc)
1225{
1226 u64 sc_ctrl, reg, pio;
1227 struct hfi1_devdata *dd;
1228 unsigned long flags;
1229 int ret = 0;
1230
1231 if (!sc)
1232 return -EINVAL;
1233 dd = sc->dd;
1234
1235 /*
1236 * Obtain the allocator lock to guard against any allocation
1237 * attempts (which should not happen prior to context being
1238 * enabled). On the release/disable side we don't need to
1239 * worry about locking since the releaser will not do anything
1240 * if the context accounting values have not changed.
1241 */
1242 spin_lock_irqsave(&sc->alloc_lock, flags);
1243 sc_ctrl = read_kctxt_csr(dd, sc->hw_context, SC(CTRL));
1244 if ((sc_ctrl & SC(CTRL_CTXT_ENABLE_SMASK)))
1245 goto unlock; /* already enabled */
1246
1247 /* IMPORTANT: only clear free and fill if transitioning 0 -> 1 */
1248
1249 *sc->hw_free = 0;
1250 sc->free = 0;
1251 sc->alloc_free = 0;
1252 sc->fill = 0;
Sebastian Sanchez2474d772016-10-25 13:12:28 -07001253 sc->fill_wrap = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001254 sc->sr_head = 0;
1255 sc->sr_tail = 0;
1256 sc->flags = 0;
Mike Marciniszyna0543742015-12-07 15:39:22 -05001257 /* the alloc lock insures no fast path allocation */
1258 reset_buffers_allocated(sc);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001259
1260 /*
1261 * Clear all per-context errors. Some of these will be set when
1262 * we are re-enabling after a context halt. Now that the context
1263 * is disabled, the halt will not clear until after the PIO init
1264 * engine runs below.
1265 */
1266 reg = read_kctxt_csr(dd, sc->hw_context, SC(ERR_STATUS));
1267 if (reg)
Jubin John17fb4f22016-02-14 20:21:52 -08001268 write_kctxt_csr(dd, sc->hw_context, SC(ERR_CLEAR), reg);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001269
1270 /*
1271 * The HW PIO initialization engine can handle only one init
1272 * request at a time. Serialize access to each device's engine.
1273 */
1274 spin_lock(&dd->sc_init_lock);
1275 /*
1276 * Since access to this code block is serialized and
1277 * each access waits for the initialization to complete
1278 * before releasing the lock, the PIO initialization engine
1279 * should not be in use, so we don't have to wait for the
1280 * InProgress bit to go down.
1281 */
1282 pio = ((sc->hw_context & SEND_PIO_INIT_CTXT_PIO_CTXT_NUM_MASK) <<
1283 SEND_PIO_INIT_CTXT_PIO_CTXT_NUM_SHIFT) |
1284 SEND_PIO_INIT_CTXT_PIO_SINGLE_CTXT_INIT_SMASK;
1285 write_csr(dd, SEND_PIO_INIT_CTXT, pio);
1286 /*
1287 * Wait until the engine is done. Give the chip the required time
1288 * so, hopefully, we read the register just once.
1289 */
1290 udelay(2);
1291 ret = pio_init_wait_progress(dd);
1292 spin_unlock(&dd->sc_init_lock);
1293 if (ret) {
1294 dd_dev_err(dd,
1295 "sctxt%u(%u): Context not enabled due to init failure %d\n",
1296 sc->sw_index, sc->hw_context, ret);
1297 goto unlock;
1298 }
1299
1300 /*
1301 * All is well. Enable the context.
1302 */
1303 sc_ctrl |= SC(CTRL_CTXT_ENABLE_SMASK);
1304 write_kctxt_csr(dd, sc->hw_context, SC(CTRL), sc_ctrl);
1305 /*
1306 * Read SendCtxtCtrl to force the write out and prevent a timing
1307 * hazard where a PIO write may reach the context before the enable.
1308 */
1309 read_kctxt_csr(dd, sc->hw_context, SC(CTRL));
1310 sc->flags |= SCF_ENABLED;
1311
1312unlock:
1313 spin_unlock_irqrestore(&sc->alloc_lock, flags);
1314
1315 return ret;
1316}
1317
1318/* force a credit return on the context */
1319void sc_return_credits(struct send_context *sc)
1320{
1321 if (!sc)
1322 return;
1323
1324 /* a 0->1 transition schedules a credit return */
1325 write_kctxt_csr(sc->dd, sc->hw_context, SC(CREDIT_FORCE),
Jubin John17fb4f22016-02-14 20:21:52 -08001326 SC(CREDIT_FORCE_FORCE_RETURN_SMASK));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001327 /*
1328 * Ensure that the write is flushed and the credit return is
1329 * scheduled. We care more about the 0 -> 1 transition.
1330 */
1331 read_kctxt_csr(sc->dd, sc->hw_context, SC(CREDIT_FORCE));
1332 /* set back to 0 for next time */
1333 write_kctxt_csr(sc->dd, sc->hw_context, SC(CREDIT_FORCE), 0);
1334}
1335
1336/* allow all in-flight packets to drain on the context */
1337void sc_flush(struct send_context *sc)
1338{
1339 if (!sc)
1340 return;
1341
1342 sc_wait_for_packet_egress(sc, 1);
1343}
1344
1345/* drop all packets on the context, no waiting until they are sent */
1346void sc_drop(struct send_context *sc)
1347{
1348 if (!sc)
1349 return;
1350
1351 dd_dev_info(sc->dd, "%s: context %u(%u) - not implemented\n",
Jubin John17fb4f22016-02-14 20:21:52 -08001352 __func__, sc->sw_index, sc->hw_context);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001353}
1354
1355/*
1356 * Start the software reaction to a context halt or SPC freeze:
1357 * - mark the context as halted or frozen
1358 * - stop buffer allocations
1359 *
1360 * Called from the error interrupt. Other work is deferred until
1361 * out of the interrupt.
1362 */
1363void sc_stop(struct send_context *sc, int flag)
1364{
1365 unsigned long flags;
1366
1367 /* mark the context */
1368 sc->flags |= flag;
1369
1370 /* stop buffer allocations */
1371 spin_lock_irqsave(&sc->alloc_lock, flags);
1372 sc->flags &= ~SCF_ENABLED;
1373 spin_unlock_irqrestore(&sc->alloc_lock, flags);
1374 wake_up(&sc->halt_wait);
1375}
1376
Jubin John8638b772016-02-14 20:19:24 -08001377#define BLOCK_DWORDS (PIO_BLOCK_SIZE / sizeof(u32))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001378#define dwords_to_blocks(x) DIV_ROUND_UP(x, BLOCK_DWORDS)
1379
1380/*
1381 * The send context buffer "allocator".
1382 *
1383 * @sc: the PIO send context we are allocating from
1384 * @len: length of whole packet - including PBC - in dwords
1385 * @cb: optional callback to call when the buffer is finished sending
1386 * @arg: argument for cb
1387 *
1388 * Return a pointer to a PIO buffer if successful, NULL if not enough room.
1389 */
1390struct pio_buf *sc_buffer_alloc(struct send_context *sc, u32 dw_len,
1391 pio_release_cb cb, void *arg)
1392{
1393 struct pio_buf *pbuf = NULL;
1394 unsigned long flags;
1395 unsigned long avail;
1396 unsigned long blocks = dwords_to_blocks(dw_len);
Sebastian Sanchez2474d772016-10-25 13:12:28 -07001397 u32 fill_wrap;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001398 int trycount = 0;
1399 u32 head, next;
1400
1401 spin_lock_irqsave(&sc->alloc_lock, flags);
1402 if (!(sc->flags & SCF_ENABLED)) {
1403 spin_unlock_irqrestore(&sc->alloc_lock, flags);
1404 goto done;
1405 }
1406
1407retry:
1408 avail = (unsigned long)sc->credits - (sc->fill - sc->alloc_free);
1409 if (blocks > avail) {
1410 /* not enough room */
1411 if (unlikely(trycount)) { /* already tried to get more room */
1412 spin_unlock_irqrestore(&sc->alloc_lock, flags);
1413 goto done;
1414 }
1415 /* copy from receiver cache line and recalculate */
1416 sc->alloc_free = ACCESS_ONCE(sc->free);
1417 avail =
1418 (unsigned long)sc->credits -
1419 (sc->fill - sc->alloc_free);
1420 if (blocks > avail) {
1421 /* still no room, actively update */
1422 spin_unlock_irqrestore(&sc->alloc_lock, flags);
1423 sc_release_update(sc);
1424 spin_lock_irqsave(&sc->alloc_lock, flags);
1425 sc->alloc_free = ACCESS_ONCE(sc->free);
1426 trycount++;
1427 goto retry;
1428 }
1429 }
1430
1431 /* there is enough room */
1432
Mike Marciniszyna0543742015-12-07 15:39:22 -05001433 preempt_disable();
1434 this_cpu_inc(*sc->buffers_allocated);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001435
1436 /* read this once */
1437 head = sc->sr_head;
1438
1439 /* "allocate" the buffer */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001440 sc->fill += blocks;
Sebastian Sanchez2474d772016-10-25 13:12:28 -07001441 fill_wrap = sc->fill_wrap;
1442 sc->fill_wrap += blocks;
1443 if (sc->fill_wrap >= sc->credits)
1444 sc->fill_wrap = sc->fill_wrap - sc->credits;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001445
1446 /*
1447 * Fill the parts that the releaser looks at before moving the head.
1448 * The only necessary piece is the sent_at field. The credits
1449 * we have just allocated cannot have been returned yet, so the
1450 * cb and arg will not be looked at for a "while". Put them
1451 * on this side of the memory barrier anyway.
1452 */
1453 pbuf = &sc->sr[head].pbuf;
1454 pbuf->sent_at = sc->fill;
1455 pbuf->cb = cb;
1456 pbuf->arg = arg;
1457 pbuf->sc = sc; /* could be filled in at sc->sr init time */
1458 /* make sure this is in memory before updating the head */
1459
1460 /* calculate next head index, do not store */
1461 next = head + 1;
1462 if (next >= sc->sr_size)
1463 next = 0;
Jubin John4d114fd2016-02-14 20:21:43 -08001464 /*
1465 * update the head - must be last! - the releaser can look at fields
1466 * in pbuf once we move the head
1467 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001468 smp_wmb();
1469 sc->sr_head = next;
1470 spin_unlock_irqrestore(&sc->alloc_lock, flags);
1471
1472 /* finish filling in the buffer outside the lock */
Sebastian Sanchez2474d772016-10-25 13:12:28 -07001473 pbuf->start = sc->base_addr + fill_wrap * PIO_BLOCK_SIZE;
Sebastian Sanchez8af8d292016-10-25 13:12:34 -07001474 pbuf->end = sc->base_addr + sc->size;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001475 pbuf->qw_written = 0;
1476 pbuf->carry_bytes = 0;
1477 pbuf->carry.val64 = 0;
1478done:
1479 return pbuf;
1480}
1481
1482/*
1483 * There are at least two entities that can turn on credit return
1484 * interrupts and they can overlap. Avoid problems by implementing
1485 * a count scheme that is enforced by a lock. The lock is needed because
1486 * the count and CSR write must be paired.
1487 */
1488
1489/*
1490 * Start credit return interrupts. This is managed by a count. If already
1491 * on, just increment the count.
1492 */
1493void sc_add_credit_return_intr(struct send_context *sc)
1494{
1495 unsigned long flags;
1496
1497 /* lock must surround both the count change and the CSR update */
1498 spin_lock_irqsave(&sc->credit_ctrl_lock, flags);
1499 if (sc->credit_intr_count == 0) {
1500 sc->credit_ctrl |= SC(CREDIT_CTRL_CREDIT_INTR_SMASK);
1501 write_kctxt_csr(sc->dd, sc->hw_context,
Jubin John17fb4f22016-02-14 20:21:52 -08001502 SC(CREDIT_CTRL), sc->credit_ctrl);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001503 }
1504 sc->credit_intr_count++;
1505 spin_unlock_irqrestore(&sc->credit_ctrl_lock, flags);
1506}
1507
1508/*
1509 * Stop credit return interrupts. This is managed by a count. Decrement the
1510 * count, if the last user, then turn the credit interrupts off.
1511 */
1512void sc_del_credit_return_intr(struct send_context *sc)
1513{
1514 unsigned long flags;
1515
1516 WARN_ON(sc->credit_intr_count == 0);
1517
1518 /* lock must surround both the count change and the CSR update */
1519 spin_lock_irqsave(&sc->credit_ctrl_lock, flags);
1520 sc->credit_intr_count--;
1521 if (sc->credit_intr_count == 0) {
1522 sc->credit_ctrl &= ~SC(CREDIT_CTRL_CREDIT_INTR_SMASK);
1523 write_kctxt_csr(sc->dd, sc->hw_context,
Jubin John17fb4f22016-02-14 20:21:52 -08001524 SC(CREDIT_CTRL), sc->credit_ctrl);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001525 }
1526 spin_unlock_irqrestore(&sc->credit_ctrl_lock, flags);
1527}
1528
1529/*
1530 * The caller must be careful when calling this. All needint calls
1531 * must be paired with !needint.
1532 */
1533void hfi1_sc_wantpiobuf_intr(struct send_context *sc, u32 needint)
1534{
1535 if (needint)
1536 sc_add_credit_return_intr(sc);
1537 else
1538 sc_del_credit_return_intr(sc);
1539 trace_hfi1_wantpiointr(sc, needint, sc->credit_ctrl);
1540 if (needint) {
1541 mmiowb();
1542 sc_return_credits(sc);
1543 }
1544}
1545
1546/**
1547 * sc_piobufavail - callback when a PIO buffer is available
1548 * @sc: the send context
1549 *
1550 * This is called from the interrupt handler when a PIO buffer is
1551 * available after hfi1_verbs_send() returned an error that no buffers were
1552 * available. Disable the interrupt if there are no more QPs waiting.
1553 */
1554static void sc_piobufavail(struct send_context *sc)
1555{
1556 struct hfi1_devdata *dd = sc->dd;
1557 struct hfi1_ibdev *dev = &dd->verbs_dev;
1558 struct list_head *list;
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001559 struct rvt_qp *qps[PIO_WAIT_BATCH_SIZE];
1560 struct rvt_qp *qp;
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -08001561 struct hfi1_qp_priv *priv;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001562 unsigned long flags;
1563 unsigned i, n = 0;
1564
Jianxin Xiong44306f12016-04-12 11:30:28 -07001565 if (dd->send_contexts[sc->sw_index].type != SC_KERNEL &&
1566 dd->send_contexts[sc->sw_index].type != SC_VL15)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001567 return;
1568 list = &sc->piowait;
1569 /*
1570 * Note: checking that the piowait list is empty and clearing
1571 * the buffer available interrupt needs to be atomic or we
1572 * could end up with QPs on the wait list with the interrupt
1573 * disabled.
1574 */
1575 write_seqlock_irqsave(&dev->iowait_lock, flags);
1576 while (!list_empty(list)) {
1577 struct iowait *wait;
1578
1579 if (n == ARRAY_SIZE(qps))
Mike Marciniszyn60df2952016-03-07 11:35:30 -08001580 break;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001581 wait = list_first_entry(list, struct iowait, list);
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -08001582 qp = iowait_to_qp(wait);
1583 priv = qp->priv;
1584 list_del_init(&priv->s_iowait.list);
Mike Marciniszyn4e045572016-10-10 06:14:28 -07001585 priv->s_iowait.lock = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001586 /* refcount held until actual wake up */
1587 qps[n++] = qp;
1588 }
1589 /*
Mike Marciniszyn60df2952016-03-07 11:35:30 -08001590 * If there had been waiters and there are more
1591 * insure that we redo the force to avoid a potential hang.
Mike Marciniszyn77241052015-07-30 15:17:43 -04001592 */
Mike Marciniszyn60df2952016-03-07 11:35:30 -08001593 if (n) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001594 hfi1_sc_wantpiobuf_intr(sc, 0);
Mike Marciniszyn60df2952016-03-07 11:35:30 -08001595 if (!list_empty(list))
1596 hfi1_sc_wantpiobuf_intr(sc, 1);
1597 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001598 write_sequnlock_irqrestore(&dev->iowait_lock, flags);
1599
1600 for (i = 0; i < n; i++)
Mike Marciniszyn14553ca2016-02-14 12:45:36 -08001601 hfi1_qp_wakeup(qps[i],
1602 RVT_S_WAIT_PIO | RVT_S_WAIT_PIO_DRAIN);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001603}
1604
1605/* translate a send credit update to a bit code of reasons */
1606static inline int fill_code(u64 hw_free)
1607{
1608 int code = 0;
1609
1610 if (hw_free & CR_STATUS_SMASK)
1611 code |= PRC_STATUS_ERR;
1612 if (hw_free & CR_CREDIT_RETURN_DUE_TO_PBC_SMASK)
1613 code |= PRC_PBC;
1614 if (hw_free & CR_CREDIT_RETURN_DUE_TO_THRESHOLD_SMASK)
1615 code |= PRC_THRESHOLD;
1616 if (hw_free & CR_CREDIT_RETURN_DUE_TO_ERR_SMASK)
1617 code |= PRC_FILL_ERR;
1618 if (hw_free & CR_CREDIT_RETURN_DUE_TO_FORCE_SMASK)
1619 code |= PRC_SC_DISABLE;
1620 return code;
1621}
1622
1623/* use the jiffies compare to get the wrap right */
1624#define sent_before(a, b) time_before(a, b) /* a < b */
1625
1626/*
1627 * The send context buffer "releaser".
1628 */
1629void sc_release_update(struct send_context *sc)
1630{
1631 struct pio_buf *pbuf;
1632 u64 hw_free;
1633 u32 head, tail;
1634 unsigned long old_free;
Mike Marciniszyne607a222015-12-03 14:34:18 -05001635 unsigned long free;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001636 unsigned long extra;
1637 unsigned long flags;
1638 int code;
1639
1640 if (!sc)
1641 return;
1642
1643 spin_lock_irqsave(&sc->release_lock, flags);
1644 /* update free */
1645 hw_free = le64_to_cpu(*sc->hw_free); /* volatile read */
1646 old_free = sc->free;
1647 extra = (((hw_free & CR_COUNTER_SMASK) >> CR_COUNTER_SHIFT)
1648 - (old_free & CR_COUNTER_MASK))
1649 & CR_COUNTER_MASK;
Mike Marciniszyne607a222015-12-03 14:34:18 -05001650 free = old_free + extra;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001651 trace_hfi1_piofree(sc, extra);
1652
1653 /* call sent buffer callbacks */
1654 code = -1; /* code not yet set */
1655 head = ACCESS_ONCE(sc->sr_head); /* snapshot the head */
1656 tail = sc->sr_tail;
1657 while (head != tail) {
1658 pbuf = &sc->sr[tail].pbuf;
1659
Mike Marciniszyne607a222015-12-03 14:34:18 -05001660 if (sent_before(free, pbuf->sent_at)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001661 /* not sent yet */
1662 break;
1663 }
1664 if (pbuf->cb) {
1665 if (code < 0) /* fill in code on first user */
1666 code = fill_code(hw_free);
1667 (*pbuf->cb)(pbuf->arg, code);
1668 }
1669
1670 tail++;
1671 if (tail >= sc->sr_size)
1672 tail = 0;
1673 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001674 sc->sr_tail = tail;
Mike Marciniszyne607a222015-12-03 14:34:18 -05001675 /* make sure tail is updated before free */
1676 smp_wmb();
1677 sc->free = free;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001678 spin_unlock_irqrestore(&sc->release_lock, flags);
1679 sc_piobufavail(sc);
1680}
1681
1682/*
1683 * Send context group releaser. Argument is the send context that caused
1684 * the interrupt. Called from the send context interrupt handler.
1685 *
1686 * Call release on all contexts in the group.
1687 *
1688 * This routine takes the sc_lock without an irqsave because it is only
1689 * called from an interrupt handler. Adjust if that changes.
1690 */
1691void sc_group_release_update(struct hfi1_devdata *dd, u32 hw_context)
1692{
1693 struct send_context *sc;
1694 u32 sw_index;
1695 u32 gc, gc_end;
1696
1697 spin_lock(&dd->sc_lock);
1698 sw_index = dd->hw_to_sw[hw_context];
1699 if (unlikely(sw_index >= dd->num_send_contexts)) {
1700 dd_dev_err(dd, "%s: invalid hw (%u) to sw (%u) mapping\n",
Jubin John17fb4f22016-02-14 20:21:52 -08001701 __func__, hw_context, sw_index);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001702 goto done;
1703 }
1704 sc = dd->send_contexts[sw_index].sc;
1705 if (unlikely(!sc))
1706 goto done;
1707
1708 gc = group_context(hw_context, sc->group);
1709 gc_end = gc + group_size(sc->group);
1710 for (; gc < gc_end; gc++) {
1711 sw_index = dd->hw_to_sw[gc];
1712 if (unlikely(sw_index >= dd->num_send_contexts)) {
1713 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001714 "%s: invalid hw (%u) to sw (%u) mapping\n",
1715 __func__, hw_context, sw_index);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001716 continue;
1717 }
1718 sc_release_update(dd->send_contexts[sw_index].sc);
1719 }
1720done:
1721 spin_unlock(&dd->sc_lock);
1722}
1723
Jubin John35f6bef2016-02-14 12:46:10 -08001724/*
1725 * pio_select_send_context_vl() - select send context
1726 * @dd: devdata
1727 * @selector: a spreading factor
1728 * @vl: this vl
1729 *
1730 * This function returns a send context based on the selector and a vl.
1731 * The mapping fields are protected by RCU
1732 */
1733struct send_context *pio_select_send_context_vl(struct hfi1_devdata *dd,
1734 u32 selector, u8 vl)
1735{
1736 struct pio_vl_map *m;
1737 struct pio_map_elem *e;
1738 struct send_context *rval;
1739
1740 /*
1741 * NOTE This should only happen if SC->VL changed after the initial
1742 * checks on the QP/AH
1743 * Default will return VL0's send context below
1744 */
1745 if (unlikely(vl >= num_vls)) {
1746 rval = NULL;
1747 goto done;
1748 }
1749
1750 rcu_read_lock();
1751 m = rcu_dereference(dd->pio_map);
1752 if (unlikely(!m)) {
1753 rcu_read_unlock();
1754 return dd->vld[0].sc;
1755 }
1756 e = m->map[vl & m->mask];
1757 rval = e->ksc[selector & e->mask];
1758 rcu_read_unlock();
1759
1760done:
1761 rval = !rval ? dd->vld[0].sc : rval;
1762 return rval;
1763}
1764
1765/*
1766 * pio_select_send_context_sc() - select send context
1767 * @dd: devdata
1768 * @selector: a spreading factor
1769 * @sc5: the 5 bit sc
1770 *
1771 * This function returns an send context based on the selector and an sc
1772 */
1773struct send_context *pio_select_send_context_sc(struct hfi1_devdata *dd,
1774 u32 selector, u8 sc5)
1775{
1776 u8 vl = sc_to_vlt(dd, sc5);
1777
1778 return pio_select_send_context_vl(dd, selector, vl);
1779}
1780
1781/*
1782 * Free the indicated map struct
1783 */
1784static void pio_map_free(struct pio_vl_map *m)
1785{
1786 int i;
1787
1788 for (i = 0; m && i < m->actual_vls; i++)
1789 kfree(m->map[i]);
1790 kfree(m);
1791}
1792
1793/*
1794 * Handle RCU callback
1795 */
1796static void pio_map_rcu_callback(struct rcu_head *list)
1797{
1798 struct pio_vl_map *m = container_of(list, struct pio_vl_map, list);
1799
1800 pio_map_free(m);
1801}
1802
1803/*
Jubin Johnb4ba6632016-06-09 07:51:08 -07001804 * Set credit return threshold for the kernel send context
1805 */
1806static void set_threshold(struct hfi1_devdata *dd, int scontext, int i)
1807{
1808 u32 thres;
1809
1810 thres = min(sc_percent_to_threshold(dd->kernel_send_context[scontext],
1811 50),
1812 sc_mtu_to_threshold(dd->kernel_send_context[scontext],
1813 dd->vld[i].mtu,
1814 dd->rcd[0]->rcvhdrqentsize));
1815 sc_set_cr_threshold(dd->kernel_send_context[scontext], thres);
1816}
1817
1818/*
Jubin John35f6bef2016-02-14 12:46:10 -08001819 * pio_map_init - called when #vls change
1820 * @dd: hfi1_devdata
1821 * @port: port number
1822 * @num_vls: number of vls
1823 * @vl_scontexts: per vl send context mapping (optional)
1824 *
1825 * This routine changes the mapping based on the number of vls.
1826 *
1827 * vl_scontexts is used to specify a non-uniform vl/send context
1828 * loading. NULL implies auto computing the loading and giving each
1829 * VL an uniform distribution of send contexts per VL.
1830 *
1831 * The auto algorithm computers the sc_per_vl and the number of extra
1832 * send contexts. Any extra send contexts are added from the last VL
1833 * on down
1834 *
1835 * rcu locking is used here to control access to the mapping fields.
1836 *
1837 * If either the num_vls or num_send_contexts are non-power of 2, the
1838 * array sizes in the struct pio_vl_map and the struct pio_map_elem are
1839 * rounded up to the next highest power of 2 and the first entry is
1840 * reused in a round robin fashion.
1841 *
1842 * If an error occurs the map change is not done and the mapping is not
1843 * chaged.
1844 *
1845 */
1846int pio_map_init(struct hfi1_devdata *dd, u8 port, u8 num_vls, u8 *vl_scontexts)
1847{
1848 int i, j;
1849 int extra, sc_per_vl;
1850 int scontext = 1;
1851 int num_kernel_send_contexts = 0;
1852 u8 lvl_scontexts[OPA_MAX_VLS];
1853 struct pio_vl_map *oldmap, *newmap;
1854
1855 if (!vl_scontexts) {
Jubin Johnf1584862016-05-24 12:51:06 -07001856 for (i = 0; i < dd->num_send_contexts; i++)
Jubin John35f6bef2016-02-14 12:46:10 -08001857 if (dd->send_contexts[i].type == SC_KERNEL)
1858 num_kernel_send_contexts++;
1859 /* truncate divide */
1860 sc_per_vl = num_kernel_send_contexts / num_vls;
1861 /* extras */
1862 extra = num_kernel_send_contexts % num_vls;
1863 vl_scontexts = lvl_scontexts;
1864 /* add extras from last vl down */
1865 for (i = num_vls - 1; i >= 0; i--, extra--)
1866 vl_scontexts[i] = sc_per_vl + (extra > 0 ? 1 : 0);
1867 }
1868 /* build new map */
1869 newmap = kzalloc(sizeof(*newmap) +
1870 roundup_pow_of_two(num_vls) *
1871 sizeof(struct pio_map_elem *),
1872 GFP_KERNEL);
1873 if (!newmap)
1874 goto bail;
1875 newmap->actual_vls = num_vls;
1876 newmap->vls = roundup_pow_of_two(num_vls);
1877 newmap->mask = (1 << ilog2(newmap->vls)) - 1;
1878 for (i = 0; i < newmap->vls; i++) {
1879 /* save for wrap around */
1880 int first_scontext = scontext;
1881
1882 if (i < newmap->actual_vls) {
1883 int sz = roundup_pow_of_two(vl_scontexts[i]);
1884
1885 /* only allocate once */
1886 newmap->map[i] = kzalloc(sizeof(*newmap->map[i]) +
1887 sz * sizeof(struct
1888 send_context *),
1889 GFP_KERNEL);
1890 if (!newmap->map[i])
1891 goto bail;
1892 newmap->map[i]->mask = (1 << ilog2(sz)) - 1;
Jubin Johnb4ba6632016-06-09 07:51:08 -07001893 /*
1894 * assign send contexts and
1895 * adjust credit return threshold
1896 */
Jubin John35f6bef2016-02-14 12:46:10 -08001897 for (j = 0; j < sz; j++) {
Jubin Johnb4ba6632016-06-09 07:51:08 -07001898 if (dd->kernel_send_context[scontext]) {
Jubin John35f6bef2016-02-14 12:46:10 -08001899 newmap->map[i]->ksc[j] =
1900 dd->kernel_send_context[scontext];
Jubin Johnb4ba6632016-06-09 07:51:08 -07001901 set_threshold(dd, scontext, i);
1902 }
Jubin John35f6bef2016-02-14 12:46:10 -08001903 if (++scontext >= first_scontext +
1904 vl_scontexts[i])
1905 /* wrap back to first send context */
1906 scontext = first_scontext;
1907 }
1908 } else {
1909 /* just re-use entry without allocating */
1910 newmap->map[i] = newmap->map[i % num_vls];
1911 }
1912 scontext = first_scontext + vl_scontexts[i];
1913 }
1914 /* newmap in hand, save old map */
1915 spin_lock_irq(&dd->pio_map_lock);
1916 oldmap = rcu_dereference_protected(dd->pio_map,
1917 lockdep_is_held(&dd->pio_map_lock));
1918
1919 /* publish newmap */
1920 rcu_assign_pointer(dd->pio_map, newmap);
1921
1922 spin_unlock_irq(&dd->pio_map_lock);
1923 /* success, free any old map after grace period */
1924 if (oldmap)
1925 call_rcu(&oldmap->list, pio_map_rcu_callback);
1926 return 0;
1927bail:
1928 /* free any partial allocation */
1929 pio_map_free(newmap);
1930 return -ENOMEM;
1931}
1932
1933void free_pio_map(struct hfi1_devdata *dd)
1934{
1935 /* Free PIO map if allocated */
1936 if (rcu_access_pointer(dd->pio_map)) {
1937 spin_lock_irq(&dd->pio_map_lock);
Jubin John79d0c082016-02-26 13:33:33 -08001938 pio_map_free(rcu_access_pointer(dd->pio_map));
Jubin John35f6bef2016-02-14 12:46:10 -08001939 RCU_INIT_POINTER(dd->pio_map, NULL);
1940 spin_unlock_irq(&dd->pio_map_lock);
1941 synchronize_rcu();
1942 }
1943 kfree(dd->kernel_send_context);
1944 dd->kernel_send_context = NULL;
1945}
1946
Mike Marciniszyn77241052015-07-30 15:17:43 -04001947int init_pervl_scs(struct hfi1_devdata *dd)
1948{
1949 int i;
Jubin John35f6bef2016-02-14 12:46:10 -08001950 u64 mask, all_vl_mask = (u64)0x80ff; /* VLs 0-7, 15 */
1951 u64 data_vls_mask = (u64)0x00ff; /* VLs 0-7 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001952 u32 ctxt;
Jubin John35f6bef2016-02-14 12:46:10 -08001953 struct hfi1_pportdata *ppd = dd->pport;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001954
Jianxin Xiong44306f12016-04-12 11:30:28 -07001955 dd->vld[15].sc = sc_alloc(dd, SC_VL15,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001956 dd->rcd[0]->rcvhdrqentsize, dd->node);
1957 if (!dd->vld[15].sc)
Ira Weiny042b0152016-07-27 21:06:15 -04001958 return -ENOMEM;
1959
Mike Marciniszyn77241052015-07-30 15:17:43 -04001960 hfi1_init_ctxt(dd->vld[15].sc);
1961 dd->vld[15].mtu = enum_to_mtu(OPA_MTU_2048);
Jubin John35f6bef2016-02-14 12:46:10 -08001962
Tymoteusz Kielan1b23f022016-07-25 13:38:01 -07001963 dd->kernel_send_context = kzalloc_node(dd->num_send_contexts *
Jubin John35f6bef2016-02-14 12:46:10 -08001964 sizeof(struct send_context *),
1965 GFP_KERNEL, dd->node);
Ira Weiny042b0152016-07-27 21:06:15 -04001966 if (!dd->kernel_send_context)
1967 goto freesc15;
1968
Jubin John35f6bef2016-02-14 12:46:10 -08001969 dd->kernel_send_context[0] = dd->vld[15].sc;
1970
Mike Marciniszyn77241052015-07-30 15:17:43 -04001971 for (i = 0; i < num_vls; i++) {
1972 /*
1973 * Since this function does not deal with a specific
1974 * receive context but we need the RcvHdrQ entry size,
1975 * use the size from rcd[0]. It is guaranteed to be
1976 * valid at this point and will remain the same for all
1977 * receive contexts.
1978 */
1979 dd->vld[i].sc = sc_alloc(dd, SC_KERNEL,
1980 dd->rcd[0]->rcvhdrqentsize, dd->node);
1981 if (!dd->vld[i].sc)
1982 goto nomem;
Jubin John35f6bef2016-02-14 12:46:10 -08001983 dd->kernel_send_context[i + 1] = dd->vld[i].sc;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001984 hfi1_init_ctxt(dd->vld[i].sc);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001985 /* non VL15 start with the max MTU */
1986 dd->vld[i].mtu = hfi1_max_mtu;
1987 }
Jubin John35f6bef2016-02-14 12:46:10 -08001988 for (i = num_vls; i < INIT_SC_PER_VL * num_vls; i++) {
1989 dd->kernel_send_context[i + 1] =
1990 sc_alloc(dd, SC_KERNEL, dd->rcd[0]->rcvhdrqentsize, dd->node);
1991 if (!dd->kernel_send_context[i + 1])
1992 goto nomem;
1993 hfi1_init_ctxt(dd->kernel_send_context[i + 1]);
1994 }
1995
Mike Marciniszyn77241052015-07-30 15:17:43 -04001996 sc_enable(dd->vld[15].sc);
1997 ctxt = dd->vld[15].sc->hw_context;
1998 mask = all_vl_mask & ~(1LL << 15);
1999 write_kctxt_csr(dd, ctxt, SC(CHECK_VL), mask);
2000 dd_dev_info(dd,
2001 "Using send context %u(%u) for VL15\n",
2002 dd->vld[15].sc->sw_index, ctxt);
Jubin John35f6bef2016-02-14 12:46:10 -08002003
Mike Marciniszyn77241052015-07-30 15:17:43 -04002004 for (i = 0; i < num_vls; i++) {
2005 sc_enable(dd->vld[i].sc);
2006 ctxt = dd->vld[i].sc->hw_context;
Jubin John35f6bef2016-02-14 12:46:10 -08002007 mask = all_vl_mask & ~(data_vls_mask);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002008 write_kctxt_csr(dd, ctxt, SC(CHECK_VL), mask);
2009 }
Jubin John35f6bef2016-02-14 12:46:10 -08002010 for (i = num_vls; i < INIT_SC_PER_VL * num_vls; i++) {
2011 sc_enable(dd->kernel_send_context[i + 1]);
2012 ctxt = dd->kernel_send_context[i + 1]->hw_context;
2013 mask = all_vl_mask & ~(data_vls_mask);
2014 write_kctxt_csr(dd, ctxt, SC(CHECK_VL), mask);
2015 }
2016
2017 if (pio_map_init(dd, ppd->port - 1, num_vls, NULL))
2018 goto nomem;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002019 return 0;
Ira Weiny042b0152016-07-27 21:06:15 -04002020
Mike Marciniszyn77241052015-07-30 15:17:43 -04002021nomem:
Ira Weiny042b0152016-07-27 21:06:15 -04002022 for (i = 0; i < num_vls; i++) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04002023 sc_free(dd->vld[i].sc);
Ira Weiny042b0152016-07-27 21:06:15 -04002024 dd->vld[i].sc = NULL;
2025 }
2026
Jubin John35f6bef2016-02-14 12:46:10 -08002027 for (i = num_vls; i < INIT_SC_PER_VL * num_vls; i++)
2028 sc_free(dd->kernel_send_context[i + 1]);
Ira Weiny042b0152016-07-27 21:06:15 -04002029
2030 kfree(dd->kernel_send_context);
2031 dd->kernel_send_context = NULL;
2032
2033freesc15:
2034 sc_free(dd->vld[15].sc);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002035 return -ENOMEM;
2036}
2037
2038int init_credit_return(struct hfi1_devdata *dd)
2039{
2040 int ret;
2041 int num_numa;
2042 int i;
2043
2044 num_numa = num_online_nodes();
2045 /* enforce the expectation that the numas are compact */
2046 for (i = 0; i < num_numa; i++) {
2047 if (!node_online(i)) {
2048 dd_dev_err(dd, "NUMA nodes are not compact\n");
2049 ret = -EINVAL;
2050 goto done;
2051 }
2052 }
2053
2054 dd->cr_base = kcalloc(
2055 num_numa,
2056 sizeof(struct credit_return_base),
2057 GFP_KERNEL);
2058 if (!dd->cr_base) {
2059 dd_dev_err(dd, "Unable to allocate credit return base\n");
2060 ret = -ENOMEM;
2061 goto done;
2062 }
2063 for (i = 0; i < num_numa; i++) {
2064 int bytes = TXE_NUM_CONTEXTS * sizeof(struct credit_return);
2065
2066 set_dev_node(&dd->pcidev->dev, i);
2067 dd->cr_base[i].va = dma_zalloc_coherent(
2068 &dd->pcidev->dev,
2069 bytes,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07002070 &dd->cr_base[i].dma,
Mike Marciniszyn77241052015-07-30 15:17:43 -04002071 GFP_KERNEL);
Jubin Johnd125a6c2016-02-14 20:19:49 -08002072 if (!dd->cr_base[i].va) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04002073 set_dev_node(&dd->pcidev->dev, dd->node);
2074 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08002075 "Unable to allocate credit return DMA range for NUMA %d\n",
2076 i);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002077 ret = -ENOMEM;
2078 goto done;
2079 }
2080 }
2081 set_dev_node(&dd->pcidev->dev, dd->node);
2082
2083 ret = 0;
2084done:
2085 return ret;
2086}
2087
2088void free_credit_return(struct hfi1_devdata *dd)
2089{
2090 int num_numa;
2091 int i;
2092
2093 if (!dd->cr_base)
2094 return;
2095
2096 num_numa = num_online_nodes();
2097 for (i = 0; i < num_numa; i++) {
2098 if (dd->cr_base[i].va) {
2099 dma_free_coherent(&dd->pcidev->dev,
Jubin John17fb4f22016-02-14 20:21:52 -08002100 TXE_NUM_CONTEXTS *
2101 sizeof(struct credit_return),
2102 dd->cr_base[i].va,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07002103 dd->cr_base[i].dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002104 }
2105 }
2106 kfree(dd->cr_base);
2107 dd->cr_base = NULL;
2108}