1 /*-
2  * Product specific probe and attach routines for:
3  *      Buslogic BT946, BT948, BT956, BT958 SCSI controllers
4  *
5  * Copyright (c) 1995, 1997, 1998 Justin T. Gibbs
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification, immediately at the beginning of the file.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD: stable/9/sys/dev/buslogic/bt_pci.c 254306 2013-08-13 22:05:50Z scottl $");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/bus.h>
40 
41 #include <dev/pci/pcireg.h>
42 #include <dev/pci/pcivar.h>
43 
44 #include <machine/bus.h>
45 #include <machine/resource.h>
46 #include <sys/rman.h>
47 
48 #include <dev/buslogic/btreg.h>
49 
50 #define BT_PCI_IOADDR	PCIR_BAR(0)
51 #define BT_PCI_MEMADDR	PCIR_BAR(1)
52 
53 #define PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER	0x1040104Bul
54 #define PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER_NC	0x0140104Bul
55 #define PCI_DEVICE_ID_BUSLOGIC_FLASHPOINT	0x8130104Bul
56 
57 static int
bt_pci_alloc_resources(device_t dev)58 bt_pci_alloc_resources(device_t dev)
59 {
60 	int		type = 0, rid, zero;
61 	struct resource *regs = 0;
62 	struct resource *irq = 0;
63 
64 #if 0
65 	/* XXX Memory Mapped I/O seems to cause problems */
66 	type = SYS_RES_MEMORY;
67 	rid = BT_PCI_MEMADDR;
68 	regs = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE);
69 #else
70 	type = SYS_RES_IOPORT;
71 	rid = BT_PCI_IOADDR;
72 	regs = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE);
73 #endif
74 	if (!regs)
75 		return (ENOMEM);
76 
77 	zero = 0;
78 	irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &zero,
79 				     RF_ACTIVE | RF_SHAREABLE);
80 	if (!irq) {
81 		bus_release_resource(dev, type, rid, regs);
82 		return (ENOMEM);
83 	}
84 
85 	bt_init_softc(dev, regs, irq, 0);
86 
87 	return (0);
88 }
89 
90 static void
bt_pci_release_resources(device_t dev)91 bt_pci_release_resources(device_t dev)
92 {
93 	struct bt_softc *bt = device_get_softc(dev);
94 
95 	if (bt->port)
96 		/* XXX can't cope with memory registers anyway */
97 		bus_release_resource(dev, SYS_RES_IOPORT,
98 				     BT_PCI_IOADDR, bt->port);
99 	if (bt->irq)
100 		bus_release_resource(dev, SYS_RES_IRQ, 0, bt->irq);
101 	bt_free_softc(dev);
102 }
103 
104 static int
bt_pci_probe(device_t dev)105 bt_pci_probe(device_t dev)
106 {
107 	switch (pci_get_devid(dev)) {
108 		case PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER:
109 		case PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER_NC:
110 		{
111 			struct bt_softc   *bt = device_get_softc(dev);
112 			pci_info_data_t pci_info;
113 			int error;
114 
115 			error = bt_pci_alloc_resources(dev);
116 			if (error)
117 				return (error);
118 
119 			/*
120 			 * Determine if an ISA compatible I/O port has been
121 			 * enabled.  If so, record the port so it will not
122 			 * be probed by our ISA probe.  If the PCI I/O port
123 			 * was not set to the compatibility port, disable it.
124 			 */
125 			error = bt_cmd(bt, BOP_INQUIRE_PCI_INFO,
126 				       /*param*/NULL, /*paramlen*/0,
127 				       (u_int8_t*)&pci_info, sizeof(pci_info),
128 				       DEFAULT_CMD_TIMEOUT);
129 			if (error == 0
130 			 && pci_info.io_port < BIO_DISABLED) {
131 				bt_mark_probed_bio(pci_info.io_port);
132 				if (rman_get_start(bt->port) !=
133 				    bt_iop_from_bio(pci_info.io_port)) {
134 					u_int8_t new_addr;
135 
136 					new_addr = BIO_DISABLED;
137 					bt_cmd(bt, BOP_MODIFY_IO_ADDR,
138 					       /*param*/&new_addr,
139 					       /*paramlen*/1, /*reply_buf*/NULL,
140 					       /*reply_len*/0,
141 					       DEFAULT_CMD_TIMEOUT);
142 				}
143 			}
144 			bt_pci_release_resources(dev);
145 			device_set_desc(dev, "Buslogic Multi-Master SCSI Host Adapter");
146 			return (BUS_PROBE_DEFAULT);
147 		}
148 		default:
149 			break;
150 	}
151 
152 	return (ENXIO);
153 }
154 
155 static int
bt_pci_attach(device_t dev)156 bt_pci_attach(device_t dev)
157 {
158 	struct bt_softc   *bt = device_get_softc(dev);
159 	int		   error;
160 
161 	/* Initialize softc */
162 	error = bt_pci_alloc_resources(dev);
163 	if (error) {
164 		device_printf(dev, "can't allocate resources in bt_pci_attach\n");
165 		return error;
166 	}
167 
168 	/* Allocate a dmatag for our CCB DMA maps */
169 	if (bus_dma_tag_create(	/* PCI parent	*/ bus_get_dma_tag(dev),
170 				/* alignemnt	*/ 1,
171 				/* boundary	*/ 0,
172 				/* lowaddr	*/ BUS_SPACE_MAXADDR_32BIT,
173 				/* highaddr	*/ BUS_SPACE_MAXADDR,
174 				/* filter	*/ NULL,
175 				/* filterarg	*/ NULL,
176 				/* maxsize	*/ BUS_SPACE_MAXSIZE_32BIT,
177 				/* nsegments	*/ ~0,
178 				/* maxsegsz	*/ BUS_SPACE_MAXSIZE_32BIT,
179 				/* flags	*/ 0,
180 				/* lockfunc	*/ NULL,
181 				/* lockarg	*/ NULL,
182 				&bt->parent_dmat) != 0) {
183 		bt_pci_release_resources(dev);
184 		return (ENOMEM);
185 	}
186 
187 	if (bt_probe(dev) || bt_fetch_adapter_info(dev) || bt_init(dev)) {
188 		bt_pci_release_resources(dev);
189 		return (ENXIO);
190 	}
191 
192 	error = bt_attach(dev);
193 
194 	if (error) {
195 		bt_pci_release_resources(dev);
196 		return (error);
197 	}
198 
199 	return (0);
200 }
201 
202 static device_method_t bt_pci_methods[] = {
203 	/* Device interface */
204 	DEVMETHOD(device_probe,		bt_pci_probe),
205 	DEVMETHOD(device_attach,	bt_pci_attach),
206 
207 	{ 0, 0 }
208 };
209 
210 static driver_t bt_pci_driver = {
211 	"bt",
212 	bt_pci_methods,
213 	sizeof(struct bt_softc),
214 };
215 
216 static devclass_t bt_devclass;
217 
218 DRIVER_MODULE(bt, pci, bt_pci_driver, bt_devclass, 0, 0);
219 MODULE_DEPEND(bt, pci, 1, 1, 1);
220