aha_isa.c revision 157085
195584Sanholt/* 2145132Sanholt * Product specific probe and attach routines for: 3145132Sanholt * Adaptec 154x. 4139749Simp */ 595584Sanholt/*- 695584Sanholt * Copyright (c) 1999-2003 M. Warner Losh 795584Sanholt * All rights reserved. 895584Sanholt * 995584Sanholt * Redistribution and use in source and binary forms, with or without 1095584Sanholt * modification, are permitted provided that the following conditions 1195584Sanholt * are met: 1295584Sanholt * 1. Redistributions of source code must retain the above copyright 1395584Sanholt * notice, this list of conditions, and the following disclaimer, 1495584Sanholt * without modification, immediately at the beginning of the file. 1595584Sanholt * 2. The name of the author may not be used to endorse or promote products 1695584Sanholt * derived from this software without specific prior written permission. 1795584Sanholt * 1895584Sanholt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1995584Sanholt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2095584Sanholt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2195584Sanholt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 2295584Sanholt * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2395584Sanholt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2495584Sanholt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2595584Sanholt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2695584Sanholt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2795584Sanholt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2895584Sanholt * SUCH DAMAGE. 2995584Sanholt * 3095584Sanholt * Derived from bt isa from end, written by: 3195584Sanholt * 3295584Sanholt * Copyright (c) 1998 Justin T. Gibbs 3395584Sanholt * All rights reserved. 3495584Sanholt * 35152909Sanholt * Redistribution and use in source and binary forms, with or without 36152909Sanholt * modification, are permitted provided that the following conditions 37152909Sanholt * are met: 38145132Sanholt * 1. Redistributions of source code must retain the above copyright 3995584Sanholt * notice, this list of conditions, and the following disclaimer, 40145132Sanholt * without modification, immediately at the beginning of the file. 4195584Sanholt * 2. The name of the author may not be used to endorse or promote products 42145132Sanholt * derived from this software without specific prior written permission. 43145132Sanholt * 44145132Sanholt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 45145132Sanholt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4695584Sanholt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 47182080Srnoland * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 48145132Sanholt * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 49183573Srnoland * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 50183573Srnoland * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 51145132Sanholt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 52183573Srnoland * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 53145132Sanholt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 54183573Srnoland * SUCH DAMAGE. 55145132Sanholt */ 56183573Srnoland 57183573Srnoland#include <sys/cdefs.h> 58183573Srnoland__FBSDID("$FreeBSD: head/sys/dev/aha/aha_isa.c 157085 2006-03-24 06:33:25Z imp $"); 59183573Srnoland 60183573Srnoland#include <sys/param.h> 61183573Srnoland#include <sys/systm.h> 62145132Sanholt#include <sys/kernel.h> 63145132Sanholt#include <sys/lock.h> 64145132Sanholt#include <sys/mutex.h> 65145132Sanholt 66145132Sanholt#include <machine/bus.h> 67145132Sanholt#include <machine/resource.h> 68145132Sanholt#include <sys/module.h> 69145132Sanholt#include <sys/bus.h> 70145132Sanholt#include <sys/rman.h> 71145132Sanholt 72145132Sanholt#include <isa/isavar.h> 73182080Srnoland 74145132Sanholt#include <dev/aha/ahareg.h> 75182080Srnoland 76183573Srnoland#include <cam/scsi/scsi_all.h> 77183604Srnoland 78183604Srnolandstatic struct isa_pnp_id aha_ids[] = { 79183604Srnoland {ADP0100_PNP, "Adaptec 1540/1542 ISA SCSI"}, /* ADP0100 */ 80145132Sanholt {AHA1540_PNP, "Adaptec 1540/aha-1640/aha-1535"},/* ADP1540 */ 81183573Srnoland {AHA1542_PNP, "Adaptec 1542/aha-1535"}, /* ADP1542 */ 82145132Sanholt {AHA1542_PNPCOMPAT, "Adaptec 1542 compatible"}, /* PNP00A0 */ 83145132Sanholt {ICU0091_PNP, "Adaptec AHA-1540/1542 SCSI"}, /* ICU0091 */ 84145132Sanholt {0} 85183573Srnoland}; 86183573Srnoland 87183573Srnoland/* 88183573Srnoland * I/O ports listed in the order enumerated by the card for certain op codes. 89183573Srnoland */ 90183573Srnolandstatic bus_addr_t aha_board_ports[] = 91183573Srnoland{ 92183573Srnoland 0x330, 93183573Srnoland 0x334, 94183573Srnoland 0x230, 95183573Srnoland 0x234, 96183573Srnoland 0x130, 97183573Srnoland 0x134 98145132Sanholt}; 99145132Sanholt 100145132Sanholt/* 101145132Sanholt * Check if the device can be found at the port given 102183573Srnoland */ 103145132Sanholtstatic int 104145132Sanholtaha_isa_probe(device_t dev) 105145132Sanholt{ 106145132Sanholt /* 107145132Sanholt * find unit and check we have that many defined 108145132Sanholt */ 109145132Sanholt struct aha_softc *aha = device_get_softc(dev); 110182080Srnoland int error; 111145132Sanholt u_long port_start; 112145132Sanholt struct resource *port_res; 113145132Sanholt int port_rid; 114153579Sjhb int drq; 115153579Sjhb int irq; 116153579Sjhb config_data_t config_data; 117145132Sanholt 118153579Sjhb aha->dev = dev; 119145132Sanholt /* Check isapnp ids */ 120 if (ISA_PNP_PROBE(device_get_parent(dev), dev, aha_ids) == ENXIO) 121 return (ENXIO); 122 123 port_rid = 0; 124 port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &port_rid, 125 0, ~0, AHA_NREGS, RF_ACTIVE); 126 127 if (port_res == NULL) 128 return (ENXIO); 129 130 port_start = rman_get_start(port_res); 131 aha_alloc(aha, device_get_unit(dev), rman_get_bustag(port_res), 132 rman_get_bushandle(port_res)); 133 134 /* See if there is really a card present */ 135 if (aha_probe(aha) || aha_fetch_adapter_info(aha)) { 136 aha_free(aha); 137 bus_release_resource(dev, SYS_RES_IOPORT, port_rid, port_res); 138 return (ENXIO); 139 } 140 141 /* 142 * Determine our IRQ, and DMA settings and 143 * export them to the configuration system. 144 */ 145 error = aha_cmd(aha, AOP_INQUIRE_CONFIG, NULL, /*parmlen*/0, 146 (uint8_t*)&config_data, sizeof(config_data), DEFAULT_CMD_TIMEOUT); 147 148 if (error != 0) { 149 device_printf(dev, "Could not determine IRQ or DMA " 150 "settings for adapter at %#jx. Failing probe\n", 151 (uintmax_t)port_start); 152 aha_free(aha); 153 bus_release_resource(dev, SYS_RES_IOPORT, port_rid, 154 port_res); 155 return (ENXIO); 156 } 157 158 bus_release_resource(dev, SYS_RES_IOPORT, port_rid, port_res); 159 160 switch (config_data.dma_chan) { 161 case DMA_CHAN_5: 162 drq = 5; 163 break; 164 case DMA_CHAN_6: 165 drq = 6; 166 break; 167 case DMA_CHAN_7: 168 drq = 7; 169 break; 170 default: 171 device_printf(dev, "Invalid DMA setting for adapter at %#jx.", 172 (uintmax_t)port_start); 173 return (ENXIO); 174 } 175 error = bus_set_resource(dev, SYS_RES_DRQ, 0, drq, 1); 176 if (error) 177 return error; 178 179 irq = ffs(config_data.irq) + 8; 180 error = bus_set_resource(dev, SYS_RES_IRQ, 0, irq, 1); 181 return (error); 182} 183 184/* 185 * Attach all the sub-devices we can find 186 */ 187static int 188aha_isa_attach(device_t dev) 189{ 190 struct aha_softc *aha = device_get_softc(dev); 191 bus_dma_filter_t *filter; 192 void *filter_arg; 193 bus_addr_t lowaddr; 194 void *ih; 195 int error = ENOMEM; 196 int aha_free_needed = 0; 197 198 aha->dev = dev; 199 aha->portrid = 0; 200 aha->port = bus_alloc_resource(dev, SYS_RES_IOPORT, &aha->portrid, 201 0, ~0, AHA_NREGS, RF_ACTIVE); 202 if (!aha->port) { 203 device_printf(dev, "Unable to allocate I/O ports\n"); 204 goto fail; 205 } 206 207 aha->irqrid = 0; 208 aha->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &aha->irqrid, 209 RF_ACTIVE); 210 if (!aha->irq) { 211 device_printf(dev, "Unable to allocate excluse use of irq\n"); 212 goto fail; 213 } 214 215 aha->drqrid = 0; 216 aha->drq = bus_alloc_resource_any(dev, SYS_RES_DRQ, &aha->drqrid, 217 RF_ACTIVE); 218 if (!aha->drq) { 219 device_printf(dev, "Unable to allocate drq\n"); 220 goto fail; 221 } 222 223#if 0 /* is the drq ever unset? */ 224 if (dev->id_drq != -1) 225 isa_dmacascade(dev->id_drq); 226#endif 227 isa_dmacascade(rman_get_start(aha->drq)); 228 229 /* Allocate our parent dmatag */ 230 filter = NULL; 231 filter_arg = NULL; 232 lowaddr = BUS_SPACE_MAXADDR_24BIT; 233 234 if (bus_dma_tag_create( /* parent */ NULL, 235 /* alignemnt */ 1, 236 /* boundary */ 0, 237 /* lowaddr */ lowaddr, 238 /* highaddr */ BUS_SPACE_MAXADDR, 239 /* filter */ filter, 240 /* filterarg */ filter_arg, 241 /* maxsize */ BUS_SPACE_MAXSIZE_24BIT, 242 /* nsegments */ ~0, 243 /* maxsegsz */ BUS_SPACE_MAXSIZE_24BIT, 244 /* flags */ 0, 245 /* lockfunc */ busdma_lock_mutex, 246 /* lockarg */ &Giant, 247 &aha->parent_dmat) != 0) { 248 device_printf(dev, "dma tag create failed.\n"); 249 goto fail; 250 } 251 252 if (aha_init(aha)) { 253 device_printf(dev, "init failed\n"); 254 goto fail; 255 } 256 /* 257 * The 1542A and B look the same. So we guess based on 258 * the firmware revision. It appears that only rev 0 is on 259 * the A cards. 260 */ 261 if (aha->boardid <= BOARD_1542 && aha->fw_major == 0) { 262 device_printf(dev, "154xA may not work\n"); 263 aha->ccb_sg_opcode = INITIATOR_SG_CCB; 264 aha->ccb_ccb_opcode = INITIATOR_CCB; 265 } 266 aha_free_needed++; 267 268 error = aha_attach(aha); 269 if (error) { 270 device_printf(dev, "attach failed\n"); 271 goto fail; 272 } 273 274 error = bus_setup_intr(dev, aha->irq, INTR_TYPE_CAM|INTR_ENTROPY, 275 aha_intr, aha, &ih); 276 if (error) { 277 device_printf(dev, "Unable to register interrupt handler\n"); 278 goto fail; 279 } 280 281 return (0); 282fail: ; 283 bus_free_resource(dev, SYS_RES_IOPORT, aha->port); 284 bus_free_resource(dev, SYS_RES_IRQ, aha->irq); 285 bus_free_resource(dev, SYS_RES_DRQ, aha->drq); 286 if (aha_free_needed) 287 aha_free(aha); 288 return (error); 289} 290 291static int 292aha_isa_detach(device_t dev) 293{ 294 struct aha_softc *aha = (struct aha_softc *)device_get_softc(dev); 295 int error; 296 297 error = bus_teardown_intr(dev, aha->irq, aha->ih); 298 if (error) 299 device_printf(dev, "failed to unregister interrupt handler\n"); 300 301 bus_free_resource(dev, SYS_RES_IOPORT, aha->port); 302 bus_free_resource(dev, SYS_RES_IRQ, aha->irq); 303 bus_free_resource(dev, SYS_RES_DRQ, aha->drq); 304 305 error = aha_detach(aha); 306 if (error) { 307 device_printf(dev, "detach failed\n"); 308 return (error); 309 } 310 aha_free(aha); 311 312 return (0); 313} 314 315static void 316aha_isa_identify(driver_t *driver, device_t parent) 317{ 318 int i; 319 bus_addr_t ioport; 320 struct aha_softc aha; 321 int rid; 322 struct resource *res; 323 device_t child; 324 325 /* Attempt to find an adapter */ 326 for (i = 0; i < sizeof(aha_board_ports) / sizeof(aha_board_ports[0]); 327 i++) { 328 bzero(&aha, sizeof(aha)); 329 ioport = aha_board_ports[i]; 330 /* 331 * XXX Check to see if we have a hard-wired aha device at 332 * XXX this port, if so, skip. This should also cover the 333 * XXX case where we are run multiple times due to, eg, 334 * XXX kldload/kldunload. 335 */ 336 rid = 0; 337 res = bus_alloc_resource(parent, SYS_RES_IOPORT, &rid, 338 ioport, ioport, AHA_NREGS, RF_ACTIVE); 339 if (res == NULL) 340 continue; 341 aha_alloc(&aha, -1, rman_get_bustag(res), 342 rman_get_bushandle(res)); 343 /* See if there is really a card present */ 344 if (aha_probe(&aha) || aha_fetch_adapter_info(&aha)) 345 goto not_this_one; 346 child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "aha", -1); 347 bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, AHA_NREGS); 348 /* 349 * Could query the board and set IRQ/DRQ, but probe does 350 * that. 351 */ 352 not_this_one:; 353 bus_release_resource(parent, SYS_RES_IOPORT, rid, res); 354 aha_free(&aha); 355 } 356} 357 358static device_method_t aha_isa_methods[] = { 359 /* Device interface */ 360 DEVMETHOD(device_probe, aha_isa_probe), 361 DEVMETHOD(device_attach, aha_isa_attach), 362 DEVMETHOD(device_detach, aha_isa_detach), 363 DEVMETHOD(device_identify, aha_isa_identify), 364 365 { 0, 0 } 366}; 367 368static driver_t aha_isa_driver = { 369 "aha", 370 aha_isa_methods, 371 sizeof(struct aha_softc), 372}; 373 374static devclass_t aha_devclass; 375 376DRIVER_MODULE(aha, isa, aha_isa_driver, aha_devclass, 0, 0); 377MODULE_DEPEND(aha, cam, 1, 1, 1); 378