b386d07210c2ba601b1bbce1eadc85c87b961ce3
[dragonfly.git] / sys / dev / raid / mly / mly_pci.c
1 /*-
2  * Copyright (c) 2000, 2001 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  *      $FreeBSD: src/sys/dev/mly/mly_pci.c,v 1.1.2.2 2001/03/05 20:17:24 msmith Exp $
28  *      $DragonFly: src/sys/dev/raid/mly/Attic/mly_pci.c,v 1.3 2003/08/07 21:17:09 dillon Exp $
29  */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/kernel.h>
35
36 #include <sys/bus.h>
37 #include <sys/conf.h>
38 #include <sys/devicestat.h>
39 #include <sys/disk.h>
40
41 #include <machine/bus_memio.h>
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44 #include <sys/rman.h>
45
46 #include <bus/pci/pcireg.h>
47 #include <bus/pci/pcivar.h>
48
49 #include "mlyreg.h"
50 #include "mlyio.h"
51 #include "mlyvar.h"
52
53 static int      mly_pci_probe(device_t dev);
54 static int      mly_pci_attach(device_t dev);
55 static int      mly_pci_detach(device_t dev);
56 static int      mly_pci_shutdown(device_t dev);
57 static int      mly_pci_suspend(device_t dev); 
58 static int      mly_pci_resume(device_t dev);
59 static void     mly_pci_intr(void *arg);
60
61 static int      mly_sg_map(struct mly_softc *sc);
62 static void     mly_sg_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error);
63 static int      mly_mmbox_map(struct mly_softc *sc);
64 static void     mly_mmbox_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error);
65
66 static device_method_t mly_methods[] = {
67     /* Device interface */
68     DEVMETHOD(device_probe,     mly_pci_probe),
69     DEVMETHOD(device_attach,    mly_pci_attach),
70     DEVMETHOD(device_detach,    mly_pci_detach),
71     DEVMETHOD(device_shutdown,  mly_pci_shutdown),
72     DEVMETHOD(device_suspend,   mly_pci_suspend),
73     DEVMETHOD(device_resume,    mly_pci_resume),
74     { 0, 0 }
75 };
76
77 static driver_t mly_pci_driver = {
78         "mly",
79         mly_methods,
80         sizeof(struct mly_softc)
81 };
82
83 static devclass_t       mly_devclass;
84 DRIVER_MODULE(mly, pci, mly_pci_driver, mly_devclass, 0, 0);
85
86 struct mly_ident
87 {
88     u_int16_t           vendor;
89     u_int16_t           device;
90     u_int16_t           subvendor;
91     u_int16_t           subdevice;
92     int                 hwif;
93     char                *desc;
94 } mly_identifiers[] = {
95     {0x1069, 0xba56, 0x1069, 0x0040, MLY_HWIF_STRONGARM, "Mylex eXtremeRAID 2000"},
96     {0x1069, 0xba56, 0x1069, 0x0030, MLY_HWIF_STRONGARM, "Mylex eXtremeRAID 3000"},
97     {0x1069, 0x0050, 0x1069, 0x0050, MLY_HWIF_I960RX,    "Mylex AcceleRAID 352"},
98     {0x1069, 0x0050, 0x1069, 0x0052, MLY_HWIF_I960RX,    "Mylex AcceleRAID 170"},
99     {0x1069, 0x0050, 0x1069, 0x0054, MLY_HWIF_I960RX,    "Mylex AcceleRAID 160"},
100     {0, 0, 0, 0, 0, 0}
101 };
102
103 /********************************************************************************
104  ********************************************************************************
105                                                                     Bus Interface
106  ********************************************************************************
107  ********************************************************************************/
108
109 static int
110 mly_pci_probe(device_t dev)
111 {
112     struct mly_ident    *m;
113
114     debug_called(1);
115
116     for (m = mly_identifiers; m->vendor != 0; m++) {
117         if ((m->vendor == pci_get_vendor(dev)) &&
118             (m->device == pci_get_device(dev)) &&
119             ((m->subvendor == 0) || ((m->subvendor == pci_get_subvendor(dev)) &&
120                                      (m->subdevice == pci_get_subdevice(dev))))) {
121             
122             device_set_desc(dev, m->desc);
123             return(-10);        /* allow room to be overridden */
124         }
125     }
126     return(ENXIO);
127 }
128
129 static int
130 mly_pci_attach(device_t dev)
131 {
132     struct mly_softc    *sc;
133     int                 i, error;
134     u_int32_t           command;
135
136     debug_called(1);
137
138     /*
139      * Initialise softc.
140      */
141     sc = device_get_softc(dev);
142     bzero(sc, sizeof(*sc));
143     sc->mly_dev = dev;
144
145 #ifdef MLY_DEBUG
146     if (device_get_unit(sc->mly_dev) == 0)
147         mly_softc0 = sc;
148 #endif    
149
150     /* assume failure is 'not configured' */
151     error = ENXIO;
152
153     /* 
154      * Verify that the adapter is correctly set up in PCI space.
155      */
156     command = pci_read_config(sc->mly_dev, PCIR_COMMAND, 2);
157     command |= PCIM_CMD_BUSMASTEREN;
158     pci_write_config(dev, PCIR_COMMAND, command, 2);
159     command = pci_read_config(sc->mly_dev, PCIR_COMMAND, 2);
160     if (!(command & PCIM_CMD_BUSMASTEREN)) {
161         mly_printf(sc, "can't enable busmaster feature\n");
162         goto fail;
163     }
164     if ((command & PCIM_CMD_MEMEN) == 0) {
165         mly_printf(sc, "memory window not available\n");
166         goto fail;
167     }
168
169     /*
170      * Allocate the PCI register window.
171      */
172     sc->mly_regs_rid = PCIR_MAPS;       /* first base address register */
173     if ((sc->mly_regs_resource = bus_alloc_resource(sc->mly_dev, SYS_RES_MEMORY, &sc->mly_regs_rid, 
174                                                     0, ~0, 1, RF_ACTIVE)) == NULL) {
175         mly_printf(sc, "can't allocate register window\n");
176         goto fail;
177     }
178     sc->mly_btag = rman_get_bustag(sc->mly_regs_resource);
179     sc->mly_bhandle = rman_get_bushandle(sc->mly_regs_resource);
180
181     /* 
182      * Allocate and connect our interrupt.
183      */
184     sc->mly_irq_rid = 0;
185     if ((sc->mly_irq = bus_alloc_resource(sc->mly_dev, SYS_RES_IRQ, &sc->mly_irq_rid, 
186                                           0, ~0, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
187         mly_printf(sc, "can't allocate interrupt\n");
188         goto fail;
189     }
190     if (bus_setup_intr(sc->mly_dev, sc->mly_irq, INTR_TYPE_CAM,  mly_pci_intr, sc, &sc->mly_intr)) {
191         mly_printf(sc, "can't set up interrupt\n");
192         goto fail;
193     }
194
195     /* assume failure is 'out of memory' */
196     error = ENOMEM;
197
198     /*
199      * Allocate the parent bus DMA tag appropriate for our PCI interface.
200      * 
201      * Note that all of these controllers are 64-bit capable.
202      */
203     if (bus_dma_tag_create(NULL,                        /* parent */
204                            1, 0,                        /* alignment, boundary */
205                            BUS_SPACE_MAXADDR_32BIT,     /* lowaddr */
206                            BUS_SPACE_MAXADDR,           /* highaddr */
207                            NULL, NULL,                  /* filter, filterarg */
208                            MAXBSIZE, MLY_MAXSGENTRIES,  /* maxsize, nsegments */
209                            BUS_SPACE_MAXSIZE_32BIT,     /* maxsegsize */
210                            BUS_DMA_ALLOCNOW,            /* flags */
211                            &sc->mly_parent_dmat)) {
212         mly_printf(sc, "can't allocate parent DMA tag\n");
213         goto fail;
214     }
215
216     /*
217      * Create DMA tag for mapping buffers into controller-addressable space.
218      */
219     if (bus_dma_tag_create(sc->mly_parent_dmat,         /* parent */
220                            1, 0,                        /* alignment, boundary */
221                            BUS_SPACE_MAXADDR,           /* lowaddr */
222                            BUS_SPACE_MAXADDR,           /* highaddr */
223                            NULL, NULL,                  /* filter, filterarg */
224                            MAXBSIZE, MLY_MAXSGENTRIES,  /* maxsize, nsegments */
225                            BUS_SPACE_MAXSIZE_32BIT,     /* maxsegsize */
226                            0,                           /* flags */
227                            &sc->mly_buffer_dmat)) {
228         mly_printf(sc, "can't allocate buffer DMA tag\n");
229         goto fail;
230     }
231
232     /*
233      * Initialise the DMA tag for command packets.
234      */
235     if (bus_dma_tag_create(sc->mly_parent_dmat,         /* parent */
236                            1, 0,                        /* alignment, boundary */
237                            BUS_SPACE_MAXADDR,           /* lowaddr */
238                            BUS_SPACE_MAXADDR,           /* highaddr */
239                            NULL, NULL,                  /* filter, filterarg */
240                            sizeof(union mly_command_packet) * MLY_MAXCOMMANDS, 1,       /* maxsize, nsegments */
241                            BUS_SPACE_MAXSIZE_32BIT,     /* maxsegsize */
242                            0,                           /* flags */
243                            &sc->mly_packet_dmat)) {
244         mly_printf(sc, "can't allocate command packet DMA tag\n");
245         goto fail;
246     }
247
248     /* 
249      * Detect the hardware interface version 
250      */
251     for (i = 0; mly_identifiers[i].vendor != 0; i++) {
252         if ((mly_identifiers[i].vendor == pci_get_vendor(dev)) &&
253             (mly_identifiers[i].device == pci_get_device(dev))) {
254             sc->mly_hwif = mly_identifiers[i].hwif;
255             switch(sc->mly_hwif) {
256             case MLY_HWIF_I960RX:
257                 debug(2, "set hardware up for i960RX");
258                 sc->mly_doorbell_true = 0x00;
259                 sc->mly_command_mailbox =  MLY_I960RX_COMMAND_MAILBOX;
260                 sc->mly_status_mailbox =   MLY_I960RX_STATUS_MAILBOX;
261                 sc->mly_idbr =             MLY_I960RX_IDBR;
262                 sc->mly_odbr =             MLY_I960RX_ODBR;
263                 sc->mly_error_status =     MLY_I960RX_ERROR_STATUS;
264                 sc->mly_interrupt_status = MLY_I960RX_INTERRUPT_STATUS;
265                 sc->mly_interrupt_mask =   MLY_I960RX_INTERRUPT_MASK;
266                 break;
267             case MLY_HWIF_STRONGARM:
268                 debug(2, "set hardware up for StrongARM");
269                 sc->mly_doorbell_true = 0xff;           /* doorbell 'true' is 0 */
270                 sc->mly_command_mailbox =  MLY_STRONGARM_COMMAND_MAILBOX;
271                 sc->mly_status_mailbox =   MLY_STRONGARM_STATUS_MAILBOX;
272                 sc->mly_idbr =             MLY_STRONGARM_IDBR;
273                 sc->mly_odbr =             MLY_STRONGARM_ODBR;
274                 sc->mly_error_status =     MLY_STRONGARM_ERROR_STATUS;
275                 sc->mly_interrupt_status = MLY_STRONGARM_INTERRUPT_STATUS;
276                 sc->mly_interrupt_mask =   MLY_STRONGARM_INTERRUPT_MASK;
277                 break;
278             }
279             break;
280         }
281     }
282
283     /*
284      * Create the scatter/gather mappings.
285      */
286     if ((error = mly_sg_map(sc)))
287         goto fail;
288
289     /*
290      * Allocate and map the memory mailbox
291      */
292     if ((error = mly_mmbox_map(sc)))
293         goto fail;
294
295     /*
296      * Do bus-independent initialisation.
297      */
298     if ((error = mly_attach(sc)))
299         goto fail;
300     
301     return(0);
302             
303 fail:
304     mly_free(sc);
305     return(error);
306 }
307
308 /********************************************************************************
309  * Disconnect from the controller completely, in preparation for unload.
310  */
311 static int
312 mly_pci_detach(device_t dev)
313 {
314     struct mly_softc    *sc = device_get_softc(dev);
315     int                 error;
316
317     debug_called(1);
318
319     if (sc->mly_state & MLY_STATE_OPEN)
320         return(EBUSY);
321
322     if ((error = mly_pci_shutdown(dev)))
323         return(error);
324
325     mly_free(sc);
326
327     return(0);
328 }
329
330 /********************************************************************************
331  * Bring the controller down to a dormant state and detach all child devices.
332  *
333  * This function is called before detach or system shutdown.
334  *
335  * Note that we can assume that the camq on the controller is empty, as we won't
336  * allow shutdown if any device is open.
337  */
338 static int
339 mly_pci_shutdown(device_t dev)
340 {
341     struct mly_softc    *sc = device_get_softc(dev);
342
343     debug_called(1);
344
345     mly_detach(sc);
346     return(0);
347 }
348
349 /********************************************************************************
350  * Bring the controller to a quiescent state, ready for system suspend.
351  *
352  * We can't assume that the controller is not active at this point, so we need
353  * to mask interrupts.
354  */
355 static int
356 mly_pci_suspend(device_t dev)
357 {
358     struct mly_softc    *sc = device_get_softc(dev);
359     int                 s;
360
361     debug_called(1);
362     s = splcam();
363     mly_detach(sc);
364     splx(s);
365     return(0);
366 }
367
368 /********************************************************************************
369  * Bring the controller back to a state ready for operation.
370  */
371 static int
372 mly_pci_resume(device_t dev)
373 {
374     struct mly_softc    *sc = device_get_softc(dev);
375
376     debug_called(1);
377     sc->mly_state &= ~MLY_STATE_SUSPEND;
378     MLY_UNMASK_INTERRUPTS(sc);
379     return(0);
380 }
381
382 /*******************************************************************************
383  * Take an interrupt, or be poked by other code to look for interrupt-worthy
384  * status.
385  */
386 static void
387 mly_pci_intr(void *arg)
388 {
389     struct mly_softc    *sc = (struct mly_softc *)arg;
390
391     debug_called(3);
392
393     /* collect finished commands, queue anything waiting */
394     mly_done(sc);
395 };
396
397 /********************************************************************************
398  ********************************************************************************
399                                                 Bus-dependant Resource Management
400  ********************************************************************************
401  ********************************************************************************/
402
403 /********************************************************************************
404  * Allocate memory for the scatter/gather tables
405  */
406 static int
407 mly_sg_map(struct mly_softc *sc)
408 {
409     size_t      segsize;
410
411     debug_called(1);
412
413     /*
414      * Create a single tag describing a region large enough to hold all of
415      * the s/g lists we will need.
416      */
417     segsize = sizeof(struct mly_sg_entry) * MLY_MAXCOMMANDS * MLY_MAXSGENTRIES;
418     if (bus_dma_tag_create(sc->mly_parent_dmat,         /* parent */
419                            1, 0,                        /* alignment, boundary */
420                            BUS_SPACE_MAXADDR,           /* lowaddr */
421                            BUS_SPACE_MAXADDR,           /* highaddr */
422                            NULL, NULL,                  /* filter, filterarg */
423                            segsize, 1,                  /* maxsize, nsegments */
424                            BUS_SPACE_MAXSIZE_32BIT,     /* maxsegsize */
425                            0,                           /* flags */
426                            &sc->mly_sg_dmat)) {
427         mly_printf(sc, "can't allocate scatter/gather DMA tag\n");
428         return(ENOMEM);
429     }
430
431     /*
432      * Allocate enough s/g maps for all commands and permanently map them into
433      * controller-visible space.
434      *  
435      * XXX this assumes we can get enough space for all the s/g maps in one 
436      * contiguous slab.
437      */
438     if (bus_dmamem_alloc(sc->mly_sg_dmat, (void **)&sc->mly_sg_table, BUS_DMA_NOWAIT, &sc->mly_sg_dmamap)) {
439         mly_printf(sc, "can't allocate s/g table\n");
440         return(ENOMEM);
441     }
442     bus_dmamap_load(sc->mly_sg_dmat, sc->mly_sg_dmamap, sc->mly_sg_table, segsize, mly_sg_map_helper, sc, 0);
443     return(0);
444 }
445
446 /********************************************************************************
447  * Save the physical address of the base of the s/g table.
448  */
449 static void
450 mly_sg_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
451 {
452     struct mly_softc    *sc = (struct mly_softc *)arg;
453
454     debug_called(2);
455
456     /* save base of s/g table's address in bus space */
457     sc->mly_sg_busaddr = segs->ds_addr;
458 }
459
460 /********************************************************************************
461  * Allocate memory for the memory-mailbox interface
462  */
463 static int
464 mly_mmbox_map(struct mly_softc *sc)
465 {
466
467     /*
468      * Create a DMA tag for a single contiguous region large enough for the
469      * memory mailbox structure.
470      */
471     if (bus_dma_tag_create(sc->mly_parent_dmat,         /* parent */
472                            1, 0,                        /* alignment, boundary */
473                            BUS_SPACE_MAXADDR,           /* lowaddr */
474                            BUS_SPACE_MAXADDR,           /* highaddr */
475                            NULL, NULL,                  /* filter, filterarg */
476                            sizeof(struct mly_mmbox), 1, /* maxsize, nsegments */
477                            BUS_SPACE_MAXSIZE_32BIT,     /* maxsegsize */
478                            0,                           /* flags */
479                            &sc->mly_mmbox_dmat)) {
480         mly_printf(sc, "can't allocate memory mailbox DMA tag\n");
481         return(ENOMEM);
482     }
483
484     /*
485      * Allocate the buffer
486      */
487     if (bus_dmamem_alloc(sc->mly_mmbox_dmat, (void **)&sc->mly_mmbox, BUS_DMA_NOWAIT, &sc->mly_mmbox_dmamap)) {
488         mly_printf(sc, "can't allocate memory mailbox\n");
489         return(ENOMEM);
490     }
491     bus_dmamap_load(sc->mly_mmbox_dmat, sc->mly_mmbox_dmamap, sc->mly_mmbox, sizeof(struct mly_mmbox), 
492                     mly_mmbox_map_helper, sc, 0);
493     bzero(sc->mly_mmbox, sizeof(*sc->mly_mmbox));
494     return(0);
495
496 }
497
498 /********************************************************************************
499  * Save the physical address of the memory mailbox 
500  */
501 static void
502 mly_mmbox_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
503 {
504     struct mly_softc    *sc = (struct mly_softc *)arg;
505
506     debug_called(2);
507
508     sc->mly_mmbox_busaddr = segs->ds_addr;
509 }
510
511 /********************************************************************************
512  * Free all of the resources associated with (sc)
513  *
514  * Should not be called if the controller is active.
515  */
516 void
517 mly_free(struct mly_softc *sc)
518 {
519     struct mly_command  *mc;
520     
521     debug_called(1);
522
523     /* detach from CAM */
524     mly_cam_detach(sc);
525
526     /* throw away command buffer DMA maps */
527     while (mly_alloc_command(sc, &mc) == 0)
528         bus_dmamap_destroy(sc->mly_buffer_dmat, mc->mc_datamap);
529
530     /* release the packet storage */
531     if (sc->mly_packet != NULL) {
532         bus_dmamap_unload(sc->mly_packet_dmat, sc->mly_packetmap);
533         bus_dmamem_free(sc->mly_packet_dmat, sc->mly_packet, sc->mly_packetmap);
534     }
535
536     /* throw away the controllerinfo structure */
537     if (sc->mly_controllerinfo != NULL)
538         free(sc->mly_controllerinfo, M_DEVBUF);
539
540     /* throw away the controllerparam structure */
541     if (sc->mly_controllerparam != NULL)
542         free(sc->mly_controllerparam, M_DEVBUF);
543
544     /* destroy data-transfer DMA tag */
545     if (sc->mly_buffer_dmat)
546         bus_dma_tag_destroy(sc->mly_buffer_dmat);
547
548     /* free and destroy DMA memory and tag for s/g lists */
549     if (sc->mly_sg_table) {
550         bus_dmamap_unload(sc->mly_sg_dmat, sc->mly_sg_dmamap);
551         bus_dmamem_free(sc->mly_sg_dmat, sc->mly_sg_table, sc->mly_sg_dmamap);
552     }
553     if (sc->mly_sg_dmat)
554         bus_dma_tag_destroy(sc->mly_sg_dmat);
555
556     /* free and destroy DMA memory and tag for memory mailbox */
557     if (sc->mly_mmbox) {
558         bus_dmamap_unload(sc->mly_mmbox_dmat, sc->mly_mmbox_dmamap);
559         bus_dmamem_free(sc->mly_mmbox_dmat, sc->mly_mmbox, sc->mly_mmbox_dmamap);
560     }
561     if (sc->mly_mmbox_dmat)
562         bus_dma_tag_destroy(sc->mly_mmbox_dmat);
563
564     /* disconnect the interrupt handler */
565     if (sc->mly_intr)
566         bus_teardown_intr(sc->mly_dev, sc->mly_irq, sc->mly_intr);
567     if (sc->mly_irq != NULL)
568         bus_release_resource(sc->mly_dev, SYS_RES_IRQ, sc->mly_irq_rid, sc->mly_irq);
569
570     /* destroy the parent DMA tag */
571     if (sc->mly_parent_dmat)
572         bus_dma_tag_destroy(sc->mly_parent_dmat);
573
574     /* release the register window mapping */
575     if (sc->mly_regs_resource != NULL)
576         bus_release_resource(sc->mly_dev, SYS_RES_MEMORY, sc->mly_regs_rid, sc->mly_regs_resource);
577 }
578