⬢ DragonFlyBSD Kernel Audit
sys/dev/disk/ahci/ahci_attach.c
← back
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
 * (MPSAFE)
 *
 * Copyright (c) 2006 David Gwynne <dlg@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 *
 * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
 *
 * This code is derived from software contributed to The DragonFly Project
 * by Matthew Dillon <dillon@backplane.com>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name of The DragonFly Project nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific, prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $OpenBSD: ahci.c,v 1.147 2009/02/16 21:19:07 miod Exp $
 */

#include "ahci.h"

static int	ahci_vt8251_attach(device_t);
static int	ahci_ati_sb600_attach(device_t);
static int	ahci_nvidia_mcp_attach(device_t);
static int	ahci_pci_attach(device_t);
static int	ahci_pci_detach(device_t);

static int	ahci_samsung_apple_attach(device_t dev);

static const struct ahci_device ahci_devices[] = {
	{ PCI_VENDOR_VIATECH,	PCI_PRODUCT_VIATECH_VT8251_SATA,
	    ahci_vt8251_attach, ahci_pci_detach, "ViaTech-VT8251-SATA" },
	{ PCI_VENDOR_SAMSUNG,	PCI_PRODUCT_SAMSUNG_APPLE_AHCI,
	    ahci_samsung_apple_attach, ahci_pci_detach, "Samsung-Apple-PCIe-SSD" },
	{ PCI_VENDOR_ATI,	PCI_PRODUCT_ATI_SB600_SATA,
	    ahci_ati_sb600_attach, ahci_pci_detach, "ATI-SB600-SATA" },
	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_2,
	    ahci_nvidia_mcp_attach, ahci_pci_detach, "NVidia-MCP65-SATA" },
	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP67_AHCI_1,
	    ahci_nvidia_mcp_attach, ahci_pci_detach, "NVidia-MCP67-SATA" },
	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP77_AHCI_5,
	    ahci_nvidia_mcp_attach, ahci_pci_detach, "NVidia-MCP77-SATA" },
	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP79_AHCI_1,
	    ahci_nvidia_mcp_attach, ahci_pci_detach, "NVidia-MCP79-SATA" },
	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP79_AHCI_9,
	    ahci_nvidia_mcp_attach, ahci_pci_detach, "NVidia-MCP79-SATA" },
	{ 0, 0,
	    ahci_pci_attach, ahci_pci_detach, "AHCI-PCI-SATA" }
};

struct ahci_pciid {
	uint16_t	ahci_vid;
	uint16_t	ahci_did;
	int		ahci_rev;
};

static const struct ahci_pciid ahci_msi_blacklist[] = {
	{ PCI_VENDOR_ATI,	PCI_PRODUCT_ATI_SB600_SATA, -1 },
	{ PCI_VENDOR_ATI,	PCI_PRODUCT_ATI_SB700_AHCI, -1 },

	{ PCI_VENDOR_MARVELL,	PCI_PRODUCT_MARVELL_88SE6121, -1 },
	{ PCI_VENDOR_MARVELL,	PCI_PRODUCT_MARVELL_88SE6145, -1 },

	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_1, 0xa1 },
	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_2, 0xa1 },
	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_3, 0xa1 },
	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_4, 0xa1 },
	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_5, 0xa1 },
	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_6, 0xa1 },
	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_7, 0xa1 },
	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_8, 0xa1 },

	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_1, 0xa2 },
	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_2, 0xa2 },
	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_3, 0xa2 },
	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_4, 0xa2 },
	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_5, 0xa2 },
	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_6, 0xa2 },
	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_7, 0xa2 },
	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_8, 0xa2 },

	/* Samsung Apple PCIe SSD: MSI fires once at init then goes silent */
	{ PCI_VENDOR_SAMSUNG,	PCI_PRODUCT_SAMSUNG_APPLE_AHCI, -1 }
};

static int	ahci_msi_enable = 1;
int	ahci_synchronous_boot = 1;
TUNABLE_INT("hw.ahci.msi.enable", &ahci_msi_enable);
TUNABLE_INT("hw.ahci.synchronous_boot", &ahci_synchronous_boot);

/*
 * Match during probe and attach.  The device does not yet have a softc.
 */
const struct ahci_device *
ahci_lookup_device(device_t dev)
{
	const struct ahci_device *ad;
	u_int16_t vendor = pci_get_vendor(dev);
	u_int16_t product = pci_get_device(dev);
	u_int8_t class = pci_get_class(dev);
	u_int8_t subclass = pci_get_subclass(dev);
	u_int8_t progif = pci_read_config(dev, PCIR_PROGIF, 1);
	int is_ahci;

	/*
	 * Generally speaking if the pci device does not identify as
	 * AHCI we skip it.
	 */
	if (class == PCIC_STORAGE && subclass == PCIS_STORAGE_SATA &&
	    progif == PCIP_STORAGE_SATA_AHCI_1_0) {
		is_ahci = 1;
	} else {
		is_ahci = 0;
	}

	for (ad = &ahci_devices[0]; ad->ad_vendor; ++ad) {
		if (ad->ad_vendor == vendor && ad->ad_product == product)
			return (ad);
	}

	/*
	 * Last ad is the default match if the PCI device matches SATA.
	 */
	if (is_ahci == 0)
		ad = NULL;
	return (ad);
}

/*
 * Attach functions.  They all eventually fall through to ahci_pci_attach().
 */
static int
ahci_vt8251_attach(device_t dev)
{
	struct ahci_softc *sc = device_get_softc(dev);

	sc->sc_flags |= AHCI_F_NO_NCQ;
	return (ahci_pci_attach(dev));
}

static int
ahci_ati_sb600_attach(device_t dev)
{
	struct ahci_softc *sc = device_get_softc(dev);
	pcireg_t magic;
	u_int8_t subclass = pci_get_subclass(dev);
	u_int8_t revid;

	if (subclass == PCIS_STORAGE_IDE) {
		revid = pci_read_config(dev, PCIR_REVID, 1);
		magic = pci_read_config(dev, AHCI_PCI_ATI_SB600_MAGIC, 4);
		pci_write_config(dev, AHCI_PCI_ATI_SB600_MAGIC,
				 magic | AHCI_PCI_ATI_SB600_LOCKED, 4);
		pci_write_config(dev, PCIR_REVID,
				 (PCIC_STORAGE << 24) |
				 (PCIS_STORAGE_SATA << 16) |
				 (PCIP_STORAGE_SATA_AHCI_1_0 << 8) |
				 revid, 4);
		pci_write_config(dev, AHCI_PCI_ATI_SB600_MAGIC, magic, 4);
	}

	sc->sc_flags |= AHCI_F_IGN_FR;
	return (ahci_pci_attach(dev));
}

static int
ahci_nvidia_mcp_attach(device_t dev)
{
	struct ahci_softc *sc = device_get_softc(dev);

	sc->sc_flags |= AHCI_F_IGN_FR;
	return (ahci_pci_attach(dev));
}

static int
ahci_samsung_apple_attach(device_t dev)
{
	struct ahci_softc *sc = device_get_softc(dev);

	sc->sc_flags |= AHCI_F_IGN_FR | AHCI_F_IGN_CR | AHCI_F_FAST_COMRESET;
	return (ahci_pci_attach(dev));
}

static int
ahci_pci_attach(device_t dev)
{
	struct ahci_softc *sc = device_get_softc(dev);
	struct ahci_port *ap;
	const char *gen;
	uint16_t vid, did;
	u_int32_t pi, reg;
	u_int32_t cap, cap2;
	u_int32_t chip;
	u_int irq_flags;
	bus_addr_t addr;
	int i, error, msi_enable, rev, fbs;
	char revbuf[32];

	if (pci_read_config(dev, PCIR_COMMAND, 2) & 0x0400) {
		device_printf(dev, "BIOS disabled PCI interrupt, "
				   "re-enabling\n");
		pci_write_config(dev, PCIR_COMMAND,
			pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
	}

	/*
	 * Chip quirks.  Sigh.  The AHCI spec is not in the least confusing
	 * when it comes to how the FR and CR bits work, but some AHCI
	 * chipsets (aka Marvell) either don't have the bits at all or they
	 * implement them poorly.
	 */
	chip = ((uint16_t)pci_get_device(dev) << 16) |
		(uint16_t)pci_get_vendor(dev);

	switch(chip) {
	case 0x91721b4b:
		device_printf(dev,
			      "Enable 88SE9172 workarounds for broken chip\n");
		sc->sc_flags |= AHCI_F_IGN_FR;
		sc->sc_flags |= AHCI_F_IGN_CR;
		break;
	case 0x92151b4b:
		device_printf(dev,
			      "Enable 88SE9215 workarounds for broken chip\n");
		sc->sc_flags |= AHCI_F_IGN_FR;
		sc->sc_flags |= AHCI_F_IGN_CR;
		break;
	case 0x92301b4b:
		device_printf(dev,
			      "Enable 88SE9230 workarounds for broken chip\n");
		sc->sc_flags |= AHCI_F_CYCLE_FR;
		break;
	case 0x07f410de:
		device_printf(dev,
			      "Enable nForce 630i workarounds for broken chip\n");
		sc->sc_flags |= AHCI_F_IGN_FR;
		sc->sc_flags |= AHCI_F_IGN_CR;
		break;
	}

	sc->sc_dev = dev;

	/*
	 * Map the AHCI controller's IRQ and BAR(5) (hardware registers)
	 */
	msi_enable = ahci_msi_enable;

	vid = pci_get_vendor(dev);
	did = pci_get_device(dev);
	rev = pci_get_revid(dev);
	for (i = 0; i < NELEM(ahci_msi_blacklist); ++i) {
		const struct ahci_pciid *id = &ahci_msi_blacklist[i];

		if (vid == id->ahci_vid && did == id->ahci_did) {
			if (id->ahci_rev < 0 || id->ahci_rev == rev) {
				msi_enable = 0;
				break;
			}
		}
	}

	sc->sc_irq_type = pci_alloc_1intr(dev, msi_enable,
	    &sc->sc_rid_irq, &irq_flags);

	sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_rid_irq,
	    irq_flags);
	if (sc->sc_irq == NULL) {
		device_printf(dev, "unable to map interrupt\n");
		ahci_pci_detach(dev);
		return (ENXIO);
	}

	/*
	 * When mapping the register window store the tag and handle
	 * separately so we can use the tag with per-port bus handle
	 * sub-spaces.
	 */
	sc->sc_rid_regs = PCIR_BAR(5);
	sc->sc_regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
					     &sc->sc_rid_regs, RF_ACTIVE);
	if (sc->sc_regs == NULL) {
		device_printf(dev, "unable to map registers\n");
		ahci_pci_detach(dev);
		return (ENXIO);
	}
	sc->sc_iot = rman_get_bustag(sc->sc_regs);
	sc->sc_ioh = rman_get_bushandle(sc->sc_regs);

	/*
	 * Initialize the chipset and then set the interrupt vector up
	 */
	error = ahci_init(sc);
	if (error) {
		ahci_pci_detach(dev);
		return (ENXIO);
	}

	/*
	 * Get the AHCI capabilities and max number of concurrent
	 * command tags and set up the DMA tags.  Adjust the saved
	 * sc_cap according to override flags.
	 */
	cap = ahci_read(sc, AHCI_REG_CAP);
	if (sc->sc_flags & AHCI_F_NO_NCQ)
		cap &= ~AHCI_REG_CAP_SNCQ;
	if (sc->sc_flags & AHCI_F_FORCE_FBSS)
		cap |= AHCI_REG_CAP_FBSS;
	if (sc->sc_flags & AHCI_F_FORCE_SCLO)
		cap |= AHCI_REG_CAP_SCLO;
	sc->sc_cap = cap;

	/*
	 * We assume at least 4 commands.
	 */
	sc->sc_ncmds = AHCI_REG_CAP_NCS(cap);
	if (sc->sc_ncmds < 4) {
		device_printf(dev, "NCS must probe a value >= 4\n");
		ahci_pci_detach(dev);
		return (ENXIO);
	}

	addr = (cap & AHCI_REG_CAP_S64A) ?
		BUS_SPACE_MAXADDR : BUS_SPACE_MAXADDR_32BIT;

	/*
	 * DMA tags for allocation of DMA memory buffers, lists, and so
	 * forth.  These are typically per-port.
	 *
	 * When FIS-based switching is supported we need a rfis for
	 * each target (4K total).  The spec also requires 4K alignment
	 * for this case.
	 */
	fbs = (cap & AHCI_REG_CAP_FBSS) ? 16 : 1;
	error = 0;

	sc->sc_rfis_size = sizeof(struct ahci_rfis) * fbs;

	error += bus_dma_tag_create(
			NULL,				/* parent tag */
			sc->sc_rfis_size,		/* alignment */
			PAGE_SIZE,			/* boundary */
			addr,				/* loaddr? */
			BUS_SPACE_MAXADDR,		/* hiaddr */
			sc->sc_rfis_size,		/* [max]size */
			1,				/* maxsegs */
			sc->sc_rfis_size,		/* maxsegsz */
			0,				/* flags */
			&sc->sc_tag_rfis);		/* return tag */

	sc->sc_cmdlist_size = sc->sc_ncmds * sizeof(struct ahci_cmd_hdr);

	error += bus_dma_tag_create(
			NULL,				/* parent tag */
			32,				/* alignment */
			4096 * 1024,			/* boundary */
			addr,				/* loaddr? */
			BUS_SPACE_MAXADDR,		/* hiaddr */
			sc->sc_cmdlist_size,
			1,				/* maxsegs */
			sc->sc_cmdlist_size,
			0,				/* flags */
			&sc->sc_tag_cmdh);		/* return tag */

	/*
	 * NOTE: ahci_cmd_table is sized to a power of 2
	 */
	error += bus_dma_tag_create(
			NULL,				/* parent tag */
			sizeof(struct ahci_cmd_table),	/* alignment */
			4096 * 1024,			/* boundary */
			addr,				/* loaddr? */
			BUS_SPACE_MAXADDR,		/* hiaddr */
			sc->sc_ncmds * sizeof(struct ahci_cmd_table),
			1,				/* maxsegs */
			sc->sc_ncmds * sizeof(struct ahci_cmd_table),
			0,				/* flags */
			&sc->sc_tag_cmdt);		/* return tag */

	/*
	 * The data tag is used for later dmamaps and not immediately
	 * allocated.
	 */
	error += bus_dma_tag_create(
			NULL,				/* parent tag */
			4,				/* alignment */
			0,				/* boundary */
			addr,				/* loaddr? */
			BUS_SPACE_MAXADDR,		/* hiaddr */
			4096 * 1024,			/* maxiosize */
			AHCI_MAX_PRDT,			/* maxsegs */
			65536,				/* maxsegsz */
			0,				/* flags */
			&sc->sc_tag_data);		/* return tag */

	if (error) {
		device_printf(dev, "unable to create dma tags\n");
		ahci_pci_detach(dev);
		return (ENXIO);
	}

	switch (cap & AHCI_REG_CAP_ISS) {
	case AHCI_REG_CAP_ISS_G1:
		gen = "1 (1.5Gbps)";
		break;
	case AHCI_REG_CAP_ISS_G2:
		gen = "2 (3Gbps)";
		break;
	case AHCI_REG_CAP_ISS_G3:
		gen = "3 (6Gbps)";
		break;
	default:
		gen = "unknown";
		break;
	}

	/* check the revision */
	reg = ahci_read(sc, AHCI_REG_VS);

	if (reg & 0x0000FF) {
		ksnprintf(revbuf, sizeof(revbuf), "AHCI %d.%d.%d",
			  (reg >> 16), (uint8_t)(reg >> 8), (uint8_t)reg);
	} else {
		ksnprintf(revbuf, sizeof(revbuf), "AHCI %d.%d",
			  (reg >> 16), (uint8_t)(reg >> 8));
	}
	sc->sc_vers = reg;

	if (reg >= AHCI_REG_VS_1_3) {
		cap2 = ahci_read(sc, AHCI_REG_CAP2);
		device_printf(dev,
			      "%s cap 0x%pb%i cap2 0x%pb%i, %d ports, "
			      "%d tags/port, gen %s\n",
			      revbuf,
			      AHCI_FMT_CAP, cap,
			      AHCI_FMT_CAP2, cap2,
			      AHCI_REG_CAP_NP(cap), sc->sc_ncmds, gen);
	} else {
		cap2 = 0;
		device_printf(dev,
			      "%s cap 0x%pb%i, %d ports, "
			      "%d tags/port, gen %s\n",
			      revbuf,
			      AHCI_FMT_CAP, cap,
			      AHCI_REG_CAP_NP(cap), sc->sc_ncmds, gen);
	}
	sc->sc_cap2 = cap2;

	pi = ahci_read(sc, AHCI_REG_PI);
	DPRINTF(AHCI_D_VERBOSE, "%s: ports implemented: 0x%08x\n",
	    device_get_nameunit(sc->sc_dev), pi);

	sc->sc_ipm_disable = AHCI_PREG_SCTL_IPM_NOPARTIAL |
			     AHCI_PREG_SCTL_IPM_NOSLUMBER;
	if (sc->sc_cap2 & AHCI_REG_CAP2_SDS)
		sc->sc_ipm_disable |= AHCI_PREG_SCTL_IPM_NODEVSLP;

	/*
	 * Allocate per-port resources
	 *
	 * Ignore attach errors, leave the port intact for
	 * rescan and continue the loop.
	 *
	 * All ports are attached in parallel but the CAM scan-bus
	 * is held up until all ports are attached so we get a deterministic
	 * order.
	 */
	for (i = 0; error == 0 && i < AHCI_MAX_PORTS; i++) {
		if ((pi & (1 << i)) == 0) {
			/* dont allocate stuff if the port isnt implemented */
			continue;
		}
		error = ahci_port_alloc(sc, i);
	}

	/*
	 * Setup the interrupt vector and enable interrupts.  Note that
	 * since the irq may be shared we do not set it up until we are
	 * ready to go.
	 */
	if (error == 0) {
		error = bus_setup_intr(dev, sc->sc_irq, INTR_MPSAFE |
							INTR_HIFREQ,
				       ahci_intr, sc,
				       &sc->sc_irq_handle, NULL);
	}

	if (error) {
		device_printf(dev, "unable to install interrupt\n");
		ahci_pci_detach(dev);
		return (ENXIO);
	}

	/*
	 * Before marking the sc as good, which allows the interrupt
	 * subsystem to operate on the ports, wait for all the port threads
	 * to get past their initial pre-probe init.  Otherwise an interrupt
	 * may try to process the port before it has been initialized.
	 */
	for (i = 0; i < AHCI_MAX_PORTS; i++) {
		if ((ap = sc->sc_ports[i]) != NULL) {
			while (ap->ap_signal & AP_SIGF_THREAD_SYNC)
				tsleep(&ap->ap_signal, 0, "ahprb1", hz);
		}
	}

	/*
	 * Master interrupt enable, and call ahci_intr() in case we race
	 * our AHCI_F_INT_GOOD flag.
	 */
	crit_enter();
	ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE | AHCI_REG_GHC_IE);
	sc->sc_flags |= AHCI_F_INT_GOOD;
	crit_exit();
	ahci_intr(sc);

	/*
	 * Synchronously wait for some of the AHCI devices to initialize.
	 *
	 * All ports are probing in parallel.  Wait for them to finish
	 * and then issue the cam attachment and bus scan serially so
	 * the 'da' assignments are deterministic.
	 */
	for (i = 0; i < AHCI_MAX_PORTS && ahci_synchronous_boot; i++) {
		if ((ap = sc->sc_ports[i]) != NULL) {
			while (ap->ap_signal & AP_SIGF_INIT)
				tsleep(&ap->ap_signal, 0, "ahprb2", hz);
			ahci_os_lock_port(ap);
			if (ahci_cam_attach(ap) == 0) {
				ahci_cam_changed(ap, NULL, -1);
				ahci_os_unlock_port(ap);
				while ((ap->ap_flags & AP_F_SCAN_COMPLETED) == 0) {
					tsleep(&ap->ap_flags, 0, "ahprb3", hz);
				}
			} else {
				ahci_os_unlock_port(ap);
			}
		}
	}

	return(0);
}

/*
 * Device unload / detachment
 */
static int
ahci_pci_detach(device_t dev)
{
	struct ahci_softc *sc = device_get_softc(dev);
	struct ahci_port *ap;
	int	i;

	/*
	 * Disable the controller and de-register the interrupt, if any.
	 *
	 * XXX interlock last interrupt?
	 */
	sc->sc_flags &= ~AHCI_F_INT_GOOD;
	if (sc->sc_regs)
		ahci_write(sc, AHCI_REG_GHC, 0);

	if (sc->sc_irq_handle) {
		bus_teardown_intr(dev, sc->sc_irq, sc->sc_irq_handle);
		sc->sc_irq_handle = NULL;
	}

	/*
	 * Free port structures and DMA memory
	 */
	for (i = 0; i < AHCI_MAX_PORTS; i++) {
		ap = sc->sc_ports[i];
		if (ap) {
			ahci_cam_detach(ap);
			ahci_port_free(sc, i);
		}
	}

	/*
	 * Clean up the bus space
	 */
	if (sc->sc_irq) {
		bus_release_resource(dev, SYS_RES_IRQ,
				     sc->sc_rid_irq, sc->sc_irq);
		sc->sc_irq = NULL;
	}

	if (sc->sc_irq_type == PCI_INTR_TYPE_MSI)
		pci_release_msi(dev);

	if (sc->sc_regs) {
		bus_release_resource(dev, SYS_RES_MEMORY,
				     sc->sc_rid_regs, sc->sc_regs);
		sc->sc_regs = NULL;
	}

	if (sc->sc_tag_rfis) {
		bus_dma_tag_destroy(sc->sc_tag_rfis);
		sc->sc_tag_rfis = NULL;
	}
	if (sc->sc_tag_cmdh) {
		bus_dma_tag_destroy(sc->sc_tag_cmdh);
		sc->sc_tag_cmdh = NULL;
	}
	if (sc->sc_tag_cmdt) {
		bus_dma_tag_destroy(sc->sc_tag_cmdt);
		sc->sc_tag_cmdt = NULL;
	}
	if (sc->sc_tag_data) {
		bus_dma_tag_destroy(sc->sc_tag_data);
		sc->sc_tag_data = NULL;
	}

	return (0);
}