3 * This file is subject to the terms and conditions of the GNU General Public
4 * License. See the file "COPYING" in the main directory of this archive
7 * Copyright (C) 2001-2003 Silicon Graphics, Inc. All rights reserved.
10 #include <linux/types.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <asm/sn/sgi.h>
14 #include <asm/sn/sn_cpuid.h>
15 #include <asm/sn/addrs.h>
16 #include <asm/sn/arch.h>
17 #include <asm/sn/iograph.h>
18 #include <asm/sn/invent.h>
19 #include <asm/sn/hcl.h>
20 #include <asm/sn/labelcl.h>
21 #include <asm/sn/xtalk/xwidget.h>
22 #include <asm/sn/pci/bridge.h>
23 #include <asm/sn/pci/pciio.h>
24 #include <asm/sn/pci/pcibr.h>
25 #include <asm/sn/pci/pcibr_private.h>
26 #include <asm/sn/pci/pci_defs.h>
27 #include <asm/sn/prio.h>
28 #include <asm/sn/xtalk/xbow.h>
29 #include <asm/sn/ioc3.h>
30 #include <asm/sn/io.h>
31 #include <asm/sn/sn_private.h>
33 void do_pcibr_rrb_clear(bridge_t *, int);
34 void do_pcibr_rrb_flush(bridge_t *, int);
35 int do_pcibr_rrb_count_valid(bridge_t *, pciio_slot_t, int);
36 int do_pcibr_rrb_count_avail(bridge_t *, pciio_slot_t);
37 int do_pcibr_rrb_alloc(bridge_t *, pciio_slot_t, int, int);
38 int do_pcibr_rrb_free(bridge_t *, pciio_slot_t, int, int);
39 void do_pcibr_rrb_free_all(pcibr_soft_t, bridge_t *, pciio_slot_t);
41 void do_pcibr_rrb_autoalloc(pcibr_soft_t, int, int, int);
43 int pcibr_wrb_flush(vertex_hdl_t);
44 int pcibr_rrb_alloc(vertex_hdl_t, int *, int *);
45 int pcibr_rrb_check(vertex_hdl_t, int *, int *, int *, int *);
46 void pcibr_rrb_flush(vertex_hdl_t);
47 int pcibr_slot_initial_rrb_alloc(vertex_hdl_t,pciio_slot_t);
49 void pcibr_rrb_debug(char *, pcibr_soft_t);
54 * All the do_pcibr_rrb_ routines manipulate the Read Response Buffer (rrb)
55 * registers within the Bridge. Two 32 registers (b_rrb_map[2] also known
56 * as the b_even_resp & b_odd_resp registers) are used to allocate the 16
57 * rrbs to devices. The b_even_resp register represents even num devices,
58 * and b_odd_resp represent odd number devices. Each rrb is represented by
59 * 4-bits within a register.
60 * BRIDGE & XBRIDGE: 1 enable bit, 1 virtual channel bit, 2 device bits
61 * PIC: 1 enable bit, 2 virtual channel bits, 1 device bit
62 * PIC has 4 devices per bus, and 4 virtual channels (1 normal & 3 virtual)
63 * per device. BRIDGE & XBRIDGE have 8 devices per bus and 2 virtual
64 * channels (1 normal & 1 virtual) per device. See the BRIDGE and PIC ASIC
65 * Programmers Reference guides for more information.
68 #define RRB_MASK (0xf) /* mask a single rrb within reg */
69 #define RRB_SIZE (4) /* sizeof rrb within reg (bits) */
71 #define RRB_ENABLE_BIT(bridge) (0x8) /* [BRIDGE | PIC]_RRB_EN */
72 #define NUM_PDEV_BITS(bridge) (1)
73 #define NUM_VDEV_BITS(bridge) (2)
74 #define NUMBER_VCHANNELS(bridge) (4)
75 #define SLOT_2_PDEV(bridge, slot) ((slot) >> 1)
76 #define SLOT_2_RRB_REG(bridge, slot) ((slot) & 0x1)
78 /* validate that the slot and virtual channel are valid for a given bridge */
79 #define VALIDATE_SLOT_n_VCHAN(bridge, s, v) \
80 (((((s) != PCIIO_SLOT_NONE) && ((s) <= (pciio_slot_t)3)) && (((v) >= 0) && ((v) <= 3))) ? 1 : 0)
83 * Count how many RRBs are marked valid for the specified PCI slot
84 * and virtual channel. Return the count.
87 do_pcibr_rrb_count_valid(bridge_t *bridge,
92 uint16_t enable_bit, vchan_bits, pdev_bits, rrb_bits;
95 if (!VALIDATE_SLOT_n_VCHAN(bridge, slot, vchan)) {
96 printk(KERN_WARNING "do_pcibr_rrb_count_valid() invalid slot/vchan [%d/%d]\n", slot, vchan);
100 enable_bit = RRB_ENABLE_BIT(bridge);
101 vchan_bits = vchan << NUM_PDEV_BITS(bridge);
102 pdev_bits = SLOT_2_PDEV(bridge, slot);
103 rrb_bits = enable_bit | vchan_bits | pdev_bits;
105 tmp = bridge->b_rrb_map[SLOT_2_RRB_REG(bridge, slot)].reg;
107 for (rrb_index = 0; rrb_index < 8; rrb_index++) {
108 if ((tmp & RRB_MASK) == rrb_bits)
110 tmp = (tmp >> RRB_SIZE);
117 * Count how many RRBs are available to be allocated to the specified
118 * slot. Return the count.
121 do_pcibr_rrb_count_avail(bridge_t *bridge,
126 int rrb_index, cnt=0;
128 if (!VALIDATE_SLOT_n_VCHAN(bridge, slot, 0)) {
129 printk(KERN_WARNING "do_pcibr_rrb_count_avail() invalid slot/vchan");
133 enable_bit = RRB_ENABLE_BIT(bridge);
135 tmp = bridge->b_rrb_map[SLOT_2_RRB_REG(bridge, slot)].reg;
137 for (rrb_index = 0; rrb_index < 8; rrb_index++) {
138 if ((tmp & enable_bit) != enable_bit)
140 tmp = (tmp >> RRB_SIZE);
147 * Allocate some additional RRBs for the specified slot and the specified
148 * virtual channel. Returns -1 if there were insufficient free RRBs to
149 * satisfy the request, or 0 if the request was fulfilled.
151 * Note that if a request can be partially filled, it will be, even if
155 do_pcibr_rrb_alloc(bridge_t *bridge,
160 bridgereg_t reg, tmp = (bridgereg_t)0;
161 uint16_t enable_bit, vchan_bits, pdev_bits, rrb_bits;
164 if (!VALIDATE_SLOT_n_VCHAN(bridge, slot, vchan)) {
165 printk(KERN_WARNING "do_pcibr_rrb_alloc() invalid slot/vchan");
169 enable_bit = RRB_ENABLE_BIT(bridge);
170 vchan_bits = vchan << NUM_PDEV_BITS(bridge);
171 pdev_bits = SLOT_2_PDEV(bridge, slot);
172 rrb_bits = enable_bit | vchan_bits | pdev_bits;
174 reg = tmp = bridge->b_rrb_map[SLOT_2_RRB_REG(bridge, slot)].reg;
176 for (rrb_index = 0; ((rrb_index < 8) && (more > 0)); rrb_index++) {
177 if ((tmp & enable_bit) != enable_bit) {
178 /* clear the rrb and OR in the new rrb into 'reg' */
179 reg = reg & ~(RRB_MASK << (RRB_SIZE * rrb_index));
180 reg = reg | (rrb_bits << (RRB_SIZE * rrb_index));
183 tmp = (tmp >> RRB_SIZE);
186 bridge->b_rrb_map[SLOT_2_RRB_REG(bridge, slot)].reg = reg;
187 return (more ? -1 : 0);
192 * Release some of the RRBs that have been allocated for the specified
193 * slot. Returns zero for success, or negative if it was unable to free
196 * Note that if a request can be partially fulfilled, it will be, even
197 * if we return failure.
200 do_pcibr_rrb_free(bridge_t *bridge,
205 bridgereg_t reg, tmp = (bridgereg_t)0, clr = 0;
206 uint16_t enable_bit, vchan_bits, pdev_bits, rrb_bits;
209 if (!VALIDATE_SLOT_n_VCHAN(bridge, slot, vchan)) {
210 printk(KERN_WARNING "do_pcibr_rrb_free() invalid slot/vchan");
214 enable_bit = RRB_ENABLE_BIT(bridge);
215 vchan_bits = vchan << NUM_PDEV_BITS(bridge);
216 pdev_bits = SLOT_2_PDEV(bridge, slot);
217 rrb_bits = enable_bit | vchan_bits | pdev_bits;
219 reg = tmp = bridge->b_rrb_map[SLOT_2_RRB_REG(bridge, slot)].reg;
221 for (rrb_index = 0; ((rrb_index < 8) && (less > 0)); rrb_index++) {
222 if ((tmp & RRB_MASK) == rrb_bits) {
224 * the old do_pcibr_rrb_free() code only clears the enable bit
225 * but I say we should clear the whole rrb (ie):
226 * reg = reg & ~(RRB_MASK << (RRB_SIZE * rrb_index));
227 * But to be compatible with old code we'll only clear enable.
229 reg = reg & ~(RRB_ENABLE_BIT(bridge) << (RRB_SIZE * rrb_index));
230 clr = clr | (enable_bit << (RRB_SIZE * rrb_index));
233 tmp = (tmp >> RRB_SIZE);
236 bridge->b_rrb_map[SLOT_2_RRB_REG(bridge, slot)].reg = reg;
238 /* call do_pcibr_rrb_clear() for all the rrbs we've freed */
239 for (rrb_index = 0; rrb_index < 8; rrb_index++) {
240 int evn_odd = SLOT_2_RRB_REG(bridge, slot);
241 if (clr & (enable_bit << (RRB_SIZE * rrb_index)))
242 do_pcibr_rrb_clear(bridge, (2 * rrb_index) + evn_odd);
245 return (less ? -1 : 0);
250 * free all the rrbs (both the normal and virtual channels) for the
254 do_pcibr_rrb_free_all(pcibr_soft_t pcibr_soft,
259 int vchan_total = NUMBER_VCHANNELS(bridge);
261 /* pretend we own all 8 rrbs and just ignore the return value */
262 for (vchan = 0; vchan < vchan_total; vchan++) {
263 (void)do_pcibr_rrb_free(bridge, slot, vchan, 8);
264 pcibr_soft->bs_rrb_valid[slot][vchan] = 0;
270 * Wait for the the specified rrb to have no outstanding XIO pkts
271 * and for all data to be drained. Mark the rrb as no longer being
275 do_pcibr_rrb_clear(bridge_t *bridge, int rrb)
279 /* bridge_lock must be held;
280 * this RRB must be disabled.
283 /* wait until RRB has no outstanduing XIO packets. */
284 while ((status = bridge->b_resp_status) & BRIDGE_RRB_INUSE(rrb)) {
285 ; /* XXX- beats on bridge. bad idea? */
288 /* if the RRB has data, drain it. */
289 if (status & BRIDGE_RRB_VALID(rrb)) {
290 bridge->b_resp_clear = BRIDGE_RRB_CLEAR(rrb);
292 /* wait until RRB is no longer valid. */
293 while ((status = bridge->b_resp_status) & BRIDGE_RRB_VALID(rrb)) {
294 ; /* XXX- beats on bridge. bad idea? */
301 * Flush the specified rrb by calling do_pcibr_rrb_clear(). This
302 * routine is just a wrapper to make sure the rrb is disabled
303 * before calling do_pcibr_rrb_clear().
306 do_pcibr_rrb_flush(bridge_t *bridge, int rrbn)
308 reg_p rrbp = &bridge->b_rrb_map[rrbn & 1].reg;
310 int shft = (RRB_SIZE * (rrbn >> 1));
311 unsigned long ebit = RRB_ENABLE_BIT(bridge) << shft;
316 *rrbp = rrbv & ~ebit;
319 do_pcibr_rrb_clear(bridge, rrbn);
328 do_pcibr_rrb_autoalloc(pcibr_soft_t pcibr_soft,
333 bridge_t *bridge = pcibr_soft->bs_base;
336 for (got = 0; got < more_rrbs; ++got) {
337 if (pcibr_soft->bs_rrb_res[slot] > 0)
338 pcibr_soft->bs_rrb_res[slot]--;
339 else if (pcibr_soft->bs_rrb_avail[slot & 1] > 0)
340 pcibr_soft->bs_rrb_avail[slot & 1]--;
343 if (do_pcibr_rrb_alloc(bridge, slot, vchan, 1) < 0)
346 pcibr_soft->bs_rrb_valid[slot][vchan]++;
349 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_RRB, pcibr_soft->bs_vhdl,
350 "do_pcibr_rrb_autoalloc: added %d (of %d requested) RRBs "
351 "to slot %d, vchan %d\n", got, more_rrbs,
352 PCIBR_DEVICE_TO_SLOT(pcibr_soft, slot), vchan));
354 pcibr_rrb_debug("do_pcibr_rrb_autoalloc", pcibr_soft);
359 * Flush all the rrb's assigned to the specified connection point.
362 pcibr_rrb_flush(vertex_hdl_t pconn_vhdl)
364 pciio_info_t pciio_info = pciio_info_get(pconn_vhdl);
365 pcibr_soft_t pcibr_soft = (pcibr_soft_t)pciio_info_mfast_get(pciio_info);
366 pciio_slot_t slot = PCIBR_INFO_SLOT_GET_INT(pciio_info);
367 bridge_t *bridge = pcibr_soft->bs_base;
370 uint16_t enable_bit, pdev_bits, rrb_bits, rrb_mask;
374 enable_bit = RRB_ENABLE_BIT(bridge);
375 pdev_bits = SLOT_2_PDEV(bridge, slot);
376 rrb_bits = enable_bit | pdev_bits;
377 rrb_mask = enable_bit | ((NUM_PDEV_BITS(bridge) << 1) - 1);
379 tmp = bridge->b_rrb_map[SLOT_2_RRB_REG(bridge, slot)].reg;
381 s = pcibr_lock(pcibr_soft);
382 for (rrb_index = 0; rrb_index < 8; rrb_index++) {
383 int evn_odd = SLOT_2_RRB_REG(bridge, slot);
384 if ((tmp & rrb_mask) == rrb_bits)
385 do_pcibr_rrb_flush(bridge, (2 * rrb_index) + evn_odd);
386 tmp = (tmp >> RRB_SIZE);
388 pcibr_unlock(pcibr_soft, s);
393 * Device driver interface to flush the write buffers for a specified
394 * device hanging off the bridge.
397 pcibr_wrb_flush(vertex_hdl_t pconn_vhdl)
399 pciio_info_t pciio_info = pciio_info_get(pconn_vhdl);
400 pciio_slot_t pciio_slot = PCIBR_INFO_SLOT_GET_INT(pciio_info);
401 pcibr_soft_t pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info);
402 bridge_t *bridge = pcibr_soft->bs_base;
403 volatile bridgereg_t *wrb_flush;
405 wrb_flush = &(bridge->b_wr_req_buf[pciio_slot].reg);
406 if ( IS_PIC_SOFT(pcibr_soft) ) {
414 * Device driver interface to request RRBs for a specified device
415 * hanging off a Bridge. The driver requests the total number of
416 * RRBs it would like for the normal channel (vchan0) and for the
417 * "virtual channel" (vchan1). The actual number allocated to each
418 * channel is returned.
420 * If we cannot allocate at least one RRB to a channel that needs
421 * at least one, return -1 (failure). Otherwise, satisfy the request
422 * as best we can and return 0.
425 pcibr_rrb_alloc(vertex_hdl_t pconn_vhdl,
429 pciio_info_t pciio_info = pciio_info_get(pconn_vhdl);
430 pciio_slot_t pciio_slot = PCIBR_INFO_SLOT_GET_INT(pciio_info);
431 pcibr_soft_t pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info);
432 bridge_t *bridge = pcibr_soft->bs_base;
449 * TBD: temper request with admin info about RRB allocation,
450 * and according to demand from other devices on this Bridge.
452 * One way of doing this would be to allocate two RRBs
453 * for each device on the bus, before any drivers start
454 * asking for extras. This has the weakness that one
455 * driver might not give back an "extra" RRB until after
456 * another driver has already failed to get one that
460 s = pcibr_lock(pcibr_soft);
462 vchan_total = NUMBER_VCHANNELS(bridge);
464 /* Save the boot-time RRB configuration for this slot */
465 if (pcibr_soft->bs_rrb_valid_dflt[pciio_slot][VCHAN0] < 0) {
466 for (vchan = 0; vchan < vchan_total; vchan++)
467 pcibr_soft->bs_rrb_valid_dflt[pciio_slot][vchan] =
468 pcibr_soft->bs_rrb_valid[pciio_slot][vchan];
469 pcibr_soft->bs_rrb_res_dflt[pciio_slot] =
470 pcibr_soft->bs_rrb_res[pciio_slot];
474 /* How many RRBs do we own? */
475 orig_vchan0 = pcibr_soft->bs_rrb_valid[pciio_slot][VCHAN0];
476 orig_vchan1 = pcibr_soft->bs_rrb_valid[pciio_slot][VCHAN1];
478 /* How many RRBs do we want? */
479 desired_vchan0 = count_vchan0 ? *count_vchan0 : orig_vchan0;
480 desired_vchan1 = count_vchan1 ? *count_vchan1 : orig_vchan1;
482 /* How many RRBs are free? */
483 avail_rrbs = pcibr_soft->bs_rrb_avail[pciio_slot & 1]
484 + pcibr_soft->bs_rrb_res[pciio_slot];
486 /* Figure desired deltas */
487 delta_vchan0 = desired_vchan0 - orig_vchan0;
488 delta_vchan1 = desired_vchan1 - orig_vchan1;
490 /* Trim back deltas to something
491 * that we can actually meet, by
492 * decreasing the ending allocation
493 * for whichever channel wants
494 * more RRBs. If both want the same
495 * number, cut the second channel.
496 * NOTE: do not change the allocation for
497 * a channel that was passed as NULL.
499 while ((delta_vchan0 + delta_vchan1) > avail_rrbs) {
502 ((orig_vchan0 + delta_vchan0) >
503 (orig_vchan1 + delta_vchan1))))
509 /* Figure final RRB allocations
511 final_vchan0 = orig_vchan0 + delta_vchan0;
512 final_vchan1 = orig_vchan1 + delta_vchan1;
514 /* If either channel wants RRBs but our actions
515 * would leave it with none, declare an error,
516 * but DO NOT change any RRB allocations.
518 if ((desired_vchan0 && !final_vchan0) ||
519 (desired_vchan1 && !final_vchan1)) {
525 /* Commit the allocations: free, then alloc.
527 if (delta_vchan0 < 0)
528 (void) do_pcibr_rrb_free(bridge, pciio_slot, VCHAN0, -delta_vchan0);
529 if (delta_vchan1 < 0)
530 (void) do_pcibr_rrb_free(bridge, pciio_slot, VCHAN1, -delta_vchan1);
532 if (delta_vchan0 > 0)
533 (void) do_pcibr_rrb_alloc(bridge, pciio_slot, VCHAN0, delta_vchan0);
534 if (delta_vchan1 > 0)
535 (void) do_pcibr_rrb_alloc(bridge, pciio_slot, VCHAN1, delta_vchan1);
537 /* Return final values to caller.
540 *count_vchan0 = final_vchan0;
542 *count_vchan1 = final_vchan1;
544 /* prevent automatic changes to this slot's RRBs
546 pcibr_soft->bs_rrb_fixed |= 1 << pciio_slot;
548 /* Track the actual allocations, release
549 * any further reservations, and update the
550 * number of available RRBs.
553 pcibr_soft->bs_rrb_valid[pciio_slot][VCHAN0] = final_vchan0;
554 pcibr_soft->bs_rrb_valid[pciio_slot][VCHAN1] = final_vchan1;
555 pcibr_soft->bs_rrb_avail[pciio_slot & 1] =
556 pcibr_soft->bs_rrb_avail[pciio_slot & 1]
557 + pcibr_soft->bs_rrb_res[pciio_slot]
560 pcibr_soft->bs_rrb_res[pciio_slot] = 0;
563 * Reserve enough RRBs so this slot's RRB configuration can be
564 * reset to its boot-time default following a hot-plug shut-down
566 res_rrbs = (pcibr_soft->bs_rrb_res_dflt[pciio_slot] -
567 pcibr_soft->bs_rrb_res[pciio_slot]);
568 for (vchan = 0; vchan < vchan_total; vchan++) {
569 res_rrbs += (pcibr_soft->bs_rrb_valid_dflt[pciio_slot][vchan] -
570 pcibr_soft->bs_rrb_valid[pciio_slot][vchan]);
574 pcibr_soft->bs_rrb_res[pciio_slot] = res_rrbs;
575 pcibr_soft->bs_rrb_avail[pciio_slot & 1] =
576 pcibr_soft->bs_rrb_avail[pciio_slot & 1]
580 pcibr_rrb_debug("pcibr_rrb_alloc", pcibr_soft);
585 pcibr_unlock(pcibr_soft, s);
591 * Device driver interface to check the current state
592 * of the RRB allocations.
594 * pconn_vhdl is your PCI connection point (specifies which
595 * PCI bus and which slot).
597 * count_vchan0 points to where to return the number of RRBs
598 * assigned to the primary DMA channel, used by all DMA
599 * that does not explicitly ask for the alternate virtual
602 * count_vchan1 points to where to return the number of RRBs
603 * assigned to the secondary DMA channel, used when
604 * PCIBR_VCHAN1 and PCIIO_DMA_A64 are specified.
606 * count_reserved points to where to return the number of RRBs
607 * that have been automatically reserved for your device at
608 * startup, but which have not been assigned to a
609 * channel. RRBs must be assigned to a channel to be used;
610 * this can be done either with an explicit pcibr_rrb_alloc
611 * call, or automatically by the infrastructure when a DMA
612 * translation is constructed. Any call to pcibr_rrb_alloc
613 * will release any unassigned reserved RRBs back to the
616 * count_pool points to where to return the number of RRBs
617 * that are currently unassigned and unreserved. This
618 * number can (and will) change as other drivers make calls
619 * to pcibr_rrb_alloc, or automatically allocate RRBs for
620 * DMA beyond their initial reservation.
622 * NULL may be passed for any of the return value pointers
623 * the caller is not interested in.
625 * The return value is "0" if all went well, or "-1" if
626 * there is a problem. Additionally, if the wrong vertex
627 * is passed in, one of the subsidiary support functions
628 * could panic with a "bad pciio fingerprint."
632 pcibr_rrb_check(vertex_hdl_t pconn_vhdl,
638 pciio_info_t pciio_info;
639 pciio_slot_t pciio_slot;
640 pcibr_soft_t pcibr_soft;
644 if ((pciio_info = pciio_info_get(pconn_vhdl)) &&
645 (pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info)) &&
646 ((pciio_slot = PCIBR_INFO_SLOT_GET_INT(pciio_info)) < PCIBR_NUM_SLOTS(pcibr_soft))) {
648 s = pcibr_lock(pcibr_soft);
652 pcibr_soft->bs_rrb_valid[pciio_slot][VCHAN0];
656 pcibr_soft->bs_rrb_valid[pciio_slot][VCHAN1];
660 pcibr_soft->bs_rrb_res[pciio_slot];
664 pcibr_soft->bs_rrb_avail[pciio_slot & 1];
668 pcibr_unlock(pcibr_soft, s);
674 * pcibr_slot_initial_rrb_alloc
675 * Allocate a default number of rrbs for this slot on
676 * the two channels. This is dictated by the rrb allocation
677 * strategy routine defined per platform.
681 pcibr_slot_initial_rrb_alloc(vertex_hdl_t pcibr_vhdl,
684 pcibr_soft_t pcibr_soft;
685 pcibr_info_h pcibr_infoh;
686 pcibr_info_t pcibr_info;
692 pcibr_soft = pcibr_soft_get(pcibr_vhdl);
697 if (!PCIBR_VALID_SLOT(pcibr_soft, slot))
700 bridge = pcibr_soft->bs_base;
702 /* How many RRBs are on this slot? */
703 vchan_total = NUMBER_VCHANNELS(bridge);
704 for (vchan = 0; vchan < vchan_total; vchan++)
705 chan[vchan] = do_pcibr_rrb_count_valid(bridge, slot, vchan);
707 if (IS_PIC_SOFT(pcibr_soft)) {
708 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_RRB, pcibr_vhdl,
709 "pcibr_slot_initial_rrb_alloc: slot %d started with %d+%d+%d+%d\n",
710 PCIBR_DEVICE_TO_SLOT(pcibr_soft, slot),
711 chan[VCHAN0], chan[VCHAN1], chan[VCHAN2], chan[VCHAN3]));
713 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_RRB, pcibr_vhdl,
714 "pcibr_slot_initial_rrb_alloc: slot %d started with %d+%d\n",
715 PCIBR_DEVICE_TO_SLOT(pcibr_soft, slot),
716 chan[VCHAN0], chan[VCHAN1]));
719 /* Do we really need any?
721 pcibr_infoh = pcibr_soft->bs_slot[slot].bss_infos;
722 pcibr_info = pcibr_infoh[0];
724 if (PCIBR_WAR_ENABLED(PV856866, pcibr_soft) && IS_PIC_SOFT(pcibr_soft) &&
725 (slot == 2 || slot == 3) &&
726 (pcibr_info->f_vendor == PCIIO_VENDOR_ID_NONE) &&
727 !pcibr_soft->bs_slot[slot].has_host) {
729 for (vchan = 0; vchan < 2; vchan++) {
730 do_pcibr_rrb_free(bridge, slot, vchan, 8);
731 pcibr_soft->bs_rrb_valid[slot][vchan] = 0;
734 pcibr_soft->bs_rrb_valid[slot][3] = chan[3];
739 /* Give back any assigned to empty slots */
740 if ((pcibr_info->f_vendor == PCIIO_VENDOR_ID_NONE) && !pcibr_soft->bs_slot[slot].has_host) {
741 do_pcibr_rrb_free_all(pcibr_soft, bridge, slot);
745 for (vchan = 0; vchan < vchan_total; vchan++)
746 pcibr_soft->bs_rrb_valid[slot][vchan] = chan[vchan];
752 rrb_reserved_free(pcibr_soft_t pcibr_soft, int slot)
754 int res = pcibr_soft->bs_rrb_res[slot];
757 pcibr_soft->bs_rrb_avail[slot & 1] += res;
758 pcibr_soft->bs_rrb_res[slot] = 0;
764 * Assign an equal total number of RRBs to all candidate slots,
765 * where the total is the sum of the number of RRBs assigned to
766 * the normal channel, the number of RRBs assigned to the virtual
767 * channels, and the number of RRBs assigned as reserved.
769 * A candidate slot is any existing (populated or empty) slot.
770 * Empty SN1 slots need RRBs to support hot-plug operations.
774 pcibr_initial_rrb(vertex_hdl_t pcibr_vhdl,
775 pciio_slot_t first, pciio_slot_t last)
777 pcibr_soft_t pcibr_soft = pcibr_soft_get(pcibr_vhdl);
778 bridge_t *bridge = pcibr_soft->bs_base;
787 have[0][0] = have[0][1] = have[0][2] = 0;
788 have[1][0] = have[1][1] = have[1][2] = 0;
791 vchan_total = NUMBER_VCHANNELS(bridge);
793 for (slot = pcibr_soft->bs_min_slot;
794 slot < PCIBR_NUM_SLOTS(pcibr_soft); ++slot) {
795 /* Initial RRB management; give back RRBs in all non-existent slots */
796 (void) pcibr_slot_initial_rrb_alloc(pcibr_vhdl, slot);
798 /* Base calculations only on existing slots */
799 if ((slot >= first) && (slot <= last)) {
801 for (vchan = 0; vchan < vchan_total; vchan++)
802 rrb_total += pcibr_soft->bs_rrb_valid[slot][vchan];
805 have[slot & 1][rrb_total]++;
809 /* Initialize even/odd slot available RRB counts */
810 pcibr_soft->bs_rrb_avail[0] = do_pcibr_rrb_count_avail(bridge, 0);
811 pcibr_soft->bs_rrb_avail[1] = do_pcibr_rrb_count_avail(bridge, 1);
814 * Calculate reserved RRBs for slots based on current RRB usage
816 for (eo = 0; eo < 2; eo++) {
817 if ((3 * have[eo][0] + 2 * have[eo][1] + have[eo][2]) <= pcibr_soft->bs_rrb_avail[eo])
819 else if ((2 * have[eo][0] + have[eo][1]) <= pcibr_soft->bs_rrb_avail[eo])
821 else if (have[eo][0] <= pcibr_soft->bs_rrb_avail[eo])
828 /* Assign reserved RRBs to existing slots */
829 for (slot = first; slot <= last; ++slot) {
833 for (vchan = 0; vchan < vchan_total; vchan++)
834 rrb_total += pcibr_soft->bs_rrb_valid[slot][vchan];
836 r = res[slot & 1] - (rrb_total);
839 pcibr_soft->bs_rrb_res[slot] = r;
840 pcibr_soft->bs_rrb_avail[slot & 1] -= r;
844 pcibr_rrb_debug("pcibr_initial_rrb", pcibr_soft);
851 * Dump the pcibr_soft_t RRB state variable
854 pcibr_rrb_debug(char *calling_func, pcibr_soft_t pcibr_soft)
859 if (pcibr_debug_mask & PCIBR_DEBUG_RRB) {
860 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_RRB, pcibr_soft->bs_vhdl,
861 "%s: rrbs available, even=%d, odd=%d\n", calling_func,
862 pcibr_soft->bs_rrb_avail[0], pcibr_soft->bs_rrb_avail[1]));
864 if (IS_PIC_SOFT(pcibr_soft)) {
865 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_RRB, pcibr_soft->bs_vhdl,
866 "\tslot\tvchan0\tvchan1\tvchan2\tvchan3\treserved\n"));
868 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_RRB, pcibr_soft->bs_vhdl,
869 "\tslot\tvchan0\tvchan1\treserved\n"));
872 for (slot=0; slot < PCIBR_NUM_SLOTS(pcibr_soft); slot++) {
874 * The kernel only allows functions to have so many variable args,
875 * attempting to call PCIBR_DEBUG_ALWAYS() with more than 5 printf
876 * arguments fails so sprintf() it into a temporary string.
878 if (IS_PIC_SOFT(pcibr_soft)) {
879 sprintf(tmp_str, "\t %d\t %d\t %d\t %d\t %d\t %d\n",
880 PCIBR_DEVICE_TO_SLOT(pcibr_soft, slot),
881 0xFFF & pcibr_soft->bs_rrb_valid[slot][VCHAN0],
882 0xFFF & pcibr_soft->bs_rrb_valid[slot][VCHAN1],
883 0xFFF & pcibr_soft->bs_rrb_valid[slot][VCHAN2],
884 0xFFF & pcibr_soft->bs_rrb_valid[slot][VCHAN3],
885 pcibr_soft->bs_rrb_res[slot]);
887 sprintf(tmp_str, "\t %d\t %d\t %d\t %d\n",
888 PCIBR_DEVICE_TO_SLOT(pcibr_soft, slot),
889 0xFFF & pcibr_soft->bs_rrb_valid[slot][VCHAN0],
890 0xFFF & pcibr_soft->bs_rrb_valid[slot][VCHAN1],
891 pcibr_soft->bs_rrb_res[slot]);
894 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_RRB, pcibr_soft->bs_vhdl,