ahd_pci.c revision 1.5 1 /* $NetBSD: ahd_pci.c,v 1.5 2003/08/29 02:59:20 thorpej Exp $ */
2
3 /*
4 * Product specific probe and attach routines for:
5 * aic7901 and aic7902 SCSI controllers
6 *
7 * Copyright (c) 1994-2001 Justin T. Gibbs.
8 * Copyright (c) 2000-2002 Adaptec Inc.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
29 *
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
42 *
43 * Id: //depot/aic7xxx/aic7xxx/aic79xx_pci.c#74 $
44 *
45 * $FreeBSD: src/sys/dev/aic7xxx/aic79xx_pci.c,v 1.12 2003/06/06 23:48:18 gibbs Exp $
46 */
47 /*
48 * Ported from FreeBSD by Pascal Renauld, Network Storage Solutions, Inc. - April 2003
49 */
50
51 #include <sys/cdefs.h>
52 __KERNEL_RCSID(0, "$NetBSD: ahd_pci.c,v 1.5 2003/08/29 02:59:20 thorpej Exp $");
53
54 #define AHD_PCI_IOADDR PCI_MAPREG_START /* I/O Address */
55 #define AHD_PCI_MEMADDR (PCI_MAPREG_START + 4) /* Mem I/O Address */
56
57 #include <dev/ic/aic79xx_osm.h>
58 #include <dev/ic/aic79xx_inline.h>
59
60 static __inline uint64_t
61 ahd_compose_id(u_int device, u_int vendor, u_int subdevice, u_int subvendor)
62 {
63 uint64_t id;
64
65 id = subvendor
66 | (subdevice << 16)
67 | ((uint64_t)vendor << 32)
68 | ((uint64_t)device << 48);
69
70 return (id);
71 }
72
73 #define ID_ALL_MASK 0xFFFFFFFFFFFFFFFFull
74 #define ID_ALL_IROC_MASK 0xFFFFFF7FFFFFFFFFull
75 #define ID_DEV_VENDOR_MASK 0xFFFFFFFF00000000ull
76 #define ID_9005_GENERIC_MASK 0xFFF0FFFF00000000ull
77 #define ID_9005_GENERIC_IROC_MASK 0xFFF0FF7F00000000ull
78
79 #define ID_AIC7901 0x800F9005FFFF9005ull
80 #define ID_AHA_29320A 0x8000900500609005ull
81 #define ID_AHA_29320ALP 0x8017900500449005ull
82
83 #define ID_AIC7901A 0x801E9005FFFF9005ull
84 #define ID_AHA_29320 0x8012900500429005ull
85 #define ID_AHA_29320B 0x8013900500439005ull
86 #define ID_AHA_29320LP 0x8014900500449005ull
87
88 #define ID_AIC7902 0x801F9005FFFF9005ull
89 #define ID_AIC7902_B 0x801D9005FFFF9005ull
90 #define ID_AHA_39320 0x8010900500409005ull
91 #define ID_AHA_39320_B 0x8015900500409005ull
92 #define ID_AHA_39320A 0x8016900500409005ull
93 #define ID_AHA_39320D 0x8011900500419005ull
94 #define ID_AHA_39320D_B 0x801C900500419005ull
95 #define ID_AHA_39320D_HP 0x8011900500AC0E11ull
96 #define ID_AHA_39320D_B_HP 0x801C900500AC0E11ull
97 #define ID_AIC7902_PCI_REV_A4 0x3
98 #define ID_AIC7902_PCI_REV_B0 0x10
99 #define SUBID_HP 0x0E11
100
101 #define DEVID_9005_TYPE(id) ((id) & 0xF)
102 #define DEVID_9005_TYPE_HBA 0x0 /* Standard Card */
103 #define DEVID_9005_TYPE_HBA_2EXT 0x1 /* 2 External Ports */
104 #define DEVID_9005_TYPE_IROC 0x8 /* Raid(0,1,10) Card */
105 #define DEVID_9005_TYPE_MB 0xF /* On Motherboard */
106
107 #define DEVID_9005_MFUNC(id) ((id) & 0x10)
108
109 #define DEVID_9005_PACKETIZED(id) ((id) & 0x8000)
110
111 #define SUBID_9005_TYPE(id) ((id) & 0xF)
112 #define SUBID_9005_TYPE_HBA 0x0 /* Standard Card */
113 #define SUBID_9005_TYPE_MB 0xF /* On Motherboard */
114
115 #define SUBID_9005_AUTOTERM(id) (((id) & 0x10) == 0)
116
117 #define SUBID_9005_LEGACYCONN_FUNC(id) ((id) & 0x20)
118
119 #define SUBID_9005_SEEPTYPE(id) ((id) & 0x0C0) >> 6)
120 #define SUBID_9005_SEEPTYPE_NONE 0x0
121 #define SUBID_9005_SEEPTYPE_4K 0x1
122
123 static ahd_device_setup_t ahd_aic7901_setup;
124 static ahd_device_setup_t ahd_aic7901A_setup;
125 static ahd_device_setup_t ahd_aic7902_setup;
126
127 struct ahd_pci_identity ahd_pci_ident_table [] =
128 {
129 /* aic7901 based controllers */
130 {
131 ID_AHA_29320A,
132 ID_ALL_MASK,
133 "Adaptec 29320A Ultra320 SCSI adapter",
134 ahd_aic7901_setup
135 },
136 {
137 ID_AHA_29320ALP,
138 ID_ALL_MASK,
139 "Adaptec 29320ALP Ultra320 SCSI adapter",
140 ahd_aic7901_setup
141 },
142 /* aic7901A based controllers */
143 {
144 ID_AHA_29320,
145 ID_ALL_MASK,
146 "Adaptec 29320 Ultra320 SCSI adapter",
147 ahd_aic7901A_setup
148 },
149 {
150 ID_AHA_29320B,
151 ID_ALL_MASK,
152 "Adaptec 29320B Ultra320 SCSI adapter",
153 ahd_aic7901A_setup
154 },
155 {
156 ID_AHA_29320LP,
157 ID_ALL_MASK,
158 "Adaptec 29320LP Ultra320 SCSI adapter",
159 ahd_aic7901A_setup
160 },
161 /* aic7902 based controllers */
162 {
163 ID_AHA_39320,
164 ID_ALL_MASK,
165 "Adaptec 39320 Ultra320 SCSI adapter",
166 ahd_aic7902_setup
167 },
168 {
169 ID_AHA_39320_B,
170 ID_ALL_MASK,
171 "Adaptec 39320 Ultra320 SCSI adapter",
172 ahd_aic7902_setup
173 },
174 {
175 ID_AHA_39320A,
176 ID_ALL_MASK,
177 "Adaptec 39320A Ultra320 SCSI adapter",
178 ahd_aic7902_setup
179 },
180 {
181 ID_AHA_39320D,
182 ID_ALL_MASK,
183 "Adaptec 39320D Ultra320 SCSI adapter",
184 ahd_aic7902_setup
185 },
186 {
187 ID_AHA_39320D_HP,
188 ID_ALL_MASK,
189 "Adaptec (HP OEM) 39320D Ultra320 SCSI adapter",
190 ahd_aic7902_setup
191 },
192 {
193 ID_AHA_39320D_B,
194 ID_ALL_MASK,
195 "Adaptec 39320D Ultra320 SCSI adapter",
196 ahd_aic7902_setup
197 },
198 {
199 ID_AHA_39320D_B_HP,
200 ID_ALL_MASK,
201 "Adaptec (HP OEM) 39320D Ultra320 SCSI adapter",
202 ahd_aic7902_setup
203 },
204 {
205 ID_AHA_29320,
206 ID_ALL_MASK,
207 "Adaptec 29320 Ultra320 SCSI adapter",
208 ahd_aic7902_setup
209 },
210 {
211 ID_AHA_29320B,
212 ID_ALL_MASK,
213 "Adaptec 29320B Ultra320 SCSI adapter",
214 ahd_aic7902_setup
215 },
216 /* Generic chip probes for devices we don't know 'exactly' */
217 {
218 ID_AIC7901 & ID_DEV_VENDOR_MASK,
219 ID_DEV_VENDOR_MASK,
220 "Adaptec AIC7901 Ultra320 SCSI adapter",
221 ahd_aic7901_setup
222 },
223 {
224 ID_AIC7901A & ID_DEV_VENDOR_MASK,
225 ID_DEV_VENDOR_MASK,
226 "Adaptec AIC7901A Ultra320 SCSI adapter",
227 ahd_aic7901A_setup
228 },
229 {
230 ID_AIC7902 & ID_9005_GENERIC_MASK,
231 ID_9005_GENERIC_MASK,
232 "Adaptec AIC7902 Ultra320 SCSI adapter",
233 ahd_aic7902_setup
234 }
235 };
236
237 const u_int ahd_num_pci_devs = NUM_ELEMENTS(ahd_pci_ident_table);
238
239 #define DEVCONFIG 0x40
240 #define PCIXINITPAT 0x0000E000ul
241 #define PCIXINIT_PCI33_66 0x0000E000ul
242 #define PCIXINIT_PCIX50_66 0x0000C000ul
243 #define PCIXINIT_PCIX66_100 0x0000A000ul
244 #define PCIXINIT_PCIX100_133 0x00008000ul
245 #define PCI_BUS_MODES_INDEX(devconfig) \
246 (((devconfig) & PCIXINITPAT) >> 13)
247
248 static const char *pci_bus_modes[] =
249 {
250 "PCI bus mode unknown",
251 "PCI bus mode unknown",
252 "PCI bus mode unknown",
253 "PCI bus mode unknown",
254 "PCI-X 101-133Mhz",
255 "PCI-X 67-100Mhz",
256 "PCI-X 50-66Mhz",
257 "PCI 33 or 66Mhz"
258 };
259
260 #define TESTMODE 0x00000800ul
261 #define IRDY_RST 0x00000200ul
262 #define FRAME_RST 0x00000100ul
263 #define PCI64BIT 0x00000080ul
264 #define MRDCEN 0x00000040ul
265 #define ENDIANSEL 0x00000020ul
266 #define MIXQWENDIANEN 0x00000008ul
267 #define DACEN 0x00000004ul
268 #define STPWLEVEL 0x00000002ul
269 #define QWENDIANSEL 0x00000001ul
270
271 #define DEVCONFIG1 0x44
272 #define PREQDIS 0x01
273
274 #define LATTIME 0x0000ff00ul
275
276 int ahd_pci_probe __P((struct device *, struct cfdata *, void *));
277 void ahd_pci_attach __P((struct device *, struct device *, void *));
278
279 CFATTACH_DECL(ahd_pci, sizeof(struct ahd_softc),
280 ahd_pci_probe, ahd_pci_attach, NULL, NULL);
281
282 static int ahd_check_extport(struct ahd_softc *ahd);
283 static void ahd_configure_termination(struct ahd_softc *ahd,
284 u_int adapter_control);
285 static void ahd_pci_split_intr(struct ahd_softc *ahd, u_int intstat);
286
287 const struct ahd_pci_identity *
288 ahd_find_pci_device(id, subid)
289 pcireg_t id, subid;
290 {
291 u_int64_t full_id;
292 const struct ahd_pci_identity *entry;
293 u_int i;
294
295 full_id = ahd_compose_id(PCI_PRODUCT(id), PCI_VENDOR(id),
296 PCI_PRODUCT(subid), PCI_VENDOR(subid));
297
298 for (i = 0; i < ahd_num_pci_devs; i++) {
299 entry = &ahd_pci_ident_table[i];
300 if (entry->full_id == (full_id & entry->id_mask))
301 return (entry);
302 }
303 return (NULL);
304 }
305
306 int
307 ahd_pci_probe(parent, match, aux)
308 struct device *parent;
309 struct cfdata *match;
310 void *aux;
311 {
312 struct pci_attach_args *pa = aux;
313 const struct ahd_pci_identity *entry;
314 pcireg_t subid;
315
316 subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
317 entry = ahd_find_pci_device(pa->pa_id, subid);
318 return entry != NULL ? 1 : 0;
319 }
320
321 void
322 ahd_pci_attach(parent, self, aux)
323 struct device *parent, *self;
324 void *aux;
325 {
326 struct pci_attach_args *pa = aux;
327 struct ahd_softc *ahd = (void *)self;
328
329 const struct ahd_pci_identity *entry;
330
331 uint32_t devconfig;
332 pcireg_t command;
333 int error;
334 pcireg_t subid;
335 uint16_t subvendor;
336 int pci_pwrmgmt_cap_reg;
337 int pci_pwrmgmt_csr_reg;
338 pcireg_t reg;
339 int ioh_valid, ioh2_valid, memh_valid;
340 pcireg_t memtype;
341 pci_intr_handle_t ih;
342 const char *intrstr;
343 struct ahd_pci_busdata *bd;
344
345 ahd_set_name(ahd, ahd->sc_dev.dv_xname);
346 ahd->parent_dmat = pa->pa_dmat;
347
348 command = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
349 subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
350 entry = ahd_find_pci_device(pa->pa_id, subid);
351 if (entry == NULL)
352 return;
353
354 /* Keep information about the PCI bus */
355 bd = malloc(sizeof (struct ahd_pci_busdata), M_DEVBUF, M_NOWAIT);
356 if (bd == NULL) {
357 printf("%s: unable to allocate bus-specific data\n", ahd_name(ahd));
358 return;
359 }
360 memset(bd, 0, sizeof(struct ahd_pci_busdata));
361
362 bd->pc = pa->pa_pc;
363 bd->tag = pa->pa_tag;
364 bd->func = pa->pa_function;
365 bd->dev = pa->pa_device;
366
367 ahd->bus_data = bd;
368
369 ahd->description = entry->name;
370
371 ahd->seep_config = malloc(sizeof(*ahd->seep_config),
372 M_DEVBUF, M_NOWAIT);
373 if (ahd->seep_config == NULL) {
374 printf("%s: cannot malloc seep_config!\n", ahd_name(ahd));
375 return;
376 }
377 memset(ahd->seep_config, 0, sizeof(*ahd->seep_config));
378
379 LIST_INIT(&ahd->pending_scbs);
380 ahd_timer_init(&ahd->reset_timer);
381 ahd_timer_init(&ahd->stat_timer);
382 ahd->int_coalescing_timer = AHD_INT_COALESCING_TIMER_DEFAULT;
383 ahd->int_coalescing_maxcmds = AHD_INT_COALESCING_MAXCMDS_DEFAULT;
384 ahd->int_coalescing_mincmds = AHD_INT_COALESCING_MINCMDS_DEFAULT;
385 ahd->int_coalescing_threshold = AHD_INT_COALESCING_THRESHOLD_DEFAULT;
386 ahd->int_coalescing_stop_threshold = AHD_INT_COALESCING_STOP_THRESHOLD_DEFAULT;
387
388 if (ahd_platform_alloc(ahd, NULL) != 0) {
389 ahd_free(ahd);
390 return;
391 }
392
393 /*
394 * Record if this is an HP board.
395 */
396 subvendor = PCI_VENDOR(subid);
397 if (subvendor == SUBID_HP)
398 ahd->flags |= AHD_HP_BOARD;
399
400 error = entry->setup(ahd, pa);
401 if (error != 0)
402 return;
403
404 devconfig = pci_conf_read(pa->pa_pc, pa->pa_tag, DEVCONFIG);
405 if ((devconfig & PCIXINITPAT) == PCIXINIT_PCI33_66 ||
406 (devconfig & PCIXINITPAT) == PCIXINIT_PCIX66_100) {
407 ahd->chip |= AHD_PCI;
408 /* Disable PCIX workarounds when running in PCI mode. */
409 ahd->bugs &= ~AHD_PCIX_BUG_MASK;
410 } else {
411 ahd->chip |= AHD_PCIX;
412 }
413 ahd->bus_description = pci_bus_modes[PCI_BUS_MODES_INDEX(devconfig)];
414
415 memh_valid = ioh_valid = ioh2_valid = 0;
416
417 if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_PCIX,
418 &bd->pcix_off, NULL)) {
419 if (ahd->chip & AHD_PCIX)
420 printf("%s: warning: can't find PCI-X capability\n",
421 ahd->sc_dev.dv_xname);
422 ahd->chip &= ~AHD_PCIX;
423 ahd->chip |= AHD_PCI;
424 ahd->bugs &= ~AHD_PCIX_BUG_MASK;
425 }
426
427 /*
428 * Map PCI Registers
429 */
430 if ((command & (PCI_COMMAND_MEM_ENABLE)) != 0) {
431 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, AHD_PCI_MEMADDR);
432 switch (memtype) {
433 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
434 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
435 memh_valid = (pci_mapreg_map(pa, AHD_PCI_MEMADDR,
436 memtype, 0, &ahd->tags[0],
437 &ahd->bshs[0], NULL, NULL) == 0);
438
439 ahd->tags[1] = ahd->tags[0];
440
441 bus_space_subregion(ahd->tags[0], ahd->bshs[0],
442 /*offset*/0x100,
443 /*size*/0x100,
444 &ahd->bshs[1]);
445 break;
446 default:
447 printf("%s: unable to map memory registers\n", ahd_name(ahd));
448 return;
449 }
450
451 if (memh_valid) {
452 command &= ~PCI_COMMAND_IO_ENABLE;
453 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
454 }
455 #ifdef AHD_DEBUG
456 printf("%s: doing memory mapping tag0 0x%x, tag1 0x%x, shs0 0x%lx, shs1 0x%lx\n",
457 ahd_name(ahd), ahd->tags[0], ahd->tags[1], ahd->bshs[0], ahd->bshs[1]);
458 #endif
459 }
460
461 if ((command & (PCI_COMMAND_IO_ENABLE)) != 0 &&
462 !(ahd->bugs & AHD_PCIX_MMAPIO_BUG)) {
463 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, AHD_PCI_IOADDR);
464
465 /* First BAR */
466 ioh_valid = (pci_mapreg_map(pa, AHD_PCI_IOADDR,
467 memtype, 0, &ahd->tags[0],
468 &ahd->bshs[0], NULL, NULL) == 0);
469
470 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, AHD_PCI_IOADDR1);
471
472 /* 2nd BAR */
473 ioh2_valid = (pci_mapreg_map(pa, AHD_PCI_IOADDR1,
474 memtype, 0, &ahd->tags[1],
475 &ahd->bshs[1], NULL, NULL) == 0);
476
477 if (ioh_valid && ioh2_valid) {
478 command &= ~PCI_COMMAND_MEM_ENABLE;
479 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
480 }
481 #ifdef AHD_DEBUG
482 printf("%s: doing io mapping tag0 0x%x, tag1 0x%x, shs0 0x%lx, shs1 0x%lx\n",
483 ahd_name(ahd), ahd->tags[0], ahd->tags[1], ahd->bshs[0], ahd->bshs[1]);
484 #endif
485
486 }
487
488 if ((memh_valid == 0) && ((ioh_valid == 0) || (ioh2_valid == 0))) {
489 printf("%s: unable to map memory registers\n", ahd_name(ahd));
490 return;
491 }
492
493 printf("\n");
494
495 /*
496 * Set Power State D0.
497 */
498 if (pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_PWRMGMT,
499 &pci_pwrmgmt_cap_reg, 0)) {
500
501 pci_pwrmgmt_csr_reg = pci_pwrmgmt_cap_reg + 4;
502 reg = pci_conf_read(pa->pa_pc, pa->pa_tag,
503 pci_pwrmgmt_csr_reg);
504 if ((reg & PCI_PMCSR_STATE_MASK) != PCI_PMCSR_STATE_D0) {
505 pci_conf_write(pa->pa_pc, pa->pa_tag, pci_pwrmgmt_csr_reg,
506 (reg & ~PCI_PMCSR_STATE_MASK) |
507 PCI_PMCSR_STATE_D0);
508 }
509 }
510
511 /*
512 * Should we bother disabling 39Bit addressing
513 * based on installed memory?
514 */
515 if (sizeof(bus_addr_t) > 4)
516 ahd->flags |= AHD_39BIT_ADDRESSING;
517
518 /*
519 * If we need to support high memory, enable dual
520 * address cycles. This bit must be set to enable
521 * high address bit generation even if we are on a
522 * 64bit bus (PCI64BIT set in devconfig).
523 */
524 if ((ahd->flags & (AHD_39BIT_ADDRESSING|AHD_64BIT_ADDRESSING)) != 0) {
525 uint32_t devconfig;
526
527 printf("%s: Enabling 39Bit Addressing\n", ahd_name(ahd));
528 devconfig = pci_conf_read(pa->pa_pc, pa->pa_tag, DEVCONFIG);
529 devconfig |= DACEN;
530 pci_conf_write(pa->pa_pc, pa->pa_tag, DEVCONFIG, devconfig);
531 }
532
533 /* Ensure busmastering is enabled */
534 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
535 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
536 reg | PCI_COMMAND_MASTER_ENABLE);
537
538 ahd_softc_init(ahd);
539
540 /*
541 * Map the interrupt routines
542 */
543 ahd->bus_intr = ahd_pci_intr;
544
545 if (pci_intr_map(pa, &ih)) {
546 printf("%s: couldn't map interrupt\n", ahd_name(ahd));
547 ahd_free(ahd);
548 return;
549 }
550 intrstr = pci_intr_string(pa->pa_pc, ih);
551 ahd->ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO, ahd_intr, ahd);
552 if (ahd->ih == NULL) {
553 printf("%s: couldn't establish interrupt",
554 ahd_name(ahd));
555 if (intrstr != NULL)
556 printf(" at %s", intrstr);
557 printf("\n");
558 ahd_free(ahd);
559 return;
560 }
561 if (intrstr != NULL)
562 printf("%s: interrupting at %s\n", ahd_name(ahd),
563 intrstr);
564
565 /* Get the size of the cache */
566 ahd->pci_cachesize = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG);
567 ahd->pci_cachesize *= 4;
568
569 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
570 /* See if we have a SEEPROM and perform auto-term */
571 error = ahd_check_extport(ahd);
572 if (error != 0)
573 return;
574
575 /* Core initialization */
576 error = ahd_init(ahd);
577 if (error != 0)
578 return;
579
580 /*
581 * Link this softc in with all other ahd instances.
582 */
583 ahd_attach(ahd);
584
585 return;
586 }
587
588
589 /*
590 * Check the external port logic for a serial eeprom
591 * and termination/cable detection contrls.
592 */
593 static int
594 ahd_check_extport(struct ahd_softc *ahd)
595 {
596 struct vpd_config vpd;
597 struct seeprom_config *sc;
598 u_int adapter_control;
599 int have_seeprom;
600 int error;
601
602 sc = ahd->seep_config;
603 have_seeprom = ahd_acquire_seeprom(ahd);
604 if (have_seeprom) {
605 u_int start_addr;
606
607 /*
608 * Fetch VPD for this function and parse it.
609 */
610 #ifdef AHD_DEBUG
611 printf("%s: Reading VPD from SEEPROM...",
612 ahd_name(ahd));
613 #endif
614 /* Address is always in units of 16bit words */
615 start_addr = ((2 * sizeof(*sc))
616 + (sizeof(vpd) * (ahd->channel - 'A'))) / 2;
617
618 error = ahd_read_seeprom(ahd, (uint16_t *)&vpd,
619 start_addr, sizeof(vpd)/2,
620 /*bytestream*/TRUE);
621 if (error == 0)
622 error = ahd_parse_vpddata(ahd, &vpd);
623 #ifdef AHD_DEBUG
624 printf("%s: VPD parsing %s\n",
625 ahd_name(ahd),
626 error == 0 ? "successful" : "failed");
627 #endif
628
629 #ifdef AHD_DEBUG
630 printf("%s: Reading SEEPROM...", ahd_name(ahd));
631 #endif
632
633 /* Address is always in units of 16bit words */
634 start_addr = (sizeof(*sc) / 2) * (ahd->channel - 'A');
635
636 error = ahd_read_seeprom(ahd, (uint16_t *)sc,
637 start_addr, sizeof(*sc)/2,
638 /*bytestream*/FALSE);
639
640 if (error != 0) {
641 #ifdef AHD_DEBUG
642 printf("Unable to read SEEPROM\n");
643 #endif
644 have_seeprom = 0;
645 } else {
646 have_seeprom = ahd_verify_cksum(sc);
647 #ifdef AHD_DEBUG
648 if (have_seeprom == 0)
649 printf ("checksum error\n");
650 else
651 printf ("done.\n");
652 #endif
653 }
654 ahd_release_seeprom(ahd);
655 }
656
657 if (!have_seeprom) {
658 u_int nvram_scb;
659
660 /*
661 * Pull scratch ram settings and treat them as
662 * if they are the contents of an seeprom if
663 * the 'ADPT', 'BIOS', or 'ASPI' signature is found
664 * in SCB 0xFF. We manually compose the data as 16bit
665 * values to avoid endian issues.
666 */
667 ahd_set_scbptr(ahd, 0xFF);
668 nvram_scb = ahd_inb_scbram(ahd, SCB_BASE + NVRAM_SCB_OFFSET);
669 if (nvram_scb != 0xFF
670 && ((ahd_inb_scbram(ahd, SCB_BASE + 0) == 'A'
671 && ahd_inb_scbram(ahd, SCB_BASE + 1) == 'D'
672 && ahd_inb_scbram(ahd, SCB_BASE + 2) == 'P'
673 && ahd_inb_scbram(ahd, SCB_BASE + 3) == 'T')
674 || (ahd_inb_scbram(ahd, SCB_BASE + 0) == 'B'
675 && ahd_inb_scbram(ahd, SCB_BASE + 1) == 'I'
676 && ahd_inb_scbram(ahd, SCB_BASE + 2) == 'O'
677 && ahd_inb_scbram(ahd, SCB_BASE + 3) == 'S')
678 || (ahd_inb_scbram(ahd, SCB_BASE + 0) == 'A'
679 && ahd_inb_scbram(ahd, SCB_BASE + 1) == 'S'
680 && ahd_inb_scbram(ahd, SCB_BASE + 2) == 'P'
681 && ahd_inb_scbram(ahd, SCB_BASE + 3) == 'I'))) {
682 uint16_t *sc_data;
683 int i;
684
685 ahd_set_scbptr(ahd, nvram_scb);
686 sc_data = (uint16_t *)sc;
687 for (i = 0; i < 64; i += 2)
688 *sc_data++ = ahd_inw_scbram(ahd, SCB_BASE+i);
689 have_seeprom = ahd_verify_cksum(sc);
690 if (have_seeprom)
691 ahd->flags |= AHD_SCB_CONFIG_USED;
692 }
693 }
694
695 #ifdef AHD_DEBUG
696 if ((have_seeprom != 0) && (ahd_debug & AHD_DUMP_SEEPROM) != 0) {
697 uint16_t *sc_data;
698 int i;
699
700 printf("%s: Seeprom Contents:", ahd_name(ahd));
701 sc_data = (uint16_t *)sc;
702 for (i = 0; i < (sizeof(*sc)); i += 2)
703 printf("\n\t0x%.4x", sc_data[i]);
704 printf("\n");
705 }
706 #endif
707
708 if (!have_seeprom) {
709 printf("%s: No SEEPROM available.\n", ahd_name(ahd));
710 ahd->flags |= AHD_USEDEFAULTS;
711 error = ahd_default_config(ahd);
712 adapter_control = CFAUTOTERM|CFSEAUTOTERM;
713 free(ahd->seep_config, M_DEVBUF);
714 ahd->seep_config = NULL;
715 } else {
716 error = ahd_parse_cfgdata(ahd, sc);
717 adapter_control = sc->adapter_control;
718 }
719 if (error != 0)
720 return (error);
721
722 ahd_configure_termination(ahd, adapter_control);
723
724 return (0);
725 }
726
727 static void
728 ahd_configure_termination(struct ahd_softc *ahd, u_int adapter_control)
729 {
730 int error;
731 u_int sxfrctl1;
732 uint8_t termctl;
733 uint32_t devconfig;
734 struct ahd_pci_busdata *bd = ahd->bus_data;
735
736 devconfig = pci_conf_read(bd->pc, bd->tag, DEVCONFIG);
737 devconfig &= ~STPWLEVEL;
738 if ((ahd->flags & AHD_STPWLEVEL_A) != 0)
739 devconfig |= STPWLEVEL;
740 #ifdef AHD_DEBUG
741 printf("%s: STPWLEVEL is %s\n",
742 ahd_name(ahd), (devconfig & STPWLEVEL) ? "on" : "off");
743 #endif
744 pci_conf_write(bd->pc, bd->tag, DEVCONFIG, devconfig);
745
746 /* Make sure current sensing is off. */
747 if ((ahd->flags & AHD_CURRENT_SENSING) != 0) {
748 (void)ahd_write_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL, 0);
749 }
750
751 /*
752 * Read to sense. Write to set.
753 */
754 error = ahd_read_flexport(ahd, FLXADDR_TERMCTL, &termctl);
755 if ((adapter_control & CFAUTOTERM) == 0) {
756 printf("%s: Manual Primary Termination\n",
757 ahd_name(ahd));
758 termctl &= ~(FLX_TERMCTL_ENPRILOW|FLX_TERMCTL_ENPRIHIGH);
759 if ((adapter_control & CFSTERM) != 0)
760 termctl |= FLX_TERMCTL_ENPRILOW;
761 if ((adapter_control & CFWSTERM) != 0)
762 termctl |= FLX_TERMCTL_ENPRIHIGH;
763 } else if (error != 0) {
764 printf("%s: Primary Auto-Term Sensing failed! "
765 "Using Defaults.\n", ahd_name(ahd));
766 termctl = FLX_TERMCTL_ENPRILOW|FLX_TERMCTL_ENPRIHIGH;
767 }
768
769 if ((adapter_control & CFSEAUTOTERM) == 0) {
770 printf("%s: Manual Secondary Termination\n",
771 ahd_name(ahd));
772 termctl &= ~(FLX_TERMCTL_ENSECLOW|FLX_TERMCTL_ENSECHIGH);
773 if ((adapter_control & CFSELOWTERM) != 0)
774 termctl |= FLX_TERMCTL_ENSECLOW;
775 if ((adapter_control & CFSEHIGHTERM) != 0)
776 termctl |= FLX_TERMCTL_ENSECHIGH;
777 } else if (error != 0) {
778 printf("%s: Secondary Auto-Term Sensing failed! "
779 "Using Defaults.\n", ahd_name(ahd));
780 termctl |= FLX_TERMCTL_ENSECLOW|FLX_TERMCTL_ENSECHIGH;
781 }
782
783 /*
784 * Now set the termination based on what we found.
785 */
786 sxfrctl1 = ahd_inb(ahd, SXFRCTL1) & ~STPWEN;
787 if ((termctl & FLX_TERMCTL_ENPRILOW) != 0) {
788 ahd->flags |= AHD_TERM_ENB_A;
789 sxfrctl1 |= STPWEN;
790 }
791 /* Must set the latch once in order to be effective. */
792 ahd_outb(ahd, SXFRCTL1, sxfrctl1|STPWEN);
793 ahd_outb(ahd, SXFRCTL1, sxfrctl1);
794
795 error = ahd_write_flexport(ahd, FLXADDR_TERMCTL, termctl);
796 if (error != 0) {
797 printf("%s: Unable to set termination settings!\n",
798 ahd_name(ahd));
799 } else {
800 printf("%s: Primary High byte termination %sabled\n",
801 ahd_name(ahd),
802 (termctl & FLX_TERMCTL_ENPRIHIGH) ? "En" : "Dis");
803
804 printf("%s: Primary Low byte termination %sabled\n",
805 ahd_name(ahd),
806 (termctl & FLX_TERMCTL_ENPRILOW) ? "En" : "Dis");
807
808 printf("%s: Secondary High byte termination %sabled\n",
809 ahd_name(ahd),
810 (termctl & FLX_TERMCTL_ENSECHIGH) ? "En" : "Dis");
811
812 printf("%s: Secondary Low byte termination %sabled\n",
813 ahd_name(ahd),
814 (termctl & FLX_TERMCTL_ENSECLOW) ? "En" : "Dis");
815 }
816 return;
817 }
818
819 #define DPE 0x80
820 #define SSE 0x40
821 #define RMA 0x20
822 #define RTA 0x10
823 #define STA 0x08
824 #define DPR 0x01
825
826 static const char *split_status_source[] =
827 {
828 "DFF0",
829 "DFF1",
830 "OVLY",
831 "CMC",
832 };
833
834 static const char *pci_status_source[] =
835 {
836 "DFF0",
837 "DFF1",
838 "SG",
839 "CMC",
840 "OVLY",
841 "NONE",
842 "MSI",
843 "TARG"
844 };
845
846 static const char *split_status_strings[] =
847 {
848 "%s: Received split response in %s.\n",
849 "%s: Received split completion error message in %s\n",
850 "%s: Receive overrun in %s\n",
851 "%s: Count not complete in %s\n",
852 "%s: Split completion data bucket in %s\n",
853 "%s: Split completion address error in %s\n",
854 "%s: Split completion byte count error in %s\n",
855 "%s: Signaled Target-abort to early terminate a split in %s\n"
856 };
857
858 static const char *pci_status_strings[] =
859 {
860 "%s: Data Parity Error has been reported via PERR# in %s\n",
861 "%s: Target initial wait state error in %s\n",
862 "%s: Split completion read data parity error in %s\n",
863 "%s: Split completion address attribute parity error in %s\n",
864 "%s: Received a Target Abort in %s\n",
865 "%s: Received a Master Abort in %s\n",
866 "%s: Signal System Error Detected in %s\n",
867 "%s: Address or Write Phase Parity Error Detected in %s.\n"
868 };
869
870 int
871 ahd_pci_intr(struct ahd_softc *ahd)
872 {
873 uint8_t pci_status[8];
874 ahd_mode_state saved_modes;
875 u_int pci_status1;
876 u_int intstat;
877 u_int i;
878 u_int reg;
879 struct ahd_pci_busdata *bd = ahd->bus_data;
880
881 intstat = ahd_inb(ahd, INTSTAT);
882
883 if ((intstat & SPLTINT) != 0)
884 ahd_pci_split_intr(ahd, intstat);
885
886 if ((intstat & PCIINT) == 0)
887 return 0;
888
889 printf("%s: PCI error Interrupt\n", ahd_name(ahd));
890 saved_modes = ahd_save_modes(ahd);
891 ahd_dump_card_state(ahd);
892 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
893 for (i = 0, reg = DF0PCISTAT; i < 8; i++, reg++) {
894
895 if (i == 5)
896 continue;
897 pci_status[i] = ahd_inb(ahd, reg);
898 /* Clear latched errors. So our interrupt deasserts. */
899 ahd_outb(ahd, reg, pci_status[i]);
900 }
901
902 for (i = 0; i < 8; i++) {
903 u_int bit;
904
905 if (i == 5)
906 continue;
907
908 for (bit = 0; bit < 8; bit++) {
909
910 if ((pci_status[i] & (0x1 << bit)) != 0) {
911 static const char *s;
912
913 s = pci_status_strings[bit];
914 if (i == 7/*TARG*/ && bit == 3)
915 s = "%s: Signaled Target Abort\n";
916 printf(s, ahd_name(ahd), pci_status_source[i]);
917 }
918 }
919 }
920 pci_status1 = pci_conf_read(bd->pc, bd->tag, PCI_COMMAND_STATUS_REG);
921 pci_conf_write(bd->pc, bd->tag, PCI_COMMAND_STATUS_REG , pci_status1);
922
923 ahd_restore_modes(ahd, saved_modes);
924 ahd_outb(ahd, CLRINT, CLRPCIINT);
925 ahd_unpause(ahd);
926
927 return 1;
928 }
929
930 static void
931 ahd_pci_split_intr(struct ahd_softc *ahd, u_int intstat)
932 {
933 uint8_t split_status[4];
934 uint8_t split_status1[4];
935 uint8_t sg_split_status[2];
936 uint8_t sg_split_status1[2];
937 ahd_mode_state saved_modes;
938 u_int i;
939 pcireg_t pcix_status;
940 struct ahd_pci_busdata *bd = ahd->bus_data;
941
942 /*
943 * Check for splits in all modes. Modes 0 and 1
944 * additionally have SG engine splits to look at.
945 */
946 pcix_status = pci_conf_read(bd->pc, bd->tag,
947 bd->pcix_off + PCI_PCIX_STATUS);
948 printf("%s: PCI Split Interrupt - PCI-X status = 0x%x\n",
949 ahd_name(ahd), pcix_status);
950
951 saved_modes = ahd_save_modes(ahd);
952 for (i = 0; i < 4; i++) {
953 ahd_set_modes(ahd, i, i);
954
955 split_status[i] = ahd_inb(ahd, DCHSPLTSTAT0);
956 split_status1[i] = ahd_inb(ahd, DCHSPLTSTAT1);
957 /* Clear latched errors. So our interrupt deasserts. */
958 ahd_outb(ahd, DCHSPLTSTAT0, split_status[i]);
959 ahd_outb(ahd, DCHSPLTSTAT1, split_status1[i]);
960 if (i > 1)
961 continue;
962 sg_split_status[i] = ahd_inb(ahd, SGSPLTSTAT0);
963 sg_split_status1[i] = ahd_inb(ahd, SGSPLTSTAT1);
964 /* Clear latched errors. So our interrupt deasserts. */
965 ahd_outb(ahd, SGSPLTSTAT0, sg_split_status[i]);
966 ahd_outb(ahd, SGSPLTSTAT1, sg_split_status1[i]);
967 }
968
969 for (i = 0; i < 4; i++) {
970 u_int bit;
971
972 for (bit = 0; bit < 8; bit++) {
973
974 if ((split_status[i] & (0x1 << bit)) != 0) {
975 static const char *s;
976
977 s = split_status_strings[bit];
978 printf(s, ahd_name(ahd),
979 split_status_source[i]);
980 }
981
982 if (i > 0)
983 continue;
984
985 if ((sg_split_status[i] & (0x1 << bit)) != 0) {
986 static const char *s;
987
988 s = split_status_strings[bit];
989 printf(s, ahd_name(ahd), "SG");
990 }
991 }
992 }
993 /*
994 * Clear PCI-X status bits.
995 */
996 pci_conf_write(bd->pc, bd->tag, bd->pcix_off + PCI_PCIX_STATUS,
997 pcix_status);
998 ahd_outb(ahd, CLRINT, CLRSPLTINT);
999 ahd_restore_modes(ahd, saved_modes);
1000 }
1001
1002 static int
1003 ahd_aic7901_setup(struct ahd_softc *ahd, struct pci_attach_args *pa)
1004 {
1005 int error;
1006
1007 error = ahd_aic7902_setup(ahd, pa);
1008 if (error != 0)
1009 return (error);
1010 ahd->chip = AHD_AIC7901;
1011 return (0);
1012 }
1013
1014 static int
1015 ahd_aic7901A_setup(struct ahd_softc *ahd, struct pci_attach_args *pa)
1016 {
1017 int error;
1018
1019 error = ahd_aic7902_setup(ahd, pa);
1020 if (error != 0)
1021 return (error);
1022 ahd->chip = AHD_AIC7901A;
1023 return (0);
1024 }
1025
1026 static int
1027 ahd_aic7902_setup(struct ahd_softc *ahd, struct pci_attach_args *pa)
1028 {
1029 u_int rev;
1030
1031 rev = PCI_REVISION(pa->pa_class);
1032 #ifdef AHD_DEBUG
1033 printf("\n%s: aic7902 chip revision 0x%x\n", ahd_name(ahd), rev);
1034 #endif
1035 if (rev < ID_AIC7902_PCI_REV_A4) {
1036 printf("%s: Unable to attach to unsupported chip revision %d\n",
1037 ahd_name(ahd), rev);
1038 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, 0);
1039 return (ENXIO);
1040 }
1041
1042 ahd->channel = (pa->pa_function == 1) ? 'B' : 'A';
1043 ahd->chip = AHD_AIC7902;
1044 ahd->features = AHD_AIC7902_FE;
1045 if (rev < ID_AIC7902_PCI_REV_B0) {
1046 /*
1047 * Enable A series workarounds.
1048 */
1049 ahd->bugs |= AHD_SENT_SCB_UPDATE_BUG|AHD_ABORT_LQI_BUG
1050 | AHD_PKT_BITBUCKET_BUG|AHD_LONG_SETIMO_BUG
1051 | AHD_NLQICRC_DELAYED_BUG|AHD_SCSIRST_BUG
1052 | AHD_LQO_ATNO_BUG|AHD_AUTOFLUSH_BUG
1053 | AHD_CLRLQO_AUTOCLR_BUG|AHD_PCIX_MMAPIO_BUG
1054 | AHD_PCIX_CHIPRST_BUG|AHD_PCIX_SCBRAM_RD_BUG
1055 | AHD_PKTIZED_STATUS_BUG|AHD_PKT_LUN_BUG
1056 | AHD_MDFF_WSCBPTR_BUG|AHD_REG_SLOW_SETTLE_BUG
1057 | AHD_SET_MODE_BUG|AHD_BUSFREEREV_BUG
1058 | AHD_NONPACKFIFO_BUG|AHD_PACED_NEGTABLE_BUG
1059 | AHD_FAINT_LED_BUG;
1060
1061
1062 /*
1063 * IO Cell paramter setup.
1064 */
1065 AHD_SET_PRECOMP(ahd, AHD_PRECOMP_CUTBACK_29);
1066
1067 if ((ahd->flags & AHD_HP_BOARD) == 0)
1068 AHD_SET_SLEWRATE(ahd, AHD_SLEWRATE_DEF_REVA);
1069 } else {
1070 u_int devconfig1;
1071
1072 ahd->features |= AHD_RTI|AHD_NEW_IOCELL_OPTS
1073 | AHD_NEW_DFCNTRL_OPTS;
1074 ahd->bugs |= AHD_LQOOVERRUN_BUG|AHD_ABORT_LQI_BUG
1075 | AHD_INTCOLLISION_BUG|AHD_EARLY_REQ_BUG;
1076
1077 /*
1078 * IO Cell paramter setup.
1079 */
1080 AHD_SET_PRECOMP(ahd, AHD_PRECOMP_CUTBACK_29);
1081 AHD_SET_SLEWRATE(ahd, AHD_SLEWRATE_DEF_REVB);
1082 AHD_SET_AMPLITUDE(ahd, AHD_AMPLITUDE_DEF);
1083
1084 /*
1085 * Set the PREQDIS bit for H2B which disables some workaround
1086 * that doesn't work on regular PCI busses.
1087 * XXX - Find out exactly what this does from the hardware
1088 * folks!
1089 */
1090 devconfig1 = pci_conf_read(pa->pa_pc, pa->pa_tag, DEVCONFIG1);
1091 pci_conf_write(pa->pa_pc, pa->pa_tag, DEVCONFIG1, devconfig1|PREQDIS);
1092 devconfig1 = pci_conf_read(pa->pa_pc, pa->pa_tag, DEVCONFIG1);
1093 }
1094
1095 return (0);
1096 }
1097
1098