xref: /freebsd/sys/dev/ips/ips_pci.c (revision 2aedd662d8ab5bdd421bbae90f92a79b96e64e57)
12aedd662SScott Long /*-
22aedd662SScott Long  * Copyright (c) 2002 Adaptec Inc.
32aedd662SScott Long  * All rights reserved.
42aedd662SScott Long  *
52aedd662SScott Long  * Written by: David Jeffery
62aedd662SScott Long  *
72aedd662SScott Long  * Redistribution and use in source and binary forms, with or without
82aedd662SScott Long  * modification, are permitted provided that the following conditions
92aedd662SScott Long  * are met:
102aedd662SScott Long  * 1. Redistributions of source code must retain the above copyright
112aedd662SScott Long  *    notice, this list of conditions and the following disclaimer.
122aedd662SScott Long  * 2. Redistributions in binary form must reproduce the above copyright
132aedd662SScott Long  *    notice, this list of conditions and the following disclaimer in the
142aedd662SScott Long  *    documentation and/or other materials provided with the distribution.
152aedd662SScott Long  *
162aedd662SScott Long  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
172aedd662SScott Long  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
182aedd662SScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
192aedd662SScott Long  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
202aedd662SScott Long  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
212aedd662SScott Long  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
222aedd662SScott Long  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
232aedd662SScott Long  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
242aedd662SScott Long  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
252aedd662SScott Long  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
262aedd662SScott Long  * SUCH DAMAGE.
272aedd662SScott Long  *
282aedd662SScott Long  * $FreeBSD$
292aedd662SScott Long  */
302aedd662SScott Long 
312aedd662SScott Long 
322aedd662SScott Long #include <dev/ips/ips.h>
332aedd662SScott Long static int ips_pci_free(ips_softc_t *sc);
342aedd662SScott Long 
352aedd662SScott Long static int ips_pci_probe(device_t dev)
362aedd662SScott Long {
372aedd662SScott Long 
382aedd662SScott Long         if ((pci_get_vendor(dev) == IPS_VENDOR_ID) &&
392aedd662SScott Long 	    (pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID)) {
402aedd662SScott Long 		device_set_desc(dev, "IBM ServeRAID Adapter");
412aedd662SScott Long                 return 0;
422aedd662SScott Long         } else if ((pci_get_vendor(dev) == IPS_VENDOR_ID) &&
432aedd662SScott Long 	    (pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID)) {
442aedd662SScott Long 		device_set_desc(dev, "IBM ServeRAID Adapter");
452aedd662SScott Long 		return (0);
462aedd662SScott Long         }
472aedd662SScott Long         return(ENXIO);
482aedd662SScott Long }
492aedd662SScott Long 
502aedd662SScott Long static int ips_pci_attach(device_t dev)
512aedd662SScott Long {
522aedd662SScott Long         u_int32_t command;
532aedd662SScott Long         ips_softc_t *sc;
542aedd662SScott Long 
552aedd662SScott Long         DEVICE_PRINTF(1, dev, "in attach.\n");
562aedd662SScott Long         sc = (ips_softc_t *)device_get_softc(dev);
572aedd662SScott Long         if(!sc){
582aedd662SScott Long                 printf("how is sc NULL?!\n");
592aedd662SScott Long                 return (ENXIO);
602aedd662SScott Long         }
612aedd662SScott Long         bzero(sc, sizeof(ips_softc_t));
622aedd662SScott Long         sc->dev = dev;
632aedd662SScott Long 
642aedd662SScott Long         if(pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID){
652aedd662SScott Long 		sc->ips_adapter_reinit = ips_morpheus_reinit;
662aedd662SScott Long                 sc->ips_adapter_intr = ips_morpheus_intr;
672aedd662SScott Long 		sc->ips_issue_cmd    = ips_issue_morpheus_cmd;
682aedd662SScott Long         } else if(pci_get_device(dev) == IPS_COPPERHEAD_DEVICE_ID){
692aedd662SScott Long 		sc->ips_adapter_reinit = ips_copperhead_reinit;
702aedd662SScott Long                 sc->ips_adapter_intr = ips_copperhead_intr;
712aedd662SScott Long 		sc->ips_issue_cmd    = ips_issue_copperhead_cmd;
722aedd662SScott Long 	} else
732aedd662SScott Long                 goto error;
742aedd662SScott Long         /* make sure busmastering is on */
752aedd662SScott Long         command = pci_read_config(dev, PCIR_COMMAND, 1);
762aedd662SScott Long 	command |= PCIM_CMD_BUSMASTEREN;
772aedd662SScott Long 	pci_write_config(dev, PCIR_COMMAND, command, 1);
782aedd662SScott Long         /* seting up io space */
792aedd662SScott Long         sc->iores = NULL;
802aedd662SScott Long         if(command & PCIM_CMD_MEMEN){
812aedd662SScott Long                 PRINTF(10, "trying MEMIO\n");
822aedd662SScott Long 		if(pci_get_device(dev) == IPS_MORPHEUS_DEVICE_ID)
832aedd662SScott Long                 	sc->rid = PCIR_MAPS;
842aedd662SScott Long 		else
852aedd662SScott Long 			sc->rid = PCIR_MAPS + 4;
862aedd662SScott Long                 sc->iotype = SYS_RES_MEMORY;
872aedd662SScott Long                 sc->iores = bus_alloc_resource(dev, sc->iotype, &sc->rid, 0, ~0, 1, RF_ACTIVE);
882aedd662SScott Long         }
892aedd662SScott Long         if(!sc->iores && command & PCIM_CMD_PORTEN){
902aedd662SScott Long                 PRINTF(10, "trying PORTIO\n");
912aedd662SScott Long                 sc->rid = PCIR_MAPS;
922aedd662SScott Long                 sc->iotype = SYS_RES_IOPORT;
932aedd662SScott Long                 sc->iores = bus_alloc_resource(dev, sc->iotype, &sc->rid, 0, ~0, 1, RF_ACTIVE);
942aedd662SScott Long         }
952aedd662SScott Long         if(sc->iores == NULL){
962aedd662SScott Long                 device_printf(dev, "resource allocation failed\n");
972aedd662SScott Long                 return (ENXIO);
982aedd662SScott Long         }
992aedd662SScott Long         sc->bustag = rman_get_bustag(sc->iores);
1002aedd662SScott Long         sc->bushandle = rman_get_bushandle(sc->iores);
1012aedd662SScott Long         /*allocate an interrupt. when does the irq become active? after leaving attach? */
1022aedd662SScott Long         sc->irqrid = 0;
1032aedd662SScott Long         if(!(sc->irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irqrid, 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE))){
1042aedd662SScott Long                 device_printf(dev, "irq allocation failed\n");
1052aedd662SScott Long                 goto error;
1062aedd662SScott Long         }
1072aedd662SScott Long 	if(bus_setup_intr(dev, sc->irqres, INTR_TYPE_BIO, sc->ips_adapter_intr, sc, &sc->irqcookie)){
1082aedd662SScott Long                 device_printf(dev, "irq setup failed\n");
1092aedd662SScott Long                 goto error;
1102aedd662SScott Long         }
1112aedd662SScott Long 	if (bus_dma_tag_create(	/* parent    */	NULL,
1122aedd662SScott Long 				/* alignemnt */	1,
1132aedd662SScott Long 				/* boundary  */	0,
1142aedd662SScott Long 				/* lowaddr   */	BUS_SPACE_MAXADDR_32BIT,
1152aedd662SScott Long 				/* highaddr  */	BUS_SPACE_MAXADDR,
1162aedd662SScott Long 				/* filter    */	NULL,
1172aedd662SScott Long 				/* filterarg */	NULL,
1182aedd662SScott Long 				/* maxsize   */	BUS_SPACE_MAXSIZE_32BIT,
1192aedd662SScott Long 				/* numsegs   */	IPS_MAX_SG_ELEMENTS,
1202aedd662SScott Long 				/* maxsegsize*/	BUS_SPACE_MAXSIZE_32BIT,
1212aedd662SScott Long 				/* flags     */	0,
1222aedd662SScott Long 				&sc->adapter_dmatag) != 0) {
1232aedd662SScott Long                 printf("IPS can't alloc dma tag\n");
1242aedd662SScott Long                 goto error;
1252aedd662SScott Long         }
1262aedd662SScott Long 	if(ips_adapter_init(sc))
1272aedd662SScott Long 		goto error;
1282aedd662SScott Long         return 0;
1292aedd662SScott Long error:
1302aedd662SScott Long 	ips_pci_free(sc);
1312aedd662SScott Long         return (ENXIO);
1322aedd662SScott Long }
1332aedd662SScott Long 
1342aedd662SScott Long static int ips_pci_free(ips_softc_t *sc)
1352aedd662SScott Long {
1362aedd662SScott Long 	if(sc->adapter_dmatag)
1372aedd662SScott Long 		bus_dma_tag_destroy(sc->adapter_dmatag);
1382aedd662SScott Long 	if(sc->irqcookie)
1392aedd662SScott Long                 bus_teardown_intr(sc->dev, sc->irqres, sc->irqcookie);
1402aedd662SScott Long         if(sc->irqres)
1412aedd662SScott Long                bus_release_resource(sc->dev, SYS_RES_IRQ, sc->irqrid, sc->irqres);
1422aedd662SScott Long         if(sc->iores)
1432aedd662SScott Long                 bus_release_resource(sc->dev, sc->iotype, sc->rid, sc->iores);
1442aedd662SScott Long 	return 0;
1452aedd662SScott Long }
1462aedd662SScott Long 
1472aedd662SScott Long static int ips_pci_detach(device_t dev)
1482aedd662SScott Long {
1492aedd662SScott Long         ips_softc_t *sc;
1502aedd662SScott Long         DEVICE_PRINTF(1, dev, "detaching ServeRaid\n");
1512aedd662SScott Long         sc = (ips_softc_t *) device_get_softc(dev);
1522aedd662SScott Long 	ips_flush_cache(sc);
1532aedd662SScott Long 	if(ips_adapter_free(sc))
1542aedd662SScott Long 		return EBUSY;
1552aedd662SScott Long         ips_pci_free(sc);
1562aedd662SScott Long 	mtx_destroy(&sc->cmd_mtx);
1572aedd662SScott Long 	return 0;
1582aedd662SScott Long }
1592aedd662SScott Long 
1602aedd662SScott Long static int ips_pci_shutdown(device_t dev)
1612aedd662SScott Long {
1622aedd662SScott Long 	ips_softc_t *sc = (ips_softc_t *) device_get_softc(dev);
1632aedd662SScott Long 	ips_flush_cache(sc);
1642aedd662SScott Long 	return 0;
1652aedd662SScott Long }
1662aedd662SScott Long 
1672aedd662SScott Long static device_method_t ips_driver_methods[] = {
1682aedd662SScott Long         DEVMETHOD(device_probe, ips_pci_probe),
1692aedd662SScott Long         DEVMETHOD(device_attach, ips_pci_attach),
1702aedd662SScott Long         DEVMETHOD(device_detach, ips_pci_detach),
1712aedd662SScott Long 	DEVMETHOD(device_shutdown, ips_pci_shutdown),
1722aedd662SScott Long         {0,0}
1732aedd662SScott Long };
1742aedd662SScott Long 
1752aedd662SScott Long static driver_t ips_pci_driver = {
1762aedd662SScott Long         "ips",
1772aedd662SScott Long         ips_driver_methods,
1782aedd662SScott Long         sizeof(ips_softc_t),
1792aedd662SScott Long };
1802aedd662SScott Long 
1812aedd662SScott Long static devclass_t ips_devclass;
1822aedd662SScott Long DRIVER_MODULE(ips, pci, ips_pci_driver, ips_devclass, 0, 0);
183