patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / ia64 / sn / io / sn2 / pcibr / pcibr_slot.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 2001-2004 Silicon Graphics, Inc. All rights reserved.
7  */
8
9 #include <linux/types.h>
10 #include <asm/sn/sgi.h>
11 #include <asm/sn/sn_cpuid.h>
12 #include <asm/uaccess.h>
13 #include <asm/sn/iograph.h>
14 #include <asm/sn/pci/pciio.h>
15 #include <asm/sn/pci/pcibr.h>
16 #include <asm/sn/pci/pcibr_private.h>
17 #include <asm/sn/pci/pci_defs.h>
18 #include <asm/sn/sn_private.h>
19 #include <asm/sn/sn_sal.h>
20
21 extern pcibr_info_t     pcibr_info_get(vertex_hdl_t);
22 extern int              pcibr_widget_to_bus(vertex_hdl_t pcibr_vhdl);
23 extern pcibr_info_t     pcibr_device_info_new(pcibr_soft_t, pciio_slot_t, pciio_function_t, pciio_vendor_id_t, pciio_device_id_t);
24 extern int              pcibr_slot_initial_rrb_alloc(vertex_hdl_t,pciio_slot_t);
25 extern int              pcibr_pcix_rbars_calc(pcibr_soft_t);
26
27 extern char *pci_space[];
28
29 int pcibr_slot_info_init(vertex_hdl_t pcibr_vhdl, pciio_slot_t slot);
30 int pcibr_slot_info_free(vertex_hdl_t pcibr_vhdl, pciio_slot_t slot);
31 int pcibr_slot_addr_space_init(vertex_hdl_t pcibr_vhdl,  pciio_slot_t slot);
32 int pcibr_slot_pcix_rbar_init(pcibr_soft_t pcibr_soft,  pciio_slot_t slot);
33 int pcibr_slot_device_init(vertex_hdl_t pcibr_vhdl,  pciio_slot_t slot);
34 int pcibr_slot_guest_info_init(vertex_hdl_t pcibr_vhdl,  pciio_slot_t slot);
35 int pcibr_slot_call_device_attach(vertex_hdl_t pcibr_vhdl,
36                  pciio_slot_t slot, int drv_flags);
37 int pcibr_slot_call_device_detach(vertex_hdl_t pcibr_vhdl,
38                  pciio_slot_t slot, int drv_flags);
39 int pcibr_slot_detach(vertex_hdl_t pcibr_vhdl, pciio_slot_t slot,
40                  int drv_flags, char *l1_msg, int *sub_errorp);
41 static int pcibr_probe_slot(pcibr_soft_t, cfg_p, unsigned int *);
42 static int pcibr_probe_work(pcibr_soft_t pcibr_soft, void *addr, int len, void *valp);
43 void pcibr_device_info_free(vertex_hdl_t, pciio_slot_t);
44 iopaddr_t pcibr_bus_addr_alloc(pcibr_soft_t, pciio_win_info_t, 
45                                pciio_space_t, int, int, int);
46 void pcibr_bus_addr_free(pciio_win_info_t);
47 cfg_p pcibr_find_capability(cfg_p, unsigned);
48 extern uint64_t  do_pcibr_config_get(cfg_p, unsigned, unsigned);
49 void do_pcibr_config_set(cfg_p, unsigned, unsigned, uint64_t); 
50 int pcibr_slot_pwr(vertex_hdl_t pcibr_vhdl, pciio_slot_t slot, int up, char *err_msg);
51
52
53 /* 
54  * PCI-X Max Outstanding Split Transactions translation array and Max Memory
55  * Read Byte Count translation array, as defined in the PCI-X Specification.
56  * Section 7.2.3 & 7.2.4 of PCI-X Specification - rev 1.0
57  */
58 #define MAX_SPLIT_TABLE 8
59 #define MAX_READCNT_TABLE 4
60 int max_splittrans_to_numbuf[MAX_SPLIT_TABLE] = {1, 2, 3, 4, 8, 12, 16, 32};
61 int max_readcount_to_bufsize[MAX_READCNT_TABLE] = {512, 1024, 2048, 4096 };
62
63 #ifdef CONFIG_HOTPLUG_PCI_SGI
64
65 /*
66  * PCI slot manipulation errors from the system controller, and their
67  * associated descriptions
68  */
69 #define SYSCTL_REQERR_BASE      (-106000)
70 #define SYSCTL_PCI_ERROR_BASE   (SYSCTL_REQERR_BASE - 100)
71 #define SYSCTL_PCIX_ERROR_BASE  (SYSCTL_REQERR_BASE - 3000)
72
73 struct sysctl_pci_error_s {
74
75     int         error;
76     char        *msg;
77
78 } sysctl_pci_errors[] = {
79
80 #define SYSCTL_PCI_UNINITIALIZED        (SYSCTL_PCI_ERROR_BASE - 0)
81     { SYSCTL_PCI_UNINITIALIZED, "module not initialized" },
82
83 #define SYSCTL_PCI_UNSUPPORTED_BUS      (SYSCTL_PCI_ERROR_BASE - 1)
84     { SYSCTL_PCI_UNSUPPORTED_BUS, "unsupported bus" },
85
86 #define SYSCTL_PCI_UNSUPPORTED_SLOT     (SYSCTL_PCI_ERROR_BASE - 2)
87     { SYSCTL_PCI_UNSUPPORTED_SLOT, "unsupported slot" },
88
89 #define SYSCTL_PCI_POWER_NOT_OKAY       (SYSCTL_PCI_ERROR_BASE - 3)
90     { SYSCTL_PCI_POWER_NOT_OKAY, "slot power not okay" },
91
92 #define SYSCTL_PCI_CARD_NOT_PRESENT     (SYSCTL_PCI_ERROR_BASE - 4)
93     { SYSCTL_PCI_CARD_NOT_PRESENT, "card not present" },
94
95 #define SYSCTL_PCI_POWER_LIMIT          (SYSCTL_PCI_ERROR_BASE - 5)
96     { SYSCTL_PCI_POWER_LIMIT, "power limit reached - some cards not powered up" },
97
98 #define SYSCTL_PCI_33MHZ_ON_66MHZ       (SYSCTL_PCI_ERROR_BASE - 6)
99     { SYSCTL_PCI_33MHZ_ON_66MHZ, "cannot add a 33 MHz card to an active 66 MHz bus" },
100
101 #define SYSCTL_PCI_INVALID_ORDER        (SYSCTL_PCI_ERROR_BASE - 7)
102     { SYSCTL_PCI_INVALID_ORDER, "invalid reset order" },
103
104 #define SYSCTL_PCI_DOWN_33MHZ           (SYSCTL_PCI_ERROR_BASE - 8)
105     { SYSCTL_PCI_DOWN_33MHZ, "cannot power down a 33 MHz card on an active bus" },
106
107 #define SYSCTL_PCI_RESET_33MHZ          (SYSCTL_PCI_ERROR_BASE - 9)
108     { SYSCTL_PCI_RESET_33MHZ, "cannot reset a 33 MHz card on an active bus" },
109
110 #define SYSCTL_PCI_SLOT_NOT_UP          (SYSCTL_PCI_ERROR_BASE - 10)
111     { SYSCTL_PCI_SLOT_NOT_UP, "cannot reset a slot that is not powered up" },
112
113 #define SYSCTL_PCIX_UNINITIALIZED       (SYSCTL_PCIX_ERROR_BASE - 0)
114     { SYSCTL_PCIX_UNINITIALIZED, "module not initialized" },
115
116 #define SYSCTL_PCIX_UNSUPPORTED_BUS     (SYSCTL_PCIX_ERROR_BASE - 1)
117     { SYSCTL_PCIX_UNSUPPORTED_BUS, "unsupported bus" },
118
119 #define SYSCTL_PCIX_UNSUPPORTED_SLOT    (SYSCTL_PCIX_ERROR_BASE - 2)
120     { SYSCTL_PCIX_UNSUPPORTED_SLOT, "unsupported slot" },
121
122 #define SYSCTL_PCIX_POWER_NOT_OKAY      (SYSCTL_PCIX_ERROR_BASE - 3)
123     { SYSCTL_PCIX_POWER_NOT_OKAY, "slot power not okay" },
124
125 #define SYSCTL_PCIX_CARD_NOT_PRESENT    (SYSCTL_PCIX_ERROR_BASE - 4)
126     { SYSCTL_PCIX_CARD_NOT_PRESENT, "card not present" },
127
128 #define SYSCTL_PCIX_POWER_LIMIT         (SYSCTL_PCIX_ERROR_BASE - 5)
129     { SYSCTL_PCIX_POWER_LIMIT, "power limit reached - some cards not powered up" },
130
131 #define SYSCTL_PCIX_33MHZ_ON_66MHZ      (SYSCTL_PCIX_ERROR_BASE - 6)
132     { SYSCTL_PCIX_33MHZ_ON_66MHZ, "cannot add a 33 MHz card to an active 66 MHz bus" },
133
134 #define SYSCTL_PCIX_PCI_ON_PCIX         (SYSCTL_PCIX_ERROR_BASE - 7)
135     { SYSCTL_PCIX_PCI_ON_PCIX, "cannot add a PCI card to an active PCIX bus" },
136
137 #define SYSCTL_PCIX_ANYTHING_ON_133MHZ          (SYSCTL_PCIX_ERROR_BASE - 8)
138     { SYSCTL_PCIX_ANYTHING_ON_133MHZ, "cannot add any card to an active 133MHz PCIX bus" },
139
140 #define SYSCTL_PCIX_X66MHZ_ON_X100MHZ           (SYSCTL_PCIX_ERROR_BASE - 9)
141     { SYSCTL_PCIX_X66MHZ_ON_X100MHZ, "cannot add a PCIX 66MHz card to an active 100MHz PCIX bus" },
142
143 #define SYSCTL_PCIX_INVALID_ORDER       (SYSCTL_PCIX_ERROR_BASE - 10)
144     { SYSCTL_PCIX_INVALID_ORDER, "invalid reset order" },
145
146 #define SYSCTL_PCIX_DOWN_33MHZ          (SYSCTL_PCIX_ERROR_BASE - 11)
147     { SYSCTL_PCIX_DOWN_33MHZ, "cannot power down a 33 MHz card on an active bus" },
148
149 #define SYSCTL_PCIX_RESET_33MHZ         (SYSCTL_PCIX_ERROR_BASE - 12)
150     { SYSCTL_PCIX_RESET_33MHZ, "cannot reset a 33 MHz card on an active bus" },
151
152 #define SYSCTL_PCIX_SLOT_NOT_UP         (SYSCTL_PCIX_ERROR_BASE - 13)
153     { SYSCTL_PCIX_SLOT_NOT_UP, "cannot reset a slot that is not powered up" },
154
155 #define SYSCTL_PCIX_INVALID_BUS_SETTING (SYSCTL_PCIX_ERROR_BASE - 14)
156     { SYSCTL_PCIX_INVALID_BUS_SETTING, "invalid bus type/speed selection (PCIX<66MHz, PCI>66MHz)" },
157
158 #define SYSCTL_PCIX_INVALID_DEPENDENT_SLOT (SYSCTL_PCIX_ERROR_BASE - 15)
159     { SYSCTL_PCIX_INVALID_DEPENDENT_SLOT, "invalid dependent slot in PCI slot configuration" },
160
161 #define SYSCTL_PCIX_SHARED_IDSELECT     (SYSCTL_PCIX_ERROR_BASE - 16)
162     { SYSCTL_PCIX_SHARED_IDSELECT, "cannot enable two slots sharing the same IDSELECT" },
163
164 #define SYSCTL_PCIX_SLOT_DISABLED       (SYSCTL_PCIX_ERROR_BASE - 17)
165     { SYSCTL_PCIX_SLOT_DISABLED, "slot is disabled" },
166
167 }; /* end sysctl_pci_errors[] */
168
169 /*
170  * look up an error message for PCI operations that fail
171  */
172 static void
173 sysctl_pci_error_lookup(int error, char *err_msg)
174 {
175     int i;
176     struct sysctl_pci_error_s *e = sysctl_pci_errors;
177     
178     for (i = 0; 
179          i < (sizeof(sysctl_pci_errors) / sizeof(*e));
180          i++, e++ )
181     {
182         if (e->error == error)
183         {
184             strcpy(err_msg, e->msg);
185             return;
186         }
187     }
188
189     sprintf(err_msg, "unrecognized PCI error type");
190 }
191
192 /*
193  * pcibr_slot_attach
194  *      This is a place holder routine to keep track of all the
195  *      slot-specific initialization that needs to be done.
196  *      This is usually called when we want to initialize a new
197  *      PCI card on the bus.
198  */
199 int
200 pcibr_slot_attach(vertex_hdl_t pcibr_vhdl,
201                   pciio_slot_t slot,
202                   int          drv_flags,
203                   char        *l1_msg,
204                   int         *sub_errorp)
205 {
206     pcibr_soft_t  pcibr_soft = pcibr_soft_get(pcibr_vhdl);
207     int           error;
208
209     if (!(pcibr_soft->bs_slot[slot].slot_status & PCI_SLOT_POWER_ON)) {
210         uint64_t speed;
211         uint64_t mode;
212
213         /* Power-up the slot */
214         error = pcibr_slot_pwr(pcibr_vhdl, slot, PCI_REQ_SLOT_POWER_ON, l1_msg);
215
216         if (error) {
217             if (sub_errorp)
218                 *sub_errorp = error;
219             return(PCI_L1_ERR);
220         } else {
221             pcibr_soft->bs_slot[slot].slot_status &= ~PCI_SLOT_POWER_MASK;
222             pcibr_soft->bs_slot[slot].slot_status |= PCI_SLOT_POWER_ON;
223         }
224
225         /* The speed/mode of the bus may have changed due to the hotplug */
226         speed = pcireg_speed_get(pcibr_soft);
227         mode = pcireg_mode_get(pcibr_soft);
228         pcibr_soft->bs_bridge_mode = ((speed << 1) | mode);
229
230         /*
231          * Allow cards like the Alteon Gigabit Ethernet Adapter to complete
232          * on-card initialization following the slot reset
233          */
234         set_current_state (TASK_INTERRUPTIBLE);
235         schedule_timeout (HZ);
236
237         /* Find out what is out there */
238         error = pcibr_slot_info_init(pcibr_vhdl, slot);
239
240         if (error) {
241             if (sub_errorp)
242                 *sub_errorp = error;
243             return(PCI_SLOT_INFO_INIT_ERR);
244         }
245
246         /* Set up the address space for this slot in the PCI land */
247
248         error = pcibr_slot_addr_space_init(pcibr_vhdl, slot);
249
250         if (error) {
251             if (sub_errorp)
252                 *sub_errorp = error;
253             return(PCI_SLOT_ADDR_INIT_ERR);
254         }
255
256         /* Allocate the PCI-X Read Buffer Attribute Registers (RBARs)*/
257         if (IS_PCIX(pcibr_soft)) {
258             int tmp_slot;
259
260             /* Recalculate the RBARs for all the devices on the bus.  Only
261              * return an error if we error for the given 'slot'
262              */
263             pcibr_soft->bs_pcix_rbar_inuse = 0;
264             pcibr_soft->bs_pcix_rbar_avail = NUM_RBAR;
265             pcibr_soft->bs_pcix_rbar_percent_allowed = 
266                                         pcibr_pcix_rbars_calc(pcibr_soft);
267             for (tmp_slot = pcibr_soft->bs_min_slot;
268                         tmp_slot < PCIBR_NUM_SLOTS(pcibr_soft); ++tmp_slot) {
269                 if (tmp_slot == slot)
270                     continue;   /* skip this 'slot', we do it below */
271                 (void)pcibr_slot_pcix_rbar_init(pcibr_soft, tmp_slot);
272             }
273
274             error = pcibr_slot_pcix_rbar_init(pcibr_soft, slot);
275             if (error) {
276                 if (sub_errorp)
277                     *sub_errorp = error;
278                 return(PCI_SLOT_RBAR_ALLOC_ERR);
279             }
280         }
281
282         /* Setup the device register */
283         error = pcibr_slot_device_init(pcibr_vhdl, slot);
284
285         if (error) {
286             if (sub_errorp)
287                 *sub_errorp = error;
288             return(PCI_SLOT_DEV_INIT_ERR);
289         }
290
291         /* Setup host/guest relations */
292         error = pcibr_slot_guest_info_init(pcibr_vhdl, slot);
293
294         if (error) {
295             if (sub_errorp)
296                 *sub_errorp = error;
297             return(PCI_SLOT_GUEST_INIT_ERR);
298         }
299
300         /* Initial RRB management */
301         error = pcibr_slot_initial_rrb_alloc(pcibr_vhdl, slot);
302
303         if (error) {
304             if (sub_errorp)
305                 *sub_errorp = error;
306             return(PCI_SLOT_RRB_ALLOC_ERR);
307         }
308
309     }
310
311     /* Call the device attach */
312     error = pcibr_slot_call_device_attach(pcibr_vhdl, slot, drv_flags);
313
314     if (error) {
315         if (sub_errorp)
316             *sub_errorp = error;
317         if (error == EUNATCH)
318             return(PCI_NO_DRIVER);
319         else
320             return(PCI_SLOT_DRV_ATTACH_ERR);
321     }
322
323     return(0);
324 }
325
326 /*
327  * pcibr_slot_enable
328  *      Enable the PCI slot for a hot-plug insert.
329  */
330 int
331 pcibr_slot_enable(vertex_hdl_t pcibr_vhdl, struct pcibr_slot_enable_req_s *req_p)
332 {
333     pcibr_soft_t                   pcibr_soft = pcibr_soft_get(pcibr_vhdl);
334     pciio_slot_t                   slot = req_p->req_device;
335     int                            error = 0;
336
337     /* Make sure that we are dealing with a bridge device vertex */
338     if (!pcibr_soft) {
339         return(PCI_NOT_A_BRIDGE);
340     }
341
342     PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_HOTPLUG, pcibr_vhdl,
343                 "pcibr_slot_enable: pcibr_soft=0x%lx, slot=%d, req_p=0x%lx\n",
344                 pcibr_soft, slot, req_p));
345
346     /* Check for the valid slot */
347     if (!PCIBR_VALID_SLOT(pcibr_soft, slot))
348         return(PCI_NOT_A_SLOT);
349
350     if (pcibr_soft->bs_slot[slot].slot_status & PCI_SLOT_ENABLE_CMPLT) {
351         error = PCI_SLOT_ALREADY_UP;
352         goto enable_unlock;
353     }
354
355     error = pcibr_slot_attach(pcibr_vhdl, slot, 0,
356                               req_p->req_resp.resp_l1_msg,
357                               &req_p->req_resp.resp_sub_errno);
358
359     req_p->req_resp.resp_l1_msg[PCI_L1_QSIZE] = '\0';
360
361     enable_unlock:
362
363     return(error);
364 }
365
366 /*
367  * pcibr_slot_disable
368  *      Disable the PCI slot for a hot-plug removal.
369  */
370 int
371 pcibr_slot_disable(vertex_hdl_t pcibr_vhdl, struct pcibr_slot_disable_req_s *req_p)
372 {
373     pcibr_soft_t                   pcibr_soft = pcibr_soft_get(pcibr_vhdl);
374     pciio_slot_t                   slot = req_p->req_device;
375     int                            error = 0;
376     pciio_slot_t                   tmp_slot;
377
378     /* Make sure that we are dealing with a bridge device vertex */
379     if (!pcibr_soft) {
380         return(PCI_NOT_A_BRIDGE);
381     }
382
383     PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_HOTPLUG, pcibr_vhdl,
384                 "pcibr_slot_disable: pcibr_soft=0x%lx, slot=%d, req_p=0x%lx\n",
385                 pcibr_soft, slot, req_p));
386
387     /* Check for valid slot */
388     if (!PCIBR_VALID_SLOT(pcibr_soft, slot))
389         return(PCI_NOT_A_SLOT);
390
391     if ((pcibr_soft->bs_slot[slot].slot_status & PCI_SLOT_DISABLE_CMPLT) ||
392        ((pcibr_soft->bs_slot[slot].slot_status & PCI_SLOT_STATUS_MASK) == 0)) {
393         error = PCI_SLOT_ALREADY_DOWN;
394         /*
395          * RJR - Should we invoke an L1 slot power-down command just in case
396          *       a previous shut-down failed to power-down the slot?
397          */
398         goto disable_unlock;
399     }
400
401     /* Do not allow the last 33 MHz card to be removed */
402     if (IS_33MHZ(pcibr_soft)) {
403         for (tmp_slot = pcibr_soft->bs_first_slot;
404              tmp_slot <= pcibr_soft->bs_last_slot; tmp_slot++)
405             if (tmp_slot != slot)
406                 if (pcibr_soft->bs_slot[tmp_slot].slot_status & PCI_SLOT_POWER_ON) {
407                     error++;
408                     break;
409                 }
410         if (!error) {
411             error = PCI_EMPTY_33MHZ;
412             goto disable_unlock;
413         }
414     }
415
416     if (req_p->req_action == PCI_REQ_SLOT_ELIGIBLE)
417         return(0);
418
419     error = pcibr_slot_detach(pcibr_vhdl, slot, 1,
420                               req_p->req_resp.resp_l1_msg,
421                               &req_p->req_resp.resp_sub_errno);
422
423     req_p->req_resp.resp_l1_msg[PCI_L1_QSIZE] = '\0';
424
425     disable_unlock:
426
427     return(error);
428 }
429
430 /*
431  * pcibr_slot_pwr
432  *      Power-up or power-down a PCI slot.  This routines makes calls to
433  *      the L1 system controller driver which requires "external" slot#.
434  */
435 int
436 pcibr_slot_pwr(vertex_hdl_t pcibr_vhdl,
437                pciio_slot_t slot,
438                int          up,
439                char        *err_msg)
440 {
441     pcibr_soft_t        pcibr_soft = pcibr_soft_get(pcibr_vhdl);
442     nasid_t             nasid;
443     u64                 connection_type;
444     int                 rv;
445
446     nasid = NASID_GET(pcibr_soft->bs_base);
447     connection_type = SAL_SYSCTL_IO_XTALK;
448
449     rv = (int) ia64_sn_sysctl_iobrick_pci_op
450         (nasid,
451          connection_type,
452          (u64) pcibr_widget_to_bus(pcibr_vhdl),
453          PCIBR_DEVICE_TO_SLOT(pcibr_soft, slot),
454          (up ? SAL_SYSCTL_PCI_POWER_UP : SAL_SYSCTL_PCI_POWER_DOWN));
455
456     if (!rv) {
457         /* everything's okay; no error message */
458         *err_msg = '\0';
459     }
460     else {
461         /* there was a problem; look up an appropriate error message */
462         sysctl_pci_error_lookup(rv, err_msg);
463     }
464     return rv;
465 }
466
467 #endif /* CONFIG_HOTPLUG_PCI_SGI */
468
469 /*
470  * pcibr_slot_info_init
471  *      Probe for this slot and see if it is populated.
472  *      If it is populated initialize the generic PCI infrastructural
473  *      information associated with this particular PCI device.
474  */
475 int
476 pcibr_slot_info_init(vertex_hdl_t       pcibr_vhdl,
477                      pciio_slot_t       slot)
478 {
479     pcibr_soft_t            pcibr_soft;
480     pcibr_info_h            pcibr_infoh;
481     pcibr_info_t            pcibr_info;
482     cfg_p                   cfgw;
483     unsigned                idword;
484     unsigned                pfail;
485     unsigned                idwords[8];
486     pciio_vendor_id_t       vendor;
487     pciio_device_id_t       device;
488     unsigned                htype;
489     unsigned                lt_time;
490     int                     nbars;
491     cfg_p                   wptr;
492     cfg_p                   pcix_cap;
493     int                     win;
494     pciio_space_t           space;
495     int                     nfunc;
496     pciio_function_t        rfunc;
497     int                     func;
498     vertex_hdl_t            conn_vhdl;
499     pcibr_soft_slot_t       slotp;
500     uint64_t                device_reg;
501
502     /* Get the basic software information required to proceed */
503     pcibr_soft = pcibr_soft_get(pcibr_vhdl);
504     if (!pcibr_soft)
505         return -EINVAL;
506
507     if (!PCIBR_VALID_SLOT(pcibr_soft, slot))
508         return -EINVAL;
509
510     /* If we have a host slot (eg:- IOC3 has 2 PCI slots and the initialization
511      * is done by the host slot then we are done.
512      */
513     if (pcibr_soft->bs_slot[slot].has_host) {
514         return 0;
515     }
516
517     /* Try to read the device-id/vendor-id from the config space */
518     cfgw = pcibr_slot_config_addr(pcibr_soft, slot, 0);
519
520     if (pcibr_probe_slot(pcibr_soft, cfgw, &idword)) 
521         return -ENODEV;
522
523     slotp = &pcibr_soft->bs_slot[slot];
524 #ifdef CONFIG_HOTPLUG_PCI_SGI
525     slotp->slot_status |= SLOT_POWER_UP;
526 #endif
527
528     vendor = 0xFFFF & idword;
529     device = 0xFFFF & (idword >> 16);
530
531     PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_PROBE, pcibr_vhdl,
532                 "pcibr_slot_info_init: slot=%d, vendor=0x%x, device=0x%x\n",
533                 PCIBR_DEVICE_TO_SLOT(pcibr_soft, slot), vendor, device));
534
535     /* If the vendor id is not valid then the slot is not populated
536      * and we are done.
537      */
538     if (vendor == 0xFFFF) 
539         return -ENODEV;
540     
541     htype = do_pcibr_config_get(cfgw, PCI_CFG_HEADER_TYPE, 1);
542     nfunc = 1;
543     rfunc = PCIIO_FUNC_NONE;
544     pfail = 0;
545
546     /* NOTE: if a card claims to be multifunction
547      * but only responds to config space 0, treat
548      * it as a unifunction card.
549      */
550
551     if (htype & 0x80) {         /* MULTIFUNCTION */
552         for (func = 1; func < 8; ++func) {
553             cfgw = pcibr_func_config_addr(pcibr_soft, 0, slot, func, 0);
554             if (pcibr_probe_slot(pcibr_soft, cfgw, &idwords[func])) {
555                 pfail |= 1 << func;
556                 continue;
557             }
558             vendor = 0xFFFF & idwords[func];
559             if (vendor == 0xFFFF) {
560                 pfail |= 1 << func;
561                 continue;
562             }
563             nfunc = func + 1;
564             rfunc = 0;
565         }
566         cfgw = pcibr_slot_config_addr(pcibr_soft, slot, 0);
567     }
568     pcibr_infoh = kmalloc(nfunc*sizeof (*(pcibr_infoh)), GFP_KERNEL);
569     if ( !pcibr_infoh ) {
570         return -ENOMEM;
571     }
572     memset(pcibr_infoh, 0, nfunc*sizeof (*(pcibr_infoh)));
573     
574     pcibr_soft->bs_slot[slot].bss_ninfo = nfunc;
575     pcibr_soft->bs_slot[slot].bss_infos = pcibr_infoh;
576
577     for (func = 0; func < nfunc; ++func) {
578         unsigned                cmd_reg;
579         
580         if (func) {
581             if (pfail & (1 << func))
582                 continue;
583             
584             idword = idwords[func];
585             cfgw = pcibr_func_config_addr(pcibr_soft, 0, slot, func, 0);
586             
587             device = 0xFFFF & (idword >> 16);
588             htype = do_pcibr_config_get(cfgw, PCI_CFG_HEADER_TYPE, 1);
589             rfunc = func;
590         }
591         htype &= 0x7f;
592         if (htype != 0x00) {
593             printk(KERN_WARNING 
594                 "%s pcibr: pci slot %d func %d has strange header type 0x%x\n",
595                     pcibr_soft->bs_name, slot, func, htype);
596             nbars = 2;
597         } else {
598             nbars = PCI_CFG_BASE_ADDRS;
599         }
600
601         PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_CONFIG, pcibr_vhdl,
602                 "pcibr_slot_info_init: slot=%d, func=%d, cfgw=0x%lx\n",
603                 PCIBR_DEVICE_TO_SLOT(pcibr_soft,slot), func, cfgw));
604
605         /* 
606          * If the latency timer has already been set, by prom or by the
607          * card itself, use that value.  Otherwise look at the device's
608          * 'min_gnt' and attempt to calculate a latency time. 
609          *
610          * NOTE: For now if the device is on the 'real time' arbitration
611          * ring we don't set the latency timer.  
612          *
613          * WAR: SGI's IOC3 and RAD devices target abort if you write a 
614          * single byte into their config space.  So don't set the Latency
615          * Timer for these devices
616          */
617
618         lt_time = do_pcibr_config_get(cfgw, PCI_CFG_LATENCY_TIMER, 1);
619         device_reg = pcireg_device_get(pcibr_soft, slot);
620         if ((lt_time == 0) && !(device_reg & BRIDGE_DEV_RT)) {
621              unsigned   min_gnt;
622              unsigned   min_gnt_mult;
623             
624             /* 'min_gnt' indicates how long of a burst period a device
625              * needs in increments of 250ns.  But latency timer is in
626              * PCI clock cycles, so a conversion is needed.
627              */
628             min_gnt = do_pcibr_config_get(cfgw, PCI_MIN_GNT, 1);
629
630             if (IS_133MHZ(pcibr_soft))
631                 min_gnt_mult = 32;      /* 250ns @ 133MHz in clocks */
632             else if (IS_100MHZ(pcibr_soft))
633                 min_gnt_mult = 24;      /* 250ns @ 100MHz in clocks */
634             else if (IS_66MHZ(pcibr_soft))
635                 min_gnt_mult = 16;      /* 250ns @ 66MHz, in clocks */
636             else
637                 min_gnt_mult = 8;       /* 250ns @ 33MHz, in clocks */
638
639             if ((min_gnt != 0) && ((min_gnt * min_gnt_mult) < 256))
640                 lt_time = (min_gnt * min_gnt_mult);
641             else
642                 lt_time = 4 * min_gnt_mult;       /* 1 micro second */
643
644             do_pcibr_config_set(cfgw, PCI_CFG_LATENCY_TIMER, 1, lt_time);
645
646             PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_CONFIG, pcibr_vhdl,
647                     "pcibr_slot_info_init: set Latency Timer for slot=%d, "
648                     "func=%d, to 0x%x\n", 
649                     PCIBR_DEVICE_TO_SLOT(pcibr_soft, slot), func, lt_time));
650         }
651
652
653         /* In our architecture the setting of the cacheline size isn't 
654          * beneficial for cards in PCI mode, but in PCI-X mode devices
655          * can optionally use the cacheline size value for internal 
656          * device optimizations    (See 7.1.5 of the PCI-X v1.0 spec).
657          * NOTE: cachline size is in doubleword increments
658          */
659         if (IS_PCIX(pcibr_soft)) {
660             if (!do_pcibr_config_get(cfgw, PCI_CFG_CACHE_LINE, 1)) {
661                 do_pcibr_config_set(cfgw, PCI_CFG_CACHE_LINE, 1, 0x20);
662                 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_CONFIG, pcibr_vhdl,
663                         "pcibr_slot_info_init: set CacheLine for slot=%d, "
664                         "func=%d, to 0x20\n",
665                         PCIBR_DEVICE_TO_SLOT(pcibr_soft, slot), func));
666             }
667         }
668
669         /* Get the PCI-X capability if running in PCI-X mode.  If the func
670          * doesnt have a pcix capability, allocate a PCIIO_VENDOR_ID_NONE
671          * pcibr_info struct so the device driver for that function is not
672          * called.
673          */
674         if (IS_PCIX(pcibr_soft)) {
675             if (!(pcix_cap = pcibr_find_capability(cfgw, PCI_CAP_PCIX))) {
676                 printk(KERN_WARNING
677                         "%s: Bus running in PCI-X mode, But card in slot %d, "
678                         "func %d not PCI-X capable\n", 
679                         pcibr_soft->bs_name, slot, func);
680                 pcibr_device_info_new(pcibr_soft, slot, PCIIO_FUNC_NONE,
681                                PCIIO_VENDOR_ID_NONE, PCIIO_DEVICE_ID_NONE);
682                 continue;
683             }
684             PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_CONFIG, pcibr_vhdl,
685                     "pcibr_slot_info_init: PCI-X capability at 0x%lx for "
686                     "slot=%d, func=%d\n", 
687                     pcix_cap, PCIBR_DEVICE_TO_SLOT(pcibr_soft, slot), func));
688         } else {
689             pcix_cap = NULL;
690         }
691
692         pcibr_info = pcibr_device_info_new
693             (pcibr_soft, slot, rfunc, vendor, device);
694
695         /* Keep a running total of the number of PIC-X functions on the bus
696          * and the number of max outstanding split trasnactions that they
697          * have requested.  NOTE: "pcix_cap != NULL" implies IS_PCIX()
698          */
699         pcibr_info->f_pcix_cap = (cap_pcix_type0_t *)pcix_cap;
700         if (pcibr_info->f_pcix_cap) {
701             int max_out;      /* max outstanding splittrans from status reg */
702
703             pcibr_soft->bs_pcix_num_funcs++;
704             max_out = pcibr_info->f_pcix_cap->pcix_type0_status.max_out_split;
705             pcibr_soft->bs_pcix_split_tot += max_splittrans_to_numbuf[max_out];
706         }
707
708         conn_vhdl = pciio_device_info_register(pcibr_vhdl, &pcibr_info->f_c);
709         if (func == 0)
710             slotp->slot_conn = conn_vhdl;
711
712         cmd_reg = do_pcibr_config_get(cfgw, PCI_CFG_COMMAND, 4);
713         
714         wptr = cfgw + PCI_CFG_BASE_ADDR_0 / 4;
715
716         for (win = 0; win < nbars; ++win) {
717             iopaddr_t               base, mask, code;
718             size_t                  size;
719
720             /*
721              * GET THE BASE & SIZE OF THIS WINDOW:
722              *
723              * The low two or four bits of the BASE register
724              * determines which address space we are in; the
725              * rest is a base address. BASE registers
726              * determine windows that are power-of-two sized
727              * and naturally aligned, so we can get the size
728              * of a window by writing all-ones to the
729              * register, reading it back, and seeing which
730              * bits are used for decode; the least
731              * significant nonzero bit is also the size of
732              * the window.
733              *
734              * WARNING: someone may already have allocated
735              * some PCI space to this window, and in fact
736              * PIO may be in process at this very moment
737              * from another processor (or even from this
738              * one, if we get interrupted)! So, if the BASE
739              * already has a nonzero address, be generous
740              * and use the LSBit of that address as the
741              * size; this could overstate the window size.
742              * Usually, when one card is set up, all are set
743              * up; so, since we don't bitch about
744              * overlapping windows, we are ok.
745              *
746              * UNFORTUNATELY, some cards do not clear their
747              * BASE registers on reset. I have two heuristics
748              * that can detect such cards: first, if the
749              * decode enable is turned off for the space
750              * that the window uses, we can disregard the
751              * initial value. second, if the address is
752              * outside the range that we use, we can disregard
753              * it as well.
754              *
755              * This is looking very PCI generic. Except for
756              * knowing how many slots and where their config
757              * spaces are, this window loop and the next one
758              * could probably be shared with other PCI host
759              * adapters. It would be interesting to see if
760              * this could be pushed up into pciio, when we
761              * start supporting more PCI providers.
762              */
763             base = do_pcibr_config_get(wptr, (win * 4), 4);
764
765             if (base & PCI_BA_IO_SPACE) {
766                 /* BASE is in I/O space. */
767                 space = PCIIO_SPACE_IO;
768                 mask = -4;
769                 code = base & 3;
770                 base = base & mask;
771                 if (base == 0) {
772                     ;           /* not assigned */
773                 } else if (!(cmd_reg & PCI_CMD_IO_SPACE)) {
774                     base = 0;   /* decode not enabled */
775                 }
776             } else {
777                 /* BASE is in MEM space. */
778                 space = PCIIO_SPACE_MEM;
779                 mask = -16;
780                 code = base & PCI_BA_MEM_LOCATION;      /* extract BAR type */
781                 base = base & mask;
782                 if (base == 0) {
783                     ;           /* not assigned */
784                 } else if (!(cmd_reg & PCI_CMD_MEM_SPACE)) {
785                     base = 0;   /* decode not enabled */
786                 } else if (base & 0xC0000000) {
787                     base = 0;   /* outside permissable range */
788                 } else if ((code == PCI_BA_MEM_64BIT) &&
789                            (do_pcibr_config_get(wptr, ((win + 1)*4), 4) != 0)) {
790                     base = 0;   /* outside permissable range */
791                 }
792             }
793
794             if (base != 0) {    /* estimate size */
795                 pciio_space_t   tmp_space = space;
796                 iopaddr_t       tmp_base;
797
798                 size = base & -base;
799
800                 /*
801                  * Reserve this space in the relavent address map.  Don't
802                  * care about the return code from pcibr_bus_addr_alloc().
803                  */
804
805                 if (space == PCIIO_SPACE_MEM && code != PCI_BA_MEM_1MEG) {
806                         tmp_space = PCIIO_SPACE_MEM32;
807                 }
808
809                 tmp_base = pcibr_bus_addr_alloc(pcibr_soft,
810                                                 &pcibr_info->f_window[win],
811                                                 tmp_space,
812                                                 base, size, 0);
813
814                 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_BAR, pcibr_vhdl,
815                         "pcibr_slot_info_init: slot=%d, func=%d win %d "
816                         "reserving space %s [0x%lx..0x%lx], tmp_base 0x%lx\n",
817                         PCIBR_DEVICE_TO_SLOT(pcibr_soft, slot), func, win,
818                         pci_space[tmp_space], (uint64_t)base,
819                         (uint64_t)(base + size - 1), (uint64_t)tmp_base));
820             } else {            /* calculate size */
821                 do_pcibr_config_set(wptr, (win * 4), 4, ~0);    /* write 1's */
822                 size = do_pcibr_config_get(wptr, (win * 4), 4); /* read back */
823                 size &= mask;   /* keep addr */
824                 size &= -size;  /* keep lsbit */
825                 if (size == 0)
826                     continue;
827             }   
828
829             pcibr_info->f_window[win].w_space = space;
830             pcibr_info->f_window[win].w_base = base;
831             pcibr_info->f_window[win].w_size = size;
832
833             if (code == PCI_BA_MEM_64BIT) {
834                 win++;          /* skip upper half */
835                 do_pcibr_config_set(wptr, (win * 4), 4, 0);  /* must be zero */
836             }
837         }                               /* next win */
838     }                           /* next func */
839
840     return 0;
841 }                                       
842
843 /*
844  * pcibr_find_capability
845  *      Walk the list of capabilities (if it exists) looking for
846  *      the requested capability.  Return a cfg_p pointer to the
847  *      capability if found, else return NULL
848  */
849 cfg_p
850 pcibr_find_capability(cfg_p     cfgw,
851                       unsigned  capability)
852 {
853     unsigned            cap_nxt;
854     unsigned            cap_id;
855     int                 defend_against_circular_linkedlist = 0;
856
857     /* Check to see if there is a capabilities pointer in the cfg header */
858     if (!(do_pcibr_config_get(cfgw, PCI_CFG_STATUS, 2) & PCI_STAT_CAP_LIST)) {
859         return NULL;
860     }
861
862     /*
863      * Read up the capabilities head pointer from the configuration header.
864      * Capabilities are stored as a linked list in the lower 48 dwords of
865      * config space and are dword aligned. (Note: spec states the least two
866      * significant bits of the next pointer must be ignored,  so we mask
867      * with 0xfc).
868      */
869     cap_nxt = (do_pcibr_config_get(cfgw, PCI_CAPABILITIES_PTR, 1) & 0xfc);
870
871     while (cap_nxt && (defend_against_circular_linkedlist <= 48)) {
872         cap_id = do_pcibr_config_get(cfgw, cap_nxt, 1);
873         if (cap_id == capability) {
874             return (cfg_p)((char *)cfgw + cap_nxt);
875         }
876         cap_nxt = (do_pcibr_config_get(cfgw, cap_nxt+1, 1) & 0xfc);
877         defend_against_circular_linkedlist++;
878     }
879
880     return NULL;
881 }
882
883 /*
884  * pcibr_slot_info_free
885  *      Remove all the PCI infrastructural information associated
886  *      with a particular PCI device.
887  */
888 int
889 pcibr_slot_info_free(vertex_hdl_t pcibr_vhdl,
890                      pciio_slot_t slot)
891 {
892     pcibr_soft_t        pcibr_soft;
893     pcibr_info_h        pcibr_infoh;
894     int                 nfunc;
895
896     pcibr_soft = pcibr_soft_get(pcibr_vhdl);
897
898     if (!pcibr_soft)
899         return -EINVAL;
900
901     if (!PCIBR_VALID_SLOT(pcibr_soft, slot))
902         return -EINVAL;
903
904     nfunc = pcibr_soft->bs_slot[slot].bss_ninfo;
905
906     pcibr_device_info_free(pcibr_vhdl, slot);
907
908     pcibr_infoh = pcibr_soft->bs_slot[slot].bss_infos;
909     kfree(pcibr_infoh);
910     pcibr_soft->bs_slot[slot].bss_ninfo = 0;
911
912     return 0;
913 }
914
915 /*
916  * pcibr_slot_pcix_rbar_init
917  *      Allocate RBARs to the PCI-X functions on a given device
918  */
919 int
920 pcibr_slot_pcix_rbar_init(pcibr_soft_t pcibr_soft,
921                             pciio_slot_t slot)
922 {
923     pcibr_info_h         pcibr_infoh;
924     pcibr_info_t         pcibr_info;
925     int                  nfunc;
926     int                  func;
927
928     if (!PCIBR_VALID_SLOT(pcibr_soft, slot))
929         return -EINVAL;
930
931     if ((nfunc = pcibr_soft->bs_slot[slot].bss_ninfo) < 1)
932         return -EINVAL;
933
934     if (!(pcibr_infoh = pcibr_soft->bs_slot[slot].bss_infos))
935         return -EINVAL;
936
937     PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_RBAR, pcibr_soft->bs_vhdl,
938                 "pcibr_slot_pcix_rbar_init for slot %d\n", 
939                 PCIBR_DEVICE_TO_SLOT(pcibr_soft, slot)));
940     PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_RBAR, pcibr_soft->bs_vhdl,
941                 "\tslot/func\trequested\tgiven\tinuse\tavail\n"));
942
943     for (func = 0; func < nfunc; ++func) {
944         cap_pcix_type0_t        *pcix_cap_p;
945         cap_pcix_stat_reg_t     *pcix_statreg_p;
946         cap_pcix_cmd_reg_t      *pcix_cmdreg_p;
947         int                      num_rbar;
948
949         if (!(pcibr_info = pcibr_infoh[func]))
950             continue;
951
952         if (pcibr_info->f_vendor == PCIIO_VENDOR_ID_NONE)
953             continue;
954
955         if (!(pcix_cap_p = pcibr_info->f_pcix_cap))
956             continue;
957
958         pcix_statreg_p = &pcix_cap_p->pcix_type0_status;
959         pcix_cmdreg_p = &pcix_cap_p->pcix_type0_command;
960
961         /* If there are enough RBARs to satify the number of "max outstanding 
962          * transactions" each function requested (bs_pcix_rbar_percent_allowed
963          * is 100%), then give each function what it requested, otherwise give 
964          * the functions a "percentage of what they requested".
965          */
966         if (pcibr_soft->bs_pcix_rbar_percent_allowed >= 100) {
967             pcix_cmdreg_p->max_split = pcix_statreg_p->max_out_split;
968             num_rbar = max_splittrans_to_numbuf[pcix_cmdreg_p->max_split];
969             pcibr_soft->bs_pcix_rbar_inuse += num_rbar;
970             pcibr_soft->bs_pcix_rbar_avail -= num_rbar;
971             pcix_cmdreg_p->max_mem_read_cnt = pcix_statreg_p->max_mem_read_cnt;
972         } else {
973             int index;      /* index into max_splittrans_to_numbuf table */
974             int max_out;    /* max outstanding transactions given to func */
975
976             /* Calculate the percentage of RBARs this function can have.
977              * NOTE: Every function gets at least 1 RBAR (thus the "+1").
978              * bs_pcix_rbar_percent_allowed is the percentage of what was
979              * requested less this 1 RBAR that all functions automatically 
980              * gets
981              */
982             max_out = ((max_splittrans_to_numbuf[pcix_statreg_p->max_out_split]
983                         * pcibr_soft->bs_pcix_rbar_percent_allowed) / 100) + 1;
984
985             /* round down the newly caclulated max_out to a valid number in
986              * max_splittrans_to_numbuf[]
987              */
988             for (index = 0; index < MAX_SPLIT_TABLE-1; index++)
989                 if (max_splittrans_to_numbuf[index + 1] > max_out)
990                     break;
991
992             pcix_cmdreg_p->max_split = index;
993             num_rbar = max_splittrans_to_numbuf[pcix_cmdreg_p->max_split];
994             pcibr_soft->bs_pcix_rbar_inuse += num_rbar;
995             pcibr_soft->bs_pcix_rbar_avail -= num_rbar;
996             pcix_cmdreg_p->max_mem_read_cnt = pcix_statreg_p->max_mem_read_cnt;
997         }
998
999         PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_RBAR, pcibr_soft->bs_vhdl,
1000                 "\t  %d/%d   \t    %d    \t  %d  \t  %d  \t  %d\n",
1001                 PCIBR_DEVICE_TO_SLOT(pcibr_soft, slot), func,
1002                 max_splittrans_to_numbuf[pcix_statreg_p->max_out_split],
1003                 max_splittrans_to_numbuf[pcix_cmdreg_p->max_split],
1004                 pcibr_soft->bs_pcix_rbar_inuse,
1005                 pcibr_soft->bs_pcix_rbar_avail));
1006     }
1007     return 0;
1008 }
1009
1010 int as_debug = 0;
1011 /*
1012  * pcibr_slot_addr_space_init
1013  *      Reserve chunks of PCI address space as required by 
1014  *      the base registers in the card.
1015  */
1016 int
1017 pcibr_slot_addr_space_init(vertex_hdl_t pcibr_vhdl,
1018                            pciio_slot_t slot)
1019 {
1020     pcibr_soft_t         pcibr_soft;
1021     pcibr_info_h         pcibr_infoh;
1022     pcibr_info_t         pcibr_info;
1023     iopaddr_t            mask;
1024     int                  nbars;
1025     int                  nfunc;
1026     int                  func;
1027     int                  win;
1028     int                  rc = 0;
1029     int                  align = 0;
1030     int                  align_slot;
1031
1032     pcibr_soft = pcibr_soft_get(pcibr_vhdl);
1033
1034     if (!pcibr_soft)
1035         return -EINVAL;
1036
1037     if (!PCIBR_VALID_SLOT(pcibr_soft, slot))
1038         return -EINVAL;
1039
1040     /* allocate address space,
1041      * for windows that have not been
1042      * previously assigned.
1043      */
1044     if (pcibr_soft->bs_slot[slot].has_host) {
1045         return 0;
1046     }
1047
1048     nfunc = pcibr_soft->bs_slot[slot].bss_ninfo;
1049     if (nfunc < 1)
1050         return -EINVAL;
1051
1052     pcibr_infoh = pcibr_soft->bs_slot[slot].bss_infos;
1053     if (!pcibr_infoh)
1054         return -EINVAL;
1055
1056     /*
1057      * Try to make the DevIO windows not
1058      * overlap by pushing the "io" and "hi"
1059      * allocation areas up to the next one
1060      * or two megabyte bound. This also
1061      * keeps them from being zero.
1062      *
1063      * DO NOT do this with "pci_lo" since
1064      * the entire "lo" area is only a
1065      * megabyte, total ...
1066      */
1067     align_slot = (slot < 2) ? 0x200000 : 0x100000;
1068
1069     for (func = 0; func < nfunc; ++func) {
1070         cfg_p                   cfgw;
1071         cfg_p                   wptr;
1072         pciio_space_t           space;
1073         iopaddr_t               base;
1074         size_t                  size;
1075         unsigned                pci_cfg_cmd_reg;
1076         unsigned                pci_cfg_cmd_reg_add = 0;
1077
1078         pcibr_info = pcibr_infoh[func];
1079
1080         if (!pcibr_info)
1081             continue;
1082
1083         if (pcibr_info->f_vendor == PCIIO_VENDOR_ID_NONE)
1084             continue;
1085         
1086         cfgw = pcibr_func_config_addr(pcibr_soft, 0, slot, func, 0);
1087         wptr = cfgw + PCI_CFG_BASE_ADDR_0 / 4;
1088
1089         if ((do_pcibr_config_get(cfgw, PCI_CFG_HEADER_TYPE, 1) & 0x7f) != 0)
1090             nbars = 2;
1091         else
1092             nbars = PCI_CFG_BASE_ADDRS;
1093
1094         for (win = 0; win < nbars; ++win) {
1095             space = pcibr_info->f_window[win].w_space;
1096             base = pcibr_info->f_window[win].w_base;
1097             size = pcibr_info->f_window[win].w_size;
1098             
1099             if (size < 1)
1100                 continue;
1101
1102             if (base >= size) {
1103                 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_BAR, pcibr_vhdl,
1104                         "pcibr_slot_addr_space_init: slot=%d, "
1105                         "func=%d win %d is in space %s [0x%lx..0x%lx], "
1106                         "allocated by prom\n",
1107                         PCIBR_DEVICE_TO_SLOT(pcibr_soft, slot), func, win,
1108                         pci_space[space], (uint64_t)base,
1109                         (uint64_t)(base + size - 1)));
1110
1111                 continue;               /* already allocated */
1112             }
1113
1114             align = (win) ? size : align_slot; 
1115
1116             if (align < PAGE_SIZE)
1117                 align = PAGE_SIZE;        /* ie. 0x00004000 */
1118  
1119             switch (space) {
1120             case PCIIO_SPACE_IO:
1121                 base = pcibr_bus_addr_alloc(pcibr_soft,
1122                                             &pcibr_info->f_window[win],
1123                                             PCIIO_SPACE_IO,
1124                                             0, size, align);
1125                 if (!base)
1126                     rc = ENOSPC;
1127                 break;
1128                 
1129             case PCIIO_SPACE_MEM:
1130                 if ((do_pcibr_config_get(wptr, (win * 4), 4) &
1131                      PCI_BA_MEM_LOCATION) == PCI_BA_MEM_1MEG) {
1132  
1133                     /* allocate from 20-bit PCI space */
1134                     base = pcibr_bus_addr_alloc(pcibr_soft,
1135                                                 &pcibr_info->f_window[win],
1136                                                 PCIIO_SPACE_MEM,
1137                                                 0, size, align);
1138                     if (!base)
1139                         rc = ENOSPC;
1140                 } else {
1141                     /* allocate from 32-bit or 64-bit PCI space */
1142                     base = pcibr_bus_addr_alloc(pcibr_soft,
1143                                                 &pcibr_info->f_window[win],
1144                                                 PCIIO_SPACE_MEM32,
1145                                                 0, size, align);
1146                     if (!base) 
1147                         rc = ENOSPC;
1148                 }
1149                 break;
1150                 
1151             default:
1152                 base = 0;
1153                 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_BAR, pcibr_vhdl,
1154                             "pcibr_slot_addr_space_init: slot=%d, window %d "
1155                             "had bad space code %d\n", 
1156                             PCIBR_DEVICE_TO_SLOT(pcibr_soft,slot), win, space));
1157             }
1158             pcibr_info->f_window[win].w_base = base;
1159             do_pcibr_config_set(wptr, (win * 4), 4, base);
1160
1161             if (base >= size) {
1162                 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_BAR, pcibr_vhdl,
1163                         "pcibr_slot_addr_space_init: slot=%d, func=%d. win %d "
1164                         "is in space %s [0x%lx..0x%lx], allocated by pcibr\n",
1165                         PCIBR_DEVICE_TO_SLOT(pcibr_soft, slot), func, win,
1166                         pci_space[space], (uint64_t)base, 
1167                         (uint64_t)(base + size - 1)));
1168             } else {
1169                 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_BAR, pcibr_vhdl,
1170                         "pcibr_slot_addr_space_init: slot=%d, func=%d, win %d, "
1171                         "unable to alloc 0x%lx in space %s\n",
1172                         PCIBR_DEVICE_TO_SLOT(pcibr_soft, slot), func, win,
1173                         (uint64_t)size, pci_space[space]));
1174             }
1175         }                               /* next base */
1176
1177         /*
1178          * Allocate space for the EXPANSION ROM
1179          */
1180         base = size = 0;
1181         {
1182             wptr = cfgw + PCI_EXPANSION_ROM / 4;
1183             do_pcibr_config_set(wptr, 0, 4, 0xFFFFF000);
1184             mask = do_pcibr_config_get(wptr, 0, 4);
1185             if (mask & 0xFFFFF000) {
1186                 size = mask & -mask;
1187                 base = pcibr_bus_addr_alloc(pcibr_soft,
1188                                             &pcibr_info->f_rwindow,
1189                                             PCIIO_SPACE_MEM32, 
1190                                             0, size, align);
1191                 if (!base)
1192                     rc = ENOSPC;
1193                 else {
1194                     do_pcibr_config_set(wptr, 0, 4, base);
1195                     PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_BAR, pcibr_vhdl,
1196                                 "pcibr_slot_addr_space_init: slot=%d, func=%d, "
1197                                 "ROM in [0x%X..0x%X], allocated by pcibr\n",
1198                                 PCIBR_DEVICE_TO_SLOT(pcibr_soft, slot), 
1199                                 func, base, base + size - 1));
1200                 }
1201             }
1202         }
1203         pcibr_info->f_rbase = base;
1204         pcibr_info->f_rsize = size;
1205
1206         /*
1207          * if necessary, update the board's
1208          * command register to enable decoding
1209          * in the windows we added.
1210          *
1211          * There are some bits we always want to
1212          * be sure are set.
1213          */
1214         pci_cfg_cmd_reg_add |= PCI_CMD_IO_SPACE;
1215
1216         /*
1217          * The Adaptec 1160 FC Controller WAR #767995:
1218          * The part incorrectly ignores the upper 32 bits of a 64 bit
1219          * address when decoding references to its registers so to
1220          * keep it from responding to a bus cycle that it shouldn't
1221          * we only use I/O space to get at it's registers.  Don't
1222          * enable memory space accesses on that PCI device.
1223          */
1224         #define FCADP_VENDID 0x9004 /* Adaptec Vendor ID from fcadp.h */
1225         #define FCADP_DEVID 0x1160  /* Adaptec 1160 Device ID from fcadp.h */
1226
1227         if ((pcibr_info->f_vendor != FCADP_VENDID) ||
1228             (pcibr_info->f_device != FCADP_DEVID))
1229             pci_cfg_cmd_reg_add |= PCI_CMD_MEM_SPACE;
1230
1231         pci_cfg_cmd_reg_add |= PCI_CMD_BUS_MASTER;
1232
1233         pci_cfg_cmd_reg = do_pcibr_config_get(cfgw, PCI_CFG_COMMAND, 4);
1234         pci_cfg_cmd_reg &= 0xFFFF;
1235         if (pci_cfg_cmd_reg_add & ~pci_cfg_cmd_reg)
1236             do_pcibr_config_set(cfgw, PCI_CFG_COMMAND, 4, 
1237                                 pci_cfg_cmd_reg | pci_cfg_cmd_reg_add);
1238     }                           /* next func */
1239     return rc;
1240 }
1241
1242 /*
1243  * pcibr_slot_device_init
1244  *      Setup the device register in the bridge for this PCI slot.
1245  */
1246
1247 int
1248 pcibr_slot_device_init(vertex_hdl_t pcibr_vhdl,
1249                        pciio_slot_t slot)
1250 {
1251     pcibr_soft_t         pcibr_soft;
1252     uint64_t             devreg;
1253
1254     pcibr_soft = pcibr_soft_get(pcibr_vhdl);
1255
1256     if (!pcibr_soft)
1257         return -EINVAL;
1258
1259     if (!PCIBR_VALID_SLOT(pcibr_soft, slot))
1260         return -EINVAL;
1261
1262     /*
1263      * Adjustments to Device(x) and init of bss_device shadow
1264      */
1265     devreg = pcireg_device_get(pcibr_soft, slot);
1266     devreg &= ~BRIDGE_DEV_PAGE_CHK_DIS;
1267
1268     /*
1269      * Enable virtual channels by default (exception: see PIC WAR below)
1270      */
1271     devreg |= BRIDGE_DEV_VIRTUAL_EN;
1272
1273     /*
1274      * PIC WAR. PV# 855271:  Disable virtual channels in the PIC since
1275      * it can cause problems with 32-bit devices.  We'll set the bit in
1276      * pcibr_try_set_device() iff we're 64-bit and requesting virtual 
1277      * channels.
1278      */
1279     if (PCIBR_WAR_ENABLED(PV855271, pcibr_soft)) {
1280         devreg &= ~BRIDGE_DEV_VIRTUAL_EN;
1281     }
1282     devreg |= BRIDGE_DEV_COH;
1283
1284     pcibr_soft->bs_slot[slot].bss_device = devreg;
1285     pcireg_device_set(pcibr_soft, slot, devreg);
1286
1287     PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_DEVREG, pcibr_vhdl,
1288                 "pcibr_slot_device_init: Device(%d): 0x%x\n",
1289                 slot, devreg));
1290     return 0;
1291 }
1292
1293 /*
1294  * pcibr_slot_guest_info_init
1295  *      Setup the host/guest relations for a PCI slot.
1296  */
1297 int
1298 pcibr_slot_guest_info_init(vertex_hdl_t pcibr_vhdl,
1299                            pciio_slot_t slot)
1300 {
1301     pcibr_soft_t        pcibr_soft;
1302     pcibr_info_h        pcibr_infoh;
1303     pcibr_info_t        pcibr_info;
1304     pcibr_soft_slot_t   slotp;
1305
1306     pcibr_soft = pcibr_soft_get(pcibr_vhdl);
1307
1308     if (!pcibr_soft)
1309         return -EINVAL;
1310
1311     if (!PCIBR_VALID_SLOT(pcibr_soft, slot))
1312         return -EINVAL;
1313
1314     slotp = &pcibr_soft->bs_slot[slot];
1315
1316     /* create info and verticies for guest slots;
1317      * for compatibilitiy macros, create info
1318      * for even unpopulated slots (but do not
1319      * build verticies for them).
1320      */
1321     if (pcibr_soft->bs_slot[slot].bss_ninfo < 1) {
1322         pcibr_infoh = kmalloc(sizeof (*(pcibr_infoh)), GFP_KERNEL);
1323         if ( !pcibr_infoh ) {
1324                 return -ENOMEM;
1325         }
1326         memset(pcibr_infoh, 0, sizeof (*(pcibr_infoh)));
1327
1328         pcibr_soft->bs_slot[slot].bss_ninfo = 1;
1329         pcibr_soft->bs_slot[slot].bss_infos = pcibr_infoh;
1330
1331         pcibr_info = pcibr_device_info_new
1332             (pcibr_soft, slot, PCIIO_FUNC_NONE,
1333              PCIIO_VENDOR_ID_NONE, PCIIO_DEVICE_ID_NONE);
1334
1335         if (pcibr_soft->bs_slot[slot].has_host) {
1336             slotp->slot_conn = pciio_device_info_register
1337                 (pcibr_vhdl, &pcibr_info->f_c);
1338         }
1339     }
1340
1341     /* generate host/guest relations
1342      */
1343     if (pcibr_soft->bs_slot[slot].has_host) {
1344         int  host = pcibr_soft->bs_slot[slot].host_slot;
1345         pcibr_soft_slot_t host_slotp = &pcibr_soft->bs_slot[host];
1346
1347         hwgraph_edge_add(slotp->slot_conn,
1348                          host_slotp->slot_conn,
1349                          EDGE_LBL_HOST);
1350
1351         /* XXX- only gives us one guest edge per
1352          * host. If/when we have a host with more than
1353          * one guest, we will need to figure out how
1354          * the host finds all its guests, and sorts
1355          * out which one is which.
1356          */
1357         hwgraph_edge_add(host_slotp->slot_conn,
1358                          slotp->slot_conn,
1359                          EDGE_LBL_GUEST);
1360     }
1361
1362     return 0;
1363 }
1364
1365
1366 /*
1367  * pcibr_slot_call_device_attach
1368  *      This calls the associated driver attach routine for the PCI
1369  *      card in this slot.
1370  */
1371 int
1372 pcibr_slot_call_device_attach(vertex_hdl_t pcibr_vhdl,
1373                               pciio_slot_t slot,
1374                               int          drv_flags)
1375 {
1376     pcibr_soft_t        pcibr_soft;
1377     pcibr_info_h        pcibr_infoh;
1378     pcibr_info_t        pcibr_info;
1379     int                 func;
1380     vertex_hdl_t        xconn_vhdl, conn_vhdl;
1381     int                 nfunc;
1382     int                 error_func;
1383     int                 error_slot = 0;
1384     int                 error = ENODEV;
1385
1386     pcibr_soft = pcibr_soft_get(pcibr_vhdl);
1387
1388     if (!pcibr_soft)
1389         return -EINVAL;
1390
1391     if (!PCIBR_VALID_SLOT(pcibr_soft, slot))
1392         return -EINVAL;
1393
1394     if (pcibr_soft->bs_slot[slot].has_host) {
1395         return -EPERM;
1396     }
1397     
1398     xconn_vhdl = pcibr_soft->bs_conn;
1399
1400     nfunc = pcibr_soft->bs_slot[slot].bss_ninfo;
1401     pcibr_infoh = pcibr_soft->bs_slot[slot].bss_infos;
1402
1403     for (func = 0; func < nfunc; ++func) {
1404
1405         pcibr_info = pcibr_infoh[func];
1406         
1407         if (!pcibr_info)
1408             continue;
1409
1410         if (pcibr_info->f_vendor == PCIIO_VENDOR_ID_NONE)
1411             continue;
1412
1413         conn_vhdl = pcibr_info->f_vertex;
1414
1415         error_func = pciio_device_attach(conn_vhdl, drv_flags);
1416
1417         PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_DEV_ATTACH, pcibr_vhdl,
1418                     "pcibr_slot_call_device_attach: slot=%d, func=%d "
1419                     "drv_flags=0x%x, pciio_device_attach returned %d\n",
1420                     PCIBR_DEVICE_TO_SLOT(pcibr_soft, slot), func, 
1421                     drv_flags, error_func));
1422         pcibr_info->f_att_det_error = error_func;
1423
1424         if (error_func)
1425             error_slot = error_func;
1426
1427         error = error_slot;
1428
1429     }                           /* next func */
1430
1431 #ifdef CONFIG_HOTPLUG_PCI_SGI
1432     if (error) {
1433         if ((error != ENODEV) && (error != EUNATCH) && (error != EPERM)) {
1434             pcibr_soft->bs_slot[slot].slot_status &= ~SLOT_STATUS_MASK;
1435             pcibr_soft->bs_slot[slot].slot_status |= SLOT_STARTUP_INCMPLT;
1436         }
1437     } else {
1438         pcibr_soft->bs_slot[slot].slot_status &= ~SLOT_STATUS_MASK;
1439         pcibr_soft->bs_slot[slot].slot_status |= SLOT_STARTUP_CMPLT;
1440     }
1441 #endif  /* CONFIG_HOTPLUG_PCI_SGI */
1442     return error;
1443 }
1444
1445 /*
1446  * pcibr_slot_call_device_detach
1447  *      This calls the associated driver detach routine for the PCI
1448  *      card in this slot.
1449  */
1450 int
1451 pcibr_slot_call_device_detach(vertex_hdl_t pcibr_vhdl,
1452                               pciio_slot_t slot,
1453                               int          drv_flags)
1454 {
1455     pcibr_soft_t        pcibr_soft;
1456     pcibr_info_h        pcibr_infoh;
1457     pcibr_info_t        pcibr_info;
1458     int                 func;
1459     vertex_hdl_t        conn_vhdl = GRAPH_VERTEX_NONE;
1460     int                 nfunc;
1461     int                 error_func;
1462     int                 error_slot = 0;
1463     int                 error = ENODEV;
1464
1465     pcibr_soft = pcibr_soft_get(pcibr_vhdl);
1466
1467     if (!pcibr_soft)
1468         return -EINVAL;
1469
1470     if (!PCIBR_VALID_SLOT(pcibr_soft, slot))
1471         return -EINVAL;
1472
1473     if (pcibr_soft->bs_slot[slot].has_host)
1474         return -EPERM;
1475
1476     nfunc = pcibr_soft->bs_slot[slot].bss_ninfo;
1477     pcibr_infoh = pcibr_soft->bs_slot[slot].bss_infos;
1478
1479     for (func = 0; func < nfunc; ++func) {
1480
1481         pcibr_info = pcibr_infoh[func];
1482         
1483         if (!pcibr_info)
1484             continue;
1485
1486         if (pcibr_info->f_vendor == PCIIO_VENDOR_ID_NONE)
1487             continue;
1488
1489         if (IS_PCIX(pcibr_soft) && pcibr_info->f_pcix_cap) {
1490             int max_out;
1491
1492             pcibr_soft->bs_pcix_num_funcs--;
1493             max_out = pcibr_info->f_pcix_cap->pcix_type0_status.max_out_split;
1494             pcibr_soft->bs_pcix_split_tot -= max_splittrans_to_numbuf[max_out];
1495         }
1496
1497         conn_vhdl = pcibr_info->f_vertex;
1498
1499         error_func = pciio_device_detach(conn_vhdl, drv_flags);
1500
1501         PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_DEV_DETACH, pcibr_vhdl,
1502                     "pcibr_slot_call_device_detach: slot=%d, func=%d "
1503                     "drv_flags=0x%x, pciio_device_detach returned %d\n",
1504                     PCIBR_DEVICE_TO_SLOT(pcibr_soft, slot), func, 
1505                     drv_flags, error_func));
1506
1507         pcibr_info->f_att_det_error = error_func;
1508
1509         if (error_func)
1510             error_slot = error_func;
1511
1512         error = error_slot;
1513
1514     }                           /* next func */
1515
1516 #ifdef CONFIG_HOTPLUG_PCI_SGI
1517     if (error) {
1518         if ((error != ENODEV) && (error != EUNATCH) && (error != EPERM)) {
1519             pcibr_soft->bs_slot[slot].slot_status &= ~SLOT_STATUS_MASK;
1520             pcibr_soft->bs_slot[slot].slot_status |= SLOT_SHUTDOWN_INCMPLT;
1521         }
1522     } else {
1523         if (conn_vhdl != GRAPH_VERTEX_NONE) 
1524             pcibr_device_unregister(conn_vhdl);
1525         pcibr_soft->bs_slot[slot].slot_status &= ~SLOT_STATUS_MASK;
1526         pcibr_soft->bs_slot[slot].slot_status |= SLOT_SHUTDOWN_CMPLT;
1527     }
1528 #endif  /* CONFIG_HOTPLUG_PCI_SGI */
1529     return error;
1530 }
1531
1532
1533
1534 /*
1535  * pcibr_slot_detach
1536  *      This is a place holder routine to keep track of all the
1537  *      slot-specific freeing that needs to be done.
1538  */
1539 int
1540 pcibr_slot_detach(vertex_hdl_t pcibr_vhdl,
1541                   pciio_slot_t slot,
1542                   int          drv_flags,
1543                   char        *l1_msg,
1544                   int         *sub_errorp)
1545 {
1546     pcibr_soft_t  pcibr_soft = pcibr_soft_get(pcibr_vhdl);
1547     int           error;
1548     
1549     /* Call the device detach function */
1550     error = (pcibr_slot_call_device_detach(pcibr_vhdl, slot, drv_flags));
1551     if (error) {
1552         if (sub_errorp)
1553             *sub_errorp = error;       
1554         if (l1_msg)
1555             ;
1556         return PCI_SLOT_DRV_DETACH_ERR;
1557     }
1558
1559     /* Recalculate the RBARs for all the devices on the bus since we've
1560      * just freed some up and some of the devices could use them.
1561      */
1562     if (IS_PCIX(pcibr_soft)) {
1563         int tmp_slot;
1564
1565         pcibr_soft->bs_pcix_rbar_inuse = 0;
1566         pcibr_soft->bs_pcix_rbar_avail = NUM_RBAR;
1567         pcibr_soft->bs_pcix_rbar_percent_allowed = 
1568                                         pcibr_pcix_rbars_calc(pcibr_soft);
1569
1570         for (tmp_slot = pcibr_soft->bs_min_slot;
1571                         tmp_slot < PCIBR_NUM_SLOTS(pcibr_soft); ++tmp_slot)
1572             (void)pcibr_slot_pcix_rbar_init(pcibr_soft, tmp_slot);
1573     }
1574
1575     return 0;
1576
1577 }
1578
1579 /*
1580  * pcibr_probe_slot_pic: read a config space word
1581  * while trapping any errors; return zero if
1582  * all went OK, or nonzero if there was an error.
1583  * The value read, if any, is passed back
1584  * through the valp parameter.
1585  */
1586 static int
1587 pcibr_probe_slot(pcibr_soft_t pcibr_soft,
1588                  cfg_p cfg,
1589                  unsigned *valp)
1590 {
1591     return pcibr_probe_work(pcibr_soft, (void *)cfg, 4, (void *)valp);
1592 }
1593
1594 /*
1595  * Probe an offset within a piomap with errors disabled.
1596  * len must be 1, 2, 4, or 8.   The probed address must be a multiple of
1597  * len.
1598  *
1599  * Returns:     0       if the offset was probed and put valid data in valp
1600  *              -1      if there was a usage error such as improper alignment
1601  *                      or out of bounds offset/len combination.  In this
1602  *                      case, the map was not probed
1603  *              1       if the offset was probed but resulted in an error
1604  *                      such as device not responding, bus error, etc.
1605  */
1606
1607 int
1608 pcibr_piomap_probe(pcibr_piomap_t piomap, off_t offset, int len, void *valp)
1609 {
1610         if (offset + len > piomap->bp_mapsz) {
1611                 return -1;
1612         }
1613
1614         return pcibr_probe_work(piomap->bp_soft,
1615                                 piomap->bp_kvaddr + offset, len, valp);
1616 }
1617
1618 static uint64_t
1619 pcibr_disable_mst_timeout(pcibr_soft_t pcibr_soft)
1620 {
1621     uint64_t            old_enable;
1622     uint64_t            new_enable;
1623     uint64_t            intr_bits;
1624
1625     intr_bits = PIC_ISR_PCI_MST_TIMEOUT
1626                 | PIC_ISR_PCIX_MTOUT | PIC_ISR_PCIX_SPLIT_EMSG;
1627     old_enable = pcireg_intr_enable_get(pcibr_soft);
1628     pcireg_intr_enable_bit_clr(pcibr_soft, intr_bits);
1629     new_enable = pcireg_intr_enable_get(pcibr_soft);
1630
1631     if (old_enable == new_enable) {
1632         return 0;               /* was already disabled */
1633     } else {
1634         return 1;
1635     }
1636 }
1637
1638 static int
1639 pcibr_enable_mst_timeout(pcibr_soft_t pcibr_soft)
1640 {
1641     uint64_t            old_enable;
1642     uint64_t            new_enable;
1643     uint64_t            intr_bits;
1644     
1645     intr_bits = PIC_ISR_PCI_MST_TIMEOUT
1646                 | PIC_ISR_PCIX_MTOUT | PIC_ISR_PCIX_SPLIT_EMSG;
1647     old_enable = pcireg_intr_enable_get(pcibr_soft);
1648     pcireg_intr_enable_bit_set(pcibr_soft, intr_bits);
1649     new_enable = pcireg_intr_enable_get(pcibr_soft);
1650
1651     if (old_enable == new_enable) {
1652         return 0;               /* was alread enabled */
1653     } else {
1654         return 1;
1655     }
1656 }
1657
1658 /*
1659  * pcibr_probe_slot: read a config space word
1660  * while trapping any errors; return zero if
1661  * all went OK, or nonzero if there was an error.
1662  * The value read, if any, is passed back
1663  * through the valp parameter.
1664  */
1665 static int
1666 pcibr_probe_work(pcibr_soft_t pcibr_soft,
1667                  void *addr,
1668                  int len,
1669                  void *valp)
1670 {
1671     int                 rv, changed;
1672
1673     /*
1674      * Sanity checks ...
1675      */
1676
1677     if (len != 1 && len != 2 && len != 4 && len != 8) {
1678         return -1;                              /* invalid len */
1679     }
1680
1681     if ((uint64_t)addr & (len-1)) {
1682         return -1;                              /* invalid alignment */
1683     }
1684
1685     changed = pcibr_disable_mst_timeout(pcibr_soft);
1686
1687     rv = snia_badaddr_val((void *)addr, len, valp);
1688
1689     /* Clear the int_view register incase it was set */
1690     pcireg_intr_reset_set(pcibr_soft, BRIDGE_IRR_MULTI_CLR);
1691
1692     if (changed) {
1693         pcibr_enable_mst_timeout(pcibr_soft);
1694     }
1695     return (rv ? 1 : 0);        /* return 1 for snia_badaddr_val error, 0 if ok */
1696 }
1697
1698 void
1699 pcibr_device_info_free(vertex_hdl_t pcibr_vhdl, pciio_slot_t slot)
1700 {
1701     pcibr_soft_t        pcibr_soft = pcibr_soft_get(pcibr_vhdl);
1702     pcibr_info_t        pcibr_info;
1703     pciio_function_t    func;
1704     pcibr_soft_slot_t   slotp = &pcibr_soft->bs_slot[slot];
1705     cfg_p               cfgw;
1706     int                 nfunc = slotp->bss_ninfo;
1707     int                 bar;
1708     int                 devio_index;
1709     unsigned long       s;
1710     unsigned            cmd_reg;
1711
1712
1713     for (func = 0; func < nfunc; func++) {
1714         pcibr_info = slotp->bss_infos[func];
1715
1716         if (!pcibr_info) 
1717             continue;
1718
1719         s = pcibr_lock(pcibr_soft);
1720
1721         /* Disable memory and I/O BARs */
1722         cfgw = pcibr_func_config_addr(pcibr_soft, 0, slot, func, 0);
1723         cmd_reg = do_pcibr_config_get(cfgw, PCI_CFG_COMMAND, 4);
1724         cmd_reg &= (PCI_CMD_MEM_SPACE | PCI_CMD_IO_SPACE);
1725         do_pcibr_config_set(cfgw, PCI_CFG_COMMAND, 4, cmd_reg);
1726
1727         for (bar = 0; bar < PCI_CFG_BASE_ADDRS; bar++) {
1728             if (pcibr_info->f_window[bar].w_space == PCIIO_SPACE_NONE)
1729                 continue;
1730
1731             /* Free the PCI bus space */
1732             pcibr_bus_addr_free(&pcibr_info->f_window[bar]);
1733
1734             /* Get index of the DevIO(x) register used to access this BAR */
1735             devio_index = pcibr_info->f_window[bar].w_devio_index;
1736
1737  
1738             /* On last use, clear the DevIO(x) used to access this BAR */
1739             if (! --pcibr_soft->bs_slot[devio_index].bss_devio.bssd_ref_cnt) {
1740                pcibr_soft->bs_slot[devio_index].bss_devio.bssd_space =
1741                                                        PCIIO_SPACE_NONE; 
1742                pcibr_soft->bs_slot[devio_index].bss_devio.bssd_base =
1743                                                        PCIBR_D32_BASE_UNSET;
1744                pcibr_soft->bs_slot[devio_index].bss_device = 0;
1745             }
1746         }
1747
1748         /* Free the Expansion ROM PCI bus space */
1749         if(pcibr_info->f_rbase && pcibr_info->f_rsize) {
1750             pcibr_bus_addr_free(&pcibr_info->f_rwindow);
1751         }
1752
1753         pcibr_unlock(pcibr_soft, s);
1754
1755         slotp->bss_infos[func] = 0;
1756         pciio_device_info_unregister(pcibr_vhdl, &pcibr_info->f_c);
1757         pciio_device_info_free(&pcibr_info->f_c);
1758
1759         kfree(pcibr_info);
1760     }
1761
1762     /* Reset the mapping usage counters */
1763     slotp->bss_pmu_uctr = 0;
1764     slotp->bss_d32_uctr = 0;
1765     slotp->bss_d64_uctr = 0;
1766
1767     /* Clear the Direct translation info */
1768     slotp->bss_d64_base = PCIBR_D64_BASE_UNSET;
1769     slotp->bss_d64_flags = 0;
1770     slotp->bss_d32_base = PCIBR_D32_BASE_UNSET;
1771     slotp->bss_d32_flags = 0;
1772 }
1773
1774
1775 iopaddr_t
1776 pcibr_bus_addr_alloc(pcibr_soft_t pcibr_soft, pciio_win_info_t win_info_p,
1777                      pciio_space_t space, int start, int size, int align)
1778 {
1779     pciio_win_map_t win_map_p;
1780     struct resource *root_resource = NULL;
1781     iopaddr_t iopaddr = 0;
1782
1783     switch (space) {
1784
1785         case PCIIO_SPACE_IO:
1786             win_map_p = &pcibr_soft->bs_io_win_map;
1787             root_resource = &pcibr_soft->bs_io_win_root_resource;
1788             break;
1789
1790         case PCIIO_SPACE_MEM:
1791             win_map_p = &pcibr_soft->bs_swin_map;
1792             root_resource = &pcibr_soft->bs_swin_root_resource;
1793             break;
1794
1795         case PCIIO_SPACE_MEM32:
1796             win_map_p = &pcibr_soft->bs_mem_win_map;
1797             root_resource = &pcibr_soft->bs_mem_win_root_resource;
1798             break;
1799
1800         default:
1801             return 0;
1802
1803     }
1804     iopaddr = pciio_device_win_alloc(root_resource,
1805                                   win_info_p
1806                                   ? &win_info_p->w_win_alloc
1807                                   : NULL,
1808                                   start, size, align);
1809     return iopaddr;
1810 }
1811
1812
1813 void
1814 pcibr_bus_addr_free(pciio_win_info_t win_info_p)
1815 {
1816         pciio_device_win_free(&win_info_p->w_win_alloc);
1817 }
1818
1819 /*
1820  * given a vertex_hdl to the pcibr_vhdl, return the brick's bus number
1821  * associated with that vertex_hdl.  The true mapping happens from the
1822  * io_brick_tab[] array defined in ml/SN/iograph.c
1823  */
1824 int
1825 pcibr_widget_to_bus(vertex_hdl_t pcibr_vhdl) 
1826 {
1827     pcibr_soft_t        pcibr_soft = pcibr_soft_get(pcibr_vhdl);
1828     xwidgetnum_t        widget = pcibr_soft->bs_xid;
1829     int                 bricktype = pcibr_soft->bs_bricktype;
1830     int                 bus;
1831     
1832     if ((bus = io_brick_map_widget(bricktype, widget)) <= 0) {
1833         printk(KERN_WARNING "pcibr_widget_to_bus() bad bricktype %d\n", bricktype);
1834         return 0;
1835     }
1836
1837     /* For PIC there are 2 busses per widget and pcibr_soft->bs_busnum
1838      * will be 0 or 1.  Add in the correct PIC bus offset.
1839      */
1840     bus += pcibr_soft->bs_busnum;
1841     return bus;
1842 }