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