4 * Copyright (c) 2006 David Gwynne <dlg@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
21 * This code is derived from software contributed to The DragonFly Project
22 * by Matthew Dillon <dillon@backplane.com>
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
28 * 1. Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in
32 * the documentation and/or other materials provided with the
34 * 3. Neither the name of The DragonFly Project nor the names of its
35 * contributors may be used to endorse or promote products derived
36 * from this software without specific, prior written permission.
38 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
41 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
42 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
43 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
44 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
46 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * $OpenBSD: sili.c,v 1.147 2009/02/16 21:19:07 miod Exp $
56 static int sili_pci_attach(device_t
);
57 static int sili_pci_detach(device_t
);
59 static const struct sili_device sili_devices
[] = {
61 .ad_vendor
= PCI_VENDOR_SII
,
62 .ad_product
= PCI_PRODUCT_SII_3132
,
64 .ad_attach
= sili_pci_attach
,
65 .ad_detach
= sili_pci_detach
,
66 .name
= "SiliconImage-3132-SATA"
69 .ad_vendor
= PCI_VENDOR_SII
,
72 .ad_attach
= sili_pci_attach
,
73 .ad_detach
= sili_pci_detach
,
74 .name
= "Rosewill-3124-SATA"
76 { 0, 0, 0, NULL
, NULL
, NULL
}
80 * Don't enable MSI by default; it does not seem to
81 * work at all on Silicon Image 3132.
83 static int sili_msi_enable
= 0;
84 TUNABLE_INT("hw.sili.msi.enable", &sili_msi_enable
);
87 * Match during probe and attach. The device does not yet have a softc.
89 const struct sili_device
*
90 sili_lookup_device(device_t dev
)
92 const struct sili_device
*ad
;
93 u_int16_t vendor
= pci_get_vendor(dev
);
94 u_int16_t product
= pci_get_device(dev
);
96 u_int8_t
class = pci_get_class(dev
);
97 u_int8_t subclass
= pci_get_subclass(dev
);
98 u_int8_t progif
= pci_read_config(dev
, PCIR_PROGIF
, 1);
101 for (ad
= &sili_devices
[0]; ad
->ad_vendor
; ++ad
) {
102 if (ad
->ad_vendor
== vendor
&& ad
->ad_product
== product
)
108 * Last ad is the default match if the PCI device matches SATA.
110 if (class == PCIC_STORAGE
&& subclass
== PCIS_STORAGE_SATA
&&
111 progif
== PCIP_STORAGE_SATA_SILI_1_0
) {
119 sili_pci_attach(device_t dev
)
121 struct sili_softc
*sc
= device_get_softc(dev
);
122 struct sili_port
*ap
;
124 u_int32_t nports
, reg
;
126 int i
, error
, msi_enable
;
130 * Map the SILI controller's IRQ, BAR(0) (global regs),
131 * and BAR(1) (port regs and lram).
134 msi_enable
= sili_msi_enable
;
135 if (!pci_is_pcie(dev
)) {
137 * Don't enable MSI on PCI devices by default;
138 * well, this may cause less trouble.
142 sc
->sc_irq_type
= pci_alloc_1intr(dev
, msi_enable
, &sc
->sc_rid_irq
,
146 sc
->sc_irq
= bus_alloc_resource_any(dev
, SYS_RES_IRQ
, &sc
->sc_rid_irq
,
148 if (sc
->sc_irq
== NULL
) {
149 device_printf(dev
, "unable to map interrupt\n");
150 sili_pci_detach(dev
);
155 * When mapping the register window store the tag and handle
156 * separately so we can use the tag with per-port bus handle
159 sc
->sc_rid_regs
= PCIR_BAR(0);
160 sc
->sc_regs
= bus_alloc_resource_any(dev
, SYS_RES_MEMORY
,
161 &sc
->sc_rid_regs
, RF_ACTIVE
);
162 if (sc
->sc_regs
== NULL
) {
163 device_printf(dev
, "unable to map registers\n");
164 sili_pci_detach(dev
);
167 sc
->sc_iot
= rman_get_bustag(sc
->sc_regs
);
168 sc
->sc_ioh
= rman_get_bushandle(sc
->sc_regs
);
170 sc
->sc_rid_pregs
= PCIR_BAR(2);
171 sc
->sc_pregs
= bus_alloc_resource_any(dev
, SYS_RES_MEMORY
,
172 &sc
->sc_rid_pregs
, RF_ACTIVE
);
173 if (sc
->sc_pregs
== NULL
) {
174 device_printf(dev
, "unable to map port registers\n");
175 sili_pci_detach(dev
);
178 sc
->sc_piot
= rman_get_bustag(sc
->sc_pregs
);
179 sc
->sc_pioh
= rman_get_bushandle(sc
->sc_pregs
);
182 * Initialize the chipset and then set the interrupt vector up
184 error
= sili_init(sc
);
186 sili_pci_detach(dev
);
191 * We assume at least 4 commands.
193 sc
->sc_ncmds
= SILI_MAX_CMDS
;
194 sc
->sc_flags
|= SILI_F_64BIT
;
195 sc
->sc_flags
|= SILI_F_NCQ
;
196 sc
->sc_flags
|= SILI_F_SSNTF
;
197 sc
->sc_flags
|= SILI_F_SPM
;
199 addr
= (sc
->sc_flags
& SILI_F_64BIT
) ?
200 BUS_SPACE_MAXADDR
: BUS_SPACE_MAXADDR_32BIT
;
203 * DMA tags for allocation of DMA memory buffers, lists, and so
204 * forth. These are typically per-port.
206 * The stuff is mostly built into the BAR mappings. We only need
207 * tags for our external SGE list and data.
210 error
+= bus_dma_tag_create(
211 NULL
, /* parent tag */
213 65536, /* boundary */
215 BUS_SPACE_MAXADDR
, /* hiaddr */
216 sizeof(struct sili_prb
) * SILI_MAX_CMDS
,
219 sizeof(struct sili_prb
) * SILI_MAX_CMDS
,
222 &sc
->sc_tag_prbs
); /* return tag */
225 * The data tag is used for later dmamaps and not immediately
228 error
+= bus_dma_tag_create(
229 NULL
, /* parent tag */
233 BUS_SPACE_MAXADDR
, /* hiaddr */
234 4096 * 1024, /* maxiosize */
235 SILI_MAX_SGET
, /* maxsegs */
236 65536, /* maxsegsz */
238 &sc
->sc_tag_data
); /* return tag */
241 device_printf(dev
, "unable to create dma tags\n");
242 sili_pci_detach(dev
);
246 if (sili_read(sc
, SILI_REG_GCTL
) & SILI_REG_GCTL_300CAP
) {
247 gen
= "1 (1.5Gbps) and 2 (3Gbps)";
248 sc
->sc_flags
|= SILI_F_300
;
253 nports
= sc
->sc_ad
->ad_nports
;
254 KKASSERT(nports
<= SILI_MAX_PORTS
);
256 device_printf(dev
, "ports=%d tags=31, gen %s, cap=NCQ,FBSS,SPM\n",
260 * Allocate per-port resources
262 * All ports are attached in parallel but the CAM scan-bus
263 * is held up until all ports are attached so we get a deterministic
266 for (i
= 0; error
== 0 && i
< nports
; i
++) {
267 error
= sili_port_alloc(sc
, i
);
271 * Setup the interrupt vector and enable interrupts. Note that
272 * since the irq may be shared we do not set it up until we are
276 error
= bus_setup_intr(dev
, sc
->sc_irq
, INTR_MPSAFE
,
278 &sc
->sc_irq_handle
, NULL
);
282 device_printf(dev
, "unable to install interrupt\n");
283 sili_pci_detach(dev
);
288 * Interrupt subsystem is good to go now, enable all port interrupts
291 reg
= sili_read(sc
, SILI_REG_GCTL
);
292 for (i
= 0; i
< nports
; ++i
)
293 reg
|= SILI_REG_GCTL_PORTEN(i
);
294 sili_write(sc
, SILI_REG_GCTL
, reg
);
295 sc
->sc_flags
|= SILI_F_INT_GOOD
;
300 * All ports are probing in parallel. Wait for them to finish
301 * and then issue the cam attachment and bus scan serially so
302 * the 'da' assignments are deterministic.
304 for (i
= 0; i
< nports
; i
++) {
305 if ((ap
= sc
->sc_ports
[i
]) != NULL
) {
306 while (ap
->ap_signal
& AP_SIGF_INIT
)
307 tsleep(&ap
->ap_signal
, 0, "ahprb1", hz
);
308 sili_os_lock_port(ap
);
309 if (sili_cam_attach(ap
) == 0) {
310 sili_cam_changed(ap
, NULL
, -1);
311 sili_os_unlock_port(ap
);
312 while ((ap
->ap_flags
& AP_F_SCAN_COMPLETED
) == 0) {
313 tsleep(&ap
->ap_flags
, 0, "ahprb2", hz
);
316 sili_os_unlock_port(ap
);
325 * Device unload / detachment
328 sili_pci_detach(device_t dev
)
330 struct sili_softc
*sc
= device_get_softc(dev
);
331 struct sili_port
*ap
;
335 * Disable the controller and de-register the interrupt, if any.
337 * XXX interlock last interrupt?
339 sc
->sc_flags
&= ~SILI_F_INT_GOOD
;
341 sili_write(sc
, SILI_REG_GCTL
, SILI_REG_GCTL_GRESET
);
343 if (sc
->sc_irq_handle
) {
344 bus_teardown_intr(dev
, sc
->sc_irq
, sc
->sc_irq_handle
);
345 sc
->sc_irq_handle
= NULL
;
349 * Free port structures and DMA memory
351 for (i
= 0; i
< SILI_MAX_PORTS
; i
++) {
352 ap
= sc
->sc_ports
[i
];
355 sili_port_free(sc
, i
);
360 * Clean up the bus space
363 bus_release_resource(dev
, SYS_RES_IRQ
,
364 sc
->sc_rid_irq
, sc
->sc_irq
);
367 if (sc
->sc_irq_type
== PCI_INTR_TYPE_MSI
)
368 pci_release_msi(dev
);
371 bus_release_resource(dev
, SYS_RES_MEMORY
,
372 sc
->sc_rid_regs
, sc
->sc_regs
);
376 bus_release_resource(dev
, SYS_RES_MEMORY
,
377 sc
->sc_rid_pregs
, sc
->sc_pregs
);
381 if (sc
->sc_tag_prbs
) {
382 bus_dma_tag_destroy(sc
->sc_tag_prbs
);
383 sc
->sc_tag_prbs
= NULL
;
385 if (sc
->sc_tag_data
) {
386 bus_dma_tag_destroy(sc
->sc_tag_data
);
387 sc
->sc_tag_data
= NULL
;