ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ia64 / sn / io / sn2 / pcibr / pcibr_error.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-2003 Silicon Graphics, Inc. All rights reserved.
7  */
8
9 #include <linux/types.h>
10 #include <linux/interrupt.h>
11 #include <asm/sn/sgi.h>
12 #include <asm/sn/addrs.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
19
20 extern int      hubii_check_widget_disabled(nasid_t, int);
21
22
23 /* =====================================================================
24  *    ERROR HANDLING
25  */
26
27 #ifdef  DEBUG
28 #ifdef  ERROR_DEBUG
29 #define BRIDGE_PIOERR_TIMEOUT   100     /* Timeout with ERROR_DEBUG defined */
30 #else
31 #define BRIDGE_PIOERR_TIMEOUT   40      /* Timeout in debug mode  */
32 #endif
33 #else
34 #define BRIDGE_PIOERR_TIMEOUT   1       /* Timeout in non-debug mode */
35 #endif
36
37 #ifdef  DEBUG
38 #ifdef ERROR_DEBUG
39 uint64_t bridge_errors_to_dump = ~BRIDGE_ISR_INT_MSK;
40 #else
41 uint64_t bridge_errors_to_dump = BRIDGE_ISR_ERROR_DUMP;
42 #endif
43 #else
44 uint64_t bridge_errors_to_dump = BRIDGE_ISR_ERROR_FATAL |
45                                    BRIDGE_ISR_PCIBUS_PIOERR;
46 #endif
47
48 int pcibr_pioerr_dump = 1;      /* always dump pio errors */
49
50 /*
51  * register values
52  * map between numeric values and symbolic values
53  */
54 struct reg_values {
55         unsigned long long rv_value;
56         char *rv_name;
57 };
58
59 /*
60  * register descriptors are used for formatted prints of register values
61  * rd_mask and rd_shift must be defined, other entries may be null
62  */
63 struct reg_desc {
64         unsigned long long rd_mask;     /* mask to extract field */
65         int rd_shift;           /* shift for extracted value, - >>, + << */
66         char *rd_name;          /* field name */
67         char *rd_format;        /* format to print field */
68         struct reg_values *rd_values;   /* symbolic names of values */
69 };
70
71 /* Crosstalk Packet Types */
72 static struct reg_values xtalk_cmd_pactyp[] =
73 {
74     {0x0, "RdReq"},
75     {0x1, "RdResp"},
76     {0x2, "WrReqWithResp"},
77     {0x3, "WrResp"},
78     {0x4, "WrReqNoResp"},
79     {0x5, "Reserved(5)"},
80     {0x6, "FetchAndOp"},
81     {0x7, "Reserved(7)"},
82     {0x8, "StoreAndOp"},
83     {0x9, "Reserved(9)"},
84     {0xa, "Reserved(a)"},
85     {0xb, "Reserved(b)"},
86     {0xc, "Reserved(c)"},
87     {0xd, "Reserved(d)"},
88     {0xe, "SpecialReq"},
89     {0xf, "SpecialResp"},
90     {0}
91 };
92
93 static struct reg_desc   xtalk_cmd_bits[] =
94 {
95     {WIDGET_DIDN, -28, "DIDN", "%x"},
96     {WIDGET_SIDN, -24, "SIDN", "%x"},
97     {WIDGET_PACTYP, -20, "PACTYP", 0, xtalk_cmd_pactyp},
98     {WIDGET_TNUM, -15, "TNUM", "%x"},
99     {WIDGET_COHERENT, 0, "COHERENT"},
100     {WIDGET_DS, 0, "DS"},
101     {WIDGET_GBR, 0, "GBR"},
102     {WIDGET_VBPM, 0, "VBPM"},
103     {WIDGET_ERROR, 0, "ERROR"},
104     {WIDGET_BARRIER, 0, "BARRIER"},
105     {0}
106 };
107
108 #define F(s,n)          { 1l<<(s),-(s), n }
109
110 char *pci_space[] = {"NONE",
111                      "ROM",
112                      "IO",
113                      "",
114                      "MEM",
115                      "MEM32",
116                      "MEM64",
117                      "CFG",
118                      "WIN0",
119                      "WIN1",
120                      "WIN2",
121                      "WIN3",
122                      "WIN4",
123                      "WIN5",
124                      "",
125                      "BAD"};
126
127 static char             *pcibr_isr_errs[] =
128 {
129     "", "", "", "", "", "", "", "",
130     "08: Reserved Bit 08",
131     "09: PCI to Crosstalk read request timeout",
132     "10: PCI retry operation count exhausted.",
133     "11: PCI bus device select timeout",
134     "12: PCI device reported parity error",
135     "13: PCI Address/Cmd parity error ",
136     "14: PCI Bridge detected parity error",
137     "15: PCI abort condition",
138     "16: Reserved Bit 16",
139     "17: LLP Transmitter Retry count wrapped",  /* PIC ONLY */
140     "18: LLP Transmitter side required Retry",  /* PIC ONLY */
141     "19: LLP Receiver retry count wrapped",     /* PIC ONLY */
142     "20: LLP Receiver check bit error",         /* PIC ONLY */
143     "21: LLP Receiver sequence number error",   /* PIC ONLY */
144     "22: Request packet overflow",
145     "23: Request operation not supported by bridge",
146     "24: Request packet has invalid address for bridge widget",
147     "25: Incoming request xtalk command word error bit set or invalid sideband",
148     "26: Incoming response xtalk command word error bit set or invalid sideband",
149     "27: Framing error, request cmd data size does not match actual",
150     "28: Framing error, response cmd data size does not match actual",
151     "29: Unexpected response arrived",
152     "30: PMU Access Fault",
153     "31: Reserved Bit 31",
154     "32: PCI-X address or attribute cycle parity error",
155     "33: PCI-X data cycle parity error",
156     "34: PCI-X master timeout (ie. master abort)",
157     "35: PCI-X pio retry counter exhausted",
158     "36: PCI-X SERR",
159     "37: PCI-X PERR", 
160     "38: PCI-X target abort",
161     "39: PCI-X read request timeout",
162     "40: PCI / PCI-X device requestin arbitration error",
163     "41: internal RAM parity error",
164     "42: PCI-X unexpected completion cycle to master",
165     "43: PCI-X split completion timeout",
166     "44: PCI-X split completion error message",
167     "45: PCI-X split completion message parity error",
168 };
169
170 /*
171  * print_register() allows formatted printing of bit fields.  individual
172  * bit fields are described by a struct reg_desc, multiple bit fields within
173  * a single word can be described by multiple reg_desc structures.
174  * %r outputs a string of the format "<bit field descriptions>"
175  * %R outputs a string of the format "0x%x<bit field descriptions>"
176  *
177  * The fields in a reg_desc are:
178  *      unsigned long long rd_mask; An appropriate mask to isolate the bit field
179  *                              within a word, and'ed with val
180  *
181  *      int rd_shift;           A shift amount to be done to the isolated
182  *                              bit field.  done before printing the isolate
183  *                              bit field with rd_format and before searching
184  *                              for symbolic value names in rd_values
185  *
186  *      char *rd_name;          If non-null, a bit field name to label any
187  *                              out from rd_format or searching rd_values.
188  *                              if neither rd_format or rd_values is non-null
189  *                              rd_name is printed only if the isolated
190  *                              bit field is non-null.
191  *
192  *      char *rd_format;        If non-null, the shifted bit field value
193  *                              is printed using this format.
194  *
195  *      struct reg_values *rd_values;   If non-null, a pointer to a table
196  *                              matching numeric values with symbolic names.
197  *                              rd_values are searched and the symbolic
198  *                              value is printed if a match is found, if no
199  *                              match is found "???" is printed.
200  *                              
201  */
202
203 static void
204 print_register(unsigned long long reg, struct reg_desc *addr)
205 {
206         register struct reg_desc *rd;
207         register struct reg_values *rv;
208         unsigned long long field;
209         int any;
210
211         printk("<");
212         any = 0;
213         for (rd = addr; rd->rd_mask; rd++) {
214                 field = reg & rd->rd_mask;
215                 field = (rd->rd_shift > 0) ? field << rd->rd_shift : field >> -rd->rd_shift;
216                 if (any && (rd->rd_format || rd->rd_values || (rd->rd_name && field)))
217                         printk(",");
218                 if (rd->rd_name) {
219                         if (rd->rd_format || rd->rd_values || field) {
220                                 printk("%s", rd->rd_name);
221                                 any = 1;
222                         }
223                         if (rd->rd_format || rd->rd_values) {
224                                 printk("=");
225                                 any = 1;
226                         }
227                 }
228                 /* You can have any format so long as it is %x */
229                 if (rd->rd_format) {
230                         printk("%llx", field);
231                         any = 1;
232                         if (rd->rd_values)
233                                 printk(":");
234                 }
235                 if (rd->rd_values) {
236                         any = 1;
237                         for (rv = rd->rd_values; rv->rv_name; rv++) {
238                                 if (field == rv->rv_value) {
239                                         printk("%s", rv->rv_name);
240                                         break;
241                                 }
242                         }
243                         if (rv->rv_name == NULL)
244                                 printk("???");
245                 }
246         }
247         printk(">\n");
248 }
249
250
251 /*
252  * display memory directory state
253  */
254 static void
255 pcibr_show_dir_state(paddr_t paddr, char *prefix)
256 {
257 #ifdef PCIBR_LATER
258         int state;
259         uint64_t vec_ptr;
260         hubreg_t elo;
261         extern char *dir_state_str[];
262         extern void get_dir_ent(paddr_t, int *, uint64_t *, hubreg_t *);
263
264         get_dir_ent(paddr, &state, &vec_ptr, &elo);
265
266         printf("%saddr 0x%lx: state 0x%x owner 0x%lx (%s)\n", 
267                 prefix, (uint64_t)paddr, state, (uint64_t)vec_ptr, 
268                 dir_state_str[state]);
269 #endif /* PCIBR_LATER */
270 }
271
272
273 void
274 print_bridge_errcmd(pcibr_soft_t pcibr_soft, uint32_t cmdword, char *errtype)
275 {
276     printk(
277             "\t    Bridge %sError Command Word Register ", errtype);
278     print_register(cmdword, xtalk_cmd_bits);
279 }
280
281
282 /*
283  *      Dump relevant error information for Bridge error interrupts.
284  */
285 /*ARGSUSED */
286 void
287 pcibr_error_dump(pcibr_soft_t pcibr_soft)
288 {
289     uint64_t                int_status;
290     uint64_t                mult_int;
291     uint64_t                bit;
292     int                     i;
293
294     int_status = (pcireg_intr_status_get(pcibr_soft) & ~BRIDGE_ISR_INT_MSK);
295
296     if (!int_status) {
297         /* No error bits set */
298         return;
299     }
300
301     /* Check if dumping the same error information multiple times */
302     if ( pcibr_soft->bs_errinfo.bserr_intstat == int_status )
303         return;
304     pcibr_soft->bs_errinfo.bserr_intstat = int_status;
305
306     printk(KERN_ALERT "PCI BRIDGE ERROR: int_status is 0x%lx for %s\n"
307         "    Dumping relevant %s registers for each bit set...\n",
308             int_status, pcibr_soft->bs_name,
309             "PIC");
310
311     for (i = PCIBR_ISR_ERR_START; i < 64; i++) {
312         bit = 1ull << i;
313
314         /* A number of int_status bits are only valid for PIC's bus0 */
315         if ((pcibr_soft->bs_busnum != 0) && 
316             ((bit == BRIDGE_ISR_UNSUPPORTED_XOP) ||
317              (bit == BRIDGE_ISR_LLP_REC_SNERR) ||
318              (bit == BRIDGE_ISR_LLP_REC_CBERR) ||
319              (bit == BRIDGE_ISR_LLP_RCTY) ||
320              (bit == BRIDGE_ISR_LLP_TX_RETRY) ||
321              (bit == BRIDGE_ISR_LLP_TCTY))) {
322             continue;
323         }
324
325         if (int_status & bit) {
326             printk("\t%s\n", pcibr_isr_errs[i]);
327
328             switch (bit) {
329
330             case PIC_ISR_INT_RAM_PERR:      /* bit41    INT_RAM_PERR */
331                 /* XXX: should breakdown meaning of bits in reg */
332                 printk("\t      Internal RAM Parity Error: 0x%lx\n",
333                     pcireg_parity_err_get(pcibr_soft));
334                 break;
335
336             case PIC_ISR_PCIX_ARB_ERR:      /* bit40    PCI_X_ARB_ERR */
337                 /* XXX: should breakdown meaning of bits in reg */
338                 printk("\t      Arbitration Reg: 0x%lx\n",
339                     pcireg_arbitration_get(pcibr_soft));
340                 break;
341
342             case PIC_ISR_PCIX_REQ_TOUT:     /* bit39    PCI_X_REQ_TOUT */
343                 /* XXX: should breakdown meaning of attribute bit */
344                 printk(
345                     "\t    PCI-X DMA Request Error Address Reg: 0x%lx\n"
346                     "\t    PCI-X DMA Request Error Attribute Reg: 0x%lx\n",
347                     pcireg_pcix_req_err_addr_get(pcibr_soft),
348                     pcireg_pcix_req_err_attr_get(pcibr_soft));
349                 break;
350
351             case PIC_ISR_PCIX_SPLIT_MSG_PE: /* bit45    PCI_X_SPLIT_MES_PE */
352             case PIC_ISR_PCIX_SPLIT_EMSG:   /* bit44    PCI_X_SPLIT_EMESS */
353             case PIC_ISR_PCIX_SPLIT_TO:     /* bit43    PCI_X_SPLIT_TO */
354                 /* XXX: should breakdown meaning of attribute bit */
355                 printk(
356                     "\t    PCI-X Split Request Address Reg: 0x%lx\n"
357                     "\t    PCI-X Split Request Attribute Reg: 0x%lx\n",
358                     pcireg_pcix_pio_split_addr_get(pcibr_soft),
359                     pcireg_pcix_pio_split_attr_get(pcibr_soft));
360                 /* FALL THRU */
361
362             case PIC_ISR_PCIX_UNEX_COMP:    /* bit42    PCI_X_UNEX_COMP */
363             case PIC_ISR_PCIX_TABORT:       /* bit38    PCI_X_TABORT */
364             case PIC_ISR_PCIX_PERR:         /* bit37    PCI_X_PERR */
365             case PIC_ISR_PCIX_SERR:         /* bit36    PCI_X_SERR */
366             case PIC_ISR_PCIX_MRETRY:       /* bit35    PCI_X_MRETRY */
367             case PIC_ISR_PCIX_MTOUT:        /* bit34    PCI_X_MTOUT */
368             case PIC_ISR_PCIX_DA_PARITY:    /* bit33    PCI_X_DA_PARITY */
369             case PIC_ISR_PCIX_AD_PARITY:    /* bit32    PCI_X_AD_PARITY */
370                 /* XXX: should breakdown meaning of attribute bit */
371                 printk(
372                     "\t    PCI-X Bus Error Address Reg: 0x%lx\n"
373                     "\t    PCI-X Bus Error Attribute Reg: 0x%lx\n"
374                     "\t    PCI-X Bus Error Data Reg: 0x%lx\n",
375                     pcireg_pcix_bus_err_addr_get(pcibr_soft),
376                     pcireg_pcix_bus_err_attr_get(pcibr_soft),
377                     pcireg_pcix_bus_err_data_get(pcibr_soft));
378                 break;
379
380             case BRIDGE_ISR_PAGE_FAULT: /* bit30    PMU_PAGE_FAULT */
381                 printk("\t    Map Fault Address Reg: 0x%lx\n",
382                     pcireg_map_fault_get(pcibr_soft));
383                 break;
384
385             case BRIDGE_ISR_UNEXP_RESP:         /* bit29    UNEXPECTED_RESP */
386                 print_bridge_errcmd(pcibr_soft,
387                             pcireg_linkside_err_get(pcibr_soft), "Aux ");
388
389                 /* PIC in PCI-X mode, dump the PCIX DMA Request registers */
390                 if (IS_PCIX(pcibr_soft)) {
391                     /* XXX: should breakdown meaning of attr bit */
392                     printk( 
393                         "\t    PCI-X DMA Request Error Addr Reg: 0x%lx\n"
394                         "\t    PCI-X DMA Request Error Attr Reg: 0x%lx\n",
395                         pcireg_pcix_req_err_addr_get(pcibr_soft),
396                         pcireg_pcix_req_err_attr_get(pcibr_soft));
397                 }
398                 break;
399
400             case BRIDGE_ISR_BAD_XRESP_PKT:      /* bit28    BAD_RESP_PACKET */
401             case BRIDGE_ISR_RESP_XTLK_ERR:      /* bit26    RESP_XTALK_ERROR */
402                 print_bridge_errcmd(pcibr_soft,
403                                 pcireg_linkside_err_get(pcibr_soft), "Aux ");
404                  
405                 /* PCI-X mode, DMA Request Error registers are valid.  But
406                  * in PCI mode, Response Buffer Address register are valid.
407                  */
408                 if (IS_PCIX(pcibr_soft)) {
409                     /* XXX: should breakdown meaning of attribute bit */
410                     printk(
411                         "\t    PCI-X DMA Request Error Addr Reg: 0x%lx\n"
412                         "\t    PCI-X DMA Request Error Attribute Reg: 0x%lx\n",
413                         pcireg_pcix_req_err_addr_get(pcibr_soft),
414                         pcireg_pcix_req_err_attr_get(pcibr_soft));
415                 } else {
416                     printk(
417                         "\t    Bridge Response Buf Error Addr Reg: 0x%lx\n"
418                         "\t    dev-num %d buff-num %d addr 0x%lx\n",
419                         pcireg_resp_err_get(pcibr_soft),
420                         (int)pcireg_resp_err_dev_get(pcibr_soft),
421                         (int)pcireg_resp_err_buf_get(pcibr_soft),
422                         pcireg_resp_err_addr_get(pcibr_soft));
423                     if (bit == BRIDGE_ISR_RESP_XTLK_ERR) {
424                         /* display memory directory associated with cacheline */
425                         pcibr_show_dir_state(
426                                     pcireg_resp_err_get(pcibr_soft), "\t    ");
427                     }
428                 }
429                 break;
430
431             case BRIDGE_ISR_BAD_XREQ_PKT:       /* bit27    BAD_XREQ_PACKET */
432             case BRIDGE_ISR_REQ_XTLK_ERR:       /* bit25    REQ_XTALK_ERROR */
433             case BRIDGE_ISR_INVLD_ADDR:         /* bit24    INVALID_ADDRESS */
434                 print_bridge_errcmd(pcibr_soft,
435                                 pcireg_cmdword_err_get(pcibr_soft), "");
436                 printk(
437                     "\t    Bridge Error Address Register: 0x%lx\n"
438                     "\t    Bridge Error Address: 0x%lx\n",
439                     pcireg_bus_err_get(pcibr_soft),
440                     pcireg_bus_err_get(pcibr_soft));
441                 break;
442
443             case BRIDGE_ISR_UNSUPPORTED_XOP:    /* bit23    UNSUPPORTED_XOP */
444                 print_bridge_errcmd(pcibr_soft,
445                                 pcireg_linkside_err_get(pcibr_soft), "Aux ");
446                 printk("\t    Address Holding Link Side Error Reg: 0x%lx\n",
447                         pcireg_linkside_err_addr_get(pcibr_soft));
448                 break;
449
450             case BRIDGE_ISR_XREQ_FIFO_OFLOW:    /* bit22    XREQ_FIFO_OFLOW */
451                 print_bridge_errcmd(pcibr_soft,
452                                 pcireg_linkside_err_get(pcibr_soft), "Aux ");
453                 printk("\t    Address Holding Link Side Error Reg: 0x%lx\n",
454                         pcireg_linkside_err_addr_get(pcibr_soft));
455                 break;
456
457             case BRIDGE_ISR_PCI_ABORT:          /* bit15    PCI_ABORT */
458             case BRIDGE_ISR_PCI_PARITY:         /* bit14    PCI_PARITY */
459             case BRIDGE_ISR_PCI_SERR:           /* bit13    PCI_SERR */
460             case BRIDGE_ISR_PCI_PERR:           /* bit12    PCI_PERR */
461             case BRIDGE_ISR_PCI_MST_TIMEOUT:    /* bit11    PCI_MASTER_TOUT */
462             case BRIDGE_ISR_PCI_RETRY_CNT:      /* bit10    PCI_RETRY_CNT */
463                 printk("\t    PCI Error Address Register: 0x%lx\n"
464                     "\t    PCI Error Address: 0x%lx\n",
465                     pcireg_pci_bus_addr_get(pcibr_soft),
466                     pcireg_pci_bus_addr_addr_get(pcibr_soft));
467                 break;
468
469             case BRIDGE_ISR_XREAD_REQ_TIMEOUT:  /* bit09    XREAD_REQ_TOUT */
470                 printk("\t    Bridge Response Buf Error Addr Reg: 0x%lx\n"
471                     "\t    dev-num %d buff-num %d addr 0x%lx\n",
472                     pcireg_resp_err_get(pcibr_soft),
473                     (int)pcireg_resp_err_dev_get(pcibr_soft),
474                     (int)pcireg_resp_err_buf_get(pcibr_soft),
475                     pcireg_resp_err_get(pcibr_soft));
476                 break;
477             }
478         }
479     }
480
481     mult_int = pcireg_intr_multiple_get(pcibr_soft);
482
483     if (mult_int & ~BRIDGE_ISR_INT_MSK) {
484         printk("    %s Multiple Interrupt Register is 0x%lx\n",
485                 pcibr_soft->bs_asic_name, mult_int);
486         for (i = PCIBR_ISR_ERR_START; i < 64; i++) {
487             if (mult_int & (1ull << i))
488                 printk( "\t%s\n", pcibr_isr_errs[i]);
489         }
490     }
491 }
492
493 /* pcibr_pioerr_check():
494  *      Check to see if this pcibr has a PCI PIO
495  *      TIMEOUT error; if so, bump the timeout-count
496  *      on any piomaps that could cover the address.
497  */
498 static void
499 pcibr_pioerr_check(pcibr_soft_t soft)
500 {
501     uint64_t                int_status;
502     iopaddr_t               pci_addr;
503     pciio_slot_t            slot;
504     pcibr_piomap_t          map;
505     iopaddr_t               base;
506     size_t                  size;
507     unsigned                win;
508     int                     func;
509
510     int_status = pcireg_intr_status_get(soft);
511
512     if (int_status & BRIDGE_ISR_PCIBUS_PIOERR) {
513         pci_addr = pcireg_pci_bus_addr_get(soft);
514
515         slot = PCIBR_NUM_SLOTS(soft);
516         while (slot-- > 0) {
517             int                 nfunc = soft->bs_slot[slot].bss_ninfo;
518             pcibr_info_h        pcibr_infoh = soft->bs_slot[slot].bss_infos;
519
520             for (func = 0; func < nfunc; func++) {
521                 pcibr_info_t    pcibr_info = pcibr_infoh[func];
522
523                 if (!pcibr_info)
524                     continue;
525
526                 for (map = pcibr_info->f_piomap;
527                         map != NULL; map = map->bp_next) {
528                     base = map->bp_pciaddr;
529                     size = map->bp_mapsz;
530                     win = map->bp_space - PCIIO_SPACE_WIN(0);
531                     if (win < 6)
532                         base += soft->bs_slot[slot].bss_window[win].bssw_base;
533                     else if (map->bp_space == PCIIO_SPACE_ROM)
534                         base += pcibr_info->f_rbase;
535                     if ((pci_addr >= base) && (pci_addr < (base + size)))
536                         atomic_inc(&map->bp_toc);
537                 }
538             }
539         }
540     }
541 }
542
543 /*
544  * PCI Bridge Error interrupt handler.
545  *      This gets invoked, whenever a PCI bridge sends an error interrupt.
546  *      Primarily this servers two purposes.
547  *              - If an error can be handled (typically a PIO read/write
548  *                error, we try to do it silently.
549  *              - If an error cannot be handled, we die violently.
550  *      Interrupt due to PIO errors:
551  *              - Bridge sends an interrupt, whenever a PCI operation
552  *                done by the bridge as the master fails. Operations could
553  *                be either a PIO read or a PIO write.
554  *                PIO Read operation also triggers a bus error, and it's
555  *                We primarily ignore this interrupt in that context..
556  *                For PIO write errors, this is the only indication.
557  *                and we have to handle with the info from here.
558  *
559  *                So, there is no way to distinguish if an interrupt is
560  *                due to read or write error!.
561  */
562
563 irqreturn_t
564 pcibr_error_intr_handler(int irq, void *arg, struct pt_regs *ep)
565 {
566     pcibr_soft_t            pcibr_soft;
567     void               *bridge;
568     uint64_t                int_status;
569     uint64_t                err_status;
570     int                     i;
571     uint64_t                disable_errintr_mask = 0;
572     nasid_t                 nasid;
573
574
575 #if PCIBR_SOFT_LIST
576     /*
577      * Defensive code for linked pcibr_soft structs
578      */
579     {
580         extern pcibr_list_p     pcibr_list;
581         pcibr_list_p            entry;
582
583         entry = pcibr_list;
584         while (1) {
585             if (entry == NULL) {
586                 printk("pcibr_error_intr_handler: (0x%lx) is not a pcibr_soft!",
587                       (uint64_t)arg);
588                 return IRQ_NONE;
589             }
590             if ((intr_arg_t) entry->bl_soft == arg)
591                 break;
592             entry = entry->bl_next;
593         }
594     }
595 #endif /* PCIBR_SOFT_LIST */
596     pcibr_soft = (pcibr_soft_t) arg;
597     bridge = pcibr_soft->bs_base;
598
599     /*
600      * pcibr_error_intr_handler gets invoked whenever bridge encounters
601      * an error situation, and the interrupt for that error is enabled.
602      * This routine decides if the error is fatal or not, and takes
603      * action accordingly.
604      *
605      * In the case of PIO read/write timeouts, there is no way
606      * to know if it was a read or write request that timed out.
607      * If the error was due to a "read", a bus error will also occur
608      * and the bus error handling code takes care of it. 
609      * If the error is due to a "write", the error is currently logged 
610      * by this routine. For SN1 and SN0, if fire-and-forget mode is 
611      * disabled, a write error response xtalk packet will be sent to 
612      * the II, which will cause an II error interrupt. No write error 
613      * recovery actions of any kind currently take place at the pcibr 
614      * layer! (e.g., no panic on unrecovered write error)
615      *
616      * Prior to reading the Bridge int_status register we need to ensure
617      * that there are no error bits set in the lower layers (hubii)
618      * that have disabled PIO access to the widget. If so, there is nothing
619      * we can do until the bits clear, so we setup a timeout and try again
620      * later.
621      */
622
623     nasid = NASID_GET(bridge);
624     if (hubii_check_widget_disabled(nasid, pcibr_soft->bs_xid)) {
625         DECLARE_WAIT_QUEUE_HEAD(wq);
626         sleep_on_timeout(&wq, BRIDGE_PIOERR_TIMEOUT*HZ );  /* sleep */
627         pcibr_soft->bs_errinfo.bserr_toutcnt++;
628         /* Let's go recursive */
629         return(pcibr_error_intr_handler(irq, arg, ep));
630     }
631
632     int_status = pcireg_intr_status_get(pcibr_soft);
633
634     PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_INTR_ERROR, pcibr_soft->bs_conn,
635                 "pcibr_error_intr_handler: int_status=0x%lx\n", int_status));
636
637     /* int_status is which bits we have to clear;
638      * err_status is the bits we haven't handled yet.
639      */
640     err_status = int_status;
641
642     if (!(int_status & ~BRIDGE_ISR_INT_MSK)) {
643         /*
644          * No error bit set!!.
645          */
646         return IRQ_HANDLED;
647     }
648     /*
649      * If we have a PCIBUS_PIOERR, hand it to the logger.
650      */
651     if (int_status & BRIDGE_ISR_PCIBUS_PIOERR) {
652         pcibr_pioerr_check(pcibr_soft);
653     }
654
655     if (err_status) {
656         struct bs_errintr_stat_s *bs_estat ;
657         bs_estat = &pcibr_soft->bs_errintr_stat[PCIBR_ISR_ERR_START];
658
659         for (i = PCIBR_ISR_ERR_START; i < 64; i++, bs_estat++) {
660             if (err_status & (1ull << i)) {
661                 uint32_t              errrate = 0;
662                 uint32_t              errcount = 0;
663                 uint32_t              errinterval = 0, current_tick = 0;
664                 int                     llp_tx_retry_errors = 0;
665                 int                     is_llp_tx_retry_intr = 0;
666
667                 bs_estat->bs_errcount_total++;
668
669                 current_tick = jiffies;
670                 errinterval = (current_tick - bs_estat->bs_lasterr_timestamp);
671                 errcount = (bs_estat->bs_errcount_total -
672                             bs_estat->bs_lasterr_snapshot);
673
674                 /* LLP interrrupt errors are only valid on BUS0 of the PIC */
675                 if (pcibr_soft->bs_busnum == 0)
676                     is_llp_tx_retry_intr = (BRIDGE_ISR_LLP_TX_RETRY==(1ull << i));
677
678                 /* Check for the divide by zero condition while
679                  * calculating the error rates.
680                  */
681
682                 if (errinterval) {
683                     errrate = errcount / errinterval;
684                     /* If able to calculate error rate
685                      * on a LLP transmitter retry interrupt, check
686                      * if the error rate is nonzero and we have seen
687                      * a certain minimum number of errors.
688                      *
689                      * NOTE : errcount is being compared to
690                      * PCIBR_ERRTIME_THRESHOLD to make sure that we are not
691                      * seeing cases like x error interrupts per y ticks for
692                      * very low x ,y (x > y ) which could result in a
693                      * rate > 100/tick.
694                      */
695                     if (is_llp_tx_retry_intr &&
696                         errrate &&
697                         (errcount >= PCIBR_ERRTIME_THRESHOLD)) {
698                         llp_tx_retry_errors = 1;
699                     }
700                 } else {
701                     errrate = 0;
702                     /* Since we are not able to calculate the
703                      * error rate check if we exceeded a certain
704                      * minimum number of errors for LLP transmitter
705                      * retries. Note that this can only happen
706                      * within the first tick after the last snapshot.
707                      */
708                     if (is_llp_tx_retry_intr &&
709                         (errcount >= PCIBR_ERRINTR_DISABLE_LEVEL)) {
710                         llp_tx_retry_errors = 1;
711                     }
712                 }
713
714                 /*
715                  * If a non-zero error rate (which is equivalent to
716                  * to 100 errors/tick at least) for the LLP transmitter
717                  * retry interrupt was seen, check if we should print
718                  * a warning message.
719                  */
720
721                 if (llp_tx_retry_errors) {
722                     static uint32_t       last_printed_rate;
723
724                     if (errrate > last_printed_rate) {
725                         last_printed_rate = errrate;
726                         /* Print the warning only if the error rate
727                          * for the transmitter retry interrupt
728                          * exceeded the previously printed rate.
729                          */
730                         printk(KERN_WARNING
731                                 "%s: %s, Excessive error interrupts : %d/tick\n",
732                                 pcibr_soft->bs_name,
733                                 pcibr_isr_errs[i],
734                                 errrate);
735
736                     }
737                     /*
738                      * Update snapshot, and time
739                      */
740                     bs_estat->bs_lasterr_timestamp = current_tick;
741                     bs_estat->bs_lasterr_snapshot =
742                         bs_estat->bs_errcount_total;
743
744                 }
745                 /*
746                  * If the error rate is high enough, print the error rate.
747                  */
748                 if (errinterval > PCIBR_ERRTIME_THRESHOLD) {
749
750                     if (errrate > PCIBR_ERRRATE_THRESHOLD) {
751                         printk(KERN_NOTICE "%s: %s, Error rate %d/tick",
752                                 pcibr_soft->bs_name,
753                                 pcibr_isr_errs[i],
754                                 errrate);
755                         /*
756                          * Update snapshot, and time
757                          */
758                         bs_estat->bs_lasterr_timestamp = current_tick;
759                         bs_estat->bs_lasterr_snapshot =
760                             bs_estat->bs_errcount_total;
761                     }
762                 }
763                 /* PIC BRINGUP WAR (PV# 856155):
764                  * Dont disable PCI_X_ARB_ERR interrupts, we need the
765                  * interrupt inorder to clear the DEV_BROKE bits in
766                  * b_arb register to re-enable the device.
767                  */
768                 if (!(err_status & PIC_ISR_PCIX_ARB_ERR) &&
769                                 PCIBR_WAR_ENABLED(PV856155, pcibr_soft)) {
770
771                 if (bs_estat->bs_errcount_total > PCIBR_ERRINTR_DISABLE_LEVEL) {
772                     /*
773                      * We have seen a fairly large number of errors of
774                      * this type. Let's disable the interrupt. But flash
775                      * a message about the interrupt being disabled.
776                      */
777                     printk(KERN_NOTICE
778                             "%s Disabling error interrupt type %s. Error count %d",
779                             pcibr_soft->bs_name,
780                             pcibr_isr_errs[i],
781                             bs_estat->bs_errcount_total);
782                     disable_errintr_mask |= (1ull << i);
783                 }
784                 } /* PIC: WAR for PV 856155 end-of-if */
785             }
786         }
787     }
788
789     if (disable_errintr_mask) {
790         unsigned long s;
791         /*
792          * Disable some high frequency errors as they
793          * could eat up too much cpu time.
794          */
795         s = pcibr_lock(pcibr_soft);
796         pcireg_intr_enable_bit_clr(pcibr_soft, disable_errintr_mask);
797         pcibr_unlock(pcibr_soft, s);
798     }
799     /*
800      * If we leave the PROM cacheable, T5 might
801      * try to do a cache line sized writeback to it,
802      * which will cause a BRIDGE_ISR_INVLD_ADDR.
803      */
804     if ((err_status & BRIDGE_ISR_INVLD_ADDR) &&
805         (0x00C00000 == (pcireg_bus_err_get(pcibr_soft) & 0xFFFFFFFFFFC00000)) &&
806         (0x00402000 == (0x00F07F00 & pcireg_cmdword_err_get(pcibr_soft)))) {
807         err_status &= ~BRIDGE_ISR_INVLD_ADDR;
808     }
809     /*
810      * pcibr_pioerr_dump is a systune that make be used to not
811      * print bridge registers for interrupts generated by pio-errors.
812      * Some customers do early probes and expect a lot of failed
813      * pios.
814      */
815     if (!pcibr_pioerr_dump) {
816         bridge_errors_to_dump &= ~BRIDGE_ISR_PCIBUS_PIOERR;
817     } else {
818         bridge_errors_to_dump |= BRIDGE_ISR_PCIBUS_PIOERR;
819     }
820
821     /* Dump/Log Bridge error interrupt info */
822     if (err_status & bridge_errors_to_dump) {
823         printk("BRIDGE ERR_STATUS 0x%lx\n", err_status);
824         pcibr_error_dump(pcibr_soft);
825     }
826
827     /* PIC BRINGUP WAR (PV# 867308):
828      * Make BRIDGE_ISR_LLP_REC_SNERR & BRIDGE_ISR_LLP_REC_CBERR fatal errors
829      * so we know we've hit the problem defined in PV 867308 that we believe
830      * has only been seen in simulation
831      */
832     if (PCIBR_WAR_ENABLED(PV867308, pcibr_soft) &&
833         (err_status & (BRIDGE_ISR_LLP_REC_SNERR | BRIDGE_ISR_LLP_REC_CBERR))) {
834         printk("BRIDGE ERR_STATUS 0x%lx\n", err_status);
835         pcibr_error_dump(pcibr_soft);
836         /* machine_error_dump(""); */
837         panic("PCI Bridge Error interrupt killed the system");
838     }
839
840     if (err_status & BRIDGE_ISR_ERROR_FATAL) {
841         panic("PCI Bridge Error interrupt killed the system");
842             /*NOTREACHED */
843     }
844
845
846     /*
847      * We can't return without re-enabling the interrupt, since
848      * it would cause problems for devices like IOC3 (Lost
849      * interrupts ?.). So, just cleanup the interrupt, and
850      * use saved values later..
851      * 
852      * PIC doesn't require groups of interrupts to be cleared...
853      */
854     pcireg_intr_reset_set(pcibr_soft, (int_status | BRIDGE_IRR_MULTI_CLR));
855
856     /* PIC BRINGUP WAR (PV# 856155):
857      * On a PCI_X_ARB_ERR error interrupt clear the DEV_BROKE bits from
858      * the b_arb register to re-enable the device.
859      */
860     if ((err_status & PIC_ISR_PCIX_ARB_ERR) &&
861                 PCIBR_WAR_ENABLED(PV856155, pcibr_soft)) {
862         pcireg_arbitration_bit_set(pcibr_soft, (0xf << 20));
863     }
864
865     /* Zero out bserr_intstat field */
866     pcibr_soft->bs_errinfo.bserr_intstat = 0;
867     return IRQ_HANDLED;
868 }
869
870 /*
871  * pcibr_addr_toslot
872  *      Given the 'pciaddr' find out which slot this address is
873  *      allocated to, and return the slot number.
874  *      While we have the info handy, construct the
875  *      function number, space code and offset as well.
876  *
877  * NOTE: if this routine is called, we don't know whether
878  * the address is in CFG, MEM, or I/O space. We have to guess.
879  * This will be the case on PIO stores, where the only way
880  * we have of getting the address is to check the Bridge, which
881  * stores the PCI address but not the space and not the xtalk
882  * address (from which we could get it).
883  */
884 static int
885 pcibr_addr_toslot(pcibr_soft_t pcibr_soft,
886                   iopaddr_t pciaddr,
887                   pciio_space_t *spacep,
888                   iopaddr_t *offsetp,
889                   pciio_function_t *funcp)
890 {
891     int                     s, f = 0, w;
892     iopaddr_t               base;
893     size_t                  size;
894     pciio_piospace_t        piosp;
895
896     /*
897      * Check if the address is in config space
898      */
899
900     if ((pciaddr >= BRIDGE_CONFIG_BASE) && (pciaddr < BRIDGE_CONFIG_END)) {
901
902         if (pciaddr >= BRIDGE_CONFIG1_BASE)
903             pciaddr -= BRIDGE_CONFIG1_BASE;
904         else
905             pciaddr -= BRIDGE_CONFIG_BASE;
906
907         s = pciaddr / BRIDGE_CONFIG_SLOT_SIZE;
908         pciaddr %= BRIDGE_CONFIG_SLOT_SIZE;
909
910         if (funcp) {
911             f = pciaddr / 0x100;
912             pciaddr %= 0x100;
913         }
914         if (spacep)
915             *spacep = PCIIO_SPACE_CFG;
916         if (offsetp)
917             *offsetp = pciaddr;
918         if (funcp)
919             *funcp = f;
920
921         return s;
922     }
923     for (s = pcibr_soft->bs_min_slot; s < PCIBR_NUM_SLOTS(pcibr_soft); ++s) {
924         int                     nf = pcibr_soft->bs_slot[s].bss_ninfo;
925         pcibr_info_h            pcibr_infoh = pcibr_soft->bs_slot[s].bss_infos;
926
927         for (f = 0; f < nf; f++) {
928             pcibr_info_t            pcibr_info = pcibr_infoh[f];
929
930             if (!pcibr_info)
931                 continue;
932             for (w = 0; w < 6; w++) {
933                 if (pcibr_info->f_window[w].w_space
934                     == PCIIO_SPACE_NONE) {
935                     continue;
936                 }
937                 base = pcibr_info->f_window[w].w_base;
938                 size = pcibr_info->f_window[w].w_size;
939
940                 if ((pciaddr >= base) && (pciaddr < (base + size))) {
941                     if (spacep)
942                         *spacep = PCIIO_SPACE_WIN(w);
943                     if (offsetp)
944                         *offsetp = pciaddr - base;
945                     if (funcp)
946                         *funcp = f;
947                     return s;
948                 }                       /* endif match */
949             }                           /* next window */
950         }                               /* next func */
951     }                                   /* next slot */
952
953     /*
954      * Check if the address was allocated as part of the
955      * pcibr_piospace_alloc calls.
956      */
957     for (s = pcibr_soft->bs_min_slot; s < PCIBR_NUM_SLOTS(pcibr_soft); ++s) {
958         int                     nf = pcibr_soft->bs_slot[s].bss_ninfo;
959         pcibr_info_h            pcibr_infoh = pcibr_soft->bs_slot[s].bss_infos;
960
961         for (f = 0; f < nf; f++) {
962             pcibr_info_t            pcibr_info = pcibr_infoh[f];
963
964             if (!pcibr_info)
965                 continue;
966             piosp = pcibr_info->f_piospace;
967             while (piosp) {
968                 if ((piosp->start <= pciaddr) &&
969                     ((piosp->count + piosp->start) > pciaddr)) {
970                     if (spacep)
971                         *spacep = piosp->space;
972                     if (offsetp)
973                         *offsetp = pciaddr - piosp->start;
974                     return s;
975                 }                       /* endif match */
976                 piosp = piosp->next;
977             }                           /* next piosp */
978         }                               /* next func */
979     }                                   /* next slot */
980
981     /*
982      * Some other random address on the PCI bus ...
983      * we have no way of knowing whether this was
984      * a MEM or I/O access; so, for now, we just
985      * assume that the low 1G is MEM, the next
986      * 3G is I/O, and anything above the 4G limit
987      * is obviously MEM.
988      */
989
990     if (spacep)
991         *spacep = ((pciaddr < (1ul << 30)) ? PCIIO_SPACE_MEM :
992                    (pciaddr < (4ul << 30)) ? PCIIO_SPACE_IO :
993                    PCIIO_SPACE_MEM);
994     if (offsetp)
995         *offsetp = pciaddr;
996
997     return PCIIO_SLOT_NONE;
998
999 }
1000
1001 void
1002 pcibr_error_cleanup(pcibr_soft_t pcibr_soft, int error_code)
1003 {
1004     uint64_t    clr_bits = BRIDGE_IRR_ALL_CLR;
1005
1006     ASSERT(error_code & IOECODE_PIO);
1007     error_code = error_code;
1008
1009     pcireg_intr_reset_set(pcibr_soft, clr_bits);
1010
1011     pcireg_tflush_get(pcibr_soft);      /* flushbus */
1012 }
1013
1014
1015 /*
1016  * pcibr_error_extract
1017  *      Given the 'pcibr vertex handle' find out which slot
1018  *      the bridge status error address (from pcibr_soft info
1019  *      hanging off the vertex)
1020  *      allocated to, and return the slot number.
1021  *      While we have the info handy, construct the
1022  *      space code and offset as well.
1023  *
1024  * NOTE: if this routine is called, we don't know whether
1025  * the address is in CFG, MEM, or I/O space. We have to guess.
1026  * This will be the case on PIO stores, where the only way
1027  * we have of getting the address is to check the Bridge, which
1028  * stores the PCI address but not the space and not the xtalk
1029  * address (from which we could get it).
1030  *
1031  * XXX- this interface has no way to return the function
1032  * number on a multifunction card, even though that data
1033  * is available.
1034  */
1035
1036 pciio_slot_t
1037 pcibr_error_extract(vertex_hdl_t pcibr_vhdl,
1038                     pciio_space_t *spacep,
1039                     iopaddr_t *offsetp)
1040 {
1041     pcibr_soft_t            pcibr_soft = 0;
1042     iopaddr_t               bserr_addr;
1043     pciio_slot_t            slot = PCIIO_SLOT_NONE;
1044     arbitrary_info_t        rev;
1045
1046     /* Do a sanity check as to whether we really got a 
1047      * bridge vertex handle.
1048      */
1049     if (hwgraph_info_get_LBL(pcibr_vhdl, INFO_LBL_PCIBR_ASIC_REV, &rev) !=
1050         GRAPH_SUCCESS) 
1051         return(slot);
1052
1053     pcibr_soft = pcibr_soft_get(pcibr_vhdl);
1054     if (pcibr_soft) {
1055         bserr_addr = pcireg_pci_bus_addr_get(pcibr_soft);
1056         slot = pcibr_addr_toslot(pcibr_soft, bserr_addr,
1057                                  spacep, offsetp, NULL);
1058     }
1059     return slot;
1060 }
1061
1062 /*ARGSUSED */
1063 void
1064 pcibr_device_disable(pcibr_soft_t pcibr_soft, int devnum)
1065 {
1066     /*
1067      * XXX
1068      * Device failed to handle error. Take steps to
1069      * disable this device ? HOW TO DO IT ?
1070      *
1071      * If there are any Read response buffers associated
1072      * with this device, it's time to get them back!!
1073      *
1074      * We can disassociate any interrupt level associated
1075      * with this device, and disable that interrupt level
1076      *
1077      * For now it's just a place holder
1078      */
1079 }
1080
1081 /*
1082  * pcibr_pioerror
1083  *      Handle PIO error that happened at the bridge pointed by pcibr_soft.
1084  *
1085  *      Queries the Bus interface attached to see if the device driver
1086  *      mapping the device-number that caused error can handle the
1087  *      situation. If so, it will clean up any error, and return
1088  *      indicating the error was handled. If the device driver is unable
1089  *      to handle the error, it expects the bus-interface to disable that
1090  *      device, and takes any steps needed here to take away any resources
1091  *      associated with this device.
1092  *
1093  * A note about slots:
1094  *
1095  *      PIC-based bridges use zero-based device numbering when devices to
1096  *      internal registers.  However, the physical slots are numbered using a
1097  *      one-based scheme because in PCI-X, device 0 is reserved (see comments
1098  *      in pcibr_private.h for a better description).
1099  *
1100  *      When building up the hwgraph, we use the external (one-based) number
1101  *      scheme when numbering slot components so that hwgraph more accuratly
1102  *      reflects what is silkscreened on the bricks.
1103  *
1104  *      Since pciio_error_handler() needs to ultimatly be able to do a hwgraph
1105  *      lookup, the ioerror that gets built up in pcibr_pioerror() encodes the
1106  *      external (one-based) slot number.  However, loops in pcibr_pioerror() 
1107  *      which attempt to translate the virtual address into the correct
1108  *      PCI physical address use the device (zero-based) numbering when 
1109  *      walking through bridge structures.
1110  *
1111  *      To that end, pcibr_pioerror() uses device to denote the 
1112  *      zero-based device number, and external_slot to denote the corresponding
1113  *      one-based slot number.  Loop counters (eg. cs) are always device based.
1114  */
1115
1116 /* BEM_ADD_IOE doesn't dump the whole ioerror, it just
1117  * decodes the PCI specific portions -- we count on our
1118  * callers to dump the raw IOE data.
1119  */
1120 #define BEM_ADD_IOE(ioe)                                                \
1121         do {                                                            \
1122             if (IOERROR_FIELDVALID(ioe, busspace)) {                    \
1123                 iopaddr_t               spc;                            \
1124                 iopaddr_t               win;                            \
1125                 short                   widdev;                         \
1126                 iopaddr_t               busaddr;                        \
1127                                                                         \
1128                 IOERROR_GETVALUE(spc, ioe, busspace);                   \
1129                 win = spc - PCIIO_SPACE_WIN(0);                         \
1130                 IOERROR_GETVALUE(busaddr, ioe, busaddr);                \
1131                 IOERROR_GETVALUE(widdev, ioe, widgetdev);               \
1132                                                                         \
1133                 switch (spc) {                                          \
1134                 case PCIIO_SPACE_CFG:                                   \
1135                     printk("\tPCI Slot %d Func %d CFG space Offset 0x%lx\n",\
1136                                 pciio_widgetdev_slot_get(widdev),       \
1137                                 pciio_widgetdev_func_get(widdev),       \
1138                                 busaddr);                               \
1139                     break;                                              \
1140                 case PCIIO_SPACE_IO:                                    \
1141                     printk("\tPCI I/O space  Offset 0x%lx\n", busaddr); \
1142                     break;                                              \
1143                 case PCIIO_SPACE_MEM:                                   \
1144                 case PCIIO_SPACE_MEM32:                                 \
1145                 case PCIIO_SPACE_MEM64:                                 \
1146                     printk("\tPCI MEM space Offset 0x%lx\n", busaddr);  \
1147                     break;                                              \
1148                 default:                                                \
1149                     if (win < 6) {                                      \
1150                     printk("\tPCI Slot %d Func %d Window %ld Offset 0x%lx\n",\
1151                                 pciio_widgetdev_slot_get(widdev),       \
1152                                 pciio_widgetdev_func_get(widdev),       \
1153                                 win,                                    \
1154                                 busaddr);                               \
1155                     }                                                   \
1156                     break;                                              \
1157                 }                                                       \
1158             }                                                           \
1159         } while (0)
1160
1161 /*ARGSUSED */
1162 int
1163 pcibr_pioerror(
1164                   pcibr_soft_t pcibr_soft,
1165                   int error_code,
1166                   ioerror_mode_t mode,
1167                   ioerror_t *ioe)
1168 {
1169     int                     retval = IOERROR_HANDLED;
1170
1171     vertex_hdl_t            pcibr_vhdl = pcibr_soft->bs_vhdl;
1172     iopaddr_t               bad_xaddr;
1173
1174     pciio_space_t           raw_space;  /* raw PCI space */
1175     iopaddr_t               raw_paddr;  /* raw PCI address */
1176
1177     pciio_space_t           space;      /* final PCI space */
1178     pciio_slot_t            device;     /* final PCI device if appropriate */
1179     pciio_slot_t            external_slot;/* external slot for device */
1180     pciio_function_t        func;       /* final PCI func, if appropriate */
1181     iopaddr_t               offset;     /* final PCI offset */
1182     
1183     int                     cs, cw, cf;
1184     pciio_space_t           wx;
1185     iopaddr_t               wb;
1186     size_t                  ws;
1187     iopaddr_t               wl;
1188
1189
1190     /*
1191      * We expect to have an "xtalkaddr" coming in,
1192      * and need to construct the slot/space/offset.
1193      */
1194
1195     IOERROR_GETVALUE(bad_xaddr, ioe, xtalkaddr);
1196
1197     PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_ERROR_HDLR, pcibr_soft->bs_conn,
1198                 "pcibr_pioerror: pcibr_soft=0x%lx, bad_xaddr=0x%lx\n",
1199                 pcibr_soft, bad_xaddr));
1200
1201     device = PCIIO_SLOT_NONE;
1202     func = PCIIO_FUNC_NONE;
1203     raw_space = PCIIO_SPACE_NONE;
1204     raw_paddr = 0;
1205
1206     if ((bad_xaddr >= PCIBR_BUS_TYPE0_CFG_DEV(pcibr_soft, 0)) &&
1207         (bad_xaddr < PCIBR_TYPE1_CFG(pcibr_soft))) {
1208         raw_paddr = bad_xaddr - PCIBR_BUS_TYPE0_CFG_DEV(pcibr_soft, 0);
1209         device = raw_paddr / BRIDGE_CONFIG_SLOT_SIZE;
1210         raw_paddr = raw_paddr % BRIDGE_CONFIG_SLOT_SIZE;
1211         raw_space = PCIIO_SPACE_CFG;
1212     }
1213     if ((bad_xaddr >= PCIBR_TYPE1_CFG(pcibr_soft)) &&
1214         (bad_xaddr < (PCIBR_TYPE1_CFG(pcibr_soft) + 0x1000))) {
1215         /* Type 1 config space:
1216          * slot and function numbers not known.
1217          * Perhaps we can read them back?
1218          */
1219         raw_paddr = bad_xaddr - PCIBR_TYPE1_CFG(pcibr_soft);
1220         raw_space = PCIIO_SPACE_CFG;
1221     }
1222     if ((bad_xaddr >= PCIBR_BRIDGE_DEVIO(pcibr_soft, 0)) &&
1223         (bad_xaddr < PCIBR_BRIDGE_DEVIO(pcibr_soft, BRIDGE_DEV_CNT))) {
1224         int                     x;
1225
1226         raw_paddr = bad_xaddr - PCIBR_BRIDGE_DEVIO(pcibr_soft, 0);
1227         x = raw_paddr / BRIDGE_DEVIO_OFF;
1228         raw_paddr %= BRIDGE_DEVIO_OFF;
1229         /* first two devio windows are double-sized */
1230         if ((x == 1) || (x == 3))
1231             raw_paddr += BRIDGE_DEVIO_OFF;
1232         if (x > 0)
1233             x--;
1234         if (x > 1)
1235             x--;
1236         /* x is which devio reg; no guarantee
1237          * PCI slot x will be responding.
1238          * still need to figure out who decodes
1239          * space/offset on the bus.
1240          */
1241         raw_space = pcibr_soft->bs_slot[x].bss_devio.bssd_space;
1242         if (raw_space == PCIIO_SPACE_NONE) {
1243             /* Someone got an error because they
1244              * accessed the PCI bus via a DevIO(x)
1245              * window that pcibr has not yet assigned
1246              * to any specific PCI address. It is
1247              * quite possible that the Device(x)
1248              * register has been changed since they
1249              * made their access, but we will give it
1250              * our best decode shot.
1251              */
1252             raw_space = pcibr_soft->bs_slot[x].bss_device
1253                 & BRIDGE_DEV_DEV_IO_MEM
1254                 ? PCIIO_SPACE_MEM
1255                 : PCIIO_SPACE_IO;
1256             raw_paddr +=
1257                 (pcibr_soft->bs_slot[x].bss_device &
1258                  BRIDGE_DEV_OFF_MASK) <<
1259                 BRIDGE_DEV_OFF_ADDR_SHFT;
1260         } else
1261             raw_paddr += pcibr_soft->bs_slot[x].bss_devio.bssd_base;
1262     }
1263
1264     if (IS_PIC_BUSNUM_SOFT(pcibr_soft, 0)) {
1265         if ((bad_xaddr >= PICBRIDGE0_PCI_MEM32_BASE) &&
1266             (bad_xaddr <= PICBRIDGE0_PCI_MEM32_LIMIT)) {
1267             raw_space = PCIIO_SPACE_MEM32;
1268             raw_paddr = bad_xaddr - PICBRIDGE0_PCI_MEM32_BASE;
1269         }
1270         if ((bad_xaddr >= PICBRIDGE0_PCI_MEM64_BASE) &&
1271             (bad_xaddr <= PICBRIDGE0_PCI_MEM64_LIMIT)) {
1272             raw_space = PCIIO_SPACE_MEM64;
1273             raw_paddr = bad_xaddr - PICBRIDGE0_PCI_MEM64_BASE;
1274         }
1275     } else if (IS_PIC_BUSNUM_SOFT(pcibr_soft, 1)) {
1276         if ((bad_xaddr >= PICBRIDGE1_PCI_MEM32_BASE) &&
1277             (bad_xaddr <= PICBRIDGE1_PCI_MEM32_LIMIT)) {
1278             raw_space = PCIIO_SPACE_MEM32;
1279             raw_paddr = bad_xaddr - PICBRIDGE1_PCI_MEM32_BASE;
1280         }
1281         if ((bad_xaddr >= PICBRIDGE1_PCI_MEM64_BASE) &&
1282             (bad_xaddr <= PICBRIDGE1_PCI_MEM64_LIMIT)) {
1283             raw_space = PCIIO_SPACE_MEM64;
1284             raw_paddr = bad_xaddr - PICBRIDGE1_PCI_MEM64_BASE;
1285         }
1286     } else {
1287         printk("pcibr_pioerror(): unknown bridge type");
1288         return IOERROR_UNHANDLED;
1289     }
1290     space = raw_space;
1291     offset = raw_paddr;
1292
1293     if ((device == PCIIO_SLOT_NONE) && (space != PCIIO_SPACE_NONE)) {
1294         /* we've got a space/offset but not which
1295          * PCI slot decodes it. Check through our
1296          * notions of which devices decode where.
1297          *
1298          * Yes, this "duplicates" some logic in
1299          * pcibr_addr_toslot; the difference is,
1300          * this code knows which space we are in,
1301          * and can really really tell what is
1302          * going on (no guessing).
1303          */
1304
1305         for (cs = pcibr_soft->bs_min_slot; 
1306                 (cs < PCIBR_NUM_SLOTS(pcibr_soft)) && 
1307                                 (device == PCIIO_SLOT_NONE); cs++) {
1308             int                     nf = pcibr_soft->bs_slot[cs].bss_ninfo;
1309             pcibr_info_h            pcibr_infoh = pcibr_soft->bs_slot[cs].bss_infos;
1310
1311             for (cf = 0; (cf < nf) && (device == PCIIO_SLOT_NONE); cf++) {
1312                 pcibr_info_t            pcibr_info = pcibr_infoh[cf];
1313
1314                 if (!pcibr_info)
1315                     continue;
1316                 for (cw = 0; (cw < 6) && (device == PCIIO_SLOT_NONE); ++cw) {
1317                     if (((wx = pcibr_info->f_window[cw].w_space) != PCIIO_SPACE_NONE) &&
1318                         ((wb = pcibr_info->f_window[cw].w_base) != 0) &&
1319                         ((ws = pcibr_info->f_window[cw].w_size) != 0) &&
1320                         ((wl = wb + ws) > wb) &&
1321                         ((wb <= offset) && (wl > offset))) {
1322                         /* MEM, MEM32 and MEM64 need to
1323                          * compare as equal ...
1324                          */
1325                         if ((wx == space) ||
1326                             (((wx == PCIIO_SPACE_MEM) ||
1327                               (wx == PCIIO_SPACE_MEM32) ||
1328                               (wx == PCIIO_SPACE_MEM64)) &&
1329                              ((space == PCIIO_SPACE_MEM) ||
1330                               (space == PCIIO_SPACE_MEM32) ||
1331                               (space == PCIIO_SPACE_MEM64)))) {
1332                             device = cs;
1333                             func = cf;
1334                             space = PCIIO_SPACE_WIN(cw);
1335                             offset -= wb;
1336                         }               /* endif window space match */
1337                     }                   /* endif window valid and addr match */
1338                 }                       /* next window unless slot set */
1339             }                           /* next func unless slot set */
1340         }                               /* next slot unless slot set */
1341         /* XXX- if slot is still -1, no PCI devices are
1342          * decoding here using their standard PCI BASE
1343          * registers. This would be a really good place
1344          * to cross-coordinate with the pciio PCI
1345          * address space allocation routines, to find
1346          * out if this address is "allocated" by any of
1347          * our subsidiary devices.
1348          */
1349     }
1350     /* Scan all piomap records on this PCI bus to update
1351      * the TimeOut Counters on all matching maps. If we
1352      * don't already know the slot number, take it from
1353      * the first matching piomap. Note that we have to
1354      * compare maps against raw_space and raw_paddr
1355      * since space and offset could already be
1356      * window-relative.
1357      *
1358      * There is a chance that one CPU could update
1359      * through this path, and another CPU could also
1360      * update due to an interrupt. Closing this hole
1361      * would only result in the possibility of some
1362      * errors never getting logged at all, and since the
1363      * use for bp_toc is as a logical test rather than a
1364      * strict count, the excess counts are not a
1365      * problem.
1366      */
1367     for (cs = pcibr_soft->bs_min_slot; 
1368                                 cs < PCIBR_NUM_SLOTS(pcibr_soft); ++cs) {
1369         int             nf = pcibr_soft->bs_slot[cs].bss_ninfo;
1370         pcibr_info_h    pcibr_infoh = pcibr_soft->bs_slot[cs].bss_infos;
1371
1372         for (cf = 0; cf < nf; cf++) {
1373             pcibr_info_t        pcibr_info = pcibr_infoh[cf];
1374             pcibr_piomap_t      map;    
1375
1376             if (!pcibr_info)
1377                 continue;
1378
1379             for (map = pcibr_info->f_piomap;
1380              map != NULL; map = map->bp_next) {
1381             wx = map->bp_space;
1382             wb = map->bp_pciaddr;
1383             ws = map->bp_mapsz;
1384             cw = wx - PCIIO_SPACE_WIN(0);
1385             if (cw >= 0 && cw < 6) {
1386                 wb += pcibr_soft->bs_slot[cs].bss_window[cw].bssw_base;
1387                 wx = pcibr_soft->bs_slot[cs].bss_window[cw].bssw_space;
1388             }
1389             if (wx == PCIIO_SPACE_ROM) {
1390                 wb += pcibr_info->f_rbase;
1391                 wx = PCIIO_SPACE_MEM;
1392             }
1393             if ((wx == PCIIO_SPACE_MEM32) ||
1394                 (wx == PCIIO_SPACE_MEM64))
1395                 wx = PCIIO_SPACE_MEM;
1396             wl = wb + ws;
1397             if ((wx == raw_space) && (raw_paddr >= wb) && (raw_paddr < wl)) {
1398                 atomic_inc(&map->bp_toc);
1399                 if (device == PCIIO_SLOT_NONE) {
1400                     device = cs;
1401                     func = cf;
1402                     space = map->bp_space;
1403                     if (cw >= 0 && cw < 6)
1404                         offset -= pcibr_soft->bs_slot[device].bss_window[cw].bssw_base;
1405                 }
1406
1407                 break;
1408             }
1409             }
1410         }
1411     }
1412
1413     PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_ERROR_HDLR, pcibr_soft->bs_conn,
1414                 "pcibr_pioerror: space=%d, offset=0x%lx, dev=0x%x, func=0x%x\n",
1415                 space, offset, device, func));
1416
1417     if (space != PCIIO_SPACE_NONE) {
1418         if (device != PCIIO_SLOT_NONE)  {
1419             external_slot = PCIBR_DEVICE_TO_SLOT(pcibr_soft, device);
1420
1421             if (func != PCIIO_FUNC_NONE)
1422                 IOERROR_SETVALUE(ioe, widgetdev, 
1423                                  pciio_widgetdev_create(external_slot,func));
1424             else
1425                 IOERROR_SETVALUE(ioe, widgetdev, 
1426                                  pciio_widgetdev_create(external_slot,0));
1427         }
1428         IOERROR_SETVALUE(ioe, busspace, space);
1429         IOERROR_SETVALUE(ioe, busaddr, offset);
1430     }
1431     if (mode == MODE_DEVPROBE) {
1432         /*
1433          * During probing, we don't really care what the
1434          * error is. Clean up the error in Bridge, notify
1435          * subsidiary devices, and return success.
1436          */
1437         pcibr_error_cleanup(pcibr_soft, error_code);
1438
1439         /* if appropriate, give the error handler for this slot
1440          * a shot at this probe access as well.
1441          */
1442         return (device == PCIIO_SLOT_NONE) ? IOERROR_HANDLED :
1443             pciio_error_handler(pcibr_vhdl, error_code, mode, ioe);
1444     }
1445     /*
1446      * If we don't know what "PCI SPACE" the access
1447      * was targeting, we may have problems at the
1448      * Bridge itself. Don't touch any bridge registers,
1449      * and do complain loudly.
1450      */
1451
1452     if (space == PCIIO_SPACE_NONE) {
1453         printk("XIO Bus Error at %s\n"
1454                 "\taccess to XIO bus offset 0x%lx\n"
1455                 "\tdoes not correspond to any PCI address\n",
1456                 pcibr_soft->bs_name, bad_xaddr);
1457
1458         /* caller will dump contents of ioe struct */
1459         return IOERROR_XTALKLEVEL;
1460     }
1461
1462     /*
1463      * Actual PCI Error handling situation.
1464      * Typically happens when a user level process accesses
1465      * PCI space, and it causes some error.
1466      *
1467      * Due to PCI Bridge implementation, we get two indication
1468      * for a read error: an interrupt and a Bus error.
1469      * We like to handle read error in the bus error context.
1470      * But the interrupt comes and goes before bus error
1471      * could make much progress. (NOTE: interrupd does
1472      * come in _after_ bus error processing starts. But it's
1473      * completed by the time bus error code reaches PCI PIO
1474      * error handling.
1475      * Similarly write error results in just an interrupt,
1476      * and error handling has to be done at interrupt level.
1477      * There is no way to distinguish at interrupt time, if an
1478      * error interrupt is due to read/write error..
1479      */
1480
1481     /* We know the xtalk addr, the raw PCI bus space,
1482      * the raw PCI bus address, the decoded PCI bus
1483      * space, the offset within that space, and the
1484      * decoded PCI slot (which may be "PCIIO_SLOT_NONE" if no slot
1485      * is known to be involved).
1486      */
1487
1488     /*
1489      * Hand the error off to the handler registered
1490      * for the slot that should have decoded the error,
1491      * or to generic PCI handling (if pciio decides that
1492      * such is appropriate).
1493      */
1494     retval = pciio_error_handler(pcibr_vhdl, error_code, mode, ioe);
1495
1496     if (retval != IOERROR_HANDLED) {
1497
1498         /* Generate a generic message for IOERROR_UNHANDLED
1499          * since the subsidiary handlers were silent, and
1500          * did no recovery.
1501          */
1502         if (retval == IOERROR_UNHANDLED) {
1503             retval = IOERROR_PANIC;
1504
1505             /* we may or may not want to print some of this,
1506              * depending on debug level and which error code.
1507              */
1508
1509             printk(KERN_ALERT
1510                     "PIO Error on PCI Bus %s",
1511                     pcibr_soft->bs_name);
1512             BEM_ADD_IOE(ioe);
1513         }
1514
1515         /*
1516          * Since error could not be handled at lower level,
1517          * error data logged has not  been cleared.
1518          * Clean up errors, and
1519          * re-enable bridge to interrupt on error conditions.
1520          * NOTE: Wheather we get the interrupt on PCI_ABORT or not is
1521          * dependent on INT_ENABLE register. This write just makes sure
1522          * that if the interrupt was enabled, we do get the interrupt.
1523          *
1524          * CAUTION: Resetting bit BRIDGE_IRR_PCI_GRP_CLR, acknowledges
1525          *      a group of interrupts. If while handling this error,
1526          *      some other error has occurred, that would be
1527          *      implicitly cleared by this write.
1528          *      Need a way to ensure we don't inadvertently clear some
1529          *      other errors.
1530          */
1531         if (IOERROR_FIELDVALID(ioe, widgetdev)) {
1532             short widdev;
1533             IOERROR_GETVALUE(widdev, ioe, widgetdev);
1534             external_slot = pciio_widgetdev_slot_get(widdev);
1535             device = PCIBR_SLOT_TO_DEVICE(pcibr_soft, external_slot);
1536             pcibr_device_disable(pcibr_soft, device);
1537         }
1538         if (mode == MODE_DEVUSERERROR)
1539             pcibr_error_cleanup(pcibr_soft, error_code);
1540     }
1541     return retval;
1542 }
1543
1544 /*
1545  * bridge_dmaerror
1546  *      Some error was identified in a DMA transaction.
1547  *      This routine will identify the <device, address> that caused the error,
1548  *      and try to invoke the appropriate bus service to handle this.
1549  */
1550
1551 int
1552 pcibr_dmard_error(
1553                      pcibr_soft_t pcibr_soft,
1554                      int error_code,
1555                      ioerror_mode_t mode,
1556                      ioerror_t *ioe)
1557 {
1558     vertex_hdl_t            pcibr_vhdl = pcibr_soft->bs_vhdl;
1559     int                     retval = 0;
1560     int                     bufnum, device;
1561
1562     /*
1563      * In case of DMA errors, bridge should have logged the
1564      * address that caused the error.
1565      * Look up the address, in the bridge error registers, and
1566      * take appropriate action
1567      */
1568     {
1569         short tmp;
1570         IOERROR_GETVALUE(tmp, ioe, widgetnum);
1571         ASSERT(tmp == pcibr_soft->bs_xid);
1572     }
1573
1574     /*
1575      * read error log registers
1576      */
1577     bufnum = pcireg_resp_err_buf_get(pcibr_soft);
1578     device = pcireg_resp_err_dev_get(pcibr_soft);
1579     IOERROR_SETVALUE(ioe, widgetdev, pciio_widgetdev_create(device, 0));
1580     IOERROR_SETVALUE(ioe, busaddr, pcireg_resp_err_get(pcibr_soft));
1581
1582     /*
1583      * need to ensure that the xtalk address in ioe
1584      * maps to PCI error address read from bridge.
1585      * How to convert PCI address back to Xtalk address ?
1586      * (better idea: convert XTalk address to PCI address
1587      * and then do the compare!)
1588      */
1589
1590     retval = pciio_error_handler(pcibr_vhdl, error_code, mode, ioe);
1591     if (retval != IOERROR_HANDLED) {
1592         short tmp;
1593         IOERROR_GETVALUE(tmp, ioe, widgetdev);
1594         pcibr_device_disable(pcibr_soft, pciio_widgetdev_slot_get(tmp));
1595     }
1596
1597     /*
1598      * Re-enable bridge to interrupt on BRIDGE_IRR_RESP_BUF_GRP_CLR
1599      * NOTE: Wheather we get the interrupt on BRIDGE_IRR_RESP_BUF_GRP_CLR or
1600      * not is dependent on INT_ENABLE register. This write just makes sure
1601      * that if the interrupt was enabled, we do get the interrupt.
1602      */
1603     pcireg_intr_reset_set(pcibr_soft, BRIDGE_IRR_RESP_BUF_GRP_CLR);
1604
1605     /*
1606      * Also, release the "bufnum" back to buffer pool that could be re-used.
1607      * This is done by "disabling" the buffer for a moment, then restoring
1608      * the original assignment.
1609      */
1610
1611     {
1612         uint64_t                rrb_reg;
1613         uint64_t                mask;
1614
1615         rrb_reg = pcireg_rrb_get(pcibr_soft, (bufnum & 1));
1616         mask = 0xF << ((bufnum >> 1) * 4);
1617         pcireg_rrb_set(pcibr_soft, (bufnum & 1), (rrb_reg & ~mask));
1618         pcireg_rrb_set(pcibr_soft, (bufnum & 1), rrb_reg);
1619     }
1620
1621     return retval;
1622 }
1623
1624 /*
1625  * pcibr_dmawr_error:
1626  *      Handle a dma write error caused by a device attached to this bridge.
1627  *
1628  *      ioe has the widgetnum, widgetdev, and memaddr fields updated
1629  *      But we don't know the PCI address that corresponds to "memaddr"
1630  *      nor do we know which device driver is generating this address.
1631  *
1632  *      There is no easy way to find out the PCI address(es) that map
1633  *      to a specific system memory address. Bus handling code is also
1634  *      of not much help, since they don't keep track of the DMA mapping
1635  *      that have been handed out.
1636  *      So it's a dead-end at this time.
1637  *
1638  *      If translation is available, we could invoke the error handling
1639  *      interface of the device driver.
1640  */
1641 /*ARGSUSED */
1642 int
1643 pcibr_dmawr_error(
1644                      pcibr_soft_t pcibr_soft,
1645                      int error_code,
1646                      ioerror_mode_t mode,
1647                      ioerror_t *ioe)
1648 {
1649     vertex_hdl_t            pcibr_vhdl = pcibr_soft->bs_vhdl;
1650     int                     retval;
1651
1652     retval = pciio_error_handler(pcibr_vhdl, error_code, mode, ioe);
1653
1654     if (retval != IOERROR_HANDLED) {
1655         short tmp;
1656
1657         IOERROR_GETVALUE(tmp, ioe, widgetdev);
1658         pcibr_device_disable(pcibr_soft, pciio_widgetdev_slot_get(tmp));
1659     }
1660     return retval;
1661 }
1662
1663 /*
1664  * Bridge error handler.
1665  *      Interface to handle all errors that involve bridge in some way.
1666  *
1667  *      This normally gets called from xtalk error handler.
1668  *      ioe has different set of fields set depending on the error that
1669  *      was encountered. So, we have a bit field indicating which of the
1670  *      fields are valid.
1671  *
1672  * NOTE: This routine could be operating in interrupt context. So,
1673  *      don't try to sleep here (till interrupt threads work!!)
1674  */
1675 int
1676 pcibr_error_handler(
1677                        error_handler_arg_t einfo,
1678                        int error_code,
1679                        ioerror_mode_t mode,
1680                        ioerror_t *ioe)
1681 {
1682     pcibr_soft_t            pcibr_soft;
1683     int                     retval = IOERROR_BADERRORCODE;
1684
1685     pcibr_soft = (pcibr_soft_t) einfo;
1686
1687     PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_ERROR_HDLR, pcibr_soft->bs_conn,
1688                 "pcibr_error_handler: pcibr_soft=0x%lx, error_code=0x%x\n",
1689                 pcibr_soft, error_code));
1690
1691 #if DEBUG && ERROR_DEBUG
1692     printk( "%s: pcibr_error_handler\n", pcibr_soft->bs_name);
1693 #endif
1694
1695     ASSERT(pcibr_soft != NULL);
1696
1697     if (error_code & IOECODE_PIO)
1698         retval = pcibr_pioerror(pcibr_soft, error_code, mode, ioe);
1699
1700     if (error_code & IOECODE_DMA) {
1701         if (error_code & IOECODE_READ) {
1702             /*
1703              * DMA read error occurs when a device attached to the bridge
1704              * tries to read some data from system memory, and this
1705              * either results in a timeout or access error.
1706              * First case is indicated by the bit "XREAD_REQ_TOUT"
1707              * and second case by "RESP_XTALK_ERROR" bit in bridge error
1708              * interrupt status register.
1709              *
1710              * pcibr_error_intr_handler would get invoked first, and it has
1711              * the responsibility of calling pcibr_error_handler with
1712              * suitable parameters.
1713              */
1714
1715             retval = pcibr_dmard_error(pcibr_soft, error_code, MODE_DEVERROR, ioe);
1716         }
1717         if (error_code & IOECODE_WRITE) {
1718             /*
1719              * A device attached to this bridge has been generating
1720              * bad DMA writes. Find out the device attached, and
1721              * slap on it's wrist.
1722              */
1723
1724             retval = pcibr_dmawr_error(pcibr_soft, error_code, MODE_DEVERROR, ioe);
1725         }
1726     }
1727     return retval;
1728
1729 }
1730
1731 /*
1732  * PIC has 2 busses under a single widget so pcibr_attach2 registers this
1733  * wrapper function rather than pcibr_error_handler() for PIC.  It's upto
1734  * this wrapper to call pcibr_error_handler() with the correct pcibr_soft
1735  * struct (ie. the pcibr_soft struct for the bus that saw the error).
1736  *
1737  * NOTE: this wrapper function is only registered for PIC ASICs and will
1738  * only be called for a PIC
1739  */
1740 int
1741 pcibr_error_handler_wrapper(
1742                        error_handler_arg_t einfo,
1743                        int error_code,
1744                        ioerror_mode_t mode,
1745                        ioerror_t *ioe)
1746 {
1747     pcibr_soft_t       pcibr_soft = (pcibr_soft_t) einfo;
1748     int                pio_retval = -1; 
1749     int                dma_retval = -1;
1750
1751     PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_ERROR_HDLR, pcibr_soft->bs_conn,
1752                 "pcibr_error_handler_wrapper: pcibr_soft=0x%lx, "
1753                 "error_code=0x%x\n", pcibr_soft, error_code));
1754
1755     /*
1756      * It is possible that both a IOECODE_PIO and a IOECODE_DMA, and both
1757      * IOECODE_READ and IOECODE_WRITE could be set in error_code so we must
1758      * process all.  Since we are a wrapper for pcibr_error_handler(), and
1759      * will be calling it several times within this routine, we turn off the
1760      * error_code bits we don't want it to be processing during that call.
1761      */
1762     /* 
1763      * If the error was a result of a PIO, we tell what bus on the PIC saw
1764      * the error from the PIO address.
1765      */
1766
1767     if (error_code & IOECODE_PIO) {
1768         iopaddr_t               bad_xaddr;
1769         /*
1770          * PIC bus0 PIO space 0x000000 - 0x7fffff or 0x40000000 - 0xbfffffff
1771          *     bus1 PIO space 0x800000 - 0xffffff or 0xc0000000 - 0x13fffffff
1772          */
1773         IOERROR_GETVALUE(bad_xaddr, ioe, xtalkaddr);
1774         if ((bad_xaddr <= 0x7fffff) ||
1775             ((bad_xaddr >= 0x40000000) && (bad_xaddr <= 0xbfffffff))) {
1776             /* bus 0 saw the error */
1777             pio_retval = pcibr_error_handler((error_handler_arg_t)pcibr_soft,
1778                          (error_code & ~IOECODE_DMA), mode, ioe);
1779         } else if (((bad_xaddr >= 0x800000) && (bad_xaddr <= 0xffffff)) ||
1780             ((bad_xaddr >= 0xc0000000) && (bad_xaddr <= 0x13fffffff))) {
1781             /* bus 1 saw the error */
1782             pcibr_soft = pcibr_soft->bs_peers_soft;
1783             if (!pcibr_soft) {
1784 #if DEBUG
1785                 printk(KERN_WARNING "pcibr_error_handler: "
1786                         "bs_peers_soft==NULL. bad_xaddr= 0x%lx mode= 0x%lx\n",
1787                                                 bad_xaddr, mode);
1788 #endif
1789                 pio_retval = IOERROR_HANDLED;
1790             } else
1791                 pio_retval= pcibr_error_handler((error_handler_arg_t)pcibr_soft,
1792                          (error_code & ~IOECODE_DMA), mode, ioe);
1793         } else {
1794             printk(KERN_WARNING "pcibr_error_handler_wrapper(): IOECODE_PIO: "
1795                     "saw an invalid pio address: 0x%lx\n", bad_xaddr);
1796             pio_retval = IOERROR_UNHANDLED;
1797         }
1798     } 
1799
1800     /* 
1801      * If the error was a result of a DMA Write, we tell what bus on the PIC
1802      * saw the error by looking at tnum.
1803      */
1804     if ((error_code & IOECODE_DMA) && (error_code & IOECODE_WRITE)) {
1805         short tmp;
1806         /*
1807          * For DMA writes [X]Bridge encodes the TNUM field of a Xtalk
1808          * packet like this:
1809          *              bits  value
1810          *              4:3   10b
1811          *              2:0   device number
1812          *
1813          * BUT PIC needs the bus number so it does this:
1814          *              bits  value
1815          *              4:3   10b
1816          *              2     busnumber
1817          *              1:0   device number
1818          *
1819          * Pull out the bus number from `tnum' and reset the `widgetdev'
1820          * since when hubiio_crb_error_handler() set `widgetdev' it had
1821          * no idea if it was a PIC or a BRIDGE ASIC so it set it based
1822          * off bits 2:0
1823          */
1824         IOERROR_GETVALUE(tmp, ioe, tnum);
1825         IOERROR_SETVALUE(ioe, widgetdev, (tmp & 0x3));
1826         if ((tmp & 0x4) == 0) {
1827             /* bus 0 saw the error. */
1828             dma_retval = pcibr_error_handler((error_handler_arg_t)pcibr_soft,
1829                          (error_code & ~(IOECODE_PIO|IOECODE_READ)), mode, ioe);
1830         } else {
1831             /* bus 1 saw the error */
1832             pcibr_soft = pcibr_soft->bs_peers_soft;
1833             dma_retval = pcibr_error_handler((error_handler_arg_t)pcibr_soft,
1834                          (error_code & ~(IOECODE_PIO|IOECODE_READ)), mode, ioe);
1835         }
1836     } 
1837     
1838     /* 
1839      * If the error was a result of a DMA READ, XXX ???
1840      */
1841     if ((error_code & IOECODE_DMA) && (error_code & IOECODE_READ)) {
1842         /*
1843          * A DMA Read error will result in a BRIDGE_ISR_RESP_XTLK_ERR
1844          * or BRIDGE_ISR_BAD_XRESP_PKT bridge error interrupt which 
1845          * are fatal interrupts (ie. BRIDGE_ISR_ERROR_FATAL) causing
1846          * pcibr_error_intr_handler() to panic the system.  So is the
1847          * error handler even going to get called???  It appears that
1848          * the pcibr_dmard_error() attempts to clear the interrupts
1849          * so pcibr_error_intr_handler() won't see them, but there
1850          * appears to be nothing to prevent pcibr_error_intr_handler()
1851          * from running before pcibr_dmard_error() has a chance to
1852          * clear the interrupt.
1853          *
1854          * Since we'll be panicing anyways, don't bother handling the
1855          * error for now until we can fix this race condition mentioned
1856          * above.
1857          */
1858         dma_retval = IOERROR_UNHANDLED;
1859     } 
1860     
1861     /* XXX: pcibr_error_handler() should probably do the same thing, it over-
1862      * write it's return value as it processes the different "error_code"s.
1863      */
1864     if ((pio_retval == -1) && (dma_retval == -1)) {
1865         return IOERROR_BADERRORCODE;
1866     } else if ((dma_retval != IOERROR_HANDLED) && (dma_retval != -1)) {
1867         return dma_retval;
1868     } else if ((pio_retval != IOERROR_HANDLED) && (pio_retval != -1)) {
1869         return pio_retval;
1870     } else {
1871         return IOERROR_HANDLED;
1872     }
1873 }