AHCI - add new kenv "hint.ahci.nofeatures"
[dragonfly.git] / sys / dev / disk / ahci / ahci_attach.c
1 /*
2  * Copyright (c) 2006 David Gwynne <dlg@openbsd.org>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  *
16  *
17  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
18  *
19  * This code is derived from software contributed to The DragonFly Project
20  * by Matthew Dillon <dillon@backplane.com>
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  *
26  * 1. Redistributions of source code must retain the above copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in
30  *    the documentation and/or other materials provided with the
31  *    distribution.
32  * 3. Neither the name of The DragonFly Project nor the names of its
33  *    contributors may be used to endorse or promote products derived
34  *    from this software without specific, prior written permission.
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
37  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
39  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
40  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
41  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
42  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
44  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  * $OpenBSD: ahci.c,v 1.147 2009/02/16 21:19:07 miod Exp $
50  */
51
52 #include "ahci.h"
53
54 static int      ahci_vt8251_attach(device_t);
55 static int      ahci_ati_sb600_attach(device_t);
56 static int      ahci_nvidia_mcp_attach(device_t);
57 static int      ahci_pci_attach(device_t);
58 static int      ahci_pci_detach(device_t);
59
60 static const struct ahci_device ahci_devices[] = {
61         { PCI_VENDOR_VIATECH,   PCI_PRODUCT_VIATECH_VT8251_SATA,
62             ahci_vt8251_attach, ahci_pci_detach, "ViaTech-VT8251-SATA" },
63         { PCI_VENDOR_ATI,       PCI_PRODUCT_ATI_SB600_SATA,
64             ahci_ati_sb600_attach, ahci_pci_detach, "ATI-SB600-SATA" },
65         { PCI_VENDOR_NVIDIA,    PCI_PRODUCT_NVIDIA_MCP65_AHCI_2,
66             ahci_nvidia_mcp_attach, ahci_pci_detach, "NVidia-MCP65-SATA" },
67         { PCI_VENDOR_NVIDIA,    PCI_PRODUCT_NVIDIA_MCP67_AHCI_1,
68             ahci_nvidia_mcp_attach, ahci_pci_detach, "NVidia-MCP67-SATA" },
69         { PCI_VENDOR_NVIDIA,    PCI_PRODUCT_NVIDIA_MCP77_AHCI_5,
70             ahci_nvidia_mcp_attach, ahci_pci_detach, "NVidia-MCP77-SATA" },
71         { 0, 0,
72             ahci_pci_attach, ahci_pci_detach, "AHCI-PCI-SATA" }
73 };
74
75 /*
76  * Match during probe and attach.  The device does not yet have a softc.
77  */
78 const struct ahci_device *
79 ahci_lookup_device(device_t dev)
80 {
81         const struct ahci_device *ad;
82         u_int16_t vendor = pci_get_vendor(dev);
83         u_int16_t product = pci_get_device(dev);
84         u_int8_t class = pci_get_class(dev);
85         u_int8_t subclass = pci_get_subclass(dev);
86         u_int8_t progif = pci_read_config(dev, PCIR_PROGIF, 1);
87
88         for (ad = &ahci_devices[0]; ad->ad_vendor; ++ad) {
89                 if (ad->ad_vendor == vendor && ad->ad_product == product)
90                         return (ad);
91         }
92
93         /*
94          * Last ad is the default match if the PCI device matches SATA.
95          */
96         if (class == PCIC_STORAGE && subclass == PCIS_STORAGE_SATA &&
97             progif == PCIP_STORAGE_SATA_AHCI_1_0) {
98                 return (ad);
99         }
100
101         return (NULL);
102 }
103
104 /*
105  * Attach functions.  They all eventually fall through to ahci_pci_attach().
106  */
107 static int
108 ahci_vt8251_attach(device_t dev)
109 {
110         struct ahci_softc *sc = device_get_softc(dev);
111
112         sc->sc_flags |= AHCI_F_NO_NCQ;
113         return (ahci_pci_attach(dev));
114 }
115
116 static int
117 ahci_ati_sb600_attach(device_t dev)
118 {
119         struct ahci_softc *sc = device_get_softc(dev);
120         pcireg_t magic;
121         u_int8_t subclass = pci_get_subclass(dev);
122         u_int8_t revid;
123
124         if (subclass == PCIS_STORAGE_IDE) {
125                 revid = pci_read_config(dev, PCIR_REVID, 1);
126                 magic = pci_read_config(dev, AHCI_PCI_ATI_SB600_MAGIC, 4);
127                 pci_write_config(dev, AHCI_PCI_ATI_SB600_MAGIC,
128                                  magic | AHCI_PCI_ATI_SB600_LOCKED, 4);
129                 pci_write_config(dev, PCIR_REVID,
130                                  (PCIC_STORAGE << 24) |
131                                  (PCIS_STORAGE_SATA << 16) |
132                                  (PCIP_STORAGE_SATA_AHCI_1_0 << 8) |
133                                  revid, 4);
134                 pci_write_config(dev, AHCI_PCI_ATI_SB600_MAGIC, magic, 4);
135         }
136
137         sc->sc_flags |= AHCI_F_IGN_FR;
138         return (ahci_pci_attach(dev));
139 }
140
141 static int
142 ahci_nvidia_mcp_attach(device_t dev)
143 {
144         struct ahci_softc *sc = device_get_softc(dev);
145
146         sc->sc_flags |= AHCI_F_IGN_FR;
147         return (ahci_pci_attach(dev));
148 }
149
150 static int
151 ahci_pci_attach(device_t dev)
152 {
153         struct ahci_softc *sc = device_get_softc(dev);
154         struct ahci_port *ap;
155         const char *gen;
156         u_int32_t cap, pi, reg;
157         bus_addr_t addr;
158         int i;
159         int error;
160         const char *revision;
161
162         /*
163          * Map the AHCI controller's IRQ and BAR(5) (hardware registers)
164          */
165         sc->sc_dev = dev;
166         sc->sc_rid_irq = AHCI_IRQ_RID;
167         sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_rid_irq,
168                                             RF_SHAREABLE | RF_ACTIVE);
169         if (sc->sc_irq == NULL) {
170                 device_printf(dev, "unable to map interrupt\n");
171                 ahci_pci_detach(dev);
172                 return (ENXIO);
173         }
174
175         /*
176          * When mapping the register window store the tag and handle
177          * separately so we can use the tag with per-port bus handle
178          * sub-spaces.
179          */
180         sc->sc_rid_regs = PCIR_BAR(5);
181         sc->sc_regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
182                                              &sc->sc_rid_regs, RF_ACTIVE);
183         if (sc->sc_regs == NULL) {
184                 device_printf(dev, "unable to map registers\n");
185                 ahci_pci_detach(dev);
186                 return (ENXIO);
187         }
188         sc->sc_iot = rman_get_bustag(sc->sc_regs);
189         sc->sc_ioh = rman_get_bushandle(sc->sc_regs);
190
191         /*
192          * Initialize the chipset and then set the interrupt vector up
193          */
194         error = ahci_init(sc);
195         if (error) {
196                 ahci_pci_detach(dev);
197                 return (ENXIO);
198         }
199
200         /*
201          * Get the AHCI capabilities and max number of concurrent
202          * command tags and set up the DMA tags.
203          */
204         cap = ahci_read(sc, AHCI_REG_CAP);
205         if (sc->sc_flags & AHCI_F_NO_NCQ)
206                 cap &= ~AHCI_REG_CAP_SNCQ;
207         sc->sc_cap = cap;
208
209         /*
210          * We assume at least 4 commands.
211          */
212         sc->sc_ncmds = AHCI_REG_CAP_NCS(cap);
213         if (sc->sc_ncmds < 4) {
214                 device_printf(dev, "NCS must probe a value >= 4\n");
215                 ahci_pci_detach(dev);
216                 return (ENXIO);
217         }
218
219         addr = (cap & AHCI_REG_CAP_S64A) ?
220                 BUS_SPACE_MAXADDR : BUS_SPACE_MAXADDR_32BIT;
221
222         /*
223          * DMA tags for allocation of DMA memory buffers, lists, and so
224          * forth.  These are typically per-port.
225          */
226         error = 0;
227         error += bus_dma_tag_create(
228                         NULL,                           /* parent tag */
229                         256,                            /* alignment */
230                         PAGE_SIZE,                      /* boundary */
231                         addr,                           /* loaddr? */
232                         BUS_SPACE_MAXADDR,              /* hiaddr */
233                         NULL,                           /* filter */
234                         NULL,                           /* filterarg */
235                         sizeof(struct ahci_rfis),       /* [max]size */
236                         1,                              /* maxsegs */
237                         sizeof(struct ahci_rfis),       /* maxsegsz */
238                         0,                              /* flags */
239                         &sc->sc_tag_rfis);              /* return tag */
240
241         error += bus_dma_tag_create(
242                         NULL,                           /* parent tag */
243                         32,                             /* alignment */
244                         4096 * 1024,                    /* boundary */
245                         addr,                           /* loaddr? */
246                         BUS_SPACE_MAXADDR,              /* hiaddr */
247                         NULL,                           /* filter */
248                         NULL,                           /* filterarg */
249                         sc->sc_ncmds * sizeof(struct ahci_cmd_hdr),
250                         1,                              /* maxsegs */
251                         sc->sc_ncmds * sizeof(struct ahci_cmd_hdr),
252                         0,                              /* flags */
253                         &sc->sc_tag_cmdh);              /* return tag */
254
255         /*
256          * NOTE: ahci_cmd_table is sized to a power of 2
257          */
258         error += bus_dma_tag_create(
259                         NULL,                           /* parent tag */
260                         sizeof(struct ahci_cmd_table),  /* alignment */
261                         4096 * 1024,                    /* boundary */
262                         addr,                           /* loaddr? */
263                         BUS_SPACE_MAXADDR,              /* hiaddr */
264                         NULL,                           /* filter */
265                         NULL,                           /* filterarg */
266                         sc->sc_ncmds * sizeof(struct ahci_cmd_table),
267                         1,                              /* maxsegs */
268                         sc->sc_ncmds * sizeof(struct ahci_cmd_table),
269                         0,                              /* flags */
270                         &sc->sc_tag_cmdt);              /* return tag */
271
272         /*
273          * The data tag is used for later dmamaps and not immediately
274          * allocated.
275          */
276         error += bus_dma_tag_create(
277                         NULL,                           /* parent tag */
278                         4,                              /* alignment */
279                         0,                              /* boundary */
280                         addr,                           /* loaddr? */
281                         BUS_SPACE_MAXADDR,              /* hiaddr */
282                         NULL,                           /* filter */
283                         NULL,                           /* filterarg */
284                         4096 * 1024,                    /* maxiosize */
285                         AHCI_MAX_PRDT,                  /* maxsegs */
286                         65536,                          /* maxsegsz */
287                         0,                              /* flags */
288                         &sc->sc_tag_data);              /* return tag */
289
290         if (error) {
291                 device_printf(dev, "unable to create dma tags\n");
292                 ahci_pci_detach(dev);
293                 return (ENXIO);
294         }
295
296         switch (cap & AHCI_REG_CAP_ISS) {
297         case AHCI_REG_CAP_ISS_G1:
298                 gen = "1 (1.5Gbps)";
299                 break;
300         case AHCI_REG_CAP_ISS_G1_2:
301                 gen = "1 (1.5Gbps) and 2 (3Gbps)";
302                 break;
303         default:
304                 gen = "unknown";
305                 break;
306         }
307
308         /* check the revision */
309         reg = ahci_read(sc, AHCI_REG_VS);
310         switch (reg) {
311         case AHCI_REG_VS_0_95:
312                 revision = "AHCI 0.95";
313                 break;
314         case AHCI_REG_VS_1_0:
315                 revision = "AHCI 1.0";
316                 break;
317         case AHCI_REG_VS_1_1:
318                 revision = "AHCI 1.1";
319                 break;
320         case AHCI_REG_VS_1_2:
321                 revision = "AHCI 1.2";
322                 break;
323         default:
324                 device_printf(sc->sc_dev,
325                               "Warning: Unknown AHCI revision 0x%08x\n", reg);
326                 revision = "AHCI <unknown>";
327                 break;
328         }
329
330         device_printf(dev,
331                       "%s capabilities 0x%b, %d ports, %d tags/port, gen %s\n",
332                       revision,
333                       cap, AHCI_FMT_CAP,
334                       AHCI_REG_CAP_NP(cap), sc->sc_ncmds, gen);
335
336         pi = ahci_read(sc, AHCI_REG_PI);
337         DPRINTF(AHCI_D_VERBOSE, "%s: ports implemented: 0x%08x\n",
338             DEVNAME(sc), pi);
339
340 #ifdef AHCI_COALESCE
341         /* Naive coalescing support - enable for all ports. */
342         if (cap & AHCI_REG_CAP_CCCS) {
343                 u_int16_t               ccc_timeout = 20;
344                 u_int8_t                ccc_numcomplete = 12;
345                 u_int32_t               ccc_ctl;
346
347                 /* disable coalescing during reconfiguration. */
348                 ccc_ctl = ahci_read(sc, AHCI_REG_CCC_CTL);
349                 ccc_ctl &= ~0x00000001;
350                 ahci_write(sc, AHCI_REG_CCC_CTL, ccc_ctl);
351
352                 sc->sc_ccc_mask = 1 << AHCI_REG_CCC_CTL_INT(ccc_ctl);
353                 if (pi & sc->sc_ccc_mask) {
354                         /* A conflict with the implemented port list? */
355                         printf("%s: coalescing interrupt/implemented port list "
356                             "conflict, PI: %08x, ccc_mask: %08x\n",
357                             DEVNAME(sc), pi, sc->sc_ccc_mask);
358                         sc->sc_ccc_mask = 0;
359                         goto noccc;
360                 }
361
362                 /* ahci_port_start will enable each port when it starts. */
363                 sc->sc_ccc_ports = pi;
364                 sc->sc_ccc_ports_cur = 0;
365
366                 /* program thresholds and enable overall coalescing. */
367                 ccc_ctl &= ~0xffffff00;
368                 ccc_ctl |= (ccc_timeout << 16) | (ccc_numcomplete << 8);
369                 ahci_write(sc, AHCI_REG_CCC_CTL, ccc_ctl);
370                 ahci_write(sc, AHCI_REG_CCC_PORTS, 0);
371                 ahci_write(sc, AHCI_REG_CCC_CTL, ccc_ctl | 1);
372         }
373 noccc:
374 #endif
375         /*
376          * Allocate per-port resources
377          *
378          * Ignore attach errors, leave the port intact for
379          * rescan and continue the loop.
380          *
381          * All ports are attached in parallel but the CAM scan-bus
382          * is held up until all ports are attached so we get a deterministic
383          * order.
384          */
385         for (i = 0; error == 0 && i < AHCI_MAX_PORTS; i++) {
386                 if ((pi & (1 << i)) == 0) {
387                         /* dont allocate stuff if the port isnt implemented */
388                         continue;
389                 }
390                 error = ahci_port_alloc(sc, i);
391         }
392
393         /*
394          * Setup the interrupt vector and enable interrupts.  Note that
395          * since the irq may be shared we do not set it up until we are
396          * ready to go.
397          */
398         if (error == 0) {
399                 error = bus_setup_intr(dev, sc->sc_irq, 0, ahci_intr, sc,
400                                        &sc->sc_irq_handle, NULL);
401         }
402
403         if (error) {
404                 device_printf(dev, "unable to install interrupt\n");
405                 ahci_pci_detach(dev);
406                 return (ENXIO);
407         }
408
409         /*
410          * Master interrupt enable, and call ahci_intr() in case we race
411          * our AHCI_F_INT_GOOD flag.
412          */
413         crit_enter();
414         ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE | AHCI_REG_GHC_IE);
415         sc->sc_flags |= AHCI_F_INT_GOOD;
416         crit_exit();
417         ahci_intr(sc);
418
419         /*
420          * All ports are probing in parallel.  Wait for them to finish
421          * and then issue the cam attachment and bus scan serially so
422          * the 'da' assignments are deterministic.
423          */
424         for (i = 0; i < AHCI_MAX_PORTS; i++) {
425                 if ((ap = sc->sc_ports[i]) != NULL) {
426                         while (ap->ap_signal & AP_SIGF_INIT)
427                                 tsleep(&ap->ap_signal, 0, "ahprb1", hz);
428                         ahci_os_lock_port(ap);
429                         if (ahci_cam_attach(ap) == 0) {
430                                 ahci_cam_changed(ap, NULL, -1);
431                                 ahci_os_unlock_port(ap);
432                                 while ((ap->ap_flags & AP_F_SCAN_COMPLETED) == 0) {
433                                         tsleep(&ap->ap_flags, 0, "ahprb2", hz);
434                                 }
435                         } else {
436                                 ahci_os_unlock_port(ap);
437                         }
438                 }
439         }
440
441         return(0);
442 }
443
444 /*
445  * Device unload / detachment
446  */
447 static int
448 ahci_pci_detach(device_t dev)
449 {
450         struct ahci_softc *sc = device_get_softc(dev);
451         struct ahci_port *ap;
452         int     i;
453
454         /*
455          * Disable the controller and de-register the interrupt, if any.
456          *
457          * XXX interlock last interrupt?
458          */
459         sc->sc_flags &= ~AHCI_F_INT_GOOD;
460         if (sc->sc_regs)
461                 ahci_write(sc, AHCI_REG_GHC, 0);
462
463         if (sc->sc_irq_handle) {
464                 bus_teardown_intr(dev, sc->sc_irq, sc->sc_irq_handle);
465                 sc->sc_irq_handle = NULL;
466         }
467
468         /*
469          * Free port structures and DMA memory
470          */
471         for (i = 0; i < AHCI_MAX_PORTS; i++) {
472                 ap = sc->sc_ports[i];
473                 if (ap) {
474                         ahci_cam_detach(ap);
475                         ahci_port_free(sc, i);
476                 }
477         }
478
479         /*
480          * Clean up the bus space
481          */
482         if (sc->sc_irq) {
483                 bus_release_resource(dev, SYS_RES_IRQ,
484                                      sc->sc_rid_irq, sc->sc_irq);
485                 sc->sc_irq = NULL;
486         }
487         if (sc->sc_regs) {
488                 bus_release_resource(dev, SYS_RES_MEMORY,
489                                      sc->sc_rid_regs, sc->sc_regs);
490                 sc->sc_regs = NULL;
491         }
492
493         if (sc->sc_tag_rfis) {
494                 bus_dma_tag_destroy(sc->sc_tag_rfis);
495                 sc->sc_tag_rfis = NULL;
496         }
497         if (sc->sc_tag_cmdh) {
498                 bus_dma_tag_destroy(sc->sc_tag_cmdh);
499                 sc->sc_tag_cmdh = NULL;
500         }
501         if (sc->sc_tag_cmdt) {
502                 bus_dma_tag_destroy(sc->sc_tag_cmdt);
503                 sc->sc_tag_cmdt = NULL;
504         }
505         if (sc->sc_tag_data) {
506                 bus_dma_tag_destroy(sc->sc_tag_data);
507                 sc->sc_tag_data = NULL;
508         }
509
510         return (0);
511 }